xbintsc 0.6.22 → 0.6.24

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 (191) hide show
  1. package/AGENTS.md +5 -0
  2. package/dist/src/cli/help.d.ts +1 -1
  3. package/dist/src/driver/build-key.js +2 -1
  4. package/dist/src/driver/build-key.js.map +1 -1
  5. package/dist/src/driver/bundler/concatenate.d.ts +23 -0
  6. package/dist/src/driver/bundler/concatenate.js +210 -0
  7. package/dist/src/driver/bundler/concatenate.js.map +1 -0
  8. package/dist/src/driver/bundler/merge.d.ts +4 -4
  9. package/dist/src/driver/bundler/merge.js +24 -207
  10. package/dist/src/driver/bundler/merge.js.map +1 -1
  11. package/dist/src/driver/bundler/{namespace-import.d.ts → namespace-object.d.ts} +5 -1
  12. package/dist/src/driver/bundler/{namespace-import.js → namespace-object.js} +6 -2
  13. package/dist/src/driver/bundler/namespace-object.js.map +1 -0
  14. package/dist/src/driver/bundler/{reexport-fixup.d.ts → reexport-resolution.d.ts} +9 -3
  15. package/dist/src/driver/bundler/{reexport-fixup.js → reexport-resolution.js} +16 -6
  16. package/dist/src/driver/bundler/reexport-resolution.js.map +1 -0
  17. package/dist/src/driver/fingerprint.d.ts +11 -0
  18. package/dist/src/driver/fingerprint.js +25 -0
  19. package/dist/src/driver/fingerprint.js.map +1 -1
  20. package/dist/src/driver/link-features.js +36 -11
  21. package/dist/src/driver/link-features.js.map +1 -1
  22. package/dist/src/driver/modules.d.ts +12 -5
  23. package/dist/src/driver/modules.js +12 -5
  24. package/dist/src/driver/modules.js.map +1 -1
  25. package/dist/src/driver/paths.d.ts +10 -0
  26. package/dist/src/driver/paths.js +18 -0
  27. package/dist/src/driver/paths.js.map +1 -1
  28. package/dist/src/driver/runtime-objects.d.ts +1 -1
  29. package/dist/src/driver/runtime-objects.js +22 -6
  30. package/dist/src/driver/runtime-objects.js.map +1 -1
  31. package/dist/src/driver/toolchain.d.ts +1 -1
  32. package/dist/src/driver/toolchain.js +10 -1
  33. package/dist/src/driver/toolchain.js.map +1 -1
  34. package/dist/src/driver/version.d.ts +1 -1
  35. package/dist/src/driver/version.js +1 -1
  36. package/dist/src/extensions/node/http2/feature.d.ts +14 -0
  37. package/dist/src/extensions/node/http2/feature.js +28 -0
  38. package/dist/src/extensions/node/http2/feature.js.map +1 -0
  39. package/dist/src/extensions/node/http2/index.d.ts +5 -1
  40. package/dist/src/extensions/node/http2/index.js +6 -1
  41. package/dist/src/extensions/node/http2/index.js.map +1 -1
  42. package/dist/src/extensions/node/index.js +3 -1
  43. package/dist/src/extensions/node/index.js.map +1 -1
  44. package/dist/src/extensions/node/tls/feature.d.ts +12 -0
  45. package/dist/src/extensions/node/tls/feature.js +24 -0
  46. package/dist/src/extensions/node/tls/feature.js.map +1 -0
  47. package/dist/src/extensions/node/tls/index.d.ts +5 -1
  48. package/dist/src/extensions/node/tls/index.js +5 -1
  49. package/dist/src/extensions/node/tls/index.js.map +1 -1
  50. package/dist/src/extensions/node/zlib/compression.js +11 -6
  51. package/dist/src/extensions/node/zlib/compression.js.map +1 -1
  52. package/dist/src/extensions/registry.js +2 -1
  53. package/dist/src/extensions/registry.js.map +1 -1
  54. package/dist/src/extensions/storage.js +4 -3
  55. package/dist/src/extensions/storage.js.map +1 -1
  56. package/dist/src/extensions/tls.d.ts +26 -0
  57. package/dist/src/extensions/tls.js +46 -0
  58. package/dist/src/extensions/tls.js.map +1 -0
  59. package/dist/tests/driver/deferred-reexport.test.js +25 -0
  60. package/dist/tests/driver/deferred-reexport.test.js.map +1 -1
  61. package/dist/tests/driver/link-features.test.js +9 -0
  62. package/dist/tests/driver/link-features.test.js.map +1 -1
  63. package/dist/tests/e2e/fetch.test.js +3 -1
  64. package/dist/tests/e2e/fetch.test.js.map +1 -1
  65. package/dist/tests/e2e/http2.test.d.ts +7 -0
  66. package/dist/tests/e2e/http2.test.js +201 -0
  67. package/dist/tests/e2e/http2.test.js.map +1 -0
  68. package/dist/tests/e2e/net-helpers.d.ts +28 -0
  69. package/dist/tests/e2e/net-helpers.js +59 -0
  70. package/dist/tests/e2e/net-helpers.js.map +1 -0
  71. package/dist/tests/e2e/node-modules.test.js +3 -5
  72. package/dist/tests/e2e/node-modules.test.js.map +1 -1
  73. package/dist/tests/e2e/tls.test.d.ts +10 -0
  74. package/dist/tests/e2e/tls.test.js +197 -0
  75. package/dist/tests/e2e/tls.test.js.map +1 -0
  76. package/dist/tests/extensions/storage.test.js +3 -2
  77. package/dist/tests/extensions/storage.test.js.map +1 -1
  78. package/doc/ai/contributing.md +14 -0
  79. package/doc/ai/extensions.md +5 -3
  80. package/doc/ai/language-support.md +1 -1
  81. package/doc/ai/zh-CN/contributing.md +12 -0
  82. package/doc/ai/zh-CN/extensions.md +4 -2
  83. package/doc/ai/zh-CN/language-support.md +1 -1
  84. package/doc/gui/api.md +1 -1
  85. package/doc/gui/build.md +5 -3
  86. package/doc/gui/rendering.md +1 -1
  87. package/doc/gui.md +1 -1
  88. package/doc/implemented/async.md +1 -1
  89. package/doc/implemented/extensions.md +1 -0
  90. package/doc/implemented/fetch.md +19 -4
  91. package/doc/implemented/node/http2.md +51 -0
  92. package/doc/implemented/node/tls.md +56 -0
  93. package/doc/implemented/node.md +8 -0
  94. package/doc/implemented/storage.md +2 -1
  95. package/doc/implemented.md +2 -2
  96. package/doc/unimplemented/node/modules.md +6 -6
  97. package/doc/unimplemented/toolchain.md +1 -1
  98. package/doc/zh-CN/gui/api.md +1 -1
  99. package/doc/zh-CN/gui/build.md +4 -2
  100. package/doc/zh-CN/gui/rendering.md +1 -1
  101. package/doc/zh-CN/gui.md +1 -1
  102. package/doc/zh-CN/implemented/async.md +1 -1
  103. package/doc/zh-CN/implemented/extensions.md +1 -0
  104. package/doc/zh-CN/implemented/fetch.md +15 -3
  105. package/doc/zh-CN/implemented/node/http2.md +44 -0
  106. package/doc/zh-CN/implemented/node/tls.md +47 -0
  107. package/doc/zh-CN/implemented/node.md +7 -0
  108. package/doc/zh-CN/implemented/storage.md +2 -1
  109. package/doc/zh-CN/implemented.md +2 -2
  110. package/doc/zh-CN/unimplemented/node/modules.md +3 -3
  111. package/doc/zh-CN/unimplemented/toolchain.md +1 -1
  112. package/package.json +16 -8
  113. package/runtime/ext_node/http2/http2.c +25 -11
  114. package/runtime/ext_node/http2/http2_backend.c +48 -0
  115. package/runtime/ext_node/http2/http2_bridge.c +25 -0
  116. package/runtime/ext_node/http2/parts/api.inc +40 -0
  117. package/runtime/ext_node/http2/parts/client.inc +215 -0
  118. package/runtime/ext_node/http2/parts/common.inc +330 -0
  119. package/runtime/ext_node/http2/parts/events.inc +139 -0
  120. package/runtime/ext_node/http2/parts/server.inc +301 -0
  121. package/runtime/ext_node/http2/parts/streams.inc +254 -0
  122. package/runtime/ext_node/tls/parts/api.inc +38 -0
  123. package/runtime/ext_node/tls/parts/connect.inc +107 -0
  124. package/runtime/ext_node/tls/parts/helpers.inc +121 -0
  125. package/runtime/ext_node/tls/parts/server.inc +101 -0
  126. package/runtime/ext_node/tls/parts/socket.inc +171 -0
  127. package/runtime/ext_node/tls/tls.c +21 -13
  128. package/runtime/ext_node/tls/tls_backend.c +50 -0
  129. package/runtime/ext_node/zlib/{miniz/compression_bridge.c → compression_bridge.c} +1 -1
  130. package/runtime/xt_fetch/async.inc +19 -12
  131. package/runtime/xt_fetch/socket.inc +55 -0
  132. package/runtime/xt_fetch.c +3 -0
  133. package/runtime/xt_http2/nghttp2_compat.h +19 -0
  134. package/runtime/xt_http2/nghttp2_static.h +18 -0
  135. package/runtime/xt_http2_api.h +38 -0
  136. package/runtime/xt_node_tls_api.h +37 -0
  137. package/runtime/xt_protocol_registry.c +22 -0
  138. package/runtime/xt_storage/storage_internal.h +3 -3
  139. package/runtime/xt_tls/parts/api.inc +21 -0
  140. package/runtime/xt_tls/parts/client.inc +112 -0
  141. package/runtime/xt_tls/parts/io.inc +157 -0
  142. package/runtime/xt_tls/parts/server.inc +102 -0
  143. package/runtime/xt_tls/parts/setup.inc +146 -0
  144. package/runtime/xt_tls/tls_bridge.c +27 -0
  145. package/runtime/xt_tls/tls_core.c +39 -0
  146. package/runtime/xt_tls/tls_internal.h +73 -0
  147. package/runtime/xt_tls_api.h +73 -0
  148. package/scripts/build-gui.ts +8 -7
  149. package/scripts/build-tls.ts +248 -0
  150. package/scripts/check-file-length.ts +4 -25
  151. package/scripts/fetch-vendored.ts +244 -0
  152. package/scripts/package-release.ts +13 -3
  153. package/src/driver/build-key.ts +7 -1
  154. package/src/driver/bundler/README.md +55 -0
  155. package/src/driver/bundler/concatenate.ts +244 -0
  156. package/src/driver/bundler/merge.ts +24 -215
  157. package/src/driver/bundler/{namespace-import.ts → namespace-object.ts} +5 -1
  158. package/src/driver/bundler/{reexport-fixup.ts → reexport-resolution.ts} +15 -5
  159. package/src/driver/fingerprint.ts +24 -0
  160. package/src/driver/link-features.ts +33 -11
  161. package/src/driver/modules.ts +12 -5
  162. package/src/driver/paths.ts +20 -0
  163. package/src/driver/runtime-objects.ts +25 -6
  164. package/src/driver/toolchain.ts +11 -2
  165. package/src/driver/version.ts +1 -1
  166. package/src/extensions/node/http2/feature.ts +33 -0
  167. package/src/extensions/node/http2/index.ts +6 -1
  168. package/src/extensions/node/index.ts +3 -1
  169. package/src/extensions/node/tls/feature.ts +29 -0
  170. package/src/extensions/node/tls/index.ts +5 -1
  171. package/src/extensions/node/zlib/compression.ts +11 -6
  172. package/src/extensions/registry.ts +2 -1
  173. package/src/extensions/storage.ts +4 -3
  174. package/src/extensions/tls.ts +52 -0
  175. package/{runtime/ext_gui → vendor/stb}/stb_image.h +17 -1
  176. package/dist/src/driver/bundler/namespace-import.js.map +0 -1
  177. package/dist/src/driver/bundler/reexport-fixup.js.map +0 -1
  178. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz.c +0 -0
  179. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz.h +0 -0
  180. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_common.h +0 -0
  181. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_export.h +0 -0
  182. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_tdef.c +0 -0
  183. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_tdef.h +0 -0
  184. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_tinfl.c +0 -0
  185. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_tinfl.h +0 -0
  186. /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_zip.h +0 -0
  187. /package/{runtime/ext_gui → vendor/nanosvg}/nanosvg.h +0 -0
  188. /package/{runtime/ext_gui → vendor/nanosvg}/nanosvgrast.h +0 -0
  189. /package/{runtime/xt_storage → vendor}/sqlite3/sqlite3.c +0 -0
  190. /package/{runtime/xt_storage → vendor}/sqlite3/sqlite3.h +0 -0
  191. /package/{runtime/ext_gui → vendor/stb}/stb_truetype.h +0 -0
