@mailwoman/core 11.2.0 → 11.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (463) hide show
  1. package/README.md +2 -2
  2. package/lib/api/ckan.ts +1 -1
  3. package/lib/api/disk-storage.ts +2 -2
  4. package/lib/api/ogc.ts +1 -1
  5. package/lib/api/retry.ts +7 -2
  6. package/lib/api/test/transport.ts +1 -1
  7. package/lib/{binary-frame.ts → asset/binary-frame.ts} +1 -1
  8. package/lib/async/abort.ts +100 -0
  9. package/lib/{memoize.ts → async/memoize.ts} +2 -2
  10. package/lib/{promises.ts → async/promises.ts} +1 -1
  11. package/lib/cloudflare/tokens.ts +1 -1
  12. package/lib/cloudflare/wrangler.ts +3 -3
  13. package/lib/crypto/digest.ts +1 -1
  14. package/lib/crypto/ed25519.ts +8 -0
  15. package/lib/{hash.ts → crypto/hash.ts} +16 -1
  16. package/lib/crypto/node.ts +1 -1
  17. package/lib/data-root.ts +16 -3
  18. package/lib/decoder/containment.ts +4 -1
  19. package/lib/decoder/serialize/outline.ts +19 -6
  20. package/lib/decoder/types.ts +9 -1
  21. package/lib/env/schema.ts +5 -0
  22. package/lib/fs/deflate64.ts +1 -1
  23. package/lib/fs/lock.ts +183 -25
  24. package/lib/fs/readers.ts +1 -1
  25. package/lib/fs/writers.ts +18 -1
  26. package/lib/geocode/result.ts +50 -6
  27. package/lib/html/text.ts +2 -2
  28. package/lib/index.ts +0 -1
  29. package/lib/layers/artifact.ts +2 -2
  30. package/lib/layers/coverage.ts +1 -1
  31. package/lib/layers/manifest.ts +94 -2
  32. package/lib/layers/provenance.ts +246 -0
  33. package/lib/layers/publication.ts +4 -3
  34. package/lib/layers/selection.ts +2 -2
  35. package/lib/license/branch.ts +66 -0
  36. package/lib/license/configured.ts +4 -2
  37. package/lib/license/declaration.ts +1 -1
  38. package/lib/license/key/file.ts +1 -1
  39. package/lib/license/key.ts +148 -201
  40. package/lib/license/provenance.ts +193 -0
  41. package/lib/license/register.ts +7 -78
  42. package/lib/license/stamp.ts +8 -2
  43. package/lib/license/text-url.ts +55 -0
  44. package/lib/license/training.ts +67 -0
  45. package/lib/license.ts +7 -6
  46. package/lib/locale/registry.ts +1 -1
  47. package/lib/locale/zh-cn-units.ts +1 -1
  48. package/lib/module/workspace.ts +1 -1
  49. package/lib/{routing.ts → network/routing.ts} +1 -1
  50. package/lib/pipeline/result.ts +6 -25
  51. package/lib/pipeline/runtime-pipeline.ts +129 -144
  52. package/lib/pipeline/stage.ts +290 -0
  53. package/lib/pipeline/types.ts +33 -14
  54. package/lib/pipeline.ts +14 -2
  55. package/lib/{git.ts → repo/git.ts} +21 -1
  56. package/lib/{release-config.ts → repo/release-config.ts} +8 -0
  57. package/lib/{spdx-header.ts → repo/spdx-header.ts} +8 -0
  58. package/lib/{workspaces.ts → repo/workspaces.ts} +1 -1
  59. package/lib/resolver/lookup-types.ts +1 -1
  60. package/lib/resolver/region-database-provider.ts +33 -0
  61. package/lib/resolver/synthetic-id-ranges.ts +19 -9
  62. package/lib/resolver/types.ts +220 -7
  63. package/lib/resolver.ts +14 -1
  64. package/lib/state-machine.ts +1 -1
  65. package/lib/strings/python-json.ts +3 -2
  66. package/lib/utils/chunk-process.ts +2 -2
  67. package/lib/utils/geodatabase-archive.ts +1 -1
  68. package/lib/utils/time.ts +136 -12
  69. package/lib/utils.ts +4 -4
  70. package/out/api/ckan.js +1 -1
  71. package/out/api/ckan.js.map +1 -1
  72. package/out/api/disk-storage.d.ts.map +1 -1
  73. package/out/api/disk-storage.js +2 -2
  74. package/out/api/disk-storage.js.map +1 -1
  75. package/out/api/ogc.js +1 -1
  76. package/out/api/ogc.js.map +1 -1
  77. package/out/api/retry.d.ts.map +1 -1
  78. package/out/api/retry.js +6 -2
  79. package/out/api/retry.js.map +1 -1
  80. package/out/api/test/transport.js +1 -1
  81. package/out/api/test/transport.js.map +1 -1
  82. package/out/asset/binary-frame.d.ts.map +1 -0
  83. package/out/{binary-frame.js → asset/binary-frame.js} +1 -1
  84. package/out/asset/binary-frame.js.map +1 -0
  85. package/out/asset/load-ledger.d.ts.map +1 -0
  86. package/out/asset/load-ledger.js.map +1 -0
  87. package/out/asset/overture-pins.d.ts.map +1 -0
  88. package/out/asset/overture-pins.js.map +1 -0
  89. package/out/asset/postcode-prefix-files.d.ts.map +1 -0
  90. package/out/asset/postcode-prefix-files.js.map +1 -0
  91. package/out/async/abort.d.ts +57 -0
  92. package/out/async/abort.d.ts.map +1 -0
  93. package/out/async/abort.js +89 -0
  94. package/out/async/abort.js.map +1 -0
  95. package/out/async/memoize.d.ts.map +1 -0
  96. package/out/{memoize.js → async/memoize.js} +2 -2
  97. package/out/async/memoize.js.map +1 -0
  98. package/out/{promises.d.ts → async/promises.d.ts} +1 -1
  99. package/out/async/promises.d.ts.map +1 -0
  100. package/out/{promises.js → async/promises.js} +1 -1
  101. package/out/async/promises.js.map +1 -0
  102. package/out/cloudflare/tokens.js +1 -1
  103. package/out/cloudflare/tokens.js.map +1 -1
  104. package/out/cloudflare/wrangler.js +3 -3
  105. package/out/cloudflare/wrangler.js.map +1 -1
  106. package/out/crypto/digest.d.ts +1 -1
  107. package/out/crypto/digest.js +1 -1
  108. package/out/crypto/ed25519.d.ts +7 -0
  109. package/out/crypto/ed25519.d.ts.map +1 -1
  110. package/out/crypto/ed25519.js +7 -0
  111. package/out/crypto/ed25519.js.map +1 -1
  112. package/out/{hash.d.ts → crypto/hash.d.ts} +12 -0
  113. package/out/crypto/hash.d.ts.map +1 -0
  114. package/out/{hash.js → crypto/hash.js} +15 -1
  115. package/out/crypto/hash.js.map +1 -0
  116. package/out/crypto/node.js +1 -1
  117. package/out/crypto/node.js.map +1 -1
  118. package/out/data-root.d.ts +11 -3
  119. package/out/data-root.d.ts.map +1 -1
  120. package/out/data-root.js +14 -3
  121. package/out/data-root.js.map +1 -1
  122. package/out/decoder/containment.d.ts.map +1 -1
  123. package/out/decoder/containment.js +1 -1
  124. package/out/decoder/containment.js.map +1 -1
  125. package/out/decoder/serialize/outline.d.ts +9 -0
  126. package/out/decoder/serialize/outline.d.ts.map +1 -1
  127. package/out/decoder/serialize/outline.js +16 -6
  128. package/out/decoder/serialize/outline.js.map +1 -1
  129. package/out/decoder/types.d.ts +8 -0
  130. package/out/decoder/types.d.ts.map +1 -1
  131. package/out/decoder/types.js +1 -1
  132. package/out/decoder/types.js.map +1 -1
  133. package/out/env/schema.d.ts +1 -0
  134. package/out/env/schema.d.ts.map +1 -1
  135. package/out/env/schema.js +4 -0
  136. package/out/env/schema.js.map +1 -1
  137. package/out/env.d.ts +1 -0
  138. package/out/env.d.ts.map +1 -1
  139. package/out/fs/deflate64.js +1 -1
  140. package/out/fs/deflate64.js.map +1 -1
  141. package/out/fs/lock.d.ts +73 -4
  142. package/out/fs/lock.d.ts.map +1 -1
  143. package/out/fs/lock.js +96 -19
  144. package/out/fs/lock.js.map +1 -1
  145. package/out/fs/readers.js +1 -1
  146. package/out/fs/readers.js.map +1 -1
  147. package/out/fs/writers.d.ts +9 -1
  148. package/out/fs/writers.d.ts.map +1 -1
  149. package/out/fs/writers.js +15 -2
  150. package/out/fs/writers.js.map +1 -1
  151. package/out/geocode/result.d.ts +25 -1
  152. package/out/geocode/result.d.ts.map +1 -1
  153. package/out/geocode/result.js +49 -6
  154. package/out/geocode/result.js.map +1 -1
  155. package/out/html/text.js +2 -2
  156. package/out/html/trust-policies.d.ts.map +1 -0
  157. package/out/html/trust-policies.js.map +1 -0
  158. package/out/index.d.ts +0 -1
  159. package/out/index.d.ts.map +1 -1
  160. package/out/index.js +0 -1
  161. package/out/index.js.map +1 -1
  162. package/out/layers/artifact.js +2 -2
  163. package/out/layers/artifact.js.map +1 -1
  164. package/out/layers/coverage.d.ts.map +1 -1
  165. package/out/layers/coverage.js +1 -1
  166. package/out/layers/coverage.js.map +1 -1
  167. package/out/layers/manifest.d.ts +53 -0
  168. package/out/layers/manifest.d.ts.map +1 -1
  169. package/out/layers/manifest.js +68 -2
  170. package/out/layers/manifest.js.map +1 -1
  171. package/out/layers/provenance.d.ts +127 -0
  172. package/out/layers/provenance.d.ts.map +1 -0
  173. package/out/layers/provenance.js +122 -0
  174. package/out/layers/provenance.js.map +1 -0
  175. package/out/layers/publication.d.ts.map +1 -1
  176. package/out/layers/publication.js +3 -3
  177. package/out/layers/publication.js.map +1 -1
  178. package/out/layers/selection.d.ts.map +1 -1
  179. package/out/layers/selection.js +2 -2
  180. package/out/layers/selection.js.map +1 -1
  181. package/out/license/branch.d.ts +33 -0
  182. package/out/license/branch.d.ts.map +1 -0
  183. package/out/license/branch.js +52 -0
  184. package/out/license/branch.js.map +1 -0
  185. package/out/license/configured.d.ts +1 -1
  186. package/out/license/configured.d.ts.map +1 -1
  187. package/out/license/configured.js +1 -9
  188. package/out/license/configured.js.map +1 -1
  189. package/out/license/declaration.d.ts +1 -1
  190. package/out/license/declaration.d.ts.map +1 -1
  191. package/out/license/key/file.js +1 -1
  192. package/out/license/key/file.js.map +1 -1
  193. package/out/license/key.d.ts +27 -115
  194. package/out/license/key.d.ts.map +1 -1
  195. package/out/license/key.js +98 -164
  196. package/out/license/key.js.map +1 -1
  197. package/out/license/provenance.d.ts +47 -0
  198. package/out/license/provenance.d.ts.map +1 -0
  199. package/out/license/provenance.js +134 -0
  200. package/out/license/provenance.js.map +1 -0
  201. package/out/license/register.d.ts +6 -47
  202. package/out/license/register.d.ts.map +1 -1
  203. package/out/license/register.js +7 -58
  204. package/out/license/register.js.map +1 -1
  205. package/out/license/stamp.d.ts +7 -1
  206. package/out/license/stamp.d.ts.map +1 -1
  207. package/out/license/stamp.js +2 -2
  208. package/out/license/stamp.js.map +1 -1
  209. package/out/license/text-url.d.ts +9 -0
  210. package/out/license/text-url.d.ts.map +1 -0
  211. package/out/license/text-url.js +49 -0
  212. package/out/license/text-url.js.map +1 -0
  213. package/out/license/training.d.ts +29 -0
  214. package/out/license/training.d.ts.map +1 -0
  215. package/out/license/training.js +52 -0
  216. package/out/license/training.js.map +1 -0
  217. package/out/license.d.ts +7 -6
  218. package/out/license.d.ts.map +1 -1
  219. package/out/license.js +7 -6
  220. package/out/license.js.map +1 -1
  221. package/out/locale/registry.d.ts.map +1 -1
  222. package/out/locale/registry.js +1 -1
  223. package/out/locale/registry.js.map +1 -1
  224. package/out/locale/zh-cn-units.js +1 -1
  225. package/out/locale/zh-cn-units.js.map +1 -1
  226. package/out/math/numeric.d.ts.map +1 -0
  227. package/out/math/numeric.js.map +1 -0
  228. package/out/math/random.d.ts.map +1 -0
  229. package/out/math/random.js.map +1 -0
  230. package/out/math/stats.d.ts.map +1 -0
  231. package/out/math/stats.js.map +1 -0
  232. package/out/module/workspace.js +1 -1
  233. package/out/module/workspace.js.map +1 -1
  234. package/out/network/routing.d.ts.map +1 -0
  235. package/out/{routing.js → network/routing.js} +1 -1
  236. package/out/network/routing.js.map +1 -0
  237. package/out/pipeline/result.d.ts +7 -24
  238. package/out/pipeline/result.d.ts.map +1 -1
  239. package/out/pipeline/runtime-pipeline.d.ts +7 -2
  240. package/out/pipeline/runtime-pipeline.d.ts.map +1 -1
  241. package/out/pipeline/runtime-pipeline.js +56 -103
  242. package/out/pipeline/runtime-pipeline.js.map +1 -1
  243. package/out/pipeline/stage.d.ts +180 -0
  244. package/out/pipeline/stage.d.ts.map +1 -0
  245. package/out/pipeline/stage.js +130 -0
  246. package/out/pipeline/stage.js.map +1 -0
  247. package/out/pipeline/types.d.ts +31 -12
  248. package/out/pipeline/types.d.ts.map +1 -1
  249. package/out/pipeline/types.js +0 -8
  250. package/out/pipeline/types.js.map +1 -1
  251. package/out/pipeline.d.ts +5 -3
  252. package/out/pipeline.d.ts.map +1 -1
  253. package/out/pipeline.js +2 -1
  254. package/out/pipeline.js.map +1 -1
  255. package/out/{git.d.ts → repo/git.d.ts} +10 -1
  256. package/out/repo/git.d.ts.map +1 -0
  257. package/out/{git.js → repo/git.js} +14 -1
  258. package/out/repo/git.js.map +1 -0
  259. package/out/{release-config.d.ts → repo/release-config.d.ts} +8 -0
  260. package/out/repo/release-config.d.ts.map +1 -0
  261. package/out/repo/release-config.js.map +1 -0
  262. package/out/repo/scope-config.d.ts.map +1 -0
  263. package/out/repo/scope-config.js.map +1 -0
  264. package/out/repo/spdx-header.d.ts.map +1 -0
  265. package/out/{spdx-header.js → repo/spdx-header.js} +7 -0
  266. package/out/repo/spdx-header.js.map +1 -0
  267. package/out/repo/vale.d.ts.map +1 -0
  268. package/out/repo/vale.js.map +1 -0
  269. package/out/repo/workspaces.d.ts.map +1 -0
  270. package/out/{workspaces.js → repo/workspaces.js} +1 -1
  271. package/out/repo/workspaces.js.map +1 -0
  272. package/out/resolver/lookup-types.d.ts +1 -1
  273. package/out/resolver/region-database-provider.d.ts +30 -0
  274. package/out/resolver/region-database-provider.d.ts.map +1 -1
  275. package/out/resolver/synthetic-id-ranges.d.ts +15 -8
  276. package/out/resolver/synthetic-id-ranges.d.ts.map +1 -1
  277. package/out/resolver/synthetic-id-ranges.js +18 -9
  278. package/out/resolver/synthetic-id-ranges.js.map +1 -1
  279. package/out/resolver/types.d.ts +192 -8
  280. package/out/resolver/types.d.ts.map +1 -1
  281. package/out/resolver/types.js +56 -0
  282. package/out/resolver/types.js.map +1 -1
  283. package/out/resolver.d.ts +3 -3
  284. package/out/resolver.d.ts.map +1 -1
  285. package/out/resolver.js +1 -1
  286. package/out/resolver.js.map +1 -1
  287. package/out/scripting/process.d.ts.map +1 -0
  288. package/out/scripting/process.js.map +1 -0
  289. package/out/scripting/workers.d.ts.map +1 -0
  290. package/out/scripting/workers.js.map +1 -0
  291. package/out/state-machine.d.ts +1 -1
  292. package/out/strings/python-json.d.ts +2 -1
  293. package/out/strings/python-json.d.ts.map +1 -1
  294. package/out/strings/python-json.js +1 -1
  295. package/out/strings/python-json.js.map +1 -1
  296. package/out/tools/build-deflate64-wasm.run.js +2 -2
  297. package/out/tools/build-deflate64-wasm.run.js.map +1 -1
  298. package/out/tools/coarse-placer/build/dataset.js +1 -1
  299. package/out/tools/coarse-placer/build/dataset.js.map +1 -1
  300. package/out/tools/coarse-placer/build/outlier/exposure.js +1 -1
  301. package/out/tools/coarse-placer/build/outlier/exposure.js.map +1 -1
  302. package/out/tools/coarse-placer/build/outlier/latin.d.ts.map +1 -1
  303. package/out/tools/coarse-placer/build/outlier/latin.js +5 -1
  304. package/out/tools/coarse-placer/build/outlier/latin.js.map +1 -1
  305. package/out/tools/coarse-placer/country-sets.d.ts +2 -2
  306. package/out/tools/coarse-placer/country-sets.js +2 -2
  307. package/out/tools/coarse-placer/outlier-rows.js +2 -2
  308. package/out/tools/coarse-placer/outlier-rows.js.map +1 -1
  309. package/out/tools/coarse-placer/train.js +1 -1
  310. package/out/tools/coarse-placer/train.js.map +1 -1
  311. package/out/tools/generate-language-types.js +2 -2
  312. package/out/tools/generate-language-types.js.map +1 -1
  313. package/out/tools/license/product-fixtures.run.d.ts +2 -0
  314. package/out/tools/license/product-fixtures.run.d.ts.map +1 -0
  315. package/out/tools/license/product-fixtures.run.js +350 -0
  316. package/out/tools/license/product-fixtures.run.js.map +1 -0
  317. package/out/utils/chunk-process.js +2 -2
  318. package/out/utils/chunk-process.js.map +1 -1
  319. package/out/utils/geodatabase-archive.js +1 -1
  320. package/out/utils/geodatabase-archive.js.map +1 -1
  321. package/out/utils/time.d.ts +15 -4
  322. package/out/utils/time.d.ts.map +1 -1
  323. package/out/utils/time.js +114 -12
  324. package/out/utils/time.js.map +1 -1
  325. package/out/utils.d.ts +4 -4
  326. package/out/utils.d.ts.map +1 -1
  327. package/out/utils.js +4 -4
  328. package/out/utils.js.map +1 -1
  329. package/out/values/identical.d.ts.map +1 -0
  330. package/out/values/identical.js.map +1 -0
  331. package/out/values/json.d.ts.map +1 -0
  332. package/out/values/json.js.map +1 -0
  333. package/out/values/objects.d.ts.map +1 -0
  334. package/out/values/objects.js.map +1 -0
  335. package/package.json +278 -243
  336. package/tools/build-deflate64-wasm.run.ts +2 -2
  337. package/tools/coarse-placer/build/dataset.ts +1 -1
  338. package/tools/coarse-placer/build/outlier/exposure.ts +1 -1
  339. package/tools/coarse-placer/build/outlier/latin.ts +5 -1
  340. package/tools/coarse-placer/country-sets.ts +2 -2
  341. package/tools/coarse-placer/outlier-rows.ts +2 -2
  342. package/tools/coarse-placer/train.ts +1 -1
  343. package/tools/generate-language-types.ts +2 -2
  344. package/tools/license/product-fixtures.run.ts +449 -0
  345. package/lib/collections.ts +0 -5
  346. package/lib/license/decision.ts +0 -329
  347. package/lib/license/obligations.ts +0 -256
  348. package/lib/license/record.ts +0 -277
  349. package/lib/scripting/ingest-chunk-script.ts +0 -87
  350. package/out/binary-frame.d.ts.map +0 -1
  351. package/out/binary-frame.js.map +0 -1
  352. package/out/collections.d.ts +0 -2
  353. package/out/collections.d.ts.map +0 -1
  354. package/out/collections.js +0 -5
  355. package/out/collections.js.map +0 -1
  356. package/out/git.d.ts.map +0 -1
  357. package/out/git.js.map +0 -1
  358. package/out/hash.d.ts.map +0 -1
  359. package/out/hash.js.map +0 -1
  360. package/out/identical.d.ts.map +0 -1
  361. package/out/identical.js.map +0 -1
  362. package/out/json.d.ts.map +0 -1
  363. package/out/json.js.map +0 -1
  364. package/out/license/decision.d.ts +0 -179
  365. package/out/license/decision.d.ts.map +0 -1
  366. package/out/license/decision.js +0 -169
  367. package/out/license/decision.js.map +0 -1
  368. package/out/license/obligations.d.ts +0 -102
  369. package/out/license/obligations.d.ts.map +0 -1
  370. package/out/license/obligations.js +0 -188
  371. package/out/license/obligations.js.map +0 -1
  372. package/out/license/record.d.ts +0 -136
  373. package/out/license/record.d.ts.map +0 -1
  374. package/out/license/record.js +0 -222
  375. package/out/license/record.js.map +0 -1
  376. package/out/load-ledger.d.ts.map +0 -1
  377. package/out/load-ledger.js.map +0 -1
  378. package/out/memoize.d.ts.map +0 -1
  379. package/out/memoize.js.map +0 -1
  380. package/out/numeric.d.ts.map +0 -1
  381. package/out/numeric.js.map +0 -1
  382. package/out/objects.d.ts.map +0 -1
  383. package/out/objects.js.map +0 -1
  384. package/out/overture-pins.d.ts.map +0 -1
  385. package/out/overture-pins.js.map +0 -1
  386. package/out/postcode-prefix-files.d.ts.map +0 -1
  387. package/out/postcode-prefix-files.js.map +0 -1
  388. package/out/process.d.ts.map +0 -1
  389. package/out/process.js.map +0 -1
  390. package/out/promises.d.ts.map +0 -1
  391. package/out/promises.js.map +0 -1
  392. package/out/random.d.ts.map +0 -1
  393. package/out/random.js.map +0 -1
  394. package/out/release-config.d.ts.map +0 -1
  395. package/out/release-config.js.map +0 -1
  396. package/out/routing.d.ts.map +0 -1
  397. package/out/routing.js.map +0 -1
  398. package/out/scope-config.d.ts.map +0 -1
  399. package/out/scope-config.js.map +0 -1
  400. package/out/scripting/ingest-chunk-script.d.ts +0 -58
  401. package/out/scripting/ingest-chunk-script.d.ts.map +0 -1
  402. package/out/scripting/ingest-chunk-script.js +0 -47
  403. package/out/scripting/ingest-chunk-script.js.map +0 -1
  404. package/out/spdx-header.d.ts.map +0 -1
  405. package/out/spdx-header.js.map +0 -1
  406. package/out/stats.d.ts.map +0 -1
  407. package/out/stats.js.map +0 -1
  408. package/out/trust-policies.d.ts.map +0 -1
  409. package/out/trust-policies.js.map +0 -1
  410. package/out/vale.d.ts.map +0 -1
  411. package/out/vale.js.map +0 -1
  412. package/out/workers.d.ts.map +0 -1
  413. package/out/workers.js.map +0 -1
  414. package/out/workspaces.d.ts.map +0 -1
  415. package/out/workspaces.js.map +0 -1
  416. /package/lib/{load-ledger.ts → asset/load-ledger.ts} +0 -0
  417. /package/lib/{overture-pins.ts → asset/overture-pins.ts} +0 -0
  418. /package/lib/{postcode-prefix-files.ts → asset/postcode-prefix-files.ts} +0 -0
  419. /package/lib/{trust-policies.ts → html/trust-policies.ts} +0 -0
  420. /package/lib/{numeric.ts → math/numeric.ts} +0 -0
  421. /package/lib/{random.ts → math/random.ts} +0 -0
  422. /package/lib/{stats.ts → math/stats.ts} +0 -0
  423. /package/lib/{scope-config.ts → repo/scope-config.ts} +0 -0
  424. /package/lib/{vale.ts → repo/vale.ts} +0 -0
  425. /package/lib/{process.ts → scripting/process.ts} +0 -0
  426. /package/lib/{workers.ts → scripting/workers.ts} +0 -0
  427. /package/lib/{identical.ts → values/identical.ts} +0 -0
  428. /package/lib/{json.ts → values/json.ts} +0 -0
  429. /package/lib/{objects.ts → values/objects.ts} +0 -0
  430. /package/out/{binary-frame.d.ts → asset/binary-frame.d.ts} +0 -0
  431. /package/out/{load-ledger.d.ts → asset/load-ledger.d.ts} +0 -0
  432. /package/out/{load-ledger.js → asset/load-ledger.js} +0 -0
  433. /package/out/{overture-pins.d.ts → asset/overture-pins.d.ts} +0 -0
  434. /package/out/{overture-pins.js → asset/overture-pins.js} +0 -0
  435. /package/out/{postcode-prefix-files.d.ts → asset/postcode-prefix-files.d.ts} +0 -0
  436. /package/out/{postcode-prefix-files.js → asset/postcode-prefix-files.js} +0 -0
  437. /package/out/{memoize.d.ts → async/memoize.d.ts} +0 -0
  438. /package/out/{trust-policies.d.ts → html/trust-policies.d.ts} +0 -0
  439. /package/out/{trust-policies.js → html/trust-policies.js} +0 -0
  440. /package/out/{numeric.d.ts → math/numeric.d.ts} +0 -0
  441. /package/out/{numeric.js → math/numeric.js} +0 -0
  442. /package/out/{random.d.ts → math/random.d.ts} +0 -0
  443. /package/out/{random.js → math/random.js} +0 -0
  444. /package/out/{stats.d.ts → math/stats.d.ts} +0 -0
  445. /package/out/{stats.js → math/stats.js} +0 -0
  446. /package/out/{routing.d.ts → network/routing.d.ts} +0 -0
  447. /package/out/{release-config.js → repo/release-config.js} +0 -0
  448. /package/out/{scope-config.d.ts → repo/scope-config.d.ts} +0 -0
  449. /package/out/{scope-config.js → repo/scope-config.js} +0 -0
  450. /package/out/{spdx-header.d.ts → repo/spdx-header.d.ts} +0 -0
  451. /package/out/{vale.d.ts → repo/vale.d.ts} +0 -0
  452. /package/out/{vale.js → repo/vale.js} +0 -0
  453. /package/out/{workspaces.d.ts → repo/workspaces.d.ts} +0 -0
  454. /package/out/{process.d.ts → scripting/process.d.ts} +0 -0
  455. /package/out/{process.js → scripting/process.js} +0 -0
  456. /package/out/{workers.d.ts → scripting/workers.d.ts} +0 -0
  457. /package/out/{workers.js → scripting/workers.js} +0 -0
  458. /package/out/{identical.d.ts → values/identical.d.ts} +0 -0
  459. /package/out/{identical.js → values/identical.js} +0 -0
  460. /package/out/{json.d.ts → values/json.d.ts} +0 -0
  461. /package/out/{json.js → values/json.js} +0 -0
  462. /package/out/{objects.d.ts → values/objects.d.ts} +0 -0
  463. /package/out/{objects.js → values/objects.js} +0 -0
