@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.
Files changed (234) hide show
  1. package/README.ja.md +29 -13
  2. package/README.md +101 -42
  3. package/dist/cli.js +131 -17
  4. package/dist/cli.js.map +1 -1
  5. package/dist/compiler/adapters/cloudflare.d.ts +2 -0
  6. package/dist/compiler/adapters/cloudflare.js +477 -0
  7. package/dist/compiler/adapters/cloudflare.js.map +1 -0
  8. package/dist/compiler/adapters/firebase.d.ts +2 -0
  9. package/dist/compiler/adapters/firebase.js +489 -0
  10. package/dist/compiler/adapters/firebase.js.map +1 -0
  11. package/dist/compiler/adapters/index.d.ts +2 -0
  12. package/dist/compiler/adapters/index.js +3 -0
  13. package/dist/compiler/adapters/index.js.map +1 -0
  14. package/dist/compiler/binary/codec.d.ts +4 -0
  15. package/dist/compiler/binary/codec.js +45 -0
  16. package/dist/compiler/binary/codec.js.map +1 -0
  17. package/dist/compiler/binary/error.d.ts +3 -0
  18. package/dist/compiler/binary/error.js +21 -0
  19. package/dist/compiler/binary/error.js.map +1 -0
  20. package/dist/compiler/binary/index.d.ts +4 -0
  21. package/dist/compiler/binary/index.js +5 -0
  22. package/dist/compiler/binary/index.js.map +1 -0
  23. package/dist/compiler/binary/runtime.d.ts +18 -0
  24. package/dist/compiler/binary/runtime.js +123 -0
  25. package/dist/compiler/binary/runtime.js.map +1 -0
  26. package/dist/compiler/binary/types.d.ts +17 -0
  27. package/dist/compiler/binary/types.js +2 -0
  28. package/dist/compiler/binary/types.js.map +1 -0
  29. package/dist/compiler/feature-flags.d.ts +4 -0
  30. package/dist/compiler/feature-flags.js +4 -0
  31. package/dist/compiler/feature-flags.js.map +1 -0
  32. package/dist/compiler/index.d.ts +35 -0
  33. package/dist/compiler/index.js +84 -0
  34. package/dist/compiler/index.js.map +1 -0
  35. package/dist/compiler/ir-v2/canonical.d.ts +3 -0
  36. package/dist/compiler/ir-v2/canonical.js +50 -0
  37. package/dist/compiler/ir-v2/canonical.js.map +1 -0
  38. package/dist/compiler/ir-v2/index.d.ts +4 -0
  39. package/dist/compiler/ir-v2/index.js +5 -0
  40. package/dist/compiler/ir-v2/index.js.map +1 -0
  41. package/dist/compiler/ir-v2/parse.d.ts +10 -0
  42. package/dist/compiler/ir-v2/parse.js +753 -0
  43. package/dist/compiler/ir-v2/parse.js.map +1 -0
  44. package/dist/compiler/ir-v2/strict-json.d.ts +4 -0
  45. package/dist/compiler/ir-v2/strict-json.js +133 -0
  46. package/dist/compiler/ir-v2/strict-json.js.map +1 -0
  47. package/dist/compiler/ir-v2/types.d.ts +412 -0
  48. package/dist/compiler/ir-v2/types.js +84 -0
  49. package/dist/compiler/ir-v2/types.js.map +1 -0
  50. package/dist/compiler/kvs/lowering.d.ts +13 -0
  51. package/dist/compiler/kvs/lowering.js +77 -0
  52. package/dist/compiler/kvs/lowering.js.map +1 -0
  53. package/dist/compiler/manifest/error.d.ts +7 -0
  54. package/dist/compiler/manifest/error.js +9 -0
  55. package/dist/compiler/manifest/error.js.map +1 -0
  56. package/dist/compiler/manifest/index.d.ts +6 -0
  57. package/dist/compiler/manifest/index.js +7 -0
  58. package/dist/compiler/manifest/index.js.map +1 -0
  59. package/dist/compiler/manifest/operations.d.ts +12 -0
  60. package/dist/compiler/manifest/operations.js +60 -0
  61. package/dist/compiler/manifest/operations.js.map +1 -0
  62. package/dist/compiler/manifest/parse.d.ts +9 -0
  63. package/dist/compiler/manifest/parse.js +280 -0
  64. package/dist/compiler/manifest/parse.js.map +1 -0
  65. package/dist/compiler/manifest/registry.d.ts +3 -0
  66. package/dist/compiler/manifest/registry.js +48 -0
  67. package/dist/compiler/manifest/registry.js.map +1 -0
  68. package/dist/compiler/manifest/resolve.d.ts +14 -0
  69. package/dist/compiler/manifest/resolve.js +116 -0
  70. package/dist/compiler/manifest/resolve.js.map +1 -0
  71. package/dist/compiler/manifest/types.d.ts +95 -0
  72. package/dist/compiler/manifest/types.js +3 -0
  73. package/dist/compiler/manifest/types.js.map +1 -0
  74. package/dist/compiler/named-body/index.d.ts +1 -0
  75. package/dist/compiler/named-body/index.js +2 -0
  76. package/dist/compiler/named-body/index.js.map +1 -0
  77. package/dist/compiler/named-body/lowering.d.ts +18 -0
  78. package/dist/compiler/named-body/lowering.js +83 -0
  79. package/dist/compiler/named-body/lowering.js.map +1 -0
  80. package/dist/compiler/pipeline/compiler.d.ts +18 -0
  81. package/dist/compiler/pipeline/compiler.js +105 -0
  82. package/dist/compiler/pipeline/compiler.js.map +1 -0
  83. package/dist/compiler/pipeline/core-generator.d.ts +3 -0
  84. package/dist/compiler/pipeline/core-generator.js +483 -0
  85. package/dist/compiler/pipeline/core-generator.js.map +1 -0
  86. package/dist/compiler/pipeline/index.d.ts +5 -0
  87. package/dist/compiler/pipeline/index.js +6 -0
  88. package/dist/compiler/pipeline/index.js.map +1 -0
  89. package/dist/compiler/pipeline/registry.d.ts +7 -0
  90. package/dist/compiler/pipeline/registry.js +17 -0
  91. package/dist/compiler/pipeline/registry.js.map +1 -0
  92. package/dist/compiler/pipeline/requirements.d.ts +9 -0
  93. package/dist/compiler/pipeline/requirements.js +135 -0
  94. package/dist/compiler/pipeline/requirements.js.map +1 -0
  95. package/dist/compiler/pipeline/types.d.ts +65 -0
  96. package/dist/compiler/pipeline/types.js +2 -0
  97. package/dist/compiler/pipeline/types.js.map +1 -0
  98. package/dist/compiler/runtime/error.d.ts +6 -0
  99. package/dist/compiler/runtime/error.js +9 -0
  100. package/dist/compiler/runtime/error.js.map +1 -0
  101. package/dist/compiler/runtime/index.d.ts +1 -0
  102. package/dist/compiler/runtime/index.js +2 -0
  103. package/dist/compiler/runtime/index.js.map +1 -0
  104. package/dist/compiler/structured-data/index.d.ts +3 -0
  105. package/dist/compiler/structured-data/index.js +4 -0
  106. package/dist/compiler/structured-data/index.js.map +1 -0
  107. package/dist/compiler/structured-data/lowering.d.ts +30 -0
  108. package/dist/compiler/structured-data/lowering.js +202 -0
  109. package/dist/compiler/structured-data/lowering.js.map +1 -0
  110. package/dist/compiler/structured-data/path.d.ts +2 -0
  111. package/dist/compiler/structured-data/path.js +60 -0
  112. package/dist/compiler/structured-data/path.js.map +1 -0
  113. package/dist/compiler/structured-data/runtime.d.ts +20 -0
  114. package/dist/compiler/structured-data/runtime.js +210 -0
  115. package/dist/compiler/structured-data/runtime.js.map +1 -0
  116. package/dist/compiler/turbowarp-v2.d.ts +8 -0
  117. package/dist/compiler/turbowarp-v2.js +479 -0
  118. package/dist/compiler/turbowarp-v2.js.map +1 -0
  119. package/dist/compiler/validator/index.d.ts +4 -0
  120. package/dist/compiler/validator/index.js +5 -0
  121. package/dist/compiler/validator/index.js.map +1 -0
  122. package/dist/compiler/validator/ir-validator.d.ts +10 -0
  123. package/dist/compiler/validator/ir-validator.js +463 -0
  124. package/dist/compiler/validator/ir-validator.js.map +1 -0
  125. package/dist/compiler/validator/resource-lifetime.d.ts +18 -0
  126. package/dist/compiler/validator/resource-lifetime.js +106 -0
  127. package/dist/compiler/validator/resource-lifetime.js.map +1 -0
  128. package/dist/compiler/validator/turbowarp-subset.d.ts +3 -0
  129. package/dist/compiler/validator/turbowarp-subset.js +241 -0
  130. package/dist/compiler/validator/turbowarp-subset.js.map +1 -0
  131. package/dist/compiler/validator/types.d.ts +30 -0
  132. package/dist/compiler/validator/types.js +7 -0
  133. package/dist/compiler/validator/types.js.map +1 -0
  134. package/dist/config.d.ts +11 -0
  135. package/dist/config.js +12 -0
  136. package/dist/config.js.map +1 -0
  137. package/dist/digest-cli.js +0 -0
  138. package/dist/extension-manifest.json +102 -0
  139. package/dist/named-body.d.ts +57 -0
  140. package/dist/named-body.js +396 -0
  141. package/dist/named-body.js.map +1 -0
  142. package/dist/named-data-namespace.d.ts +3 -0
  143. package/dist/named-data-namespace.js +6 -0
  144. package/dist/named-data-namespace.js.map +1 -0
  145. package/dist/protocol.d.ts +13 -1
  146. package/dist/protocol.js +34 -0
  147. package/dist/protocol.js.map +1 -1
  148. package/dist/resource-named-body-provider.d.ts +8 -0
  149. package/dist/resource-named-body-provider.js +88 -0
  150. package/dist/resource-named-body-provider.js.map +1 -0
  151. package/dist/server.d.ts +10 -1
  152. package/dist/server.js +37 -7
  153. package/dist/server.js.map +1 -1
  154. package/dist/turbowarp-http-server.js +516 -1
  155. package/docs/binary-resource-semantics.ja.md +84 -0
  156. package/docs/cloudflare-worker-compiler.ja.md +90 -0
  157. package/docs/compiler-manifest-registry.ja.md +79 -0
  158. package/docs/compiler-platform-pipeline.ja.md +94 -0
  159. package/docs/deploy-ir-v2.ja.md +123 -0
  160. package/docs/fixed-input-editors.ja.md +23 -0
  161. package/docs/index.html +80 -0
  162. package/docs/ir-v2-conformance.ja.md +65 -0
  163. package/docs/named-response-body.ja.md +120 -0
  164. package/docs/platform-storage-adapters.ja.md +99 -0
  165. package/docs/server-executable-subset.ja.md +88 -0
  166. package/docs/structured-data-lowering.ja.md +63 -0
  167. package/examples/compiler/message-app.v2.ir.json +41 -0
  168. package/package.json +44 -20
  169. package/schemas/block-editor-fixed-inputs.schema.json +85 -0
  170. package/schemas/compiler-extension-manifest.schema.json +126 -0
  171. package/schemas/compiler-manifest-lock-v1.schema.json +48 -0
  172. package/schemas/deploy-ir-v2.schema.json +835 -0
  173. package/schemas/ir-v2-conformance-fixture.schema.json +44 -0
  174. package/src/block-definitions.json +76 -0
  175. package/src/cli.ts +136 -23
  176. package/src/compiler/adapters/cloudflare.ts +510 -0
  177. package/src/compiler/adapters/firebase.ts +538 -0
  178. package/src/compiler/adapters/index.ts +2 -0
  179. package/src/compiler/binary/codec.ts +61 -0
  180. package/src/compiler/binary/error.ts +25 -0
  181. package/src/compiler/binary/index.ts +4 -0
  182. package/src/compiler/binary/runtime.ts +133 -0
  183. package/src/compiler/binary/types.ts +26 -0
  184. package/src/compiler/feature-flags.ts +7 -0
  185. package/src/compiler/index.ts +114 -0
  186. package/src/compiler/ir-v2/canonical.ts +46 -0
  187. package/src/compiler/ir-v2/index.ts +9 -0
  188. package/src/compiler/ir-v2/parse.ts +921 -0
  189. package/src/compiler/ir-v2/strict-json.ts +129 -0
  190. package/src/compiler/ir-v2/types.ts +462 -0
  191. package/src/compiler/kvs/lowering.ts +88 -0
  192. package/src/compiler/manifest/error.ts +29 -0
  193. package/src/compiler/manifest/index.ts +17 -0
  194. package/src/compiler/manifest/operations.ts +175 -0
  195. package/src/compiler/manifest/parse.ts +390 -0
  196. package/src/compiler/manifest/registry.ts +74 -0
  197. package/src/compiler/manifest/resolve.ts +159 -0
  198. package/src/compiler/manifest/types.ts +112 -0
  199. package/src/compiler/named-body/index.ts +1 -0
  200. package/src/compiler/named-body/lowering.ts +186 -0
  201. package/src/compiler/pipeline/compiler.ts +138 -0
  202. package/src/compiler/pipeline/core-generator.ts +486 -0
  203. package/src/compiler/pipeline/index.ts +5 -0
  204. package/src/compiler/pipeline/registry.ts +18 -0
  205. package/src/compiler/pipeline/requirements.ts +139 -0
  206. package/src/compiler/pipeline/types.ts +59 -0
  207. package/src/compiler/runtime/error.ts +11 -0
  208. package/src/compiler/runtime/index.ts +1 -0
  209. package/src/compiler/structured-data/index.ts +3 -0
  210. package/src/compiler/structured-data/lowering.ts +314 -0
  211. package/src/compiler/structured-data/path.ts +53 -0
  212. package/src/compiler/structured-data/runtime.ts +221 -0
  213. package/src/compiler/turbowarp-v2.ts +610 -0
  214. package/src/compiler/validator/index.ts +4 -0
  215. package/src/compiler/validator/ir-validator.ts +812 -0
  216. package/src/compiler/validator/resource-lifetime.ts +153 -0
  217. package/src/compiler/validator/turbowarp-subset.ts +387 -0
  218. package/src/compiler/validator/types.ts +68 -0
  219. package/src/extension.ts +87 -1
  220. package/src/fixed-input-editors.ts +181 -0
  221. package/src/fixed-input-schema.ts +15 -0
  222. package/src/named-body.ts +495 -0
  223. package/src/named-data-namespace.ts +6 -0
  224. package/src/protocol.ts +50 -1
  225. package/src/resource-named-body-provider.ts +106 -0
  226. package/src/server.ts +88 -8
  227. package/docs/turbowarp-http-server.architecture.visual-check.1440x900.dark.png +0 -0
  228. package/docs/turbowarp-http-server.architecture.visual-check.1440x900.light.png +0 -0
  229. package/docs/turbowarp-http-server.architecture.visual-check.2048x1320.dark.png +0 -0
  230. package/docs/turbowarp-http-server.architecture.visual-check.2048x1320.light.png +0 -0
  231. package/docs/turbowarp-http-server.architecture.visual-check.html +0 -32
  232. package/docs/turbowarp-http-server.architecture.visual-check.json +0 -547
  233. package/schemas/extension-manifest.schema.json +0 -49
  234. package/src/extension-manifest.ts +0 -149
