@kubohiroya/turbowarp-http-server 0.5.0 → 0.5.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 +2 -2
- package/README.md +2 -2
- package/dist/compiler/ir-v2/parse.js +38 -0
- package/dist/compiler/ir-v2/parse.js.map +1 -1
- package/dist/compiler/ir-v2/types.d.ts +39 -2
- package/dist/compiler/ir-v2/types.js +10 -1
- package/dist/compiler/ir-v2/types.js.map +1 -1
- package/dist/compiler/manifest/operations.js +35 -14
- package/dist/compiler/manifest/operations.js.map +1 -1
- package/dist/compiler/pipeline/core-generator.js +63 -2
- package/dist/compiler/pipeline/core-generator.js.map +1 -1
- package/dist/compiler/pipeline/requirements.js +10 -0
- package/dist/compiler/pipeline/requirements.js.map +1 -1
- package/dist/compiler/structured-data/lowering.d.ts +21 -2
- package/dist/compiler/structured-data/lowering.js +202 -65
- package/dist/compiler/structured-data/lowering.js.map +1 -1
- package/dist/compiler/structured-data/runtime.d.ts +22 -1
- package/dist/compiler/structured-data/runtime.js +82 -1
- package/dist/compiler/structured-data/runtime.js.map +1 -1
- package/dist/compiler/turbowarp-v2.js +7 -1
- package/dist/compiler/turbowarp-v2.js.map +1 -1
- package/dist/compiler/validator/ir-validator.js +33 -1
- package/dist/compiler/validator/ir-validator.js.map +1 -1
- package/dist/turbowarp-http-server.js +48 -21
- package/docs/compiler-manifest-registry.ja.md +2 -2
- package/docs/deploy-ir-v2.ja.md +18 -3
- package/docs/ir-v2-conformance.ja.md +1 -0
- package/docs/server-executable-subset.ja.md +3 -1
- package/docs/structured-data-lowering.ja.md +124 -27
- package/docs/structured-data-named-values.ja.md +129 -0
- package/package.json +2 -1
- package/schemas/deploy-ir-v2.schema.json +69 -0
- package/src/compiler/ir-v2/parse.ts +48 -0
- package/src/compiler/ir-v2/types.ts +35 -2
- package/src/compiler/manifest/operations.ts +86 -51
- package/src/compiler/pipeline/core-generator.ts +63 -2
- package/src/compiler/pipeline/requirements.ts +5 -0
- package/src/compiler/structured-data/lowering.ts +225 -65
- package/src/compiler/structured-data/runtime.ts +79 -1
- package/src/compiler/turbowarp-v2.ts +6 -1
- package/src/compiler/validator/ir-validator.ts +49 -1
- package/src/fixed-input-editors.ts +44 -19
|
@@ -9,11 +9,12 @@ import {
|
|
|
9
9
|
type StatementIrV2
|
|
10
10
|
} from '../ir-v2/types.js';
|
|
11
11
|
import {IrRuntimeError} from '../runtime/error.js';
|
|
12
|
-
import {
|
|
12
|
+
import {decodeStructuredJson, parseApplicationJson} from './runtime.js';
|
|
13
13
|
import {parseStructuredDataPath} from './path.js';
|
|
14
14
|
|
|
15
15
|
export type StructuredDataLoweringDiagnosticCode =
|
|
16
16
|
| 'TW2_STRUCTURED_INVALID_LITERAL'
|
|
17
|
+
| 'TW2_STRUCTURED_INVALID_NAME'
|
|
17
18
|
| 'TW2_STRUCTURED_DYNAMIC_PATH'
|
|
18
19
|
| 'TW2_STRUCTURED_INVALID_PATH'
|
|
19
20
|
| 'TW2_STRUCTURED_INVALID_MAX'
|
|
@@ -32,9 +33,38 @@ export interface StructuredDataLoweringDiagnostic {
|
|
|
32
33
|
|
|
33
34
|
export type StructuredDataArguments = Readonly<Record<string, ExpressionIrV2>>;
|
|
34
35
|
|
|
36
|
+
export const STRUCTURED_DATA_EXTENSION_ID = 'kubohiroyastructureddata';
|
|
35
37
|
export const STRUCTURED_DATA_PACKAGE_NAME = '@kubohiroya/turbowarp-structured-data';
|
|
36
|
-
|
|
38
|
+
/**
|
|
39
|
+
* Exact package versions whose manifest signatures and block semantics this lowering was checked
|
|
40
|
+
* against. Any other locked version stops at the first Structured Data block; nothing falls back.
|
|
41
|
+
*/
|
|
42
|
+
export const STRUCTURED_DATA_PACKAGE_VERSIONS: readonly string[] = ['0.11.0-rc.1'];
|
|
37
43
|
|
|
44
|
+
/** Server-supported operations of the manifest that later phases of the named-values design lower. */
|
|
45
|
+
const LATER_PHASE_OPERATIONS: Readonly<Record<string, string>> = {
|
|
46
|
+
'structuredData.parseYaml': 'phase 2 (YAML)',
|
|
47
|
+
'structuredData.toYaml': 'phase 2 (YAML)',
|
|
48
|
+
'structuredData.defineSchema': 'phase 3 (schema)',
|
|
49
|
+
'structuredData.beginSchema': 'phase 3 (schema)',
|
|
50
|
+
'structuredData.addSchemaProperty': 'phase 3 (schema)',
|
|
51
|
+
'structuredData.commitSchema': 'phase 3 (schema)',
|
|
52
|
+
'structuredData.deleteSchema': 'phase 3 (schema)',
|
|
53
|
+
'structuredData.hasSchema': 'phase 3 (schema)',
|
|
54
|
+
'structuredData.schemaOf': 'phase 3 (schema)',
|
|
55
|
+
'structuredData.parseJsonWithSchema': 'phase 3 (schema)',
|
|
56
|
+
'structuredData.parseYamlWithSchema': 'phase 3 (schema)'
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export function isStructuredDataEntry(entry: CompilerOpcodeRegistryEntry): boolean {
|
|
60
|
+
return entry.extensionId === STRUCTURED_DATA_EXTENSION_ID;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Lowers Structured Data 0.11 blocks. Named values become request-local IR state: reads go through
|
|
65
|
+
* `named-value`, writes through `named-value-set`, and every path operation is one of the existing
|
|
66
|
+
* immutable json nodes between them.
|
|
67
|
+
*/
|
|
38
68
|
export class StructuredDataLoweringContext {
|
|
39
69
|
public readonly diagnostics: StructuredDataLoweringDiagnostic[];
|
|
40
70
|
|
|
@@ -52,58 +82,124 @@ export class StructuredDataLoweringContext {
|
|
|
52
82
|
): ExpressionIrV2 | undefined {
|
|
53
83
|
const operation = this.operation(entry, sourceRef);
|
|
54
84
|
if (operation === undefined) return undefined;
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
85
|
+
switch (operation) {
|
|
86
|
+
case 'structuredData.currentKey':
|
|
87
|
+
return this.current('key', sourceRef);
|
|
88
|
+
case 'structuredData.currentIndex':
|
|
89
|
+
return this.current('index', sourceRef);
|
|
90
|
+
case 'structuredData.currentValue': {
|
|
91
|
+
const value = this.current('value', sourceRef);
|
|
92
|
+
return value === undefined ? undefined : jsonStringify(value, sourceRef);
|
|
93
|
+
}
|
|
94
|
+
case 'structuredData.currentGet':
|
|
95
|
+
case 'structuredData.currentGetText': {
|
|
96
|
+
// The browser checks for an active loop before it parses the path, so a missing loop is the
|
|
97
|
+
// only diagnostic even when the path is also invalid.
|
|
98
|
+
const value = this.current('value', sourceRef);
|
|
99
|
+
if (value === undefined) return undefined;
|
|
100
|
+
const path = this.literalPath(args.PATH, inputSource(sourceRef, 'PATH'));
|
|
101
|
+
if (path === undefined) return undefined;
|
|
102
|
+
const read = jsonGet(value, path, sourceRef);
|
|
103
|
+
return operation === 'structuredData.currentGet' ? jsonStringify(read, sourceRef) : scalarText(read, sourceRef);
|
|
104
|
+
}
|
|
105
|
+
case 'structuredData.hasData': {
|
|
106
|
+
const name = this.name(args, sourceRef);
|
|
107
|
+
return name === undefined ? undefined : {kind: 'named-value-exists', valueType: 'boolean', name, sourceRef};
|
|
108
|
+
}
|
|
109
|
+
case 'structuredData.toJson': {
|
|
110
|
+
const name = this.name(args, sourceRef);
|
|
111
|
+
return name === undefined ? undefined : jsonStringify(namedValue(name, sourceRef), sourceRef);
|
|
112
|
+
}
|
|
113
|
+
case 'structuredData.get':
|
|
114
|
+
case 'structuredData.getText':
|
|
115
|
+
case 'structuredData.has':
|
|
116
|
+
case 'structuredData.keys':
|
|
117
|
+
case 'structuredData.length': {
|
|
118
|
+
const name = this.name(args, sourceRef);
|
|
119
|
+
const path = this.literalPath(args.PATH, inputSource(sourceRef, 'PATH'));
|
|
120
|
+
if (name === undefined || path === undefined) return undefined;
|
|
121
|
+
const root = namedValue(name, sourceRef);
|
|
122
|
+
if (operation === 'structuredData.get') return jsonStringify(jsonGet(root, path, sourceRef), sourceRef);
|
|
123
|
+
if (operation === 'structuredData.getText') return scalarText(jsonGet(root, path, sourceRef), sourceRef);
|
|
124
|
+
if (operation === 'structuredData.has') return {kind: 'json-has', valueType: 'boolean', root, path, sourceRef};
|
|
125
|
+
if (operation === 'structuredData.keys') {
|
|
126
|
+
return jsonStringify({kind: 'json-keys', valueType: 'json-array', root, path, sourceRef}, sourceRef);
|
|
127
|
+
}
|
|
128
|
+
return {kind: 'json-length', valueType: 'number', root, path, sourceRef};
|
|
129
|
+
}
|
|
130
|
+
default:
|
|
131
|
+
return this.notLowered(operation, sourceRef);
|
|
68
132
|
}
|
|
69
|
-
|
|
70
|
-
if (root === undefined) return undefined;
|
|
71
|
-
if (operation === 'structuredData.normalizeJson') return jsonStringify(root, sourceRef);
|
|
133
|
+
}
|
|
72
134
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
if (operation ===
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
);
|
|
88
|
-
return
|
|
135
|
+
public lowerStatement(
|
|
136
|
+
entry: CompilerOpcodeRegistryEntry,
|
|
137
|
+
args: StructuredDataArguments,
|
|
138
|
+
sourceRef: SourceRefV2
|
|
139
|
+
): StatementIrV2 | undefined {
|
|
140
|
+
const operation = this.operation(entry, sourceRef);
|
|
141
|
+
if (operation === undefined) return undefined;
|
|
142
|
+
switch (operation) {
|
|
143
|
+
case 'structuredData.parseJson': {
|
|
144
|
+
// The browser decodes TEXT before it checks NAME, so the TEXT diagnostic comes first.
|
|
145
|
+
const text = args.TEXT;
|
|
146
|
+
if (text === undefined) return this.missingArgument('TEXT', sourceRef);
|
|
147
|
+
const value = this.decodeText(text, inputSource(sourceRef, 'TEXT'));
|
|
148
|
+
const name = this.name(args, sourceRef);
|
|
149
|
+
if (value === undefined || name === undefined) return undefined;
|
|
150
|
+
return {kind: 'named-value-set', name, value, schema: 'drop', sourceRef};
|
|
89
151
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
152
|
+
case 'structuredData.set': {
|
|
153
|
+
const name = this.name(args, sourceRef);
|
|
154
|
+
const path = this.literalPath(args.PATH, inputSource(sourceRef, 'PATH'));
|
|
155
|
+
const valueArgument = args.VALUE;
|
|
156
|
+
if (valueArgument === undefined) return this.missingArgument('VALUE', sourceRef);
|
|
157
|
+
const replacement = this.parseJsonArgument(valueArgument, inputSource(sourceRef, 'VALUE'));
|
|
158
|
+
if (name === undefined || path === undefined || replacement === undefined) return undefined;
|
|
159
|
+
return {
|
|
160
|
+
kind: 'named-value-set',
|
|
161
|
+
name,
|
|
162
|
+
value: {
|
|
163
|
+
kind: 'json-set',
|
|
164
|
+
valueType: JSON_VALUE_TYPE_V2,
|
|
165
|
+
root: namedValue(name, sourceRef),
|
|
166
|
+
path,
|
|
167
|
+
value: replacement,
|
|
168
|
+
sourceRef
|
|
169
|
+
},
|
|
170
|
+
schema: 'keep',
|
|
171
|
+
sourceRef
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
case 'structuredData.delete': {
|
|
175
|
+
const name = this.name(args, sourceRef);
|
|
176
|
+
const path = this.literalPath(args.PATH, inputSource(sourceRef, 'PATH'));
|
|
177
|
+
if (path !== undefined && path.length === 0) {
|
|
178
|
+
this.report(
|
|
179
|
+
'TW2_STRUCTURED_INVALID_PATH',
|
|
180
|
+
'Structured Data delete cannot target the root path.',
|
|
181
|
+
'The browser extension rejects deleting the root value.',
|
|
182
|
+
'Delete a child key/index, or use delete data to remove the whole value.',
|
|
183
|
+
inputSource(sourceRef, 'PATH')
|
|
184
|
+
);
|
|
185
|
+
return undefined;
|
|
186
|
+
}
|
|
187
|
+
if (name === undefined || path === undefined) return undefined;
|
|
188
|
+
return {
|
|
189
|
+
kind: 'named-value-set',
|
|
190
|
+
name,
|
|
191
|
+
value: {kind: 'json-delete', valueType: JSON_VALUE_TYPE_V2, root: namedValue(name, sourceRef), path, sourceRef},
|
|
192
|
+
schema: 'keep',
|
|
193
|
+
sourceRef
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
case 'structuredData.deleteData': {
|
|
197
|
+
const name = this.name(args, sourceRef);
|
|
198
|
+
return name === undefined ? undefined : {kind: 'named-value-delete', name, sourceRef};
|
|
199
|
+
}
|
|
200
|
+
default:
|
|
201
|
+
return this.notLowered(operation, sourceRef);
|
|
105
202
|
}
|
|
106
|
-
this.unsupported(operation, sourceRef);
|
|
107
203
|
}
|
|
108
204
|
|
|
109
205
|
public lowerForEach(
|
|
@@ -114,26 +210,26 @@ export class StructuredDataLoweringContext {
|
|
|
114
210
|
): StatementIrV2 | undefined {
|
|
115
211
|
const operation = this.operation(entry, sourceRef);
|
|
116
212
|
if (operation !== 'structuredData.forEach') {
|
|
117
|
-
if (operation !== undefined) this.
|
|
213
|
+
if (operation !== undefined) this.notLowered(operation, sourceRef);
|
|
118
214
|
return undefined;
|
|
119
215
|
}
|
|
120
|
-
const
|
|
121
|
-
if (jsonArgument === undefined) return this.missingArgument('JSON', sourceRef);
|
|
122
|
-
const root = this.parseJsonArgument(jsonArgument, inputSource(sourceRef, 'JSON'));
|
|
216
|
+
const name = this.name(args, sourceRef);
|
|
123
217
|
const path = this.literalPath(args.PATH, inputSource(sourceRef, 'PATH'));
|
|
124
218
|
const maxIterations = this.literalMaximum(args.MAX, inputSource(sourceRef, 'MAX'));
|
|
125
|
-
if (
|
|
219
|
+
if (name === undefined || path === undefined || maxIterations === undefined) return undefined;
|
|
126
220
|
const loopId = `structured_${sourceRef.targetIndex}_${sanitizeId(sourceRef.blockId)}`;
|
|
127
221
|
const nested = new StructuredDataLoweringContext(this.diagnostics, [...this.loopIds, loopId]);
|
|
128
222
|
const body = lowerBody(nested);
|
|
129
|
-
|
|
223
|
+
// json-for-each evaluates its root once, so the loop walks the value as it was when the loop
|
|
224
|
+
// started, as the browser does by copying the entries out before the first iteration.
|
|
225
|
+
return {kind: 'json-for-each', loopId, root: namedValue(name, sourceRef), path, maxIterations, body, sourceRef};
|
|
130
226
|
}
|
|
131
227
|
|
|
132
228
|
private operation(entry: CompilerOpcodeRegistryEntry, sourceRef: SourceRefV2): string | undefined {
|
|
133
229
|
if (
|
|
134
|
-
entry.extensionId !==
|
|
230
|
+
entry.extensionId !== STRUCTURED_DATA_EXTENSION_ID ||
|
|
135
231
|
entry.packageName !== STRUCTURED_DATA_PACKAGE_NAME ||
|
|
136
|
-
entry.packageVersion
|
|
232
|
+
!STRUCTURED_DATA_PACKAGE_VERSIONS.includes(entry.packageVersion) ||
|
|
137
233
|
entry.block.server?.supported !== true ||
|
|
138
234
|
entry.block.server.irOperation === undefined
|
|
139
235
|
) {
|
|
@@ -163,6 +259,48 @@ export class StructuredDataLoweringContext {
|
|
|
163
259
|
return {kind: 'iteration-value', valueType: JSON_VALUE_TYPE_V2, loopId, sourceRef};
|
|
164
260
|
}
|
|
165
261
|
|
|
262
|
+
/** NAME may be any expression; only a literal that is empty after trimming is known to fail. */
|
|
263
|
+
private name(args: StructuredDataArguments, sourceRef: SourceRefV2): ExpressionIrV2 | undefined {
|
|
264
|
+
const name = args.NAME;
|
|
265
|
+
if (name === undefined) return this.missingArgument('NAME', sourceRef);
|
|
266
|
+
if (isLiteralText(name) && name.value.trim().length === 0) {
|
|
267
|
+
this.report(
|
|
268
|
+
'TW2_STRUCTURED_INVALID_NAME',
|
|
269
|
+
'Structured Data name literal is empty.',
|
|
270
|
+
'The browser extension trims the name and rejects an empty one with INVALID_NAME.',
|
|
271
|
+
'Give the value a non-empty name.',
|
|
272
|
+
inputSource(sourceRef, 'NAME')
|
|
273
|
+
);
|
|
274
|
+
return undefined;
|
|
275
|
+
}
|
|
276
|
+
return name;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** parse JSON TEXT: a literal is decoded now with the codec limits, anything else at runtime. */
|
|
280
|
+
private decodeText(expression: ExpressionIrV2, sourceRef: SourceRefV2): ExpressionIrV2 | undefined {
|
|
281
|
+
if (!isLiteralText(expression)) {
|
|
282
|
+
return {kind: 'structured-decode', valueType: JSON_VALUE_TYPE_V2, format: 'json', text: expression, sourceRef};
|
|
283
|
+
}
|
|
284
|
+
try {
|
|
285
|
+
const value = decodeStructuredJson(expression.value);
|
|
286
|
+
return literal(jsonLiteralType(value), value, sourceRef);
|
|
287
|
+
} catch (error) {
|
|
288
|
+
if (!(error instanceof IrRuntimeError)) throw error;
|
|
289
|
+
const overLimit = error.code === 'PARSE_LIMIT_EXCEEDED';
|
|
290
|
+
this.report(
|
|
291
|
+
'TW2_STRUCTURED_INVALID_LITERAL',
|
|
292
|
+
overLimit ? 'Structured Data JSON literal exceeds the parse limits.' : 'Structured Data JSON literal is invalid.',
|
|
293
|
+
overLimit
|
|
294
|
+
? 'The browser codec rejects text over 256 KiB, deeper than 64 levels, or with more than 50,000 values.'
|
|
295
|
+
: 'Literal JSON is validated during compilation instead of deferring a known failure to runtime.',
|
|
296
|
+
overLimit ? 'Shrink the literal or read the text from the request.' : 'Provide valid JSON text.',
|
|
297
|
+
sourceRef
|
|
298
|
+
);
|
|
299
|
+
return undefined;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/** set VALUE: the browser only JSON.parses it, so neither literal nor dynamic VALUE has codec limits. */
|
|
166
304
|
private parseJsonArgument(expression: ExpressionIrV2, sourceRef: SourceRefV2): ExpressionIrV2 | undefined {
|
|
167
305
|
if (isLiteralText(expression)) {
|
|
168
306
|
try {
|
|
@@ -204,7 +342,7 @@ export class StructuredDataLoweringContext {
|
|
|
204
342
|
this.report(
|
|
205
343
|
'TW2_STRUCTURED_DYNAMIC_PATH',
|
|
206
344
|
'Structured Data path must be a compile-time string literal.',
|
|
207
|
-
'The
|
|
345
|
+
'The compiler accepts only the locked tw-structured-path grammar and does not evaluate dynamic paths.',
|
|
208
346
|
'Replace the path input with a literal such as $.items[0].',
|
|
209
347
|
sourceRef
|
|
210
348
|
);
|
|
@@ -247,12 +385,25 @@ export class StructuredDataLoweringContext {
|
|
|
247
385
|
);
|
|
248
386
|
}
|
|
249
387
|
|
|
388
|
+
/** An allowlisted operation this phase does not lower, or one used in the wrong block position. */
|
|
389
|
+
private notLowered(operation: string, sourceRef: SourceRefV2): undefined {
|
|
390
|
+
const phase = LATER_PHASE_OPERATIONS[operation];
|
|
391
|
+
if (phase === undefined) return this.unsupported(operation, sourceRef);
|
|
392
|
+
this.report(
|
|
393
|
+
'TW2_UNSUPPORTED_OPERATION',
|
|
394
|
+
`Structured Data operation ${operation} is not lowered for the server until ${phase}.`,
|
|
395
|
+
`Phase 1 lowers only the JSON operations; ${phase} of the named-values design adds this one, and the compiler never substitutes a similar operation.`,
|
|
396
|
+
'Run this block in the browser or through the WebSocket bridge, or remove it from the HTTP handler.',
|
|
397
|
+
sourceRef
|
|
398
|
+
);
|
|
399
|
+
}
|
|
400
|
+
|
|
250
401
|
private unsupported(operation: string, sourceRef: SourceRefV2): undefined {
|
|
251
402
|
this.report(
|
|
252
403
|
'TW2_UNSUPPORTED_OPERATION',
|
|
253
404
|
`Structured Data operation is not supported by this lowering phase: ${operation}`,
|
|
254
|
-
'Only compiler-allowlisted operations from
|
|
255
|
-
'
|
|
405
|
+
'Only compiler-allowlisted operations from a verified locked manifest version may be lowered.',
|
|
406
|
+
'Lock a verified Structured Data version and use its blocks.',
|
|
256
407
|
sourceRef
|
|
257
408
|
);
|
|
258
409
|
}
|
|
@@ -268,10 +419,6 @@ export class StructuredDataLoweringContext {
|
|
|
268
419
|
}
|
|
269
420
|
}
|
|
270
421
|
|
|
271
|
-
function requireArgument(args: StructuredDataArguments, name: string): ExpressionIrV2 | undefined {
|
|
272
|
-
return args[name];
|
|
273
|
-
}
|
|
274
|
-
|
|
275
422
|
function isLiteralText(expression: ExpressionIrV2 | undefined): expression is Extract<ExpressionIrV2, {kind: 'literal'}> & {
|
|
276
423
|
valueType: 'string' | 'json-text';
|
|
277
424
|
value: string;
|
|
@@ -300,10 +447,23 @@ function literal(
|
|
|
300
447
|
return {kind: 'literal', valueType, value, sourceRef} as ExpressionIrV2;
|
|
301
448
|
}
|
|
302
449
|
|
|
450
|
+
/** Each read gets its own copy of the name, so no IR node is shared between two parents. */
|
|
451
|
+
function namedValue(name: ExpressionIrV2, sourceRef: SourceRefV2): ExpressionIrV2 {
|
|
452
|
+
return {kind: 'named-value', valueType: JSON_VALUE_TYPE_V2, name: structuredClone(name), sourceRef};
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
function jsonGet(root: ExpressionIrV2, path: PathSegmentV2[], sourceRef: SourceRefV2): ExpressionIrV2 {
|
|
456
|
+
return {kind: 'json-get', valueType: JSON_VALUE_TYPE_V2, root, path, sourceRef};
|
|
457
|
+
}
|
|
458
|
+
|
|
303
459
|
function jsonStringify(value: ExpressionIrV2, sourceRef: SourceRefV2): ExpressionIrV2 {
|
|
304
460
|
return {kind: 'json-stringify', valueType: 'json-text', value, sourceRef};
|
|
305
461
|
}
|
|
306
462
|
|
|
463
|
+
function scalarText(value: ExpressionIrV2, sourceRef: SourceRefV2): ExpressionIrV2 {
|
|
464
|
+
return {kind: 'json-scalar-text', valueType: 'string', value, sourceRef};
|
|
465
|
+
}
|
|
466
|
+
|
|
307
467
|
function inputSource(sourceRef: SourceRefV2, input: string): SourceRefV2 {
|
|
308
468
|
return {...sourceRef, input};
|
|
309
469
|
}
|
|
@@ -5,14 +5,28 @@ export const STRUCTURED_DATA_HTTP_STATUS = 422;
|
|
|
5
5
|
|
|
6
6
|
export const STRUCTURED_DATA_RUNTIME_ERROR_CODES = [
|
|
7
7
|
'INVALID_JSON',
|
|
8
|
+
'INVALID_NAME',
|
|
9
|
+
'DATA_NOT_FOUND',
|
|
8
10
|
'INVALID_PATH',
|
|
9
11
|
'PATH_NOT_FOUND',
|
|
10
12
|
'TYPE_MISMATCH',
|
|
11
13
|
'INDEX_OUT_OF_RANGE',
|
|
12
14
|
'ITERATION_LIMIT_EXCEEDED',
|
|
13
|
-
'ITERATION_CONTEXT_REQUIRED'
|
|
15
|
+
'ITERATION_CONTEXT_REQUIRED',
|
|
16
|
+
'PARSE_LIMIT_EXCEEDED'
|
|
14
17
|
] as const;
|
|
15
18
|
|
|
19
|
+
/**
|
|
20
|
+
* The limits `@kubohiroya/turbowarp-yaml-json`'s codec applies when Structured Data parses text. The
|
|
21
|
+
* root value is at depth 0 and each nested value one deeper; every value counts toward the node limit,
|
|
22
|
+
* object keys do not.
|
|
23
|
+
*/
|
|
24
|
+
export const STRUCTURED_DECODE_LIMITS = {
|
|
25
|
+
maxInputBytes: 256 * 1024,
|
|
26
|
+
maxDepth: 64,
|
|
27
|
+
maxNodes: 50_000
|
|
28
|
+
} as const;
|
|
29
|
+
|
|
16
30
|
export type StructuredDataRuntimeErrorCode = (typeof STRUCTURED_DATA_RUNTIME_ERROR_CODES)[number];
|
|
17
31
|
|
|
18
32
|
export interface StructuredIterationEntry {
|
|
@@ -32,6 +46,61 @@ export function parseApplicationJson(text: unknown): JsonValue {
|
|
|
32
46
|
}
|
|
33
47
|
}
|
|
34
48
|
|
|
49
|
+
/**
|
|
50
|
+
* Decodes JSON text the way Structured Data's `parse JSON` block does: the UTF-8 size is checked before
|
|
51
|
+
* parsing, a syntax error is INVALID_JSON, and the depth and node limits are then checked in the same
|
|
52
|
+
* depth-first order as the codec, so a non-finite number met first is INVALID_JSON and a limit met
|
|
53
|
+
* first is PARSE_LIMIT_EXCEEDED.
|
|
54
|
+
*/
|
|
55
|
+
export function decodeStructuredJson(text: string): JsonValue {
|
|
56
|
+
if (exceedsInputBytes(text)) {
|
|
57
|
+
runtimeError('PARSE_LIMIT_EXCEEDED', 'Input exceeds the Structured Data size limit.');
|
|
58
|
+
}
|
|
59
|
+
let value: unknown;
|
|
60
|
+
try {
|
|
61
|
+
value = JSON.parse(text);
|
|
62
|
+
} catch {
|
|
63
|
+
runtimeError('INVALID_JSON', 'Input must be valid JSON.');
|
|
64
|
+
}
|
|
65
|
+
let nodes = 0;
|
|
66
|
+
const visit = (item: unknown, depth: number): void => {
|
|
67
|
+
if (depth > STRUCTURED_DECODE_LIMITS.maxDepth) {
|
|
68
|
+
runtimeError('PARSE_LIMIT_EXCEEDED', 'Data exceeds the Structured Data nesting limit.');
|
|
69
|
+
}
|
|
70
|
+
nodes += 1;
|
|
71
|
+
if (nodes > STRUCTURED_DECODE_LIMITS.maxNodes) {
|
|
72
|
+
runtimeError('PARSE_LIMIT_EXCEEDED', 'Data exceeds the Structured Data node limit.');
|
|
73
|
+
}
|
|
74
|
+
if (item === null || typeof item === 'string' || typeof item === 'boolean') return;
|
|
75
|
+
if (typeof item === 'number') {
|
|
76
|
+
if (Number.isFinite(item)) return;
|
|
77
|
+
runtimeError('INVALID_JSON', 'JSON contains an unsupported value.');
|
|
78
|
+
}
|
|
79
|
+
if (Array.isArray(item)) {
|
|
80
|
+
for (const child of item) visit(child, depth + 1);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
const record = item as Record<string, unknown>;
|
|
84
|
+
for (const key of Object.keys(record)) visit(record[key], depth + 1);
|
|
85
|
+
};
|
|
86
|
+
visit(value, 0);
|
|
87
|
+
return value as JsonValue;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** The name a Structured Data block reads: Scratch's Cast.toString, then trimmed; empty is INVALID_NAME. */
|
|
91
|
+
export function structuredDataName(value: unknown): string {
|
|
92
|
+
const name = String(value).trim();
|
|
93
|
+
if (name.length === 0) runtimeError('INVALID_NAME', 'Structured data name cannot be empty.');
|
|
94
|
+
return name;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** A scalar as reporter text: a string as is, a number or boolean as JSON; null and containers fail. */
|
|
98
|
+
export function jsonScalarText(value: JsonValue): string {
|
|
99
|
+
if (typeof value === 'string') return value;
|
|
100
|
+
if (typeof value === 'number' || typeof value === 'boolean') return stringifyApplicationJson(value);
|
|
101
|
+
runtimeError('TYPE_MISMATCH', 'Text requires a string, number, or boolean.');
|
|
102
|
+
}
|
|
103
|
+
|
|
35
104
|
export function isValidApplicationJson(text: unknown): boolean {
|
|
36
105
|
try {
|
|
37
106
|
parseApplicationJson(text);
|
|
@@ -208,6 +277,15 @@ function assertJsonValue(value: unknown): asserts value is JsonValue {
|
|
|
208
277
|
runtimeError('INVALID_JSON', 'JSON contains an unsupported value.');
|
|
209
278
|
}
|
|
210
279
|
|
|
280
|
+
/**
|
|
281
|
+
* Counts with TextEncoder as the codec does, so a lone surrogate is the three bytes of U+FFFD. Every
|
|
282
|
+
* UTF-16 code unit encodes to at least one byte, so a longer string is over the limit without encoding.
|
|
283
|
+
*/
|
|
284
|
+
function exceedsInputBytes(text: string): boolean {
|
|
285
|
+
const limit = STRUCTURED_DECODE_LIMITS.maxInputBytes;
|
|
286
|
+
return text.length > limit || new TextEncoder().encode(text).byteLength > limit;
|
|
287
|
+
}
|
|
288
|
+
|
|
211
289
|
function isJsonObject(value: JsonValue): value is {[key: string]: JsonValue} {
|
|
212
290
|
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
213
291
|
}
|
|
@@ -13,7 +13,7 @@ import {
|
|
|
13
13
|
import type {CompilerOpcodeRegistry, CompilerOpcodeRegistryEntry} from './manifest/types.js';
|
|
14
14
|
import {isKvsEntry, KvsLoweringContext} from './kvs/lowering.js';
|
|
15
15
|
import {lowerNamedBodyResponse, type NamedBodyArguments} from './named-body/lowering.js';
|
|
16
|
-
import {StructuredDataLoweringContext} from './structured-data/lowering.js';
|
|
16
|
+
import {isStructuredDataEntry, StructuredDataLoweringContext} from './structured-data/lowering.js';
|
|
17
17
|
import {validateTurboWarpServerSubset} from './validator/turbowarp-subset.js';
|
|
18
18
|
|
|
19
19
|
const PREFIX = `${extensionConfig.id}_`;
|
|
@@ -326,6 +326,11 @@ function compileStatement(
|
|
|
326
326
|
if (statement !== undefined) addCapability(context, {kind: 'key-value-store'});
|
|
327
327
|
return statement ?? null;
|
|
328
328
|
}
|
|
329
|
+
if (manifestEntry !== undefined && isStructuredDataEntry(manifestEntry)) {
|
|
330
|
+
const args = compileManifestArguments(context, manifestEntry, inputs, id, structured);
|
|
331
|
+
if (args === null) return null;
|
|
332
|
+
return structured.lowerStatement(manifestEntry, args, source) ?? null;
|
|
333
|
+
}
|
|
329
334
|
report(context, 'TW2_UNSUPPORTED_COMMAND', `Unsupported command block: ${opcode}`, source);
|
|
330
335
|
return null;
|
|
331
336
|
}
|
|
@@ -177,6 +177,17 @@ function analyzeStatement(
|
|
|
177
177
|
if (statement.kind === 'kvs-set-text') {
|
|
178
178
|
requireScratchScalar(validateExpression(statement.value, bindings, context), context, statement.sourceRef, 'KVS text values');
|
|
179
179
|
}
|
|
180
|
+
} else if (statement.kind === 'named-value-set') {
|
|
181
|
+
// The value is evaluated before the name at runtime; the order here only affects diagnostics.
|
|
182
|
+
requireJsonValue(
|
|
183
|
+
validateExpression(statement.value, bindings, context),
|
|
184
|
+
context,
|
|
185
|
+
statement.value.sourceRef ?? statement.sourceRef,
|
|
186
|
+
'Structured Data named value'
|
|
187
|
+
);
|
|
188
|
+
requireScratchScalar(validateExpression(statement.name, bindings, context), context, statement.sourceRef, 'Structured Data names');
|
|
189
|
+
} else if (statement.kind === 'named-value-delete') {
|
|
190
|
+
requireScratchScalar(validateExpression(statement.name, bindings, context), context, statement.sourceRef, 'Structured Data names');
|
|
180
191
|
} else if (statement.kind === 'record-create') {
|
|
181
192
|
requireCapability({kind: 'record-store'}, context, statement.sourceRef);
|
|
182
193
|
validateExpression(statement.data, bindings, context);
|
|
@@ -481,6 +492,30 @@ function validateExpression(
|
|
|
481
492
|
'Structured Data replacement'
|
|
482
493
|
);
|
|
483
494
|
}
|
|
495
|
+
if (expression.kind === 'named-value' || expression.kind === 'named-value-exists') {
|
|
496
|
+
requireScratchScalar(
|
|
497
|
+
validateExpression(expression.name, bindings, context),
|
|
498
|
+
context,
|
|
499
|
+
expression.sourceRef,
|
|
500
|
+
'Structured Data names'
|
|
501
|
+
);
|
|
502
|
+
}
|
|
503
|
+
if (expression.kind === 'structured-decode') {
|
|
504
|
+
requireScratchScalar(
|
|
505
|
+
validateExpression(expression.text, bindings, context),
|
|
506
|
+
context,
|
|
507
|
+
expression.sourceRef,
|
|
508
|
+
'Structured Data decode text'
|
|
509
|
+
);
|
|
510
|
+
}
|
|
511
|
+
if (expression.kind === 'json-scalar-text') {
|
|
512
|
+
requireJsonValue(
|
|
513
|
+
validateExpression(expression.value, bindings, context),
|
|
514
|
+
context,
|
|
515
|
+
expression.sourceRef,
|
|
516
|
+
'Structured Data text input'
|
|
517
|
+
);
|
|
518
|
+
}
|
|
484
519
|
if (
|
|
485
520
|
expression.kind === 'iteration-key' ||
|
|
486
521
|
expression.kind === 'iteration-index' ||
|
|
@@ -611,8 +646,11 @@ function requireScratchScalar(
|
|
|
611
646
|
sourceRef: SourceRefV2 | undefined,
|
|
612
647
|
subject: string
|
|
613
648
|
): void {
|
|
649
|
+
// json-text is a string at runtime, so it is Scratch text wherever a scalar is cast to a string.
|
|
614
650
|
const members = valueTypeMembers(type);
|
|
615
|
-
if (members.every((member) => member === 'string' || member === 'number' || member === 'boolean'))
|
|
651
|
+
if (members.every((member) => member === 'string' || member === 'json-text' || member === 'number' || member === 'boolean')) {
|
|
652
|
+
return;
|
|
653
|
+
}
|
|
616
654
|
report(
|
|
617
655
|
context,
|
|
618
656
|
'TW2_HANDLER_VARIABLE_TYPE',
|
|
@@ -692,6 +730,11 @@ function statementWork(statement: StatementIrV2, limit: number): number {
|
|
|
692
730
|
work = saturatedAdd(work, expressionWork(statement.key, limit), limit);
|
|
693
731
|
if (statement.kind === 'kvs-set-text') work = saturatedAdd(work, expressionWork(statement.value, limit), limit);
|
|
694
732
|
}
|
|
733
|
+
if (statement.kind === 'named-value-set') {
|
|
734
|
+
work = saturatedAdd(work, expressionWork(statement.name, limit), limit);
|
|
735
|
+
work = saturatedAdd(work, expressionWork(statement.value, limit), limit);
|
|
736
|
+
}
|
|
737
|
+
if (statement.kind === 'named-value-delete') work = saturatedAdd(work, expressionWork(statement.name, limit), limit);
|
|
695
738
|
if (statement.kind === 'record-create') work = saturatedAdd(work, expressionWork(statement.data, limit), limit);
|
|
696
739
|
if (statement.kind === 'record-get' || statement.kind === 'record-delete') {
|
|
697
740
|
work = saturatedAdd(work, expressionWork(statement.id, limit), limit);
|
|
@@ -746,6 +789,11 @@ function expressionWork(expression: ExpressionIrV2, limit: number): number {
|
|
|
746
789
|
limit
|
|
747
790
|
);
|
|
748
791
|
}
|
|
792
|
+
if (expression.kind === 'named-value' || expression.kind === 'named-value-exists') {
|
|
793
|
+
return saturatedAdd(1, expressionWork(expression.name, limit), limit);
|
|
794
|
+
}
|
|
795
|
+
if (expression.kind === 'structured-decode') return saturatedAdd(1, expressionWork(expression.text, limit), limit);
|
|
796
|
+
if (expression.kind === 'json-scalar-text') return saturatedAdd(1, expressionWork(expression.value, limit), limit);
|
|
749
797
|
return 1;
|
|
750
798
|
}
|
|
751
799
|
|