@@ -10,12 +10,19 @@ import type { ComponentTag } from "@mailwoman/codex/component"
10
10
 
11
11
  import { isBareTreeOf } from "#decoder/tree/shape"
12
12
  import type { AddressNode, AddressTree } from "#decoder/types"
13
- import { errorMessage } from "#errors/schema"
14
- import type { PipelineFault, PipelineResult } from "#pipeline/result"
13
+ import type { PipelineResult } from "#pipeline/result"
14
+ import {
15
+ createRequestContext,
16
+ type PipelineFaultStage,
17
+ type PipelineStage,
18
+ type RequestContext,
19
+ runStage,
20
+ StageID,
21
+ type StageRecord,
22
+ } from "#pipeline/stage"
15
23
  import {
16
24
  DEFAULT_CASE_NORMALIZATION,
17
25
  DEFAULT_PLACER_COUNTRY_USE,
18
- PipelineFaultStage,
19
26
  WORD_CONSISTENCY_SHIP_DEFAULT,
20
27
  deriveInputMode,
21
28
  isPostcodeFormat,
@@ -263,35 +270,49 @@ function buildFastPathTree(text: string, kind: QueryKindResult, shape: QueryShap
263
270
  /**
264
271
  * Runs the configured stages in order and returns the decoded tree with per-stage timings and faults.
265
272
  *
273
+ * Each stage runs through {@linkcode runStage}, which times it and checks the abort signal before it.
266
274
  * A failing classifier, grouper or resolver records a fault and the run continues.
267
- * An aborted signal throws between stages.
275
+ *
276
+ * A caller that runs stages of its own around the parse passes its request's `context`.
277
+ * The result's timing and faults are then that context's, and the context's signal cancels the run.
278
+ * Without one, the run builds a context from `opts.signal`.
268
279
  */
269
- export async function runPipeline(
280
+ export async function runPipeline<FaultStage extends string = PipelineFaultStage>(
270
281
  raw: string,
271
282
  stages: RuntimePipelineStages,
272
- opts?: PipelineOpts
273
- ): Promise<PipelineResult> {
274
- const timing: Record<string, number> = {}
275
- const faults: PipelineFault[] = []
276
- const t0 = performance.now()
283
+ opts?: PipelineOpts,
284
+ context: RequestContext<StageRecord, FaultStage | PipelineFaultStage> = createRequestContext({
285
+ signal: opts?.signal,
286
+ })
287
+ ): Promise<PipelineResult<FaultStage | PipelineFaultStage>> {
288
+ const { timing, faults } = context
277
289
 
278
290
  const normalize = stages.normalize ?? identityNormalize
279
291
  const computeQueryShape = stages.computeQueryShape ?? emptyQueryShape
280
292
  const detectLocale = stages.detectLocale ?? defaultDetectLocale
281
293
  const classifyKind = stages.classifyKind ?? defaultClassifyKind
282
294
 
283
- throwIfAborted(opts)
284
- const normalized = rewriteLineBreaks(normalize(raw, { locale: opts?.locale }), opts?.lineBreaks ?? "keep")
285
- timing["normalize"] = performance.now() - t0
295
+ const normalized = await runStage(
296
+ {
297
+ id: StageID.Normalize,
298
+ run: (text: string) => rewriteLineBreaks(normalize(text, { locale: opts?.locale }), opts?.lineBreaks ?? "keep"),
299
+ },
300
+ raw,
301
+ context
302
+ )
286
303
 
287
304
  // The coarse placer adds a country prior unless the caller already supplied one.
288
305
  let effectiveOpts = opts
289
306
  let placerAnchorApplied = false
290
307
 
291
308
  if (stages.placeCountry) {
292
- const tPlace = performance.now()
293
- const placed = stages.placeCountry(normalized.normalized)
294
- timing["place-country"] = performance.now() - tPlace
309
+ const placeCountry = stages.placeCountry
310
+
311
+ const placed = await runStage(
312
+ { id: StageID.PlaceCountry, run: (text: string) => placeCountry(text) },
313
+ normalized.normalized,
314
+ context
315
+ )
295
316
 
296
317
  if (placed.country && placed.country !== "OTHER" && !opts?.resolveOpts?.anchorPosterior) {
297
318
  const hardCountry = hardCountryFor(
@@ -319,30 +340,39 @@ export async function runPipeline(
319
340
  }
320
341
  }
321
342
 
322
- throwIfAborted(opts)
323
- const tQs = performance.now()
324
- const queryShape = computeQueryShape(normalized)
325
- timing["query-shape"] = performance.now() - tQs
326
-
327
- throwIfAborted(opts)
328
- const tLocale = performance.now()
329
- const locale = await detectLocale(normalized, queryShape, { hint: opts?.locale })
330
- timing["locale-hint"] = performance.now() - tLocale
343
+ const queryShape = await runStage(
344
+ { id: StageID.QueryShape, run: (input: NormalizedInputLite) => computeQueryShape(input) },
345
+ normalized,
346
+ context
347
+ )
348
+
349
+ const locale = await runStage(
350
+ {
351
+ id: StageID.LocaleHint,
352
+ run: (input: NormalizedInputLite) => detectLocale(input, queryShape, { hint: opts?.locale }),
353
+ },
354
+ normalized,
355
+ context
356
+ )
331
357
 
332
- throwIfAborted(opts)
333
- const tKind = performance.now()
334
- const kind = await classifyKind(normalized, queryShape, locale)
335
- timing["kind-classifier"] = performance.now() - tKind
358
+ const kind = await runStage(
359
+ { id: StageID.KindClassifier, run: (input: NormalizedInputLite) => classifyKind(input, queryShape, locale) },
360
+ normalized,
361
+ context
362
+ )
336
363
 
337
364
  const intentMarkers: QueryIntentMarker[] = [...(kind.intentMarkers ?? [])]
338
365
 
339
366
  // A POI-shaped query goes to the intent stage first.
340
367
  // A null result falls through to parsing.
341
368
  if ((kind.kind === "poi_query" || kind.kind === "poi_category") && stages.poiIntent) {
342
- throwIfAborted(opts)
343
- const tPoi = performance.now()
344
- const poiOutcome = await stages.poiIntent(normalized, locale, effectiveOpts)
345
- timing["poi-intent"] = performance.now() - tPoi
369
+ const poiIntent = stages.poiIntent
370
+
371
+ const poiOutcome = await runStage(
372
+ { id: StageID.POIIntent, run: (input: NormalizedInputLite) => poiIntent(input, locale, effectiveOpts) },
373
+ normalized,
374
+ context
375
+ )
346
376
 
347
377
  if (poiOutcome) {
348
378
  const emptyTree: AddressTree = { raw: normalized.normalized, roots: [] }
@@ -369,10 +399,7 @@ export async function runPipeline(
369
399
  let tree = buildFastPathTree(normalized.normalized, kind, queryShape)
370
400
 
371
401
  if (stages.resolver) {
372
- throwIfAborted(opts)
373
- const tResolve = performance.now()
374
- tree = await safeResolve(faults, stages.resolver, tree, effectiveOpts)
375
- timing["resolve"] = performance.now() - tResolve
402
+ tree = await runStage(resolveStage(stages.resolver, effectiveOpts), tree, context)
376
403
  }
377
404
 
378
405
  return {
@@ -394,50 +421,57 @@ export async function runPipeline(
394
421
  let phraseProposals: PhraseProposal[] = []
395
422
 
396
423
  if (stages.groupPhrases) {
397
- throwIfAborted(opts)
398
- const tGroup = performance.now()
399
- phraseProposals = await safeGroupPhrases(faults, stages.groupPhrases, normalized, queryShape, locale)
400
- timing["phrase-grouper"] = performance.now() - tGroup
424
+ const groupPhrases = stages.groupPhrases
425
+
426
+ phraseProposals = await runStage(
427
+ {
428
+ id: StageID.PhraseGrouper,
429
+ run: (input: NormalizedInputLite) => groupPhrases(input, queryShape, locale),
430
+ recover: (): PhraseProposal[] => [],
431
+ },
432
+ normalized,
433
+ context
434
+ )
401
435
  }
402
436
 
403
437
  let tree: AddressTree = { raw: normalized.normalized, roots: [] }
404
438
 
405
439
  if (stages.classifier) {
406
- throwIfAborted(opts)
407
- const tClassify = performance.now()
408
-
409
- tree = await safeClassify(faults, stages.classifier, normalized.normalized, queryShape, {
410
- fst: stages.fst ?? null,
411
- caseNormalization: opts?.caseNormalization ?? DEFAULT_CASE_NORMALIZATION,
412
- placetypePair: opts?.placetypePair ?? "inherit",
413
- streetMorphology: stages.streetMorphology ?? null,
414
- streetMorphologyPrior: stages.streetMorphologyPrior ?? false,
415
- anchorRegions: stages.anchorRegions ?? null,
416
- venueHead: stages.venueHead?.(locale) ?? null,
417
- inputMode: deriveInputMode(opts?.inputMode ?? "auto", kind.kind),
418
- localeHint: opts?.localeHint ?? null,
419
- })
420
-
421
- timing["token-classify"] = performance.now() - tClassify
440
+ tree = await runStage(
441
+ classifyStage(stages.classifier, queryShape, {
442
+ fst: stages.fst ?? null,
443
+ caseNormalization: opts?.caseNormalization ?? DEFAULT_CASE_NORMALIZATION,
444
+ placetypePair: opts?.placetypePair ?? "inherit",
445
+ streetMorphology: stages.streetMorphology ?? null,
446
+ streetMorphologyPrior: stages.streetMorphologyPrior ?? false,
447
+ anchorRegions: stages.anchorRegions ?? null,
448
+ venueHead: stages.venueHead?.(locale) ?? null,
449
+ inputMode: deriveInputMode(opts?.inputMode ?? "auto", kind.kind),
450
+ localeHint: opts?.localeHint ?? null,
451
+ }),
452
+ normalized.normalized,
453
+ context
454
+ )
422
455
  }
423
456
 
424
457
  if (phraseProposals.length && tree.roots.length >= 0) {
425
- const tAudit = performance.now()
426
- tree = grouperAudit(tree, phraseProposals, normalized.normalized)
427
- timing["grouper-audit"] = performance.now() - tAudit
458
+ tree = await runStage(
459
+ {
460
+ id: StageID.GrouperAudit,
461
+ run: (decoded: AddressTree) => grouperAudit(decoded, phraseProposals, normalized.normalized),
462
+ },
463
+ tree,
464
+ context
465
+ )
428
466
  }
429
467
 
430
468
  if (stages.resolver) {
431
- throwIfAborted(opts)
432
- const tResolve = performance.now()
433
-
434
469
  // A bare locality or postcode query resolves without the placer's prior.
435
470
  if (placerAnchorApplied && (isBareLocalityTree(tree) || isBarePostcodeTree(tree))) {
436
471
  effectiveOpts = opts
437
472
  }
438
473
 
439
- tree = await safeResolve(faults, stages.resolver, tree, effectiveOpts)
440
- timing["resolve"] = performance.now() - tResolve
474
+ tree = await runStage(resolveStage(stages.resolver, effectiveOpts), tree, context)
441
475
  }
442
476
 
443
477
  return {
@@ -457,36 +491,12 @@ export async function runPipeline(
457
491
  }
458
492
 
459
493
  /**
460
- * Throws the signal's abort reason.
494
+ * The stage that runs the classifier over the normalized text.
461
495
  *
462
- * The pipeline checks between stages, so a running stage finishes first.
463
- */
464
- function throwIfAborted(opts?: PipelineOpts): void {
465
- if (opts?.signal?.aborted) {
466
- throw opts.signal.reason ?? new DOMException("Pipeline aborted", "AbortError")
467
- }
468
- }
469
-
470
- /**
471
- * Records a stage failure so callers can tell degraded output from a clean result.
496
+ * When the classifier throws, the run continues with an empty tree.
472
497
  */
473
- function recordFault(faults: PipelineFault[], stage: PipelineFaultStage, cause: unknown): void {
474
- faults.push({
475
- stage,
476
- name: cause instanceof Error ? cause.name : "Error",
477
- message: errorMessage(cause),
478
- cause,
479
- })
480
- }
481
-
482
- /**
483
- * Runs the classifier.
484
- * On failure it records a fault and returns an empty tree.
485
- */
486
- async function safeClassify(
487
- faults: PipelineFault[],
498
+ function classifyStage(
488
499
  classifier: AddressClassifier,
489
- text: string,
490
500
  queryShape: QueryShapeLite,
491
501
  knobs: {
492
502
  fst: FSTMatcherLike | null
@@ -499,7 +509,7 @@ async function safeClassify(
499
509
  inputMode: InputMode
500
510
  localeHint: string | null
501
511
  }
502
- ): Promise<AddressTree> {
512
+ ): PipelineStage<string, AddressTree> {
503
513
  const {
504
514
  fst,
505
515
  caseNormalization,
@@ -512,25 +522,24 @@ async function safeClassify(
512
522
  localeHint,
513
523
  } = knobs
514
524
 
515
- try {
516
- return await classifier.parse(text, {
517
- queryShape,
518
- inputMode,
519
- ...(localeHint ? { localeHint } : {}),
520
- fst,
521
- postcodeRepair: true,
522
- caseNormalization,
523
- enforceWordConsistency: WORD_CONSISTENCY_SHIP_DEFAULT,
524
- placetypePair,
525
- ...streetContextRequirementFor({ fst, streetMorphology }, { streetMorphologyPrior }),
526
- ...(fst && anchorRegions ? { anchorRegions } : {}),
527
- ...(venueHead ? { venueHead: venueHead.venueHead } : {}),
528
- ...(venueHead?.venueHeadOpts ? { venueHeadOpts: venueHead.venueHeadOpts } : {}),
529
- })
530
- } catch (error) {
531
- recordFault(faults, PipelineFaultStage.Classifier, error)
532
-
533
- return { raw: text, roots: [] }
525
+ return {
526
+ id: StageID.TokenClassify,
527
+ run: (text) =>
528
+ classifier.parse(text, {
529
+ queryShape,
530
+ inputMode,
531
+ ...(localeHint ? { localeHint } : {}),
532
+ fst,
533
+ postcodeRepair: true,
534
+ caseNormalization,
535
+ enforceWordConsistency: WORD_CONSISTENCY_SHIP_DEFAULT,
536
+ placetypePair,
537
+ ...streetContextRequirementFor({ fst, streetMorphology }, { streetMorphologyPrior }),
538
+ ...(fst && anchorRegions ? { anchorRegions } : {}),
539
+ ...(venueHead ? { venueHead: venueHead.venueHead } : {}),
540
+ ...(venueHead?.venueHeadOpts ? { venueHeadOpts: venueHead.venueHeadOpts } : {}),
541
+ }),
542
+ recover: (text) => ({ raw: text, roots: [] }),
534
543
  }
535
544
  }
536
545
 
@@ -585,26 +594,6 @@ export function streetContextRequirementFor(
585
594
  : {}
586
595
  }
587
596
 
588
- /**
589
- * Runs the phrase grouper.
590
- * On failure it records a fault and returns no proposals.
591
- */
592
- async function safeGroupPhrases(
593
- faults: PipelineFault[],
594
- groupPhrases: NonNullable<RuntimePipelineStages["groupPhrases"]>,
595
- normalized: NormalizedInputLite,
596
- shape: QueryShapeLite,
597
- locale: LocaleHint
598
- ): Promise<PhraseProposal[]> {
599
- try {
600
- return await groupPhrases(normalized, shape, locale)
601
- } catch (error) {
602
- recordFault(faults, PipelineFaultStage.PhraseGrouper, error)
603
-
604
- return []
605
- }
606
- }
607
-
608
597
  // MARK: Grouper-audit pass
609
598
 
610
599
  /**
@@ -681,21 +670,17 @@ export function grouperAudit(tree: AddressTree, proposals: PhraseProposal[], tex
681
670
  }
682
671
 
683
672
  /**
684
- * Runs the resolver.
673
+ * The stage that resolves the decoded tree.
685
674
  *
686
- * On failure it records a fault and returns the unresolved tree.
675
+ * When the resolver throws, the run continues with the unresolved tree.
687
676
  */
688
- async function safeResolve(
689
- faults: PipelineFault[],
677
+ function resolveStage(
690
678
  resolver: NonNullable<RuntimePipelineStages["resolver"]>,
691
- tree: AddressTree,
692
679
  opts?: PipelineOpts
693
- ): Promise<AddressTree> {
694
- try {
695
- return await resolver.resolveTree(tree, opts?.resolveOpts)
696
- } catch (error) {
697
- recordFault(faults, PipelineFaultStage.Resolver, error)
698
-
699
- return tree
680
+ ): PipelineStage<AddressTree, AddressTree> {
681
+ return {
682
+ id: StageID.Resolve,
683
+ run: (tree) => resolver.resolveTree(tree, opts?.resolveOpts),
684
+ recover: (tree) => tree,
700
685
  }
701
686
  }
@@ -0,0 +1,290 @@
1
+ // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
2
+ // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
3
+
4
+ /**
5
+ * A pipeline stage, the context every stage of one request reads, and the runner.
6
+ *
7
+ * A stage is an object with an `id` and a `run` function.
8
+ * The runner checks the abort signal, times the stage and captures its fault,
9
+ * so a stage does none of the three.
10
+ *
11
+ * The parse pipeline and the geocode path run their stages through {@linkcode runStage}.
12
+ */
13
+
14
+ import { decodeAsOutlineLines } from "#decoder/serialize/outline"
15
+ import type { AddressTree } from "#decoder/types"
16
+ import { errorMessage } from "#errors/schema"
17
+ import type { PipelineTiming } from "#pipeline/types"
18
+
19
+ /**
20
+ * The identifier of each stage of the parse pipeline, spelled as its timing keys were first published.
21
+ * A timing record keys a stage's duration by its identifier.
22
+ *
23
+ * A package that runs stages of its own declares their identifiers beside them,
24
+ * as `GeocodeStageID` in `mailwoman` does, so a new stage there changes zero lines here.
25
+ */
26
+ export const StageID = {
27
+ Normalize: "normalize",
28
+ PlaceCountry: "place-country",
29
+ QueryShape: "query-shape",
30
+ LocaleHint: "locale-hint",
31
+ KindClassifier: "kind-classifier",
32
+ POIIntent: "poi-intent",
33
+ PhraseGrouper: "phrase-grouper",
34
+ TokenClassify: "token-classify",
35
+ GrouperAudit: "grouper-audit",
36
+ Resolve: "resolve",
37
+ } as const
38
+
39
+ /**
40
+ * One of the {@link StageID} values.
41
+ */
42
+ export type StageID = (typeof StageID)[keyof typeof StageID]
43
+
44
+ /**
45
+ * The two stages a fault spells differently from the stage's identifier.
46
+ *
47
+ * `mailwoman parse` prints a fault's `stage`, so each spelling stays as it was first published.
48
+ */
49
+ const FAULT_STAGE_SPELLINGS: Readonly<Record<string, string>> = {
50
+ [StageID.TokenClassify]: "classifier",
51
+ [StageID.Resolve]: "resolver",
52
+ }
53
+
54
+ /**
55
+ * A stage of the parse pipeline as a fault spells it: `classifier` for the decode,
56
+ * `resolver` for the resolve, and the stage's identifier for every other stage.
57
+ */
58
+ export type PipelineFaultStage =
59
+ | Exclude<StageID, typeof StageID.TokenClassify | typeof StageID.Resolve>
60
+ | "classifier"
61
+ | "resolver"
62
+
63
+ /**
64
+ * One failure that a request caught and degraded past.
65
+ *
66
+ * `runPipeline` still resolves.
67
+ * `tree` contains whatever the remaining stages could prove.
68
+ *
69
+ * @typeParam Stage The spellings of the stages that can fault.
70
+ * The parse pipeline's result states {@link PipelineFaultStage}, and a context with stages of another package states `string`.
71
+ */
72
+ export interface PipelineFault<Stage extends string = string> {
73
+ /**
74
+ * The stage that threw, as a fault spells it.
75
+ */
76
+ stage: Stage
77
+ /**
78
+ * The thrown value's `name`, or `"Error"` when something that isn't an `Error` was thrown.
79
+ *
80
+ * It is machine-stable enough to branch on, with the `cause` carrying the rest.
81
+ */
82
+ name: string
83
+ message: string
84
+ /**
85
+ * The value the stage threw, verbatim: not JSON-serializable in the useful sense,
86
+ * so serialize `stage`/`name`/`message` when you need this on a wire.
87
+ */
88
+ cause: unknown
89
+ }
90
+
91
+ /**
92
+ * What a stage that rewrites the parse tree changed, as outline lines of the tree.
93
+ *
94
+ * A line reads `tag [start, end): "value"` with the node's metadata after it, indented two
95
+ * spaces for each level, so a node that only moved under another parent appears in both lists.
96
+ */
97
+ export interface TreeRewriteRecord {
98
+ kind: "tree-rewrite"
99
+ /**
100
+ * The identifier of the stage that rewrote the tree.
101
+ */
102
+ stage: string
103
+ /**
104
+ * The lines the tree had before the stage and lacks after it.
105
+ */
106
+ removed: string[]
107
+ /**
108
+ * The lines the tree has after the stage and lacked before it.
109
+ */
110
+ added: string[]
111
+ }
112
+
113
+ /**
114
+ * A record that a stage of this package writes to a request's trace.
115
+ */
116
+ export type StageRecord = TreeRewriteRecord
117
+
118
+ /**
119
+ * What every stage of one request reads and writes.
120
+ *
121
+ * @typeParam Trace The records the request's stages write.
122
+ * @typeParam FaultStage The spellings of the stages whose failures the request records.
123
+ */
124
+ export interface RequestContext<Trace = StageRecord, FaultStage extends string = string> {
125
+ /**
126
+ * The signal that cancels the request, or null for a request that can't be cancelled.
127
+ */
128
+ readonly signal: AbortSignal | null
129
+
130
+ /**
131
+ * Receives each record a stage writes, or null when the request records none.
132
+ *
133
+ * A stage builds a record only after it has read a sink here.
134
+ */
135
+ readonly trace: ((record: Trace) => void) | null
136
+
137
+ /**
138
+ * Whether a stage also writes the records that cost work to build, such as the
139
+ * outlines of a tree rewrite or the decoder's trace.
140
+ *
141
+ * A stage writes a record it already holds, such as a decision it made, whenever {@link trace} is set.
142
+ */
143
+ readonly detail: boolean
144
+
145
+ /**
146
+ * The milliseconds each stage has taken, summed when a stage runs more than once.
147
+ */
148
+ readonly timing: PipelineTiming
149
+
150
+ /**
151
+ * Every failure the request caught and degraded past, in order.
152
+ */
153
+ readonly faults: PipelineFault<FaultStage>[]
154
+ }
155
+
156
+ /**
157
+ * Builds the context of one request.
158
+ *
159
+ * `detail` defaults to whether the request has a trace, so a caller states it only to
160
+ * receive the records a stage already holds without the costly ones.
161
+ */
162
+ export function createRequestContext<Trace = StageRecord, FaultStage extends string = string>(
163
+ init: { signal?: AbortSignal | null; trace?: ((record: Trace) => void) | null; detail?: boolean } = {}
164
+ ): RequestContext<Trace, FaultStage> {
165
+ const trace = init.trace ?? null
166
+
167
+ return { signal: init.signal ?? null, trace, detail: init.detail ?? trace != null, timing: {}, faults: [] }
168
+ }
169
+
170
+ /**
171
+ * Records a failure the request degrades past, under `stage`.
172
+ *
173
+ * The runner calls it for a stage that declares `recover`.
174
+ * A stage calls it for a failure of work the answer doesn't depend on, such as building a trace.
175
+ */
176
+ export function recordFault(context: Pick<RequestContext<never>, "faults">, stage: string, cause: unknown): void {
177
+ context.faults.push({
178
+ stage,
179
+ name: cause instanceof Error ? cause.name : "Error",
180
+ message: errorMessage(cause),
181
+ cause,
182
+ })
183
+ }
184
+
185
+ /**
186
+ * One step of a pipeline.
187
+ *
188
+ * @typeParam Input What the stage reads.
189
+ * @typeParam Output What the stage returns.
190
+ * @typeParam Trace The records the stage writes.
191
+ * @typeParam ID The stage identifiers of the pipeline the stage belongs to.
192
+ */
193
+ export interface PipelineStage<Input, Output, Trace = StageRecord, ID extends string = string> {
194
+ /**
195
+ * The stage's identifier.
196
+ * The context's timing keys the stage's duration by it.
197
+ */
198
+ readonly id: ID
199
+ run(input: Input, context: RequestContext<Trace>): Output | Promise<Output>
200
+
201
+ /**
202
+ * The output the pipeline continues with when `run` throws.
203
+ *
204
+ * The runner records a fault for a stage that declares it, and rethrows for a stage that leaves it out.
205
+ */
206
+ recover?(input: Input): Output
207
+ }
208
+
209
+ /**
210
+ * Runs one stage: checks the abort signal, runs the stage, and adds its duration to the context's timing.
211
+ *
212
+ * An aborted request rejects with the signal's reason, the value the cancelling code aborted with.
213
+ * `abortKindOf` in `#async/abort` reads that reason's kind.
214
+ *
215
+ * The signal is checked before the stage, so a running stage finishes first.
216
+ *
217
+ * A stage that declares `recover` and throws has a fault recorded, and the run
218
+ * continues with the recovered output.
219
+ */
220
+ export async function runStage<Input, Output, Trace>(
221
+ stage: PipelineStage<Input, Output, Trace>,
222
+ input: Input,
223
+ context: RequestContext<Trace>
224
+ ): Promise<Output> {
225
+ if (context.signal?.aborted) {
226
+ throw context.signal.reason ?? new DOMException("Pipeline aborted", "AbortError")
227
+ }
228
+
229
+ const startedAt = performance.now()
230
+
231
+ try {
232
+ return await stage.run(input, context)
233
+ } catch (error) {
234
+ if (!stage.recover) throw error
235
+
236
+ recordFault(context, FAULT_STAGE_SPELLINGS[stage.id] ?? stage.id, error)
237
+
238
+ return stage.recover(input)
239
+ } finally {
240
+ context.timing[stage.id] = (context.timing[stage.id] ?? 0) + (performance.now() - startedAt)
241
+ }
242
+ }
243
+
244
+ /**
245
+ * Runs stages in order, each reading the output of the stage before it.
246
+ */
247
+ export async function runStages<Value, Trace>(
248
+ stages: readonly PipelineStage<Value, Value, Trace>[],
249
+ input: Value,
250
+ context: RequestContext<Trace>
251
+ ): Promise<Value> {
252
+ let value = input
253
+
254
+ for (const stage of stages) {
255
+ value = await runStage(stage, value, context)
256
+ }
257
+
258
+ return value
259
+ }
260
+
261
+ /**
262
+ * A stage that rewrites the parse tree and writes what it changed to the trace.
263
+ *
264
+ * It writes a {@linkcode TreeRewriteRecord} only when the request traces in detail
265
+ * and the tree's outline changed.
266
+ * The rewrite may change the tree in place, so the stage reads the outline before the rewrite runs.
267
+ */
268
+ export function treeRewriteStage<ID extends string>(
269
+ id: ID,
270
+ rewrite: (tree: AddressTree) => AddressTree
271
+ ): PipelineStage<AddressTree, AddressTree, TreeRewriteRecord, ID> {
272
+ return {
273
+ id,
274
+ run(tree, context) {
275
+ if (!context.trace || !context.detail) return rewrite(tree)
276
+
277
+ const before = decodeAsOutlineLines(tree, { metadata: true })
278
+ const rewritten = rewrite(tree)
279
+ const after = decodeAsOutlineLines(rewritten, { metadata: true })
280
+ const removed = before.filter((line) => !after.includes(line))
281
+ const added = after.filter((line) => !before.includes(line))
282
+
283
+ if (removed.length || added.length) {
284
+ context.trace({ kind: "tree-rewrite", stage: id, removed, added })
285
+ }
286
+
287
+ return rewritten
288
+ },
289
+ }
290
+ }