@distilled.cloud/core 1.0.0-rc.17 → 1.0.0-rc.19

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 (314) hide show
  1. package/lib/_bundle/base.js +2 -2
  2. package/lib/_bundle/base.js.map +1 -1
  3. package/lib/_bundle/graphql.js +2 -2
  4. package/lib/_bundle/graphql.js.map +1 -1
  5. package/lib/_bundle/grpc.js +2 -0
  6. package/lib/_bundle/grpc.js.map +1 -0
  7. package/lib/_bundle/rest.js +1 -1
  8. package/lib/_bundle/rest.js.map +1 -1
  9. package/lib/_bundle/xml.js +1 -1
  10. package/lib/_bundle/xml.js.map +1 -1
  11. package/lib/annotations.d.ts +17 -0
  12. package/lib/annotations.d.ts.map +1 -0
  13. package/lib/annotations.js +2 -0
  14. package/lib/annotations.js.map +1 -0
  15. package/lib/api.d.ts +52 -1
  16. package/lib/api.d.ts.map +1 -1
  17. package/lib/api.js +1 -1
  18. package/lib/category.d.ts.map +1 -1
  19. package/lib/config.d.ts +54 -13
  20. package/lib/config.d.ts.map +1 -1
  21. package/lib/config.js +1 -1
  22. package/lib/error-category.d.ts.map +1 -1
  23. package/lib/errors.d.ts +3 -13
  24. package/lib/errors.d.ts.map +1 -1
  25. package/lib/errors.js +1 -1
  26. package/lib/graphql.d.ts +26 -5
  27. package/lib/graphql.d.ts.map +1 -1
  28. package/lib/graphql.types.d.ts.map +1 -1
  29. package/lib/grpc-transport-cloudflare-http2.d.ts +7 -0
  30. package/lib/grpc-transport-cloudflare-http2.d.ts.map +1 -0
  31. package/lib/grpc-transport-cloudflare-http2.js +6 -0
  32. package/lib/grpc-transport-cloudflare-http2.js.map +1 -0
  33. package/lib/grpc-transport-cloudflare-sockets.d.ts +7 -0
  34. package/lib/grpc-transport-cloudflare-sockets.d.ts.map +1 -0
  35. package/lib/grpc-transport-cloudflare-sockets.js +5 -0
  36. package/lib/grpc-transport-cloudflare-sockets.js.map +1 -0
  37. package/lib/grpc-transport-node-http2.d.ts +19 -0
  38. package/lib/grpc-transport-node-http2.d.ts.map +1 -0
  39. package/lib/grpc-transport-node-http2.js +2 -0
  40. package/lib/grpc-transport-node-http2.js.map +1 -0
  41. package/lib/grpc-transport.d.ts +76 -0
  42. package/lib/grpc-transport.d.ts.map +1 -0
  43. package/lib/grpc-transport.js +1 -0
  44. package/lib/hpack.d.ts +34 -0
  45. package/lib/hpack.d.ts.map +1 -0
  46. package/lib/hpack.js +2 -0
  47. package/lib/hpack.js.map +1 -0
  48. package/lib/index.d.ts +6 -2
  49. package/lib/index.d.ts.map +1 -1
  50. package/lib/index.js +4 -0
  51. package/lib/pagination.d.ts +55 -3
  52. package/lib/pagination.d.ts.map +1 -1
  53. package/lib/pagination.js +1 -1
  54. package/lib/protobuf.d.ts +92 -0
  55. package/lib/protobuf.d.ts.map +1 -0
  56. package/lib/protobuf.js +1 -0
  57. package/lib/protocol-grpc.d.ts +86 -0
  58. package/lib/protocol-grpc.d.ts.map +1 -0
  59. package/lib/protocol-grpc.js +1 -0
  60. package/lib/protocol-http.d.ts +38 -9
  61. package/lib/protocol-http.d.ts.map +1 -1
  62. package/lib/protocol-http.js +1 -1
  63. package/lib/protocol-rest.d.ts +60 -42
  64. package/lib/protocol-rest.d.ts.map +1 -1
  65. package/lib/protocol-rest.js +1 -1
  66. package/lib/response-validation.d.ts +5 -4
  67. package/lib/response-validation.d.ts.map +1 -1
  68. package/lib/schema-lite-typed.js +1 -2
  69. package/lib/sdk-error.d.ts +27 -0
  70. package/lib/sdk-error.d.ts.map +1 -0
  71. package/lib/sdk-error.js +1 -0
  72. package/lib/sensitive.d.ts +28 -0
  73. package/lib/sensitive.d.ts.map +1 -0
  74. package/lib/sensitive.js +2 -0
  75. package/lib/sensitive.js.map +1 -0
  76. package/lib/telemetry-debug.d.ts +6 -0
  77. package/lib/telemetry-debug.d.ts.map +1 -0
  78. package/lib/telemetry-debug.js +2 -0
  79. package/lib/telemetry-debug.js.map +1 -0
  80. package/lib/telemetry-hooks.d.ts +32 -0
  81. package/lib/telemetry-hooks.d.ts.map +1 -0
  82. package/lib/telemetry-hooks.js +2 -0
  83. package/lib/telemetry-hooks.js.map +1 -0
  84. package/lib/telemetry-host-rules.d.ts +23 -0
  85. package/lib/telemetry-host-rules.d.ts.map +1 -0
  86. package/lib/telemetry-host-rules.js +2 -0
  87. package/lib/telemetry-host-rules.js.map +1 -0
  88. package/lib/telemetry-lambda-context.d.ts +17 -0
  89. package/lib/telemetry-lambda-context.d.ts.map +1 -0
  90. package/lib/telemetry-lambda-context.js +2 -0
  91. package/lib/telemetry-lambda-context.js.map +1 -0
  92. package/lib/telemetry-message-context.d.ts +52 -0
  93. package/lib/telemetry-message-context.d.ts.map +1 -0
  94. package/lib/telemetry-message-context.js +2 -0
  95. package/lib/telemetry-message-context.js.map +1 -0
  96. package/lib/telemetry-metrics.d.ts +6 -0
  97. package/lib/telemetry-metrics.d.ts.map +1 -0
  98. package/lib/telemetry-metrics.js +2 -0
  99. package/lib/telemetry-metrics.js.map +1 -0
  100. package/lib/telemetry-name-template.d.ts +13 -0
  101. package/lib/telemetry-name-template.d.ts.map +1 -0
  102. package/lib/telemetry-name-template.js +2 -0
  103. package/lib/telemetry-name-template.js.map +1 -0
  104. package/lib/telemetry-stream.d.ts +11 -0
  105. package/lib/telemetry-stream.d.ts.map +1 -0
  106. package/lib/telemetry-stream.js +2 -0
  107. package/lib/telemetry-stream.js.map +1 -0
  108. package/lib/telemetry-xray.d.ts +28 -0
  109. package/lib/telemetry-xray.d.ts.map +1 -0
  110. package/lib/telemetry-xray.js +2 -0
  111. package/lib/telemetry-xray.js.map +1 -0
  112. package/lib/telemetry.d.ts +288 -0
  113. package/lib/telemetry.d.ts.map +1 -0
  114. package/lib/telemetry.js +3 -0
  115. package/lib/telemetry.js.map +1 -0
  116. package/lib/trait.d.ts +7 -0
  117. package/lib/trait.d.ts.map +1 -1
  118. package/lib/trait.js +1 -1
  119. package/lib/xml.d.ts.map +1 -1
  120. package/package.json +3 -5
  121. package/src/annotations.ts +53 -0
  122. package/src/api.ts +316 -43
  123. package/src/category.ts +21 -22
  124. package/src/config.ts +58 -29
  125. package/src/error-category.ts +12 -14
  126. package/src/errors.ts +11 -7
  127. package/src/graphql.ts +191 -63
  128. package/src/graphql.types.ts +5 -1
  129. package/src/grpc-transport-cloudflare-http2.ts +494 -0
  130. package/src/grpc-transport-cloudflare-sockets.ts +299 -0
  131. package/src/grpc-transport-node-http2.ts +234 -0
  132. package/src/grpc-transport.ts +118 -0
  133. package/src/hpack.ts +331 -0
  134. package/src/index.ts +6 -2
  135. package/src/pagination.ts +193 -8
  136. package/src/protobuf.ts +1332 -0
  137. package/src/protocol-grpc.ts +641 -0
  138. package/src/protocol-http.ts +87 -67
  139. package/src/protocol-rest.ts +183 -191
  140. package/src/response-validation.ts +8 -5
  141. package/src/sdk-error.ts +33 -0
  142. package/src/sensitive.ts +154 -0
  143. package/src/telemetry-debug.ts +179 -0
  144. package/src/telemetry-hooks.ts +33 -0
  145. package/src/telemetry-host-rules.ts +39 -0
  146. package/src/telemetry-lambda-context.ts +46 -0
  147. package/src/telemetry-message-context.ts +170 -0
  148. package/src/telemetry-metrics.ts +69 -0
  149. package/src/telemetry-name-template.ts +26 -0
  150. package/src/telemetry-stream.ts +100 -0
  151. package/src/telemetry-xray.ts +66 -0
  152. package/src/telemetry.ts +832 -0
  153. package/src/trait.ts +10 -0
  154. package/src/xml.ts +10 -1
  155. package/lib/codegen/boolean-string-enums.d.ts +0 -36
  156. package/lib/codegen/boolean-string-enums.d.ts.map +0 -1
  157. package/lib/codegen/boolean-string-enums.js +0 -147
  158. package/lib/codegen/boolean-string-enums.js.map +0 -1
  159. package/lib/codegen/chunk.d.ts +0 -7
  160. package/lib/codegen/chunk.d.ts.map +0 -1
  161. package/lib/codegen/chunk.js +0 -139
  162. package/lib/codegen/chunk.js.map +0 -1
  163. package/lib/codegen/cli.d.ts +0 -80
  164. package/lib/codegen/cli.d.ts.map +0 -1
  165. package/lib/codegen/cli.js +0 -166
  166. package/lib/codegen/cli.js.map +0 -1
  167. package/lib/codegen/datafy.d.ts +0 -14
  168. package/lib/codegen/datafy.d.ts.map +0 -1
  169. package/lib/codegen/datafy.js +0 -853
  170. package/lib/codegen/datafy.js.map +0 -1
  171. package/lib/codegen/dedupe.d.ts +0 -5
  172. package/lib/codegen/dedupe.d.ts.map +0 -1
  173. package/lib/codegen/dedupe.js +0 -153
  174. package/lib/codegen/dedupe.js.map +0 -1
  175. package/lib/codegen/emit.d.ts +0 -125
  176. package/lib/codegen/emit.d.ts.map +0 -1
  177. package/lib/codegen/emit.js +0 -99
  178. package/lib/codegen/emit.js.map +0 -1
  179. package/lib/codegen/format.d.ts +0 -11
  180. package/lib/codegen/format.d.ts.map +0 -1
  181. package/lib/codegen/format.js +0 -32
  182. package/lib/codegen/format.js.map +0 -1
  183. package/lib/codegen/generated-at.d.ts +0 -29
  184. package/lib/codegen/generated-at.d.ts.map +0 -1
  185. package/lib/codegen/generated-at.js +0 -78
  186. package/lib/codegen/generated-at.js.map +0 -1
  187. package/lib/codegen/generator.d.ts +0 -353
  188. package/lib/codegen/generator.d.ts.map +0 -1
  189. package/lib/codegen/generator.js +0 -810
  190. package/lib/codegen/generator.js.map +0 -1
  191. package/lib/codegen/graph.d.ts +0 -36
  192. package/lib/codegen/graph.d.ts.map +0 -1
  193. package/lib/codegen/graph.js +0 -136
  194. package/lib/codegen/graph.js.map +0 -1
  195. package/lib/codegen/graphql-client.d.ts +0 -70
  196. package/lib/codegen/graphql-client.d.ts.map +0 -1
  197. package/lib/codegen/graphql-client.js +0 -439
  198. package/lib/codegen/graphql-client.js.map +0 -1
  199. package/lib/codegen/graphql.d.ts +0 -207
  200. package/lib/codegen/graphql.d.ts.map +0 -1
  201. package/lib/codegen/graphql.js +0 -786
  202. package/lib/codegen/graphql.js.map +0 -1
  203. package/lib/codegen/latin1.d.ts +0 -3
  204. package/lib/codegen/latin1.d.ts.map +0 -1
  205. package/lib/codegen/latin1.js +0 -126
  206. package/lib/codegen/latin1.js.map +0 -1
  207. package/lib/codegen/members.d.ts +0 -32
  208. package/lib/codegen/members.d.ts.map +0 -1
  209. package/lib/codegen/members.js +0 -62
  210. package/lib/codegen/members.js.map +0 -1
  211. package/lib/codegen/model-diff.d.ts +0 -10
  212. package/lib/codegen/model-diff.d.ts.map +0 -1
  213. package/lib/codegen/model-diff.js +0 -67
  214. package/lib/codegen/model-diff.js.map +0 -1
  215. package/lib/codegen/naming.d.ts +0 -29
  216. package/lib/codegen/naming.d.ts.map +0 -1
  217. package/lib/codegen/naming.js +0 -74
  218. package/lib/codegen/naming.js.map +0 -1
  219. package/lib/codegen/openapi-cli.d.ts +0 -52
  220. package/lib/codegen/openapi-cli.d.ts.map +0 -1
  221. package/lib/codegen/openapi-cli.js +0 -84
  222. package/lib/codegen/openapi-cli.js.map +0 -1
  223. package/lib/codegen/openapi.d.ts +0 -209
  224. package/lib/codegen/openapi.d.ts.map +0 -1
  225. package/lib/codegen/openapi.js +0 -1548
  226. package/lib/codegen/openapi.js.map +0 -1
  227. package/lib/codegen/operations.d.ts +0 -24
  228. package/lib/codegen/operations.d.ts.map +0 -1
  229. package/lib/codegen/operations.js +0 -56
  230. package/lib/codegen/operations.js.map +0 -1
  231. package/lib/codegen/pagination.d.ts +0 -39
  232. package/lib/codegen/pagination.d.ts.map +0 -1
  233. package/lib/codegen/pagination.js +0 -31
  234. package/lib/codegen/pagination.js.map +0 -1
  235. package/lib/codegen/patch-audit.d.ts +0 -84
  236. package/lib/codegen/patch-audit.d.ts.map +0 -1
  237. package/lib/codegen/patch-audit.js +0 -366
  238. package/lib/codegen/patch-audit.js.map +0 -1
  239. package/lib/codegen/patches.d.ts +0 -129
  240. package/lib/codegen/patches.d.ts.map +0 -1
  241. package/lib/codegen/patches.js +0 -472
  242. package/lib/codegen/patches.js.map +0 -1
  243. package/lib/codegen/prelude.d.ts +0 -15
  244. package/lib/codegen/prelude.d.ts.map +0 -1
  245. package/lib/codegen/prelude.js +0 -60
  246. package/lib/codegen/prelude.js.map +0 -1
  247. package/lib/codegen/privatize.d.ts +0 -21
  248. package/lib/codegen/privatize.d.ts.map +0 -1
  249. package/lib/codegen/privatize.js +0 -176
  250. package/lib/codegen/privatize.js.map +0 -1
  251. package/lib/codegen/proto.d.ts +0 -121
  252. package/lib/codegen/proto.d.ts.map +0 -1
  253. package/lib/codegen/proto.js +0 -960
  254. package/lib/codegen/proto.js.map +0 -1
  255. package/lib/codegen/pure.d.ts +0 -6
  256. package/lib/codegen/pure.d.ts.map +0 -1
  257. package/lib/codegen/pure.js +0 -91
  258. package/lib/codegen/pure.js.map +0 -1
  259. package/lib/codegen/rewrite-operation-ids.d.ts +0 -154
  260. package/lib/codegen/rewrite-operation-ids.d.ts.map +0 -1
  261. package/lib/codegen/rewrite-operation-ids.js +0 -1117
  262. package/lib/codegen/rewrite-operation-ids.js.map +0 -1
  263. package/lib/codegen/spec-path.d.ts +0 -16
  264. package/lib/codegen/spec-path.d.ts.map +0 -1
  265. package/lib/codegen/spec-path.js +0 -101
  266. package/lib/codegen/spec-path.js.map +0 -1
  267. package/lib/codegen/table.d.ts +0 -5
  268. package/lib/codegen/table.d.ts.map +0 -1
  269. package/lib/codegen/table.js +0 -249
  270. package/lib/codegen/table.js.map +0 -1
  271. package/lib/debug.d.ts +0 -2
  272. package/lib/debug.d.ts.map +0 -1
  273. package/lib/debug.js +0 -1
  274. package/lib/json-patch.d.ts +0 -58
  275. package/lib/json-patch.d.ts.map +0 -1
  276. package/lib/json-patch.js +0 -2
  277. package/lib/json-patch.js.map +0 -1
  278. package/lib/schema-lite-typed.js.map +0 -1
  279. package/lib/testing.d.ts +0 -40
  280. package/lib/testing.d.ts.map +0 -1
  281. package/lib/testing.js +0 -2
  282. package/lib/testing.js.map +0 -1
  283. package/src/codegen/boolean-string-enums.ts +0 -155
  284. package/src/codegen/chunk.ts +0 -147
  285. package/src/codegen/cli.ts +0 -293
  286. package/src/codegen/datafy.ts +0 -874
  287. package/src/codegen/dedupe.ts +0 -173
  288. package/src/codegen/emit.ts +0 -200
  289. package/src/codegen/format.ts +0 -49
  290. package/src/codegen/generated-at.ts +0 -88
  291. package/src/codegen/generator.ts +0 -1229
  292. package/src/codegen/graph.ts +0 -151
  293. package/src/codegen/graphql-client.ts +0 -545
  294. package/src/codegen/graphql.ts +0 -1145
  295. package/src/codegen/latin1.ts +0 -131
  296. package/src/codegen/members.ts +0 -79
  297. package/src/codegen/model-diff.ts +0 -67
  298. package/src/codegen/naming.ts +0 -85
  299. package/src/codegen/openapi-cli.ts +0 -141
  300. package/src/codegen/openapi.ts +0 -1845
  301. package/src/codegen/operations.ts +0 -73
  302. package/src/codegen/pagination.ts +0 -64
  303. package/src/codegen/patch-audit.ts +0 -480
  304. package/src/codegen/patches.ts +0 -581
  305. package/src/codegen/prelude.ts +0 -70
  306. package/src/codegen/privatize.ts +0 -200
  307. package/src/codegen/proto.ts +0 -1092
  308. package/src/codegen/pure.ts +0 -94
  309. package/src/codegen/rewrite-operation-ids.ts +0 -1219
  310. package/src/codegen/spec-path.ts +0 -112
  311. package/src/codegen/table.ts +0 -254
  312. package/src/debug.ts +0 -8
  313. package/src/json-patch.ts +0 -273
  314. package/src/testing.ts +0 -74
