@kubohiroya/turbowarp-http-server 0.2.0 → 0.3.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/README.ja.md +32 -14
- package/README.md +104 -43
- package/assets/block-icon.svg +1 -0
- package/dist/cli.js +131 -17
- package/dist/cli.js.map +1 -1
- package/dist/compiler/adapters/cloudflare.d.ts +2 -0
- package/dist/compiler/adapters/cloudflare.js +477 -0
- package/dist/compiler/adapters/cloudflare.js.map +1 -0
- package/dist/compiler/adapters/firebase.d.ts +2 -0
- package/dist/compiler/adapters/firebase.js +489 -0
- package/dist/compiler/adapters/firebase.js.map +1 -0
- package/dist/compiler/adapters/index.d.ts +2 -0
- package/dist/compiler/adapters/index.js +3 -0
- package/dist/compiler/adapters/index.js.map +1 -0
- package/dist/compiler/binary/codec.d.ts +4 -0
- package/dist/compiler/binary/codec.js +45 -0
- package/dist/compiler/binary/codec.js.map +1 -0
- package/dist/compiler/binary/error.d.ts +3 -0
- package/dist/compiler/binary/error.js +21 -0
- package/dist/compiler/binary/error.js.map +1 -0
- package/dist/compiler/binary/index.d.ts +4 -0
- package/dist/compiler/binary/index.js +5 -0
- package/dist/compiler/binary/index.js.map +1 -0
- package/dist/compiler/binary/runtime.d.ts +18 -0
- package/dist/compiler/binary/runtime.js +123 -0
- package/dist/compiler/binary/runtime.js.map +1 -0
- package/dist/compiler/binary/types.d.ts +17 -0
- package/dist/compiler/binary/types.js +2 -0
- package/dist/compiler/binary/types.js.map +1 -0
- package/dist/compiler/feature-flags.d.ts +4 -0
- package/dist/compiler/feature-flags.js +4 -0
- package/dist/compiler/feature-flags.js.map +1 -0
- package/dist/compiler/index.d.ts +35 -0
- package/dist/compiler/index.js +84 -0
- package/dist/compiler/index.js.map +1 -0
- package/dist/compiler/ir-v2/canonical.d.ts +3 -0
- package/dist/compiler/ir-v2/canonical.js +50 -0
- package/dist/compiler/ir-v2/canonical.js.map +1 -0
- package/dist/compiler/ir-v2/index.d.ts +4 -0
- package/dist/compiler/ir-v2/index.js +5 -0
- package/dist/compiler/ir-v2/index.js.map +1 -0
- package/dist/compiler/ir-v2/parse.d.ts +10 -0
- package/dist/compiler/ir-v2/parse.js +753 -0
- package/dist/compiler/ir-v2/parse.js.map +1 -0
- package/dist/compiler/ir-v2/strict-json.d.ts +4 -0
- package/dist/compiler/ir-v2/strict-json.js +133 -0
- package/dist/compiler/ir-v2/strict-json.js.map +1 -0
- package/dist/compiler/ir-v2/types.d.ts +412 -0
- package/dist/compiler/ir-v2/types.js +84 -0
- package/dist/compiler/ir-v2/types.js.map +1 -0
- package/dist/compiler/kvs/lowering.d.ts +13 -0
- package/dist/compiler/kvs/lowering.js +77 -0
- package/dist/compiler/kvs/lowering.js.map +1 -0
- package/dist/compiler/manifest/error.d.ts +7 -0
- package/dist/compiler/manifest/error.js +9 -0
- package/dist/compiler/manifest/error.js.map +1 -0
- package/dist/compiler/manifest/index.d.ts +6 -0
- package/dist/compiler/manifest/index.js +7 -0
- package/dist/compiler/manifest/index.js.map +1 -0
- package/dist/compiler/manifest/operations.d.ts +12 -0
- package/dist/compiler/manifest/operations.js +60 -0
- package/dist/compiler/manifest/operations.js.map +1 -0
- package/dist/compiler/manifest/parse.d.ts +9 -0
- package/dist/compiler/manifest/parse.js +280 -0
- package/dist/compiler/manifest/parse.js.map +1 -0
- package/dist/compiler/manifest/registry.d.ts +3 -0
- package/dist/compiler/manifest/registry.js +48 -0
- package/dist/compiler/manifest/registry.js.map +1 -0
- package/dist/compiler/manifest/resolve.d.ts +14 -0
- package/dist/compiler/manifest/resolve.js +116 -0
- package/dist/compiler/manifest/resolve.js.map +1 -0
- package/dist/compiler/manifest/types.d.ts +95 -0
- package/dist/compiler/manifest/types.js +3 -0
- package/dist/compiler/manifest/types.js.map +1 -0
- package/dist/compiler/named-body/index.d.ts +1 -0
- package/dist/compiler/named-body/index.js +2 -0
- package/dist/compiler/named-body/index.js.map +1 -0
- package/dist/compiler/named-body/lowering.d.ts +18 -0
- package/dist/compiler/named-body/lowering.js +83 -0
- package/dist/compiler/named-body/lowering.js.map +1 -0
- package/dist/compiler/pipeline/compiler.d.ts +18 -0
- package/dist/compiler/pipeline/compiler.js +105 -0
- package/dist/compiler/pipeline/compiler.js.map +1 -0
- package/dist/compiler/pipeline/core-generator.d.ts +3 -0
- package/dist/compiler/pipeline/core-generator.js +483 -0
- package/dist/compiler/pipeline/core-generator.js.map +1 -0
- package/dist/compiler/pipeline/index.d.ts +5 -0
- package/dist/compiler/pipeline/index.js +6 -0
- package/dist/compiler/pipeline/index.js.map +1 -0
- package/dist/compiler/pipeline/registry.d.ts +7 -0
- package/dist/compiler/pipeline/registry.js +17 -0
- package/dist/compiler/pipeline/registry.js.map +1 -0
- package/dist/compiler/pipeline/requirements.d.ts +9 -0
- package/dist/compiler/pipeline/requirements.js +135 -0
- package/dist/compiler/pipeline/requirements.js.map +1 -0
- package/dist/compiler/pipeline/types.d.ts +65 -0
- package/dist/compiler/pipeline/types.js +2 -0
- package/dist/compiler/pipeline/types.js.map +1 -0
- package/dist/compiler/runtime/error.d.ts +6 -0
- package/dist/compiler/runtime/error.js +9 -0
- package/dist/compiler/runtime/error.js.map +1 -0
- package/dist/compiler/runtime/index.d.ts +1 -0
- package/dist/compiler/runtime/index.js +2 -0
- package/dist/compiler/runtime/index.js.map +1 -0
- package/dist/compiler/structured-data/index.d.ts +3 -0
- package/dist/compiler/structured-data/index.js +4 -0
- package/dist/compiler/structured-data/index.js.map +1 -0
- package/dist/compiler/structured-data/lowering.d.ts +30 -0
- package/dist/compiler/structured-data/lowering.js +202 -0
- package/dist/compiler/structured-data/lowering.js.map +1 -0
- package/dist/compiler/structured-data/path.d.ts +2 -0
- package/dist/compiler/structured-data/path.js +60 -0
- package/dist/compiler/structured-data/path.js.map +1 -0
- package/dist/compiler/structured-data/runtime.d.ts +20 -0
- package/dist/compiler/structured-data/runtime.js +210 -0
- package/dist/compiler/structured-data/runtime.js.map +1 -0
- package/dist/compiler/turbowarp-v2.d.ts +8 -0
- package/dist/compiler/turbowarp-v2.js +479 -0
- package/dist/compiler/turbowarp-v2.js.map +1 -0
- package/dist/compiler/validator/index.d.ts +4 -0
- package/dist/compiler/validator/index.js +5 -0
- package/dist/compiler/validator/index.js.map +1 -0
- package/dist/compiler/validator/ir-validator.d.ts +10 -0
- package/dist/compiler/validator/ir-validator.js +463 -0
- package/dist/compiler/validator/ir-validator.js.map +1 -0
- package/dist/compiler/validator/resource-lifetime.d.ts +18 -0
- package/dist/compiler/validator/resource-lifetime.js +106 -0
- package/dist/compiler/validator/resource-lifetime.js.map +1 -0
- package/dist/compiler/validator/turbowarp-subset.d.ts +3 -0
- package/dist/compiler/validator/turbowarp-subset.js +241 -0
- package/dist/compiler/validator/turbowarp-subset.js.map +1 -0
- package/dist/compiler/validator/types.d.ts +30 -0
- package/dist/compiler/validator/types.js +7 -0
- package/dist/compiler/validator/types.js.map +1 -0
- package/dist/config.d.ts +11 -0
- package/dist/config.js +12 -0
- package/dist/config.js.map +1 -0
- package/dist/digest-cli.js +0 -0
- package/dist/extension-manifest.json +102 -0
- package/dist/named-body.d.ts +57 -0
- package/dist/named-body.js +396 -0
- package/dist/named-body.js.map +1 -0
- package/dist/named-data-namespace.d.ts +3 -0
- package/dist/named-data-namespace.js +6 -0
- package/dist/named-data-namespace.js.map +1 -0
- package/dist/protocol.d.ts +13 -1
- package/dist/protocol.js +34 -0
- package/dist/protocol.js.map +1 -1
- package/dist/resource-named-body-provider.d.ts +8 -0
- package/dist/resource-named-body-provider.js +88 -0
- package/dist/resource-named-body-provider.js.map +1 -0
- package/dist/server.d.ts +10 -1
- package/dist/server.js +37 -7
- package/dist/server.js.map +1 -1
- package/dist/turbowarp-http-server.js +1614 -1099
- package/docs/binary-resource-semantics.ja.md +84 -0
- package/docs/cloudflare-worker-compiler.ja.md +90 -0
- package/docs/compiler-manifest-registry.ja.md +79 -0
- package/docs/compiler-platform-pipeline.ja.md +94 -0
- package/docs/deploy-ir-v2.ja.md +123 -0
- package/docs/fixed-input-editors.ja.md +23 -0
- package/docs/index.html +86 -0
- package/docs/ir-v2-conformance.ja.md +65 -0
- package/docs/named-response-body.ja.md +120 -0
- package/docs/platform-storage-adapters.ja.md +99 -0
- package/docs/server-executable-subset.ja.md +88 -0
- package/docs/structured-data-lowering.ja.md +63 -0
- package/docs/turbowarp-http-server.architecture.html +4 -0
- package/examples/compiler/message-app.v2.ir.json +41 -0
- package/package.json +45 -20
- package/schemas/block-editor-fixed-inputs.schema.json +85 -0
- package/schemas/compiler-extension-manifest.schema.json +126 -0
- package/schemas/compiler-manifest-lock-v1.schema.json +48 -0
- package/schemas/deploy-ir-v2.schema.json +835 -0
- package/schemas/ir-v2-conformance-fixture.schema.json +44 -0
- package/src/block-definitions.json +76 -0
- package/src/cli.ts +136 -23
- package/src/compiler/adapters/cloudflare.ts +510 -0
- package/src/compiler/adapters/firebase.ts +538 -0
- package/src/compiler/adapters/index.ts +2 -0
- package/src/compiler/binary/codec.ts +61 -0
- package/src/compiler/binary/error.ts +25 -0
- package/src/compiler/binary/index.ts +4 -0
- package/src/compiler/binary/runtime.ts +133 -0
- package/src/compiler/binary/types.ts +26 -0
- package/src/compiler/feature-flags.ts +7 -0
- package/src/compiler/index.ts +114 -0
- package/src/compiler/ir-v2/canonical.ts +46 -0
- package/src/compiler/ir-v2/index.ts +9 -0
- package/src/compiler/ir-v2/parse.ts +921 -0
- package/src/compiler/ir-v2/strict-json.ts +129 -0
- package/src/compiler/ir-v2/types.ts +462 -0
- package/src/compiler/kvs/lowering.ts +88 -0
- package/src/compiler/manifest/error.ts +29 -0
- package/src/compiler/manifest/index.ts +17 -0
- package/src/compiler/manifest/operations.ts +175 -0
- package/src/compiler/manifest/parse.ts +390 -0
- package/src/compiler/manifest/registry.ts +74 -0
- package/src/compiler/manifest/resolve.ts +159 -0
- package/src/compiler/manifest/types.ts +112 -0
- package/src/compiler/named-body/index.ts +1 -0
- package/src/compiler/named-body/lowering.ts +186 -0
- package/src/compiler/pipeline/compiler.ts +138 -0
- package/src/compiler/pipeline/core-generator.ts +486 -0
- package/src/compiler/pipeline/index.ts +5 -0
- package/src/compiler/pipeline/registry.ts +18 -0
- package/src/compiler/pipeline/requirements.ts +139 -0
- package/src/compiler/pipeline/types.ts +59 -0
- package/src/compiler/runtime/error.ts +11 -0
- package/src/compiler/runtime/index.ts +1 -0
- package/src/compiler/structured-data/index.ts +3 -0
- package/src/compiler/structured-data/lowering.ts +314 -0
- package/src/compiler/structured-data/path.ts +53 -0
- package/src/compiler/structured-data/runtime.ts +221 -0
- package/src/compiler/turbowarp-v2.ts +610 -0
- package/src/compiler/validator/index.ts +4 -0
- package/src/compiler/validator/ir-validator.ts +812 -0
- package/src/compiler/validator/resource-lifetime.ts +153 -0
- package/src/compiler/validator/turbowarp-subset.ts +387 -0
- package/src/compiler/validator/types.ts +68 -0
- package/src/config.ts +1 -1
- package/src/extension.ts +87 -1
- package/src/fixed-input-editors.ts +181 -0
- package/src/fixed-input-schema.ts +15 -0
- package/src/named-body.ts +495 -0
- package/src/named-data-namespace.ts +6 -0
- package/src/protocol.ts +50 -1
- package/src/resource-named-body-provider.ts +106 -0
- package/src/server.ts +88 -8
- package/docs/turbowarp-http-server.architecture.visual-check.1440x900.dark.png +0 -0
- package/docs/turbowarp-http-server.architecture.visual-check.1440x900.light.png +0 -0
- package/docs/turbowarp-http-server.architecture.visual-check.2048x1320.dark.png +0 -0
- package/docs/turbowarp-http-server.architecture.visual-check.2048x1320.light.png +0 -0
- package/docs/turbowarp-http-server.architecture.visual-check.html +0 -32
- package/docs/turbowarp-http-server.architecture.visual-check.json +0 -547
- package/schemas/extension-manifest.schema.json +0 -49
- package/src/extension-manifest.ts +0 -149
|
@@ -0,0 +1,610 @@
|
|
|
1
|
+
import {extensionConfig} from '../config.js';
|
|
2
|
+
import {
|
|
3
|
+
DEPLOY_IR_V2_VERSION,
|
|
4
|
+
type CapabilityRequirementV2,
|
|
5
|
+
type CompilerDiagnosticV2,
|
|
6
|
+
type DeployIrV2,
|
|
7
|
+
type ExpressionIrV2,
|
|
8
|
+
type HttpMethodV2,
|
|
9
|
+
type RouteIrV2,
|
|
10
|
+
type SourceRefV2,
|
|
11
|
+
type StatementIrV2
|
|
12
|
+
} from './ir-v2/types.js';
|
|
13
|
+
import type {CompilerOpcodeRegistry, CompilerOpcodeRegistryEntry} from './manifest/types.js';
|
|
14
|
+
import {isKvsEntry, KvsLoweringContext} from './kvs/lowering.js';
|
|
15
|
+
import {lowerNamedBodyResponse, type NamedBodyArguments} from './named-body/lowering.js';
|
|
16
|
+
import {StructuredDataLoweringContext} from './structured-data/lowering.js';
|
|
17
|
+
import {validateTurboWarpServerSubset} from './validator/turbowarp-subset.js';
|
|
18
|
+
|
|
19
|
+
const PREFIX = `${extensionConfig.id}_`;
|
|
20
|
+
const HAT_OPCODE = `${PREFIX}whenHttpRequestReceived`;
|
|
21
|
+
const NAMED_RESPONSE_OPCODE = `${PREFIX}respondWithNamedBody`;
|
|
22
|
+
const METHODS = new Set<HttpMethodV2>(['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS']);
|
|
23
|
+
|
|
24
|
+
interface ScratchProject {
|
|
25
|
+
targets?: unknown;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
interface ScratchTarget {
|
|
29
|
+
isStage?: unknown;
|
|
30
|
+
name?: unknown;
|
|
31
|
+
blocks?: unknown;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
interface ScratchBlock {
|
|
35
|
+
opcode?: unknown;
|
|
36
|
+
next?: unknown;
|
|
37
|
+
parent?: unknown;
|
|
38
|
+
topLevel?: unknown;
|
|
39
|
+
inputs?: unknown;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
type BlockMap = Record<string, ScratchBlock>;
|
|
43
|
+
|
|
44
|
+
interface FrontendContext {
|
|
45
|
+
blocks: BlockMap;
|
|
46
|
+
targetIndex: number;
|
|
47
|
+
targetName: string;
|
|
48
|
+
registry?: CompilerOpcodeRegistry;
|
|
49
|
+
diagnostics: CompilerDiagnosticV2[];
|
|
50
|
+
capabilities: CapabilityRequirementV2[];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface CompileTurboWarpProjectV2Result {
|
|
54
|
+
ir: DeployIrV2;
|
|
55
|
+
diagnostics: CompilerDiagnosticV2[];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Compiles the locked server-executable TurboWarp subset directly into typed IR v2. */
|
|
59
|
+
export function compileTurboWarpProjectV2(
|
|
60
|
+
value: unknown,
|
|
61
|
+
registry?: CompilerOpcodeRegistry
|
|
62
|
+
): CompileTurboWarpProjectV2Result {
|
|
63
|
+
const subsetDiagnostics = validateTurboWarpServerSubset(value, registry).map(toCompilerDiagnostic);
|
|
64
|
+
if (subsetDiagnostics.length > 0) return {ir: emptyIr(), diagnostics: subsetDiagnostics};
|
|
65
|
+
|
|
66
|
+
const project = object(value) as ScratchProject;
|
|
67
|
+
if (!Array.isArray(project.targets)) throw new Error('TurboWarp project.targets must be an array.');
|
|
68
|
+
const diagnostics: CompilerDiagnosticV2[] = [];
|
|
69
|
+
const capabilities: CapabilityRequirementV2[] = [];
|
|
70
|
+
const routes: RouteIrV2[] = [];
|
|
71
|
+
const structured = new StructuredDataLoweringContext();
|
|
72
|
+
let discoveredHandlers = 0;
|
|
73
|
+
|
|
74
|
+
project.targets.forEach((rawTarget, targetIndex) => {
|
|
75
|
+
const target = object(rawTarget) as ScratchTarget;
|
|
76
|
+
const targetName = typeof target.name === 'string' ? target.name : `target-${targetIndex}`;
|
|
77
|
+
const blocks = blockMap(target.blocks);
|
|
78
|
+
const path = target.isStage === true ? '/' : `/${routeSegment(targetName)}`;
|
|
79
|
+
const context: FrontendContext = {
|
|
80
|
+
blocks,
|
|
81
|
+
targetIndex,
|
|
82
|
+
targetName,
|
|
83
|
+
...(registry === undefined ? {} : {registry}),
|
|
84
|
+
diagnostics,
|
|
85
|
+
capabilities
|
|
86
|
+
};
|
|
87
|
+
for (const [hatId, hat] of Object.entries(blocks)) {
|
|
88
|
+
if (hat.opcode !== HAT_OPCODE || hat.topLevel !== true) continue;
|
|
89
|
+
discoveredHandlers += 1;
|
|
90
|
+
const firstId = blockId(hat.next);
|
|
91
|
+
const hatSource = sourceRef(context, hatId, HAT_OPCODE);
|
|
92
|
+
if (firstId === null) {
|
|
93
|
+
report(context, 'TW2_EMPTY_HANDLER', 'HTTP request hat has no handler body.', hatSource);
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
const first = blocks[firstId];
|
|
97
|
+
const route =
|
|
98
|
+
first?.opcode === 'control_if'
|
|
99
|
+
? compileMethodGuard(context, firstId, path, hatSource, routes.length, structured)
|
|
100
|
+
: compileRouteBody(context, firstId, 'ALL', path, hatSource, routes.length, structured);
|
|
101
|
+
if (route !== null) routes.push(route);
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
diagnostics.push(...structured.diagnostics);
|
|
106
|
+
if (discoveredHandlers === 0) {
|
|
107
|
+
diagnostics.push({
|
|
108
|
+
severity: 'error',
|
|
109
|
+
code: 'TW2_NO_ROUTES',
|
|
110
|
+
message: 'No compilable HTTP request handlers were found.'
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
rejectDuplicateRoutes(routes, diagnostics);
|
|
114
|
+
return {
|
|
115
|
+
ir: {
|
|
116
|
+
version: DEPLOY_IR_V2_VERSION,
|
|
117
|
+
name: 'turbowarp-worker',
|
|
118
|
+
auth: {kind: 'none'},
|
|
119
|
+
capabilities: deduplicateCapabilities(capabilities),
|
|
120
|
+
routes
|
|
121
|
+
},
|
|
122
|
+
diagnostics
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function compileMethodGuard(
|
|
127
|
+
context: FrontendContext,
|
|
128
|
+
guardId: string,
|
|
129
|
+
path: string,
|
|
130
|
+
hatSource: SourceRefV2,
|
|
131
|
+
routeIndex: number,
|
|
132
|
+
structured: StructuredDataLoweringContext
|
|
133
|
+
): RouteIrV2 | null {
|
|
134
|
+
const guard = context.blocks[guardId];
|
|
135
|
+
if (guard === undefined) return null;
|
|
136
|
+
const inputs = inputMap(guard.inputs);
|
|
137
|
+
const conditionId = inputBlockId(inputs.CONDITION);
|
|
138
|
+
const bodyId = inputBlockId(inputs.SUBSTACK);
|
|
139
|
+
const method = conditionId === null ? null : readMethodCondition(context.blocks, conditionId);
|
|
140
|
+
if (method === null || bodyId === null) {
|
|
141
|
+
report(
|
|
142
|
+
context,
|
|
143
|
+
'TW2_UNSUPPORTED_GUARD',
|
|
144
|
+
'Top-level control_if must compare current HTTP method with GET, POST, PUT, PATCH, DELETE, or OPTIONS.',
|
|
145
|
+
sourceRef(context, guardId, 'control_if')
|
|
146
|
+
);
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
if (blockId(guard.next) !== null) {
|
|
150
|
+
report(
|
|
151
|
+
context,
|
|
152
|
+
'TW2_UNSUPPORTED_CONTROL_FLOW',
|
|
153
|
+
'A method guard must be the only command after the HTTP request hat.',
|
|
154
|
+
sourceRef(context, guardId, 'control_if')
|
|
155
|
+
);
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
return compileRouteBody(context, bodyId, method, path, hatSource, routeIndex, structured);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function compileRouteBody(
|
|
162
|
+
context: FrontendContext,
|
|
163
|
+
firstId: string,
|
|
164
|
+
method: HttpMethodV2,
|
|
165
|
+
path: string,
|
|
166
|
+
hatSource: SourceRefV2,
|
|
167
|
+
routeIndex: number,
|
|
168
|
+
structured: StructuredDataLoweringContext
|
|
169
|
+
): RouteIrV2 | null {
|
|
170
|
+
const body = compileStack(context, firstId, structured, true);
|
|
171
|
+
if (body === null) return null;
|
|
172
|
+
return {
|
|
173
|
+
id: routeId(context.targetName, method.toLowerCase(), routeIndex),
|
|
174
|
+
method,
|
|
175
|
+
path,
|
|
176
|
+
auth: 'public',
|
|
177
|
+
body,
|
|
178
|
+
sourceRef: hatSource
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function compileStack(
|
|
183
|
+
context: FrontendContext,
|
|
184
|
+
firstId: string,
|
|
185
|
+
structured: StructuredDataLoweringContext,
|
|
186
|
+
requireResponse: boolean
|
|
187
|
+
): StatementIrV2[] | null {
|
|
188
|
+
const statements: StatementIrV2[] = [];
|
|
189
|
+
const visited = new Set<string>();
|
|
190
|
+
let id: string | null = firstId;
|
|
191
|
+
while (id !== null) {
|
|
192
|
+
if (visited.has(id)) {
|
|
193
|
+
report(context, 'TW2_BLOCK_CYCLE', 'Handler block chain contains a cycle.', blockSource(context, id));
|
|
194
|
+
return null;
|
|
195
|
+
}
|
|
196
|
+
visited.add(id);
|
|
197
|
+
const block = context.blocks[id];
|
|
198
|
+
if (block === undefined || typeof block.opcode !== 'string') {
|
|
199
|
+
report(context, 'TW2_MISSING_BLOCK', `Handler references missing block ${id}.`, sourceRef(context, id, '<missing>'));
|
|
200
|
+
return null;
|
|
201
|
+
}
|
|
202
|
+
const statement = compileStatement(context, id, block, structured);
|
|
203
|
+
if (statement === null) return null;
|
|
204
|
+
statements.push(statement);
|
|
205
|
+
id = blockId(block.next);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const responseIndex = statements.findIndex(isTerminalResponse);
|
|
209
|
+
if (requireResponse && responseIndex < 0) {
|
|
210
|
+
report(context, 'TW2_MISSING_RESPONSE', 'Every handler must end with a response block.', blockSource(context, firstId));
|
|
211
|
+
return null;
|
|
212
|
+
}
|
|
213
|
+
if (responseIndex >= 0 && responseIndex !== statements.length - 1) {
|
|
214
|
+
report(context, 'TW2_RESPONSE_NOT_LAST', 'The response block must be the final command.', blockSource(context, firstId));
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
return statements;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function compileStatement(
|
|
221
|
+
context: FrontendContext,
|
|
222
|
+
id: string,
|
|
223
|
+
block: ScratchBlock,
|
|
224
|
+
structured: StructuredDataLoweringContext
|
|
225
|
+
): StatementIrV2 | null {
|
|
226
|
+
const opcode = block.opcode as string;
|
|
227
|
+
const inputs = inputMap(block.inputs);
|
|
228
|
+
const source = sourceRef(context, id, opcode);
|
|
229
|
+
|
|
230
|
+
if (opcode === 'control_repeat') {
|
|
231
|
+
const count = integerLiteral(inputs.TIMES);
|
|
232
|
+
const bodyId = inputBlockId(inputs.SUBSTACK);
|
|
233
|
+
if (count === null || count < 0 || count > 1000) {
|
|
234
|
+
report(context, 'TW2_LOOP_BOUND_INVALID', 'repeat requires an integer literal from 0 to 1000.', source);
|
|
235
|
+
return null;
|
|
236
|
+
}
|
|
237
|
+
const body = bodyId === null ? [] : compileStack(context, bodyId, structured, false);
|
|
238
|
+
return body === null ? null : {kind: 'bounded-loop', maxIterations: count, body, sourceRef: source};
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
const manifestEntry = context.registry?.byProjectOpcode.get(opcode);
|
|
242
|
+
if (manifestEntry?.block.blockType === 'LOOP') {
|
|
243
|
+
const bodyId = inputBlockId(inputs.SUBSTACK);
|
|
244
|
+
const args = compileManifestArguments(context, manifestEntry, inputs, id, structured);
|
|
245
|
+
if (args === null) return null;
|
|
246
|
+
let bodyFailed = false;
|
|
247
|
+
const statement = structured.lowerForEach(manifestEntry, args, source, (nested) => {
|
|
248
|
+
const body = bodyId === null ? [] : compileStack(context, bodyId, nested, false);
|
|
249
|
+
if (body === null) {
|
|
250
|
+
bodyFailed = true;
|
|
251
|
+
return [];
|
|
252
|
+
}
|
|
253
|
+
return body;
|
|
254
|
+
});
|
|
255
|
+
return bodyFailed || statement === undefined ? null : statement;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
if (opcode === `${PREFIX}setHttpStatus`) {
|
|
259
|
+
const status = Number(literalValue(inputs.STATUS));
|
|
260
|
+
if (!Number.isInteger(status) || status < 100 || status > 599) {
|
|
261
|
+
report(context, 'TW2_DYNAMIC_STATUS', 'HTTP status must be a literal integer from 100 to 599.', source);
|
|
262
|
+
return null;
|
|
263
|
+
}
|
|
264
|
+
return {kind: 'set-status', status, sourceRef: source};
|
|
265
|
+
}
|
|
266
|
+
if (opcode === `${PREFIX}setResponseHeader`) {
|
|
267
|
+
const name = literalValue(inputs.NAME).trim().toLowerCase();
|
|
268
|
+
if (!safeHeader(name)) {
|
|
269
|
+
report(context, 'TW2_UNSAFE_HEADER', `Unsafe or runtime-owned response header: ${name}`, source);
|
|
270
|
+
return null;
|
|
271
|
+
}
|
|
272
|
+
const value = compileExpression(context, inputs.VALUE, id, structured);
|
|
273
|
+
return value === null ? null : {kind: 'set-header', name, value, sourceRef: source};
|
|
274
|
+
}
|
|
275
|
+
if (opcode === `${PREFIX}removeResponseHeader`) {
|
|
276
|
+
const name = literalValue(inputs.NAME).trim().toLowerCase();
|
|
277
|
+
if (!safeHeader(name)) {
|
|
278
|
+
report(context, 'TW2_UNSAFE_HEADER', `Unsafe or runtime-owned response header: ${name}`, source);
|
|
279
|
+
return null;
|
|
280
|
+
}
|
|
281
|
+
return {kind: 'remove-header', name, sourceRef: source};
|
|
282
|
+
}
|
|
283
|
+
if (opcode === `${PREFIX}setHandlerVariable` || opcode === `${PREFIX}changeHandlerVariable`) {
|
|
284
|
+
const name = compileExpression(context, inputs.NAME, id, structured);
|
|
285
|
+
const value = compileExpression(
|
|
286
|
+
context,
|
|
287
|
+
opcode === `${PREFIX}setHandlerVariable` ? inputs.VALUE : inputs.AMOUNT,
|
|
288
|
+
id,
|
|
289
|
+
structured
|
|
290
|
+
);
|
|
291
|
+
if (name === null || value === null) return null;
|
|
292
|
+
return {
|
|
293
|
+
kind: opcode === `${PREFIX}setHandlerVariable` ? 'set-handler-variable' : 'change-handler-variable',
|
|
294
|
+
name,
|
|
295
|
+
value,
|
|
296
|
+
sourceRef: source
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
if (opcode === `${PREFIX}deleteHandlerVariable`) {
|
|
300
|
+
const name = compileExpression(context, inputs.NAME, id, structured);
|
|
301
|
+
return name === null ? null : {kind: 'delete-handler-variable', name, sourceRef: source};
|
|
302
|
+
}
|
|
303
|
+
if (opcode === `${PREFIX}clearHandlerVariables`) return {kind: 'clear-handler-variables', sourceRef: source};
|
|
304
|
+
if (opcode === NAMED_RESPONSE_OPCODE) {
|
|
305
|
+
const lowered = lowerNamedBodyResponse(namedArguments(inputs, source), source);
|
|
306
|
+
context.diagnostics.push(...lowered.diagnostics);
|
|
307
|
+
if (lowered.statement !== undefined) addCapability(context, {kind: 'named-body-provider'});
|
|
308
|
+
return lowered.statement ?? null;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
const responseFormats: Record<string, 'text' | 'html' | 'json'> = {
|
|
312
|
+
[`${PREFIX}respondWithText`]: 'text',
|
|
313
|
+
[`${PREFIX}respondWithHtml`]: 'html',
|
|
314
|
+
[`${PREFIX}respondWithJson`]: 'json',
|
|
315
|
+
[`${PREFIX}sendResponse`]: 'text'
|
|
316
|
+
};
|
|
317
|
+
const format = responseFormats[opcode];
|
|
318
|
+
if (format !== undefined) {
|
|
319
|
+
const body = compileExpression(context, inputs.BODY, id, structured);
|
|
320
|
+
return body === null ? null : {kind: 'respond', format, body, sourceRef: source};
|
|
321
|
+
}
|
|
322
|
+
if (manifestEntry !== undefined && isKvsEntry(manifestEntry)) {
|
|
323
|
+
const args = compileManifestArguments(context, manifestEntry, inputs, id, structured);
|
|
324
|
+
if (args === null) return null;
|
|
325
|
+
const statement = new KvsLoweringContext(context.diagnostics).lowerStatement(manifestEntry, args, source);
|
|
326
|
+
if (statement !== undefined) addCapability(context, {kind: 'key-value-store'});
|
|
327
|
+
return statement ?? null;
|
|
328
|
+
}
|
|
329
|
+
report(context, 'TW2_UNSUPPORTED_COMMAND', `Unsupported command block: ${opcode}`, source);
|
|
330
|
+
return null;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function compileExpression(
|
|
334
|
+
context: FrontendContext,
|
|
335
|
+
input: unknown,
|
|
336
|
+
ownerId: string,
|
|
337
|
+
structured: StructuredDataLoweringContext
|
|
338
|
+
): ExpressionIrV2 | null {
|
|
339
|
+
const reporterId = inputBlockId(input);
|
|
340
|
+
if (reporterId === null) return literal('string', literalValue(input), blockSource(context, ownerId));
|
|
341
|
+
const reporter = context.blocks[reporterId];
|
|
342
|
+
if (reporter === undefined || typeof reporter.opcode !== 'string') {
|
|
343
|
+
report(
|
|
344
|
+
context,
|
|
345
|
+
'TW2_MISSING_REPORTER',
|
|
346
|
+
`Expression references missing block ${reporterId}.`,
|
|
347
|
+
blockSource(context, ownerId)
|
|
348
|
+
);
|
|
349
|
+
return null;
|
|
350
|
+
}
|
|
351
|
+
const source = sourceRef(context, reporterId, reporter.opcode);
|
|
352
|
+
const inputs = inputMap(reporter.inputs);
|
|
353
|
+
const simpleSources: Record<string, Extract<ExpressionIrV2, {kind: 'request'}>['source']> = {
|
|
354
|
+
[`${PREFIX}currentHttpMethod`]: 'method',
|
|
355
|
+
[`${PREFIX}currentRequestPath`]: 'path',
|
|
356
|
+
[`${PREFIX}currentRequestUrl`]: 'url',
|
|
357
|
+
[`${PREFIX}currentRequestBody`]: 'body-text',
|
|
358
|
+
[`${PREFIX}currentRequestContentType`]: 'content-type',
|
|
359
|
+
[`${PREFIX}currentRequestClientAddress`]: 'client-address'
|
|
360
|
+
};
|
|
361
|
+
const requestSource = simpleSources[reporter.opcode];
|
|
362
|
+
if (requestSource !== undefined) {
|
|
363
|
+
if (requestSource === 'client-address') addCapability(context, {kind: 'request-metadata', field: 'client-address'});
|
|
364
|
+
return {kind: 'request', valueType: 'string', source: requestSource, sourceRef: source};
|
|
365
|
+
}
|
|
366
|
+
const namedSources: Record<string, Extract<ExpressionIrV2, {kind: 'request-value'}>['source']> = {
|
|
367
|
+
[`${PREFIX}queryParameter`]: 'query',
|
|
368
|
+
[`${PREFIX}pathParameter`]: 'path-param',
|
|
369
|
+
[`${PREFIX}requestHeader`]: 'header'
|
|
370
|
+
};
|
|
371
|
+
const namedSource = namedSources[reporter.opcode];
|
|
372
|
+
if (namedSource !== undefined) {
|
|
373
|
+
if (inputBlockId(inputs.NAME) !== null) {
|
|
374
|
+
report(context, 'TW2_DYNAMIC_LOOKUP_NAME', 'Request lookup names must be non-empty literals.', source);
|
|
375
|
+
return null;
|
|
376
|
+
}
|
|
377
|
+
const name = literalValue(inputs.NAME);
|
|
378
|
+
if (name.length === 0) {
|
|
379
|
+
report(context, 'TW2_DYNAMIC_LOOKUP_NAME', 'Request lookup names must be non-empty literals.', source);
|
|
380
|
+
return null;
|
|
381
|
+
}
|
|
382
|
+
return {kind: 'request-value', valueType: 'string', source: namedSource, name, sourceRef: source};
|
|
383
|
+
}
|
|
384
|
+
if (reporter.opcode === 'operator_join') {
|
|
385
|
+
const left = compileExpression(context, inputs.STRING1, reporterId, structured);
|
|
386
|
+
const right = compileExpression(context, inputs.STRING2, reporterId, structured);
|
|
387
|
+
return left === null || right === null
|
|
388
|
+
? null
|
|
389
|
+
: {kind: 'concat', valueType: 'string', left, right, sourceRef: source};
|
|
390
|
+
}
|
|
391
|
+
if (reporter.opcode === `${PREFIX}handlerVariable` || reporter.opcode === `${PREFIX}handlerVariableExists`) {
|
|
392
|
+
const name = compileExpression(context, inputs.NAME, reporterId, structured);
|
|
393
|
+
if (name === null) return null;
|
|
394
|
+
return reporter.opcode === `${PREFIX}handlerVariable`
|
|
395
|
+
? {kind: 'handler-variable', valueType: {kind: 'union', members: ['number', 'string']}, name, sourceRef: source}
|
|
396
|
+
: {kind: 'handler-variable-exists', valueType: 'boolean', name, sourceRef: source};
|
|
397
|
+
}
|
|
398
|
+
if (reporter.opcode === `${PREFIX}listHandlerVariables`) {
|
|
399
|
+
return {kind: 'handler-variable-names', valueType: 'string', sourceRef: source};
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
const manifestEntry = context.registry?.byProjectOpcode.get(reporter.opcode);
|
|
403
|
+
if (manifestEntry !== undefined) {
|
|
404
|
+
const args = compileManifestArguments(context, manifestEntry, inputs, reporterId, structured);
|
|
405
|
+
if (args === null) return null;
|
|
406
|
+
if (isKvsEntry(manifestEntry)) {
|
|
407
|
+
const expression = new KvsLoweringContext(context.diagnostics).lowerReporter(manifestEntry, args, source);
|
|
408
|
+
if (expression !== undefined) addCapability(context, {kind: 'key-value-store'});
|
|
409
|
+
return expression ?? null;
|
|
410
|
+
}
|
|
411
|
+
return structured.lowerReporter(manifestEntry, args, source) ?? null;
|
|
412
|
+
}
|
|
413
|
+
report(context, 'TW2_UNSUPPORTED_REPORTER', `Unsupported reporter block: ${reporter.opcode}`, source);
|
|
414
|
+
return null;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
function compileManifestArguments(
|
|
418
|
+
context: FrontendContext,
|
|
419
|
+
entry: CompilerOpcodeRegistryEntry,
|
|
420
|
+
inputs: Record<string, unknown>,
|
|
421
|
+
ownerId: string,
|
|
422
|
+
structured: StructuredDataLoweringContext
|
|
423
|
+
): Record<string, ExpressionIrV2> | null {
|
|
424
|
+
const args: Record<string, ExpressionIrV2> = {};
|
|
425
|
+
for (const argument of entry.block.arguments) {
|
|
426
|
+
const input = inputs[argument.id];
|
|
427
|
+
if (argument.type === 'NUMBER' && inputBlockId(input) === null) {
|
|
428
|
+
args[argument.id] = literal('number', Number(literalValue(input)), undefined);
|
|
429
|
+
continue;
|
|
430
|
+
}
|
|
431
|
+
const expression = compileExpression(context, input, ownerId, structured);
|
|
432
|
+
if (expression === null) return null;
|
|
433
|
+
args[argument.id] = expression;
|
|
434
|
+
}
|
|
435
|
+
return args;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
function readMethodCondition(blocks: BlockMap, id: string): HttpMethodV2 | null {
|
|
439
|
+
const condition = blocks[id];
|
|
440
|
+
if (condition?.opcode !== 'operator_equals') return null;
|
|
441
|
+
const inputs = inputMap(condition.inputs);
|
|
442
|
+
for (const [reporterInput, literalInput] of [
|
|
443
|
+
[inputs.OPERAND1, inputs.OPERAND2],
|
|
444
|
+
[inputs.OPERAND2, inputs.OPERAND1]
|
|
445
|
+
]) {
|
|
446
|
+
const reporterId = inputBlockId(reporterInput);
|
|
447
|
+
const reporter = reporterId === null ? undefined : blocks[reporterId];
|
|
448
|
+
if (reporter?.opcode !== `${PREFIX}currentHttpMethod`) continue;
|
|
449
|
+
const method = literalValue(literalInput).toUpperCase() as HttpMethodV2;
|
|
450
|
+
if (METHODS.has(method)) return method;
|
|
451
|
+
}
|
|
452
|
+
return null;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
function namedArguments(inputs: Record<string, unknown>, source: SourceRefV2): NamedBodyArguments {
|
|
456
|
+
return {
|
|
457
|
+
NAMESPACE: scratchLiteral(inputs.NAMESPACE, 'string', source),
|
|
458
|
+
NAME: scratchLiteral(inputs.NAME, 'string', source),
|
|
459
|
+
KIND: scratchLiteral(inputs.KIND, 'string', source),
|
|
460
|
+
SCOPE: scratchLiteral(inputs.SCOPE, 'string', source),
|
|
461
|
+
TARGET_ID: scratchLiteral(inputs.TARGET_ID, 'string', source),
|
|
462
|
+
REPRESENTATION: scratchLiteral(inputs.REPRESENTATION, 'string', source),
|
|
463
|
+
MAX_BYTES: scratchLiteral(inputs.MAX_BYTES, 'number', source)
|
|
464
|
+
};
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
function scratchLiteral(input: unknown, expected: 'string' | 'number', source: SourceRefV2): ExpressionIrV2 {
|
|
468
|
+
if (inputBlockId(input) !== null) return {kind: 'request', valueType: 'string', source: 'path', sourceRef: source};
|
|
469
|
+
const raw = literalValue(input);
|
|
470
|
+
return expected === 'number' ? literal('number', Number(raw), source) : literal('string', raw, source);
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
function literal(
|
|
474
|
+
valueType: 'string' | 'number',
|
|
475
|
+
value: string | number,
|
|
476
|
+
source: SourceRefV2 | undefined
|
|
477
|
+
): ExpressionIrV2 {
|
|
478
|
+
return {kind: 'literal', valueType, value, ...(source === undefined ? {} : {sourceRef: source})};
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
function integerLiteral(input: unknown): number | null {
|
|
482
|
+
if (inputBlockId(input) !== null) return null;
|
|
483
|
+
const raw = literalValue(input);
|
|
484
|
+
if (!/^-?\d+$/u.test(raw)) return null;
|
|
485
|
+
const value = Number(raw);
|
|
486
|
+
return Number.isSafeInteger(value) ? value : null;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
function isTerminalResponse(statement: StatementIrV2): boolean {
|
|
490
|
+
return statement.kind === 'respond' || statement.kind === 'respond-binary' || statement.kind === 'respond-named-body';
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
function rejectDuplicateRoutes(routes: RouteIrV2[], diagnostics: CompilerDiagnosticV2[]): void {
|
|
494
|
+
const seen = new Set<string>();
|
|
495
|
+
for (const route of routes) {
|
|
496
|
+
const key = `${route.method} ${route.path}`;
|
|
497
|
+
if (seen.has(key)) {
|
|
498
|
+
diagnostics.push({
|
|
499
|
+
severity: 'error',
|
|
500
|
+
code: 'TW2_DUPLICATE_ROUTE',
|
|
501
|
+
message: `Duplicate route: ${key}`,
|
|
502
|
+
...(route.sourceRef === undefined ? {} : {sourceRef: route.sourceRef})
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
seen.add(key);
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
function deduplicateCapabilities(capabilities: CapabilityRequirementV2[]): CapabilityRequirementV2[] {
|
|
510
|
+
const entries = new Map<string, CapabilityRequirementV2>();
|
|
511
|
+
for (const capability of capabilities) entries.set(capabilityKey(capability), capability);
|
|
512
|
+
return [...entries.entries()]
|
|
513
|
+
.sort(([left], [right]) => left.localeCompare(right))
|
|
514
|
+
.map(([, capability]) => capability);
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
function addCapability(context: FrontendContext, capability: CapabilityRequirementV2): void {
|
|
518
|
+
context.capabilities.push(capability);
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
function report(
|
|
522
|
+
context: FrontendContext,
|
|
523
|
+
code: `TW2_${string}`,
|
|
524
|
+
message: string,
|
|
525
|
+
source?: SourceRefV2
|
|
526
|
+
): void {
|
|
527
|
+
context.diagnostics.push({severity: 'error', code, message, ...(source === undefined ? {} : {sourceRef: source})});
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
function sourceRef(context: FrontendContext, blockIdValue: string, opcode: string): SourceRefV2 {
|
|
531
|
+
return {targetIndex: context.targetIndex, targetName: context.targetName, blockId: blockIdValue, opcode};
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
function blockSource(context: FrontendContext, id: string): SourceRefV2 {
|
|
535
|
+
const opcode = context.blocks[id]?.opcode;
|
|
536
|
+
return sourceRef(context, id, typeof opcode === 'string' ? opcode : '<missing>');
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
function emptyIr(): DeployIrV2 {
|
|
540
|
+
return {version: DEPLOY_IR_V2_VERSION, name: 'turbowarp-worker', auth: {kind: 'none'}, capabilities: [], routes: []};
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
function capabilityKey(value: unknown): string {
|
|
544
|
+
return JSON.stringify(value);
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
function safeHeader(name: string): boolean {
|
|
548
|
+
return (
|
|
549
|
+
/^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/u.test(name) &&
|
|
550
|
+
!['connection', 'content-length', 'transfer-encoding', 'upgrade'].includes(name)
|
|
551
|
+
);
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
function routeSegment(name: string): string {
|
|
555
|
+
const normalized = name.normalize('NFKC').trim().toLowerCase().replace(/[^a-z0-9_-]+/gu, '-');
|
|
556
|
+
return normalized.replace(/^-+|-+$/gu, '') || 'sprite';
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
function routeId(target: string, method: string, index: number): string {
|
|
560
|
+
return `${routeSegment(target)}-${method}-${index + 1}`;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
function inputBlockId(value: unknown): string | null {
|
|
564
|
+
if (!Array.isArray(value)) return null;
|
|
565
|
+
return typeof value[1] === 'string' ? value[1] : null;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
function literalValue(value: unknown): string {
|
|
569
|
+
if (!Array.isArray(value)) return '';
|
|
570
|
+
const primary = value[1];
|
|
571
|
+
if (Array.isArray(primary)) return String(primary[1] ?? '');
|
|
572
|
+
const shadow = value[2];
|
|
573
|
+
if (Array.isArray(shadow)) return String(shadow[1] ?? '');
|
|
574
|
+
return typeof primary === 'number' || typeof primary === 'boolean' ? String(primary) : '';
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
function inputMap(value: unknown): Record<string, unknown> {
|
|
578
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
579
|
+
? (value as Record<string, unknown>)
|
|
580
|
+
: {};
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
function blockMap(value: unknown): BlockMap {
|
|
584
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value) ? (value as BlockMap) : {};
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
function blockId(value: unknown): string | null {
|
|
588
|
+
return typeof value === 'string' ? value : null;
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
function object(value: unknown): Record<string, unknown> {
|
|
592
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
593
|
+
throw new Error('TurboWarp project value must be an object.');
|
|
594
|
+
}
|
|
595
|
+
return value as Record<string, unknown>;
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
function toCompilerDiagnostic(diagnostic: {
|
|
599
|
+
severity: 'error';
|
|
600
|
+
code: `TW2_${string}`;
|
|
601
|
+
message: string;
|
|
602
|
+
sourceRef?: SourceRefV2;
|
|
603
|
+
}): CompilerDiagnosticV2 {
|
|
604
|
+
return {
|
|
605
|
+
severity: diagnostic.severity,
|
|
606
|
+
code: diagnostic.code,
|
|
607
|
+
message: diagnostic.message,
|
|
608
|
+
...(diagnostic.sourceRef === undefined ? {} : {sourceRef: diagnostic.sourceRef})
|
|
609
|
+
};
|
|
610
|
+
}
|