quickwin 2.0.0 → 2.1.1

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 (103) hide show
  1. package/README.en.md +21 -26
  2. package/README.md +3 -6
  3. package/examples/exec_server.js +139 -31
  4. package/examples/exec_server.tsx +7 -8
  5. package/examples/exec_server_worker.js +1 -1
  6. package/examples/exec_server_worker.ts +2 -2
  7. package/examples/pdf_preview2.js +1 -1
  8. package/examples/pdf_preview2.ts +1 -1
  9. package/examples/pdf_viewer.js +132 -28
  10. package/examples/pdf_viewer.tsx +1 -1
  11. package/examples/test_gallery.js +131 -27
  12. package/examples/test_react_lazy.js +131 -27
  13. package/examples/test_react_listbox.js +131 -27
  14. package/examples/test_root_window.js +131 -27
  15. package/examples/test_tab.js +131 -27
  16. package/lib/certs.js +13 -8
  17. package/lib/certs.ts +13 -8
  18. package/lib/fetch.js +51 -3
  19. package/lib/fetch.ts +38 -3
  20. package/lib/ffi/bind.js +94 -5
  21. package/lib/ffi/bind.ts +107 -10
  22. package/lib/ffi/ctype.js +33 -7
  23. package/lib/ffi/ctype.ts +48 -10
  24. package/lib/ffi/struct.js +92 -14
  25. package/lib/ffi/struct.ts +108 -19
  26. package/lib/react-qw/components/ListView.js +1 -0
  27. package/lib/react-qw/components/PathPicker.js +2 -2
  28. package/lib/react-qw/index.js +1 -1
  29. package/lib/react-qw/index.ts +4 -1
  30. package/lib/text-measure.js +3 -1
  31. package/lib/text-measure.ts +4 -3
  32. package/lib/websocket.js +32 -2
  33. package/lib/websocket.ts +25 -2
  34. package/lib/windows/advapi32.js +53 -0
  35. package/lib/windows/advapi32.ts +64 -0
  36. package/lib/windows/comctl32.js +15 -15
  37. package/lib/windows/comctl32.ts +15 -16
  38. package/lib/windows/comdlg32.js +3 -2
  39. package/lib/windows/comdlg32.ts +3 -2
  40. package/lib/windows/gdi32.js +328 -26
  41. package/lib/windows/gdi32.ts +349 -26
  42. package/lib/windows/kernel32.js +206 -10
  43. package/lib/windows/kernel32.ts +225 -10
  44. package/lib/windows/msimg32.js +38 -0
  45. package/lib/windows/msimg32.ts +45 -0
  46. package/lib/windows/ntdll.js +3 -2
  47. package/lib/windows/ntdll.ts +3 -2
  48. package/lib/windows/ole32.js +1 -1
  49. package/lib/windows/ole32.ts +1 -1
  50. package/lib/windows/psapi.js +32 -0
  51. package/lib/windows/psapi.ts +37 -0
  52. package/lib/windows/shell32.js +5 -3
  53. package/lib/windows/shell32.ts +5 -3
  54. package/lib/windows/structs.js +135 -0
  55. package/lib/windows/structs.ts +144 -0
  56. package/lib/windows/user32.js +202 -99
  57. package/lib/windows/user32.ts +204 -99
  58. package/lib/windows/version.js +29 -0
  59. package/lib/windows/version.ts +35 -0
  60. package/lib/windows/winspool.js +71 -0
  61. package/lib/windows/winspool.ts +80 -0
  62. package/package.json +1 -1
  63. package/quickwin.d.ts +11 -5
  64. package/quickwin_const.d.ts +136 -0
  65. package/qwin-nowasm-x86.exe +0 -0
  66. package/qwin-nowasm.exe +0 -0
  67. package/qwin-x86.exe +0 -0
  68. package/qwin.exe +0 -0
  69. package/test/run.js +7 -0
  70. package/test/run.ts +7 -0
  71. package/test/test_basic_fetch.js +21 -0
  72. package/test/test_basic_fetch.ts +17 -0
  73. package/test/test_ffi.js +6 -6
  74. package/test/test_ffi.ts +6 -6
  75. package/test/test_ffi_abi.js +55 -0
  76. package/test/test_ffi_abi.ts +56 -0
  77. package/test/test_ffi_array.js +116 -0
  78. package/test/test_ffi_array.ts +141 -0
  79. package/test/test_ffi_bind.js +82 -0
  80. package/test/test_ffi_bind.ts +71 -0
  81. package/test/test_files.js +102 -0
  82. package/test/test_files.ts +121 -0
  83. package/test/test_gdi_draw.js +681 -0
  84. package/test/test_gdi_draw.ts +725 -0
  85. package/test/test_net_fetch.js +4 -3
  86. package/test/test_net_fetch.ts +4 -3
  87. package/test/test_printer.js +137 -0
  88. package/test/test_printer.ts +148 -0
  89. package/test/test_proc.js +141 -0
  90. package/test/test_proc.ts +163 -0
  91. package/test/test_react_complex.js +131 -27
  92. package/test/test_react_counter.js +131 -27
  93. package/test/test_react_flex.js +131 -27
  94. package/test/test_react_hidden.js +131 -27
  95. package/test/test_react_listview_link.js +131 -27
  96. package/test/test_react_notify.js +131 -27
  97. package/test/test_react_render.js +131 -27
  98. package/test/test_registry.js +74 -0
  99. package/test/test_registry.ts +95 -0
  100. package/test/test_sysinfo.js +122 -0
  101. package/test/test_sysinfo.ts +141 -0
  102. package/test/test_wolfssl_read.js +45 -44
  103. package/test/test_wolfssl_read.ts +42 -42
