@runeditrun/rer 0.2.16 → 0.2.18

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 (46) hide show
  1. package/cli/demo-host.mjs +2 -0
  2. package/cli/deploy-claim-request.mjs +32 -0
  3. package/cli/deploy-command.mjs +22 -8
  4. package/cli/deploy-hook.mjs +11 -3
  5. package/cli/deploy-observation.mjs +7 -4
  6. package/cli/deploy-owner.mjs +5 -4
  7. package/cli/deploy.mjs +4 -4
  8. package/cli/door.mjs +4 -3
  9. package/cli/local.mjs +5 -3
  10. package/cli/output.mjs +7 -3
  11. package/cli/pnpm-probe.mjs +19 -10
  12. package/cli/port.mjs +17 -10
  13. package/cli/project-pnpm.mjs +4 -2
  14. package/cli/site-diagnostics.mjs +35 -0
  15. package/cli/site-render.mjs +4 -1
  16. package/cli/site.mjs +5 -0
  17. package/cli/stdio.mjs +12 -0
  18. package/global/CHANGELOG.md +12 -0
  19. package/global/output.mjs +1 -12
  20. package/modules/container/boundary.mjs +70 -34
  21. package/modules/container/build.mjs +20 -3
  22. package/modules/container/import-graph.mjs +233 -0
  23. package/modules/demo-host/README.md +25 -6
  24. package/modules/demo-host/capacity.mjs +27 -6
  25. package/modules/demo-host/container.mjs +37 -9
  26. package/modules/demo-host/entrypoint.sh +9 -0
  27. package/modules/demo-host/image.mjs +18 -0
  28. package/modules/demo-host/pages.mjs +7 -7
  29. package/modules/demo-host/plan.mjs +1 -1
  30. package/modules/demo-host/prepare.mjs +28 -0
  31. package/modules/demo-host/start-script.mjs +11 -4
  32. package/modules/demo-host/startup.mjs +25 -0
  33. package/modules/demo-host/template.mjs +71 -0
  34. package/modules/demo-host/worker.mjs +74 -31
  35. package/modules/deploy-kit/deploy.mjs +8 -3
  36. package/modules/deploy-kit/health-request.mjs +14 -11
  37. package/modules/deploy-kit/health.mjs +16 -6
  38. package/modules/deploy-kit/owner-cli.mjs +24 -4
  39. package/modules/deploy-kit/owner.mjs +6 -3
  40. package/modules/deploy-kit/provider.mjs +6 -2
  41. package/modules/deploy-kit/readiness-window.mjs +32 -0
  42. package/modules/deploy-kit/uploaded-worker.mjs +22 -0
  43. package/modules/deploy-kit/worker.mjs +1 -1
  44. package/onboarding/adapter.mjs +13 -4
  45. package/onboarding/process-diagnostics.mjs +142 -36
  46. package/package.json +1 -1
