@nimiplatform/app-tools 0.12.1 → 0.12.2

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.
Files changed (2) hide show
  1. package/lib/app-pack.mjs +48 -6
  2. package/package.json +4 -3
package/lib/app-pack.mjs CHANGED
@@ -9,6 +9,11 @@
9
9
  import { createHash } from 'node:crypto';
10
10
  import {
11
11
  existsSync,
12
+ openSync,
13
+ closeSync,
14
+ fstatSync,
15
+ readSync,
16
+ writeSync,
12
17
  lstatSync,
13
18
  mkdirSync,
14
19
  readFileSync,
@@ -51,6 +56,43 @@ function canonicalJson(value) {
51
56
  return `${JSON.stringify(sortedValue(value), null, 2)}\n`;
52
57
  }
53
58
 
59
+ export function hashAppArchive(bytes) {
60
+ const hash = createHash('sha256');
61
+ // Node's native crypto update accepts at most an int32-sized input even
62
+ // though a valid ZIP32 App archive can exceed 2 GiB.
63
+ const chunkBytes = 64 * 1024 * 1024;
64
+ for (let offset = 0; offset < bytes.length; offset += chunkBytes) {
65
+ hash.update(bytes.subarray(offset, offset + chunkBytes));
66
+ }
67
+ return hash.digest('hex');
68
+ }
69
+
70
+ export function writeAppArchiveFile(filePath, bytes) {
71
+ const fd = openSync(filePath, 'w');
72
+ try {
73
+ let offset = 0;
74
+ while (offset < bytes.length) {
75
+ const written = writeSync(fd, bytes, offset, Math.min(64 * 1024 * 1024, bytes.length - offset));
76
+ if (written <= 0) throw new Error('App archive write did not make progress');
77
+ offset += written;
78
+ }
79
+ } finally { closeSync(fd); }
80
+ }
81
+
82
+ export function readAppArchiveFile(filePath) {
83
+ const fd = openSync(filePath, 'r');
84
+ try {
85
+ const bytes = Buffer.allocUnsafe(fstatSync(fd).size);
86
+ let offset = 0;
87
+ while (offset < bytes.length) {
88
+ const read = readSync(fd, bytes, offset, Math.min(64 * 1024 * 1024, bytes.length - offset), null);
89
+ if (read <= 0) throw new Error('App archive ended before its recorded size');
90
+ offset += read;
91
+ }
92
+ return bytes;
93
+ } finally { closeSync(fd); }
94
+ }
95
+
54
96
  function sameCanonicalValue(left, right) {
55
97
  return JSON.stringify(sortedValue(left)) === JSON.stringify(sortedValue(right));
56
98
  }
@@ -114,7 +156,7 @@ function collectPayload(payloadPath, os) {
114
156
  const stat = lstatSync(payloadPath);
115
157
  if (stat.isSymbolicLink()) throw new Error('Pack payload must not contain symbolic links');
116
158
  if (stat.isFile()) {
117
- return [{ relative: path.basename(payloadPath), bytes: readFileSync(payloadPath), mode: stat.mode & 0o111 ? 0o755 : 0o644 }];
159
+ return [{ relative: path.basename(payloadPath), bytes: readAppArchiveFile(payloadPath), mode: stat.mode & 0o111 ? 0o755 : 0o644 }];
118
160
  }
119
161
  if (!stat.isDirectory()) throw new Error('Pack payload must be a regular file or directory');
120
162
  const entries = [];
@@ -129,7 +171,7 @@ function collectPayload(payloadPath, os) {
129
171
  } else if (entryStat.isDirectory()) {
130
172
  walk(absolute, relative);
131
173
  } else if (entryStat.isFile()) {
132
- entries.push({ relative, bytes: readFileSync(absolute), mode: entryStat.mode & 0o111 ? 0o755 : 0o644 });
174
+ entries.push({ relative, bytes: readAppArchiveFile(absolute), mode: entryStat.mode & 0o111 ? 0o755 : 0o644 });
133
175
  } else {
134
176
  throw new Error(`Unsupported pack payload entry: ${relative}`);
135
177
  }
@@ -478,14 +520,14 @@ export function packAppTarget(cwd, options = {}) {
478
520
  { name: 'app-info.json', bytes: infoBytes, mode: 0o644 },
479
521
  ...payloadEntries,
480
522
  ]);
481
- const sha256 = createHash('sha256').update(archive).digest('hex');
523
+ const sha256 = hashAppArchive(archive);
482
524
  const outputDir = path.join(targetDir, OUTPUT_DIR);
483
525
  mkdirSync(outputDir, { recursive: true });
484
526
  const stem = `${input.appId}-${input.version}-${input.targetId}`;
485
527
  const artifactPath = path.join(outputDir, `${stem}.nimiapp`);
486
528
  const metadataPath = path.join(outputDir, `${stem}.target.json`);
487
529
  const appInfoPath = path.join(outputDir, `${stem}.app-info.json`);
488
- writeFileSync(artifactPath, archive);
530
+ writeAppArchiveFile(artifactPath, archive);
489
531
  writeFileSync(appInfoPath, infoBytes);
490
532
  const metadata = {
491
533
  format: TARGET_METADATA_FORMAT,
@@ -535,8 +577,8 @@ export function aggregateAppTargetCandidates(cwd, options = {}) {
535
577
  validateNativeExecutionFacts(target.native_trust, target.execution_profile, metadataLabel, target.os);
536
578
  const artifactPath = path.join(outputDir, assetName);
537
579
  if (!existsSync(artifactPath)) throw new Error(`Target artifact is missing: ${target.asset_name}`);
538
- const bytes = readFileSync(artifactPath);
539
- if (bytes.length !== target.size || createHash('sha256').update(bytes).digest('hex') !== target.sha256) throw new Error(`Target artifact changed after pack: ${target.asset_name}`);
580
+ const bytes = readAppArchiveFile(artifactPath);
581
+ if (bytes.length !== target.size || hashAppArchive(bytes) !== target.sha256) throw new Error(`Target artifact changed after pack: ${target.asset_name}`);
540
582
  const entries = readNimiAppArchive(bytes);
541
583
  const infoAsset = target.app_info;
542
584
  if (infoAsset?.asset_name !== `${appId}-${version}-${target.target_id}.app-info.json` || !Number.isSafeInteger(infoAsset.size) || infoAsset.size <= 0 || infoAsset.size > APP_INFO_MAX_BYTES) throw new Error(`Target App info reference is invalid: ${target.target_id}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nimiplatform/app-tools",
3
- "version": "0.12.1",
3
+ "version": "0.12.2",
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.21.0",
22
- "appToolsVersion": "^0.12.1",
22
+ "appToolsVersion": "^0.12.2",
23
23
  "nimicodingVersion": "0.6.5",
24
24
  "aiSdkVersion": "^6.0.85",
25
25
  "kitVersion": "^0.19.0",
@@ -55,7 +55,8 @@
55
55
  "prepack": "pnpm run build",
56
56
  "prepublishOnly": "pnpm run build",
57
57
  "test": "node ../scripts/with-workspace-surfaces.mjs -- pnpm run test:prepared",
58
- "test:prepared": "node scripts/sync-scaffold-versions.mjs --check && node --test test/dev-shell.test.mjs && node --test 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-scaffold.test.mjs"
58
+ "test:large-archive": "node --test test/app-pack-large.test.mjs",
59
+ "test:prepared": "node scripts/sync-scaffold-versions.mjs --check && node --test test/dev-shell.test.mjs && node --test 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"
59
60
  },
60
61
  "files": [
61
62
  "bin",