@scriptc/compiler 0.4.2 → 0.4.3

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 (636) hide show
  1. package/ambient/scriptc-node-fallback.d.ts +397 -37
  2. package/dist/backend/cycle-analysis.d.ts +4 -1
  3. package/dist/backend/cycle-analysis.js +6 -4
  4. package/dist/backend/cycle-analysis.js.map +1 -1
  5. package/dist/backend/cycle-analysis.test.js +55 -0
  6. package/dist/backend/cycle-analysis.test.js.map +1 -1
  7. package/dist/backend/cycle-mutability.d.ts +7 -0
  8. package/dist/backend/cycle-mutability.js +19 -1
  9. package/dist/backend/cycle-mutability.js.map +1 -1
  10. package/dist/backend/executable-features.js +2 -0
  11. package/dist/backend/executable-features.js.map +1 -1
  12. package/dist/backend/immutability.d.ts +6 -3
  13. package/dist/backend/immutability.js +33 -29
  14. package/dist/backend/immutability.js.map +1 -1
  15. package/dist/backend/library-pack.js +1 -0
  16. package/dist/backend/library-pack.js.map +1 -1
  17. package/dist/backend/llvm/alloc.d.ts +16 -1
  18. package/dist/backend/llvm/alloc.js +54 -4
  19. package/dist/backend/llvm/alloc.js.map +1 -1
  20. package/dist/backend/llvm/appended-array-literals.d.ts +2 -1
  21. package/dist/backend/llvm/appended-array-literals.js +44 -36
  22. package/dist/backend/llvm/appended-array-literals.js.map +1 -1
  23. package/dist/backend/llvm/appended-array-literals.test.js +4 -3
  24. package/dist/backend/llvm/appended-array-literals.test.js.map +1 -1
  25. package/dist/backend/llvm/array-search.d.ts +4 -3
  26. package/dist/backend/llvm/array-search.js +21 -9
  27. package/dist/backend/llvm/array-search.js.map +1 -1
  28. package/dist/backend/llvm/blocks.d.ts +1 -1
  29. package/dist/backend/llvm/blocks.js +2 -2
  30. package/dist/backend/llvm/blocks.js.map +1 -1
  31. package/dist/backend/llvm/byte-windows.js +2 -0
  32. package/dist/backend/llvm/byte-windows.js.map +1 -1
  33. package/dist/backend/llvm/bytes-alias.d.ts +6 -1
  34. package/dist/backend/llvm/bytes-alias.js +8 -2
  35. package/dist/backend/llvm/bytes-alias.js.map +1 -1
  36. package/dist/backend/llvm/bytes-loop-versions.d.ts +47 -5
  37. package/dist/backend/llvm/bytes-loop-versions.js +473 -32
  38. package/dist/backend/llvm/bytes-loop-versions.js.map +1 -1
  39. package/dist/backend/llvm/bytes-loop-versions.test.js +258 -2
  40. package/dist/backend/llvm/bytes-loop-versions.test.js.map +1 -1
  41. package/dist/backend/llvm/checked-value-lifetimes.js +2 -0
  42. package/dist/backend/llvm/checked-value-lifetimes.js.map +1 -1
  43. package/dist/backend/llvm/classes.d.ts +7 -5
  44. package/dist/backend/llvm/classes.js +44 -20
  45. package/dist/backend/llvm/classes.js.map +1 -1
  46. package/dist/backend/llvm/classes.test.js +167 -7
  47. package/dist/backend/llvm/classes.test.js.map +1 -1
  48. package/dist/backend/llvm/common.d.ts +9 -3
  49. package/dist/backend/llvm/common.js +9 -3
  50. package/dist/backend/llvm/common.js.map +1 -1
  51. package/dist/backend/llvm/constant-callbacks.d.ts +2 -1
  52. package/dist/backend/llvm/constant-callbacks.js +83 -52
  53. package/dist/backend/llvm/constant-callbacks.js.map +1 -1
  54. package/dist/backend/llvm/counted-loops.d.ts +7 -0
  55. package/dist/backend/llvm/counted-loops.js +18 -0
  56. package/dist/backend/llvm/counted-loops.js.map +1 -1
  57. package/dist/backend/llvm/dense-array-access.d.ts +39 -8
  58. package/dist/backend/llvm/dense-array-access.js +222 -52
  59. package/dist/backend/llvm/dense-array-access.js.map +1 -1
  60. package/dist/backend/llvm/dense-array-access.test.js +36 -0
  61. package/dist/backend/llvm/dense-array-access.test.js.map +1 -1
  62. package/dist/backend/llvm/dense-array-loops.d.ts +17 -1
  63. package/dist/backend/llvm/dense-array-loops.js +194 -5
  64. package/dist/backend/llvm/dense-array-loops.js.map +1 -1
  65. package/dist/backend/llvm/dense-array-loops.test.js +90 -1
  66. package/dist/backend/llvm/dense-array-loops.test.js.map +1 -1
  67. package/dist/backend/llvm/dyn-handle-tags.test.js +6 -1
  68. package/dist/backend/llvm/dyn-handle-tags.test.js.map +1 -1
  69. package/dist/backend/llvm/dyn.d.ts +26 -0
  70. package/dist/backend/llvm/dyn.js +352 -61
  71. package/dist/backend/llvm/dyn.js.map +1 -1
  72. package/dist/backend/llvm/emit-function.js +11 -20
  73. package/dist/backend/llvm/emit-function.js.map +1 -1
  74. package/dist/backend/llvm/emit-journal.d.ts +93 -0
  75. package/dist/backend/llvm/emit-journal.js +170 -0
  76. package/dist/backend/llvm/emit-journal.js.map +1 -0
  77. package/dist/backend/llvm/emit-module.js +195 -27
  78. package/dist/backend/llvm/emit-module.js.map +1 -1
  79. package/dist/backend/llvm/emit-statements.d.ts +0 -4
  80. package/dist/backend/llvm/emit-statements.js +268 -84
  81. package/dist/backend/llvm/emit-statements.js.map +1 -1
  82. package/dist/backend/llvm/emitter-state.d.ts +7 -0
  83. package/dist/backend/llvm/emitter-state.js +14 -0
  84. package/dist/backend/llvm/emitter-state.js.map +1 -0
  85. package/dist/backend/llvm/emitter.d.ts +176 -37
  86. package/dist/backend/llvm/emitter.js +724 -85
  87. package/dist/backend/llvm/emitter.js.map +1 -1
  88. package/dist/backend/llvm/emitter.test.js +54 -5
  89. package/dist/backend/llvm/emitter.test.js.map +1 -1
  90. package/dist/backend/llvm/expr-bytes.js +34 -12
  91. package/dist/backend/llvm/expr-bytes.js.map +1 -1
  92. package/dist/backend/llvm/expr-callbacks.js +47 -0
  93. package/dist/backend/llvm/expr-callbacks.js.map +1 -1
  94. package/dist/backend/llvm/expr-calls.js +28 -14
  95. package/dist/backend/llvm/expr-calls.js.map +1 -1
  96. package/dist/backend/llvm/expr-containers.d.ts +14 -2
  97. package/dist/backend/llvm/expr-containers.js +129 -37
  98. package/dist/backend/llvm/expr-containers.js.map +1 -1
  99. package/dist/backend/llvm/expr-context.d.ts +14 -0
  100. package/dist/backend/llvm/expr-dynamic.js +18 -11
  101. package/dist/backend/llvm/expr-dynamic.js.map +1 -1
  102. package/dist/backend/llvm/expr-primitives.js +100 -28
  103. package/dist/backend/llvm/expr-primitives.js.map +1 -1
  104. package/dist/backend/llvm/expr-primitives.test.js +11 -6
  105. package/dist/backend/llvm/expr-primitives.test.js.map +1 -1
  106. package/dist/backend/llvm/expr-records.d.ts +12 -0
  107. package/dist/backend/llvm/expr-records.js +106 -0
  108. package/dist/backend/llvm/expr-records.js.map +1 -1
  109. package/dist/backend/llvm/expr-stream-bridges.d.ts +2 -2
  110. package/dist/backend/llvm/expr-stream-bridges.js +194 -65
  111. package/dist/backend/llvm/expr-stream-bridges.js.map +1 -1
  112. package/dist/backend/llvm/expr-stream-bridges.test.js +1 -1
  113. package/dist/backend/llvm/expr-stream-bridges.test.js.map +1 -1
  114. package/dist/backend/llvm/expr-stream-callbacks.d.ts +2 -2
  115. package/dist/backend/llvm/expr-stream-callbacks.js +74 -20
  116. package/dist/backend/llvm/expr-stream-callbacks.js.map +1 -1
  117. package/dist/backend/llvm/int32-kind-loops.d.ts +39 -0
  118. package/dist/backend/llvm/int32-kind-loops.js +106 -0
  119. package/dist/backend/llvm/int32-kind-loops.js.map +1 -0
  120. package/dist/backend/llvm/integer-or-nan-indexes.test.d.ts +1 -0
  121. package/dist/backend/llvm/integer-or-nan-indexes.test.js +118 -0
  122. package/dist/backend/llvm/integer-or-nan-indexes.test.js.map +1 -0
  123. package/dist/backend/llvm/integer-values.d.ts +20 -1
  124. package/dist/backend/llvm/integer-values.js +71 -0
  125. package/dist/backend/llvm/integer-values.js.map +1 -1
  126. package/dist/backend/llvm/js-ic.d.ts +1 -1
  127. package/dist/backend/llvm/js-ic.js +1 -1
  128. package/dist/backend/llvm/js-ic.js.map +1 -1
  129. package/dist/backend/llvm/key-orders.d.ts +7 -0
  130. package/dist/backend/llvm/key-orders.js +33 -0
  131. package/dist/backend/llvm/key-orders.js.map +1 -0
  132. package/dist/backend/llvm/lib-connect.js +4 -0
  133. package/dist/backend/llvm/lib-connect.js.map +1 -1
  134. package/dist/backend/llvm/lib-datagram.js +11 -0
  135. package/dist/backend/llvm/lib-datagram.js.map +1 -1
  136. package/dist/backend/llvm/lib-dispatch.js +80 -2
  137. package/dist/backend/llvm/lib-dispatch.js.map +1 -1
  138. package/dist/backend/llvm/lib-filesystem.js +4 -4
  139. package/dist/backend/llvm/lib-filesystem.js.map +1 -1
  140. package/dist/backend/llvm/lib-http2.js +4 -0
  141. package/dist/backend/llvm/lib-http2.js.map +1 -1
  142. package/dist/backend/llvm/lib-lookup.js +16 -6
  143. package/dist/backend/llvm/lib-lookup.js.map +1 -1
  144. package/dist/backend/llvm/lib-shared.js +73 -2
  145. package/dist/backend/llvm/lib-shared.js.map +1 -1
  146. package/dist/backend/llvm/lib-stream.js +35 -5
  147. package/dist/backend/llvm/lib-stream.js.map +1 -1
  148. package/dist/backend/llvm/local-array-reads.js +115 -19
  149. package/dist/backend/llvm/local-array-reads.js.map +1 -1
  150. package/dist/backend/llvm/local-array-reads.test.js +14 -0
  151. package/dist/backend/llvm/local-array-reads.test.js.map +1 -1
  152. package/dist/backend/llvm/loop-state.d.ts +33 -0
  153. package/dist/backend/llvm/loop-state.js +42 -0
  154. package/dist/backend/llvm/loop-state.js.map +1 -0
  155. package/dist/backend/llvm/map-number-lookup.d.ts +26 -8
  156. package/dist/backend/llvm/map-number-lookup.js +91 -31
  157. package/dist/backend/llvm/map-number-lookup.js.map +1 -1
  158. package/dist/backend/llvm/map-number-lookup.test.js +55 -0
  159. package/dist/backend/llvm/map-number-lookup.test.js.map +1 -1
  160. package/dist/backend/llvm/map-read-lifetimes.js +4 -2
  161. package/dist/backend/llvm/map-read-lifetimes.js.map +1 -1
  162. package/dist/backend/llvm/nullable-unions.d.ts +2 -1
  163. package/dist/backend/llvm/nullable-unions.js +15 -3
  164. package/dist/backend/llvm/nullable-unions.js.map +1 -1
  165. package/dist/backend/llvm/publish.d.ts +10 -0
  166. package/dist/backend/llvm/publish.js +12 -0
  167. package/dist/backend/llvm/publish.js.map +1 -1
  168. package/dist/backend/llvm/receivers.js +6 -1
  169. package/dist/backend/llvm/receivers.js.map +1 -1
  170. package/dist/backend/llvm/record-results.d.ts +1 -9
  171. package/dist/backend/llvm/record-results.js +2 -11
  172. package/dist/backend/llvm/record-results.js.map +1 -1
  173. package/dist/backend/llvm/reference-writes.d.ts +7 -2
  174. package/dist/backend/llvm/reference-writes.js +89 -72
  175. package/dist/backend/llvm/reference-writes.js.map +1 -1
  176. package/dist/backend/llvm/running-extrema.d.ts +11 -0
  177. package/dist/backend/llvm/running-extrema.js +115 -0
  178. package/dist/backend/llvm/running-extrema.js.map +1 -0
  179. package/dist/backend/llvm/running-extrema.test.d.ts +1 -0
  180. package/dist/backend/llvm/running-extrema.test.js +89 -0
  181. package/dist/backend/llvm/running-extrema.test.js.map +1 -0
  182. package/dist/backend/llvm/shapes.d.ts +71 -15
  183. package/dist/backend/llvm/shapes.js +222 -33
  184. package/dist/backend/llvm/shapes.js.map +1 -1
  185. package/dist/backend/llvm/shapes.test.js +53 -7
  186. package/dist/backend/llvm/shapes.test.js.map +1 -1
  187. package/dist/backend/llvm/speculative-integers.d.ts +6 -0
  188. package/dist/backend/llvm/speculative-integers.js +2 -2
  189. package/dist/backend/llvm/speculative-integers.js.map +1 -1
  190. package/dist/backend/llvm/stack-objects.d.ts +3 -2
  191. package/dist/backend/llvm/stack-objects.js +5 -2
  192. package/dist/backend/llvm/stack-objects.js.map +1 -1
  193. package/dist/backend/llvm/string-reads.d.ts +12 -0
  194. package/dist/backend/llvm/string-reads.js +44 -0
  195. package/dist/backend/llvm/string-reads.js.map +1 -1
  196. package/dist/backend/llvm/walk-borrows.d.ts +3 -0
  197. package/dist/backend/llvm/walk-borrows.js +46 -41
  198. package/dist/backend/llvm/walk-borrows.js.map +1 -1
  199. package/dist/backend/llvm/walkers.d.ts +3 -0
  200. package/dist/backend/llvm/walkers.js +88 -65
  201. package/dist/backend/llvm/walkers.js.map +1 -1
  202. package/dist/backend/llvm/wrapping-sums.d.ts +16 -1
  203. package/dist/backend/llvm/wrapping-sums.js +126 -0
  204. package/dist/backend/llvm/wrapping-sums.js.map +1 -1
  205. package/dist/backend/may-throw.d.ts +8 -10
  206. package/dist/backend/may-throw.js +14 -10
  207. package/dist/backend/may-throw.js.map +1 -1
  208. package/dist/backend/native/contracts.d.ts +5 -0
  209. package/dist/backend/native/executable.js +7 -0
  210. package/dist/backend/native/executable.js.map +1 -1
  211. package/dist/backend/native/runtime-inputs.d.ts +1 -1
  212. package/dist/backend/native/runtime-inputs.js +1 -0
  213. package/dist/backend/native/runtime-inputs.js.map +1 -1
  214. package/dist/backend/native-link-info-core.d.ts +4 -0
  215. package/dist/backend/native-link-info-core.js.map +1 -1
  216. package/dist/backend/publication.d.ts +12 -1
  217. package/dist/backend/publication.js +37 -14
  218. package/dist/backend/publication.js.map +1 -1
  219. package/dist/backend/publication.test.js +30 -0
  220. package/dist/backend/publication.test.js.map +1 -1
  221. package/dist/backend/result-slots.d.ts +16 -0
  222. package/dist/backend/result-slots.js +23 -0
  223. package/dist/backend/result-slots.js.map +1 -0
  224. package/dist/backend/runtime-abi.d.ts +2 -2
  225. package/dist/backend/runtime-abi.js +2 -2
  226. package/dist/backend/runtime-pack-core.d.ts +1 -0
  227. package/dist/backend/runtime-pack-core.js +1 -0
  228. package/dist/backend/runtime-pack-core.js.map +1 -1
  229. package/dist/backend/runtime-pack-targets.test.js +2 -1
  230. package/dist/backend/runtime-pack-targets.test.js.map +1 -1
  231. package/dist/backend/runtime-pack.test.js +2 -1
  232. package/dist/backend/runtime-pack.test.js.map +1 -1
  233. package/dist/backend/stack-observations.d.ts +2 -1
  234. package/dist/backend/stack-observations.js +67 -42
  235. package/dist/backend/stack-observations.js.map +1 -1
  236. package/dist/backend/target-diagnostics.js +5 -3
  237. package/dist/backend/target-diagnostics.js.map +1 -1
  238. package/dist/backend/warm-cache.js +1 -0
  239. package/dist/backend/warm-cache.js.map +1 -1
  240. package/dist/cli/usage.d.ts +1 -1
  241. package/dist/cli/usage.js +3 -3
  242. package/dist/compat/fetch-profile.js +1 -1
  243. package/dist/compat/fetch-profile.js.map +1 -1
  244. package/dist/contracts/check.js +4 -3
  245. package/dist/contracts/check.js.map +1 -1
  246. package/dist/contracts/int32-elements.js +10 -2
  247. package/dist/contracts/int32-elements.js.map +1 -1
  248. package/dist/coverage/report.js +12 -3
  249. package/dist/coverage/report.js.map +1 -1
  250. package/dist/diagnostics/diagnostic.d.ts +4 -0
  251. package/dist/diagnostics/diagnostic.js.map +1 -1
  252. package/dist/diagnostics/envelope.d.ts +3 -0
  253. package/dist/diagnostics/envelope.js +1 -0
  254. package/dist/diagnostics/envelope.js.map +1 -1
  255. package/dist/executable/executable-cache.d.ts +2 -0
  256. package/dist/executable/executable-cache.js +1 -0
  257. package/dist/executable/executable-cache.js.map +1 -1
  258. package/dist/executable/executable-cache.test.js +1 -0
  259. package/dist/executable/executable-cache.test.js.map +1 -1
  260. package/dist/frontend/decl-projection.d.ts +41 -0
  261. package/dist/frontend/decl-projection.js +854 -0
  262. package/dist/frontend/decl-projection.js.map +1 -0
  263. package/dist/frontend/decl-projection.test.d.ts +1 -0
  264. package/dist/frontend/decl-projection.test.js +30 -0
  265. package/dist/frontend/decl-projection.test.js.map +1 -0
  266. package/dist/frontend/dts-paths.d.ts +15 -0
  267. package/dist/frontend/dts-paths.js +25 -0
  268. package/dist/frontend/dts-paths.js.map +1 -1
  269. package/dist/frontend/input-tracker.d.ts +6 -0
  270. package/dist/frontend/input-tracker.js +51 -28
  271. package/dist/frontend/input-tracker.js.map +1 -1
  272. package/dist/frontend/input-tracker.test.js +19 -0
  273. package/dist/frontend/input-tracker.test.js.map +1 -1
  274. package/dist/frontend/lowering/array-values.js +100 -2
  275. package/dist/frontend/lowering/array-values.js.map +1 -1
  276. package/dist/frontend/lowering/builtins/child-process-methods.js +46 -15
  277. package/dist/frontend/lowering/builtins/child-process-methods.js.map +1 -1
  278. package/dist/frontend/lowering/builtins/child-process.js +13 -2
  279. package/dist/frontend/lowering/builtins/child-process.js.map +1 -1
  280. package/dist/frontend/lowering/builtins/crypto.js +84 -9
  281. package/dist/frontend/lowering/builtins/crypto.js.map +1 -1
  282. package/dist/frontend/lowering/builtins/global-namespace-keys.d.ts +3 -0
  283. package/dist/frontend/lowering/builtins/global-namespace-keys.js +122 -0
  284. package/dist/frontend/lowering/builtins/global-namespace-keys.js.map +1 -0
  285. package/dist/frontend/lowering/builtins/numbers.js +9 -1
  286. package/dist/frontend/lowering/builtins/numbers.js.map +1 -1
  287. package/dist/frontend/lowering/builtins/promises.d.ts +6 -0
  288. package/dist/frontend/lowering/builtins/promises.js +88 -2
  289. package/dist/frontend/lowering/builtins/promises.js.map +1 -1
  290. package/dist/frontend/lowering/builtins/readline.d.ts +5 -1
  291. package/dist/frontend/lowering/builtins/readline.js +35 -3
  292. package/dist/frontend/lowering/builtins/readline.js.map +1 -1
  293. package/dist/frontend/lowering/builtins/url.js +24 -1
  294. package/dist/frontend/lowering/builtins/url.js.map +1 -1
  295. package/dist/frontend/lowering/builtins/webassembly.js +6 -3
  296. package/dist/frontend/lowering/builtins/webassembly.js.map +1 -1
  297. package/dist/frontend/lowering/class-callbacks.d.ts +5 -2
  298. package/dist/frontend/lowering/class-callbacks.js +82 -12
  299. package/dist/frontend/lowering/class-callbacks.js.map +1 -1
  300. package/dist/frontend/lowering/class-construction.d.ts +4 -1
  301. package/dist/frontend/lowering/class-construction.js +17 -3
  302. package/dist/frontend/lowering/class-construction.js.map +1 -1
  303. package/dist/frontend/lowering/class-dynamic-dispatch.d.ts +39 -2
  304. package/dist/frontend/lowering/class-dynamic-dispatch.js +766 -108
  305. package/dist/frontend/lowering/class-dynamic-dispatch.js.map +1 -1
  306. package/dist/frontend/lowering/class-dynamic-dispatch.test.js +47 -0
  307. package/dist/frontend/lowering/class-dynamic-dispatch.test.js.map +1 -1
  308. package/dist/frontend/lowering/class-iterables.d.ts +2 -1
  309. package/dist/frontend/lowering/class-iterables.js +9 -1
  310. package/dist/frontend/lowering/class-iterables.js.map +1 -1
  311. package/dist/frontend/lowering/class-method-mutations.js +32 -9
  312. package/dist/frontend/lowering/class-method-mutations.js.map +1 -1
  313. package/dist/frontend/lowering/class-prototypes.js +31 -2
  314. package/dist/frontend/lowering/class-prototypes.js.map +1 -1
  315. package/dist/frontend/lowering/class-runtime-statics.d.ts +9 -3
  316. package/dist/frontend/lowering/class-runtime-statics.js +29 -4
  317. package/dist/frontend/lowering/class-runtime-statics.js.map +1 -1
  318. package/dist/frontend/lowering/coercions/structural-plans.js +48 -1
  319. package/dist/frontend/lowering/coercions/structural-plans.js.map +1 -1
  320. package/dist/frontend/lowering/coercions/structural.js +91 -1
  321. package/dist/frontend/lowering/coercions/structural.js.map +1 -1
  322. package/dist/frontend/lowering/containers/array-callback-call.js +2 -2
  323. package/dist/frontend/lowering/containers/array-callback-call.js.map +1 -1
  324. package/dist/frontend/lowering/containers/array-callbacks.js +338 -18
  325. package/dist/frontend/lowering/containers/array-callbacks.js.map +1 -1
  326. package/dist/frontend/lowering/containers/array-concat.js +10 -0
  327. package/dist/frontend/lowering/containers/array-concat.js.map +1 -1
  328. package/dist/frontend/lowering/containers/array-methods.js +15 -0
  329. package/dist/frontend/lowering/containers/array-methods.js.map +1 -1
  330. package/dist/frontend/lowering/containers/array-pipelines.js +19 -8
  331. package/dist/frontend/lowering/containers/array-pipelines.js.map +1 -1
  332. package/dist/frontend/lowering/containers/bytes.js +2 -1
  333. package/dist/frontend/lowering/containers/bytes.js.map +1 -1
  334. package/dist/frontend/lowering/containers/callback-arguments.js +3 -0
  335. package/dist/frontend/lowering/containers/callback-arguments.js.map +1 -1
  336. package/dist/frontend/lowering/containers/collection-methods.js +7 -2
  337. package/dist/frontend/lowering/containers/collection-methods.js.map +1 -1
  338. package/dist/frontend/lowering/containers/filter-predicates.d.ts +6 -0
  339. package/dist/frontend/lowering/containers/filter-predicates.js +205 -0
  340. package/dist/frontend/lowering/containers/filter-predicates.js.map +1 -0
  341. package/dist/frontend/lowering/containers/indexed-objects.d.ts +0 -12
  342. package/dist/frontend/lowering/containers/indexed-objects.js +46 -7
  343. package/dist/frontend/lowering/containers/indexed-objects.js.map +1 -1
  344. package/dist/frontend/lowering/containers/string-and-regexp.js +55 -9
  345. package/dist/frontend/lowering/containers/string-and-regexp.js.map +1 -1
  346. package/dist/frontend/lowering/containers/string-replacement.js +43 -19
  347. package/dist/frontend/lowering/containers/string-replacement.js.map +1 -1
  348. package/dist/frontend/lowering/dispatch-sites.js +15 -1
  349. package/dist/frontend/lowering/dispatch-sites.js.map +1 -1
  350. package/dist/frontend/lowering/divergences.js +9 -1
  351. package/dist/frontend/lowering/divergences.js.map +1 -1
  352. package/dist/frontend/lowering/error-methods.js +47 -2
  353. package/dist/frontend/lowering/error-methods.js.map +1 -1
  354. package/dist/frontend/lowering/expressions/array-literals.js +47 -0
  355. package/dist/frontend/lowering/expressions/array-literals.js.map +1 -1
  356. package/dist/frontend/lowering/expressions/binary.js +36 -2
  357. package/dist/frontend/lowering/expressions/binary.js.map +1 -1
  358. package/dist/frontend/lowering/expressions/element-access.js +90 -13
  359. package/dist/frontend/lowering/expressions/element-access.js.map +1 -1
  360. package/dist/frontend/lowering/expressions/identifiers.js +33 -4
  361. package/dist/frontend/lowering/expressions/identifiers.js.map +1 -1
  362. package/dist/frontend/lowering/expressions/narrowing.js +95 -1
  363. package/dist/frontend/lowering/expressions/narrowing.js.map +1 -1
  364. package/dist/frontend/lowering/expressions/nullish-assignment.js +3 -1
  365. package/dist/frontend/lowering/expressions/nullish-assignment.js.map +1 -1
  366. package/dist/frontend/lowering/expressions/object-literals.d.ts +10 -0
  367. package/dist/frontend/lowering/expressions/object-literals.js +138 -51
  368. package/dist/frontend/lowering/expressions/object-literals.js.map +1 -1
  369. package/dist/frontend/lowering/expressions/optional-chains.js +12 -0
  370. package/dist/frontend/lowering/expressions/optional-chains.js.map +1 -1
  371. package/dist/frontend/lowering/expressions/property-access.js +113 -11
  372. package/dist/frontend/lowering/expressions/property-access.js.map +1 -1
  373. package/dist/frontend/lowering/expressions/relational.js +212 -33
  374. package/dist/frontend/lowering/expressions/relational.js.map +1 -1
  375. package/dist/frontend/lowering/expressions/union-fields.js +4 -0
  376. package/dist/frontend/lowering/expressions/union-fields.js.map +1 -1
  377. package/dist/frontend/lowering/expressions/updates.js +22 -3
  378. package/dist/frontend/lowering/expressions/updates.js.map +1 -1
  379. package/dist/frontend/lowering/frozen-records.d.ts +28 -0
  380. package/dist/frontend/lowering/frozen-records.js +322 -0
  381. package/dist/frontend/lowering/frozen-records.js.map +1 -0
  382. package/dist/frontend/lowering/function-returns.js +49 -0
  383. package/dist/frontend/lowering/function-returns.js.map +1 -1
  384. package/dist/frontend/lowering/generic-functions.js +7 -2
  385. package/dist/frontend/lowering/generic-functions.js.map +1 -1
  386. package/dist/frontend/lowering/iterable-values.d.ts +49 -0
  387. package/dist/frontend/lowering/iterable-values.js +297 -0
  388. package/dist/frontend/lowering/iterable-values.js.map +1 -0
  389. package/dist/frontend/lowering/iterator-adapters.js +186 -1
  390. package/dist/frontend/lowering/iterator-adapters.js.map +1 -1
  391. package/dist/frontend/lowering/js-class-fields.d.ts +8 -0
  392. package/dist/frontend/lowering/js-class-fields.js +45 -10
  393. package/dist/frontend/lowering/js-class-fields.js.map +1 -1
  394. package/dist/frontend/lowering/key-order-witness.d.ts +18 -3
  395. package/dist/frontend/lowering/key-order-witness.js +770 -22
  396. package/dist/frontend/lowering/key-order-witness.js.map +1 -1
  397. package/dist/frontend/lowering/key-order-witness.test.js +428 -8
  398. package/dist/frontend/lowering/key-order-witness.test.js.map +1 -1
  399. package/dist/frontend/lowering/lower-async-iteration.js +4 -1
  400. package/dist/frontend/lowering/lower-async-iteration.js.map +1 -1
  401. package/dist/frontend/lowering/lower-calls.d.ts +7 -0
  402. package/dist/frontend/lowering/lower-calls.js +443 -41
  403. package/dist/frontend/lowering/lower-calls.js.map +1 -1
  404. package/dist/frontend/lowering/lower-classes.d.ts +11 -2
  405. package/dist/frontend/lowering/lower-classes.js +302 -22
  406. package/dist/frontend/lowering/lower-classes.js.map +1 -1
  407. package/dist/frontend/lowering/lower-dgram.js +3 -1
  408. package/dist/frontend/lowering/lower-dgram.js.map +1 -1
  409. package/dist/frontend/lowering/lower-exprs.js +10 -2
  410. package/dist/frontend/lowering/lower-exprs.js.map +1 -1
  411. package/dist/frontend/lowering/lower-functions.d.ts +11 -0
  412. package/dist/frontend/lowering/lower-functions.js +83 -0
  413. package/dist/frontend/lowering/lower-functions.js.map +1 -1
  414. package/dist/frontend/lowering/lower-generators.js +41 -31
  415. package/dist/frontend/lowering/lower-generators.js.map +1 -1
  416. package/dist/frontend/lowering/lower-global-value.js +6 -3
  417. package/dist/frontend/lowering/lower-global-value.js.map +1 -1
  418. package/dist/frontend/lowering/lower-inspect.js +18 -6
  419. package/dist/frontend/lowering/lower-inspect.js.map +1 -1
  420. package/dist/frontend/lowering/lower-modules.js +32 -0
  421. package/dist/frontend/lowering/lower-modules.js.map +1 -1
  422. package/dist/frontend/lowering/lower-namespaces.js +5 -1
  423. package/dist/frontend/lowering/lower-namespaces.js.map +1 -1
  424. package/dist/frontend/lowering/lower-server.js +2 -2
  425. package/dist/frontend/lowering/lower-server.js.map +1 -1
  426. package/dist/frontend/lowering/lower-stdlib.d.ts +5 -0
  427. package/dist/frontend/lowering/lower-stdlib.js +30 -0
  428. package/dist/frontend/lowering/lower-stdlib.js.map +1 -1
  429. package/dist/frontend/lowering/lower-stmts.d.ts +23 -0
  430. package/dist/frontend/lowering/lower-stmts.js +454 -48
  431. package/dist/frontend/lowering/lower-stmts.js.map +1 -1
  432. package/dist/frontend/lowering/lower-stream.d.ts +20 -2
  433. package/dist/frontend/lowering/lower-stream.js +1483 -156
  434. package/dist/frontend/lowering/lower-stream.js.map +1 -1
  435. package/dist/frontend/lowering/lowerer.d.ts +77 -6
  436. package/dist/frontend/lowering/lowerer.js +313 -29
  437. package/dist/frontend/lowering/lowerer.js.map +1 -1
  438. package/dist/frontend/lowering/module-namespace-elements.d.ts +11 -1
  439. package/dist/frontend/lowering/module-namespace-elements.js +82 -5
  440. package/dist/frontend/lowering/module-namespace-elements.js.map +1 -1
  441. package/dist/frontend/lowering/new-target.d.ts +12 -0
  442. package/dist/frontend/lowering/new-target.js +280 -0
  443. package/dist/frontend/lowering/new-target.js.map +1 -0
  444. package/dist/frontend/lowering/surfaces.d.ts +3 -0
  445. package/dist/frontend/lowering/surfaces.js +18 -6
  446. package/dist/frontend/lowering/surfaces.js.map +1 -1
  447. package/dist/frontend/lowering/undefined-index.d.ts +27 -0
  448. package/dist/frontend/lowering/undefined-index.js +82 -0
  449. package/dist/frontend/lowering/undefined-index.js.map +1 -0
  450. package/dist/frontend/lowering/var-hoisting.js +29 -13
  451. package/dist/frontend/lowering/var-hoisting.js.map +1 -1
  452. package/dist/frontend/lowering/width-lift.d.ts +18 -0
  453. package/dist/frontend/npm-static-bundled-chunks.js +9 -11
  454. package/dist/frontend/npm-static-bundled-chunks.js.map +1 -1
  455. package/dist/frontend/npm-static-bundled-chunks.test.js +6 -5
  456. package/dist/frontend/npm-static-bundled-chunks.test.js.map +1 -1
  457. package/dist/frontend/npm-static-rewrite-syntax.js +9 -1
  458. package/dist/frontend/npm-static-rewrite-syntax.js.map +1 -1
  459. package/dist/frontend/npm-static.js +4 -20
  460. package/dist/frontend/npm-static.js.map +1 -1
  461. package/dist/frontend/package-shims.d.ts +3 -0
  462. package/dist/frontend/package-shims.js +71 -8
  463. package/dist/frontend/package-shims.js.map +1 -1
  464. package/dist/frontend/pipeline.js +18 -10
  465. package/dist/frontend/pipeline.js.map +1 -1
  466. package/dist/frontend/process-fork.d.ts +25 -0
  467. package/dist/frontend/process-fork.js +2 -0
  468. package/dist/frontend/process-fork.js.map +1 -0
  469. package/dist/frontend/program.d.ts +8 -1
  470. package/dist/frontend/program.js +484 -69
  471. package/dist/frontend/program.js.map +1 -1
  472. package/dist/frontend/program.test.js +43 -3
  473. package/dist/frontend/program.test.js.map +1 -1
  474. package/dist/frontend/token-equivalence.d.ts +3 -0
  475. package/dist/frontend/token-equivalence.js +409 -0
  476. package/dist/frontend/token-equivalence.js.map +1 -0
  477. package/dist/frontend/token-equivalence.test.d.ts +1 -0
  478. package/dist/frontend/token-equivalence.test.js +67 -0
  479. package/dist/frontend/token-equivalence.test.js.map +1 -0
  480. package/dist/frontend/ts7/ast-node.d.ts +22 -0
  481. package/dist/frontend/ts7/ast-node.js +1048 -103
  482. package/dist/frontend/ts7/ast-node.js.map +1 -1
  483. package/dist/frontend/ts7/ast-node.test.js +24 -0
  484. package/dist/frontend/ts7/ast-node.test.js.map +1 -1
  485. package/dist/frontend/ts7/ast-wire.d.ts +4 -3
  486. package/dist/frontend/ts7/ast-wire.js +34 -24
  487. package/dist/frontend/ts7/ast-wire.js.map +1 -1
  488. package/dist/frontend/ts7/ast-wire.test.js +23 -0
  489. package/dist/frontend/ts7/ast-wire.test.js.map +1 -1
  490. package/dist/frontend/ts7/checker-backend.d.ts +3 -4
  491. package/dist/frontend/ts7/checker-backend.js +2 -2
  492. package/dist/frontend/ts7/checker-backend.js.map +1 -1
  493. package/dist/frontend/ts7/checker-backend.test.js +5 -9
  494. package/dist/frontend/ts7/checker-backend.test.js.map +1 -1
  495. package/dist/frontend/ts7/checker.d.ts +4 -0
  496. package/dist/frontend/ts7/checker.js +7 -0
  497. package/dist/frontend/ts7/checker.js.map +1 -1
  498. package/dist/frontend/ts7/lib-diagnostics.d.ts +24 -0
  499. package/dist/frontend/ts7/lib-diagnostics.js +307 -0
  500. package/dist/frontend/ts7/lib-diagnostics.js.map +1 -0
  501. package/dist/frontend/ts7/lib-diagnostics.test.d.ts +1 -0
  502. package/dist/frontend/ts7/lib-diagnostics.test.js +199 -0
  503. package/dist/frontend/ts7/lib-diagnostics.test.js.map +1 -0
  504. package/dist/frontend/ts7/program-host.d.ts +7 -0
  505. package/dist/frontend/ts7/program-host.js +25 -2
  506. package/dist/frontend/ts7/program-host.js.map +1 -1
  507. package/dist/frontend/ts7/session.d.ts +14 -1
  508. package/dist/frontend/ts7/session.js +22 -0
  509. package/dist/frontend/ts7/session.js.map +1 -1
  510. package/dist/frontend/type-mapper.d.ts +13 -1
  511. package/dist/frontend/type-mapper.js +191 -11
  512. package/dist/frontend/type-mapper.js.map +1 -1
  513. package/dist/index.d.ts +0 -5
  514. package/dist/index.js +34 -4
  515. package/dist/index.js.map +1 -1
  516. package/dist/ir/analysis.js +5 -3
  517. package/dist/ir/analysis.js.map +1 -1
  518. package/dist/ir/array-length-stability.d.ts +4 -0
  519. package/dist/ir/array-length-stability.js +19 -6
  520. package/dist/ir/array-length-stability.js.map +1 -1
  521. package/dist/ir/array-length-stability.test.js +24 -0
  522. package/dist/ir/array-length-stability.test.js.map +1 -1
  523. package/dist/ir/builtin-effects.d.ts +7 -0
  524. package/dist/ir/builtin-effects.js +52 -0
  525. package/dist/ir/builtin-effects.js.map +1 -1
  526. package/dist/ir/builtin-signatures.d.ts +6 -4
  527. package/dist/ir/builtin-signatures.js +166 -13
  528. package/dist/ir/builtin-signatures.js.map +1 -1
  529. package/dist/ir/callback-specialization.d.ts +2 -1
  530. package/dist/ir/callback-specialization.js +23 -18
  531. package/dist/ir/callback-specialization.js.map +1 -1
  532. package/dist/ir/char-unit-locals.d.ts +19 -0
  533. package/dist/ir/char-unit-locals.js +185 -0
  534. package/dist/ir/char-unit-locals.js.map +1 -0
  535. package/dist/ir/char-unit-locals.test.d.ts +1 -0
  536. package/dist/ir/char-unit-locals.test.js +94 -0
  537. package/dist/ir/char-unit-locals.test.js.map +1 -0
  538. package/dist/ir/final-fields.d.ts +2 -1
  539. package/dist/ir/final-fields.js +14 -16
  540. package/dist/ir/final-fields.js.map +1 -1
  541. package/dist/ir/function-references.d.ts +2 -0
  542. package/dist/ir/function-references.js +2 -0
  543. package/dist/ir/function-references.js.map +1 -1
  544. package/dist/ir/function-references.test.js +3 -0
  545. package/dist/ir/function-references.test.js.map +1 -1
  546. package/dist/ir/initializer-bindings.d.ts +2 -1
  547. package/dist/ir/initializer-bindings.js +27 -20
  548. package/dist/ir/initializer-bindings.js.map +1 -1
  549. package/dist/ir/int32-arrays.d.ts +108 -0
  550. package/dist/ir/int32-arrays.js +831 -0
  551. package/dist/ir/int32-arrays.js.map +1 -0
  552. package/dist/ir/int32-slots.d.ts +24 -2
  553. package/dist/ir/int32-slots.js +259 -57
  554. package/dist/ir/int32-slots.js.map +1 -1
  555. package/dist/ir/integer-loops.d.ts +3 -3
  556. package/dist/ir/integer-loops.js +14 -6
  557. package/dist/ir/integer-loops.js.map +1 -1
  558. package/dist/ir/integer-loops.test.js +10 -0
  559. package/dist/ir/integer-loops.test.js.map +1 -1
  560. package/dist/ir/integer-ranges.d.ts +5 -0
  561. package/dist/ir/integer-ranges.js +125 -12
  562. package/dist/ir/integer-ranges.js.map +1 -1
  563. package/dist/ir/integer-ranges.test.js +30 -0
  564. package/dist/ir/integer-ranges.test.js.map +1 -1
  565. package/dist/ir/ir.d.ts +70 -7
  566. package/dist/ir/ir.js +58 -1
  567. package/dist/ir/ir.js.map +1 -1
  568. package/dist/ir/ir.test.js +21 -1
  569. package/dist/ir/ir.test.js.map +1 -1
  570. package/dist/ir/js-ic.d.ts +19 -2
  571. package/dist/ir/js-ic.js +95 -11
  572. package/dist/ir/js-ic.js.map +1 -1
  573. package/dist/ir/js-ic.test.js +188 -0
  574. package/dist/ir/js-ic.test.js.map +1 -1
  575. package/dist/ir/node-index.d.ts +57 -0
  576. package/dist/ir/node-index.js +215 -0
  577. package/dist/ir/node-index.js.map +1 -0
  578. package/dist/ir/paired-while-loops.d.ts +25 -0
  579. package/dist/ir/paired-while-loops.js +335 -0
  580. package/dist/ir/paired-while-loops.js.map +1 -0
  581. package/dist/ir/paired-while-loops.test.d.ts +1 -0
  582. package/dist/ir/paired-while-loops.test.js +163 -0
  583. package/dist/ir/paired-while-loops.test.js.map +1 -0
  584. package/dist/ir/parameter-versions.d.ts +25 -0
  585. package/dist/ir/parameter-versions.js +300 -0
  586. package/dist/ir/parameter-versions.js.map +1 -0
  587. package/dist/ir/runtime-features.d.ts +34 -13
  588. package/dist/ir/runtime-features.js +152 -38
  589. package/dist/ir/runtime-features.js.map +1 -1
  590. package/dist/ir/serialize.d.ts +12 -2
  591. package/dist/ir/serialize.js +16 -3
  592. package/dist/ir/serialize.js.map +1 -1
  593. package/dist/ir/serialize.test.js +17 -0
  594. package/dist/ir/serialize.test.js.map +1 -1
  595. package/dist/ir/traverse.js +18 -6
  596. package/dist/ir/traverse.js.map +1 -1
  597. package/dist/ir/validate.js +122 -36
  598. package/dist/ir/validate.js.map +1 -1
  599. package/dist/library/ir-policy.js +2 -0
  600. package/dist/library/ir-policy.js.map +1 -1
  601. package/dist/native/cache.d.ts +6 -1
  602. package/dist/native/cache.js +28 -5
  603. package/dist/native/cache.js.map +1 -1
  604. package/dist/native/cache.test.js +16 -2
  605. package/dist/native/cache.test.js.map +1 -1
  606. package/dist/native/cli.js +21 -1
  607. package/dist/native/cli.js.map +1 -1
  608. package/dist/native/compiler.js +8 -6
  609. package/dist/native/compiler.js.map +1 -1
  610. package/dist/native/executable-cache.d.ts +1 -1
  611. package/dist/native/executable-cache.js +2 -2
  612. package/dist/native/executable-cache.js.map +1 -1
  613. package/dist/native/prepare.d.ts +3 -1
  614. package/dist/native/prepare.js +119 -31
  615. package/dist/native/prepare.js.map +1 -1
  616. package/dist/native/prepare.test.js +78 -18
  617. package/dist/native/prepare.test.js.map +1 -1
  618. package/dist/native/sanitizer.test.js +2 -0
  619. package/dist/native/sanitizer.test.js.map +1 -1
  620. package/dist/native/text-file.d.ts +13 -3
  621. package/dist/native/text-file.js +63 -9
  622. package/dist/native/text-file.js.map +1 -1
  623. package/dist/native/text-file.test.d.ts +1 -0
  624. package/dist/native/text-file.test.js +49 -0
  625. package/dist/native/text-file.test.js.map +1 -0
  626. package/dist/native/toolchain.js +4 -4
  627. package/dist/native/toolchain.js.map +1 -1
  628. package/dist/native/toolchain.test.js +3 -3
  629. package/dist/native/toolchain.test.js.map +1 -1
  630. package/package.json +21 -21
  631. package/shims/chalk/v5.ts +688 -0
  632. package/shims/hono-node-server/v2.ts +645 -0
  633. package/shims/hot-shots/client.ts +1126 -0
  634. package/shims/hot-shots/v8-unix-dgram.ts +8 -0
  635. package/shims/hot-shots/v8.ts +8 -0
  636. package/surface-manifest.json +26 -4
