@runeditrun/rer 0.2.29 → 0.2.30

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 (48) hide show
  1. package/cli/contract.mjs +1 -1
  2. package/cli/demo-build.mjs +23 -0
  3. package/cli/demo-host.mjs +10 -70
  4. package/cli/demo-native.mjs +74 -0
  5. package/cli/lifecycle.mjs +1 -1
  6. package/cli/parse.mjs +1 -1
  7. package/global/CHANGELOG.md +13 -0
  8. package/modules/container/README.md +2 -15
  9. package/modules/container/host.mjs +1 -3
  10. package/modules/container/ingress.mjs +1 -15
  11. package/modules/container/worker-engine.mjs +2 -3
  12. package/modules/demo-host/README.md +23 -156
  13. package/modules/demo-host/build.mjs +49 -0
  14. package/modules/demo-host/capacity.mjs +8 -2
  15. package/modules/demo-host/claim.mjs +2 -2
  16. package/modules/demo-host/diagnostics.mjs +1 -18
  17. package/modules/demo-host/native/alarms.mjs +51 -0
  18. package/modules/demo-host/native/bindings.mjs +24 -0
  19. package/modules/demo-host/native/bridge.mjs +17 -0
  20. package/modules/demo-host/native/config.mjs +35 -0
  21. package/modules/demo-host/native/cron.mjs +31 -0
  22. package/modules/demo-host/native/d1.mjs +37 -0
  23. package/modules/demo-host/native/deploy-r2.mjs +43 -0
  24. package/modules/demo-host/native/isolate.mjs +36 -0
  25. package/modules/demo-host/native/loader.mjs +30 -0
  26. package/modules/demo-host/native/namespace.mjs +35 -0
  27. package/modules/demo-host/native/prepare.mjs +35 -0
  28. package/modules/demo-host/native/queues.mjs +62 -0
  29. package/modules/demo-host/native/r2-client.mjs +37 -0
  30. package/modules/demo-host/native/r2.mjs +43 -0
  31. package/modules/demo-host/native/resources.mjs +37 -0
  32. package/modules/demo-host/native/seed-object.mjs +22 -0
  33. package/modules/demo-host/native/sql.mjs +17 -0
  34. package/modules/demo-host/native/visitor.mjs +147 -0
  35. package/modules/demo-host/native/worker.mjs +22 -0
  36. package/modules/demo-host/pages.mjs +1 -1
  37. package/modules/demo-host/plan.mjs +9 -30
  38. package/modules/demo-host/remove.mjs +13 -23
  39. package/modules/demo-host/upgrade.mjs +41 -0
  40. package/modules/demo-host/worker.mjs +3 -4
  41. package/package.json +1 -1
  42. package/modules/demo-host/container.mjs +0 -221
  43. package/modules/demo-host/entrypoint.sh +0 -9
  44. package/modules/demo-host/image.mjs +0 -18
  45. package/modules/demo-host/placement.mjs +0 -37
  46. package/modules/demo-host/prepare.mjs +0 -28
  47. package/modules/demo-host/startup.mjs +0 -27
  48. package/modules/demo-host/template.mjs +0 -71
