quickwin 3.0.0 → 3.0.2

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 (253) hide show
  1. package/README.en.md +3 -0
  2. package/README.md +2 -0
  3. package/lib/abort.d.ts +21 -0
  4. package/lib/assert.d.ts +2 -0
  5. package/lib/bitmap.d.ts +7 -0
  6. package/lib/cache-utils.d.ts +6 -0
  7. package/lib/certs.d.ts +1 -0
  8. package/lib/dpi.d.ts +9 -0
  9. package/lib/dumpRects.d.ts +2 -0
  10. package/lib/exec-log.d.ts +23 -0
  11. package/lib/fetch.d.ts +4 -0
  12. package/lib/ffi/bind.d.ts +70 -0
  13. package/lib/ffi/ctype.d.ts +117 -0
  14. package/lib/ffi/index.d.ts +2 -0
  15. package/lib/ffi/struct.d.ts +174 -0
  16. package/lib/http-common.d.ts +143 -0
  17. package/lib/http-server.d.ts +59 -0
  18. package/lib/polyfill.d.ts +22 -0
  19. package/lib/react-qw/components/Button.d.ts +9 -0
  20. package/lib/react-qw/components/CheckBox.d.ts +11 -0
  21. package/lib/react-qw/components/ComboBox.d.ts +11 -0
  22. package/lib/react-qw/components/DateTimePicker.d.ts +15 -0
  23. package/lib/react-qw/components/Input.d.ts +15 -0
  24. package/lib/react-qw/components/Link.d.ts +12 -0
  25. package/lib/react-qw/components/ListBox.d.ts +13 -0
  26. package/lib/react-qw/components/ListView.d.ts +37 -0
  27. package/lib/react-qw/components/PathPicker.d.ts +23 -0
  28. package/lib/react-qw/components/ProgressBar.d.ts +9 -0
  29. package/lib/react-qw/components/RadioButton.d.ts +11 -0
  30. package/lib/react-qw/components/ScrollView.d.ts +9 -0
  31. package/lib/react-qw/components/Slider.d.ts +12 -0
  32. package/lib/react-qw/components/Tab.d.ts +14 -0
  33. package/lib/react-qw/components/Tooltip.d.ts +7 -0
  34. package/lib/react-qw/components/TreeView.d.ts +18 -0
  35. package/lib/react-qw/components/index.d.ts +16 -0
  36. package/lib/react-qw/index.d.ts +24 -0
  37. package/lib/react-qw/layout.d.ts +29 -0
  38. package/lib/react-qw/nmhdr.d.ts +1 -0
  39. package/lib/react-qw/props.d.ts +2 -0
  40. package/lib/react-qw/reconciler.d.ts +21 -0
  41. package/lib/stream.d.ts +20 -0
  42. package/lib/text-codec.d.ts +39 -0
  43. package/lib/text-measure.d.ts +14 -0
  44. package/lib/url.d.ts +54 -0
  45. package/lib/websocket.d.ts +73 -0
  46. package/lib/windows/{advapi32.ts → advapi32.d.ts} +6 -21
  47. package/lib/windows/comctl32.d.ts +14 -0
  48. package/lib/windows/comdlg32.d.ts +4 -0
  49. package/lib/windows/{gdi32.ts → gdi32.d.ts} +161 -98
  50. package/lib/windows/{kernel32.ts → kernel32.d.ts} +138 -115
  51. package/lib/windows/{msimg32.ts → msimg32.d.ts} +11 -14
  52. package/lib/windows/ntdll.d.ts +5 -0
  53. package/lib/windows/ole32.d.ts +3 -0
  54. package/lib/windows/{psapi.ts → psapi.d.ts} +9 -18
  55. package/lib/windows/{shell32.ts → shell32.d.ts} +25 -24
  56. package/lib/windows/structs.d.ts +3141 -0
  57. package/lib/windows/{user32.ts → user32.d.ts} +196 -193
  58. package/lib/windows/version.d.ts +27 -0
  59. package/lib/windows/{winspool.ts → winspool.d.ts} +18 -32
  60. package/package.json +15 -8
  61. package/qwin-nowasm-x86.exe +0 -0
  62. package/qwin-nowasm.exe +0 -0
  63. package/qwin-x86.exe +0 -0
  64. package/qwin.exe +0 -0
  65. package/examples/PdfCanvas.js +0 -38
  66. package/examples/PdfCanvas.tsx +0 -54
  67. package/examples/execd/main.js +0 -23379
  68. package/examples/execd/tray.js +0 -192
  69. package/examples/execd/worker.js +0 -122
  70. package/examples/pdf_preview2.js +0 -398
  71. package/examples/pdf_preview2.ts +0 -353
  72. package/examples/pdf_viewer.js +0 -22515
  73. package/examples/pdf_viewer.tsx +0 -228
  74. package/examples/test_gallery.js +0 -18692
  75. package/examples/test_gallery.tsx +0 -291
  76. package/examples/test_react_lazy.js +0 -18560
  77. package/examples/test_react_lazy.tsx +0 -65
  78. package/examples/test_react_listbox.js +0 -18562
  79. package/examples/test_react_listbox.tsx +0 -73
  80. package/examples/test_root_window.js +0 -18532
  81. package/examples/test_root_window.tsx +0 -41
  82. package/examples/test_tab.js +0 -18563
  83. package/examples/test_tab.tsx +0 -77
  84. package/examples/tray_demo.js +0 -86
  85. package/examples/tray_demo.tsx +0 -89
  86. package/lib/abort.ts +0 -69
  87. package/lib/assert.ts +0 -7
  88. package/lib/bitmap.ts +0 -39
  89. package/lib/cache-utils.ts +0 -40
  90. package/lib/certs.ts +0 -258
  91. package/lib/dpi.ts +0 -31
  92. package/lib/dumpRects.ts +0 -34
  93. package/lib/exec-log.ts +0 -30
  94. package/lib/fetch.ts +0 -589
  95. package/lib/ffi/bind.ts +0 -540
  96. package/lib/ffi/ctype.ts +0 -371
  97. package/lib/ffi/index.ts +0 -6
  98. package/lib/ffi/struct.ts +0 -780
  99. package/lib/http-common.ts +0 -441
  100. package/lib/http-server.ts +0 -604
  101. package/lib/polyfill.ts +0 -171
  102. package/lib/react-qw/components/index.ts +0 -17
  103. package/lib/react-qw/index.ts +0 -96
  104. package/lib/react-qw/layout.ts +0 -111
  105. package/lib/react-qw/nmhdr.ts +0 -7
  106. package/lib/react-qw/props.ts +0 -41
  107. package/lib/react-qw/reconciler.ts +0 -379
  108. package/lib/stream.ts +0 -22
  109. package/lib/text-codec.ts +0 -171
  110. package/lib/text-measure.ts +0 -44
  111. package/lib/url.ts +0 -201
  112. package/lib/websocket.ts +0 -600
  113. package/lib/windows/comctl32.ts +0 -20
  114. package/lib/windows/comdlg32.ts +0 -10
  115. package/lib/windows/ntdll.ts +0 -11
  116. package/lib/windows/ole32.ts +0 -9
  117. package/lib/windows/structs.ts +0 -807
  118. package/lib/windows/version.ts +0 -35
  119. package/test/add.wasm +0 -0
  120. package/test/bench_ffi_callraw.js +0 -235
  121. package/test/bench_ffi_callraw.ts +0 -235
  122. package/test/bench_ffi_overhead.js +0 -139
  123. package/test/bench_ffi_overhead.ts +0 -141
  124. package/test/bench_sendmessage.js +0 -104
  125. package/test/bench_sendmessage.ts +0 -100
  126. package/test/bench_sendmessage_nonnull.js +0 -187
  127. package/test/bench_sendmessage_nonnull.ts +0 -188
  128. package/test/complex.wasm +0 -0
  129. package/test/complex_imports.wasm +0 -0
  130. package/test/debug/http_server_e2e.js +0 -31
  131. package/test/debug/minwasm.js +0 -52
  132. package/test/debug/mupdf_debug.js +0 -86
  133. package/test/debug/wasm.js +0 -122
  134. package/test/debug/wasm_export_debug.js +0 -54
  135. package/test/debug/wasm_names.js +0 -36
  136. package/test/example.pdf +0 -0
  137. package/test/global_imports.wasm +0 -0
  138. package/test/import_func.wasm +0 -0
  139. package/test/imports.wasm +0 -0
  140. package/test/mupdf-cdn.js +0 -40
  141. package/test/mupdf-cdn.ts +0 -43
  142. package/test/run.js +0 -111
  143. package/test/run.ts +0 -116
  144. package/test/sjlj.wasm +0 -0
  145. package/test/test_basic.js +0 -7
  146. package/test/test_basic.ts +0 -9
  147. package/test/test_basic_fetch.js +0 -63
  148. package/test/test_basic_fetch.ts +0 -59
  149. package/test/test_brotli.js +0 -48
  150. package/test/test_brotli.ts +0 -52
  151. package/test/test_fetch_cache.js +0 -169
  152. package/test/test_fetch_cache.ts +0 -187
  153. package/test/test_ffi.js +0 -157
  154. package/test/test_ffi.ts +0 -170
  155. package/test/test_ffi_abi.js +0 -290
  156. package/test/test_ffi_abi.ts +0 -274
  157. package/test/test_ffi_array.js +0 -116
  158. package/test/test_ffi_array.ts +0 -141
  159. package/test/test_ffi_bind.js +0 -656
  160. package/test/test_ffi_bind.ts +0 -670
  161. package/test/test_ffi_struct.js +0 -779
  162. package/test/test_ffi_struct.ts +0 -828
  163. package/test/test_ffi_struct_layout.js +0 -495
  164. package/test/test_ffi_struct_layout.ts +0 -543
  165. package/test/test_ffi_user32.js +0 -103
  166. package/test/test_ffi_user32.ts +0 -113
  167. package/test/test_ffi_windows.js +0 -172
  168. package/test/test_ffi_windows.ts +0 -187
  169. package/test/test_files.js +0 -102
  170. package/test/test_files.ts +0 -121
  171. package/test/test_frame_encoding.js +0 -128
  172. package/test/test_frame_encoding.ts +0 -132
  173. package/test/test_gdi_draw.js +0 -681
  174. package/test/test_gdi_draw.ts +0 -725
  175. package/test/test_helper.js +0 -91
  176. package/test/test_helper.ts +0 -88
  177. package/test/test_http_import.js +0 -96
  178. package/test/test_http_import.ts +0 -93
  179. package/test/test_http_server.js +0 -214
  180. package/test/test_http_server.ts +0 -211
  181. package/test/test_import_dir.js +0 -4
  182. package/test/test_import_dir.ts +0 -5
  183. package/test/test_ipv6.js +0 -134
  184. package/test/test_ipv6.ts +0 -130
  185. package/test/test_jsx.js +0 -1314
  186. package/test/test_meta_url.js +0 -3
  187. package/test/test_meta_url.ts +0 -4
  188. package/test/test_mupdf_render.js +0 -51
  189. package/test/test_mupdf_render.ts +0 -55
  190. package/test/test_mupdf_twice.js +0 -62
  191. package/test/test_mupdf_twice.ts +0 -65
  192. package/test/test_mupdf_wasm.js +0 -39
  193. package/test/test_mupdf_wasm.ts +0 -32
  194. package/test/test_net_event.js +0 -57
  195. package/test/test_net_event.ts +0 -56
  196. package/test/test_net_fetch.js +0 -384
  197. package/test/test_net_fetch.ts +0 -373
  198. package/test/test_net_websocket.js +0 -180
  199. package/test/test_net_websocket.ts +0 -164
  200. package/test/test_polyfill.js +0 -144
  201. package/test/test_polyfill.ts +0 -159
  202. package/test/test_printer.js +0 -137
  203. package/test/test_printer.ts +0 -148
  204. package/test/test_proc.js +0 -141
  205. package/test/test_proc.ts +0 -163
  206. package/test/test_react_complex.js +0 -18678
  207. package/test/test_react_counter.js +0 -18555
  208. package/test/test_react_flex.js +0 -18871
  209. package/test/test_react_hidden.js +0 -18560
  210. package/test/test_react_listview_link.js +0 -18590
  211. package/test/test_react_notify.js +0 -18797
  212. package/test/test_react_render.js +0 -18550
  213. package/test/test_registry.js +0 -74
  214. package/test/test_registry.ts +0 -95
  215. package/test/test_sysinfo.js +0 -122
  216. package/test/test_sysinfo.ts +0 -141
  217. package/test/test_url.js +0 -173
  218. package/test/test_url.ts +0 -183
  219. package/test/test_wasm_basic.js +0 -85
  220. package/test/test_wasm_basic.ts +0 -73
  221. package/test/test_wasm_import_global.js +0 -41
  222. package/test/test_wasm_import_global.ts +0 -45
  223. package/test/test_wasm_sjlj.js +0 -145
  224. package/test/test_wasm_sjlj.ts +0 -125
  225. package/test/test_wasm_types.js +0 -97
  226. package/test/test_wasm_types.ts +0 -124
  227. package/test/test_wolfssl_read.js +0 -203
  228. package/test/test_wolfssl_read.ts +0 -184
  229. package/test/test_worker.js +0 -21
  230. package/test/test_worker.ts +0 -24
  231. package/test/test_worker_concurrent.js +0 -45
  232. package/test/test_worker_concurrent.ts +0 -56
  233. package/test/test_worker_data_url.js +0 -46
  234. package/test/test_worker_data_url.ts +0 -51
  235. package/test/test_worker_fetch.js +0 -26
  236. package/test/test_worker_fetch.ts +0 -27
  237. package/test/test_worker_net.js +0 -69
  238. package/test/test_worker_net.ts +0 -66
  239. package/test/test_worker_wasm_concurrent.js +0 -44
  240. package/test/test_worker_wasm_concurrent.ts +0 -45
  241. package/test/types.wasm +0 -0
  242. package/test/worker_fetch.js +0 -13
  243. package/test/worker_fetch.ts +0 -13
  244. package/test/worker_helper.js +0 -1
  245. package/test/worker_helper.ts +0 -1
  246. package/test/worker_http.js +0 -18
  247. package/test/worker_http.ts +0 -20
  248. package/test/worker_import_lib.js +0 -12
  249. package/test/worker_import_lib.ts +0 -13
  250. package/test/worker_module.js +0 -16
  251. package/test/worker_module.ts +0 -17
  252. package/test/worker_wasm_concurrent.js +0 -22
  253. package/test/worker_wasm_concurrent.ts +0 -22
