@lark-base-open/base-database-toolchain 0.0.0-stage → 1.1.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.
Files changed (52) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/LICENSE +6 -0
  3. package/README.md +82 -2
  4. package/bin/base-database-dev.mjs +4 -0
  5. package/bin/base-database-toolchain.mjs +22 -0
  6. package/dist/app-builder/core.d.mts +37 -0
  7. package/dist/app-builder/core.mjs +716 -0
  8. package/dist/app-builder/package.json +1 -0
  9. package/dist/app-builder/schema.d.ts +3 -0
  10. package/dist/app-builder/schema.js +71 -0
  11. package/dist/contract-conversion.d.ts +41 -0
  12. package/dist/contract-conversion.js +116 -0
  13. package/dist/contract-extractor.d.ts +23 -0
  14. package/dist/contract-extractor.js +245 -0
  15. package/dist/data/development-pairing.json +11 -0
  16. package/dist/data/template-hmr-scripts.json +9 -0
  17. package/dist/dependency-inputs.d.ts +3 -0
  18. package/dist/dependency-inputs.js +18 -0
  19. package/dist/dev-ipc-worker.d.ts +1 -0
  20. package/dist/dev-ipc-worker.js +35 -0
  21. package/dist/dev-redaction.d.ts +1 -0
  22. package/dist/dev-redaction.js +3 -0
  23. package/dist/dev-snapshot.d.ts +3 -0
  24. package/dist/dev-snapshot.js +98 -0
  25. package/dist/dev-vite-server.d.ts +14 -0
  26. package/dist/dev-vite-server.js +213 -0
  27. package/dist/dev-worker.d.mts +1 -0
  28. package/dist/dev-worker.mjs +134 -0
  29. package/dist/development-pairing.generated.d.ts +11 -0
  30. package/dist/development-pairing.generated.js +12 -0
  31. package/dist/development.d.ts +6 -0
  32. package/dist/development.js +6 -0
  33. package/dist/durable-runner.d.ts +102 -0
  34. package/dist/durable-runner.js +128 -0
  35. package/dist/function-batch.d.ts +48 -0
  36. package/dist/function-batch.js +99 -0
  37. package/dist/go-adapter.d.mts +11 -0
  38. package/dist/go-adapter.mjs +847 -0
  39. package/dist/index.d.ts +8 -0
  40. package/dist/index.js +8 -0
  41. package/dist/page-bootstrap.d.ts +3 -0
  42. package/dist/page-bootstrap.js +178 -0
  43. package/dist/page-inspection.d.ts +11 -0
  44. package/dist/page-inspection.js +49 -0
  45. package/dist/page-link-guard.d.ts +12 -0
  46. package/dist/page-link-guard.js +137 -0
  47. package/dist/sdk-codegen.d.ts +16 -0
  48. package/dist/sdk-codegen.js +208 -0
  49. package/dist/trigger-validation.d.ts +75 -0
  50. package/dist/trigger-validation.js +73 -0
  51. package/npm-shrinkwrap.json +1193 -0
  52. package/package.json +87 -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
