@nimiplatform/app-tools 0.16.0-rc.13 → 0.16.0-rc.15
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 +1078 -177
- package/lib/app-scaffold-profiles.mjs +189 -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';",
|
|
@@ -246,6 +252,7 @@ function renderDefaultElectronProductionPackager(identity) {
|
|
|
246
252
|
"const appPackage = JSON.parse(await readFile(path.join(appRoot, 'package.json'), 'utf8'));",
|
|
247
253
|
'const APP_VERSION = appPackage.version;',
|
|
248
254
|
'const RESOURCE_VERSION = MACOS_BUILD ? APP_VERSION : resolveWindowsResourceVersion(APP_VERSION);',
|
|
255
|
+
"const APP_DECLARES_SHARP = Object.hasOwn(appPackage.dependencies || {}, 'sharp');",
|
|
249
256
|
"for (const sectionName of ['dependencies', 'devDependencies', 'optionalDependencies', 'peerDependencies']) {",
|
|
250
257
|
' if (Object.hasOwn(appPackage[sectionName] || {}, NATIVE_BINDING_PACKAGE)) {',
|
|
251
258
|
" throw new Error('The protected native binding must arrive through the Kit optional dependency.');",
|
|
@@ -274,56 +281,71 @@ function renderDefaultElectronProductionPackager(identity) {
|
|
|
274
281
|
'',
|
|
275
282
|
'try {',
|
|
276
283
|
' const icons = prepareElectronIcons(appRoot, { required: true });',
|
|
284
|
+
" const mainBundlePath = path.join(appRoot, 'dist-electron/main.js');",
|
|
277
285
|
' await build({',
|
|
278
286
|
" entryPoints: [path.join(appRoot, 'src-electron/main.ts')],",
|
|
279
|
-
|
|
287
|
+
' outfile: mainBundlePath,',
|
|
280
288
|
' bundle: true,',
|
|
281
289
|
" platform: 'node',",
|
|
282
290
|
" target: 'node22',",
|
|
283
291
|
" format: 'esm',",
|
|
284
|
-
"
|
|
285
|
-
|
|
292
|
+
" external: ['electron', 'original-fs', 'sharp', '@nimiplatform/kit-protected-local-win32-x64', '@nimiplatform/kit-protected-local-darwin-arm64'],",
|
|
293
|
+
' banner: {',
|
|
294
|
+
' js: "import { createRequire as __nimiCreateRequire } from \'node:module\'; const require = __nimiCreateRequire(import.meta.url);",',
|
|
295
|
+
' },',
|
|
286
296
|
" define: { __NIMI_ELECTRON_PRODUCTION__: 'true' },",
|
|
287
297
|
" logLevel: 'silent',",
|
|
288
298
|
' });',
|
|
299
|
+
' await verifyNodeSyntax(mainBundlePath);',
|
|
289
300
|
'',
|
|
290
301
|
" await realpath(path.join(appRoot, 'dist', 'index.html'));",
|
|
291
302
|
" await realpath(path.join(appRoot, 'dist-electron', 'preload.cjs'));",
|
|
303
|
+
' const preloadCheck = await build({',
|
|
304
|
+
" entryPoints: [path.join(appRoot, 'src-electron/preload.cts')],",
|
|
305
|
+
" outfile: path.join(stagingRoot, 'preload-check.cjs'),",
|
|
306
|
+
' bundle: true,',
|
|
307
|
+
" platform: 'node',",
|
|
308
|
+
" target: 'node22',",
|
|
309
|
+
" format: 'cjs',",
|
|
310
|
+
" external: ['electron'],",
|
|
311
|
+
' metafile: true,',
|
|
312
|
+
' write: false,',
|
|
313
|
+
" logLevel: 'silent',",
|
|
314
|
+
' });',
|
|
315
|
+
' for (const output of Object.values(preloadCheck.metafile?.outputs || {})) {',
|
|
316
|
+
' for (const imp of output.imports || []) {',
|
|
317
|
+
" if (imp.external && imp.path.startsWith('@nimiplatform/')) {",
|
|
318
|
+
' throw new Error(`Electron preload bundle must not retain external Nimi import: ${imp.path}`);',
|
|
319
|
+
' }',
|
|
320
|
+
' }',
|
|
321
|
+
' }',
|
|
292
322
|
'',
|
|
293
323
|
' 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
|
-
' }',
|
|
324
|
+
' const productionManifest = {',
|
|
325
|
+
' name: appPackage.name,',
|
|
326
|
+
' version: APP_VERSION,',
|
|
327
|
+
" type: 'module',",
|
|
328
|
+
" main: 'dist-electron/main.js',",
|
|
329
|
+
' ...(appPackage.license ? { license: appPackage.license } : {}),',
|
|
330
|
+
' ...(appPackage.author ? { author: appPackage.author } : {}),',
|
|
331
|
+
' };',
|
|
332
|
+
" await writeFile(path.join(productionSourceRoot, 'package.json'), `${JSON.stringify(productionManifest, null, 2)}\\n`);",
|
|
305
333
|
" await cp(path.join(appRoot, 'dist'), path.join(productionSourceRoot, 'dist'), { recursive: true, force: false });",
|
|
306
|
-
" await copyFile(
|
|
334
|
+
" await copyFile(mainBundlePath, path.join(productionSourceRoot, 'dist-electron', 'main.js'));",
|
|
307
335
|
" await copyFile(path.join(appRoot, 'dist-electron', 'preload.cjs'), path.join(productionSourceRoot, 'dist-electron', 'preload.cjs'));",
|
|
308
336
|
" await copyFile(icons.png, path.join(productionSourceRoot, 'dist-electron', 'app-icon.png'));",
|
|
309
337
|
" await copyFile(icons.macPng, path.join(productionSourceRoot, 'dist-electron', 'app-icon-macos.png'));",
|
|
310
338
|
" 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
339
|
'',
|
|
320
340
|
" const installedNativePackage = path.join(productionSourceRoot, 'node_modules', ...NATIVE_BINDING_PACKAGE.split('/'));",
|
|
321
341
|
' const nativeDestination = MACOS_BUILD',
|
|
322
342
|
" ? path.join(stagingRoot, 'nimi-native', 'protected-local')",
|
|
323
343
|
' : installedNativePackage;',
|
|
324
|
-
' await rm(installedNativePackage, { recursive: true, force: true });',
|
|
325
344
|
' await mkdir(path.dirname(nativeDestination), { recursive: true });',
|
|
326
345
|
' await cp(nativePackageRoot, nativeDestination, { recursive: true, dereference: true, force: false });',
|
|
346
|
+
' if (APP_DECLARES_SHARP) {',
|
|
347
|
+
' await stageDeclaredSharpRuntime(productionSourceRoot);',
|
|
348
|
+
' }',
|
|
327
349
|
' await mkdir(packagerTempRoot, { recursive: true });',
|
|
328
350
|
'',
|
|
329
351
|
' const packagePaths = await packager({',
|
|
@@ -379,6 +401,10 @@ function renderDefaultElectronProductionPackager(identity) {
|
|
|
379
401
|
' await realpath(MACOS_BUILD',
|
|
380
402
|
" ? path.join(expectedPackageRoot, `${APP_EXECUTABLE_NAME}.app`, 'Contents', 'MacOS', APP_EXECUTABLE_NAME)",
|
|
381
403
|
' : path.join(expectedPackageRoot, `${APP_EXECUTABLE_NAME}.exe`));',
|
|
404
|
+
' const resourcesRoot = MACOS_BUILD',
|
|
405
|
+
" ? path.join(expectedPackageRoot, `${APP_EXECUTABLE_NAME}.app`, 'Contents', 'Resources')",
|
|
406
|
+
" : path.join(expectedPackageRoot, 'resources');",
|
|
407
|
+
' await assertCleanAppBundle(resourcesRoot);',
|
|
382
408
|
' packageCompleted = true;',
|
|
383
409
|
' process.stdout.write(`[nimi-app] Electron production package: ${expectedPackageRoot}\\n`);',
|
|
384
410
|
'} finally {',
|
|
@@ -387,27 +413,148 @@ function renderDefaultElectronProductionPackager(identity) {
|
|
|
387
413
|
' await Promise.all(cleanupTasks);',
|
|
388
414
|
'}',
|
|
389
415
|
'',
|
|
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];",
|
|
416
|
+
'async function verifyNodeSyntax(scriptPath) {',
|
|
394
417
|
' await new Promise((resolve, reject) => {',
|
|
395
|
-
|
|
396
|
-
' cwd: projectRoot,',
|
|
397
|
-
" stdio: 'inherit',",
|
|
398
|
-
' windowsHide: true,',
|
|
399
|
-
' });',
|
|
418
|
+
" const child = spawn(process.execPath, ['--check', scriptPath], { stdio: 'inherit', windowsHide: true });",
|
|
400
419
|
" child.once('error', reject);",
|
|
401
420
|
" 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)}`}).`));",
|
|
421
|
+
' if (code === 0) { resolve(); return; }',
|
|
422
|
+
' reject(new Error(`Node syntax check failed for ${scriptPath} (${signal || `exit ${String(code)}`}).`));',
|
|
407
423
|
' });',
|
|
408
424
|
' });',
|
|
409
425
|
'}',
|
|
410
426
|
'',
|
|
427
|
+
'async function listAsarPaths(asarPath) {',
|
|
428
|
+
" const handle = await open(asarPath, 'r');",
|
|
429
|
+
' try {',
|
|
430
|
+
' const headerPrefix = Buffer.alloc(16);',
|
|
431
|
+
' const prefixRead = await handle.read(headerPrefix, 0, 16, 0);',
|
|
432
|
+
" if (prefixRead.bytesRead !== 16) throw new Error('Invalid app.asar header.');",
|
|
433
|
+
' const jsonLength = headerPrefix.readUInt32LE(12);',
|
|
434
|
+
" if (jsonLength === 0 || jsonLength > 64 * 1024 * 1024) throw new Error('Invalid app.asar header length.');",
|
|
435
|
+
' const jsonBuffer = Buffer.alloc(jsonLength);',
|
|
436
|
+
' const jsonRead = await handle.read(jsonBuffer, 0, jsonLength, 16);',
|
|
437
|
+
" if (jsonRead.bytesRead !== jsonLength) throw new Error('Truncated app.asar header.');",
|
|
438
|
+
" const root = JSON.parse(jsonBuffer.toString('utf8'));",
|
|
439
|
+
' const entries = [];',
|
|
440
|
+
' const walk = (node, prefix) => {',
|
|
441
|
+
' for (const [name, child] of Object.entries(node?.files || {})) {',
|
|
442
|
+
' const next = prefix ? `${prefix}/${name}` : name;',
|
|
443
|
+
" if (child && typeof child === 'object' && child.files) walk(child, next);",
|
|
444
|
+
' else entries.push(next);',
|
|
445
|
+
' }',
|
|
446
|
+
' };',
|
|
447
|
+
" walk(root, '');",
|
|
448
|
+
' return entries;',
|
|
449
|
+
' } finally {',
|
|
450
|
+
' await handle.close();',
|
|
451
|
+
' }',
|
|
452
|
+
'}',
|
|
453
|
+
'',
|
|
454
|
+
"async function listTreePaths(rootDir, prefix = '') {",
|
|
455
|
+
' let items;',
|
|
456
|
+
' try {',
|
|
457
|
+
' items = await readdir(rootDir, { withFileTypes: true });',
|
|
458
|
+
' } catch (error) {',
|
|
459
|
+
" if (error?.code === 'ENOENT') return [];",
|
|
460
|
+
' throw error;',
|
|
461
|
+
' }',
|
|
462
|
+
' const paths = [];',
|
|
463
|
+
' for (const item of items) {',
|
|
464
|
+
' const rel = prefix ? `${prefix}/${item.name}` : item.name;',
|
|
465
|
+
' if (item.isDirectory()) paths.push(...(await listTreePaths(path.join(rootDir, item.name), rel)));',
|
|
466
|
+
' else paths.push(rel);',
|
|
467
|
+
' }',
|
|
468
|
+
' return paths;',
|
|
469
|
+
'}',
|
|
470
|
+
'',
|
|
471
|
+
'async function assertCleanAppBundle(resourcesRoot) {',
|
|
472
|
+
" const asarPaths = await listAsarPaths(path.join(resourcesRoot, 'app.asar'));",
|
|
473
|
+
" const unpackedPaths = await listTreePaths(path.join(resourcesRoot, 'app.asar.unpacked'));",
|
|
474
|
+
' const allPaths = [...asarPaths, ...unpackedPaths];',
|
|
475
|
+
' const forbiddenPatterns = [',
|
|
476
|
+
' /(?:^|\\/)@electron-internal(?:\\/|$)/,',
|
|
477
|
+
' /(?:^|\\/)node_modules\\/typescript(?:\\/|$)/,',
|
|
478
|
+
' ...(APP_DECLARES_SHARP',
|
|
479
|
+
' ? [MACOS_BUILD',
|
|
480
|
+
' ? /(?:^|\\/)@img\\/sharp-(?:libvips-)?(?!darwin-arm64(?:\\/|$))/',
|
|
481
|
+
' : /(?:^|\\/)@img\\/sharp-(?:libvips-)?(?!win32-x64(?:\\/|$))/]',
|
|
482
|
+
' : [/(?:^|\\/)@img\\/sharp-/]),',
|
|
483
|
+
' ...(MACOS_BUILD',
|
|
484
|
+
' ? [/\\.dll$/i, /(?:^|\\/)kit-protected-local-win32-x64(?:\\/|$)/, /win32-x64.*\\.node$/i, /linux-.*\\.node$/i]',
|
|
485
|
+
' : [/\\.dylib$/i, /(?:^|\\/)kit-protected-local-darwin-arm64(?:\\/|$)/, /darwin-arm64.*\\.node$/i, /linux-.*\\.node$/i]),',
|
|
486
|
+
' ];',
|
|
487
|
+
' for (const entryPath of allPaths) {',
|
|
488
|
+
' for (const pattern of forbiddenPatterns) {',
|
|
489
|
+
' if (pattern.test(entryPath)) {',
|
|
490
|
+
' throw new Error(`Unexpected contaminated payload entry in Electron bundle: ${entryPath}`);',
|
|
491
|
+
' }',
|
|
492
|
+
' }',
|
|
493
|
+
' }',
|
|
494
|
+
'}',
|
|
495
|
+
'',
|
|
496
|
+
'async function resolveInstalledPackageDir(packageName, fromRoot) {',
|
|
497
|
+
" const parts = packageName.split('/');",
|
|
498
|
+
' let current = fromRoot;',
|
|
499
|
+
' for (;;) {',
|
|
500
|
+
" const candidates = [path.join(current, 'node_modules', ...parts)];",
|
|
501
|
+
" if (path.basename(current) === 'node_modules') {",
|
|
502
|
+
' candidates.unshift(path.join(current, ...parts));',
|
|
503
|
+
' }',
|
|
504
|
+
' for (const candidate of candidates) {',
|
|
505
|
+
' try {',
|
|
506
|
+
' return await realpath(candidate);',
|
|
507
|
+
' } catch (error) {',
|
|
508
|
+
" if (error?.code !== 'ENOENT') throw error;",
|
|
509
|
+
' }',
|
|
510
|
+
' }',
|
|
511
|
+
' const parent = path.dirname(current);',
|
|
512
|
+
' if (parent === current) break;',
|
|
513
|
+
' current = parent;',
|
|
514
|
+
' }',
|
|
515
|
+
' try {',
|
|
516
|
+
" return await realpath(path.join(appRoot, 'node_modules', ...parts));",
|
|
517
|
+
' } catch (error) {',
|
|
518
|
+
" if (error?.code === 'ENOENT') return null;",
|
|
519
|
+
' throw error;',
|
|
520
|
+
' }',
|
|
521
|
+
'}',
|
|
522
|
+
'',
|
|
523
|
+
'async function stageDeclaredSharpRuntime(sourceRoot) {',
|
|
524
|
+
" const sharpRoot = await resolveInstalledPackageDir('sharp', appRoot);",
|
|
525
|
+
' if (!sharpRoot) {',
|
|
526
|
+
" throw new Error('App declares sharp in dependencies, but node_modules/sharp could not be resolved.');",
|
|
527
|
+
' }',
|
|
528
|
+
' const platformSuffix = `-${NATIVE_PLATFORM}-${NATIVE_ARCH}`;',
|
|
529
|
+
' const staged = new Set();',
|
|
530
|
+
" const queue = [['sharp', sharpRoot]];",
|
|
531
|
+
' while (queue.length > 0) {',
|
|
532
|
+
' const [pkgName, pkgRoot] = queue.shift();',
|
|
533
|
+
' if (staged.has(pkgName)) continue;',
|
|
534
|
+
' staged.add(pkgName);',
|
|
535
|
+
" const destination = path.join(sourceRoot, 'node_modules', ...pkgName.split('/'));",
|
|
536
|
+
' await mkdir(path.dirname(destination), { recursive: true });',
|
|
537
|
+
' await cp(pkgRoot, destination, { recursive: true, dereference: true, force: false });',
|
|
538
|
+
" const manifest = JSON.parse(await readFile(path.join(pkgRoot, 'package.json'), 'utf8'));",
|
|
539
|
+
' for (const depName of Object.keys(manifest.dependencies || {})) {',
|
|
540
|
+
' if (staged.has(depName)) continue;',
|
|
541
|
+
' const depRoot = await resolveInstalledPackageDir(depName, pkgRoot);',
|
|
542
|
+
' if (!depRoot) {',
|
|
543
|
+
' throw new Error(`Unable to resolve declared sharp dependency ${depName} from ${pkgName}.`);',
|
|
544
|
+
' }',
|
|
545
|
+
' queue.push([depName, depRoot]);',
|
|
546
|
+
' }',
|
|
547
|
+
' for (const optName of Object.keys(manifest.optionalDependencies || {})) {',
|
|
548
|
+
' if (!optName.endsWith(platformSuffix) || staged.has(optName)) continue;',
|
|
549
|
+
' const optRoot = await resolveInstalledPackageDir(optName, pkgRoot);',
|
|
550
|
+
' if (!optRoot) {',
|
|
551
|
+
' throw new Error(`Unable to resolve declared sharp platform package ${optName} from ${pkgName}.`);',
|
|
552
|
+
' }',
|
|
553
|
+
' queue.push([optName, optRoot]);',
|
|
554
|
+
' }',
|
|
555
|
+
' }',
|
|
556
|
+
'}',
|
|
557
|
+
'',
|
|
411
558
|
'async function findPackageRoot(entry, expectedName) {',
|
|
412
559
|
' let current = path.dirname(await realpath(entry));',
|
|
413
560
|
' 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.15",
|
|
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.15",
|
|
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",
|