@@ -0,0 +1,645 @@
1
+ // scriptc's built-in implementation of `@hono/node-server` 2.x: the Node.js
2
+ // adapter that serves a fetch-style handler (Hono's `app.fetch`) over
3
+ // node:http. The published package swaps the global Request and Response
4
+ // for lightweight objects and reparents prototypes, which only pays off
5
+ // under V8; this port builds native Request objects from the incoming
6
+ // message, with a body streamed from it as the app reads it, and writes
7
+ // the handler's Response the way the package's listener does (header
8
+ // record, content length for bodies that complete within the first reads,
9
+ // streamed bodies, error responses), so a client sees the same status
10
+ // lines, headers and bodies. A body the app leaves unread is drained after
11
+ // the response, within the package's limits.
12
+ //
13
+ // HTTP/1.1 only: the http2/https server options and WebSocket upgrades are
14
+ // not declared. A Response that is still exactly its constructor arguments
15
+ // takes the package's cached-response path (its lightweight Response), read
16
+ // from the runtime through scriptcResponseLight; every other Response takes
17
+ // the general path.
18
+ import { createServer, type IncomingMessage, type Server, type ServerResponse } from "node:http";
19
+ import type { AddressInfo } from "node:net";
20
+
21
+ export type HttpBindings = {
22
+ incoming: IncomingMessage;
23
+ outgoing: ServerResponse;
24
+ };
25
+
26
+ export type ServerType = Server;
27
+
28
+ export type FetchCallback = (request: Request, env: HttpBindings) => Response | Promise<Response>;
29
+
30
+ export type CustomErrorHandler = (err: unknown) => void | Response | Promise<void | Response>;
31
+
32
+ export type Options = {
33
+ fetch: FetchCallback;
34
+ overrideGlobalObjects?: boolean;
35
+ autoCleanupIncoming?: boolean;
36
+ port?: number;
37
+ hostname?: string;
38
+ };
39
+
40
+ export interface RequestListenerOptions {
41
+ hostname?: string;
42
+ errorHandler?: CustomErrorHandler;
43
+ overrideGlobalObjects?: boolean;
44
+ autoCleanupIncoming?: boolean;
45
+ }
46
+
47
+ export class RequestError extends Error {
48
+ constructor(message: string, options?: { cause?: unknown }) {
49
+ super(message, options);
50
+ this.name = "RequestError";
51
+ }
52
+ }
53
+
54
+ const DEFAULT_CONTENT_TYPE = "text/plain; charset=UTF-8";
55
+ const X_ALREADY_SENT = "x-hono-already-sent";
56
+
57
+ const reValidRequestUrl = /^\/[!#$&-;=?-[\]_a-z~]*$/;
58
+ const reDotSegment = /\/\.\.?(?:[/?#]|$)/;
59
+ const reValidHost = /^[a-z0-9._-]+(?::(?:[1-5]\d{3,4}|[6-9]\d{3}))?$/;
60
+
61
+ function buildUrl(scheme: string, host: string, incomingUrl: string): string {
62
+ const url = `${scheme}://${host}${incomingUrl}`;
63
+ if (!reValidHost.test(host)) {
64
+ const urlObj = new URL(url);
65
+ if (
66
+ urlObj.hostname.length !== host.length &&
67
+ urlObj.hostname !== (host.includes(":") ? host.replace(/:\d+$/, "") : host).toLowerCase()
68
+ )
69
+ throw new RequestError("Invalid host header");
70
+ return urlObj.href;
71
+ }
72
+ if (incomingUrl.length === 0) return url + "/";
73
+ if (incomingUrl.charCodeAt(0) !== 47) throw new RequestError("Invalid URL");
74
+ if (!reValidRequestUrl.test(incomingUrl) || reDotSegment.test(incomingUrl))
75
+ return new URL(url).href;
76
+ return url;
77
+ }
78
+
79
+ function normalizeIncomingMethod(method: string | undefined): string {
80
+ if (method === undefined || method.length === 0) return "GET";
81
+ switch (method) {
82
+ case "DELETE":
83
+ case "GET":
84
+ case "HEAD":
85
+ case "OPTIONS":
86
+ case "PATCH":
87
+ case "POST":
88
+ case "PUT":
89
+ case "QUERY":
90
+ return method;
91
+ }
92
+ const upper = method.toUpperCase();
93
+ switch (upper) {
94
+ case "DELETE":
95
+ case "GET":
96
+ case "HEAD":
97
+ case "OPTIONS":
98
+ case "POST":
99
+ case "PUT":
100
+ return upper;
101
+ default:
102
+ return method;
103
+ }
104
+ }
105
+
106
+ /** The request URL the package builds: an absolute request target as is,
107
+ * otherwise the Host header (or the configured hostname) over http. */
108
+ function requestUrl(incoming: IncomingMessage, defaultHostname: string | undefined): string {
109
+ const incomingUrl = incoming.url ?? "";
110
+ if (
111
+ incomingUrl[0] !== "/" &&
112
+ (incomingUrl.startsWith("http://") || incomingUrl.startsWith("https://"))
113
+ ) {
114
+ try {
115
+ return new URL(incomingUrl).href;
116
+ } catch (e) {
117
+ throw new RequestError("Invalid absolute URL", { cause: e });
118
+ }
119
+ }
120
+ const hostHeader = incoming.headers.host;
121
+ const host = hostHeader !== undefined && hostHeader !== "" ? hostHeader : (defaultHostname ?? "");
122
+ if (host === "") throw new RequestError("Missing host header");
123
+ try {
124
+ return buildUrl("http", host, incomingUrl);
125
+ } catch (e) {
126
+ if (e instanceof RequestError) throw e;
127
+ throw new RequestError("Invalid URL", { cause: e });
128
+ }
129
+ }
130
+
131
+ function requestHeaders(incoming: IncomingMessage): Headers {
132
+ const headers = new Headers();
133
+ const raw = incoming.rawHeaders;
134
+ for (let i = 0; i < raw.length; i += 2) {
135
+ const name = raw[i]!;
136
+ if (!name.startsWith(":")) headers.append(name, raw[i + 1]!);
137
+ }
138
+ return headers;
139
+ }
140
+
141
+ /** What the listener knows about the incoming body: whether it ended
142
+ * (the package's `readableEnded`) and whether a drain started. */
143
+ type IncomingState = { ended: boolean; draining: boolean };
144
+
145
+ /** The incoming message's readableHighWaterMark: Readable.toWeb queues
146
+ * this many bytes before it pauses the message. */
147
+ const BODY_HIGH_WATER_MARK = 64 * 1024;
148
+
149
+ /** The request body as the package streams it: Readable.toWeb over the
150
+ * incoming message, read one chunk per pull. Chunks queue until
151
+ * BODY_HIGH_WATER_MARK bytes wait unread, then the message pauses until a
152
+ * pull takes them, so the socket holds the rest. The stream pulls once on
153
+ * its own when it is constructed; the package builds its stream only when
154
+ * the app reads the body, so until the app's first read a drain discards
155
+ * the chunks instead of queueing them. */
156
+ function incomingBody(incoming: IncomingMessage, state: IncomingState): ReadableStream<Uint8Array> {
157
+ let pulls = 0;
158
+ let listening = false;
159
+ let ended = false;
160
+ let failure: Error | undefined = undefined;
161
+ const queue: Buffer[] = [];
162
+ let queued = 0;
163
+ let wake: (() => void) | undefined = undefined;
164
+ const woken = (): void => {
165
+ const resolve = wake;
166
+ wake = undefined;
167
+ if (resolve !== undefined) resolve();
168
+ };
169
+ const listen = (): void => {
170
+ listening = true;
171
+ incoming.on("data", (chunk: Buffer) => {
172
+ if (state.draining && pulls === 1) return;
173
+ queue.push(chunk);
174
+ queued += chunk.length;
175
+ if (queued >= BODY_HIGH_WATER_MARK) incoming.pause();
176
+ woken();
177
+ });
178
+ incoming.on("end", () => {
179
+ ended = true;
180
+ woken();
181
+ });
182
+ incoming.on("error", (err: Error) => {
183
+ failure = err;
184
+ woken();
185
+ });
186
+ };
187
+ return new ReadableStream<Uint8Array>({
188
+ async pull(controller) {
189
+ pulls++;
190
+ if (!listening) listen();
191
+ for (;;) {
192
+ const chunk = queue.shift();
193
+ if (chunk !== undefined) {
194
+ queued -= chunk.length;
195
+ if (queued < BODY_HIGH_WATER_MARK) incoming.resume();
196
+ controller.enqueue(chunk);
197
+ return;
198
+ }
199
+ if (failure !== undefined) {
200
+ controller.error(failure);
201
+ return;
202
+ }
203
+ if (ended) {
204
+ controller.close();
205
+ return;
206
+ }
207
+ await new Promise<void>((resolve) => {
208
+ wake = resolve;
209
+ });
210
+ }
211
+ },
212
+ });
213
+ }
214
+
215
+ function newRequest(
216
+ incoming: IncomingMessage,
217
+ defaultHostname: string | undefined,
218
+ state: IncomingState,
219
+ ): Request {
220
+ const method = normalizeIncomingMethod(incoming.method);
221
+ const url = requestUrl(incoming, defaultHostname);
222
+ const headers = requestHeaders(incoming);
223
+ if (method === "GET" || method === "HEAD") return new Request(url, { method, headers });
224
+ return new Request(url, {
225
+ method,
226
+ headers,
227
+ body: incomingBody(incoming, state),
228
+ duplex: "half",
229
+ });
230
+ }
231
+
232
+ const DRAIN_TIMEOUT_MS = 500;
233
+ const MAX_DRAIN_BYTES = 64 * 1024 * 1024;
234
+
235
+ /** The package's drain of a body the app left unread: read and discard
236
+ * up to MAX_DRAIN_BYTES or DRAIN_TIMEOUT_MS, then close the connection
237
+ * once its writes are out. */
238
+ function drainIncoming(incoming: IncomingMessage, state: IncomingState): void {
239
+ const socket = incoming.socket;
240
+ if (socket.destroyed || state.draining) return;
241
+ state.draining = true;
242
+ let bytesRead = 0;
243
+ let done = false;
244
+ const cleanup = (): void => {
245
+ if (done) return;
246
+ done = true;
247
+ clearTimeout(timer);
248
+ };
249
+ const forceClose = (): void => {
250
+ if (done) return;
251
+ cleanup();
252
+ if (!socket.destroyed) socket.destroySoon();
253
+ };
254
+ const timer = setTimeout(forceClose, DRAIN_TIMEOUT_MS);
255
+ timer.unref();
256
+ incoming.on("data", (chunk: Buffer) => {
257
+ if (done) return;
258
+ bytesRead += chunk.length;
259
+ if (bytesRead > MAX_DRAIN_BYTES) forceClose();
260
+ });
261
+ incoming.on("end", cleanup);
262
+ incoming.on("error", cleanup);
263
+ incoming.resume();
264
+ }
265
+
266
+ /** The package's close handler cleanup: an unread body drains two timer
267
+ * turns after the response closes. */
268
+ function drainAfterClose(incoming: IncomingMessage, state: IncomingState): void {
269
+ if (state.ended) return;
270
+ setTimeout(() => {
271
+ if (!state.ended)
272
+ setTimeout(() => {
273
+ drainIncoming(incoming, state);
274
+ });
275
+ });
276
+ }
277
+
278
+ /** The package's outgoing header record: Headers order (lowercase,
279
+ * sorted), Set-Cookie values gathered into one array entry at the end,
280
+ * and the default content type for a response with a body. */
281
+ function buildOutgoingHttpHeaders(
282
+ headers: Headers,
283
+ hasBody: boolean,
284
+ ): Record<string, string | string[]> {
285
+ const res: Record<string, string | string[]> = {};
286
+ if (headers.has("set-cookie")) {
287
+ const cookies: string[] = [];
288
+ headers.forEach((v, k) => {
289
+ if (k === "set-cookie") cookies.push(v);
290
+ else res[k] = v;
291
+ });
292
+ // Headers.forEach joins nothing but set-cookie, which getSetCookie
293
+ // lists value by value, as the package's iteration does.
294
+ if (cookies.length > 0) res["set-cookie"] = headers.getSetCookie();
295
+ } else {
296
+ headers.forEach((v, k) => {
297
+ res[k] = v;
298
+ });
299
+ }
300
+ if (hasBody && res["content-type"] === undefined) res["content-type"] = DEFAULT_CONTENT_TYPE;
301
+ return res;
302
+ }
303
+
304
+ /* Spelled out rather than ReadableStreamReadResult<Uint8Array>, whose
305
+ * declaration depends on the program's lib and @types/node settings: the
306
+ * shim compiles inside every program that imports the package. */
307
+ type ReadResult = { done: boolean; value?: Uint8Array };
308
+
309
+ function readWithoutBlocking(read: Promise<ReadResult>): Promise<ReadResult | undefined> {
310
+ return Promise.race([read, Promise.resolve().then(() => Promise.resolve(undefined))]);
311
+ }
312
+
313
+ function nextTick(): Promise<void> {
314
+ return new Promise<void>((resolve) => {
315
+ setTimeout(resolve);
316
+ });
317
+ }
318
+
319
+ /** Writes the rest of a body stream. As in the package, a response that
320
+ * closes before the stream ends (the client disconnected) cancels the
321
+ * stream, so its producer stops; the pending read then answers done. A
322
+ * response that fails closes too, and its stream is cancelled the same way
323
+ * (the package passes the error as the reason). The listener stays attached
324
+ * and does nothing once the stream has ended. */
325
+ async function pump(
326
+ reader: ReadableStreamDefaultReader<Uint8Array>,
327
+ outgoing: ServerResponse,
328
+ pending: Promise<ReadResult> | undefined,
329
+ ): Promise<void> {
330
+ let finished = false;
331
+ outgoing.on("close", () => {
332
+ if (!finished) reader.cancel().catch(() => {});
333
+ });
334
+ try {
335
+ let read = pending ?? reader.read();
336
+ for (;;) {
337
+ const { done, value } = await read;
338
+ if (done) {
339
+ outgoing.end();
340
+ return;
341
+ }
342
+ outgoing.write(value!);
343
+ read = reader.read();
344
+ }
345
+ } finally {
346
+ finished = true;
347
+ }
348
+ }
349
+
350
+ function handleResponseError(e: unknown, outgoing: ServerResponse): void {
351
+ const err = e instanceof Error ? e : new Error("unknown error", { cause: e });
352
+ console.error(e);
353
+ if (!outgoing.headersSent) outgoing.writeHead(500, { "Content-Type": "text/plain" });
354
+ outgoing.end(`Error: ${err.message}`);
355
+ outgoing.destroy();
356
+ }
357
+
358
+ function emptyResponse(status: number): Response {
359
+ return new Response(null, { status });
360
+ }
361
+
362
+ /** The lightweight state of a constructed Response (the runtime's
363
+ * scr_fetch_response_light): [form, flags, byte length, ...record pairs]. */
364
+ declare function scriptcResponseLight(res: Response): string[];
365
+
366
+ const UNDICI_TEXT_TYPE = "text/plain;charset=UTF-8";
367
+
368
+ /** Content-Length as Node's implicit header: set the headers one by one and
369
+ * let end(body) compute the length, which Node then writes after its own
370
+ * Date/Connection headers, as the package's preset length does. */
371
+ function endWithImplicitLength(
372
+ outgoing: ServerResponse,
373
+ status: number,
374
+ header: Record<string, string | string[]>,
375
+ body: Uint8Array,
376
+ ): void {
377
+ outgoing.statusCode = status;
378
+ for (const name of Object.keys(header)) outgoing.setHeader(name, header[name]!);
379
+ outgoing.end(body);
380
+ }
381
+
382
+ /** The package's cached-response path: a Response that is still exactly
383
+ * its constructor arguments (no body or metadata read since) is written
384
+ * from them. Form 1 has no init headers, form 2 Headers, form 3 a plain
385
+ * record kept verbatim. */
386
+ async function writeLightResponse(
387
+ res: Response,
388
+ outgoing: ServerResponse,
389
+ light: string[],
390
+ head: boolean,
391
+ ): Promise<void> {
392
+ const form = light[0]!;
393
+ const flags = light[1]!;
394
+ const length = Number(light[2]!);
395
+ const status = res.status;
396
+ const sized = length >= 0;
397
+ if (form === "1") {
398
+ if (length === -1) {
399
+ outgoing.writeHead(status);
400
+ outgoing.end();
401
+ } else if (sized) {
402
+ const body = await res.bytes();
403
+ if (head) {
404
+ outgoing.writeHead(status, {
405
+ "Content-Type": DEFAULT_CONTENT_TYPE,
406
+ "Content-Length": String(length),
407
+ });
408
+ outgoing.end(body);
409
+ } else
410
+ endWithImplicitLength(outgoing, status, { "Content-Type": DEFAULT_CONTENT_TYPE }, body);
411
+ } else {
412
+ outgoing.writeHead(status, { "Content-Type": DEFAULT_CONTENT_TYPE });
413
+ outgoing.flushHeaders();
414
+ await pump(res.body!.getReader(), outgoing, undefined);
415
+ }
416
+ return;
417
+ }
418
+ let header: Record<string, string | string[]>;
419
+ let hasContentLength = false;
420
+ let plain = false;
421
+ let canAutoLength = true;
422
+ if (form === "2") {
423
+ const headers = new Headers(res.headers);
424
+ if (flags.includes("a") && headers.get("content-type") === UNDICI_TEXT_TYPE) {
425
+ if (flags.includes("d")) headers.set("content-type", DEFAULT_CONTENT_TYPE);
426
+ else headers.delete("content-type");
427
+ } else if (flags.includes("d") && !headers.has("content-type")) {
428
+ headers.set("content-type", DEFAULT_CONTENT_TYPE);
429
+ }
430
+ hasContentLength = headers.has("content-length");
431
+ header = buildOutgoingHttpHeaders(headers, length !== -1);
432
+ } else {
433
+ plain = true;
434
+ header = {};
435
+ for (let i = 3; i + 1 < light.length; i += 2) {
436
+ const name = light[i]!;
437
+ const value = light[i + 1]!;
438
+ const previous = header[name];
439
+ if (previous === undefined) header[name] = value;
440
+ else if (typeof previous === "string") header[name] = [previous, value];
441
+ else previous.push(value);
442
+ const lower = name.toLowerCase();
443
+ if (lower === "content-length") hasContentLength = true;
444
+ else if (lower === "transfer-encoding" || lower === "trailer") canAutoLength = false;
445
+ }
446
+ }
447
+ const body = sized ? await res.bytes() : undefined;
448
+ if (!hasContentLength && body !== undefined) {
449
+ if (plain && canAutoLength && !head && status !== 204 && status !== 304) {
450
+ endWithImplicitLength(outgoing, status, header, body);
451
+ return;
452
+ }
453
+ header["Content-Length"] = String(length);
454
+ }
455
+ outgoing.writeHead(status, header);
456
+ if (length === -1) outgoing.end();
457
+ else if (body !== undefined) outgoing.end(body);
458
+ else {
459
+ outgoing.flushHeaders();
460
+ await pump(res.body!.getReader(), outgoing, undefined);
461
+ }
462
+ }
463
+
464
+ async function writeResponse(
465
+ res: Response,
466
+ outgoing: ServerResponse,
467
+ head: boolean,
468
+ ): Promise<void> {
469
+ const light = scriptcResponseLight(res);
470
+ if (light[0] !== "0") return writeLightResponse(res, outgoing, light, head);
471
+ const body = res.body;
472
+ const record = buildOutgoingHttpHeaders(res.headers, body !== null);
473
+ if (body !== null) {
474
+ const reader: ReadableStreamDefaultReader<Uint8Array> = body.getReader();
475
+ const values: Uint8Array[] = [];
476
+ let done = false;
477
+ let pending: Promise<ReadResult> | undefined = undefined;
478
+ if (record["transfer-encoding"] !== "chunked") {
479
+ let maxReadCount = 2;
480
+ for (let i = 0; i < maxReadCount; i++) {
481
+ pending ??= reader.read();
482
+ let chunk: ReadResult | undefined = undefined;
483
+ try {
484
+ chunk = await readWithoutBlocking(pending);
485
+ } catch (e) {
486
+ console.error(e);
487
+ done = true;
488
+ }
489
+ if (chunk === undefined) {
490
+ if (i === 1 && !done) {
491
+ await nextTick();
492
+ maxReadCount = 3;
493
+ continue;
494
+ }
495
+ break;
496
+ }
497
+ pending = undefined;
498
+ if (chunk.value !== undefined) values.push(chunk.value);
499
+ if (chunk.done) {
500
+ done = true;
501
+ break;
502
+ }
503
+ }
504
+ if (done && record["content-length"] === undefined) {
505
+ let length = 0;
506
+ for (const value of values) length += value.length;
507
+ record["content-length"] = String(length);
508
+ }
509
+ }
510
+ outgoing.writeHead(res.status, record);
511
+ for (const value of values) outgoing.write(value);
512
+ if (done) outgoing.end();
513
+ else {
514
+ if (values.length === 0) outgoing.flushHeaders();
515
+ await pump(reader, outgoing, pending);
516
+ }
517
+ } else if (record[X_ALREADY_SENT] === undefined) {
518
+ outgoing.writeHead(res.status, record);
519
+ outgoing.end();
520
+ }
521
+ }
522
+
523
+ async function respond(
524
+ pending: Response | Promise<Response>,
525
+ outgoing: ServerResponse,
526
+ errorHandler: CustomErrorHandler | undefined,
527
+ head: boolean,
528
+ ): Promise<void> {
529
+ let res: Response;
530
+ if (pending instanceof Promise) {
531
+ try {
532
+ res = await pending;
533
+ } catch (err) {
534
+ if (errorHandler === undefined) {
535
+ res = handleFetchError(err);
536
+ } else {
537
+ const errRes = await errorHandler(err);
538
+ if (errRes === undefined) return;
539
+ res = errRes;
540
+ }
541
+ }
542
+ } else res = pending;
543
+ await writeResponse(res, outgoing, head);
544
+ }
545
+
546
+ /** A Response the package writes from its cache without waiting: still
547
+ * exactly its constructor arguments, with no body or a string or bytes
548
+ * body. */
549
+ function immediateCacheable(res: Response): boolean {
550
+ const light = scriptcResponseLight(res);
551
+ return light[0] !== "0" && Number(light[2]!) >= -1;
552
+ }
553
+
554
+ function handleFetchError(e: unknown): Response {
555
+ return emptyResponse(e instanceof Error && e.name === "TimeoutError" ? 504 : 500);
556
+ }
557
+
558
+ async function handleRequest(
559
+ fetchCallback: FetchCallback,
560
+ options: RequestListenerOptions,
561
+ incoming: IncomingMessage,
562
+ outgoing: ServerResponse,
563
+ ): Promise<void> {
564
+ let req: Request | undefined = undefined;
565
+ let res: Response | Promise<Response> | undefined = undefined;
566
+ const state: IncomingState = { ended: false, draining: false };
567
+ let needsBodyCleanup = false;
568
+ let closeHandlerAttached = false;
569
+ const ensureCloseHandler = (): void => {
570
+ if (req === undefined || closeHandlerAttached) return;
571
+ closeHandlerAttached = true;
572
+ if (needsBodyCleanup)
573
+ outgoing.on("close", () => {
574
+ drainAfterClose(incoming, state);
575
+ });
576
+ };
577
+ try {
578
+ req = newRequest(incoming, options.hostname, state);
579
+ needsBodyCleanup =
580
+ options.autoCleanupIncoming !== false &&
581
+ !(incoming.method === "GET" || incoming.method === "HEAD");
582
+ if (needsBodyCleanup)
583
+ incoming.on("end", () => {
584
+ state.ended = true;
585
+ });
586
+ res = fetchCallback(req, { incoming, outgoing });
587
+ if (!(res instanceof Promise) && immediateCacheable(res)) {
588
+ if (needsBodyCleanup)
589
+ outgoing.once("finish", () => {
590
+ if (!state.ended) drainIncoming(incoming, state);
591
+ });
592
+ } else ensureCloseHandler();
593
+ } catch (e) {
594
+ if (res !== undefined) return handleResponseError(e, outgoing);
595
+ const errorHandler = options.errorHandler;
596
+ if (errorHandler !== undefined) {
597
+ ensureCloseHandler();
598
+ const handled = await errorHandler(
599
+ req !== undefined ? e : e instanceof RequestError ? e : toRequestError(e),
600
+ );
601
+ if (handled === undefined) return;
602
+ res = handled;
603
+ } else if (req === undefined) res = emptyResponse(400);
604
+ else res = handleFetchError(e);
605
+ }
606
+ try {
607
+ await respond(res, outgoing, options.errorHandler, incoming.method === "HEAD");
608
+ } catch (e) {
609
+ handleResponseError(e, outgoing);
610
+ }
611
+ }
612
+
613
+ function toRequestError(e: unknown): RequestError {
614
+ return new RequestError(e instanceof Error ? e.message : String(e), { cause: e });
615
+ }
616
+
617
+ export function getRequestListener(
618
+ fetchCallback: FetchCallback,
619
+ options: RequestListenerOptions = {},
620
+ ): (incoming: IncomingMessage, outgoing: ServerResponse) => Promise<void> {
621
+ return (incoming, outgoing) => handleRequest(fetchCallback, options, incoming, outgoing);
622
+ }
623
+
624
+ export function createAdaptorServer(options: Options): ServerType {
625
+ const listener = getRequestListener(options.fetch, {
626
+ hostname: options.hostname,
627
+ overrideGlobalObjects: options.overrideGlobalObjects,
628
+ autoCleanupIncoming: options.autoCleanupIncoming,
629
+ });
630
+ return createServer((incoming, outgoing) => {
631
+ void listener(incoming, outgoing);
632
+ });
633
+ }
634
+
635
+ export function serve(
636
+ options: Options,
637
+ listeningListener?: (info: AddressInfo) => void,
638
+ ): ServerType {
639
+ const server = createAdaptorServer(options);
640
+ server.listen(options.port ?? 3000, options.hostname, () => {
641
+ const info = server.address() as AddressInfo;
642
+ if (listeningListener !== undefined) listeningListener(info);
643
+ });
644
+ return server;
645
+ }