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,146 @@
1
+ /* -- configuration, credentials and trust store ---------------------------- */
2
+
3
+ /*
4
+ * mbedTLS ships no platform trust-store loader. Windows exposes its roots
5
+ * through the crypto API; the Unix hosts use the conventional CA bundle paths
6
+ * (macOS ships `/etc/ssl/cert.pem`, the BSDs use the same file, and Linux
7
+ * distributions use one of the two bundle locations). A caller-supplied `ca`
8
+ * (Node's `tls.connect({ ca })`) bypasses the platform store entirely.
9
+ */
10
+
11
+ #if defined(_WIN32)
12
+ #include <wincrypt.h>
13
+ #include <windows.h>
14
+ #else
15
+ #include <unistd.h>
16
+ #endif
17
+
18
+ #include <stdio.h>
19
+ #include <stdlib.h>
20
+ #include <string.h>
21
+
22
+ int xt_tls_platform_init(void) { return 0; }
23
+
24
+ void xt_tls_setup_alpn(xt_tls_setup *setup, const char *list) {
25
+ setup->alpn_count = 0;
26
+ setup->alpn_list[0] = NULL;
27
+ if (list == NULL) return;
28
+ const char *cursor = list;
29
+ while (*cursor != '\0' && setup->alpn_count < XT_TLS_MAX_ALPN) {
30
+ const char *comma = strchr(cursor, ',');
31
+ size_t length = comma ? (size_t)(comma - cursor) : strlen(cursor);
32
+ if (length > 0 && length < XT_TLS_ALPN_MAX) {
33
+ memcpy(setup->alpn[setup->alpn_count], cursor, length);
34
+ setup->alpn[setup->alpn_count][length] = '\0';
35
+ setup->alpn_list[setup->alpn_count] = setup->alpn[setup->alpn_count];
36
+ setup->alpn_count++;
37
+ }
38
+ if (!comma) break;
39
+ cursor = comma + 1;
40
+ }
41
+ setup->alpn_list[setup->alpn_count] = NULL;
42
+ }
43
+
44
+ #if defined(_WIN32)
45
+ static int xt_tls_load_system_roots(mbedtls_x509_crt *chain) {
46
+ HCERTSTORE store = CertOpenSystemStoreA(0, "ROOT");
47
+ if (store == NULL) return -1;
48
+ int loaded = 0;
49
+ PCCERT_CONTEXT certificate = NULL;
50
+ while ((certificate = CertEnumCertificatesInStore(store, certificate)) != NULL) {
51
+ if (mbedtls_x509_crt_parse_der(chain, certificate->pbCertEncoded,
52
+ certificate->cbCertEncoded) == 0) {
53
+ loaded++;
54
+ }
55
+ }
56
+ CertCloseStore(store, 0);
57
+ return loaded > 0 ? 0 : -1;
58
+ }
59
+ #else
60
+ static int xt_tls_load_system_roots(mbedtls_x509_crt *chain) {
61
+ static const char *bundles[] = {
62
+ "/etc/ssl/certs/ca-certificates.crt", /* Debian/Ubuntu/Alpine */
63
+ "/etc/pki/tls/certs/ca-bundle.crt", /* Fedora/RHEL */
64
+ "/etc/ssl/cert.pem", /* macOS/BSD */
65
+ "/etc/ssl/ca-bundle.pem",
66
+ NULL,
67
+ };
68
+ for (size_t i = 0; bundles[i] != NULL; i++) {
69
+ if (access(bundles[i], R_OK) != 0) continue;
70
+ if (mbedtls_x509_crt_parse_file(chain, bundles[i]) == 0) return 0;
71
+ }
72
+ /* Last resort: every hashed certificate in the directory. */
73
+ return mbedtls_x509_crt_parse_path(chain, "/etc/ssl/certs");
74
+ }
75
+ #endif
76
+
77
+ int xt_tls_setup_trust(xt_tls_setup *setup, const char *ca_pem, char *err, size_t errlen) {
78
+ int loaded = 0;
79
+ if (ca_pem != NULL && ca_pem[0] != '\0') {
80
+ if (mbedtls_x509_crt_parse(&setup->ca, (const unsigned char *)ca_pem, strlen(ca_pem) + 1) != 0) {
81
+ snprintf(err, errlen, "cannot parse the supplied CA certificates");
82
+ return -1;
83
+ }
84
+ loaded = 1;
85
+ } else if (xt_tls_load_system_roots(&setup->ca) == 0) {
86
+ loaded = 1;
87
+ }
88
+ /* Node-compatible escape hatch for a private CA (and the e2e tests): the
89
+ * named PEM bundle is trusted in addition to the platform roots. */
90
+ const char *extra = getenv("NODE_EXTRA_CA_CERTS");
91
+ if (extra != NULL && extra[0] != '\0') {
92
+ if (mbedtls_x509_crt_parse_file(&setup->ca, extra) != 0) {
93
+ snprintf(err, errlen, "cannot read NODE_EXTRA_CA_CERTS (%s)", extra);
94
+ return -1;
95
+ }
96
+ loaded = 1;
97
+ }
98
+ if (!loaded) {
99
+ snprintf(err, errlen, "no CA certificates are available");
100
+ return -1;
101
+ }
102
+ setup->has_ca = 1;
103
+ return 0;
104
+ }
105
+
106
+ static int xt_tls_setup_crypto(xt_tls_setup *setup, char *err, size_t errlen) {
107
+ mbedtls_ssl_config_init(&setup->config);
108
+ mbedtls_entropy_init(&setup->entropy);
109
+ mbedtls_ctr_drbg_init(&setup->ctr_drbg);
110
+ mbedtls_x509_crt_init(&setup->ca);
111
+ mbedtls_x509_crt_init(&setup->cert);
112
+ mbedtls_pk_init(&setup->key);
113
+ setup->has_ca = setup->has_cert = setup->has_key = 0;
114
+ setup->alpn_count = 0;
115
+ setup->alpn_list[0] = NULL;
116
+ static const char pers[] = "xbintsc-tls";
117
+ int ret = mbedtls_ctr_drbg_seed(&setup->ctr_drbg, mbedtls_entropy_func, &setup->entropy,
118
+ (const unsigned char *)pers, sizeof(pers) - 1);
119
+ if (ret != 0) {
120
+ snprintf(err, errlen, "cannot seed the TLS RNG (mbedtls %d)", ret);
121
+ return -1;
122
+ }
123
+ setup->initialized = 1;
124
+ return 0;
125
+ }
126
+
127
+ void xt_tls_setup_free(xt_tls_setup *setup) {
128
+ if (setup == NULL) return;
129
+ mbedtls_ssl_config_free(&setup->config);
130
+ mbedtls_ctr_drbg_free(&setup->ctr_drbg);
131
+ mbedtls_entropy_free(&setup->entropy);
132
+ mbedtls_x509_crt_free(&setup->ca);
133
+ mbedtls_x509_crt_free(&setup->cert);
134
+ mbedtls_pk_free(&setup->key);
135
+ setup->initialized = 0;
136
+ }
137
+
138
+ void xt_tls_conn_fail(xt_tls_conn *conn, const char *message, int code) {
139
+ char detail[128];
140
+ if (code != 0) {
141
+ mbedtls_strerror(code, detail, sizeof(detail));
142
+ snprintf(conn->error, sizeof(conn->error), "%s (mbedtls: %s)", message, detail);
143
+ } else {
144
+ snprintf(conn->error, sizeof(conn->error), "%s", message);
145
+ }
146
+ }
@@ -0,0 +1,27 @@
1
+ /*
2
+ * xbintsc `tls` link feature — registration bridge.
3
+ *
4
+ * The mbedTLS transport (and `tls_core.c`) lives in `tls.lib`, which is linked
5
+ * only when the feature is active. A static archive member with just a
6
+ * constructor is never extracted, so this tiny translation unit — the
7
+ * feature's `runtimeSources()` entry — references `xt_tls_backend_boot()`.
8
+ * That call pulls the implementation (and the mbedTLS members it uses) out of
9
+ * the archive and installs the dispatch table before `main`.
10
+ *
11
+ * It intentionally includes no mbedTLS header: the npm package ships the
12
+ * archive, not the vendored headers, so a program that enables TLS must be
13
+ * compilable without `vendor/`.
14
+ */
15
+
16
+ extern void xt_tls_backend_boot(void);
17
+
18
+ #if defined(__GNUC__) || defined(__clang__)
19
+ __attribute__((constructor)) static void xt_tls_bridge_boot(void) {
20
+ xt_tls_backend_boot();
21
+ }
22
+ #elif defined(_MSC_VER)
23
+ void xt_tls_bridge_boot(void);
24
+ #pragma section(".CRT$XCU", read)
25
+ __declspec(allocate(".CRT$XCU")) static void (*const xt_tls_bridge_boot_ctor)(void) = xt_tls_bridge_boot;
26
+ void xt_tls_bridge_boot(void) { xt_tls_backend_boot(); }
27
+ #endif
@@ -0,0 +1,39 @@
1
+ /*
2
+ * xbintsc `tls` link feature — the mbedTLS transport.
3
+ *
4
+ * This translation unit is compiled only when a program may use TLS (an
5
+ * `https://` URL, `node:tls`, or an explicit `--feature tls`). It installs the
6
+ * `XtTlsApi` table into the always-linked core registry from a constructor, so
7
+ * `fetch` and `node:tls` can fall back to a clear runtime error when the
8
+ * feature is not linked instead of failing at link time.
9
+ *
10
+ * The mbedTLS implementation itself lives in the `tls` archive
11
+ * (`runtime/lib/<os>-<arch>/tls.{a,lib}`), built by `npm run tls`. This file
12
+ * is the feature's `runtimeSources()` entry: linking it directly is what pulls
13
+ * the archive members it references out of the static library.
14
+ */
15
+
16
+ #if defined(_WIN32)
17
+ /* winsock2.h must precede windows.h; the configuration code below includes the
18
+ * latter for the certificate store API. */
19
+ #include <winsock2.h>
20
+ #include <ws2tcpip.h>
21
+ #endif
22
+
23
+ #include "../xt_tls_api.h"
24
+ #include "tls_internal.h"
25
+
26
+ #include <stdio.h>
27
+ #include <stdlib.h>
28
+ #include <string.h>
29
+
30
+ #include "mbedtls/error.h"
31
+
32
+ static int xt_tls_finish_handshake(xt_tls_conn *conn, int verify);
33
+ static void xt_tls_conn_close(xt_tls_conn *conn);
34
+
35
+ #include "parts/setup.inc"
36
+ #include "parts/io.inc"
37
+ #include "parts/client.inc"
38
+ #include "parts/server.inc"
39
+ #include "parts/api.inc"
@@ -0,0 +1,73 @@
1
+ /*
2
+ * Shared state for the mbedTLS-backed TLS backend (`tls_core.c`).
3
+ *
4
+ * One `xt_tls_setup` owns the configuration and credentials; a
5
+ * `xt_tls_conn` owns one handshake/stream. Servers build the setup once and
6
+ * borrow it for every accepted connection, clients own one per connection.
7
+ */
8
+
9
+ #ifndef XT_TLS_INTERNAL_H
10
+ #define XT_TLS_INTERNAL_H
11
+
12
+ #include <stddef.h>
13
+
14
+ #include "mbedtls/ctr_drbg.h"
15
+ #include "mbedtls/entropy.h"
16
+ #include "mbedtls/net_sockets.h"
17
+ #include "mbedtls/pk.h"
18
+ #include "mbedtls/ssl.h"
19
+ #include "mbedtls/x509_crt.h"
20
+
21
+ /** How many ALPN protocols a single configuration accepts. */
22
+ #define XT_TLS_MAX_ALPN 8
23
+ #define XT_TLS_ALPN_MAX 64
24
+
25
+ typedef struct xt_tls_setup {
26
+ mbedtls_ssl_config config;
27
+ mbedtls_entropy_context entropy;
28
+ mbedtls_ctr_drbg_context ctr_drbg;
29
+ mbedtls_x509_crt ca;
30
+ mbedtls_x509_crt cert;
31
+ mbedtls_pk_context key;
32
+ char alpn[XT_TLS_MAX_ALPN][XT_TLS_ALPN_MAX];
33
+ const char *alpn_list[XT_TLS_MAX_ALPN + 1];
34
+ int alpn_count;
35
+ int has_ca;
36
+ int has_cert;
37
+ int has_key;
38
+ int initialized;
39
+ } xt_tls_setup;
40
+
41
+ struct xt_tls_conn {
42
+ int fd;
43
+ mbedtls_ssl_context ssl;
44
+ /* Borrowed for accepted connections, owned for clients. */
45
+ xt_tls_setup *setup;
46
+ int owns_setup;
47
+ char error[256];
48
+ char alpn[XT_TLS_ALPN_MAX];
49
+ };
50
+
51
+ struct xt_tls_server_config {
52
+ xt_tls_setup setup;
53
+ int verify;
54
+ };
55
+
56
+ int xt_tls_platform_init(void);
57
+
58
+ /** Parse a comma-separated ALPN list into `setup` (empty list clears it). */
59
+ void xt_tls_setup_alpn(xt_tls_setup *setup, const char *list);
60
+
61
+ /** Load `ca_pem` or, when NULL, the host trust store. */
62
+ int xt_tls_setup_trust(xt_tls_setup *setup, const char *ca_pem, char *err, size_t errlen);
63
+
64
+ void xt_tls_setup_free(xt_tls_setup *setup);
65
+
66
+ int xt_tls_send_bio(void *ctx, const unsigned char *buf, size_t len);
67
+ int xt_tls_recv_bio(void *ctx, unsigned char *buf, size_t len);
68
+
69
+ /** Write a formatted error into `conn->error`. */
70
+ void xt_tls_conn_fail(xt_tls_conn *conn, const char *message, int code);
71
+ void xt_tls_conn_free(xt_tls_conn *conn);
72
+
73
+ #endif /* XT_TLS_INTERNAL_H */
@@ -0,0 +1,73 @@
1
+ /*
2
+ * Low-level TLS transport used by the core `fetch` implementation and by the
3
+ * Node `tls` module.
4
+ *
5
+ * The mbedTLS backend is an optional *link feature*: its archive is only linked
6
+ * when a program may use TLS, so this table is the only thing the always-linked
7
+ * runtime knows about it. `xt_protocol_registry.c` owns the (initially NULL)
8
+ * table; the feature's bridge installs the implementation in a constructor
9
+ * before `main` runs. When the table is NULL, `https://` requests reject and
10
+ * `node:tls` reports that TLS is unavailable, instead of failing at link time.
11
+ */
12
+
13
+ #ifndef XT_TLS_API_H
14
+ #define XT_TLS_API_H
15
+
16
+ #include <stddef.h>
17
+
18
+ #ifdef __cplusplus
19
+ extern "C" {
20
+ #endif
21
+
22
+ /** One established TLS connection (client or accepted server side). */
23
+ typedef struct xt_tls_conn xt_tls_conn;
24
+
25
+ /** Server credentials and verification policy, reusable across connections. */
26
+ typedef struct xt_tls_server_config xt_tls_server_config;
27
+
28
+ typedef struct xt_tls_api {
29
+ /**
30
+ * Connect a TCP socket and perform the TLS handshake, blocking until both
31
+ * complete. `verify` enables certificate verification; `ca_pem` replaces the
32
+ * system trust store when non-NULL. `cert_pem`/`key_pem` present a client
33
+ * certificate. `alpn` is a comma-separated protocol list. Returns NULL on
34
+ * failure and writes a human-readable reason into `err`.
35
+ */
36
+ xt_tls_conn *(*client_connect)(const char *host, int port, const char *servername, int verify,
37
+ const char *ca_pem, const char *cert_pem, const char *key_pem,
38
+ const char *alpn, char *err, size_t errlen);
39
+ /** Build the server credentials/verification policy from PEM strings. */
40
+ xt_tls_server_config *(*server_config)(const char *cert_pem, const char *key_pem,
41
+ const char *ca_pem, int request_cert, int verify,
42
+ const char *alpn, char *err, size_t errlen);
43
+ /** Wrap an accepted socket (blocking handshake); NULL on failure. */
44
+ xt_tls_conn *(*server_accept)(xt_tls_server_config *config, int fd, char *err, size_t errlen);
45
+ /** Decrypted read: positive byte count, 0 on orderly EOF, negative on error. */
46
+ int (*read)(xt_tls_conn *conn, unsigned char *buffer, size_t capacity);
47
+ /** Encrypt and write all bytes; returns 0 on success. */
48
+ int (*write)(xt_tls_conn *conn, const unsigned char *data, size_t length);
49
+ /** Send close_notify (best effort) without closing the descriptor. */
50
+ void (*shutdown)(xt_tls_conn *conn);
51
+ /** Send close_notify, close the descriptor and free `conn`. */
52
+ void (*close)(xt_tls_conn *conn);
53
+ /** Free `conn` without touching the descriptor (already closed). */
54
+ void (*conn_free)(xt_tls_conn *conn);
55
+ void (*config_free)(xt_tls_server_config *config);
56
+ /** Negotiated ALPN protocol, or NULL. */
57
+ const char *(*alpn)(xt_tls_conn *conn);
58
+ /** Last error message for `conn`, or NULL. */
59
+ const char *(*error)(xt_tls_conn *conn);
60
+ /** The underlying socket descriptor. */
61
+ int (*fd)(xt_tls_conn *conn);
62
+ } XtTlsApi;
63
+
64
+ /** The installed backend, or NULL when the `tls` link feature is not linked. */
65
+ extern const XtTlsApi *xt_tls_core_api;
66
+
67
+ void xt_tls_core_register(const XtTlsApi *api);
68
+
69
+ #ifdef __cplusplus
70
+ }
71
+ #endif
72
+
73
+ #endif /* XT_TLS_API_H */
@@ -12,22 +12,17 @@
12
12
  * Steps:
