@scriptc/cli-darwin-arm64 0.2.2 → 0.2.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 (153) hide show
  1. package/dist/THIRD_PARTY_NOTICES +1 -1
  2. package/dist/bin/scriptc +0 -0
  3. package/dist/bin/scriptc.json +1 -1
  4. package/dist/lib/declarations/scriptc-node-fallback.d.ts +99 -27
  5. package/dist/lib/llvm/bin/scriptc-llvm-codegen +0 -0
  6. package/dist/lib/llvm/package.json +1 -1
  7. package/dist/lib/runtime-darwin-arm64/artifacts/dev/runtime/default/scr_array.o +0 -0
  8. package/dist/lib/runtime-darwin-arm64/artifacts/dev/runtime/default/scr_assert.o +0 -0
  9. package/dist/lib/runtime-darwin-arm64/artifacts/dev/runtime/default/scr_async.o +0 -0
  10. package/dist/lib/runtime-darwin-arm64/artifacts/dev/runtime/default/scr_bytes_io.o +0 -0
  11. package/dist/lib/runtime-darwin-arm64/artifacts/dev/runtime/default/scr_events.o +0 -0
  12. package/dist/lib/runtime-darwin-arm64/artifacts/dev/runtime/default/scr_exception.o +0 -0
  13. package/dist/lib/runtime-darwin-arm64/artifacts/dev/runtime/default/scr_file_handle.o +0 -0
  14. package/dist/lib/runtime-darwin-arm64/artifacts/dev/runtime/default/scr_json.o +0 -0
  15. package/dist/lib/runtime-darwin-arm64/artifacts/dev/runtime/default/scr_lib.o +0 -0
  16. package/dist/lib/runtime-darwin-arm64/artifacts/dev/runtime/default/scr_map.o +0 -0
  17. package/dist/lib/runtime-darwin-arm64/artifacts/dev/runtime/default/scr_node_builtin.o +0 -0
  18. package/dist/lib/runtime-darwin-arm64/artifacts/dev/runtime/default/scr_regex.o +0 -0
  19. package/dist/lib/runtime-darwin-arm64/artifacts/dev/runtime/default/scr_symbol.o +0 -0
  20. package/dist/lib/runtime-darwin-arm64/artifacts/dev/runtime/default/scr_url.o +0 -0
  21. package/dist/lib/runtime-darwin-arm64/artifacts/dev/runtime/default/scr_url_params.o +0 -0
  22. package/dist/lib/runtime-darwin-arm64/artifacts/dev/runtime/default/scr_util_compare.o +0 -0
  23. package/dist/lib/runtime-darwin-arm64/artifacts/dev/runtime/default/scr_util_style.o +0 -0
  24. package/dist/lib/runtime-darwin-arm64/artifacts/dev/runtime/dynamic/scr_array.o +0 -0
  25. package/dist/lib/runtime-darwin-arm64/artifacts/dev/runtime/dynamic/scr_assert.o +0 -0
  26. package/dist/lib/runtime-darwin-arm64/artifacts/dev/runtime/dynamic/scr_async.o +0 -0
  27. package/dist/lib/runtime-darwin-arm64/artifacts/dev/runtime/dynamic/scr_bytes_io.o +0 -0
  28. package/dist/lib/runtime-darwin-arm64/artifacts/dev/runtime/dynamic/scr_events.o +0 -0
  29. package/dist/lib/runtime-darwin-arm64/artifacts/dev/runtime/dynamic/scr_exception.o +0 -0
  30. package/dist/lib/runtime-darwin-arm64/artifacts/dev/runtime/dynamic/scr_file_handle.o +0 -0
  31. package/dist/lib/runtime-darwin-arm64/artifacts/dev/runtime/dynamic/scr_island.o +0 -0
  32. package/dist/lib/runtime-darwin-arm64/artifacts/dev/runtime/dynamic/scr_json.o +0 -0
  33. package/dist/lib/runtime-darwin-arm64/artifacts/dev/runtime/dynamic/scr_lib.o +0 -0
  34. package/dist/lib/runtime-darwin-arm64/artifacts/dev/runtime/dynamic/scr_map.o +0 -0
  35. package/dist/lib/runtime-darwin-arm64/artifacts/dev/runtime/dynamic/scr_node_builtin.o +0 -0
  36. package/dist/lib/runtime-darwin-arm64/artifacts/dev/runtime/dynamic/scr_regex.o +0 -0
  37. package/dist/lib/runtime-darwin-arm64/artifacts/dev/runtime/dynamic/scr_symbol.o +0 -0
  38. package/dist/lib/runtime-darwin-arm64/artifacts/dev/runtime/dynamic/scr_url.o +0 -0
  39. package/dist/lib/runtime-darwin-arm64/artifacts/dev/runtime/dynamic/scr_url_params.o +0 -0
  40. package/dist/lib/runtime-darwin-arm64/artifacts/dev/runtime/dynamic/scr_util_compare.o +0 -0
  41. package/dist/lib/runtime-darwin-arm64/artifacts/dev/runtime/dynamic/scr_util_style.o +0 -0
  42. package/dist/lib/runtime-darwin-arm64/artifacts/dev/runtime/dynamic/scr_web.o +0 -0
  43. package/dist/lib/runtime-darwin-arm64/artifacts/library-dev/runtime/default/scr_array.o +0 -0
  44. package/dist/lib/runtime-darwin-arm64/artifacts/library-dev/runtime/default/scr_assert.o +0 -0
  45. package/dist/lib/runtime-darwin-arm64/artifacts/library-dev/runtime/default/scr_bytes_io.o +0 -0
  46. package/dist/lib/runtime-darwin-arm64/artifacts/library-dev/runtime/default/scr_exception.o +0 -0
  47. package/dist/lib/runtime-darwin-arm64/artifacts/library-dev/runtime/default/scr_json.o +0 -0
  48. package/dist/lib/runtime-darwin-arm64/artifacts/library-dev/runtime/default/scr_lib.o +0 -0
  49. package/dist/lib/runtime-darwin-arm64/artifacts/library-dev/runtime/default/scr_map.o +0 -0
  50. package/dist/lib/runtime-darwin-arm64/artifacts/library-dev/runtime/default/scr_node_builtin.o +0 -0
  51. package/dist/lib/runtime-darwin-arm64/artifacts/library-dev/runtime/default/scr_regex.o +0 -0
  52. package/dist/lib/runtime-darwin-arm64/artifacts/library-dev/runtime/default/scr_symbol.o +0 -0
  53. package/dist/lib/runtime-darwin-arm64/artifacts/library-dev/runtime/default/scr_url.o +0 -0
  54. package/dist/lib/runtime-darwin-arm64/artifacts/library-dev/runtime/default/scr_url_params.o +0 -0
  55. package/dist/lib/runtime-darwin-arm64/artifacts/library-release/runtime/default/scr_array.o +0 -0
  56. package/dist/lib/runtime-darwin-arm64/artifacts/library-release/runtime/default/scr_assert.o +0 -0
  57. package/dist/lib/runtime-darwin-arm64/artifacts/library-release/runtime/default/scr_bytes_io.o +0 -0
  58. package/dist/lib/runtime-darwin-arm64/artifacts/library-release/runtime/default/scr_exception.o +0 -0
  59. package/dist/lib/runtime-darwin-arm64/artifacts/library-release/runtime/default/scr_json.o +0 -0
  60. package/dist/lib/runtime-darwin-arm64/artifacts/library-release/runtime/default/scr_lib.o +0 -0
  61. package/dist/lib/runtime-darwin-arm64/artifacts/library-release/runtime/default/scr_map.o +0 -0
  62. package/dist/lib/runtime-darwin-arm64/artifacts/library-release/runtime/default/scr_node_builtin.o +0 -0
  63. package/dist/lib/runtime-darwin-arm64/artifacts/library-release/runtime/default/scr_regex.o +0 -0
  64. package/dist/lib/runtime-darwin-arm64/artifacts/library-release/runtime/default/scr_symbol.o +0 -0
  65. package/dist/lib/runtime-darwin-arm64/artifacts/library-release/runtime/default/scr_url.o +0 -0
  66. package/dist/lib/runtime-darwin-arm64/artifacts/library-release/runtime/default/scr_url_params.o +0 -0
  67. package/dist/lib/runtime-darwin-arm64/artifacts/library-thread-dev/runtime/default/scr_array.o +0 -0
  68. package/dist/lib/runtime-darwin-arm64/artifacts/library-thread-dev/runtime/default/scr_assert.o +0 -0
  69. package/dist/lib/runtime-darwin-arm64/artifacts/library-thread-dev/runtime/default/scr_bytes_io.o +0 -0
  70. package/dist/lib/runtime-darwin-arm64/artifacts/library-thread-dev/runtime/default/scr_exception.o +0 -0
  71. package/dist/lib/runtime-darwin-arm64/artifacts/library-thread-dev/runtime/default/scr_json.o +0 -0
  72. package/dist/lib/runtime-darwin-arm64/artifacts/library-thread-dev/runtime/default/scr_lib.o +0 -0
  73. package/dist/lib/runtime-darwin-arm64/artifacts/library-thread-dev/runtime/default/scr_map.o +0 -0
  74. package/dist/lib/runtime-darwin-arm64/artifacts/library-thread-dev/runtime/default/scr_node_builtin.o +0 -0
  75. package/dist/lib/runtime-darwin-arm64/artifacts/library-thread-dev/runtime/default/scr_regex.o +0 -0
  76. package/dist/lib/runtime-darwin-arm64/artifacts/library-thread-dev/runtime/default/scr_symbol.o +0 -0
  77. package/dist/lib/runtime-darwin-arm64/artifacts/library-thread-dev/runtime/default/scr_url.o +0 -0
  78. package/dist/lib/runtime-darwin-arm64/artifacts/library-thread-dev/runtime/default/scr_url_params.o +0 -0
  79. package/dist/lib/runtime-darwin-arm64/artifacts/library-thread-release/runtime/default/scr_array.o +0 -0
  80. package/dist/lib/runtime-darwin-arm64/artifacts/library-thread-release/runtime/default/scr_assert.o +0 -0
  81. package/dist/lib/runtime-darwin-arm64/artifacts/library-thread-release/runtime/default/scr_bytes_io.o +0 -0
  82. package/dist/lib/runtime-darwin-arm64/artifacts/library-thread-release/runtime/default/scr_exception.o +0 -0
  83. package/dist/lib/runtime-darwin-arm64/artifacts/library-thread-release/runtime/default/scr_json.o +0 -0
  84. package/dist/lib/runtime-darwin-arm64/artifacts/library-thread-release/runtime/default/scr_lib.o +0 -0
  85. package/dist/lib/runtime-darwin-arm64/artifacts/library-thread-release/runtime/default/scr_map.o +0 -0
  86. package/dist/lib/runtime-darwin-arm64/artifacts/library-thread-release/runtime/default/scr_node_builtin.o +0 -0
  87. package/dist/lib/runtime-darwin-arm64/artifacts/library-thread-release/runtime/default/scr_regex.o +0 -0
  88. package/dist/lib/runtime-darwin-arm64/artifacts/library-thread-release/runtime/default/scr_symbol.o +0 -0
  89. package/dist/lib/runtime-darwin-arm64/artifacts/library-thread-release/runtime/default/scr_url.o +0 -0
  90. package/dist/lib/runtime-darwin-arm64/artifacts/library-thread-release/runtime/default/scr_url_params.o +0 -0
  91. package/dist/lib/runtime-darwin-arm64/artifacts/release/runtime/default/scr_array.o +0 -0
  92. package/dist/lib/runtime-darwin-arm64/artifacts/release/runtime/default/scr_assert.o +0 -0
  93. package/dist/lib/runtime-darwin-arm64/artifacts/release/runtime/default/scr_async.o +0 -0
  94. package/dist/lib/runtime-darwin-arm64/artifacts/release/runtime/default/scr_bytes_io.o +0 -0
  95. package/dist/lib/runtime-darwin-arm64/artifacts/release/runtime/default/scr_events.o +0 -0
  96. package/dist/lib/runtime-darwin-arm64/artifacts/release/runtime/default/scr_exception.o +0 -0
  97. package/dist/lib/runtime-darwin-arm64/artifacts/release/runtime/default/scr_file_handle.o +0 -0
  98. package/dist/lib/runtime-darwin-arm64/artifacts/release/runtime/default/scr_json.o +0 -0
  99. package/dist/lib/runtime-darwin-arm64/artifacts/release/runtime/default/scr_lib.o +0 -0
  100. package/dist/lib/runtime-darwin-arm64/artifacts/release/runtime/default/scr_map.o +0 -0
  101. package/dist/lib/runtime-darwin-arm64/artifacts/release/runtime/default/scr_node_builtin.o +0 -0
  102. package/dist/lib/runtime-darwin-arm64/artifacts/release/runtime/default/scr_regex.o +0 -0
  103. package/dist/lib/runtime-darwin-arm64/artifacts/release/runtime/default/scr_symbol.o +0 -0
  104. package/dist/lib/runtime-darwin-arm64/artifacts/release/runtime/default/scr_url.o +0 -0
  105. package/dist/lib/runtime-darwin-arm64/artifacts/release/runtime/default/scr_url_params.o +0 -0
  106. package/dist/lib/runtime-darwin-arm64/artifacts/release/runtime/default/scr_util_compare.o +0 -0
  107. package/dist/lib/runtime-darwin-arm64/artifacts/release/runtime/default/scr_util_style.o +0 -0
  108. package/dist/lib/runtime-darwin-arm64/artifacts/release/runtime/dynamic/scr_array.o +0 -0
  109. package/dist/lib/runtime-darwin-arm64/artifacts/release/runtime/dynamic/scr_assert.o +0 -0
  110. package/dist/lib/runtime-darwin-arm64/artifacts/release/runtime/dynamic/scr_async.o +0 -0
  111. package/dist/lib/runtime-darwin-arm64/artifacts/release/runtime/dynamic/scr_bytes_io.o +0 -0
  112. package/dist/lib/runtime-darwin-arm64/artifacts/release/runtime/dynamic/scr_events.o +0 -0
  113. package/dist/lib/runtime-darwin-arm64/artifacts/release/runtime/dynamic/scr_exception.o +0 -0
  114. package/dist/lib/runtime-darwin-arm64/artifacts/release/runtime/dynamic/scr_file_handle.o +0 -0
  115. package/dist/lib/runtime-darwin-arm64/artifacts/release/runtime/dynamic/scr_island.o +0 -0
  116. package/dist/lib/runtime-darwin-arm64/artifacts/release/runtime/dynamic/scr_json.o +0 -0
  117. package/dist/lib/runtime-darwin-arm64/artifacts/release/runtime/dynamic/scr_lib.o +0 -0
  118. package/dist/lib/runtime-darwin-arm64/artifacts/release/runtime/dynamic/scr_map.o +0 -0
  119. package/dist/lib/runtime-darwin-arm64/artifacts/release/runtime/dynamic/scr_node_builtin.o +0 -0
  120. package/dist/lib/runtime-darwin-arm64/artifacts/release/runtime/dynamic/scr_regex.o +0 -0
  121. package/dist/lib/runtime-darwin-arm64/artifacts/release/runtime/dynamic/scr_symbol.o +0 -0
  122. package/dist/lib/runtime-darwin-arm64/artifacts/release/runtime/dynamic/scr_url.o +0 -0
  123. package/dist/lib/runtime-darwin-arm64/artifacts/release/runtime/dynamic/scr_url_params.o +0 -0
  124. package/dist/lib/runtime-darwin-arm64/artifacts/release/runtime/dynamic/scr_util_compare.o +0 -0
  125. package/dist/lib/runtime-darwin-arm64/artifacts/release/runtime/dynamic/scr_util_style.o +0 -0
  126. package/dist/lib/runtime-darwin-arm64/artifacts/release/runtime/dynamic/scr_web.o +0 -0
  127. package/dist/lib/runtime-darwin-arm64/package.json +1 -1
  128. package/dist/lib/runtime-darwin-arm64/runtime-pack.json +351 -225
  129. package/dist/lib/runtime-sources/package.json +1 -1
  130. package/dist/lib/runtime-sources/src/scr_array.c +5 -0
  131. package/dist/lib/runtime-sources/src/scr_assert.c +26 -0
  132. package/dist/lib/runtime-sources/src/scr_async.c +44 -0
  133. package/dist/lib/runtime-sources/src/scr_bytes_io.c +305 -48
  134. package/dist/lib/runtime-sources/src/scr_events.c +9 -0
  135. package/dist/lib/runtime-sources/src/scr_exception.c +1 -0
  136. package/dist/lib/runtime-sources/src/scr_file_handle.c +11 -0
  137. package/dist/lib/runtime-sources/src/scr_island.c +1001 -417
  138. package/dist/lib/runtime-sources/src/scr_island_manifest.h +11 -7
  139. package/dist/lib/runtime-sources/src/scr_json.c +135 -2
  140. package/dist/lib/runtime-sources/src/scr_lib.c +379 -12
  141. package/dist/lib/runtime-sources/src/scr_map.c +8 -0
  142. package/dist/lib/runtime-sources/src/scr_node_builtin.c +78 -0
  143. package/dist/lib/runtime-sources/src/scr_regex.c +65 -0
  144. package/dist/lib/runtime-sources/src/scr_runtime.h +69 -1
  145. package/dist/lib/runtime-sources/src/scr_symbol.c +5 -0
  146. package/dist/lib/runtime-sources/src/scr_system_errors.h +573 -0
  147. package/dist/lib/runtime-sources/src/scr_url.c +515 -59
  148. package/dist/lib/runtime-sources/src/scr_url_internal.h +6 -0
  149. package/dist/lib/runtime-sources/src/scr_url_params.c +10 -0
  150. package/dist/lib/runtime-sources/src/scr_util_compare.c +222 -0
  151. package/dist/lib/runtime-sources/src/scr_util_style.c +337 -0
  152. package/dist/lib/runtime-sources/src/scr_web.c +720 -238
  153. package/package.json +1 -1
