quickwin 3.0.1 → 3.0.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 (163) hide show
  1. package/README.en.md +3 -0
  2. package/README.md +2 -0
  3. package/package.json +2 -3
  4. package/qwin-nowasm-x86.exe +0 -0
  5. package/qwin-nowasm.exe +0 -0
  6. package/qwin-x86.exe +0 -0
  7. package/qwin.exe +0 -0
  8. package/examples/PdfCanvas.js +0 -38
  9. package/examples/PdfCanvas.tsx +0 -54
  10. package/examples/execd/main.js +0 -23379
  11. package/examples/execd/tray.js +0 -192
  12. package/examples/execd/worker.js +0 -122
  13. package/examples/pdf_preview2.js +0 -398
  14. package/examples/pdf_preview2.ts +0 -353
  15. package/examples/pdf_viewer.js +0 -22515
  16. package/examples/pdf_viewer.tsx +0 -228
  17. package/examples/test_gallery.js +0 -18692
  18. package/examples/test_gallery.tsx +0 -291
  19. package/examples/test_react_lazy.js +0 -18560
  20. package/examples/test_react_lazy.tsx +0 -65
  21. package/examples/test_react_listbox.js +0 -18562
  22. package/examples/test_react_listbox.tsx +0 -73
  23. package/examples/test_root_window.js +0 -18532
  24. package/examples/test_root_window.tsx +0 -41
  25. package/examples/test_tab.js +0 -18563
  26. package/examples/test_tab.tsx +0 -77
  27. package/examples/tray_demo.js +0 -86
  28. package/examples/tray_demo.tsx +0 -89
  29. package/test/add.wasm +0 -0
  30. package/test/bench_ffi_callraw.js +0 -235
  31. package/test/bench_ffi_callraw.ts +0 -235
  32. package/test/bench_ffi_overhead.js +0 -139
  33. package/test/bench_ffi_overhead.ts +0 -141
  34. package/test/bench_sendmessage.js +0 -104
  35. package/test/bench_sendmessage.ts +0 -100
  36. package/test/bench_sendmessage_nonnull.js +0 -187
  37. package/test/bench_sendmessage_nonnull.ts +0 -188
  38. package/test/complex.wasm +0 -0
  39. package/test/complex_imports.wasm +0 -0
  40. package/test/debug/http_server_e2e.js +0 -31
  41. package/test/debug/minwasm.js +0 -52
  42. package/test/debug/mupdf_debug.js +0 -86
  43. package/test/debug/wasm.js +0 -122
  44. package/test/debug/wasm_export_debug.js +0 -54
  45. package/test/debug/wasm_names.js +0 -36
  46. package/test/example.pdf +0 -0
  47. package/test/global_imports.wasm +0 -0
  48. package/test/import_func.wasm +0 -0
  49. package/test/imports.wasm +0 -0
  50. package/test/mupdf-cdn.js +0 -40
  51. package/test/mupdf-cdn.ts +0 -43
  52. package/test/run.js +0 -111
  53. package/test/run.ts +0 -116
  54. package/test/sjlj.wasm +0 -0
  55. package/test/test_basic.js +0 -7
  56. package/test/test_basic.ts +0 -9
  57. package/test/test_basic_fetch.js +0 -63
  58. package/test/test_basic_fetch.ts +0 -59
  59. package/test/test_brotli.js +0 -48
  60. package/test/test_brotli.ts +0 -52
  61. package/test/test_fetch_cache.js +0 -169
  62. package/test/test_fetch_cache.ts +0 -187
  63. package/test/test_ffi.js +0 -157
  64. package/test/test_ffi.ts +0 -170
  65. package/test/test_ffi_abi.js +0 -290
  66. package/test/test_ffi_abi.ts +0 -274
  67. package/test/test_ffi_array.js +0 -116
  68. package/test/test_ffi_array.ts +0 -141
  69. package/test/test_ffi_bind.js +0 -656
  70. package/test/test_ffi_bind.ts +0 -670
  71. package/test/test_ffi_struct.js +0 -779
  72. package/test/test_ffi_struct.ts +0 -828
  73. package/test/test_ffi_struct_layout.js +0 -495
  74. package/test/test_ffi_struct_layout.ts +0 -543
  75. package/test/test_ffi_user32.js +0 -103
  76. package/test/test_ffi_user32.ts +0 -113
  77. package/test/test_ffi_windows.js +0 -172
  78. package/test/test_ffi_windows.ts +0 -187
  79. package/test/test_files.js +0 -102
  80. package/test/test_files.ts +0 -121
  81. package/test/test_frame_encoding.js +0 -128
  82. package/test/test_frame_encoding.ts +0 -132
  83. package/test/test_gdi_draw.js +0 -681
  84. package/test/test_gdi_draw.ts +0 -725
  85. package/test/test_helper.js +0 -91
  86. package/test/test_helper.ts +0 -88
  87. package/test/test_http_import.js +0 -96
  88. package/test/test_http_import.ts +0 -93
  89. package/test/test_http_server.js +0 -214
  90. package/test/test_http_server.ts +0 -211
  91. package/test/test_import_dir.js +0 -4
  92. package/test/test_import_dir.ts +0 -5
  93. package/test/test_ipv6.js +0 -134
  94. package/test/test_ipv6.ts +0 -130
  95. package/test/test_jsx.js +0 -1314
  96. package/test/test_meta_url.js +0 -3
  97. package/test/test_meta_url.ts +0 -4
  98. package/test/test_mupdf_render.js +0 -51
  99. package/test/test_mupdf_render.ts +0 -55
  100. package/test/test_mupdf_twice.js +0 -62
  101. package/test/test_mupdf_twice.ts +0 -65
  102. package/test/test_mupdf_wasm.js +0 -39
  103. package/test/test_mupdf_wasm.ts +0 -32
  104. package/test/test_net_event.js +0 -57
  105. package/test/test_net_event.ts +0 -56
  106. package/test/test_net_fetch.js +0 -384
  107. package/test/test_net_fetch.ts +0 -373
  108. package/test/test_net_websocket.js +0 -180
  109. package/test/test_net_websocket.ts +0 -164
  110. package/test/test_polyfill.js +0 -144
  111. package/test/test_polyfill.ts +0 -159
  112. package/test/test_printer.js +0 -137
  113. package/test/test_printer.ts +0 -148
  114. package/test/test_proc.js +0 -141
  115. package/test/test_proc.ts +0 -163
  116. package/test/test_react_complex.js +0 -18678
  117. package/test/test_react_counter.js +0 -18555
  118. package/test/test_react_flex.js +0 -18871
  119. package/test/test_react_hidden.js +0 -18560
  120. package/test/test_react_listview_link.js +0 -18590
  121. package/test/test_react_notify.js +0 -18797
  122. package/test/test_react_render.js +0 -18550
  123. package/test/test_registry.js +0 -74
  124. package/test/test_registry.ts +0 -95
  125. package/test/test_sysinfo.js +0 -122
  126. package/test/test_sysinfo.ts +0 -141
  127. package/test/test_url.js +0 -173
  128. package/test/test_url.ts +0 -183
  129. package/test/test_wasm_basic.js +0 -85
  130. package/test/test_wasm_basic.ts +0 -73
  131. package/test/test_wasm_import_global.js +0 -41
  132. package/test/test_wasm_import_global.ts +0 -45
  133. package/test/test_wasm_sjlj.js +0 -145
  134. package/test/test_wasm_sjlj.ts +0 -125
  135. package/test/test_wasm_types.js +0 -97
  136. package/test/test_wasm_types.ts +0 -124
  137. package/test/test_wolfssl_read.js +0 -203
  138. package/test/test_wolfssl_read.ts +0 -184
  139. package/test/test_worker.js +0 -21
  140. package/test/test_worker.ts +0 -24
  141. package/test/test_worker_concurrent.js +0 -45
  142. package/test/test_worker_concurrent.ts +0 -56
  143. package/test/test_worker_data_url.js +0 -46
  144. package/test/test_worker_data_url.ts +0 -51
  145. package/test/test_worker_fetch.js +0 -26
  146. package/test/test_worker_fetch.ts +0 -27
  147. package/test/test_worker_net.js +0 -69
  148. package/test/test_worker_net.ts +0 -66
  149. package/test/test_worker_wasm_concurrent.js +0 -44
  150. package/test/test_worker_wasm_concurrent.ts +0 -45
  151. package/test/types.wasm +0 -0
  152. package/test/worker_fetch.js +0 -13
  153. package/test/worker_fetch.ts +0 -13
  154. package/test/worker_helper.js +0 -1
  155. package/test/worker_helper.ts +0 -1
  156. package/test/worker_http.js +0 -18
  157. package/test/worker_http.ts +0 -20
  158. package/test/worker_import_lib.js +0 -12
  159. package/test/worker_import_lib.ts +0 -13
  160. package/test/worker_module.js +0 -16
  161. package/test/worker_module.ts +0 -17
  162. package/test/worker_wasm_concurrent.js +0 -22
  163. package/test/worker_wasm_concurrent.ts +0 -22