13
13
  * 1. fetch the pinned SDL3 source into `vendor/SDL` (gitignored),
14
14
  * 2. build `libSDL3.a` with CMake,
15
- * 2a. fetch + build static HarfBuzz and FreeType for the optional `shaping`
16
- * feature; the default text path uses the bundled stb_truetype,
17
- * 2b. fetch + build static glslang (GLSL ES -> SPIR-V) and SPIRV-Cross
18
- * (SPIR-V -> Metal Shading Language),
15
+ * 2a. fetch + build static HarfBuzz/FreeType (optional `shaping`) and
16
+ * glslang/SPIRV-Cross (GLSL ES -> SPIR-V -> Metal Shading Language),
19
17
  * 3. compile the engine translation units,
20
18
  * 4. merge everything into `gui.a` (libtool on macOS, `ar -M` elsewhere;
21
19
  * `gui.lib` on Windows, built from MSVC-compatible COFF objects).
22
- *
23
20
  * Media decoding is *not* part of this archive: it is an optional link feature
24
21
  * of the `gui` extension (`npm run media`, `runtime/xt_media/`), so a program
25
22
  * that only renders HTML/CSS never links FFmpeg.
26
- *
27
23
  * Every library and every engine object is compiled with function/data sections;
28
24
  * SDL is configured without the subsystems the engine never calls. The linker's
