xbintsc 0.6.20 → 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/cli/main.js +1 -0
- package/dist/src/cli/main.js.map +1 -1
- package/dist/src/driver/build-key.d.ts +3 -0
- package/dist/src/driver/build-key.js +5 -2
- package/dist/src/driver/build-key.js.map +1 -1
- package/dist/src/driver/compiler.js +11 -0
- package/dist/src/driver/compiler.js.map +1 -1
- package/dist/src/driver/config.d.ts +12 -0
- package/dist/src/driver/config.js +22 -0
- package/dist/src/driver/config.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/index.d.ts +2 -1
- package/dist/src/driver/index.js +1 -0
- package/dist/src/driver/index.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/options.d.ts +8 -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/storage.d.ts +34 -0
- package/dist/src/driver/storage.js +67 -0
- package/dist/src/driver/storage.js.map +1 -0
- 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/gui/features.d.ts +1 -1
- package/dist/src/extensions/gui/features.js +2 -2
- package/dist/src/extensions/gui/features.js.map +1 -1
- package/dist/src/extensions/gui/index.js +3 -1
- package/dist/src/extensions/gui/index.js.map +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.d.ts +4 -1
- package/dist/src/extensions/registry.js +9 -2
- package/dist/src/extensions/registry.js.map +1 -1
- package/dist/src/extensions/storage.d.ts +22 -0
- package/dist/src/extensions/storage.js +51 -0
- package/dist/src/extensions/storage.js.map +1 -0
- 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/config.test.js +31 -0
- package/dist/tests/driver/config.test.js.map +1 -1
- package/dist/tests/driver/link-features.test.js +13 -2
- package/dist/tests/driver/link-features.test.js.map +1 -1
- package/dist/tests/driver/storage.test.d.ts +1 -0
- package/dist/tests/driver/storage.test.js +29 -0
- package/dist/tests/driver/storage.test.js.map +1 -0
- 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/storage.test.d.ts +7 -0
- package/dist/tests/e2e/storage.test.js +86 -0
- package/dist/tests/e2e/storage.test.js.map +1 -0
- 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/gui-features.test.js +1 -1
- package/dist/tests/extensions/gui-features.test.js.map +1 -1
- package/dist/tests/extensions/storage.test.d.ts +1 -0
- package/dist/tests/extensions/storage.test.js +51 -0
- package/dist/tests/extensions/storage.test.js.map +1 -0
- package/doc/ai/cli.md +2 -0
- package/doc/ai/contributing.md +14 -0
- package/doc/ai/extensions.md +31 -3
- package/doc/ai/language-support.md +1 -1
- package/doc/ai/zh-CN/cli.md +2 -0
- package/doc/ai/zh-CN/contributing.md +12 -0
- package/doc/ai/zh-CN/extensions.md +27 -2
- package/doc/ai/zh-CN/language-support.md +1 -1
- package/doc/gui/api.md +1 -1
- package/doc/gui/build.md +7 -5
- package/doc/gui/media.md +4 -4
- package/doc/gui/rendering.md +1 -1
- package/doc/gui.md +3 -3
- package/doc/implemented/async.md +1 -1
- package/doc/implemented/extensions.md +2 -1
- 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 +65 -0
- package/doc/implemented.md +4 -2
- package/doc/unimplemented/node/modules.md +6 -6
- package/doc/unimplemented/toolchain.md +1 -1
- package/doc/xbintsc.config.schema.json +16 -1
- package/doc/zh-CN/gui/api.md +1 -1
- package/doc/zh-CN/gui/build.md +6 -4
- package/doc/zh-CN/gui/media.md +3 -3
- package/doc/zh-CN/gui/rendering.md +1 -1
- package/doc/zh-CN/gui.md +3 -3
- package/doc/zh-CN/implemented/async.md +1 -1
- package/doc/zh-CN/implemented/extensions.md +2 -1
- 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 +52 -0
- package/doc/zh-CN/implemented.md +4 -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_gui/dom_api_form.cpp +10 -0
- package/runtime/ext_gui/gui_media.cpp +1 -1
- package/runtime/ext_gui/media.cpp +1 -1
- package/runtime/ext_gui/media_api.h +2 -2
- 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/indexed_db.c +408 -0
- package/runtime/xt_storage/indexed_db_internal.h +26 -0
- package/runtime/xt_storage/indexed_db_store.c +305 -0
- package/runtime/xt_storage/storage.c +209 -0
- package/runtime/xt_storage/storage_internal.h +66 -0
- 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 +9 -8
- package/scripts/build-media.ts +2 -2
- package/scripts/build-tls.ts +248 -0
- package/scripts/check-file-length.ts +4 -21
- package/scripts/fetch-vendored.ts +244 -0
- package/scripts/package-release.ts +13 -3
- package/src/cli/main.ts +1 -0
- package/src/driver/build-key.ts +13 -2
- package/src/driver/compiler.ts +13 -0
- package/src/driver/config.ts +35 -0
- package/src/driver/fingerprint.ts +24 -0
- package/src/driver/index.ts +8 -0
- package/src/driver/link-features.ts +33 -11
- package/src/driver/options.ts +8 -1
- package/src/driver/paths.ts +20 -0
- package/src/driver/runtime-objects.ts +25 -6
- package/src/driver/storage.ts +84 -0
- package/src/driver/toolchain.ts +11 -2
- package/src/driver/version.ts +1 -1
- package/src/extensions/gui/features.ts +2 -2
- package/src/extensions/gui/index.ts +3 -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 +9 -2
- package/src/extensions/storage.ts +58 -0
- package/src/extensions/tls.ts +52 -0
- package/vendor/sqlite3/sqlite3.c +271131 -0
- package/vendor/sqlite3/sqlite3.h +14447 -0
- package/{runtime/ext_gui → vendor/stb}/stb_image.h +17 -1
- /package/runtime/{ext_media → xt_media}/decode_audio.cpp +0 -0
- /package/runtime/{ext_media → xt_media}/media.cpp +0 -0
- /package/runtime/{ext_media → xt_media}/media_bridge.c +0 -0
- /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/ext_gui → vendor/stb}/stb_truetype.h +0 -0
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
/* -- server: h2c and TLS+ALPN --------------------------------------------- */
|
|
2
|
+
|
|
3
|
+
static xt_value xt_h2_start_server(xt_h2_conn *conn, xt_value owner) {
|
|
4
|
+
conn->is_server = 1;
|
|
5
|
+
conn->owner = owner;
|
|
6
|
+
snprintf(conn->scheme, sizeof(conn->scheme), "%s", conn->tls != NULL ? "https" : "http");
|
|
7
|
+
snprintf(conn->authority, sizeof(conn->authority), "%s", "localhost");
|
|
8
|
+
nghttp2_session_callbacks *callbacks = NULL;
|
|
9
|
+
nghttp2_session_callbacks_new(&callbacks);
|
|
10
|
+
xt_h2_install_callbacks(callbacks);
|
|
11
|
+
nghttp2_session_server_new(&conn->session, callbacks, conn);
|
|
12
|
+
nghttp2_session_callbacks_del(callbacks);
|
|
13
|
+
nghttp2_submit_settings(conn->session, NGHTTP2_FLAG_NONE, NULL, 0);
|
|
14
|
+
conn->session_value = xt_h2_session_object(conn);
|
|
15
|
+
/* A server session is never returned to JavaScript, so keep it reachable from
|
|
16
|
+
* the server object (`server.__sessions`); otherwise the collector may free
|
|
17
|
+
* it while nghttp2 callbacks still need it. */
|
|
18
|
+
if (XT_IS_OBJECT(owner)) {
|
|
19
|
+
xt_value sessions = xt_object_get_cstr(owner, "__sessions");
|
|
20
|
+
if (!XT_IS_ARRAY(sessions)) {
|
|
21
|
+
sessions = xt_array_new(0, NULL);
|
|
22
|
+
xt_node_set(owner, "__sessions", sessions);
|
|
23
|
+
}
|
|
24
|
+
xt_array_push(sessions, conn->session_value);
|
|
25
|
+
}
|
|
26
|
+
xt_h2_attach(conn, conn->session_value);
|
|
27
|
+
return conn->session_value;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
static void xt_h2_server_adopt_socket(xt_value socket, int *fd, xt_tls_conn **tls) {
|
|
31
|
+
*fd = (int)xt_to_number(xt_object_get_cstr(socket, "__fd"));
|
|
32
|
+
*tls = NULL;
|
|
33
|
+
if (*fd >= 0) xt_loop_remove(*fd);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/* `net` connection: cleartext HTTP/2 with prior knowledge (h2c). */
|
|
37
|
+
static xt_value xt_h2_server_connection(xt_value server, xt_value env, int32_t argc,
|
|
38
|
+
xt_value *argv) {
|
|
39
|
+
(void)env;
|
|
40
|
+
if (argc < 1) return XT_UNDEFINED;
|
|
41
|
+
xt_value socket = argv[0];
|
|
42
|
+
int fd = -1;
|
|
43
|
+
xt_tls_conn *tls = NULL;
|
|
44
|
+
xt_h2_server_adopt_socket(socket, &fd, &tls);
|
|
45
|
+
if (fd < 0) return XT_UNDEFINED;
|
|
46
|
+
xt_node_set(socket, "__fd", xt_number(-1));
|
|
47
|
+
xt_node_set(socket, "__closed", xt_bool(1));
|
|
48
|
+
xt_h2_conn *conn = (xt_h2_conn *)calloc(1, sizeof(xt_h2_conn));
|
|
49
|
+
if (conn == NULL) {
|
|
50
|
+
xt_h2_closesocket(fd);
|
|
51
|
+
return XT_UNDEFINED;
|
|
52
|
+
}
|
|
53
|
+
conn->fd = fd;
|
|
54
|
+
conn->tls = tls;
|
|
55
|
+
xt_value session = xt_h2_start_server(conn, server);
|
|
56
|
+
if (!conn->closed) xt_node_emit1(server, "session", session);
|
|
57
|
+
return XT_UNDEFINED;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/* `tls` secureConnection: HTTP/2 over TLS (ALPN negotiated `h2`). */
|
|
61
|
+
static xt_value xt_h2_secure_connection(xt_value server, xt_value env, int32_t argc,
|
|
62
|
+
xt_value *argv) {
|
|
63
|
+
(void)env;
|
|
64
|
+
if (argc < 1) return XT_UNDEFINED;
|
|
65
|
+
xt_value socket = argv[0];
|
|
66
|
+
int fd = -1;
|
|
67
|
+
xt_tls_conn *tls = NULL;
|
|
68
|
+
xt_h2_server_adopt_socket(socket, &fd, &tls);
|
|
69
|
+
if (fd < 0) return XT_UNDEFINED;
|
|
70
|
+
tls = (xt_tls_conn *)(uintptr_t)xt_to_number(xt_object_get_cstr(socket, "__conn"));
|
|
71
|
+
xt_node_set(socket, "__conn", xt_number(0));
|
|
72
|
+
xt_node_set(socket, "__fd", xt_number(-1));
|
|
73
|
+
xt_node_set(socket, "__closed", xt_bool(1));
|
|
74
|
+
if (tls == NULL) {
|
|
75
|
+
xt_h2_closesocket(fd);
|
|
76
|
+
return XT_UNDEFINED;
|
|
77
|
+
}
|
|
78
|
+
xt_h2_conn *conn = (xt_h2_conn *)calloc(1, sizeof(xt_h2_conn));
|
|
79
|
+
if (conn == NULL) {
|
|
80
|
+
xt_tls_core_api->close(tls);
|
|
81
|
+
return XT_UNDEFINED;
|
|
82
|
+
}
|
|
83
|
+
conn->fd = fd;
|
|
84
|
+
conn->tls = tls;
|
|
85
|
+
xt_value session = xt_h2_start_server(conn, server);
|
|
86
|
+
if (!conn->closed) xt_node_emit1(server, "session", session);
|
|
87
|
+
return XT_UNDEFINED;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
static xt_value xt_h2_attach_handler(xt_value server, xt_value handler) {
|
|
91
|
+
if (XT_IS_FUNCTION(handler)) {
|
|
92
|
+
xt_array_push(xt_node_listeners(server, "stream", 1), handler);
|
|
93
|
+
}
|
|
94
|
+
return server;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
xt_value xt_h2_create_server(int32_t argc, xt_value *argv) {
|
|
98
|
+
xt_value handler = xt_undefined();
|
|
99
|
+
for (int32_t i = 0; i < argc; i++) {
|
|
100
|
+
if (XT_IS_FUNCTION(argv[i]) && handler == xt_undefined()) handler = argv[i];
|
|
101
|
+
}
|
|
102
|
+
xt_value server = xt_net_static(xt_string_from_cstr("createServer"), 0, NULL);
|
|
103
|
+
xt_array_push(xt_node_listeners(server, "connection", 1),
|
|
104
|
+
xt_closure_new((void *)xt_h2_server_connection, 0, NULL));
|
|
105
|
+
return xt_h2_attach_handler(server, handler);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
xt_value xt_h2_create_secure_server(int32_t argc, xt_value *argv) {
|
|
109
|
+
if (xt_tls_node_api == NULL || xt_tls_core_api == NULL) {
|
|
110
|
+
xt_throw(xt_h2_error("createSecureServer requires the node-tls and tls link features"));
|
|
111
|
+
return XT_UNDEFINED;
|
|
112
|
+
}
|
|
113
|
+
xt_value options = xt_object_new();
|
|
114
|
+
xt_value handler = xt_undefined();
|
|
115
|
+
for (int32_t i = 0; i < argc; i++) {
|
|
116
|
+
if (XT_IS_OBJECT(argv[i])) {
|
|
117
|
+
xt_value keys = xt_object_keys(argv[i]);
|
|
118
|
+
int32_t count = (int32_t)xt_to_number(xt_array_length(keys));
|
|
119
|
+
for (int32_t k = 0; k < count; k++) {
|
|
120
|
+
xt_value key = xt_array_get(keys, xt_number((double)k));
|
|
121
|
+
xt_object_set(options, key, xt_object_get(argv[i], key));
|
|
122
|
+
}
|
|
123
|
+
} else if (XT_IS_FUNCTION(argv[i]) && handler == xt_undefined()) {
|
|
124
|
+
handler = argv[i];
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
xt_value protocols = xt_array_new(0, NULL);
|
|
128
|
+
xt_array_push(protocols, xt_string_from_cstr("h2"));
|
|
129
|
+
xt_node_set(options, "ALPNProtocols", protocols);
|
|
130
|
+
xt_value arguments[1] = {options};
|
|
131
|
+
xt_value server = xt_tls_node_api->create_server(1, arguments);
|
|
132
|
+
xt_array_push(xt_node_listeners(server, "secureConnection", 1),
|
|
133
|
+
xt_closure_new((void *)xt_h2_secure_connection, 0, NULL));
|
|
134
|
+
return xt_h2_attach_handler(server, handler);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/* -- server response ------------------------------------------------------ */
|
|
138
|
+
|
|
139
|
+
static void xt_h2_response_submit(xt_value response) {
|
|
140
|
+
if (xt_truthy(xt_object_get_cstr(response, "__submitted"))) return;
|
|
141
|
+
xt_h2_conn *conn = xt_h2_conn_of(response);
|
|
142
|
+
if (conn == NULL || conn->closed) return;
|
|
143
|
+
xt_h2_body *body = xt_h2_body_of(response);
|
|
144
|
+
if (body == NULL) return;
|
|
145
|
+
xt_node_set(response, "__submitted", xt_bool(1));
|
|
146
|
+
body->ended = 1;
|
|
147
|
+
int32_t id = (int32_t)xt_to_number(xt_object_get_cstr(response, "__id"));
|
|
148
|
+
nghttp2_nv nva[XT_H2_MAX_HEADERS];
|
|
149
|
+
char status[16];
|
|
150
|
+
snprintf(status, sizeof(status), "%d",
|
|
151
|
+
(int)xt_to_number(xt_object_get_cstr(response, "__status")));
|
|
152
|
+
size_t count = xt_h2_add_nv(nva, 0, XT_H2_MAX_HEADERS, ":status", status);
|
|
153
|
+
count = xt_h2_add_headers(nva, count, XT_H2_MAX_HEADERS,
|
|
154
|
+
xt_object_get_cstr(response, "__headers"));
|
|
155
|
+
nghttp2_data_provider2 provider;
|
|
156
|
+
provider.source.ptr = body;
|
|
157
|
+
provider.read_callback = xt_h2_read_body;
|
|
158
|
+
nghttp2_submit_response2(conn->session, id, nva, count, &provider);
|
|
159
|
+
xt_h2_free_nv(nva, count);
|
|
160
|
+
xt_h2_flush(conn);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
static void xt_h2_merge_headers(xt_value target, xt_value source) {
|
|
164
|
+
if (!XT_IS_OBJECT(source)) return;
|
|
165
|
+
xt_value keys = xt_object_keys(source);
|
|
166
|
+
int32_t count = (int32_t)xt_to_number(xt_array_length(keys));
|
|
167
|
+
for (int32_t i = 0; i < count; i++) {
|
|
168
|
+
xt_value key = xt_array_get(keys, xt_number((double)i));
|
|
169
|
+
const char *name = xt_string_data(xt_to_string(key));
|
|
170
|
+
if (name != NULL && name[0] == ':') continue;
|
|
171
|
+
xt_object_set(target, key, xt_object_get(source, key));
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
xt_value xt_h2_response_write_head(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
|
|
176
|
+
(void)env;
|
|
177
|
+
int index = 0;
|
|
178
|
+
if (argc > 0 && XT_IS_NUMBER(argv[0])) {
|
|
179
|
+
xt_node_set(thisValue, "__status", xt_number(xt_to_number(argv[0])));
|
|
180
|
+
index = 1;
|
|
181
|
+
}
|
|
182
|
+
for (int32_t i = index; i < argc; i++) {
|
|
183
|
+
if (XT_IS_OBJECT(argv[i])) {
|
|
184
|
+
xt_value status = xt_object_get_cstr(argv[i], ":status");
|
|
185
|
+
if (!xt_truthy(xt_is_nullish(status))) {
|
|
186
|
+
xt_node_set(thisValue, "__status", xt_number(xt_to_number(status)));
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
xt_h2_merge_headers(xt_object_get_cstr(thisValue, "__headers"), argv[i]);
|
|
190
|
+
}
|
|
191
|
+
return thisValue;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
xt_value xt_h2_response_set_header(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
|
|
195
|
+
(void)env;
|
|
196
|
+
xt_object_set(xt_object_get_cstr(thisValue, "__headers"), xt_arg(argc, argv, 0), xt_arg(argc, argv, 1));
|
|
197
|
+
return thisValue;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
xt_value xt_h2_response_get_header(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
|
|
201
|
+
(void)env;
|
|
202
|
+
return xt_object_get(xt_object_get_cstr(thisValue, "__headers"), xt_arg(argc, argv, 0));
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
xt_value xt_h2_response_get_headers(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
|
|
206
|
+
(void)env;
|
|
207
|
+
(void)argc;
|
|
208
|
+
(void)argv;
|
|
209
|
+
return xt_object_get_cstr(thisValue, "__headers");
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
xt_value xt_h2_response_has_header(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
|
|
213
|
+
(void)env;
|
|
214
|
+
xt_value value = xt_object_get(xt_object_get_cstr(thisValue, "__headers"), xt_arg(argc, argv, 0));
|
|
215
|
+
return xt_bool(!xt_truthy(xt_is_nullish(value)));
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
xt_value xt_h2_response_remove_header(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
|
|
219
|
+
(void)env;
|
|
220
|
+
xt_value headers = xt_object_get_cstr(thisValue, "__headers");
|
|
221
|
+
xt_value key = xt_arg(argc, argv, 0);
|
|
222
|
+
xt_value kept = xt_object_new();
|
|
223
|
+
xt_value keys = xt_object_keys(headers);
|
|
224
|
+
int32_t count = (int32_t)xt_to_number(xt_array_length(keys));
|
|
225
|
+
for (int32_t i = 0; i < count; i++) {
|
|
226
|
+
xt_value item = xt_array_get(keys, xt_number((double)i));
|
|
227
|
+
if (!xt_truthy(xt_eq(item, key))) xt_object_set(kept, item, xt_object_get(headers, item));
|
|
228
|
+
}
|
|
229
|
+
xt_node_set(thisValue, "__headers", kept);
|
|
230
|
+
return thisValue;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
xt_value xt_h2_response_write(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
|
|
234
|
+
(void)env;
|
|
235
|
+
if (argc > 0) xt_h2_stream_append(thisValue, argv[0]);
|
|
236
|
+
return xt_bool(1);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
xt_value xt_h2_response_end(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
|
|
240
|
+
(void)env;
|
|
241
|
+
if (argc > 0 && !xt_truthy(xt_is_nullish(argv[0]))) xt_h2_stream_append(thisValue, argv[0]);
|
|
242
|
+
xt_h2_response_submit(thisValue);
|
|
243
|
+
return thisValue;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
xt_value xt_h2_response_flush(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
|
|
247
|
+
(void)env;
|
|
248
|
+
(void)argc;
|
|
249
|
+
(void)argv;
|
|
250
|
+
return thisValue;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
xt_value xt_h2_response_close(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
|
|
254
|
+
(void)env;
|
|
255
|
+
(void)argc;
|
|
256
|
+
(void)argv;
|
|
257
|
+
xt_h2_conn *conn = xt_h2_conn_of(thisValue);
|
|
258
|
+
if (conn == NULL || conn->closed) return thisValue;
|
|
259
|
+
int32_t id = (int32_t)xt_to_number(xt_object_get_cstr(thisValue, "__id"));
|
|
260
|
+
nghttp2_submit_rst_stream(conn->session, NGHTTP2_FLAG_NONE, id, NGHTTP2_CANCEL);
|
|
261
|
+
xt_h2_flush(conn);
|
|
262
|
+
return thisValue;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
xt_value xt_h2_request_respond(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
|
|
266
|
+
xt_value response = xt_object_get_cstr(thisValue, "__res");
|
|
267
|
+
if (!XT_IS_OBJECT(response)) return thisValue;
|
|
268
|
+
xt_h2_response_write_head(response, env, argc, argv);
|
|
269
|
+
return thisValue;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
xt_value xt_h2_request_write(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
|
|
273
|
+
xt_value response = xt_object_get_cstr(thisValue, "__res");
|
|
274
|
+
if (!XT_IS_OBJECT(response)) return xt_bool(0);
|
|
275
|
+
return xt_h2_response_write(response, env, argc, argv);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
xt_value xt_h2_request_end(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
|
|
279
|
+
xt_value response = xt_object_get_cstr(thisValue, "__res");
|
|
280
|
+
if (!XT_IS_OBJECT(response)) return thisValue;
|
|
281
|
+
xt_h2_response_end(response, env, argc, argv);
|
|
282
|
+
return thisValue;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
xt_value xt_h2_request_set_header(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
|
|
286
|
+
xt_value response = xt_object_get_cstr(thisValue, "__res");
|
|
287
|
+
if (!XT_IS_OBJECT(response)) return thisValue;
|
|
288
|
+
return xt_h2_response_set_header(response, env, argc, argv);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
xt_value xt_h2_request_get_header(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
|
|
292
|
+
xt_value response = xt_object_get_cstr(thisValue, "__res");
|
|
293
|
+
if (!XT_IS_OBJECT(response)) return xt_undefined();
|
|
294
|
+
return xt_h2_response_get_header(response, env, argc, argv);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
xt_value xt_h2_request_get_headers(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
|
|
298
|
+
xt_value response = xt_object_get_cstr(thisValue, "__res");
|
|
299
|
+
if (!XT_IS_OBJECT(response)) return xt_object_new();
|
|
300
|
+
return xt_h2_response_get_headers(response, env, argc, argv);
|
|
301
|
+
}
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
/* -- JavaScript objects: sessions, streams, requests, responses ----------- */
|
|
2
|
+
|
|
3
|
+
#define XT_H2_MAX_HEADERS 64
|
|
4
|
+
|
|
5
|
+
static xt_value xt_h2_client_session_proto(void);
|
|
6
|
+
static xt_value xt_h2_stream_proto(void);
|
|
7
|
+
static xt_value xt_h2_request_proto(void);
|
|
8
|
+
static xt_value xt_h2_response_proto(void);
|
|
9
|
+
|
|
10
|
+
static xt_value xt_h2_session_request(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv);
|
|
11
|
+
static xt_value xt_h2_session_close(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv);
|
|
12
|
+
static xt_value xt_h2_stream_write(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv);
|
|
13
|
+
static xt_value xt_h2_stream_end(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv);
|
|
14
|
+
static xt_value xt_h2_stream_close(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv);
|
|
15
|
+
static xt_value xt_h2_stream_set_encoding(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv);
|
|
16
|
+
static xt_value xt_h2_request_respond(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv);
|
|
17
|
+
static xt_value xt_h2_request_write(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv);
|
|
18
|
+
static xt_value xt_h2_request_end(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv);
|
|
19
|
+
static xt_value xt_h2_request_set_header(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv);
|
|
20
|
+
static xt_value xt_h2_request_get_header(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv);
|
|
21
|
+
static xt_value xt_h2_request_get_headers(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv);
|
|
22
|
+
static xt_value xt_h2_response_write_head(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv);
|
|
23
|
+
static xt_value xt_h2_response_set_header(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv);
|
|
24
|
+
static xt_value xt_h2_response_get_header(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv);
|
|
25
|
+
static xt_value xt_h2_response_get_headers(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv);
|
|
26
|
+
static xt_value xt_h2_response_remove_header(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv);
|
|
27
|
+
static xt_value xt_h2_response_has_header(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv);
|
|
28
|
+
static xt_value xt_h2_response_write(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv);
|
|
29
|
+
static xt_value xt_h2_response_end(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv);
|
|
30
|
+
static xt_value xt_h2_response_flush(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv);
|
|
31
|
+
static xt_value xt_h2_response_close(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv);
|
|
32
|
+
|
|
33
|
+
static xt_value xt_h2_noop(xt_value thisValue, xt_value env, int32_t argc, xt_value *argv) {
|
|
34
|
+
(void)env;
|
|
35
|
+
(void)argc;
|
|
36
|
+
(void)argv;
|
|
37
|
+
return thisValue;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/* -- header vector -------------------------------------------------------- */
|
|
41
|
+
|
|
42
|
+
static char *xt_h2_lowercase(const char *name) {
|
|
43
|
+
size_t length = strlen(name);
|
|
44
|
+
char *copy = (char *)malloc(length + 1);
|
|
45
|
+
if (copy == NULL) return NULL;
|
|
46
|
+
for (size_t i = 0; i < length; i++) {
|
|
47
|
+
char c = name[i];
|
|
48
|
+
copy[i] = (c >= 'A' && c <= 'Z') ? (char)(c + 32) : c;
|
|
49
|
+
}
|
|
50
|
+
copy[length] = '\0';
|
|
51
|
+
return copy;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
static size_t xt_h2_add_nv(nghttp2_nv *nva, size_t count, size_t max, const char *name,
|
|
55
|
+
const char *value) {
|
|
56
|
+
if (count >= max || name == NULL || name[0] == '\0') return count;
|
|
57
|
+
size_t nameLength = strlen(name);
|
|
58
|
+
size_t valueLength = value != NULL ? strlen(value) : 0;
|
|
59
|
+
uint8_t *nameCopy = (uint8_t *)malloc(nameLength);
|
|
60
|
+
uint8_t *valueCopy = (uint8_t *)malloc(valueLength > 0 ? valueLength : 1);
|
|
61
|
+
if (nameCopy == NULL || valueCopy == NULL) {
|
|
62
|
+
free(nameCopy);
|
|
63
|
+
free(valueCopy);
|
|
64
|
+
return count;
|
|
65
|
+
}
|
|
66
|
+
memcpy(nameCopy, name, nameLength);
|
|
67
|
+
if (valueLength > 0) memcpy(valueCopy, value, valueLength);
|
|
68
|
+
nva[count].name = nameCopy;
|
|
69
|
+
nva[count].namelen = nameLength;
|
|
70
|
+
nva[count].value = valueCopy;
|
|
71
|
+
nva[count].valuelen = valueLength;
|
|
72
|
+
nva[count].flags = NGHTTP2_NV_FLAG_NONE;
|
|
73
|
+
return count + 1;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
static void xt_h2_free_nv(nghttp2_nv *nva, size_t count) {
|
|
77
|
+
for (size_t i = 0; i < count; i++) {
|
|
78
|
+
free(nva[i].name);
|
|
79
|
+
free(nva[i].value);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Append every field of a JS header object (array values expand). */
|
|
84
|
+
static size_t xt_h2_add_headers(nghttp2_nv *nva, size_t count, size_t max, xt_value headers) {
|
|
85
|
+
if (!XT_IS_OBJECT(headers)) return count;
|
|
86
|
+
xt_value keys = xt_object_keys(headers);
|
|
87
|
+
int32_t keyCount = (int32_t)xt_to_number(xt_array_length(keys));
|
|
88
|
+
for (int32_t i = 0; i < keyCount && count < max; i++) {
|
|
89
|
+
xt_value key = xt_array_get(keys, xt_number((double)i));
|
|
90
|
+
const char *name = xt_string_data(xt_to_string(key));
|
|
91
|
+
if (name == NULL || strcmp(name, "__proto__") == 0) continue;
|
|
92
|
+
char *lower = xt_h2_lowercase(name);
|
|
93
|
+
if (lower == NULL) continue;
|
|
94
|
+
if (lower[0] == ':') {
|
|
95
|
+
free(lower);
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
xt_value value = xt_object_get(headers, key);
|
|
99
|
+
if (XT_IS_ARRAY(value)) {
|
|
100
|
+
int32_t values = (int32_t)xt_to_number(xt_array_length(value));
|
|
101
|
+
for (int32_t k = 0; k < values && count < max; k++) {
|
|
102
|
+
const char *text = xt_string_data(xt_to_string(xt_array_get(value, xt_number((double)k))));
|
|
103
|
+
if (text != NULL) count = xt_h2_add_nv(nva, count, max, lower, text);
|
|
104
|
+
}
|
|
105
|
+
} else if (!xt_truthy(xt_is_nullish(value))) {
|
|
106
|
+
const char *text = xt_string_data(xt_to_string(value));
|
|
107
|
+
if (text != NULL) count = xt_h2_add_nv(nva, count, max, lower, text);
|
|
108
|
+
}
|
|
109
|
+
free(lower);
|
|
110
|
+
}
|
|
111
|
+
return count;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/* -- bodies --------------------------------------------------------------- */
|
|
115
|
+
|
|
116
|
+
static nghttp2_ssize xt_h2_read_body(nghttp2_session *session, int32_t stream_id, uint8_t *buf,
|
|
117
|
+
size_t length, uint32_t *data_flags,
|
|
118
|
+
nghttp2_data_source *source, void *user_data) {
|
|
119
|
+
(void)session;
|
|
120
|
+
(void)stream_id;
|
|
121
|
+
(void)user_data;
|
|
122
|
+
xt_h2_body *body = (xt_h2_body *)source->ptr;
|
|
123
|
+
if (body == NULL) {
|
|
124
|
+
*data_flags |= NGHTTP2_DATA_FLAG_EOF;
|
|
125
|
+
return 0;
|
|
126
|
+
}
|
|
127
|
+
if (body->sent < body->length) {
|
|
128
|
+
size_t remaining = body->length - body->sent;
|
|
129
|
+
size_t take = remaining < length ? remaining : length;
|
|
130
|
+
memcpy(buf, body->data + body->sent, take);
|
|
131
|
+
body->sent += take;
|
|
132
|
+
return (nghttp2_ssize)take;
|
|
133
|
+
}
|
|
134
|
+
if (body->ended) {
|
|
135
|
+
*data_flags |= NGHTTP2_DATA_FLAG_EOF;
|
|
136
|
+
return 0;
|
|
137
|
+
}
|
|
138
|
+
return NGHTTP2_ERR_DEFERRED;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
static void xt_h2_stream_append(xt_value stream, xt_value chunk) {
|
|
142
|
+
xt_h2_body *body = xt_h2_body_of(stream);
|
|
143
|
+
if (body == NULL) return;
|
|
144
|
+
if (xt_node_is_buffer(chunk)) {
|
|
145
|
+
size_t byteLength = 0;
|
|
146
|
+
unsigned char *data = xt_node_buffer_bytes(chunk, &byteLength);
|
|
147
|
+
xt_h2_body_append(body, data, byteLength);
|
|
148
|
+
free(data);
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
const char *text = xt_string_data(xt_to_string(chunk));
|
|
152
|
+
if (text == NULL) return;
|
|
153
|
+
xt_h2_body_append(body, (const unsigned char *)text, strlen(text));
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/* -- protos --------------------------------------------------------------- */
|
|
157
|
+
|
|
158
|
+
static xt_value xt_h2_client_session_proto(void) {
|
|
159
|
+
static xt_value proto = 0;
|
|
160
|
+
xt_gc_add_root(&proto);
|
|
161
|
+
if (proto) return proto;
|
|
162
|
+
proto = xt_object_new();
|
|
163
|
+
xt_node_install_emitter(proto);
|
|
164
|
+
xt_node_define_method(proto, "request", (void *)xt_h2_session_request);
|
|
165
|
+
xt_node_define_method(proto, "close", (void *)xt_h2_session_close);
|
|
166
|
+
xt_node_define_method(proto, "destroy", (void *)xt_h2_session_close);
|
|
167
|
+
xt_node_define_method(proto, "setTimeout", (void *)xt_h2_noop);
|
|
168
|
+
xt_node_define_method(proto, "ping", (void *)xt_h2_noop);
|
|
169
|
+
xt_node_define_method(proto, "goaway", (void *)xt_h2_session_close);
|
|
170
|
+
return proto;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
static xt_value xt_h2_stream_proto(void) {
|
|
174
|
+
static xt_value proto = 0;
|
|
175
|
+
xt_gc_add_root(&proto);
|
|
176
|
+
if (proto) return proto;
|
|
177
|
+
proto = xt_object_new();
|
|
178
|
+
xt_node_install_emitter(proto);
|
|
179
|
+
xt_node_define_method(proto, "write", (void *)xt_h2_stream_write);
|
|
180
|
+
xt_node_define_method(proto, "end", (void *)xt_h2_stream_end);
|
|
181
|
+
xt_node_define_method(proto, "close", (void *)xt_h2_stream_close);
|
|
182
|
+
xt_node_define_method(proto, "setEncoding", (void *)xt_h2_stream_set_encoding);
|
|
183
|
+
xt_node_define_method(proto, "pause", (void *)xt_h2_noop);
|
|
184
|
+
xt_node_define_method(proto, "resume", (void *)xt_h2_noop);
|
|
185
|
+
return proto;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
static xt_value xt_h2_request_proto(void) {
|
|
189
|
+
static xt_value proto = 0;
|
|
190
|
+
xt_gc_add_root(&proto);
|
|
191
|
+
if (proto) return proto;
|
|
192
|
+
proto = xt_object_new();
|
|
193
|
+
xt_node_install_emitter(proto);
|
|
194
|
+
/* Node's server-side `stream` argument combines the request emitter with the
|
|
195
|
+
* response API, so the request object forwards these to `__res`. */
|
|
196
|
+
xt_node_define_method(proto, "respond", (void *)xt_h2_request_respond);
|
|
197
|
+
xt_node_define_method(proto, "writeHead", (void *)xt_h2_request_respond);
|
|
198
|
+
xt_node_define_method(proto, "write", (void *)xt_h2_request_write);
|
|
199
|
+
xt_node_define_method(proto, "end", (void *)xt_h2_request_end);
|
|
200
|
+
xt_node_define_method(proto, "setHeader", (void *)xt_h2_request_set_header);
|
|
201
|
+
xt_node_define_method(proto, "getHeader", (void *)xt_h2_request_get_header);
|
|
202
|
+
xt_node_define_method(proto, "getHeaders", (void *)xt_h2_request_get_headers);
|
|
203
|
+
xt_node_define_method(proto, "flushHeaders", (void *)xt_h2_response_flush);
|
|
204
|
+
xt_node_define_method(proto, "setEncoding", (void *)xt_h2_stream_set_encoding);
|
|
205
|
+
xt_node_define_method(proto, "pause", (void *)xt_h2_noop);
|
|
206
|
+
xt_node_define_method(proto, "resume", (void *)xt_h2_noop);
|
|
207
|
+
xt_node_define_method(proto, "close", (void *)xt_h2_stream_close);
|
|
208
|
+
return proto;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
static xt_value xt_h2_response_proto(void) {
|
|
212
|
+
static xt_value proto = 0;
|
|
213
|
+
xt_gc_add_root(&proto);
|
|
214
|
+
if (proto) return proto;
|
|
215
|
+
proto = xt_object_new();
|
|
216
|
+
xt_node_install_emitter(proto);
|
|
217
|
+
xt_node_define_method(proto, "writeHead", (void *)xt_h2_response_write_head);
|
|
218
|
+
xt_node_define_method(proto, "setHeader", (void *)xt_h2_response_set_header);
|
|
219
|
+
xt_node_define_method(proto, "getHeader", (void *)xt_h2_response_get_header);
|
|
220
|
+
xt_node_define_method(proto, "getHeaders", (void *)xt_h2_response_get_headers);
|
|
221
|
+
xt_node_define_method(proto, "removeHeader", (void *)xt_h2_response_remove_header);
|
|
222
|
+
xt_node_define_method(proto, "hasHeader", (void *)xt_h2_response_has_header);
|
|
223
|
+
xt_node_define_method(proto, "write", (void *)xt_h2_response_write);
|
|
224
|
+
xt_node_define_method(proto, "end", (void *)xt_h2_response_end);
|
|
225
|
+
xt_node_define_method(proto, "flushHeaders", (void *)xt_h2_response_flush);
|
|
226
|
+
xt_node_define_method(proto, "close", (void *)xt_h2_response_close);
|
|
227
|
+
return proto;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
static xt_value xt_h2_session_object(xt_h2_conn *conn) {
|
|
231
|
+
xt_value session = xt_object_new_with_proto(xt_h2_client_session_proto());
|
|
232
|
+
xt_node_set(session, "__h2", xt_number((double)(uintptr_t)conn));
|
|
233
|
+
xt_node_set(session, "__streams", xt_array_new(0, NULL));
|
|
234
|
+
xt_node_set(session, "__closed", xt_bool(0));
|
|
235
|
+
xt_node_set(session, "type", xt_string_from_cstr(conn->is_server ? "server" : "client"));
|
|
236
|
+
xt_node_set(session, "destroyed", xt_bool(0));
|
|
237
|
+
xt_node_set(session, "authority", xt_string_from_cstr(conn->authority));
|
|
238
|
+
xt_node_set(session, "scheme", xt_string_from_cstr(conn->scheme));
|
|
239
|
+
return session;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
static xt_value xt_h2_new_stream(xt_h2_conn *conn, int32_t id, xt_h2_body *body) {
|
|
243
|
+
xt_value stream = xt_object_new_with_proto(xt_h2_stream_proto());
|
|
244
|
+
xt_node_set(stream, "__h2", xt_number((double)(uintptr_t)conn));
|
|
245
|
+
xt_node_set(stream, "__id", xt_number((double)id));
|
|
246
|
+
xt_node_set(stream, "__headers", xt_object_new());
|
|
247
|
+
xt_node_set(stream, "__body", xt_number((double)(uintptr_t)body));
|
|
248
|
+
xt_node_set(stream, "__ended", xt_bool(0));
|
|
249
|
+
xt_node_set(stream, "__closed", xt_bool(0));
|
|
250
|
+
xt_node_set(stream, "__encoding", xt_undefined());
|
|
251
|
+
xt_node_set(stream, "id", xt_number((double)id));
|
|
252
|
+
xt_h2_stream_put(conn, id, stream);
|
|
253
|
+
return stream;
|
|
254
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/* -- API table and registration ------------------------------------------- */
|
|
2
|
+
|
|
3
|
+
xt_value xt_tls_backend_socket_ctor(int32_t argc, xt_value *argv) {
|
|
4
|
+
xt_value socket = xt_object_new_with_proto(xt_tls_socket_proto());
|
|
5
|
+
xt_node_set(socket, "__conn", xt_number(0));
|
|
6
|
+
xt_node_set(socket, "__fd", xt_number(-1));
|
|
7
|
+
xt_node_set(socket, "encrypted", xt_bool(1));
|
|
8
|
+
xt_node_set(socket, "authorized", xt_bool(0));
|
|
9
|
+
if (argc > 0 && XT_IS_OBJECT(argv[0])) {
|
|
10
|
+
xt_value conn = xt_object_get_cstr(argv[0], "__conn");
|
|
11
|
+
xt_value fd = xt_object_get_cstr(argv[0], "__fd");
|
|
12
|
+
if (XT_IS_NUMBER(conn) && XT_IS_NUMBER(fd)) {
|
|
13
|
+
xt_node_set(socket, "__conn", conn);
|
|
14
|
+
xt_node_set(socket, "__fd", fd);
|
|
15
|
+
xt_node_set(argv[0], "__fd", xt_number(-1));
|
|
16
|
+
int descriptor = (int)xt_to_number(fd);
|
|
17
|
+
if (descriptor >= 0) xt_loop_add(descriptor, XT_IO_READ, xt_tls_socket_read, xt_tls_box(socket));
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return socket;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
static const XtNodeTlsApi kNodeTlsApi = {
|
|
24
|
+
xt_tls_backend_connect,
|
|
25
|
+
xt_tls_backend_create_server,
|
|
26
|
+
xt_tls_backend_socket_ctor,
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
#if defined(__GNUC__) || defined(__clang__)
|
|
30
|
+
__attribute__((constructor)) static void xt_node_tls_boot(void) {
|
|
31
|
+
xt_node_tls_register(&kNodeTlsApi);
|
|
32
|
+
}
|
|
33
|
+
#elif defined(_MSC_VER)
|
|
34
|
+
void xt_node_tls_boot(void);
|
|
35
|
+
#pragma section(".CRT$XCU", read)
|
|
36
|
+
__declspec(allocate(".CRT$XCU")) static void (*const xt_node_tls_boot_ctor)(void) = xt_node_tls_boot;
|
|
37
|
+
void xt_node_tls_boot(void) { xt_node_tls_register(&kNodeTlsApi); }
|
|
38
|
+
#endif
|