@@ -0,0 +1,44 @@
1
+ # `http2` 模块
2
+
3
+ > 属于[Node 扩展:已实现](../node.md) · [English](../../../implemented/node/http2.md)
4
+
5
+ 实现位置:`src/extensions/node/http2/`、`runtime/ext_node/http2/`、`runtime/xt_http2/`
6
+
7
+ HTTP/2 分帧与 HPACK 来自随仓库引入的 nghttp2。导入 `http2` / `node:http2`
8
+ (或出现 `createSecureServer`)时选中 `http2` 链接特性;它 `requires:
9
+ ["tls", "node-tls"]`,因为安全服务端复用 `node:tls` 的服务端封装,两种方案都通过
10
+ mbedTLS 协商 ALPN `h2`。`runtime/ext_node/http2/http2_backend.c` 被编译进固定版本的 nghttp2 归档
11
+ (`runtime/lib/<os>-<arch>/nghttp2.{a,lib}`);该特性编译的入口是
12
+ `http2_bridge.c`,它的调用会取出后端并完成安装。`npm run tls` 会同时构建该归档与
13
+ mbedTLS 归档,因此已安装的 npm 包不需要 vendor 头文件。
14
+
15
+ 一个 `Http2Session` 拥有一个套接字和一个 `nghttp2_session`。运行时的可读 reactor
16
+ 把字节送入 `nghttp2_session_mem_recv2` 并冲刷 `nghttp2_session_mem_send2` 的输出;
17
+ nghttp2 回调负责触发 JavaScript 事件。没有 HTTP/1.1 upgrade:明文 `http://` 连接
18
+ 直接使用 h2c prior knowledge,与 Node 客户端一致。
19
+
20
+ | API | 说明 |
21
+ | --- | --- |
22
+ | `http2.connect(authority[, options][, listener])` | `http(s)://host:port`;`options`:`ca`、`rejectUnauthorized`;返回已连接的 `ClientHttp2Session` 并触发 `connect` |
23
+ | `session.request(headers[, options])` | `headers` 含 `:method`(默认 `GET`)、`:path`、`:scheme`、`:authority` 及普通字段;返回 `ClientHttp2Stream` |
24
+ | `stream.end([data])` / `write(data)` / `close()` | 请求体与取消;触发 `response(headers, flags)`、`data`、`end`、`close`、`error` |
25
+ | `session.close()` / `destroy()` | 发送 GOAWAY 并关闭传输 |
26
+ | `http2.createServer([options][, onRequest])` | 明文 h2c 服务端;`listen` / `address` / `close` 与 `net.Server` 一致 |
27
+ | `http2.createSecureServer(options[, onRequest])` | TLS + ALPN `h2`;`options` 需要 `cert` 与 `key`(客户端证书再加 `ca`) |
28
+ | `server.on("stream", (stream, headers) => …)` | 服务端 stream 同时具有请求事件与 `respond` / `setHeader` / `write` / `end` |
29
+ | `http2.constants` | 帧类型与错误码数值(`NGHTTP2_FLAG_END_STREAM`、`NGHTTP2_CANCEL` 等) |
30
+
31
+ 服务端 stream 暴露 `method`、`url`、`scheme`、`authority`、`httpVersion`
32
+ (`"2.0"`)与 `headers`;`stream` 事件的第二个参数就是同一个 headers 对象。
33
+ `respond(statusOrHeaders[, headers])`(或 `respond({":status": 201})`)、
34
+ `setHeader` / `getHeader` 与 `write` / `end` 负责提交响应。请求体通过 `data` /
35
+ `end` 事件送达,也可在同一个 stream 对象上访问。
36
+
37
+ 端到端测试双向验证互操作性:编译后的客户端连接 Node 的 `http2` 服务端(h2c 与 TLS),
38
+ 以及 Node 客户端连接编译后的 `createServer` / `createSecureServer`。
39
+
40
+ ## 已知限制
41
+
42
+ 未实现 `pushStream`、trailers、extended CONNECT、会话池 / 连接复用、`stream.priority`
43
+ 与流控调优。响应头与响应体在 `end()` 之前会缓冲(`end()` 时才提交响应),因此想流式
44
+ 发送大 body 的处理函数仍可工作,但不会提前冲刷部分响应。
@@ -0,0 +1,47 @@
1
+ # `tls` 模块
2
+
3
+ > 属于[Node 扩展:已实现](../node.md) · [English](../../../implemented/node/tls.md)
4
+
5
+ 实现位置:`src/extensions/node/tls/`、`runtime/ext_node/tls/`、`runtime/xt_tls/`
6
+
7
+ `node:tls` 由随仓库引入(vendored)的 mbedTLS 实现(TLS 1.2/1.3、X.509 与 PSA crypto)。
8
+ 两个链接特性使它保持可选:
9
+
10
+ - **`tls`**(由核心扩展声明)是 mbedTLS 传输层:`runtime/xt_tls/tls_core.c`
11
+ 被归档进 `tls.{a,lib}`,该特性编译的入口是极小的 `tls_bridge.c`,它的调用会取出
12
+ 后端并完成安装。`fetch` 的 `https://` 请求使用它。出现 `https://` 字面量、使用
13
+ `node:tls` / `node:http2`,或传入 `--feature tls` 时会选中。
14
+ - **`node-tls`**(`requires: ["tls"]`)在 `runtime/ext_node/tls/tls_backend.c`
15
+ 中提供 JavaScript 接口,导入 `tls` / `node:tls` 时选中(导入说明符在打包后是字符串字面量)。
16
+
17
+ `npm run tls` 把固定版本的 mbedTLS 下载到 `vendor/mbedtls`,与 `tls_core.c`
18
+ 一起编译,并写出 `runtime/lib/<os>-<arch>/tls.{a,lib}`。只有 bridge 与后端是受版本控制
19
+ 的源码,因此已安装的 npm 包无需 vendor 头文件即可编译使用 TLS 的程序。缺少归档时模块
20
+ 仍可导入,但调用会抛出明确的运行时错误;显式 `--feature tls` 则会让缺失的归档变成构建错误。
21
+
22
+ | API | 说明 |
23
+ | --- | --- |
24
+ | `tls.connect(options[, callback])` | `options`:`port`、`host`、`servername`、`ca`、`cert`、`key`、`rejectUnauthorized`、`ALPNProtocols`;也支持 `connect(port[, host][, options][, callback])` |
25
+ | `tls.createServer(options[, onSecure])` | `options`:`cert`、`key`、`ca`、`requestCert`、`rejectUnauthorized`、`ALPNProtocols`;`listen` / `address` / `close` 复用 `net.Server` |
26
+ | `tls.TLSSocket` | EventEmitter,含 `write` / `end` / `destroy` / `address` / `setEncoding` / `getPeerCertificate`;触发 `data`、`end`、`close`、`error` |
27
+
28
+ `connect` 同步完成 TCP 连接与 TLS 握手,返回的套接字已经安全。`secureConnect`
29
+ 与回调在零延迟定时器中触发,因此晚于 `const socket = connect(...)` 的赋值,
30
+ 与 Node 的时序一致。套接字的可读事件在 reactor 上解密记录;写入只在一个记录期间阻塞。
31
+
32
+ ## 证书校验
33
+
34
+ - `rejectUnauthorized` 默认为 `true`。传入的 `ca`(PEM 字符串或 Buffer)会替换平台
35
+ 信任库;未传时,Windows 通过 crypt32 读取 `ROOT` 存储,Linux 读取
36
+ `ca-certificates.crt` / `ca-bundle.crt`(或 `/etc/ssl/certs`),macOS/BSD 读取
37
+ `/etc/ssl/cert.pem`。
38
+ - `NODE_EXTRA_CA_CERTS` 指向的 PEM 包会在平台根证书之外**额外**被信任,与 Node 一致。
39
+ `tls` 端到端测试用自签名证书夹具配合它运行。
40
+ - `servername` 默认取 `host`,用于 SNI 与主机名校验。
41
+
42
+ ## 已知限制
43
+
44
+ `createServer` 的外观与 `net.Server` 一致,但在 accept 路径上完成握手(运行时没有独立的
45
+ TLS 线程)。不支持会话恢复、0-RTT,也没有基于已连接 `net` 套接字的 `TLSSocket`
46
+ 构造器:`TLSSocket` 只包裹本模块建立或接受的连接。PEM 输入必须是字符串或 Buffer;
47
+ `key` 与 `cert` 必须同时提供。
@@ -27,6 +27,8 @@ TypeScript 导出与 C 实现一一对应。新增模块不需要改动核心编
27
27
  | `net` | [net](./node/net.md) | TCP 服务端与客户端 |