+ }
@@ -0,0 +1,48 @@
1
+ export interface DMLBatchCapability {
2
+ version: 1;
3
+ maxGroups: number;
4
+ maxRowsPerGroup: 1;
5
+ maxTotalRows: number;
6
+ }
7
+ export interface FunctionExecutionSnapshot {
8
+ version: 1;
9
+ environment: 'preview' | 'prod';
10
+ view: 'draft' | 'online';
11
+ sourceDigest: string;
12
+ }
13
+ export interface FunctionBatchSnapshot {
14
+ siteToken: string;
15
+ environment: 'preview' | 'prod';
16
+ versionId: string;
17
+ skillHash: string;
18
+ functions: Record<string, {
19
+ batchCapability?: DMLBatchCapability;
20
+ inputSchema?: Record<string, unknown>;
21
+ executionSnapshot?: FunctionExecutionSnapshot;
22
+ }>;
23
+ }
24
+ export interface BoundFunctionBatchSnapshot extends FunctionBatchSnapshot {
25
+ runtimeProfile: string;
26
+ localSources: Record<string, string>;
27
+ }
28
+ /** Closed published contract; generated types never establish runtime permission. */
29
+ export declare function normalizeDMLBatchCapability(functionType: number, capability: unknown): DMLBatchCapability | undefined;
30
+ /** Execution provenance describes the observed provider view, not a future lock. */
31
+ export declare function normalizeFunctionExecutionSnapshot(value: unknown, environment: 'preview' | 'prod'): FunctionExecutionSnapshot | undefined;
32
+ export declare function bindFunctionBatchSnapshot(options: {
33
+ siteToken: string;
34
+ runtimeProfile: string;
35
+ localSources: Record<string, string>;
36
+ snapshot?: FunctionBatchSnapshot;
37
+ previous?: unknown;
38
+ }): BoundFunctionBatchSnapshot | undefined;
39
+ export declare function withFunctionBatchCapability<T extends {
40
+ functionId: string;
41
+ functionType: number;
42
+ }>(contract: T, snapshot: BoundFunctionBatchSnapshot | undefined, localSources: Record<string, string>): T & {
43
+ batchCapability?: DMLBatchCapability;
44
+ };
45
+ /** An older SDK keeps its single-call clients; callers must report this warning. */
46
+ export declare function batchClientWarning(data: unknown, files: {
47
+ content: string;
48
+ }[]): string | undefined;
@@ -0,0 +1,99 @@
1
+ function record(value) {
2
+ return !!value && typeof value === 'object' && !Array.isArray(value);
3
+ }
4
+ /** Closed published contract; generated types never establish runtime permission. */
5
+ export function normalizeDMLBatchCapability(functionType, capability) {
6
+ if (functionType !== 1 || !record(capability))
7
+ return;
8
+ if (Object.keys(capability).some((key) => !['version', 'maxGroups', 'maxRowsPerGroup', 'maxTotalRows'].includes(key)) ||
9
+ capability.version !== 1 ||
10
+ capability.maxRowsPerGroup !== 1 ||
11
+ !Number.isSafeInteger(capability.maxGroups) ||
12
+ capability.maxGroups < 1 ||
13
+ capability.maxGroups > 200 ||
14
+ !Number.isSafeInteger(capability.maxTotalRows) ||
15
+ capability.maxTotalRows < 1 ||
16
+ capability.maxTotalRows > 200)
17
+ return;
18
+ return {
19
+ version: 1,
20
+ maxGroups: capability.maxGroups,
21
+ maxRowsPerGroup: 1,
22
+ maxTotalRows: capability.maxTotalRows,
23
+ };
24
+ }
25
+ /** Execution provenance describes the observed provider view, not a future lock. */
26
+ export function normalizeFunctionExecutionSnapshot(value, environment) {
27
+ if (!record(value) ||
28
+ Object.keys(value).length !== 4 ||
29
+ Object.keys(value).some((key) => !['version', 'environment', 'view', 'sourceDigest'].includes(key)) ||
30
+ value.version !== 1 ||
31
+ value.environment !== environment ||
32
+ value.view !== (environment === 'preview' ? 'draft' : 'online') ||
33
+ typeof value.sourceDigest !== 'string' ||
34
+ !/^[a-f0-9]{64}$/.test(value.sourceDigest))
35
+ return;
36
+ return value;
37
+ }
38
+ export function bindFunctionBatchSnapshot(options) {
39
+ const { snapshot, previous, siteToken, runtimeProfile, localSources } = options;
40
+ if (snapshot && snapshot.siteToken !== siteToken) {
41
+ throw new Error('BATCH_CAPABILITY_SITE_MISMATCH: discovery must use this project site');
42
+ }
43
+ if (snapshot &&
44
+ (!['preview', 'prod'].includes(snapshot.environment) ||
45
+ typeof snapshot.versionId !== 'string' ||
46
+ !/^\d+$/.test(snapshot.versionId) ||
47
+ typeof snapshot.skillHash !== 'string' ||
48
+ !record(snapshot.functions))) {
49
+ throw new Error('BATCH_CAPABILITY_SNAPSHOT_INVALID: discovery requires a valid site, environment and Function snapshot');
50
+ }
51
+ if (snapshot)
52
+ return { ...snapshot, localSources: { ...localSources }, runtimeProfile };
53
+ if (record(previous) &&
54
+ previous.siteToken === siteToken &&
55
+ previous.runtimeProfile === runtimeProfile &&
56
+ ['preview', 'prod'].includes(String(previous.environment)) &&
57
+ typeof previous.versionId === 'string' &&
58
+ /^\d+$/.test(previous.versionId) &&
59
+ typeof previous.skillHash === 'string' &&
60
+ record(previous.functions) &&
61
+ record(previous.localSources)) {
62
+ return previous;
63
+ }
64
+ return;
65
+ }
66
+ export function withFunctionBatchCapability(contract, snapshot, localSources) {
67
+ const id = contract.functionId;
68
+ const remote = snapshot && Object.hasOwn(snapshot.functions, id)
69
+ ? snapshot.functions[id]
70
+ : undefined;
71
+ const capability = snapshot &&
72
+ typeof localSources[id] === 'string' &&
73
+ snapshot.localSources[id] === localSources[id] &&
74
+ normalizeFunctionExecutionSnapshot(remote?.executionSnapshot, snapshot.environment)
75
+ ? normalizeDMLBatchCapability(contract.functionType, remote?.batchCapability)
76
+ : undefined;
77
+ return capability && record(remote?.inputSchema)
78
+ ? {
79
+ ...contract,
80
+ batchCapability: capability,
81
+ ...('input' in contract ? { input: remote.inputSchema } : {}),
82
+ ...('inputSchema' in contract
83
+ ? { inputSchema: remote.inputSchema }
84
+ : {}),
85
+ }
86
+ : contract;
87
+ }
88
+ /** An older SDK keeps its single-call clients; callers must report this warning. */
89
+ export function batchClientWarning(data, files) {
90
+ if (!record(data) || data.client !== true || !Array.isArray(data.functions))
91
+ return;
92
+ const ids = data.functions
93
+ .filter((fn) => record(fn) && typeof fn.functionId === 'string' && fn.batchCapability)
94
+ .map((fn) => fn.functionId);
95
+ if (!ids.length ||
96
+ files.some((file) => file.content.includes('export const batchFunctions')))
97
+ return;
98
+ return `BATCH_SDK_UNSUPPORTED: The installed SDK generated no SQL batch DML client for: ${ids.join(', ')}. Generation continues with single-call clients; Page batch writes are unavailable. Do not add batch types manually. For multiple writes, obtain the user's acceptance of non-atomic single writes before invoking them individually. When a batch-capable SDK release is available, upgrade with user authorization, then run contracts generate and check --json again.`;
99
+ }
@@ -0,0 +1,11 @@
1
+ export declare function runCompiler(request: any): Promise<{
2
+ protocolVersion: 1;
3
+ bootstrap: import("./page-bootstrap.js").DevBootstrapStatus;
4
+ warnings: import("./page-link-guard.js").LinkGuardWarning[];
5
+ ok: boolean;
6
+ operation: any;
7
+ } | {
8
+ ok: boolean;
9
+ operation: any;
10
+ warnings?: any[] | undefined;
11
+ }>;