@kubohiroya/turbowarp-http-server 0.2.0 → 0.2.1
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 +29 -13
- package/README.md +101 -42
- 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 +516 -1
- 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 +80 -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/examples/compiler/message-app.v2.ir.json +41 -0
- package/package.json +44 -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/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,153 @@
|
|
|
1
|
+
import type {SourceRefV2} from '../ir-v2/types.js';
|
|
2
|
+
import type {ServerSubsetDiagnosticCode, TargetNeutralDiagnostic} from './types.js';
|
|
3
|
+
|
|
4
|
+
interface ResourceState {
|
|
5
|
+
consumed: boolean;
|
|
6
|
+
sourceRef?: SourceRefV2;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export class BinaryBodyLifetimeTracker {
|
|
10
|
+
private readonly scopes: Array<Map<string, ResourceState>> = [new Map()];
|
|
11
|
+
private readonly retired = new Set<string>();
|
|
12
|
+
|
|
13
|
+
public constructor(
|
|
14
|
+
private readonly routeId: string,
|
|
15
|
+
private readonly diagnostics: TargetNeutralDiagnostic[]
|
|
16
|
+
) {}
|
|
17
|
+
|
|
18
|
+
public enterScope(): void {
|
|
19
|
+
this.scopes.push(new Map());
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
public fork(): BinaryBodyLifetimeTracker {
|
|
23
|
+
const tracker = new BinaryBodyLifetimeTracker(this.routeId, this.diagnostics);
|
|
24
|
+
tracker.scopes.splice(
|
|
25
|
+
0,
|
|
26
|
+
tracker.scopes.length,
|
|
27
|
+
...this.scopes.map(
|
|
28
|
+
(scope) =>
|
|
29
|
+
new Map(
|
|
30
|
+
[...scope].map(([id, state]) => [
|
|
31
|
+
id,
|
|
32
|
+
state.sourceRef === undefined
|
|
33
|
+
? {consumed: state.consumed}
|
|
34
|
+
: {consumed: state.consumed, sourceRef: state.sourceRef}
|
|
35
|
+
])
|
|
36
|
+
)
|
|
37
|
+
)
|
|
38
|
+
);
|
|
39
|
+
for (const id of this.retired) tracker.retired.add(id);
|
|
40
|
+
return tracker;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
public mergeBranches(left: BinaryBodyLifetimeTracker, right: BinaryBodyLifetimeTracker): void {
|
|
44
|
+
for (let scopeIndex = 0; scopeIndex < this.scopes.length; scopeIndex += 1) {
|
|
45
|
+
const scope = this.scopes[scopeIndex]!;
|
|
46
|
+
const leftScope = left.scopes[scopeIndex]!;
|
|
47
|
+
const rightScope = right.scopes[scopeIndex]!;
|
|
48
|
+
for (const [id, state] of scope) {
|
|
49
|
+
state.consumed = leftScope.get(id)?.consumed === true || rightScope.get(id)?.consumed === true;
|
|
50
|
+
}
|
|
51
|
+
for (const [id, leftState] of leftScope) {
|
|
52
|
+
if (scope.has(id)) continue;
|
|
53
|
+
const rightState = rightScope.get(id);
|
|
54
|
+
if (rightState !== undefined) {
|
|
55
|
+
scope.set(id, {
|
|
56
|
+
consumed: leftState.consumed || rightState.consumed,
|
|
57
|
+
...(leftState.sourceRef === undefined ? {} : {sourceRef: leftState.sourceRef})
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
public absorbPossibleExecution(
|
|
65
|
+
child: BinaryBodyLifetimeTracker,
|
|
66
|
+
maximumExecutions = 1,
|
|
67
|
+
sourceRef?: SourceRefV2
|
|
68
|
+
): void {
|
|
69
|
+
if (maximumExecutions < 1) return;
|
|
70
|
+
for (let scopeIndex = 0; scopeIndex < this.scopes.length; scopeIndex += 1) {
|
|
71
|
+
for (const [id, state] of this.scopes[scopeIndex]!) {
|
|
72
|
+
if (child.scopes[scopeIndex]?.get(id)?.consumed === true) {
|
|
73
|
+
if (!state.consumed && maximumExecutions > 1) {
|
|
74
|
+
this.report(
|
|
75
|
+
'TW2_BINARY_BODY_CONSUMED',
|
|
76
|
+
`binary-body ${id} may be consumed more than once by a loop.`,
|
|
77
|
+
'An affine body declared outside a loop cannot be consumed by multiple iterations.',
|
|
78
|
+
'Acquire the body inside the loop or make the loop execute at most once.',
|
|
79
|
+
sourceRef
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
state.consumed = true;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
public leaveScope(): void {
|
|
89
|
+
if (this.scopes.length === 1) throw new Error('Cannot leave the root binary-body scope.');
|
|
90
|
+
const scope = this.scopes.pop()!;
|
|
91
|
+
for (const id of scope.keys()) this.retired.add(id);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
public declare(id: string, sourceRef?: SourceRefV2): void {
|
|
95
|
+
const current = this.scopes[this.scopes.length - 1]!;
|
|
96
|
+
current.set(id, sourceRef === undefined ? {consumed: false} : {consumed: false, sourceRef});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
public consume(id: string, sourceRef?: SourceRefV2): boolean {
|
|
100
|
+
const state = this.find(id);
|
|
101
|
+
if (state === undefined) {
|
|
102
|
+
const outOfScope = this.retired.has(id);
|
|
103
|
+
this.report(
|
|
104
|
+
outOfScope ? 'TW2_BINARY_BODY_SCOPE' : 'TW2_BINARY_BODY_UNDECLARED',
|
|
105
|
+
outOfScope ? `binary-body ${id} is outside its lexical scope.` : `binary-body ${id} is not declared.`,
|
|
106
|
+
outOfScope ? 'The resource-producing operation completed in a narrower lexical scope.' : 'No resource producer declares this binding.',
|
|
107
|
+
'Consume the body inside its declaring scope, or produce a new binary-body binding.',
|
|
108
|
+
sourceRef
|
|
109
|
+
);
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
if (state.consumed) {
|
|
113
|
+
this.report(
|
|
114
|
+
'TW2_BINARY_BODY_CONSUMED',
|
|
115
|
+
`binary-body ${id} is consumed more than once.`,
|
|
116
|
+
'binary-body is affine and cannot be cloned, buffered, or reused by the MVP.',
|
|
117
|
+
'Acquire a new body for each put or response operation.',
|
|
118
|
+
sourceRef ?? state.sourceRef
|
|
119
|
+
);
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
state.consumed = true;
|
|
123
|
+
return true;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
private find(id: string): ResourceState | undefined {
|
|
127
|
+
for (let index = this.scopes.length - 1; index >= 0; index -= 1) {
|
|
128
|
+
const state = this.scopes[index]!.get(id);
|
|
129
|
+
if (state !== undefined) return state;
|
|
130
|
+
}
|
|
131
|
+
return undefined;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
private report(
|
|
135
|
+
code: Extract<ServerSubsetDiagnosticCode, `TW2_BINARY_${string}`>,
|
|
136
|
+
message: string,
|
|
137
|
+
reason: string,
|
|
138
|
+
suggestion: string,
|
|
139
|
+
sourceRef?: SourceRefV2
|
|
140
|
+
): void {
|
|
141
|
+
const diagnostic: TargetNeutralDiagnostic = {
|
|
142
|
+
severity: 'error',
|
|
143
|
+
phase: 'target-neutral',
|
|
144
|
+
code,
|
|
145
|
+
message,
|
|
146
|
+
routeId: this.routeId,
|
|
147
|
+
reason,
|
|
148
|
+
suggestion
|
|
149
|
+
};
|
|
150
|
+
if (sourceRef !== undefined) diagnostic.sourceRef = sourceRef;
|
|
151
|
+
this.diagnostics.push(diagnostic);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
@@ -0,0 +1,387 @@
|
|
|
1
|
+
import {extensionConfig} from '../../config.js';
|
|
2
|
+
import type {CompilerOpcodeRegistry} from '../manifest/types.js';
|
|
3
|
+
import type {SourceRefV2} from '../ir-v2/types.js';
|
|
4
|
+
import {
|
|
5
|
+
DEFAULT_SERVER_SUBSET_POLICY,
|
|
6
|
+
type ServerSubsetDiagnosticCode,
|
|
7
|
+
type ServerSubsetPolicy,
|
|
8
|
+
type TargetNeutralDiagnostic
|
|
9
|
+
} from './types.js';
|
|
10
|
+
|
|
11
|
+
interface ScratchProject {
|
|
12
|
+
targets?: unknown;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface ScratchTarget {
|
|
16
|
+
name?: unknown;
|
|
17
|
+
blocks?: unknown;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface ScratchBlock {
|
|
21
|
+
opcode?: unknown;
|
|
22
|
+
next?: unknown;
|
|
23
|
+
parent?: unknown;
|
|
24
|
+
topLevel?: unknown;
|
|
25
|
+
inputs?: unknown;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
type BlockMap = Record<string, ScratchBlock>;
|
|
29
|
+
|
|
30
|
+
const HTTP_PREFIX = `${extensionConfig.id}_`;
|
|
31
|
+
const HTTP_HAT = `${HTTP_PREFIX}whenHttpRequestReceived`;
|
|
32
|
+
const HTTP_SERVER_OPCODES = new Set(
|
|
33
|
+
[
|
|
34
|
+
'whenHttpRequestReceived',
|
|
35
|
+
'setHttpStatus',
|
|
36
|
+
'setResponseHeader',
|
|
37
|
+
'removeResponseHeader',
|
|
38
|
+
'setHandlerVariable',
|
|
39
|
+
'changeHandlerVariable',
|
|
40
|
+
'deleteHandlerVariable',
|
|
41
|
+
'clearHandlerVariables',
|
|
42
|
+
'respondWithText',
|
|
43
|
+
'respondWithHtml',
|
|
44
|
+
'respondWithJson',
|
|
45
|
+
'respondWithNamedBody',
|
|
46
|
+
'sendResponse',
|
|
47
|
+
'currentHttpMethod',
|
|
48
|
+
'currentRequestPath',
|
|
49
|
+
'currentRequestUrl',
|
|
50
|
+
'currentRequestBody',
|
|
51
|
+
'currentRequestContentType',
|
|
52
|
+
'currentRequestClientAddress',
|
|
53
|
+
'queryParameter',
|
|
54
|
+
'pathParameter',
|
|
55
|
+
'requestHeader',
|
|
56
|
+
'handlerVariable',
|
|
57
|
+
'handlerVariableExists',
|
|
58
|
+
'listHandlerVariables'
|
|
59
|
+
].map((opcode) => `${HTTP_PREFIX}${opcode}`)
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
const MESSAGE_OPCODES = new Set(['event_broadcast', 'event_broadcastandwait', 'event_whenbroadcastreceived']);
|
|
63
|
+
const CLONE_OPCODES = new Set(['control_create_clone_of', 'control_start_as_clone', 'control_delete_this_clone']);
|
|
64
|
+
const RUNTIME_PREFIXES = ['motion_', 'looks_', 'sound_', 'pen_', 'music_', 'videoSensing_', 'sensing_'];
|
|
65
|
+
|
|
66
|
+
export function validateTurboWarpServerSubset(
|
|
67
|
+
value: unknown,
|
|
68
|
+
registry?: CompilerOpcodeRegistry,
|
|
69
|
+
policy: Readonly<ServerSubsetPolicy> = DEFAULT_SERVER_SUBSET_POLICY
|
|
70
|
+
): TargetNeutralDiagnostic[] {
|
|
71
|
+
const project = object(value) as ScratchProject;
|
|
72
|
+
if (!Array.isArray(project.targets)) throw new Error('TurboWarp project.targets must be an array.');
|
|
73
|
+
const diagnostics: TargetNeutralDiagnostic[] = [];
|
|
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 reachable = reachableHandlerBlocks(blocks);
|
|
79
|
+
const methodGuardBlocks = new Set<string>();
|
|
80
|
+
for (const id of reachable) {
|
|
81
|
+
if (blocks[id]?.opcode === 'control_if' && isMethodGuard(blocks, id)) {
|
|
82
|
+
methodGuardBlocks.add(id);
|
|
83
|
+
const condition = inputBlockId(inputMap(blocks[id]?.inputs).CONDITION, blocks);
|
|
84
|
+
if (condition !== null) methodGuardBlocks.add(condition);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
for (const id of reachable) {
|
|
88
|
+
const block = blocks[id];
|
|
89
|
+
if (block === undefined || typeof block.opcode !== 'string') continue;
|
|
90
|
+
validateBlock(
|
|
91
|
+
blocks,
|
|
92
|
+
id,
|
|
93
|
+
block,
|
|
94
|
+
targetIndex,
|
|
95
|
+
targetName,
|
|
96
|
+
methodGuardBlocks,
|
|
97
|
+
registry,
|
|
98
|
+
policy,
|
|
99
|
+
diagnostics
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
return diagnostics;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function validateBlock(
|
|
107
|
+
blocks: BlockMap,
|
|
108
|
+
id: string,
|
|
109
|
+
block: ScratchBlock,
|
|
110
|
+
targetIndex: number,
|
|
111
|
+
targetName: string,
|
|
112
|
+
methodGuardBlocks: ReadonlySet<string>,
|
|
113
|
+
registry: CompilerOpcodeRegistry | undefined,
|
|
114
|
+
policy: Readonly<ServerSubsetPolicy>,
|
|
115
|
+
diagnostics: TargetNeutralDiagnostic[]
|
|
116
|
+
): void {
|
|
117
|
+
const opcode = block.opcode as string;
|
|
118
|
+
const sourceRef: SourceRefV2 = {targetIndex, targetName, blockId: id, opcode};
|
|
119
|
+
const routeId = `project:${targetIndex}:${targetName}`;
|
|
120
|
+
if (opcode.startsWith('data_')) {
|
|
121
|
+
addDiagnostic(
|
|
122
|
+
diagnostics,
|
|
123
|
+
routeId,
|
|
124
|
+
sourceRef,
|
|
125
|
+
'TW2_GLOBAL_STATE_UNSUPPORTED',
|
|
126
|
+
'Scratch global variables and lists are not server-executable.',
|
|
127
|
+
'Their lifetime is shared across requests and workers.',
|
|
128
|
+
'Use request-local handler variables or compiler-generated typed bindings.'
|
|
129
|
+
);
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
if (MESSAGE_OPCODES.has(opcode)) {
|
|
133
|
+
addDiagnostic(
|
|
134
|
+
diagnostics,
|
|
135
|
+
routeId,
|
|
136
|
+
sourceRef,
|
|
137
|
+
'TW2_MESSAGE_UNSUPPORTED',
|
|
138
|
+
'Broadcast and message blocks are not server-executable.',
|
|
139
|
+
'Message scheduling has no bounded request-local lifetime.',
|
|
140
|
+
'Call a supported operation directly inside the HTTP handler.'
|
|
141
|
+
);
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
if (opcode === 'control_forever') {
|
|
145
|
+
addDiagnostic(
|
|
146
|
+
diagnostics,
|
|
147
|
+
routeId,
|
|
148
|
+
sourceRef,
|
|
149
|
+
'TW2_UNBOUNDED_LOOP',
|
|
150
|
+
'forever is not allowed in a server handler.',
|
|
151
|
+
'The loop has no statically provable upper bound.',
|
|
152
|
+
'Use a literal bounded repeat or a manifest-declared bounded loop.'
|
|
153
|
+
);
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
if (CLONE_OPCODES.has(opcode)) {
|
|
157
|
+
addDiagnostic(
|
|
158
|
+
diagnostics,
|
|
159
|
+
routeId,
|
|
160
|
+
sourceRef,
|
|
161
|
+
'TW2_CLONE_UNSUPPORTED',
|
|
162
|
+
'Clone blocks are not server-executable.',
|
|
163
|
+
'Clone state belongs to the sprite runtime and can outlive a request.',
|
|
164
|
+
'Represent request data with lexical values instead.'
|
|
165
|
+
);
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
if (RUNTIME_PREFIXES.some((prefix) => opcode.startsWith(prefix))) {
|
|
169
|
+
addDiagnostic(
|
|
170
|
+
diagnostics,
|
|
171
|
+
routeId,
|
|
172
|
+
sourceRef,
|
|
173
|
+
'TW2_RUNTIME_DEPENDENCY_UNSUPPORTED',
|
|
174
|
+
`Runtime-dependent block ${opcode} is not server-executable.`,
|
|
175
|
+
'The operation depends on sprite, UI, renderer, audio, camera, or browser state.',
|
|
176
|
+
'Move the operation to the browser project and pass only request data to the server.'
|
|
177
|
+
);
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
if (opcode === 'control_repeat') {
|
|
181
|
+
const count = repeatLiteral(inputMap(block.inputs).TIMES, blocks);
|
|
182
|
+
if (count === null || count < 0 || count > policy.maxLoopIterations) {
|
|
183
|
+
addDiagnostic(
|
|
184
|
+
diagnostics,
|
|
185
|
+
routeId,
|
|
186
|
+
sourceRef,
|
|
187
|
+
'TW2_LOOP_BOUND_INVALID',
|
|
188
|
+
`repeat count must be an integer literal from 0 to ${policy.maxLoopIterations}.`,
|
|
189
|
+
'Dynamic Scratch number coercion cannot prove a server work bound.',
|
|
190
|
+
'Replace the repeat input with a supported integer literal.'
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
const depth = repeatNestingDepth(blocks, id);
|
|
194
|
+
if (depth > policy.maxLoopNesting) {
|
|
195
|
+
addDiagnostic(
|
|
196
|
+
diagnostics,
|
|
197
|
+
routeId,
|
|
198
|
+
sourceRef,
|
|
199
|
+
'TW2_LOOP_NESTING_EXCEEDED',
|
|
200
|
+
`Loop nesting exceeds the ${policy.maxLoopNesting} level limit.`,
|
|
201
|
+
'Nested bounded loops can multiply into excessive work.',
|
|
202
|
+
'Flatten the loop or split the handler.'
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
if (opcode === 'control_if' && !methodGuardBlocks.has(id)) {
|
|
208
|
+
addDiagnostic(
|
|
209
|
+
diagnostics,
|
|
210
|
+
routeId,
|
|
211
|
+
sourceRef,
|
|
212
|
+
'TW2_CONTROL_FLOW_UNSUPPORTED',
|
|
213
|
+
'Scratch control_if is not lowered by the MVP frontend except for the top-level HTTP method guard.',
|
|
214
|
+
'Scratch truthiness and branch binding semantics are not inferred.',
|
|
215
|
+
'Use the supported method guard or direct typed IR input.'
|
|
216
|
+
);
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
if (opcode === 'control_if_else') {
|
|
220
|
+
addDiagnostic(
|
|
221
|
+
diagnostics,
|
|
222
|
+
routeId,
|
|
223
|
+
sourceRef,
|
|
224
|
+
'TW2_CONTROL_FLOW_UNSUPPORTED',
|
|
225
|
+
'Scratch control_if_else is not lowered by the MVP frontend.',
|
|
226
|
+
'Scratch truthiness and branch binding semantics are not inferred.',
|
|
227
|
+
'Use direct typed IR input until frontend coercion rules are specified.'
|
|
228
|
+
);
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
if (
|
|
232
|
+
opcode === HTTP_HAT ||
|
|
233
|
+
HTTP_SERVER_OPCODES.has(opcode) ||
|
|
234
|
+
opcode === 'operator_join' ||
|
|
235
|
+
methodGuardBlocks.has(id)
|
|
236
|
+
) {
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
const manifestEntry = registry?.byProjectOpcode.get(opcode);
|
|
240
|
+
if (manifestEntry !== undefined) {
|
|
241
|
+
if (manifestEntry.block.server?.supported === true) return;
|
|
242
|
+
addDiagnostic(
|
|
243
|
+
diagnostics,
|
|
244
|
+
routeId,
|
|
245
|
+
sourceRef,
|
|
246
|
+
'TW2_UNSUPPORTED_OPERATION',
|
|
247
|
+
`Extension block ${opcode} is not declared server-compatible.`,
|
|
248
|
+
'A block API manifest entry alone does not authorize server execution.',
|
|
249
|
+
'Use a block with a validated server-operation hint.'
|
|
250
|
+
);
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
addDiagnostic(
|
|
254
|
+
diagnostics,
|
|
255
|
+
routeId,
|
|
256
|
+
sourceRef,
|
|
257
|
+
'TW2_UNSUPPORTED_BLOCK',
|
|
258
|
+
`Block ${opcode} is outside the server-executable allowlist.`,
|
|
259
|
+
'No built-in rule or locked server-operation manifest authorizes this opcode.',
|
|
260
|
+
'Remove the block or provide a compatible locked extension manifest.'
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function reachableHandlerBlocks(blocks: BlockMap): Set<string> {
|
|
265
|
+
const reachable = new Set<string>();
|
|
266
|
+
const pending = Object.entries(blocks)
|
|
267
|
+
.filter(([, block]) => block.opcode === HTTP_HAT && block.topLevel === true)
|
|
268
|
+
.map(([id]) => id);
|
|
269
|
+
while (pending.length > 0) {
|
|
270
|
+
const id = pending.pop()!;
|
|
271
|
+
if (reachable.has(id)) continue;
|
|
272
|
+
reachable.add(id);
|
|
273
|
+
const block = blocks[id];
|
|
274
|
+
if (block === undefined) continue;
|
|
275
|
+
const next = typeof block.next === 'string' && blocks[block.next] !== undefined ? block.next : undefined;
|
|
276
|
+
if (next !== undefined) pending.push(next);
|
|
277
|
+
for (const input of Object.values(inputMap(block.inputs))) {
|
|
278
|
+
for (const referenced of referencedBlockIds(input, blocks)) pending.push(referenced);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
return reachable;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function isMethodGuard(blocks: BlockMap, id: string): boolean {
|
|
285
|
+
const block = blocks[id];
|
|
286
|
+
if (block?.opcode !== 'control_if' || block.next !== null) return false;
|
|
287
|
+
const parent = typeof block.parent === 'string' ? blocks[block.parent] : undefined;
|
|
288
|
+
if (parent?.opcode !== HTTP_HAT) return false;
|
|
289
|
+
const conditionId = inputBlockId(inputMap(block.inputs).CONDITION, blocks);
|
|
290
|
+
if (conditionId === null) return false;
|
|
291
|
+
const condition = blocks[conditionId];
|
|
292
|
+
if (condition?.opcode !== 'operator_equals') return false;
|
|
293
|
+
const inputs = inputMap(condition.inputs);
|
|
294
|
+
return [
|
|
295
|
+
[inputs.OPERAND1, inputs.OPERAND2],
|
|
296
|
+
[inputs.OPERAND2, inputs.OPERAND1]
|
|
297
|
+
].some(([reporterInput, literalInput]) => {
|
|
298
|
+
const reporterId = inputBlockId(reporterInput, blocks);
|
|
299
|
+
const reporter = reporterId === null ? undefined : blocks[reporterId];
|
|
300
|
+
const method = primitiveLiteral(literalInput);
|
|
301
|
+
return (
|
|
302
|
+
reporter?.opcode === `${HTTP_PREFIX}currentHttpMethod` &&
|
|
303
|
+
typeof method === 'string' &&
|
|
304
|
+
['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'].includes(method.toUpperCase())
|
|
305
|
+
);
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
function repeatNestingDepth(blocks: BlockMap, id: string): number {
|
|
310
|
+
let depth = 1;
|
|
311
|
+
let parent = blocks[id]?.parent;
|
|
312
|
+
const visited = new Set<string>([id]);
|
|
313
|
+
while (typeof parent === 'string' && !visited.has(parent)) {
|
|
314
|
+
visited.add(parent);
|
|
315
|
+
const block = blocks[parent];
|
|
316
|
+
if (block?.opcode === 'control_repeat') depth += 1;
|
|
317
|
+
parent = block?.parent;
|
|
318
|
+
}
|
|
319
|
+
return depth;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
function repeatLiteral(input: unknown, blocks: BlockMap): number | null {
|
|
323
|
+
if (inputBlockId(input, blocks) !== null) return null;
|
|
324
|
+
const value = primitiveLiteral(input);
|
|
325
|
+
if (typeof value === 'number') return Number.isSafeInteger(value) ? value : null;
|
|
326
|
+
if (typeof value !== 'string' || !/^-?\d+$/.test(value)) return null;
|
|
327
|
+
const parsed = Number(value);
|
|
328
|
+
return Number.isSafeInteger(parsed) ? parsed : null;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function primitiveLiteral(input: unknown): unknown {
|
|
332
|
+
if (!Array.isArray(input)) return input;
|
|
333
|
+
const primary = input[1];
|
|
334
|
+
if (Array.isArray(primary)) return primary[1];
|
|
335
|
+
return primary;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
function referencedBlockIds(input: unknown, blocks: BlockMap): string[] {
|
|
339
|
+
if (!Array.isArray(input)) return [];
|
|
340
|
+
const result: string[] = [];
|
|
341
|
+
for (const candidate of [input[1], input[2]]) {
|
|
342
|
+
if (typeof candidate === 'string' && blocks[candidate] !== undefined) result.push(candidate);
|
|
343
|
+
}
|
|
344
|
+
return result;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
function inputBlockId(input: unknown, blocks: BlockMap): string | null {
|
|
348
|
+
return referencedBlockIds(input, blocks)[0] ?? null;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
function inputMap(value: unknown): Record<string, unknown> {
|
|
352
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
353
|
+
? (value as Record<string, unknown>)
|
|
354
|
+
: {};
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
function blockMap(value: unknown): BlockMap {
|
|
358
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value) ? (value as BlockMap) : {};
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
function object(value: unknown): Record<string, unknown> {
|
|
362
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
363
|
+
throw new Error('TurboWarp project value must be an object.');
|
|
364
|
+
}
|
|
365
|
+
return value as Record<string, unknown>;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
function addDiagnostic(
|
|
369
|
+
diagnostics: TargetNeutralDiagnostic[],
|
|
370
|
+
routeId: string,
|
|
371
|
+
sourceRef: SourceRefV2,
|
|
372
|
+
code: ServerSubsetDiagnosticCode,
|
|
373
|
+
message: string,
|
|
374
|
+
reason: string,
|
|
375
|
+
suggestion: string
|
|
376
|
+
): void {
|
|
377
|
+
diagnostics.push({
|
|
378
|
+
severity: 'error',
|
|
379
|
+
phase: 'target-neutral',
|
|
380
|
+
code,
|
|
381
|
+
message,
|
|
382
|
+
routeId,
|
|
383
|
+
reason,
|
|
384
|
+
suggestion,
|
|
385
|
+
sourceRef
|
|
386
|
+
});
|
|
387
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type {SourceRefV2} from '../ir-v2/types.js';
|
|
2
|
+
|
|
3
|
+
export type ServerSubsetDiagnosticCode =
|
|
4
|
+
| 'TW2_GLOBAL_STATE_UNSUPPORTED'
|
|
5
|
+
| 'TW2_MESSAGE_UNSUPPORTED'
|
|
6
|
+
| 'TW2_UNBOUNDED_LOOP'
|
|
7
|
+
| 'TW2_CLONE_UNSUPPORTED'
|
|
8
|
+
| 'TW2_RUNTIME_DEPENDENCY_UNSUPPORTED'
|
|
9
|
+
| 'TW2_UNSUPPORTED_BLOCK'
|
|
10
|
+
| 'TW2_UNSUPPORTED_OPERATION'
|
|
11
|
+
| 'TW2_CONTROL_FLOW_UNSUPPORTED'
|
|
12
|
+
| 'TW2_LOOP_BOUND_INVALID'
|
|
13
|
+
| 'TW2_LOOP_NESTING_EXCEEDED'
|
|
14
|
+
| 'TW2_WORK_BUDGET_EXCEEDED'
|
|
15
|
+
| 'TW2_MISSING_RESPONSE'
|
|
16
|
+
| 'TW2_MULTIPLE_RESPONSE'
|
|
17
|
+
| 'TW2_RESPONSE_AFTER_TERMINAL'
|
|
18
|
+
| 'TW2_RESPONSE_IN_LOOP'
|
|
19
|
+
| 'TW2_HANDLER_VARIABLE_TYPE'
|
|
20
|
+
| 'TW2_BINDING_UNDECLARED'
|
|
21
|
+
| 'TW2_BINDING_TYPE_MISMATCH'
|
|
22
|
+
| 'TW2_BINDING_DUPLICATE'
|
|
23
|
+
| 'TW2_BRANCH_BINDING_MISMATCH'
|
|
24
|
+
| 'TW2_CAPABILITY_MISSING'
|
|
25
|
+
| 'TW2_CONDITION_TYPE'
|
|
26
|
+
| 'TW2_ITERATION_CONTEXT_REQUIRED'
|
|
27
|
+
| 'TW2_BINARY_BODY_CONSUMED'
|
|
28
|
+
| 'TW2_BINARY_BODY_SCOPE'
|
|
29
|
+
| 'TW2_BINARY_BODY_UNDECLARED'
|
|
30
|
+
| 'TW2_BINARY_LIMIT_INVALID'
|
|
31
|
+
| 'TW2_NAMED_RESPONSE_BODY_DISABLED';
|
|
32
|
+
|
|
33
|
+
export interface TargetNeutralDiagnostic {
|
|
34
|
+
severity: 'error';
|
|
35
|
+
phase: 'target-neutral';
|
|
36
|
+
code: ServerSubsetDiagnosticCode;
|
|
37
|
+
message: string;
|
|
38
|
+
routeId: string;
|
|
39
|
+
reason: string;
|
|
40
|
+
suggestion: string;
|
|
41
|
+
sourceRef?: SourceRefV2;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface TargetCapabilityDiagnostic {
|
|
45
|
+
severity: 'error';
|
|
46
|
+
phase: 'target-capability';
|
|
47
|
+
code: 'TW2_TARGET_CAPABILITY_UNSUPPORTED';
|
|
48
|
+
message: string;
|
|
49
|
+
routeId: string;
|
|
50
|
+
reason: string;
|
|
51
|
+
suggestion: string;
|
|
52
|
+
targetId: string;
|
|
53
|
+
sourceRef?: SourceRefV2;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface ServerSubsetPolicy {
|
|
57
|
+
maxLoopIterations: number;
|
|
58
|
+
maxLoopNesting: number;
|
|
59
|
+
maxRouteWork: number;
|
|
60
|
+
maxBinaryBytes: number;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export const DEFAULT_SERVER_SUBSET_POLICY: Readonly<ServerSubsetPolicy> = {
|
|
64
|
+
maxLoopIterations: 1000,
|
|
65
|
+
maxLoopNesting: 8,
|
|
66
|
+
maxRouteWork: 10_000,
|
|
67
|
+
maxBinaryBytes: 16 * 1024 * 1024
|
|
68
|
+
};
|