29
25
  * dead-section pass can then drop whatever a particular program does not reach.
30
- *
31
26
  * Environment:
32
27
  * xbintsc_CXX C++ compiler override (default: clang++ next to the resolved clang)
33
28
  * xbintsc_CMAKE CMake override (default: a vendored CMake, then `cmake`)
@@ -61,6 +56,7 @@ import {
61
56
  SECTION_FLAGS,
62
57
  } from "./gui-build-options.js";
63
58
  import { ensureDeploymentTarget } from "./gui-build-cache.js";
59
+ import { ensureVendoredSources } from "./fetch-vendored.js";
64
60
 
65
61
  const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
66
62
  const runtimeDir = join(root, "runtime");
@@ -106,6 +102,9 @@ const output = join(outDir, isWindows ? "gui.lib" : "gui.a");
106
102
  * oldest supported macOS, not the build machine's version. */
107
103
  applyMacosDeploymentTarget();
108
104
 
105
+ /* Fetch the stb/NanoSVG headers the engine's default text/image paths include. */
106
+ ensureVendoredSources(["stb", "nanosvg"]);
107
+
109
108
  const SDL_TAG = process.env.SDL3_TAG || "release-3.2.10";
110
109
  const FREETYPE_VERSION = process.env.FREETYPE_VERSION || "2.13.3";