28
28
  | `dgram` | [dgram](./node/dgram.md) | UDP 套接字 |
29
29
  | `http` | [http](./node/http.md) | 服务端与客户端,`Connection: close` |
30
+ | `tls` | [tls](./node/tls.md) | mbedTLS 客户端/服务端(`node-tls` 链接特性) |
31
+ | `http2` | [http2](./node/http2.md) | nghttp2 h2c 与 TLS/ALPN(`http2` 链接特性) |
30
32
  | `child_process` | [child_process](./node/child-process.md) | `spawnSync` / `spawn` / `execFile` / `execSync` |
31
33
  | `events` | [events](./node/events.md) | `EventEmitter`(全局与具名导出) |
32
34
  | `util` | [util](./node/util.md) | 格式化、inspect 与类型谓词 |
@@ -39,6 +41,11 @@ TypeScript 导出与 C 实现一一对应。新增模块不需要改动核心编
39
41
  | `zlib` | [zlib](./node/zlib.md) | 仅 `createGzip` |
40
42
  | `worker_threads` | [worker_threads](./node/worker-threads.md) | 以子进程模拟 |
41
43
 
44
+ `tls` 与 `http2` 以可选**链接特性**实现,底层是随仓库引入的 mbedTLS 与 nghttp2:
45
+ `npm run tls` 会下载并构建 `runtime/lib/<os>-<arch>/{tls,nghttp2}.{a,lib}`,
46
+ 驱动只在程序可能用到它们时才链接。没有这些归档的检出仍可编译,但在运行
47
+ `npm run tls` 之前调用会返回明确的运行时错误。
48
+
42
49
  ## 改动应该写在哪一页?
