@kubohiroya/turbowarp-http-server 0.5.0 → 0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/README.ja.md +2 -2
  2. package/README.md +2 -2
  3. package/dist/compiler/ir-v2/parse.js +38 -0
  4. package/dist/compiler/ir-v2/parse.js.map +1 -1
  5. package/dist/compiler/ir-v2/types.d.ts +39 -2
  6. package/dist/compiler/ir-v2/types.js +10 -1
  7. package/dist/compiler/ir-v2/types.js.map +1 -1
  8. package/dist/compiler/manifest/operations.js +35 -14
  9. package/dist/compiler/manifest/operations.js.map +1 -1
  10. package/dist/compiler/pipeline/core-generator.js +63 -2
  11. package/dist/compiler/pipeline/core-generator.js.map +1 -1
  12. package/dist/compiler/pipeline/requirements.js +10 -0
  13. package/dist/compiler/pipeline/requirements.js.map +1 -1
  14. package/dist/compiler/structured-data/lowering.d.ts +21 -2
  15. package/dist/compiler/structured-data/lowering.js +202 -65
  16. package/dist/compiler/structured-data/lowering.js.map +1 -1
  17. package/dist/compiler/structured-data/runtime.d.ts +22 -1
  18. package/dist/compiler/structured-data/runtime.js +82 -1
  19. package/dist/compiler/structured-data/runtime.js.map +1 -1
  20. package/dist/compiler/turbowarp-v2.js +7 -1
  21. package/dist/compiler/turbowarp-v2.js.map +1 -1
  22. package/dist/compiler/validator/ir-validator.js +33 -1
  23. package/dist/compiler/validator/ir-validator.js.map +1 -1
  24. package/dist/turbowarp-http-server.js +49 -22
  25. package/docs/compiler-manifest-registry.ja.md +2 -2
  26. package/docs/deploy-ir-v2.ja.md +18 -3
  27. package/docs/ir-v2-conformance.ja.md +1 -0
  28. package/docs/server-executable-subset.ja.md +3 -1
  29. package/docs/structured-data-lowering.ja.md +124 -27
  30. package/docs/structured-data-named-values.ja.md +129 -0
  31. package/package.json +6 -5
  32. package/schemas/deploy-ir-v2.schema.json +69 -0
  33. package/src/compiler/ir-v2/parse.ts +48 -0
  34. package/src/compiler/ir-v2/types.ts +35 -2
  35. package/src/compiler/manifest/operations.ts +86 -51
  36. package/src/compiler/pipeline/core-generator.ts +63 -2
  37. package/src/compiler/pipeline/requirements.ts +5 -0
  38. package/src/compiler/structured-data/lowering.ts +225 -65
  39. package/src/compiler/structured-data/runtime.ts +79 -1
  40. package/src/compiler/turbowarp-v2.ts +6 -1
  41. package/src/compiler/validator/ir-validator.ts +49 -1
  42. package/src/fixed-input-editors.ts +44 -19
@@ -24,7 +24,11 @@ export const IR_V2_EXPRESSION_KINDS = [
24
24
  'json-length',
25
25
  'iteration-key',
26
26
  'iteration-index',
27
- 'iteration-value'
27
+ 'iteration-value',
28
+ 'named-value',
29
+ 'named-value-exists',
30
+ 'structured-decode',
31
+ 'json-scalar-text'
28
32
  ] as const;
29
33
 