111
110
  const HARFBUZZ_VERSION = process.env.HARFBUZZ_VERSION || "10.1.0";
@@ -522,6 +521,8 @@ if (metalBackend) {
522
521
  mkdirSync(objDir, { recursive: true });
523
522
  const includeFlags = [
524
523
  "-I", runtimeDir,
524
+ "-I", join(vendorDir, "stb"),
525
+ "-I", join(vendorDir, "nanosvg"),
525
526
  "-I", join(sdlSrc, "include"),
526
527
  "-I", join(freetypeSrc, "include"),
527
528
  "-I", join(freetypeBuild, "include"),
@@ -0,0 +1,248 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Build the archives behind the `tls` and `http2` link features:
4
+ *
5
+ * runtime/lib/<os>-<arch>/tls.{a,lib} mbedTLS (TLS 1.2/1.3, X.509, PSA)
6
+ * runtime/lib/<os>-<arch>/nghttp2.{a,lib} nghttp2 (HTTP/2 framing + HPACK)
7
+ *
8
+ * Both libraries are fetched as release tarballs into `vendor/` (gitignored,
9
+ * like SDL/FFmpeg for `gui`/`media`) and compiled with the same toolchain the
10
+ * driver uses, so the objects match the rest of the runtime:
11
+ *
12
+ * - Windows: MSVC-compatible COFF objects, merged by `llvm-ar` into `.lib`.
13
+ * - Linux/macOS: ELF/Mach-O objects, merged by `llvm-ar`/`ar` into `.a`.
14
+ *
15
+ * The C wrappers that *use* these archives (`runtime/xt_tls/tls_core.c`,
16
+ * `runtime/xt_node/tls/...`, `runtime/ext_node/http2/...`) are compiled on
17
+ * demand by the driver, so this script only produces the third-party half.
18
+ *
19
+ * Environment:
20
+ * xbintsc_TLS mbedTLS release tag (default 3.6.2)
21
+ * xbintsc_NGHTTP2 nghttp2 release tag (default 1.64.0)
22
+ * xbintsc_CLANG C compiler override
23
+ * xbintsc_AR archiver override
24
+ *
25
+ * Run `npm run tls` after changing either pin; the e2e suites that need the
26
+ * archives skip when they are absent.
27
+ */
28
+
29
+ import { existsSync, mkdirSync, readFileSync, readdirSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
30
+ import { spawnSync } from "node:child_process";
31
+ import { basename, dirname, join, resolve } from "node:path";
32
+ import { fileURLToPath } from "node:url";
33
+ import { platformSlug } from "../src/driver/paths.js";
34
+ import { applyMacosDeploymentTarget } from "../src/driver/platform-support.js";
35
+ import { resolveToolchain } from "../src/driver/toolchain-provider.js";
36
+ import { RUNTIME_CFLAGS, sysrootFlags } from "../src/driver/toolchain.js";
37
+
38
+ const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
39
+ const vendorDir = join(root, "vendor");
40
+ const outDir = join(root, "runtime", "lib", platformSlug());
41
+ const objRoot = join(root, "build", "tls-obj");
42
+ const isWindows = process.platform === "win32";
43
+ const archiveSuffix = isWindows ? ".lib" : ".a";
44
+
45
+ const MBEDTLS_VERSION = process.env.xbintsc_TLS ?? "3.6.2";
46
+ const NGHTTP2_VERSION = process.env.xbintsc_NGHTTP2 ?? "1.64.0";
47
+
48
+ applyMacosDeploymentTarget();
49
+
50
+ const toolchain = resolveToolchain();
51
+ const clang = toolchain.clang;
52
+ const childEnv = Object.assign({}, process.env, toolchain.env);
53
+
54
+ function fail(message: string): never {
55
+ console.error(`xbintsc: ${message}`);
56
+ process.exit(1);
57
+ }
58
+
59
+ function run(command: string, args: readonly string[]): void {
60
+ const result = spawnSync(command, [...args], { stdio: "inherit", env: childEnv });
61
+ if (result.error) fail(`failed to run ${command}: ${result.error.message}`);
62
+ if (result.status !== 0) fail(`${command} ${args.join(" ")} failed (${result.status})`);
63
+ }
64
+
65
+ function isRunnable(candidate: string): boolean {
66
+ return !spawnSync(candidate, [], { encoding: "utf8" }).error;
67
+ }
68
+
69
+ function findArchiver(clangPath: string): string {
70
+ const dir = dirname(clangPath);
71
+ const exe = isWindows ? ".exe" : "";
72
+ const candidates = [
73
+ process.env.xbintsc_AR,
74
+ dir === "." ? undefined : join(dir, `llvm-ar${exe}`),
75
+ "llvm-ar",
76
+ isWindows ? undefined : "ar",
77
+ ].filter((candidate): candidate is string => Boolean(candidate));
78
+ for (const candidate of candidates) {
79
+ if (isRunnable(candidate)) return candidate;
80
+ }
81
+ return fail("no archiver found; install llvm-ar or set xbintsc_AR to it");
82
+ }
83
+
84
+ /** Download a release tarball and extract it under `vendor/<name>`. */
85
+ function ensureSource(name: string, url: string, extractedName: string, marker: string): string {
86
+ const destination = join(vendorDir, name);
87
+ if (existsSync(join(destination, marker))) return destination;
88
+ const tarball = join(vendorDir, `${name}.tar.gz`);
89
+ console.log(`xbintsc: fetching ${name} from ${url}`);
90
+ mkdirSync(vendorDir, { recursive: true });
91
+ run("curl", ["-sSL", "--fail", "--max-time", "300", "-o", tarball, url]);
92
+ run("tar", ["-xzf", tarball, "-C", vendorDir]);
93
+ const extracted = join(vendorDir, extractedName);
94
+ if (!existsSync(extracted)) fail(`expected ${extractedName} after extracting ${name}`);
95
+ rmSync(destination, { recursive: true, force: true });
96
+ renameSync(extracted, destination);
97
+ rmSync(tarball, { force: true });
98
+ return destination;
99
+ }
100
+
101
+ interface LibraryBuild {
102
+ readonly name: string;
103
+ readonly sources: readonly string[];
104
+ readonly includeDirs: readonly string[];
105
+ readonly defines: readonly string[];
106
+ }
107
+
108
+ /** Newest modification time of the inputs, used to reuse a fresh archive. */
109
+ function newestSource(sources: readonly string[], dirs: readonly string[]): number {
110
+ let newest = 0;
111
+ const visit = (path: string): void => {
112
+ const info = statSync(path);
113
+ if (info.isDirectory()) {
114
+ for (const entry of readdirSync(path)) visit(join(path, entry));
115
+ return;
116
+ }
117
+ if (info.mtimeMs > newest) newest = info.mtimeMs;
118
+ };
119
+ for (const source of sources) visit(source);
120
+ for (const dir of dirs) visit(dir);
121
+ return newest;
122
+ }
123
+
124
+ function archiveIsFresh(library: LibraryBuild, output: string): boolean {
125
+ if (!existsSync(output)) return false;
126
+ try {
127
+ return statSync(output).mtimeMs >= newestSource(library.sources, library.includeDirs);
128
+ } catch {
129
+ return false;
130
+ }
131
+ }
132
+
133
+ function compileLibrary(library: LibraryBuild, output: string, ar: string): void {
134
+ if (archiveIsFresh(library, output)) {
135
+ console.log(`xbintsc: reusing existing ${output}`);
136
+ return;
137
+ }
138
+ const objDir = join(objRoot, library.name);
139
+ rmSync(objDir, { recursive: true, force: true });
140
+ mkdirSync(objDir, { recursive: true });
141
+ const objects: string[] = [];
142
+ library.sources.forEach((source, index) => {
143
+ const object = join(objDir, `${index}_${basename(source, ".c")}.o`);
144
+ const args = [
145
+ ...RUNTIME_CFLAGS,
146
+ ...sysrootFlags(toolchain.sdkRoot),
147
+ ...library.defines,
148
+ ...library.includeDirs.map((dir) => `-I${dir}`),
149
+ "-c",
150
+ source,
151
+ "-o",
152
+ object,
153
+ ];
154
+ const result = spawnSync(clang, args, { stdio: ["ignore", "ignore", "pipe"], env: childEnv, encoding: "utf8" });
155
+ if (result.status !== 0) {
156
+ process.stderr.write(result.stderr ?? "");
157
+ fail(`failed to compile ${source}`);
158
+ }
159
+ objects.push(object);
160
+ });
161
+ mkdirSync(dirname(output), { recursive: true });
162
+ rmSync(output, { force: true });
163
+ run(ar, ["rcs", output, ...objects]);
164
+ console.log(`xbintsc: wrote ${output}`);
165
+ }
166
+
167
+ function cSources(dir: string): string[] {
168
+ return readdirSync(dir)
169
+ .filter((name) => name.endsWith(".c"))
170
+ .sort()
171
+ .map((name) => join(dir, name));
172
+ }
173
+
174
+ /* -- mbedTLS -------------------------------------------------------------- */
175
+
176
+ const mbedtlsDir = ensureSource(
177
+ "mbedtls",
178
+ `https://codeload.github.com/Mbed-TLS/mbedtls/tar.gz/refs/tags/mbedtls-${MBEDTLS_VERSION}`,
179
+ `mbedtls-mbedtls-${MBEDTLS_VERSION}`,
180
+ join("library", "ssl_tls.c"),
181
+ );
182
+ const mbedtlsLibrary: LibraryBuild = {
183
+ name: "tls",
184
+ // The backend wrapper is archived too: its bridge (`tls_bridge.c`) is what
185
+ // the driver compiles, so an installed npm package needs no vendor headers.
186
+ sources: [...cSources(join(mbedtlsDir, "library")), join(root, "runtime", "xt_tls", "tls_core.c")],
187
+ includeDirs: [join(mbedtlsDir, "include"), join(mbedtlsDir, "library"), join(root, "runtime")],
188
+ defines: [],
189
+ };
190
+
191
+ /* -- nghttp2 -------------------------------------------------------------- */
192
+
193
+ const nghttp2Dir = ensureSource(
194
+ "nghttp2",
195
+ `https://codeload.github.com/nghttp2/nghttp2/tar.gz/refs/tags/v${NGHTTP2_VERSION}`,
196
+ `nghttp2-${NGHTTP2_VERSION}`,
197
+ join("lib", "nghttp2_session.c"),
198
+ );
199
+ /**
200
+ * nghttp2's public headers expect a generated `nghttp2ver.h`; generate it
201
+ * exactly as its autotools build does. The number is the 24-bit
202
+ * major/minor/patch representation (1.64.0 -> 0x014000).
203
+ */
204
+ const versionParts = NGHTTP2_VERSION.split(".").map((part) => Number(part) || 0);
205
+ const ngHeader = join(nghttp2Dir, "lib", "includes", "nghttp2", "nghttp2ver.h");
206
+ const ngTemplate = join(nghttp2Dir, "lib", "includes", "nghttp2", "nghttp2ver.h.in");
207
+ const versionNum = `0x${versionParts
208
+ .map((part) => part.toString(16).padStart(2, "0"))
209
+ .join("")}`;
210
+ writeFileSync(
211
+ ngHeader,
212
+ readFileSync(ngTemplate, "utf8")
213
+ .replaceAll("@PACKAGE_VERSION@", NGHTTP2_VERSION)
214
+ .replaceAll("@PACKAGE_VERSION_NUM@", versionNum),
215
+ );
216
+
217
+ const nghttp2Library: LibraryBuild = {
218
+ name: "nghttp2",
219
+ // `nghttp2_alpn.c` only wraps NPN/ALPN helper callbacks for TLS backends; it
220
+ // is part of the public library surface, so keep it.
221
+ sources: [
222
+ ...cSources(join(nghttp2Dir, "lib")),
223
+ join(root, "runtime", "ext_node", "http2", "http2_backend.c"),
224
+ ],
225
+ includeDirs: [
226
+ join(nghttp2Dir, "lib", "includes"),
227
+ join(nghttp2Dir, "lib"),
228
+ join(root, "runtime"),
229
+ ],
230
+ // nghttp2's public header uses POSIX `ssize_t`; MSVC/UCRT has none.
231
+ defines: [
232
+ "-DNGHTTP2_STATICLIB",
233
+ // `nghttp2_net.h` only includes `<arpa/inet.h>`/`<netinet/in.h>` when its
234
+ // generated `config.h` (which this build has none of) says the host has
235
+ // them. Without those includes `htonl`/`htons` are implicit, which clang
236
+ // 16 and newer reject in C99 mode; declare them on every POSIX host.
237
+ // Windows takes `nghttp2_net.h`'s `WIN32` branch, which defines the
238
+ // byte-order helpers inline.
239
+ ...(isWindows ? [] : ["-DHAVE_ARPA_INET_H", "-DHAVE_NETINET_IN_H"]),
240
+ "-include",
241
+ join(root, "runtime", "xt_http2", "nghttp2_compat.h"),
242
+ ],
243
+ };
244
+
245
+ console.log(`xbintsc: TLS toolchain ${clang} (${toolchain.source})`);
246
+ const ar = findArchiver(clang);
247
+ compileLibrary(mbedtlsLibrary, join(outDir, `tls${archiveSuffix}`), ar);
248
+ compileLibrary(nghttp2Library, join(outDir, `nghttp2${archiveSuffix}`), ar);