quickwin 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (175) hide show
  1. package/README.en.md +197 -0
  2. package/README.md +196 -0
  3. package/examples/PdfCanvas.js +67 -0
  4. package/examples/PdfCanvas.tsx +83 -0
  5. package/examples/pdf_preview2.js +431 -0
  6. package/examples/pdf_preview2.ts +375 -0
  7. package/examples/pdf_viewer.js +17623 -0
  8. package/examples/pdf_viewer.tsx +279 -0
  9. package/examples/test_gallery.js +17452 -0
  10. package/examples/test_gallery.tsx +285 -0
  11. package/examples/test_react_lazy.js +17336 -0
  12. package/examples/test_react_lazy.tsx +63 -0
  13. package/examples/test_react_listbox.js +17339 -0
  14. package/examples/test_react_listbox.tsx +71 -0
  15. package/examples/test_root_window.js +17313 -0
  16. package/examples/test_root_window.tsx +41 -0
  17. package/examples/test_tab.js +17339 -0
  18. package/examples/test_tab.tsx +75 -0
  19. package/examples/tray_demo.js +79 -0
  20. package/examples/tray_demo.tsx +81 -0
  21. package/lib/assert.js +8 -0
  22. package/lib/assert.ts +7 -0
  23. package/lib/cache-utils.js +43 -0
  24. package/lib/cache-utils.ts +40 -0
  25. package/lib/dumpRects.js +25 -0
  26. package/lib/dumpRects.ts +25 -0
  27. package/lib/fetch.js +490 -0
  28. package/lib/fetch.ts +502 -0
  29. package/lib/http-common.js +342 -0
  30. package/lib/http-common.ts +399 -0
  31. package/lib/http-server.js +416 -0
  32. package/lib/http-server.ts +424 -0
  33. package/lib/polyfill.js +156 -0
  34. package/lib/polyfill.ts +171 -0
  35. package/lib/react-qw/components/Button.js +8 -0
  36. package/lib/react-qw/components/CheckBox.js +31 -0
  37. package/lib/react-qw/components/ComboBox.js +42 -0
  38. package/lib/react-qw/components/DateTimePicker.js +96 -0
  39. package/lib/react-qw/components/Input.js +50 -0
  40. package/lib/react-qw/components/Link.js +43 -0
  41. package/lib/react-qw/components/ListBox.js +48 -0
  42. package/lib/react-qw/components/ListView.js +328 -0
  43. package/lib/react-qw/components/PathPicker.js +136 -0
  44. package/lib/react-qw/components/ProgressBar.js +22 -0
  45. package/lib/react-qw/components/RadioButton.js +31 -0
  46. package/lib/react-qw/components/ScrollView.js +139 -0
  47. package/lib/react-qw/components/Slider.js +45 -0
  48. package/lib/react-qw/components/Tab.js +61 -0
  49. package/lib/react-qw/components/Tooltip.js +55 -0
  50. package/lib/react-qw/components/TreeView.js +130 -0
  51. package/lib/react-qw/components/index.js +16 -0
  52. package/lib/react-qw/components/index.ts +17 -0
  53. package/lib/react-qw/index.js +73 -0
  54. package/lib/react-qw/index.ts +90 -0
  55. package/lib/react-qw/jsx.d.ts +34 -0
  56. package/lib/react-qw/layout.js +82 -0
  57. package/lib/react-qw/layout.ts +111 -0
  58. package/lib/react-qw/props.js +27 -0
  59. package/lib/react-qw/props.ts +36 -0
  60. package/lib/react-qw/reconciler.js +333 -0
  61. package/lib/react-qw/reconciler.ts +373 -0
  62. package/lib/stream.js +2 -0
  63. package/lib/stream.ts +22 -0
  64. package/lib/text-codec.js +197 -0
  65. package/lib/text-codec.ts +171 -0
  66. package/lib/text-measure.js +52 -0
  67. package/lib/text-measure.ts +54 -0
  68. package/lib/url.js +278 -0
  69. package/lib/url.ts +201 -0
  70. package/lib/vendor/web-streams/ponyfill.d.mts +829 -0
  71. package/lib/vendor/web-streams/ponyfill.mjs +8 -0
  72. package/lib/websocket.js +541 -0
  73. package/lib/websocket.ts +555 -0
  74. package/package.json +44 -0
  75. package/quickwin.d.ts +594 -0
  76. package/quickwin_const.d.ts +900 -0
  77. package/qwin-nowasm.exe +0 -0
  78. package/qwin.exe +0 -0
  79. package/test/add.wasm +0 -0
  80. package/test/complex.wasm +0 -0
  81. package/test/complex_imports.wasm +0 -0
  82. package/test/debug/http_server_e2e.js +31 -0
  83. package/test/debug/minwasm.js +52 -0
  84. package/test/debug/mupdf_debug.js +104 -0
  85. package/test/debug/wasm.js +122 -0
  86. package/test/debug/wasm_export_debug.js +64 -0
  87. package/test/debug/wasm_names.js +46 -0
  88. package/test/global_imports.wasm +0 -0
  89. package/test/import_func.wasm +0 -0
  90. package/test/imports.wasm +0 -0
  91. package/test/run.js +91 -0
  92. package/test/run.ts +96 -0
  93. package/test/sjlj.wasm +0 -0
  94. package/test/test_basic.js +7 -0
  95. package/test/test_basic.ts +9 -0
  96. package/test/test_basic_fetch.js +34 -0
  97. package/test/test_basic_fetch.ts +33 -0
  98. package/test/test_brotli.js +48 -0
  99. package/test/test_brotli.ts +52 -0
  100. package/test/test_fetch_cache.js +169 -0
  101. package/test/test_fetch_cache.ts +187 -0
  102. package/test/test_ffi.js +157 -0
  103. package/test/test_ffi.ts +174 -0
  104. package/test/test_frame_encoding.js +128 -0
  105. package/test/test_frame_encoding.ts +132 -0
  106. package/test/test_helper.js +84 -0
  107. package/test/test_helper.ts +80 -0
  108. package/test/test_http_import.js +78 -0
  109. package/test/test_http_import.ts +74 -0
  110. package/test/test_http_server.js +142 -0
  111. package/test/test_http_server.ts +136 -0
  112. package/test/test_import_dir.js +4 -0
  113. package/test/test_import_dir.ts +5 -0
  114. package/test/test_ipv6.js +134 -0
  115. package/test/test_ipv6.ts +129 -0
  116. package/test/test_jsx.js +1314 -0
  117. package/test/test_meta_url.js +3 -0
  118. package/test/test_meta_url.ts +4 -0
  119. package/test/test_mupdf_render.js +69 -0
  120. package/test/test_mupdf_render.ts +74 -0
  121. package/test/test_mupdf_twice.js +77 -0
  122. package/test/test_mupdf_twice.ts +81 -0
  123. package/test/test_mupdf_wasm.js +33 -0
  124. package/test/test_mupdf_wasm.ts +30 -0
  125. package/test/test_net_event.js +57 -0
  126. package/test/test_net_event.ts +55 -0
  127. package/test/test_net_fetch.js +259 -0
  128. package/test/test_net_fetch.ts +238 -0
  129. package/test/test_net_websocket.js +180 -0
  130. package/test/test_net_websocket.ts +164 -0
  131. package/test/test_polyfill.js +144 -0
  132. package/test/test_polyfill.ts +159 -0
  133. package/test/test_react_complex.js +17440 -0
  134. package/test/test_react_counter.js +17331 -0
  135. package/test/test_react_flex.js +17643 -0
  136. package/test/test_react_hidden.js +17336 -0
  137. package/test/test_react_listview_link.js +17371 -0
  138. package/test/test_react_render.js +17326 -0
  139. package/test/test_url.js +173 -0
  140. package/test/test_url.ts +183 -0
  141. package/test/test_wasm_basic.js +85 -0
  142. package/test/test_wasm_basic.ts +73 -0
  143. package/test/test_wasm_import_global.js +41 -0
  144. package/test/test_wasm_import_global.ts +45 -0
  145. package/test/test_wasm_sjlj.js +145 -0
  146. package/test/test_wasm_sjlj.ts +125 -0
  147. package/test/test_wasm_types.js +97 -0
  148. package/test/test_wasm_types.ts +124 -0
  149. package/test/test_worker.js +21 -0
  150. package/test/test_worker.ts +24 -0
  151. package/test/test_worker_fetch.js +26 -0
  152. package/test/test_worker_fetch.ts +27 -0
  153. package/test/test_worker_net.js +51 -0
  154. package/test/test_worker_net.ts +54 -0
  155. package/test/test_worker_wasm_concurrent.js +44 -0
  156. package/test/test_worker_wasm_concurrent.ts +45 -0
  157. package/test/types.wasm +0 -0
  158. package/test/worker_fetch.js +13 -0
  159. package/test/worker_fetch.ts +13 -0
  160. package/test/worker_helper.js +1 -0
  161. package/test/worker_helper.ts +1 -0
  162. package/test/worker_http.js +18 -0
  163. package/test/worker_http.ts +20 -0
  164. package/test/worker_import_lib.js +12 -0
  165. package/test/worker_import_lib.ts +13 -0
  166. package/test/worker_module.js +16 -0
  167. package/test/worker_module.ts +17 -0
  168. package/test/worker_wasm_concurrent.js +22 -0
  169. package/test/worker_wasm_concurrent.ts +22 -0
  170. package/tsconfig.json +27 -0
  171. package/vendor/mupdf-wasm/mupdf-wasm.d.ts +571 -0
  172. package/vendor/mupdf-wasm/mupdf-wasm.js +2749 -0
  173. package/vendor/mupdf-wasm/mupdf-wasm.wasm +0 -0
  174. package/vendor/mupdf-wasm/mupdf.d.ts +939 -0
  175. package/vendor/mupdf-wasm/mupdf.js +3317 -0