30
34
  export const IR_V2_STATEMENT_KINDS = [
@@ -49,6 +53,8 @@ export const IR_V2_STATEMENT_KINDS = [
49
53
  'if',
50
54
  'bounded-loop',
51
55
  'json-for-each',
56
+ 'named-value-set',
57
+ 'named-value-delete',
52
58
  'respond-binary',
53
59
  'respond-named-body',
54
60
  'respond'
@@ -88,6 +94,9 @@ export const IR_V2_STATEMENT_EFFECTS = {
88
94
  if: ['control'],
89
95
  'bounded-loop': ['control'],
90
96
  'json-for-each': ['control'],
97
+ // Named values live in the same request-local store lifetime as handler variables.
98
+ 'named-value-set': ['handler-state-write'],
99
+ 'named-value-delete': ['handler-state-write'],
91
100
  'respond-binary': ['response-write', 'binary-consume'],
92
101
  'respond-named-body': ['response-write'],
93
102
  respond: ['response-write']
@@ -129,6 +138,12 @@ export type NamedDataKindV2 = 'structured' | 'document' | 'binary' | 'asset';
129
138
  export type NamedDataScopeV2 = 'target' | 'project';
130
139
  export type NamedBodyRepresentationV2 = 'json' | 'yaml' | 'html' | 'markdown' | 'raw';
131
140
 
141
+ /** Structured Data codec formats; phase 1 decodes JSON only. */
142
+ export type StructuredDecodeFormatV2 = 'json';
143
+
144
+ /** Whether a named-value write drops (`parse`) or keeps (path update) the binding's schema reference. */
145
+ export type NamedValueSchemaPolicyV2 = 'drop' | 'keep';
146
+
132
147
  export interface NamedDataReferenceV2 {
133
148
  namespace: string;
134
149
  name: string;
@@ -299,7 +314,17 @@ export type ExpressionIrV2 =
299
314
  }
300
315
  | {kind: 'iteration-key'; valueType: 'string'; loopId: string; sourceRef?: SourceRefV2}
301
316
  | {kind: 'iteration-index'; valueType: 'number'; loopId: string; sourceRef?: SourceRefV2}
302
- | {kind: 'iteration-value'; valueType: JsonValueTypeV2; loopId: string; sourceRef?: SourceRefV2};
317
+ | {kind: 'iteration-value'; valueType: JsonValueTypeV2; loopId: string; sourceRef?: SourceRefV2}
318
+ | {kind: 'named-value'; valueType: JsonValueTypeV2; name: ExpressionIrV2; sourceRef?: SourceRefV2}
319
+ | {kind: 'named-value-exists'; valueType: 'boolean'; name: ExpressionIrV2; sourceRef?: SourceRefV2}
320
+ | {
321
+ kind: 'structured-decode';
322
+ valueType: JsonValueTypeV2;
323
+ format: StructuredDecodeFormatV2;
324
+ text: ExpressionIrV2;
325
+ sourceRef?: SourceRefV2;
326
+ }
327
+ | {kind: 'json-scalar-text'; valueType: 'string'; value: ExpressionIrV2; sourceRef?: SourceRefV2};
303
328
 
304
329
  export type StatementIrV2 =
305
330
  | {kind: 'set-status'; status: number; sourceRef?: SourceRefV2}
@@ -401,6 +426,14 @@ export type StatementIrV2 =
401
426
  body: StatementIrV2[];
402
427
  sourceRef?: SourceRefV2;
403
428
  }
429
+ | {
430
+ kind: 'named-value-set';
431
+ name: ExpressionIrV2;
432
+ value: ExpressionIrV2;
433
+ schema: NamedValueSchemaPolicyV2;
434
+ sourceRef?: SourceRefV2;
435
+ }
436
+ | {kind: 'named-value-delete'; name: ExpressionIrV2; sourceRef?: SourceRefV2}
404
437
  | {
405
438
  kind: 'respond-binary';
406
439
  body: string;
@@ -57,76 +57,102 @@ export const KNOWN_SERVER_OPERATIONS: Readonly<Record<string, ServerOperationSig
57
57
  'storage-write',
58
58
  false
59
59
  ),
60
+ // Structured Data 0.11.0-rc.1: every server-supported block of the published manifest. Phases 2
61
+ // (YAML) and 3 (schema) are listed so the whole manifest registers; their lowering still stops.
62
+ 'structuredData.addSchemaProperty': signature(
63
+ 'addSchemaProperty',
64
+ 'COMMAND',
65
+ [stringArgument('PROPERTY'), stringArgument('REQUIRED'), stringArgument('TYPE')],
66
+ 'void',
67
+ 'state'
68
+ ),
69
+ 'structuredData.beginSchema': signature(
70
+ 'beginSchema',
71
+ 'COMMAND',
72
+ [stringArgument('EXTRA'), stringArgument('SCHEMA_ID'), stringArgument('VERSION')],
73
+ 'void',
74
+ 'state'
75
+ ),
76
+ 'structuredData.commitSchema': signature('commitSchema', 'COMMAND', [], 'void', 'state'),
77
+ 'structuredData.currentGet': signature('currentJsonAtPath', 'REPORTER', [pathArgument()], 'jsonText', 'control'),
78
+ 'structuredData.currentGetText': signature('currentTextAtPath', 'REPORTER', [pathArgument()], 'string', 'control'),
60
79
  'structuredData.currentIndex': signature('currentIndex', 'REPORTER', [], 'number', 'control'),
61
80
  'structuredData.currentKey': signature('currentKey', 'REPORTER', [], 'string', 'control'),
62
- 'structuredData.currentValue': signature('currentValueJson', 'REPORTER', [], 'json', 'control'),
63
- 'structuredData.delete': signature(
64
- 'deleteAtPath',
65
- 'REPORTER',
66
- [{id: 'JSON', type: STRING}, pathArgument()],
67
- 'json',
68
- 'immutable'
81
+ 'structuredData.currentValue': signature('currentValueJson', 'REPORTER', [], 'jsonText', 'control'),
82
+ 'structuredData.defineSchema': signature(
83
+ 'defineSchema',
84
+ 'COMMAND',
85
+ [stringArgument('SCHEMA_ID'), stringArgument('TEXT'), stringArgument('VERSION')],
86
+ 'void',
87
+ 'state'
88
+ ),
89
+ 'structuredData.delete': signature('deleteAtPath', 'COMMAND', [nameArgument(), pathArgument()], 'void', 'state'),
90
+ 'structuredData.deleteData': signature('deleteStructuredData', 'COMMAND', [nameArgument()], 'void', 'state'),
91
+ 'structuredData.deleteSchema': signature(
92
+ 'deleteSchema',
93
+ 'COMMAND',
94
+ [stringArgument('SCHEMA_ID'), stringArgument('VERSION')],
95
+ 'void',
96
+ 'state'
69
97
  ),
70
98
  'structuredData.forEach': signature(
71
99
  'forEachAtPath',
72
100
  'LOOP',
73
- [
74
- {id: 'JSON', type: STRING},
75
- {id: 'MAX', type: NUMBER, staticLiteral: true, minimum: 1, maximum: 1000},
76
- pathArgument()
77
- ],
101
+ [{id: 'MAX', type: NUMBER, staticLiteral: true, minimum: 1, maximum: 1000}, nameArgument(), pathArgument()],
78
102
  'void',
79
103
  'control'
80
104
  ),
81
- 'structuredData.get': signature(
82
- 'getJsonAtPath',
83
- 'REPORTER',
84
- [{id: 'JSON', type: STRING}, pathArgument()],
85
- 'json',
86
- 'pure'
87
- ),
88
- 'structuredData.has': signature(
89
- 'hasPath',
105
+ 'structuredData.get': signature('getJsonAtPath', 'REPORTER', [nameArgument(), pathArgument()], 'jsonText', 'pure'),
106
+ 'structuredData.getText': signature('getTextAtPath', 'REPORTER', [nameArgument(), pathArgument()], 'string', 'pure'),
107
+ 'structuredData.has': signature('hasPath', 'BOOLEAN', [nameArgument(), pathArgument()], 'boolean', 'pure'),
108
+ 'structuredData.hasData': signature('hasStructuredData', 'BOOLEAN', [nameArgument()], 'boolean', 'pure'),
109
+ 'structuredData.hasSchema': signature(
110
+ 'hasSchema',
90
111
  'BOOLEAN',
91
- [{id: 'JSON', type: STRING}, pathArgument()],
112
+ [stringArgument('SCHEMA_ID'), stringArgument('VERSION')],
92
113
  'boolean',
93
114
  'pure'
94
115
  ),
95
- 'structuredData.isValidJson': signature(
96
- 'isValidJson',
97
- 'BOOLEAN',
98
- [{id: 'JSON', type: STRING}],
99
- 'boolean',
100
- 'pure'
116
+ 'structuredData.keys': signature('keysAtPath', 'REPORTER', [nameArgument(), pathArgument()], 'jsonText', 'pure'),
117
+ 'structuredData.length': signature('lengthAtPath', 'REPORTER', [nameArgument(), pathArgument()], 'number', 'pure'),
118
+ 'structuredData.parseJson': signature(
119
+ 'parseJson',
120
+ 'COMMAND',
121
+ [nameArgument(), stringArgument('TEXT')],
122
+ 'void',
123
+ 'state'
101
124
  ),
102
- 'structuredData.keys': signature(
103
- 'keysAtPath',
104
- 'REPORTER',
105
- [{id: 'JSON', type: STRING}, pathArgument()],
106
- 'json',
107
- 'pure'
125
+ 'structuredData.parseJsonWithSchema': signature(
126
+ 'parseJsonWithSchema',
127
+ 'COMMAND',
128
+ [nameArgument(), stringArgument('SCHEMA_ID'), stringArgument('TEXT'), stringArgument('VERSION')],
129
+ 'void',
130
+ 'state'
108
131
  ),
109
- 'structuredData.length': signature(
110
- 'lengthAtPath',
111
- 'REPORTER',
112
- [{id: 'JSON', type: STRING}, pathArgument()],
113
- 'number',
114
- 'pure'
132
+ 'structuredData.parseYaml': signature(
133
+ 'parseYaml',
134
+ 'COMMAND',
135
+ [nameArgument(), stringArgument('TEXT')],
136
+ 'void',
137
+ 'state'
115
138
  ),
116
- 'structuredData.normalizeJson': signature(
117
- 'normalizeJson',
118
- 'REPORTER',
119
- [{id: 'JSON', type: STRING}],
120
- 'json',
121
- 'pure'
139
+ 'structuredData.parseYamlWithSchema': signature(
140
+ 'parseYamlWithSchema',
141
+ 'COMMAND',
142
+ [nameArgument(), stringArgument('SCHEMA_ID'), stringArgument('TEXT'), stringArgument('VERSION')],
143
+ 'void',
144
+ 'state'
122
145
  ),
146
+ 'structuredData.schemaOf': signature('schemaOf', 'REPORTER', [nameArgument()], 'jsonText', 'pure'),
123
147
  'structuredData.set': signature(
124
148
  'setJsonAtPath',
125
- 'REPORTER',
126
- [{id: 'JSON', type: STRING}, pathArgument(), {id: 'VALUE', type: STRING}],
127
- 'json',
128
- 'immutable'
129
- )
149
+ 'COMMAND',
150
+ [nameArgument(), pathArgument(), stringArgument('VALUE')],
151
+ 'void',
152
+ 'state'
153
+ ),
154
+ 'structuredData.toJson': signature('toJson', 'REPORTER', [nameArgument()], 'jsonText', 'pure'),
155
+ 'structuredData.toYaml': signature('toYaml', 'REPORTER', [nameArgument()], 'yamlText', 'pure')
130
156
  };
131
157
 
132
158
  export function validateServerOperationHint(block: CompilerManifestBlock): void {
@@ -163,6 +189,15 @@ function signature(
163
189
  return {opcode, blockType, arguments: argumentsValue, resultType, effect, immutable};
164
190
  }
165
191
 
192
+ /** Argument objects keep the manifest parser's key order, because signatures compare as JSON text. */
193
+ function stringArgument(id: string): CompilerManifestArgument {
194
+ return {id, type: STRING};
195
+ }
196
+
197
+ function nameArgument(): CompilerManifestArgument {
198
+ return stringArgument('NAME');
199
+ }
200
+
166
201
  function pathArgument(): CompilerManifestArgument {
167
202
  return {id: 'PATH', type: STRING, normalizesTo: 'pathSegments'};
168
203
  }
@@ -53,7 +53,8 @@ export interface CoreServices {
53
53
  clientAddress?: (context: unknown) => string;
54
54
  }
55
55
  type App = Hono<any>;
56
- const ir: any = ${canonicalizeDeployIrV2(ir)};
56
+ // Parsed from text: in an object literal a "__proto__" key would set the prototype instead of a property.
57
+ const ir: any = JSON.parse(${JSON.stringify(canonicalizeDeployIrV2(ir))});
57
58
 
58
59
  export function registerCoreRoutes(app: App, services: CoreServices): void {
59
60
  for (const route of ir.routes) {
@@ -67,6 +68,8 @@ async function executeRoute(route: (typeof ir.routes)[number], c: any, services:
67
68
  const headers = new Headers();
68
69
  const bindings = new Map<string, unknown>();
69
70
  const handlerVariables = new Map<string, string>();
71
+ // Structured Data named values: request-local like handler variables, so nothing outlives the request.
72
+ const namedValues = new Map<string, {value: JsonValue; schemaRef?: {id: string; version: string}}>();
70
73
  const iterations = new Map<string, {key: string; index: number; value: unknown}>();
71
74
  let bodyText: string | undefined;
72
75
  try {
@@ -101,7 +104,16 @@ async function executeRoute(route: (typeof ir.routes)[number], c: any, services:
101
104
  } else if (statement.kind === 'delete-handler-variable') {
102
105
  handlerVariables.delete(asText(await expression(statement.name)));
103
106
  } else if (statement.kind === 'clear-handler-variables') handlerVariables.clear();
104
- else if (statement.kind === 'kvs-set-text') {
107
+ else if (statement.kind === 'named-value-set') {
108
+ // The value comes first: the browser decodes parse JSON text before it checks the name.
109
+ const value = await expression(statement.value);
110
+ assertJsonValue(value);
111
+ const name = structuredName(await expression(statement.name));
112
+ const schemaRef = statement.schema === 'keep' ? namedValues.get(name)?.schemaRef : undefined;
113
+ namedValues.set(name, schemaRef === undefined ? {value} : {value, schemaRef});
114
+ } else if (statement.kind === 'named-value-delete') {
115
+ if (!namedValues.delete(structuredName(await expression(statement.name)))) throw runtimeError('DATA_NOT_FOUND', 422);
116
+ } else if (statement.kind === 'kvs-set-text') {
105
117
  await requiredKeyValues().set(asText(await expression(statement.namespace)), asText(await expression(statement.key)), asText(await expression(statement.value)));
106
118
  } else if (statement.kind === 'kvs-delete') {
107
119
  await requiredKeyValues().delete(asText(await expression(statement.namespace)), asText(await expression(statement.key)));
@@ -212,6 +224,17 @@ async function executeRoute(route: (typeof ir.routes)[number], c: any, services:
212
224
  if (value.kind === 'iteration-key') return requiredIteration(value.loopId).key;
213
225
  if (value.kind === 'iteration-index') return requiredIteration(value.loopId).index;
214
226
  if (value.kind === 'iteration-value') return requiredIteration(value.loopId).value;
227
+ if (value.kind === 'named-value') {
228
+ const entry = namedValues.get(structuredName(await expression(value.name)));
229
+ if (entry === undefined) throw runtimeError('DATA_NOT_FOUND', 422);
230
+ return entry.value;
231
+ }
232
+ if (value.kind === 'named-value-exists') return namedValues.has(structuredName(await expression(value.name)));
233
+ if (value.kind === 'structured-decode') {
234
+ if (value.format !== 'json') throw runtimeError('IR_EXPRESSION_UNSUPPORTED');
235
+ return decodeStructuredJson(String(await expression(value.text)));
236
+ }
237
+ if (value.kind === 'json-scalar-text') return jsonScalarText(await expression(value.value));
215
238
  throw runtimeError('IR_EXPRESSION_UNSUPPORTED');
216
239
  }
217
240
  function requiredRecords(): RecordStore {
@@ -310,6 +333,9 @@ function runtimeFault(error: unknown): {code: string; status: number} {
310
333
  }
311
334
  const KNOWN_RUNTIME_STATUS: Readonly<Record<string, number>> = {
312
335
  INVALID_JSON: 422,
336
+ INVALID_NAME: 422,
337
+ DATA_NOT_FOUND: 422,
338
+ PARSE_LIMIT_EXCEEDED: 422,
313
339
  INVALID_PATH: 422,
314
340
  PATH_NOT_FOUND: 422,
315
341
  TYPE_MISMATCH: 422,
@@ -346,6 +372,41 @@ function parseApplicationJson(text: unknown): JsonValue {
346
372
  throw runtimeError('INVALID_JSON', 422);
347
373
  }
348
374
  }
375
+ // Structured Data's codec limits: UTF-8 input bytes, nesting below the depth-0 root, and values (keys excluded).
376
+ const STRUCTURED_DECODE_LIMITS = {maxInputBytes: 262144, maxDepth: 64, maxNodes: 50000};
377
+ function decodeStructuredJson(text: string): JsonValue {
378
+ const limit = STRUCTURED_DECODE_LIMITS.maxInputBytes;
379
+ if (text.length > limit || new TextEncoder().encode(text).byteLength > limit) throw runtimeError('PARSE_LIMIT_EXCEEDED', 422);
380
+ let value: unknown;
381
+ try {value = JSON.parse(text);}
382
+ catch {throw runtimeError('INVALID_JSON', 422);}
383
+ let nodes = 0;
384
+ const visit = (item: unknown, depth: number): void => {
385
+ if (depth > STRUCTURED_DECODE_LIMITS.maxDepth) throw runtimeError('PARSE_LIMIT_EXCEEDED', 422);
386
+ nodes += 1;
387
+ if (nodes > STRUCTURED_DECODE_LIMITS.maxNodes) throw runtimeError('PARSE_LIMIT_EXCEEDED', 422);
388
+ if (item === null || typeof item === 'string' || typeof item === 'boolean') return;
389
+ if (typeof item === 'number') {
390
+ if (Number.isFinite(item)) return;
391
+ throw runtimeError('INVALID_JSON', 422);
392
+ }
393
+ if (Array.isArray(item)) {for (const child of item) visit(child, depth + 1); return;}
394
+ const record = item as Record<string, unknown>;
395
+ for (const key of Object.keys(record)) visit(record[key], depth + 1);
396
+ };
397
+ visit(value, 0);
398
+ return value as JsonValue;
399
+ }
400
+ function structuredName(value: unknown): string {
401
+ const name = String(value).trim();
402
+ if (name.length === 0) throw runtimeError('INVALID_NAME', 422);
403
+ return name;
404
+ }
405
+ function jsonScalarText(value: unknown): string {
406
+ if (typeof value === 'string') return value;
407
+ if (typeof value === 'number' || typeof value === 'boolean') return JSON.stringify(value);
408
+ throw runtimeError('TYPE_MISMATCH', 422);
409
+ }
349
410
  function stringifyApplicationJson(value: unknown): string {
350
411
  assertJsonValue(value);
351
412
  if (Array.isArray(value)) return '[' + value.map(stringifyApplicationJson).join(',') + ']';
@@ -85,6 +85,8 @@ function statementExpressions(statement: StatementIrV2): ExpressionIrV2[] {
85
85
  if (statement.kind === 'asset-object-delete' && statement.target.kind === 'ref') return [statement.target.ref];
86
86
  if (statement.kind === 'if') return [statement.condition];
87
87
  if (statement.kind === 'json-for-each') return [statement.root];
88
+ if (statement.kind === 'named-value-set') return [statement.value, statement.name];
89
+ if (statement.kind === 'named-value-delete') return [statement.name];
88
90
  if (statement.kind === 'respond') return [statement.body];
89
91
  return [];
90
92
  }
@@ -135,5 +137,8 @@ function childExpressions(expression: ExpressionIrV2): ExpressionIrV2[] {
135
137
  return [expression.namespace, expression.key];
136
138
  }
137
139
  if (expression.kind === 'kvs-list-keys') return [expression.namespace];
140
+ if (expression.kind === 'named-value' || expression.kind === 'named-value-exists') return [expression.name];
141
+ if (expression.kind === 'structured-decode') return [expression.text];
142
+ if (expression.kind === 'json-scalar-text') return [expression.value];
138
143
  return [];
139
144
  }