space-data-module-sdk 0.9.13 → 0.9.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/package.json
CHANGED
|
@@ -45,8 +45,10 @@ const execFileAsync = promisify(execFile);
|
|
|
45
45
|
const require = createRequire(import.meta.url);
|
|
46
46
|
|
|
47
47
|
// Bump when the entry layout or the meaning of a key changes.
|
|
48
|
-
export const COMPILE_CACHE_FORMAT = "sdm-compile-cache-
|
|
49
|
-
|
|
48
|
+
export const COMPILE_CACHE_FORMAT = "sdm-compile-cache-v2";
|
|
49
|
+
// v2 (0.9.14): binary keys and a fixed binary entry layout. A v1 entry (0.9.12)
|
|
50
|
+
// has another magic and is a miss.
|
|
51
|
+
const ENTRY_MAGIC = new TextEncoder().encode("SDMCC2\n");
|
|
50
52
|
const ENTRY_SUFFIX = ".entry";
|
|
51
53
|
const DISABLED_VALUES = new Set(["0", "off", "false", "no", "disabled"]);
|
|
52
54
|
const DEFAULT_MAX_BYTES = 4 * 1024 ** 3;
|
|
@@ -81,16 +83,60 @@ export function sha256Hex(bytes) {
|
|
|
81
83
|
return createHash("sha256").update(bytes).digest("hex");
|
|
82
84
|
}
|
|
83
85
|
|
|
84
|
-
// A stable digest of a
|
|
85
|
-
|
|
86
|
-
|
|
86
|
+
// A stable digest of a value made of null, booleans, numbers, strings, byte
|
|
87
|
+
// arrays, arrays and plain objects. Each field is fed into the sha256 as a type
|
|
88
|
+
// tag and length-prefixed bytes (object keys sorted, undefined members
|
|
89
|
+
// skipped), so distinct values never share an encoding.
|
|
90
|
+
const DIGEST_TAG = Object.freeze({ NULL: 0, FALSE: 1, TRUE: 2, NUMBER: 3, STRING: 4, BYTES: 5, ARRAY: 6, OBJECT: 7 });
|
|
91
|
+
const textEncoder = new TextEncoder();
|
|
92
|
+
|
|
93
|
+
function u32(value) {
|
|
94
|
+
const out = Buffer.alloc(4);
|
|
95
|
+
out.writeUInt32LE(value >>> 0, 0);
|
|
96
|
+
return out;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function feedLengthPrefixed(hash, tag, bytes) {
|
|
100
|
+
hash.update(Uint8Array.of(tag));
|
|
101
|
+
hash.update(u32(bytes.length));
|
|
102
|
+
hash.update(bytes);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function feedDigestValue(hash, value) {
|
|
106
|
+
if (value === null || value === undefined) {
|
|
107
|
+
hash.update(Uint8Array.of(DIGEST_TAG.NULL));
|
|
108
|
+
} else if (typeof value === "boolean") {
|
|
109
|
+
hash.update(Uint8Array.of(value ? DIGEST_TAG.TRUE : DIGEST_TAG.FALSE));
|
|
110
|
+
} else if (typeof value === "number") {
|
|
111
|
+
const bytes = Buffer.alloc(8);
|
|
112
|
+
bytes.writeDoubleLE(value, 0);
|
|
113
|
+
hash.update(Uint8Array.of(DIGEST_TAG.NUMBER));
|
|
114
|
+
hash.update(bytes);
|
|
115
|
+
} else if (typeof value === "string") {
|
|
116
|
+
feedLengthPrefixed(hash, DIGEST_TAG.STRING, textEncoder.encode(value));
|
|
117
|
+
} else if (value instanceof Uint8Array) {
|
|
118
|
+
feedLengthPrefixed(hash, DIGEST_TAG.BYTES, value);
|
|
119
|
+
} else if (Array.isArray(value)) {
|
|
120
|
+
hash.update(Uint8Array.of(DIGEST_TAG.ARRAY));
|
|
121
|
+
hash.update(u32(value.length));
|
|
122
|
+
for (const item of value) feedDigestValue(hash, item);
|
|
123
|
+
} else if (typeof value === "object") {
|
|
124
|
+
const keys = Object.keys(value).filter((key) => value[key] !== undefined).sort();
|
|
125
|
+
hash.update(Uint8Array.of(DIGEST_TAG.OBJECT));
|
|
126
|
+
hash.update(u32(keys.length));
|
|
127
|
+
for (const key of keys) {
|
|
128
|
+
feedLengthPrefixed(hash, DIGEST_TAG.STRING, textEncoder.encode(key));
|
|
129
|
+
feedDigestValue(hash, value[key]);
|
|
130
|
+
}
|
|
131
|
+
} else {
|
|
132
|
+
throw new TypeError(`digestOf cannot encode a ${typeof value}.`);
|
|
133
|
+
}
|
|
87
134
|
}
|
|
88
135
|
|
|
89
|
-
function
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
return `{${keys.map((key) => `${JSON.stringify(key)}:${stableStringify(value[key])}`).join(",")}}`;
|
|
136
|
+
export function digestOf(value) {
|
|
137
|
+
const hash = createHash("sha256");
|
|
138
|
+
feedDigestValue(hash, value);
|
|
139
|
+
return hash.digest("hex");
|
|
94
140
|
}
|
|
95
141
|
|
|
96
142
|
function toBytes(content) {
|
|
@@ -110,38 +156,91 @@ export function digestFiles(files) {
|
|
|
110
156
|
return hash.digest("hex");
|
|
111
157
|
}
|
|
112
158
|
|
|
113
|
-
|
|
159
|
+
// One entry, a fixed little-endian layout:
|
|
160
|
+
//
|
|
161
|
+
// magic "SDMCC2\n"
|
|
162
|
+
// key 32 bytes (the sha256 key the entry is stored under)
|
|
163
|
+
// u32 dependency count, then per dependency:
|
|
164
|
+
// u32 path length, path (utf-8), f64 size, f64 mtimeMs, 32-byte sha256
|
|
165
|
+
// u32 blob count, then per blob (sorted by name):
|
|
166
|
+
// u16 name length, name (utf-8), u32 size, 32-byte sha256
|
|
167
|
+
// blob bytes, in the same order
|
|
168
|
+
function encodeEntry(key, blobs, externalDeps = []) {
|
|
169
|
+
const parts = [ENTRY_MAGIC, Buffer.from(key, "hex"), u32(externalDeps.length)];
|
|
170
|
+
for (const dep of externalDeps) {
|
|
171
|
+
const pathBytes = textEncoder.encode(dep.path);
|
|
172
|
+
const stats = Buffer.alloc(16);
|
|
173
|
+
stats.writeDoubleLE(dep.size, 0);
|
|
174
|
+
stats.writeDoubleLE(dep.mtimeMs, 8);
|
|
175
|
+
parts.push(u32(pathBytes.length), pathBytes, stats, Buffer.from(dep.sha256, "hex"));
|
|
176
|
+
}
|
|
114
177
|
const names = Object.keys(blobs).sort();
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
return Buffer.concat([ENTRY_MAGIC, lengthBytes, headerBytes, ...names.map((name) => blobs[name])]);
|
|
178
|
+
parts.push(u32(names.length));
|
|
179
|
+
for (const name of names) {
|
|
180
|
+
const nameBytes = textEncoder.encode(name);
|
|
181
|
+
const nameLength = Buffer.alloc(2);
|
|
182
|
+
nameLength.writeUInt16LE(nameBytes.length, 0);
|
|
183
|
+
parts.push(nameLength, nameBytes, u32(blobs[name].length), Buffer.from(sha256Hex(blobs[name]), "hex"));
|
|
184
|
+
}
|
|
185
|
+
return Buffer.concat([...parts, ...names.map((name) => blobs[name])]);
|
|
124
186
|
}
|
|
125
187
|
|
|
126
188
|
function decodeEntry(bytes) {
|
|
127
|
-
|
|
189
|
+
const buffer = Buffer.from(bytes.buffer, bytes.byteOffset, bytes.length);
|
|
190
|
+
let offset = 0;
|
|
191
|
+
const need = (count) => {
|
|
192
|
+
if (offset + count > buffer.length) throw new RangeError("truncated cache entry");
|
|
193
|
+
};
|
|
194
|
+
need(ENTRY_MAGIC.length + 32);
|
|
128
195
|
for (let index = 0; index < ENTRY_MAGIC.length; index += 1) {
|
|
129
|
-
if (
|
|
196
|
+
if (buffer[index] !== ENTRY_MAGIC[index]) return null;
|
|
197
|
+
}
|
|
198
|
+
offset = ENTRY_MAGIC.length;
|
|
199
|
+
const key = buffer.subarray(offset, offset + 32).toString("hex");
|
|
200
|
+
offset += 32;
|
|
201
|
+
need(4);
|
|
202
|
+
const depCount = buffer.readUInt32LE(offset);
|
|
203
|
+
offset += 4;
|
|
204
|
+
const externalDeps = [];
|
|
205
|
+
for (let index = 0; index < depCount; index += 1) {
|
|
206
|
+
need(4);
|
|
207
|
+
const pathLength = buffer.readUInt32LE(offset);
|
|
208
|
+
offset += 4;
|
|
209
|
+
need(pathLength + 48);
|
|
210
|
+
const depPath = buffer.subarray(offset, offset + pathLength).toString("utf8");
|
|
211
|
+
offset += pathLength;
|
|
212
|
+
const size = buffer.readDoubleLE(offset);
|
|
213
|
+
const mtimeMs = buffer.readDoubleLE(offset + 8);
|
|
214
|
+
const sha256 = buffer.subarray(offset + 16, offset + 48).toString("hex");
|
|
215
|
+
offset += 48;
|
|
216
|
+
externalDeps.push({ path: depPath, size, mtimeMs, sha256 });
|
|
217
|
+
}
|
|
218
|
+
need(4);
|
|
219
|
+
const blobCount = buffer.readUInt32LE(offset);
|
|
220
|
+
offset += 4;
|
|
221
|
+
const index = [];
|
|
222
|
+
for (let blob = 0; blob < blobCount; blob += 1) {
|
|
223
|
+
need(2);
|
|
224
|
+
const nameLength = buffer.readUInt16LE(offset);
|
|
225
|
+
offset += 2;
|
|
226
|
+
need(nameLength + 36);
|
|
227
|
+
const name = buffer.subarray(offset, offset + nameLength).toString("utf8");
|
|
228
|
+
offset += nameLength;
|
|
229
|
+
const size = buffer.readUInt32LE(offset);
|
|
230
|
+
const sha256 = buffer.subarray(offset + 4, offset + 36).toString("hex");
|
|
231
|
+
offset += 36;
|
|
232
|
+
index.push({ name, size, sha256 });
|
|
130
233
|
}
|
|
131
|
-
const headerLength = new DataView(bytes.buffer, bytes.byteOffset + ENTRY_MAGIC.length, 4).getUint32(0, true);
|
|
132
|
-
const headerStart = ENTRY_MAGIC.length + 4;
|
|
133
|
-
const header = JSON.parse(new TextDecoder().decode(bytes.subarray(headerStart, headerStart + headerLength)));
|
|
134
|
-
if (header?.format !== COMPILE_CACHE_FORMAT || !Array.isArray(header.blobs)) return null;
|
|
135
234
|
const blobs = {};
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
const blob = new Uint8Array(
|
|
139
|
-
if (
|
|
235
|
+
for (const { name, size, sha256 } of index) {
|
|
236
|
+
need(size);
|
|
237
|
+
const blob = new Uint8Array(buffer.subarray(offset, offset + size));
|
|
238
|
+
if (sha256Hex(blob) !== sha256) return null;
|
|
140
239
|
blobs[name] = blob;
|
|
141
240
|
offset += size;
|
|
142
241
|
}
|
|
143
|
-
if (offset !==
|
|
144
|
-
return {
|
|
242
|
+
if (offset !== buffer.length) return null;
|
|
243
|
+
return { key, externalDeps, blobs };
|
|
145
244
|
}
|
|
146
245
|
|
|
147
246
|
// Header dependencies outside the staged build dir: each is recorded with its
|
|
@@ -216,7 +315,7 @@ export class CompileCache {
|
|
|
216
315
|
return path.join(this.entriesDir, key.slice(0, 2), `${key}${ENTRY_SUFFIX}`);
|
|
217
316
|
}
|
|
218
317
|
|
|
219
|
-
/** The stored entry for `key` ({
|
|
318
|
+
/** The stored entry for `key` ({ key, externalDeps, blobs }), or null. */
|
|
220
319
|
async get(key) {
|
|
221
320
|
const file = this.entryPath(key);
|
|
222
321
|
let bytes;
|
|
@@ -232,7 +331,7 @@ export class CompileCache {
|
|
|
232
331
|
} catch {
|
|
233
332
|
entry = null;
|
|
234
333
|
}
|
|
235
|
-
if (!entry || entry.
|
|
334
|
+
if (!entry || entry.key !== key || !(await externalDependenciesUnchanged(entry.externalDeps))) {
|
|
236
335
|
this.stats.misses += 1;
|
|
237
336
|
return null;
|
|
238
337
|
}
|
|
@@ -243,14 +342,17 @@ export class CompileCache {
|
|
|
243
342
|
return entry;
|
|
244
343
|
}
|
|
245
344
|
|
|
246
|
-
/**
|
|
247
|
-
|
|
345
|
+
/**
|
|
346
|
+
* Store `blobs` ({ name: Uint8Array }) under `key`, atomically, with the
|
|
347
|
+
* external header dependencies a lookup re-checks (`{ externalDeps }`).
|
|
348
|
+
*/
|
|
349
|
+
async put(key, blobs, { externalDeps = [] } = {}) {
|
|
248
350
|
const file = this.entryPath(key);
|
|
249
351
|
const dir = path.dirname(file);
|
|
250
352
|
try {
|
|
251
353
|
await mkdir(dir, { recursive: true });
|
|
252
354
|
const temp = path.join(dir, `.${key}.${process.pid}-${randomBytes(6).toString("hex")}.tmp`);
|
|
253
|
-
await writeFile(temp, encodeEntry(
|
|
355
|
+
await writeFile(temp, encodeEntry(key, blobs, externalDeps));
|
|
254
356
|
try {
|
|
255
357
|
await rename(temp, file);
|
|
256
358
|
} catch (error) {
|
|
@@ -339,9 +441,7 @@ let sdkIdentityPromise = null;
|
|
|
339
441
|
export function getSdkCompileIdentity() {
|
|
340
442
|
sdkIdentityPromise ??= (async () => {
|
|
341
443
|
const compilerDir = path.dirname(fileURLToPath(import.meta.url));
|
|
342
|
-
const packageJson =
|
|
343
|
-
await readFile(path.join(compilerDir, "..", "..", "package.json"), "utf8"),
|
|
344
|
-
);
|
|
444
|
+
const packageJson = require(path.join(compilerDir, "..", "..", "package.json"));
|
|
345
445
|
const files = {};
|
|
346
446
|
for (const name of (await readdir(compilerDir)).filter((entry) => entry.endsWith(".js")).sort()) {
|
|
347
447
|
files[name] = await readFile(path.join(compilerDir, name));
|
|
@@ -385,7 +485,7 @@ const toolchainIdentityCache = new Map();
|
|
|
385
485
|
* libraries.
|
|
386
486
|
*/
|
|
387
487
|
export function getNativeClangIdentity({ drivers, toolchainArgs = [], roots = [] }) {
|
|
388
|
-
const cacheKey =
|
|
488
|
+
const cacheKey = ["native-clang", drivers.join("\u0001"), toolchainArgs.join("\u0001"), roots.join("\u0001")].join("\u0000");
|
|
389
489
|
if (!toolchainIdentityCache.has(cacheKey)) {
|
|
390
490
|
toolchainIdentityCache.set(cacheKey, (async () => {
|
|
391
491
|
const driverIds = [];
|
|
@@ -436,7 +536,7 @@ async function describeLibraries(root) {
|
|
|
436
536
|
|
|
437
537
|
/** Identity of a system Emscripten (em++ / emcc on PATH). */
|
|
438
538
|
export function getSystemEmscriptenIdentity(commands) {
|
|
439
|
-
const cacheKey =
|
|
539
|
+
const cacheKey = ["system-emscripten", ...commands].join("\u0000");
|
|
440
540
|
if (!toolchainIdentityCache.has(cacheKey)) {
|
|
441
541
|
toolchainIdentityCache.set(cacheKey, (async () => {
|
|
442
542
|
const ids = [];
|
|
@@ -463,7 +563,7 @@ export function getEmceptionPackageIdentity(extra = "") {
|
|
|
463
563
|
let version = "unknown";
|
|
464
564
|
for (;;) {
|
|
465
565
|
try {
|
|
466
|
-
const pkg =
|
|
566
|
+
const pkg = require(path.join(packageRoot, "package.json"));
|
|
467
567
|
if (pkg?.name === "sdn-emception") {
|
|
468
568
|
version = String(pkg.version);
|
|
469
569
|
break;
|
|
@@ -956,7 +956,7 @@ function normalizeLibraries(libraries) {
|
|
|
956
956
|
if (!LIBRARY_NAME.test(name) || !LIBRARY_VERSION.test(version)) {
|
|
957
957
|
throw new Error(
|
|
958
958
|
`libraries[${index}] needs a name and a version of letters, digits, '.', '_' or '-' ` +
|
|
959
|
-
`(got name ${
|
|
959
|
+
`(got name "${String(library?.name)}", version "${String(library?.version)}").`,
|
|
960
960
|
);
|
|
961
961
|
}
|
|
962
962
|
if (seen.has(name)) {
|
|
@@ -969,12 +969,12 @@ function normalizeLibraries(libraries) {
|
|
|
969
969
|
}
|
|
970
970
|
const language = String(library?.language ?? "c").trim().toLowerCase();
|
|
971
971
|
if (!["c", "c++", "cpp", "cxx"].includes(language)) {
|
|
972
|
-
throw new Error(`libraries[${index}] ("${name}"): language must be "c" or "c++", got ${
|
|
972
|
+
throw new Error(`libraries[${index}] ("${name}"): language must be "c" or "c++", got "${String(library?.language)}".`);
|
|
973
973
|
}
|
|
974
974
|
const defines = (Array.isArray(library?.defines) ? library.defines : []).map((define) => {
|
|
975
975
|
const text = String(define ?? "").trim();
|
|
976
976
|
if (!PREPROCESSOR_DEFINE.test(text)) {
|
|
977
|
-
throw new Error(`libraries[${index}] ("${name}"): define ${
|
|
977
|
+
throw new Error(`libraries[${index}] ("${name}"): define "${String(define)}" is not NAME or NAME=VALUE.`);
|
|
978
978
|
}
|
|
979
979
|
return text;
|
|
980
980
|
});
|
|
@@ -16,6 +16,15 @@
|
|
|
16
16
|
import path from "node:path";
|
|
17
17
|
import { mkdir, rm, writeFile } from "node:fs/promises";
|
|
18
18
|
|
|
19
|
+
import { canonicalBytes } from "../auth/canonicalize.js";
|
|
20
|
+
|
|
21
|
+
function concatNewline(bytes) {
|
|
22
|
+
const out = new Uint8Array(bytes.length + 1);
|
|
23
|
+
out.set(bytes, 0);
|
|
24
|
+
out[bytes.length] = 0x0a;
|
|
25
|
+
return out;
|
|
26
|
+
}
|
|
27
|
+
|
|
19
28
|
/**
|
|
20
29
|
* Write a compilation's guest-link object, its library objects and
|
|
21
30
|
* metadata.json into `dir` (usually `<package>/dist/guest-link`). `metadata`
|
|
@@ -55,6 +64,8 @@ export async function writeGuestLinkFiles(compilation, dir, metadata = {}) {
|
|
|
55
64
|
...(libraryEntries.length > 0 ? { libraries: libraryEntries } : {}),
|
|
56
65
|
...metadata,
|
|
57
66
|
};
|
|
58
|
-
|
|
67
|
+
// The SDK's one canonical writer of this metadata (the guest-link bundle entry
|
|
68
|
+
// encodes the same object the same way): sorted keys, no whitespace.
|
|
69
|
+
await writeFile(path.join(dir, "metadata.json"), concatNewline(canonicalBytes(written)));
|
|
59
70
|
return written;
|
|
60
71
|
}
|