@@ -0,0 +1,17 @@
1
+ export {CompilerManifestError, type CompilerManifestDiagnosticCode} from './error.js';
2
+ export {KNOWN_SERVER_OPERATIONS, validateServerOperationHint} from './operations.js';
3
+ export {
4
+ DEFAULT_COMPILER_MANIFEST_LIMITS,
5
+ parseCompilerExtensionManifestJson,
6
+ parseCompilerManifestLockJson,
7
+ type CompilerManifestLimits
8
+ } from './parse.js';
9
+ export {buildCompilerOpcodeRegistry, resolveCompilerProjectOpcode} from './registry.js';
10
+ export {
11
+ manifestIntegrity,
12
+ resolveCompilerManifestLock,
13
+ type BundledCompilerManifests,
14
+ type ResolveCompilerManifestOptions,
15
+ type ResolvedCompilerManifestLock
16
+ } from './resolve.js';
17
+ export * from './types.js';
@@ -0,0 +1,175 @@
1
+ import {CompilerManifestError} from './error.js';
2
+ import type {
3
+ CompilerManifestArgument,
4
+ CompilerManifestBlock,
5
+ ManifestBlockType,
6
+ ManifestEffect,
7
+ ManifestResultType
8
+ } from './types.js';
9
+
10
+ interface ServerOperationSignature {
11
+ opcode: string;
12
+ blockType: ManifestBlockType;
13
+ arguments: readonly CompilerManifestArgument[];
14
+ resultType: ManifestResultType;
15
+ effect: ManifestEffect;
16
+ immutable: boolean;
17
+ }
18
+
19
+ const STRING = 'STRING' as const;
20
+ const NUMBER = 'NUMBER' as const;
21
+
22
+ export const KNOWN_SERVER_OPERATIONS: Readonly<Record<string, ServerOperationSignature>> = {
23
+ 'kvs.delete': signature(
24
+ 'deleteKey',
25
+ 'COMMAND',
26
+ [{id: 'KEY', type: STRING}, {id: 'NAMESPACE', type: STRING}],
27
+ 'void',
28
+ 'storage-write',
29
+ false
30
+ ),
31
+ 'kvs.getText': signature(
32
+ 'getValue',
33
+ 'REPORTER',
34
+ [{id: 'KEY', type: STRING}, {id: 'NAMESPACE', type: STRING}],
35
+ 'string',
36
+ 'storage-read'
37
+ ),
38
+ 'kvs.has': signature(
39
+ 'hasKey',
40
+ 'BOOLEAN',
41
+ [{id: 'KEY', type: STRING}, {id: 'NAMESPACE', type: STRING}],
42
+ 'boolean',
43
+ 'storage-read'
44
+ ),
45
+ 'kvs.listKeys': signature(
46
+ 'listKeys',
47
+ 'REPORTER',
48
+ [{id: 'NAMESPACE', type: STRING}],
49
+ 'json',
50
+ 'storage-read'
51
+ ),
52
+ 'kvs.setText': signature(
53
+ 'setValue',
54
+ 'COMMAND',
55
+ [{id: 'KEY', type: STRING}, {id: 'NAMESPACE', type: STRING}, {id: 'VALUE', type: STRING}],
56
+ 'void',
57
+ 'storage-write',
58
+ false
59
+ ),
60
+ 'structuredData.currentIndex': signature('currentIndex', 'REPORTER', [], 'number', 'control'),
61
+ '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'
69
+ ),
70
+ 'structuredData.forEach': signature(
71
+ 'forEachAtPath',
72
+ 'LOOP',
73
+ [
74
+ {id: 'JSON', type: STRING},
75
+ {id: 'MAX', type: NUMBER, staticLiteral: true, minimum: 1, maximum: 1000},
76
+ pathArgument()
77
+ ],
78
+ 'void',
79
+ 'control'
80
+ ),
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',
90
+ 'BOOLEAN',
91
+ [{id: 'JSON', type: STRING}, pathArgument()],
92
+ 'boolean',
93
+ 'pure'
94
+ ),
95
+ 'structuredData.isValidJson': signature(
96
+ 'isValidJson',
97
+ 'BOOLEAN',
98
+ [{id: 'JSON', type: STRING}],
99
+ 'boolean',
100
+ 'pure'
101
+ ),
102
+ 'structuredData.keys': signature(
103
+ 'keysAtPath',
104
+ 'REPORTER',
105
+ [{id: 'JSON', type: STRING}, pathArgument()],
106
+ 'json',
107
+ 'pure'
108
+ ),
109
+ 'structuredData.length': signature(
110
+ 'lengthAtPath',
111
+ 'REPORTER',
112
+ [{id: 'JSON', type: STRING}, pathArgument()],
113
+ 'number',
114
+ 'pure'
115
+ ),
116
+ 'structuredData.normalizeJson': signature(
117
+ 'normalizeJson',
118
+ 'REPORTER',
119
+ [{id: 'JSON', type: STRING}],
120
+ 'json',
121
+ 'pure'
122
+ ),
123
+ 'structuredData.set': signature(
124
+ 'setJsonAtPath',
125
+ 'REPORTER',
126
+ [{id: 'JSON', type: STRING}, pathArgument(), {id: 'VALUE', type: STRING}],
127
+ 'json',
128
+ 'immutable'
129
+ )
130
+ };
131
+
132
+ export function validateServerOperationHint(block: CompilerManifestBlock): void {
133
+ const operation = block.server?.irOperation;
134
+ if (block.server?.supported !== true) {
135
+ if (operation !== undefined) operationMismatch(block, operation, 'unsupported blocks must not declare an operation');
136
+ return;
137
+ }
138
+ if (operation === undefined) operationMismatch(block, '<missing>', 'supported blocks require an operation');
139
+ const expected = KNOWN_SERVER_OPERATIONS[operation];
140
+ if (expected === undefined) operationMismatch(block, operation, 'operation is not in the compiler allowlist');
141
+
142
+ const actual = {
143
+ opcode: block.opcode,
144
+ blockType: block.blockType,
145
+ arguments: block.arguments,
146
+ resultType: block.resultType,
147
+ effect: block.effect,
148
+ immutable: block.immutable
149
+ };
150
+ if (JSON.stringify(actual) !== JSON.stringify(expected)) {
151
+ operationMismatch(block, operation, 'block signature does not match the compiler allowlist');
152
+ }
153
+ }
154
+
155
+ function signature(
156
+ opcode: string,
157
+ blockType: ManifestBlockType,
158
+ argumentsValue: readonly CompilerManifestArgument[],
159
+ resultType: ManifestResultType,
160
+ effect: ManifestEffect,
161
+ immutable = true
162
+ ): ServerOperationSignature {
163
+ return {opcode, blockType, arguments: argumentsValue, resultType, effect, immutable};
164
+ }
165
+
166
+ function pathArgument(): CompilerManifestArgument {
167
+ return {id: 'PATH', type: STRING, normalizesTo: 'pathSegments'};
168
+ }
169
+
170
+ function operationMismatch(block: CompilerManifestBlock, operation: string, reason: string): never {
171
+ throw new CompilerManifestError(
172
+ 'TW2_MANIFEST_OPERATION_MISMATCH',
173
+ `Manifest operation ${operation} on block ${block.opcode} is invalid: ${reason}.`
174
+ );
175
+ }
@@ -0,0 +1,390 @@
1
+ import {parseJsonWithoutDuplicateKeys} from '../ir-v2/strict-json.js';
2
+ import {CompilerManifestError} from './error.js';
3
+ import {
4
+ COMPILER_MANIFEST_LOCK_VERSION,
5
+ type CompilerExtensionManifest,
6
+ type CompilerManifestArgument,
7
+ type CompilerManifestBlock,
8
+ type CompilerManifestFormatVersion,
9
+ type CompilerManifestLock,
10
+ type CompilerManifestLockEntry,
11
+ type CompilerManifestServerHint,
12
+ type CompilerManifestSource,
13
+ type ManifestArgumentType,
14
+ type ManifestBlockType,
15
+ type ManifestEffect,
16
+ type ManifestResultType
17
+ } from './types.js';
18
+
19
+ export interface CompilerManifestLimits {
20
+ maxBytes: number;
21
+ maxDepth: number;
22
+ maxBlocks: number;
23
+ }
24
+
25
+ export const DEFAULT_COMPILER_MANIFEST_LIMITS: Readonly<CompilerManifestLimits> = {
26
+ maxBytes: 1024 * 1024,
27
+ maxDepth: 64,
28
+ maxBlocks: 2048
29
+ };
30
+
31
+ const BLOCK_TYPES = new Set<ManifestBlockType>(['COMMAND', 'REPORTER', 'BOOLEAN', 'HAT', 'LOOP']);
32
+ const ARGUMENT_TYPES = new Set<ManifestArgumentType>(['STRING', 'NUMBER', 'BOOLEAN']);
33
+ const RESULT_TYPES = new Set<ManifestResultType>(['json', 'boolean', 'number', 'string', 'void', 'jsonText', 'yamlText']);
34
+ const EFFECTS = new Set<ManifestEffect>([
35
+ 'pure',
36
+ 'immutable',
37
+ 'control',
38
+ 'request-read',
39
+ 'response-write',
40
+ 'storage-read',
41
+ 'storage-write',
42
+ 'binary-read',
43
+ 'binary-write',
44
+ 'state'
45
+ ]);
46
+ const EXTENSION_ID = /^[a-z0-9]+$/;
47
+ const OPCODE = /^[A-Za-z][A-Za-z0-9_]*$/;
48
+ const EXACT_PACKAGE_VERSION = /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$/;
49
+ const INTEGRITY = /^sha256-([A-Za-z0-9+/]{43}=)$/;
50
+
51
+ export function parseCompilerExtensionManifestJson(
52
+ text: string,
53
+ limits: Readonly<CompilerManifestLimits> = DEFAULT_COMPILER_MANIFEST_LIMITS
54
+ ): CompilerExtensionManifest {
55
+ assertByteLimit(text, limits.maxBytes, 'manifest');
56
+ const value = parseStrictJson(text, limits.maxDepth);
57
+ const root = object(value, 'manifest');
58
+ exactKeys(root, ['formatVersion', 'id', 'pathSegmentType', 'dataReferenceType', 'blocks', 'menus'], 'manifest');
59
+ const formatVersion = manifestFormatVersion(root.formatVersion, 'manifest.formatVersion');
60
+ const id = extensionId(root.id, 'manifest.id');
61
+ const rawBlocks = array(root.blocks, 'manifest.blocks');
62
+ if (rawBlocks.length === 0) schemaError('manifest.blocks must not be empty.');
63
+ if (rawBlocks.length > limits.maxBlocks) {
64
+ throw new CompilerManifestError(
65
+ 'TW2_MANIFEST_TOO_MANY_BLOCKS',
66
+ `manifest.blocks exceeds the ${limits.maxBlocks} block limit.`
67
+ );
68
+ }
69
+ const blocks = rawBlocks.map((block, index) => parseBlock(block, `manifest.blocks[${index}]`, formatVersion));
70
+ rejectDuplicates(blocks.map((block) => block.opcode), 'TW2_MANIFEST_DUPLICATE_OPCODE', 'manifest opcode');
71
+
72
+ const pathSegmentType =
73
+ root.pathSegmentType === undefined
74
+ ? undefined
75
+ : parsePathSegmentType(root.pathSegmentType, 'manifest.pathSegmentType');
76
+ const dataReferenceType =
77
+ root.dataReferenceType === undefined
78
+ ? undefined
79
+ : parseDataReferenceType(root.dataReferenceType, 'manifest.dataReferenceType');
80
+ const menus = root.menus === undefined ? undefined : array(root.menus, 'manifest.menus');
81
+ return optional(
82
+ optional(
83
+ optional({formatVersion, id, blocks}, 'pathSegmentType', pathSegmentType),
84
+ 'dataReferenceType',
85
+ dataReferenceType
86
+ ),
87
+ 'menus',
88
+ menus
89
+ );
90
+ }
91
+
92
+ /** The counterpart of pathSegmentType: what the values an extension hands out refer to. */
93
+ function parseDataReferenceType(
94
+ value: unknown,
95
+ location: string
96
+ ): NonNullable<CompilerExtensionManifest['dataReferenceType']> {
97
+ const reference = object(value, location);
98
+ exactKeys(reference, ['kind', 'scope', 'lifetime', 'valueType'], location);
99
+ return {
100
+ kind: nonEmptyString(reference.kind, `${location}.kind`),
101
+ scope: nonEmptyString(reference.scope, `${location}.scope`),
102
+ lifetime: nonEmptyString(reference.lifetime, `${location}.lifetime`),
103
+ valueType: nonEmptyString(reference.valueType, `${location}.valueType`)
104
+ };
105
+ }
106
+
107
+ export function parseCompilerManifestLockJson(
108
+ text: string,
109
+ limits: Readonly<Pick<CompilerManifestLimits, 'maxBytes' | 'maxDepth'>> = DEFAULT_COMPILER_MANIFEST_LIMITS
110
+ ): CompilerManifestLock {
111
+ assertByteLimit(text, limits.maxBytes, 'manifest lock');
112
+ const root = object(parseStrictJson(text, limits.maxDepth), 'manifest lock');
113
+ exactKeys(root, ['lockVersion', 'extensions'], 'manifest lock');
114
+ if (root.lockVersion !== COMPILER_MANIFEST_LOCK_VERSION) {
115
+ throw new CompilerManifestError(
116
+ 'TW2_MANIFEST_UNSUPPORTED_VERSION',
117
+ `manifest lock.lockVersion must be ${COMPILER_MANIFEST_LOCK_VERSION}.`
118
+ );
119
+ }
120
+ const extensions = array(root.extensions, 'manifest lock.extensions').map((entry, index) =>
121
+ parseLockEntry(entry, `manifest lock.extensions[${index}]`)
122
+ );
123
+ rejectDuplicates(
124
+ extensions.map((entry) => entry.extensionId),
125
+ 'TW2_MANIFEST_DUPLICATE_EXTENSION',
126
+ 'locked extension ID'
127
+ );
128
+ return {
129
+ lockVersion: COMPILER_MANIFEST_LOCK_VERSION,
130
+ extensions: extensions.sort((left, right) => lexicalCompare(left.extensionId, right.extensionId))
131
+ };
132
+ }
133
+
134
+ function parseBlock(value: unknown, location: string, formatVersion: CompilerManifestFormatVersion): CompilerManifestBlock {
135
+ const block = object(value, location);
136
+ exactKeys(
137
+ block,
138
+ ['opcode', 'blockType', 'arguments', 'resultType', 'effect', 'immutable', 'errors', 'server'],
139
+ location
140
+ );
141
+ const opcode = matches(block.opcode, OPCODE, `${location}.opcode`);
142
+ const blockType = enumValue(block.blockType, BLOCK_TYPES, `${location}.blockType`);
143
+ const args = array(block.arguments, `${location}.arguments`).map((argument, index) =>
144
+ parseArgument(argument, `${location}.arguments[${index}]`)
145
+ );
146
+ rejectDuplicates(
147
+ args.map((argument) => argument.id),
148
+ 'TW2_MANIFEST_SCHEMA',
149
+ `${location} argument ID`
150
+ );
151
+ const resultType = optionalEnum(block.resultType, RESULT_TYPES, `${location}.resultType`);
152
+ const effect = optionalEnum(block.effect, EFFECTS, `${location}.effect`);
153
+ const immutable = optionalBoolean(block.immutable, `${location}.immutable`);
154
+ const errors = optionalStringArray(block.errors, `${location}.errors`);
155
+ const server = block.server === undefined ? undefined : parseServerHint(block.server, `${location}.server`);
156
+ if (formatVersion === 2 && [resultType, effect, immutable, errors, server].some((item) => item === undefined)) {
157
+ schemaError(`${location} is missing formatVersion 2 compiler metadata.`);
158
+ }
159
+ return optional(
160
+ optional(
161
+ optional(optional(optional({opcode, blockType, arguments: args}, 'resultType', resultType), 'effect', effect), 'immutable', immutable),
162
+ 'errors',
163
+ errors
164
+ ),
165
+ 'server',
166
+ server
167
+ );
168
+ }
169
+
170
+ function parseArgument(value: unknown, location: string): CompilerManifestArgument {
171
+ const argument = object(value, location);
172
+ exactKeys(argument, ['id', 'type', 'menu', 'normalizesTo', 'staticLiteral', 'minimum', 'maximum'], location);
173
+ const id = nonEmptyString(argument.id, `${location}.id`);
174
+ const type = enumValue(argument.type, ARGUMENT_TYPES, `${location}.type`);
175
+ const menu = optionalString(argument.menu, `${location}.menu`);
176
+ const rawNormalizesTo = argument.normalizesTo;
177
+ if (rawNormalizesTo !== undefined && rawNormalizesTo !== 'pathSegments') {
178
+ schemaError(`${location}.normalizesTo must be pathSegments.`);
179
+ }
180
+ const normalizesTo: 'pathSegments' | undefined = rawNormalizesTo;
181
+ const staticLiteral = optionalBoolean(argument.staticLiteral, `${location}.staticLiteral`);
182
+ const minimum = optionalFiniteNumber(argument.minimum, `${location}.minimum`);
183
+ const maximum = optionalFiniteNumber(argument.maximum, `${location}.maximum`);
184
+ if (minimum !== undefined && maximum !== undefined && minimum > maximum) {
185
+ schemaError(`${location}.minimum must not exceed maximum.`);
186
+ }
187
+ return optional(
188
+ optional(
189
+ optional(
190
+ optional(optional({id, type}, 'menu', menu), 'normalizesTo', normalizesTo),
191
+ 'staticLiteral',
192
+ staticLiteral
193
+ ),
194
+ 'minimum',
195
+ minimum
196
+ ),
197
+ 'maximum',
198
+ maximum
199
+ );
200
+ }
201
+
202
+ function parseServerHint(value: unknown, location: string): CompilerManifestServerHint {
203
+ const server = object(value, location);
204
+ exactKeys(server, ['supported', 'irOperation'], location);
205
+ const supported = boolean(server.supported, `${location}.supported`);
206
+ const irOperation = optionalString(server.irOperation, `${location}.irOperation`);
207
+ if (supported && irOperation === undefined) schemaError(`${location}.irOperation is required when supported is true.`);
208
+ return optional({supported}, 'irOperation', irOperation);
209
+ }
210
+
211
+ function parsePathSegmentType(
212
+ value: unknown,
213
+ location: string
214
+ ): NonNullable<CompilerExtensionManifest['pathSegmentType']> {
215
+ const pathType = object(value, location);
216
+ exactKeys(pathType, ['kind', 'variants'], location);
217
+ if (pathType.kind !== 'discriminatedUnion') schemaError(`${location}.kind must be discriminatedUnion.`);
218
+ const variants = array(pathType.variants, `${location}.variants`);
219
+ if (variants.length !== 2) schemaError(`${location}.variants must contain key and index in that order.`);
220
+ const key = object(variants[0], `${location}.variants[0]`);
221
+ const index = object(variants[1], `${location}.variants[1]`);
222
+ exactKeys(key, ['kind', 'valueType'], `${location}.variants[0]`);
223
+ exactKeys(index, ['kind', 'valueType'], `${location}.variants[1]`);
224
+ if (key.kind !== 'key' || key.valueType !== 'string') schemaError(`${location}.variants[0] must be a string key.`);
225
+ if (index.kind !== 'index' || index.valueType !== 'nonNegativeInteger') {
226
+ schemaError(`${location}.variants[1] must be a non-negative integer index.`);
227
+ }
228
+ return {
229
+ kind: 'discriminatedUnion',
230
+ variants: [
231
+ {kind: 'key', valueType: 'string'},
232
+ {kind: 'index', valueType: 'nonNegativeInteger'}
233
+ ]
234
+ };
235
+ }
236
+
237
+ function parseLockEntry(value: unknown, location: string): CompilerManifestLockEntry {
238
+ const entry = object(value, location);
239
+ exactKeys(
240
+ entry,
241
+ ['extensionId', 'packageName', 'packageVersion', 'manifestFormatVersion', 'integrity', 'source'],
242
+ location
243
+ );
244
+ const extensionIdValue = extensionId(entry.extensionId, `${location}.extensionId`);
245
+ const packageName = nonEmptyString(entry.packageName, `${location}.packageName`);
246
+ const packageVersion = matches(entry.packageVersion, EXACT_PACKAGE_VERSION, `${location}.packageVersion`);
247
+ const manifestFormatVersion = manifestFormatVersionValue(
248
+ entry.manifestFormatVersion,
249
+ `${location}.manifestFormatVersion`
250
+ );
251
+ const integrity = matches(entry.integrity, INTEGRITY, `${location}.integrity`) as `sha256-${string}`;
252
+ const source = parseSource(entry.source, `${location}.source`);
253
+ return {extensionId: extensionIdValue, packageName, packageVersion, manifestFormatVersion, integrity, source};
254
+ }
255
+
256
+ function parseSource(value: unknown, location: string): CompilerManifestSource {
257
+ const source = object(value, location);
258
+ const kind = nonEmptyString(source.kind, `${location}.kind`);
259
+ if (kind === 'bundled') {
260
+ exactKeys(source, ['kind', 'id'], location);
261
+ return {kind, id: nonEmptyString(source.id, `${location}.id`)};
262
+ }
263
+ if (kind === 'local') {
264
+ exactKeys(source, ['kind', 'path'], location);
265
+ return {kind, path: nonEmptyString(source.path, `${location}.path`)};
266
+ }
267
+ schemaError(`${location}.kind is unsupported: ${kind}`);
268
+ }
269
+
270
+ function manifestFormatVersion(value: unknown, location: string): CompilerManifestFormatVersion {
271
+ return manifestFormatVersionValue(value, location);
272
+ }
273
+
274
+ function manifestFormatVersionValue(value: unknown, location: string): CompilerManifestFormatVersion {
275
+ if (value !== 1 && value !== 2) {
276
+ throw new CompilerManifestError('TW2_MANIFEST_UNSUPPORTED_VERSION', `${location} must be 1 or 2.`);
277
+ }
278
+ return value;
279
+ }
280
+
281
+ function parseStrictJson(text: string, maxDepth: number): unknown {
282
+ try {
283
+ return parseJsonWithoutDuplicateKeys(text, {maxDepth});
284
+ } catch (error) {
285
+ const message = error instanceof Error ? error.message : String(error);
286
+ const code = message.startsWith('Duplicate object key:')
287
+ ? 'TW2_MANIFEST_DUPLICATE_KEY'
288
+ : message.startsWith('JSON nesting depth exceeds')
289
+ ? 'TW2_MANIFEST_TOO_DEEP'
290
+ : 'TW2_MANIFEST_JSON_SYNTAX';
291
+ throw new CompilerManifestError(code, message, error);
292
+ }
293
+ }
294
+
295
+ function assertByteLimit(text: string, maxBytes: number, label: string): void {
296
+ const bytes = Buffer.byteLength(text, 'utf8');
297
+ if (bytes > maxBytes) {
298
+ throw new CompilerManifestError('TW2_MANIFEST_TOO_LARGE', `${label} is ${bytes} bytes; limit is ${maxBytes}.`);
299
+ }
300
+ }
301
+
302
+ function rejectDuplicates(
303
+ values: readonly string[],
304
+ code: ConstructorParameters<typeof CompilerManifestError>[0],
305
+ label: string
306
+ ): void {
307
+ const seen = new Set<string>();
308
+ for (const value of values) {
309
+ if (seen.has(value)) throw new CompilerManifestError(code, `Duplicate ${label}: ${value}`);
310
+ seen.add(value);
311
+ }
312
+ }
313
+
314
+ function object(value: unknown, location: string): Record<string, unknown> {
315
+ if (typeof value !== 'object' || value === null || Array.isArray(value)) schemaError(`${location} must be an object.`);
316
+ return value as Record<string, unknown>;
317
+ }
318
+
319
+ function array(value: unknown, location: string): unknown[] {
320
+ if (!Array.isArray(value)) schemaError(`${location} must be an array.`);
321
+ return value;
322
+ }
323
+
324
+ function exactKeys(record: Record<string, unknown>, allowed: readonly string[], location: string): void {
325
+ const allowedSet = new Set(allowed);
326
+ const unknown = Object.keys(record).filter((key) => !allowedSet.has(key)).sort()[0];
327
+ if (unknown !== undefined) schemaError(`${location} contains unknown field: ${unknown}`);
328
+ }
329
+
330
+ function extensionId(value: unknown, location: string): string {
331
+ return matches(value, EXTENSION_ID, location);
332
+ }
333
+
334
+ function matches(value: unknown, pattern: RegExp, location: string): string {
335
+ const text = nonEmptyString(value, location);
336
+ if (!pattern.test(text)) schemaError(`${location} has an invalid format.`);
337
+ return text;
338
+ }
339
+
340
+ function nonEmptyString(value: unknown, location: string): string {
341
+ if (typeof value !== 'string' || value.length === 0) schemaError(`${location} must be a non-empty string.`);
342
+ return value;
343
+ }
344
+
345
+ function optionalString(value: unknown, location: string): string | undefined {
346
+ return value === undefined ? undefined : nonEmptyString(value, location);
347
+ }
348
+
349
+ function boolean(value: unknown, location: string): boolean {
350
+ if (typeof value !== 'boolean') schemaError(`${location} must be a boolean.`);
351
+ return value;
352
+ }
353
+
354
+ function optionalBoolean(value: unknown, location: string): boolean | undefined {
355
+ return value === undefined ? undefined : boolean(value, location);
356
+ }
357
+
358
+ function optionalFiniteNumber(value: unknown, location: string): number | undefined {
359
+ if (value === undefined) return undefined;
360
+ if (typeof value !== 'number' || !Number.isFinite(value)) schemaError(`${location} must be a finite number.`);
361
+ return value;
362
+ }
363
+
364
+ function enumValue<T extends string>(value: unknown, allowed: ReadonlySet<T>, location: string): T {
365
+ if (typeof value !== 'string' || !allowed.has(value as T)) schemaError(`${location} is unsupported.`);
366
+ return value as T;
367
+ }
368
+
369
+ function optionalEnum<T extends string>(value: unknown, allowed: ReadonlySet<T>, location: string): T | undefined {
370
+ return value === undefined ? undefined : enumValue(value, allowed, location);
371
+ }
372
+
373
+ function optionalStringArray(value: unknown, location: string): string[] | undefined {
374
+ if (value === undefined) return undefined;
375
+ const values = array(value, location).map((item, index) => matches(item, /^[A-Z][A-Z0-9_]*$/, `${location}[${index}]`));
376
+ rejectDuplicates(values, 'TW2_MANIFEST_SCHEMA', `${location} value`);
377
+ return values;
378
+ }
379
+
380
+ function schemaError(message: string): never {
381
+ throw new CompilerManifestError('TW2_MANIFEST_SCHEMA', message);
382
+ }
383
+
384
+ function lexicalCompare(left: string, right: string): number {
385
+ return left < right ? -1 : left > right ? 1 : 0;
386
+ }
387
+
388
+ function optional<T extends object, K extends string, V>(base: T, key: K, value: V | undefined): T & Partial<Record<K, V>> {
389
+ return value === undefined ? base : {...base, [key]: value};
390
+ }
@@ -0,0 +1,74 @@
1
+ import {CompilerManifestError} from './error.js';
2
+ import {validateServerOperationHint} from './operations.js';
3
+ import type {
4
+ CompilerOpcodeRegistry,
5
+ CompilerOpcodeRegistryEntry,
6
+ ResolvedCompilerManifest
7
+ } from './types.js';
8
+
9
+ export function buildCompilerOpcodeRegistry(
10
+ resolvedManifests: readonly ResolvedCompilerManifest[]
11
+ ): CompilerOpcodeRegistry {
12
+ const extensionIds = new Set<string>();
13
+ const entries: CompilerOpcodeRegistryEntry[] = [];
14
+ for (const resolved of resolvedManifests) {
15
+ const {manifest, lock} = resolved;
16
+ if (extensionIds.has(manifest.id)) {
17
+ throw new CompilerManifestError(
18
+ 'TW2_MANIFEST_DUPLICATE_EXTENSION',
19
+ `Duplicate resolved extension ID: ${manifest.id}`
20
+ );
21
+ }
22
+ extensionIds.add(manifest.id);
23
+ if (
24
+ manifest.blocks.some((block) => block.server?.irOperation?.startsWith('structuredData.')) &&
25
+ manifest.pathSegmentType === undefined
26
+ ) {
27
+ throw new CompilerManifestError(
28
+ 'TW2_MANIFEST_OPERATION_MISMATCH',
29
+ `Structured Data manifest ${manifest.id} must declare the supported typed path segment format.`
30
+ );
31
+ }
32
+ for (const block of manifest.blocks) {
33
+ validateServerOperationHint(block);
34
+ entries.push({
35
+ extensionId: manifest.id,
36
+ opcode: block.opcode,
37
+ projectOpcode: `${manifest.id}_${block.opcode}`,
38
+ packageName: lock.packageName,
39
+ packageVersion: lock.packageVersion,
40
+ block
41
+ });
42
+ }
43
+ }
44
+ entries.sort((left, right) => lexicalCompare(left.projectOpcode, right.projectOpcode));
45
+ const byProjectOpcode = new Map<string, CompilerOpcodeRegistryEntry>();
46
+ for (const entry of entries) {
47
+ if (byProjectOpcode.has(entry.projectOpcode)) {
48
+ throw new CompilerManifestError(
49
+ 'TW2_MANIFEST_DUPLICATE_OPCODE',
50
+ `Duplicate project opcode: ${entry.projectOpcode}`
51
+ );
52
+ }
53
+ byProjectOpcode.set(entry.projectOpcode, entry);
54
+ }
55
+ return {entries, byProjectOpcode};
56
+ }
57
+
58
+ export function resolveCompilerProjectOpcode(
59
+ registry: CompilerOpcodeRegistry,
60
+ projectOpcode: string
61
+ ): CompilerOpcodeRegistryEntry {
62
+ const entry = registry.byProjectOpcode.get(projectOpcode);
63
+ if (entry === undefined) {
64
+ throw new CompilerManifestError(
65
+ 'TW2_MANIFEST_UNKNOWN_OPCODE',
66
+ `Project opcode is not present in the locked compiler registry: ${projectOpcode}`
67
+ );
68
+ }
69
+ return entry;
70
+ }
71
+
72
+ function lexicalCompare(left: string, right: string): number {
73
+ return left < right ? -1 : left > right ? 1 : 0;
74
+ }