@@ -1,41 +1,77 @@
1
- import { readdir, readFile } from 'node:fs/promises';
2
- import { join, relative, sep } from 'node:path';
3
- import { createRequire } from 'node:module';
4
- import { pathToFileURL } from 'node:url';
5
- export async function checkBoundary(root, { engine='node', cacheContract }={}) {
6
- const ts=await import(pathToFileURL(createRequire(join(root,'package.json')).resolve('typescript'))), failures=[];
7
- async function walk(directory){
8
- for(const item of await readdir(directory,{withFileTypes:true})){
9
- const path=join(directory,item.name);if(item.isDirectory()){await walk(path);continue;}if(!/\.[cm]?[jt]sx?$/.test(item.name))continue;
10
- const name=relative(root,path),tree=ts.createSourceFile(path,await readFile(path,'utf8'),ts.ScriptTarget.Latest,true);
11
- const cache=cacheContract==='lamp-retained-v1'&&/(?:^|\/)platform\/blobs\/snapshot-cache\.ts$/.test(name);
12
- const bindings=/(?:^|\/)platform\/bindings\.server\.ts$/.test(name);
1
+ import { relative, sep } from 'node:path';
2
+ import { configuredEntry, walkImportGraph } from './import-graph.mjs';
3
+
4
+ export async function checkBoundary(root, { engine='node', cacheContract, entries, emittedFiles, outputRoot }={}) {
5
+ const failures = [];
6
+ if (Boolean(emittedFiles) !== Boolean(outputRoot)) throw new Error('Emitted files and output root must be supplied together');
7
+ const roots = entries ?? [await configuredEntry(root)];
8
+ if (!Array.isArray(roots) || !roots.length) throw new Error('At least one app entry is required');
9
+ const graph = await walkImportGraph(root, {
10
+ entries: roots,
11
+ emittedFiles,
12
+ outputRoot,
13
+ onFile({ ts, tree, path }) {
14
+ const name = relative(root, path).split(sep).join('/');
15
+ const cache = cacheContract === 'lamp-retained-v1' && /(?:^|\/)platform\/blobs\/snapshot-cache\.ts$/.test(name);
16
+ const bindings = /(?:^|\/)platform\/bindings\.server\.ts$/.test(name);
13
17
  // The demo guard an exported seed carries reads RER_DEMO from env: that one
14
18
  // file, and only its unrenamed `env` import.
15
- const demo=name.split(sep).join('/')==='src/modules/demo/index.mjs';
16
- const fail=api=>failures.push({path:name,api}),localNames=new Set();
17
- function names(node){if((ts.isVariableDeclaration(node)||ts.isFunctionDeclaration(node)||ts.isImportSpecifier(node))&&node.name&&ts.isIdentifier(node.name))localNames.add(node.name.text);ts.forEachChild(node,names);}names(tree);
18
- function visit(node){
19
- if((ts.isImportDeclaration(node)||ts.isExportDeclaration(node))&&node.moduleSpecifier&&ts.isStringLiteral(node.moduleSpecifier)){
20
- const module=node.moduleSpecifier.text;
21
- if(module.startsWith('cloudflare:')){
22
- const imports=node.importClause?.namedBindings;
23
- const allowed=module==='cloudflare:workers'&&imports&&ts.isNamedImports(imports)&&imports.elements.every(element=>(demo&&!node.importClause.name&&!element.propertyName&&element.name.text==='env')||(bindings&&(element.propertyName??element.name).text==='env')||(cache&&(element.propertyName??element.name).text==='DurableObject'));
24
- if(!allowed&&!(engine==='workerd'&&module==='cloudflare:workers'))fail('Cloudflare import outside binding/cache contract');
19
+ const demo = name === 'src/modules/demo/index.mjs';
20
+ const fail = api => failures.push({ path:name, api }), localNames = new Set();
21
+ function collectNames(node) {
22
+ if ((ts.isVariableDeclaration(node) || ts.isFunctionDeclaration(node) || ts.isImportSpecifier(node)) && node.name && ts.isIdentifier(node.name)) localNames.add(node.name.text);
23
+ ts.forEachChild(node, collectNames);
24
+ }
25
+ collectNames(tree);
26
+ function visit(node) {
27
+ const runtimeDeclaration = declaration => {
28
+ if (ts.isImportDeclaration(declaration)) {
29
+ const clause = declaration.importClause;
30
+ if (!clause) return true;
31
+ if (clause.isTypeOnly) return false;
32
+ if (clause.name) return true;
33
+ const bindings = clause.namedBindings;
34
+ return !bindings || ts.isNamespaceImport(bindings) || bindings.elements.length === 0 || bindings.elements.some(element => !element.isTypeOnly);
35
+ }
36
+ if (ts.isExportDeclaration(declaration)) {
37
+ if (declaration.isTypeOnly) return false;
38
+ const clause = declaration.exportClause;
39
+ return !clause || !ts.isNamedExports(clause) || clause.elements.length === 0 || clause.elements.some(element => !element.isTypeOnly);
40
+ }
41
+ return false;
42
+ };
43
+ if ((ts.isImportDeclaration(node) || ts.isExportDeclaration(node)) && runtimeDeclaration(node) && node.moduleSpecifier && ts.isStringLiteralLike(node.moduleSpecifier)) {
44
+ const module = node.moduleSpecifier.text;
45
+ if (module.startsWith('cloudflare:')) {
46
+ const imports = node.importClause?.namedBindings;
47
+ const runtimeImports = imports && ts.isNamedImports(imports) ? imports.elements.filter(element => !element.isTypeOnly) : [];
48
+ const allowed = module === 'cloudflare:workers' && runtimeImports.length > 0 && imports && ts.isNamedImports(imports) && runtimeImports.every(element =>
49
+ (demo && !node.importClause.name && !element.propertyName && element.name.text === 'env') ||
50
+ (bindings && (element.propertyName ?? element.name).text === 'env') ||
51
+ (cache && (element.propertyName ?? element.name).text === 'DurableObject'));
52
+ if (!allowed && !(engine === 'workerd' && module === 'cloudflare:workers')) fail('Cloudflare import outside binding/cache contract');
25
53
  }
26
54
  }
27
- if(ts.isCallExpression(node)&&node.arguments.some((arg,index)=>ts.isStringLiteral(arg)&&arg.text.startsWith('cloudflare:')&&!(engine==='workerd'&&node.expression.kind===ts.SyntaxKind.ImportKeyword&&index===0&&arg.text==='cloudflare:workers')))fail('Dynamic Cloudflare import');
28
- if(ts.isIdentifier(node)&&engine!=='workerd'&&['WebSocketPair','passThroughOnException','timingSafeEqual','HTMLRewriter'].includes(node.text))fail(node.text);
29
- if(ts.isCallExpression(node)&&ts.isIdentifier(node.expression)&&node.expression.text==='email'&&!localNames.has('email'))fail('email()');
30
- if(ts.isPropertyAccessExpression(node)&&((node.expression.getText(tree)==='caches'&&node.name.text==='default')||(node.expression.getText(tree)==='request'&&node.name.text==='cf')))fail(node.getText(tree));
31
- if(ts.isElementAccessExpression(node)&&ts.isStringLiteral(node.argumentExpression)&&((node.expression.getText(tree)==='caches'&&node.argumentExpression.text==='default')||(node.expression.getText(tree)==='request'&&node.argumentExpression.text==='cf')))fail(node.getText(tree));
32
- if(engine!=='workerd'&&ts.isHeritageClause(node)&&node.token===ts.SyntaxKind.ExtendsKeyword&&node.types.some(type=>/DurableObject/.test(type.expression.getText(tree)))&&!cache)fail('New Durable Object');
33
- ts.forEachChild(node,visit);
55
+ if (ts.isImportEqualsDeclaration(node) && !node.isTypeOnly && ts.isExternalModuleReference(node.moduleReference) && ts.isStringLiteralLike(node.moduleReference.expression) && node.moduleReference.expression.text.startsWith('cloudflare:')) fail('Dynamic Cloudflare import');
56
+ if (ts.isCallExpression(node) && node.arguments[0] && ts.isStringLiteralLike(node.arguments[0])) {
57
+ const module = node.arguments[0].text;
58
+ const isDynamicImport = node.expression.kind === ts.SyntaxKind.ImportKeyword;
59
+ const isRequire = ts.isIdentifier(node.expression) && node.expression.text === 'require';
60
+ if ((isDynamicImport || isRequire) && module.startsWith('cloudflare:') && !(engine === 'workerd' && isDynamicImport && module === 'cloudflare:workers')) fail('Dynamic Cloudflare import');
61
+ }
62
+ if (ts.isIdentifier(node) && engine !== 'workerd' && ['WebSocketPair','passThroughOnException','timingSafeEqual','HTMLRewriter'].includes(node.text)) fail(node.text);
63
+ if (ts.isCallExpression(node) && ts.isIdentifier(node.expression) && node.expression.text === 'email' && !localNames.has('email')) fail('email()');
64
+ if (ts.isPropertyAccessExpression(node) && ((node.expression.getText(tree) === 'caches' && node.name.text === 'default') || (node.expression.getText(tree) === 'request' && node.name.text === 'cf'))) fail(node.getText(tree));
65
+ if (ts.isElementAccessExpression(node) && ts.isStringLiteral(node.argumentExpression) && ((node.expression.getText(tree) === 'caches' && node.argumentExpression.text === 'default') || (node.expression.getText(tree) === 'request' && node.argumentExpression.text === 'cf'))) fail(node.getText(tree));
66
+ if (engine !== 'workerd' && ts.isHeritageClause(node) && node.token === ts.SyntaxKind.ExtendsKeyword && node.types.some(type => /DurableObject/.test(type.expression.getText(tree))) && !cache) fail('New Durable Object');
67
+ ts.forEachChild(node, visit);
34
68
  }
35
69
  visit(tree);
36
- }
37
- }
38
- await walk(join(root,'src'));
39
- if(failures.length){const cause=new Error('Container import boundary failed');cause.failures=failures;throw cause;}
40
- return {engine,ok:true};
70
+ },
71
+ });
72
+ failures.push(...graph.failures);
73
+ if (failures.length) { const cause = new Error('Container import boundary failed'); cause.failures = failures; throw cause; }
74
+ return { engine, ok:true };
41
75
  }
76
+
77
+ export { configuredEntry };
@@ -1,5 +1,5 @@
1
1
  import { readFile, writeFile, readdir, realpath, cp, mkdir, rm } from 'node:fs/promises';
2
- import { dirname, join, resolve } from 'node:path';
2
+ import { dirname, join, resolve, sep } from 'node:path';
3
3
  import { createRequire } from 'node:module';
4
4
  import { pathToFileURL } from 'node:url';
5
5
  import { createHash } from 'node:crypto';
@@ -23,10 +23,18 @@ async function buildImage(root, { output = join(root,'.rer/container/image'), en
23
23
  if (!loaded) throw new Error('Vite configuration missing');
24
24
  const config = loaded.config;
25
25
  const outputExports = new Map();
26
+ const freshServerFiles = new Set(), serverOutput = resolve(root,'dist/server');
26
27
  if (engine === 'workerd') config.plugins = [...(config.plugins ?? []), {
27
28
  name:'rer-container-exports',
28
29
  async writeBundle(output,bundle) {
29
- for (const chunk of Object.values(bundle)) if (chunk.type === 'chunk' && chunk.isEntry) outputExports.set(await realpath(resolve(root,output.dir ?? dirname(output.file),chunk.fileName)),new Set(chunk.exports));
30
+ const outputDir = resolve(root,output.dir ?? dirname(output.file));
31
+ if (outputDir === serverOutput || outputDir.startsWith(serverOutput + sep)) {
32
+ for (const chunk of Object.values(bundle)) if (chunk.type === 'chunk') {
33
+ const path = resolve(outputDir,chunk.fileName);
34
+ freshServerFiles.add(path);
35
+ if (chunk.isEntry) outputExports.set(await realpath(path),new Set(chunk.exports));
36
+ }
37
+ }
30
38
  },
31
39
  }];
32
40
  if (engine === 'node') {
@@ -35,7 +43,13 @@ async function buildImage(root, { output = join(root,'.rer/container/image'), en
35
43
  config.plugins = [{ name:'rer-container-bindings', enforce:'pre',
36
44
  resolveId(id) { if (id === 'cloudflare:workers') return bindingShim; },
37
45
  transform(source,id) { if (cacheContract === 'lamp-retained-v1' && id === join(root,'src/server.ts')) return source.replace(/export\s*\{\s*SnapshotCache\s*\}\s*from\s*['"][^'"]+['"];?/,''); },
38
- },...plugins];
46
+ },...plugins,{
47
+ name:'rer-container-boundary-output',
48
+ writeBundle(output,bundle) {
49
+ const outputDir = resolve(root,output.dir ?? dirname(output.file));
50
+ if (outputDir === serverOutput || outputDir.startsWith(serverOutput + sep)) for (const chunk of Object.values(bundle)) if (chunk.type === 'chunk') freshServerFiles.add(resolve(outputDir,chunk.fileName));
51
+ },
52
+ }];
39
53
  config.ssr = { ...config.ssr,noExternal:true }; config.environments = { ...config.environments,ssr:{ ...config.environments?.ssr,resolve:{ noExternal:true },build:{ ...config.environments?.ssr?.build,rolldownOptions:{ output:{ entryFileNames:'index.mjs' } } } } };
40
54
  }
41
55
  const builder = await vite.createBuilder({ ...config,root,configFile:false }); await builder.buildApp();
@@ -47,6 +61,9 @@ async function buildImage(root, { output = join(root,'.rer/container/image'), en
47
61
  localDurableObjects(workerConfig,outputExports.get(await realpath(resolve(root,'dist/server',workerConfig.main))));
48
62
  }
49
63
  else workerConfig = parseJsonc(await readFile(join(root,'wrangler.jsonc'),'utf8'));
64
+ const serverEntry = engine === 'workerd' ? resolve(root,'dist/server',workerConfig.main) : resolve(root,'dist/server/index.mjs');
65
+ if (!freshServerFiles.has(serverEntry)) throw new Error('Container server entry was not emitted by this build');
66
+ await checkBoundary(root,{engine,cacheContract,entries:[serverEntry],emittedFiles:freshServerFiles,outputRoot:serverOutput});
50
67
  const { unstable_splitSqlQuery:split } = require('wrangler');
51
68
  const migrations = {}, rewrites = await readMigrationRewrites(root);
52
69
  for (const db of workerConfig.d1_databases ?? []) {
@@ -0,0 +1,233 @@
1
+ import { readFile, stat } from 'node:fs/promises';
2
+ import { createRequire } from 'node:module';
3
+ import { dirname, extname, isAbsolute, join, relative, resolve, sep } from 'node:path';
4
+ import { fileURLToPath, pathToFileURL } from 'node:url';
5
+ import { parseJsonc } from '../../cli/jsonc.mjs';
6
+ import { tomlMain } from '../../cli/deploy-config.mjs';
7
+
8
+ const sourceExtensions = new Set(['.js', '.jsx', '.mjs', '.cjs', '.ts', '.tsx', '.mts', '.cts']);
9
+ const builtExtensions = ['.mjs', '.js', '.cjs', '.jsx'];
10
+
11
+ const normalized = path => resolve(path);
12
+ const displayPath = (root, path) => relative(root, path).split(sep).join('/') || '.';
13
+ const isDeclaration = path => /\.d\.(?:ts|mts|cts)$/i.test(path);
14
+ const isCode = path => sourceExtensions.has(extname(path).toLowerCase()) && !isDeclaration(path);
15
+ const isPackagePath = path => path.split(sep).includes('node_modules');
16
+ const stripQuery = specifier => specifier.startsWith('#') ? specifier : specifier.split(/[?#]/, 1)[0];
17
+ const isRelative = specifier => specifier.startsWith('./') || specifier.startsWith('../');
18
+ const isScheme = specifier => /^[a-z][a-z\d+.-]*:/i.test(specifier);
19
+
20
+ function runtimeImport(ts, declaration) {
21
+ const clause = declaration.importClause;
22
+ if (!clause) return true;
23
+ if (clause.isTypeOnly) return false;
24
+ if (clause.name) return true;
25
+ const bindings = clause.namedBindings;
26
+ if (!bindings || ts.isNamespaceImport(bindings)) return Boolean(bindings);
27
+ return bindings.elements.length === 0 || bindings.elements.some(element => !element.isTypeOnly);
28
+ }
29
+
30
+ function runtimeReexport(ts, declaration) {
31
+ if (declaration.isTypeOnly) return false;
32
+ const clause = declaration.exportClause;
33
+ return !clause || !ts.isNamedExports(clause) || clause.elements.length === 0 || clause.elements.some(element => !element.isTypeOnly);
34
+ }
35
+
36
+ function runtimeEdges(ts, tree, options) {
37
+ const edges = [];
38
+ const add = (node, kind) => {
39
+ if (node && ts.isStringLiteralLike(node)) {
40
+ // Dynamic import keeps its ESM condition inside a CommonJS module; static
41
+ // TypeScript imports follow that file's emitted format instead.
42
+ const mode = kind === 'require' || kind === 'import-equals' ? ts.ModuleKind.CommonJS
43
+ : kind === 'dynamic-import' ? ts.ModuleKind.ESNext
44
+ : ts.getModeForUsageLocation(tree, node, options) ?? ts.ModuleKind.ESNext;
45
+ edges.push({ specifier: node.text, kind, mode });
46
+ }
47
+ };
48
+ function visit(node) {
49
+ if (ts.isImportDeclaration(node) && runtimeImport(ts, node)) add(node.moduleSpecifier, 'import');
50
+ else if (ts.isExportDeclaration(node) && node.moduleSpecifier && runtimeReexport(ts, node)) add(node.moduleSpecifier, 're-export');
51
+ else if (ts.isImportEqualsDeclaration(node) && !node.isTypeOnly && ts.isExternalModuleReference(node.moduleReference)) add(node.moduleReference.expression, 'import-equals');
52
+ else if (ts.isCallExpression(node)) {
53
+ if (node.expression.kind === ts.SyntaxKind.ImportKeyword) add(node.arguments[0], 'dynamic-import');
54
+ else if (ts.isIdentifier(node.expression) && node.expression.text === 'require') add(node.arguments[0], 'require');
55
+ }
56
+ ts.forEachChild(node, visit);
57
+ }
58
+ visit(tree);
59
+ return edges;
60
+ }
61
+
62
+ function matchesPathAlias(specifier, options) {
63
+ for (const pattern of Object.keys(options.paths ?? {})) {
64
+ const wildcard = pattern.indexOf('*');
65
+ if (wildcard < 0 ? specifier === pattern : specifier.startsWith(pattern.slice(0, wildcard)) && specifier.endsWith(pattern.slice(wildcard + 1))) return true;
66
+ }
67
+ return false;
68
+ }
69
+
70
+ async function localAssetExists(from, specifier) {
71
+ const target = resolve(dirname(from), specifier);
72
+ try { return (await stat(target)).isFile() ? target : null; } catch (error) { if (error.code === 'ENOENT' || error.code === 'ENOTDIR') return null; throw error; }
73
+ }
74
+
75
+ async function loadTypeScript(root) {
76
+ const require = createRequire(join(root, 'package.json'));
77
+ const path = require.resolve('typescript');
78
+ return import(pathToFileURL(path));
79
+ }
80
+
81
+ async function compilerOptions(root, ts) {
82
+ const path = join(root, 'tsconfig.json');
83
+ if (!ts.sys.fileExists(path)) return { allowJs: true, resolveJsonModule: true, module: ts.ModuleKind.ESNext, moduleResolution: ts.ModuleResolutionKind.Bundler };
84
+ try {
85
+ const parsed = ts.getParsedCommandLineOfConfigFile(path, {}, {
86
+ ...ts.sys,
87
+ onUnRecoverableConfigFileDiagnostic(diagnostic) {
88
+ throw new Error(ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'));
89
+ },
90
+ });
91
+ if (!parsed) throw new Error('TypeScript did not return parsed compiler options');
92
+ const errors = parsed.errors.filter(diagnostic => diagnostic.category === ts.DiagnosticCategory.Error);
93
+ if (errors.length) throw new Error(errors.map(diagnostic => ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n')).join('\n'));
94
+ return { ...parsed.options, allowJs: true, resolveJsonModule: true };
95
+ } catch (error) {
96
+ if (error.code === 'ENOENT') return { allowJs: true, resolveJsonModule: true, module: ts.ModuleKind.ESNext, moduleResolution: ts.ModuleResolutionKind.Bundler };
97
+ throw new Error(`Could not read TypeScript project configuration: ${error.message}`, { cause: error });
98
+ }
99
+ }
100
+
101
+ async function configuredEntry(root) {
102
+ for (const name of ['wrangler.jsonc', 'wrangler.json', 'wrangler.toml']) {
103
+ let text;
104
+ try { text = await readFile(join(root, name), 'utf8'); }
105
+ catch (error) { if (error.code === 'ENOENT') continue; throw error; }
106
+ let main;
107
+ try {
108
+ main = name.endsWith('.toml') ? tomlMain(text) : parseJsonc(text).main;
109
+ } catch (error) {
110
+ throw new Error(`Could not read Wrangler main from ${name}: ${error.message}`, { cause: error });
111
+ }
112
+ if (typeof main !== 'string' || !main.trim()) throw new Error(`${name} does not declare a Wrangler main entry`);
113
+ return resolve(root, main);
114
+ }
115
+ throw new Error('No root wrangler.jsonc, wrangler.json, or wrangler.toml declares the app main entry');
116
+ }
117
+
118
+ function failure(path, api, extra = {}) {
119
+ return { path, api, ...extra };
120
+ }
121
+
122
+ function runtimeRequest(specifier) {
123
+ return /^file:/i.test(specifier) ? fileURLToPath(specifier) : stripQuery(specifier);
124
+ }
125
+
126
+ async function resolveSourceEdge(ts, edge, importer, options) {
127
+ const { specifier, mode } = edge;
128
+ let request;
129
+ try { request = runtimeRequest(specifier); }
130
+ catch { return { unresolved: true, reason: 'invalid local file URL' }; }
131
+ const local = isRelative(request) || isAbsolute(request) || request.startsWith('#') || matchesPathAlias(request, options);
132
+ if (isScheme(request)) return { external: true };
133
+ const javascriptRequest = builtExtensions.includes(extname(request).toLowerCase());
134
+ if (javascriptRequest && (isRelative(request) || isAbsolute(request))) {
135
+ const implementation = await localAssetExists(importer, request);
136
+ if (implementation) return isPackagePath(implementation) ? { external: true } : { path: implementation };
137
+ }
138
+ // TypeScript prefers declaration sidecars to JavaScript implementations. They
139
+ // describe types, not the code the runtime will execute, so exclude them from
140
+ // graph resolution. Explicit JavaScript imports first try real JavaScript;
141
+ // the second pass retains TypeScript's extension substitution for source apps
142
+ // that import ./module.js but author module.ts (including configured aliases).
143
+ const implementationHost = { ...ts.sys, fileExists: path => !isDeclaration(path) && ts.sys.fileExists(path) };
144
+ const javascriptHost = { ...ts.sys, fileExists: path => !/\.(?:ts|tsx|mts|cts)$/i.test(path) && ts.sys.fileExists(path) };
145
+ const resolveModule = host => ts.resolveModuleName(request, importer, options, host, undefined, undefined, mode).resolvedModule;
146
+ const resolved = (javascriptRequest && resolveModule(javascriptHost)) || resolveModule(implementationHost);
147
+ if (resolved) {
148
+ const path = normalized(resolved.resolvedFileName);
149
+ if (resolved.isExternalLibraryImport || isPackagePath(path)) return { external: true };
150
+ if (isCode(path)) return { path };
151
+ return { external: true };
152
+ }
153
+ if (!local) return { external: true };
154
+ const asset = await localAssetExists(importer, request);
155
+ if (asset && !isCode(asset) && !isDeclaration(asset)) return { external: true };
156
+ return { unresolved: true };
157
+ }
158
+
159
+ async function builtCandidates(importer, request) {
160
+ const target = resolve(dirname(importer), request);
161
+ if (extname(target)) return [target];
162
+ return [...builtExtensions.map(extension => target + extension), ...builtExtensions.map(extension => join(target, 'index' + extension))];
163
+ }
164
+
165
+ function emittedModule(path, emittedFiles, outputRoot) {
166
+ const canonical = normalized(path), rel = relative(outputRoot, canonical);
167
+ if (rel.startsWith('..') || isAbsolute(rel)) return { unresolved: true, reason: 'local import escapes the built server output' };
168
+ if (!emittedFiles.has(canonical)) return { unresolved: true, reason: 'local import was not emitted by this build' };
169
+ return { path: canonical };
170
+ }
171
+
172
+ async function resolveBuiltEdge(ts, importer, edge, options, emittedFiles, outputRoot) {
173
+ let request;
174
+ try { request = runtimeRequest(edge.specifier); }
175
+ catch { return { unresolved: true, reason: 'invalid local file URL' }; }
176
+ if (!isScheme(request) && !isRelative(request) && !isAbsolute(request)) {
177
+ const result = await resolveSourceEdge(ts, edge, importer, options);
178
+ return result.path ? emittedModule(result.path, emittedFiles, outputRoot) : result;
179
+ }
180
+ if (isScheme(request)) return { external: true };
181
+ const requested = resolve(dirname(importer), request);
182
+ const candidates = await builtCandidates(importer, request);
183
+ for (const candidate of candidates) {
184
+ try {
185
+ if (!(await stat(candidate)).isFile()) continue;
186
+ } catch (error) { if (error.code === 'ENOENT' || error.code === 'ENOTDIR') continue; throw error; }
187
+ if (!isCode(candidate)) return { external: true };
188
+ return emittedModule(candidate, emittedFiles, outputRoot);
189
+ }
190
+ if (extname(requested) && !sourceExtensions.has(extname(requested).toLowerCase())) {
191
+ const asset = await localAssetExists(importer, request);
192
+ if (asset) return { external: true };
193
+ }
194
+ return { unresolved: true };
195
+ }
196
+
197
+ export async function walkImportGraph(root, { entries, emittedFiles, outputRoot, onFile }) {
198
+ // A package's `types` condition must not select a different runtime branch.
199
+ const ts = await loadTypeScript(root), options = { ...await compilerOptions(root, ts), noDtsResolution: true };
200
+ const failures = [], visited = new Set(), pending = (await Promise.all(entries.map(async entry => {
201
+ if (isAbsolute(entry)) return normalized(entry);
202
+ return resolve(root, entry);
203
+ }))).reverse();
204
+ const emitted = emittedFiles ? new Set([...emittedFiles].map(normalized)) : null;
205
+ const builtRoot = outputRoot ? normalized(outputRoot) : null;
206
+ const built = Boolean(emitted && builtRoot);
207
+
208
+ while (pending.length) {
209
+ const path = pending.pop(), canonical = normalized(path);
210
+ if (visited.has(canonical)) continue;
211
+ visited.add(canonical);
212
+ let source;
213
+ try { source = await readFile(canonical, 'utf8'); }
214
+ catch (error) { failures.push(failure(displayPath(root, canonical), 'Missing app entry or runtime module', { message: error.message })); continue; }
215
+ if (!isCode(canonical)) { failures.push(failure(displayPath(root, canonical), 'App graph entry is not a JavaScript or TypeScript module')); continue; }
216
+ const tree = ts.createSourceFile(canonical, source, ts.ScriptTarget.Latest, true);
217
+ tree.impliedNodeFormat = ts.getImpliedNodeFormatForFile(canonical, undefined, ts.sys, options);
218
+ onFile?.({ ts, tree, path: canonical, source });
219
+ // Emitted JavaScript already contains its runtime syntax; do not apply the
220
+ // source project's TypeScript module transform to it again.
221
+ for (const edge of runtimeEdges(ts, tree, built ? { ...options, module: ts.ModuleKind.Preserve } : options)) {
222
+ const result = built
223
+ ? await resolveBuiltEdge(ts, canonical, edge, options, emitted, builtRoot)
224
+ : await resolveSourceEdge(ts, edge, canonical, options);
225
+ if (result.unresolved) {
226
+ failures.push(failure(displayPath(root, canonical), 'Unresolved local runtime import', { specifier: edge.specifier, kind: edge.kind, ...(result.reason ? { message: result.reason } : {}) }));
227
+ } else if (result.path) pending.push(result.path);
228
+ }
229
+ }
230
+ return { ts, files: visited, failures };
231
+ }
232
+
233
+ export { configuredEntry, isCode };
@@ -9,8 +9,11 @@ it with these files and the seed's container image folder (workerd engine) to
9
9
 
10
10
  - `worker.mjs`: `GET /` is the start page; `POST /__demo/start` checks the
11
11
  exact Origin, the `STARTS` rate limit (3 per IP per 60 s), Turnstile's
12
- siteverify and capacity, then starts the visitor's demo and answers with the
13
- owner claim's 303, its session cookie and the `rer_demo` cookie. A request
12
+ siteverify and capacity, then starts the visitor's demo. It answers with
13
+ 202 progress and a `rer_demo` cookie; `GET /__demo/status` polls that same
14
+ copy until it can relay the owner claim's 303 (JSON `{ location }` for the
15
+ page script) and session cookie. Every external admission step is bounded:
16
+ the start answers within 12 seconds, and status within two seconds. A request
14
17
  with that cookie goes to the visitor's Durable Object; nothing else reaches
15
18
  a container.
16
19
  - `container.mjs`: `DemoContainer`, one per visitor (`newUniqueId`, checked
@@ -28,11 +31,23 @@ it with these files and the seed's container image folder (workerd engine) to
28
31
  minutes, and arms the next at once, as the Containers class keeps its own
29
32
  object alive. A request or alarm that finds the container gone ends the demo
30
33
  and answers the ended page (410), never a 5xx.
31
- - `capacity.mjs`: `DemoCapacity` admits at most `max_instances` live demos.
34
+ - `capacity.mjs`: `DemoCapacity` counts unexpired reservation rows inside a
35
+ storage transaction, excluding diagnostics and expired rows. A lost start
36
+ handoff expires after one minute; a successful claim extends the reservation
37
+ to the demo's end. Refusals log the held ids as SHA-256 fingerprints (also
38
+ logged with each start's correlation), never visitor bearer cookies.
39
+ - `image.mjs`, `prepare.mjs`, `template.mjs`, `entrypoint.sh`: the demo image
40
+ build migrates fresh workerd persistence with the packaged engine and closes
41
+ it into `/template`, with no network and no app requests. The entrypoint
42
+ copies that state into a fresh `/data` under the host's existing flock;
43
+ partial or foreign volumes are refused. Startup validates the existing
44
+ migration hashes instead of replaying SQL. Owners, sessions and the seed's
45
+ owner-dependent sample data are still created separately for each visitor.
32
46
  - `pages.mjs`: the start and ended pages (12ui designs), strict CSP.
33
47
  `start-script.mjs` is the start page's script: once Turnstile passes it
34
48
  posts the form with `Accept: application/json` (the Worker then answers
35
- `{ location }` with the cookies, or `{ notice }` with the refusal's status)
49
+ 202 `{ pending: true }`, `{ location }` with the cookies, or `{ notice }`
50
+ with the refusal's status)
36
51
  and shows each step: the check, the private copy starting, the sign-in. A
37
52
  refusal, a network failure or a start past 25 seconds is shown with Try
38
53
  again, which runs a new check; progress is announced through a
@@ -55,8 +70,12 @@ Chromium (default-seed's Playwright) through waiting, failure, Try again,
55
70
  working and the signed-in landing. The end-to-end proof (`wrangler dev` with Docker)
56
71
  is in DEMO.md.
57
72
 
58
- Startup probes keep the existing 120-second budget, with a separate five-second
59
- HTTP probe limit. Every attempt logs its timestamp, elapsed time, duration and
73
+ Startup has a 45-second budget, with a separate five-second HTTP probe limit.
74
+ The Durable Object owns that work and cleanup while the page polls; a browser
75
+ that leaves cannot abandon it. Retrying a pending copy resumes the same
76
+ reservation. Failed starts retain the bounded operator diagnostic and destroy
77
+ the container; cleanup retries keep a reservation only while destruction is
78
+ unconfirmed. Every attempt logs its timestamp, elapsed time, duration and
60
79
  HTTP status or fixed error category, including successful probes. Exit records
61
80
  include the numeric platform exit code when supplied. A separate random
62
81
  `x-rer-demo-correlation` response header joins these events without disclosing
@@ -19,15 +19,36 @@ export class DemoCapacity extends DurableObject {
19
19
  async alarm() { await this.ctx.storage.deleteAll(); }
20
20
 
21
21
  async acquire(id, ends, max) {
22
- const now = Date.now(), places = await this.ctx.storage.list();
23
- const over = [...places].filter(([, until]) => until <= now).map(([key]) => key);
24
- if (over.length) await this.ctx.storage.delete(over);
25
- if (places.size - over.length >= max) return false;
26
- await this.ctx.storage.put(id, ends);
27
- return true;
22
+ return this.ctx.storage.transaction(async storage => {
23
+ const now = Date.now(), places = [...await storage.list()]
24
+ .filter(([key, until]) => /^[0-9a-f]{64}$/.test(key) && typeof until === 'number');
25
+ const expired = places.filter(([, until]) => until <= now).map(([key]) => key);
26
+ if (expired.length) await storage.delete(expired);
27
+ const held = places.filter(([key, until]) => key !== id && until > now);
28
+ if (held.length >= max) {
29
+ // Fingerprints identify held reservations without logging bearer cookies.
30
+ const heldIds = await Promise.all(held.map(async ([key, until]) => ({ id: await fingerprint(key), ends: until })));
31
+ console.log(JSON.stringify({ event: 'demo_capacity_refused', max, held: heldIds }));
32
+ return false;
33
+ }
34
+ await storage.put(id, ends);
35
+ return true;
36
+ });
37
+ }
38
+
39
+ async extend(id, ends) {
40
+ return this.ctx.storage.transaction(async storage => {
41
+ if (!((await storage.get(id)) > Date.now())) return false;
42
+ await storage.put(id, ends);
43
+ return true;
44
+ });
28
45
  }
29
46
 
30
47
  async release(id) {
31
48
  await this.ctx.storage.delete(id);
32
49
  }
33
50
  }
51
+
52
+ export async function fingerprint(id) {
53
+ return [...new Uint8Array(await crypto.subtle.digest('SHA-256', new TextEncoder().encode(id)))].slice(0, 12).map(byte => byte.toString(16).padStart(2, '0')).join('');
54
+ }
@@ -2,6 +2,7 @@ import { DurableObject } from 'cloudflare:workers';
2
2
  import { claimKeys, signClaim } from './claim.mjs';
3
3
  import { bounded, boundedFetch, category, report } from './diagnostics.mjs';
4
4
  import { endedPage, clearCookie } from './pages.mjs';
5
+ import { finishStart } from './startup.mjs';
5
6
 
6
7
  // One visitor's demo: a Durable Object (newUniqueId) and its container, the
7
8
  // seed's image from `rer build --platform container` on the workerd engine.
@@ -23,9 +24,14 @@ export class DemoContainer extends DurableObject {
23
24
  constructor(ctx, env) {
24
25
  super(ctx, env);
25
26
  ctx.blockConcurrencyWhile(async () => {
26
- const state = await ctx.storage.get(['correlation', 'started']);
27
+ const state = await ctx.storage.get(['correlation', 'started', 'startup']);
27
28
  this.correlation = state.get('correlation'); this.started = state.get('started');
28
- if (this.container.running) this.watch();
29
+ if (state.get('startup')?.phase === 'starting') {
30
+ // An interrupted start has lost its private claim key and cannot be
31
+ // resumed after object eviction. End it instead of polling forever.
32
+ await ctx.storage.put('startup', { phase: 'failed' });
33
+ ctx.waitUntil(this.end().catch(cause => report(this.correlation, this.started, 'cleanup', { error: category(cause) })));
34
+ } else if (this.container.running) this.watch();
29
35
  });
30
36
  }
31
37
  watch() {
@@ -42,20 +48,34 @@ export class DemoContainer extends DurableObject {
42
48
  }
43
49
  diagnostic() { return { correlation: this.correlation, tail: this.records, stdout: 'Provider container logs; not exposed by the lifecycle API.' }; }
44
50
  idle = IDLE;
45
- startTimeout = 120_000;
51
+ startTimeout = 45_000;
46
52
  probeTimeout = 5000;
47
53
  cleanupRetry = 30_000;
48
54
  hold = 3 * 60_000;
49
55
  get container() { return this.ctx.container; }
50
56
  port() { return this.container.getTcpPort(PORT); }
51
57
 
58
+ async begin(options) {
59
+ if (!await this.ctx.storage.get('startup')) {
60
+ await this.ctx.storage.put('startup', { phase: 'starting' });
61
+ this.ctx.waitUntil(finishStart(this, options));
62
+ }
63
+ return this.status();
64
+ }
65
+
66
+ async status() {
67
+ const startup = await this.ctx.storage.get('startup');
68
+ return { ...(startup ?? { phase: 'failed' }), correlation: this.correlation };
69
+ }
70
+
52
71
  /** Start the container, wait for /health, claim the owner; returns the claim's 303. */
53
72
  async start({ origin, originVar, ends, endsLocal, settings, correlation }) {
54
73
  this.correlation = correlation; this.started = Date.now(); this.stopped = false;
55
74
  const deadline = this.started + this.startTimeout;
56
75
  const id = this.ctx.id.toString(), keys = await claimKeys(), now = Date.now();
57
- await this.ctx.storage.put({ ends, seen: now, correlation, started: this.started });
76
+ await this.ctx.storage.put({ ends, seen: now, correlation, started: this.started, startDeadline: deadline });
58
77
  report(correlation, this.started, 'start');
78
+ if (this.stopped) throw new Error('container-stopped');
59
79
  this.container.start({ enableInternet: false, env: { ...settings, RER_DEMO: '1', [originVar]: origin, RER_INGRESS_ORIGIN: origin,
60
80
  SESSION_SECRET: random(), SETUP_TOKEN: random(), OWNER_EMAIL: OWNER, RER_OWNER_CLAIM_ISSUER: origin, RER_OWNER_CLAIM_INSTALLATION_ID: id,
61
81
  RER_OWNER_CLAIM_PUBLIC_KEYS: JSON.stringify(keys.publicKeys), RER_DEMO_EXPIRES: endsLocal } });
@@ -94,6 +114,7 @@ export class DemoContainer extends DurableObject {
94
114
  async exited(error) {
95
115
  this.stopped = true;
96
116
  this.record('exit', { outcome: error ? category(error) : 'exited', ...(Number.isInteger(error?.exitCode) ? { exitCode: error.exitCode } : {}) });
117
+ if ((await this.ctx.storage.get('startup'))?.phase === 'starting') return;
97
118
  try { await this.end(); } catch (cause) { report(this.correlation, this.started, 'cleanup', { error: category(cause) }); }
98
119
  }
99
120
 
@@ -117,11 +138,14 @@ export class DemoContainer extends DurableObject {
117
138
  }
118
139
 
119
140
  async alarm() {
120
- const state = await this.ctx.storage.get(['ends', 'seen']), now = Date.now();
141
+ const state = await this.ctx.storage.get(['ends', 'seen', 'startDeadline']), now = Date.now();
121
142
  if (await this.ctx.storage.get('stopping')) return this.end();
122
143
  if (!state.get('ends')) return;
123
- const due = Math.min(state.get('ends'), state.get('seen') + this.idle);
124
- if (now >= due || !this.container.running) return this.end();
144
+ const due = Math.min(state.get('ends'), state.get('seen') + this.idle, state.get('startDeadline') ?? Infinity);
145
+ if (now >= due || !this.container.running) {
146
+ if ((await this.ctx.storage.get('startup'))?.phase === 'starting') { this.stopped = true; return; }
147
+ return this.end();
148
+ }
125
149
  await new Promise(resolve => { this.wake = resolve; setTimeout(resolve, Math.min(due - now, this.hold)); });
126
150
  if (!this.stopped) await this.ctx.storage.setAlarm(Date.now());
127
151
  }
@@ -138,8 +162,12 @@ export class DemoContainer extends DurableObject {
138
162
  // Keep the alarm and capacity reservation if destruction fails; retry cleanup.
139
163
  await this.ctx.storage.put('stopping', true);
140
164
  await this.ctx.storage.setAlarm(Date.now() + this.cleanupRetry);
141
- if (this.container.running) await bounded(this.container.destroy(), 5000);
142
- await this.env.CAPACITY.get(this.env.CAPACITY.idFromName('capacity')).release(this.ctx.id.toString());
165
+ if (this.container.running) {
166
+ try { await bounded(this.env.CAPACITY.get(this.env.CAPACITY.idFromName('capacity')).extend(this.ctx.id.toString(), Date.now() + 60_000), 2000); }
167
+ catch (cause) { report(this.correlation, this.started, 'cleanup-reservation', { error: category(cause) }); }
168
+ await bounded(this.container.destroy(), 5000);
169
+ }
170
+ await bounded(this.env.CAPACITY.get(this.env.CAPACITY.idFromName('capacity')).release(this.ctx.id.toString()), 2000);
143
171
  await this.ctx.storage.deleteAlarm();
144
172
  await this.ctx.storage.deleteAll();
145
173
  report(this.correlation, this.started, 'ended');
@@ -0,0 +1,9 @@
1
+ #!/bin/sh
2
+ set -eu
3
+ umask 077
4
+ mkdir -p /data
5
+ # Keep this same lock inode held through template copying and the host lifetime.
6
+ exec flock --nonblock --no-fork /data/host.lock sh -ec '
7
+ node /runtime/modules/demo-host/template.mjs /template /data
8
+ exec node /runtime/modules/container/host.mjs "$@"
9
+ ' sh "$@"
@@ -0,0 +1,18 @@
1
+ import { cp, mkdir, readFile, writeFile } from 'node:fs/promises';
2
+ import { join } from 'node:path';
3
+
4
+ /** Add demo-only preparation to an ordinary workerd image build folder. */
5
+ export async function prepareDemoImage(image) {
6
+ const manifest = JSON.parse(await readFile(join(image, 'app/container.json'), 'utf8'));
7
+ if (manifest.engine !== 'workerd') throw new Error('demo-template-requires-workerd');
8
+ const path = join(image, 'Dockerfile'), dockerfile = await readFile(path, 'utf8');
9
+ const entrypoint = 'ENTRYPOINT ["sh", "/runtime/modules/container/entrypoint.sh"]';
10
+ if (!dockerfile.endsWith(entrypoint + '\n')) throw new Error('demo-image-entrypoint-changed');
11
+ const runtime = join(image, 'runtime/modules/demo-host');
12
+ await mkdir(runtime, { recursive: true });
13
+ for (const file of ['prepare.mjs', 'template.mjs', 'entrypoint.sh']) await cp(new URL(file, import.meta.url), join(runtime, file));
14
+ await writeFile(path, dockerfile.slice(0, -entrypoint.length - 1)
15
+ + 'RUN --network=none node /runtime/modules/demo-host/prepare.mjs\n'
16
+ + 'ENTRYPOINT ["sh", "/runtime/modules/demo-host/entrypoint.sh"]\n');
17
+ return { output: image };
18
+ }