43
50
 
44
51
  Node 模块改动通常涉及 `src/extensions/node/<module>/` 与 `runtime/ext_node/<module>/`;
@@ -3,7 +3,8 @@
3
3
  > 属于[已实现语法与功能](../implemented.md) · [English](../../implemented/storage.md)
4
4
 
5
5
  三个浏览器存储全局变量 —— `localStorage`、`sessionStorage` 与 `indexedDB` —— **始终可用**
6
- (它们是核心全局),由内置的 SQLite(`runtime/xt_storage/sqlite3`)支撑。无需任何扩展开关。
6
+ (它们是核心全局),由内置的 SQLite(`vendor/sqlite3`,由 `npm run vendor` 获取)支撑。
7
+ 无需任何扩展开关。
7
8
 
8
9
  ## 后端选择
9
10
 
@@ -32,14 +32,14 @@
32
32
  - [集合](./implemented/collections.md) —— Map、Set、WeakMap、WeakSet、WeakRef、迭代
33
33
  - [类型化数组与二进制数据](./implemented/typed-arrays.md) —— 类型化数组、ArrayBuffer、DataView
34
34
  - [异步、Promise 与生成器](./implemented/async.md) —— async/await、异步生成器、Promise、定时器
35
- - [fetch](./implemented/fetch.md) —— 由 reactor 驱动的 HTTP/1.1 客户端与 Headers
35
+ - [fetch](./implemented/fetch.md) —— 由 reactor 驱动的 HTTP/1.1 客户端(HTTP 与 HTTPS)与 Headers
36
36
  - [WebAssembly](./implemented/wasm.md) —— `WebAssembly` 命名空间表面(字节码执行尚未实现)
37
37
 
38
38
  ### 模块与工具
39
39
 
40
40
  - [模块系统](./implemented/modules.md) —— import/export、打包、`node_modules`、`import.meta`
41
41
  - [扩展机制](./implemented/extensions.md) —— 注册表、`node` 扩展、原生 C++/Rust 扩展
42
- - [Node 扩展](./implemented/node.md) —— 21 个 Node 内置模块,每个模块一页
42
+ - [Node 扩展](./implemented/node.md) —— 24 个 Node 内置模块,每个模块一页
43
43
  - [Storage 扩展](./implemented/storage.md) —— 基于 SQLite 的 `localStorage`、`sessionStorage` 与 `indexedDB`
44
44
  - [驱动、增量编译与工具链](./implemented/driver.md) —— 缓存、clang/LTO/PGO、配置、图标
45
45
  - [CLI 与编程接口](./implemented/cli.md) —— 命令、参数、嵌入式 API
@@ -2,12 +2,12 @@
2
2
 
3
3
  > 属于[Node 扩展:未实现](../node.md) · [English](../../../unimplemented/node/modules.md)
4
4
 
5
- 以下 Node 内置模块没有任何对应扩展 / 内置函数(`fs` / `fs/promises` / `path` / `os` / `process` / `buffer` / `stream` / `dgram` / `http` / `events` / `util` / `querystring` / `readline` / `assert` / `test` 已实现,`crypto` / `url` / `child_process` / `zlib` / `stream/promises` / `worker_threads` 部分实现,见[已实现:Node 扩展](../../implemented/node.md)):
5
+ 以下 Node 内置模块没有任何对应扩展 / 内置函数(`fs` / `fs/promises` / `path` / `os` / `process` / `buffer` / `stream` / `dgram` / `http` / `tls` / `http2` / `events` / `util` / `querystring` / `readline` / `assert` / `test` 已实现,`crypto` / `url` / `child_process` / `zlib` / `stream/promises` / `worker_threads` 部分实现,见[已实现:Node 扩展](../../implemented/node.md)):
6
6
 