@@ -0,0 +1,141 @@
1
+ import * as ffi from 'ffi'
2
+ import * as os from 'os'
3
+ import { WCHAR } from '../lib/ffi/bind.js'
4
+ import { NULL, PtrArrayBuffer, type Ptr } from '../lib/ffi/ctype.js'
5
+ import {
6
+ FormatMessage, GetCommandLineW, GetComputerName, GetEnvironmentVariable,
7
+ GetLocalTime, GetModuleFileName, GetSystemDirectory, GetSystemTimeAsFileTime,
8
+ GlobalAlloc, GlobalLock, GlobalUnlock, QueryPerformanceCounter,
9
+ QueryPerformanceFrequency, RtlMoveMemory, Sleep,
10
+ } from '../lib/windows/kernel32.js'
11
+ import {
12
+ CloseClipboard, EmptyClipboard, GetClipboardData, OpenClipboard,
13
+ RegisterClipboardFormat, SetClipboardData,
14
+ } from '../lib/windows/user32.js'
15
+ import {
16
+ GetFileVersionInfoSize, GetFileVersionInfo, VerQueryValue,
17
+ } from '../lib/windows/version.js'
18
+ import { SYSTEMTIME } from '../lib/windows/structs.js'
19
+ import { Tester } from './test_helper.js'
20
+
21
+ // 进程内存跟进读:ffi 只有 readByte(无 DataView/写通道),u32 小端拼值 / 指针随位宽
22
+ // (| 是有符号 32 位运算,>>> 0 归一无符号——0xFEEF04BD 类高位值否则读成负数)
23
+ function readU32At(addr: number): number {
24
+ return (ffi.readByte(addr) | (ffi.readByte(addr + 1) << 8) |
25
+ (ffi.readByte(addr + 2) << 16) | (ffi.readByte(addr + 3) << 24)) >>> 0
26
+ }
27
+ function readPtr(dv: DataView, offset: number): number {
28
+ if (os.arch === 'ia32') return dv.getUint32(offset, true)
29
+ return dv.getUint32(offset, true) + dv.getUint32(offset + 4, true) * 4294967296
30
+ }
31
+
32
+ export const suite = {
33
+ name: 'sysinfo',
34
+ run: (t: Tester) => {
35
+ t.section('QueryPerformanceCounter: frequency + monotonic')
36
+ const freqB = new PtrArrayBuffer(8)
37
+ t.checkTrue('QueryPerformanceFrequency ok', QueryPerformanceFrequency(freqB) !== 0)
38
+ const freq = new DataView(freqB).getBigInt64(0, true)
39
+ t.checkTrue(`frequency sane (${freq})`, freq > 0n)
40
+ const c1 = new PtrArrayBuffer(8)
41
+ const c2 = new PtrArrayBuffer(8)
42
+ QueryPerformanceCounter(c1)
43
+ QueryPerformanceCounter(c2)
44
+ const v1 = new DataView(c1).getBigInt64(0, true)
45
+ const v2 = new DataView(c2).getBigInt64(0, true)
46
+ t.checkTrue(`counter monotonic (${v1} <= ${v2})`, v1 <= v2)
47
+
48
+ t.section('Sleep: QPC-measured elapsed lands in interval')
49
+ QueryPerformanceCounter(c1)
50
+ Sleep(50)
51
+ QueryPerformanceCounter(c2)
52
+ const elapsedMs = Number((new DataView(c2).getBigInt64(0, true) -
53
+ new DataView(c1).getBigInt64(0, true)) * 1000n / freq)
54
+ t.checkTrue(`Sleep(50) -> ${elapsedMs}ms in [30, 3000)`, elapsedMs >= 30 && elapsedMs < 3000)
55
+
56
+ t.section('GetLocalTime / GetSystemTimeAsFileTime')
57
+ const st = SYSTEMTIME.alloc()
58
+ GetLocalTime(st.ptr)
59
+ const got = SYSTEMTIME.decode(st)
60
+ t.checkTrue(`year sane (${got.wYear})`, got.wYear >= 2024 && got.wYear <= 2100)
61
+ t.checkTrue(`month sane (${got.wMonth})`, got.wMonth >= 1 && got.wMonth <= 12)
62
+ const ftB = new PtrArrayBuffer(8)
63
+ GetSystemTimeAsFileTime(ftB)
64
+ const ft = new DataView(ftB).getBigUint64(0, true)
65
+ t.checkTrue(`FILETIME > 0 (${ft})`, ft > 0n)
66
+
67
+ t.section('env / computer name / module path')
68
+ const pathW = WCHAR.alloc(4096)
69
+ const pathLen = GetEnvironmentVariable('PATH', pathW, 4096)
70
+ t.checkTrue(`PATH non-empty (${pathLen} chars)`, pathLen > 0)
71
+ const nameW = WCHAR.alloc(260)
72
+ const nameLenB = new PtrArrayBuffer(4)
73
+ new Uint32Array(nameLenB)[0] = 260
74
+ t.checkTrue('GetComputerName ok', GetComputerName(nameW, nameLenB) !== 0)
75
+ const compName = WCHAR.decode(nameW)
76
+ t.checkTrue(`computer name non-empty (${compName})`, compName.length > 0)
77
+ const modW = WCHAR.alloc(520)
78
+ const modLen = GetModuleFileName(NULL, modW, 520)
79
+ const modPath = WCHAR.decode(modW)
80
+ t.checkTrue(`exe path has qwin (${modLen > 0})`, /qwin/i.test(modPath))
81
+ // 生产绑定的返回位 `<WCHAR>ptr!`:内建 WCHAR 自动解码 + `!` 非空担保(直接 string)
82
+ const cmdLine = GetCommandLineW()
83
+ t.checkTrue(`GetCommandLineW decodes to non-empty string (${cmdLine.length} chars)`,
84
+ typeof cmdLine === 'string' && cmdLine.length > 0)
85
+
86
+ t.section('FormatMessage: error 2 -> readable text')
87
+ const msgW = WCHAR.alloc(512)
88
+ const msgLen = FormatMessage(0x1200 /* FROM_SYSTEM | IGNORE_INSERTS */,
89
+ NULL, 2 /* ERROR_FILE_NOT_FOUND */, 0, msgW, 512, NULL)
90
+ // 语言无关断言(VM 中/英文系统都可过):错误 2 的系统文字必然 > 4 字符
91
+ t.checkTrue(`error 2 message non-empty (${msgLen} chars)`, msgLen > 4)
92
+
93
+ t.section('clipboard roundtrip: CF_UNICODETEXT (GlobalAlloc/RtlMoveMemory)')
94
+ t.checkTrue('OpenClipboard', OpenClipboard(NULL) !== 0)
95
+ t.checkTrue('EmptyClipboard', EmptyClipboard() !== 0)
96
+ const clipText = 'QuickWin clip roundtrip 你好'
97
+ const clipSrc = WCHAR.encode(clipText) // 自带 NUL 终止
98
+ const hMem = GlobalAlloc(0x2 /* GMEM_MOVEABLE */, clipSrc.byteLength)
99
+ t.checkTrue(`GlobalAlloc valid (${hMem})`, hMem !== 0)
100
+ const clipDst = GlobalLock(hMem)
101
+ t.checkTrue(`GlobalLock address (${clipDst})`, clipDst !== 0)
102
+ RtlMoveMemory(clipDst, clipSrc, clipSrc.byteLength)
103
+ GlobalUnlock(hMem)
104
+ const setR = SetClipboardData(13 /* CF_UNICODETEXT */, hMem)
105
+ t.checkTrue('SetClipboardData (ownership -> system)', setR !== 0)
106
+ CloseClipboard()
107
+ t.checkTrue('OpenClipboard (read back)', OpenClipboard(NULL) !== 0)
108
+ const hRead = GetClipboardData(13)
109
+ t.checkTrue('GetClipboardData handle', hRead !== 0)
110
+ const pRead = hRead ? GlobalLock(hRead) : 0
111
+ const back = pRead ? WCHAR.decode(pRead as Ptr<'WCHAR'>) : ''
112
+ if (pRead) GlobalUnlock(hRead)
113
+ CloseClipboard()
114
+ t.check('clip text roundtrip', clipText, back)
115
+ t.checkTrue('RegisterClipboardFormat', RegisterClipboardFormat('QuickWinClipFmt') !== 0)
116
+
117
+ t.section('version resource of kernel32.dll')
118
+ const sysW = WCHAR.alloc(300)
119
+ GetSystemDirectory(sysW, 300) // 尾不带反斜杠
120
+ const k32Path = `${WCHAR.decode(sysW)}\\kernel32.dll`
121
+ const hB = new PtrArrayBuffer(4)
122
+ const vSize = GetFileVersionInfoSize(k32Path, hB)
123
+ t.checkTrue(`version size > 0 (${vSize})`, vSize > 0)
124
+ if (vSize > 0) {
125
+ const vHandle = new Uint32Array(hB)[0]!
126
+ const vBuf = new PtrArrayBuffer(vSize)
127
+ t.checkTrue('GetFileVersionInfo', GetFileVersionInfo(k32Path, vHandle, vSize, vBuf) !== 0)
128
+ const pBuf = new PtrArrayBuffer(8)
129
+ const pLen = new PtrArrayBuffer(4)
130
+ t.checkTrue('VerQueryValue root block', VerQueryValue(vBuf, '\\', pBuf, pLen) !== 0)
131
+ const rootAddr = readPtr(new DataView(pBuf), 0)
132
+ const rootLen = new Uint32Array(pLen)[0]
133
+ t.checkTrue(`VS_FIXEDFILEINFO len >= 52 (${rootLen})`, rootLen! >= 52)
134
+ t.check('VS_FIXEDFILEINFO signature', 0xfeef04bd, readU32At(rootAddr))
135
+ const fms = readU32At(rootAddr + 8)
136
+ const fls = readU32At(rootAddr + 12)
137
+ const ver = `${fms >>> 16}.${fms & 0xffff}.${fls >>> 16}.${fls & 0xffff}`
138
+ t.checkTrue(`file version sane (${ver})`, fms !== 0 || fls !== 0)
139
+ }
140
+ },
141
+ }
@@ -19,7 +19,8 @@ export const suite = {
19
19
  std.printf(' FAIL: %s\n', name);
20
20
  }
