rwebtransport 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 (122) hide show
  1. package/Cargo.lock +666 -0
  2. package/Cargo.toml +31 -0
  3. package/LICENSE +202 -0
  4. package/NOTICE +41 -0
  5. package/README.md +550 -0
  6. package/crates/echo-server/Cargo.toml +20 -0
  7. package/crates/echo-server/src/main.rs +666 -0
  8. package/crates/native/Cargo.toml +23 -0
  9. package/crates/native/src/config.rs +129 -0
  10. package/crates/native/src/driver.rs +463 -0
  11. package/crates/native/src/lib.rs +821 -0
  12. package/crates/native/src/server.rs +718 -0
  13. package/crates/native/src/session.rs +1191 -0
  14. package/crates/native/src/tls.rs +96 -0
  15. package/crates/wtcore/Cargo.toml +15 -0
  16. package/crates/wtcore/src/h3.rs +226 -0
  17. package/crates/wtcore/src/lib.rs +9 -0
  18. package/dist/datagrams.d.ts +16 -0
  19. package/dist/errors.d.ts +10 -0
  20. package/dist/index.d.ts +7 -0
  21. package/dist/index.js +1512 -0
  22. package/dist/index.js.map +1 -0
  23. package/dist/index.mjs +1473 -0
  24. package/dist/index.mjs.map +1 -0
  25. package/dist/loader.d.ts +2 -0
  26. package/dist/native.d.ts +226 -0
  27. package/dist/server.d.ts +44 -0
  28. package/dist/streams.d.ts +14 -0
  29. package/dist/types.d.ts +24 -0
  30. package/dist/webtransport.d.ts +20 -0
  31. package/package.json +79 -0
  32. package/prebuilds/win32-x64/rwebtransport.node +0 -0
  33. package/scripts/bench.mjs +180 -0
  34. package/scripts/build.js +67 -0
  35. package/scripts/postinstall.js +67 -0
  36. package/scripts/setup-unix.sh +129 -0
  37. package/scripts/setup-windows.ps1 +229 -0
  38. package/vendor/quiche/AGENTS.md +75 -0
  39. package/vendor/quiche/COPYING +23 -0
  40. package/vendor/quiche/Cargo.lock +1212 -0
  41. package/vendor/quiche/Cargo.toml +215 -0
  42. package/vendor/quiche/README.md +466 -0
  43. package/vendor/quiche/examples/Makefile +46 -0
  44. package/vendor/quiche/examples/README.md +20 -0
  45. package/vendor/quiche/examples/cert-big.crt +90 -0
  46. package/vendor/quiche/examples/cert.crt +18 -0
  47. package/vendor/quiche/examples/cert.key +28 -0
  48. package/vendor/quiche/examples/client.c +343 -0
  49. package/vendor/quiche/examples/client.rs +281 -0
  50. package/vendor/quiche/examples/gen-certs.sh +15 -0
  51. package/vendor/quiche/examples/http3-client.c +473 -0
  52. package/vendor/quiche/examples/http3-client.rs +349 -0
  53. package/vendor/quiche/examples/http3-server.c +614 -0
  54. package/vendor/quiche/examples/http3-server.rs +680 -0
  55. package/vendor/quiche/examples/qpack-decode.rs +87 -0
  56. package/vendor/quiche/examples/qpack-encode.rs +88 -0
  57. package/vendor/quiche/examples/rootca.crt +21 -0
  58. package/vendor/quiche/examples/server.c +529 -0
  59. package/vendor/quiche/examples/server.rs +529 -0
  60. package/vendor/quiche/include/quiche.h +1282 -0
  61. package/vendor/quiche/quiche.svg +1 -0
  62. package/vendor/quiche/src/buffers.rs +105 -0
  63. package/vendor/quiche/src/build.rs +66 -0
  64. package/vendor/quiche/src/cid.rs +1079 -0
  65. package/vendor/quiche/src/crypto/boringssl.rs +373 -0
  66. package/vendor/quiche/src/crypto/mod.rs +671 -0
  67. package/vendor/quiche/src/dgram.rs +118 -0
  68. package/vendor/quiche/src/error.rs +263 -0
  69. package/vendor/quiche/src/ffi.rs +2290 -0
  70. package/vendor/quiche/src/flowcontrol.rs +246 -0
  71. package/vendor/quiche/src/frame.rs +2172 -0
  72. package/vendor/quiche/src/h3/AGENTS.md +54 -0
  73. package/vendor/quiche/src/h3/ffi.rs +442 -0
  74. package/vendor/quiche/src/h3/frame.rs +1404 -0
  75. package/vendor/quiche/src/h3/mod.rs +8223 -0
  76. package/vendor/quiche/src/h3/qpack/decoder.rs +288 -0
  77. package/vendor/quiche/src/h3/qpack/encoder.rs +235 -0
  78. package/vendor/quiche/src/h3/qpack/mod.rs +202 -0
  79. package/vendor/quiche/src/h3/qpack/static_table.rs +323 -0
  80. package/vendor/quiche/src/h3/stream.rs +1595 -0
  81. package/vendor/quiche/src/lib.rs +9556 -0
  82. package/vendor/quiche/src/minmax.rs +446 -0
  83. package/vendor/quiche/src/packet.rs +2334 -0
  84. package/vendor/quiche/src/path.rs +1374 -0
  85. package/vendor/quiche/src/pmtud.rs +652 -0
  86. package/vendor/quiche/src/rand.rs +64 -0
  87. package/vendor/quiche/src/range_buf.rs +186 -0
  88. package/vendor/quiche/src/ranges.rs +755 -0
  89. package/vendor/quiche/src/recovery/AGENTS.md +80 -0
  90. package/vendor/quiche/src/recovery/bandwidth.rs +565 -0
  91. package/vendor/quiche/src/recovery/bytes_in_flight.rs +179 -0
  92. package/vendor/quiche/src/recovery/congestion/cubic.rs +1038 -0
  93. package/vendor/quiche/src/recovery/congestion/delivery_rate.rs +514 -0
  94. package/vendor/quiche/src/recovery/congestion/hystart.rs +241 -0
  95. package/vendor/quiche/src/recovery/congestion/mod.rs +361 -0
  96. package/vendor/quiche/src/recovery/congestion/prr.rs +238 -0
  97. package/vendor/quiche/src/recovery/congestion/recovery.rs +1170 -0
  98. package/vendor/quiche/src/recovery/congestion/reno.rs +291 -0
  99. package/vendor/quiche/src/recovery/congestion/test_sender.rs +201 -0
  100. package/vendor/quiche/src/recovery/gcongestion/bbr/bandwidth_sampler.rs +1947 -0
  101. package/vendor/quiche/src/recovery/gcongestion/bbr/windowed_filter.rs +158 -0
  102. package/vendor/quiche/src/recovery/gcongestion/bbr.rs +37 -0
  103. package/vendor/quiche/src/recovery/gcongestion/bbr2/drain.rs +120 -0
  104. package/vendor/quiche/src/recovery/gcongestion/bbr2/mode.rs +318 -0
  105. package/vendor/quiche/src/recovery/gcongestion/bbr2/network_model.rs +784 -0
  106. package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_bw.rs +729 -0
  107. package/vendor/quiche/src/recovery/gcongestion/bbr2/probe_rtt.rs +175 -0
  108. package/vendor/quiche/src/recovery/gcongestion/bbr2/startup.rs +190 -0
  109. package/vendor/quiche/src/recovery/gcongestion/bbr2.rs +855 -0
  110. package/vendor/quiche/src/recovery/gcongestion/mod.rs +275 -0
  111. package/vendor/quiche/src/recovery/gcongestion/pacer.rs +294 -0
  112. package/vendor/quiche/src/recovery/gcongestion/recovery.rs +1384 -0
  113. package/vendor/quiche/src/recovery/mod.rs +3086 -0
  114. package/vendor/quiche/src/recovery/rtt.rs +152 -0
  115. package/vendor/quiche/src/stream/mod.rs +2210 -0
  116. package/vendor/quiche/src/stream/recv_buf.rs +1236 -0
  117. package/vendor/quiche/src/stream/send_buf.rs +849 -0
  118. package/vendor/quiche/src/test_utils.rs +681 -0
  119. package/vendor/quiche/src/tests.rs +12922 -0
  120. package/vendor/quiche/src/tls/boringssl.rs +375 -0
  121. package/vendor/quiche/src/tls/mod.rs +1245 -0
  122. package/vendor/quiche/src/transport_params.rs +628 -0
