visaljs 0.0.9 → 0.0.11
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/README.md +12 -4
- package/THIRD_PARTY_LICENSES +31 -1
- package/cli.js +3745 -890
- package/node/include/js_node.h +12 -1
- package/node/prelude/_http_common.js +29 -0
- package/node/prelude/async_hooks.js +50 -11
- package/node/prelude/child_process.js +31 -4
- package/node/prelude/console.js +311 -143
- package/node/prelude/crypto.js +6 -4
- package/node/prelude/dgram.js +1169 -0
- package/node/prelude/diagnostics_channel.js +3 -1
- package/node/prelude/dns.js +250 -45
- package/node/prelude/dns_promises.js +8 -2
- package/node/prelude/fs_promises.js +9 -1
- package/node/prelude/http.js +405 -94
- package/node/prelude/https.js +13 -4
- package/node/prelude/internal/console_class.js +7 -0
- package/node/prelude/internal/console_global.js +25 -0
- package/node/prelude/internal/crypto/key_encryption.js +217 -0
- package/node/prelude/internal/crypto/keys.js +161 -26
- package/node/prelude/internal/crypto/random.js +24 -0
- package/node/prelude/internal/crypto/util.js +2 -1
- package/node/prelude/internal/crypto_x509.js +161 -0
- package/node/prelude/internal/dns_classes.js +6 -0
- package/node/prelude/internal/dns_resolver.js +371 -0
- package/node/prelude/internal/errors.js +9 -0
- package/node/prelude/internal/events_on.js +82 -37
- package/node/prelude/internal/features.js +8 -4
- package/node/prelude/internal/fs_filehandle.js +10 -4
- package/node/prelude/internal/http_chars.ts +22 -0
- package/node/prelude/internal/perf_histogram.js +1 -1
- package/node/prelude/internal/process_more.js +32 -17
- package/node/prelude/internal/process_signals.js +8 -2
- package/node/prelude/internal/punycode.ts +163 -0
- package/node/prelude/internal/readline.js +648 -274
- package/node/prelude/internal/readline_editor.js +1240 -0
- package/node/prelude/internal/string_width.js +43 -0
- package/node/prelude/internal/tls/common.js +145 -33
- package/node/prelude/internal/tls/wrap.js +182 -49
- package/node/prelude/internal/tls/x509.js +4 -1
- package/node/prelude/internal/udp.js +177 -0
- package/node/prelude/net.js +30 -14
- package/node/prelude/os.js +24 -6
- package/node/prelude/perf_hooks.js +37 -2
- package/node/prelude/punycode.js +2 -130
- package/node/prelude/readline.js +113 -4
- package/node/prelude/readline_promises.js +56 -32
- package/node/prelude/tls.js +21 -4
- package/node/prelude/util.js +3 -16
- package/node/src/host_dns.c +623 -0
- package/node/src/host_http.c +12 -0
- package/node/src/host_net.c +46 -0
- package/node/src/host_os.c +1 -6
- package/node/src/host_sys.c +79 -0
- package/node/src/host_udp.c +418 -0
- package/node/third_party/c-ares/LICENSE.md +24 -0
- package/node/third_party/c-ares/config/darwin/ares_config.h +573 -0
- package/node/third_party/c-ares/config/freebsd/ares_config.h +573 -0
- package/node/third_party/c-ares/config/linux/ares_config.h +573 -0
- package/node/third_party/c-ares/config/openbsd/ares_config.h +583 -0
- package/node/third_party/c-ares/include/ares.h +1237 -0
- package/node/third_party/c-ares/include/ares_build.h +202 -0
- package/node/third_party/c-ares/include/ares_dns.h +127 -0
- package/node/third_party/c-ares/include/ares_dns_record.h +1119 -0
- package/node/third_party/c-ares/include/ares_nameser.h +510 -0
- package/node/third_party/c-ares/include/ares_version.h +47 -0
- package/node/third_party/c-ares/src/lib/ares_addrinfo2hostent.c +305 -0
- package/node/third_party/c-ares/src/lib/ares_addrinfo_localhost.c +245 -0
- package/node/third_party/c-ares/src/lib/ares_android.c +496 -0
- package/node/third_party/c-ares/src/lib/ares_android.h +38 -0
- package/node/third_party/c-ares/src/lib/ares_cancel.c +87 -0
- package/node/third_party/c-ares/src/lib/ares_close_sockets.c +137 -0
- package/node/third_party/c-ares/src/lib/ares_conn.c +511 -0
- package/node/third_party/c-ares/src/lib/ares_conn.h +202 -0
- package/node/third_party/c-ares/src/lib/ares_cookie.c +461 -0
- package/node/third_party/c-ares/src/lib/ares_data.c +168 -0
- package/node/third_party/c-ares/src/lib/ares_data.h +92 -0
- package/node/third_party/c-ares/src/lib/ares_destroy.c +159 -0
- package/node/third_party/c-ares/src/lib/ares_free_hostent.c +54 -0
- package/node/third_party/c-ares/src/lib/ares_free_string.c +33 -0
- package/node/third_party/c-ares/src/lib/ares_freeaddrinfo.c +67 -0
- package/node/third_party/c-ares/src/lib/ares_getaddrinfo.c +806 -0
- package/node/third_party/c-ares/src/lib/ares_getenv.c +38 -0
- package/node/third_party/c-ares/src/lib/ares_getenv.h +34 -0
- package/node/third_party/c-ares/src/lib/ares_gethostbyaddr.c +229 -0
- package/node/third_party/c-ares/src/lib/ares_gethostbyname.c +341 -0
- package/node/third_party/c-ares/src/lib/ares_getnameinfo.c +437 -0
- package/node/third_party/c-ares/src/lib/ares_hosts_file.c +960 -0
- package/node/third_party/c-ares/src/lib/ares_inet_net_pton.h +31 -0
- package/node/third_party/c-ares/src/lib/ares_init.c +631 -0
- package/node/third_party/c-ares/src/lib/ares_ipv6.h +115 -0
- package/node/third_party/c-ares/src/lib/ares_library_init.c +190 -0
- package/node/third_party/c-ares/src/lib/ares_metrics.c +261 -0
- package/node/third_party/c-ares/src/lib/ares_options.c +492 -0
- package/node/third_party/c-ares/src/lib/ares_parse_into_addrinfo.c +179 -0
- package/node/third_party/c-ares/src/lib/ares_private.h +648 -0
- package/node/third_party/c-ares/src/lib/ares_process.c +1649 -0
- package/node/third_party/c-ares/src/lib/ares_qcache.c +442 -0
- package/node/third_party/c-ares/src/lib/ares_query.c +151 -0
- package/node/third_party/c-ares/src/lib/ares_search.c +623 -0
- package/node/third_party/c-ares/src/lib/ares_send.c +300 -0
- package/node/third_party/c-ares/src/lib/ares_set_socket_functions.c +590 -0
- package/node/third_party/c-ares/src/lib/ares_setup.h +224 -0
- package/node/third_party/c-ares/src/lib/ares_socket.c +427 -0
- package/node/third_party/c-ares/src/lib/ares_socket.h +163 -0
- package/node/third_party/c-ares/src/lib/ares_sortaddrinfo.c +447 -0
- package/node/third_party/c-ares/src/lib/ares_strerror.c +91 -0
- package/node/third_party/c-ares/src/lib/ares_sysconfig.c +642 -0
- package/node/third_party/c-ares/src/lib/ares_sysconfig_files.c +851 -0
- package/node/third_party/c-ares/src/lib/ares_sysconfig_mac.c +373 -0
- package/node/third_party/c-ares/src/lib/ares_sysconfig_win.c +673 -0
- package/node/third_party/c-ares/src/lib/ares_timeout.c +162 -0
- package/node/third_party/c-ares/src/lib/ares_update_servers.c +1376 -0
- package/node/third_party/c-ares/src/lib/ares_version.c +36 -0
- package/node/third_party/c-ares/src/lib/config-dos.h +114 -0
- package/node/third_party/c-ares/src/lib/config-win32.h +396 -0
- package/node/third_party/c-ares/src/lib/dsa/ares_array.c +409 -0
- package/node/third_party/c-ares/src/lib/dsa/ares_htable.c +451 -0
- package/node/third_party/c-ares/src/lib/dsa/ares_htable.h +174 -0
- package/node/third_party/c-ares/src/lib/dsa/ares_htable_asvp.c +224 -0
- package/node/third_party/c-ares/src/lib/dsa/ares_htable_dict.c +230 -0
- package/node/third_party/c-ares/src/lib/dsa/ares_htable_strvp.c +210 -0
- package/node/third_party/c-ares/src/lib/dsa/ares_htable_szvp.c +188 -0
- package/node/third_party/c-ares/src/lib/dsa/ares_htable_vpstr.c +186 -0
- package/node/third_party/c-ares/src/lib/dsa/ares_htable_vpvp.c +194 -0
- package/node/third_party/c-ares/src/lib/dsa/ares_llist.c +385 -0
- package/node/third_party/c-ares/src/lib/dsa/ares_slist.c +479 -0
- package/node/third_party/c-ares/src/lib/dsa/ares_slist.h +207 -0
- package/node/third_party/c-ares/src/lib/event/ares_event.h +191 -0
- package/node/third_party/c-ares/src/lib/event/ares_event_configchg.c +746 -0
- package/node/third_party/c-ares/src/lib/event/ares_event_epoll.c +192 -0
- package/node/third_party/c-ares/src/lib/event/ares_event_kqueue.c +248 -0
- package/node/third_party/c-ares/src/lib/event/ares_event_poll.c +140 -0
- package/node/third_party/c-ares/src/lib/event/ares_event_select.c +159 -0
- package/node/third_party/c-ares/src/lib/event/ares_event_thread.c +575 -0
- package/node/third_party/c-ares/src/lib/event/ares_event_wake_pipe.c +172 -0
- package/node/third_party/c-ares/src/lib/event/ares_event_win32.c +981 -0
- package/node/third_party/c-ares/src/lib/event/ares_event_win32.h +161 -0
- package/node/third_party/c-ares/src/lib/include/ares_array.h +276 -0
- package/node/third_party/c-ares/src/lib/include/ares_buf.h +732 -0
- package/node/third_party/c-ares/src/lib/include/ares_htable_asvp.h +130 -0
- package/node/third_party/c-ares/src/lib/include/ares_htable_dict.h +123 -0
- package/node/third_party/c-ares/src/lib/include/ares_htable_strvp.h +130 -0
- package/node/third_party/c-ares/src/lib/include/ares_htable_szvp.h +118 -0
- package/node/third_party/c-ares/src/lib/include/ares_htable_vpstr.h +111 -0
- package/node/third_party/c-ares/src/lib/include/ares_htable_vpvp.h +128 -0
- package/node/third_party/c-ares/src/lib/include/ares_llist.h +239 -0
- package/node/third_party/c-ares/src/lib/include/ares_mem.h +41 -0
- package/node/third_party/c-ares/src/lib/include/ares_str.h +261 -0
- package/node/third_party/c-ares/src/lib/inet_net_pton.c +451 -0
- package/node/third_party/c-ares/src/lib/inet_ntop.c +219 -0
- package/node/third_party/c-ares/src/lib/legacy/ares_create_query.c +78 -0
- package/node/third_party/c-ares/src/lib/legacy/ares_expand_name.c +99 -0
- package/node/third_party/c-ares/src/lib/legacy/ares_expand_string.c +107 -0
- package/node/third_party/c-ares/src/lib/legacy/ares_fds.c +80 -0
- package/node/third_party/c-ares/src/lib/legacy/ares_getsock.c +85 -0
- package/node/third_party/c-ares/src/lib/legacy/ares_parse_a_reply.c +107 -0
- package/node/third_party/c-ares/src/lib/legacy/ares_parse_aaaa_reply.c +109 -0
- package/node/third_party/c-ares/src/lib/legacy/ares_parse_caa_reply.c +137 -0
- package/node/third_party/c-ares/src/lib/legacy/ares_parse_mx_reply.c +110 -0
- package/node/third_party/c-ares/src/lib/legacy/ares_parse_naptr_reply.c +132 -0
- package/node/third_party/c-ares/src/lib/legacy/ares_parse_ns_reply.c +154 -0
- package/node/third_party/c-ares/src/lib/legacy/ares_parse_ptr_reply.c +213 -0
- package/node/third_party/c-ares/src/lib/legacy/ares_parse_soa_reply.c +115 -0
- package/node/third_party/c-ares/src/lib/legacy/ares_parse_srv_reply.c +114 -0
- package/node/third_party/c-ares/src/lib/legacy/ares_parse_txt_reply.c +144 -0
- package/node/third_party/c-ares/src/lib/legacy/ares_parse_uri_reply.c +113 -0
- package/node/third_party/c-ares/src/lib/record/ares_dns_mapping.c +982 -0
- package/node/third_party/c-ares/src/lib/record/ares_dns_multistring.c +309 -0
- package/node/third_party/c-ares/src/lib/record/ares_dns_multistring.h +72 -0
- package/node/third_party/c-ares/src/lib/record/ares_dns_name.c +736 -0
- package/node/third_party/c-ares/src/lib/record/ares_dns_parse.c +1360 -0
- package/node/third_party/c-ares/src/lib/record/ares_dns_private.h +273 -0
- package/node/third_party/c-ares/src/lib/record/ares_dns_record.c +1701 -0
- package/node/third_party/c-ares/src/lib/record/ares_dns_write.c +1229 -0
- package/node/third_party/c-ares/src/lib/str/ares_buf.c +1510 -0
- package/node/third_party/c-ares/src/lib/str/ares_str.c +562 -0
- package/node/third_party/c-ares/src/lib/str/ares_strsplit.c +90 -0
- package/node/third_party/c-ares/src/lib/str/ares_strsplit.h +51 -0
- package/node/third_party/c-ares/src/lib/thirdparty/apple/dnsinfo.h +122 -0
- package/node/third_party/c-ares/src/lib/util/ares_iface_ips.c +640 -0
- package/node/third_party/c-ares/src/lib/util/ares_iface_ips.h +140 -0
- package/node/third_party/c-ares/src/lib/util/ares_math.c +167 -0
- package/node/third_party/c-ares/src/lib/util/ares_math.h +58 -0
- package/node/third_party/c-ares/src/lib/util/ares_rand.c +389 -0
- package/node/third_party/c-ares/src/lib/util/ares_rand.h +36 -0
- package/node/third_party/c-ares/src/lib/util/ares_threads.c +613 -0
- package/node/third_party/c-ares/src/lib/util/ares_threads.h +60 -0
- package/node/third_party/c-ares/src/lib/util/ares_time.h +49 -0
- package/node/third_party/c-ares/src/lib/util/ares_timeval.c +95 -0
- package/node/third_party/c-ares/src/lib/util/ares_uri.c +1627 -0
- package/node/third_party/c-ares/src/lib/util/ares_uri.h +252 -0
- package/node/third_party/c-ares/src/lib/windows_port.c +29 -0
- package/package.json +1 -1
- package/prebuilt/darwin-arm64/cares-a6286c65ca9374a3.a +0 -0
- package/prebuilt/darwin-x64/cares-a6286c65ca9374a3.a +0 -0
- package/prebuilt/linux-arm64/cares-4ba05ac8e948af8e.a +0 -0
- package/prebuilt/linux-arm64-musl/cares-4ba05ac8e948af8e.a +0 -0
- package/prebuilt/linux-x64/cares-4ba05ac8e948af8e.a +0 -0
- package/prebuilt/linux-x64-musl/cares-4ba05ac8e948af8e.a +0 -0
- package/prebuilt/win32-x64/boringssl-ce573858472b7fc9.a +0 -0
- package/prebuilt/win32-x64/brotli-20d86a61f30baeb3.a +0 -0
- package/prebuilt/win32-x64/cares-49c9ec95162b912d.a +0 -0
- package/prebuilt/win32-x64/uv-b11bae65d7fced82.a +0 -0
- package/prebuilt/win32-x64/zlib-4ce2317a4c3f5fb5.a +0 -0
- package/prebuilt/win32-x64/zstd-015315ac7c784c73.a +0 -0
- package/runtime/include/js_buffer.h +185 -0
- package/runtime/include/js_builtins.h +58 -0
- package/runtime/include/js_crypto.h +11 -0
- package/runtime/include/js_exception.h +3 -0
- package/runtime/include/js_function.h +1 -1
- package/runtime/include/js_gc.h +13 -10
- package/runtime/include/js_jobs.h +2 -0
- package/runtime/include/js_object.h +79 -3
- package/runtime/include/js_ops.h +27 -0
- package/runtime/include/js_runtime.h +1 -0
- package/runtime/include/js_web.h +0 -3
- package/runtime/include/jscontext.h +8 -0
- package/runtime/src/builtins.c +17 -4
- package/runtime/src/crypto_internal.h +1 -0
- package/runtime/src/crypto_subtle.c +66 -7
- package/runtime/src/crypto_tls.c +265 -19
- package/runtime/src/function.c +37 -10
- package/runtime/src/gc.c +76 -93
- package/runtime/src/inspect.c +18 -1
- package/runtime/src/iterator.c +5 -1
- package/runtime/src/jobs.c +37 -22
- package/runtime/src/object.c +91 -8
- package/runtime/src/os.c +16 -6
- package/runtime/src/os.h +2 -0
- package/runtime/src/process.c +97 -10
- package/runtime/src/typedarray.c +23 -0
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
// Node's udp_wrap binding (UDP and SendWrap, as src/udp_wrap.cc defines them) over the host's UDP
|
|
2
|
+
// handles (src/host_udp.c): what lib/dgram.js calls, with the same results (0 or a negative errno,
|
|
3
|
+
// a send's length + 1 when it went at once) and callbacks (onmessage, a SendWrap's oncomplete).
|
|
4
|
+
import {
|
|
5
|
+
errnoMessage, errnoName, udpAddress, udpBind, udpBufferSize, udpClose, udpConnect, udpDisconnect, udpFd, udpHasRef, udpMembership, udpOpen, udpOpenFd,
|
|
6
|
+
udpRecvStart, udpRecvStop, udpRef, udpSend, udpSendQueue, udpSetBroadcast, udpSetMulticastInterface, udpSetMulticastLoopback,
|
|
7
|
+
udpSetMulticastTTL, udpSetTTL, udpSourceMembership,
|
|
8
|
+
} from "visaljs:host";
|
|
9
|
+
import { Buffer } from "../buffer.js";
|
|
10
|
+
|
|
11
|
+
const UDP_MESSAGE = 0, UDP_SENT = 1;
|
|
12
|
+
let nextAsyncId = 1;
|
|
13
|
+
|
|
14
|
+
export class SendWrap {}
|
|
15
|
+
|
|
16
|
+
export class UDP {
|
|
17
|
+
constructor() {
|
|
18
|
+
this._id = udpOpen((event, a, b) => this._event(event, a, b));
|
|
19
|
+
this._pending = []; /* SendWraps whose sends were queued, in order */
|
|
20
|
+
this._closeCallback = null;
|
|
21
|
+
this._asyncId = nextAsyncId++;
|
|
22
|
+
this.onmessage = null;
|
|
23
|
+
this.onerror = null;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
_event(event, a, b) {
|
|
27
|
+
if (event === UDP_MESSAGE) {
|
|
28
|
+
if (typeof a === "number") this.onmessage?.(a, this, undefined, undefined);
|
|
29
|
+
else this.onmessage?.(a.byteLength, this, Buffer.from(a.buffer, a.byteOffset, a.byteLength), { address: b[0], family: b[1], port: b[2] });
|
|
30
|
+
} else if (event === UDP_SENT) {
|
|
31
|
+
const req = this._pending.shift();
|
|
32
|
+
req?.oncomplete?.(a, b);
|
|
33
|
+
} else {
|
|
34
|
+
this._closeCallback?.();
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
getAsyncId() {
|
|
39
|
+
return this._asyncId;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
open(fd) {
|
|
43
|
+
return udpOpenFd(this._id, fd);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
get fd() {
|
|
47
|
+
return udpFd(this._id);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
bind(ip, port, flags) {
|
|
51
|
+
return udpBind(this._id, ip, port, flags);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
bind6(ip, port, flags) {
|
|
55
|
+
return udpBind(this._id, ip, port, flags);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
connect(ip, port) {
|
|
59
|
+
return udpConnect(this._id, ip, port);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
connect6(ip, port) {
|
|
63
|
+
return udpConnect(this._id, ip, port);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
disconnect() {
|
|
67
|
+
return udpDisconnect(this._id);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** send(req, list, count, port, ip, hasCallback), or a connected socket's send(req, list, count, hasCallback). */
|
|
71
|
+
send(req, list, count, port, ip) {
|
|
72
|
+
const connected = typeof port !== "number";
|
|
73
|
+
const err = udpSend(this._id, list, connected ? null : ip, connected ? 0 : port);
|
|
74
|
+
if (err === 0) this._pending.push(req);
|
|
75
|
+
return err;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
send6(req, list, count, port, ip) {
|
|
79
|
+
return this.send(req, list, count, port, ip);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
recvStart() {
|
|
83
|
+
return udpRecvStart(this._id);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
recvStop() {
|
|
87
|
+
return udpRecvStop(this._id);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
getsockname(out) {
|
|
91
|
+
return address(udpAddress(this._id, false), out);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
getpeername(out) {
|
|
95
|
+
return address(udpAddress(this._id, true), out);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
setBroadcast(on) {
|
|
99
|
+
return udpSetBroadcast(this._id, on);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
setTTL(ttl) {
|
|
103
|
+
return udpSetTTL(this._id, ttl);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
setMulticastTTL(ttl) {
|
|
107
|
+
return udpSetMulticastTTL(this._id, ttl);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
setMulticastLoopback(on) {
|
|
111
|
+
return udpSetMulticastLoopback(this._id, on);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
setMulticastInterface(iface) {
|
|
115
|
+
return udpSetMulticastInterface(this._id, iface);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
addMembership(group, iface) {
|
|
119
|
+
return udpMembership(this._id, group, iface ?? null, true);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
dropMembership(group, iface) {
|
|
123
|
+
return udpMembership(this._id, group, iface ?? null, false);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
addSourceSpecificMembership(source, group, iface) {
|
|
127
|
+
return udpSourceMembership(this._id, source, group, iface ?? null, true);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
dropSourceSpecificMembership(source, group, iface) {
|
|
131
|
+
return udpSourceMembership(this._id, source, group, iface ?? null, false);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/* the size, or undefined with what failed in ctx (for ERR_SOCKET_BUFFER_SIZE) */
|
|
135
|
+
bufferSize(size, recv, ctx) {
|
|
136
|
+
const r = udpBufferSize(this._id, size, recv);
|
|
137
|
+
if (r >= 0) return r;
|
|
138
|
+
ctx.errno = r;
|
|
139
|
+
ctx.code = errnoName(r);
|
|
140
|
+
ctx.message = errnoMessage(r);
|
|
141
|
+
ctx.syscall = recv ? "uv_recv_buffer_size" : "uv_send_buffer_size";
|
|
142
|
+
return undefined;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
getSendQueueSize() {
|
|
146
|
+
return udpSendQueue(this._id, false);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
getSendQueueCount() {
|
|
150
|
+
return udpSendQueue(this._id, true);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
close(callback) {
|
|
154
|
+
if (typeof callback === "function") this._closeCallback = callback;
|
|
155
|
+
udpClose(this._id);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
ref() {
|
|
159
|
+
udpRef(this._id, true);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
unref() {
|
|
163
|
+
udpRef(this._id, false);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
hasRef() {
|
|
167
|
+
return udpHasRef(this._id);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function address(result, out) {
|
|
172
|
+
if (typeof result === "number") return result;
|
|
173
|
+
out.address = result[0];
|
|
174
|
+
out.family = result[1];
|
|
175
|
+
out.port = result[2];
|
|
176
|
+
return 0;
|
|
177
|
+
}
|
package/node/prelude/net.js
CHANGED
|
@@ -9,8 +9,9 @@
|
|
|
9
9
|
// Ported from Node.js (Copyright Node.js contributors, MIT): node/third_party/nodejs/LICENSE.
|
|
10
10
|
import {
|
|
11
11
|
errnoMessage, errnoName, netAddress, netBind, netCallback, netChmod, netClose, netConnect, netGuessHandle, netKeepAlive, netListen,
|
|
12
|
-
netFd, netNoDelay, netOpen, netOpenFd, netReadStart, netReadStop, netRef, netReset, netShutdown, netWrite, timerUnref,
|
|
12
|
+
loopNow, netFd, netNoDelay, netOpen, netOpenFd, netReadStart, netReadStop, netRef, netReset, netShutdown, netWrite, timerUnref,
|
|
13
13
|
} from "visaljs:host";
|
|
14
|
+
import { channel, tracingChannel } from "./diagnostics_channel.js";
|
|
14
15
|
import { ADDRCONFIG, lookup } from "./dns.js";
|
|
15
16
|
import { EventEmitter } from "./events.js";
|
|
16
17
|
import { bindContext } from "./internal/async_context.js";
|
|
@@ -30,11 +31,9 @@ const UV_EOF = -4095;
|
|
|
30
31
|
const UV_EINVAL = -22;
|
|
31
32
|
const TIMEOUT_MAX = 2 ** 31 - 1;
|
|
32
33
|
|
|
33
|
-
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
return seconds * 1e3 + nanoseconds / 1e6;
|
|
37
|
-
};
|
|
34
|
+
const netClientSocketChannel = channel("net.client.socket");
|
|
35
|
+
const netServerSocketChannel = channel("net.server.socket");
|
|
36
|
+
const netServerListen = tracingChannel("net.server.listen");
|
|
38
37
|
|
|
39
38
|
/* ---- Node's error shapes ------------------------------------------------------------------- */
|
|
40
39
|
|
|
@@ -210,7 +209,8 @@ class SocketClass extends Duplex {
|
|
|
210
209
|
this._keepAlive = options.keepAlive === true ? [true, ~~((options.keepAliveInitialDelay ?? 0) / 1000)] : null;
|
|
211
210
|
this._unref = false;
|
|
212
211
|
this._timer = null; /* the timeout: an unref'd timer, re-armed for the idle time left when it fires */
|
|
213
|
-
this.
|
|
212
|
+
this._timerDue = 0; /* when _timer fires (loopNow's clock) */
|
|
213
|
+
this._timeoutMs = 0; /* 0: off; a pending _timer then finds nothing to do */
|
|
214
214
|
this._lastActive = 0;
|
|
215
215
|
this._onread = null;
|
|
216
216
|
if (options.typeOfService !== undefined) feature("validateTypeOfService")(options.typeOfService);
|
|
@@ -386,6 +386,7 @@ class SocketClass extends Duplex {
|
|
|
386
386
|
if (!writableBuffer) return undefined;
|
|
387
387
|
let bytes = this._bytesDispatched;
|
|
388
388
|
for (const el of writableBuffer) bytes += typeof el.chunk === "string" ? Buffer.byteLength(el.chunk, el.encoding) : el.chunk.length;
|
|
389
|
+
if (this._httpWritten) bytes += this._httpWritten(); /* a server's responses, written through llhttp's connection */
|
|
389
390
|
const data = this._pendingData;
|
|
390
391
|
if (Array.isArray(data)) {
|
|
391
392
|
for (const el of data) bytes += typeof el.chunk === "string" ? Buffer.byteLength(el.chunk, el.encoding) : el.chunk.length;
|
|
@@ -468,16 +469,19 @@ class SocketClass extends Duplex {
|
|
|
468
469
|
if (typeof msecs !== "number") throw invalidArgType("msecs", "of type number", msecs);
|
|
469
470
|
if (!(msecs >= 0 && msecs !== Infinity)) throw outOfRange("msecs", "a non-negative finite number", msecs);
|
|
470
471
|
if (msecs > TIMEOUT_MAX) msecs = TIMEOUT_MAX;
|
|
471
|
-
|
|
472
|
+
/* the pending timer stays when it fires no later than the new deadline: _onTimer re-arms it
|
|
473
|
+
* for the rest, so a keep-alive socket, off for each request and on after it, sets no timer */
|
|
472
474
|
if (msecs === 0) {
|
|
475
|
+
this._timeoutMs = 0;
|
|
473
476
|
if (callback !== undefined) {
|
|
474
477
|
validateFunction(callback, "callback");
|
|
475
478
|
this.removeListener("timeout", callback);
|
|
476
479
|
}
|
|
477
480
|
} else {
|
|
478
481
|
this._timeoutMs = msecs;
|
|
479
|
-
this._lastActive =
|
|
480
|
-
this.
|
|
482
|
+
this._lastActive = loopNow();
|
|
483
|
+
if (this._timer !== null && this._timerDue > this._lastActive + msecs) this._stopTimer();
|
|
484
|
+
if (this._timer === null) this._armTimeout(msecs);
|
|
481
485
|
if (callback !== undefined) {
|
|
482
486
|
validateFunction(callback, "callback");
|
|
483
487
|
this.once("timeout", callback);
|
|
@@ -487,26 +491,32 @@ class SocketClass extends Duplex {
|
|
|
487
491
|
}
|
|
488
492
|
|
|
489
493
|
_armTimeout(ms) {
|
|
494
|
+
this._timerDue = loopNow() + ms;
|
|
490
495
|
this._timer = timerUnref(() => this._onTimer(), ms);
|
|
491
496
|
}
|
|
492
497
|
|
|
493
498
|
_onTimer() {
|
|
494
499
|
this._timer = null;
|
|
495
|
-
|
|
500
|
+
if (this._timeoutMs === 0) return;
|
|
501
|
+
const idle = loopNow() - this._lastActive;
|
|
496
502
|
if (idle < this._timeoutMs - 1) this._armTimeout(this._timeoutMs - idle);
|
|
497
503
|
else this.emit("timeout");
|
|
498
504
|
}
|
|
499
505
|
|
|
500
|
-
|
|
506
|
+
_stopTimer() {
|
|
501
507
|
if (this._timer !== null) clearTimeout(this._timer);
|
|
502
508
|
this._timer = null;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
_clearTimeout() {
|
|
512
|
+
this._stopTimer();
|
|
503
513
|
this._timeoutMs = 0;
|
|
504
514
|
}
|
|
505
515
|
|
|
506
516
|
/* activity: the timeout counts from now (Node's timer.refresh(), which re-arms a fired one) */
|
|
507
517
|
_unrefTimer() {
|
|
508
518
|
if (this._timeoutMs) {
|
|
509
|
-
this._lastActive =
|
|
519
|
+
this._lastActive = loopNow();
|
|
510
520
|
if (this._timer === null) this._armTimeout(this._timeoutMs);
|
|
511
521
|
}
|
|
512
522
|
}
|
|
@@ -661,6 +671,7 @@ class SocketClass extends Duplex {
|
|
|
661
671
|
this._perfConnect = null;
|
|
662
672
|
this._undestroy();
|
|
663
673
|
}
|
|
674
|
+
if (netClientSocketChannel.hasSubscribers) netClientSocketChannel.publish({ socket: this });
|
|
664
675
|
const { path } = options;
|
|
665
676
|
if (cb !== null) this.once("connect", cb);
|
|
666
677
|
this._unrefTimer();
|
|
@@ -719,7 +730,7 @@ class SocketClass extends Duplex {
|
|
|
719
730
|
});
|
|
720
731
|
return;
|
|
721
732
|
}
|
|
722
|
-
let family = options.family
|
|
733
|
+
let family = options.family;
|
|
723
734
|
if (family === "IPv4") family = 4;
|
|
724
735
|
else if (family === "IPv6") family = 6;
|
|
725
736
|
const lookupFn = options.lookup ?? lookup;
|
|
@@ -950,6 +961,7 @@ class ServerClass extends EventEmitter {
|
|
|
950
961
|
let [options, cb] = normalizeArgs(args);
|
|
951
962
|
if (typeof options.callback === "function") cb = options.callback;
|
|
952
963
|
if (this._handle !== null) throw codeError(Error, "ERR_SERVER_ALREADY_LISTEN", "Listen method has been called more than once without closing.");
|
|
964
|
+
if (netServerListen.hasSubscribers) netServerListen.asyncStart.publish({ server: this, options });
|
|
953
965
|
if (cb !== null) this.once("listening", cb);
|
|
954
966
|
this._listeningId++;
|
|
955
967
|
const backlogFromArgs = toNumber(args.length > 1 && args[1]) || toNumber(args.length > 2 && args[2]);
|
|
@@ -1091,6 +1103,7 @@ class ServerClass extends EventEmitter {
|
|
|
1091
1103
|
if (rval === null) rval = this._createHandle(address, port, addressType, fd, flags);
|
|
1092
1104
|
if (rval < 0) {
|
|
1093
1105
|
const error = uvExceptionWithHostPort(rval, "listen", address, port);
|
|
1106
|
+
if (netServerListen.hasSubscribers) netServerListen.error.publish({ server: this, error });
|
|
1094
1107
|
process.nextTick(() => this.emit("error", error));
|
|
1095
1108
|
return;
|
|
1096
1109
|
}
|
|
@@ -1102,9 +1115,11 @@ class ServerClass extends EventEmitter {
|
|
|
1102
1115
|
const ex = uvExceptionWithHostPort(err, "listen", address, port);
|
|
1103
1116
|
netClose(id);
|
|
1104
1117
|
this._handle = null;
|
|
1118
|
+
if (netServerListen.hasSubscribers) netServerListen.error.publish({ server: this, error: ex });
|
|
1105
1119
|
process.nextTick(() => this.emit("error", ex));
|
|
1106
1120
|
return;
|
|
1107
1121
|
}
|
|
1122
|
+
if (netServerListen.hasSubscribers) netServerListen.asyncEnd.publish({ server: this });
|
|
1108
1123
|
this._connectionKey = addressType + ":" + address + ":" + port;
|
|
1109
1124
|
if (this._unref) netRef(id, false);
|
|
1110
1125
|
process.nextTick(() => {
|
|
@@ -1145,6 +1160,7 @@ class ServerClass extends EventEmitter {
|
|
|
1145
1160
|
socket.server = this;
|
|
1146
1161
|
socket._server = this;
|
|
1147
1162
|
this.emit("connection", socket);
|
|
1163
|
+
if (netServerSocketChannel.hasSubscribers) netServerSocketChannel.publish({ socket });
|
|
1148
1164
|
}
|
|
1149
1165
|
|
|
1150
1166
|
/* { address, family, port }, or a pipe's path */
|
package/node/prelude/os.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
// Ported from Node.js (Copyright Node.js contributors, MIT): node/third_party/nodejs/LICENSE.
|
|
4
4
|
import {
|
|
5
5
|
errnoMessage, errnoName, osConstants, osCpus, osGetPriority, osHomedir, osHostname, osInterfaces, osLoadavg, osMemory, osParallelism,
|
|
6
|
-
osSetPriority,
|
|
6
|
+
osSetPriority, osUname, osUptime, osUserInfo,
|
|
7
7
|
} from "visaljs:host";
|
|
8
8
|
import { codeError, invalidArgType } from "./internal/errors.js";
|
|
9
9
|
|
|
@@ -64,10 +64,18 @@ export function homedir() {
|
|
|
64
64
|
return check(osHomedir(), "uv_os_homedir");
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
+
/* Node's: TEMP, TMP or %SystemRoot%\temp on Windows; elsewhere TMPDIR, TMP or TEMP (getTempDir), without a trailing separator */
|
|
67
68
|
export function tmpdir() {
|
|
68
|
-
const
|
|
69
|
-
|
|
69
|
+
const env = process.env;
|
|
70
|
+
if (process.platform === "win32") {
|
|
71
|
+
const path = env.TEMP || env.TMP || (env.SystemRoot || env.windir) + "\\temp";
|
|
72
|
+
return path.length > 1 && path.endsWith("\\") && path[path.length - 2] !== ":" ? path.slice(0, -1) : path;
|
|
73
|
+
}
|
|
74
|
+
const dir = env.TMPDIR || env.TMP || env.TEMP;
|
|
75
|
+
if (!dir) return "/tmp";
|
|
76
|
+
return dir.length > 1 && dir.endsWith("/") ? dir.slice(0, -1) : dir;
|
|
70
77
|
}
|
|
78
|
+
tmpdir[Symbol.toPrimitive] = () => tmpdir();
|
|
71
79
|
|
|
72
80
|
export function totalmem() {
|
|
73
81
|
return osMemory()[0];
|
|
@@ -200,7 +208,17 @@ export const constants = Object.freeze({
|
|
|
200
208
|
}),
|
|
201
209
|
});
|
|
202
210
|
|
|
203
|
-
|
|
211
|
+
/* as Node's: `${os.hostname}` and the like give their value */
|
|
212
|
+
for (const f of [availableParallelism, freemem, hostname, version, type, release, machine, homedir, totalmem, uptime, arch, platform, endianness]) {
|
|
213
|
+
f[Symbol.toPrimitive] = () => f();
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/* as Node's module object: EOL and devNull read-only (but configurable), constants fixed */
|
|
217
|
+
export default Object.defineProperties({
|
|
204
218
|
arch, availableParallelism, cpus, endianness, freemem, getPriority, homedir, hostname, loadavg, networkInterfaces, platform, release,
|
|
205
|
-
setPriority, tmpdir, totalmem, type, userInfo, uptime, version, machine,
|
|
206
|
-
}
|
|
219
|
+
setPriority, tmpdir, totalmem, type, userInfo, uptime, version, machine,
|
|
220
|
+
}, {
|
|
221
|
+
constants: { configurable: false, enumerable: true, value: constants },
|
|
222
|
+
EOL: { configurable: true, enumerable: true, writable: false, value: EOL },
|
|
223
|
+
devNull: { configurable: true, enumerable: true, writable: false, value: devNull },
|
|
224
|
+
});
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// process.hrtime's, from when this module loads: timeOrigin is that time on the epoch clock.
|
|
3
3
|
// PerformanceObserver and the histograms are modules of their own (internal/perf_observe.js,
|
|
4
4
|
// internal/perf_histogram.js), linked when the program names them.
|
|
5
|
-
import { codeError, invalidArgType } from "./internal/errors.js";
|
|
5
|
+
import { codeError, invalidArgType, validateFunction } from "./internal/errors.js";
|
|
6
6
|
import { features, lazyClass, lazyFunction } from "./internal/features.js";
|
|
7
7
|
|
|
8
8
|
const hrMs = () => {
|
|
@@ -61,6 +61,39 @@ function markTime(mark) {
|
|
|
61
61
|
throw new globalThis.DOMException('The "' + mark + '" performance mark has not been set', "SyntaxError");
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
+
function processComplete(name, start, args, histogram) {
|
|
65
|
+
const duration = now() - start;
|
|
66
|
+
if (histogram !== undefined) histogram.record(Math.ceil(duration * 1e6));
|
|
67
|
+
const entry = new PerformanceEntry(kConstruct, name, "function", start, duration, args);
|
|
68
|
+
for (let n = 0; n < args.length; n++) entry[n] = args[n];
|
|
69
|
+
features.perfObserve?.(entry);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** fn, timed: each call is a 'function' entry for the observers, and its duration (ns) in the histogram. */
|
|
73
|
+
export function timerify(fn, options = {}) {
|
|
74
|
+
validateFunction(fn, "fn");
|
|
75
|
+
if (options === null || typeof options !== "object") throw invalidArgType("options", "of type object", options);
|
|
76
|
+
const { histogram } = options;
|
|
77
|
+
if (histogram !== undefined && (!features.isHistogram?.(histogram) || typeof histogram.record !== "function")) {
|
|
78
|
+
throw invalidArgType("options.histogram", "an instance of RecordableHistogram", histogram);
|
|
79
|
+
}
|
|
80
|
+
function timerified(...args) {
|
|
81
|
+
const isConstructorCall = new.target !== undefined;
|
|
82
|
+
const start = now();
|
|
83
|
+
const result = isConstructorCall ? Reflect.construct(fn, args, fn) : Reflect.apply(fn, this, args);
|
|
84
|
+
if (!isConstructorCall && typeof result?.finally === "function") {
|
|
85
|
+
return result.finally(processComplete.bind(result, fn.name, start, args, histogram));
|
|
86
|
+
}
|
|
87
|
+
processComplete(fn.name, start, args, histogram);
|
|
88
|
+
return result;
|
|
89
|
+
}
|
|
90
|
+
Object.defineProperties(timerified, {
|
|
91
|
+
length: { configurable: false, enumerable: true, value: fn.length },
|
|
92
|
+
name: { configurable: false, enumerable: true, value: `timerified ${fn.name}` },
|
|
93
|
+
});
|
|
94
|
+
return timerified;
|
|
95
|
+
}
|
|
96
|
+
|
|
64
97
|
export class Performance {
|
|
65
98
|
constructor(key) {
|
|
66
99
|
if (key !== kConstruct) throw codeError(TypeError, "ERR_ILLEGAL_CONSTRUCTOR", "Illegal constructor");
|
|
@@ -130,6 +163,8 @@ function clear(type, name) {
|
|
|
130
163
|
}
|
|
131
164
|
}
|
|
132
165
|
|
|
166
|
+
Object.defineProperty(Performance.prototype, "timerify", { configurable: true, enumerable: false, writable: true, value: timerify });
|
|
167
|
+
|
|
133
168
|
export const performance = new Performance(kConstruct);
|
|
134
169
|
|
|
135
170
|
export const constants = {
|
|
@@ -147,5 +182,5 @@ export const monitorEventLoopDelay = lazyFunction("monitorEventLoopDelay");
|
|
|
147
182
|
|
|
148
183
|
export default {
|
|
149
184
|
Performance, PerformanceEntry, PerformanceMark, PerformanceMeasure, PerformanceObserver, PerformanceObserverEntryList, PerformanceResourceTiming,
|
|
150
|
-
performance, constants, createHistogram, monitorEventLoopDelay,
|
|
185
|
+
performance, constants, createHistogram, monitorEventLoopDelay, timerify,
|
|
151
186
|
};
|
package/node/prelude/punycode.js
CHANGED
|
@@ -2,30 +2,12 @@
|
|
|
2
2
|
// punycode.js 2.1.0 Node bundles (deprecated in Node, still required by older packages).
|
|
3
3
|
// Ported from punycode.js 2.1.0 (Copyright Mathias Bynens, MIT): node/third_party/punycode/LICENSE.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
const base = 36;
|
|
7
|
-
const tMin = 1;
|
|
8
|
-
const tMax = 26;
|
|
9
|
-
const skew = 38;
|
|
10
|
-
const damp = 700;
|
|
11
|
-
const initialBias = 72;
|
|
12
|
-
const initialN = 128;
|
|
13
|
-
const delimiter = "-";
|
|
5
|
+
import { decode, encode } from "./internal/punycode.ts";
|
|
14
6
|
|
|
15
7
|
const regexPunycode = /^xn--/;
|
|
16
8
|
const regexNonASCII = /[^\0-\x7F]/;
|
|
17
9
|
const regexSeparators = /[\x2E。.。]/g; /* RFC 3490 separators */
|
|
18
10
|
|
|
19
|
-
const errors = {
|
|
20
|
-
overflow: "Overflow: input needs wider integers to process",
|
|
21
|
-
"not-basic": "Illegal input >= 0x80 (not a basic code point)",
|
|
22
|
-
"invalid-input": "Invalid input",
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
function error(type) {
|
|
26
|
-
throw new RangeError(errors[type]);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
11
|
/* a domain name's labels mapped, an email address's local part kept */
|
|
30
12
|
function mapDomain(domain, fn) {
|
|
31
13
|
const parts = domain.split("@");
|
|
@@ -61,117 +43,6 @@ function ucs2decode(string) {
|
|
|
61
43
|
/** Code points as a string. */
|
|
62
44
|
const ucs2encode = (codePoints) => String.fromCodePoint(...codePoints);
|
|
63
45
|
|
|
64
|
-
/* a basic code point's digit value, or base for anything else */
|
|
65
|
-
function basicToDigit(codePoint) {
|
|
66
|
-
if (codePoint >= 0x30 && codePoint < 0x3a) return 26 + (codePoint - 0x30);
|
|
67
|
-
if (codePoint >= 0x41 && codePoint < 0x5b) return codePoint - 0x41;
|
|
68
|
-
if (codePoint >= 0x61 && codePoint < 0x7b) return codePoint - 0x61;
|
|
69
|
-
return base;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/* a digit's code point, lowercase (0..25 a..z, 26..35 0..9) */
|
|
73
|
-
const digitToBasic = (digit) => digit + 22 + 75 * (digit < 26);
|
|
74
|
-
|
|
75
|
-
/* RFC 3492 3.4 */
|
|
76
|
-
function adapt(delta, numPoints, firstTime) {
|
|
77
|
-
let k = 0;
|
|
78
|
-
delta = firstTime ? Math.floor(delta / damp) : delta >> 1;
|
|
79
|
-
delta += Math.floor(delta / numPoints);
|
|
80
|
-
for (; delta > ((base - tMin) * tMax) >> 1; k += base) delta = Math.floor(delta / (base - tMin));
|
|
81
|
-
return Math.floor(k + ((base - tMin + 1) * delta) / (delta + skew));
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/** Punycode (ASCII only) to the Unicode string it encodes. */
|
|
85
|
-
export function decode(input) {
|
|
86
|
-
const output = [];
|
|
87
|
-
const inputLength = input.length;
|
|
88
|
-
let i = 0;
|
|
89
|
-
let n = initialN;
|
|
90
|
-
let bias = initialBias;
|
|
91
|
-
|
|
92
|
-
/* the basic code points: everything before the last delimiter */
|
|
93
|
-
let basic = input.lastIndexOf(delimiter);
|
|
94
|
-
if (basic < 0) basic = 0;
|
|
95
|
-
for (let j = 0; j < basic; ++j) {
|
|
96
|
-
if (input.charCodeAt(j) >= 0x80) error("not-basic");
|
|
97
|
-
output.push(input.charCodeAt(j));
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/* the deltas, each inserting a code point */
|
|
101
|
-
for (let index = basic > 0 ? basic + 1 : 0; index < inputLength;) {
|
|
102
|
-
const oldi = i;
|
|
103
|
-
for (let w = 1, k = base; ; k += base) {
|
|
104
|
-
if (index >= inputLength) error("invalid-input");
|
|
105
|
-
const digit = basicToDigit(input.charCodeAt(index++));
|
|
106
|
-
if (digit >= base) error("invalid-input");
|
|
107
|
-
if (digit > Math.floor((maxInt - i) / w)) error("overflow");
|
|
108
|
-
i += digit * w;
|
|
109
|
-
const t = k <= bias ? tMin : k >= bias + tMax ? tMax : k - bias;
|
|
110
|
-
if (digit < t) break;
|
|
111
|
-
const baseMinusT = base - t;
|
|
112
|
-
if (w > Math.floor(maxInt / baseMinusT)) error("overflow");
|
|
113
|
-
w *= baseMinusT;
|
|
114
|
-
}
|
|
115
|
-
const out = output.length + 1;
|
|
116
|
-
bias = adapt(i - oldi, out, oldi === 0);
|
|
117
|
-
if (Math.floor(i / out) > maxInt - n) error("overflow");
|
|
118
|
-
n += Math.floor(i / out);
|
|
119
|
-
i %= out;
|
|
120
|
-
output.splice(i++, 0, n);
|
|
121
|
-
}
|
|
122
|
-
return String.fromCodePoint(...output);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/** A Unicode string as Punycode (ASCII only). */
|
|
126
|
-
export function encode(input) {
|
|
127
|
-
const output = [];
|
|
128
|
-
input = ucs2decode(input);
|
|
129
|
-
const inputLength = input.length;
|
|
130
|
-
let n = initialN;
|
|
131
|
-
let delta = 0;
|
|
132
|
-
let bias = initialBias;
|
|
133
|
-
|
|
134
|
-
for (const currentValue of input) {
|
|
135
|
-
if (currentValue < 0x80) output.push(String.fromCharCode(currentValue));
|
|
136
|
-
}
|
|
137
|
-
const basicLength = output.length;
|
|
138
|
-
let handledCPCount = basicLength;
|
|
139
|
-
if (basicLength) output.push(delimiter);
|
|
140
|
-
|
|
141
|
-
while (handledCPCount < inputLength) {
|
|
142
|
-
/* the next code point to insert: the smallest not yet handled */
|
|
143
|
-
let m = maxInt;
|
|
144
|
-
for (const currentValue of input) {
|
|
145
|
-
if (currentValue >= n && currentValue < m) m = currentValue;
|
|
146
|
-
}
|
|
147
|
-
const handledCPCountPlusOne = handledCPCount + 1;
|
|
148
|
-
if (m - n > Math.floor((maxInt - delta) / handledCPCountPlusOne)) error("overflow");
|
|
149
|
-
delta += (m - n) * handledCPCountPlusOne;
|
|
150
|
-
n = m;
|
|
151
|
-
for (const currentValue of input) {
|
|
152
|
-
if (currentValue < n && ++delta > maxInt) error("overflow");
|
|
153
|
-
if (currentValue === n) {
|
|
154
|
-
let q = delta;
|
|
155
|
-
for (let k = base; ; k += base) {
|
|
156
|
-
const t = k <= bias ? tMin : k >= bias + tMax ? tMax : k - bias;
|
|
157
|
-
if (q < t) break;
|
|
158
|
-
const qMinusT = q - t;
|
|
159
|
-
const baseMinusT = base - t;
|
|
160
|
-
output.push(String.fromCharCode(digitToBasic(t + (qMinusT % baseMinusT))));
|
|
161
|
-
q = Math.floor(qMinusT / baseMinusT);
|
|
162
|
-
}
|
|
163
|
-
output.push(String.fromCharCode(digitToBasic(q)));
|
|
164
|
-
bias = adapt(delta, handledCPCountPlusOne, handledCPCount === basicLength);
|
|
165
|
-
delta = 0;
|
|
166
|
-
++handledCPCount;
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
++delta;
|
|
170
|
-
++n;
|
|
171
|
-
}
|
|
172
|
-
return output.join("");
|
|
173
|
-
}
|
|
174
|
-
|
|
175
46
|
/** A domain name (or email address) with its Punycode labels (xn--…) as Unicode. */
|
|
176
47
|
export function toUnicode(input) {
|
|
177
48
|
return mapDomain(input, (string) => (regexPunycode.test(string) ? decode(string.slice(4).toLowerCase()) : string));
|
|
@@ -182,6 +53,7 @@ export function toASCII(input) {
|
|
|
182
53
|
return mapDomain(input, (string) => (regexNonASCII.test(string) ? "xn--" + encode(string) : string));
|
|
183
54
|
}
|
|
184
55
|
|
|
56
|
+
export { decode, encode };
|
|
185
57
|
export const version = "2.1.0";
|
|
186
58
|
export const ucs2 = { decode: ucs2decode, encode: ucs2encode };
|
|
187
59
|
|