@@ -1,91 +0,0 @@
1
- import * as std from 'std';
2
- const GREEN = '\x1b[32m';
3
- const RED = '\x1b[31m';
4
- const YELLOW = '\x1b[33m';
5
- const BOLD = '\x1b[1m';
6
- const RESET = '\x1b[0m';
7
- function formatDuration(ms) {
8
- if (ms >= 1000)
9
- return (ms / 1000).toFixed(2) + 's';
10
- return ms + 'ms';
11
- }
12
- export function readWasmFile(path) {
13
- const base = import.meta.url.slice(0, import.meta.url.lastIndexOf('/') + 1);
14
- const parts = (base + path).split('/');
15
- const out = [];
16
- for (const p of parts) {
17
- if (p === '..')
18
- out.pop();
19
- else if (p !== '.')
20
- out.push(p);
21
- }
22
- let filePath = out.join('/').slice(7);
23
- if (filePath.length >= 3 && filePath[1] === ':')
24
- filePath = filePath.slice(1);
25
- const fp = std.open(filePath, 'rb');
26
- if (!fp)
27
- return null;
28
- fp.seek(0, 2);
29
- const size = fp.tell();
30
- fp.seek(0, 0);
31
- const buffer = new ArrayBuffer(size);
32
- fp.read(buffer, 0, size);
33
- fp.close();
34
- return buffer;
35
- }
36
- export class Tester {
37
- ok = 0;
38
- fail = 0;
39
- skip = 0;
40
- startTime = Date.now();
41
- sectionStart = this.startTime;
42
- lastSection = '';
43
- section(name) {
44
- const now = Date.now();
45
- if (this.lastSection) {
46
- const elapsed = now - this.sectionStart;
47
- std.printf(' (%s)\n', formatDuration(elapsed));
48
- }
49
- this.sectionStart = now;
50
- this.lastSection = name;
51
- std.printf('\n%s=== %s ===%s\n', BOLD, name, RESET);
52
- }
53
- check(name, expected, actual) {
54
- if (typeof expected === 'number' && typeof actual === 'number') {
55
- if (Math.abs(expected - actual) < 0.0001) {
56
- this.ok++;
57
- std.printf(' %sPASS:%s %s = %s\n', GREEN, RESET, name, String(actual));
58
- }
59
- else {
60
- this.fail++;
61
- std.printf(' %sFAIL:%s %s = %s (expected %s)\n', RED, RESET, name, String(actual), String(expected));
62
- }
63
- }
64
- else if (expected === actual) {
65
- this.ok++;
66
- std.printf(' %sPASS:%s %s = %s\n', GREEN, RESET, name, String(actual));
67
- }
68
- else {
69
- this.fail++;
70
- std.printf(' %sFAIL:%s %s = %s (expected %s)\n', RED, RESET, name, String(actual), String(expected));
71
- }
72
- }
73
- checkTrue(name, actual) {
74
- this.check(name, true, actual);
75
- }
76
- skipCase(name) {
77
- this.skip++;
78
- std.printf(' %sSKIP:%s %s\n', YELLOW, RESET, name);
79
- }
80
- summary() {
81
- const now = Date.now();
82
- if (this.lastSection) {
83
- const elapsed = now - this.sectionStart;
84
- std.printf(' (%s)\n', formatDuration(elapsed));
85
- }
86
- const total = now - this.startTime;
87
- const color = this.fail > 0 ? RED : GREEN;
88
- const skipped = this.skip > 0 ? ` (${this.skip} skipped)` : '';
89
- std.printf('\n%s%d/%d passed%s (%s)%s\n', color, this.ok, this.ok + this.fail, skipped, formatDuration(total), RESET);
90
- }
91
- }
@@ -1,88 +0,0 @@
1
- import * as std from 'std'
2
-
3
- const GREEN = '\x1b[32m'
4
- const RED = '\x1b[31m'
5
- const YELLOW = '\x1b[33m'
6
- const BOLD = '\x1b[1m'
7
- const RESET = '\x1b[0m'
8
-
9
- function formatDuration(ms: number): string {
10
- if (ms >= 1000) return (ms / 1000).toFixed(2) + 's'
11
- return ms + 'ms'
12
- }
13
-
14
- export function readWasmFile(path: string): ArrayBuffer | null {
15
- const base = import.meta.url.slice(0, import.meta.url.lastIndexOf('/') + 1)
16
- const parts = (base + path).split('/')
17
- const out: string[] = []
18
- for (const p of parts) {
19
- if (p === '..') out.pop()
20
- else if (p !== '.') out.push(p)
21
- }
22
- let filePath = out.join('/').slice(7)
23
- if (filePath.length >= 3 && filePath[1] === ':') filePath = filePath.slice(1)
24
- const fp = std.open(filePath, 'rb')
25
- if (!fp) return null
26
- fp.seek(0, 2)
27
- const size = fp.tell()
28
- fp.seek(0, 0)
29
- const buffer = new ArrayBuffer(size)
30
- fp.read(buffer, 0, size)
31
- fp.close()
32
- return buffer
33
- }
34
-
35
- export class Tester {
36
- ok = 0
37
- fail = 0
38
- skip = 0
39
- private startTime = Date.now()
40
- private sectionStart = this.startTime
41
- private lastSection = ''
42
-
43
- section(name: string): void {
44
- const now = Date.now()
45
- if (this.lastSection) {
46
- const elapsed = now - this.sectionStart
47
- std.printf(' (%s)\n', formatDuration(elapsed))
48
- }
49
- this.sectionStart = now
50
- this.lastSection = name
51
- std.printf('\n%s=== %s ===%s\n', BOLD, name, RESET)
52
- }
53
-
54
- check(name: string, expected: unknown, actual: unknown): void {
55
- if (typeof expected === 'number' && typeof actual === 'number') {
56
- if (Math.abs(expected - actual) < 0.0001) {
57
- this.ok++; std.printf(' %sPASS:%s %s = %s\n', GREEN, RESET, name, String(actual))
58
- } else {
59
- this.fail++; std.printf(' %sFAIL:%s %s = %s (expected %s)\n', RED, RESET, name, String(actual), String(expected))
60
- }
61
- } else if (expected === actual) {
62
- this.ok++; std.printf(' %sPASS:%s %s = %s\n', GREEN, RESET, name, String(actual))
63
- } else {
64
- this.fail++; std.printf(' %sFAIL:%s %s = %s (expected %s)\n', RED, RESET, name, String(actual), String(expected))
65
- }
66
- }
67
-
68
- checkTrue(name: string, actual: boolean): void {
69
- this.check(name, true, actual)
70
- }
71
-
72
- skipCase(name: string): void {
73
- this.skip++
74
- std.printf(' %sSKIP:%s %s\n', YELLOW, RESET, name)
75
- }
76
-
77
- summary(): void {
78
- const now = Date.now()
79
- if (this.lastSection) {
80
- const elapsed = now - this.sectionStart
81
- std.printf(' (%s)\n', formatDuration(elapsed))
82
- }
83
- const total = now - this.startTime
84
- const color = this.fail > 0 ? RED : GREEN
85
- const skipped = this.skip > 0 ? ` (${this.skip} skipped)` : ''
86
- std.printf('\n%s%d/%d passed%s (%s)%s\n', color, this.ok, this.ok + this.fail, skipped, formatDuration(total), RESET)
87
- }
88
- }
@@ -1,96 +0,0 @@
1
- import * as std from 'std';
2
- import * as os from 'os';
3
- import { parseIni } from '../lib/cache-utils.js';
4
- function getCacheDir() {
5
- // import.meta.url: file:///C:/.../_build/test/file.js
6
- const url = import.meta.url;
7
- // Strip file:// (7 chars), giving /C:/.../
8
- let path = url.slice(7);
9
- // Remove leading / before drive letter
10
- if (path.length >= 3 && path[1] === ':')
11
- path = path.slice(1);
12
- // path now: C:/Users/Docker/quickwin/_build/test/file.js
13
- // Go up to _build/
14
- const idx = path.lastIndexOf('/');
15
- if (idx < 0)
16
- return '_cache';
17
- const buildDir = path.slice(0, idx); // .../_build/test
18
- const idx2 = buildDir.lastIndexOf('/');
19
- if (idx2 < 0)
20
- return '_cache';
21
- return buildDir.slice(0, idx2 + 1) + '_cache'; // .../_build/_cache
22
- }
23
- export const suite = {
24
- name: 'http-import',
25
- run: async (t) => {
26
- function assert(name, ok) {
27
- if (ok) {
28
- t.ok++;
29
- std.printf(' PASS: %s\n', name);
30
- }
31
- else {
32
- t.fail++;
33
- std.printf(' FAIL: %s\n', name);
34
- }
35
- }
36
- const url = 'https://esm.sh/left-pad@1.3.0';
37
- const cacheDir = getCacheDir();
38
- t.section('list cache before');
39
- const [before, err] = os.readdir(cacheDir);
40
- if (err !== 0) {
41
- std.printf(' cache dir: %s (error %d, will be created on first request)\n', cacheDir, err);
42
- }
43
- t.section('import module');
44
- let mod;
45
- try {
46
- mod = await import(url);
47
- }
48
- catch (e) {
49
- assert('import succeeds', false);
50
- std.printf(' ERROR: %s\n', String(e));
51
- return;
52
- }
53
- assert('import succeeds', true);
54
- assert('default is function', typeof mod.default === 'function');
55
- assert('left-pad works', mod.default('hello', 8) === ' hello');
56
- t.section('check cache');
57
- const [after, err2] = os.readdir(cacheDir);
58
- assert('cache dir readable', err2 === 0);
59
- if (err2 !== 0)
60
- return;
61
- assert('cache has body files', after.some(function (f) { return f.endsWith('.body'); }));
62
- assert('cache has meta files', after.some(function (f) { return f.endsWith('.meta'); }));
63
- t.section('repeat https import (fresh url, forces TLS)');
64
- // http_get_sync() short-circuits on a warm disk cache, so a second
65
- // import of the same URL would never build a TLS connection. Use a
66
- // distinct query string to force a real handshake into the
67
- // process-wide CA store, then import once more to confirm repeated
68
- // loadTrustedCerts() calls keep verifying.
69
- const url2 = 'https://esm.sh/left-pad@1.3.0?twice=1';
70
- let mod2;
71
- try {
72
- mod2 = await import(url2);
73
- assert('repeat import succeeds', true);
74
- assert('repeat default is function', typeof mod2.default === 'function');
75
- assert('repeat left-pad works', mod2.default('hello', 8) === ' hello');
76
- }
77
- catch (e) {
78
- assert('repeat import succeeds', false);
79
- std.printf(' ERROR: %s\n', String(e));
80
- }
81
- for (const f of after) {
82
- if (f.endsWith('.meta') && (err !== 0 || before.indexOf(f) < 0)) {
83
- std.printf(' meta: %s\n', f);
84
- const fp = std.open(cacheDir + '/' + f, 'r');
85
- if (fp) {
86
- const metaStr = fp.readAsString();
87
- fp.close();
88
- if (metaStr) {
89
- const ini = parseIni(metaStr);
90
- std.printf(' storedAt: %s, maxAge: %s\n', ini.meta.storedAt || '?', ini.meta.maxAge || '?');
91
- }
92
- }
93
- }
94
- }
95
- }
96
- };
@@ -1,93 +0,0 @@
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
-
60
- t.section('repeat https import (fresh url, forces TLS)')
61
- // http_get_sync() short-circuits on a warm disk cache, so a second
62
- // import of the same URL would never build a TLS connection. Use a
63
- // distinct query string to force a real handshake into the
64
- // process-wide CA store, then import once more to confirm repeated
65
- // loadTrustedCerts() calls keep verifying.
66
- const url2 = 'https://esm.sh/left-pad@1.3.0?twice=1'
67
- let mod2: any
68
- try {
69
- mod2 = await import(url2)
70
- assert('repeat import succeeds', true)
71
- assert('repeat default is function', typeof mod2.default === 'function')
72
- assert('repeat left-pad works', mod2.default('hello', 8) === ' hello')
73
- } catch (e) {
74
- assert('repeat import succeeds', false)
75
- std.printf(' ERROR: %s\n', String(e))
76
- }
77
-
78
- for (const f of after) {
79
- if (f.endsWith('.meta') && (err !== 0 || before.indexOf(f) < 0)) {
80
- std.printf(' meta: %s\n', f)
81
- const fp = std.open(cacheDir + '/' + f, 'r')
82
- if (fp) {
83
- const metaStr = fp.readAsString()
84
- fp.close()
85
- if (metaStr) {
86
- const ini = parseIni(metaStr)
87
- std.printf(' storedAt: %s, maxAge: %s\n', ini.meta.storedAt || '?', ini.meta.maxAge || '?')
88
- }
89
- }
90
- }
91
- }
92
- }
93
- }
@@ -1,214 +0,0 @@
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
- import '../lib/fetch.js';
7
- // ── Minimal raw HTTP client for testing ──
8
- function httpSend(host, port, raw, nResponses = 1) {
9
- return new Promise((resolve, reject) => {
10
- const s = sock.socket();
11
- if (s < 0) {
12
- reject(new Error('socket create failed'));
13
- return;
14
- }
15
- let buf = '';
16
- let done = false;
17
- const timer = os.setTimeout(() => {
18
- if (!done) {
19
- done = true;
20
- sock.closesocket(s);
21
- reject(new Error('timeout: ' + buf));
22
- }
23
- }, 10000);
24
- sock.set_on_event(s, (event) => {
25
- if (done)
26
- return;
27
- if (event.lNetworkEvents & 16 /* FdEvent.FD_CONNECT */) {
28
- const err = event.iErrorCode[0];
29
- if (err !== 0) {
30
- done = true;
31
- os.clearTimeout(timer);
32
- sock.closesocket(s);
33
- reject(new Error('connect error ' + err));
34
- return;
35
- }
36
- const bytes = new TextEncoder().encode(raw);
37
- sock.send(s, bytes.buffer);
38
- }
39
- if (event.lNetworkEvents & 1 /* FdEvent.FD_READ */) {
40
- while (true) {
41
- const d = sock.recv(s, 8192);
42
- if (typeof d === 'number')
43
- break;
44
- if (!d || d.byteLength === 0)
45
- break;
46
- buf += new TextDecoder().decode(new Uint8Array(d));
47
- }
48
- const count = (buf.match(/HTTP\/1\.1 /g) || []).length;
49
- if (count >= nResponses) {
50
- done = true;
51
- os.clearTimeout(timer);
52
- sock.closesocket(s);
53
- resolve(buf);
54
- }
55
- }
56
- if (event.lNetworkEvents & 32 /* FdEvent.FD_CLOSE */) {
57
- if (!done) {
58
- done = true;
59
- os.clearTimeout(timer);
60
- sock.closesocket(s);
61
- resolve(buf);
62
- }
63
- }
64
- });
65
- sock.connect(s, host, port);
66
- });
67
- }
68
- // ── Test suite ──
69
- export const suite = {
70
- name: 'http-server',
71
- run: async (t) => {
72
- const requests = [];
73
- const server = createServer(async (req) => {
74
- const url = new URL(req.url);
75
- const path = url.pathname;
76
- requests.push(req.method + ' ' + path);
77
- if (path === '/') {
78
- return new Response('hello ' + (url.searchParams.get('name') || ''), { headers: { 'Content-Type': 'text/plain' } });
79
- }
80
- else if (path === '/echo') {
81
- return new Response(req.method + ':' + await req.text(), { headers: { 'Content-Type': 'text/plain' } });
82
- }
83
- else if (path === '/json') {
84
- return new Response(JSON.stringify({ method: req.method, q: Object.fromEntries(url.searchParams) }), { headers: { 'Content-Type': 'application/json' } });
85
- }
86
- else if (path === '/stream') {
87
- // length unknown: server must send chunked frames
88
- return new Response(new ReadableStream({
89
- start(control) {
90
- control.enqueue(new TextEncoder().encode('chunkA/'));
91
- control.enqueue(new TextEncoder().encode('chunkB'));
92
- control.close();
93
- }
94
- }), { headers: { 'Content-Type': 'text/plain' } });
95
- }
96
- else if (path === '/file') {
97
- // length known: server streams raw bytes with Content-Length
98
- return new Response(new ReadableStream({
99
- start(control) {
100
- control.enqueue(new TextEncoder().encode('chunkA/'));
101
- control.enqueue(new TextEncoder().encode('chunkB'));
102
- control.close();
103
- }
104
- }), { headers: { 'Content-Type': 'text/plain', 'Content-Length': '13' } });
105
- }
106
- else if (path === '/stream-trailer') {
107
- // chunked stream with trailing headers (Node addTrailers
108
- // semantics: `Trailer` declaration + frames, then the footer)
109
- return new Response(new ReadableStream({
110
- start(control) {
111
- control.enqueue(new TextEncoder().encode('chunkA/'));
112
- control.enqueue(new TextEncoder().encode('chunkB'));
113
- control.close();
114
- }
115
- }), {
116
- headers: { 'Content-Type': 'text/plain', 'Trailer': 'X-Exit-Code' },
117
- trailers: Promise.resolve({ 'X-Exit-Code': '3' })
118
- });
119
- }
120
- else {
121
- return new Response('not found', { status: 404 });
122
- }
123
- });
124
- const rc = server.listen(0, '127.0.0.1');
125
- t.checkTrue('listen ok', rc === 0);
126
- const addr = server.address();
127
- if (!addr) {
128
- t.check('address', 'non-null', 'null');
129
- return;
130
- }
131
- t.checkTrue('bound port > 0', addr.port > 0);
132
- const port = addr.port;
133
- // ── GET with query ──
134
- t.section('GET with query');
135
- 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');
136
- t.checkTrue('200 status', r.indexOf('HTTP/1.1 200') === 0);
137
- t.checkTrue('body = hello world', r.endsWith('hello world'));
138
- t.checkTrue('content-type text', r.toLowerCase().indexOf('content-type: text/plain') >= 0);
139
- t.checkTrue('static body gets content-length', r.toLowerCase().indexOf('content-length: 11') >= 0);
140
- // ── streaming chunked response (length unknown) ──
141
- t.section('streaming chunked response');
142
- r = await httpSend('127.0.0.1', port, 'GET /stream HTTP/1.1\r\nHost: x\r\nConnection: close\r\n\r\n');
143
- t.checkTrue('200 status', r.indexOf('HTTP/1.1 200') === 0);
144
- t.checkTrue('transfer-encoding chunked', r.toLowerCase().indexOf('transfer-encoding: chunked') >= 0);
145
- t.checkTrue('no content-length', r.toLowerCase().indexOf('content-length:') < 0);
146
- t.checkTrue('chunk frames + terminator', r.indexOf('7\r\nchunkA/\r\n6\r\nchunkB\r\n0\r\n\r\n') >= 0);
147
- // ── streaming raw response with known length ──
148
- t.section('streaming known-length response');
149
- r = await httpSend('127.0.0.1', port, 'GET /file HTTP/1.1\r\nHost: x\r\nConnection: close\r\n\r\n');
150
- t.checkTrue('200 status', r.indexOf('HTTP/1.1 200') === 0);
151
- t.checkTrue('content-length 13', r.toLowerCase().indexOf('content-length: 13') >= 0);
152
- t.checkTrue('no chunked encoding', r.toLowerCase().indexOf('transfer-encoding: chunked') < 0);
153
- t.checkTrue('streamed raw body', r.includes('chunkA/chunkB'));
154
- t.checkTrue('no chunk frame headers', r.indexOf('7\r\nchunkA/') < 0);
155
- // ── HEAD of a streamed body: headers only, empty body ──
156
- t.section('HEAD streaming response');
157
- r = await httpSend('127.0.0.1', port, 'HEAD /stream HTTP/1.1\r\nHost: x\r\nConnection: close\r\n\r\n');
158
- t.checkTrue('HEAD 200', r.indexOf('HTTP/1.1 200') === 0);
159
- t.checkTrue('HEAD content-length 13', r.toLowerCase().indexOf('content-length: 13') >= 0);
160
- const headBodyIdx = r.indexOf('\r\n\r\n');
161
- t.checkTrue('HEAD empty body', r.slice(headBodyIdx + 4).length === 0);
162
- // ── streaming chunked response with trailing headers ──
163
- t.section('streaming trailer response');
164
- r = await httpSend('127.0.0.1', port, 'GET /stream-trailer HTTP/1.1\r\nHost: x\r\nConnection: close\r\n\r\n');
165
- t.checkTrue('200 status', r.indexOf('HTTP/1.1 200') === 0);
166
- t.checkTrue('trailer declared in head', r.toLowerCase().indexOf('trailer: x-exit-code') >= 0);
167
- t.checkTrue('terminator carries trailer', r.indexOf('0\r\nX-Exit-Code: 3\r\n\r\n') >= 0);
168
- t.checkTrue('trailer footer after body', r.indexOf('7\r\nchunkA/\r\n6\r\nchunkB\r\n0\r\nX-Exit-Code: 3\r\n\r\n') >= 0);
169
- // ── fetch client consumes chunked response with trailer footer ──
170
- t.section('fetch consumes trailer response');
171
- {
172
- const rr = await fetch(`http://127.0.0.1:${port}/stream-trailer`);
173
- t.checkTrue('status 200', rr.status === 200);
174
- t.checkTrue('body decoded without trailer', await rr.text() === 'chunkA/chunkB');
175
- }
176
- // ── POST with body ──
177
- t.section('POST with body');
178
- 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');
179
- t.checkTrue('echo body', r.endsWith('POST:payload'));
180
- // ── POST with chunked request body ──
181
- t.section('chunked request body');
182
- const chunkedRaw = 'POST /echo HTTP/1.1\r\nHost: x\r\nTransfer-Encoding: chunked\r\nConnection: close\r\n\r\n' +
183
- '3\r\nabc\r\n4\r\ndefg\r\n0\r\n\r\n';
184
- r = await httpSend('127.0.0.1', port, chunkedRaw);
185
- t.checkTrue('chunked decoded', r.endsWith('POST:abcdefg'));
186
- // ── 404 ──
187
- t.section('404');
188
- r = await httpSend('127.0.0.1', port, 'GET /missing HTTP/1.1\r\nHost: x\r\nConnection: close\r\n\r\n');
189
- t.checkTrue('404 status', r.indexOf('HTTP/1.1 404') === 0);
190
- t.checkTrue('404 body', r.endsWith('not found'));
191
- // ── HEAD (headers present, empty body) ──
192
- t.section('HEAD');
193
- r = await httpSend('127.0.0.1', port, 'HEAD / HTTP/1.1\r\nHost: x\r\nConnection: close\r\n\r\n');
194
- t.checkTrue('HEAD 200', r.indexOf('HTTP/1.1 200') === 0);
195
- t.checkTrue('HEAD content-length present', r.toLowerCase().indexOf('content-length: 6') >= 0);
196
- const bodyIdx = r.indexOf('\r\n\r\n');
197
- t.checkTrue('HEAD empty body', r.slice(bodyIdx + 4).length === 0);
198
- // ── keep-alive: two requests on one connection ──
199
- t.section('keep-alive pipelining');
200
- const pipelined = 'GET /?name=a HTTP/1.1\r\nHost: x\r\n\r\n' +
201
- 'GET /?name=b HTTP/1.1\r\nHost: x\r\nConnection: close\r\n\r\n';
202
- r = await httpSend('127.0.0.1', port, pipelined, 2);
203
- t.checkTrue('two responses received', r.indexOf('hello a') >= 0 && r.indexOf('hello b') >= 0);
204
- t.checkTrue('connection close on last', r.toLowerCase().indexOf('connection: close') >= 0);
205
- // ── server received expected requests ──
206
- t.section('request log');
207
- t.check('total requests', 12, requests.length);
208
- t.checkTrue('saw GET /', requests.indexOf('GET /') >= 0);
209
- t.checkTrue('saw POST /echo', requests.indexOf('POST /echo') >= 0);
210
- server.close();
211
- t.check('closed ok', true, true);
212
- std.printf(' (http-server suite done, %d requests)\n', requests.length);
213
- },
214
- };