@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.
Files changed (237) hide show
  1. package/README.ja.md +32 -14
  2. package/README.md +104 -43
  3. package/assets/block-icon.svg +1 -0
  4. package/dist/cli.js +131 -17
  5. package/dist/cli.js.map +1 -1
  6. package/dist/compiler/adapters/cloudflare.d.ts +2 -0
  7. package/dist/compiler/adapters/cloudflare.js +477 -0
  8. package/dist/compiler/adapters/cloudflare.js.map +1 -0
  9. package/dist/compiler/adapters/firebase.d.ts +2 -0
  10. package/dist/compiler/adapters/firebase.js +489 -0
  11. package/dist/compiler/adapters/firebase.js.map +1 -0
  12. package/dist/compiler/adapters/index.d.ts +2 -0
  13. package/dist/compiler/adapters/index.js +3 -0
  14. package/dist/compiler/adapters/index.js.map +1 -0
  15. package/dist/compiler/binary/codec.d.ts +4 -0
  16. package/dist/compiler/binary/codec.js +45 -0
  17. package/dist/compiler/binary/codec.js.map +1 -0
  18. package/dist/compiler/binary/error.d.ts +3 -0
  19. package/dist/compiler/binary/error.js +21 -0
  20. package/dist/compiler/binary/error.js.map +1 -0
  21. package/dist/compiler/binary/index.d.ts +4 -0
  22. package/dist/compiler/binary/index.js +5 -0
  23. package/dist/compiler/binary/index.js.map +1 -0
  24. package/dist/compiler/binary/runtime.d.ts +18 -0
  25. package/dist/compiler/binary/runtime.js +123 -0
  26. package/dist/compiler/binary/runtime.js.map +1 -0
  27. package/dist/compiler/binary/types.d.ts +17 -0
  28. package/dist/compiler/binary/types.js +2 -0
  29. package/dist/compiler/binary/types.js.map +1 -0
  30. package/dist/compiler/feature-flags.d.ts +4 -0
  31. package/dist/compiler/feature-flags.js +4 -0
  32. package/dist/compiler/feature-flags.js.map +1 -0
  33. package/dist/compiler/index.d.ts +35 -0
  34. package/dist/compiler/index.js +84 -0
  35. package/dist/compiler/index.js.map +1 -0
  36. package/dist/compiler/ir-v2/canonical.d.ts +3 -0
  37. package/dist/compiler/ir-v2/canonical.js +50 -0
  38. package/dist/compiler/ir-v2/canonical.js.map +1 -0
  39. package/dist/compiler/ir-v2/index.d.ts +4 -0
  40. package/dist/compiler/ir-v2/index.js +5 -0
  41. package/dist/compiler/ir-v2/index.js.map +1 -0
  42. package/dist/compiler/ir-v2/parse.d.ts +10 -0
  43. package/dist/compiler/ir-v2/parse.js +753 -0
  44. package/dist/compiler/ir-v2/parse.js.map +1 -0
  45. package/dist/compiler/ir-v2/strict-json.d.ts +4 -0
  46. package/dist/compiler/ir-v2/strict-json.js +133 -0
  47. package/dist/compiler/ir-v2/strict-json.js.map +1 -0
  48. package/dist/compiler/ir-v2/types.d.ts +412 -0
  49. package/dist/compiler/ir-v2/types.js +84 -0
  50. package/dist/compiler/ir-v2/types.js.map +1 -0
  51. package/dist/compiler/kvs/lowering.d.ts +13 -0
  52. package/dist/compiler/kvs/lowering.js +77 -0
  53. package/dist/compiler/kvs/lowering.js.map +1 -0
  54. package/dist/compiler/manifest/error.d.ts +7 -0
  55. package/dist/compiler/manifest/error.js +9 -0
  56. package/dist/compiler/manifest/error.js.map +1 -0
  57. package/dist/compiler/manifest/index.d.ts +6 -0
  58. package/dist/compiler/manifest/index.js +7 -0
  59. package/dist/compiler/manifest/index.js.map +1 -0
  60. package/dist/compiler/manifest/operations.d.ts +12 -0
  61. package/dist/compiler/manifest/operations.js +60 -0
  62. package/dist/compiler/manifest/operations.js.map +1 -0
  63. package/dist/compiler/manifest/parse.d.ts +9 -0
  64. package/dist/compiler/manifest/parse.js +280 -0
  65. package/dist/compiler/manifest/parse.js.map +1 -0
  66. package/dist/compiler/manifest/registry.d.ts +3 -0
  67. package/dist/compiler/manifest/registry.js +48 -0
  68. package/dist/compiler/manifest/registry.js.map +1 -0
  69. package/dist/compiler/manifest/resolve.d.ts +14 -0
  70. package/dist/compiler/manifest/resolve.js +116 -0
  71. package/dist/compiler/manifest/resolve.js.map +1 -0
  72. package/dist/compiler/manifest/types.d.ts +95 -0
  73. package/dist/compiler/manifest/types.js +3 -0
  74. package/dist/compiler/manifest/types.js.map +1 -0
  75. package/dist/compiler/named-body/index.d.ts +1 -0
  76. package/dist/compiler/named-body/index.js +2 -0
  77. package/dist/compiler/named-body/index.js.map +1 -0
  78. package/dist/compiler/named-body/lowering.d.ts +18 -0
  79. package/dist/compiler/named-body/lowering.js +83 -0
  80. package/dist/compiler/named-body/lowering.js.map +1 -0
  81. package/dist/compiler/pipeline/compiler.d.ts +18 -0
  82. package/dist/compiler/pipeline/compiler.js +105 -0
  83. package/dist/compiler/pipeline/compiler.js.map +1 -0
  84. package/dist/compiler/pipeline/core-generator.d.ts +3 -0
  85. package/dist/compiler/pipeline/core-generator.js +483 -0
  86. package/dist/compiler/pipeline/core-generator.js.map +1 -0
  87. package/dist/compiler/pipeline/index.d.ts +5 -0
  88. package/dist/compiler/pipeline/index.js +6 -0
  89. package/dist/compiler/pipeline/index.js.map +1 -0
  90. package/dist/compiler/pipeline/registry.d.ts +7 -0
  91. package/dist/compiler/pipeline/registry.js +17 -0
  92. package/dist/compiler/pipeline/registry.js.map +1 -0
  93. package/dist/compiler/pipeline/requirements.d.ts +9 -0
  94. package/dist/compiler/pipeline/requirements.js +135 -0
  95. package/dist/compiler/pipeline/requirements.js.map +1 -0
  96. package/dist/compiler/pipeline/types.d.ts +65 -0
  97. package/dist/compiler/pipeline/types.js +2 -0
  98. package/dist/compiler/pipeline/types.js.map +1 -0
  99. package/dist/compiler/runtime/error.d.ts +6 -0
  100. package/dist/compiler/runtime/error.js +9 -0
  101. package/dist/compiler/runtime/error.js.map +1 -0
  102. package/dist/compiler/runtime/index.d.ts +1 -0
  103. package/dist/compiler/runtime/index.js +2 -0
  104. package/dist/compiler/runtime/index.js.map +1 -0
  105. package/dist/compiler/structured-data/index.d.ts +3 -0
  106. package/dist/compiler/structured-data/index.js +4 -0
  107. package/dist/compiler/structured-data/index.js.map +1 -0
  108. package/dist/compiler/structured-data/lowering.d.ts +30 -0
  109. package/dist/compiler/structured-data/lowering.js +202 -0
  110. package/dist/compiler/structured-data/lowering.js.map +1 -0
  111. package/dist/compiler/structured-data/path.d.ts +2 -0
  112. package/dist/compiler/structured-data/path.js +60 -0
  113. package/dist/compiler/structured-data/path.js.map +1 -0
  114. package/dist/compiler/structured-data/runtime.d.ts +20 -0
  115. package/dist/compiler/structured-data/runtime.js +210 -0
  116. package/dist/compiler/structured-data/runtime.js.map +1 -0
  117. package/dist/compiler/turbowarp-v2.d.ts +8 -0
  118. package/dist/compiler/turbowarp-v2.js +479 -0
  119. package/dist/compiler/turbowarp-v2.js.map +1 -0
  120. package/dist/compiler/validator/index.d.ts +4 -0
  121. package/dist/compiler/validator/index.js +5 -0
  122. package/dist/compiler/validator/index.js.map +1 -0
  123. package/dist/compiler/validator/ir-validator.d.ts +10 -0
  124. package/dist/compiler/validator/ir-validator.js +463 -0
  125. package/dist/compiler/validator/ir-validator.js.map +1 -0
  126. package/dist/compiler/validator/resource-lifetime.d.ts +18 -0
  127. package/dist/compiler/validator/resource-lifetime.js +106 -0
  128. package/dist/compiler/validator/resource-lifetime.js.map +1 -0
  129. package/dist/compiler/validator/turbowarp-subset.d.ts +3 -0
  130. package/dist/compiler/validator/turbowarp-subset.js +241 -0
  131. package/dist/compiler/validator/turbowarp-subset.js.map +1 -0
  132. package/dist/compiler/validator/types.d.ts +30 -0
  133. package/dist/compiler/validator/types.js +7 -0
  134. package/dist/compiler/validator/types.js.map +1 -0
  135. package/dist/config.d.ts +11 -0
  136. package/dist/config.js +12 -0
  137. package/dist/config.js.map +1 -0
  138. package/dist/digest-cli.js +0 -0
  139. package/dist/extension-manifest.json +102 -0
  140. package/dist/named-body.d.ts +57 -0
  141. package/dist/named-body.js +396 -0
  142. package/dist/named-body.js.map +1 -0
  143. package/dist/named-data-namespace.d.ts +3 -0
  144. package/dist/named-data-namespace.js +6 -0
  145. package/dist/named-data-namespace.js.map +1 -0
  146. package/dist/protocol.d.ts +13 -1
  147. package/dist/protocol.js +34 -0
  148. package/dist/protocol.js.map +1 -1
  149. package/dist/resource-named-body-provider.d.ts +8 -0
  150. package/dist/resource-named-body-provider.js +88 -0
  151. package/dist/resource-named-body-provider.js.map +1 -0
  152. package/dist/server.d.ts +10 -1
  153. package/dist/server.js +37 -7
  154. package/dist/server.js.map +1 -1
  155. package/dist/turbowarp-http-server.js +1614 -1099
  156. package/docs/binary-resource-semantics.ja.md +84 -0
  157. package/docs/cloudflare-worker-compiler.ja.md +90 -0
  158. package/docs/compiler-manifest-registry.ja.md +79 -0
  159. package/docs/compiler-platform-pipeline.ja.md +94 -0
  160. package/docs/deploy-ir-v2.ja.md +123 -0
  161. package/docs/fixed-input-editors.ja.md +23 -0
  162. package/docs/index.html +86 -0
  163. package/docs/ir-v2-conformance.ja.md +65 -0
  164. package/docs/named-response-body.ja.md +120 -0
  165. package/docs/platform-storage-adapters.ja.md +99 -0
  166. package/docs/server-executable-subset.ja.md +88 -0
  167. package/docs/structured-data-lowering.ja.md +63 -0
  168. package/docs/turbowarp-http-server.architecture.html +4 -0
  169. package/examples/compiler/message-app.v2.ir.json +41 -0
  170. package/package.json +45 -20
  171. package/schemas/block-editor-fixed-inputs.schema.json +85 -0
  172. package/schemas/compiler-extension-manifest.schema.json +126 -0
  173. package/schemas/compiler-manifest-lock-v1.schema.json +48 -0
  174. package/schemas/deploy-ir-v2.schema.json +835 -0
  175. package/schemas/ir-v2-conformance-fixture.schema.json +44 -0
  176. package/src/block-definitions.json +76 -0
  177. package/src/cli.ts +136 -23
  178. package/src/compiler/adapters/cloudflare.ts +510 -0
  179. package/src/compiler/adapters/firebase.ts +538 -0
  180. package/src/compiler/adapters/index.ts +2 -0
  181. package/src/compiler/binary/codec.ts +61 -0
  182. package/src/compiler/binary/error.ts +25 -0
  183. package/src/compiler/binary/index.ts +4 -0
  184. package/src/compiler/binary/runtime.ts +133 -0
  185. package/src/compiler/binary/types.ts +26 -0
  186. package/src/compiler/feature-flags.ts +7 -0
  187. package/src/compiler/index.ts +114 -0
  188. package/src/compiler/ir-v2/canonical.ts +46 -0
  189. package/src/compiler/ir-v2/index.ts +9 -0
  190. package/src/compiler/ir-v2/parse.ts +921 -0
  191. package/src/compiler/ir-v2/strict-json.ts +129 -0
  192. package/src/compiler/ir-v2/types.ts +462 -0
  193. package/src/compiler/kvs/lowering.ts +88 -0
  194. package/src/compiler/manifest/error.ts +29 -0
  195. package/src/compiler/manifest/index.ts +17 -0
  196. package/src/compiler/manifest/operations.ts +175 -0
  197. package/src/compiler/manifest/parse.ts +390 -0
  198. package/src/compiler/manifest/registry.ts +74 -0
  199. package/src/compiler/manifest/resolve.ts +159 -0
  200. package/src/compiler/manifest/types.ts +112 -0
  201. package/src/compiler/named-body/index.ts +1 -0
  202. package/src/compiler/named-body/lowering.ts +186 -0
  203. package/src/compiler/pipeline/compiler.ts +138 -0
  204. package/src/compiler/pipeline/core-generator.ts +486 -0
  205. package/src/compiler/pipeline/index.ts +5 -0
  206. package/src/compiler/pipeline/registry.ts +18 -0
  207. package/src/compiler/pipeline/requirements.ts +139 -0
  208. package/src/compiler/pipeline/types.ts +59 -0
  209. package/src/compiler/runtime/error.ts +11 -0
  210. package/src/compiler/runtime/index.ts +1 -0
  211. package/src/compiler/structured-data/index.ts +3 -0
  212. package/src/compiler/structured-data/lowering.ts +314 -0
  213. package/src/compiler/structured-data/path.ts +53 -0
  214. package/src/compiler/structured-data/runtime.ts +221 -0
  215. package/src/compiler/turbowarp-v2.ts +610 -0
  216. package/src/compiler/validator/index.ts +4 -0
  217. package/src/compiler/validator/ir-validator.ts +812 -0
  218. package/src/compiler/validator/resource-lifetime.ts +153 -0
  219. package/src/compiler/validator/turbowarp-subset.ts +387 -0
  220. package/src/compiler/validator/types.ts +68 -0
  221. package/src/config.ts +1 -1
  222. package/src/extension.ts +87 -1
  223. package/src/fixed-input-editors.ts +181 -0
  224. package/src/fixed-input-schema.ts +15 -0
  225. package/src/named-body.ts +495 -0
  226. package/src/named-data-namespace.ts +6 -0
  227. package/src/protocol.ts +50 -1
  228. package/src/resource-named-body-provider.ts +106 -0
  229. package/src/server.ts +88 -8
  230. package/docs/turbowarp-http-server.architecture.visual-check.1440x900.dark.png +0 -0
  231. package/docs/turbowarp-http-server.architecture.visual-check.1440x900.light.png +0 -0
  232. package/docs/turbowarp-http-server.architecture.visual-check.2048x1320.dark.png +0 -0
  233. package/docs/turbowarp-http-server.architecture.visual-check.2048x1320.light.png +0 -0
  234. package/docs/turbowarp-http-server.architecture.visual-check.html +0 -32
  235. package/docs/turbowarp-http-server.architecture.visual-check.json +0 -547
  236. package/schemas/extension-manifest.schema.json +0 -49
  237. package/src/extension-manifest.ts +0 -149
