@nimiplatform/app-tools 0.16.0-rc.13 → 0.16.0-rc.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/nimi-app.mjs +1 -1
- package/lib/app-dependency-combinations.mjs +1 -0
- package/lib/app-pack.mjs +1063 -177
- package/lib/app-scaffold-profiles.mjs +119 -42
- package/lib/hosted-web-release.mjs +31 -5
- package/lib/index.mjs +3 -3
- package/lib/payload-links.mjs +96 -18
- package/package.json +5 -5
|
@@ -192,7 +192,7 @@ const DEFAULT_ELECTRON_PRELOAD_BUNDLER = [
|
|
|
192
192
|
"const appRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');",
|
|
193
193
|
'prepareElectronIcons(appRoot);',
|
|
194
194
|
'',
|
|
195
|
-
'await build({',
|
|
195
|
+
'const result = await build({',
|
|
196
196
|
" entryPoints: [path.join(appRoot, 'src-electron/preload.cts')],",
|
|
197
197
|
" outfile: path.join(appRoot, 'dist-electron/preload.cjs'),",
|
|
198
198
|
' bundle: true,',
|
|
@@ -200,8 +200,16 @@ const DEFAULT_ELECTRON_PRELOAD_BUNDLER = [
|
|
|
200
200
|
" target: 'node22',",
|
|
201
201
|
" format: 'cjs',",
|
|
202
202
|
" external: ['electron'],",
|
|
203
|
+
' metafile: true,',
|
|
203
204
|
" logLevel: 'silent',",
|
|
204
205
|
'});',
|
|
206
|
+
"for (const output of Object.values(result.metafile?.outputs || {})) {",
|
|
207
|
+
' for (const imp of output.imports || []) {',
|
|
208
|
+
" if (imp.external && imp.path.startsWith('@nimiplatform/')) {",
|
|
209
|
+
' throw new Error(`Electron preload bundle must not retain external Nimi import: ${imp.path}`);',
|
|
210
|
+
' }',
|
|
211
|
+
' }',
|
|
212
|
+
'}',
|
|
205
213
|
'',
|
|
206
214
|
].join('\n');
|
|
207
215
|
|
|
@@ -218,19 +226,17 @@ const DEFAULT_ELECTRON_PRODUCTION_CLEANER = [
|
|
|
218
226
|
function renderDefaultElectronProductionPackager(identity) {
|
|
219
227
|
return [
|
|
220
228
|
"import { spawn } from 'node:child_process';",
|
|
221
|
-
"import { copyFile, cp, mkdir, mkdtemp, readFile, realpath, rm, writeFile } from 'node:fs/promises';",
|
|
229
|
+
"import { copyFile, cp, mkdir, mkdtemp, open, readdir, readFile, realpath, rm, writeFile } from 'node:fs/promises';",
|
|
222
230
|
"import { createRequire } from 'node:module';",
|
|
223
231
|
"import path from 'node:path';",
|
|
224
232
|
"import { fileURLToPath } from 'node:url';",
|
|
225
233
|
"import { packager } from '@electron/packager';",
|
|
226
234
|
"import { prepareElectronIcons } from './app-icons.mjs';",
|
|
227
235
|
"import { build } from 'esbuild';",
|
|
228
|
-
"import { parse as parseYaml, stringify as stringifyYaml } from 'yaml';",
|
|
229
236
|
'',
|
|
230
237
|
`const APP_EXECUTABLE_NAME = ${JSON.stringify(identity.cargoPackageName)};`,
|
|
231
238
|
`const APP_PRODUCT_NAME = ${JSON.stringify(identity.appTitle)};`,
|
|
232
239
|
resolveWindowsResourceVersion.toString(),
|
|
233
|
-
rebaseLocalPackagePaths.toString(),
|
|
234
240
|
"const MACOS_BUILD = process.platform === 'darwin' && process.arch === 'arm64';",
|
|
235
241
|
"const NATIVE_PLATFORM = MACOS_BUILD ? 'darwin' : 'win32';",
|
|
236
242
|
"const NATIVE_ARCH = MACOS_BUILD ? 'arm64' : 'x64';",
|
|
@@ -274,54 +280,66 @@ function renderDefaultElectronProductionPackager(identity) {
|
|
|
274
280
|
'',
|
|
275
281
|
'try {',
|
|
276
282
|
' const icons = prepareElectronIcons(appRoot, { required: true });',
|
|
283
|
+
" const mainBundlePath = path.join(appRoot, 'dist-electron/main.js');",
|
|
277
284
|
' await build({',
|
|
278
285
|
" entryPoints: [path.join(appRoot, 'src-electron/main.ts')],",
|
|
279
|
-
|
|
286
|
+
' outfile: mainBundlePath,',
|
|
280
287
|
' bundle: true,',
|
|
281
288
|
" platform: 'node',",
|
|
282
289
|
" target: 'node22',",
|
|
283
290
|
" format: 'esm',",
|
|
284
|
-
"
|
|
285
|
-
|
|
291
|
+
" external: ['electron', 'original-fs', 'sharp', '@nimiplatform/kit-protected-local-win32-x64', '@nimiplatform/kit-protected-local-darwin-arm64'],",
|
|
292
|
+
' banner: {',
|
|
293
|
+
' js: "import { createRequire as __nimiCreateRequire } from \'node:module\'; const require = __nimiCreateRequire(import.meta.url);",',
|
|
294
|
+
' },',
|
|
286
295
|
" define: { __NIMI_ELECTRON_PRODUCTION__: 'true' },",
|
|
287
296
|
" logLevel: 'silent',",
|
|
288
297
|
' });',
|
|
298
|
+
' await verifyNodeSyntax(mainBundlePath);',
|
|
289
299
|
'',
|
|
290
300
|
" await realpath(path.join(appRoot, 'dist', 'index.html'));",
|
|
291
301
|
" await realpath(path.join(appRoot, 'dist-electron', 'preload.cjs'));",
|
|
302
|
+
' const preloadCheck = await build({',
|
|
303
|
+
" entryPoints: [path.join(appRoot, 'src-electron/preload.cts')],",
|
|
304
|
+
" outfile: path.join(stagingRoot, 'preload-check.cjs'),",
|
|
305
|
+
' bundle: true,',
|
|
306
|
+
" platform: 'node',",
|
|
307
|
+
" target: 'node22',",
|
|
308
|
+
" format: 'cjs',",
|
|
309
|
+
" external: ['electron'],",
|
|
310
|
+
' metafile: true,',
|
|
311
|
+
' write: false,',
|
|
312
|
+
" logLevel: 'silent',",
|
|
313
|
+
' });',
|
|
314
|
+
' for (const output of Object.values(preloadCheck.metafile?.outputs || {})) {',
|
|
315
|
+
' for (const imp of output.imports || []) {',
|
|
316
|
+
" if (imp.external && imp.path.startsWith('@nimiplatform/')) {",
|
|
317
|
+
' throw new Error(`Electron preload bundle must not retain external Nimi import: ${imp.path}`);',
|
|
318
|
+
' }',
|
|
319
|
+
' }',
|
|
320
|
+
' }',
|
|
292
321
|
'',
|
|
293
322
|
' await mkdir(path.join(productionSourceRoot, \'dist-electron\'), { recursive: true });',
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
"
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
" await writeFile(path.join(productionSourceRoot, 'pnpm-workspace.yaml'), stringifyYaml({ packages: ['.'] }));",
|
|
304
|
-
' }',
|
|
323
|
+
' const productionManifest = {',
|
|
324
|
+
' name: appPackage.name,',
|
|
325
|
+
' version: APP_VERSION,',
|
|
326
|
+
" type: 'module',",
|
|
327
|
+
" main: 'dist-electron/main.js',",
|
|
328
|
+
' ...(appPackage.license ? { license: appPackage.license } : {}),',
|
|
329
|
+
' ...(appPackage.author ? { author: appPackage.author } : {}),',
|
|
330
|
+
' };',
|
|
331
|
+
" await writeFile(path.join(productionSourceRoot, 'package.json'), `${JSON.stringify(productionManifest, null, 2)}\\n`);",
|
|
305
332
|
" await cp(path.join(appRoot, 'dist'), path.join(productionSourceRoot, 'dist'), { recursive: true, force: false });",
|
|
306
|
-
" await copyFile(
|
|
333
|
+
" await copyFile(mainBundlePath, path.join(productionSourceRoot, 'dist-electron', 'main.js'));",
|
|
307
334
|
" await copyFile(path.join(appRoot, 'dist-electron', 'preload.cjs'), path.join(productionSourceRoot, 'dist-electron', 'preload.cjs'));",
|
|
308
335
|
" await copyFile(icons.png, path.join(productionSourceRoot, 'dist-electron', 'app-icon.png'));",
|
|
309
336
|
" await copyFile(icons.macPng, path.join(productionSourceRoot, 'dist-electron', 'app-icon-macos.png'));",
|
|
310
337
|
" await copyFile(icons.ico, path.join(productionSourceRoot, 'dist-electron', 'app-icon.ico'));",
|
|
311
|
-
' await installProductionDependencies(productionSourceRoot);',
|
|
312
|
-
'',
|
|
313
|
-
" const productionManifestPath = path.join(productionSourceRoot, 'package.json');",
|
|
314
|
-
" const productionManifest = JSON.parse(await readFile(productionManifestPath, 'utf8'));",
|
|
315
|
-
' delete productionManifest.devDependencies;',
|
|
316
|
-
" await writeFile(productionManifestPath, `${JSON.stringify(productionManifest, null, 2)}\\n`);",
|
|
317
|
-
" await rm(path.join(productionSourceRoot, 'pnpm-lock.yaml'));",
|
|
318
|
-
" await rm(path.join(productionSourceRoot, 'pnpm-workspace.yaml'), { force: true });",
|
|
319
338
|
'',
|
|
320
339
|
" const installedNativePackage = path.join(productionSourceRoot, 'node_modules', ...NATIVE_BINDING_PACKAGE.split('/'));",
|
|
321
340
|
' const nativeDestination = MACOS_BUILD',
|
|
322
341
|
" ? path.join(stagingRoot, 'nimi-native', 'protected-local')",
|
|
323
342
|
' : installedNativePackage;',
|
|
324
|
-
' await rm(installedNativePackage, { recursive: true, force: true });',
|
|
325
343
|
' await mkdir(path.dirname(nativeDestination), { recursive: true });',
|
|
326
344
|
' await cp(nativePackageRoot, nativeDestination, { recursive: true, dereference: true, force: false });',
|
|
327
345
|
' await mkdir(packagerTempRoot, { recursive: true });',
|
|
@@ -379,6 +397,10 @@ function renderDefaultElectronProductionPackager(identity) {
|
|
|
379
397
|
' await realpath(MACOS_BUILD',
|
|
380
398
|
" ? path.join(expectedPackageRoot, `${APP_EXECUTABLE_NAME}.app`, 'Contents', 'MacOS', APP_EXECUTABLE_NAME)",
|
|
381
399
|
' : path.join(expectedPackageRoot, `${APP_EXECUTABLE_NAME}.exe`));',
|
|
400
|
+
' const resourcesRoot = MACOS_BUILD',
|
|
401
|
+
" ? path.join(expectedPackageRoot, `${APP_EXECUTABLE_NAME}.app`, 'Contents', 'Resources')",
|
|
402
|
+
" : path.join(expectedPackageRoot, 'resources');",
|
|
403
|
+
' await assertCleanAppBundle(resourcesRoot);',
|
|
382
404
|
' packageCompleted = true;',
|
|
383
405
|
' process.stdout.write(`[nimi-app] Electron production package: ${expectedPackageRoot}\\n`);',
|
|
384
406
|
'} finally {',
|
|
@@ -387,27 +409,82 @@ function renderDefaultElectronProductionPackager(identity) {
|
|
|
387
409
|
' await Promise.all(cleanupTasks);',
|
|
388
410
|
'}',
|
|
389
411
|
'',
|
|
390
|
-
'async function
|
|
391
|
-
" const command = 'pnpm install --prod --frozen-lockfile --ignore-scripts --node-linker=hoisted';",
|
|
392
|
-
" const commandShell = MACOS_BUILD ? 'pnpm' : process.env.ComSpec || 'cmd.exe';",
|
|
393
|
-
" const commandArguments = MACOS_BUILD ? ['install', '--prod', '--frozen-lockfile', '--ignore-scripts', '--node-linker=hoisted'] : ['/d', '/s', '/c', command];",
|
|
412
|
+
'async function verifyNodeSyntax(scriptPath) {',
|
|
394
413
|
' await new Promise((resolve, reject) => {',
|
|
395
|
-
|
|
396
|
-
' cwd: projectRoot,',
|
|
397
|
-
" stdio: 'inherit',",
|
|
398
|
-
' windowsHide: true,',
|
|
399
|
-
' });',
|
|
414
|
+
" const child = spawn(process.execPath, ['--check', scriptPath], { stdio: 'inherit', windowsHide: true });",
|
|
400
415
|
" child.once('error', reject);",
|
|
401
416
|
" child.once('exit', (code, signal) => {",
|
|
402
|
-
' if (code === 0) {',
|
|
403
|
-
'
|
|
404
|
-
' return;',
|
|
405
|
-
' }',
|
|
406
|
-
" reject(new Error(`pnpm production dependency staging failed (${signal || `exit ${String(code)}`}).`));",
|
|
417
|
+
' if (code === 0) { resolve(); return; }',
|
|
418
|
+
' reject(new Error(`Node syntax check failed for ${scriptPath} (${signal || `exit ${String(code)}`}).`));',
|
|
407
419
|
' });',
|
|
408
420
|
' });',
|
|
409
421
|
'}',
|
|
410
422
|
'',
|
|
423
|
+
'async function listAsarPaths(asarPath) {',
|
|
424
|
+
" const handle = await open(asarPath, 'r');",
|
|
425
|
+
' try {',
|
|
426
|
+
' const headerPrefix = Buffer.alloc(16);',
|
|
427
|
+
' const prefixRead = await handle.read(headerPrefix, 0, 16, 0);',
|
|
428
|
+
" if (prefixRead.bytesRead !== 16) throw new Error('Invalid app.asar header.');",
|
|
429
|
+
' const jsonLength = headerPrefix.readUInt32LE(12);',
|
|
430
|
+
" if (jsonLength === 0 || jsonLength > 64 * 1024 * 1024) throw new Error('Invalid app.asar header length.');",
|
|
431
|
+
' const jsonBuffer = Buffer.alloc(jsonLength);',
|
|
432
|
+
' const jsonRead = await handle.read(jsonBuffer, 0, jsonLength, 16);',
|
|
433
|
+
" if (jsonRead.bytesRead !== jsonLength) throw new Error('Truncated app.asar header.');",
|
|
434
|
+
" const root = JSON.parse(jsonBuffer.toString('utf8'));",
|
|
435
|
+
' const entries = [];',
|
|
436
|
+
' const walk = (node, prefix) => {',
|
|
437
|
+
' for (const [name, child] of Object.entries(node?.files || {})) {',
|
|
438
|
+
' const next = prefix ? `${prefix}/${name}` : name;',
|
|
439
|
+
" if (child && typeof child === 'object' && child.files) walk(child, next);",
|
|
440
|
+
' else entries.push(next);',
|
|
441
|
+
' }',
|
|
442
|
+
' };',
|
|
443
|
+
" walk(root, '');",
|
|
444
|
+
' return entries;',
|
|
445
|
+
' } finally {',
|
|
446
|
+
' await handle.close();',
|
|
447
|
+
' }',
|
|
448
|
+
'}',
|
|
449
|
+
'',
|
|
450
|
+
"async function listTreePaths(rootDir, prefix = '') {",
|
|
451
|
+
' let items;',
|
|
452
|
+
' try {',
|
|
453
|
+
' items = await readdir(rootDir, { withFileTypes: true });',
|
|
454
|
+
' } catch (error) {',
|
|
455
|
+
" if (error?.code === 'ENOENT') return [];",
|
|
456
|
+
' throw error;',
|
|
457
|
+
' }',
|
|
458
|
+
' const paths = [];',
|
|
459
|
+
' for (const item of items) {',
|
|
460
|
+
' const rel = prefix ? `${prefix}/${item.name}` : item.name;',
|
|
461
|
+
' if (item.isDirectory()) paths.push(...(await listTreePaths(path.join(rootDir, item.name), rel)));',
|
|
462
|
+
' else paths.push(rel);',
|
|
463
|
+
' }',
|
|
464
|
+
' return paths;',
|
|
465
|
+
'}',
|
|
466
|
+
'',
|
|
467
|
+
'async function assertCleanAppBundle(resourcesRoot) {',
|
|
468
|
+
" const asarPaths = await listAsarPaths(path.join(resourcesRoot, 'app.asar'));",
|
|
469
|
+
" const unpackedPaths = await listTreePaths(path.join(resourcesRoot, 'app.asar.unpacked'));",
|
|
470
|
+
' const allPaths = [...asarPaths, ...unpackedPaths];',
|
|
471
|
+
' const forbiddenPatterns = [',
|
|
472
|
+
' /(?:^|\\/)@electron-internal(?:\\/|$)/,',
|
|
473
|
+
' /(?:^|\\/)node_modules\\/typescript(?:\\/|$)/,',
|
|
474
|
+
' /(?:^|\\/)@img\\/sharp-/,',
|
|
475
|
+
' ...(MACOS_BUILD',
|
|
476
|
+
' ? [/\\.dll$/i, /(?:^|\\/)kit-protected-local-win32-x64(?:\\/|$)/, /win32-x64.*\\.node$/i, /linux-.*\\.node$/i]',
|
|
477
|
+
' : [/\\.dylib$/i, /(?:^|\\/)kit-protected-local-darwin-arm64(?:\\/|$)/, /darwin-arm64.*\\.node$/i, /linux-.*\\.node$/i]),',
|
|
478
|
+
' ];',
|
|
479
|
+
' for (const entryPath of allPaths) {',
|
|
480
|
+
' for (const pattern of forbiddenPatterns) {',
|
|
481
|
+
' if (pattern.test(entryPath)) {',
|
|
482
|
+
' throw new Error(`Unexpected contaminated payload entry in Electron bundle: ${entryPath}`);',
|
|
483
|
+
' }',
|
|
484
|
+
' }',
|
|
485
|
+
' }',
|
|
486
|
+
'}',
|
|
487
|
+
'',
|
|
411
488
|
'async function findPackageRoot(entry, expectedName) {',
|
|
412
489
|
' let current = path.dirname(await realpath(entry));',
|
|
413
490
|
' for (;;) {',
|
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
import { readdir, readFile, writeFile } from 'node:fs/promises';
|
|
1
|
+
import { lstat, readdir, readFile, writeFile } from 'node:fs/promises';
|
|
2
2
|
import path from 'node:path';
|
|
3
|
-
import { writeNimiAppArchive } from './app-pack.mjs';
|
|
3
|
+
import { MAX_ZIP32_ENTRIES, writeNimiAppArchive } from './app-pack.mjs';
|
|
4
4
|
import { hostedWebIdentity } from './app-identifiers.mjs';
|
|
5
5
|
import { resolveDevelopmentRealm } from './development-environment.mjs';
|
|
6
6
|
|
|
7
|
+
const HOSTED_WEB_MAX_ARCHIVE_BYTES = 256 * 1024 * 1024;
|
|
8
|
+
const HOSTED_WEB_MAX_UNCOMPRESSED_BYTES = 256 * 1024 * 1024;
|
|
9
|
+
|
|
7
10
|
const versionPattern =
|
|
8
11
|
/^(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/u;
|
|
9
12
|
|
|
@@ -76,6 +79,7 @@ export async function packHostedWeb(directory, outputFile) {
|
|
|
76
79
|
if (path.resolve(outputFile).startsWith(root + path.sep))
|
|
77
80
|
throw new Error('Write the archive outside the static directory.');
|
|
78
81
|
const entries = [];
|
|
82
|
+
let totalUncompressed = 0;
|
|
79
83
|
async function collect(relative) {
|
|
80
84
|
for (const item of await readdir(path.join(root, relative), { withFileTypes: true })) {
|
|
81
85
|
const name = relative ? relative + '/' + item.name : item.name;
|
|
@@ -84,8 +88,25 @@ export async function packHostedWeb(directory, outputFile) {
|
|
|
84
88
|
if (/^(?:\.git|\.env(?:\..*)?)(?:\/|$)/u.test(name))
|
|
85
89
|
throw new Error('Private working files cannot be hosted: ' + name);
|
|
86
90
|
if (item.isDirectory()) await collect(name);
|
|
87
|
-
else if (item.isFile())
|
|
88
|
-
|
|
91
|
+
else if (item.isFile()) {
|
|
92
|
+
const fullPath = path.join(root, name);
|
|
93
|
+
const st = await lstat(fullPath);
|
|
94
|
+
if (!st.isFile() || st.isSymbolicLink()) {
|
|
95
|
+
throw new Error('Unsupported file in hosted Web bundle: ' + name);
|
|
96
|
+
}
|
|
97
|
+
if (entries.length >= MAX_ZIP32_ENTRIES) {
|
|
98
|
+
throw new Error(`Hosted Web bundle exceeds ${MAX_ZIP32_ENTRIES} entries.`);
|
|
99
|
+
}
|
|
100
|
+
totalUncompressed += st.size;
|
|
101
|
+
if (totalUncompressed > HOSTED_WEB_MAX_UNCOMPRESSED_BYTES) {
|
|
102
|
+
throw new Error(`Hosted Web bundle exceeds ${HOSTED_WEB_MAX_UNCOMPRESSED_BYTES} bytes uncompressed.`);
|
|
103
|
+
}
|
|
104
|
+
const bytes = await readFile(fullPath);
|
|
105
|
+
if (bytes.length !== st.size) {
|
|
106
|
+
throw new Error('Hosted Web bundle file changed during packaging: ' + name);
|
|
107
|
+
}
|
|
108
|
+
entries.push({ name, bytes });
|
|
109
|
+
} else throw new Error('Unsupported file in hosted Web bundle: ' + name);
|
|
89
110
|
}
|
|
90
111
|
}
|
|
91
112
|
await collect('');
|
|
@@ -94,5 +115,10 @@ export async function packHostedWeb(directory, outputFile) {
|
|
|
94
115
|
!entries.some((e) => e.name === 'nimi-web-bundle.json')
|
|
95
116
|
)
|
|
96
117
|
throw new Error('Missing hosted Web entry or release manifest.');
|
|
97
|
-
|
|
118
|
+
const archive = writeNimiAppArchive(entries, {
|
|
119
|
+
compress: true,
|
|
120
|
+
maxArchiveBytes: HOSTED_WEB_MAX_ARCHIVE_BYTES,
|
|
121
|
+
maxTotalUncompressedBytes: HOSTED_WEB_MAX_UNCOMPRESSED_BYTES,
|
|
122
|
+
});
|
|
123
|
+
await writeFile(outputFile, archive, { flag: 'wx' });
|
|
98
124
|
}
|
package/lib/index.mjs
CHANGED
|
@@ -221,12 +221,12 @@ export function buildApp(cwd, options = {}) {
|
|
|
221
221
|
return buildAppProject(cwd, options, appToolRunners());
|
|
222
222
|
}
|
|
223
223
|
|
|
224
|
-
export function packApp(cwd, options = {}) {
|
|
224
|
+
export async function packApp(cwd, options = {}) {
|
|
225
225
|
const manifest = parseYaml(readFileSync(path.join(path.resolve(cwd, options.dir || '.'), 'nimi.app.yaml'), 'utf8'));
|
|
226
226
|
if (manifest.platform === 'web') throw new Error('Web pack is not implemented; use the App-owned website build and deployment process.');
|
|
227
227
|
return options.aggregate
|
|
228
|
-
? aggregateAppTargetCandidates(cwd, options)
|
|
229
|
-
: packAppTarget(cwd, options);
|
|
228
|
+
? await aggregateAppTargetCandidates(cwd, options)
|
|
229
|
+
: await packAppTarget(cwd, options);
|
|
230
230
|
}
|
|
231
231
|
|
|
232
232
|
// @nimi-authority: rule.nimi.platform.app-ecosystem.scaffold-web-carrier
|
package/lib/payload-links.mjs
CHANGED
|
@@ -1,10 +1,27 @@
|
|
|
1
1
|
// @nimi-authority: rule.nimi.platform.app-ecosystem.p-napp-014a
|
|
2
|
+
import { isUtf8 } from 'node:buffer';
|
|
2
3
|
import path from 'node:path';
|
|
3
4
|
|
|
4
5
|
export const SYMBOLIC_LINK_MODE = 0o120777;
|
|
6
|
+
export const MAX_SYMLINK_HOPS = 32;
|
|
7
|
+
export const MAX_SYMLINK_TARGET_BYTES = 4096;
|
|
8
|
+
|
|
9
|
+
const UTF8_DECODER = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true });
|
|
10
|
+
|
|
11
|
+
function decodeStrictUtf8Target(bytes, entryName) {
|
|
12
|
+
if (!Buffer.isBuffer(bytes) || bytes.length === 0 || bytes.length > MAX_SYMLINK_TARGET_BYTES || !isUtf8(bytes)) {
|
|
13
|
+
throw new Error(`Invalid payload symbolic link: ${entryName}`);
|
|
14
|
+
}
|
|
15
|
+
try {
|
|
16
|
+
return UTF8_DECODER.decode(bytes);
|
|
17
|
+
} catch {
|
|
18
|
+
throw new Error(`Invalid payload symbolic link: ${entryName}`);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
5
21
|
|
|
6
22
|
// Electron frameworks contain relative links such as Versions/Current -> A.
|
|
7
|
-
// Resolve against the archive tree
|
|
23
|
+
// Resolve component-by-component against the archive tree (matching XNU
|
|
24
|
+
// MAXSYMLINKS = 32), never collapsing `..` before expanding intermediate links.
|
|
8
25
|
export function validatePayloadLinks(entries, os) {
|
|
9
26
|
const directories = new Set();
|
|
10
27
|
for (const name of entries.keys()) {
|
|
@@ -15,27 +32,88 @@ export function validatePayloadLinks(entries, os) {
|
|
|
15
32
|
for (const name of directories) {
|
|
16
33
|
if (entries.has(name)) throw new Error(`nimiapp file/directory collision: ${name}`);
|
|
17
34
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
35
|
+
|
|
36
|
+
const resolveComponents = (base, rawTarget, entryName, state, active) => {
|
|
37
|
+
if (!rawTarget || rawTarget.startsWith('/') || rawTarget.includes('\\') || rawTarget.includes('\0')) {
|
|
38
|
+
throw new Error(`Invalid payload symbolic link: ${entryName}`);
|
|
39
|
+
}
|
|
40
|
+
let cur = [...base];
|
|
41
|
+
let trailingDir = rawTarget.endsWith('/');
|
|
42
|
+
const parts = rawTarget.split('/');
|
|
43
|
+
for (let i = 0; i < parts.length; i += 1) {
|
|
44
|
+
const part = parts[i];
|
|
45
|
+
const isLast = i === parts.length - 1;
|
|
46
|
+
if (part === '') {
|
|
47
|
+
if (!directories.has(cur.join('/'))) {
|
|
48
|
+
throw new Error(`Payload symbolic link traverses non-directory: ${entryName}`);
|
|
49
|
+
}
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
if (part === '.') {
|
|
53
|
+
if (!directories.has(cur.join('/'))) {
|
|
54
|
+
throw new Error(`Payload symbolic link traverses non-directory: ${entryName}`);
|
|
55
|
+
}
|
|
56
|
+
if (isLast) trailingDir = true;
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
if (part === '..') {
|
|
60
|
+
if (!directories.has(cur.join('/'))) {
|
|
61
|
+
throw new Error(`Payload symbolic link traverses non-directory: ${entryName}`);
|
|
62
|
+
}
|
|
63
|
+
if (cur.length <= 1) {
|
|
64
|
+
throw new Error(`Payload symbolic link escapes its root: ${entryName}`);
|
|
65
|
+
}
|
|
66
|
+
cur.pop();
|
|
67
|
+
if (isLast) trailingDir = true;
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
if (!directories.has(cur.join('/'))) {
|
|
71
|
+
throw new Error(`Payload symbolic link traverses non-directory: ${entryName}`);
|
|
72
|
+
}
|
|
73
|
+
cur.push(part);
|
|
74
|
+
const joined = cur.join('/');
|
|
75
|
+
const nextEntry = entries.get(joined);
|
|
76
|
+
if (nextEntry?.mode === SYMBOLIC_LINK_MODE) {
|
|
77
|
+
if (active.has(joined)) {
|
|
78
|
+
throw new Error(`Cyclic payload symbolic link: ${joined}`);
|
|
79
|
+
}
|
|
80
|
+
state.hops += 1;
|
|
81
|
+
if (state.hops > MAX_SYMLINK_HOPS) {
|
|
82
|
+
throw new Error(`Excessive payload symbolic link hops: ${joined}`);
|
|
83
|
+
}
|
|
84
|
+
active.add(joined);
|
|
85
|
+
const target = decodeStrictUtf8Target(nextEntry.bytes, joined);
|
|
86
|
+
const expanded = resolveComponents(cur.slice(0, -1), target, joined, state, active);
|
|
87
|
+
active.delete(joined);
|
|
88
|
+
cur = expanded.resolved;
|
|
89
|
+
if (isLast && expanded.trailingDir) {
|
|
90
|
+
trailingDir = true;
|
|
91
|
+
}
|
|
28
92
|
}
|
|
29
|
-
const destination = path.posix.normalize(path.posix.join(path.posix.dirname(prefix), target, ...parts.slice(length)));
|
|
30
|
-
if (!destination.startsWith('payload/')) throw new Error(`Payload symbolic link escapes its root: ${prefix}`);
|
|
31
|
-
return resolve(destination, new Set([...visited, prefix]));
|
|
32
93
|
}
|
|
33
|
-
|
|
34
|
-
return name;
|
|
94
|
+
return { resolved: cur, trailingDir };
|
|
35
95
|
};
|
|
96
|
+
|
|
36
97
|
for (const [name, entry] of entries) {
|
|
37
98
|
if (entry.mode !== SYMBOLIC_LINK_MODE) continue;
|
|
38
|
-
if (os !== 'macos' || !name.startsWith('payload/'))
|
|
39
|
-
|
|
99
|
+
if (os !== 'macos' || !name.startsWith('payload/')) {
|
|
100
|
+
throw new Error(`Symbolic link is not admitted for this target: ${name}`);
|
|
101
|
+
}
|
|
102
|
+
const target = decodeStrictUtf8Target(entry.bytes, name);
|
|
103
|
+
const parentParts = path.posix.dirname(name).split('/');
|
|
104
|
+
const state = { hops: 1 };
|
|
105
|
+
const active = new Set([name]);
|
|
106
|
+
const { resolved, trailingDir } = resolveComponents(parentParts, target, name, state, active);
|
|
107
|
+
if (resolved.length < 2 || resolved[0] !== 'payload') {
|
|
108
|
+
throw new Error(`Payload symbolic link escapes its root: ${name}`);
|
|
109
|
+
}
|
|
110
|
+
const finalPath = resolved.join('/');
|
|
111
|
+
if (trailingDir) {
|
|
112
|
+
if (!directories.has(finalPath)) {
|
|
113
|
+
throw new Error(`Missing payload symbolic link destination: ${finalPath}`);
|
|
114
|
+
}
|
|
115
|
+
} else if (!entries.has(finalPath) && !directories.has(finalPath)) {
|
|
116
|
+
throw new Error(`Missing payload symbolic link destination: ${finalPath}`);
|
|
117
|
+
}
|
|
40
118
|
}
|
|
41
119
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nimiplatform/app-tools",
|
|
3
|
-
"version": "0.16.0-rc.
|
|
3
|
+
"version": "0.16.0-rc.14",
|
|
4
4
|
"description": "Public app-authoring CLI for Nimi App projects.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|
|
@@ -18,11 +18,11 @@
|
|
|
18
18
|
"provenance": true
|
|
19
19
|
},
|
|
20
20
|
"nimiScaffoldVersions": {
|
|
21
|
-
"sdkVersion": "^0.24.0-rc.
|
|
22
|
-
"appToolsVersion": "^0.16.0-rc.
|
|
21
|
+
"sdkVersion": "^0.24.0-rc.11",
|
|
22
|
+
"appToolsVersion": "^0.16.0-rc.14",
|
|
23
23
|
"nimicodingVersion": "0.7.0",
|
|
24
24
|
"aiSdkVersion": "^7.0.171",
|
|
25
|
-
"kitVersion": "^0.24.0-rc.
|
|
25
|
+
"kitVersion": "^0.24.0-rc.18",
|
|
26
26
|
"reactVersion": "^19.3.1",
|
|
27
27
|
"reactDomVersion": "^19.3.1",
|
|
28
28
|
"i18nextVersion": "^26.7.2",
|
|
@@ -68,7 +68,7 @@
|
|
|
68
68
|
"prepublishOnly": "pnpm run build",
|
|
69
69
|
"test": "node ../scripts/with-workspace-surfaces.mjs -- pnpm run test:prepared",
|
|
70
70
|
"test:large-archive": "node --test test/app-pack-large.test.mjs",
|
|
71
|
-
"test:prepared": "node scripts/sync-scaffold-versions.mjs --check && node --test test/dev-shell.test.mjs && node --test test/app-icons.test.mjs test/app-storefront.test.mjs test/app-dependency-combinations.test.mjs test/app-safety-profile.test.mjs test/app-project-lifecycle.test.mjs test/app-pack.test.mjs test/app-pack-large.test.mjs test/app-scaffold.test.mjs test/managed-web-carrier.test.mjs test/web-carrier.test.mjs test/hosted-web-release.test.mjs test/mobile-development.test.mjs"
|
|
71
|
+
"test:prepared": "node scripts/sync-scaffold-versions.mjs --check && node --test test/dev-shell.test.mjs && node --test test/app-icons.test.mjs test/app-storefront.test.mjs test/app-dependency-combinations.test.mjs test/app-safety-profile.test.mjs test/app-project-lifecycle.test.mjs test/app-pack.test.mjs test/app-pack-deflate.test.mjs test/app-pack-large.test.mjs test/app-scaffold.test.mjs test/managed-web-carrier.test.mjs test/web-carrier.test.mjs test/hosted-web-release.test.mjs test/mobile-development.test.mjs"
|
|
72
72
|
},
|
|
73
73
|
"files": [
|
|
74
74
|
"bin",
|