@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,753 @@
1
+ import { parseJsonWithoutDuplicateKeys } from './strict-json.js';
2
+ import { isNamedDataNamespace } from '../../named-data-namespace.js';
3
+ import { DEPLOY_IR_V2_VERSION, JSON_VALUE_TYPE_V2 } from './types.js';
4
+ const METHODS = new Set(['ALL', 'GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS']);
5
+ const NAMED_TARGET_ID = /^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$/u;
6
+ const ATOMIC_TYPES = new Set([
7
+ 'null',
8
+ 'boolean',
9
+ 'number',
10
+ 'string',
11
+ 'json-text',
12
+ 'json-array',
13
+ 'json-object',
14
+ 'binary-ref'
15
+ ]);
16
+ const BINDING_ID = /^[A-Za-z_][A-Za-z0-9_]*$/;
17
+ const ATOMIC_TYPE_ORDER = [
18
+ 'null',
19
+ 'boolean',
20
+ 'number',
21
+ 'string',
22
+ 'json-text',
23
+ 'json-array',
24
+ 'json-object',
25
+ 'binary-ref'
26
+ ];
27
+ export class DeployIrV2ParseError extends Error {
28
+ constructor(code, message, cause) {
29
+ super(message);
30
+ this.code = code;
31
+ this.cause = cause;
32
+ this.name = 'DeployIrV2ParseError';
33
+ }
34
+ }
35
+ export function parseDeployIrV2Json(text) {
36
+ return withParseDiagnostic(() => parseDeployIrV2Value(parseJsonWithoutDuplicateKeys(text)));
37
+ }
38
+ export function parseDeployIrV2(value) {
39
+ return withParseDiagnostic(() => parseDeployIrV2Value(value));
40
+ }
41
+ function parseDeployIrV2Value(value) {
42
+ const root = object(value, 'IR');
43
+ exactKeys(root, ['version', 'name', 'auth', 'capabilities', 'routes'], 'IR');
44
+ if (root.version !== DEPLOY_IR_V2_VERSION)
45
+ throw new Error('IR.version must be 2.');
46
+ const name = nonEmptyString(root.name, 'IR.name');
47
+ const auth = parseAuth(root.auth, 'IR.auth');
48
+ const capabilities = normalizeCapabilities(array(root.capabilities, 'IR.capabilities').map((item, index) => parseCapability(item, `IR.capabilities[${index}]`)));
49
+ const routes = array(root.routes, 'IR.routes').map((item, index) => parseRoute(item, `IR.routes[${index}]`));
50
+ if (routes.length === 0)
51
+ throw new Error('IR.routes must not be empty.');
52
+ return { version: DEPLOY_IR_V2_VERSION, name, auth, capabilities, routes };
53
+ }
54
+ function withParseDiagnostic(operation) {
55
+ try {
56
+ return operation();
57
+ }
58
+ catch (error) {
59
+ if (error instanceof DeployIrV2ParseError)
60
+ throw error;
61
+ const message = error instanceof Error ? error.message : String(error);
62
+ let code = 'TW2_IR_INVALID_VALUE';
63
+ if (error instanceof SyntaxError) {
64
+ code = message.startsWith('Duplicate object key:') ? 'TW2_IR_DUPLICATE_KEY' : 'TW2_IR_JSON_SYNTAX';
65
+ }
66
+ else if (message === 'IR.version must be 2.') {
67
+ code = 'TW2_IR_VERSION';
68
+ }
69
+ else if (message.includes('contains unknown field:')) {
70
+ code = 'TW2_IR_UNKNOWN_FIELD';
71
+ }
72
+ else if (message.includes('.kind is unsupported:')) {
73
+ code = 'TW2_IR_UNKNOWN_NODE';
74
+ }
75
+ throw new DeployIrV2ParseError(code, message, error);
76
+ }
77
+ }
78
+ function parseAuth(value, location) {
79
+ const auth = object(value, location);
80
+ const kind = nonEmptyString(auth.kind, `${location}.kind`);
81
+ if (kind === 'none') {
82
+ exactKeys(auth, ['kind'], location);
83
+ return { kind };
84
+ }
85
+ if (kind === 'jwt') {
86
+ exactKeys(auth, ['kind', 'scheme'], location);
87
+ const scheme = nonEmptyString(auth.scheme, `${location}.scheme`);
88
+ if (scheme !== 'external-jwt' && scheme !== 'trusted-access-jwt') {
89
+ throw new Error(`${location}.scheme is unsupported: ${scheme}`);
90
+ }
91
+ return { kind, scheme };
92
+ }
93
+ throw new Error(`${location}.kind is unsupported: ${kind}`);
94
+ }
95
+ function parseCapability(value, location) {
96
+ const capability = object(value, location);
97
+ const kind = nonEmptyString(capability.kind, `${location}.kind`);
98
+ if (kind === 'record-store' || kind === 'key-value-store') {
99
+ exactKeys(capability, ['kind'], location);
100
+ return { kind };
101
+ }
102
+ if (kind === 'object-storage' || kind === 'streaming-body' || kind === 'named-body-provider') {
103
+ exactKeys(capability, ['kind'], location);
104
+ return { kind };
105
+ }
106
+ if (kind === 'request-metadata') {
107
+ exactKeys(capability, ['kind', 'field'], location);
108
+ if (capability.field !== 'client-address')
109
+ throw new Error(`${location}.field is unsupported.`);
110
+ return { kind, field: 'client-address' };
111
+ }
112
+ if (kind === 'auth') {
113
+ exactKeys(capability, ['kind', 'scheme'], location);
114
+ const scheme = capability.scheme;
115
+ if (scheme !== 'external-jwt' && scheme !== 'trusted-access-jwt') {
116
+ throw new Error(`${location}.scheme is unsupported.`);
117
+ }
118
+ return { kind, scheme };
119
+ }
120
+ throw new Error(`${location}.kind is unsupported: ${kind}`);
121
+ }
122
+ function parseRoute(value, location) {
123
+ const route = object(value, location);
124
+ exactKeys(route, ['id', 'method', 'path', 'auth', 'body', 'sourceRef'], location);
125
+ const id = nonEmptyString(route.id, `${location}.id`);
126
+ const method = nonEmptyString(route.method, `${location}.method`);
127
+ if (!METHODS.has(method))
128
+ throw new Error(`${location}.method is unsupported: ${method}`);
129
+ const path = nonEmptyString(route.path, `${location}.path`);
130
+ const auth = route.auth;
131
+ if (auth !== 'public' && auth !== 'required')
132
+ throw new Error(`${location}.auth is invalid.`);
133
+ const body = array(route.body, `${location}.body`).map((item, index) => parseStatement(item, `${location}.body[${index}]`));
134
+ if (body.length === 0)
135
+ throw new Error(`${location}.body must not be empty.`);
136
+ const sourceRef = optionalSourceRef(route.sourceRef, `${location}.sourceRef`);
137
+ return optional({ id, method, path, auth, body }, 'sourceRef', sourceRef);
138
+ }
139
+ function parseStatement(value, location) {
140
+ const statement = object(value, location);
141
+ const kind = nonEmptyString(statement.kind, `${location}.kind`);
142
+ const sourceRef = optionalSourceRef(statement.sourceRef, `${location}.sourceRef`);
143
+ if (kind === 'set-status') {
144
+ exactKeys(statement, ['kind', 'status', 'sourceRef'], location);
145
+ const status = integer(statement.status, `${location}.status`);
146
+ if (status < 100 || status > 599)
147
+ throw new Error(`${location}.status must be from 100 to 599.`);
148
+ return optional({ kind, status }, 'sourceRef', sourceRef);
149
+ }
150
+ if (kind === 'set-header') {
151
+ exactKeys(statement, ['kind', 'name', 'value', 'sourceRef'], location);
152
+ return optional({ kind, name: nonEmptyString(statement.name, `${location}.name`), value: parseExpression(statement.value, `${location}.value`) }, 'sourceRef', sourceRef);
153
+ }
154
+ if (kind === 'remove-header') {
155
+ exactKeys(statement, ['kind', 'name', 'sourceRef'], location);
156
+ return optional({ kind, name: nonEmptyString(statement.name, `${location}.name`) }, 'sourceRef', sourceRef);
157
+ }
158
+ if (kind === 'set-handler-variable' || kind === 'change-handler-variable') {
159
+ exactKeys(statement, ['kind', 'name', 'value', 'sourceRef'], location);
160
+ return optional({
161
+ kind,
162
+ name: parseExpression(statement.name, `${location}.name`),
163
+ value: parseExpression(statement.value, `${location}.value`)
164
+ }, 'sourceRef', sourceRef);
165
+ }
166
+ if (kind === 'delete-handler-variable') {
167
+ exactKeys(statement, ['kind', 'name', 'sourceRef'], location);
168
+ return optional({ kind, name: parseExpression(statement.name, `${location}.name`) }, 'sourceRef', sourceRef);
169
+ }
170
+ if (kind === 'clear-handler-variables') {
171
+ exactKeys(statement, ['kind', 'sourceRef'], location);
172
+ return optional({ kind }, 'sourceRef', sourceRef);
173
+ }
174
+ if (kind === 'kvs-set-text') {
175
+ exactKeys(statement, ['kind', 'namespace', 'key', 'value', 'sourceRef'], location);
176
+ return optional({
177
+ kind,
178
+ namespace: parseExpression(statement.namespace, `${location}.namespace`),
179
+ key: parseExpression(statement.key, `${location}.key`),
180
+ value: parseExpression(statement.value, `${location}.value`)
181
+ }, 'sourceRef', sourceRef);
182
+ }
183
+ if (kind === 'kvs-delete') {
184
+ exactKeys(statement, ['kind', 'namespace', 'key', 'sourceRef'], location);
185
+ return optional({
186
+ kind,
187
+ namespace: parseExpression(statement.namespace, `${location}.namespace`),
188
+ key: parseExpression(statement.key, `${location}.key`)
189
+ }, 'sourceRef', sourceRef);
190
+ }
191
+ if (kind === 'record-create') {
192
+ exactKeys(statement, ['kind', 'collection', 'data', 'result', 'sourceRef'], location);
193
+ return optional({
194
+ kind,
195
+ collection: nonEmptyString(statement.collection, `${location}.collection`),
196
+ data: parseExpression(statement.data, `${location}.data`),
197
+ result: parseBinding(statement.result, `${location}.result`)
198
+ }, 'sourceRef', sourceRef);
199
+ }
200
+ if (kind === 'record-list') {
201
+ exactKeys(statement, ['kind', 'collection', 'result', 'sourceRef'], location);
202
+ return optional({
203
+ kind,
204
+ collection: nonEmptyString(statement.collection, `${location}.collection`),
205
+ result: parseBinding(statement.result, `${location}.result`)
206
+ }, 'sourceRef', sourceRef);
207
+ }
208
+ if (kind === 'record-get' || kind === 'record-delete') {
209
+ exactKeys(statement, ['kind', 'id', 'result', 'sourceRef'], location);
210
+ return optional({ kind, id: parseExpression(statement.id, `${location}.id`), result: parseBinding(statement.result, `${location}.result`) }, 'sourceRef', sourceRef);
211
+ }
212
+ if (kind === 'asset-resolve') {
213
+ exactKeys(statement, ['kind', 'locator', 'result', 'sourceRef'], location);
214
+ return optional({
215
+ kind,
216
+ locator: parseBinaryLocator(statement.locator, `${location}.locator`),
217
+ result: parseBinding(statement.result, `${location}.result`)
218
+ }, 'sourceRef', sourceRef);
219
+ }
220
+ if (kind === 'request-body-binary') {
221
+ exactKeys(statement, ['kind', 'maxBytes', 'result', 'sourceRef'], location);
222
+ return optional({
223
+ kind,
224
+ maxBytes: binaryLimit(statement.maxBytes, `${location}.maxBytes`),
225
+ result: parseBinding(statement.result, `${location}.result`)
226
+ }, 'sourceRef', sourceRef);
227
+ }
228
+ if (kind === 'asset-object-get') {
229
+ exactKeys(statement, ['kind', 'ref', 'maxBytes', 'result', 'sourceRef'], location);
230
+ return optional({
231
+ kind,
232
+ ref: parseExpression(statement.ref, `${location}.ref`),
233
+ maxBytes: binaryLimit(statement.maxBytes, `${location}.maxBytes`),
234
+ result: parseBinding(statement.result, `${location}.result`)
235
+ }, 'sourceRef', sourceRef);
236
+ }
237
+ if (kind === 'asset-object-put') {
238
+ exactKeys(statement, ['kind', 'locator', 'body', 'metadata', 'maxBytes', 'result', 'sourceRef'], location);
239
+ return optional({
240
+ kind,
241
+ locator: parseBinaryLocator(statement.locator, `${location}.locator`),
242
+ body: bindingId(statement.body, `${location}.body`),
243
+ metadata: parseBinaryMetadata(statement.metadata, `${location}.metadata`),
244
+ maxBytes: binaryLimit(statement.maxBytes, `${location}.maxBytes`),
245
+ result: parseBinding(statement.result, `${location}.result`)
246
+ }, 'sourceRef', sourceRef);
247
+ }
248
+ if (kind === 'asset-object-delete') {
249
+ exactKeys(statement, ['kind', 'target', 'result', 'sourceRef'], location);
250
+ return optional({
251
+ kind,
252
+ target: parseBinaryDeleteTarget(statement.target, `${location}.target`),
253
+ result: parseBinding(statement.result, `${location}.result`)
254
+ }, 'sourceRef', sourceRef);
255
+ }
256
+ if (kind === 'if') {
257
+ exactKeys(statement, ['kind', 'condition', 'then', 'else', 'sourceRef'], location);
258
+ const condition = parseExpression(statement.condition, `${location}.condition`);
259
+ const then = parseStatements(statement.then, `${location}.then`);
260
+ const otherwise = statement.else === undefined ? undefined : parseStatements(statement.else, `${location}.else`);
261
+ return optional(optional({ kind, condition, then }, 'else', otherwise), 'sourceRef', sourceRef);
262
+ }
263
+ if (kind === 'bounded-loop') {
264
+ exactKeys(statement, ['kind', 'maxIterations', 'body', 'sourceRef'], location);
265
+ const maxIterations = integer(statement.maxIterations, `${location}.maxIterations`);
266
+ if (maxIterations < 0 || maxIterations > 1000) {
267
+ throw new Error(`${location}.maxIterations must be from 0 to 1000.`);
268
+ }
269
+ return optional({ kind, maxIterations, body: parseStatements(statement.body, `${location}.body`) }, 'sourceRef', sourceRef);
270
+ }
271
+ if (kind === 'json-for-each') {
272
+ exactKeys(statement, ['kind', 'loopId', 'root', 'path', 'maxIterations', 'body', 'sourceRef'], location);
273
+ const maxIterations = integer(statement.maxIterations, `${location}.maxIterations`);
274
+ if (maxIterations < 1 || maxIterations > 1000) {
275
+ throw new Error(`${location}.maxIterations must be from 1 to 1000.`);
276
+ }
277
+ return optional({
278
+ kind,
279
+ loopId: bindingId(statement.loopId, `${location}.loopId`),
280
+ root: parseExpression(statement.root, `${location}.root`),
281
+ path: parsePath(statement.path, `${location}.path`),
282
+ maxIterations,
283
+ body: parseStatements(statement.body, `${location}.body`)
284
+ }, 'sourceRef', sourceRef);
285
+ }
286
+ if (kind === 'respond') {
287
+ exactKeys(statement, ['kind', 'format', 'body', 'sourceRef'], location);
288
+ const format = statement.format;
289
+ if (format !== 'text' && format !== 'html' && format !== 'json') {
290
+ throw new Error(`${location}.format is invalid.`);
291
+ }
292
+ return optional({ kind, format, body: parseExpression(statement.body, `${location}.body`) }, 'sourceRef', sourceRef);
293
+ }
294
+ if (kind === 'respond-binary') {
295
+ exactKeys(statement, ['kind', 'body', 'disposition', 'sourceRef'], location);
296
+ const disposition = statement.disposition === undefined
297
+ ? undefined
298
+ : parseBinaryDisposition(statement.disposition, `${location}.disposition`);
299
+ return optional(optional({ kind, body: bindingId(statement.body, `${location}.body`) }, 'disposition', disposition), 'sourceRef', sourceRef);
300
+ }
301
+ if (kind === 'respond-named-body') {
302
+ exactKeys(statement, ['kind', 'reference', 'representation', 'targetId', 'maxBytes', 'sourceRef'], location);
303
+ const referenceValue = object(statement.reference, `${location}.reference`);
304
+ exactKeys(referenceValue, ['namespace', 'name', 'kind', 'scope'], `${location}.reference`);
305
+ const namespace = nonEmptyString(referenceValue.namespace, `${location}.reference.namespace`);
306
+ if (!isNamedDataNamespace(namespace))
307
+ throw new Error(`${location}.reference.namespace is invalid.`);
308
+ const name = nonEmptyString(referenceValue.name, `${location}.reference.name`);
309
+ if (name.length > 256 || [...name].some(isControlCharacter)) {
310
+ throw new Error(`${location}.reference.name is invalid.`);
311
+ }
312
+ const dataKind = referenceValue.kind;
313
+ if (dataKind !== 'structured' && dataKind !== 'document' && dataKind !== 'binary' && dataKind !== 'asset') {
314
+ throw new Error(`${location}.reference.kind is invalid.`);
315
+ }
316
+ const scope = referenceValue.scope;
317
+ if (scope !== 'target' && scope !== 'project')
318
+ throw new Error(`${location}.reference.scope is invalid.`);
319
+ const representation = statement.representation;
320
+ if (representation !== 'json' &&
321
+ representation !== 'yaml' &&
322
+ representation !== 'html' &&
323
+ representation !== 'markdown' &&
324
+ representation !== 'raw') {
325
+ throw new Error(`${location}.representation is invalid.`);
326
+ }
327
+ const targetId = optionalString(statement.targetId, `${location}.targetId`);
328
+ if (scope === 'target' && (targetId === undefined || !NAMED_TARGET_ID.test(targetId))) {
329
+ throw new Error(`${location}.targetId is required for target scope.`);
330
+ }
331
+ if (scope === 'project' && targetId !== undefined) {
332
+ throw new Error(`${location}.targetId is not allowed for project scope.`);
333
+ }
334
+ return optional(optional({
335
+ kind,
336
+ reference: { namespace, name, kind: dataKind, scope },
337
+ representation,
338
+ maxBytes: binaryLimit(statement.maxBytes, `${location}.maxBytes`)
339
+ }, 'targetId', targetId), 'sourceRef', sourceRef);
340
+ }
341
+ throw new Error(`${location}.kind is unsupported: ${kind}`);
342
+ }
343
+ function parseStatements(value, location) {
344
+ return array(value, location).map((item, index) => parseStatement(item, `${location}[${index}]`));
345
+ }
346
+ function parseExpression(value, location) {
347
+ const expression = object(value, location);
348
+ const kind = nonEmptyString(expression.kind, `${location}.kind`);
349
+ const sourceRef = optionalSourceRef(expression.sourceRef, `${location}.sourceRef`);
350
+ if (kind === 'literal') {
351
+ exactKeys(expression, ['kind', 'valueType', 'value', 'sourceRef'], location);
352
+ const valueType = atomicType(expression.valueType, `${location}.valueType`);
353
+ if (valueType === 'binary-ref') {
354
+ const binaryRef = parseBinaryRef(expression.value, `${location}.value`);
355
+ return optional({ kind, valueType, value: binaryRef }, 'sourceRef', sourceRef);
356
+ }
357
+ const literal = jsonValue(expression.value, `${location}.value`);
358
+ assertLiteralType(valueType, literal, location);
359
+ return optional({ kind, valueType, value: literal }, 'sourceRef', sourceRef);
360
+ }
361
+ if (kind === 'request') {
362
+ exactKeys(expression, ['kind', 'valueType', 'source', 'sourceRef'], location);
363
+ if (expression.valueType !== 'string')
364
+ throw new Error(`${location}.valueType must be string.`);
365
+ const source = expression.source;
366
+ if (!['method', 'path', 'url', 'body-text', 'content-type', 'client-address'].includes(String(source))) {
367
+ throw new Error(`${location}.source is unsupported.`);
368
+ }
369
+ return optional({ kind, valueType: 'string', source: source }, 'sourceRef', sourceRef);
370
+ }
371
+ if (kind === 'request-value') {
372
+ exactKeys(expression, ['kind', 'valueType', 'source', 'name', 'sourceRef'], location);
373
+ if (expression.valueType !== 'string')
374
+ throw new Error(`${location}.valueType must be string.`);
375
+ const source = expression.source;
376
+ if (source !== 'query' && source !== 'path-param' && source !== 'header') {
377
+ throw new Error(`${location}.source is unsupported.`);
378
+ }
379
+ return optional({ kind, valueType: 'string', source, name: nonEmptyString(expression.name, `${location}.name`) }, 'sourceRef', sourceRef);
380
+ }
381
+ if (kind === 'concat') {
382
+ exactKeys(expression, ['kind', 'valueType', 'left', 'right', 'sourceRef'], location);
383
+ if (expression.valueType !== 'string')
384
+ throw new Error(`${location}.valueType must be string.`);
385
+ return optional({
386
+ kind,
387
+ valueType: 'string',
388
+ left: parseExpression(expression.left, `${location}.left`),
389
+ right: parseExpression(expression.right, `${location}.right`)
390
+ }, 'sourceRef', sourceRef);
391
+ }
392
+ if (kind === 'handler-variable') {
393
+ exactKeys(expression, ['kind', 'valueType', 'name', 'sourceRef'], location);
394
+ const valueType = parseValueType(expression.valueType, `${location}.valueType`);
395
+ if (!isStringNumberUnion(valueType))
396
+ throw new Error(`${location}.valueType must be the number|string union.`);
397
+ return optional({ kind, valueType: { kind: 'union', members: ['number', 'string'] }, name: parseExpression(expression.name, `${location}.name`) }, 'sourceRef', sourceRef);
398
+ }
399
+ if (kind === 'handler-variable-exists') {
400
+ exactKeys(expression, ['kind', 'valueType', 'name', 'sourceRef'], location);
401
+ if (expression.valueType !== 'boolean')
402
+ throw new Error(`${location}.valueType must be boolean.`);
403
+ return optional({ kind, valueType: 'boolean', name: parseExpression(expression.name, `${location}.name`) }, 'sourceRef', sourceRef);
404
+ }
405
+ if (kind === 'handler-variable-names') {
406
+ exactKeys(expression, ['kind', 'valueType', 'sourceRef'], location);
407
+ if (expression.valueType !== 'string')
408
+ throw new Error(`${location}.valueType must be string.`);
409
+ return optional({ kind, valueType: 'string' }, 'sourceRef', sourceRef);
410
+ }
411
+ if (kind === 'kvs-get-text' || kind === 'kvs-has') {
412
+ exactKeys(expression, ['kind', 'valueType', 'namespace', 'key', 'sourceRef'], location);
413
+ const valueType = kind === 'kvs-get-text' ? 'string' : 'boolean';
414
+ requireValueType(expression.valueType, valueType, `${location}.valueType`);
415
+ const namespace = parseExpression(expression.namespace, `${location}.namespace`);
416
+ const key = parseExpression(expression.key, `${location}.key`);
417
+ return kind === 'kvs-get-text'
418
+ ? optional({ kind, valueType: 'string', namespace, key }, 'sourceRef', sourceRef)
419
+ : optional({ kind, valueType: 'boolean', namespace, key }, 'sourceRef', sourceRef);
420
+ }
421
+ if (kind === 'kvs-list-keys') {
422
+ exactKeys(expression, ['kind', 'valueType', 'namespace', 'sourceRef'], location);
423
+ requireValueType(expression.valueType, 'json-text', `${location}.valueType`);
424
+ return optional({ kind, valueType: 'json-text', namespace: parseExpression(expression.namespace, `${location}.namespace`) }, 'sourceRef', sourceRef);
425
+ }
426
+ if (kind === 'binding') {
427
+ exactKeys(expression, ['kind', 'valueType', 'binding', 'sourceRef'], location);
428
+ return optional({
429
+ kind,
430
+ valueType: parseValueType(expression.valueType, `${location}.valueType`),
431
+ binding: bindingId(expression.binding, `${location}.binding`)
432
+ }, 'sourceRef', sourceRef);
433
+ }
434
+ if (kind === 'json-text-coerce') {
435
+ exactKeys(expression, ['kind', 'valueType', 'input', 'sourceRef'], location);
436
+ requireValueType(expression.valueType, 'json-text', `${location}.valueType`);
437
+ return optional({ kind, valueType: 'json-text', input: parseExpression(expression.input, `${location}.input`) }, 'sourceRef', sourceRef);
438
+ }
439
+ if (kind === 'json-parse') {
440
+ exactKeys(expression, ['kind', 'valueType', 'text', 'sourceRef'], location);
441
+ requireJsonValueType(expression.valueType, `${location}.valueType`);
442
+ return optional({ kind, valueType: JSON_VALUE_TYPE_V2, text: parseExpression(expression.text, `${location}.text`) }, 'sourceRef', sourceRef);
443
+ }
444
+ if (kind === 'json-stringify') {
445
+ exactKeys(expression, ['kind', 'valueType', 'value', 'sourceRef'], location);
446
+ requireValueType(expression.valueType, 'json-text', `${location}.valueType`);
447
+ return optional({ kind, valueType: 'json-text', value: parseExpression(expression.value, `${location}.value`) }, 'sourceRef', sourceRef);
448
+ }
449
+ if (kind === 'json-is-valid') {
450
+ exactKeys(expression, ['kind', 'valueType', 'text', 'sourceRef'], location);
451
+ requireValueType(expression.valueType, 'boolean', `${location}.valueType`);
452
+ return optional({ kind, valueType: 'boolean', text: parseExpression(expression.text, `${location}.text`) }, 'sourceRef', sourceRef);
453
+ }
454
+ if (kind === 'json-get' ||
455
+ kind === 'json-has' ||
456
+ kind === 'json-delete' ||
457
+ kind === 'json-keys' ||
458
+ kind === 'json-length') {
459
+ exactKeys(expression, ['kind', 'valueType', 'root', 'path', 'sourceRef'], location);
460
+ const root = parseExpression(expression.root, `${location}.root`);
461
+ const path = parsePath(expression.path, `${location}.path`);
462
+ if (kind === 'json-get' || kind === 'json-delete') {
463
+ requireJsonValueType(expression.valueType, `${location}.valueType`);
464
+ return optional({ kind, valueType: JSON_VALUE_TYPE_V2, root, path }, 'sourceRef', sourceRef);
465
+ }
466
+ if (kind === 'json-has') {
467
+ requireValueType(expression.valueType, 'boolean', `${location}.valueType`);
468
+ return optional({ kind, valueType: 'boolean', root, path }, 'sourceRef', sourceRef);
469
+ }
470
+ if (kind === 'json-keys') {
471
+ requireValueType(expression.valueType, 'json-array', `${location}.valueType`);
472
+ return optional({ kind, valueType: 'json-array', root, path }, 'sourceRef', sourceRef);
473
+ }
474
+ requireValueType(expression.valueType, 'number', `${location}.valueType`);
475
+ return optional({ kind, valueType: 'number', root, path }, 'sourceRef', sourceRef);
476
+ }
477
+ if (kind === 'json-set') {
478
+ exactKeys(expression, ['kind', 'valueType', 'root', 'path', 'value', 'sourceRef'], location);
479
+ requireJsonValueType(expression.valueType, `${location}.valueType`);
480
+ return optional({
481
+ kind,
482
+ valueType: JSON_VALUE_TYPE_V2,
483
+ root: parseExpression(expression.root, `${location}.root`),
484
+ path: parsePath(expression.path, `${location}.path`),
485
+ value: parseExpression(expression.value, `${location}.value`)
486
+ }, 'sourceRef', sourceRef);
487
+ }
488
+ if (kind === 'iteration-key' || kind === 'iteration-index' || kind === 'iteration-value') {
489
+ exactKeys(expression, ['kind', 'valueType', 'loopId', 'sourceRef'], location);
490
+ const loopId = bindingId(expression.loopId, `${location}.loopId`);
491
+ if (kind === 'iteration-key') {
492
+ requireValueType(expression.valueType, 'string', `${location}.valueType`);
493
+ return optional({ kind, valueType: 'string', loopId }, 'sourceRef', sourceRef);
494
+ }
495
+ if (kind === 'iteration-index') {
496
+ requireValueType(expression.valueType, 'number', `${location}.valueType`);
497
+ return optional({ kind, valueType: 'number', loopId }, 'sourceRef', sourceRef);
498
+ }
499
+ requireJsonValueType(expression.valueType, `${location}.valueType`);
500
+ return optional({ kind, valueType: JSON_VALUE_TYPE_V2, loopId }, 'sourceRef', sourceRef);
501
+ }
502
+ throw new Error(`${location}.kind is unsupported: ${kind}`);
503
+ }
504
+ function parseBinding(value, location) {
505
+ const binding = object(value, location);
506
+ exactKeys(binding, ['id', 'type'], location);
507
+ return { id: bindingId(binding.id, `${location}.id`), type: parseBindingType(binding.type, `${location}.type`) };
508
+ }
509
+ function parseBindingType(value, location) {
510
+ const type = object(value, location);
511
+ const kind = nonEmptyString(type.kind, `${location}.kind`);
512
+ if (kind === 'value') {
513
+ exactKeys(type, ['kind', 'valueType'], location);
514
+ return { kind, valueType: parseValueType(type.valueType, `${location}.valueType`) };
515
+ }
516
+ if (kind === 'resource') {
517
+ exactKeys(type, ['kind', 'resourceType'], location);
518
+ if (type.resourceType !== 'binary-body')
519
+ throw new Error(`${location}.resourceType is unsupported.`);
520
+ return { kind, resourceType: 'binary-body' };
521
+ }
522
+ throw new Error(`${location}.kind is unsupported: ${kind}`);
523
+ }
524
+ function parseValueType(value, location) {
525
+ if (typeof value === 'string')
526
+ return atomicType(value, location);
527
+ const type = object(value, location);
528
+ exactKeys(type, ['kind', 'members'], location);
529
+ if (type.kind !== 'union')
530
+ throw new Error(`${location}.kind must be union.`);
531
+ const members = array(type.members, `${location}.members`).map((member, index) => atomicType(member, `${location}.members[${index}]`));
532
+ if (members.length < 2 || new Set(members).size !== members.length) {
533
+ throw new Error(`${location}.members must contain at least two unique atomic types.`);
534
+ }
535
+ return {
536
+ kind: 'union',
537
+ members: members.sort((left, right) => ATOMIC_TYPE_ORDER.indexOf(left) - ATOMIC_TYPE_ORDER.indexOf(right))
538
+ };
539
+ }
540
+ function requireValueType(value, expected, location) {
541
+ if (value !== expected)
542
+ throw new Error(`${location} must be ${expected}.`);
543
+ }
544
+ function requireJsonValueType(value, location) {
545
+ const parsed = parseValueType(value, location);
546
+ if (JSON.stringify(parsed) !== JSON.stringify(JSON_VALUE_TYPE_V2)) {
547
+ throw new Error(`${location} must be the canonical JSON value union.`);
548
+ }
549
+ }
550
+ function parsePath(value, location) {
551
+ return array(value, location).map((item, index) => {
552
+ const segmentLocation = `${location}[${index}]`;
553
+ const segment = object(item, segmentLocation);
554
+ exactKeys(segment, ['kind', 'value'], segmentLocation);
555
+ if (segment.kind === 'key') {
556
+ if (typeof segment.value !== 'string')
557
+ throw new Error(`${segmentLocation}.value must be a string.`);
558
+ return { kind: 'key', value: segment.value };
559
+ }
560
+ if (segment.kind === 'index') {
561
+ const indexValue = integer(segment.value, `${segmentLocation}.value`);
562
+ if (indexValue < 0)
563
+ throw new Error(`${segmentLocation}.value must not be negative.`);
564
+ return { kind: 'index', value: indexValue };
565
+ }
566
+ throw new Error(`${segmentLocation}.kind is unsupported: ${String(segment.kind)}`);
567
+ });
568
+ }
569
+ function normalizeCapabilities(capabilities) {
570
+ const entries = capabilities.map((capability) => [JSON.stringify(capability), capability]);
571
+ if (new Set(entries.map(([key]) => key)).size !== entries.length) {
572
+ throw new Error('IR.capabilities must not contain duplicates.');
573
+ }
574
+ return entries
575
+ .sort(([left], [right]) => (left < right ? -1 : left > right ? 1 : 0))
576
+ .map(([, capability]) => capability);
577
+ }
578
+ function optionalSourceRef(value, location) {
579
+ if (value === undefined)
580
+ return undefined;
581
+ const source = object(value, location);
582
+ exactKeys(source, ['targetIndex', 'targetName', 'blockId', 'opcode', 'input'], location);
583
+ const result = {
584
+ targetIndex: integer(source.targetIndex, `${location}.targetIndex`),
585
+ targetName: nonEmptyString(source.targetName, `${location}.targetName`),
586
+ blockId: nonEmptyString(source.blockId, `${location}.blockId`),
587
+ opcode: nonEmptyString(source.opcode, `${location}.opcode`)
588
+ };
589
+ if (result.targetIndex < 0)
590
+ throw new Error(`${location}.targetIndex must not be negative.`);
591
+ const input = source.input === undefined ? undefined : nonEmptyString(source.input, `${location}.input`);
592
+ return optional(result, 'input', input);
593
+ }
594
+ function jsonValue(value, location) {
595
+ if (value === null || typeof value === 'boolean' || typeof value === 'string')
596
+ return value;
597
+ if (typeof value === 'number') {
598
+ if (!Number.isFinite(value))
599
+ throw new Error(`${location} must be a finite JSON number.`);
600
+ return Object.is(value, -0) ? 0 : value;
601
+ }
602
+ if (Array.isArray(value))
603
+ return value.map((item, index) => jsonValue(item, `${location}[${index}]`));
604
+ const record = object(value, location);
605
+ return Object.fromEntries(Object.entries(record).map(([key, item]) => [key, jsonValue(item, `${location}.${key}`)]));
606
+ }
607
+ function parseBinaryRef(value, location) {
608
+ const descriptor = object(value, location);
609
+ exactKeys(descriptor, ['namespace', 'key', 'contentType', 'size', 'integrity', 'revision'], location);
610
+ return {
611
+ ...parseBinaryLocator(descriptor, location, true),
612
+ ...parseBinaryMetadata(descriptor, location, true)
613
+ };
614
+ }
615
+ function parseBinaryLocator(value, location, includesMetadata = false) {
616
+ const locator = object(value, location);
617
+ exactKeys(locator, includesMetadata
618
+ ? ['namespace', 'key', 'contentType', 'size', 'integrity', 'revision']
619
+ : ['namespace', 'key'], location);
620
+ const namespace = nonEmptyString(locator.namespace, `${location}.namespace`);
621
+ if (!isNamedDataNamespace(namespace)) {
622
+ throw new Error(`${location}.namespace must be a logical namespace identifier of at most 64 characters.`);
623
+ }
624
+ const key = nonEmptyString(locator.key, `${location}.key`);
625
+ if (key.length > 512 || key.includes('\0') || key.includes('\\') || key.startsWith('/')) {
626
+ throw new Error(`${location}.key is not a safe logical key.`);
627
+ }
628
+ const segments = key.split('/');
629
+ if (segments.some((segment) => segment.length === 0 || segment === '.' || segment === '..')) {
630
+ throw new Error(`${location}.key contains an unsafe path segment.`);
631
+ }
632
+ return { namespace, key };
633
+ }
634
+ function parseBinaryMetadata(value, location, includesLocator = false) {
635
+ const metadata = object(value, location);
636
+ exactKeys(metadata, includesLocator
637
+ ? ['namespace', 'key', 'contentType', 'size', 'integrity', 'revision']
638
+ : ['contentType', 'size', 'integrity', 'revision'], location);
639
+ const contentType = optionalString(metadata.contentType, `${location}.contentType`);
640
+ if (contentType !== undefined && (contentType.length > 255 || !/^[^\s/;]+\/[^\s;]+(?:\s*;.*)?$/u.test(contentType) || /[\r\n]/u.test(contentType))) {
641
+ throw new Error(`${location}.contentType must be a safe media type.`);
642
+ }
643
+ const size = metadata.size === undefined ? undefined : integer(metadata.size, `${location}.size`);
644
+ if (size !== undefined && size < 0)
645
+ throw new Error(`${location}.size must not be negative.`);
646
+ const integrityValue = optionalString(metadata.integrity, `${location}.integrity`);
647
+ if (integrityValue !== undefined && !/^sha256:[0-9a-f]{64}$/.test(integrityValue)) {
648
+ throw new Error(`${location}.integrity must use sha256:<64 lowercase hexadecimal characters>.`);
649
+ }
650
+ const integrity = integrityValue;
651
+ const revision = optionalString(metadata.revision, `${location}.revision`);
652
+ if (revision !== undefined && revision.length > 256)
653
+ throw new Error(`${location}.revision is too long.`);
654
+ return optional(optional(optional(optional({}, 'contentType', contentType), 'size', size), 'integrity', integrity), 'revision', revision);
655
+ }
656
+ function parseBinaryDeleteTarget(value, location) {
657
+ const target = object(value, location);
658
+ const kind = nonEmptyString(target.kind, `${location}.kind`);
659
+ if (kind === 'ref') {
660
+ exactKeys(target, ['kind', 'ref'], location);
661
+ return { kind, ref: parseExpression(target.ref, `${location}.ref`) };
662
+ }
663
+ if (kind === 'locator') {
664
+ exactKeys(target, ['kind', 'locator'], location);
665
+ return { kind, locator: parseBinaryLocator(target.locator, `${location}.locator`) };
666
+ }
667
+ throw new Error(`${location}.kind is unsupported: ${kind}`);
668
+ }
669
+ function parseBinaryDisposition(value, location) {
670
+ const disposition = object(value, location);
671
+ const kind = nonEmptyString(disposition.kind, `${location}.kind`);
672
+ if (kind === 'inline') {
673
+ exactKeys(disposition, ['kind'], location);
674
+ return { kind };
675
+ }
676
+ if (kind === 'attachment') {
677
+ exactKeys(disposition, ['kind', 'filename'], location);
678
+ const filename = nonEmptyString(disposition.filename, `${location}.filename`);
679
+ if (filename.length > 255 || /[\r\n\0]/u.test(filename))
680
+ throw new Error(`${location}.filename is unsafe.`);
681
+ return { kind, filename };
682
+ }
683
+ throw new Error(`${location}.kind is unsupported: ${kind}`);
684
+ }
685
+ function binaryLimit(value, location) {
686
+ const limit = integer(value, location);
687
+ if (limit < 1 || limit > 16 * 1024 * 1024)
688
+ throw new Error(`${location} must be from 1 to 16777216.`);
689
+ return limit;
690
+ }
691
+ function assertLiteralType(type, value, location) {
692
+ const actual = value === null ? 'null' : Array.isArray(value) ? 'json-array' : typeof value === 'object' ? 'json-object' : typeof value;
693
+ if (type === 'json-text') {
694
+ if (actual !== 'string')
695
+ throw new Error(`${location}.value must be a JSON text string.`);
696
+ return;
697
+ }
698
+ if (type !== actual)
699
+ throw new Error(`${location}.valueType ${type} does not match ${actual}.`);
700
+ }
701
+ function isStringNumberUnion(type) {
702
+ return typeof type !== 'string' && type.members.length === 2 && type.members[0] === 'number' && type.members[1] === 'string';
703
+ }
704
+ function atomicType(value, location) {
705
+ if (typeof value !== 'string' || !ATOMIC_TYPES.has(value)) {
706
+ throw new Error(`${location} is not an atomic IR value type.`);
707
+ }
708
+ return value;
709
+ }
710
+ function bindingId(value, location) {
711
+ const id = nonEmptyString(value, location);
712
+ if (!BINDING_ID.test(id))
713
+ throw new Error(`${location} is not a valid binding ID.`);
714
+ return id;
715
+ }
716
+ function object(value, location) {
717
+ if (typeof value !== 'object' || value === null || Array.isArray(value)) {
718
+ throw new Error(`${location} must be an object.`);
719
+ }
720
+ return value;
721
+ }
722
+ function array(value, location) {
723
+ if (!Array.isArray(value))
724
+ throw new Error(`${location} must be an array.`);
725
+ return value;
726
+ }
727
+ function nonEmptyString(value, location) {
728
+ if (typeof value !== 'string' || value.length === 0)
729
+ throw new Error(`${location} must be a non-empty string.`);
730
+ return value;
731
+ }
732
+ function optionalString(value, location) {
733
+ return value === undefined ? undefined : nonEmptyString(value, location);
734
+ }
735
+ function integer(value, location) {
736
+ if (typeof value !== 'number' || !Number.isSafeInteger(value))
737
+ throw new Error(`${location} must be a safe integer.`);
738
+ return value;
739
+ }
740
+ function isControlCharacter(character) {
741
+ const codePoint = character.codePointAt(0);
742
+ return codePoint <= 0x1f || codePoint === 0x7f;
743
+ }
744
+ function exactKeys(record, allowed, location) {
745
+ const allowedSet = new Set(allowed);
746
+ const unknown = Object.keys(record).filter((key) => !allowedSet.has(key));
747
+ if (unknown.length > 0)
748
+ throw new Error(`${location} contains unknown field: ${unknown.sort()[0]}`);
749
+ }
750
+ function optional(base, key, value) {
751
+ return value === undefined ? base : { ...base, [key]: value };
752
+ }
753
+ //# sourceMappingURL=parse.js.map