7
7
  | 模块 | 说明 |
8
8
  | --- | --- |
9
- | `https` | TLS 版 HTTP |
10
- | `tls` / `cluster` / `vm` | 其余未列出的核心模块 |
9
+ | `https` | TLS 版 HTTP;可直接使用 `http2` / `tls`,或对 `https://` 使用 `fetch` |
10
+ | `cluster` / `vm` | 其余未列出的核心模块 |
11
11
 
12
12
  > `crypto`(仅 `createHash`,现支持流式 API)、`url`(仅 `pathToFileURL` / `fileURLToPath`)、
13
13
  > `child_process`(仅 `spawnSync` / `spawn` / `execFile` / `execSync`)、
@@ -9,6 +9,6 @@
9
9
  | 类型检查器 | 部分:默认[语义检查](../implemented/checker.md)会报告未定义名、重复声明、实参个数、属性是否存在与确定的可赋值性错误,支持结构化接口/类、简单泛型与基础流收窄;没有完整推断与 strict-null |
10
10
  | 完整标准库 | 部分:Math / JSON / Date / Map / Set / RegExp / `Error` / `BigInt` / `Symbol` / `structuredClone` 已实现;String.normalize 缺失 |
11
11
  | 多文件模块打包 | 部分:相对路径 `.ts` 打包、ESM `node_modules` 包、命名空间导入、裸说明符扩展模块导入已实现;CommonJS 仅在 `node_modules` 内处理;循环依赖 / 实时绑定未实现 |
12
- | 异步运行时 | ✓ `async`/`await` 在 `select(2)` reactor 上对 pending promise 挂起:定时器、TCP 套接字、offload 的文件/HTTP 工作与 promise 回调都能恢复协程。仍缺:真正的 OS 级文件监听、非阻塞 `net.connect`、`fetch` 的 TLS,以及用户可见的 worker 线程(`worker_threads` 仍用子进程) |
12
+ | 异步运行时 | ✓ `async`/`await` 在 `select(2)` reactor 上对 pending promise 挂起:定时器、TCP 套接字、offload 的文件/HTTP 工作与 promise 回调都能恢复协程。仍缺:真正的 OS 级文件监听、非阻塞 `net.connect`,以及用户可见的 worker 线程(`worker_threads` 仍用子进程) |
13
13
  | Windows 二进制产物验证 | 构建层已适配(`.exe` 后缀、链接参数分支),并在 CI 中验证 |
14
14
  | 精确的 ECMAScript 数值 / 字符串 / 比较语义 | 部分,见[运行时偏差](./deviations.md) |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xbintsc",
3
- "version": "0.6.22",
3
+ "version": "0.6.24",
4
4
  "description": "xbintsc - a TypeScript binary compiler (TypeScript -> LLVM IR -> native binary)",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -30,6 +30,10 @@
30
30
  "!dist/*.exe",
31
31
  "runtime",
32
32
  "!runtime/lib",
33
+ "vendor/stb",
34
+ "vendor/nanosvg",
35
+ "vendor/miniz",
36
+ "vendor/sqlite3",
33
37
  "scripts",
34
38
  "bin",
35
39
  "doc",
@@ -39,10 +43,12 @@
39
43
  ],
40
44
  "scripts": {
41
45
  "build": "tsc -p tsconfig.json",
42
- "prepublishOnly": "npm run build",
46
+ "prepublishOnly": "npm run vendor && npm run build",
43
47
  "runtime": "tsx scripts/build-runtime.ts",
48
+ "vendor": "tsx scripts/fetch-vendored.ts",
44
49
  "gui": "tsx scripts/build-gui.ts",
45
50
  "media": "tsx scripts/build-media.ts",
51
+ "tls": "tsx scripts/build-tls.ts",
46
52
  "package-release": "tsx scripts/package-release.ts",
47
53
  "package-platform": "tsx scripts/package-platform.ts",
48
54
  "platform-versions": "tsx scripts/set-platform-version.ts",
@@ -54,8 +60,10 @@
54
60
  "docs:matrix": "tsx scripts/support-matrix.ts",
55
61
  "bench": "tsx scripts/bench.ts",
56
62
  "lint": "eslint . && tsx scripts/check-file-length.ts",
63
+ "pretest": "npm run vendor",
57
64
  "test": "vitest run",
58
65
  "test:watch": "vitest",
66
+ "pretest:e2e": "npm run vendor",
59
67
  "test:e2e": "vitest run tests/e2e",
60
68
  "coverage": "vitest run --coverage",
61
69
  "coverage:runtime": "tsx scripts/coverage-runtime.ts"
@@ -72,12 +80,12 @@
72
80
  "vitest": "^2.1.8"
73
81
  },
74
82
  "optionalDependencies": {
75
- "@xbintsc/linux-x64": "0.6.22",
76
- "@xbintsc/linux-arm64": "0.6.22",
77
- "@xbintsc/darwin-x64": "0.6.22",
78
- "@xbintsc/darwin-arm64": "0.6.22",
79
- "@xbintsc/win32-x64": "0.6.22",
80
- "@xbintsc/win32-arm64": "0.6.22"
83
+ "@xbintsc/linux-x64": "0.6.24",
84
+ "@xbintsc/linux-arm64": "0.6.24",
85
+ "@xbintsc/darwin-x64": "0.6.24",
86
+ "@xbintsc/darwin-arm64": "0.6.24",
87
+ "@xbintsc/win32-x64": "0.6.24",
88
+ "@xbintsc/win32-arm64": "0.6.24"
81
89
  },