@@ -1,441 +0,0 @@
1
- import './polyfill.js'
2
- import './stream.js'
3
-
4
- // ── Byte helpers ──
5
-
6
- export function _concat(parts: Uint8Array[]): Uint8Array<ArrayBuffer> {
7
- if (parts.length === 0) return new Uint8Array(0)
8
- let totalLen = 0
9
- for (const p of parts) totalLen += p.length
10
- const result = new Uint8Array(totalLen)
11
- let offset = 0
12
- for (const p of parts) { result.set(p, offset); offset += p.length }
13
- return result
14
- }
15
-
16
- export function _toReadableStream(body: BodyInit): ReadableStream<Uint8Array> {
17
- if (body instanceof ReadableStream) return body
18
- const bytes = (typeof body === 'string')
19
- ? new TextEncoder().encode(body)
20
- : (body instanceof ArrayBuffer)
21
- ? new Uint8Array(body)
22
- : new Uint8Array(body.buffer, body.byteOffset, body.byteLength)
23
- return new ReadableStream<Uint8Array>({
24
- start(ctrl) {
25
- ctrl.enqueue(bytes)
26
- ctrl.close()
27
- }
28
- })
29
- }
30
-
31
- export async function _readStream(stream: ReadableStream<Uint8Array>): Promise<Uint8Array<ArrayBuffer>> {
32
- const reader = stream.getReader()
33
- const chunks: Uint8Array[] = []
34
- while (true) {
35
- const { done, value } = await reader.read()
36
- if (done) break
37
- chunks.push(value)
38
- }
39
- return _concat(chunks)
40
- }
41
-
42
- export function _hasChunkedEnd(data: Uint8Array): boolean {
43
- if (data.length < 7) return false
44
- let i = data.length - 7
45
- return data[i] === 0x0D && data[i+1] === 0x0A && data[i+2] === 0x30 && data[i+3] === 0x0D && data[i+4] === 0x0A && data[i+5] === 0x0D && data[i+6] === 0x0A
46
- }
47
-
48
- export function decodeChunked(data: Uint8Array): Uint8Array {
49
- const chunks: Uint8Array[] = []
50
- let pos = 0
51
- while (pos < data.length) {
52
- let crlf = -1
53
- for (let i = pos; i < data.length - 1; i++) {
54
- if (data[i] === 0x0D && data[i + 1] === 0x0A) { crlf = i; break }
55
- }
56
- if (crlf < 0) break
57
-
58
- let size = 0
59
- for (let i = pos; i < crlf; i++) {
60
- const b = data[i]!
61
- if (b >= 0x30 && b <= 0x39) size = size * 16 + (b - 0x30)
62
- else if (b >= 0x41 && b <= 0x46) size = size * 16 + (b - 0x37)
63
- else if (b >= 0x61 && b <= 0x66) size = size * 16 + (b - 0x57)
64
- else break
65
- }
66
-
67
- const dataStart = crlf + 2
68
- if (size === 0) {
69
- for (let i = dataStart; i < data.length - 3; i++) {
70
- if (data[i] === 0x0D && data[i+1] === 0x0A && data[i+2] === 0x0D && data[i+3] === 0x0A) {
71
- pos = i + 4; break
72
- }
73
- }
74
- if (pos === dataStart) break
75
- break
76
- }
77
-
78
- if (dataStart + size > data.length) break
79
- chunks.push(data.subarray(dataStart, dataStart + size))
80
- pos = dataStart + size + 2
81
- }
82
- return _concat(chunks)
83
- }
84
-
85
- // ── Headers ──
86
-
87
- // Flat array storage: [name0, value0, name1, value1, ...]
88
- // Pairs at even/odd indices. Strings are immutable, so [...this._headers] is a correct deep copy.
89
-
90
- export class HeadersImpl {
91
- private _headers: string[] = []
92
-
93
- constructor(init?: HeadersInit) {
94
- if (init) {
95
- if (init instanceof HeadersImpl) {
96
- this._headers = [...init._headers]
97
- } else if (Array.isArray(init)) {
98
- this._headers = init.flatMap(([k, v]) => [k.toLowerCase(), v])
99
- } else if (typeof init === 'object') {
100
- this._headers = Object.entries(init).flatMap(([k, v]) => [k.toLowerCase(), v])
101
- }
102
- }
103
- }
104
-
105
- append(name: string, value: string): void {
106
- this._headers.push(name.toLowerCase(), value)
107
- }
108
-
109
- delete(name: string): void {
110
- const key = name.toLowerCase()
111
- const h = this._headers
112
- let i = h.length - 2
113
- while (i >= 0) {
114
- if (h[i] === key) { h.splice(i, 2); i -= 2 } else i -= 2
115
- }
116
- }
117
-
118
- get(name: string): string | null {
119
- const key = name.toLowerCase()
120
- const matches = [...this.entries()].filter(([k]) => k === key).map(([, v]) => v)
121
- return matches.length ? matches.join(', ') : null
122
- }
123
-
124
- has(name: string): boolean {
125
- return [...this.keys()].includes(name.toLowerCase())
126
- }
127
-
128
- set(name: string, value: string): void {
129
- const key = name.toLowerCase()
130
- const h = this._headers
131
- let found = false
132
- let i = h.length - 2
133
- while (i >= 0) {
134
- if (h[i] === key) {
135
- if (!found) {
136
- h[i + 1] = value
137
- found = true
138
- } else {
139
- h.splice(i, 2)
140
- }
141
- }
142
- i -= 2
143
- }
144
- if (!found) h.push(key, value)
145
- }
146
-
147
- forEach(callback: (value: string, name: string, headers: HeadersImpl) => void): void {
148
- for (const [name, value] of this) {
149
- callback(value, name, this)
150
- }
151
- }
152
-
153
- *entries(): IterableIterator<[string, string]> {
154
- const h = this._headers
155
- for (let i = 0; i < h.length; i += 2) {
156
- yield [h[i]!, h[i + 1]!]
157
- }
158
- }
159
-
160
- *keys(): IterableIterator<string> {
161
- for (const [k] of this) yield k
162
- }
163
-
164
- *values(): IterableIterator<string> {
165
- for (const [, v] of this) yield v
166
- }
167
-
168
- [Symbol.iterator](): IterableIterator<[string, string]> {
169
- return this.entries()
170
- }
171
- }
172
-
173
- // ── Request ──
174
-
175
- export class RequestImpl {
176
- readonly url: string
177
- readonly method: string
178
- readonly headers: HeadersImpl
179
- private _body: ReadableStream<Uint8Array> | null
180
- readonly redirect: RequestRedirect
181
- readonly timeout: number
182
- readonly maxRedirects: number
183
- private _bodyConsumed: boolean = false
184
- readonly integrity: string = ''
185
- readonly keepalive: boolean = false
186
- readonly mode: string = 'cors'
187
- readonly cache: string = 'default'
188
- readonly credentials: string = 'same-origin'
189
- readonly referrer: string = 'about:client'
190
- readonly referrerPolicy: string = ''
191
- readonly signal: AbortSignal | null
192
- readonly rejectUnauthorized: boolean
193
- readonly destination: string = ''
194
-
195
- get body(): ReadableStream<Uint8Array> | null { return this._body }
196
-
197
- get bodyUsed(): boolean {
198
- return this._bodyConsumed || (this._body?.locked ?? false)
199
- }
200
-
201
- constructor(input: string | RequestImpl, init: RequestInit = {}) {
202
- const initBody = init.body
203
- if (input instanceof RequestImpl) {
204
- this.url = input.url
205
- this.method = init.method || input.method
206
- this.headers = new HeadersImpl(init.headers || input.headers)
207
- if (initBody !== undefined && initBody !== null) {
208
- this._body = _toReadableStream(initBody)
209
- } else {
210
- this._body = input._body
211
- }
212
- this.redirect = init.redirect || input.redirect
213
- this.timeout = init.timeout || input.timeout
214
- this.maxRedirects = init.maxRedirects || input.maxRedirects
215
- this.rejectUnauthorized = init.rejectUnauthorized === undefined
216
- ? input.rejectUnauthorized
217
- : init.rejectUnauthorized
218
- } else {
219
- this.url = input
220
- this.method = init.method || 'GET'
221
- this.headers = new HeadersImpl(init.headers)
222
- this._body = initBody !== null && initBody !== undefined ? _toReadableStream(initBody) : null
223
- this.redirect = init.redirect || 'follow'
224
- this.timeout = init.timeout || 30000
225
- this.maxRedirects = init.maxRedirects || 5
226
- this.rejectUnauthorized = init.rejectUnauthorized ?? true
227
- }
228
- this.signal = init.signal !== undefined
229
- ? init.signal
230
- : (input instanceof RequestImpl ? input.signal : null)
231
- }
232
-
233
- async arrayBuffer(): Promise<ArrayBuffer> {
234
- if (!this.body) return new ArrayBuffer(0)
235
- if (this._bodyConsumed) throw new TypeError('Body already used')
236
- this._bodyConsumed = true
237
- const bytes = await _readStream(this.body)
238
- return bytes.buffer
239
- }
240
-
241
- async json<T = unknown>(): Promise<T> {
242
- return JSON.parse(await this.text())
243
- }
244
-
245
- async text(): Promise<string> {
246
- if (!this.body) return ''
247
- if (this._bodyConsumed) throw new TypeError('Body already used')
248
- this._bodyConsumed = true
249
- const bytes = await _readStream(this.body)
250
- return new TextDecoder('utf-8').decode(bytes)
251
- }
252
-
253
- clone(): RequestImpl {
254
- if (this.bodyUsed) throw new TypeError('Body already used')
255
- if (this._body) {
256
- const [branch1, branch2] = this._body.tee()
257
- this._body = branch1
258
- const req = new RequestImpl(this.url, {
259
- method: this.method,
260
- headers: this.headers,
261
- body: branch2,
262
- redirect: this.redirect,
263
- timeout: this.timeout,
264
- maxRedirects: this.maxRedirects,
265
- signal: this.signal,
266
- })
267
- return req
268
- }
269
- return new RequestImpl(this.url, {
270
- method: this.method,
271
- headers: this.headers,
272
- redirect: this.redirect,
273
- timeout: this.timeout,
274
- maxRedirects: this.maxRedirects,
275
- signal: this.signal,
276
- })
277
- }
278
- }
279
-
280
- // ── Response ──
281
-
282
- export class ResponseImpl {
283
- readonly status: number
284
- readonly statusText: string
285
- readonly ok: boolean
286
-
287
- private _headers: HeadersImpl
288
- _redirected: boolean = false
289
- private _type: ResponseType = 'basic'
290
- _url: string = ''
291
- private _body: ReadableStream<Uint8Array>
292
- private _bodyConsumed: boolean = false
293
- private _trailers: Promise<HeadersInit> | undefined
294
-
295
- get body(): ReadableStream<Uint8Array> { return this._body }
296
- get bodyUsed(): boolean { return this._bodyConsumed || this._body.locked }
297
- /** Trailing headers to emit after a chunked body (Promise resolves to
298
- * a HeadersInit), when a `Trailer` declaration header is present. */
299
- get trailers(): Promise<HeadersInit> | undefined { return this._trailers }
300
- get headers(): HeadersImpl { return this._headers }
301
- get redirected(): boolean { return this._redirected }
302
- get type(): ResponseType { return this._type }
303
- get url(): string { return this._url }
304
-
305
- constructor(body?: BodyInit | null, init?: ResponseInit) {
306
- const status = init?.status ?? 200
307
- const statusText = init?.statusText ?? ''
308
- const headers = new HeadersImpl(init?.headers)
309
- this.status = status
310
- this.statusText = statusText
311
- this._headers = headers
312
- this._trailers = init?.trailers
313
- this.ok = status >= 200 && status < 300
314
- if (body instanceof ReadableStream) {
315
- this._body = body
316
- } else {
317
- let bytes: Uint8Array
318
- if (body == null) {
319
- bytes = new Uint8Array(0)
320
- } else if (typeof body === 'string') {
321
- bytes = new TextEncoder().encode(body)
322
- } else if (body instanceof ArrayBuffer) {
323
- bytes = new Uint8Array(body)
324
- } else {
325
- bytes = new Uint8Array(body.buffer, body.byteOffset, body.byteLength)
326
- }
327
- if (!headers.has('content-length')) headers.set('Content-Length', String(bytes.byteLength))
328
- this._body = new ReadableStream<Uint8Array>({
329
- start(ctrl) {
330
- ctrl.enqueue(bytes)
331
- ctrl.close()
332
- }
333
- })
334
- }
335
- }
336
-
337
- async text(): Promise<string> {
338
- if (this.bodyUsed) throw new TypeError('Body already used')
339
- this._bodyConsumed = true
340
- const bytes = await _readStream(this._body)
341
- return new TextDecoder('utf-8').decode(bytes)
342
- }
343
-
344
- async json<T = unknown>(): Promise<T> {
345
- return JSON.parse(await this.text())
346
- }
347
-
348
- async arrayBuffer(): Promise<ArrayBuffer> {
349
- if (this.bodyUsed) throw new TypeError('Body already used')
350
- this._bodyConsumed = true
351
- const bytes = await _readStream(this._body)
352
- return bytes.buffer
353
- }
354
-
355
- async bytes(): Promise<Uint8Array> {
356
- if (this.bodyUsed) throw new TypeError('Body already used')
357
- this._bodyConsumed = true
358
- return await _readStream(this._body)
359
- }
360
-
361
- clone(): Response {
362
- if (this.bodyUsed) throw new TypeError('Body already used')
363
- const [branch1, branch2] = this._body.tee()
364
- this._body = branch1
365
- return new ResponseImpl(branch2, { status: this.status, statusText: this.statusText, headers: this._headers, trailers: this._trailers })
366
- }
367
-
368
- _applyDecompressedBody(stream: ReadableStream<Uint8Array>, _body: ArrayBuffer, newHeaders: HeadersImpl): void {
369
- this._bodyConsumed = false
370
- this._body = stream
371
- this._headers = newHeaders
372
- }
373
- }
374
-
375
- // ── Global declarations ──
376
-
377
- declare global {
378
- type HeadersInit = [string, string][] | Record<string, string> | Headers
379
- type BodyInit = ReadableStream<Uint8Array> | string | ArrayBuffer | ArrayBufferView
380
- type RequestRedirect = 'error' | 'follow' | 'manual'
381
- type RequestCache = 'default' | 'force-cache' | 'no-cache' | 'no-store' | 'only-if-cached' | 'reload'
382
- type RequestCredentials = 'include' | 'omit' | 'same-origin'
383
- type RequestMode = 'cors' | 'navigate' | 'no-cors' | 'same-origin'
384
- type ResponseType = 'basic' | 'cors' | 'default' | 'error' | 'opaque' | 'opaqueredirect'
385
-
386
- interface AbortSignal {
387
- readonly aborted: boolean;
388
- readonly reason: unknown;
389
- onabort: ((event: { type: 'abort'; target: unknown }) => void) | null;
390
- addEventListener(type: 'abort', listener: (e: { type: 'abort'; target: unknown }) => void): void;
391
- removeEventListener(type: 'abort', listener: (e: { type: 'abort'; target: unknown }) => void): void;
392
- throwIfAborted(): void;
393
- }
394
-
395
- interface RequestInit {
396
- body?: BodyInit | null;
397
- cache?: RequestCache;
398
- credentials?: RequestCredentials;
399
- headers?: HeadersInit;
400
- integrity?: string;
401
- keepalive?: boolean;
402
- method?: string;
403
- mode?: RequestMode;
404
- redirect?: RequestRedirect;
405
- referrer?: string;
406
- referrerPolicy?: string;
407
- signal?: AbortSignal | null;
408
- window?: null;
409
- timeout?: number;
410
- maxRedirects?: number;
411
- /** Non-standard: when false, TLS certificate verification is skipped
412
- * (mirrors Node's https.request rejectUnauthorized option). */
413
- rejectUnauthorized?: boolean;
414
- }
415
-
416
- interface ResponseInit {
417
- status?: number;
418
- statusText?: string;
419
- headers?: HeadersInit;
420
- /** Non-standard: trailing headers, resolved after the body streams.
421
- * http-server emits them (Node addTrailers semantics) when a `Trailer`
422
- * declaration header is present and the body is chunk-framed. */
423
- trailers?: Promise<HeadersInit>;
424
- }
425
-
426
- interface Headers extends HeadersImpl {}
427
- var Headers: typeof HeadersImpl
428
- interface Response extends Omit<ResponseImpl, '_url' | '_redirected' | '_applyDecompressedBody'> {}
429
- interface ResponseConstructor {
430
- new(body?: BodyInit | null, init?: ResponseInit): Response
431
- }
432
- var Response: ResponseConstructor
433
- interface Request extends RequestImpl {}
434
- var Request: typeof RequestImpl
435
- }
436
-
437
- // ── Register globals ──
438
-
439
- globalThis.Headers = HeadersImpl
440
- globalThis.Request = RequestImpl
441
- globalThis.Response = ResponseImpl