@lark-base-open/base-database-toolchain 0.0.0-stage → 1.0.0
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 +8 -0
- package/LICENSE +6 -0
- package/README.md +48 -2
- package/bin/base-database-toolchain.mjs +22 -0
- package/dist/app-builder/core.d.mts +37 -0
- package/dist/app-builder/core.mjs +716 -0
- package/dist/app-builder/package.json +1 -0
- package/dist/app-builder/schema.d.ts +3 -0
- package/dist/app-builder/schema.js +71 -0
- package/dist/contract-conversion.d.ts +41 -0
- package/dist/contract-conversion.js +116 -0
- package/dist/contract-extractor.d.ts +23 -0
- package/dist/contract-extractor.js +245 -0
- package/dist/durable-runner.d.ts +102 -0
- package/dist/durable-runner.js +128 -0
- package/dist/go-adapter.d.mts +4 -0
- package/dist/go-adapter.mjs +776 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/dist/sdk-codegen.d.ts +16 -0
- package/dist/sdk-codegen.js +208 -0
- package/dist/trigger-validation.d.ts +75 -0
- package/dist/trigger-validation.js +73 -0
- package/npm-shrinkwrap.json +1167 -0
- package/package.json +73 -4
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
3
|
+
import { setTimeout as delay } from 'node:timers/promises';
|
|
4
|
+
export function parseDurableEvents(values) {
|
|
5
|
+
return values.map((value) => {
|
|
6
|
+
const separator = value.indexOf('=');
|
|
7
|
+
const name = value.slice(0, separator);
|
|
8
|
+
if (separator < 1 || !/^[A-Za-z][A-Za-z0-9_]{0,63}$/.test(name))
|
|
9
|
+
throw new Error('Invalid --event: expected name=JSON');
|
|
10
|
+
try {
|
|
11
|
+
return { name, data: JSON.parse(value.slice(separator + 1)) };
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
throw new Error(`Invalid JSON for event ${name}`);
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
/** In-memory P0 drive loop; the caller supplies its request context, never an identity. */
|
|
19
|
+
export async function driveLocalDurable(executor, functionId, input, ctx, options = {}) {
|
|
20
|
+
const steps = [];
|
|
21
|
+
const inbox = [...(options.events ?? [])];
|
|
22
|
+
const wait = options.wait ?? delay;
|
|
23
|
+
const runId = `local:${randomUUID()}`;
|
|
24
|
+
const context = new AsyncLocalStorage();
|
|
25
|
+
const runtimeKey = Symbol.for('base-site.runtime');
|
|
26
|
+
const globals = globalThis;
|
|
27
|
+
const previous = globals[runtimeKey];
|
|
28
|
+
const runtime = previous;
|
|
29
|
+
if (previous && (runtime?.v !== 1 || !runtime.context?.run))
|
|
30
|
+
throw new Error('DURABLE_VERSION_UNSUPPORTED: incompatible runtime context');
|
|
31
|
+
const storage = runtime?.context ?? context;
|
|
32
|
+
if (!previous)
|
|
33
|
+
globals[runtimeKey] = { v: 1, context: storage };
|
|
34
|
+
let pending;
|
|
35
|
+
let progress;
|
|
36
|
+
try {
|
|
37
|
+
for (let seq = 1;; seq++) {
|
|
38
|
+
const issuedAt = Date.now();
|
|
39
|
+
const deadline = issuedAt + 300_000;
|
|
40
|
+
const request = {
|
|
41
|
+
v: 1,
|
|
42
|
+
runId,
|
|
43
|
+
functionId,
|
|
44
|
+
input: structuredClone(input),
|
|
45
|
+
steps: structuredClone(steps),
|
|
46
|
+
invocation: {
|
|
47
|
+
seq,
|
|
48
|
+
attempt: pending?.attempt ?? 1,
|
|
49
|
+
issuedAt,
|
|
50
|
+
deadline,
|
|
51
|
+
expiresAt: deadline,
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
const op = await storage.run({ kind: 'durable', ctx, durable: { runId, functionId, seq } }, () => executor.execute(request, {
|
|
55
|
+
signal: new AbortController().signal,
|
|
56
|
+
deadline,
|
|
57
|
+
ctx,
|
|
58
|
+
}));
|
|
59
|
+
if (op.v !== 1 || op.seq !== seq)
|
|
60
|
+
throw new Error('PROTOCOL_REJECTED: invalid Durable version or sequence');
|
|
61
|
+
options.onOp?.(op);
|
|
62
|
+
if (op.progress !== undefined)
|
|
63
|
+
progress = op.progress;
|
|
64
|
+
if (op.op === 'done')
|
|
65
|
+
return { ok: true, output: op.output, steps, progress };
|
|
66
|
+
if (op.op === 'fail')
|
|
67
|
+
return { ok: false, error: op.error, steps, progress };
|
|
68
|
+
const attempt = pending?.key === op.key ? pending.attempt : 1;
|
|
69
|
+
const startedAt = pending?.key === op.key ? pending.startedAt : issuedAt;
|
|
70
|
+
if (op.op === 'step') {
|
|
71
|
+
if (!op.ok &&
|
|
72
|
+
op.error.retryable !== false &&
|
|
73
|
+
attempt <= op.policy.retries) {
|
|
74
|
+
pending = { key: op.key, attempt: attempt + 1, startedAt };
|
|
75
|
+
if (!options.fast)
|
|
76
|
+
await wait(Math.min(1000 * 2 ** (attempt - 1), 60_000));
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
steps.push({
|
|
80
|
+
key: op.key,
|
|
81
|
+
kind: 'run',
|
|
82
|
+
ok: op.ok,
|
|
83
|
+
...(op.ok ? { value: op.value } : { error: op.error }),
|
|
84
|
+
attempts: attempt,
|
|
85
|
+
startedAt,
|
|
86
|
+
endedAt: Date.now(),
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
else if (op.op === 'sleep') {
|
|
90
|
+
if (options.realSleep) {
|
|
91
|
+
// Node timers overflow above ~24 days; SDK durations permit 30 days.
|
|
92
|
+
for (let remaining = op.durationMs; remaining > 0; remaining -= Math.min(remaining, 2_147_483_647))
|
|
93
|
+
await wait(Math.min(remaining, 2_147_483_647));
|
|
94
|
+
}
|
|
95
|
+
steps.push({
|
|
96
|
+
key: op.key,
|
|
97
|
+
kind: 'sleep',
|
|
98
|
+
ok: true,
|
|
99
|
+
attempts: 1,
|
|
100
|
+
startedAt,
|
|
101
|
+
endedAt: Date.now(),
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
else if (op.op === 'wait') {
|
|
105
|
+
const index = inbox.findIndex((event) => event.name === op.event);
|
|
106
|
+
const event = index < 0 ? undefined : inbox.splice(index, 1)[0];
|
|
107
|
+
steps.push({
|
|
108
|
+
key: op.key,
|
|
109
|
+
kind: 'wait',
|
|
110
|
+
ok: true,
|
|
111
|
+
value: event ? event.data : null,
|
|
112
|
+
...(!event ? { timedOut: true } : {}),
|
|
113
|
+
attempts: 1,
|
|
114
|
+
startedAt,
|
|
115
|
+
endedAt: Date.now(),
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
throw new Error('PROTOCOL_REJECTED: unknown Durable operation');
|
|
120
|
+
}
|
|
121
|
+
pending = undefined;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
finally {
|
|
125
|
+
if (!previous)
|
|
126
|
+
delete globals[runtimeKey];
|
|
127
|
+
}
|
|
128
|
+
}
|