82
90
  "engines": {
83
91
  "node": ">=22"
@@ -1,26 +1,40 @@
1
1
  /*
2
- * Node.js `http2` module for xbintsc — a stub (HTTP/2 is not implemented).
2
+ * Node.js `http2` module for xbintsc — importable bindings.
3
+ *
4
+ * The implementation is the `http2` link feature (`http2_backend.c`, nghttp2).
5
+ * This translation unit is always compiled with the node extension so the
6
+ * import resolves either way; without the feature the calls throw a clear
7
+ * runtime error instead of failing at link time.
3
8
  */
4
9
 
5
10
  #include "../node_common.h"
11
+ #include "../../xt_http2_api.h"
6
12
 
7
- xt_value xt_http2_connect(int32_t argc, xt_value *argv) {
8
- (void)argc;
9
- (void)argv;
10
- xt_throw(xt_error_from_message("Error: node:http2 is not supported in compiled binaries"));
13
+ static xt_value xt_http2_unsupported(void) {
14
+ xt_throw(xt_error_from_message(
15
+ "Error: node:http2 is not available (the 'http2' link feature is not linked; "
16
+ "run `npm run tls` to build it)"));
11
17
  return XT_UNDEFINED;
12
18
  }
13
19
 
20
+ xt_value xt_http2_connect(int32_t argc, xt_value *argv) {
21
+ if (xt_http2_api == NULL) return xt_http2_unsupported();
22
+ return xt_http2_api->connect(argc, argv);
23
+ }
24
+
14
25
  xt_value xt_http2_create_server(int32_t argc, xt_value *argv) {
15
- (void)argc;
16
- (void)argv;
17
- xt_throw(xt_error_from_message("Error: node:http2 is not supported in compiled binaries"));
18
- return XT_UNDEFINED;
26
+ if (xt_http2_api == NULL) return xt_http2_unsupported();
27
+ return xt_http2_api->create_server(argc, argv);
19
28
  }
20
29
 
21
30
  xt_value xt_http2_create_secure_server(int32_t argc, xt_value *argv) {
31
+ if (xt_http2_api == NULL) return xt_http2_unsupported();
32
+ return xt_http2_api->create_secure_server(argc, argv);
33
+ }
34
+
35
+ xt_value xt_http2_constants(int32_t argc, xt_value *argv) {
22
36
  (void)argc;
23
37
  (void)argv;
24
- xt_throw(xt_error_from_message("Error: node:http2 is not supported in compiled binaries"));
25
- return XT_UNDEFINED;
38
+ if (xt_http2_api == NULL || xt_http2_api->constants == NULL) return xt_object_new();
39
+ return xt_http2_api->constants();
26
40
  }
@@ -0,0 +1,48 @@
1
+ /*
2
+ * xbintsc `http2` link feature — nghttp2-backed `node:http2`.
3
+ *
4
+ * Compiled only when a program imports `node:http2`; its `requires` pulls in
5
+ * `tls` (mbedTLS) and `node-tls` (the TLSSocket/server wrapper). The
6
+ * implementation is split by role under `http2/parts/` and `#include`d here as
7
+ * one translation unit so the helpers stay `static`.
8
+ *
9
+ * A session owns one socket (plain h2c for `createServer`/`http://`, TLS with
10
+ * ALPN `h2` for `createSecureServer`/`https://`) and one `nghttp2_session`.
11
+ * The reactor's readiness callback feeds bytes into
12
+ * `nghttp2_session_mem_recv2` and flushes `nghttp2_session_mem_send2` output;
13
+ * nghttp2's callbacks turn frames into EventEmitter events on the JavaScript
14
+ * stream objects.
15
+ */
16
+
17
+ #include "../node_common.h"
18
+ #include "../../xt_http2_api.h"
19
+ #include "../../xt_node_tls_api.h"
20
+ #include "../../xt_tls_api.h"
21
+
22
+ #include <stdint.h>
23
+ #include <string.h>
24
+
25
+ #if defined(_WIN32)
26
+ #include <winsock2.h>
27
+ #include <ws2tcpip.h>
28
+ typedef int xt_h2_socklen_t;
29
+ #else
30
+ #include <netdb.h>
31
+ #include <sys/socket.h>
32
+ #include <unistd.h>
33
+ typedef socklen_t xt_h2_socklen_t;
34
+ #endif
35
+
36
+ /* Provided by net.c (linked with the node extension). */
37
+ extern xt_value xt_net_static(xt_value name, int32_t argc, xt_value *argv);
38
+
39
+ /* Byte helpers exported by buffer.c. */
40
+ int xt_node_is_buffer(xt_value value);
41
+ unsigned char *xt_node_buffer_bytes(xt_value value, size_t *outLength);
42
+
43
+ #include "parts/common.inc"
44
+ #include "parts/streams.inc"
45
+ #include "parts/events.inc"
46
+ #include "parts/client.inc"
47
+ #include "parts/server.inc"
48
+ #include "parts/api.inc"
@@ -0,0 +1,25 @@
1
+ /*
2
+ * xbintsc `http2` link feature — registration bridge.
3
+ *
4
+ * The nghttp2-backed implementation (and `http2_backend.c`) is archived in
5
+ * `nghttp2.lib`. This direct-linked unit references `xt_http2_backend_boot()`,
6
+ * which forces the linker to extract the backend and the nghttp2 members it
7
+ * uses, then installs the dispatch table before `main`.
8
+ *
9
+ * No nghttp2 header is included here: the npm package ships the archive, not
10
+ * the vendored headers, so the bridge must compile without `vendor/`.
11
+ */
12
+
13
+ extern void xt_http2_backend_boot(void);
14
+
15
+ #if defined(__GNUC__) || defined(__clang__)
16
+ __attribute__((constructor)) static void xt_http2_bridge_boot(void) {
17
+ xt_http2_backend_boot();
18
+ }
19
+ #elif defined(_MSC_VER)
20
+ void xt_http2_bridge_boot(void);
21
+ #pragma section(".CRT$XCU", read)
22
+ __declspec(allocate(".CRT$XCU")) static void (*const xt_http2_bridge_boot_ctor)(void) =
23
+ xt_http2_bridge_boot;
24
+ void xt_http2_bridge_boot(void) { xt_http2_backend_boot(); }
25
+ #endif
@@ -0,0 +1,40 @@
1
+ /* -- API table and registration ------------------------------------------- */
2
+
3
+ xt_value xt_h2_constants(void) {
4
+ xt_value constants = xt_object_new();
5
+ xt_node_set(constants, "NGHTTP2_NO_ERROR", xt_number(0));
6
+ xt_node_set(constants, "NGHTTP2_PROTOCOL_ERROR", xt_number(1));
7
+ xt_node_set(constants, "NGHTTP2_INTERNAL_ERROR", xt_number(2));
8
+ xt_node_set(constants, "NGHTTP2_FLOW_CONTROL_ERROR", xt_number(3));
9
+ xt_node_set(constants, "NGHTTP2_SETTINGS_TIMEOUT", xt_number(4));
10
+ xt_node_set(constants, "NGHTTP2_STREAM_CLOSED", xt_number(5));
11
+ xt_node_set(constants, "NGHTTP2_FRAME_SIZE_ERROR", xt_number(6));
12
+ xt_node_set(constants, "NGHTTP2_REFUSED_STREAM", xt_number(7));
13
+ xt_node_set(constants, "NGHTTP2_CANCEL", xt_number(8));
14
+ xt_node_set(constants, "NGHTTP2_COMPRESSION_ERROR", xt_number(9));
15
+ xt_node_set(constants, "NGHTTP2_CONNECT_ERROR", xt_number(10));
16
+ xt_node_set(constants, "NGHTTP2_ENHANCE_YOUR_CALM", xt_number(11));
17
+ xt_node_set(constants, "NGHTTP2_INADEQUATE_SECURITY", xt_number(12));
18
+ xt_node_set(constants, "NGHTTP2_HTTP_1_1_REQUIRED", xt_number(13));
19
+ xt_node_set(constants, "NGHTTP2_HEADERS", xt_number(NGHTTP2_HEADERS));
20
+ xt_node_set(constants, "NGHTTP2_DATA", xt_number(NGHTTP2_DATA));
21
+ xt_node_set(constants, "NGHTTP2_SETTINGS", xt_number(NGHTTP2_SETTINGS));
22
+ xt_node_set(constants, "NGHTTP2_FLAG_NONE", xt_number(NGHTTP2_FLAG_NONE));
23
+ xt_node_set(constants, "NGHTTP2_FLAG_ACK", xt_number(NGHTTP2_FLAG_ACK));
24
+ xt_node_set(constants, "NGHTTP2_FLAG_END_STREAM", xt_number(NGHTTP2_FLAG_END_STREAM));
25
+ xt_node_set(constants, "NGHTTP2_FLAG_END_HEADERS", xt_number(NGHTTP2_FLAG_END_HEADERS));
26
+ xt_node_set(constants, "NGHTTP2_DEFAULT_WEIGHT", xt_number(NGHTTP2_DEFAULT_WEIGHT));
27
+ return constants;
28
+ }
29
+
30
+ static const XtHttp2Api kHttp2Api = {
31
+ xt_h2_connect,
32
+ xt_h2_create_server,
33
+ xt_h2_create_secure_server,
34
+ xt_h2_constants,
35
+ };
36
+
37
+ /* Called by `http2_bridge.c`, which is the feature's compiled `runtimeSources()`
38
+ * entry. The call is what makes the linker extract this object (and the nghttp2
39
+ * members it references) from `nghttp2.lib`. */
40
+ void xt_http2_backend_boot(void) { xt_http2_register(&kHttp2Api); }
@@ -0,0 +1,215 @@
1
+ /* -- client: connect and request ------------------------------------------ */
2
+
3
+ static int xt_h2_tcp_connect(const char *host, int port) {
4
+ char service[16];
5
+ snprintf(service, sizeof(service), "%d", port);
6
+ struct addrinfo hints;
7
+ memset(&hints, 0, sizeof(hints));
8
+ hints.ai_family = AF_UNSPEC;
9
+ hints.ai_socktype = SOCK_STREAM;
10
+ struct addrinfo *result = NULL;
11
+ if (getaddrinfo(host, service, &hints, &result) != 0) return -1;
12
+ int fd = -1;
13
+ for (struct addrinfo *info = result; info != NULL; info = info->ai_next) {
14
+ int candidate = socket(info->ai_family, info->ai_socktype, info->ai_protocol);
15
+ if (candidate < 0) continue;
16
+ if (connect(candidate, info->ai_addr, (xt_h2_socklen_t)info->ai_addrlen) == 0) {
17
+ fd = candidate;
18
+ break;
19
+ }
20
+ xt_h2_closesocket(candidate);
21
+ }
22
+ freeaddrinfo(result);
23
+ return fd;
24
+ }
25
+
26
+ static void xt_h2_install_callbacks(nghttp2_session_callbacks *callbacks) {
27
+ nghttp2_session_callbacks_set_on_begin_headers_callback(callbacks, xt_h2_on_begin_headers);
28
+ nghttp2_session_callbacks_set_on_header_callback(callbacks, xt_h2_on_header);
29
+ nghttp2_session_callbacks_set_on_frame_recv_callback(callbacks, xt_h2_on_frame_recv);
30
+ nghttp2_session_callbacks_set_on_data_chunk_recv_callback(callbacks, xt_h2_on_data_chunk_recv);
31
+ nghttp2_session_callbacks_set_on_stream_close_callback(callbacks, xt_h2_on_stream_close);
32
+ }
33
+
34
+ static xt_value xt_h2_session_request(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
35
+ (void)env;
36
+ xt_h2_conn *conn = xt_h2_conn_of(thisValue);
37
+ if (conn == NULL || conn->closed) {
38
+ xt_throw(xt_h2_error("the HTTP/2 session is closed"));
39
+ return XT_UNDEFINED;
40
+ }
41
+ xt_value headers = xt_arg(argc, argv, 0);
42
+ if (!XT_IS_OBJECT(headers)) {
43
+ xt_throw(xt_h2_error("http2 request headers must be an object"));
44
+ return XT_UNDEFINED;
45
+ }
46
+ const char *method = xt_string_data(xt_object_get_cstr(headers, ":method"));
47
+ const char *path = xt_string_data(xt_object_get_cstr(headers, ":path"));
48
+ const char *scheme = xt_string_data(xt_object_get_cstr(headers, ":scheme"));
49
+ const char *authority = xt_string_data(xt_object_get_cstr(headers, ":authority"));
50
+ if (method == NULL) method = "GET";
51
+ if (path == NULL) path = "/";
52
+ if (scheme == NULL) scheme = conn->scheme;
53
+ if (authority == NULL) authority = conn->authority;
54
+
55
+ nghttp2_nv nva[XT_H2_MAX_HEADERS];
56
+ size_t count = 0;
57
+ count = xt_h2_add_nv(nva, count, XT_H2_MAX_HEADERS, ":method", method);
58
+ count = xt_h2_add_nv(nva, count, XT_H2_MAX_HEADERS, ":path", path);
59
+ count = xt_h2_add_nv(nva, count, XT_H2_MAX_HEADERS, ":scheme", scheme);
60
+ count = xt_h2_add_nv(nva, count, XT_H2_MAX_HEADERS, ":authority", authority);
61
+ count = xt_h2_add_headers(nva, count, XT_H2_MAX_HEADERS, headers);
62
+
63
+ xt_h2_body *body = xt_h2_body_new();
64
+ nghttp2_data_provider2 provider;
65
+ provider.source.ptr = body;
66
+ provider.read_callback = xt_h2_read_body;
67
+ int32_t id = nghttp2_submit_request2(conn->session, NULL, nva, count, &provider, NULL);
68
+ xt_h2_free_nv(nva, count);
69
+ if (id < 0) {
70
+ xt_h2_body_free(body);
71
+ xt_throw(xt_h2_error("cannot submit the HTTP/2 request"));
72
+ return XT_UNDEFINED;
73
+ }
74
+ xt_value stream = xt_h2_new_stream(conn, id, body);
75
+ xt_h2_flush(conn);
76
+ return stream;
77
+ }
78
+
79
+ xt_value xt_h2_session_close(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
80
+ (void)env;
81
+ (void)argc;
82
+ (void)argv;
83
+ xt_h2_conn *conn = xt_h2_conn_of(thisValue);
84
+ if (conn == NULL || conn->closed) return thisValue;
85
+ nghttp2_session_terminate_session(conn->session, NGHTTP2_NO_ERROR);
86
+ xt_h2_flush(conn);
87
+ xt_h2_finish(conn, NULL);
88
+ return thisValue;
89
+ }
90
+
91
+ xt_value xt_h2_stream_write(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
92
+ (void)env;
93
+ if (argc > 0) xt_h2_stream_append(thisValue, argv[0]);
94
+ xt_h2_conn *conn = xt_h2_conn_of(thisValue);
95
+ if (conn == NULL || conn->closed) return xt_bool(0);
96
+ int32_t id = (int32_t)xt_to_number(xt_object_get_cstr(thisValue, "__id"));
97
+ nghttp2_session_resume_data(conn->session, id);
98
+ xt_h2_flush(conn);
99
+ return xt_bool(1);
100
+ }
101
+
102
+ xt_value xt_h2_stream_end(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
103
+ (void)env;
104
+ if (argc > 0 && !xt_truthy(xt_is_nullish(argv[0]))) xt_h2_stream_append(thisValue, argv[0]);
105
+ xt_h2_body *body = xt_h2_body_of(thisValue);
106
+ if (body != NULL) body->ended = 1;
107
+ xt_h2_conn *conn = xt_h2_conn_of(thisValue);
108
+ if (conn == NULL || conn->closed) return thisValue;
109
+ int32_t id = (int32_t)xt_to_number(xt_object_get_cstr(thisValue, "__id"));
110
+ nghttp2_session_resume_data(conn->session, id);
111
+ xt_h2_flush(conn);
112
+ return thisValue;
113
+ }
114
+
115
+ xt_value xt_h2_stream_close(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
116
+ (void)env;
117
+ (void)argc;
118
+ (void)argv;
119
+ xt_h2_conn *conn = xt_h2_conn_of(thisValue);
120
+ if (conn == NULL || conn->closed) return thisValue;
121
+ int32_t id = (int32_t)xt_to_number(xt_object_get_cstr(thisValue, "__id"));
122
+ nghttp2_submit_rst_stream(conn->session, NGHTTP2_FLAG_NONE, id, NGHTTP2_CANCEL);
123
+ xt_h2_flush(conn);
124
+ return thisValue;
125
+ }
126
+
127
+ xt_value xt_h2_stream_set_encoding(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
128
+ (void)env;
129
+ xt_node_set(thisValue, "__encoding", xt_arg(argc, argv, 0));
130
+ return thisValue;
131
+ }
132
+
133
+ /* `http2.connect(authority[, options][, listener])` */
134
+ xt_value xt_h2_connect(int32_t argc, xt_value *argv) {
135
+ const char *authority = xt_string_data(xt_to_string(xt_arg(argc, argv, 0)));
136
+ if (authority == NULL) {
137
+ xt_throw(xt_h2_error("http2.connect requires an authority URL"));
138
+ return XT_UNDEFINED;
139
+ }
140
+ char scheme[8];
141
+ char host[256];
142
+ char authorityCopy[512];
143
+ int port = 0;
144
+ int secure = xt_h2_parse_authority(authority, scheme, sizeof(scheme), host, sizeof(host), &port,
145
+ authorityCopy, sizeof(authorityCopy));
146
+ if (secure < 0) {
147
+ xt_throw(xt_h2_error("http2.connect requires an http:// or https:// URL"));
148
+ return XT_UNDEFINED;
149
+ }
150
+
151
+ xt_value options = xt_undefined();
152
+ xt_value listener = xt_undefined();
153
+ for (int32_t i = 1; i < argc; i++) {
154
+ if (XT_IS_OBJECT(argv[i]) && options == xt_undefined()) options = argv[i];
155
+ else if (XT_IS_FUNCTION(argv[i])) listener = argv[i];
156
+ }
157
+
158
+ xt_h2_conn *conn = (xt_h2_conn *)calloc(1, sizeof(xt_h2_conn));
159
+ if (conn == NULL) {
160
+ xt_throw(xt_h2_error("out of memory"));
161
+ return XT_UNDEFINED;
162
+ }
163
+ conn->fd = -1;
164
+ snprintf(conn->scheme, sizeof(conn->scheme), "%s", scheme);
165
+ snprintf(conn->authority, sizeof(conn->authority), "%s", authorityCopy);
166
+
167
+ if (secure) {
168
+ if (xt_tls_core_api == NULL) {
169
+ free(conn);
170
+ xt_throw(xt_h2_error("https requires the tls link feature"));
171
+ return XT_UNDEFINED;
172
+ }
173
+ char error[256];
174
+ error[0] = '\0';
175
+ int verify = 1;
176
+ if (XT_IS_OBJECT(options)) {
177
+ xt_value reject = xt_object_get_cstr(options, "rejectUnauthorized");
178
+ if (!xt_truthy(xt_is_nullish(reject))) verify = xt_truthy(reject) ? 1 : 0;
179
+ }
180
+ xt_value caValue = XT_IS_OBJECT(options) ? xt_object_get_cstr(options, "ca") : xt_undefined();
181
+ const char *ca = XT_IS_STRING(caValue) ? xt_string_data(caValue) : NULL;
182
+ conn->tls = xt_tls_core_api->client_connect(host, port, host, verify, ca, NULL, NULL, "h2",
183
+ error, sizeof(error));
184
+ if (conn->tls == NULL) {
185
+ free(conn);
186
+ xt_throw(xt_h2_error(error));
187
+ return XT_UNDEFINED;
188
+ }
189
+ conn->fd = xt_tls_core_api->fd(conn->tls);
190
+ } else {
191
+ conn->fd = xt_h2_tcp_connect(host, port);
192
+ if (conn->fd < 0) {
193
+ free(conn);
194
+ xt_throw(xt_h2_error("http2.connect could not reach the server"));
195
+ return XT_UNDEFINED;
196
+ }
197
+ }
198
+
199
+ nghttp2_session_callbacks *callbacks = NULL;
200
+ nghttp2_session_callbacks_new(&callbacks);
201
+ xt_h2_install_callbacks(callbacks);
202
+ nghttp2_session_client_new(&conn->session, callbacks, conn);
203
+ nghttp2_session_callbacks_del(callbacks);
204
+ nghttp2_submit_settings(conn->session, NGHTTP2_FLAG_NONE, NULL, 0);
205
+
206
+ conn->is_server = 0;
207
+ conn->session_value = xt_h2_session_object(conn);
208
+ xt_value session = conn->session_value;
209
+ xt_h2_attach(conn, session);
210
+ if (XT_IS_FUNCTION(listener)) {
211
+ xt_array_push(xt_node_listeners(session, "connect", 1), listener);
212
+ }
213
+ xt_node_emit0(session, "connect");
214
+ return session;
215
+ }