xbintsc 0.6.22 → 0.6.23
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.
- package/AGENTS.md +5 -0
- package/dist/src/cli/help.d.ts +1 -1
- package/dist/src/driver/build-key.js +2 -1
- package/dist/src/driver/build-key.js.map +1 -1
- package/dist/src/driver/fingerprint.d.ts +11 -0
- package/dist/src/driver/fingerprint.js +25 -0
- package/dist/src/driver/fingerprint.js.map +1 -1
- package/dist/src/driver/link-features.js +36 -11
- package/dist/src/driver/link-features.js.map +1 -1
- package/dist/src/driver/paths.d.ts +10 -0
- package/dist/src/driver/paths.js +18 -0
- package/dist/src/driver/paths.js.map +1 -1
- package/dist/src/driver/runtime-objects.d.ts +1 -1
- package/dist/src/driver/runtime-objects.js +22 -6
- package/dist/src/driver/runtime-objects.js.map +1 -1
- package/dist/src/driver/toolchain.d.ts +1 -1
- package/dist/src/driver/toolchain.js +10 -1
- package/dist/src/driver/toolchain.js.map +1 -1
- package/dist/src/driver/version.d.ts +1 -1
- package/dist/src/driver/version.js +1 -1
- package/dist/src/extensions/node/http2/feature.d.ts +14 -0
- package/dist/src/extensions/node/http2/feature.js +28 -0
- package/dist/src/extensions/node/http2/feature.js.map +1 -0
- package/dist/src/extensions/node/http2/index.d.ts +5 -1
- package/dist/src/extensions/node/http2/index.js +6 -1
- package/dist/src/extensions/node/http2/index.js.map +1 -1
- package/dist/src/extensions/node/index.js +3 -1
- package/dist/src/extensions/node/index.js.map +1 -1
- package/dist/src/extensions/node/tls/feature.d.ts +12 -0
- package/dist/src/extensions/node/tls/feature.js +24 -0
- package/dist/src/extensions/node/tls/feature.js.map +1 -0
- package/dist/src/extensions/node/tls/index.d.ts +5 -1
- package/dist/src/extensions/node/tls/index.js +5 -1
- package/dist/src/extensions/node/tls/index.js.map +1 -1
- package/dist/src/extensions/node/zlib/compression.js +11 -6
- package/dist/src/extensions/node/zlib/compression.js.map +1 -1
- package/dist/src/extensions/registry.js +2 -1
- package/dist/src/extensions/registry.js.map +1 -1
- package/dist/src/extensions/storage.js +4 -3
- package/dist/src/extensions/storage.js.map +1 -1
- package/dist/src/extensions/tls.d.ts +26 -0
- package/dist/src/extensions/tls.js +46 -0
- package/dist/src/extensions/tls.js.map +1 -0
- package/dist/tests/driver/link-features.test.js +9 -0
- package/dist/tests/driver/link-features.test.js.map +1 -1
- package/dist/tests/e2e/fetch.test.js +3 -1
- package/dist/tests/e2e/fetch.test.js.map +1 -1
- package/dist/tests/e2e/http2.test.d.ts +7 -0
- package/dist/tests/e2e/http2.test.js +201 -0
- package/dist/tests/e2e/http2.test.js.map +1 -0
- package/dist/tests/e2e/net-helpers.d.ts +28 -0
- package/dist/tests/e2e/net-helpers.js +59 -0
- package/dist/tests/e2e/net-helpers.js.map +1 -0
- package/dist/tests/e2e/node-modules.test.js +3 -5
- package/dist/tests/e2e/node-modules.test.js.map +1 -1
- package/dist/tests/e2e/tls.test.d.ts +10 -0
- package/dist/tests/e2e/tls.test.js +197 -0
- package/dist/tests/e2e/tls.test.js.map +1 -0
- package/dist/tests/extensions/storage.test.js +3 -2
- package/dist/tests/extensions/storage.test.js.map +1 -1
- package/doc/ai/contributing.md +14 -0
- package/doc/ai/extensions.md +5 -3
- package/doc/ai/language-support.md +1 -1
- package/doc/ai/zh-CN/contributing.md +12 -0
- package/doc/ai/zh-CN/extensions.md +4 -2
- package/doc/ai/zh-CN/language-support.md +1 -1
- package/doc/gui/api.md +1 -1
- package/doc/gui/build.md +5 -3
- package/doc/gui/rendering.md +1 -1
- package/doc/gui.md +1 -1
- package/doc/implemented/async.md +1 -1
- package/doc/implemented/extensions.md +1 -0
- package/doc/implemented/fetch.md +19 -4
- package/doc/implemented/node/http2.md +51 -0
- package/doc/implemented/node/tls.md +56 -0
- package/doc/implemented/node.md +8 -0
- package/doc/implemented/storage.md +2 -1
- package/doc/implemented.md +2 -2
- package/doc/unimplemented/node/modules.md +6 -6
- package/doc/unimplemented/toolchain.md +1 -1
- package/doc/zh-CN/gui/api.md +1 -1
- package/doc/zh-CN/gui/build.md +4 -2
- package/doc/zh-CN/gui/rendering.md +1 -1
- package/doc/zh-CN/gui.md +1 -1
- package/doc/zh-CN/implemented/async.md +1 -1
- package/doc/zh-CN/implemented/extensions.md +1 -0
- package/doc/zh-CN/implemented/fetch.md +15 -3
- package/doc/zh-CN/implemented/node/http2.md +44 -0
- package/doc/zh-CN/implemented/node/tls.md +47 -0
- package/doc/zh-CN/implemented/node.md +7 -0
- package/doc/zh-CN/implemented/storage.md +2 -1
- package/doc/zh-CN/implemented.md +2 -2
- package/doc/zh-CN/unimplemented/node/modules.md +3 -3
- package/doc/zh-CN/unimplemented/toolchain.md +1 -1
- package/package.json +16 -8
- package/runtime/ext_node/http2/http2.c +25 -11
- package/runtime/ext_node/http2/http2_backend.c +48 -0
- package/runtime/ext_node/http2/http2_bridge.c +25 -0
- package/runtime/ext_node/http2/parts/api.inc +40 -0
- package/runtime/ext_node/http2/parts/client.inc +215 -0
- package/runtime/ext_node/http2/parts/common.inc +330 -0
- package/runtime/ext_node/http2/parts/events.inc +139 -0
- package/runtime/ext_node/http2/parts/server.inc +301 -0
- package/runtime/ext_node/http2/parts/streams.inc +254 -0
- package/runtime/ext_node/tls/parts/api.inc +38 -0
- package/runtime/ext_node/tls/parts/connect.inc +107 -0
- package/runtime/ext_node/tls/parts/helpers.inc +121 -0
- package/runtime/ext_node/tls/parts/server.inc +101 -0
- package/runtime/ext_node/tls/parts/socket.inc +171 -0
- package/runtime/ext_node/tls/tls.c +21 -13
- package/runtime/ext_node/tls/tls_backend.c +50 -0
- package/runtime/ext_node/zlib/{miniz/compression_bridge.c → compression_bridge.c} +1 -1
- package/runtime/xt_fetch/async.inc +19 -12
- package/runtime/xt_fetch/socket.inc +55 -0
- package/runtime/xt_fetch.c +3 -0
- package/runtime/xt_http2/nghttp2_compat.h +19 -0
- package/runtime/xt_http2/nghttp2_static.h +18 -0
- package/runtime/xt_http2_api.h +38 -0
- package/runtime/xt_node_tls_api.h +37 -0
- package/runtime/xt_protocol_registry.c +22 -0
- package/runtime/xt_storage/storage_internal.h +3 -3
- package/runtime/xt_tls/parts/api.inc +21 -0
- package/runtime/xt_tls/parts/client.inc +112 -0
- package/runtime/xt_tls/parts/io.inc +157 -0
- package/runtime/xt_tls/parts/server.inc +102 -0
- package/runtime/xt_tls/parts/setup.inc +146 -0
- package/runtime/xt_tls/tls_bridge.c +27 -0
- package/runtime/xt_tls/tls_core.c +39 -0
- package/runtime/xt_tls/tls_internal.h +73 -0
- package/runtime/xt_tls_api.h +73 -0
- package/scripts/build-gui.ts +8 -7
- package/scripts/build-tls.ts +248 -0
- package/scripts/check-file-length.ts +4 -25
- package/scripts/fetch-vendored.ts +244 -0
- package/scripts/package-release.ts +13 -3
- package/src/driver/build-key.ts +7 -1
- package/src/driver/fingerprint.ts +24 -0
- package/src/driver/link-features.ts +33 -11
- package/src/driver/paths.ts +20 -0
- package/src/driver/runtime-objects.ts +25 -6
- package/src/driver/toolchain.ts +11 -2
- package/src/driver/version.ts +1 -1
- package/src/extensions/node/http2/feature.ts +33 -0
- package/src/extensions/node/http2/index.ts +6 -1
- package/src/extensions/node/index.ts +3 -1
- package/src/extensions/node/tls/feature.ts +29 -0
- package/src/extensions/node/tls/index.ts +5 -1
- package/src/extensions/node/zlib/compression.ts +11 -6
- package/src/extensions/registry.ts +2 -1
- package/src/extensions/storage.ts +4 -3
- package/src/extensions/tls.ts +52 -0
- package/{runtime/ext_gui → vendor/stb}/stb_image.h +17 -1
- /package/{runtime/ext_node/zlib → vendor}/miniz/miniz.c +0 -0
- /package/{runtime/ext_node/zlib → vendor}/miniz/miniz.h +0 -0
- /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_common.h +0 -0
- /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_export.h +0 -0
- /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_tdef.c +0 -0
- /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_tdef.h +0 -0
- /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_tinfl.c +0 -0
- /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_tinfl.h +0 -0
- /package/{runtime/ext_node/zlib → vendor}/miniz/miniz_zip.h +0 -0
- /package/{runtime/ext_gui → vendor/nanosvg}/nanosvg.h +0 -0
- /package/{runtime/ext_gui → vendor/nanosvg}/nanosvgrast.h +0 -0
- /package/{runtime/xt_storage → vendor}/sqlite3/sqlite3.c +0 -0
- /package/{runtime/xt_storage → vendor}/sqlite3/sqlite3.h +0 -0
- /package/{runtime/ext_gui → vendor/stb}/stb_truetype.h +0 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "xbintsc",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.23",
|
|
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.
|
|
76
|
-
"@xbintsc/linux-arm64": "0.6.
|
|
77
|
-
"@xbintsc/darwin-x64": "0.6.
|
|
78
|
-
"@xbintsc/darwin-arm64": "0.6.
|
|
79
|
-
"@xbintsc/win32-x64": "0.6.
|
|
80
|
-
"@xbintsc/win32-arm64": "0.6.
|
|
83
|
+
"@xbintsc/linux-x64": "0.6.23",
|
|
84
|
+
"@xbintsc/linux-arm64": "0.6.23",
|
|
85
|
+
"@xbintsc/darwin-x64": "0.6.23",
|
|
86
|
+
"@xbintsc/darwin-arm64": "0.6.23",
|
|
87
|
+
"@xbintsc/win32-x64": "0.6.23",
|
|
88
|
+
"@xbintsc/win32-arm64": "0.6.23"
|
|
81
89
|
},
|
|
82
90
|
"engines": {
|
|
83
91
|
"node": ">=22"
|
|
@@ -1,26 +1,40 @@
|
|
|
1
1
|
/*
|
|
2
|
-
* Node.js `http2` module for xbintsc —
|
|
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
|
|
8
|
-
(
|
|
9
|
-
|
|
10
|
-
|
|
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
|
-
(
|
|
16
|
-
(
|
|
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
|
-
|
|
25
|
-
return
|
|
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
|
+
}
|