@@ -0,0 +1,20 @@
1
+ import { SessionCore } from './native.js';
2
+ import { WebTransportDatagramDuplexStream } from './datagrams.js';
3
+ import { WebTransportBidirectionalStream, WebTransportReceiveStream, WebTransportSendStream } from './streams.js';
4
+ import type { WebTransportCloseInfo, WebTransportCloseOptions, WebTransportOptions } from './types.js';
5
+ export declare class WebTransportSession {
6
+ protected readonly core: SessionCore;
7
+ readonly datagrams: WebTransportDatagramDuplexStream;
8
+ readonly incomingBidirectionalStreams: ReadableStream<WebTransportBidirectionalStream>;
9
+ readonly incomingUnidirectionalStreams: ReadableStream<WebTransportReceiveStream>;
10
+ private closeCalled;
11
+ constructor(core: SessionCore);
12
+ get ready(): Promise<void>;
13
+ get closed(): Promise<WebTransportCloseInfo>;
14
+ createBidirectionalStream(): Promise<WebTransportBidirectionalStream>;
15
+ createUnidirectionalStream(): Promise<WebTransportSendStream>;
16
+ close(closeInfo?: WebTransportCloseOptions): void;
17
+ }
18
+ export declare class WebTransport extends WebTransportSession {
19
+ constructor(url: string, options?: WebTransportOptions);
20
+ }
package/package.json ADDED
@@ -0,0 +1,79 @@
1
+ {
2
+ "name": "rwebtransport",
3
+ "version": "0.1.0",
4
+ "description": "A fully-compatible WebTransport client and server for Node.js, powered by Cloudflare quiche + BoringSSL via neon.",
5
+ "author": "Dacely Cloud",
6
+ "homepage": "https://github.com/dacely-cloud/rwebtransport#readme",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/dacely-cloud/rwebtransport.git"
10
+ },
11
+ "bugs": {
12
+ "url": "https://github.com/dacely-cloud/rwebtransport/issues"
13
+ },
14
+ "type": "commonjs",
15
+ "main": "./dist/index.js",
16
+ "module": "./dist/index.mjs",
17
+ "types": "./dist/index.d.ts",
18
+ "exports": {
19
+ ".": {
20
+ "types": "./dist/index.d.ts",
21
+ "import": "./dist/index.mjs",
22
+ "require": "./dist/index.js"
23
+ }
24
+ },
25
+ "files": [
26
+ "dist",
27
+ "prebuilds",
28
+ "scripts",
29
+ "crates",
30
+ "vendor",
31
+ "Cargo.toml",
32
+ "Cargo.lock",
33
+ "README.md",
34
+ "NOTICE"
35
+ ],
36
+ "engines": {
37
+ "node": ">=24 <25 || >=26 <27"
38
+ },
39
+ "scripts": {
40
+ "build": "npm run build:rust && npm run build:ts",
41
+ "build:rust": "node scripts/build.js",
42
+ "build:rust:debug": "node scripts/build.js --debug",
43
+ "build:ts": "tsup src/index.ts --format cjs,esm --sourcemap --clean --shims --out-dir dist && tsc -p tsconfig.build.json",
44
+ "setup:windows": "powershell -ExecutionPolicy Bypass -File scripts/setup-windows.ps1",
45
+ "setup:unix": "bash scripts/setup-unix.sh",
46
+ "postinstall": "node scripts/postinstall.js",
47
+ "pretest": "npm run build:ts",
48
+ "test": "vitest run",
49
+ "test:watch": "vitest",
50
+ "typecheck": "tsc --noEmit",
51
+ "lint": "eslint .",
52
+ "lint:fix": "eslint . --fix",
53
+ "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
54
+ "format:check": "prettier --check \"src/**/*.ts\" \"test/**/*.ts\""
55
+ },
56
+ "devDependencies": {
57
+ "@eslint/js": "^10.0.1",
58
+ "@types/node": "^26.1.1",
59
+ "@vitest/coverage-v8": "^4.1.10",
60
+ "esbuild": "^0.28.1",
61
+ "eslint": "^10.7.0",
62
+ "prettier": "^3.9.5",
63
+ "tsup": "^8.5.1",
64
+ "typescript": "^6.0.3",
65
+ "typescript-eslint": "^8.64.0",
66
+ "vitest": "^4.1.10"
67
+ },
68
+ "overrides": {
69
+ "esbuild": "^0.28.1"
70
+ },
71
+ "keywords": [
72
+ "webtransport",
73
+ "quic",
74
+ "http3",
75
+ "quiche",
76
+ "neon"
77
+ ],
78
+ "license": "Apache-2.0"
79
+ }
@@ -0,0 +1,180 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ // Loopback micro-benchmark: our client <-> our server (both rwebtransport).
3
+ // Measures handshake rate, bidi stream throughput, request/response RTT, and
4
+ // datagram rate. Numbers reflect library + crypto + loopback overhead (no real
5
+ // network), so they characterise CPU cost, not internet performance.
6
+
7
+ import { X509Certificate, createHash } from 'node:crypto';
8
+ import { readFileSync } from 'node:fs';
9
+ import { dirname, join } from 'node:path';
10
+ import { performance } from 'node:perf_hooks';
11
+ import { fileURLToPath } from 'node:url';
12
+
13
+ import { WebTransport, WebTransportServer } from '../dist/index.mjs';
14
+
15
+ const ROOT = join(dirname(fileURLToPath(import.meta.url)), '..');
16
+ const CERT = join(ROOT, 'test', 'fixtures', 'cert.pem');
17
+ const KEY = join(ROOT, 'test', 'fixtures', 'key.pem');
18
+ const certHash = new Uint8Array(
19
+ createHash('sha256')
20
+ .update(new X509Certificate(readFileSync(CERT)).raw)
21
+ .digest(),
22
+ );
23
+
24
+ // Watchdog: never let the benchmark hang the box.
25
+ const watchdog = setTimeout(() => {
26
+ console.error('bench watchdog: timed out, exiting');
27
+ process.exit(1);
28
+ }, 120_000);
29
+ watchdog.unref();
30
+
31
+ const server = new WebTransportServer({ port: 0, host: '127.0.0.1', cert: CERT, key: KEY });
32
+ await server.ready;
33
+ const url = `https://127.0.0.1:${server.port}/bench`;
34
+
35
+ // Server: echo every bidi stream and every datagram.
36
+ void (async () => {
37
+ const reader = server.incomingSessions.getReader();
38
+ for (;;) {
39
+ const { value: session, done } = await reader.read();
40
+ if (done) break;
41
+ void (async () => {
42
+ const r = session.incomingBidirectionalStreams.getReader();
43
+ for (;;) {
44
+ const { value: s, done } = await r.read();
45
+ if (done) break;
46
+ if (s) void s.readable.pipeTo(s.writable).catch(() => {});
47
+ }
48
+ })();
49
+ void (async () => {
50
+ const r = session.datagrams.readable.getReader();
51
+ const w = session.datagrams.writable.getWriter();
52
+ for (;;) {
53
+ const { value, done } = await r.read();
54
+ if (done) break;
55
+ if (value) await w.write(value);
56
+ }
57
+ })();
58
+ }
59
+ })();
60
+
61
+ function connect() {
62
+ return new WebTransport(url, {
63
+ serverCertificateHashes: [{ algorithm: 'sha-256', value: certHash }],
64
+ });
65
+ }
66
+
67
+ const fmt = (n, d = 1) => n.toLocaleString('en-US', { maximumFractionDigits: d });
68
+
69
+ // Close each session right after it establishes so we do not accumulate live
70
+ // connections (that is a load test, not a handshake-latency measurement).
71
+ async function benchHandshake(n) {
72
+ const t0 = performance.now();
73
+ for (let i = 0; i < n; i++) {
74
+ const wt = connect();
75
+ await wt.ready;
76
+ wt.close();
77
+ await wt.closed.catch(() => {});
78
+ }
79
+ const ms = performance.now() - t0;
80
+ return { rate: n / (ms / 1000), each: ms / n };
81
+ }
82
+
83
+ async function benchThroughput(totalBytes, chunkSize) {
84
+ const wt = connect();
85
+ await wt.ready;
86
+ const stream = await wt.createBidirectionalStream();
87
+ const writer = stream.writable.getWriter();
88
+ const reader = stream.readable.getReader();
89
+ const chunk = new Uint8Array(chunkSize);
90
+ let received = 0;
91
+
92
+ const t0 = performance.now();
93
+ const readP = (async () => {
94
+ for (;;) {
95
+ const { value, done } = await reader.read();
96
+ if (done) break;
97
+ if (value) received += value.byteLength;
98
+ }
99
+ })();
100
+ for (let off = 0; off < totalBytes; off += chunkSize) await writer.write(chunk);
101
+ await writer.close();
102
+ await readP;
103
+ const secs = (performance.now() - t0) / 1000;
104
+ wt.close();
105
+ return {
106
+ received,
107
+ oneWayMBs: totalBytes / 1e6 / secs,
108
+ biMBs: (2 * totalBytes) / 1e6 / secs,
109
+ };
110
+ }
111
+
112
+ async function benchRtt(n, size) {
113
+ const wt = connect();
114
+ await wt.ready;
115
+ const stream = await wt.createBidirectionalStream();
116
+ const writer = stream.writable.getWriter();
117
+ const reader = stream.readable.getReader();
118
+ const msg = new Uint8Array(size);
119
+ const t0 = performance.now();
120
+ for (let i = 0; i < n; i++) {
121
+ await writer.write(msg);
122
+ let got = 0;
123
+ while (got < size) {
124
+ const { value, done } = await reader.read();
125
+ if (done) break;
126
+ if (value) got += value.byteLength;
127
+ }
128
+ }
129
+ const ms = performance.now() - t0;
130
+ wt.close();
131
+ return { rttMs: ms / n, rate: n / (ms / 1000) };
132
+ }
133
+
134
+ async function benchDatagrams(n, size) {
135
+ const wt = connect();
136
+ await wt.ready;
137
+ const writer = wt.datagrams.writable.getWriter();
138
+ const reader = wt.datagrams.readable.getReader();
139
+ const dg = new Uint8Array(size);
140
+ let echoed = 0;
141
+ const readP = (async () => {
142
+ for (;;) {
143
+ const { value, done } = await reader.read();
144
+ if (done) break;
145
+ if (value) echoed++;
146
+ }
147
+ })();
148
+ const t0 = performance.now();
149
+ for (let i = 0; i < n; i++) await writer.write(dg);
150
+ const sendMs = performance.now() - t0;
151
+ await new Promise((r) => setTimeout(r, 400)); // let echoes drain
152
+ wt.close();
153
+ await readP.catch(() => {});
154
+ return { sendRate: n / (sendMs / 1000), echoed, sent: n };
155
+ }
156
+
157
+ console.log(
158
+ `\nrwebtransport benchmark: node ${process.version}, ${process.platform}/${process.arch}`,
159
+ );
160
+ console.log('peer: our WebTransportServer over loopback (127.0.0.1)\n');
161
+
162
+ const hs = await benchHandshake(100);
163
+ console.log(`handshake ${fmt(hs.rate, 0)} sessions/s (${fmt(hs.each, 2)} ms each)`);
164
+
165
+ const tp = await benchThroughput(64 * 1024 * 1024, 256 * 1024);
166
+ console.log(
167
+ `bidi throughput ${fmt(tp.oneWayMBs, 0)} MB/s one-way (${fmt(tp.biMBs, 0)} MB/s bidirectional)`,
168
+ );
169
+
170
+ const rtt = await benchRtt(2000, 16);
171
+ console.log(`rtt (16 B) ${fmt(rtt.rttMs, 3)} ms (${fmt(rtt.rate, 0)} req/s)`);
172
+
173
+ const dg = await benchDatagrams(20000, 1200);
174
+ console.log(
175
+ `datagrams ${fmt(dg.sendRate, 0)} sent/s (${fmt(dg.echoed, 0)}/${fmt(dg.sent, 0)} echoed back)`,
176
+ );
177
+
178
+ console.log('');
179
+ server.close();
180
+ process.exit(0);
@@ -0,0 +1,67 @@
1
+ #!/usr/bin/env node
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ 'use strict';
4
+
5
+ // Builds the rwebtransport native addon with cargo and copies the resulting
6
+ // cdylib into prebuilds/<platform>-<arch>/rwebtransport.node so the loader can
7
+ // find it. Used both for local development and as the compile step in CI and in
8
+ // the install-time fallback.
9
+
10
+ const { execFileSync } = require('node:child_process');
11
+ const fs = require('node:fs');
12
+ const path = require('node:path');
13
+
14
+ const root = path.resolve(__dirname, '..');
15
+ const debug = process.argv.includes('--debug');
16
+ const profile = debug ? 'debug' : 'release';
17
+
18
+ const CRATE = 'rwebtransport-native';
19
+ const LIB_BASENAME = 'rwebtransport_native';
20
+
21
+ function cdylibName() {
22
+ switch (process.platform) {
23
+ case 'win32':
24
+ return `${LIB_BASENAME}.dll`;
25
+ case 'darwin':
26
+ return `lib${LIB_BASENAME}.dylib`;
27
+ default:
28
+ return `lib${LIB_BASENAME}.so`;
29
+ }
30
+ }
31
+
32
+ function run(cmd, args) {
33
+ console.error(`> ${cmd} ${args.join(' ')}`);
34
+ execFileSync(cmd, args, { cwd: root, stdio: 'inherit' });
35
+ }
36
+
37
+ function main() {
38
+ const cargoArgs = ['build', '-p', CRATE];
39
+ if (!debug) cargoArgs.push('--release');
40
+ // Allow an explicit target (used by cross-compiling CI jobs).
41
+ const target = process.env.CARGO_BUILD_TARGET;
42
+ if (target) cargoArgs.push('--target', target);
43
+
44
+ run('cargo', cargoArgs);
45
+
46
+ const targetDir = process.env.CARGO_TARGET_DIR
47
+ ? path.resolve(process.env.CARGO_TARGET_DIR)
48
+ : path.join(root, 'target');
49
+ const artifactDir = target
50
+ ? path.join(targetDir, target, profile)
51
+ : path.join(targetDir, profile);
52
+ const artifact = path.join(artifactDir, cdylibName());
53
+
54
+ if (!fs.existsSync(artifact)) {
55
+ throw new Error(`built artifact not found: ${artifact}`);
56
+ }
57
+
58
+ const arch = process.env.PREBUILD_ARCH || process.arch;
59
+ const plat = process.env.PREBUILD_PLATFORM || process.platform;
60
+ const outDir = path.join(root, 'prebuilds', `${plat}-${arch}`);
61
+ fs.mkdirSync(outDir, { recursive: true });
62
+ const out = path.join(outDir, 'rwebtransport.node');
63
+ fs.copyFileSync(artifact, out);
64
+ console.error(`copied ${artifact} -> ${out}`);
65
+ }
66
+
67
+ main();
@@ -0,0 +1,67 @@
1
+ #!/usr/bin/env node
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ 'use strict';
4
+
5
+ // Ensures a usable native addon after install. If a prebuilt binary already
6
+ // exists for this platform, we do nothing. Otherwise, if a Rust toolchain is
7
+ // available, we compile it now. Failures here are non-fatal: the loader will
8
+ // retry the build lazily on first use and surface a clear error if it can't.
9
+
10
+ const { execFileSync } = require('node:child_process');
11
+ const fs = require('node:fs');
12
+ const path = require('node:path');
13
+
14
+ const root = path.resolve(__dirname, '..');
15
+
16
+ function log(msg) {
17
+ process.stderr.write(`rwebtransport: ${msg}\n`);
18
+ }
19
+
20
+ function hasPrebuilt() {
21
+ const p = path.join(root, 'prebuilds', `${process.platform}-${process.arch}`, 'rwebtransport.node');
22
+ return fs.existsSync(p);
23
+ }
24
+
25
+ function hasCargo() {
26
+ try {
27
+ execFileSync('cargo', ['--version'], { stdio: 'ignore' });
28
+ return true;
29
+ } catch {
30
+ return false;
31
+ }
32
+ }
33
+
34
+ function main() {
35
+ // Skip during the package's own repo install / CI (source-tree development).
36
+ if (process.env.RWEBTRANSPORT_SKIP_POSTINSTALL === '1') return;
37
+
38
+ const major = Number(process.versions.node.split('.')[0]);
39
+ if (major !== 24 && major !== 26) {
40
+ log(`warning: Node ${process.versions.node} is unsupported (need 24 or 26).`);
41
+ return;
42
+ }
43
+
44
+ if (hasPrebuilt()) return;
45
+
46
+ if (!hasCargo()) {
47
+ log('no prebuilt binary for this platform and no Rust toolchain found;');
48
+ log('install Rust (https://rustup.rs) + cmake, or use a supported prebuilt platform.');
49
+ return;
50
+ }
51
+
52
+ log('no prebuilt binary for this platform; compiling the native addon...');
53
+ try {
54
+ execFileSync(process.execPath, [path.join(root, 'scripts', 'build.js')], {
55
+ cwd: root,
56
+ stdio: 'inherit',
57
+ });
58
+ } catch {
59
+ log('build failed during postinstall; will retry on first use.');
60
+ }
61
+ }
62
+
63
+ try {
64
+ main();
65
+ } catch (e) {
66
+ log(`postinstall skipped: ${e && e.message ? e.message : e}`);
67
+ }
@@ -0,0 +1,129 @@
1
+ #!/usr/bin/env bash
2
+ # SPDX-License-Identifier: Apache-2.0
3
+ #
4
+ # scripts/setup-unix.sh
5
+ #
6
+ # Prepares a Linux or macOS machine to build rwebtransport's native addon from
7
+ # source (Cloudflare quiche + BoringSSL via the boring-sys crate). BoringSSL is
8
+ # compiled from source, so you need:
9
+ #
10
+ # * a C/C++ compiler (clang or gcc)
11
+ # * CMake - configures the BoringSSL build
12
+ # * Ninja - the recommended CMake generator (optional; CMake can fall back)
13
+ # * Go - a BoringSSL build-time code generator
14
+ # * Rust - the toolchain that compiles the addon
15
+ # * Node - version 24 or 26 (the only supported lines)
16
+ #
17
+ # NASM is NOT needed off Windows; the system assembler handles BoringSSL here.
18
+ #
19
+ # Usage (from the repository root):
20
+ # bash scripts/setup-unix.sh # install what is missing
21
+ # bash scripts/setup-unix.sh --check # report only, install nothing
22
+ # npm run setup:unix # the same, via npm
23
+ #
24
+ # It uses the native package manager (apt, dnf, pacman, zypper, or Homebrew) and
25
+ # installs Rust via rustup if it is missing. When it finishes, run:
26
+ # npm install
27
+ # npm run build
28
+
29
+ set -euo pipefail
30
+
31
+ info() { printf '\033[36m==>\033[0m %s\n' "$*"; }
32
+ ok() { printf ' \033[32mok\033[0m %s\n' "$*"; }
33
+ warn() { printf ' \033[33m!!\033[0m %s\n' "$*"; }
34
+
35
+ have() { command -v "$1" >/dev/null 2>&1; }
36
+
37
+ CHECK=0
38
+ case "${1:-}" in
39
+ --check | -n) CHECK=1 ;;
40
+ "") ;;
41
+ *) warn "Ignoring unknown argument: $1" ;;
42
+ esac
43
+
44
+ SUDO=""
45
+ if [ "$(id -u)" -ne 0 ] && have sudo; then SUDO="sudo"; fi
46
+
47
+ os="$(uname -s)"
48
+
49
+ install_linux() {
50
+ if have apt-get; then
51
+ $SUDO apt-get update
52
+ $SUDO apt-get install -y cmake ninja-build golang-go build-essential pkg-config curl
53
+ elif have dnf; then
54
+ $SUDO dnf install -y cmake ninja-build golang gcc gcc-c++ pkgconf-pkg-config curl
55
+ elif have pacman; then
56
+ $SUDO pacman -Sy --needed --noconfirm cmake ninja go base-devel pkgconf curl
57
+ elif have zypper; then
58
+ $SUDO zypper install -y cmake ninja go gcc gcc-c++ pkg-config curl
59
+ else
60
+ warn "No supported package manager (apt, dnf, pacman, zypper) was found."
61
+ warn "Install these manually: a C/C++ compiler, cmake, ninja, go."
62
+ return 1
63
+ fi
64
+ }
65
+
66
+ install_mac() {
67
+ if ! xcode-select -p >/dev/null 2>&1 && ! have clang; then
68
+ info "Installing the Xcode Command Line Tools (C/C++ compiler)..."
69
+ xcode-select --install || true
70
+ warn "Finish the Command Line Tools install if a dialog appeared, then re-run."
71
+ fi
72
+ if ! have brew; then
73
+ warn "Homebrew was not found. Install it from https://brew.sh and re-run,"
74
+ warn "or install cmake, ninja, and go by hand."
75
+ return 1
76
+ fi
77
+ brew install cmake ninja go
78
+ }
79
+
80
+ if [ "$CHECK" -eq 1 ]; then
81
+ info "Detected $os (check mode: nothing will be installed)"
82
+ else
83
+ info "Detected $os"
84
+ case "$os" in
85
+ Linux) install_linux || warn "Package install step did not fully complete." ;;
86
+ Darwin) install_mac || warn "Package install step did not fully complete." ;;
87
+ *) warn "Unsupported OS '$os'. This script handles Linux and macOS." ;;
88
+ esac
89
+
90
+ # Rust via rustup, if cargo is not already available.
91
+ if have cargo; then
92
+ ok "Rust already installed ($(command -v cargo))"
93
+ else
94
+ info "Installing Rust via rustup..."
95
+ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
96
+ # shellcheck source=/dev/null
97
+ [ -f "$HOME/.cargo/env" ] && . "$HOME/.cargo/env"
98
+ fi
99
+ fi
100
+
101
+ # Report what the build needs. A missing C compiler, cmake, or go is fatal for
102
+ # the build; ninja is optional (cmake falls back to another generator).
103
+ info "Toolchain status"
104
+ if have cc || have clang || have gcc; then ok "C/C++ compiler present"; else warn "C/C++ compiler MISSING (required)"; fi
105
+ for tool in cmake go cargo; do
106
+ if have "$tool"; then ok "$tool -> $(command -v "$tool")"; else warn "$tool MISSING (required)"; fi
107
+ done
108
+ if have ninja; then ok "ninja -> $(command -v ninja)"; else warn "ninja missing (optional)"; fi
109
+
110
+ info "Node.js (rwebtransport supports 24 and 26 only)"
111
+ if have node; then
112
+ major="$(node -p 'process.versions.node.split(".")[0]' 2>/dev/null || echo '?')"
113
+ if [ "$major" = "24" ] || [ "$major" = "26" ]; then
114
+ ok "Node $(node -v)"
115
+ else
116
+ warn "Node $(node -v) is not supported. Install Node 24 or 26 (nvm, or https://nodejs.org)."
117
+ fi
118
+ else
119
+ warn "Node.js not found. Install Node 24 or 26 (nvm, or https://nodejs.org)."
120
+ fi
121
+
122
+ echo ""
123
+ if [ "$CHECK" -eq 1 ]; then
124
+ info "Check complete. Re-run without --check to install what is missing."
125
+ else
126
+ info "Setup finished. If Rust was just installed, open a new shell (or run '. \"\$HOME/.cargo/env\"'), then:"
127
+ echo " npm install"
128
+ echo " npm run build"
129
+ fi