21
21
  }
22
- // ── wolfSSL_read return contract: ArrayBuffer | null, never number ──
22
+ // ── wolfSSL_read return contract: raw int, data lands in the
23
+ // caller-owned buffer (thin wrapper — errors stay numbers) ──
23
24
  t.section('wolfSSL_read contract (HTTPS /large/65536)');
24
25
  await new Promise((resolve) => {
25
26
  const s = sock.socket();
@@ -35,10 +36,9 @@ export const suite = {
35
36
  let sentRequest = false;
36
37
  let total = 0;
37
38
  const EXPECTED = 65536;
38
- let sawNumber = false;
39
- let sawArray = false;
40
- let sawNull = false;
41
- let nullErrOk = true;
39
+ let sawNonNumber = false;
40
+ let sawEof = false;
41
+ let errOk = true;
42
42
  let done = false;
43
43
  const finish = () => {
44
44
  if (done)
@@ -48,10 +48,10 @@ export const suite = {
48
48
  assert('connected', connected);
49
49
  assert('handshake ok', handshakeDone);
50
50
  assert('request sent', sentRequest);
51
- assert('received some data', sawArray && total > 0);
52
- assert('wolfSSL_read never returned number', !sawNumber);
53
- assert('wolfSSL_read returned null on no-data/EOF', sawNull);
54
- assert('null path get_error in {WANT_READ,WANT_WRITE,ZERO_RETURN}', nullErrOk);
51
+ assert('received full body', total >= EXPECTED);
52
+ assert('wolfSSL_read always returned number', !sawNonNumber);
53
+ assert('no-data/EOF observed via int (0 or ZERO_RETURN)', sawEof);
54
+ assert('n<=0 get_error in {WANT_READ,WANT_WRITE,ZERO_RETURN}', errOk);
55
55
  if (ssl) {
56
56
  wolfssl.wolfSSL_free(ssl);
57
57
  ssl = null;
@@ -77,39 +77,39 @@ export const suite = {
77
77
  sentRequest = true;
78
78
  };
79
79
  const onReadable = () => {
80
- // Drain until content complete or null
80
+ // Drain until EOF (n===0 / ZERO_RETURN) or WANT_* hands back
81
+ // to the event loop.
81
82
  for (let guard = 0; guard < 10000; guard++) {
82
- const d = wolfssl.wolfSSL_read(ssl, 8192);
83
- if (d === null) {
84
- sawNull = true;
85
- const err = wolfssl.wolfSSL_get_error(ssl, -1);
86
- if (err !== WANT_READ && err !== WANT_WRITE && err !== ZERO_RETURN) {
87
- nullErrOk = false;
88
- std.printf(' null get_error=%d\n', err);
89
- }
90
- if (err === ZERO_RETURN || err === 1 || err === 5) {
91
- // EOF / fatal — done draining
92
- break;
93
- }
94
- // WANT_*: wait for next FD_READ/WRITE
95
- return;
96
- }
97
- if (typeof d === 'number') {
98
- // Pre-fix C API returns number 0/-1 — record and stop
99
- sawNumber = true;
100
- std.printf(' FAIL got number %d (want ArrayBuffer|null)\n', d);
83
+ const buf = new ArrayBuffer(8192);
84
+ const n = wolfssl.wolfSSL_read(ssl, buf);
85
+ if (typeof n !== 'number') {
86
+ sawNonNumber = true;
87
+ std.printf(' FAIL got non-number %s (want int)\n', String(n));
101
88
  finish();
102
89
  return;
103
90
  }
104
- if (d instanceof ArrayBuffer) {
105
- sawArray = true;
106
- total += d.byteLength;
91
+ if (n > 0) {
92
+ total += n;
107
93
  // Keep draining after the body is complete: the point of
108
94
  // this suite is the no-data/EOF contract, so a full body
109
- // must not short-circuit the read that would observe null.
95
+ // must not short-circuit the read that would observe it.
96
+ continue;
97
+ }
98
+ const err = wolfssl.wolfSSL_get_error(ssl, n);
99
+ if (err !== WANT_READ && err !== WANT_WRITE && err !== ZERO_RETURN) {
100
+ errOk = false;
101
+ std.printf(' n=%d get_error=%d outside {WANT_READ,WANT_WRITE,ZERO_RETURN}\n', n, err);
102
+ finish();
103
+ return;
110
104
  }
105
+ if (n === 0 || err === ZERO_RETURN) {
106
+ sawEof = true;
107
+ break;
108
+ }
109
+ // WANT_*: wait for next FD_READ/WRITE
110
+ return;
111
111
  }
112
- if (total >= EXPECTED)
112
+ if (sawEof)
113
113
  finish();
114
114
  };
115
115
  sock.set_on_event(s, (event) => {
@@ -178,21 +178,22 @@ export const suite = {
178
178
  }
179
179
  if (event.lNetworkEvents & 32 /* sock.FdEvent.FD_CLOSE */) {
180
180
  // drain remaining then finish
181
- if (ssl && sentRequest && !sawNumber) {
181
+ if (ssl && sentRequest && !sawNonNumber) {
182
182
  onReadable();
183
183
  }
184
- if (!sawNumber) {
185
- // Even if short, require null contract seen OR full body
186
- if (!sawNull && total < EXPECTED && total > 0) {
187
- // got data but no null — still OK if full body; if short, null expected
188
- sawNull = true; // server close without extra null read is edge; don't fail
184
+ if (!sawNonNumber) {
185
+ // Server close without an observed EOF read is an edge —
186
+ // don't fail if the body already completed.
187
+ if (!sawEof && total < EXPECTED && total > 0) {
188
+ sawEof = true;
189
189
  }
190
- if (total >= EXPECTED || sawNull)
190
+ if (total >= EXPECTED || sawEof)
191
191
  finish();
192
- else
192
+ else {
193
193
  assert('data before close', false);
194
- if (!done)
195
- finish();
194
+ if (!done)
195
+ finish();
196
+ }
196
197
  }
197
198
  }
198
199
  });
@@ -17,7 +17,8 @@ export const suite = {
17
17
  else { t.fail++; std.printf(' FAIL: %s\n', name) }
18
18
  }
19
19
 
20
- // ── wolfSSL_read return contract: ArrayBuffer | null, never number ──
20
+ // ── wolfSSL_read return contract: raw int, data lands in the
21
+ // caller-owned buffer (thin wrapper — errors stay numbers) ──
21
22
  t.section('wolfSSL_read contract (HTTPS /large/65536)')
22
23
  await new Promise<void>((resolve) => {
23
24
  const s = sock.socket()
@@ -34,10 +35,9 @@ export const suite = {
34
35
  let sentRequest = false
35
36
  let total = 0
36
37
  const EXPECTED = 65536
37
- let sawNumber = false
38
- let sawArray = false
39
- let sawNull = false
40
- let nullErrOk = true
38
+ let sawNonNumber = false
39
+ let sawEof = false
40
+ let errOk = true
41
41
  let done = false
42
42
 
43
43
  const finish = () => {
@@ -47,10 +47,10 @@ export const suite = {
47
47
  assert('connected', connected)
48
48
  assert('handshake ok', handshakeDone)
49
49
  assert('request sent', sentRequest)
50
- assert('received some data', sawArray && total > 0)
51
- assert('wolfSSL_read never returned number', !sawNumber)
52
- assert('wolfSSL_read returned null on no-data/EOF', sawNull)
53
- assert('null path get_error in {WANT_READ,WANT_WRITE,ZERO_RETURN}', nullErrOk)
50
+ assert('received full body', total >= EXPECTED)
51
+ assert('wolfSSL_read always returned number', !sawNonNumber)
52
+ assert('no-data/EOF observed via int (0 or ZERO_RETURN)', sawEof)
53
+ assert('n<=0 get_error in {WANT_READ,WANT_WRITE,ZERO_RETURN}', errOk)
54
54
  if (ssl) { wolfssl.wolfSSL_free(ssl); ssl = null }
55
55
  if (ctx) { wolfssl.wolfSSL_CTX_free(ctx); ctx = null }
56
56
  sock.closesocket(s)
@@ -72,39 +72,39 @@ export const suite = {
72
72
  }
73
73
 
74
74
  const onReadable = () => {
75
- // Drain until content complete or null
75
+ // Drain until EOF (n===0 / ZERO_RETURN) or WANT_* hands back
76
+ // to the event loop.
76
77
  for (let guard = 0; guard < 10000; guard++) {
77
- const d = wolfssl.wolfSSL_read(ssl!, 8192)
78
- if (d === null) {
79
- sawNull = true
80
- const err = wolfssl.wolfSSL_get_error(ssl!, -1)
81
- if (err !== WANT_READ && err !== WANT_WRITE && err !== ZERO_RETURN) {
82
- nullErrOk = false
83
- std.printf(' null get_error=%d\n', err)
84
- }
85
- if (err === ZERO_RETURN || err === 1 || err === 5) {
86
- // EOF / fatal — done draining
87
- break
88
- }
89
- // WANT_*: wait for next FD_READ/WRITE
90
- return
91
- }
92
- if (typeof d === 'number') {
93
- // Pre-fix C API returns number 0/-1 — record and stop
94
- sawNumber = true
95
- std.printf(' FAIL got number %d (want ArrayBuffer|null)\n', d)
78
+ const buf = new ArrayBuffer(8192)
79
+ const n = wolfssl.wolfSSL_read(ssl!, buf)
80
+ if (typeof n !== 'number') {
81
+ sawNonNumber = true
82
+ std.printf(' FAIL got non-number %s (want int)\n', String(n))
96
83
  finish()
97
84
  return
98
85
  }
99
- if (d instanceof ArrayBuffer) {
100
- sawArray = true
101
- total += d.byteLength
86
+ if (n > 0) {
87
+ total += n
102
88
  // Keep draining after the body is complete: the point of
103
89
  // this suite is the no-data/EOF contract, so a full body
104
- // must not short-circuit the read that would observe null.
90
+ // must not short-circuit the read that would observe it.
91
+ continue
92
+ }
93
+ const err = wolfssl.wolfSSL_get_error(ssl!, n)
94
+ if (err !== WANT_READ && err !== WANT_WRITE && err !== ZERO_RETURN) {
95
+ errOk = false
96
+ std.printf(' n=%d get_error=%d outside {WANT_READ,WANT_WRITE,ZERO_RETURN}\n', n, err)
97
+ finish()
98
+ return
99
+ }
100
+ if (n === 0 || err === ZERO_RETURN) {
101
+ sawEof = true
102
+ break
105
103
  }
104
+ // WANT_*: wait for next FD_READ/WRITE
105
+ return
106
106
  }
107
- if (total >= EXPECTED) finish()
107
+ if (sawEof) finish()
108
108
  }
109
109
 
110
110
  sock.set_on_event(s, (event: { lNetworkEvents: number; iErrorCode: number[] }) => {
@@ -163,17 +163,17 @@ export const suite = {
163
163
 
164
164
  if (event.lNetworkEvents & sock.FdEvent.FD_CLOSE) {
165
165
  // drain remaining then finish
166
- if (ssl && sentRequest && !sawNumber) {
166
+ if (ssl && sentRequest && !sawNonNumber) {
167
167
  onReadable()
168
168
  }
169
- if (!sawNumber) {
170
- // Even if short, require null contract seen OR full body
171
- if (!sawNull && total < EXPECTED && total > 0) {
172
- // got data but no null — still OK if full body; if short, null expected
173
- sawNull = true // server close without extra null read is edge; don't fail
169
+ if (!sawNonNumber) {
170
+ // Server close without an observed EOF read is an edge —
171
+ // don't fail if the body already completed.
172
+ if (!sawEof && total < EXPECTED && total > 0) {
173
+ sawEof = true
174
174
  }
175
- if (total >= EXPECTED || sawNull) finish()
176
- else assert('data before close', false); if (!done) finish()
175
+ if (total >= EXPECTED || sawEof) finish()
176
+ else { assert('data before close', false); if (!done) finish() }
177
177
  }
178
178
  }
179
179
  })