@@ -5,9 +5,11 @@
5
5
  * under -DSCR_DYNAMIC, like scr_island.c.
6
6
  *
7
7
  * This file owns the semantics-heavy globals: the streams library
8
- * (ReadableStream, TransformStream), the encoders (TextEncoder,
8
+ * (ReadableStream, WritableStream with queued sink writes and strategy
9
+ * backpressure, TransformStream with readable-side backpressure), the encoders
10
+ * (TextEncoder,
9
11
  * TextDecoder — the WHATWG utf-8 state machine exactly, other labels
10
- * fenced — and TextDecoderStream), URLSearchParams, btoa/atob, Headers,
12
+ * fenced — and TextEncoderStream/TextDecoderStream), URLSearchParams, btoa/atob, Headers,
11
13
  * the fetch value classes (Request/Response with stream-or-bytes bodies,
12
14
  * consumed through text/json/bytes/arrayBuffer; clone() fenced; plus the
13
15
  * __scr_mk_response seam scr_fetch.c's glue builds wire responses
@@ -24,16 +26,12 @@
24
26
  * implementations (the web-streams npm fixture, corpus 1120) plus
25
27
  * scriptc-only fence tests (tests/harness/web-globals.test.ts). OUT,
26
28
  * with clear errors: byte streams ('type': 'bytes') and BYOB readers,
27
- * tee(), pipeTo(), custom queuing strategies (accepted, ignored — HWM is
28
- * always 1), a global WritableStream (TransformStream.writable is an
29
- * internal writable; the class is not exposed), and non-utf-8 TextDecoder
30
- * labels. One documented behavioral divergence: transforms run EAGERLY on
31
- * write (no readable-side backpressure), so writer.write() never blocks
32
- * on downstream demand — order-exact, buffering-different. SEMANTICS.md
33
- * states the subset; keep the three in sync.
29
+ * stream transfer, and non-utf-8 TextDecoder labels. Default-stream piping,
30
+ * teeing, reader/controller lifecycles, and transform backpressure are
31
+ * pinned by differential fixtures; the limitations page records the boundaries.
34
32
  *
35
- * The JS was verified line-for-line against Node's native WHATWG
36
- * implementations (a transcript battery: reader protocol, pull timing,
33
+ * JS behavior is pinned against Node's native WHATWG implementations
34
+ * by differential fixtures (a transcript battery: reader protocol, pull timing,
37
35
  * error propagation both directions, cancel-on-break, flush ordering,
38
36
  * utf-8 maximal-subpart replacement, BOM handling, form encoding, header
39
37
  * combining/sorting) running under BOTH engines before being embedded. */
@@ -134,16 +132,405 @@ static const char web_prelude[] =
134
132
  " };\n"
135
133
  " g.clearImmediate = (immediate) => { if (immediate instanceof Immediate) immediate._close(); };\n"
136
134
  "\n"
135
+ " // Queue sizing is shared by the readable and writable default streams.\n"
136
+ " const streamError = (Ctor, code, message) => Object.assign(new Ctor(message), { code });\n"
137
+ " const streamReceived = (v) => {\n"
138
+ " if (v === null || v === undefined) return String(v);\n"
139
+ " if (typeof v === 'function') return 'function ' + v.name;\n"
140
+ " if (typeof v === 'object') return 'an instance of ' + (v.constructor && v.constructor.name || 'Object');\n"
141
+ " return 'type ' + typeof v + ' (' + (typeof v === 'string' ? \"'\" + v + \"'\" : String(v) + (typeof v === 'bigint' ? 'n' : '')) + ')';\n"
142
+ " };\n"
143
+ " const streamArgument = (name, expected, v) => streamError(TypeError, 'ERR_INVALID_ARG_TYPE',\n"
144
+ " 'The \"' + name + '\" ' + (name.includes('.') ? 'property' : 'argument') + ' must be ' + expected + '. Received ' + streamReceived(v));\n"
145
+ " const streamInvalidThis = (name) => streamError(TypeError, 'ERR_INVALID_THIS', 'Value of \"this\" must be of type ' + name);\n"
146
+ " const streamInvalidState = (message) => streamError(TypeError, 'ERR_INVALID_STATE', 'Invalid state: ' + message);\n"
147
+ " const streamInvalidValue = (name, value) => streamError(RangeError, 'ERR_INVALID_ARG_VALUE',\n"
148
+ " \"The \" + (name.includes('.') ? 'property' : 'argument') + \" '\" + name + \"' is invalid. Received \" + (typeof value === 'string' ? \"'\" + value + \"'\" : String(value)));\n"
149
+ " const streamStrategy = (strategy, fallback) => {\n"
150
+ " if (strategy === undefined || strategy === null) strategy = {};\n"
151
+ " if (typeof strategy !== 'object' && typeof strategy !== 'function') throw streamArgument('strategy', 'of type object', strategy);\n"
152
+ " const size = strategy.size;\n"
153
+ " if (size !== undefined && typeof size !== 'function') throw streamArgument('strategy.size', 'of type function', size);\n"
154
+ " const value = strategy.highWaterMark;\n"
155
+ " const highWaterMark = value === undefined ? fallback : +value;\n"
156
+ " if (Number.isNaN(highWaterMark) || highWaterMark < 0) throw streamInvalidValue('strategy.highWaterMark', highWaterMark);\n"
157
+ " return { size: size === undefined ? () => 1 : size, highWaterMark };\n"
158
+ " };\n"
159
+ " const streamChunkSize = (value) => {\n"
160
+ " const size = +value;\n"
161
+ " if (!Number.isFinite(size) || size < 0) throw streamInvalidValue('size', size);\n"
162
+ " return size;\n"
163
+ " };\n"
164
+ " const streamDeferred = (handled = false) => {\n"
165
+ " const d = { settled: false };\n"
166
+ " d.promise = new Promise((resolve, reject) => {\n"
167
+ " d.resolve = () => { d.settled = true; resolve(); };\n"
168
+ " d.reject = (e) => { d.settled = true; reject(e); };\n"
169
+ " });\n"
170
+ " if (handled) d.promise.catch(() => {});\n"
171
+ " return d;\n"
172
+ " };\n"
173
+ " const streamRejectSlot = (owner, key, e) => {\n"
174
+ " if (owner[key].settled) owner[key] = streamDeferred(true);\n"
175
+ " owner[key].reject(e);\n"
176
+ " };\n"
177
+ " const countStrategyStates = new WeakMap();\n"
178
+ " const byteStrategyStates = new WeakMap();\n"
179
+ " const countSize = Object.defineProperty(() => 1, 'name', { value: 'size' });\n"
180
+ " const byteSize = Object.defineProperty((chunk) => chunk.byteLength, 'name', { value: 'size' });\n"
181
+ " const strategyInit = (init) => {\n"
182
+ " if (init === null || typeof init !== 'object' || Array.isArray(init)) throw streamArgument('init', 'of type object', init);\n"
183
+ " if (init.highWaterMark === undefined) throw streamError(TypeError, 'ERR_MISSING_OPTION', 'init.highWaterMark is required');\n"
184
+ " return +init.highWaterMark;\n"
185
+ " };\n"
186
+ " class CountQueuingStrategy {\n"
187
+ " constructor(init) { countStrategyStates.set(this, strategyInit(init)); }\n"
188
+ " get highWaterMark() { if (!countStrategyStates.has(this)) throw new TypeError('Invalid CountQueuingStrategy receiver'); return countStrategyStates.get(this); }\n"
189
+ " get size() { if (!countStrategyStates.has(this)) throw new TypeError('Invalid CountQueuingStrategy receiver'); return countSize; }\n"
190
+ " }\n"
191
+ " class ByteLengthQueuingStrategy {\n"
192
+ " constructor(init) { byteStrategyStates.set(this, strategyInit(init)); }\n"
193
+ " get highWaterMark() { if (!byteStrategyStates.has(this)) throw new TypeError('Invalid ByteLengthQueuingStrategy receiver'); return byteStrategyStates.get(this); }\n"
194
+ " get size() { if (!byteStrategyStates.has(this)) throw new TypeError('Invalid ByteLengthQueuingStrategy receiver'); return byteSize; }\n"
195
+ " }\n"
196
+ "\n"
197
+ " const writableStreams = new WeakMap();\n"
198
+ " const writableWriters = new WeakMap();\n"
199
+ " const writableControllers = new WeakMap();\n"
200
+ " const writableToken = Symbol('WritableStreamDefaultController');\n"
201
+ " const writableWriterState = (s) => s.writer === undefined ? undefined : writableWriters.get(s.writer);\n"
202
+ " const writableBackpressure = (s) => {\n"
203
+ " const pressure = s.highWaterMark - s.queueSize <= 0;\n"
204
+ " const w = writableWriterState(s);\n"
205
+ " if (w && s.backpressure !== pressure) {\n"
206
+ " if (pressure) w.ready = streamDeferred(true);\n"
207
+ " else w.ready.resolve();\n"
208
+ " }\n"
209
+ " s.backpressure = pressure;\n"
210
+ " };\n"
211
+ " const writableClear = (s) => { s.write = s.close = s.abort = s.size = undefined; };\n"
212
+ " const writableRejectClosed = (s) => {\n"
213
+ " if (s.closeRequest) { s.closeRequest.reject(s.error); s.closeRequest = undefined; }\n"
214
+ " const w = writableWriterState(s);\n"
215
+ " if (w) w.closed.reject(s.error);\n"
216
+ " };\n"
217
+ " const writableFinishError = (s) => {\n"
218
+ " s.state = 'errored';\n"
219
+ " for (const q of s.queue) if (q.kind === 'write') q.request.reject(s.error);\n"
220
+ " s.queue = []; s.queueSize = 0;\n"
221
+ " const a = s.abortRequest;\n"
222
+ " s.abortRequest = undefined;\n"
223
+ " if (!a) { writableRejectClosed(s); return; }\n"
224
+ " if (a.alreadyErroring) { a.request.reject(s.error); writableRejectClosed(s); return; }\n"
225
+ " const result = writableInvoke(s.abort, s.sink, [a.reason]);\n"
226
+ " writableClear(s);\n"
227
+ " result.then(\n"
228
+ " () => { a.request.resolve(); writableRejectClosed(s); },\n"
229
+ " (e) => { a.request.reject(e); writableRejectClosed(s); },\n"
230
+ " );\n"
231
+ " };\n"
232
+ " const writableError = (s, e) => {\n"
233
+ " if (s.state !== 'writable') return;\n"
234
+ " s.state = 'erroring'; s.error = e;\n"
235
+ " const w = writableWriterState(s);\n"
236
+ " if (w) streamRejectSlot(w, 'ready', e);\n"
237
+ " if (s.started && !s.inFlight) writableFinishError(s);\n"
238
+ " };\n"
239
+ " // Async callback wrappers adopt returned thenables and turn sync throws into rejections.\n"
240
+ " const writableInvoke = async (fn, receiver, args) => fn ? fn.apply(receiver, args) : undefined;\n"
241
+ " const writableAdvance = (s) => {\n"
242
+ " if (!s.started || s.inFlight || s.state === 'closed' || s.state === 'errored') return;\n"
243
+ " if (s.state === 'erroring') { writableFinishError(s); return; }\n"
244
+ " if (!s.queue.length) return;\n"
245
+ " const q = s.queue[0];\n"
246
+ " s.inFlight = q;\n"
247
+ " if (q.kind === 'close') {\n"
248
+ " s.queue.shift();\n"
249
+ " const result = writableInvoke(s.close, s.sink, []);\n"
250
+ " writableClear(s);\n"
251
+ " result.then(\n"
252
+ " () => {\n"
253
+ " q.request.resolve(); s.inFlight = undefined;\n"
254
+ " if (s.abortRequest) { s.abortRequest.request.resolve(); s.abortRequest = undefined; }\n"
255
+ " s.state = 'closed'; s.error = undefined; s.closeRequest = undefined;\n"
256
+ " const w = writableWriterState(s);\n"
257
+ " if (w) w.closed.resolve();\n"
258
+ " },\n"
259
+ " (e) => {\n"
260
+ " q.request.reject(e); s.inFlight = undefined; s.closeRequest = undefined;\n"
261
+ " if (s.abortRequest) { s.abortRequest.request.reject(e); s.abortRequest = undefined; }\n"
262
+ " if (s.state === 'writable') writableError(s, e); else writableFinishError(s);\n"
263
+ " },\n"
264
+ " );\n"
265
+ " return;\n"
266
+ " }\n"
267
+ " writableInvoke(s.write, s.sink, [q.chunk, s.controller]).then(\n"
268
+ " () => {\n"
269
+ " q.request.resolve(); s.inFlight = undefined; s.queue.shift();\n"
270
+ " s.queueSize = Math.max(0, s.queueSize - q.size);\n"
271
+ " if (s.state === 'writable' && !s.closeRequest) writableBackpressure(s);\n"
272
+ " writableAdvance(s);\n"
273
+ " },\n"
274
+ " (e) => {\n"
275
+ " q.request.reject(e); s.inFlight = undefined; s.queue.shift();\n"
276
+ " if (s.state === 'writable') { writableClear(s); writableError(s, e); }\n"
277
+ " else writableFinishError(s);\n"
278
+ " },\n"
279
+ " );\n"
280
+ " };\n"
281
+ " const writableAbort = (s, reason) => {\n"
282
+ " if (s.state === 'closed' || s.state === 'errored') return Promise.resolve();\n"
283
+ " s.abortController.abort(reason);\n"
284
+ " if (s.abortRequest) return s.abortRequest.request.promise;\n"
285
+ " // Signal listeners may have closed or errored the stream synchronously.\n"
286
+ " if (s.state === 'closed' || s.state === 'errored') return Promise.resolve();\n"
287
+ " const alreadyErroring = s.state === 'erroring';\n"
288
+ " const request = streamDeferred();\n"
289
+ " s.abortRequest = { request, reason, alreadyErroring };\n"
290
+ " if (!alreadyErroring) writableError(s, reason);\n"
291
+ " return request.promise;\n"
292
+ " };\n"
293
+ " const writableClose = (s, writer) => {\n"
294
+ " if (s.closeRequest) return Promise.reject(streamInvalidState(writer ? 'Failure to close WritableStream' : 'Failure closing WritableStream'));\n"
295
+ " if (s.state === 'closed' || s.state === 'errored') return Promise.reject(streamInvalidState('WritableStream is closed'));\n"
296
+ " const request = streamDeferred();\n"
297
+ " s.closeRequest = request;\n"
298
+ " const w = writableWriterState(s);\n"
299
+ " if (w && s.backpressure && s.state === 'writable') w.ready.resolve();\n"
300
+ " s.queue.push({ kind: 'close', request });\n"
301
+ " writableAdvance(s);\n"
302
+ " return request.promise;\n"
303
+ " };\n"
304
+ " class WritableStream {\n"
305
+ " constructor(sink = {}, strategy) {\n"
306
+ " if (sink === null || (typeof sink !== 'object' && typeof sink !== 'function')) throw streamArgument('sink', 'of type object', sink);\n"
307
+ " const type = sink.type;\n"
308
+ " if (type !== undefined) throw streamInvalidValue('type', type);\n"
309
+ " const { size, highWaterMark } = streamStrategy(strategy, 1);\n"
310
+ " // Capture algorithms once: replacing sink methods later must not change the stream.\n"
311
+ " const start = sink.start, write = sink.write, close = sink.close, abort = sink.abort;\n"
312
+ " for (const [name, fn] of [['write', write], ['close', close], ['abort', abort]]) {\n"
313
+ " if (fn && typeof fn !== 'function') throw streamArgument('sink.' + name, 'of type function', fn);\n"
314
+ " }\n"
315
+ " if (start && typeof start !== 'function') throw new TypeError('Bind must be called on a function');\n"
316
+ " const s = { sink, size, highWaterMark, write, close, abort, state: 'writable', error: undefined,\n"
317
+ " queue: [], queueSize: 0, started: false, inFlight: undefined, writer: undefined,\n"
318
+ " closeRequest: undefined, abortRequest: undefined, backpressure: highWaterMark <= 0, abortController: new g.AbortController() };\n"
319
+ " writableStreams.set(this, s);\n"
320
+ " s.controller = new WritableStreamDefaultController(writableToken, s);\n"
321
+ " const result = start ? start.call(sink, s.controller) : undefined;\n"
322
+ " new Promise((resolve) => resolve(result)).then(\n"
323
+ " () => { s.started = true; writableAdvance(s); },\n"
324
+ " (e) => { s.started = true; if (s.state === 'writable') writableError(s, e); else writableFinishError(s); },\n"
325
+ " );\n"
326
+ " }\n"
327
+ " get locked() { const s = writableStreams.get(this); if (!s) throw streamInvalidThis('WritableStream'); return s.writer !== undefined; }\n"
328
+ " getWriter() { if (!writableStreams.has(this)) throw streamInvalidThis('WritableStream'); return new WritableStreamDefaultWriter(this); }\n"
329
+ " abort(reason) {\n"
330
+ " const s = writableStreams.get(this);\n"
331
+ " if (!s) return Promise.reject(streamInvalidThis('WritableStream'));\n"
332
+ " if (s.writer !== undefined) return Promise.reject(streamInvalidState('WritableStream is locked'));\n"
333
+ " return writableAbort(s, reason);\n"
334
+ " }\n"
335
+ " close() {\n"
336
+ " const s = writableStreams.get(this);\n"
337
+ " if (!s) return Promise.reject(streamInvalidThis('WritableStream'));\n"
338
+ " if (s.writer !== undefined) return Promise.reject(streamInvalidState('WritableStream is locked'));\n"
339
+ " return writableClose(s, false);\n"
340
+ " }\n"
341
+ " }\n"
342
+ " class WritableStreamDefaultWriter {\n"
343
+ " constructor(stream) {\n"
344
+ " const s = writableStreams.get(stream);\n"
345
+ " if (!s) throw streamArgument('stream', 'an instance of WritableStream', stream);\n"
346
+ " if (s.writer !== undefined) throw streamInvalidState('WritableStream is locked');\n"
347
+ " const w = { stream: s, ready: streamDeferred(true), closed: streamDeferred(true) };\n"
348
+ " writableWriters.set(this, w); s.writer = this;\n"
349
+ " if (s.state === 'writable') { if (!s.backpressure || s.closeRequest) w.ready.resolve(); }\n"
350
+ " else if (s.state === 'closed') { w.ready.resolve(); w.closed.resolve(); }\n"
351
+ " else { w.ready.reject(s.error); if (s.state === 'errored') w.closed.reject(s.error); }\n"
352
+ " }\n"
353
+ " get closed() { const w = writableWriters.get(this); return w ? w.closed.promise : Promise.reject(streamInvalidThis('WritableStreamDefaultWriter')); }\n"
354
+ " get ready() { const w = writableWriters.get(this); return w ? w.ready.promise : Promise.reject(streamInvalidThis('WritableStreamDefaultWriter')); }\n"
355
+ " get desiredSize() {\n"
356
+ " const w = writableWriters.get(this);\n"
357
+ " if (!w) throw streamInvalidThis('WritableStreamDefaultWriter');\n"
358
+ " if (!w.stream) throw streamInvalidState('Writer is not bound to a WritableStream');\n"
359
+ " const s = w.stream;\n"
360
+ " return s.state === 'closed' ? 0 : s.state === 'writable' ? s.highWaterMark - s.queueSize : null;\n"
361
+ " }\n"
362
+ " write(chunk) {\n"
363
+ " const w = writableWriters.get(this);\n"
364
+ " if (!w) return Promise.reject(streamInvalidThis('WritableStreamDefaultWriter'));\n"
365
+ " const s = w.stream;\n"
366
+ " if (!s) return Promise.reject(streamInvalidState('Writer is not bound to a WritableStream'));\n"
367
+ " let value = 1;\n"
368
+ " try { if (s.size) value = s.size.call(undefined, chunk); }\n"
369
+ " catch (e) { if (s.state === 'writable') { writableClear(s); writableError(s, e); } }\n"
370
+ " if (w.stream !== s) return Promise.reject(streamInvalidState('Mismatched WritableStreams'));\n"
371
+ " if (s.state === 'errored') return Promise.reject(s.error);\n"
372
+ " if (s.closeRequest || s.state === 'closed') return Promise.reject(streamInvalidState('WritableStream is closed'));\n"
373
+ " if (s.state === 'erroring') return Promise.reject(s.error);\n"
374
+ " const request = streamDeferred();\n"
375
+ " let size;\n"
376
+ " try { size = streamChunkSize(value); }\n"
377
+ " catch (e) {\n"
378
+ " // The write request joins the queue before invalid sizing errors the stream.\n"
379
+ " s.queue.push({ kind: 'write', request }); writableClear(s); writableError(s, e);\n"
380
+ " return request.promise;\n"
381
+ " }\n"
382
+ " s.queue.push({ kind: 'write', chunk, size, request }); s.queueSize += size;\n"
383
+ " writableBackpressure(s); writableAdvance(s);\n"
384
+ " return request.promise;\n"
385
+ " }\n"
386
+ " close() {\n"
387
+ " const w = writableWriters.get(this);\n"
388
+ " if (!w) return Promise.reject(streamInvalidThis('WritableStreamDefaultWriter'));\n"
389
+ " if (!w.stream) return Promise.reject(streamInvalidState('Writer is not bound to a WritableStream'));\n"
390
+ " return writableClose(w.stream, true);\n"
391
+ " }\n"
392
+ " abort(reason) {\n"
393
+ " const w = writableWriters.get(this);\n"
394
+ " if (!w) return Promise.reject(streamInvalidThis('WritableStreamDefaultWriter'));\n"
395
+ " if (!w.stream) return Promise.reject(streamInvalidState('Writer is not bound to a WritableStream'));\n"
396
+ " return writableAbort(w.stream, reason);\n"
397
+ " }\n"
398
+ " releaseLock() {\n"
399
+ " const w = writableWriters.get(this);\n"
400
+ " if (!w) throw streamInvalidThis('WritableStreamDefaultWriter');\n"
401
+ " if (!w.stream) return;\n"
402
+ " const e = streamInvalidState('Writer has been released');\n"
403
+ " streamRejectSlot(w, 'ready', e); streamRejectSlot(w, 'closed', e);\n"
404
+ " w.stream.writer = undefined; w.stream = undefined;\n"
405
+ " }\n"
406
+ " }\n"
407
+ " class WritableStreamDefaultController {\n"
408
+ " constructor(token, stream) {\n"
409
+ " if (token !== writableToken) throw streamError(TypeError, 'ERR_ILLEGAL_CONSTRUCTOR', 'Illegal constructor');\n"
410
+ " writableControllers.set(this, stream);\n"
411
+ " }\n"
412
+ " get signal() { const s = writableControllers.get(this); if (!s) throw streamInvalidThis('WritableStreamDefaultController'); return s.abortController.signal; }\n"
413
+ " error(e) {\n"
414
+ " const s = writableControllers.get(this);\n"
415
+ " if (!s) throw streamInvalidThis('WritableStreamDefaultController');\n"
416
+ " if (s.state === 'writable') { writableClear(s); writableError(s, e); }\n"
417
+ " }\n"
418
+ " }\n"
419
+ " for (const C of [WritableStream, WritableStreamDefaultWriter, WritableStreamDefaultController, CountQueuingStrategy, ByteLengthQueuingStrategy]) {\n"
420
+ " Object.defineProperty(C.prototype, Symbol.toStringTag, { value: C.name, configurable: true });\n"
421
+ " for (const name of Object.getOwnPropertyNames(C.prototype)) {\n"
422
+ " if (name !== 'constructor') Object.defineProperty(C.prototype, name, { enumerable: true });\n"
423
+ " }\n"
424
+ " g[C.name] = C;\n"
425
+ " }\n"
426
+ "\n"
427
+ " const readableStreams = new WeakSet();\n"
428
+ " const readableReaders = new WeakMap(), readableControllers = new WeakMap();\n"
429
+ " const readableToken = Symbol('ReadableStreamDefaultController');\n"
430
+ " const readableControllerStream = (controller) => {\n"
431
+ " const stream = readableControllers.get(controller);\n"
432
+ " if (!stream) throw new TypeError('Invalid ReadableStreamDefaultController receiver');\n"
433
+ " return stream;\n"
434
+ " };\n"
435
+ " class ReadableStreamDefaultController {\n"
436
+ " constructor(token, stream) {\n"
437
+ " if (token !== readableToken) throw streamError(TypeError, 'ERR_ILLEGAL_CONSTRUCTOR', 'Illegal constructor');\n"
438
+ " readableControllers.set(this, stream);\n"
439
+ " }\n"
440
+ " get desiredSize() {\n"
441
+ " const s = readableControllerStream(this);\n"
442
+ " return s._state === 'errored' ? null : s._state === 'closed' ? 0 : s._highWaterMark - s._queueSize;\n"
443
+ " }\n"
444
+ " enqueue(chunk) {\n"
445
+ " const s = readableControllerStream(this);\n"
446
+ " if (s._closeRequested || s._state !== 'readable') throw streamInvalidState('Controller is already closed');\n"
447
+ " if (s._reads.length) s._reads.shift().resolve({ value: chunk, done: false });\n"
448
+ " else {\n"
449
+ " let size;\n"
450
+ " try { size = streamChunkSize(s._size.call(undefined, chunk)); }\n"
451
+ " catch (e) { s._errorStream(e); throw e; }\n"
452
+ " s._queue.push({ chunk, size }); s._queueSize += size;\n"
453
+ " }\n"
454
+ " s._maybePull();\n"
455
+ " }\n"
456
+ " close() {\n"
457
+ " const s = readableControllerStream(this);\n"
458
+ " if (s._closeRequested || s._state !== 'readable') throw streamInvalidState('Controller is already closed');\n"
459
+ " s._closeRequested = true;\n"
460
+ " if (!s._queue.length) s._finishClose();\n"
461
+ " }\n"
462
+ " error(e) { readableControllerStream(this)._errorStream(e); }\n"
463
+ " }\n"
464
+ " class ReadableStreamDefaultReader {\n"
465
+ " constructor(stream) {\n"
466
+ " if (!readableStreams.has(stream)) throw streamArgument('stream', 'an instance of ReadableStream', stream);\n"
467
+ " if (stream._locked) throw streamInvalidState('ReadableStream is locked');\n"
468
+ " const r = { stream, closed: streamDeferred(true) };\n"
469
+ " readableReaders.set(this, r); stream._locked = true;\n"
470
+ " stream._closedResolve = r.closed.resolve; stream._closedReject = r.closed.reject;\n"
471
+ " if (stream._state === 'closed') r.closed.resolve();\n"
472
+ " else if (stream._state === 'errored') r.closed.reject(stream._storedError);\n"
473
+ " }\n"
474
+ " get closed() {\n"
475
+ " const r = readableReaders.get(this);\n"
476
+ " return r ? r.closed.promise : Promise.reject(streamInvalidThis('ReadableStreamDefaultReader'));\n"
477
+ " }\n"
478
+ " read() {\n"
479
+ " const r = readableReaders.get(this);\n"
480
+ " if (!r) return Promise.reject(streamInvalidThis('ReadableStreamDefaultReader'));\n"
481
+ " const s = r.stream;\n"
482
+ " if (!s) return Promise.reject(streamInvalidState('The reader is not attached to a stream'));\n"
483
+ " if (s._queue.length) {\n"
484
+ " const queued = s._queue.shift();\n"
485
+ " s._queueSize = Math.max(0, s._queueSize - queued.size);\n"
486
+ " if (s._closeRequested && !s._queue.length) s._finishClose(); else s._maybePull();\n"
487
+ " return Promise.resolve({ value: queued.chunk, done: false });\n"
488
+ " }\n"
489
+ " if (s._state === 'closed') return Promise.resolve({ value: undefined, done: true });\n"
490
+ " if (s._state === 'errored') return Promise.reject(s._storedError);\n"
491
+ " return new Promise((resolve, reject) => { s._reads.push({ resolve, reject }); s._maybePull(); });\n"
492
+ " }\n"
493
+ " cancel(reason) {\n"
494
+ " const r = readableReaders.get(this);\n"
495
+ " if (!r) return Promise.reject(streamInvalidThis('ReadableStreamDefaultReader'));\n"
496
+ " if (!r.stream) return Promise.reject(streamInvalidState('The reader is not attached to a stream'));\n"
497
+ " return r.stream._cancelInternal(reason);\n"
498
+ " }\n"
499
+ " releaseLock() {\n"
500
+ " const r = readableReaders.get(this);\n"
501
+ " if (!r) throw streamInvalidThis('ReadableStreamDefaultReader');\n"
502
+ " const s = r.stream;\n"
503
+ " if (!s) return;\n"
504
+ " streamRejectSlot(r, 'closed', streamInvalidState('Reader released'));\n"
505
+ " const reads = s._reads; s._reads = [];\n"
506
+ " const error = streamInvalidState('Releasing reader');\n"
507
+ " for (const request of reads) request.reject(error);\n"
508
+ " s._locked = false; s._closedResolve = s._closedReject = null; r.stream = undefined;\n"
509
+ " }\n"
510
+ " }\n"
511
+ " for (const C of [ReadableStreamDefaultReader, ReadableStreamDefaultController]) {\n"
512
+ " Object.defineProperty(C.prototype, Symbol.toStringTag, { value: C.name, configurable: true });\n"
513
+ " for (const name of Object.getOwnPropertyNames(C.prototype)) if (name !== 'constructor') Object.defineProperty(C.prototype, name, { enumerable: true });\n"
514
+ " g[C.name] = C;\n"
515
+ " }\n"
516
+ "\n"
137
517
  " class ReadableStream {\n"
138
- " constructor(source, _strategy) {\n"
518
+ " constructor(source, strategy) {\n"
139
519
  " if (source === undefined) source = {};\n"
140
- " if (source === null || typeof source !== 'object') {\n"
141
- " throw new TypeError('underlying source must be an object');\n"
142
- " }\n"
143
- " if (source.type !== undefined) {\n"
144
- " throw new RangeError(\"byte streams (type: 'bytes') are not supported in the scriptc island\");\n"
520
+ " if (source === null || (typeof source !== 'object' && typeof source !== 'function')) {\n"
521
+ " throw streamArgument('source', 'of type object', source);\n"
145
522
  " }\n"
146
- " this._src = source;\n"
523
+ " if (strategy !== undefined && strategy !== null && typeof strategy !== 'object' && typeof strategy !== 'function') throw streamArgument('strategy', 'of type object', strategy);\n"
524
+ " const size = strategy?.size, mark = strategy?.highWaterMark, type = source.type;\n"
525
+ " if (`${type}` === 'bytes') throw new RangeError(\"byte streams (type: 'bytes') are not supported in the scriptc island\");\n"
526
+ " if (type !== undefined) throw streamError(TypeError, 'ERR_INVALID_ARG_VALUE', \"The property 'source.type' is invalid. Received \" + (typeof type === 'string' ? \"'\" + type + \"'\" : String(type)));\n"
527
+ " const highWaterMark = mark === undefined ? 1 : +mark;\n"
528
+ " if (Number.isNaN(highWaterMark) || highWaterMark < 0) throw streamInvalidValue('strategy.highWaterMark', highWaterMark);\n"
529
+ " const sizing = streamStrategy({ size, highWaterMark }, 1);\n"
530
+ " this._highWaterMark = sizing.highWaterMark;\n"
531
+ " this._size = sizing.size;\n"
532
+ " this._queueSize = 0;\n"
533
+ " this._src = source; readableStreams.add(this);\n"
147
534
  " this._queue = [];\n"
148
535
  " this._state = 'readable';\n"
149
536
  " this._storedError = undefined;\n"
@@ -155,34 +542,15 @@ static const char web_prelude[] =
155
542
  " this._pullAgain = false;\n"
156
543
  " this._closedResolve = null;\n"
157
544
  " this._closedReject = null;\n"
158
- " const self = this;\n"
159
- " this._controller = {\n"
160
- " get desiredSize() {\n"
161
- " if (self._state === 'errored') return null;\n"
162
- " if (self._state === 'closed') return 0;\n"
163
- " return 1 - self._queue.length;\n"
164
- " },\n"
165
- " enqueue(chunk) {\n"
166
- " if (self._closeRequested || self._state !== 'readable') {\n"
167
- " throw new TypeError('cannot enqueue on a stream that is ' + (self._closeRequested ? 'closing' : self._state));\n"
168
- " }\n"
169
- " if (self._reads.length > 0) self._reads.shift().resolve({ value: chunk, done: false });\n"
170
- " else self._queue.push(chunk);\n"
171
- " self._maybePull();\n"
172
- " },\n"
173
- " close() {\n"
174
- " if (self._closeRequested || self._state !== 'readable') {\n"
175
- " throw new TypeError('cannot close a stream that is ' + (self._closeRequested ? 'closing' : self._state));\n"
176
- " }\n"
177
- " self._closeRequested = true;\n"
178
- " if (self._queue.length === 0) self._finishClose();\n"
179
- " },\n"
180
- " error(e) {\n"
181
- " self._errorStream(e);\n"
182
- " },\n"
183
- " };\n"
545
+ " const start = source.start, pull = source.pull, cancel = source.cancel;\n"
546
+ " for (const [name, fn] of [['pull', pull], ['cancel', cancel]]) {\n"
547
+ " if (fn && typeof fn !== 'function') throw streamArgument('source.' + name, 'of type function', fn);\n"
548
+ " }\n"
549
+ " if (start && typeof start !== 'function') throw new TypeError('Bind must be called on a function');\n"
550
+ " this._pull = pull; this._cancel = cancel;\n"
551
+ " this._controller = new ReadableStreamDefaultController(readableToken, this);\n"
184
552
  " let startResult;\n"
185
- " if (source.start) startResult = source.start(this._controller); // a sync throw propagates, per spec\n"
553
+ " if (start) startResult = start.call(source, this._controller); // a sync throw propagates, per spec\n"
186
554
  " Promise.resolve(startResult).then(\n"
187
555
  " () => { this._started = true; this._maybePull(); },\n"
188
556
  " (e) => this._errorStream(e),\n"
@@ -190,7 +558,7 @@ static const char web_prelude[] =
190
558
  " }\n"
191
559
  " _finishClose() {\n"
192
560
  " if (this._state !== 'readable') return;\n"
193
- " this._state = 'closed';\n"
561
+ " this._state = 'closed'; this._pull = this._cancel = this._size = undefined;\n"
194
562
  " const reads = this._reads;\n"
195
563
  " this._reads = [];\n"
196
564
  " for (const r of reads) r.resolve({ value: undefined, done: true });\n"
@@ -199,96 +567,59 @@ static const char web_prelude[] =
199
567
  " _errorStream(e) {\n"
200
568
  " if (this._state !== 'readable') return;\n"
201
569
  " this._state = 'errored';\n"
202
- " this._storedError = e;\n"
203
- " this._queue.length = 0;\n"
570
+ " this._storedError = e; this._pull = this._cancel = this._size = undefined;\n"
571
+ " this._queue.length = 0; this._queueSize = 0;\n"
204
572
  " const reads = this._reads;\n"
205
573
  " this._reads = [];\n"
206
574
  " for (const r of reads) r.reject(e);\n"
207
575
  " if (this._closedReject) this._closedReject(e);\n"
208
576
  " }\n"
209
- " /* Proactive pull, like the spec with the default HWM-1 strategy: pull\n"
210
- " * whenever there is space (queue empty), started, and no pull running. */\n"
577
+ " /* Pull while demand is pending or the strategy has queue capacity. */\n"
211
578
  " _maybePull() {\n"
212
579
  " const src = this._src;\n"
213
- " if (!src.pull || !this._started) return;\n"
580
+ " if (!this._pull || !this._started) return;\n"
214
581
  " if (this._state !== 'readable' || this._closeRequested) return;\n"
215
- " if (this._queue.length >= 1) return;\n"
582
+ " if (!this._reads.length && this._highWaterMark - this._queueSize <= 0) return;\n"
216
583
  " if (this._pulling) { this._pullAgain = true; return; }\n"
217
584
  " this._pulling = true;\n"
218
585
  " Promise.resolve().then(() => {\n"
219
586
  " if (this._state !== 'readable' || this._closeRequested) { this._pulling = false; return; }\n"
220
587
  " let r;\n"
221
- " try { r = src.pull(this._controller); } catch (e) { this._pulling = false; this._errorStream(e); return; }\n"
588
+ " try { r = this._pull.call(src, this._controller); } catch (e) { this._pulling = false; this._errorStream(e); return; }\n"
222
589
  " Promise.resolve(r).then(\n"
223
590
  " () => {\n"
224
591
  " this._pulling = false;\n"
225
- " if (this._pullAgain || this._queue.length === 0) { this._pullAgain = false; this._maybePull(); }\n"
592
+ " if (this._pullAgain) { this._pullAgain = false; this._maybePull(); }\n"
226
593
  " },\n"
227
594
  " (e) => { this._pulling = false; this._errorStream(e); },\n"
228
595
  " );\n"
229
596
  " });\n"
230
597
  " }\n"
231
- " get locked() { return this._locked; }\n"
598
+ " get locked() { if (!readableStreams.has(this)) throw streamInvalidThis('ReadableStream'); return this._locked; }\n"
232
599
  " cancel(reason) {\n"
233
- " if (this._locked) return Promise.reject(new TypeError('cannot cancel a locked ReadableStream'));\n"
600
+ " if (!readableStreams.has(this)) return Promise.reject(streamInvalidThis('ReadableStream'));\n"
601
+ " if (this._locked) return Promise.reject(streamInvalidState('ReadableStream is locked'));\n"
234
602
  " return this._cancelInternal(reason);\n"
235
603
  " }\n"
236
604
  " _cancelInternal(reason) {\n"
237
605
  " if (this._state === 'closed') return Promise.resolve();\n"
238
606
  " if (this._state === 'errored') return Promise.reject(this._storedError);\n"
239
- " this._queue.length = 0;\n"
607
+ " const cancel = this._cancel;\n"
608
+ " this._queue.length = 0; this._queueSize = 0;\n"
240
609
  " this._closeRequested = true;\n"
241
610
  " this._finishClose();\n"
242
611
  " const src = this._src;\n"
243
- " return Promise.resolve(src.cancel ? src.cancel(reason) : undefined).then(() => undefined);\n"
612
+ " return writableInvoke(cancel, src, [reason]).then(() => undefined);\n"
244
613
  " }\n"
245
614
  " getReader(options) {\n"
246
- " if (options !== undefined && options !== null && options.mode !== undefined) {\n"
615
+ " if (!readableStreams.has(this)) throw streamInvalidThis('ReadableStream');\n"
616
+ " if (options !== undefined && options !== null && typeof options !== 'object' && typeof options !== 'function') throw streamArgument('options', 'of type object', options);\n"
617
+ " const mode = options?.mode;\n"
618
+ " if (mode !== undefined) {\n"
619
+ " if (`${mode}` !== 'byob') throw streamError(TypeError, 'ERR_INVALID_ARG_VALUE', \"The property 'options.mode' is invalid. Received \" + (typeof mode === 'string' ? \"'\" + mode + \"'\" : String(mode)));\n"
247
620
  " throw new TypeError('BYOB readers are not supported in the scriptc island');\n"
248
621
  " }\n"
249
- " if (this._locked) throw new TypeError('ReadableStream is locked');\n"
250
- " this._locked = true;\n"
251
- " const self = this;\n"
252
- " let res, rej;\n"
253
- " const closed = new Promise((a, b) => { res = a; rej = b; });\n"
254
- " closed.catch(() => {});\n"
255
- " this._closedResolve = res;\n"
256
- " this._closedReject = rej;\n"
257
- " if (this._state === 'closed') res();\n"
258
- " else if (this._state === 'errored') rej(this._storedError);\n"
259
- " let released = false;\n"
260
- " return {\n"
261
- " get closed() { return closed; },\n"
262
- " read() {\n"
263
- " if (released) return Promise.reject(new TypeError('reader has been released'));\n"
264
- " if (self._queue.length > 0) {\n"
265
- " const chunk = self._queue.shift();\n"
266
- " if (self._closeRequested && self._queue.length === 0) self._finishClose();\n"
267
- " else self._maybePull();\n"
268
- " return Promise.resolve({ value: chunk, done: false });\n"
269
- " }\n"
270
- " if (self._state === 'closed') return Promise.resolve({ value: undefined, done: true });\n"
271
- " if (self._state === 'errored') return Promise.reject(self._storedError);\n"
272
- " return new Promise((resolve, reject) => {\n"
273
- " self._reads.push({ resolve, reject });\n"
274
- " self._maybePull();\n"
275
- " });\n"
276
- " },\n"
277
- " releaseLock() {\n"
278
- " if (released) return;\n"
279
- " released = true;\n"
280
- " const reads = self._reads;\n"
281
- " self._reads = [];\n"
282
- " for (const r of reads) r.reject(new TypeError('reader was released'));\n"
283
- " self._locked = false;\n"
284
- " self._closedResolve = null;\n"
285
- " self._closedReject = null;\n"
286
- " },\n"
287
- " cancel(reason) {\n"
288
- " if (released) return Promise.reject(new TypeError('reader has been released'));\n"
289
- " return self._cancelInternal(reason);\n"
290
- " },\n"
291
- " };\n"
622
+ " return new ReadableStreamDefaultReader(this);\n"
292
623
  " }\n"
293
624
  " /* The iterator carries a `finished` latch because the ENGINE's\n"
294
625
  " * for-await closes more eagerly than V8: quickjs-ng calls return()\n"
@@ -301,7 +632,8 @@ static const char web_prelude[] =
301
632
  " * who DO await it — the break path — still see the rejection). */\n"
302
633
  " values(options) {\n"
303
634
  " const preventCancel = options !== undefined && options !== null && !!options.preventCancel;\n"
304
- " const reader = this.getReader();\n"
635
+ " if (!readableStreams.has(this)) throw streamInvalidThis('ReadableStream');\n"
636
+ " const reader = new ReadableStreamDefaultReader(this);\n"
305
637
  " let finished = false;\n"
306
638
  " const it = {\n"
307
639
  " next() {\n"
@@ -385,157 +717,276 @@ static const char web_prelude[] =
385
717
  " });\n"
386
718
  " }\n"
387
719
  " pipeThrough(transform, options) {\n"
388
- " if (transform === null || typeof transform !== 'object' || !transform.writable || !transform.readable) {\n"
389
- " throw new TypeError('pipeThrough requires a { writable, readable } pair');\n"
390
- " }\n"
391
- " if (options !== undefined && options !== null && options.signal !== undefined) {\n"
392
- " throw new Error('scriptc: AbortSignal is not supported by island streams yet');\n"
393
- " }\n"
394
- " pump(this, transform.writable);\n"
395
- " return transform.readable;\n"
396
- " }\n"
397
- " pipeTo() {\n"
398
- " throw new Error('ReadableStream.pipeTo is not supported in the scriptc island (use pipeThrough or a reader)');\n"
720
+ " if (!readableStreams.has(this)) throw streamInvalidThis('ReadableStream');\n"
721
+ " const readable = transform?.readable;\n"
722
+ " if (!readableStreams.has(readable)) throw streamArgument('transform.readable', 'an instance of ReadableStream', readable);\n"
723
+ " const writable = transform?.writable;\n"
724
+ " if (!writableStreams.has(writable)) throw streamArgument('transform.writable', 'an instance of WritableStream', writable);\n"
725
+ " const opts = pipeOptions(options); pipeLocks(this, writable);\n"
726
+ " pipeStreams(this, writable, opts).catch(() => {});\n"
727
+ " return readable;\n"
728
+ " }\n"
729
+ " pipeTo(destination, options) {\n"
730
+ " try {\n"
731
+ " if (!readableStreams.has(this)) throw streamInvalidThis('ReadableStream');\n"
732
+ " if (!writableStreams.has(destination)) throw streamArgument('transform.writable', 'an instance of WritableStream', destination);\n"
733
+ " const opts = pipeOptions(options); pipeLocks(this, destination);\n"
734
+ " return pipeStreams(this, destination, opts);\n"
735
+ " } catch (e) { return Promise.reject(e); }\n"
399
736
  " }\n"
400
737
  " tee() {\n"
401
- " throw new Error('ReadableStream.tee is not supported in the scriptc island');\n"
402
- " }\n"
403
- " }\n"
404
- "\n"
405
- " /* The internal writable half of TransformStream. Deliberately NOT\n"
406
- " * installed as a global WritableStream: the island fences the class\n"
407
- " * (nothing in the supported graph constructs one). */\n"
408
- " class WritableLite {\n"
409
- " constructor(sink) {\n"
410
- " this._sink = sink;\n"
411
- " this._state = 'writable';\n"
412
- " this._err = undefined;\n"
413
- " this._locked = false;\n"
414
- " this._closedRes = null;\n"
415
- " this._closedRej = null;\n"
416
- " }\n"
417
- " get locked() { return this._locked; }\n"
418
- " _error(e) {\n"
419
- " if (this._state === 'errored') return;\n"
420
- " this._state = 'errored';\n"
421
- " this._err = e;\n"
422
- " if (this._closedRej) this._closedRej(e);\n"
423
- " }\n"
424
- " abort(e) {\n"
425
- " if (this._state === 'errored') return Promise.resolve();\n"
426
- " this._error(e);\n"
427
- " return Promise.resolve(this._sink.abort ? this._sink.abort(e) : undefined).then(() => undefined);\n"
428
- " }\n"
429
- " getWriter() {\n"
430
- " if (this._locked) throw new TypeError('WritableStream is locked');\n"
431
- " this._locked = true;\n"
432
- " const self = this;\n"
433
- " let res, rej;\n"
434
- " const closed = new Promise((a, b) => { res = a; rej = b; });\n"
435
- " closed.catch(() => {});\n"
436
- " this._closedRes = res;\n"
437
- " this._closedRej = rej;\n"
438
- " if (this._state === 'errored') rej(this._err);\n"
439
- " return {\n"
440
- " get closed() { return closed; },\n"
441
- " get ready() { return self._state === 'errored' ? Promise.reject(self._err) : Promise.resolve(); },\n"
442
- " get desiredSize() {\n"
443
- " if (self._state === 'errored') return null;\n"
444
- " return self._state === 'writable' ? 1 : 0;\n"
445
- " },\n"
446
- " write(chunk) {\n"
447
- " if (self._state === 'errored') return Promise.reject(self._err);\n"
448
- " if (self._state !== 'writable') return Promise.reject(new TypeError('cannot write to a ' + self._state + ' stream'));\n"
449
- " let r;\n"
450
- " try { r = self._sink.write(chunk); } catch (e) { self._error(e); return Promise.reject(e); }\n"
451
- " return Promise.resolve(r).catch((e) => { self._error(e); throw e; });\n"
452
- " },\n"
453
- " close() {\n"
454
- " if (self._state === 'errored') return Promise.reject(self._err);\n"
455
- " if (self._state !== 'writable') return Promise.reject(new TypeError('cannot close a ' + self._state + ' stream'));\n"
456
- " self._state = 'closed';\n"
457
- " let r;\n"
458
- " try { r = self._sink.close ? self._sink.close() : undefined; } catch (e) { self._state = 'errored'; self._err = e; rej(e); return Promise.reject(e); }\n"
459
- " return Promise.resolve(r).then(\n"
460
- " () => { res(); },\n"
461
- " (e) => { self._state = 'errored'; self._err = e; rej(e); throw e; },\n"
462
- " );\n"
463
- " },\n"
464
- " abort(e) { return self.abort(e); },\n"
465
- " releaseLock() { self._locked = false; },\n"
466
- " };\n"
738
+ " if (!readableStreams.has(this)) throw streamInvalidThis('ReadableStream');\n"
739
+ " return readableTee(this);\n"
467
740
  " }\n"
468
741
  " }\n"
469
742
  "\n"
470
- " const pump = (rs, ws) => {\n"
471
- " const reader = rs.getReader();\n"
472
- " const writer = ws.getWriter();\n"
473
- " const step = () =>\n"
743
+ " const readableTee = (source) => {\n"
744
+ " const reader = new ReadableStreamDefaultReader(source);\n"
745
+ " const canceled = [false, false], reasons = [], branches = [];\n"
746
+ " let reading = false, resolveCancel;\n"
747
+ " const cancelPromise = new Promise((resolve) => { resolveCancel = resolve; });\n"
748
+ " const pull = () => {\n"
749
+ " if (reading) return;\n"
750
+ " reading = true;\n"
474
751
  " reader.read().then((r) => {\n"
475
- " if (r.done) return writer.close();\n"
476
- " return writer.write(r.value).then(step);\n"
752
+ " if (r.done) {\n"
753
+ " // Node defers branch close until its nextTick phase; the island's\n"
754
+ " // next microtask preserves final-chunk delivery before closing.\n"
755
+ " Promise.resolve().then(() => {\n"
756
+ " reading = false;\n"
757
+ " for (let i = 0; i < 2; i++) if (!canceled[i] && branches[i]._state === 'readable' && !branches[i]._closeRequested) branches[i]._controller.close();\n"
758
+ " if (!canceled[0] || !canceled[1]) resolveCancel();\n"
759
+ " });\n"
760
+ " } else {\n"
761
+ " reading = false;\n"
762
+ " for (let i = 0; i < 2; i++) if (!canceled[i] && branches[i]._state === 'readable') branches[i]._controller.enqueue(r.value);\n"
763
+ " }\n"
764
+ " }, () => { reading = false; });\n"
765
+ " };\n"
766
+ " for (let i = 0; i < 2; i++) branches.push(new ReadableStream({\n"
767
+ " pull,\n"
768
+ " cancel(reason) {\n"
769
+ " canceled[i] = true; reasons[i] = reason;\n"
770
+ " if (canceled[0] && canceled[1]) resolveCancel(source._cancelInternal([reasons[0], reasons[1]]));\n"
771
+ " return cancelPromise;\n"
772
+ " },\n"
773
+ " }));\n"
774
+ " reader.closed.catch((e) => {\n"
775
+ " for (const branch of branches) branch._errorStream(e);\n"
776
+ " if (!canceled[0] || !canceled[1]) resolveCancel();\n"
777
+ " });\n"
778
+ " return branches;\n"
779
+ " };\n"
780
+ "\n"
781
+ " const pipeOptions = (options) => {\n"
782
+ " if (options !== undefined && options !== null && typeof options !== 'object' && typeof options !== 'function') throw streamArgument('options', 'of type object', options);\n"
783
+ " const preventAbort = options?.preventAbort, preventCancel = options?.preventCancel, preventClose = options?.preventClose, signal = options?.signal;\n"
784
+ " if (signal !== undefined && (signal === null || typeof signal !== 'object' || !('aborted' in signal))) throw streamArgument('options.signal', 'an instance of AbortSignal', signal);\n"
785
+ " return { preventAbort: !!preventAbort, preventCancel: !!preventCancel, preventClose: !!preventClose, signal };\n"
786
+ " };\n"
787
+ " const pipeLocks = (source, dest) => {\n"
788
+ " if (source._locked) throw streamInvalidState('The ReadableStream is locked');\n"
789
+ " if (writableStreams.get(dest).writer !== undefined) throw streamInvalidState('The WritableStream is locked');\n"
790
+ " };\n"
791
+ " const pipeStreams = (source, dest, { preventClose, preventAbort, preventCancel, signal }) => {\n"
792
+ " const reader = new ReadableStreamDefaultReader(source), writer = new WritableStreamDefaultWriter(dest), sink = writableStreams.get(dest);\n"
793
+ " const completion = streamDeferred();\n"
794
+ " let shuttingDown = false, currentWrite = Promise.resolve(), listening = false;\n"
795
+ " const finalize = (rejected, error) => {\n"
796
+ " writer.releaseLock(); reader.releaseLock();\n"
797
+ " if (listening) signal.removeEventListener('abort', abort);\n"
798
+ " if (rejected) completion.reject(error); else completion.resolve();\n"
799
+ " };\n"
800
+ " const waitForWrites = async () => {\n"
801
+ " let write;\n"
802
+ " do { write = currentWrite; await write; } while (write !== currentWrite);\n"
803
+ " };\n"
804
+ " const shutdown = (action, rejected = false, error) => {\n"
805
+ " if (shuttingDown) return;\n"
806
+ " shuttingDown = true;\n"
807
+ " const waiting = sink.state === 'writable' && !sink.closeRequest ? waitForWrites() : Promise.resolve();\n"
808
+ " waiting.then(() => action ? action() : undefined).then(\n"
809
+ " () => finalize(rejected, error), (e) => finalize(true, e));\n"
810
+ " };\n"
811
+ " const abort = () => {\n"
812
+ " const reason = signal.reason;\n"
813
+ " shutdown(() => {\n"
814
+ " const actions = [];\n"
815
+ " if (!preventAbort && sink.state === 'writable') actions.push(writer.abort(reason));\n"
816
+ " if (!preventCancel && source._state === 'readable') actions.push(reader.cancel(reason));\n"
817
+ " return Promise.all(actions);\n"
818
+ " }, true, reason);\n"
819
+ " };\n"
820
+ " const writeChunk = (chunk) => { currentWrite = writer.write(chunk); currentWrite.catch(() => {}); };\n"
821
+ " // Buffered reads register their write before source.closed observers run. Pending\n"
822
+ " // reads queue the write during enqueue, ahead of a subsequent close notification.\n"
823
+ " const step = async () => {\n"
824
+ " if (shuttingDown) return true;\n"
825
+ " if (sink.backpressure) { await writer.ready; if (shuttingDown) return true; }\n"
826
+ " if (source._state === 'readable' && source._queue.length) {\n"
827
+ " while (source._queue.length) {\n"
828
+ " if (shuttingDown) return true;\n"
829
+ " const queued = source._queue.shift();\n"
830
+ " source._queueSize = Math.max(0, source._queueSize - queued.size);\n"
831
+ " if (source._closeRequested && !source._queue.length) source._finishClose();\n"
832
+ " writeChunk(queued.chunk);\n"
833
+ " if (sink.backpressure || sink.state !== 'writable' || sink.closeRequest) break;\n"
834
+ " }\n"
835
+ " source._maybePull();\n"
836
+ " return source._state === 'closed';\n"
837
+ " }\n"
838
+ " if (source._state === 'closed') return true;\n"
839
+ " if (source._state === 'errored') throw source._storedError;\n"
840
+ " return new Promise((resolve, reject) => {\n"
841
+ " source._reads.push({ resolve(r) {\n"
842
+ " if (r.done) resolve(true);\n"
843
+ " else Promise.resolve().then(() => { writeChunk(r.value); resolve(false); });\n"
844
+ " }, reject });\n"
845
+ " source._maybePull();\n"
477
846
  " });\n"
478
- " step().catch((e) => {\n"
479
- " writer.abort(e).catch(() => {});\n"
480
- " reader.cancel(e).catch(() => {});\n"
847
+ " };\n"
848
+ " if (signal !== undefined) {\n"
849
+ " if (signal.aborted) { abort(); return completion.promise; }\n"
850
+ " signal.addEventListener('abort', abort, { once: true, [Symbol.for('scriptc.events.resistStopPropagation')]: true });\n"
851
+ " listening = true;\n"
852
+ " }\n"
853
+ " (async () => { while (!await step()); })().catch(() => {});\n"
854
+ " const sourceError = (e) => shutdown(preventAbort ? undefined : () => writer.abort(e), true, e);\n"
855
+ " const sinkError = (e) => shutdown(preventCancel ? undefined : () => reader.cancel(e), true, e);\n"
856
+ " const sourceClose = () => shutdown(preventClose ? undefined : () => {\n"
857
+ " if (sink.closeRequest || sink.state === 'closed') return;\n"
858
+ " if (sink.state === 'errored') throw sink.error;\n"
859
+ " return writer.close();\n"
481
860
  " });\n"
861
+ " if (source._state === 'errored') sourceError(source._storedError); else reader.closed.catch(sourceError);\n"
862
+ " if (sink.state === 'errored') sinkError(sink.error); else writer.closed.catch(sinkError);\n"
863
+ " if (source._state === 'closed') sourceClose(); else reader.closed.then(sourceClose, () => {});\n"
864
+ " if (sink.closeRequest || sink.state === 'closed') {\n"
865
+ " const e = streamInvalidState('Destination WritableStream is closed');\n"
866
+ " shutdown(preventCancel ? undefined : () => reader.cancel(e), true, e);\n"
867
+ " }\n"
868
+ " return completion.promise;\n"
482
869
  " };\n"
483
870
  "\n"
871
+ " const transformStreams = new WeakMap(), transformControllers = new WeakMap();\n"
872
+ " const transformToken = Symbol('TransformStreamDefaultController');\n"
873
+ " const transformPressure = (s, pressure) => {\n"
874
+ " if (s.change) s.change.resolve();\n"
875
+ " s.change = streamDeferred(); s.backpressure = pressure;\n"
876
+ " };\n"
877
+ " const transformClear = (s) => { s.transform = s.flush = s.cancel = undefined; };\n"
878
+ " const transformUnblock = (s) => { if (s.backpressure) transformPressure(s, false); };\n"
879
+ " const transformErrorWritable = (s, e) => {\n"
880
+ " transformClear(s);\n"
881
+ " const w = writableStreams.get(s.writable);\n"
882
+ " if (w.state === 'writable') { writableClear(w); writableError(w, e); }\n"
883
+ " transformUnblock(s);\n"
884
+ " };\n"
885
+ " const transformError = (s, e) => { s.readable._errorStream(e); transformErrorWritable(s, e); };\n"
886
+ " const transformFinish = (s, kind, reason) => {\n"
887
+ " if (s.finish) return s.finish;\n"
888
+ " const d = streamDeferred(); s.finish = d.promise;\n"
889
+ " const result = writableInvoke(kind === 'close' ? s.flush : s.cancel, s.transformer, kind === 'close' ? [s.controller] : [reason]);\n"
890
+ " transformClear(s);\n"
891
+ " result.then(() => {\n"
892
+ " if (kind === 'cancel') {\n"
893
+ " const w = writableStreams.get(s.writable);\n"
894
+ " if (w.state === 'errored') { d.reject(w.error); return; }\n"
895
+ " transformErrorWritable(s, reason);\n"
896
+ " } else {\n"
897
+ " if (s.readable._state === 'errored') { d.reject(s.readable._storedError); return; }\n"
898
+ " if (kind === 'close') { if (s.readable._state === 'readable' && !s.readable._closeRequested) s.readable._controller.close(); }\n"
899
+ " else s.readable._errorStream(reason);\n"
900
+ " }\n"
901
+ " d.resolve();\n"
902
+ " }, (e) => {\n"
903
+ " if (kind === 'cancel') transformErrorWritable(s, e); else s.readable._errorStream(e);\n"
904
+ " d.reject(e);\n"
905
+ " }).catch((e) => d.reject(e));\n"
906
+ " return s.finish;\n"
907
+ " };\n"
908
+ " class TransformStreamDefaultController {\n"
909
+ " constructor(token, stream) {\n"
910
+ " if (token !== transformToken) throw streamError(TypeError, 'ERR_ILLEGAL_CONSTRUCTOR', 'Illegal constructor');\n"
911
+ " transformControllers.set(this, stream);\n"
912
+ " }\n"
913
+ " get desiredSize() {\n"
914
+ " const s = transformControllers.get(this);\n"
915
+ " if (!s) throw streamInvalidThis('TransformStreamDefaultController');\n"
916
+ " return s.readable._controller.desiredSize;\n"
917
+ " }\n"
918
+ " enqueue(chunk) {\n"
919
+ " const s = transformControllers.get(this);\n"
920
+ " if (!s) throw streamInvalidThis('TransformStreamDefaultController');\n"
921
+ " const r = s.readable;\n"
922
+ " if (r._state !== 'readable' || r._closeRequested) throw streamInvalidState('Unable to enqueue');\n"
923
+ " try { r._controller.enqueue(chunk); }\n"
924
+ " catch (e) { transformErrorWritable(s, e); throw r._storedError; }\n"
925
+ " if (!r._reads.length && r._controller.desiredSize <= 0 && !s.backpressure) transformPressure(s, true);\n"
926
+ " }\n"
927
+ " error(e) {\n"
928
+ " const s = transformControllers.get(this);\n"
929
+ " if (!s) throw streamInvalidThis('TransformStreamDefaultController');\n"
930
+ " transformError(s, e);\n"
931
+ " }\n"
932
+ " terminate() {\n"
933
+ " const s = transformControllers.get(this);\n"
934
+ " if (!s) throw streamInvalidThis('TransformStreamDefaultController');\n"
935
+ " if (s.readable._state === 'readable' && !s.readable._closeRequested) s.readable._controller.close();\n"
936
+ " transformErrorWritable(s, streamInvalidState('TransformStream has been terminated'));\n"
937
+ " }\n"
938
+ " }\n"
484
939
  " class TransformStream {\n"
485
- " constructor(transformer, _ws, _rs) {\n"
486
- " if (transformer === undefined || transformer === null) transformer = {};\n"
487
- " if (transformer.readableType !== undefined || transformer.writableType !== undefined) {\n"
488
- " throw new RangeError('readableType/writableType are not supported in the scriptc island');\n"
940
+ " constructor(transformer = {}, ws, rs) {\n"
941
+ " if (transformer === null || (typeof transformer !== 'object' && typeof transformer !== 'function')) throw streamArgument('transformer', 'of type object', transformer);\n"
942
+ " for (const [name, value] of [['writableStrategy', ws], ['readableStrategy', rs]]) {\n"
943
+ " if (value !== undefined && value !== null && typeof value !== 'object' && typeof value !== 'function') throw streamArgument(name, 'of type object', value);\n"
489
944
  " }\n"
490
- " const t = transformer;\n"
491
- " const self = this;\n"
492
- " let rc = null;\n"
493
- " this._readable = new ReadableStream({\n"
494
- " start(c) { rc = c; },\n"
495
- " cancel(reason) { self._writable._error(reason); },\n"
496
- " });\n"
497
- " const tc = {\n"
498
- " enqueue(chunk) { rc.enqueue(chunk); },\n"
499
- " error(e) {\n"
500
- " self._readable._errorStream(e);\n"
501
- " self._writable._error(e);\n"
502
- " },\n"
503
- " terminate() {\n"
504
- " if (self._readable._state === 'readable' && !self._readable._closeRequested) rc.close();\n"
505
- " self._writable._error(new TypeError('The transform stream has been terminated'));\n"
506
- " },\n"
507
- " get desiredSize() { return rc.desiredSize; },\n"
508
- " };\n"
509
- " this._controller = tc;\n"
510
- " this._writable = new WritableLite({\n"
511
- " /* Eager transform: writes run the transformer immediately (no\n"
512
- " * readable-side backpressure) — order-exact, documented. */\n"
513
- " write(chunk) {\n"
514
- " let r;\n"
515
- " try { r = t.transform ? t.transform(chunk, tc) : tc.enqueue(chunk); } catch (e) { tc.error(e); throw e; }\n"
516
- " return Promise.resolve(r).catch((e) => { tc.error(e); throw e; });\n"
517
- " },\n"
518
- " close() {\n"
519
- " let r;\n"
520
- " try { r = t.flush ? t.flush(tc) : undefined; } catch (e) { tc.error(e); throw e; }\n"
521
- " return Promise.resolve(r).then(\n"
522
- " () => {\n"
523
- " if (self._readable._state === 'readable' && !self._readable._closeRequested) rc.close();\n"
524
- " },\n"
525
- " (e) => { tc.error(e); throw e; },\n"
526
- " );\n"
945
+ " const readableType = transformer.readableType, writableType = transformer.writableType, start = transformer.start;\n"
946
+ " if (readableType !== undefined) throw streamInvalidValue('transformer.readableType', readableType);\n"
947
+ " if (writableType !== undefined) throw streamInvalidValue('transformer.writableType', writableType);\n"
948
+ " const readableSizing = streamStrategy(rs, 0), writableSizing = streamStrategy(ws, 1);\n"
949
+ " const s = { transformer, finish: undefined, backpressure: true };\n"
950
+ " transformPressure(s, true); transformStreams.set(this, s);\n"
951
+ " let resolveStart;\n"
952
+ " const startGate = new Promise((resolve) => { resolveStart = resolve; });\n"
953
+ " s.writable = new WritableStream({\n"
954
+ " start() { return startGate; },\n"
955
+ " async write(chunk) {\n"
956
+ " if (s.backpressure) {\n"
957
+ " await s.change.promise;\n"
958
+ " const w = writableStreams.get(s.writable);\n"
959
+ " if (w.state === 'erroring' || w.state === 'errored') throw w.error;\n"
960
+ " }\n"
961
+ " try { if (s.transform) await s.transform.call(transformer, chunk, s.controller); }\n"
962
+ " catch (e) { transformError(s, e); throw e; }\n"
527
963
  " },\n"
528
- " abort(e) { self._readable._errorStream(e); },\n"
529
- " });\n"
530
- " if (t.start) t.start(tc); // sync start, like eventsource-parser needs\n"
964
+ " close() { return transformFinish(s, 'close'); },\n"
965
+ " abort(reason) { return transformFinish(s, 'abort', reason); },\n"
966
+ " }, writableSizing);\n"
967
+ " s.readable = new ReadableStream({\n"
968
+ " start() { return startGate; },\n"
969
+ " pull() { transformPressure(s, false); return s.change.promise; },\n"
970
+ " cancel(reason) { return transformFinish(s, 'cancel', reason); },\n"
971
+ " }, readableSizing);\n"
972
+ " s.controller = new TransformStreamDefaultController(transformToken, s);\n"
973
+ " const transform = transformer.transform, flush = transformer.flush, cancel = transformer.cancel;\n"
974
+ " for (const [name, fn] of [['transform', transform], ['flush', flush], ['cancel', cancel]]) {\n"
975
+ " if (fn && typeof fn !== 'function') throw streamArgument('transformer.' + name, 'of type function', fn);\n"
976
+ " }\n"
977
+ " s.transform = transform || ((chunk, controller) => controller.enqueue(chunk)); s.flush = flush; s.cancel = cancel;\n"
978
+ " const startResult = start === undefined ? undefined : start.call(transformer, s.controller);\n"
979
+ " resolveStart(Promise.resolve(startResult).catch((e) => { transformError(s, e); throw e; }));\n"
531
980
  " }\n"
532
- " get readable() { return this._readable; }\n"
533
- " get writable() { return this._writable; }\n"
981
+ " get readable() { const s = transformStreams.get(this); if (!s) throw streamInvalidThis('TransformStream'); return s.readable; }\n"
982
+ " get writable() { const s = transformStreams.get(this); if (!s) throw streamInvalidThis('TransformStream'); return s.writable; }\n"
983
+ " }\n"
984
+ " for (const C of [TransformStream, TransformStreamDefaultController]) {\n"
985
+ " Object.defineProperty(C.prototype, Symbol.toStringTag, { value: C.name, configurable: true });\n"
986
+ " for (const name of Object.getOwnPropertyNames(C.prototype)) if (name !== 'constructor') Object.defineProperty(C.prototype, name, { enumerable: true });\n"
987
+ " g[C.name] = C;\n"
534
988
  " }\n"
535
- "\n"
536
989
  " g.ReadableStream = ReadableStream;\n"
537
- " g.TransformStream = TransformStream;\n"
538
- "\n"
539
990
  "\n"
540
991
  " const textEncoders = new WeakSet();\n"
541
992
  " class TextEncoder {\n"
@@ -675,6 +1126,37 @@ static const char web_prelude[] =
675
1126
  " }\n"
676
1127
  " }\n"
677
1128
  "\n"
1129
+ " class TextEncoderStream {\n"
1130
+ " #transform;\n"
1131
+ " constructor() {\n"
1132
+ " const encoder = new TextEncoder();\n"
1133
+ " let pending = null;\n"
1134
+ " this.#transform = new TransformStream({\n"
1135
+ " transform(chunk, controller) {\n"
1136
+ " chunk = String(chunk);\n"
1137
+ " let text = '';\n"
1138
+ " for (let i = 0; i < chunk.length; i++) {\n"
1139
+ " const unit = chunk.charCodeAt(i), item = chunk[i];\n"
1140
+ " if (pending !== null) {\n"
1141
+ " const high = pending; pending = null;\n"
1142
+ " if (unit >= 0xdc00 && unit <= 0xdfff) { text += high + item; continue; }\n"
1143
+ " text += '\\ufffd';\n"
1144
+ " }\n"
1145
+ " if (unit >= 0xd800 && unit <= 0xdbff) pending = item;\n"
1146
+ " else text += unit >= 0xdc00 && unit <= 0xdfff ? '\\ufffd' : item;\n"
1147
+ " }\n"
1148
+ " if (text) controller.enqueue(encoder.encode(text));\n"
1149
+ " },\n"
1150
+ " flush(controller) { if (pending !== null) controller.enqueue(new Uint8Array([0xef, 0xbf, 0xbd])); },\n"
1151
+ " });\n"
1152
+ " }\n"
1153
+ " get encoding() { this.#transform; return 'utf-8'; }\n"
1154
+ " get readable() { return this.#transform.readable; }\n"
1155
+ " get writable() { return this.#transform.writable; }\n"
1156
+ " }\n"
1157
+ " for (const name of ['encoding', 'readable', 'writable']) Object.defineProperty(TextEncoderStream.prototype, name, { enumerable: true });\n"
1158
+ " g.TextEncoderStream = TextEncoderStream;\n"
1159
+ "\n"
678
1160
  " class TextDecoderStream extends g.TransformStream {\n"
679
1161
  " constructor(label, options) {\n"
680
1162
  " const dec = new TextDecoder(label, options);\n"