@@ -22,9 +22,15 @@ export class DemoCapacity extends DurableObject {
22
22
  return this.ctx.storage.transaction(async storage => {
23
23
  const now = Date.now(), places = [...await storage.list()]
24
24
  .filter(([key, until]) => /^[0-9a-f]{64}$/.test(key) && typeof until === 'number');
25
- const expired = places.filter(([, until]) => until <= now).map(([key]) => key);
25
+ // A class migration retires visitor ids from the old namespace. Release
26
+ // those reservations on the first native start rather than holding a
27
+ // full legacy host closed until its former visitors' hour expires.
28
+ const expired = places.filter(([key, until]) => {
29
+ if (until <= now) return true;
30
+ try { this.env.DEMO.idFromString(key); return false; } catch { return true; }
31
+ }).map(([key]) => key);
26
32
  if (expired.length) await storage.delete(expired);
27
- const held = places.filter(([key, until]) => key !== id && until > now);
33
+ const held = places.filter(([key]) => key !== id && !expired.includes(key));
28
34
  if (held.length >= max) {
29
35
  // Fingerprints identify held reservations without logging bearer cookies.
30
36
  const heldIds = await Promise.all(held.map(async ([key, until]) => ({ id: await fingerprint(key), ends: until })));
@@ -1,6 +1,6 @@
1
1
  // The signed owner claim (modules/owner-claim) a demo's front Worker makes for
2
- // its container: one ES256 key pair per container, its public JWK given to the
3
- // container at start, and one five-minute claim signed with the private key,
2
+ // its isolate: one ES256 key pair per isolate, its public JWK given to the
3
+ // isolate at start, and one five-minute claim signed with the private key,
4
4
  // which is never stored.
5
5
  const base64url = bytes => btoa(String.fromCharCode(...new Uint8Array(bytes))).replaceAll('+', '-').replaceAll('/', '_').replace(/=+$/, '');
6
6
  const segment = value => base64url(new TextEncoder().encode(JSON.stringify(value)));
@@ -1,31 +1,14 @@
1
1
  // Only fixed categories and numeric metadata enter provider logs. No request,
2
- // container response body, cookie, environment value or raw error is retained.
2
+ // isolate response body, cookie, environment value or raw error is retained.
3
3
  export function category(error) {
4
4
  const message = String(error?.message ?? '');
5
- if (message === 'placement-unavailable' || /there is no container instance that can be provided to this Durable Object, try again later/i.test(message)) return 'placement-unavailable';
6
5
  if (error?.name === 'TimeoutError' || error?.name === 'AbortError') return 'probe-timeout';
7
- if (/connection refused|not listening|port.*not.*ready/i.test(message)) return 'port-not-ready';
8
- if (/container.*not.*running|container.*stopped/i.test(message)) return 'container-stopped';
9
6
  if (/disconnect|connection reset|network connection lost/i.test(message)) return 'connection-lost';
10
7
  return error instanceof TypeError ? 'transport-type-error' : 'platform-error';
11
8
  }
12
9
  export function report(correlation, started, event, detail = {}) {
13
10
  console.log(JSON.stringify({ event: `demo_${event}`, correlation, at: new Date().toISOString(), elapsedMs: Date.now() - started, ...detail }));
14
11
  }
15
- export async function boundedFetch(port, url, init, milliseconds) {
16
- const controller = new AbortController();
17
- let timer;
18
- const timeout = new Promise((_, reject) => { timer = setTimeout(() => { controller.abort(); reject(new DOMException('probe deadline', 'TimeoutError')); }, milliseconds); });
19
- try {
20
- const request = port.fetch(url, { ...init, signal: controller.signal }).then(response => {
21
- if (controller.signal.aborted) void response.body?.cancel().catch(() => {});
22
- return response;
23
- });
24
- return await Promise.race([request, timeout]);
25
- }
26
- finally { clearTimeout(timer); }
27
- }
28
-
29
12
  export async function bounded(operation, milliseconds) {
30
13
  let timer;
31
14
  try {
@@ -0,0 +1,51 @@
1
+ import { bundle } from './loader.mjs';
2
+
3
+ export async function seedAlarm(host, binding, id, method, at) {
4
+ await host.active();
5
+ if (!bundle.bindings.objects.some(item => item.name === binding) || !/^[a-f0-9]{64}$/.test(id)) throw new Error(`Unknown Durable Object binding or id: ${binding}`);
6
+ const key = `alarm:${binding}:${id}`;
7
+ if (method === 'get') {
8
+ const record = await host.ctx.storage.get(key);
9
+ return record && !record.firing ? record.at : null;
10
+ }
11
+ if (method === 'delete') { await host.ctx.storage.delete(key); return; }
12
+ if (method !== 'set' || !Number.isFinite(at) || at <= 0) throw new TypeError('Invalid Durable Object alarm time');
13
+ await host.ctx.storage.put(key, { binding, id, at: Math.max(Date.now(), at), token: crypto.randomUUID(), attempts: 0 });
14
+ await host.schedule(at);
15
+ }
16
+ export async function nextSeedAlarm(host) {
17
+ return Math.min(Infinity, ...[...(await host.ctx.storage.list({ prefix: 'alarm:' })).values()].map(item => item.at));
18
+ }
19
+ export async function deliverSeedAlarms(host, now) {
20
+ const records = await host.ctx.storage.list({ prefix: 'alarm:' });
21
+ for (const [key, record] of records) {
22
+ if (record.at > now) continue;
23
+ const claimed = await host.ctx.storage.transaction(async txn => {
24
+ const current = await txn.get(key);
25
+ if (current?.token !== record.token) return false;
26
+ // Persist a retry before invoking the seed, so an interrupted delivery is
27
+ // retried too. A seed's set/delete during delivery supersedes this token.
28
+ await txn.put(key, { ...record, firing: true, at: Date.now() + 2000 * 2 ** record.attempts, attempts: record.attempts + 1 });
29
+ return true;
30
+ });
31
+ if (!claimed) continue;
32
+ let failed = false;
33
+ try {
34
+ const response = await host.object(record.binding, record.id, 'fetch', [new Request('https://native.invalid/alarm', { headers: {
35
+ 'x-rer-native-alarm-key': host.state.runtime.RER_NATIVE_KEY,
36
+ 'x-rer-native-alarm-info': JSON.stringify({ retryCount: record.attempts, isRetry: record.attempts > 0 }),
37
+ } })]);
38
+ if (response.status !== 204) throw new Error(`Seed alarm refused: ${response.status}`);
39
+ }
40
+ catch (error) {
41
+ failed = true;
42
+ console.error(JSON.stringify({ event: 'native-demo-seed-alarm-failed', binding: record.binding, message: String(error.message) }));
43
+ }
44
+ await host.ctx.storage.transaction(async txn => {
45
+ const current = await txn.get(key);
46
+ if (current?.token !== record.token) return;
47
+ if (failed && current.attempts <= 6) await txn.put(key, { ...current, firing: false });
48
+ else await txn.delete(key);
49
+ });
50
+ }
51
+ }
@@ -0,0 +1,24 @@
1
+ import { env } from 'cloudflare:workers';
2
+ import { AsyncLocalStorage } from 'node:async_hooks';
3
+ import { DemoD1 } from './d1.js';
4
+ import { DemoR2 } from './r2-client.js';
5
+ import { DemoNamespace } from './namespace.js';
6
+
7
+ // Seed DOs use RPC rather than another object's SQL storage handle.
8
+ export const localSQL = new AsyncLocalStorage();
9
+ const config = env.RER_NATIVE_CONFIG;
10
+ for (const binding of config.databases) {
11
+ const call = (method, ...args) => {
12
+ const sql = localSQL.getStore();
13
+ return sql && binding === config.databases[0]
14
+ ? sql[method](...args) : env.RER_NATIVE_HOST.database(binding, method, args);
15
+ };
16
+ env[binding] = new DemoD1({ query: (...args) => call('query', ...args), batch: (...args) => call('batch', ...args), exec: (...args) => call('exec', ...args) });
17
+ }
18
+ for (const binding of config.buckets) env[binding] = new DemoR2(env.RER_NATIVE_HOST, binding);
19
+ for (const { binding, queue } of config.queues.producers) env[binding] = {
20
+ metrics: () => env.RER_NATIVE_HOST.queueMetrics(queue),
21
+ send: (body, options = {}) => env.RER_NATIVE_HOST.enqueue(queue, [{ body, ...options }]),
22
+ sendBatch: (messages, options = {}) => env.RER_NATIVE_HOST.enqueue(queue, Array.from(messages).map(message => ({ ...options, ...message }))),
23
+ };
24
+ for (const { name } of config.objects) env[name] = new DemoNamespace(env.RER_NATIVE_HOST, name, env.RER_NATIVE_KEY);
@@ -0,0 +1,17 @@
1
+ import { WorkerEntrypoint } from 'cloudflare:workers';
2
+
3
+ // Only visitor-local resource operations cross into the loaded seed.
4
+ export class HostBridge extends WorkerEntrypoint {
5
+ #host() { return this.env.DEMO.get(this.env.DEMO.idFromString(this.ctx.props.id)); }
6
+ database(binding, method, args) { return this.#host().database(binding, method, args); }
7
+ object(binding, id, method, args) { return this.#host().object(binding, id, method, args); }
8
+ r2(binding, method, args) { return this.#host().r2(binding, method, args); }
9
+ enqueue(queue, messages) { return this.#host().enqueue(queue, messages); }
10
+ queueMetrics(queue) { return this.#host().queueMetrics(queue); }
11
+ seedAlarm(binding, id, method, at) { return this.#host().seedAlarm(binding, id, method, at); }
12
+ fetch(request) {
13
+ // The host validates the visitor key and removes the routing headers.
14
+ if (!request.headers.has('x-rer-native-object') || request.headers.get('x-rer-native-key') !== this.ctx.props.key) return new Response('Not found.', { status: 404 });
15
+ return this.#host().fetch(request);
16
+ }
17
+ }
@@ -0,0 +1,35 @@
1
+ import { parseCron } from './cron.mjs';
2
+
3
+ export const RUNTIME_FILES = ['config', 'worker', 'visitor', 'loader', 'd1', 'sql', 'r2', 'queues', 'resources', 'cron', 'bridge', 'alarms'];
4
+ export const ISOLATE_FILES = ['bindings', 'namespace', 'r2-client', 'd1', 'sql', 'seed-object'];
5
+ export function nativeBindings(config) {
6
+ for (const kind of ['kv_namespaces', 'services', 'workflows', 'ai', 'vectorize', 'hyperdrive', 'analytics_engine_datasets', 'browser', 'images', 'send_email', 'dispatch_namespaces', 'mtls_certificates', 'pipelines', 'secrets_store_secrets', 'unsafe', 'vpc_services', 'vpc_networks', 'ratelimits', 'worker_loaders', 'containers', 'ai_search_namespaces', 'ai_search', 'agent_memory', 'connect', 'artifacts', 'unsafe_hello_world', 'flagship', 'logfwdr', 'version_metadata']) {
7
+ const value = config[kind];
8
+ if (!value || (Array.isArray(value) && !value.length)) continue;
9
+ const entries = Array.isArray(value) ? value : value.bindings ?? [value];
10
+ for (const item of entries) throw new Error(`Loader demo cannot bind ${kind}: ${item.binding ?? item.name ?? kind}`);
11
+ }
12
+ const databases = (config.d1_databases ?? []).map(item => item.binding);
13
+ const buckets = (config.r2_buckets ?? []).map(item => item.binding);
14
+ const objects = config.durable_objects?.bindings ?? [];
15
+ for (const item of objects) {
16
+ if (item.script_name || item.environment) throw new Error(`Loader demo cannot bind external Durable Object: ${item.name}`);
17
+ if (!/^[A-Za-z_$][\w$]*$/.test(item.class_name) || ['DemoApp', 'DemoDatabase'].includes(item.class_name)) throw new Error(`Loader demo cannot export Durable Object: ${item.name} (${item.class_name})`);
18
+ }
19
+ const queues = { producers: config.queues?.producers ?? [], consumers: config.queues?.consumers ?? [] };
20
+ for (const consumer of queues.consumers) {
21
+ if (consumer.type && consumer.type !== 'worker') throw new Error(`Loader demo cannot bind pull queue: ${consumer.queue}`);
22
+ }
23
+ for (const cron of config.triggers?.crons ?? []) {
24
+ try { parseCron(cron); } catch { throw new Error(`Loader demo cannot schedule cron: ${cron}`); }
25
+ }
26
+ const names = [...databases, ...buckets, ...objects.map(item => item.name), ...queues.producers.map(item => item.binding)];
27
+ for (const name of names) if (!/^[A-Za-z_][\w]*$/.test(name) || name.startsWith('RER_NATIVE_')) throw new Error(`Loader demo cannot bind reserved or invalid name: ${name}`);
28
+ if (new Set(names).size !== names.length) throw new Error('Loader demo has duplicate binding names');
29
+ return { databases, buckets, objects, queues, assets: config.assets?.binding ?? 'ASSETS' };
30
+ }
31
+
32
+ // Export one wrapper per binding, including when several bindings share a class.
33
+ export const objectClass = binding => `RerSeed_${binding}`;
34
+ export const objectExports = bindings => (bindings.objects.length ? "\nimport { seedObject } from './seed-object.js';" : '') + bindings.objects.map(({ name, class_name }) =>
35
+ `\nexport const ${objectClass(name)} = seedObject(application[${JSON.stringify(class_name)}], ${JSON.stringify(name)});`).join('');
@@ -0,0 +1,31 @@
1
+ const fields = [[0, 59], [0, 23], [1, 31], [1, 12], [1, 7]];
2
+
3
+ function field(text, [low, high]) {
4
+ const values = new Set();
5
+ for (const part of text.split(',')) {
6
+ const match = /^(?:(\*)|(\d+)(?:-(\d+))?)(?:\/(\d+))?$/.exec(part);
7
+ if (!match) throw new Error(`Unsupported cron field "${text}"`);
8
+ const step = match[4] === undefined ? 1 : Number(match[4]);
9
+ const start = match[1] ? low : Number(match[2]), end = match[1] ? high : match[3] === undefined ? (match[4] === undefined ? start : high) : Number(match[3]);
10
+ if (!step || start < low || end > high || start > end) throw new Error(`Cron field "${text}" is out of range`);
11
+ for (let value = start; value <= end; value += step) values.add(value);
12
+ }
13
+ return values;
14
+ }
15
+
16
+ // Same grammar the deploy kit accepts: five numeric fields with lists, ranges and
17
+ // steps, in UTC. Day of week counts 1 (Sunday) to 7 (Saturday), as Cloudflare does.
18
+ export function parseCron(expression) {
19
+ const parts = expression.trim().split(/\s+/);
20
+ if (parts.length !== 5) throw new Error(`Cron "${expression}" needs five fields`);
21
+ const [minute, hour, day, month, weekday] = parts.map((part, index) => field(part, fields[index]));
22
+ return { minute, hour, day, month, weekday, dayRestricted: parts[2] !== '*', weekdayRestricted: parts[4] !== '*' };
23
+ }
24
+
25
+ export function cronMatches(cron, date) {
26
+ const parsed = typeof cron === 'string' ? parseCron(cron) : cron;
27
+ const day = parsed.day.has(date.getUTCDate()), weekday = parsed.weekday.has(date.getUTCDay() + 1);
28
+ const days = parsed.dayRestricted && parsed.weekdayRestricted ? day || weekday : day && weekday;
29
+ return parsed.minute.has(date.getUTCMinutes()) && parsed.hour.has(date.getUTCHours()) && parsed.month.has(date.getUTCMonth() + 1) && days;
30
+ }
31
+
@@ -0,0 +1,37 @@
1
+ // The synchronous D1 statement builder stays inside the app isolate. Only SQL
2
+ // and bound values cross RPC; a batch remains one SQLite transaction.
3
+ export class DemoD1 {
4
+ constructor(sql) { this.sql = sql; }
5
+ prepare(query) { return new Statement(this, query); }
6
+ async batch(statements) {
7
+ if (statements.some(statement => statement.db !== this)) throw new Error('Foreign D1 statement');
8
+ return (await this.sql.batch(statements.map(({ query, args }) => ({ query, args })))).map(result);
9
+ }
10
+ exec(query) { return this.sql.exec(query); }
11
+ }
12
+ const result = value => ({ success: value.success, results: value.results, meta: value.meta });
13
+ class Statement {
14
+ constructor(db, query, args = []) { this.db = db; this.query = query; this.args = args; }
15
+ bind(...args) {
16
+ return new Statement(this.db, this.query, args.map(value => {
17
+ if (typeof value === 'boolean') return Number(value);
18
+ if (value === null || typeof value === 'string' || typeof value === 'number' || value instanceof ArrayBuffer || ArrayBuffer.isView(value)) return value;
19
+ if (Array.isArray(value) && value.every(byte => Number.isInteger(byte) && byte >= 0 && byte <= 255)) return new Uint8Array(value);
20
+ throw new TypeError('D1_TYPE_ERROR: unsupported bound value');
21
+ }));
22
+ }
23
+ execute() { return this.db.sql.query(this.query, this.args); }
24
+ async all() { return result(await this.execute()); }
25
+ run() { return this.all(); }
26
+ async first(column) {
27
+ const row = (await this.execute()).results[0];
28
+ if (!row) return null;
29
+ if (column === undefined) return row;
30
+ if (!Object.hasOwn(row, column)) throw new Error(`D1_COLUMN_NOTFOUND: ${column}`);
31
+ return row[column];
32
+ }
33
+ async raw({ columnNames = false } = {}) {
34
+ const { columns, rows } = await this.execute();
35
+ return columnNames ? [columns, ...rows] : rows;
36
+ }
37
+ }
@@ -0,0 +1,43 @@
1
+ import { workerSettings } from '../../deploy-kit/owner.mjs';
2
+ import { servedVersion } from '../../deploy-kit/site.mjs';
3
+ import { r2ObjectPages, r2ObjectPath } from '../../deploy-kit/r2-objects.mjs';
4
+
5
+ export const demoBucketName = worker => `${worker.replace(/-+/g, '-').slice(0, 48)}-objects`;
6
+ const missing = async (api, name) => {
7
+ try { return await api.request(`/r2/buckets/${name}`); }
8
+ catch (error) { if (error.status === 404) return null; throw error; }
9
+ };
10
+ export async function prepareBucket(api, plan, pending) {
11
+ const name = demoBucketName(plan.worker), bucket = await missing(api, name), settings = await workerSettings(api, plan.worker);
12
+ if (!bucket && settings?.bindings?.some(item => item.name === 'NATIVE_BUCKET')) throw new Error(`Demo R2 bucket is missing: ${name}`);
13
+ if (bucket) {
14
+ const version = settings && await servedVersion(api, plan.worker);
15
+ if (!settings && pending?.record === plan.record && pending.bucket?.name === name && pending.bucket.creation_date === bucket.creation_date) return bucket;
16
+ if (!version?.current || version.annotation !== plan.record || !ownsBucket(settings, bucket)) throw new Error(`Demo R2 bucket ownership is unproven: ${name}`);
17
+ return bucket;
18
+ }
19
+ return api.request('/r2/buckets', { method: 'POST', body: { name } });
20
+ }
21
+ function ownsBucket(settings, bucket) {
22
+ return settings?.bindings?.some(item => item.type === 'r2_bucket' && item.name === 'NATIVE_BUCKET' && item.bucket_name === bucket.name)
23
+ && settings.bindings.some(item => item.name === 'NATIVE_BUCKET_CREATED' && item.text === bucket.creation_date);
24
+ }
25
+ export async function ownedBucket(api, plan, settings) {
26
+ const binding = settings?.bindings?.find(item => item.name === 'NATIVE_BUCKET' && item.type === 'r2_bucket');
27
+ if (!binding) return null;
28
+ if (binding.bucket_name !== demoBucketName(plan.worker)) throw new Error('demo-r2-name-collision');
29
+ const bucket = await missing(api, binding.bucket_name);
30
+ if (bucket && !ownsBucket(settings, bucket)) throw new Error('demo-r2-replaced');
31
+ return bucket;
32
+ }
33
+ export async function removeBucket(api, bucket) {
34
+ const check = async () => {
35
+ const current = await missing(api, bucket.name);
36
+ if (!current || current.creation_date !== bucket.creation_date) throw new Error('demo-r2-replaced');
37
+ };
38
+ for await (const page of r2ObjectPages(api, bucket.name)) for (const { key } of page) {
39
+ await check(); await api.request(r2ObjectPath(bucket.name, key), { method: 'DELETE' });
40
+ }
41
+ await check(); await api.request(`/r2/buckets/${bucket.name}`, { method: 'DELETE' });
42
+ if (await missing(api, bucket.name)) throw new Error('demo-r2-remove-incomplete');
43
+ }
@@ -0,0 +1,36 @@
1
+ // Bindings must exist before evaluating the seed's cloudflare:workers imports.
2
+ import { localSQL } from './bindings.js';
3
+ import { env, DurableObject } from 'cloudflare:workers';
4
+ import * as application from '__RER_APP_ENTRY__';
5
+ import { sqlite } from './sql.js';
6
+
7
+ export class DemoDatabase extends DurableObject {
8
+ constructor(ctx, bindings) { super(ctx, bindings); this.sql = sqlite(ctx.storage); }
9
+ query(query, args) { return this.sql.query(query, args); }
10
+ batch(statements) { return this.sql.batch(statements); }
11
+ exec(query) { return this.sql.exec(query); }
12
+ }
13
+ export class DemoApp extends DemoDatabase {
14
+ async fetch(request) { return localSQL.run(this.sql, () => application.default.fetch(request, env, this.ctx)); }
15
+ async runScheduled(scheduledTime, cron) {
16
+ if (application.default.scheduled) await localSQL.run(this.sql, () => application.default.scheduled({ scheduledTime, cron, noRetry() {} }, env, this.ctx));
17
+ }
18
+ async runQueue(queue, messages) {
19
+ if (!application.default.queue) throw new Error(`Seed has no queue() handler for ${queue}`);
20
+ const outcomes = new Map(), waits = [];
21
+ const settle = (id, retry, options = {}) => { if (!outcomes.has(id)) outcomes.set(id, { id, retry, delaySeconds: options.delaySeconds }); };
22
+ const batch = { queue, messages: messages.map(message => ({ ...message, timestamp: new Date(message.timestamp),
23
+ ack: () => settle(message.id, false), retry: options => settle(message.id, true, options) })),
24
+ ackAll: () => messages.forEach(message => settle(message.id, false)),
25
+ retryAll: options => messages.forEach(message => settle(message.id, true, options)),
26
+ };
27
+ let failed = false;
28
+ try {
29
+ await localSQL.run(this.sql, async () => {
30
+ await application.default.queue(batch, env, { waitUntil: promise => waits.push(promise), passThroughOnException() {} });
31
+ await Promise.all(waits);
32
+ });
33
+ } catch { failed = true; }
34
+ return messages.map(message => outcomes.get(message.id) ?? { id: message.id, retry: failed });
35
+ }
36
+ }
@@ -0,0 +1,30 @@
1
+ import { WorkerEntrypoint } from 'cloudflare:workers';
2
+ import demoEgress from '../../container/demo-egress.mjs';
3
+ import bundle from './bundle.mjs';
4
+
5
+ export function loaded(env, ctx, id, runtime) {
6
+ return env.LOADER.get(`${bundle.digest}:${id}`, () => ({
7
+ compatibilityDate: bundle.compatibilityDate, compatibilityFlags: bundle.compatibilityFlags,
8
+ mainModule: '_rer/isolate.js', modules: bundle.modules,
9
+ globalOutbound: ctx.exports.DemoEgress({ props: { id } }),
10
+ env: { ...runtime, RER_NATIVE_CONFIG: bundle.bindings, RER_NATIVE_HOST: ctx.exports.HostBridge({ props: { id, key: runtime.RER_NATIVE_KEY } }), [bundle.bindings.assets]: ctx.exports.DemoAssets({}) },
11
+ }));
12
+ }
13
+ export function application(env, ctx, id, runtime) {
14
+ return ctx.facets.get('app', () => ({ class: loaded(env, ctx, id, runtime).getDurableObjectClass('DemoApp') }));
15
+ }
16
+ export class DemoAssets extends WorkerEntrypoint {
17
+ fetch(request) { return this.env.ASSETS.fetch(request); }
18
+ }
19
+ export { bundle };
20
+
21
+ export class DemoEgress extends WorkerEntrypoint {
22
+ fetch(request) {
23
+ return demoEgress.fetch(request, {
24
+ RER_DEMO: '1',
25
+ RER_DEMO_ORIGINS: JSON.stringify([this.env.DEMO_ORIGIN, ...JSON.parse(this.env.DEMO_SECONDARY_ORIGINS ?? '[]')]),
26
+ SELF: { fetch: request => this.env.DEMO.get(this.env.DEMO.idFromString(this.ctx.props.id)).fetch(request) },
27
+ NETWORK: { fetch: request => fetch(request) },
28
+ });
29
+ }
30
+ }
@@ -0,0 +1,35 @@
1
+ import { createHash } from 'node:crypto';
2
+
3
+ class Id {
4
+ constructor(value, name) { this.value = value; this.name = name; }
5
+ toString() { return this.value; }
6
+ equals(other) { return this.value === other.toString(); }
7
+ }
8
+ export class DemoNamespace {
9
+ constructor(host, binding, key) { this.host = host; this.binding = binding; this.key = key; }
10
+ idFromName(name) { return new Id(createHash('sha256').update(`${this.binding}:${name}`).digest('hex'), name); }
11
+ idFromString(value) {
12
+ if (!/^[a-f0-9]{64}$/i.test(value)) throw new TypeError('Invalid Durable Object id');
13
+ return new Id(value.toLowerCase());
14
+ }
15
+ newUniqueId() { return new Id([...crypto.getRandomValues(new Uint8Array(32))].map(byte => byte.toString(16).padStart(2, '0')).join('')); }
16
+ jurisdiction() { return this; }
17
+ getByName(name, options) { return this.get(this.idFromName(name), options); }
18
+ get(id) {
19
+ const host = this.host, binding = this.binding, key = this.key, value = this.idFromString(id.toString());
20
+ return new Proxy({ id, name: id.name }, { get(target, method) {
21
+ if (method in target) return target[method];
22
+ if (method === 'then' || typeof method === 'symbol') return undefined;
23
+ return async (...args) => {
24
+ if (method === 'fetch') {
25
+ const request = args[0] instanceof Request ? args[0] : new Request(...args);
26
+ const headers = new Headers(request.headers);
27
+ headers.set('x-rer-native-object', `${binding}:${value}`);
28
+ headers.set('x-rer-native-key', key);
29
+ return host.fetch(new Request(request, { headers }));
30
+ }
31
+ return host.object(binding, value.toString(), method, args);
32
+ };
33
+ } });
34
+ }
35
+ }
@@ -0,0 +1,35 @@
1
+ import { readFile, writeFile, cp } from 'node:fs/promises';
2
+ import { resolve, join, relative } from 'node:path';
3
+ import { createHash } from 'node:crypto';
4
+ import { collectBuildModules } from '../../deploy-kit/build-modules.mjs';
5
+ import { collectFiles } from '../../deploy-kit/artifact.mjs';
6
+ import { nativeBindings, ISOLATE_FILES, objectExports } from './config.mjs';
7
+
8
+ // Reuse the normal production build and its migration inventory, without
9
+ // building or running a container. The bundle is stored once in the front.
10
+ export async function prepareNative(folder) {
11
+ const app = join(folder, 'bundle/app');
12
+ const manifest = JSON.parse(await readFile(join(app, 'demo.json'), 'utf8'));
13
+ const config = manifest.config, root = join(app, 'dist/server');
14
+ const bindings = nativeBindings(config);
15
+ const main = relative(root, resolve(root, config.main));
16
+ const modules = {};
17
+ for (const file of await collectBuildModules(root, config, main)) {
18
+ const source = Buffer.from(file.base64, 'base64');
19
+ if (/\.(m?js)$/.test(file.path)) modules[`app/${file.path}`] = { js: source.toString('utf8') };
20
+ else if (/\.(txt|html|sql)$/.test(file.path)) modules[`app/${file.path}`] = { text: source.toString('utf8') };
21
+ else throw new Error(`Loader demo does not implement module type: ${file.path}`);
22
+ }
23
+ const entry = `../app/${main}`;
24
+ modules['_rer/isolate.js'] = { js: (await readFile(new URL('./isolate.mjs', import.meta.url), 'utf8')).replace('__RER_APP_ENTRY__', entry)
25
+ + objectExports(bindings) };
26
+ for (const file of ISOLATE_FILES) modules[`_rer/${file}.js`] = { js: await readFile(new URL(`./${file}.mjs`, import.meta.url), 'utf8') };
27
+ const assets = await collectFiles(join(app, 'dist/client'));
28
+ const bundle = { compatibilityDate: config.compatibility_date, compatibilityFlags: [...new Set([...(config.compatibility_flags ?? []), 'nodejs_compat'])], modules,
29
+ bindings, migrations: manifest.migrations, vars: config.vars ?? {}, crons: config.triggers?.crons ?? [],
30
+ assetPaths: assets.filter(file => !file.path.split('/').some(part => part.startsWith('.'))).map(file => '/' + file.path) };
31
+ bundle.digest = createHash('sha256').update(JSON.stringify(bundle)).digest('hex');
32
+ await cp(join(app, 'dist/client'), join(folder, 'assets'), { recursive: true });
33
+ await writeFile(join(folder, 'modules/demo-host/native/bundle.mjs'), `export default ${JSON.stringify(bundle)};\n`);
34
+ return bundle;
35
+ }
@@ -0,0 +1,62 @@
1
+ import { application, bundle } from './loader.mjs';
2
+ const consumerFor = queue => bundle.bindings.queues.consumers.find(item => item.queue === queue);
3
+ export async function enqueue(host, queue, messages) {
4
+ await host.active();
5
+ if (!bundle.bindings.queues.producers.some(item => item.queue === queue)) throw new Error(`Unknown queue: ${queue}`);
6
+ if (!Array.isArray(messages) || !messages.length || messages.length > 100) throw new Error('Queue batch must contain 1–100 messages');
7
+ const now = Date.now(), records = {};
8
+ for (const message of messages) {
9
+ const type = message.contentType ?? 'json';
10
+ if (!['json', 'text', 'bytes', 'v8'].includes(type)) throw new TypeError('Unknown queue contentType');
11
+ const body = type === 'json' ? JSON.parse(JSON.stringify(message.body)) : message.body;
12
+ if (type === 'text' && typeof body !== 'string') throw new TypeError('Queue text body must be a string');
13
+ const delay = message.delaySeconds ?? consumerFor(queue)?.delivery_delay ?? 0;
14
+ if (!Number.isInteger(delay) || delay < 0 || delay > 43200) throw new TypeError('Invalid queue delaySeconds');
15
+ const id = crypto.randomUUID();
16
+ records[`queue:${id}`] = { id, queue, body, timestamp: now, attempts: 0, at: now + Math.max(delay * 1000, Math.min(1000, (consumerFor(queue)?.max_batch_timeout ?? 1) * 1000)) };
17
+ }
18
+ await host.ctx.storage.put(records);
19
+ await host.schedule(Math.min(...Object.values(records).map(item => item.at)));
20
+ return { metadata: { metrics: await queueMetrics(host, queue) } };
21
+ }
22
+ export async function deliverQueues(host, now) {
23
+ const records = await host.ctx.storage.list({ prefix: 'queue:' });
24
+ const groups = new Map();
25
+ for (const message of records.values()) if (message.at <= now) {
26
+ if (!groups.has(message.queue)) groups.set(message.queue, []);
27
+ groups.get(message.queue).push(message);
28
+ }
29
+ for (const [queue, pending] of groups) {
30
+ const consumer = consumerFor(queue) ?? {}, size = consumer.max_batch_size ?? 10;
31
+ // Bound alarm work. Undelivered batches retain their original durable records.
32
+ const messages = pending.slice(0, size).map(message => ({ ...message, attempts: message.attempts + 1 }));
33
+ let outcomes;
34
+ try { outcomes = await application(host.env, host.ctx, host.ctx.id.toString(), host.state.runtime).runQueue(queue, messages); }
35
+ catch { outcomes = messages.map(message => ({ id: message.id, retry: true })); }
36
+ console.log(JSON.stringify({ event: 'native-demo-queue', correlation: host.state?.correlation, queue, messages: messages.length, acknowledged: outcomes.filter(item => !item.retry).length }));
37
+ for (const message of messages) {
38
+ if (!host.state || host.state.phase !== 'ready') return;
39
+ const result = outcomes.find(item => item.id === message.id);
40
+ if (result && !result.retry) await host.ctx.storage.delete(`queue:${message.id}`);
41
+ else if (message.attempts <= (consumer.max_retries ?? 3)) {
42
+ message.at = Date.now() + (result?.delaySeconds ?? consumer.retry_delay ?? Math.min(60, 2 ** message.attempts)) * 1000;
43
+ await host.ctx.storage.put(`queue:${message.id}`, message);
44
+ } else if (consumer.dead_letter_queue) {
45
+ const target = consumer.dead_letter_queue;
46
+ await host.ctx.storage.put(`queue:${message.id}`, { ...message, queue: target, attempts: 0, at: consumerFor(target) ? Date.now() : Number.MAX_SAFE_INTEGER });
47
+ } else await host.ctx.storage.delete(`queue:${message.id}`);
48
+ }
49
+ }
50
+ }
51
+ export async function nextQueue(host) {
52
+ return Math.min(Infinity, ...[...(await host.ctx.storage.list({ prefix: 'queue:' })).values()].map(item => item.at));
53
+ }
54
+
55
+ export async function queueMetrics(host, queue) {
56
+ await host.active();
57
+ if (!bundle.bindings.queues.producers.some(item => item.queue === queue)) throw new Error(`Unknown queue: ${queue}`);
58
+ const messages = [...(await host.ctx.storage.list({ prefix: 'queue:' })).values()].filter(item => item.queue === queue);
59
+ const bytes = body => body instanceof ArrayBuffer || ArrayBuffer.isView(body) ? body.byteLength : new TextEncoder().encode(typeof body === 'string' ? body : JSON.stringify(body)).byteLength;
60
+ return { backlogCount: messages.length, backlogBytes: messages.reduce((sum, item) => sum + bytes(item.body), 0),
61
+ ...(messages.length ? { oldestMessageTimestamp: new Date(Math.min(...messages.map(item => item.timestamp))) } : {}) };
62
+ }
@@ -0,0 +1,37 @@
1
+ function object(value) {
2
+ if (!value) return value;
3
+ value.writeHttpMetadata = headers => {
4
+ for (const [key, content] of Object.entries(value.httpMetadata ?? {})) {
5
+ const header = { contentType: 'content-type', contentLanguage: 'content-language', contentDisposition: 'content-disposition', contentEncoding: 'content-encoding', cacheControl: 'cache-control', cacheExpiry: 'expires' }[key];
6
+ if (header) headers.set(header, content instanceof Date ? content.toUTCString() : content);
7
+ }
8
+ };
9
+ if (value.body) {
10
+ const response = new Response(value.body);
11
+ Object.defineProperty(value, 'bodyUsed', { get: () => response.bodyUsed });
12
+ for (const method of ['arrayBuffer', 'text', 'json', 'blob']) value[method] = () => response[method]();
13
+ value.bytes = async () => new Uint8Array(await response.arrayBuffer());
14
+ }
15
+ return value;
16
+ }
17
+ // RPC does not preserve a stream's known byte length, which R2 requires.
18
+ // Seed upload limits keep these demo bodies bounded; multipart parts are
19
+ // buffered separately rather than collecting the complete multipart object.
20
+ const bytes = value => value instanceof Blob ? value.arrayBuffer() : value instanceof ReadableStream ? new Response(value).arrayBuffer() : value;
21
+ export class DemoR2 {
22
+ constructor(host, binding) { this.host = host; this.binding = binding; }
23
+ call(method, ...args) { return this.host.r2(this.binding, method, args); }
24
+ async get(key, options) { return object(await this.call('get', key, options)); }
25
+ async head(key) { return object(await this.call('head', key)); }
26
+ async put(key, body, options) { return object(await this.call('put', key, await bytes(body), options)); }
27
+ delete(keys) { return this.call('delete', keys); }
28
+ async list(options) { const page = await this.call('list', options); page.objects = page.objects.map(object); return page; }
29
+ async createMultipartUpload(key, options) { return this.resumeMultipartUpload(key, await this.call('createMultipartUpload', key, options)); }
30
+ resumeMultipartUpload(key, uploadId) {
31
+ return { key, uploadId,
32
+ uploadPart: async (part, body, options) => this.call('uploadPart', key, uploadId, part, await bytes(body), options),
33
+ abort: () => this.call('abort', key, uploadId),
34
+ complete: async parts => object(await this.call('complete', key, uploadId, parts)),
35
+ };
36
+ }
37
+ }
@@ -0,0 +1,43 @@
1
+ export const prefixFor = (id, binding) => `${id}/${binding}/`;
2
+ function metadata(value, prefix) {
3
+ if (!value) return value;
4
+ const result = {};
5
+ for (const key of ['key', 'version', 'size', 'etag', 'httpEtag', 'uploaded', 'httpMetadata', 'customMetadata', 'range', 'storageClass', 'body']) {
6
+ if (value[key] !== undefined) result[key] = value[key];
7
+ }
8
+ result.key = result.key.slice(prefix.length);
9
+ if (value.checksums) result.checksums = Object.fromEntries(['md5', 'sha1', 'sha256', 'sha384', 'sha512'].filter(key => value.checksums[key]).map(key => [key, value.checksums[key]]));
10
+ return result;
11
+ }
12
+ export async function r2Call(bucket, storage, id, binding, method, args) {
13
+ const prefix = prefixFor(id, binding), key = value => prefix + String(value);
14
+ if (method === 'list') {
15
+ const options = args[0] ?? {};
16
+ const page = await bucket.list({ ...options, prefix: key(options.prefix ?? ''), ...(options.startAfter ? { startAfter: key(options.startAfter) } : {}) });
17
+ return { ...page, objects: page.objects.map(value => metadata(value, prefix)), delimitedPrefixes: page.delimitedPrefixes.map(value => value.slice(prefix.length)) };
18
+ }
19
+ if (method === 'delete') return bucket.delete(Array.isArray(args[0]) ? args[0].map(key) : key(args[0]));
20
+ if (['get', 'head', 'put'].includes(method)) return metadata(await bucket[method](key(args[0]), ...args.slice(1)), prefix);
21
+ if (method === 'createMultipartUpload') {
22
+ const upload = await bucket.createMultipartUpload(key(args[0]), args[1]);
23
+ try { await storage.put(`upload:${upload.uploadId}`, { key: key(args[0]), uploadId: upload.uploadId }); }
24
+ catch (error) { await upload.abort(); throw error; }
25
+ return upload.uploadId;
26
+ }
27
+ if (['uploadPart', 'abort', 'complete'].includes(method)) {
28
+ const upload = bucket.resumeMultipartUpload(key(args[0]), args[1]);
29
+ const result = await upload[method](...args.slice(2));
30
+ if (method !== 'uploadPart') await storage.delete(`upload:${args[1]}`);
31
+ return method === 'complete' ? metadata(result, prefix) : result;
32
+ }
33
+ throw new Error(`Unsupported R2 method: ${method}`);
34
+ }
35
+ export async function eraseR2(bucket, storage, id) {
36
+ if (!bucket) return;
37
+ for (const { key, uploadId } of (await storage.list({ prefix: 'upload:' })).values()) await bucket.resumeMultipartUpload(key, uploadId).abort();
38
+ for (;;) {
39
+ const page = await bucket.list({ prefix: `${id}/`, limit: 1000 });
40
+ if (!page.objects.length) break;
41
+ await bucket.delete(page.objects.map(value => value.key));
42
+ }
43
+ }
@@ -0,0 +1,37 @@
1
+ import { objectClass } from './config.mjs';
2
+ import { application, loaded, bundle } from './loader.mjs';
3
+ import { r2Call, eraseR2 } from './r2.mjs';
4
+
5
+ export async function database(host, binding, method, args) {
6
+ await host.active();
7
+ if (!bundle.bindings.databases.includes(binding) || !['query', 'batch', 'exec'].includes(method)) throw new Error(`Unknown D1 binding or method: ${binding}.${method}`);
8
+ const { ctx, env, state } = host, id = ctx.id.toString();
9
+ const stub = binding === bundle.bindings.databases[0] ? application(env, ctx, id, state.runtime)
10
+ : ctx.facets.get(`database:${binding}`, () => ({ class: loaded(env, ctx, id, state.runtime).getDurableObjectClass('DemoDatabase') }));
11
+ return stub[method](...args);
12
+ }
13
+ export async function object(host, binding, id, method, args) {
14
+ await host.active();
15
+ const declared = bundle.bindings.objects.find(item => item.name === binding);
16
+ if (!declared || !/^[a-f0-9]{64}$/.test(id)) throw new Error(`Unknown Durable Object binding or id: ${binding}`);
17
+ const state = host.state, name = `object:${binding}:${id}`;
18
+ await host.ctx.storage.put(`facet:${name}`, true);
19
+ if (host.state !== state || state.phase === 'ending') throw new Error('demo-ended');
20
+ const stub = host.ctx.facets.get(name, () => ({ class: loaded(host.env, host.ctx, host.ctx.id.toString(), host.state.runtime).getDurableObjectClass(objectClass(binding)), id }));
21
+ if (['constructor', 'then'].includes(method) || method.startsWith('_')) throw new Error('Invalid Durable Object method');
22
+ return stub[method](...args);
23
+ }
24
+ export async function r2(host, binding, method, args) {
25
+ await host.active();
26
+ if (!bundle.bindings.buckets.includes(binding)) throw new Error(`Unknown R2 binding: ${binding}`);
27
+ const operation = r2Call(host.env.NATIVE_BUCKET, host.ctx.storage, host.ctx.id.toString(), binding, method, args);
28
+ host.r2Pending ??= new Set(); host.r2Pending.add(operation);
29
+ try { return await operation; } finally { host.r2Pending.delete(operation); }
30
+ }
31
+ export async function eraseResources(host) {
32
+ host.ctx.facets.delete('app');
33
+ for (const binding of bundle.bindings.databases.slice(1)) host.ctx.facets.delete(`database:${binding}`);
34
+ for (const key of (await host.ctx.storage.list({ prefix: 'facet:' })).keys()) host.ctx.facets.delete(key.slice(6));
35
+ await Promise.allSettled(host.r2Pending ?? []);
36
+ await eraseR2(host.env.NATIVE_BUCKET, host.ctx.storage, host.ctx.id.toString());
37
+ }