@ac-kit/net-webrtc 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/CHANGELOG.md +35 -0
  2. package/LICENSE +28 -0
  3. package/README.md +99 -2
  4. package/dist/_ice-servers.d.ts +15 -0
  5. package/dist/_ice-servers.d.ts.map +1 -0
  6. package/dist/_ice-servers.js +33 -0
  7. package/dist/_ice-servers.js.map +1 -0
  8. package/dist/_mdns.d.ts +53 -0
  9. package/dist/_mdns.d.ts.map +1 -0
  10. package/dist/_mdns.js +239 -0
  11. package/dist/_mdns.js.map +1 -0
  12. package/dist/_native.d.ts +82 -0
  13. package/dist/_native.d.ts.map +1 -0
  14. package/dist/_native.js +51 -0
  15. package/dist/_native.js.map +1 -0
  16. package/dist/_peer-connection-driver.d.ts +95 -0
  17. package/dist/_peer-connection-driver.d.ts.map +1 -0
  18. package/dist/_peer-connection-driver.js +500 -0
  19. package/dist/_peer-connection-driver.js.map +1 -0
  20. package/dist/_stun.d.ts +28 -0
  21. package/dist/_stun.d.ts.map +1 -0
  22. package/dist/_stun.js +115 -0
  23. package/dist/_stun.js.map +1 -0
  24. package/dist/dtls-fingerprint.d.ts +18 -0
  25. package/dist/dtls-fingerprint.d.ts.map +1 -0
  26. package/dist/dtls-fingerprint.js +25 -0
  27. package/dist/dtls-fingerprint.js.map +1 -0
  28. package/dist/errors.d.ts +27 -0
  29. package/dist/errors.d.ts.map +1 -0
  30. package/dist/errors.js +42 -0
  31. package/dist/errors.js.map +1 -0
  32. package/dist/index.d.ts +5 -0
  33. package/dist/index.d.ts.map +1 -0
  34. package/dist/index.js +5 -0
  35. package/dist/index.js.map +1 -0
  36. package/dist/webrtc-data-channel.d.ts +80 -0
  37. package/dist/webrtc-data-channel.d.ts.map +1 -0
  38. package/dist/webrtc-data-channel.js +138 -0
  39. package/dist/webrtc-data-channel.js.map +1 -0
  40. package/dist/webrtc-peer-connection.d.ts +205 -0
  41. package/dist/webrtc-peer-connection.d.ts.map +1 -0
  42. package/dist/webrtc-peer-connection.js +324 -0
  43. package/dist/webrtc-peer-connection.js.map +1 -0
  44. package/package.json +75 -3
