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

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 (318) hide show
  1. package/lib/_bundle/aws.js +2 -0
  2. package/lib/_bundle/aws.js.map +1 -0
  3. package/lib/_bundle/base.js +5 -0
  4. package/lib/_bundle/base.js.map +1 -0
  5. package/lib/_bundle/graphql.js +3 -0
  6. package/lib/_bundle/graphql.js.map +1 -0
  7. package/lib/_bundle/rest.js +2 -0
  8. package/lib/_bundle/rest.js.map +1 -0
  9. package/lib/_bundle/xml.js +4 -0
  10. package/lib/_bundle/xml.js.map +1 -0
  11. package/lib/api.d.ts +118 -13
  12. package/lib/api.d.ts.map +1 -1
  13. package/lib/api.js +1 -188
  14. package/lib/category.d.ts.map +1 -1
  15. package/lib/category.js +1 -278
  16. package/lib/chunks.d.ts +3 -0
  17. package/lib/chunks.d.ts.map +1 -0
  18. package/lib/chunks.js +1 -0
  19. package/lib/codegen/boolean-string-enums.js +36 -36
  20. package/lib/codegen/chunk.d.ts +7 -0
  21. package/lib/codegen/chunk.d.ts.map +1 -0
  22. package/lib/codegen/chunk.js +139 -0
  23. package/lib/codegen/chunk.js.map +1 -0
  24. package/lib/codegen/cli.d.ts.map +1 -1
  25. package/lib/codegen/cli.js +28 -14
  26. package/lib/codegen/cli.js.map +1 -1
  27. package/lib/codegen/datafy.d.ts +14 -0
  28. package/lib/codegen/datafy.d.ts.map +1 -0
  29. package/lib/codegen/datafy.js +853 -0
  30. package/lib/codegen/datafy.js.map +1 -0
  31. package/lib/codegen/dedupe.d.ts +5 -0
  32. package/lib/codegen/dedupe.d.ts.map +1 -0
  33. package/lib/codegen/dedupe.js +153 -0
  34. package/lib/codegen/dedupe.js.map +1 -0
  35. package/lib/codegen/emit.js +56 -56
  36. package/lib/codegen/format.js +18 -18
  37. package/lib/codegen/generated-at.js +29 -29
  38. package/lib/codegen/generator.d.ts.map +1 -1
  39. package/lib/codegen/generator.js +92 -84
  40. package/lib/codegen/generator.js.map +1 -1
  41. package/lib/codegen/graph.js +36 -36
  42. package/lib/codegen/graphql-client.d.ts +2 -0
  43. package/lib/codegen/graphql-client.d.ts.map +1 -1
  44. package/lib/codegen/graphql-client.js +55 -26
  45. package/lib/codegen/graphql-client.js.map +1 -1
  46. package/lib/codegen/graphql.js +113 -113
  47. package/lib/codegen/latin1.d.ts +3 -0
  48. package/lib/codegen/latin1.d.ts.map +1 -0
  49. package/lib/codegen/latin1.js +126 -0
  50. package/lib/codegen/latin1.js.map +1 -0
  51. package/lib/codegen/members.d.ts +7 -0
  52. package/lib/codegen/members.d.ts.map +1 -1
  53. package/lib/codegen/members.js +26 -19
  54. package/lib/codegen/members.js.map +1 -1
  55. package/lib/codegen/model-diff.js +6 -6
  56. package/lib/codegen/naming.js +19 -19
  57. package/lib/codegen/openapi-cli.js +15 -15
  58. package/lib/codegen/openapi.d.ts +17 -0
  59. package/lib/codegen/openapi.d.ts.map +1 -1
  60. package/lib/codegen/openapi.js +323 -169
  61. package/lib/codegen/openapi.js.map +1 -1
  62. package/lib/codegen/operations.js +14 -14
  63. package/lib/codegen/pagination.js +11 -11
  64. package/lib/codegen/patch-audit.js +57 -57
  65. package/lib/codegen/patches.js +79 -79
  66. package/lib/codegen/prelude.js +19 -19
  67. package/lib/codegen/privatize.d.ts +21 -0
  68. package/lib/codegen/privatize.d.ts.map +1 -0
  69. package/lib/codegen/privatize.js +176 -0
  70. package/lib/codegen/privatize.js.map +1 -0
  71. package/lib/codegen/proto.js +25 -25
  72. package/lib/codegen/pure.d.ts +6 -0
  73. package/lib/codegen/pure.d.ts.map +1 -0
  74. package/lib/codegen/pure.js +91 -0
  75. package/lib/codegen/pure.js.map +1 -0
  76. package/lib/codegen/rewrite-operation-ids.js +108 -108
  77. package/lib/codegen/spec-path.js +57 -57
  78. package/lib/codegen/table.d.ts +5 -0
  79. package/lib/codegen/table.d.ts.map +1 -0
  80. package/lib/codegen/table.js +249 -0
  81. package/lib/codegen/table.js.map +1 -0
  82. package/lib/config.d.ts +27 -0
  83. package/lib/config.d.ts.map +1 -0
  84. package/lib/config.js +1 -0
  85. package/lib/crypto.d.ts +76 -0
  86. package/lib/crypto.d.ts.map +1 -0
  87. package/lib/crypto.js +1 -0
  88. package/lib/debug.d.ts +2 -0
  89. package/lib/debug.d.ts.map +1 -0
  90. package/lib/debug.js +1 -0
  91. package/lib/error-category.d.ts +1 -1
  92. package/lib/error-category.d.ts.map +1 -1
  93. package/lib/error-category.js +1 -46
  94. package/lib/errors.d.ts +62 -62
  95. package/lib/errors.d.ts.map +1 -1
  96. package/lib/errors.js +1 -195
  97. package/lib/graphql.d.ts +31 -20
  98. package/lib/graphql.d.ts.map +1 -1
  99. package/lib/graphql.js +1 -1027
  100. package/lib/graphql.types.js +1 -5
  101. package/lib/index.d.ts +3 -2
  102. package/lib/index.d.ts.map +1 -1
  103. package/lib/index.js +1 -15
  104. package/lib/json-patch.d.ts +2 -2
  105. package/lib/json-patch.js +1 -226
  106. package/lib/json-patch.js.map +1 -1
  107. package/lib/pagination.d.ts +9 -25
  108. package/lib/pagination.d.ts.map +1 -1
  109. package/lib/pagination.js +1 -266
  110. package/lib/plain-values.d.ts +10 -0
  111. package/lib/plain-values.d.ts.map +1 -0
  112. package/lib/plain-values.js +2 -0
  113. package/lib/plain-values.js.map +1 -0
  114. package/lib/protocol-http.d.ts +50 -14
  115. package/lib/protocol-http.d.ts.map +1 -1
  116. package/lib/protocol-http.js +1 -639
  117. package/lib/protocol-rest.d.ts +38 -25
  118. package/lib/protocol-rest.d.ts.map +1 -1
  119. package/lib/protocol-rest.js +1 -267
  120. package/lib/query.js +1 -27
  121. package/lib/response-validation-strict.d.ts +48 -0
  122. package/lib/response-validation-strict.d.ts.map +1 -0
  123. package/lib/response-validation-strict.js +1 -0
  124. package/lib/response-validation.d.ts +43 -25
  125. package/lib/response-validation.d.ts.map +1 -1
  126. package/lib/response-validation.js +1 -76
  127. package/lib/retry-after.d.ts +3 -3
  128. package/lib/retry-after.js +1 -112
  129. package/lib/retry.d.ts +48 -29
  130. package/lib/retry.d.ts.map +1 -1
  131. package/lib/retry.js +1 -209
  132. package/lib/schema-lite-impl.d.ts +158 -0
  133. package/lib/schema-lite-impl.d.ts.map +1 -0
  134. package/lib/schema-lite-impl.js +1 -0
  135. package/lib/schema-lite-typed.d.ts +41 -0
  136. package/lib/schema-lite-typed.d.ts.map +1 -0
  137. package/lib/schema-lite-typed.js +2 -0
  138. package/lib/schema-lite-typed.js.map +1 -0
  139. package/lib/schema-lite.d.ts +40 -0
  140. package/lib/schema-lite.d.ts.map +1 -0
  141. package/lib/schema-lite.js +1 -0
  142. package/lib/schema.d.ts +20 -6
  143. package/lib/schema.d.ts.map +1 -1
  144. package/lib/schema.js +1 -48
  145. package/lib/testing.d.ts +3 -2
  146. package/lib/testing.d.ts.map +1 -1
  147. package/lib/testing.js +1 -40
  148. package/lib/testing.js.map +1 -1
  149. package/lib/trait.d.ts +13 -13
  150. package/lib/trait.d.ts.map +1 -1
  151. package/lib/trait.js +1 -131
  152. package/lib/xml.d.ts +1 -1
  153. package/lib/xml.d.ts.map +1 -1
  154. package/lib/xml.js +1 -311
  155. package/package.json +9 -7
  156. package/src/api.ts +1080 -180
  157. package/src/category.ts +33 -33
  158. package/src/chunks.ts +30 -0
  159. package/src/codegen/chunk.ts +147 -0
  160. package/src/codegen/cli.ts +27 -1
  161. package/src/codegen/datafy.ts +874 -0
  162. package/src/codegen/dedupe.ts +173 -0
  163. package/src/codegen/generator.ts +10 -3
  164. package/src/codegen/graphql-client.ts +49 -15
  165. package/src/codegen/latin1.ts +131 -0
  166. package/src/codegen/members.ts +8 -0
  167. package/src/codegen/openapi.ts +194 -3
  168. package/src/codegen/privatize.ts +200 -0
  169. package/src/codegen/pure.ts +94 -0
  170. package/src/codegen/table.ts +254 -0
  171. package/src/config.ts +216 -0
  172. package/src/crypto.ts +202 -0
  173. package/src/debug.ts +8 -0
  174. package/src/error-category.ts +2 -1
  175. package/src/errors.ts +65 -56
  176. package/src/graphql.ts +109 -62
  177. package/src/graphql.types.ts +9 -7
  178. package/src/index.ts +3 -2
  179. package/src/json-patch.ts +2 -2
  180. package/src/pagination.ts +37 -18
  181. package/src/plain-values.ts +41 -0
  182. package/src/protocol-http.ts +378 -145
  183. package/src/protocol-rest.ts +337 -169
  184. package/src/response-validation-strict.ts +327 -0
  185. package/src/response-validation.ts +101 -50
  186. package/src/retry-after.ts +3 -3
  187. package/src/retry.ts +94 -93
  188. package/src/schema-lite-impl.ts +550 -0
  189. package/src/schema-lite-typed.ts +44 -0
  190. package/src/schema-lite.ts +44 -0
  191. package/src/schema.ts +258 -14
  192. package/src/testing.ts +4 -2
  193. package/src/trait.ts +36 -28
  194. package/src/xml.ts +31 -17
  195. package/lib/api.js.map +0 -1
  196. package/lib/category.js.map +0 -1
  197. package/lib/codegen/boolean-string-enums.test.d.ts +0 -2
  198. package/lib/codegen/boolean-string-enums.test.d.ts.map +0 -1
  199. package/lib/codegen/boolean-string-enums.test.js +0 -275
  200. package/lib/codegen/boolean-string-enums.test.js.map +0 -1
  201. package/lib/codegen/defer-naming.test.d.ts +0 -2
  202. package/lib/codegen/defer-naming.test.d.ts.map +0 -1
  203. package/lib/codegen/defer-naming.test.js +0 -105
  204. package/lib/codegen/defer-naming.test.js.map +0 -1
  205. package/lib/codegen/generated-at.test.d.ts +0 -2
  206. package/lib/codegen/generated-at.test.d.ts.map +0 -1
  207. package/lib/codegen/generated-at.test.js +0 -81
  208. package/lib/codegen/generated-at.test.js.map +0 -1
  209. package/lib/codegen/generator.test.d.ts +0 -2
  210. package/lib/codegen/generator.test.d.ts.map +0 -1
  211. package/lib/codegen/generator.test.js +0 -121
  212. package/lib/codegen/generator.test.js.map +0 -1
  213. package/lib/codegen/graphql-client.test.d.ts +0 -2
  214. package/lib/codegen/graphql-client.test.d.ts.map +0 -1
  215. package/lib/codegen/graphql-client.test.js +0 -171
  216. package/lib/codegen/graphql-client.test.js.map +0 -1
  217. package/lib/codegen/openapi-binary.test.d.ts +0 -2
  218. package/lib/codegen/openapi-binary.test.d.ts.map +0 -1
  219. package/lib/codegen/openapi-binary.test.js +0 -67
  220. package/lib/codegen/openapi-binary.test.js.map +0 -1
  221. package/lib/codegen/patch-audit.test.d.ts +0 -2
  222. package/lib/codegen/patch-audit.test.d.ts.map +0 -1
  223. package/lib/codegen/patch-audit.test.js +0 -141
  224. package/lib/codegen/patch-audit.test.js.map +0 -1
  225. package/lib/codegen/patches.test.d.ts +0 -2
  226. package/lib/codegen/patches.test.d.ts.map +0 -1
  227. package/lib/codegen/patches.test.js +0 -155
  228. package/lib/codegen/patches.test.js.map +0 -1
  229. package/lib/codegen/protocol-imports.test.d.ts +0 -2
  230. package/lib/codegen/protocol-imports.test.d.ts.map +0 -1
  231. package/lib/codegen/protocol-imports.test.js +0 -36
  232. package/lib/codegen/protocol-imports.test.js.map +0 -1
  233. package/lib/codegen/rewrite-operation-ids.test.d.ts +0 -2
  234. package/lib/codegen/rewrite-operation-ids.test.d.ts.map +0 -1
  235. package/lib/codegen/rewrite-operation-ids.test.js +0 -451
  236. package/lib/codegen/rewrite-operation-ids.test.js.map +0 -1
  237. package/lib/codegen/untagged-unions.test.d.ts +0 -2
  238. package/lib/codegen/untagged-unions.test.d.ts.map +0 -1
  239. package/lib/codegen/untagged-unions.test.js +0 -80
  240. package/lib/codegen/untagged-unions.test.js.map +0 -1
  241. package/lib/error-category.js.map +0 -1
  242. package/lib/errors.js.map +0 -1
  243. package/lib/fixtures/xml/upstream-validator.json +0 -397
  244. package/lib/graphql.js.map +0 -1
  245. package/lib/graphql.test.d.ts +0 -2
  246. package/lib/graphql.test.d.ts.map +0 -1
  247. package/lib/graphql.test.js +0 -151
  248. package/lib/graphql.test.js.map +0 -1
  249. package/lib/graphql.types.js.map +0 -1
  250. package/lib/index.js.map +0 -1
  251. package/lib/json-patch.test.d.ts +0 -2
  252. package/lib/json-patch.test.d.ts.map +0 -1
  253. package/lib/json-patch.test.js +0 -65
  254. package/lib/json-patch.test.js.map +0 -1
  255. package/lib/pagination.js.map +0 -1
  256. package/lib/pagination.test.d.ts +0 -2
  257. package/lib/pagination.test.d.ts.map +0 -1
  258. package/lib/pagination.test.js +0 -131
  259. package/lib/pagination.test.js.map +0 -1
  260. package/lib/protocol-http.js.map +0 -1
  261. package/lib/protocol-http.test.d.ts +0 -2
  262. package/lib/protocol-http.test.d.ts.map +0 -1
  263. package/lib/protocol-http.test.js +0 -228
  264. package/lib/protocol-http.test.js.map +0 -1
  265. package/lib/protocol-rest.js.map +0 -1
  266. package/lib/protocol-rest.test.d.ts +0 -2
  267. package/lib/protocol-rest.test.d.ts.map +0 -1
  268. package/lib/protocol-rest.test.js +0 -498
  269. package/lib/protocol-rest.test.js.map +0 -1
  270. package/lib/query.js.map +0 -1
  271. package/lib/response-validation.js.map +0 -1
  272. package/lib/response-validation.test.d.ts +0 -2
  273. package/lib/response-validation.test.d.ts.map +0 -1
  274. package/lib/response-validation.test.js +0 -72
  275. package/lib/response-validation.test.js.map +0 -1
  276. package/lib/retry-after.js.map +0 -1
  277. package/lib/retry-after.test.d.ts +0 -2
  278. package/lib/retry-after.test.d.ts.map +0 -1
  279. package/lib/retry-after.test.js +0 -204
  280. package/lib/retry-after.test.js.map +0 -1
  281. package/lib/retry.js.map +0 -1
  282. package/lib/retry.test.d.ts +0 -2
  283. package/lib/retry.test.d.ts.map +0 -1
  284. package/lib/retry.test.js +0 -407
  285. package/lib/retry.test.js.map +0 -1
  286. package/lib/schema.js.map +0 -1
  287. package/lib/sdks.test.d.ts +0 -2
  288. package/lib/sdks.test.d.ts.map +0 -1
  289. package/lib/sdks.test.js +0 -76
  290. package/lib/sdks.test.js.map +0 -1
  291. package/lib/trait.js.map +0 -1
  292. package/lib/xml.js.map +0 -1
  293. package/lib/xml.test.d.ts +0 -2
  294. package/lib/xml.test.d.ts.map +0 -1
  295. package/lib/xml.test.js +0 -210
  296. package/lib/xml.test.js.map +0 -1
  297. package/src/codegen/boolean-string-enums.test.ts +0 -299
  298. package/src/codegen/defer-naming.test.ts +0 -113
  299. package/src/codegen/generated-at.test.ts +0 -89
  300. package/src/codegen/generator.test.ts +0 -137
  301. package/src/codegen/graphql-client.test.ts +0 -189
  302. package/src/codegen/openapi-binary.test.ts +0 -91
  303. package/src/codegen/patch-audit.test.ts +0 -160
  304. package/src/codegen/patches.test.ts +0 -188
  305. package/src/codegen/protocol-imports.test.ts +0 -39
  306. package/src/codegen/rewrite-operation-ids.test.ts +0 -472
  307. package/src/codegen/untagged-unions.test.ts +0 -85
  308. package/src/fixtures/xml/upstream-validator.json +0 -397
  309. package/src/graphql.test.ts +0 -198
  310. package/src/json-patch.test.ts +0 -77
  311. package/src/pagination.test.ts +0 -169
  312. package/src/protocol-http.test.ts +0 -276
  313. package/src/protocol-rest.test.ts +0 -703
  314. package/src/response-validation.test.ts +0 -89
  315. package/src/retry-after.test.ts +0 -267
  316. package/src/retry.test.ts +0 -566
  317. package/src/sdks.test.ts +0 -87
  318. package/src/xml.test.ts +0 -251
@@ -0,0 +1,853 @@
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