@@ -0,0 +1,74 @@
1
+ import * as std from 'std'
2
+ import * as os from 'os'
3
+ import { Tester } from './test_helper.js'
4
+ import { parseIni } from '../lib/cache-utils.js'
5
+
6
+ function getCacheDir(): string {
7
+ // import.meta.url: file:///C:/.../_build/test/file.js
8
+ const url = import.meta.url
9
+ // Strip file:// (7 chars), giving /C:/.../
10
+ let path = url.slice(7)
11
+ // Remove leading / before drive letter
12
+ if (path.length >= 3 && path[1] === ':') path = path.slice(1)
13
+ // path now: C:/Users/Docker/quickwin/_build/test/file.js
14
+ // Go up to _build/
15
+ const idx = path.lastIndexOf('/')
16
+ if (idx < 0) return '_cache'
17
+ const buildDir = path.slice(0, idx) // .../_build/test
18
+ const idx2 = buildDir.lastIndexOf('/')
19
+ if (idx2 < 0) return '_cache'
20
+ return buildDir.slice(0, idx2 + 1) + '_cache' // .../_build/_cache
21
+ }
22
+
23
+ export const suite = {
24
+ name: 'http-import',
25
+ run: async (t: Tester) => {
26
+ function assert(name: string, ok: boolean): void {
27
+ if (ok) { t.ok++; std.printf(' PASS: %s\n', name) }
28
+ else { t.fail++; std.printf(' FAIL: %s\n', name) }
29
+ }
30
+
31
+ const url = 'https://esm.sh/left-pad@1.3.0'
32
+ const cacheDir = getCacheDir()
33
+
34
+ t.section('list cache before')
35
+ const [before, err] = os.readdir(cacheDir)
36
+ if (err !== 0) {
37
+ std.printf(' cache dir: %s (error %d, will be created on first request)\n', cacheDir, err)
38
+ }
39
+
40
+ t.section('import module')
41
+ let mod: any
42
+ try {
43
+ mod = await import(url)
44
+ } catch (e) {
45
+ assert('import succeeds', false)
46
+ std.printf(' ERROR: %s\n', String(e))
47
+ return
48
+ }
49
+ assert('import succeeds', true)
50
+ assert('default is function', typeof mod.default === 'function')
51
+ assert('left-pad works', mod.default('hello', 8) === ' hello')
52
+
53
+ t.section('check cache')
54
+ const [after, err2] = os.readdir(cacheDir)
55
+ assert('cache dir readable', err2 === 0)
56
+ if (err2 !== 0) return
57
+ assert('cache has body files', after.some(function(f: string) { return f.endsWith('.body') }))
58
+ assert('cache has meta files', after.some(function(f: string) { return f.endsWith('.meta') }))
59
+ for (const f of after) {
60
+ if (f.endsWith('.meta') && (err !== 0 || before.indexOf(f) < 0)) {
61
+ std.printf(' meta: %s\n', f)
62
+ const fp = std.open(cacheDir + '/' + f, 'r')
63
+ if (fp) {
64
+ const metaStr = fp.readAsString()
65
+ fp.close()
66
+ if (metaStr) {
67
+ const ini = parseIni(metaStr)
68
+ std.printf(' storedAt: %s, maxAge: %s\n', ini.meta.storedAt || '?', ini.meta.maxAge || '?')
69
+ }
70
+ }
71
+ }
72
+ }
73
+ }
74
+ }
@@ -0,0 +1,142 @@
1
+ import './test_helper.js';
2
+ import * as std from 'std';
3
+ import * as os from 'os';
4
+ import * as sock from 'sock';
5
+ import { createServer } from '../lib/http-server.js';
6
+ // ── Minimal raw HTTP client for testing ──
7
+ function httpSend(host, port, raw, nResponses = 1) {
8
+ return new Promise((resolve, reject) => {
9
+ const s = sock.socket();
10
+ if (s < 0) {
11
+ reject(new Error('socket create failed'));
12
+ return;
13
+ }
14
+ let buf = '';
15
+ let done = false;
16
+ const timer = os.setTimeout(() => {
17
+ if (!done) {
18
+ done = true;
19
+ sock.closesocket(s);
20
+ reject(new Error('timeout: ' + buf));
21
+ }
22
+ }, 10000);
23
+ sock.set_on_event(s, (event) => {
24
+ if (done)
25
+ return;
26
+ if (event.lNetworkEvents & 16 /* sock.FdEvent.FD_CONNECT */) {
27
+ const err = event.iErrorCode[0];
28
+ if (err !== 0) {
29
+ done = true;
30
+ os.clearTimeout(timer);
31
+ sock.closesocket(s);
32
+ reject(new Error('connect error ' + err));
33
+ return;
34
+ }
35
+ const bytes = new TextEncoder().encode(raw);
36
+ sock.send(s, bytes.buffer);
37
+ }
38
+ if (event.lNetworkEvents & 1 /* sock.FdEvent.FD_READ */) {
39
+ while (true) {
40
+ const d = sock.recv(s, 8192);
41
+ if (typeof d === 'number')
42
+ break;
43
+ if (!d || d.byteLength === 0)
44
+ break;
45
+ buf += new TextDecoder().decode(new Uint8Array(d));
46
+ }
47
+ const count = (buf.match(/HTTP\/1\.1 /g) || []).length;
48
+ if (count >= nResponses) {
49
+ done = true;
50
+ os.clearTimeout(timer);
51
+ sock.closesocket(s);
52
+ resolve(buf);
53
+ }
54
+ }
55
+ if (event.lNetworkEvents & 32 /* sock.FdEvent.FD_CLOSE */) {
56
+ if (!done) {
57
+ done = true;
58
+ os.clearTimeout(timer);
59
+ sock.closesocket(s);
60
+ resolve(buf);
61
+ }
62
+ }
63
+ });
64
+ sock.connect(s, host, port);
65
+ });
66
+ }
67
+ // ── Test suite ──
68
+ export const suite = {
69
+ name: 'http-server',
70
+ run: async (t) => {
71
+ const requests = [];
72
+ const server = createServer(async (req) => {
73
+ const url = new URL(req.url);
74
+ const path = url.pathname;
75
+ requests.push(req.method + ' ' + path);
76
+ if (path === '/') {
77
+ return new Response('hello ' + (url.searchParams.get('name') || ''), { headers: { 'Content-Type': 'text/plain' } });
78
+ }
79
+ else if (path === '/echo') {
80
+ return new Response(req.method + ':' + await req.text(), { headers: { 'Content-Type': 'text/plain' } });
81
+ }
82
+ else if (path === '/json') {
83
+ return new Response(JSON.stringify({ method: req.method, q: Object.fromEntries(url.searchParams) }), { headers: { 'Content-Type': 'application/json' } });
84
+ }
85
+ else {
86
+ return new Response('not found', { status: 404 });
87
+ }
88
+ });
89
+ const rc = server.listen(0, '127.0.0.1');
90
+ t.checkTrue('listen ok', rc === 0);
91
+ const addr = server.address();
92
+ if (!addr) {
93
+ t.check('address', 'non-null', 'null');
94
+ return;
95
+ }
96
+ t.checkTrue('bound port > 0', addr.port > 0);
97
+ const port = addr.port;
98
+ // ── GET with query ──
99
+ t.section('GET with query');
100
+ let r = await httpSend('127.0.0.1', port, 'GET /?name=world HTTP/1.1\r\nHost: x\r\nConnection: close\r\n\r\n');
101
+ t.checkTrue('200 status', r.indexOf('HTTP/1.1 200') === 0);
102
+ t.checkTrue('body = hello world', r.endsWith('hello world'));
103
+ t.checkTrue('content-type text', r.toLowerCase().indexOf('content-type: text/plain') >= 0);
104
+ // ── POST with body ──
105
+ t.section('POST with body');
106
+ r = await httpSend('127.0.0.1', port, 'POST /echo HTTP/1.1\r\nHost: x\r\nContent-Length: 7\r\nConnection: close\r\n\r\npayload');
107
+ t.checkTrue('echo body', r.endsWith('POST:payload'));
108
+ // ── POST with chunked request body ──
109
+ t.section('chunked request body');
110
+ const chunkedRaw = 'POST /echo HTTP/1.1\r\nHost: x\r\nTransfer-Encoding: chunked\r\nConnection: close\r\n\r\n' +
111
+ '3\r\nabc\r\n4\r\ndefg\r\n0\r\n\r\n';
112
+ r = await httpSend('127.0.0.1', port, chunkedRaw);
113
+ t.checkTrue('chunked decoded', r.endsWith('POST:abcdefg'));
114
+ // ── 404 ──
115
+ t.section('404');
116
+ r = await httpSend('127.0.0.1', port, 'GET /missing HTTP/1.1\r\nHost: x\r\nConnection: close\r\n\r\n');
117
+ t.checkTrue('404 status', r.indexOf('HTTP/1.1 404') === 0);
118
+ t.checkTrue('404 body', r.endsWith('not found'));
119
+ // ── HEAD (headers present, empty body) ──
120
+ t.section('HEAD');
121
+ r = await httpSend('127.0.0.1', port, 'HEAD / HTTP/1.1\r\nHost: x\r\nConnection: close\r\n\r\n');
122
+ t.checkTrue('HEAD 200', r.indexOf('HTTP/1.1 200') === 0);
123
+ t.checkTrue('HEAD content-length present', r.toLowerCase().indexOf('content-length: 6') >= 0);
124
+ const bodyIdx = r.indexOf('\r\n\r\n');
125
+ t.checkTrue('HEAD empty body', r.slice(bodyIdx + 4).length === 0);
126
+ // ── keep-alive: two requests on one connection ──
127
+ t.section('keep-alive pipelining');
128
+ const pipelined = 'GET /?name=a HTTP/1.1\r\nHost: x\r\n\r\n' +
129
+ 'GET /?name=b HTTP/1.1\r\nHost: x\r\nConnection: close\r\n\r\n';
130
+ r = await httpSend('127.0.0.1', port, pipelined, 2);
131
+ t.checkTrue('two responses received', r.indexOf('hello a') >= 0 && r.indexOf('hello b') >= 0);
132
+ t.checkTrue('connection close on last', r.toLowerCase().indexOf('connection: close') >= 0);
133
+ // ── server received expected requests ──
134
+ t.section('request log');
135
+ t.check('total requests', 7, requests.length);
136
+ t.checkTrue('saw GET /', requests.indexOf('GET /') >= 0);
137
+ t.checkTrue('saw POST /echo', requests.indexOf('POST /echo') >= 0);
138
+ server.close();
139
+ t.check('closed ok', true, true);
140
+ std.printf(' (http-server suite done, %d requests)\n', requests.length);
141
+ },
142
+ };
@@ -0,0 +1,136 @@
1
+ import './test_helper.js'
2
+ import * as std from 'std'
3
+ import * as os from 'os'
4
+ import * as sock from 'sock'
5
+ import { Tester } from './test_helper.js'
6
+ import { createServer } from '../lib/http-server.js'
7
+
8
+ // ── Minimal raw HTTP client for testing ──
9
+
10
+ function httpSend(host: string, port: number, raw: string, nResponses = 1): Promise<string> {
11
+ return new Promise((resolve, reject) => {
12
+ const s = sock.socket()
13
+ if (s < 0) { reject(new Error('socket create failed')); return }
14
+ let buf = ''
15
+ let done = false
16
+ const timer = os.setTimeout(() => {
17
+ if (!done) { done = true; sock.closesocket(s); reject(new Error('timeout: ' + buf)) }
18
+ }, 10000)
19
+ sock.set_on_event(s, (event) => {
20
+ if (done) return
21
+ if (event.lNetworkEvents & sock.FdEvent.FD_CONNECT) {
22
+ const err = event.iErrorCode[0]
23
+ if (err !== 0) {
24
+ done = true
25
+ os.clearTimeout(timer)
26
+ sock.closesocket(s)
27
+ reject(new Error('connect error ' + err))
28
+ return
29
+ }
30
+ const bytes = new TextEncoder().encode(raw)
31
+ sock.send(s, bytes.buffer)
32
+ }
33
+ if (event.lNetworkEvents & sock.FdEvent.FD_READ) {
34
+ while (true) {
35
+ const d = sock.recv(s, 8192) as unknown as ArrayBuffer | number
36
+ if (typeof d === 'number') break
37
+ if (!d || d.byteLength === 0) break
38
+ buf += new TextDecoder().decode(new Uint8Array(d))
39
+ }
40
+ const count = (buf.match(/HTTP\/1\.1 /g) || []).length
41
+ if (count >= nResponses) {
42
+ done = true
43
+ os.clearTimeout(timer)
44
+ sock.closesocket(s)
45
+ resolve(buf)
46
+ }
47
+ }
48
+ if (event.lNetworkEvents & sock.FdEvent.FD_CLOSE) {
49
+ if (!done) { done = true; os.clearTimeout(timer); sock.closesocket(s); resolve(buf) }
50
+ }
51
+ })
52
+ sock.connect(s, host, port)
53
+ })
54
+ }
55
+
56
+ // ── Test suite ──
57
+
58
+ export const suite = {
59
+ name: 'http-server',
60
+ run: async (t: Tester) => {
61
+ const requests: string[] = []
62
+ const server = createServer(async (req) => {
63
+ const url = new URL(req.url)
64
+ const path = url.pathname
65
+ requests.push(req.method + ' ' + path)
66
+ if (path === '/') {
67
+ return new Response('hello ' + (url.searchParams.get('name') || ''), { headers: { 'Content-Type': 'text/plain' } })
68
+ } else if (path === '/echo') {
69
+ return new Response(req.method + ':' + await req.text(), { headers: { 'Content-Type': 'text/plain' } })
70
+ } else if (path === '/json') {
71
+ return new Response(JSON.stringify({ method: req.method, q: Object.fromEntries(url.searchParams) }), { headers: { 'Content-Type': 'application/json' } })
72
+ } else {
73
+ return new Response('not found', { status: 404 })
74
+ }
75
+ })
76
+
77
+ const rc = server.listen(0, '127.0.0.1')
78
+ t.checkTrue('listen ok', rc === 0)
79
+ const addr = server.address()
80
+ if (!addr) { t.check('address', 'non-null', 'null'); return }
81
+ t.checkTrue('bound port > 0', addr.port > 0)
82
+ const port = addr.port
83
+
84
+ // ── GET with query ──
85
+ t.section('GET with query')
86
+ let r = await httpSend('127.0.0.1', port, 'GET /?name=world HTTP/1.1\r\nHost: x\r\nConnection: close\r\n\r\n')
87
+ t.checkTrue('200 status', r.indexOf('HTTP/1.1 200') === 0)
88
+ t.checkTrue('body = hello world', r.endsWith('hello world'))
89
+ t.checkTrue('content-type text', r.toLowerCase().indexOf('content-type: text/plain') >= 0)
90
+
91
+ // ── POST with body ──
92
+ t.section('POST with body')
93
+ r = await httpSend('127.0.0.1', port, 'POST /echo HTTP/1.1\r\nHost: x\r\nContent-Length: 7\r\nConnection: close\r\n\r\npayload')
94
+ t.checkTrue('echo body', r.endsWith('POST:payload'))
95
+
96
+ // ── POST with chunked request body ──
97
+ t.section('chunked request body')
98
+ const chunkedRaw = 'POST /echo HTTP/1.1\r\nHost: x\r\nTransfer-Encoding: chunked\r\nConnection: close\r\n\r\n' +
99
+ '3\r\nabc\r\n4\r\ndefg\r\n0\r\n\r\n'
100
+ r = await httpSend('127.0.0.1', port, chunkedRaw)
101
+ t.checkTrue('chunked decoded', r.endsWith('POST:abcdefg'))
102
+
103
+ // ── 404 ──
104
+ t.section('404')
105
+ r = await httpSend('127.0.0.1', port, 'GET /missing HTTP/1.1\r\nHost: x\r\nConnection: close\r\n\r\n')
106
+ t.checkTrue('404 status', r.indexOf('HTTP/1.1 404') === 0)
107
+ t.checkTrue('404 body', r.endsWith('not found'))
108
+
109
+ // ── HEAD (headers present, empty body) ──
110
+ t.section('HEAD')
111
+ r = await httpSend('127.0.0.1', port, 'HEAD / HTTP/1.1\r\nHost: x\r\nConnection: close\r\n\r\n')
112
+ t.checkTrue('HEAD 200', r.indexOf('HTTP/1.1 200') === 0)
113
+ t.checkTrue('HEAD content-length present', r.toLowerCase().indexOf('content-length: 6') >= 0)
114
+ const bodyIdx = r.indexOf('\r\n\r\n')
115
+ t.checkTrue('HEAD empty body', r.slice(bodyIdx + 4).length === 0)
116
+
117
+ // ── keep-alive: two requests on one connection ──
118
+ t.section('keep-alive pipelining')
119
+ const pipelined = 'GET /?name=a HTTP/1.1\r\nHost: x\r\n\r\n' +
120
+ 'GET /?name=b HTTP/1.1\r\nHost: x\r\nConnection: close\r\n\r\n'
121
+ r = await httpSend('127.0.0.1', port, pipelined, 2)
122
+ t.checkTrue('two responses received', r.indexOf('hello a') >= 0 && r.indexOf('hello b') >= 0)
123
+ t.checkTrue('connection close on last', r.toLowerCase().indexOf('connection: close') >= 0)
124
+
125
+ // ── server received expected requests ──
126
+ t.section('request log')
127
+ t.check('total requests', 7, requests.length)
128
+ t.checkTrue('saw GET /', requests.indexOf('GET /') >= 0)
129
+ t.checkTrue('saw POST /echo', requests.indexOf('POST /echo') >= 0)
130
+
131
+ server.close()
132
+ t.check('closed ok', true, true)
133
+
134
+ std.printf(' (http-server suite done, %d requests)\n', requests.length)
135
+ },
136
+ }
@@ -0,0 +1,4 @@
1
+ import '../lib/fetch.js';
2
+ import * as os from 'os';
3
+ const parent = os.Worker.parent;
4
+ parent.postMessage({ type: 'loaded' });
@@ -0,0 +1,5 @@
1
+ import '../lib/fetch.js'
2
+ import * as os from 'os'
3
+
4
+ const parent = os.Worker.parent
5
+ parent.postMessage({ type: 'loaded' })
@@ -0,0 +1,134 @@
1
+ import * as sock from 'sock';
2
+ import * as std from 'std';
3
+ import * as os from 'os';
4
+ import '../lib/fetch.js';
5
+ import '../lib/websocket.js';
6
+ const HTTP_PORT = 18923;
7
+ const HTTPS_PORT = 18924;
8
+ export const suite = {
9
+ name: 'ipv6',
10
+ run: async (t) => {
11
+ function assert(name, ok) {
12
+ if (ok) {
13
+ t.ok++;
14
+ std.printf(' PASS: %s\n', name);
15
+ }
16
+ else {
17
+ t.fail++;
18
+ std.printf(' FAIL: %s\n', name);
19
+ }
20
+ }
21
+ // ── 1. sock.resolve ──
22
+ t.section('sock.resolve ::1');
23
+ {
24
+ const ip = sock.resolve('::1');
25
+ assert('resolve ::1 returns ::1', ip === '::1');
26
+ }
27
+ // ── 2. Raw socket IPv6 ──
28
+ t.section('raw socket ::1 HTTP GET');
29
+ await new Promise((resolve, reject) => {
30
+ const s = sock.socket();
31
+ if (s === null || s < 0) {
32
+ t.fail++;
33
+ reject(new Error('socket() failed'));
34
+ return;
35
+ }
36
+ let connected = false, gotData = false;
37
+ sock.set_on_event(s, (event) => {
38
+ if (event.lNetworkEvents & 16 /* sock.FdEvent.FD_CONNECT */) {
39
+ if (event.iErrorCode[0] === 0) {
40
+ connected = true;
41
+ t.ok++;
42
+ const req = "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n";
43
+ const buf = new ArrayBuffer(req.length);
44
+ const v = new Uint8Array(buf);
45
+ for (let i = 0; i < req.length; i++)
46
+ v[i] = req.charCodeAt(i);
47
+ sock.send(s, buf);
48
+ }
49
+ else {
50
+ sock.closesocket(s);
51
+ reject(new Error('connect error: ' + event.iErrorCode[0]));
52
+ }
53
+ }
54
+ if (event.lNetworkEvents & 1 /* sock.FdEvent.FD_READ */ && connected && !gotData) {
55
+ const data = sock.recv(s, 4096);
56
+ if (data && data.byteLength > 0) {
57
+ gotData = true;
58
+ t.ok++;
59
+ }
60
+ }
61
+ if (event.lNetworkEvents & 32 /* sock.FdEvent.FD_CLOSE */) {
62
+ sock.closesocket(s);
63
+ if (gotData && connected)
64
+ resolve();
65
+ else
66
+ reject(new Error('test incomplete'));
67
+ }
68
+ });
69
+ sock.connect(s, '::1', HTTP_PORT);
70
+ });
71
+ // ── 3. fetch HTTP IPv6 ──
72
+ t.section('fetch http://[::1]');
73
+ {
74
+ const r = await fetch(`http://[::1]:${HTTP_PORT}/`, { timeout: 5000 });
75
+ assert('fetch IPv6 HTTP status 200', r.status === 200);
76
+ const body = await r.text();
77
+ assert('fetch IPv6 HTTP body', body === 'hello from test server');
78
+ }
79
+ // ── 4. fetch HTTPS IPv6 ──
80
+ t.section('fetch https://[::1]');
81
+ {
82
+ const r = await fetch(`https://[::1]:${HTTPS_PORT}/`, { timeout: 5000 });
83
+ assert('fetch IPv6 HTTPS status 200', r.status === 200);
84
+ const body = await r.text();
85
+ assert('fetch IPv6 HTTPS body', body === 'hello from test server');
86
+ }
87
+ // ── 5. WebSocket IPv6 ──
88
+ t.section('ws://[::1] echo');
89
+ await new Promise((resolve, reject) => {
90
+ const ws = new WebSocket(`ws://[::1]:${HTTP_PORT}/`);
91
+ const timeoutId = os.setTimeout(() => reject(new Error('WS IPv6 timeout')), 10000);
92
+ ws.onopen = () => {
93
+ t.ok++;
94
+ ws.send('hello ipv6');
95
+ };
96
+ ws.onmessage = (evt) => {
97
+ t.ok++;
98
+ assert('WS IPv6 echo matches', evt.data === 'hello ipv6');
99
+ ws.close();
100
+ };
101
+ ws.onclose = () => {
102
+ os.clearTimeout(timeoutId);
103
+ resolve();
104
+ };
105
+ ws.onerror = () => {
106
+ os.clearTimeout(timeoutId);
107
+ reject(new Error('WS IPv6 error'));
108
+ };
109
+ });
110
+ // ── 6. WebSocket WSS IPv6 ──
111
+ t.section('wss://[::1] echo');
112
+ await new Promise((resolve, reject) => {
113
+ const ws = new WebSocket(`wss://[::1]:${HTTPS_PORT}/`);
114
+ const timeoutId = os.setTimeout(() => reject(new Error('WSS IPv6 timeout')), 10000);
115
+ ws.onopen = () => {
116
+ t.ok++;
117
+ ws.send('hello wss ipv6');
118
+ };
119
+ ws.onmessage = (evt) => {
120
+ t.ok++;
121
+ assert('WSS IPv6 echo matches', evt.data === 'hello wss ipv6');
122
+ ws.close();
123
+ };
124
+ ws.onclose = () => {
125
+ os.clearTimeout(timeoutId);
126
+ resolve();
127
+ };
128
+ ws.onerror = () => {
129
+ os.clearTimeout(timeoutId);
130
+ reject(new Error('WSS IPv6 error'));
131
+ };
132
+ });
133
+ }
134
+ };
@@ -0,0 +1,129 @@
1
+ import * as sock from 'sock'
2
+ import * as std from 'std'
3
+ import * as os from 'os'
4
+ import '../lib/fetch.js'
5
+ import '../lib/websocket.js'
6
+ import { Tester } from './test_helper.js'
7
+
8
+ const HTTP_PORT = 18923
9
+ const HTTPS_PORT = 18924
10
+
11
+ export const suite = {
12
+ name: 'ipv6',
13
+ run: async (t: Tester) => {
14
+ function assert(name: string, ok: boolean): void {
15
+ if (ok) { t.ok++; std.printf(' PASS: %s\n', name) }
16
+ else { t.fail++; std.printf(' FAIL: %s\n', name) }
17
+ }
18
+
19
+ // ── 1. sock.resolve ──
20
+ t.section('sock.resolve ::1')
21
+ {
22
+ const ip = sock.resolve('::1')
23
+ assert('resolve ::1 returns ::1', ip === '::1')
24
+ }
25
+
26
+ // ── 2. Raw socket IPv6 ──
27
+ t.section('raw socket ::1 HTTP GET')
28
+ await new Promise<void>((resolve, reject) => {
29
+ const s = sock.socket()
30
+ if (s === null || s < 0) { t.fail++; reject(new Error('socket() failed')); return }
31
+
32
+ let connected = false, gotData = false
33
+
34
+ sock.set_on_event(s, (event: { lNetworkEvents: number; iErrorCode: number[] }) => {
35
+ if (event.lNetworkEvents & sock.FdEvent.FD_CONNECT) {
36
+ if (event.iErrorCode[0] === 0) {
37
+ connected = true; t.ok++
38
+ const req = "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n"
39
+ const buf = new ArrayBuffer(req.length)
40
+ const v = new Uint8Array(buf)
41
+ for (let i = 0; i < req.length; i++) v[i] = req.charCodeAt(i)
42
+ sock.send(s, buf)
43
+ } else {
44
+ sock.closesocket(s)
45
+ reject(new Error('connect error: ' + event.iErrorCode[0]))
46
+ }
47
+ }
48
+ if (event.lNetworkEvents & sock.FdEvent.FD_READ && connected && !gotData) {
49
+ const data = sock.recv(s, 4096)
50
+ if (data && data.byteLength > 0) { gotData = true; t.ok++ }
51
+ }
52
+ if (event.lNetworkEvents & sock.FdEvent.FD_CLOSE) {
53
+ sock.closesocket(s)
54
+ if (gotData && connected) resolve()
55
+ else reject(new Error('test incomplete'))
56
+ }
57
+ })
58
+ sock.connect(s, '::1', HTTP_PORT)
59
+ })
60
+
61
+ // ── 3. fetch HTTP IPv6 ──
62
+ t.section('fetch http://[::1]')
63
+ {
64
+ const r = await fetch(`http://[::1]:${HTTP_PORT}/`, { timeout: 5000 })
65
+ assert('fetch IPv6 HTTP status 200', r.status === 200)
66
+ const body = await r.text()
67
+ assert('fetch IPv6 HTTP body', body === 'hello from test server')
68
+ }
69
+
70
+ // ── 4. fetch HTTPS IPv6 ──
71
+ t.section('fetch https://[::1]')
72
+ {
73
+ const r = await fetch(`https://[::1]:${HTTPS_PORT}/`, { timeout: 5000 })
74
+ assert('fetch IPv6 HTTPS status 200', r.status === 200)
75
+ const body = await r.text()
76
+ assert('fetch IPv6 HTTPS body', body === 'hello from test server')
77
+ }
78
+
79
+ // ── 5. WebSocket IPv6 ──
80
+ t.section('ws://[::1] echo')
81
+ await new Promise<void>((resolve, reject) => {
82
+ const ws = new WebSocket(`ws://[::1]:${HTTP_PORT}/`)
83
+ const timeoutId = os.setTimeout(() => reject(new Error('WS IPv6 timeout')), 10000)
84
+
85
+ ws.onopen = () => {
86
+ t.ok++
87
+ ws.send('hello ipv6')
88
+ }
89
+ ws.onmessage = (evt) => {
90
+ t.ok++
91
+ assert('WS IPv6 echo matches', evt.data === 'hello ipv6')
92
+ ws.close()
93
+ }
94
+ ws.onclose = () => {
95
+ os.clearTimeout(timeoutId)
96
+ resolve()
97
+ }
98
+ ws.onerror = () => {
99
+ os.clearTimeout(timeoutId)
100
+ reject(new Error('WS IPv6 error'))
101
+ }
102
+ })
103
+
104
+ // ── 6. WebSocket WSS IPv6 ──
105
+ t.section('wss://[::1] echo')
106
+ await new Promise<void>((resolve, reject) => {
107
+ const ws = new WebSocket(`wss://[::1]:${HTTPS_PORT}/`)
108
+ const timeoutId = os.setTimeout(() => reject(new Error('WSS IPv6 timeout')), 10000)
109
+
110
+ ws.onopen = () => {
111
+ t.ok++
112
+ ws.send('hello wss ipv6')
113
+ }
114
+ ws.onmessage = (evt) => {
115
+ t.ok++
116
+ assert('WSS IPv6 echo matches', evt.data === 'hello wss ipv6')
117
+ ws.close()
118
+ }
119
+ ws.onclose = () => {
120
+ os.clearTimeout(timeoutId)
121
+ resolve()
122
+ }
123
+ ws.onerror = () => {
124
+ os.clearTimeout(timeoutId)
125
+ reject(new Error('WSS IPv6 error'))
126
+ }
127
+ })
128
+ }
129
+ }