@lark-base-open/base-database-toolchain 1.2.0 → 1.3.1
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/CHANGELOG.md +27 -0
- package/README.md +7 -1
- package/bin/base-database-toolchain.mjs +13 -1
- package/dist/app-builder/core.d.mts +4 -0
- package/dist/app-builder/core.mjs +736 -110
- package/dist/app-builder/package.json +1 -1
- package/dist/data/config-schema.json +95 -0
- package/dist/dependency-policy.d.ts +13 -0
- package/dist/dependency-policy.js +187 -0
- package/dist/dev-worker.mjs +27 -21
- package/dist/function-description.d.ts +12 -0
- package/dist/function-description.js +38 -0
- package/dist/function-params.d.ts +50 -0
- package/dist/function-params.js +157 -0
- package/dist/function-schema.d.ts +35 -0
- package/dist/function-schema.js +490 -0
- package/dist/go-adapter.d.mts +1 -1
- package/dist/go-adapter.mjs +299 -225
- package/dist/mutation-schema.d.ts +151 -0
- package/dist/mutation-schema.js +158 -0
- package/dist/mutation.d.ts +12 -0
- package/dist/mutation.js +145 -0
- package/dist/npm-policy.d.ts +16 -0
- package/dist/npm-policy.js +193 -0
- package/dist/page-link-guard.js +3 -2
- package/dist/project-contract.d.ts +2 -0
- package/dist/project-contract.js +38 -0
- package/dist/project-layout.d.ts +15 -0
- package/dist/project-layout.js +89 -0
- package/dist/source-archive-inputs.d.ts +13 -0
- package/dist/source-archive-inputs.js +105 -0
- package/npm-shrinkwrap.json +13 -3
- package/package.json +15 -2
|
@@ -21,8 +21,7 @@ export function defineFunctionCall(ast, definitionName = 'defineFunction') {
|
|
|
21
21
|
namespaces.add(specifier.local.name);
|
|
22
22
|
if (specifier.type === 'ImportSpecifier' &&
|
|
23
23
|
specifier.importKind !== 'type' &&
|
|
24
|
-
(specifier.imported.name ?? specifier.imported.value) ===
|
|
25
|
-
definitionName)
|
|
24
|
+
(specifier.imported.name ?? specifier.imported.value) === definitionName)
|
|
26
25
|
bindings.add(specifier.local.name);
|
|
27
26
|
}
|
|
28
27
|
}
|
|
@@ -223,7 +222,9 @@ export function ids(root, server) {
|
|
|
223
222
|
}
|
|
224
223
|
export function durableIds(root, server) {
|
|
225
224
|
const durable = functionInventory(join(root, server, 'durable'), [
|
|
226
|
-
'index.ts',
|
|
225
|
+
'index.ts',
|
|
226
|
+
'contract.ts',
|
|
227
|
+
'meta.json',
|
|
227
228
|
]);
|
|
228
229
|
const functions = new Set(ids(root, server));
|
|
229
230
|
for (const id of durable)
|
|
@@ -254,16 +255,308 @@ export function run(command, args, cwd) {
|
|
|
254
255
|
fail(result.error?.message ??
|
|
255
256
|
`${command} failed (${result.status})\n${result.stdout}\n${result.stderr}`);
|
|
256
257
|
}
|
|
257
|
-
//
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
258
|
+
// The shared registry is a build detail, independent of the v1 artifact ABI.
|
|
259
|
+
const SHARED_DEPENDENCY_POLICY = 'controlled-cjs@2';
|
|
260
|
+
const SHARED_DEPENDENCY_DIRECTORY = 'shared/dependencies';
|
|
261
|
+
const portable = (value) => value.split(sep).join('/');
|
|
262
|
+
const sharedPath = (id) => `${SHARED_DEPENDENCY_DIRECTORY}/${id}.cjs`;
|
|
263
|
+
const sharedNamespace = 'platform-shared-dependency';
|
|
264
|
+
const facadeSpecifier = 'platform-shared-facade';
|
|
265
|
+
const sharedPackageName = (specifier) => /^zod(?:\/|$)/.test(specifier) && !/^zod\/v3(?:\/|$)/.test(specifier)
|
|
266
|
+
? 'zod'
|
|
267
|
+
: /^@lark-base-open\/base-database-app-sdk\/(server|shared)$/.test(specifier)
|
|
268
|
+
? '@lark-base-open/base-database-app-sdk'
|
|
269
|
+
: undefined;
|
|
270
|
+
export function isSupportedSharedVersion(packageName, version) {
|
|
271
|
+
if (typeof version !== 'string')
|
|
272
|
+
return false;
|
|
273
|
+
const match = /^(\d+)\.(\d+)\.(\d+)$/.exec(version.trim());
|
|
274
|
+
if (!match)
|
|
275
|
+
return false;
|
|
276
|
+
const major = Number(match[1]);
|
|
277
|
+
const minor = Number(match[2]);
|
|
278
|
+
if (packageName === 'zod')
|
|
279
|
+
return major === 4;
|
|
280
|
+
if (packageName === '@lark-base-open/base-database-app-sdk')
|
|
281
|
+
return major === 0 && (minor === 8 || minor === 9);
|
|
282
|
+
return false;
|
|
283
|
+
}
|
|
284
|
+
function canonicalInput(root, file) {
|
|
285
|
+
const path = resolve(root, file);
|
|
286
|
+
return existsSync(path) ? realpathSync(path) : path;
|
|
287
|
+
}
|
|
288
|
+
function hashInputs(root, files) {
|
|
289
|
+
const result = {};
|
|
290
|
+
for (const name of files) {
|
|
291
|
+
if (!existsSync(resolve(root, name)))
|
|
292
|
+
continue;
|
|
293
|
+
const file = realpathSync(resolve(root, name));
|
|
294
|
+
if (!lstatSync(file).isFile())
|
|
295
|
+
continue;
|
|
296
|
+
result[portable(relative(root, file))] = hash(readFileSync(file));
|
|
297
|
+
}
|
|
298
|
+
return result;
|
|
299
|
+
}
|
|
300
|
+
function validHashes(value) {
|
|
301
|
+
return (value &&
|
|
302
|
+
typeof value === 'object' &&
|
|
303
|
+
!Array.isArray(value) &&
|
|
304
|
+
Object.entries(value).every(([path, digest]) => path && !path.includes('\\') && /^[a-f0-9]{64}$/.test(digest)));
|
|
305
|
+
}
|
|
306
|
+
function hashesMatch(root, value) {
|
|
307
|
+
if (!validHashes(value))
|
|
308
|
+
return false;
|
|
309
|
+
try {
|
|
310
|
+
return Object.entries(value).every(([path, digest]) => hash(readFileSync(resolve(root, path))) === digest);
|
|
311
|
+
}
|
|
312
|
+
catch {
|
|
313
|
+
return false;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
function sharedOwner(file, name, excluded) {
|
|
317
|
+
for (let dir = dirname(realpathSync(file));; dir = dirname(dir)) {
|
|
318
|
+
if (excluded.has(dir))
|
|
319
|
+
return undefined;
|
|
320
|
+
const manifest = join(dir, 'package.json');
|
|
321
|
+
if (existsSync(manifest)) {
|
|
322
|
+
const pkg = readJSON(manifest);
|
|
323
|
+
if (pkg.name && pkg.name !== name)
|
|
324
|
+
return undefined;
|
|
325
|
+
if (pkg.name === name)
|
|
326
|
+
return isSupportedSharedVersion(name, pkg.version)
|
|
327
|
+
? manifest
|
|
328
|
+
: undefined;
|
|
329
|
+
}
|
|
330
|
+
if (dirname(dir) === dir)
|
|
331
|
+
return undefined;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
function createSharedDependencies(root, output, enabled, inspect) {
|
|
335
|
+
const canonicalRoot = realpathSync(root), server = dirname(output);
|
|
336
|
+
const excluded = new Set([
|
|
337
|
+
canonicalRoot,
|
|
338
|
+
existsSync(server) ? realpathSync(server) : server,
|
|
339
|
+
]);
|
|
340
|
+
const active = new Map(), inspections = new Map();
|
|
341
|
+
async function discover(record) {
|
|
342
|
+
if (!record ||
|
|
343
|
+
typeof record.path !== 'string' ||
|
|
344
|
+
!record.path ||
|
|
345
|
+
record.path.includes('\\') ||
|
|
346
|
+
isAbsolute(record.path) ||
|
|
347
|
+
!['zod', '@lark-base-open/base-database-app-sdk'].includes(record.packageName))
|
|
348
|
+
fail('Invalid shared dependency cache');
|
|
349
|
+
const file = realpathSync(resolve(canonicalRoot, record.path));
|
|
350
|
+
const manifest = sharedOwner(file, record.packageName, excluded);
|
|
351
|
+
if (!manifest)
|
|
352
|
+
fail('Invalid shared dependency owner');
|
|
353
|
+
const path = portable(relative(canonicalRoot, file)), id = hash(path);
|
|
354
|
+
if (!inspections.has(id))
|
|
355
|
+
inspections.set(id, (async () => ({
|
|
356
|
+
id,
|
|
357
|
+
path,
|
|
358
|
+
file,
|
|
359
|
+
packageName: record.packageName,
|
|
360
|
+
manifest,
|
|
361
|
+
...(await inspect(file, manifest)),
|
|
362
|
+
}))());
|
|
363
|
+
return inspections.get(id);
|
|
364
|
+
}
|
|
365
|
+
return {
|
|
366
|
+
forFunction(kind, id) {
|
|
367
|
+
const candidates = new Map(), selected = new Map();
|
|
368
|
+
const target = join(output, kind, id);
|
|
369
|
+
const external = (entry) => portable(relative(target, join(output, sharedPath(entry.id))));
|
|
370
|
+
const merge = (entries, key) => Object.assign({}, ...[...entries.values()].map((entry) => entry[key]));
|
|
371
|
+
const unique = (entries, key) => [
|
|
372
|
+
...new Set([...entries.values()].flatMap((entry) => entry[key])),
|
|
373
|
+
].sort();
|
|
374
|
+
return {
|
|
375
|
+
get records() {
|
|
376
|
+
return [...selected.values()].map(({ path, packageName }) => ({
|
|
377
|
+
path,
|
|
378
|
+
packageName,
|
|
379
|
+
}));
|
|
380
|
+
},
|
|
381
|
+
get files() {
|
|
382
|
+
return [...selected.values()]
|
|
383
|
+
.map((entry) => sharedPath(entry.id))
|
|
384
|
+
.sort();
|
|
385
|
+
},
|
|
386
|
+
get inputHashes() {
|
|
387
|
+
return merge(selected, 'inputHashes');
|
|
388
|
+
},
|
|
389
|
+
get resolutionInputHashes() {
|
|
390
|
+
return merge(candidates, 'inputHashes');
|
|
391
|
+
},
|
|
392
|
+
get externalModules() {
|
|
393
|
+
return unique(selected, 'externalModules');
|
|
394
|
+
},
|
|
395
|
+
get stubbedModules() {
|
|
396
|
+
return unique(selected, 'stubbedModules');
|
|
397
|
+
},
|
|
398
|
+
get resolutionStubbedModules() {
|
|
399
|
+
return unique(candidates, 'stubbedModules');
|
|
400
|
+
},
|
|
401
|
+
async restore(records, allowedPaths) {
|
|
402
|
+
if (!Array.isArray(records))
|
|
403
|
+
fail('Invalid shared dependency cache');
|
|
404
|
+
const allowed = allowedPaths ? new Set(allowedPaths) : undefined;
|
|
405
|
+
for (const record of records) {
|
|
406
|
+
if (allowed && !allowed.has(record?.path))
|
|
407
|
+
fail('Invalid shared dependency cache');
|
|
408
|
+
const entry = await discover(record);
|
|
409
|
+
if (allowed && !allowed.has(entry.path))
|
|
410
|
+
fail('Invalid shared dependency cache');
|
|
411
|
+
candidates.set(entry.id, entry);
|
|
412
|
+
selected.set(entry.id, entry);
|
|
413
|
+
}
|
|
414
|
+
},
|
|
415
|
+
retain(paths) {
|
|
416
|
+
for (const [key, entry] of selected)
|
|
417
|
+
if (!paths.includes(external(entry)))
|
|
418
|
+
selected.delete(key);
|
|
419
|
+
},
|
|
420
|
+
activate() {
|
|
421
|
+
for (const [key, entry] of selected)
|
|
422
|
+
active.set(key, entry);
|
|
423
|
+
},
|
|
424
|
+
allows(file, item) {
|
|
425
|
+
return (file.startsWith(`${sharedNamespace}:`) &&
|
|
426
|
+
[...candidates.values()].some((entry) => item.path === external(entry)));
|
|
427
|
+
},
|
|
428
|
+
plugin: {
|
|
429
|
+
name: 'controlled-shared-dependencies',
|
|
430
|
+
setup(b) {
|
|
431
|
+
if (!enabled)
|
|
432
|
+
return;
|
|
433
|
+
b.onResolve({
|
|
434
|
+
filter: /^(zod(?:\/|$)|@lark-base-open\/base-database-app-sdk\/(server|shared)$)/,
|
|
435
|
+
}, async (args) => {
|
|
436
|
+
if (args.pluginData?.sharedResolution ||
|
|
437
|
+
args.pluginData?.optionalResolution ||
|
|
438
|
+
args.kind === 'require-resolve')
|
|
439
|
+
return;
|
|
440
|
+
const result = await b.resolve(args.path, {
|
|
441
|
+
importer: args.importer,
|
|
442
|
+
resolveDir: args.resolveDir,
|
|
443
|
+
kind: args.kind,
|
|
444
|
+
namespace: args.namespace,
|
|
445
|
+
pluginData: { sharedResolution: true },
|
|
446
|
+
});
|
|
447
|
+
if (result.errors.length ||
|
|
448
|
+
!result.path ||
|
|
449
|
+
result.external ||
|
|
450
|
+
!/\.[cm]?[jt]sx?$/.test(result.path))
|
|
451
|
+
return;
|
|
452
|
+
const name = sharedPackageName(args.path);
|
|
453
|
+
if (!name || !sharedOwner(result.path, name, excluded))
|
|
454
|
+
return;
|
|
455
|
+
const entry = await discover({
|
|
456
|
+
path: portable(relative(canonicalRoot, realpathSync(result.path))),
|
|
457
|
+
packageName: name,
|
|
458
|
+
});
|
|
459
|
+
// Export-less ESM scripts have no shared API and the existing
|
|
460
|
+
// optional resolver may classify their dynamic-import namespace
|
|
461
|
+
// differently. Leave these tiny side-effect entries inline.
|
|
462
|
+
if (entry.format === 'esm' && !entry.exportNames.length)
|
|
463
|
+
return;
|
|
464
|
+
candidates.set(entry.id, entry);
|
|
465
|
+
selected.set(entry.id, entry);
|
|
466
|
+
const mode = entry.format === 'esm' &&
|
|
467
|
+
['import-statement', 'dynamic-import'].includes(args.kind)
|
|
468
|
+
? 'esm'
|
|
469
|
+
: 'cjs';
|
|
470
|
+
return {
|
|
471
|
+
path: `${entry.id}:${mode}`,
|
|
472
|
+
namespace: sharedNamespace,
|
|
473
|
+
sideEffects: result.sideEffects,
|
|
474
|
+
};
|
|
475
|
+
});
|
|
476
|
+
b.onResolve({
|
|
477
|
+
filter: /^platform-shared-facade$/,
|
|
478
|
+
namespace: sharedNamespace,
|
|
479
|
+
}, (args) => {
|
|
480
|
+
const entry = candidates.get(args.importer.split(':')[0]);
|
|
481
|
+
if (!entry)
|
|
482
|
+
fail('Invalid generated shared dependency reference');
|
|
483
|
+
return { path: external(entry), external: true };
|
|
484
|
+
});
|
|
485
|
+
b.onLoad({ filter: /.*/, namespace: sharedNamespace }, (args) => {
|
|
486
|
+
const [id, mode] = args.path.split(':'), entry = candidates.get(id);
|
|
487
|
+
if (!entry)
|
|
488
|
+
fail('Invalid shared dependency bridge');
|
|
489
|
+
// A virtual .js ESM bridge has no Node-mode package boundary. Its
|
|
490
|
+
// static re-exports preserve getters and unwrap __toCommonJS once.
|
|
491
|
+
// CJS inputs/require calls retain the caller's normal interop.
|
|
492
|
+
const contents = mode === 'esm'
|
|
493
|
+
? entry.exportNames.length
|
|
494
|
+
? `export {${entry.exportNames.map((name) => JSON.stringify(name)).join(',')}} from ${JSON.stringify(facadeSpecifier)};`
|
|
495
|
+
: `import ${JSON.stringify(facadeSpecifier)}; export {};`
|
|
496
|
+
: `module.exports = require(${JSON.stringify(facadeSpecifier)});`;
|
|
497
|
+
return { contents, loader: 'js', resolveDir: canonicalRoot };
|
|
498
|
+
});
|
|
499
|
+
},
|
|
500
|
+
},
|
|
501
|
+
};
|
|
502
|
+
},
|
|
503
|
+
source() {
|
|
504
|
+
return [...active.values()]
|
|
505
|
+
.sort((a, b) => (a.id < b.id ? -1 : a.id > b.id ? 1 : 0))
|
|
506
|
+
.map((entry) => `export function load_${entry.id}() { return require(${JSON.stringify(entry.file)}); }`)
|
|
507
|
+
.join('\n');
|
|
508
|
+
},
|
|
509
|
+
get size() {
|
|
510
|
+
return active.size;
|
|
511
|
+
},
|
|
512
|
+
get manifests() {
|
|
513
|
+
return [...new Set([...active.values()].map((entry) => entry.manifest))];
|
|
514
|
+
},
|
|
515
|
+
writeFacades() {
|
|
516
|
+
for (const entry of active.values()) {
|
|
517
|
+
const file = join(output, sharedPath(entry.id));
|
|
518
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
519
|
+
writeFileSync(file, `module.exports = require('./runtime.cjs').load_${entry.id}();\n`);
|
|
520
|
+
}
|
|
521
|
+
},
|
|
522
|
+
resetOutput() {
|
|
523
|
+
rmSync(join(output, SHARED_DEPENDENCY_DIRECTORY), {
|
|
524
|
+
recursive: true,
|
|
525
|
+
force: true,
|
|
526
|
+
});
|
|
527
|
+
},
|
|
528
|
+
runtime: join(output, SHARED_DEPENDENCY_DIRECTORY, 'runtime.cjs'),
|
|
529
|
+
};
|
|
530
|
+
}
|
|
531
|
+
export function verifySharedModules(manifest) {
|
|
532
|
+
if (manifest.dependencySharing !== undefined &&
|
|
533
|
+
!['none', SHARED_DEPENDENCY_POLICY].includes(manifest.dependencySharing))
|
|
534
|
+
fail('Unknown shared dependency policy');
|
|
535
|
+
for (const fn of [...manifest.functions, ...(manifest.durable ?? [])]) {
|
|
536
|
+
if (fn.sharedModules === undefined)
|
|
537
|
+
continue; // Existing v1 artifacts.
|
|
538
|
+
if ((fn.sharedModules?.length &&
|
|
539
|
+
manifest.dependencySharing !== SHARED_DEPENDENCY_POLICY) ||
|
|
540
|
+
!Array.isArray(fn.sharedModules) ||
|
|
541
|
+
new Set(fn.sharedModules).size !== fn.sharedModules.length ||
|
|
542
|
+
fn.sharedModules.some((path) => typeof path !== 'string' ||
|
|
543
|
+
!/^shared\/dependencies\/[a-f0-9]{64}\.cjs$/.test(path) ||
|
|
544
|
+
!manifest.commonFiles.some((file) => file.path === path)) ||
|
|
545
|
+
(fn.sharedModules.length &&
|
|
546
|
+
!manifest.commonFiles.some((file) => file.path === `${SHARED_DEPENDENCY_DIRECTORY}/runtime.cjs`)))
|
|
547
|
+
throw Error('Invalid shared dependency manifest');
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
function optionalDependencyResolver(root, server, owned = []) {
|
|
551
|
+
const owners = new Set(owned.map((file) => realpathSync(file)));
|
|
261
552
|
const missing = new Set();
|
|
262
553
|
const approved = new Set();
|
|
263
554
|
const stubbed = new Set();
|
|
264
555
|
const canonical = (file) => realpathSync(file);
|
|
265
556
|
const excluded = new Set([canonical(root), canonical(server)]);
|
|
266
|
-
const packageName = (name) => /^(?:@[a-zA-Z0-9_.-]+\/)?[a-zA-Z0-9_][a-zA-Z0-9_.-]*(?:\/|$)
|
|
557
|
+
const packageName = (name) => /^(?:@[a-zA-Z0-9_.-]+\/)?[a-zA-Z0-9_][a-zA-Z0-9_.-]*(?:\/|$)/
|
|
558
|
+
.exec(name)?.[0]
|
|
559
|
+
.replace(/\/$/, '');
|
|
267
560
|
const key = (importer, name, kind) => JSON.stringify([canonical(importer), name, kind]);
|
|
268
561
|
function owner(file) {
|
|
269
562
|
let dir = dirname(canonical(file));
|
|
@@ -298,19 +591,27 @@ function optionalDependencyResolver(root, server) {
|
|
|
298
591
|
for (const dir of req.resolve.paths(name) ?? []) {
|
|
299
592
|
const candidate = join(dir, name);
|
|
300
593
|
// Include broken symlinks: an installed but broken package is not absent.
|
|
301
|
-
if (existsSync(candidate) ||
|
|
594
|
+
if (existsSync(candidate) ||
|
|
595
|
+
lstatSync(candidate, { throwIfNoEntry: false }))
|
|
302
596
|
return candidate;
|
|
303
597
|
}
|
|
304
598
|
return null;
|
|
305
599
|
}
|
|
306
600
|
return {
|
|
307
601
|
stubbed,
|
|
308
|
-
|
|
602
|
+
get manifests() {
|
|
603
|
+
return [...owners]
|
|
604
|
+
.map((dir) => join(dir, 'package.json'))
|
|
605
|
+
.filter(existsSync);
|
|
606
|
+
},
|
|
607
|
+
approve(metafile, allowsShared = () => false) {
|
|
309
608
|
for (const [file, input] of Object.entries(metafile.inputs)) {
|
|
310
609
|
if (!existsSync(resolve(root, file)))
|
|
311
610
|
continue;
|
|
312
611
|
for (const item of input.imports) {
|
|
313
|
-
if (!item.external ||
|
|
612
|
+
if (!item.external ||
|
|
613
|
+
isBuiltin(item.path) ||
|
|
614
|
+
allowsShared(file, item))
|
|
314
615
|
continue;
|
|
315
616
|
const k = key(resolve(root, file), item.path, item.kind);
|
|
316
617
|
if (!missing.has(k) || !owners.has(owner(resolve(root, file))))
|
|
@@ -324,9 +625,13 @@ function optionalDependencyResolver(root, server) {
|
|
|
324
625
|
},
|
|
325
626
|
setup(b) {
|
|
326
627
|
b.onResolve({ filter: /.*/ }, async (args) => {
|
|
327
|
-
if (args.pluginData?.optionalResolution ||
|
|
628
|
+
if (args.pluginData?.optionalResolution ||
|
|
629
|
+
args.pluginData?.sharedResolution ||
|
|
630
|
+
args.namespace === 'optional-missing')
|
|
328
631
|
return;
|
|
329
|
-
if (args.importer &&
|
|
632
|
+
if (args.importer &&
|
|
633
|
+
existsSync(args.importer) &&
|
|
634
|
+
approved.has(key(args.importer, args.path, args.kind))) {
|
|
330
635
|
stubbed.add(args.path);
|
|
331
636
|
return { path: args.path, namespace: 'optional-missing' };
|
|
332
637
|
}
|
|
@@ -334,8 +639,10 @@ function optionalDependencyResolver(root, server) {
|
|
|
334
639
|
if (!name || isBuiltin(args.path) || !args.importer)
|
|
335
640
|
return;
|
|
336
641
|
const result = await b.resolve(args.path, {
|
|
337
|
-
importer: args.importer,
|
|
338
|
-
|
|
642
|
+
importer: args.importer,
|
|
643
|
+
resolveDir: args.resolveDir,
|
|
644
|
+
kind: args.kind,
|
|
645
|
+
namespace: args.namespace,
|
|
339
646
|
pluginData: { optionalResolution: true },
|
|
340
647
|
});
|
|
341
648
|
const installed = installation(name, args.importer);
|
|
@@ -344,13 +651,19 @@ function optionalDependencyResolver(root, server) {
|
|
|
344
651
|
const dir = canonical(installed);
|
|
345
652
|
const file = canonical(result.path);
|
|
346
653
|
const rel = relative(dir, file);
|
|
347
|
-
if (!excluded.has(dir) &&
|
|
654
|
+
if (!excluded.has(dir) &&
|
|
655
|
+
rel &&
|
|
656
|
+
rel !== '..' &&
|
|
657
|
+
!rel.startsWith(`..${sep}`) &&
|
|
658
|
+
!isAbsolute(rel))
|
|
348
659
|
owners.add(dir);
|
|
349
660
|
}
|
|
350
661
|
// Use esbuild's original result; no custom resolution or externals.
|
|
351
662
|
return result;
|
|
352
663
|
}
|
|
353
|
-
if (result.errors.length &&
|
|
664
|
+
if (result.errors.length &&
|
|
665
|
+
!installed &&
|
|
666
|
+
existsSync(args.importer) &&
|
|
354
667
|
['require-call', 'dynamic-import'].includes(args.kind))
|
|
355
668
|
missing.add(key(args.importer, args.path, args.kind));
|
|
356
669
|
// Let esbuild classify failure itself, including its try/import.catch
|
|
@@ -365,10 +678,61 @@ function optionalDependencyResolver(root, server) {
|
|
|
365
678
|
},
|
|
366
679
|
};
|
|
367
680
|
}
|
|
681
|
+
// Enabled by the platform scripts; embedded CLI builds stay quiet by default.
|
|
682
|
+
export function startBuildStage(lane, stage) {
|
|
683
|
+
if (process.env.SCB_BUILD_STAGE_TIMING !== '1')
|
|
684
|
+
return () => { };
|
|
685
|
+
const startedMs = Date.now(), started = performance.now();
|
|
686
|
+
let finished = false;
|
|
687
|
+
return (success, details = {}) => {
|
|
688
|
+
if (finished)
|
|
689
|
+
return;
|
|
690
|
+
finished = true;
|
|
691
|
+
process.stdout.write('SCB_BUILD_STAGE_TIMING:' +
|
|
692
|
+
JSON.stringify({
|
|
693
|
+
lane,
|
|
694
|
+
stage,
|
|
695
|
+
startedMs,
|
|
696
|
+
finishedMs: Date.now(),
|
|
697
|
+
durationMs: Math.round((performance.now() - started) * 1000) / 1000,
|
|
698
|
+
success,
|
|
699
|
+
...details,
|
|
700
|
+
}) +
|
|
701
|
+
'\n');
|
|
702
|
+
};
|
|
703
|
+
}
|
|
704
|
+
// Synchronous checks only; async build stages explicitly finish after awaiting.
|
|
705
|
+
export function measureBuildStage(lane, stage, operation) {
|
|
706
|
+
const finish = startBuildStage(lane, stage);
|
|
707
|
+
try {
|
|
708
|
+
const value = operation();
|
|
709
|
+
finish(true);
|
|
710
|
+
return value;
|
|
711
|
+
}
|
|
712
|
+
catch (error) {
|
|
713
|
+
finish(false);
|
|
714
|
+
throw error;
|
|
715
|
+
}
|
|
716
|
+
}
|
|
368
717
|
export async function buildProject(root, options = {}) {
|
|
369
|
-
root = resolve(root);
|
|
718
|
+
root = realpathSync(resolve(root));
|
|
370
719
|
const info = layout(root), server = join(root, info.server), output = join(server, 'dist');
|
|
371
720
|
let stage = 'typecheck', functionId = null;
|
|
721
|
+
let finishStage = startBuildStage('faas', 'typecheck');
|
|
722
|
+
const timingEnabled = process.env.SCB_BUILD_STAGE_TIMING === '1';
|
|
723
|
+
const dependencySharing = (options.sharedDependencies ??
|
|
724
|
+
(options.env ?? process.env).SCB_SHARED_DEPENDENCIES !== '0')
|
|
725
|
+
? SHARED_DEPENDENCY_POLICY
|
|
726
|
+
: 'none';
|
|
727
|
+
let inspectSharedEntry;
|
|
728
|
+
const shared = createSharedDependencies(root, output, dependencySharing !== 'none', (...args) => inspectSharedEntry(...args));
|
|
729
|
+
let staticValidationMs = 0, staticValidationCalls = 0, staticValidationCacheHits = 0;
|
|
730
|
+
// These AST checks depend only on file contents. Reuse only successful checks
|
|
731
|
+
// within this invocation, and hash fresh bytes on every load to detect edits.
|
|
732
|
+
const staticValidationCacheEnabled = options.staticValidationCache ??
|
|
733
|
+
process.env.SCB_STATIC_VALIDATION_CACHE !== '0';
|
|
734
|
+
const staticValidationCache = new Map();
|
|
735
|
+
const staticValidationFiles = new Set();
|
|
372
736
|
try {
|
|
373
737
|
if (existsSync(server) && lstatSync(server).isSymbolicLink())
|
|
374
738
|
fail('Server directory is a symlink');
|
|
@@ -386,29 +750,65 @@ export async function buildProject(root, options = {}) {
|
|
|
386
750
|
join(server, 'tsconfig.json'),
|
|
387
751
|
], root);
|
|
388
752
|
}
|
|
753
|
+
finishStage(true);
|
|
389
754
|
stage = 'bundle';
|
|
755
|
+
finishStage = startBuildStage('faas', 'bundle');
|
|
390
756
|
if (existsSync(output) && lstatSync(output).isSymbolicLink())
|
|
391
757
|
fail('Output directory is a symlink');
|
|
392
758
|
const cacheFile = join(root, '.base-database-cli/state/platform-builder-cache.json');
|
|
393
759
|
let previous;
|
|
394
760
|
if (options.incremental &&
|
|
395
761
|
existsSync(join(output, 'build-manifest.json'))) {
|
|
396
|
-
|
|
397
|
-
|
|
762
|
+
// Keep the existing public-manifest traversal guard fail-closed. Ordinary
|
|
763
|
+
// malformed cache data is still a cache miss, handled below.
|
|
764
|
+
let cachedManifest;
|
|
765
|
+
try {
|
|
766
|
+
cachedManifest = readJSON(join(output, 'build-manifest.json'));
|
|
767
|
+
}
|
|
768
|
+
catch { }
|
|
769
|
+
if (Array.isArray(cachedManifest?.functions) &&
|
|
770
|
+
cachedManifest.functions.some((fn) => typeof fn?.id !== 'string' || !/^[A-Za-z0-9_-]{1,64}$/.test(fn.id)))
|
|
398
771
|
fail('Invalid cached Function ID');
|
|
399
772
|
try {
|
|
400
773
|
previous = readBuildCache(output);
|
|
401
774
|
const cache = readJSON(cacheFile);
|
|
402
|
-
if (cache.
|
|
403
|
-
|
|
775
|
+
if (cache.version !== 2 ||
|
|
776
|
+
previous.buildCacheDigest !==
|
|
777
|
+
hash(JSON.stringify({
|
|
778
|
+
version: cache.version,
|
|
779
|
+
functions: cache.functions,
|
|
780
|
+
})) ||
|
|
781
|
+
cache.manifestDigest !==
|
|
782
|
+
hash(readFileSync(join(output, 'build-manifest.json'))))
|
|
404
783
|
fail('Build cache mismatch');
|
|
405
|
-
previous.functions = previous.functions.map((fn) =>
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
784
|
+
previous.functions = previous.functions.map((fn) => {
|
|
785
|
+
const state = cache.functions?.find((item) => item.id === fn.id);
|
|
786
|
+
if (!state ||
|
|
787
|
+
!validHashes(state.ownInputHashes) ||
|
|
788
|
+
!validHashes(state.sharedInputHashes) ||
|
|
789
|
+
!validHashes(state.resolutionInputHashes) ||
|
|
790
|
+
!Array.isArray(state.ownExternalModules) ||
|
|
791
|
+
state.ownExternalModules.some((p) => !isBuiltin(p)) ||
|
|
792
|
+
!Array.isArray(state.ownStubbedModules) ||
|
|
793
|
+
!Array.isArray(state.resolutionStubbedModules) ||
|
|
794
|
+
!Array.isArray(state.sharedEntries))
|
|
795
|
+
fail('Invalid function build cache');
|
|
796
|
+
return {
|
|
797
|
+
...fn,
|
|
798
|
+
ownInputHashes: state.ownInputHashes,
|
|
799
|
+
ownExternalModules: state.ownExternalModules,
|
|
800
|
+
ownStubbedModules: state.ownStubbedModules,
|
|
801
|
+
sharedInputHashes: state.sharedInputHashes,
|
|
802
|
+
resolutionInputHashes: state.resolutionInputHashes,
|
|
803
|
+
resolutionStubbedModules: state.resolutionStubbedModules,
|
|
804
|
+
sharedEntries: state.sharedEntries,
|
|
805
|
+
externalModules: [...state.ownExternalModules],
|
|
806
|
+
stubbedModules: [...state.ownStubbedModules],
|
|
807
|
+
};
|
|
808
|
+
});
|
|
410
809
|
if (previous.builderVersion !== BUILDER_VERSION ||
|
|
411
|
-
previous.esbuildVersion !== esbuildVersion
|
|
810
|
+
previous.esbuildVersion !== esbuildVersion ||
|
|
811
|
+
previous.dependencySharing !== dependencySharing)
|
|
412
812
|
previous = undefined;
|
|
413
813
|
}
|
|
414
814
|
catch {
|
|
@@ -431,28 +831,182 @@ export async function buildProject(root, options = {}) {
|
|
|
431
831
|
const entries = [];
|
|
432
832
|
const sourceHashes = {};
|
|
433
833
|
let rebuilt = 0;
|
|
834
|
+
const contractPlugin = {
|
|
835
|
+
name: 'platform-contract',
|
|
836
|
+
setup(b) {
|
|
837
|
+
b.onResolve({
|
|
838
|
+
filter: /^(react(?:-dom)?(?:\/|$)|@lark-base-open\/base-database-app-sdk\/(page|components|testing|vite)(?:\/|$))/,
|
|
839
|
+
}, (args) => {
|
|
840
|
+
fail(`FAAS_UI_DEPENDENCY_UNSUPPORTED: ${args.path}`);
|
|
841
|
+
});
|
|
842
|
+
b.onResolve({ filter: /\.node$/ }, (args) => {
|
|
843
|
+
fail(`DEPENDENCY_RUNTIME_UNSUPPORTED: ${args.path}`);
|
|
844
|
+
});
|
|
845
|
+
b.onLoad({ filter: /\.node$/ }, (args) => {
|
|
846
|
+
fail(`DEPENDENCY_RUNTIME_UNSUPPORTED: ${args.path}`);
|
|
847
|
+
});
|
|
848
|
+
b.onLoad({ filter: /\.[cm]?[jt]sx?$/, namespace: 'file' }, (args) => {
|
|
849
|
+
const started = timingEnabled ? performance.now() : 0;
|
|
850
|
+
const source = readFileSync(args.path, 'utf8');
|
|
851
|
+
const digest = staticValidationCacheEnabled ? hash(source) : null;
|
|
852
|
+
if (staticValidationCacheEnabled &&
|
|
853
|
+
staticValidationCache.get(args.path) === digest) {
|
|
854
|
+
staticValidationCacheHits++;
|
|
855
|
+
return undefined;
|
|
856
|
+
}
|
|
857
|
+
try {
|
|
858
|
+
const ast = parseJS(source, {
|
|
859
|
+
sourceType: 'unambiguous',
|
|
860
|
+
plugins: ['typescript', 'jsx'],
|
|
861
|
+
});
|
|
862
|
+
const traverse = babelTraverse.default ?? babelTraverse;
|
|
863
|
+
traverse(ast, {
|
|
864
|
+
enter(path) {
|
|
865
|
+
const n = path.node;
|
|
866
|
+
const call = n.type === 'CallExpression' &&
|
|
867
|
+
((n.callee?.name === 'require' &&
|
|
868
|
+
!path.scope.getBinding('require')) ||
|
|
869
|
+
n.callee?.type === 'Import');
|
|
870
|
+
if ((call &&
|
|
871
|
+
(n.arguments?.length !== 1 ||
|
|
872
|
+
n.arguments[0]?.type !== 'StringLiteral')) ||
|
|
873
|
+
(n.type === 'ImportExpression' &&
|
|
874
|
+
n.source?.type !== 'StringLiteral'))
|
|
875
|
+
fail(`${n.callee?.name === 'require' ? 'FAAS_DYNAMIC_REQUIRE_UNSUPPORTED' : 'FAAS_DYNAMIC_IMPORT_UNSUPPORTED'}: ${args.path}:${n.loc?.start.line}`);
|
|
876
|
+
if (n.type === 'MetaProperty' &&
|
|
877
|
+
n.meta?.name === 'import' &&
|
|
878
|
+
n.property?.name === 'meta')
|
|
879
|
+
fail(`FAAS_IMPORT_META_UNSUPPORTED: ${args.path}:${n.loc?.start.line}`);
|
|
880
|
+
},
|
|
881
|
+
});
|
|
882
|
+
if (staticValidationCacheEnabled)
|
|
883
|
+
staticValidationCache.set(args.path, digest);
|
|
884
|
+
return undefined;
|
|
885
|
+
}
|
|
886
|
+
finally {
|
|
887
|
+
if (timingEnabled) {
|
|
888
|
+
staticValidationMs += performance.now() - started;
|
|
889
|
+
staticValidationCalls++;
|
|
890
|
+
staticValidationFiles.add(args.path);
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
});
|
|
894
|
+
},
|
|
895
|
+
};
|
|
896
|
+
const assetLoaders = {
|
|
897
|
+
'.json': 'json',
|
|
898
|
+
'.txt': 'file',
|
|
899
|
+
'.wasm': 'file',
|
|
900
|
+
'.bin': 'file',
|
|
901
|
+
'.png': 'file',
|
|
902
|
+
'.svg': 'file',
|
|
903
|
+
};
|
|
904
|
+
inspectSharedEntry = async (file, manifest) => {
|
|
905
|
+
const optional = optionalDependencyResolver(root, server, [
|
|
906
|
+
dirname(manifest),
|
|
907
|
+
]);
|
|
908
|
+
const config = {
|
|
909
|
+
absWorkingDir: root,
|
|
910
|
+
entryPoints: [file],
|
|
911
|
+
outfile: join(output, 'shared-inspection.mjs'),
|
|
912
|
+
bundle: true,
|
|
913
|
+
format: 'esm',
|
|
914
|
+
platform: 'node',
|
|
915
|
+
target: 'node22',
|
|
916
|
+
write: false,
|
|
917
|
+
metafile: true,
|
|
918
|
+
logLevel: 'silent',
|
|
919
|
+
loader: assetLoaders,
|
|
920
|
+
plugins: [
|
|
921
|
+
contractPlugin,
|
|
922
|
+
{ name: 'optional-dependencies', setup: optional.setup },
|
|
923
|
+
],
|
|
924
|
+
};
|
|
925
|
+
let result = await esbuild(config);
|
|
926
|
+
if (optional.approve(result.metafile))
|
|
927
|
+
result = await esbuild(config);
|
|
928
|
+
const input = Object.entries(result.metafile.inputs).find(([name]) => canonicalInput(root, name) === file)?.[1];
|
|
929
|
+
const main = Object.values(result.metafile.outputs).find((item) => item.entryPoint);
|
|
930
|
+
const externalModules = [
|
|
931
|
+
...new Set(Object.values(result.metafile.outputs).flatMap((item) => item.imports.filter((i) => i.external).map((i) => i.path))),
|
|
932
|
+
].sort();
|
|
933
|
+
if (externalModules.some((p) => !isBuiltin(p)))
|
|
934
|
+
fail('Only Node built-ins may be external');
|
|
935
|
+
return {
|
|
936
|
+
format: input?.format === 'esm' ? 'esm' : 'cjs',
|
|
937
|
+
exportNames: main?.exports ?? [],
|
|
938
|
+
inputHashes: hashInputs(root, [
|
|
939
|
+
...Object.keys(result.metafile.inputs),
|
|
940
|
+
manifest,
|
|
941
|
+
...optional.manifests,
|
|
942
|
+
]),
|
|
943
|
+
externalModules,
|
|
944
|
+
stubbedModules: [...optional.stubbed].sort(),
|
|
945
|
+
};
|
|
946
|
+
};
|
|
947
|
+
const completeEntry = (entry, sharing, { reuse = false } = {}) => {
|
|
948
|
+
entry.sharedEntries = sharing.records;
|
|
949
|
+
entry.sharedInputHashes = sharing.inputHashes;
|
|
950
|
+
// Restoring selected facades cannot rediscover candidates removed by
|
|
951
|
+
// tree-shaking. Their already-validated resolution metadata must survive
|
|
952
|
+
// cache hits so changes to sideEffects/exports still invalidate the build.
|
|
953
|
+
if (!reuse) {
|
|
954
|
+
entry.resolutionInputHashes = sharing.resolutionInputHashes;
|
|
955
|
+
entry.resolutionStubbedModules = sharing.resolutionStubbedModules;
|
|
956
|
+
}
|
|
957
|
+
if (sharing.files.length)
|
|
958
|
+
entry.sharedModules = sharing.files;
|
|
959
|
+
else
|
|
960
|
+
delete entry.sharedModules;
|
|
961
|
+
entry.externalModules = [
|
|
962
|
+
...new Set([...entry.ownExternalModules, ...sharing.externalModules]),
|
|
963
|
+
].sort();
|
|
964
|
+
entry.stubbedModules = [
|
|
965
|
+
...new Set([...entry.ownStubbedModules, ...sharing.stubbedModules]),
|
|
966
|
+
].sort();
|
|
967
|
+
sharing.activate();
|
|
968
|
+
return entry;
|
|
969
|
+
};
|
|
434
970
|
for (const { id, kind } of [
|
|
435
|
-
...functions.map(id => ({ id, kind: 'functions' })),
|
|
436
|
-
...durable.map(id => ({ id, kind: 'durable' })),
|
|
971
|
+
...functions.map((id) => ({ id, kind: 'functions' })),
|
|
972
|
+
...durable.map((id) => ({ id, kind: 'durable' })),
|
|
437
973
|
]) {
|
|
438
974
|
options.signal?.throwIfAborted();
|
|
439
975
|
functionId = id;
|
|
440
976
|
const entry = safePath(server, `${kind}/${id}/index.ts`);
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
977
|
+
let sharing = shared.forFunction(kind, id);
|
|
978
|
+
const cached = kind === 'functions'
|
|
979
|
+
? previous?.functions.find((fn) => fn.id === id)
|
|
980
|
+
: undefined;
|
|
981
|
+
if (cached?.ownInputHashes &&
|
|
982
|
+
!cached.ownStubbedModules.length &&
|
|
983
|
+
!cached.resolutionStubbedModules.length &&
|
|
444
984
|
!isDefineFunctionSource(readFileSync(entry, 'utf8')) &&
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
985
|
+
hashesMatch(root, cached.ownInputHashes) &&
|
|
986
|
+
hashesMatch(root, cached.sharedInputHashes) &&
|
|
987
|
+
hashesMatch(root, cached.resolutionInputHashes)) {
|
|
988
|
+
try {
|
|
989
|
+
await sharing.restore(cached.sharedEntries, Object.keys(cached.sharedInputHashes));
|
|
990
|
+
if (JSON.stringify(sharing.files) !==
|
|
991
|
+
JSON.stringify(cached.sharedModules ?? []))
|
|
992
|
+
fail('Invalid shared dependency cache');
|
|
993
|
+
entries.push(completeEntry(cached, sharing, { reuse: true }));
|
|
994
|
+
continue;
|
|
995
|
+
}
|
|
996
|
+
catch {
|
|
997
|
+
// Malformed private cache state is a miss; no candidate is activated yet.
|
|
998
|
+
sharing = shared.forFunction(kind, id);
|
|
999
|
+
}
|
|
450
1000
|
}
|
|
451
1001
|
rebuilt++;
|
|
452
1002
|
rmSync(join(output, kind, id), { recursive: true, force: true });
|
|
453
1003
|
const source = readFileSync(entry, 'utf8');
|
|
454
1004
|
const sdk = kind === 'functions' && isDefineFunctionSource(source);
|
|
455
|
-
if (kind === 'durable' &&
|
|
1005
|
+
if (kind === 'durable' &&
|
|
1006
|
+
!defineFunctionCall(parseJS(source, {
|
|
1007
|
+
sourceType: 'module',
|
|
1008
|
+
plugins: ['typescript', 'jsx'],
|
|
1009
|
+
}), 'defineDurableFunction'))
|
|
456
1010
|
fail('DURABLE_ENTRY_INVALID: default export must be defineDurableFunction(...)');
|
|
457
1011
|
let profile = options.profile ?? {
|
|
458
1012
|
platformEnv: info.config.runtimeProfile ?? 'online',
|
|
@@ -523,56 +1077,15 @@ export async function buildProject(root, options = {}) {
|
|
|
523
1077
|
'.svg': 'file',
|
|
524
1078
|
},
|
|
525
1079
|
plugins: [
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
setup(b) {
|
|
529
|
-
b.onResolve({
|
|
530
|
-
filter: /^(react(?:-dom)?(?:\/|$)|@lark-base-open\/base-database-app-sdk\/(page|components|testing|vite)(?:\/|$))/,
|
|
531
|
-
}, (args) => {
|
|
532
|
-
fail(`FAAS_UI_DEPENDENCY_UNSUPPORTED: ${args.path}`);
|
|
533
|
-
});
|
|
534
|
-
b.onResolve({ filter: /\.node$/ }, (args) => {
|
|
535
|
-
fail(`DEPENDENCY_RUNTIME_UNSUPPORTED: ${args.path}`);
|
|
536
|
-
});
|
|
537
|
-
b.onLoad({ filter: /\.node$/ }, (args) => {
|
|
538
|
-
fail(`DEPENDENCY_RUNTIME_UNSUPPORTED: ${args.path}`);
|
|
539
|
-
});
|
|
540
|
-
b.onLoad({ filter: /\.[cm]?[jt]sx?$/, namespace: 'file' }, (args) => {
|
|
541
|
-
const ast = parseJS(readFileSync(args.path, 'utf8'), {
|
|
542
|
-
sourceType: 'unambiguous',
|
|
543
|
-
plugins: ['typescript', 'jsx'],
|
|
544
|
-
});
|
|
545
|
-
const traverse = babelTraverse.default ?? babelTraverse;
|
|
546
|
-
traverse(ast, {
|
|
547
|
-
enter(path) {
|
|
548
|
-
const n = path.node;
|
|
549
|
-
const call = n.type === 'CallExpression' &&
|
|
550
|
-
((n.callee?.name === 'require' &&
|
|
551
|
-
!path.scope.getBinding('require')) ||
|
|
552
|
-
n.callee?.type === 'Import');
|
|
553
|
-
if ((call &&
|
|
554
|
-
(n.arguments?.length !== 1 ||
|
|
555
|
-
n.arguments[0]?.type !== 'StringLiteral')) ||
|
|
556
|
-
(n.type === 'ImportExpression' &&
|
|
557
|
-
n.source?.type !== 'StringLiteral'))
|
|
558
|
-
fail(`${n.callee?.name === 'require' ? 'FAAS_DYNAMIC_REQUIRE_UNSUPPORTED' : 'FAAS_DYNAMIC_IMPORT_UNSUPPORTED'}: ${args.path}:${n.loc?.start.line}`);
|
|
559
|
-
if (n.type === 'MetaProperty' &&
|
|
560
|
-
n.meta?.name === 'import' &&
|
|
561
|
-
n.property?.name === 'meta')
|
|
562
|
-
fail(`FAAS_IMPORT_META_UNSUPPORTED: ${args.path}:${n.loc?.start.line}`);
|
|
563
|
-
},
|
|
564
|
-
});
|
|
565
|
-
return undefined;
|
|
566
|
-
});
|
|
567
|
-
},
|
|
568
|
-
},
|
|
1080
|
+
contractPlugin,
|
|
1081
|
+
sharing.plugin,
|
|
569
1082
|
{ name: 'optional-dependencies', setup: optional.setup },
|
|
570
1083
|
],
|
|
571
1084
|
};
|
|
572
1085
|
// Pass one must succeed without stubs. Its input edges, not our AST,
|
|
573
1086
|
// determine whether esbuild accepted a missing import as optional.
|
|
574
1087
|
let result = await esbuild({ ...buildOptions, write: false });
|
|
575
|
-
if (optional.approve(result.metafile))
|
|
1088
|
+
if (optional.approve(result.metafile, sharing.allows))
|
|
576
1089
|
result = await esbuild(buildOptions);
|
|
577
1090
|
else
|
|
578
1091
|
for (const file of result.outputFiles) {
|
|
@@ -582,10 +1095,11 @@ export async function buildProject(root, options = {}) {
|
|
|
582
1095
|
const externalModules = [
|
|
583
1096
|
...new Set(Object.values(result.metafile.outputs).flatMap((o) => o.imports.filter((i) => i.external).map((i) => i.path))),
|
|
584
1097
|
].sort();
|
|
585
|
-
|
|
1098
|
+
sharing.retain(externalModules);
|
|
1099
|
+
if (externalModules.some((p) => !isBuiltin(p) &&
|
|
1100
|
+
!sharing.files.some((file) => p === `../../${file}`)))
|
|
586
1101
|
fail('Only Node built-ins may be external');
|
|
587
|
-
const
|
|
588
|
-
for (const file of [
|
|
1102
|
+
const ownInputHashes = hashInputs(root, [
|
|
589
1103
|
...Object.keys(result.metafile.inputs),
|
|
590
1104
|
'base-database.jsonc',
|
|
591
1105
|
'package.json',
|
|
@@ -594,46 +1108,148 @@ export async function buildProject(root, options = {}) {
|
|
|
594
1108
|
`${info.server}/tsconfig.json`,
|
|
595
1109
|
`${info.server}/package-lock.json`,
|
|
596
1110
|
`${info.server}/generated/runtime-profile.ts`,
|
|
597
|
-
])
|
|
598
|
-
if (existsSync(resolve(root, file)))
|
|
599
|
-
inputHashes[file] = hash(readFileSync(resolve(root, file)));
|
|
600
|
-
Object.assign(sourceHashes, inputHashes);
|
|
1111
|
+
]);
|
|
601
1112
|
const files = Object.keys(result.metafile.outputs).map((p) => {
|
|
602
1113
|
const name = artifactRelativePath(output, resolve(root, p)), bytes = readFileSync(safePath(output, name));
|
|
603
1114
|
return { path: name, bytes: bytes.length, sha256: hash(bytes) };
|
|
604
1115
|
});
|
|
605
|
-
entries.push({
|
|
1116
|
+
entries.push(completeEntry({
|
|
606
1117
|
id,
|
|
607
1118
|
entry: `${kind}/${id}/index.js`,
|
|
608
1119
|
export: 'default',
|
|
609
|
-
type: kind === 'durable'
|
|
610
|
-
|
|
611
|
-
|
|
1120
|
+
type: kind === 'durable'
|
|
1121
|
+
? 'defineDurableFunction'
|
|
1122
|
+
: sdk
|
|
1123
|
+
? 'defineFunction'
|
|
1124
|
+
: 'handler',
|
|
612
1125
|
files,
|
|
613
|
-
|
|
614
|
-
|
|
1126
|
+
ownInputHashes,
|
|
1127
|
+
ownExternalModules: externalModules.filter(isBuiltin),
|
|
1128
|
+
ownStubbedModules: [...optional.stubbed].sort(),
|
|
1129
|
+
}, sharing));
|
|
615
1130
|
}
|
|
616
|
-
const
|
|
1131
|
+
for (const entry of entries)
|
|
1132
|
+
Object.assign(sourceHashes, entry.ownInputHashes);
|
|
1133
|
+
shared.resetOutput();
|
|
1134
|
+
let runtimeStubbedModules = [];
|
|
1135
|
+
if (shared.size) {
|
|
1136
|
+
options.signal?.throwIfAborted();
|
|
1137
|
+
functionId = null;
|
|
1138
|
+
const optional = optionalDependencyResolver(root, server, shared.manifests.map(dirname));
|
|
1139
|
+
const buildOptions = {
|
|
1140
|
+
absWorkingDir: root,
|
|
1141
|
+
stdin: {
|
|
1142
|
+
contents: shared.source(),
|
|
1143
|
+
resolveDir: server,
|
|
1144
|
+
sourcefile: 'platform-shared-dependencies.js',
|
|
1145
|
+
loader: 'js',
|
|
1146
|
+
},
|
|
1147
|
+
outfile: shared.runtime,
|
|
1148
|
+
bundle: true,
|
|
1149
|
+
format: 'cjs',
|
|
1150
|
+
platform: 'node',
|
|
1151
|
+
target: 'node22',
|
|
1152
|
+
metafile: true,
|
|
1153
|
+
logLevel: 'silent',
|
|
1154
|
+
sourcemap: 'external',
|
|
1155
|
+
sourcesContent: false,
|
|
1156
|
+
assetNames: 'assets/[name]-[hash]',
|
|
1157
|
+
loader: {
|
|
1158
|
+
'.json': 'json',
|
|
1159
|
+
'.txt': 'file',
|
|
1160
|
+
'.wasm': 'file',
|
|
1161
|
+
'.bin': 'file',
|
|
1162
|
+
'.png': 'file',
|
|
1163
|
+
'.svg': 'file',
|
|
1164
|
+
},
|
|
1165
|
+
plugins: [
|
|
1166
|
+
contractPlugin,
|
|
1167
|
+
{ name: 'optional-dependencies', setup: optional.setup },
|
|
1168
|
+
],
|
|
1169
|
+
};
|
|
1170
|
+
let result = await esbuild({ ...buildOptions, write: false });
|
|
1171
|
+
if (optional.approve(result.metafile))
|
|
1172
|
+
result = await esbuild(buildOptions);
|
|
1173
|
+
else
|
|
1174
|
+
for (const file of result.outputFiles) {
|
|
1175
|
+
mkdirSync(dirname(file.path), { recursive: true });
|
|
1176
|
+
writeFileSync(file.path, file.contents);
|
|
1177
|
+
}
|
|
1178
|
+
if (Object.values(result.metafile.outputs).some((o) => o.imports.some((i) => i.external && !isBuiltin(i.path))))
|
|
1179
|
+
fail('Only Node built-ins may be external');
|
|
1180
|
+
shared.writeFacades();
|
|
1181
|
+
runtimeStubbedModules = [...optional.stubbed];
|
|
1182
|
+
Object.assign(sourceHashes, hashInputs(root, [
|
|
1183
|
+
...Object.keys(result.metafile.inputs),
|
|
1184
|
+
...shared.manifests,
|
|
1185
|
+
...optional.manifests,
|
|
1186
|
+
]));
|
|
1187
|
+
}
|
|
1188
|
+
finishStage(true, {
|
|
1189
|
+
functionCount: functions.length + durable.length,
|
|
1190
|
+
rebuiltCount: rebuilt,
|
|
1191
|
+
staticValidationMs: Math.round(staticValidationMs * 1000) / 1000,
|
|
1192
|
+
staticValidationCalls,
|
|
1193
|
+
uniqueStaticValidationFiles: staticValidationFiles.size,
|
|
1194
|
+
staticValidationCacheHits,
|
|
1195
|
+
staticValidationCacheEnabled,
|
|
1196
|
+
});
|
|
1197
|
+
finishStage = startBuildStage('faas', 'manifest');
|
|
1198
|
+
const commonFiles = inventory(output).filter((f) => !entries.some((e) => e.type !== 'defineDurableFunction' &&
|
|
1199
|
+
e.files.some((g) => g.path === f.path)));
|
|
617
1200
|
const lock = join(info.version === 2 ? root : server, 'package-lock.json');
|
|
1201
|
+
const sortedHashes = (value) => Object.fromEntries(Object.entries(value).sort());
|
|
1202
|
+
const cacheState = {
|
|
1203
|
+
version: 2,
|
|
1204
|
+
functions: entries
|
|
1205
|
+
.filter((fn) => fn.type !== 'defineDurableFunction')
|
|
1206
|
+
.map((fn) => ({
|
|
1207
|
+
id: fn.id,
|
|
1208
|
+
ownInputHashes: sortedHashes(fn.ownInputHashes),
|
|
1209
|
+
ownExternalModules: fn.ownExternalModules,
|
|
1210
|
+
ownStubbedModules: fn.ownStubbedModules,
|
|
1211
|
+
sharedInputHashes: sortedHashes(fn.sharedInputHashes),
|
|
1212
|
+
resolutionInputHashes: sortedHashes(fn.resolutionInputHashes),
|
|
1213
|
+
resolutionStubbedModules: fn.resolutionStubbedModules,
|
|
1214
|
+
sharedEntries: [...fn.sharedEntries].sort((a, b) => a.path < b.path ? -1 : a.path > b.path ? 1 : 0),
|
|
1215
|
+
})),
|
|
1216
|
+
};
|
|
618
1217
|
const manifest = {
|
|
619
1218
|
contract: 'base-database-functions',
|
|
620
1219
|
contractVersion: 1,
|
|
1220
|
+
dependencySharing,
|
|
1221
|
+
buildCacheDigest: hash(JSON.stringify(cacheState)),
|
|
621
1222
|
nodeMajor: 22,
|
|
622
1223
|
format: 'cjs',
|
|
623
1224
|
builderVersion: BUILDER_VERSION,
|
|
624
1225
|
esbuildVersion,
|
|
625
1226
|
templateVersion: info.version,
|
|
626
1227
|
serverDirectory: info.server,
|
|
627
|
-
sourceDigest: hash(JSON.stringify(sourceHashes)),
|
|
1228
|
+
sourceDigest: hash(JSON.stringify(Object.fromEntries(Object.entries(sourceHashes).sort()))),
|
|
628
1229
|
lockfileDigest: existsSync(lock) ? hash(readFileSync(lock)) : null,
|
|
629
1230
|
runtimeBindingABI: 'bindFunctionRuntime@1',
|
|
630
|
-
functions: entries
|
|
631
|
-
|
|
1231
|
+
functions: entries
|
|
1232
|
+
.filter((fn) => fn.type !== 'defineDurableFunction')
|
|
1233
|
+
.map(({ ownInputHashes, ownExternalModules, ownStubbedModules, sharedInputHashes, resolutionInputHashes, resolutionStubbedModules, sharedEntries, ...fn }) => fn),
|
|
1234
|
+
...(durable.length
|
|
1235
|
+
? {
|
|
1236
|
+
durable: entries
|
|
1237
|
+
.filter((fn) => fn.type === 'defineDurableFunction')
|
|
1238
|
+
.map(({ id, entry, export: exported, sharedModules }) => ({
|
|
1239
|
+
id,
|
|
1240
|
+
entry,
|
|
1241
|
+
export: exported,
|
|
1242
|
+
...(sharedModules?.length ? { sharedModules } : {}),
|
|
1243
|
+
})),
|
|
1244
|
+
}
|
|
1245
|
+
: {}),
|
|
632
1246
|
commonFiles,
|
|
633
1247
|
totalBytes: inventory(output).reduce((sum, f) => sum + f.bytes, 0),
|
|
634
1248
|
};
|
|
635
1249
|
writeFileSync(join(output, 'build-manifest.json'), JSON.stringify(manifest, null, 2) + '\n');
|
|
1250
|
+
finishStage(true);
|
|
636
1251
|
stage = 'verify';
|
|
1252
|
+
finishStage = startBuildStage('faas', 'core-verify');
|
|
637
1253
|
readBuildCache(output, functions, durable);
|
|
638
1254
|
if (options.incremental) {
|
|
639
1255
|
for (const path of [
|
|
@@ -645,23 +1261,27 @@ export async function buildProject(root, options = {}) {
|
|
|
645
1261
|
fail('Build cache symlink');
|
|
646
1262
|
mkdirSync(dirname(cacheFile), { recursive: true });
|
|
647
1263
|
writeFileSync(cacheFile, JSON.stringify({
|
|
1264
|
+
...cacheState,
|
|
648
1265
|
manifestDigest: hash(readFileSync(join(output, 'build-manifest.json'))),
|
|
649
|
-
functions: entries.map(({ id, inputHashes }) => ({
|
|
650
|
-
id,
|
|
651
|
-
inputHashes,
|
|
652
|
-
})),
|
|
653
1266
|
}));
|
|
654
1267
|
}
|
|
1268
|
+
finishStage(true);
|
|
655
1269
|
return {
|
|
656
1270
|
ok: true,
|
|
657
1271
|
functions: functions.length,
|
|
658
1272
|
rebuilt,
|
|
659
1273
|
output,
|
|
660
1274
|
builderVersion: BUILDER_VERSION,
|
|
661
|
-
stubbedModules: [
|
|
1275
|
+
stubbedModules: [
|
|
1276
|
+
...new Set([
|
|
1277
|
+
...entries.flatMap((fn) => fn.stubbedModules ?? []),
|
|
1278
|
+
...runtimeStubbedModules,
|
|
1279
|
+
]),
|
|
1280
|
+
].sort(),
|
|
662
1281
|
};
|
|
663
1282
|
}
|
|
664
1283
|
catch (error) {
|
|
1284
|
+
finishStage(false);
|
|
665
1285
|
if (existsSync(server) &&
|
|
666
1286
|
!lstatSync(server).isSymbolicLink() &&
|
|
667
1287
|
existsSync(output) &&
|
|
@@ -680,20 +1300,26 @@ export function readBuildCache(output, expectedIds, expectedDurableIds) {
|
|
|
680
1300
|
!Array.isArray(manifest.commonFiles) ||
|
|
681
1301
|
(manifest.durable !== undefined && !Array.isArray(manifest.durable)))
|
|
682
1302
|
fail('Invalid build manifest schema');
|
|
1303
|
+
verifySharedModules(manifest);
|
|
683
1304
|
const files = [...manifest.commonFiles];
|
|
684
|
-
const seen = new Set(manifest.functions.map(fn => fn.id));
|
|
1305
|
+
const seen = new Set(manifest.functions.map((fn) => fn.id));
|
|
685
1306
|
if (seen.size !== manifest.functions.length)
|
|
686
1307
|
fail('Duplicate Function ID in manifest');
|
|
687
1308
|
for (const fn of manifest.durable ?? []) {
|
|
688
|
-
if (typeof fn.id !== 'string' ||
|
|
689
|
-
|
|
1309
|
+
if (typeof fn.id !== 'string' ||
|
|
1310
|
+
!/^[A-Za-z0-9_-]{1,64}$/.test(fn.id) ||
|
|
1311
|
+
seen.has(fn.id) ||
|
|
1312
|
+
fn.entry !== `durable/${fn.id}/index.js` ||
|
|
1313
|
+
fn.export !== 'default')
|
|
690
1314
|
fail('Invalid Durable entry in manifest');
|
|
691
1315
|
seen.add(fn.id);
|
|
692
1316
|
safePath(output, fn.entry);
|
|
693
|
-
if (!files.some(file => file.path === fn.entry))
|
|
1317
|
+
if (!files.some((file) => file.path === fn.entry))
|
|
694
1318
|
fail('Durable entry missing from artifact inventory');
|
|
695
1319
|
}
|
|
696
|
-
if (expectedDurableIds &&
|
|
1320
|
+
if (expectedDurableIds &&
|
|
1321
|
+
JSON.stringify((manifest.durable ?? []).map((fn) => fn.id).sort()) !==
|
|
1322
|
+
JSON.stringify([...expectedDurableIds].sort()))
|
|
697
1323
|
fail('Durable entry set mismatch');
|
|
698
1324
|
for (const fn of manifest.functions) {
|
|
699
1325
|
if (typeof fn.id !== 'string' ||
|