@@ -1,853 +0,0 @@
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
-
34
-
35
-
36
-
37
-
38
-
39
-
40
-
41
-
42
-
43
- import { parseAst } from "rolldown/parseAst";
44
- const isNode = (v) => v !== null && typeof v === "object" && typeof v.type === "string";
45
- const strip = (n) => {
46
- while (n.type === "TSAsExpression" ||
47
- n.type === "TSSatisfiesExpression" ||
48
- n.type === "TSNonNullExpression" ||
49
- n.type === "ParenthesizedExpression")
50
- n = n.expression;
51
- return n;
52
- };
53
-
54
- const tableRef = (n) => {
55
- if (n.type !== "CallExpression")
56
- return undefined;
57
- const c = n.callee;
58
- const table = c.type === "Identifier" && c.name === "__S";
59
- const at = c.type === "MemberExpression" && c.object.name === "__L" && c.property.name === "at";
60
- if (!table && !at)
61
- return undefined;
62
- const [f, j] = n.arguments.slice(table ? 1 : 0);
63
- const m = /^__c(\d+)$/.exec(f.name ?? "");
64
- if (!m || j.type !== "Literal")
65
- throw new Error("datafy: unexpected __S call");
66
- return `${m[1]}:${j.value}`;
67
- };
68
- const walk = (n, visit) => {
69
- if (Array.isArray(n)) {
70
- for (const c of n)
71
- walk(c, visit);
72
- return;
73
- }
74
- if (!isNode(n) || !visit(n))
75
- return;
76
- for (const [key, v] of Object.entries(n))
77
- if (key !== "type" && v !== null && typeof v === "object")
78
- walk(v, visit);
79
- };
80
- const IDENT = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
81
-
82
- const encodable = (n) => {
83
- n = strip(n);
84
- if (tableRef(n))
85
- return true;
86
- switch (n.type) {
87
- case "Literal": {
88
- const v = n.value;
89
- return (typeof v === "string" ||
90
- typeof v === "boolean" ||
91
- (typeof v === "number" && Number.isFinite(v)) ||
92
- (v === null && !n.regex && !n.bigint));
93
- }
94
- case "TemplateLiteral":
95
- return !n.expressions.length;
96
- case "UnaryExpression":
97
- return (n.operator === "void" ||
98
- (n.operator === "-" &&
99
- n.argument.type === "Literal" &&
100
- typeof n.argument.value === "number"));
101
- case "Identifier":
102
- return true;
103
- case "MemberExpression":
104
- return !n.computed && encodable(n.object);
105
- case "CallExpression":
106
- return (encodable(n.callee) &&
107
- n.arguments.every((a) => a.type !== "SpreadElement" && encodable(a)));
108
- case "ObjectExpression":
109
- return n.properties.every((p) => p.type === "Property" &&
110
- p.kind === "init" &&
111
- !p.method &&
112
- ((!p.computed && p.key.type === "Identifier") || p.key.type === "Literal") &&
113
- encodable(p.value));
114
- case "ArrayExpression":
115
- return n.elements.every((e) => !!e && e.type !== "SpreadElement" && encodable(e));
116
- case "ArrowFunctionExpression":
117
- return !n.params.length && n.body.type !== "BlockStatement" && encodable(n.body);
118
- default:
119
- return false;
120
- }
121
- };
122
-
123
-
124
-
125
-
126
- const quote = (s) => JSON.stringify(s).replace(/[^\x00-\x24\x26-\x3a\x3c-\x7f]/g, (c) => `\\u${c.charCodeAt(0).toString(16).padStart(4, "0")}`);
127
-
128
- const VOCAB = /^(?:S|T|__L)\.[A-Za-z_$][\w$]*$/;
129
-
130
- class Holder {
131
- entries = [];
132
- refs = ["__0"];
133
- refIndex = new Map([["__0", 0]]);
134
-
135
- local = new Map();
136
- name;
137
-
138
- stem;
139
- constructor(name) {
140
- this.name = name;
141
- }
142
- ref(text) {
143
- let i = this.refIndex.get(text);
144
- if (i === undefined) {
145
- i = this.refs.length;
146
- this.refs.push(text);
147
- this.refIndex.set(text, i);
148
- }
149
- return i;
150
- }
151
- }
152
-
153
- const identifierOf = (n) => {
154
- const callee = strip(n.callee);
155
- const [arg] = n.arguments;
156
- const props = arg?.type === "ObjectExpression" ? arg.properties : [];
157
- const p = props[0];
158
- return callee.property.name === "annotate" &&
159
- n.arguments.length === 1 &&
160
- props.length === 1 &&
161
- p.type === "Property" &&
162
- !p.computed &&
163
- p.key.name === "identifier" &&
164
- p.value.type === "Literal" &&
165
- typeof p.value.value === "string"
166
- ? p.value.value
167
- : undefined;
168
- };
169
-
170
-
171
-
172
-
173
-
174
-
175
- const canon = (n, isNamespace, onNode, up) => {
176
- n = strip(n);
177
- const go = (x) => canon(x, isNamespace, onNode, n);
178
- const all = (xs) => {
179
- const parts = xs.map(go);
180
- return parts.every((p) => p) ? parts : undefined;
181
- };
182
- const join = (ps) => ps.map((p) => p[0]).join(",");
183
- const width = (ps) => ps.reduce((a, p) => a + p[1] + 1, 0);
184
- let out;
185
- if (tableRef(n))
186
- out = undefined;
187
- else
188
- switch (n.type) {
189
- case "Literal":
190
- case "TemplateLiteral":
191
- case "UnaryExpression": {
192
- const v = n.type === "TemplateLiteral"
193
- ? n.expressions.length
194
- ? undefined
195
- : quote(n.quasis[0].value.cooked)
196
- : n.type === "UnaryExpression"
197
- ? n.operator === "void"
198
- ? "u"
199
- : `-${n.argument.value}`
200
- : typeof n.value === "string"
201
- ? quote(n.value)
202
- : String(n.value);
203
- out = v === undefined ? undefined : [v, v.length];
204
- break;
205
- }
206
- case "Identifier":
207
- out = n.name === "undefined" ? ["u", 1] : [`$${n.name}`, 3];
208
- break;
209
- case "MemberExpression": {
210
- const obj = strip(n.object);
211
- if (isNamespace(obj))
212
- out = [`$${obj.name}.${n.property.name}`, 3];
213
- else {
214
- const o = go(obj);
215
- out = o && [`${o[0]}.${n.property.name}`, o[1] + 1 + n.property.name.length];
216
- }
217
- break;
218
- }
219
- case "CallExpression": {
220
- const callee = strip(n.callee);
221
- if (callee.type === "MemberExpression" && !isNamespace(strip(callee.object))) {
222
- const o = go(callee.object);
223
- const name = callee.property.name;
224
- const id = identifierOf(n);
225
- if (id !== undefined)
226
- out = o && [`${o[0]}@${quote(id)}`, o[1] + 3 + id.length];
227
- else {
228
- const args = all(n.arguments);
229
- out = o &&
230
- args && [`${o[0]}.${name}(${join(args)})`, o[1] + name.length + 2 + width(args)];
231
- }
232
- }
233
- else {
234
- const args = all(n.arguments);
235
- const c = go(callee);
236
- out = c && args && [`${c[0]}(${join(args)})`, c[1] + 1 + width(args)];
237
- }
238
- break;
239
- }
240
- case "ObjectExpression": {
241
- const props = n.properties;
242
- const vals = all(props.map((p) => p.value));
243
- out = vals && [
244
- `{${props.map((p, i) => `${quote(String(p.key.name ?? p.key.value))}:${vals[i][0]}`).join(",")}}`,
245
- props.reduce((a, p, i) => a + String(p.key.name ?? p.key.value).length + 2 + vals[i][1], 1),
246
- ];
247
- break;
248
- }
249
- case "ArrayExpression": {
250
- const els = all(n.elements);
251
- out = els && [`[${join(els)}]`, width(els) + 1];
252
- break;
253
- }
254
- case "ArrowFunctionExpression":
255
- go(n.body);
256
- out = undefined;
257
- break;
258
- default:
259
- out = undefined;
260
- }
261
- onNode?.(n, up, out?.[0], out?.[1] ?? 0);
262
- return out;
263
- };
264
-
265
- const configOf = (o) => {
266
- if (!o.strategy)
267
- return o.config;
268
- const c = strip(o.config);
269
- if (c.type !== "ObjectExpression")
270
- throw new Error(`datafy: ${o.name} config is not an object`);
271
- const key = { type: "Identifier", start: 0, end: 0, name: "strategy" };
272
- const member = {
273
- type: "Property",
274
- start: 0,
275
- end: 0,
276
- kind: "init",
277
- method: false,
278
- computed: false,
279
- key,
280
- value: o.strategy,
281
- };
282
- return { ...c, properties: [...c.properties, member] };
283
- };
284
-
285
- const MAX_MOVED_FUNCTION = 4000;
286
-
287
- const MIN_LEN = 16;
288
-
289
- const MIN_SAVING = 64;
290
-
291
-
292
-
293
-
294
- const NEW_HOLDER_COST = 3000;
295
-
296
-
297
-
298
-
299
-
300
-
301
-
302
- const hoistConstants = (roots, isNamespace, holderOf, rootsOf, pinned) => {
303
- const pinnedUses = new Map();
304
- const parent = new Map();
305
- const container = new Map();
306
- const byKey = new Map();
307
- for (const [root, h] of roots) {
308
- container.set(strip(root), h);
309
- canon(root, isNamespace, (n, up, key, len) => {
310
- if (up)
311
- parent.set(n, up);
312
- if (n.type === "Identifier" && pinned.has(n.name)) {
313
- let uses = pinnedUses.get(n.name);
314
- if (!uses)
315
- pinnedUses.set(n.name, (uses = []));
316
- uses.push(n);
317
- }
318
- if (key === undefined)
319
- return;
320
- const candidate = (len >= MIN_LEN && n.type === "CallExpression" && n.arguments.length > 0) ||
321
- ((n.type === "Literal" || n.type === "TemplateLiteral") && len >= MIN_LEN);
322
- if (!candidate)
323
- return;
324
- let c = byKey.get(key);
325
- if (!c)
326
- byKey.set(key, (c = { len, nodes: [] }));
327
- c.nodes.push(n);
328
- });
329
- }
330
- const valueOf = new Map();
331
- const liveIn = (n) => {
332
- for (let x = n;;) {
333
- const p = parent.get(x);
334
- if (!p)
335
- return container.get(x);
336
- const v = valueOf.get(p);
337
- if (v)
338
- return v.nodes[0] === p ? v.holder : undefined;
339
- x = p;
340
- }
341
- };
342
- const keyOf = (holders) => {
343
- const rs = new Set();
344
- for (const h of holders)
345
- for (const r of rootsOf.get(h).split(","))
346
- rs.add(+r);
347
- return [...rs].sort((a, b) => a - b).join(",");
348
- };
349
- // Each moved constant was at least one object at import; the holder it moves to may be new.
350
- let newHolders = Math.max(0, pinned.size - 1);
351
- const values = [];
352
- const candidates = [...byKey.values()]
353
- .filter((c) => c.nodes.length > 1)
354
- .sort((a, b) => (b.nodes.length - 1) * b.len - (a.nodes.length - 1) * a.len);
355
- for (const c of candidates) {
356
- const live = [];
357
- for (const n of c.nodes) {
358
- const h = liveIn(n);
359
- if (h)
360
- live.push([n, h]);
361
- }
362
- if (live.length < 2)
363
- continue;
364
- const key = keyOf(live.map(([, h]) => h));
365
- const existing = holderOf(key, false);
366
- // A new holder is one more function at import: only as many as moved constants free.
367
- if (!existing && !newHolders)
368
- continue;
369
- let cost = c.len + (existing ? 0 : NEW_HOLDER_COST);
370
- const users = new Set();
371
- for (const [, h] of live) {
372
- cost += h === existing ? 3 : 6;
373
- users.add(h);
374
- }
375
- cost += 6 * [...users].filter((h) => h !== existing).length;
376
- if (live.length * c.len - cost < MIN_SAVING)
377
- continue;
378
- if (!existing)
379
- newHolders--;
380
- const holder = existing ?? holderOf(key, true);
381
- rootsOf.set(holder, key);
382
- const v = { nodes: live.map(([n]) => n), holder, j: -1 };
383
- for (const n of v.nodes)
384
- valueOf.set(n, v);
385
- values.push(v);
386
- }
387
- for (const [name, code] of pinned) {
388
- const live = [];
389
- for (const n of pinnedUses.get(name) ?? []) {
390
- const h = liveIn(n);
391
- if (h)
392
- live.push([n, h]);
393
- }
394
- if (!live.length)
395
- throw new Error(`datafy: constant ${name} has no live reader`);
396
- const key = keyOf(live.map(([, h]) => h));
397
- const holder = holderOf(key, true);
398
- rootsOf.set(holder, key);
399
- values.push({ nodes: live.map(([n]) => n), holder, j: -1, code });
400
- }
401
- return values;
402
- };
403
- const STRING = /"(?:[^"\\]|\\.)*"/g;
404
- const STEM = /^[\w.-]{4,}$/;
405
-
406
-
407
-
408
-
409
-
410
- const stemOf = (h) => {
411
- const strings = h.entries.flatMap((e) => e.match(STRING) ?? []).map((t) => t.slice(1, -1));
412
- let best = 0;
413
- for (const c of new Set(strings)) {
414
- if (!STEM.test(c))
415
- continue;
416
- let uses = 0;
417
- for (const t of strings)
418
- uses += t.split(c).length - 1;
419
- const saving = uses * (c.length - 1) - (c.length + 4);
420
- if (saving > best)
421
- [best, h.stem] = [saving, c];
422
- }
423
- const stem = h.stem;
424
- if (stem)
425
- for (let j = 0; j < h.entries.length; j++)
426
- h.entries[j] = h.entries[j].replace(STRING, (t) => t.split(stem).join("%"));
427
- };
428
- export const datafyOperations = (code, filename = "generated.ts",
429
-
430
-
431
-
432
-
433
- { aliases: aliased = true } = {}) => {
434
- if (!/\bfunction __S\(/.test(code))
435
- return { code, operations: 0, holders: 0, errors: 0 };
436
- const program = parseAst(code, { sourceType: "module", lang: "ts" }, filename);
437
- const body = program.body;
438
- const text = (n) => code.slice(n.start, n.end);
439
- // `import * as X`: `X.y` is a plain value; any other receiver keeps `this` in calls.
440
- const namespaces = new Set();
441
- for (const st of body)
442
- if (st.type === "ImportDeclaration" && st.importKind !== "type")
443
- for (const sp of st.specifiers)
444
- if (sp.type === "ImportNamespaceSpecifier")
445
- namespaces.add(sp.local.name);
446
- const isNamespace = (n) => n.type === "Identifier" && namespaces.has(n.name);
447
- // Emitted JS (lib/) gets no type annotations.
448
- const ts = !filename.endsWith(".js");
449
- const any = ts ? ": any" : "";
450
- // Graph nodes: schemas (`K:j`, from the builders).
451
- const nodes = new Map();
452
- const removals = [];
453
- const ops = [];
454
- const errors = [];
455
- const rest = [];
456
- for (const st of body) {
457
- const cls = st.type === "ExportNamedDeclaration" ? st.declaration : st;
458
- if (cls?.type === "ClassDeclaration" &&
459
- cls.id &&
460
- cls.superClass &&
461
- !cls.body.body.length &&
462
- !cls.decorators?.length &&
463
- encodable(cls.superClass)) {
464
- errors.push({
465
- name: cls.id.name,
466
- init: st,
467
- paginated: false,
468
- config: cls.superClass,
469
- error: { exported: st !== cls },
470
- });
471
- continue;
472
- }
473
- if (st.type === "FunctionDeclaration" && /^__c\d+$/.test(st.id?.name ?? "")) {
474
- const k = st.id.name.slice(3);
475
- const sw = st.body.body[0];
476
- if (sw?.type !== "SwitchStatement")
477
- throw new Error(`datafy: ${st.id.name} is not a switch`);
478
- for (const c of sw.cases) {
479
- const ret = c.consequent[0];
480
- if (ret?.type !== "ReturnStatement")
481
- throw new Error("datafy: case without return");
482
- nodes.set(`${k}:${c.test.value}`, ret.argument);
483
- }
484
- removals.push(st);
485
- continue;
486
- }
487
- if (st.type === "FunctionDeclaration" && st.id?.name === "__S") {
488
- removals.push(st);
489
- continue;
490
- }
491
- if (st.type === "VariableDeclaration" && st.declarations.length === 1) {
492
- const d = st.declarations[0];
493
- if (d.id.name === "__s") {
494
- removals.push(st);
495
- continue;
496
- }
497
- }
498
- const decl = st.type === "ExportNamedDeclaration" ? st.declaration : st;
499
- if (decl?.type === "VariableDeclaration" && decl.declarations.length === 1) {
500
- const d = decl.declarations[0];
501
- const init = d.init ? strip(d.init) : undefined;
502
- if (init?.type === "CallExpression" &&
503
- init.callee.type === "MemberExpression" &&
504
- init.callee.object.name === "API" &&
505
- (init.callee.property.name === "make" || init.callee.property.name === "makePaginated")) {
506
- const [fn, strategy] = init.arguments;
507
- if (fn?.type !== "ArrowFunctionExpression" ||
508
- fn.params.length ||
509
- fn.body.type === "BlockStatement")
510
- throw new Error(`datafy: unexpected operation form in ${d.id.name}`);
511
- ops.push({
512
- name: d.id.name,
513
- init: d.init,
514
- paginated: init.callee.property.name === "makePaginated",
515
- config: strip(fn.body),
516
- strategy,
517
- });
518
- continue;
519
- }
520
- }
521
- if (st.type !== "ImportDeclaration")
522
- rest.push(st);
523
- }
524
- if (!ops.length && !errors.length)
525
- return { code, operations: 0, holders: 0, errors: 0 };
526
- const operations = ops.length;
527
- // From here on an error class is a root like an operation; its entry 0 is its base class.
528
- ops.push(...errors);
529
- // Module constants only data reads (service traits, endpoint rules, ...)
530
- // move, as written, into the holder of the roots that reach them.
531
- const reads = (n, name) => {
532
- let count = 0;
533
- walk(n, (x) => {
534
- if (x.type === "Identifier" && x.name === name)
535
- count++;
536
- return true;
537
- });
538
- return count;
539
- };
540
- const dataRoots = [[...nodes.values()], ops.map((o) => [o.config, o.strategy])];
541
- const pinnable = new Map();
542
- for (const st of rest) {
543
- if (st.type !== "VariableDeclaration" || st.kind !== "const" || st.declarations.length !== 1)
544
- continue;
545
- const d = st.declarations[0];
546
- if (d.id.type !== "Identifier" || !d.init || strip(d.init).type !== "CallExpression")
547
- continue;
548
- if (!/\/\*[@#]__PURE__\*\/\s*$/.test(code.slice(Math.max(0, d.init.start - 20), d.init.start)))
549
- continue;
550
- let internal = false;
551
- walk(d.init, (x) => {
552
- if (tableRef(x) || (x.type === "Identifier" && /^__[cSs]\d*$/.test(x.name)))
553
- internal = true;
554
- // A first call would parse a big function twice (holder, then itself): it stays.
555
- if (/Function/.test(x.type) && x.end - x.start > MAX_MOVED_FUNCTION)
556
- internal = true;
557
- return true;
558
- });
559
- if (!internal)
560
- pinnable.set(d.id.name, { st, init: d.init });
561
- }
562
- for (let changed = true; changed;) {
563
- changed = false;
564
- for (const [name, c] of pinnable) {
565
- const outside = rest.some((st) => st !== c.st && reads(st, name) > 0);
566
- const chained = [...pinnable.keys()].some((other) => reads(c.init, other) > 0);
567
- if (outside || chained || reads(c.init, name) || !reads(dataRoots, name)) {
568
- pinnable.delete(name);
569
- changed = true;
570
- }
571
- }
572
- }
573
- const pinned = new Map();
574
- for (const [name, c] of pinnable) {
575
- pinned.set(name, text(c.init));
576
- removals.push(c.st);
577
- }
578
- const roots = rest.filter((st) => !removals.includes(st));
579
-
580
- const refsOf = (n) => {
581
- const out = new Set();
582
- walk(n, (x) => {
583
- const k = tableRef(x);
584
- if (k)
585
- out.add(k);
586
- return !k;
587
- });
588
- return out;
589
- };
590
- // Reachability: which roots reach each node.
591
- const edges = new Map();
592
- for (const [k, n] of nodes)
593
- edges.set(k, refsOf(n));
594
- const otherRoots = roots.filter((st) => refsOf(st).size);
595
- const rootRefs = [...ops.map((o) => refsOf(o.config)), ...otherRoots.map((st) => refsOf(st))];
596
- const reachedBy = new Map();
597
- rootRefs.forEach((start, r) => {
598
- const seen = new Set();
599
- const stack = [...start];
600
- while (stack.length) {
601
- const k = stack.pop();
602
- if (seen.has(k))
603
- continue;
604
- seen.add(k);
605
- if (!nodes.has(k))
606
- throw new Error(`datafy: missing schema ${k}`);
607
- let list = reachedBy.get(k);
608
- if (!list)
609
- reachedBy.set(k, (list = []));
610
- list.push(r);
611
- for (const x of edges.get(k))
612
- stack.push(x);
613
- }
614
- });
615
- // Placement: an operation's own holder, or a shared holder per root set.
616
- const opHolders = ops.map((o) => new Holder(o.name));
617
- const shared = new Map();
618
- const home = new Map();
619
- for (const k of nodes.keys()) {
620
- const rs = reachedBy.get(k);
621
- if (!rs)
622
- continue; // unreachable: dropped
623
- let h;
624
- if (rs.length === 1 && rs[0] < ops.length)
625
- h = opHolders[rs[0]];
626
- else {
627
- const key = rs.join(",");
628
- h = shared.get(key) ?? new Holder(`__d${shared.size}`);
629
- shared.set(key, h);
630
- }
631
- home.set(k, h);
632
- }
633
- // Entry indexes: operation config first, then nodes in source order.
634
- for (const h of opHolders)
635
- h.entries.push("");
636
- for (const [k, h] of home) {
637
- h.local.set(k, h.entries.length);
638
- h.entries.push("");
639
- }
640
- // Aliases: `__0` is `at`; other namespace members are numbered on first use.
641
- const aliases = new Map([["__L.at", "__0"]]);
642
- const alias = (t) => {
643
- let a = aliases.get(t);
644
- if (!a)
645
- aliases.set(t, (a = `__${aliases.size}`));
646
- return a;
647
- };
648
- const nodeRef = (k, h) => {
649
- const target = home.get(k);
650
- const j = target.local.get(k);
651
- if (target === h)
652
- return `#${j}`;
653
- return `#${h.ref(target.name)}:${j}`;
654
- };
655
- const configs = ops.map(configOf);
656
- // Repeated constant expressions become value entries (`~N`, `~R:N`).
657
- const holderOfKey = (key, create) => {
658
- const rs = key.split(",").map(Number);
659
- if (rs.length === 1 && rs[0] < ops.length)
660
- return opHolders[rs[0]];
661
- let h = shared.get(key);
662
- if (!h && create)
663
- shared.set(key, (h = new Holder(`__d${shared.size}`)));
664
- return h;
665
- };
666
- const rootsOf = new Map();
667
- opHolders.forEach((h, i) => rootsOf.set(h, String(i)));
668
- for (const [key, h] of shared)
669
- rootsOf.set(h, key);
670
- const exprRoots = [...home].map(([k, h]) => [nodes.get(k), h]);
671
- configs.forEach((c, i) => exprRoots.push([c, opHolders[i]]));
672
- const values = hoistConstants(exprRoots, isNamespace, holderOfKey, rootsOf, pinned);
673
- for (const v of values) {
674
- v.j = v.holder.entries.length;
675
- v.holder.entries.push("");
676
- }
677
- const valueOf = new Map();
678
- for (const v of values)
679
- for (const n of v.nodes)
680
- valueOf.set(n, v);
681
- const encode = (n, h, self) => {
682
- const value = (t) => `$${h.ref(aliased && VOCAB.test(t) ? alias(t) : t)}`;
683
- n = strip(n);
684
- const v = valueOf.get(n);
685
- if (v && v !== self)
686
- return v.holder === h ? `~${v.j}` : `~${h.ref(v.holder.name)}:${v.j}`;
687
- const k = tableRef(n);
688
- if (k)
689
- return nodeRef(k, h);
690
- const enc = (x) => encode(x, h);
691
- switch (n.type) {
692
- case "Literal": {
693
- const v = n.value;
694
- if (typeof v === "string")
695
- return quote(v);
696
- if (typeof v === "number" && Number.isFinite(v))
697
- return String(v);
698
- if (typeof v === "boolean")
699
- return String(v);
700
- if (v === null && !n.regex && !n.bigint)
701
- return "null";
702
- throw new Error(`datafy: unsupported literal ${text(n)}`);
703
- }
704
- case "TemplateLiteral":
705
- if (n.expressions.length)
706
- throw new Error("datafy: template with expressions");
707
- return quote(n.quasis[0].value.cooked);
708
- case "UnaryExpression":
709
- if (n.operator === "-" &&
710
- n.argument.type === "Literal" &&
711
- typeof n.argument.value === "number")
712
- return String(-n.argument.value);
713
- if (n.operator === "void")
714
- return "u";
715
- throw new Error(`datafy: unsupported unary ${text(n)}`);
716
- case "Identifier":
717
- if (n.name === "undefined")
718
- return "u";
719
- return value(n.name);
720
- case "MemberExpression": {
721
- if (n.computed)
722
- throw new Error(`datafy: computed member ${text(n)}`);
723
- const obj = strip(n.object);
724
- // `S.String`, `Retry.Retry`: one value.
725
- if (isNamespace(obj))
726
- return value(`${obj.name}.${n.property.name}`);
727
- return `${enc(obj)}.${n.property.name}`;
728
- }
729
- case "CallExpression": {
730
- const args = n.arguments.map((a) => {
731
- if (a.type === "SpreadElement")
732
- throw new Error("datafy: spread argument");
733
- return enc(a);
734
- });
735
- const callee = strip(n.callee);
736
- // `S.Struct(...)` calls the namespace member as a plain function;
737
- // `x.pipe(...)` on any other receiver is a method call.
738
- if (callee.type === "MemberExpression" &&
739
- !callee.computed &&
740
- !isNamespace(strip(callee.object))) {
741
- // `.annotate({ identifier: "X" })`, on most schemas: `@"X"`.
742
- const id = identifierOf(n);
743
- if (id !== undefined)
744
- return `${enc(callee.object)}@${quote(id)}`;
745
- return `${enc(callee.object)}.${callee.property.name}(${args.join(",")})`;
746
- }
747
- return `${enc(callee)}(${args.join(",")})`;
748
- }
749
- case "ObjectExpression":
750
- return `{${n.properties
751
- .map((p) => {
752
- if (p.type !== "Property" || p.kind !== "init" || p.method)
753
- throw new Error(`datafy: unsupported property ${text(p)}`);
754
- let key;
755
- if (!p.computed && p.key.type === "Identifier")
756
- key = p.key.name;
757
- else if (p.key.type === "Literal")
758
- key = String(p.key.value);
759
- else
760
- throw new Error(`datafy: unsupported key ${text(p)}`);
761
- return `${IDENT.test(key) ? key : quote(key)}:${enc(p.value)}`;
762
- })
763
- .join(",")}}`;
764
- case "ArrayExpression":
765
- return `[${n.elements
766
- .map((e) => {
767
- if (!e || e.type === "SpreadElement")
768
- throw new Error("datafy: array hole or spread");
769
- return enc(e);
770
- })
771
- .join(",")}]`;
772
- case "ArrowFunctionExpression":
773
- if (n.params.length || n.body.type === "BlockStatement")
774
- throw new Error(`datafy: unsupported function ${text(n).slice(0, 80)}`);
775
- {
776
- // The body becomes its own entry, so a reader never has to skip it.
777
- const j = h.entries.length;
778
- h.entries.push("");
779
- h.entries[j] = enc(n.body);
780
- return `>${j}`;
781
- }
782
- default:
783
- throw new Error(`datafy: unsupported ${n.type}: ${text(n).slice(0, 80)}`);
784
- }
785
- };
786
- for (const [k, h] of home)
787
- h.entries[h.local.get(k)] = encode(nodes.get(k), h);
788
- configs.forEach((c, i) => (opHolders[i].entries[0] = encode(c, opHolders[i])));
789
- for (const v of values)
790
- v.holder.entries[v.j] = v.code ? `$${v.holder.ref(v.code)}` : encode(v.nodes[0], v.holder, v);
791
- // Operations only: an error class's data is built on every construction.
792
- for (const h of opHolders.slice(0, operations))
793
- stemOf(h);
794
- // The data string is a single-quoted JS literal.
795
- const literal = (h) => {
796
- const data = `'${h.entries.join(";").replace(/[\\']/g, (c) => `\\${c}`)}'`;
797
- const refs = `[${(aliased ? h.refs : ["__L.at", ...h.refs.slice(1)]).join(", ")}]`;
798
- return h.stem ? `API.named(${quote(h.stem)}, ${data}, ${refs})` : `[${data}, ${refs}]`;
799
- };
800
- const edits = [];
801
- for (const st of removals) {
802
- let end = st.end;
803
- while (code[end] === "\n")
804
- end++;
805
- edits.push([st.start, end, ""]);
806
- }
807
- ops.forEach((o, i) => {
808
- if (o.error) {
809
- // A plain function, not an arrow: it is the class (`new`, `instanceof`, `extends`).
810
- edits.push([
811
- o.init.start,
812
- o.init.end,
813
- `${o.error.exported ? "export " : ""}const ${o.name}${any} = /*@__PURE__*/ API.error(function ${o.name}(i${any})${any} {\n return API.newError(i, new.target, ${literal(opHolders[i])});\n});`,
814
- ]);
815
- return;
816
- }
817
- const fn = o.paginated ? "API.opPaginated" : "API.op";
818
- const marked = /\/\*[@#]__PURE__\*\/\s*$/.test(code.slice(o.init.start - 20, o.init.start));
819
- edits.push([
820
- o.init.start,
821
- o.init.end,
822
- `${marked ? "" : "/*@__PURE__*/ "}${fn}((i${any})${any} => i === API.D ? ${literal(opHolders[i])} : API.call(${o.name}, i))`,
823
- ]);
824
- });
825
- // Module-level code that names a schema at import (error classes).
826
- for (const st of otherRoots)
827
- walk(st, (n) => {
828
- const k = tableRef(n);
829
- if (!k)
830
- return true;
831
- const target = home.get(k);
832
- edits.push([n.start, n.end, `API.schema(${target.name}, ${target.local.get(k)})`]);
833
- return false;
834
- });
835
- edits.sort((a, b) => b[0] - a[0]);
836
- let out = code;
837
- for (const [s, e, t] of edits)
838
- out = out.slice(0, s) + t + out.slice(e);
839
- out = out.replace(/\n*$/, "\n").replace(/^\/\/ Schema table, filled on first reach.*\n/m, "");
840
- if (shared.size)
841
- out +=
842
- "\n// Shared schema data (see `op` in core/api).\n" +
843
- [...shared.values()]
844
- .map((h) => `function ${h.name}()${any} {\n return ${literal(h)};\n}\n`)
845
- .join("");
846
- // Aliases go right after the imports: error classes reach schemas while the module evaluates.
847
- const lastImport = [...body].reverse().find((st) => st.type === "ImportDeclaration");
848
- const at = lastImport ? lastImport.end : 0;
849
- const decls = aliased ? [...aliases].map(([t, a]) => `const ${a} = ${t};`) : [];
850
- out = `${out.slice(0, at)}\n${decls.join("\n")}${out.slice(at)}`;
851
- return { code: out, operations, holders: shared.size, errors: errors.length };
852
- };
853
- //# sourceMappingURL=datafy.js.map