package/CHANGELOG.md ADDED
@@ -0,0 +1,35 @@
1
+ # @ac-kit/net-webrtc
2
+
3
+ ## 0.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - a484936: `@ac-kit/net-webrtc`: WebRTC data channels for Node.js, interoperable with
8
+ Chromium, Firefox and Safari's WebKit.
9
+
10
+ - `WebRtcPeerConnection`, with browser-shaped descriptions and candidates:
11
+ `setLocalDescription`, `setRemoteDescription`, `addIceCandidate`, trickled
12
+ `candidate` events, connection, ICE, gathering and signalling states,
13
+ `connected()`, and closing through `close()` or an `AbortSignal`.
14
+ - `WebRtcDataChannel`: reliable or partially reliable (`ordered`,
15
+ `maxRetransmits`, `maxPacketLifeTime`), binary and text messages,
16
+ `bufferedAmount` and `bufferedAmountLow` for pacing.
17
+ - Host candidates on every local IPv4 address, server-reflexive ones from
18
+ `stun:` servers, and browsers' `.local` (mDNS) candidates resolved by
19
+ multicast DNS. A `turn:` URL throws `WebRtcTurnUnsupportedError`.
20
+ - `localFingerprints`, `remoteFingerprints` and `parseSdpFingerprints`, to check
21
+ the DTLS certificates against a trusted channel.
22
+ - ICE, DTLS and SCTP run in webrtc-rs's sans-I/O `rtc`, a native binding
23
+ prebuilt per platform (`@ac-kit/net-webrtc-<platform>`, optional
24
+ dependencies); the UDP sockets and the timer are the JavaScript side's.
25
+
26
+ ### Patch Changes
27
+
28
+ - Updated dependencies [99ca4b9]
29
+ - Updated dependencies [99ca4b9]
30
+ - Updated dependencies [99ca4b9]
31
+ - Updated dependencies [99ca4b9]
32
+ - @ac-kit/async@0.4.0
33
+ - @ac-kit/core@0.8.0
34
+ - @ac-kit/net-address@0.4.0
35
+ - @ac-kit/node@0.8.0
package/LICENSE ADDED
@@ -0,0 +1,28 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2024-2026 Anthony Champagne and contributors
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation
13
+ and/or other materials provided with the distribution.
14
+
15
+ 3. Neither the name of the copyright holder nor the names of its
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md CHANGED
@@ -1,3 +1,100 @@
1
- # Temporary Holding Version
1
+ # @ac-kit/net-webrtc
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ WebRTC data channels for Node.js: a peer connection that browsers can talk to,
4
+ carrying reliable and partially reliable message channels. No audio or video.
5
+
6
+ ICE, DTLS and SCTP are webrtc-rs's [`rtc`](https://github.com/webrtc-rs/rtc)
7
+ state machine, compiled into a native addon. It performs no I/O: the
8
+ connection owns its UDP sockets and its timer on the JavaScript thread, as
9
+ `@ac-kit/net-quic` does.
10
+
11
+ Signalling is the caller's. Descriptions and candidates have the browser's
12
+ shapes (`RTCSessionDescriptionInit`, `RTCIceCandidateInit`), so they travel
13
+ unchanged between a page and Node.
14
+
15
+ ```ts
16
+ import { WebRtcPeerConnection } from "@ac-kit/net-webrtc";
17
+
18
+ const connection = new WebRtcPeerConnection({
19
+ iceServers: ["stun:stun.example.org:3478"],
20
+ });
21
+ connection.subscribe("candidate", (candidate) => signal({ candidate }));
22
+
23
+ const control = connection.createDataChannel("control");
24
+ const state = connection.createDataChannel("state", {
25
+ ordered: false,
26
+ maxRetransmits: 0,
27
+ });
28
+ control.subscribe("message", (data) => {
29
+ // `Uint8Array` for a binary message, `string` for a text one.
30
+ });
31
+
32
+ signal({ description: connection.setLocalDescription() });
33
+ // …then, from the peer:
34
+ connection.setRemoteDescription(answer);
35
+ connection.addIceCandidate(remoteCandidate);
36
+
37
+ await connection.connected();
38
+ control.send("hello");
39
+ ```
40
+
41
+ A channel the peer opens arrives as a `dataChannel` event. `close()` closes
42
+ every channel, then the connection; an `AbortSignal` passed as `signal` does
43
+ the same.
44
+
45
+ ## Candidates
46
+
47
+ Gathering starts with `setLocalDescription`. Each local address gets a host
48
+ candidate: `bindAddress` alone when given, otherwise every non-internal IPv4
49
+ address of the machine, each on its own UDP socket. Each `stun:` server in
50
+ `iceServers` then adds a server-reflexive candidate per socket, when it
51
+ answers within three seconds. `gatheringStateChange` reports `complete` once
52
+ every server has answered or given up.
53
+
54
+ TURN is not supported yet: a `turn:` or `turns:` URL throws
55
+ `WebRtcTurnUnsupportedError` from the constructor, so a configuration that
56
+ relies on a relay fails where it is written rather than silently connecting
57
+ less often. `stuns:` throws `UnsupportedError`.
58
+
59
+ Browsers hide their local addresses behind `.local` names (mDNS). A remote
60
+ candidate carrying one is added once a multicast DNS query resolves it, and
61
+ dropped if nothing answers within three seconds, as a browser drops it. The
62
+ query is sent from port 5353, shared with the system's own responder, because
63
+ browsers answer by multicast only. A dropped candidate often costs nothing:
64
+ the browser's own connectivity checks still reach this side, which learns
65
+ their address from them.
66
+
67
+ ## Authenticating the peer
68
+
69
+ WebRTC authenticates nothing by itself: each side checks the DTLS handshake
70
+ against the certificate fingerprint in the other's description, and whoever
71
+ relays the descriptions can substitute both. `localFingerprints()` and
72
+ `remoteFingerprints()` read them out of the descriptions, to be compared with
73
+ a copy obtained over a channel the application trusts.
74
+
75
+ ## Limits
76
+
77
+ - `maxMessageSize` is 64 KiB by default (RFC 8841) and 256 KiB at most.
78
+ - `send` never blocks: `bufferedAmount` counts the bytes not yet acknowledged
79
+ by the peer, and `bufferedAmountLow` fires when it falls to
80
+ `bufferedAmountLowThreshold`. Pace a bulk sender on it.
81
+ - A channel the peer opened reports `maxRetransmits` and `maxPacketLifeTime`
82
+ as `null`, whatever it was opened with: `rtc` does not expose them. The
83
+ channel still behaves as the peer asked.
84
+ - IPv6 is used only when `bindAddress` is an IPv6 address or `"::"`.
85
+
86
+ ## Platforms
87
+
88
+ The binding is prebuilt for `linux-x64-gnu` and `linux-arm64-gnu` (glibc 2.28
89
+ and later), `darwin-x64`, `darwin-arm64` and `win32-x64-msvc`, each in its own
90
+ package, `@ac-kit/net-webrtc-<platform>`: an optional dependency the package
91
+ manager installs only on the platform it runs on. A package manager told to
92
+ skip optional dependencies (`--no-optional`, `--omit=optional`) leaves the
93
+ binding out.
94
+
95
+ It is loaded on first use: importing the package never fails, but creating a
96
+ connection without the binding throws `UnsupportedError`.
97
+
98
+ See the
99
+ [generated API docs](https://anthochamp.pages.grenadine.io/node-essentials/api/net-webrtc/)
100
+ for the full reference.
@@ -0,0 +1,15 @@
1
+ /** A STUN server to learn server-reflexive addresses from. */
2
+ export type StunServer_ = {
3
+ /** A host name or an IP address, without brackets. */
4
+ readonly host: string;
5
+ readonly port: number;
6
+ };
7
+ /**
8
+ * Parses `iceServers` into the STUN servers to query (RFC 7064 `stun:` URIs).
9
+ *
10
+ * @throws {WebRtcTurnUnsupportedError} For a `turn:` or `turns:` URI.
11
+ * @throws {UnsupportedError} For a `stuns:` URI: STUN over TLS needs TCP.
12
+ * @throws {RangeError} For anything else that is not a `stun:` URI.
13
+ */
14
+ export declare function parseIceServers_(urls: readonly string[]): StunServer_[];
15
+ //# sourceMappingURL=_ice-servers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_ice-servers.d.ts","sourceRoot":"","sources":["../src/_ice-servers.ts"],"names":[],"mappings":"AAIA,8DAA8D;AAC9D,MAAM,MAAM,WAAW,GAAG;IACzB,sDAAsD;IACtD,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACtB,CAAC;AAQF;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,SAAS,MAAM,EAAE,GAAG,WAAW,EAAE,CAmBvE"}
@@ -0,0 +1,33 @@
1
+ import { UnsupportedError } from "@ac-kit/core";
2
+ import { WebRtcTurnUnsupportedError } from "./errors.js";
3
+ /** RFC 8489 §9: STUN's default port over UDP. */
4
+ const DEFAULT_STUN_PORT_ = 3478;
5
+ const STUN_URI_ = /^stun:(?:\[([0-9A-Fa-f:.]+)\]|([^\s:/?#[\]@]+))(?::(\d{1,5}))?$/;
6
+ /**
7
+ * Parses `iceServers` into the STUN servers to query (RFC 7064 `stun:` URIs).
8
+ *
9
+ * @throws {WebRtcTurnUnsupportedError} For a `turn:` or `turns:` URI.
10
+ * @throws {UnsupportedError} For a `stuns:` URI: STUN over TLS needs TCP.
11
+ * @throws {RangeError} For anything else that is not a `stun:` URI.
12
+ */
13
+ export function parseIceServers_(urls) {
14
+ return urls.map((url) => {
15
+ const scheme = url.slice(0, url.indexOf(":") + 1).toLowerCase();
16
+ if (scheme === "turn:" || scheme === "turns:") {
17
+ throw new WebRtcTurnUnsupportedError(url);
18
+ }
19
+ if (scheme === "stuns:") {
20
+ throw new UnsupportedError(`STUN over TLS is not supported: ${url}`);
21
+ }
22
+ const match = STUN_URI_.exec(`stun:${url.slice(scheme.length)}`);
23
+ if (scheme !== "stun:" || match === null) {
24
+ throw new RangeError(`Not a stun: URI: ${url}`);
25
+ }
26
+ const port = match[3] === undefined ? DEFAULT_STUN_PORT_ : Number(match[3]);
27
+ if (port < 1 || port > 65535) {
28
+ throw new RangeError(`Invalid port in ${url}`);
29
+ }
30
+ return { host: match[1] ?? match[2], port };
31
+ });
32
+ }
33
+ //# sourceMappingURL=_ice-servers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_ice-servers.js","sourceRoot":"","sources":["../src/_ice-servers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAEhD,OAAO,EAAE,0BAA0B,EAAE,MAAM,aAAa,CAAC;AASzD,iDAAiD;AACjD,MAAM,kBAAkB,GAAG,IAAI,CAAC;AAEhC,MAAM,SAAS,GACd,iEAAiE,CAAC;AAEnE;;;;;;GAMG;AACH,MAAM,UAAU,gBAAgB,CAAC,IAAuB;IACvD,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE;QACvB,MAAM,MAAM,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC;QAChE,IAAI,MAAM,KAAK,OAAO,IAAI,MAAM,KAAK,QAAQ,EAAE,CAAC;YAC/C,MAAM,IAAI,0BAA0B,CAAC,GAAG,CAAC,CAAC;QAC3C,CAAC;QACD,IAAI,MAAM,KAAK,QAAQ,EAAE,CAAC;YACzB,MAAM,IAAI,gBAAgB,CAAC,mCAAmC,GAAG,EAAE,CAAC,CAAC;QACtE,CAAC;QACD,MAAM,KAAK,GAAG,SAAS,CAAC,IAAI,CAAC,QAAQ,GAAG,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;QACjE,IAAI,MAAM,KAAK,OAAO,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;YAC1C,MAAM,IAAI,UAAU,CAAC,oBAAoB,GAAG,EAAE,CAAC,CAAC;QACjD,CAAC;QACD,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,KAAK,SAAS,CAAC,CAAC,CAAC,kBAAkB,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;QAC5E,IAAI,IAAI,GAAG,CAAC,IAAI,IAAI,GAAG,KAAM,EAAE,CAAC;YAC/B,MAAM,IAAI,UAAU,CAAC,mBAAmB,GAAG,EAAE,CAAC,CAAC;QAChD,CAAC;QACD,OAAO,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,CAAC,CAAE,EAAE,IAAI,EAAE,CAAC;IAC9C,CAAC,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,53 @@
1
+ /**
2
+ * A one-shot multicast DNS query (RFC 6762 §5.1) for the address behind a
3
+ * `.local` name: what a browser puts in its host ICE candidates instead of the
4
+ * address itself (draft-ietf-mmusic-mdns-ice-candidates).
5
+ *
6
+ * Only what that needs: one message with an A and an AAAA question, and the
7
+ * answers matching the name read back. The query goes out from port 5353,
8
+ * shared with the system's responder and joined to the group on every IPv4
9
+ * interface, because browsers' responders answer by multicast only. Where 5353
10
+ * cannot be shared it goes out from an ephemeral port with the unicast-response
11
+ * bit (§5.4, §6.7), which Avahi answers but browsers do not. No caching, no
12
+ * probing, no announcing: `@ac-kit/net-dns` is the place for a DNS message
13
+ * codec when it exists.
14
+ */
15
+ /** Where queries go: the mDNS IPv4 group and port. */
16
+ export declare const MDNS_IPV4_GROUP_: {
17
+ readonly address: "224.0.0.251";
18
+ readonly port: 5353;
19
+ };
20
+ /**
21
+ * A query for `name`'s A and AAAA records, asking for a unicast response unless
22
+ * `unicastResponse` is `false`.
23
+ */
24
+ export declare function encodeMdnsQuery_(name: string, unicastResponse?: boolean): Uint8Array<ArrayBuffer>;
25
+ /**
26
+ * The addresses a response gives `name`, IPv4 first; empty when it answers
27
+ * something else or is malformed.
28
+ */
29
+ export declare function decodeMdnsAnswers_(data: Uint8Array, name: string): string[];
30
+ /** Where and how long {@link resolveMdnsName_} asks. */
31
+ export type MdnsQueryOptions_ = {
32
+ /**
33
+ * Asks this one responder by unicast instead of the link by multicast: for a
34
+ * test's fake responder.
35
+ */
36
+ readonly server?: {
37
+ readonly address: string;
38
+ readonly port: number;
39
+ };
40
+ /** Gives up after this long. */
41
+ readonly timeoutMs: number;
42
+ /** Interval between retransmissions of the query. */
43
+ readonly retryMs: number;
44
+ readonly signal?: AbortSignal;
45
+ };
46
+ /**
47
+ * Asks the local link for `name`'s address.
48
+ *
49
+ * @returns The first address answered, IPv4 preferred, or `null` when nobody
50
+ * answered in time or the signal aborted.
51
+ */
52
+ export declare function resolveMdnsName_(name: string, options: MdnsQueryOptions_): Promise<string | null>;
53
+ //# sourceMappingURL=_mdns.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_mdns.d.ts","sourceRoot":"","sources":["../src/_mdns.ts"],"names":[],"mappings":"AAUA;;;;;;;;;;;;;GAaG;AAEH,sDAAsD;AACtD,eAAO,MAAM,gBAAgB;aAAK,OAAO,EAAE,aAAa;aAAE,IAAI,EAAE,IAAI;CAAW,CAAC;AAShF;;;GAGG;AACH,wBAAgB,gBAAgB,CAC/B,IAAI,EAAE,MAAM,EACZ,eAAe,UAAO,GACpB,UAAU,CAAC,WAAW,CAAC,CAiCzB;AAiDD;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,CAoD3E;AAED,wDAAwD;AACxD,MAAM,MAAM,iBAAiB,GAAG;IAC/B;;;OAGG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE;QAAE,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC;IACtE,gCAAgC;IAChC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,qDAAqD;IACrD,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,MAAM,CAAC,EAAE,WAAW,CAAC;CAC9B,CAAC;AAEF;;;;;GAKG;AACH,wBAAsB,gBAAgB,CACrC,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,iBAAiB,GACxB,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAkDxB"}
package/dist/_mdns.js ADDED
@@ -0,0 +1,239 @@
1
+ import { isIP } from "node:net";
2
+ import { networkInterfaces } from "node:os";
3
+ import { bitAddressFromBytes, printIpv4Address, printIpv6Address, } from "@ac-kit/net-address";
4
+ import { DgramSocket } from "@ac-kit/node";
5
+ /**
6
+ * A one-shot multicast DNS query (RFC 6762 §5.1) for the address behind a
7
+ * `.local` name: what a browser puts in its host ICE candidates instead of the
8
+ * address itself (draft-ietf-mmusic-mdns-ice-candidates).
9
+ *
10
+ * Only what that needs: one message with an A and an AAAA question, and the
11
+ * answers matching the name read back. The query goes out from port 5353,
12
+ * shared with the system's responder and joined to the group on every IPv4
13
+ * interface, because browsers' responders answer by multicast only. Where 5353
14
+ * cannot be shared it goes out from an ephemeral port with the unicast-response
15
+ * bit (§5.4, §6.7), which Avahi answers but browsers do not. No caching, no
16
+ * probing, no announcing: `@ac-kit/net-dns` is the place for a DNS message
17
+ * codec when it exists.
18
+ */
19
+ /** Where queries go: the mDNS IPv4 group and port. */
20
+ export const MDNS_IPV4_GROUP_ = { address: "224.0.0.251", port: 5353 };
21
+ const TYPE_A_ = 1;
22
+ const TYPE_AAAA_ = 28;
23
+ const CLASS_IN_ = 1;
24
+ /** The unicast-response bit of a question's class (RFC 6762 §5.4). */
25
+ const UNICAST_RESPONSE_ = 0x8000;
26
+ const MAX_POINTER_JUMPS_ = 32;
27
+ /**
28
+ * A query for `name`'s A and AAAA records, asking for a unicast response unless
29
+ * `unicastResponse` is `false`.
30
+ */
31
+ export function encodeMdnsQuery_(name, unicastResponse = true) {
32
+ const labels = name.replace(/\.$/, "").split(".");
33
+ const encodedLabels = labels.map((label) => new TextEncoder().encode(label));
34
+ for (const label of encodedLabels) {
35
+ if (label.byteLength === 0 || label.byteLength > 63) {
36
+ throw new RangeError(`Invalid DNS label in ${JSON.stringify(name)}`);
37
+ }
38
+ }
39
+ const nameLength = encodedLabels.reduce((sum, label) => sum + 1 + label.byteLength, 1);
40
+ const questionLength = nameLength + 4;
41
+ const message = new Uint8Array(12 + 2 * questionLength);
42
+ const view = new DataView(message.buffer);
43
+ // Id 0, flags 0 (a standard query), two questions.
44
+ view.setUint16(4, 2);
45
+ let offset = 12;
46
+ for (const type of [TYPE_A_, TYPE_AAAA_]) {
47
+ for (const label of encodedLabels) {
48
+ message[offset++] = label.byteLength;
49
+ message.set(label, offset);
50
+ offset += label.byteLength;
51
+ }
52
+ message[offset++] = 0;
53
+ view.setUint16(offset, type);
54
+ view.setUint16(offset + 2, unicastResponse ? CLASS_IN_ | UNICAST_RESPONSE_ : CLASS_IN_);
55
+ offset += 4;
56
+ }
57
+ return message;
58
+ }
59
+ /**
60
+ * Reads a possibly compressed name at `offset`.
61
+ *
62
+ * @returns The name, lowercased, and the offset just past it in place, or
63
+ * `null` when it is malformed.
64
+ */
65
+ function readName_(data, offset) {
66
+ const labels = [];
67
+ let position = offset;
68
+ let next = null;
69
+ for (let jumps = 0; jumps <= MAX_POINTER_JUMPS_;) {
70
+ const length = data[position];
71
+ if (length === undefined) {
72
+ return null;
73
+ }
74
+ if (length === 0) {
75
+ return {
76
+ name: labels.join(".").toLowerCase(),
77
+ next: next ?? position + 1,
78
+ };
79
+ }
80
+ if ((length & 0xc0) === 0xc0) {
81
+ const low = data[position + 1];
82
+ if (low === undefined) {
83
+ return null;
84
+ }
85
+ next ??= position + 2;
86
+ position = ((length & 0x3f) << 8) | low;
87
+ jumps++;
88
+ continue;
89
+ }
90
+ if ((length & 0xc0) !== 0 || position + 1 + length > data.byteLength) {
91
+ return null;
92
+ }
93
+ labels.push(new TextDecoder().decode(data.subarray(position + 1, position + 1 + length)));
94
+ position += 1 + length;
95
+ }
96
+ return null;
97
+ }
98
+ /**
99
+ * The addresses a response gives `name`, IPv4 first; empty when it answers
100
+ * something else or is malformed.
101
+ */
102
+ export function decodeMdnsAnswers_(data, name) {
103
+ if (data.byteLength < 12) {
104
+ return [];
105
+ }
106
+ const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
107
+ // A response (QR set) only.
108
+ if ((view.getUint16(2) & 0x8000) === 0) {
109
+ return [];
110
+ }
111
+ const questions = view.getUint16(4);
112
+ const records = view.getUint16(6) + view.getUint16(8) + view.getUint16(10);
113
+ const wanted = name.replace(/\.$/, "").toLowerCase();
114
+ let offset = 12;
115
+ for (let index = 0; index < questions; index++) {
116
+ const read = readName_(data, offset);
117
+ if (read === null) {
118
+ return [];
119
+ }
120
+ offset = read.next + 4;
121
+ }
122
+ const ipv4 = [];
123
+ const ipv6 = [];
124
+ for (let index = 0; index < records; index++) {
125
+ const read = readName_(data, offset);
126
+ if (read === null || read.next + 10 > data.byteLength) {
127
+ break;
128
+ }
129
+ const type = view.getUint16(read.next);
130
+ const recordClass = view.getUint16(read.next + 2) & ~UNICAST_RESPONSE_;
131
+ const length = view.getUint16(read.next + 8);
132
+ const rdata = read.next + 10;
133
+ if (rdata + length > data.byteLength) {
134
+ break;
135
+ }
136
+ if (read.name === wanted && recordClass === CLASS_IN_) {
137
+ if (type === TYPE_A_ && length === 4) {
138
+ ipv4.push(printIpv4Address(bitAddressFromBytes(data.slice(rdata, rdata + 4), 32)));
139
+ }
140
+ else if (type === TYPE_AAAA_ && length === 16) {
141
+ ipv6.push(printIpv6Address(bitAddressFromBytes(data.slice(rdata, rdata + 16), 128)));
142
+ }
143
+ }
144
+ offset = rdata + length;
145
+ }
146
+ return [...ipv4, ...ipv6];
147
+ }
148
+ /**
149
+ * Asks the local link for `name`'s address.
150
+ *
151
+ * @returns The first address answered, IPv4 preferred, or `null` when nobody
152
+ * answered in time or the signal aborted.
153
+ */
154
+ export async function resolveMdnsName_(name, options) {
155
+ if (options.signal?.aborted) {
156
+ return null;
157
+ }
158
+ const server = options.server ?? MDNS_IPV4_GROUP_;
159
+ // Throws on a malformed name before any socket exists.
160
+ encodeMdnsQuery_(name);
161
+ const multicast = options.server === undefined ? await openMulticastSocket_() : null;
162
+ const socket = multicast ??
163
+ DgramSocket.from({ type: isIP(server.address) === 6 ? "udp6" : "udp4" });
164
+ const query = encodeMdnsQuery_(name, multicast === null);
165
+ const answered = Promise.withResolvers();
166
+ socket.subscribe("message", (data) => {
167
+ const [address] = decodeMdnsAnswers_(data, name);
168
+ if (address !== undefined) {
169
+ answered.resolve(address);
170
+ }
171
+ });
172
+ socket.subscribe("error", () => answered.resolve(null));
173
+ const timeout = setTimeout(() => answered.resolve(null), options.timeoutMs);
174
+ const abort = () => answered.resolve(null);
175
+ options.signal?.addEventListener("abort", abort, { once: true });
176
+ // Aborted while the multicast socket was being opened.
177
+ if (options.signal?.aborted) {
178
+ abort();
179
+ }
180
+ let retry;
181
+ try {
182
+ if (multicast === null) {
183
+ await socket.bind(0);
184
+ }
185
+ const send = () => {
186
+ socket.send(server.port, server.address, query).catch(() => {
187
+ // Lost, as the network might have lost it: the next retry or the
188
+ // timeout follows.
189
+ });
190
+ };
191
+ send();
192
+ retry = setInterval(send, options.retryMs);
193
+ return await answered.promise;
194
+ }
195
+ catch {
196
+ return null;
197
+ }
198
+ finally {
199
+ clearTimeout(timeout);
200
+ clearInterval(retry);
201
+ options.signal?.removeEventListener("abort", abort);
202
+ await socket.close().catch(() => undefined);
203
+ }
204
+ }
205
+ /**
206
+ * A socket on port 5353 joined to the IPv4 mDNS group on every interface, or
207
+ * `null` when the port cannot be shared.
208
+ */
209
+ async function openMulticastSocket_() {
210
+ const socket = DgramSocket.from({ type: "udp4", reuseAddr: true });
211
+ try {
212
+ await socket.bind(MDNS_IPV4_GROUP_.port);
213
+ }
214
+ catch {
215
+ await socket.close().catch(() => undefined);
216
+ return null;
217
+ }
218
+ let joined = false;
219
+ for (const addresses of Object.values(networkInterfaces())) {
220
+ for (const { address, family } of addresses ?? []) {
221
+ if (family !== "IPv4") {
222
+ continue;
223
+ }
224
+ try {
225
+ socket.addMembership(MDNS_IPV4_GROUP_.address, address);
226
+ joined = true;
227
+ }
228
+ catch {
229
+ // An interface without multicast: the others may answer.
230
+ }
231
+ }
232
+ }
233
+ if (!joined) {
234
+ await socket.close().catch(() => undefined);
235
+ return null;
236
+ }
237
+ return socket;
238
+ }
239
+ //# sourceMappingURL=_mdns.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_mdns.js","sourceRoot":"","sources":["../src/_mdns.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AAChC,OAAO,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAE5C,OAAO,EACN,mBAAmB,EACnB,gBAAgB,EAChB,gBAAgB,GAChB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAE3C;;;;;;;;;;;;;GAaG;AAEH,sDAAsD;AACtD,MAAM,CAAC,MAAM,gBAAgB,GAAG,EAAE,OAAO,EAAE,aAAa,EAAE,IAAI,EAAE,IAAI,EAAW,CAAC;AAEhF,MAAM,OAAO,GAAG,CAAC,CAAC;AAClB,MAAM,UAAU,GAAG,EAAE,CAAC;AACtB,MAAM,SAAS,GAAG,CAAC,CAAC;AACpB,sEAAsE;AACtE,MAAM,iBAAiB,GAAG,MAAM,CAAC;AACjC,MAAM,kBAAkB,GAAG,EAAE,CAAC;AAE9B;;;GAGG;AACH,MAAM,UAA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,SAAS;YACV,CAAC;YACD,IAAI,CAAC;gBACJ,MAAM,CAAC,aAAa,CAAC,gBAAgB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;gBACxD,MAAM,GAAG,IAAI,CAAC;YACf,CAAC;YAAC,MAAM,CAAC;gBACR,yDAAyD;YAC1D,CAAC;QACF,CAAC;IACF,CAAC;IACD,IAAI,CAAC,MAAM,EAAE,CAAC;QACb,MAAM,MAAM,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAAC;QAC5C,OAAO,IAAI,CAAC;IACb,CAAC;IACD,OAAO,MAAM,CAAC;AACf,CAAC"}
@@ -0,0 +1,82 @@
1
+ /** One event of a peer connection or of one of its channels (`channel`). */
2
+ export type NativeEvent_ = {
3
+ readonly type: "candidate" | "state" | "iceState" | "gatheringState" | "signalingState" | "open" | "closed" | "error" | "message" | "bufferedAmountLow";
4
+ readonly channel?: number;
5
+ readonly candidate?: string;
6
+ readonly mid?: string;
7
+ readonly state?: string;
8
+ readonly message?: string;
9
+ /** A `Buffer` over memory the binding allocated: never shared. */
10
+ readonly binary?: Uint8Array<ArrayBuffer>;
11
+ readonly text?: string;
12
+ };
13
+ /** A datagram to send from the socket bound to `localAddress`. */
14
+ export type NativeDatagram_ = {
15
+ readonly data: Uint8Array<ArrayBuffer>;
16
+ readonly address: string;
17
+ readonly port: number;
18
+ readonly localAddress: string;
19
+ };
20
+ /** What one `poll` returns. */
21
+ export type NativePoll_ = {
22
+ readonly datagrams: NativeDatagram_[];
23
+ readonly events: NativeEvent_[];
24
+ readonly timeoutMs?: number;
25
+ };
26
+ export type NativeSessionDescription_ = {
27
+ readonly type: string;
28
+ readonly sdp: string;
29
+ };
30
+ export type NativeDataChannelOptions_ = {
31
+ readonly ordered: boolean;
32
+ readonly maxRetransmits?: number;
33
+ readonly maxPacketLifeTime?: number;
34
+ readonly protocol?: string;
35
+ readonly negotiatedId?: number;
36
+ };
37
+ export type NativeDataChannelInfo_ = {
38
+ readonly label: string;
39
+ readonly protocol: string;
40
+ readonly ordered: boolean;
41
+ readonly maxRetransmits?: number;
42
+ readonly maxPacketLifeTime?: number;
43
+ readonly stream?: number;
44
+ };
45
+ /** Webrtc-rs's sans-I/O peer connection, as `rust/rtc-node` exposes it. */
46
+ export type NativePeerConnection_ = {
47
+ setLocalDescription(type: string | null): NativeSessionDescription_;
48
+ setRemoteDescription(description: NativeSessionDescription_): void;
49
+ localDescription(): NativeSessionDescription_ | null;
50
+ remoteDescription(): NativeSessionDescription_ | null;
51
+ addLocalHostCandidate(address: string, port: number): void;
52
+ addLocalServerReflexiveCandidate(address: string, port: number, relatedAddress: string, relatedPort: number): void;
53
+ endOfLocalCandidates(): void;
54
+ addRemoteCandidate(candidate: string, mid: string | null): void;
55
+ handleRead(data: Uint8Array, localAddress: string, localPort: number, peerAddress: string, peerPort: number): void;
56
+ poll(maxDatagrams: number): NativePoll_;
57
+ onTimeout(): void;
58
+ createDataChannel(label: string, options: NativeDataChannelOptions_): number;
59
+ dataChannelInfo(channel: number): NativeDataChannelInfo_;
60
+ dataChannelSendBinary(channel: number, data: Uint8Array): void;
61
+ dataChannelSendText(channel: number, text: string): void;
62
+ dataChannelBufferedAmount(channel: number): number;
63
+ dataChannelSetBufferedAmountLowThreshold(channel: number, amount: number): void;
64
+ dataChannelClose(channel: number): void;
65
+ close(): void;
66
+ };
67
+ type NativeBinding_ = {
68
+ readonly RtcPeerConnection: new (options: {
69
+ readonly maxMessageSize?: number;
70
+ }) => NativePeerConnection_;
71
+ };
72
+ /**
73
+ * The native binding for this platform, loaded on first use so that importing
74
+ * the package never fails on a platform without one. Each platform's binary is
75
+ * its own package, `@ac-kit/net-webrtc-<platform>`, which the package manager
76
+ * installs as an optional dependency only where it runs.
77
+ *
78
+ * @throws {UnsupportedError} When no binary was installed for this platform.
79
+ */
80
+ export declare function nativeWebRtc_(): NativeBinding_;
81
+ export {};
82
+ //# sourceMappingURL=_native.d.ts.map
@@ -0,0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