mirror of
https://github.com/duckietm/Nitro-V3.git
synced 2026-06-19 15:06:20 +00:00
checkpoint: secure assets and login flow baseline
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
import { encodeBytes } from './asset-codec.mjs';
|
||||
import { copyFileSync, existsSync, mkdirSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
import { gzipSync } from 'zlib';
|
||||
|
||||
const dist = 'dist';
|
||||
const buildVersion = Date.now().toString(36);
|
||||
|
||||
const walk = dir =>
|
||||
{
|
||||
const out = [];
|
||||
|
||||
for(const entry of readdirSync(dir))
|
||||
{
|
||||
const path = join(dir, entry);
|
||||
const stat = statSync(path);
|
||||
|
||||
if(stat.isDirectory()) out.push(...walk(path));
|
||||
else out.push(path);
|
||||
}
|
||||
|
||||
return out;
|
||||
};
|
||||
|
||||
const minifyJson = path =>
|
||||
{
|
||||
try
|
||||
{
|
||||
writeFileSync(path, JSON.stringify(JSON.parse(readFileSync(path, 'utf8'))));
|
||||
}
|
||||
catch {}
|
||||
};
|
||||
|
||||
const encryptFile = path =>
|
||||
{
|
||||
const bytes = gzipSync(readFileSync(path), { level: 9 });
|
||||
writeFileSync(path + '.dat', encodeBytes(bytes));
|
||||
rmSync(path);
|
||||
};
|
||||
|
||||
if(!existsSync(dist)) throw new Error('dist folder not found');
|
||||
|
||||
for(const file of walk(dist))
|
||||
{
|
||||
if(file.endsWith('.json')) minifyJson(file);
|
||||
}
|
||||
|
||||
for(const file of [ 'renderer-config.json', 'ui-config.json' ])
|
||||
{
|
||||
const target = join(dist, file);
|
||||
if(existsSync(target)) rmSync(target);
|
||||
}
|
||||
|
||||
for(const file of walk(dist))
|
||||
{
|
||||
if(file.endsWith('.js') && !file.endsWith('asset-loader.js')) encryptFile(file);
|
||||
if(file.endsWith('.css')) encryptFile(file);
|
||||
}
|
||||
|
||||
const assetMirrorDir = join(dist, 'src', 'assets');
|
||||
mkdirSync(assetMirrorDir, { recursive: true });
|
||||
|
||||
for(const file of [ 'app.css.dat', 'app.js.dat' ])
|
||||
{
|
||||
const source = join(dist, 'assets', file);
|
||||
const target = join(assetMirrorDir, file);
|
||||
|
||||
if(existsSync(source)) copyFileSync(source, target);
|
||||
}
|
||||
|
||||
const publicLoaderAssets = [
|
||||
[ 'src/assets/images/loading/loading.gif', 'loading.gif' ],
|
||||
[ 'src/assets/images/notifications/nitro_v3.png', 'nitro_v3.png' ]
|
||||
];
|
||||
|
||||
for(const [ source, file ] of publicLoaderAssets)
|
||||
{
|
||||
const target = join(dist, 'assets', file);
|
||||
const mirrorTarget = join(assetMirrorDir, file);
|
||||
|
||||
if(existsSync(source))
|
||||
{
|
||||
copyFileSync(source, target);
|
||||
copyFileSync(source, mirrorTarget);
|
||||
}
|
||||
}
|
||||
|
||||
writeFileSync(join(dist, 'index.html'), `<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"></head><body><div id="root"></div><script src="asset-loader.js?v=${ buildVersion }"></script></body></html>`);
|
||||
Reference in New Issue
Block a user