Lines 1-39javascript
1import crypto from "node:crypto";
2import fs from "node:fs";
3import { createRequire } from "node:module";
4import path from "node:path";
5import { __buildJobStateAccessor, createSDKTrigger, fetchRegisteredJobs } from "terse-sdk";
6import { getRun, resumeHook, start } from "workflow/api";
7import { setWorld } from "workflow/runtime";
8import { createTerseWorld } from "../../terseWorld.js";
9import { transformJobSource } from "./jobMacro.js";
10import { TerseWorkflowBuilder } from "./terseWorkflowBuilder.js";
11let runtimePromise = null;
12export function getDurableRuntime(cwd = process.cwd()) {
13 return (runtimePromise ??= startDurableRuntime(cwd));
15async function startDurableRuntime(cwd) {
16 const out = path.join(cwd, ".terse", "wf");
17 const workflowFnByJob = isBuildFresh(out, cwd) ? loadWorkflowArtifacts(out) : await buildWorkflowArtifacts(cwd);
18 const world = createTerseWorld();
20 const require = createRequire(import.meta.url);
21 world.registerHandler("__wkf_step_", require(path.join(out, "steps.cjs")).POST);
MediumDynamic Require
Package source references dynamic require/import behavior.
dist/providers/typescript/durableRuntime.jsView on unpkg · L19 22 world.registerHandler("__wkf_workflow_", require(path.join(out, "workflows.cjs")).POST);
23 const manifest = JSON.parse(fs.readFileSync(path.join(out, "manifest.json"), "utf8"));
24 const workflowIdByJob = new Map();
26 for (const [name, { fnName, file }] of workflowFnByJob) {
27 const workflowId = lookupWorkflowId(manifest, fnName);
29 workflowIdByJob.set(name, workflowId);
31 missing.push({ name, file });
33 if (missing.length > 0) {
34 const lines = missing.map(m => ` - "${m.name}" (${m.file})`).join("\n");
35 throw new Error(`The durable build produced no "use workflow" for ${missing.length} job(s):\n${lines}\n\nEach file was transformed but its workflow never reached the manifest, which almost always means the file failed to compile during the bundle. Check that each file builds on its own, then re-run.`);
37 // start() re-enqueues recovered (pending/running) runs and begins draining the queue.
38 // It is NOT called during construction: a hook-resume must journal its payload first,
39 // or the re-enqueued replay races the payload write, re-arms the raced sleep, and