@nimiplatform/app-tools 0.16.0-rc.14 → 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/lib/app-pack.mjs CHANGED
@@ -449,17 +449,12 @@ export function writeNimiAppArchive(entries, options = {}) {
449
449
  if (normalized.length === 0 || normalized.length > MAX_ZIP32_ENTRIES) {
450
450
  throw new Error('nimiapp archive requires between 1 and 65535 files');
451
451
  }
452
- for (let index = 1; index < normalized.length; index += 1) {
453
- if (Buffer.compare(normalized[index - 1].rawName, normalized[index].rawName) >= 0) {
452
+ let totalUncompressed = 0;
453
+ for (let index = 0; index < normalized.length; index += 1) {
454
+ const entry = normalized[index];
455
+ if (index > 0 && Buffer.compare(normalized[index - 1].rawName, entry.rawName) >= 0) {
454
456
  throw new Error('nimiapp archive entries must be unique');
455
457
  }
456
- }
457
-
458
- const localParts = [];
459
- const centralParts = [];
460
- let offset = 0;
461
- let totalUncompressed = 0;
462
- for (const entry of normalized) {
463
458
  const uncompSize = entry.bytes.length;
464
459
  if (uncompSize > NATIVE_MAX_ENTRY_BYTES || uncompSize >= 0xffffffff) {
465
460
  throw new Error(`Archive entry is too large: ${entry.name}`);
@@ -471,12 +466,23 @@ export function writeNimiAppArchive(entries, options = {}) {
471
466
  if (totalUncompressed > maxTotalUncompressedBytes) {
472
467
  throw new Error(`Archive uncompressed size exceeds ${maxTotalUncompressedBytes} bytes`);
473
468
  }
469
+ }
470
+ const enforceExpansionBudget = totalUncompressed > EXPANSION_RATIO_FLOOR_BYTES;
471
+
472
+ const localParts = [];
473
+ const centralParts = [];
474
+ let offset = 0;
475
+ let emittedUncompressed = 0;
476
+ for (const entry of normalized) {
477
+ const uncompSize = entry.bytes.length;
474
478
  const crc = uncompSize === 0 ? 0 : nativeCrc32(entry.bytes);
475
479
  let method = ZIP_METHOD_STORE;
476
480
  let payload = entry.bytes;
477
481
  if (compress && entry.mode !== SYMBOLIC_LINK_MODE && uncompSize > 0) {
478
482
  const deflated = deflateRawSync(entry.bytes, { level: DEFLATE_LEVEL });
479
- if (deflated.length > 0 && deflated.length < uncompSize) {
483
+ const fitsBudget = !enforceExpansionBudget
484
+ || emittedUncompressed + uncompSize <= (offset + 30 + entry.rawName.length + deflated.length) * MAX_EXPANSION_RATIO;
485
+ if (deflated.length > 0 && deflated.length < uncompSize && fitsBudget) {
480
486
  method = ZIP_METHOD_DEFLATE;
481
487
  payload = deflated;
482
488
  }
@@ -499,6 +505,7 @@ export function writeNimiAppArchive(entries, options = {}) {
499
505
  );
500
506
  centralParts.push(central, entry.rawName);
501
507
  offset = nextOffset;
508
+ emittedUncompressed += uncompSize;
502
509
  }
503
510
  const central = Buffer.concat(centralParts);
504
511
  const finalSize = offset + central.length + 22;
@@ -678,6 +685,8 @@ export async function writeNimiAppArchiveToFile(filePath, entries, options = {})
678
685
  }
679
686
  }
680
687
 
688
+ const enforceExpansionBudget = totalUncompressed > EXPANSION_RATIO_FLOOR_BYTES;
689
+
681
690
  await fsp.mkdir(path.dirname(filePath), { recursive: true });
682
691
  const tempPath = `${filePath}.tmp-${process.pid}-${Date.now()}-${Math.random().toString(16).slice(2, 8)}`;
683
692
  const shardPath = `${tempPath}.shard`;
@@ -685,6 +694,7 @@ export async function writeNimiAppArchiveToFile(filePath, entries, options = {})
685
694
  const archiveHash = createHash('sha256');
686
695
  const centralParts = [];
687
696
  let offset = 0;
697
+ let emittedUncompressed = 0;
688
698
  let hashedEntrySha256 = null;
689
699
  let renamed = false;
690
700
 
@@ -716,7 +726,9 @@ export async function writeNimiAppArchiveToFile(filePath, entries, options = {})
716
726
  let payload = plain;
717
727
  if (compress && entry.mode !== SYMBOLIC_LINK_MODE) {
718
728
  const deflated = deflateRawSync(plain, { level: DEFLATE_LEVEL });
719
- if (deflated.length > 0 && deflated.length < uncompSize) {
729
+ const fitsBudget = !enforceExpansionBudget
730
+ || emittedUncompressed + uncompSize <= (offset + 30 + entry.rawName.length + deflated.length) * MAX_EXPANSION_RATIO;
731
+ if (deflated.length > 0 && deflated.length < uncompSize && fitsBudget) {
720
732
  method = ZIP_METHOD_DEFLATE;
721
733
  payload = deflated;
722
734
  }
@@ -735,7 +747,9 @@ export async function writeNimiAppArchiveToFile(filePath, entries, options = {})
735
747
  const stats = await streamDeflateFileToShard(entry.filePath, shardPath, uncompSize, wantHash);
736
748
  crc = stats.crc;
737
749
  if (wantHash) hashedEntrySha256 = stats.entrySha256;
738
- if (stats.compSize > 0 && stats.compSize < uncompSize) {
750
+ const fitsBudget = !enforceExpansionBudget
751
+ || emittedUncompressed + uncompSize <= (offset + 30 + entry.rawName.length + stats.compSize) * MAX_EXPANSION_RATIO;
752
+ if (stats.compSize > 0 && stats.compSize < uncompSize && fitsBudget) {
739
753
  method = ZIP_METHOD_DEFLATE;
740
754
  compSize = stats.compSize;
741
755
  const nextOffset = offset + 30 + entry.rawName.length + compSize;
@@ -806,6 +820,7 @@ export async function writeNimiAppArchiveToFile(filePath, entries, options = {})
806
820
  );
807
821
  centralParts.push(central, entry.rawName);
808
822
  offset += 30 + entry.rawName.length + compSize;
823
+ emittedUncompressed += uncompSize;
809
824
  }
810
825
 
811
826
  const central = Buffer.concat(centralParts);
@@ -252,6 +252,7 @@ function renderDefaultElectronProductionPackager(identity) {
252
252
  "const appPackage = JSON.parse(await readFile(path.join(appRoot, 'package.json'), 'utf8'));",
253
253
  'const APP_VERSION = appPackage.version;',
254
254
  'const RESOURCE_VERSION = MACOS_BUILD ? APP_VERSION : resolveWindowsResourceVersion(APP_VERSION);',
255
+ "const APP_DECLARES_SHARP = Object.hasOwn(appPackage.dependencies || {}, 'sharp');",
255
256
  "for (const sectionName of ['dependencies', 'devDependencies', 'optionalDependencies', 'peerDependencies']) {",
256
257
  ' if (Object.hasOwn(appPackage[sectionName] || {}, NATIVE_BINDING_PACKAGE)) {',
257
258
  " throw new Error('The protected native binding must arrive through the Kit optional dependency.');",
@@ -342,6 +343,9 @@ function renderDefaultElectronProductionPackager(identity) {
342
343
  ' : installedNativePackage;',
343
344
  ' await mkdir(path.dirname(nativeDestination), { recursive: true });',
344
345
  ' await cp(nativePackageRoot, nativeDestination, { recursive: true, dereference: true, force: false });',
346
+ ' if (APP_DECLARES_SHARP) {',
347
+ ' await stageDeclaredSharpRuntime(productionSourceRoot);',
348
+ ' }',
345
349
  ' await mkdir(packagerTempRoot, { recursive: true });',
346
350
  '',
347
351
  ' const packagePaths = await packager({',
@@ -471,7 +475,11 @@ function renderDefaultElectronProductionPackager(identity) {
471
475
  ' const forbiddenPatterns = [',
472
476
  ' /(?:^|\\/)@electron-internal(?:\\/|$)/,',
473
477
  ' /(?:^|\\/)node_modules\\/typescript(?:\\/|$)/,',
474
- ' /(?:^|\\/)@img\\/sharp-/,',
478
+ ' ...(APP_DECLARES_SHARP',
479
+ ' ? [MACOS_BUILD',
480
+ ' ? /(?:^|\\/)@img\\/sharp-(?:libvips-)?(?!darwin-arm64(?:\\/|$))/',
481
+ ' : /(?:^|\\/)@img\\/sharp-(?:libvips-)?(?!win32-x64(?:\\/|$))/]',
482
+ ' : [/(?:^|\\/)@img\\/sharp-/]),',
475
483
  ' ...(MACOS_BUILD',
476
484
  ' ? [/\\.dll$/i, /(?:^|\\/)kit-protected-local-win32-x64(?:\\/|$)/, /win32-x64.*\\.node$/i, /linux-.*\\.node$/i]',
477
485
  ' : [/\\.dylib$/i, /(?:^|\\/)kit-protected-local-darwin-arm64(?:\\/|$)/, /darwin-arm64.*\\.node$/i, /linux-.*\\.node$/i]),',
@@ -485,6 +493,68 @@ function renderDefaultElectronProductionPackager(identity) {
485
493
  ' }',
486
494
  '}',
487
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
+ '',
488
558
  'async function findPackageRoot(entry, expectedName) {',
489
559
  ' let current = path.dirname(await realpath(entry));',
490
560
  ' for (;;) {',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nimiplatform/app-tools",
3
- "version": "0.16.0-rc.14",
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",
@@ -19,7 +19,7 @@
19
19
  },
20
20
  "nimiScaffoldVersions": {
21
21
  "sdkVersion": "^0.24.0-rc.11",
22
- "appToolsVersion": "^0.16.0-rc.14",
22
+ "appToolsVersion": "^0.16.0-rc.15",
23
23
  "nimicodingVersion": "0.7.0",
24
24
  "aiSdkVersion": "^7.0.171",
25
25
  "kitVersion": "^0.24.0-rc.18",