@@ -0,0 +1,129 @@
1
+ export interface StrictJsonParseOptions {
2
+ maxDepth?: number;
3
+ }
4
+
5
+ export function parseJsonWithoutDuplicateKeys(text: string, options: StrictJsonParseOptions = {}): unknown {
6
+ const scanner = new JsonStructureScanner(text, options.maxDepth ?? Number.POSITIVE_INFINITY);
7
+ scanner.scan();
8
+ return JSON.parse(text) as unknown;
9
+ }
10
+
11
+ class JsonStructureScanner {
12
+ private index = 0;
13
+
14
+ public constructor(
15
+ private readonly text: string,
16
+ private readonly maxDepth: number
17
+ ) {}
18
+
19
+ public scan(): void {
20
+ this.skipWhitespace();
21
+ this.value(0);
22
+ this.skipWhitespace();
23
+ if (this.index !== this.text.length) this.fail('Unexpected trailing content');
24
+ }
25
+
26
+ private value(depth: number): void {
27
+ if (depth > this.maxDepth) this.fail(`JSON nesting depth exceeds ${this.maxDepth}`);
28
+ this.skipWhitespace();
29
+ const token = this.text[this.index];
30
+ if (token === '{') return this.object(depth);
31
+ if (token === '[') return this.array(depth);
32
+ if (token === '"') {
33
+ this.string();
34
+ return;
35
+ }
36
+ if (this.text.startsWith('true', this.index)) return this.advance(4);
37
+ if (this.text.startsWith('false', this.index)) return this.advance(5);
38
+ if (this.text.startsWith('null', this.index)) return this.advance(4);
39
+ this.number();
40
+ }
41
+
42
+ private object(depth: number): void {
43
+ this.index += 1;
44
+ this.skipWhitespace();
45
+ const keys = new Set<string>();
46
+ if (this.consume('}')) return;
47
+ while (true) {
48
+ this.skipWhitespace();
49
+ if (this.text[this.index] !== '"') this.fail('Expected an object key');
50
+ const key = this.string();
51
+ if (keys.has(key)) this.fail(`Duplicate object key: ${key}`);
52
+ keys.add(key);
53
+ this.skipWhitespace();
54
+ this.expect(':');
55
+ this.value(depth + 1);
56
+ this.skipWhitespace();
57
+ if (this.consume('}')) return;
58
+ this.expect(',');
59
+ }
60
+ }
61
+
62
+ private array(depth: number): void {
63
+ this.index += 1;
64
+ this.skipWhitespace();
65
+ if (this.consume(']')) return;
66
+ while (true) {
67
+ this.value(depth + 1);
68
+ this.skipWhitespace();
69
+ if (this.consume(']')) return;
70
+ this.expect(',');
71
+ }
72
+ }
73
+
74
+ private string(): string {
75
+ const start = this.index;
76
+ this.index += 1;
77
+ while (this.index < this.text.length) {
78
+ const token = this.text[this.index];
79
+ if (token === '"') {
80
+ this.index += 1;
81
+ return JSON.parse(this.text.slice(start, this.index)) as string;
82
+ }
83
+ if (token === '\\') {
84
+ this.index += 1;
85
+ const escape = this.text[this.index];
86
+ if (escape === 'u') {
87
+ const digits = this.text.slice(this.index + 1, this.index + 5);
88
+ if (!/^[0-9a-fA-F]{4}$/.test(digits)) this.fail('Invalid Unicode escape');
89
+ this.index += 5;
90
+ continue;
91
+ }
92
+ if (escape === undefined || !'"\\/bfnrt'.includes(escape)) this.fail('Invalid string escape');
93
+ this.index += 1;
94
+ continue;
95
+ }
96
+ if (token === undefined || token.charCodeAt(0) < 0x20) this.fail('Invalid string character');
97
+ this.index += 1;
98
+ }
99
+ this.fail('Unterminated string');
100
+ }
101
+
102
+ private number(): void {
103
+ const match = /^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?/.exec(this.text.slice(this.index));
104
+ if (!match) this.fail('Expected a JSON value');
105
+ this.index += match[0].length;
106
+ }
107
+
108
+ private skipWhitespace(): void {
109
+ while (/\s/.test(this.text[this.index] ?? '') && this.index < this.text.length) this.index += 1;
110
+ }
111
+
112
+ private consume(token: string): boolean {
113
+ if (this.text[this.index] !== token) return false;
114
+ this.index += 1;
115
+ return true;
116
+ }
117
+
118
+ private expect(token: string): void {
119
+ if (!this.consume(token)) this.fail(`Expected ${token}`);
120
+ }
121
+
122
+ private advance(length: number): void {
123
+ this.index += length;
124
+ }
125
+
126
+ private fail(message: string): never {
127
+ throw new SyntaxError(`${message} at JSON offset ${this.index}.`);
128
+ }
129
+ }
@@ -0,0 +1,462 @@
1
+ export const DEPLOY_IR_V2_VERSION = 2 as const;
2
+
3
+ export const IR_V2_EXPRESSION_KINDS = [
4
+ 'literal',
5
+ 'request',
6
+ 'request-value',
7
+ 'concat',
8
+ 'handler-variable',
9
+ 'handler-variable-exists',
10
+ 'handler-variable-names',
11
+ 'kvs-get-text',
12
+ 'kvs-has',
13
+ 'kvs-list-keys',
14
+ 'binding',
15
+ 'json-text-coerce',
16
+ 'json-parse',
17
+ 'json-stringify',
18
+ 'json-is-valid',
19
+ 'json-get',
20
+ 'json-has',
21
+ 'json-set',
22
+ 'json-delete',
23
+ 'json-keys',
24
+ 'json-length',
25
+ 'iteration-key',
26
+ 'iteration-index',
27
+ 'iteration-value'
28
+ ] as const;
29
+
30
+ export const IR_V2_STATEMENT_KINDS = [
31
+ 'set-status',
32
+ 'set-header',
33
+ 'remove-header',
34
+ 'set-handler-variable',
35
+ 'change-handler-variable',
36
+ 'delete-handler-variable',
37
+ 'clear-handler-variables',
38
+ 'kvs-set-text',
39
+ 'kvs-delete',
40
+ 'record-create',
41
+ 'record-list',
42
+ 'record-get',
43
+ 'record-delete',
44
+ 'asset-resolve',
45
+ 'request-body-binary',
46
+ 'asset-object-get',
47
+ 'asset-object-put',
48
+ 'asset-object-delete',
49
+ 'if',
50
+ 'bounded-loop',
51
+ 'json-for-each',
52
+ 'respond-binary',
53
+ 'respond-named-body',
54
+ 'respond'
55
+ ] as const;
56
+
57
+ export type EffectKindV2 =
58
+ | 'response-write'
59
+ | 'handler-state-write'
60
+ | 'record-read'
61
+ | 'record-write'
62
+ | 'key-value-read'
63
+ | 'key-value-write'
64
+ | 'object-read'
65
+ | 'object-write'
66
+ | 'binary-consume'
67
+ | 'control';
68
+
69
+ export const IR_V2_STATEMENT_EFFECTS = {
70
+ 'set-status': ['response-write'],
71
+ 'set-header': ['response-write'],
72
+ 'remove-header': ['response-write'],
73
+ 'set-handler-variable': ['handler-state-write'],
74
+ 'change-handler-variable': ['handler-state-write'],
75
+ 'delete-handler-variable': ['handler-state-write'],
76
+ 'clear-handler-variables': ['handler-state-write'],
77
+ 'kvs-set-text': ['key-value-write'],
78
+ 'kvs-delete': ['key-value-write'],
79
+ 'record-create': ['record-write'],
80
+ 'record-list': ['record-read'],
81
+ 'record-get': ['record-read'],
82
+ 'record-delete': ['record-write'],
83
+ 'asset-resolve': ['object-read'],
84
+ 'request-body-binary': ['control'],
85
+ 'asset-object-get': ['object-read'],
86
+ 'asset-object-put': ['object-write', 'binary-consume'],
87
+ 'asset-object-delete': ['object-write'],
88
+ if: ['control'],
89
+ 'bounded-loop': ['control'],
90
+ 'json-for-each': ['control'],
91
+ 'respond-binary': ['response-write', 'binary-consume'],
92
+ 'respond-named-body': ['response-write'],
93
+ respond: ['response-write']
94
+ } as const satisfies Record<(typeof IR_V2_STATEMENT_KINDS)[number], readonly EffectKindV2[]>;
95
+
96
+ export type JsonPrimitive = null | boolean | number | string;
97
+ export type JsonValue = JsonPrimitive | JsonValue[] | {[key: string]: JsonValue};
98
+
99
+ export interface BinaryRefDescriptorV2 {
100
+ namespace: string;
101
+ key: string;
102
+ contentType?: string;
103
+ size?: number;
104
+ integrity?: `sha256:${string}`;
105
+ revision?: string;
106
+ }
107
+
108
+ export interface BinaryLocatorV2 {
109
+ namespace: string;
110
+ key: string;
111
+ }
112
+
113
+ export interface BinaryMetadataV2 {
114
+ contentType?: string;
115
+ size?: number;
116
+ integrity?: `sha256:${string}`;
117
+ revision?: string;
118
+ }
119
+
120
+ export type BinaryDeleteTargetV2 =
121
+ | {kind: 'ref'; ref: ExpressionIrV2}
122
+ | {kind: 'locator'; locator: BinaryLocatorV2};
123
+
124
+ export type BinaryContentDispositionV2 =
125
+ | {kind: 'inline'}
126
+ | {kind: 'attachment'; filename: string};
127
+
128
+ export type NamedDataKindV2 = 'structured' | 'document' | 'binary' | 'asset';
129
+ export type NamedDataScopeV2 = 'target' | 'project';
130
+ export type NamedBodyRepresentationV2 = 'json' | 'yaml' | 'html' | 'markdown' | 'raw';
131
+
132
+ export interface NamedDataReferenceV2 {
133
+ namespace: string;
134
+ name: string;
135
+ kind: NamedDataKindV2;
136
+ scope: NamedDataScopeV2;
137
+ }
138
+
139
+ export type AtomicValueTypeV2 =
140
+ | 'null'
141
+ | 'boolean'
142
+ | 'number'
143
+ | 'string'
144
+ | 'json-text'
145
+ | 'json-array'
146
+ | 'json-object'
147
+ | 'binary-ref';
148
+
149
+ export type ValueTypeV2 =
150
+ | AtomicValueTypeV2
151
+ | {kind: 'union'; members: AtomicValueTypeV2[]};
152
+
153
+ export type JsonValueTypeV2 = {
154
+ kind: 'union';
155
+ members: ['null', 'boolean', 'number', 'string', 'json-array', 'json-object'];
156
+ };
157
+
158
+ export const JSON_VALUE_TYPE_V2: JsonValueTypeV2 = {
159
+ kind: 'union',
160
+ members: ['null', 'boolean', 'number', 'string', 'json-array', 'json-object']
161
+ };
162
+
163
+ export type ResourceTypeV2 = 'binary-body';
164
+
165
+ export type BindingTypeV2 =
166
+ | {kind: 'value'; valueType: ValueTypeV2}
167
+ | {kind: 'resource'; resourceType: ResourceTypeV2};
168
+
169
+ export interface BindingDeclarationV2 {
170
+ id: string;
171
+ type: BindingTypeV2;
172
+ }
173
+
174
+ export interface SourceRefV2 {
175
+ targetIndex: number;
176
+ targetName: string;
177
+ blockId: string;
178
+ opcode: string;
179
+ input?: string;
180
+ }
181
+
182
+ export type PathSegmentV2 =
183
+ | {kind: 'key'; value: string}
184
+ | {kind: 'index'; value: number};
185
+
186
+ export type ExpressionIrV2 =
187
+ | {
188
+ kind: 'literal';
189
+ valueType: Exclude<AtomicValueTypeV2, 'binary-ref'>;
190
+ value: JsonValue;
191
+ sourceRef?: SourceRefV2;
192
+ }
193
+ | {kind: 'literal'; valueType: 'binary-ref'; value: BinaryRefDescriptorV2; sourceRef?: SourceRefV2}
194
+ | {
195
+ kind: 'request';
196
+ valueType: 'string';
197
+ source: 'method' | 'path' | 'url' | 'body-text' | 'content-type' | 'client-address';
198
+ sourceRef?: SourceRefV2;
199
+ }
200
+ | {
201
+ kind: 'request-value';
202
+ valueType: 'string';
203
+ source: 'query' | 'path-param' | 'header';
204
+ name: string;
205
+ sourceRef?: SourceRefV2;
206
+ }
207
+ | {
208
+ kind: 'concat';
209
+ valueType: 'string';
210
+ left: ExpressionIrV2;
211
+ right: ExpressionIrV2;
212
+ sourceRef?: SourceRefV2;
213
+ }
214
+ | {
215
+ kind: 'handler-variable';
216
+ valueType: {kind: 'union'; members: ['number', 'string']};
217
+ name: ExpressionIrV2;
218
+ sourceRef?: SourceRefV2;
219
+ }
220
+ | {
221
+ kind: 'handler-variable-exists';
222
+ valueType: 'boolean';
223
+ name: ExpressionIrV2;
224
+ sourceRef?: SourceRefV2;
225
+ }
226
+ | {kind: 'handler-variable-names'; valueType: 'string'; sourceRef?: SourceRefV2}
227
+ | {
228
+ kind: 'kvs-get-text';
229
+ valueType: 'string';
230
+ namespace: ExpressionIrV2;
231
+ key: ExpressionIrV2;
232
+ sourceRef?: SourceRefV2;
233
+ }
234
+ | {
235
+ kind: 'kvs-has';
236
+ valueType: 'boolean';
237
+ namespace: ExpressionIrV2;
238
+ key: ExpressionIrV2;
239
+ sourceRef?: SourceRefV2;
240
+ }
241
+ | {
242
+ kind: 'kvs-list-keys';
243
+ valueType: 'json-text';
244
+ namespace: ExpressionIrV2;
245
+ sourceRef?: SourceRefV2;
246
+ }
247
+ | {
248
+ kind: 'binding';
249
+ valueType: ValueTypeV2;
250
+ binding: string;
251
+ sourceRef?: SourceRefV2;
252
+ }
253
+ | {kind: 'json-text-coerce'; valueType: 'json-text'; input: ExpressionIrV2; sourceRef?: SourceRefV2}
254
+ | {kind: 'json-parse'; valueType: JsonValueTypeV2; text: ExpressionIrV2; sourceRef?: SourceRefV2}
255
+ | {kind: 'json-stringify'; valueType: 'json-text'; value: ExpressionIrV2; sourceRef?: SourceRefV2}
256
+ | {kind: 'json-is-valid'; valueType: 'boolean'; text: ExpressionIrV2; sourceRef?: SourceRefV2}
257
+ | {
258
+ kind: 'json-get';
259
+ valueType: JsonValueTypeV2;
260
+ root: ExpressionIrV2;
261
+ path: PathSegmentV2[];
262
+ sourceRef?: SourceRefV2;
263
+ }
264
+ | {
265
+ kind: 'json-has';
266
+ valueType: 'boolean';
267
+ root: ExpressionIrV2;
268
+ path: PathSegmentV2[];
269
+ sourceRef?: SourceRefV2;
270
+ }
271
+ | {
272
+ kind: 'json-set';
273
+ valueType: JsonValueTypeV2;
274
+ root: ExpressionIrV2;
275
+ path: PathSegmentV2[];
276
+ value: ExpressionIrV2;
277
+ sourceRef?: SourceRefV2;
278
+ }
279
+ | {
280
+ kind: 'json-delete';
281
+ valueType: JsonValueTypeV2;
282
+ root: ExpressionIrV2;
283
+ path: PathSegmentV2[];
284
+ sourceRef?: SourceRefV2;
285
+ }
286
+ | {
287
+ kind: 'json-keys';
288
+ valueType: 'json-array';
289
+ root: ExpressionIrV2;
290
+ path: PathSegmentV2[];
291
+ sourceRef?: SourceRefV2;
292
+ }
293
+ | {
294
+ kind: 'json-length';
295
+ valueType: 'number';
296
+ root: ExpressionIrV2;
297
+ path: PathSegmentV2[];
298
+ sourceRef?: SourceRefV2;
299
+ }
300
+ | {kind: 'iteration-key'; valueType: 'string'; loopId: string; sourceRef?: SourceRefV2}
301
+ | {kind: 'iteration-index'; valueType: 'number'; loopId: string; sourceRef?: SourceRefV2}
302
+ | {kind: 'iteration-value'; valueType: JsonValueTypeV2; loopId: string; sourceRef?: SourceRefV2};
303
+
304
+ export type StatementIrV2 =
305
+ | {kind: 'set-status'; status: number; sourceRef?: SourceRefV2}
306
+ | {kind: 'set-header'; name: string; value: ExpressionIrV2; sourceRef?: SourceRefV2}
307
+ | {kind: 'remove-header'; name: string; sourceRef?: SourceRefV2}
308
+ | {
309
+ kind: 'set-handler-variable' | 'change-handler-variable';
310
+ name: ExpressionIrV2;
311
+ value: ExpressionIrV2;
312
+ sourceRef?: SourceRefV2;
313
+ }
314
+ | {kind: 'delete-handler-variable'; name: ExpressionIrV2; sourceRef?: SourceRefV2}
315
+ | {kind: 'clear-handler-variables'; sourceRef?: SourceRefV2}
316
+ | {
317
+ kind: 'kvs-set-text';
318
+ namespace: ExpressionIrV2;
319
+ key: ExpressionIrV2;
320
+ value: ExpressionIrV2;
321
+ sourceRef?: SourceRefV2;
322
+ }
323
+ | {
324
+ kind: 'kvs-delete';
325
+ namespace: ExpressionIrV2;
326
+ key: ExpressionIrV2;
327
+ sourceRef?: SourceRefV2;
328
+ }
329
+ | {
330
+ kind: 'record-create';
331
+ collection: string;
332
+ data: ExpressionIrV2;
333
+ result: BindingDeclarationV2;
334
+ sourceRef?: SourceRefV2;
335
+ }
336
+ | {
337
+ kind: 'record-list';
338
+ collection: string;
339
+ result: BindingDeclarationV2;
340
+ sourceRef?: SourceRefV2;
341
+ }
342
+ | {
343
+ kind: 'record-get' | 'record-delete';
344
+ id: ExpressionIrV2;
345
+ result: BindingDeclarationV2;
346
+ sourceRef?: SourceRefV2;
347
+ }
348
+ | {
349
+ kind: 'asset-resolve';
350
+ locator: BinaryLocatorV2;
351
+ result: BindingDeclarationV2;
352
+ sourceRef?: SourceRefV2;
353
+ }
354
+ | {
355
+ kind: 'request-body-binary';
356
+ maxBytes: number;
357
+ result: BindingDeclarationV2;
358
+ sourceRef?: SourceRefV2;
359
+ }
360
+ | {
361
+ kind: 'asset-object-get';
362
+ ref: ExpressionIrV2;
363
+ maxBytes: number;
364
+ result: BindingDeclarationV2;
365
+ sourceRef?: SourceRefV2;
366
+ }
367
+ | {
368
+ kind: 'asset-object-put';
369
+ locator: BinaryLocatorV2;
370
+ body: string;
371
+ metadata: BinaryMetadataV2;
372
+ maxBytes: number;
373
+ result: BindingDeclarationV2;
374
+ sourceRef?: SourceRefV2;
375
+ }
376
+ | {
377
+ kind: 'asset-object-delete';
378
+ target: BinaryDeleteTargetV2;
379
+ result: BindingDeclarationV2;
380
+ sourceRef?: SourceRefV2;
381
+ }
382
+ | {
383
+ kind: 'if';
384
+ condition: ExpressionIrV2;
385
+ then: StatementIrV2[];
386
+ else?: StatementIrV2[];
387
+ sourceRef?: SourceRefV2;
388
+ }
389
+ | {
390
+ kind: 'bounded-loop';
391
+ maxIterations: number;
392
+ body: StatementIrV2[];
393
+ sourceRef?: SourceRefV2;
394
+ }
395
+ | {
396
+ kind: 'json-for-each';
397
+ loopId: string;
398
+ root: ExpressionIrV2;
399
+ path: PathSegmentV2[];
400
+ maxIterations: number;
401
+ body: StatementIrV2[];
402
+ sourceRef?: SourceRefV2;
403
+ }
404
+ | {
405
+ kind: 'respond-binary';
406
+ body: string;
407
+ disposition?: BinaryContentDispositionV2;
408
+ sourceRef?: SourceRefV2;
409
+ }
410
+ | {
411
+ kind: 'respond-named-body';
412
+ reference: NamedDataReferenceV2;
413
+ representation: NamedBodyRepresentationV2;
414
+ targetId?: string;
415
+ maxBytes: number;
416
+ sourceRef?: SourceRefV2;
417
+ }
418
+ | {
419
+ kind: 'respond';
420
+ format: 'text' | 'html' | 'json';
421
+ body: ExpressionIrV2;
422
+ sourceRef?: SourceRefV2;
423
+ };
424
+
425
+ export type CapabilityRequirementV2 =
426
+ | {kind: 'record-store'}
427
+ | {kind: 'key-value-store'}
428
+ | {kind: 'object-storage'}
429
+ | {kind: 'streaming-body'}
430
+ | {kind: 'named-body-provider'}
431
+ | {kind: 'request-metadata'; field: 'client-address'}
432
+ | {kind: 'auth'; scheme: 'external-jwt' | 'trusted-access-jwt'};
433
+
434
+ export type AuthPolicyV2 =
435
+ | {kind: 'none'}
436
+ | {kind: 'jwt'; scheme: 'external-jwt' | 'trusted-access-jwt'};
437
+
438
+ export type HttpMethodV2 = 'ALL' | 'GET' | 'HEAD' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS';
439
+
440
+ export interface RouteIrV2 {
441
+ id: string;
442
+ method: HttpMethodV2;
443
+ path: string;
444
+ auth: 'public' | 'required';
445
+ body: StatementIrV2[];
446
+ sourceRef?: SourceRefV2;
447
+ }
448
+
449
+ export interface DeployIrV2 {
450
+ version: typeof DEPLOY_IR_V2_VERSION;
451
+ name: string;
452
+ auth: AuthPolicyV2;
453
+ capabilities: CapabilityRequirementV2[];
454
+ routes: RouteIrV2[];
455
+ }
456
+
457
+ export interface CompilerDiagnosticV2 {
458
+ severity: 'warning' | 'error';
459
+ code: `TW2_${string}`;
460
+ message: string;
461
+ sourceRef?: SourceRefV2;
462
+ }
@@ -0,0 +1,88 @@
1
+ import type {CompilerDiagnosticV2, ExpressionIrV2, SourceRefV2, StatementIrV2} from '../ir-v2/types.js';
2
+ import type {CompilerOpcodeRegistryEntry} from '../manifest/types.js';
3
+
4
+ const EXTENSION_ID = 'kubohiroyakvs';
5
+ const PACKAGE_NAME = '@kubohiroya/turbowarp-kvs';
6
+ const PACKAGE_VERSION = '0.1.0';
7
+
8
+ export type KvsArguments = Readonly<Record<string, ExpressionIrV2>>;
9
+
10
+ export class KvsLoweringContext {
11
+ public constructor(public readonly diagnostics: CompilerDiagnosticV2[] = []) {}
12
+
13
+ public lowerStatement(
14
+ entry: CompilerOpcodeRegistryEntry,
15
+ args: KvsArguments,
16
+ sourceRef: SourceRefV2
17
+ ): StatementIrV2 | undefined {
18
+ const operation = this.operation(entry, sourceRef);
19
+ if (operation === undefined) return undefined;
20
+ const namespace = args.NAMESPACE;
21
+ const key = args.KEY;
22
+ if (namespace === undefined || key === undefined) return this.missingArgument(sourceRef);
23
+ if (operation === 'kvs.delete') return {kind: 'kvs-delete', namespace, key, sourceRef};
24
+ if (operation === 'kvs.setText') {
25
+ const value = args.VALUE;
26
+ return value === undefined
27
+ ? this.missingArgument(sourceRef)
28
+ : {kind: 'kvs-set-text', namespace, key, value, sourceRef};
29
+ }
30
+ return this.unsupported(operation, sourceRef);
31
+ }
32
+
33
+ public lowerReporter(
34
+ entry: CompilerOpcodeRegistryEntry,
35
+ args: KvsArguments,
36
+ sourceRef: SourceRefV2
37
+ ): ExpressionIrV2 | undefined {
38
+ const operation = this.operation(entry, sourceRef);
39
+ if (operation === undefined) return undefined;
40
+ const namespace = args.NAMESPACE;
41
+ if (namespace === undefined) return this.missingArgument(sourceRef);
42
+ if (operation === 'kvs.listKeys') {
43
+ return {kind: 'kvs-list-keys', valueType: 'json-text', namespace, sourceRef};
44
+ }
45
+ const key = args.KEY;
46
+ if (key === undefined) return this.missingArgument(sourceRef);
47
+ if (operation === 'kvs.getText') return {kind: 'kvs-get-text', valueType: 'string', namespace, key, sourceRef};
48
+ if (operation === 'kvs.has') return {kind: 'kvs-has', valueType: 'boolean', namespace, key, sourceRef};
49
+ return this.unsupported(operation, sourceRef);
50
+ }
51
+
52
+ private operation(entry: CompilerOpcodeRegistryEntry, sourceRef: SourceRefV2): string | undefined {
53
+ if (
54
+ entry.extensionId !== EXTENSION_ID ||
55
+ entry.packageName !== PACKAGE_NAME ||
56
+ entry.packageVersion !== PACKAGE_VERSION ||
57
+ entry.block.server?.supported !== true ||
58
+ entry.block.server.irOperation === undefined
59
+ ) {
60
+ return this.unsupported(entry.projectOpcode, sourceRef);
61
+ }
62
+ return entry.block.server.irOperation;
63
+ }
64
+
65
+ private missingArgument(sourceRef: SourceRefV2): undefined {
66
+ this.diagnostics.push({
67
+ severity: 'error',
68
+ code: 'TW2_KVS_SIGNATURE_MISMATCH',
69
+ message: 'The KVS block does not match its locked manifest signature.',
70
+ sourceRef
71
+ });
72
+ return undefined;
73
+ }
74
+
75
+ private unsupported(operation: string, sourceRef: SourceRefV2): undefined {
76
+ this.diagnostics.push({
77
+ severity: 'error',
78
+ code: 'TW2_UNSUPPORTED_OPERATION',
79
+ message: `KVS operation is not supported by this lowering phase: ${operation}`,
80
+ sourceRef
81
+ });
82
+ return undefined;
83
+ }
84
+ }
85
+
86
+ export function isKvsEntry(entry: CompilerOpcodeRegistryEntry): boolean {
87
+ return entry.extensionId === EXTENSION_ID;
88
+ }
@@ -0,0 +1,29 @@
1
+ export type CompilerManifestDiagnosticCode =
2
+ | 'TW2_MANIFEST_JSON_SYNTAX'
3
+ | 'TW2_MANIFEST_DUPLICATE_KEY'
4
+ | 'TW2_MANIFEST_SCHEMA'
5
+ | 'TW2_MANIFEST_UNSUPPORTED_VERSION'
6
+ | 'TW2_MANIFEST_TOO_LARGE'
7
+ | 'TW2_MANIFEST_TOO_DEEP'
8
+ | 'TW2_MANIFEST_TOO_MANY_BLOCKS'
9
+ | 'TW2_MANIFEST_DUPLICATE_OPCODE'
10
+ | 'TW2_MANIFEST_DUPLICATE_EXTENSION'
11
+ | 'TW2_MANIFEST_LOCK_REQUIRED'
12
+ | 'TW2_MANIFEST_LOCK_MISMATCH'
13
+ | 'TW2_MANIFEST_INTEGRITY_MISMATCH'
14
+ | 'TW2_MANIFEST_SOURCE_UNSAFE'
15
+ | 'TW2_MANIFEST_SOURCE_NOT_FOUND'
16
+ | 'TW2_MANIFEST_OPERATION_MISMATCH'
17
+ | 'TW2_MANIFEST_UNKNOWN_OPCODE';
18
+
19
+ export class CompilerManifestError extends Error {
20
+ public readonly name = 'CompilerManifestError';
21
+
22
+ public constructor(
23
+ public readonly code: CompilerManifestDiagnosticCode,
24
+ message: string,
25
+ public readonly cause?: unknown
26
+ ) {
27
+ super(message);
28
+ }
29
+ }