@webtransport-bun/webtransport 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.
@@ -0,0 +1,72 @@
1
+ /**
2
+ * Node stream wrappers for WebTransport QUIC streams.
3
+ *
4
+ * All bidi streams are exposed as Duplex.
5
+ * Outgoing uni streams are Writable, incoming uni streams are Readable.
6
+ *
7
+ * Stream control extensions (reset / stopSending) are attached via symbols
8
+ * to avoid name collisions with Node stream methods.
9
+ */
10
+ import { Duplex, Readable, Writable } from "node:stream";
11
+ import type { DuplexOptions, ReadableOptions, WritableOptions } from "node:stream";
12
+ /**
13
+ * Symbol to call stream reset (abort receiving). Use on BidiStream, SendStream, RecvStream.
14
+ * @example `(stream as Resettable)[WT_RESET](code)`
15
+ */
16
+ export declare const WT_RESET: unique symbol;
17
+ /**
18
+ * Symbol to send stopSending (abort sending). Use on BidiStream, RecvStream.
19
+ * @example `(stream as StopSendable)[WT_STOP_SENDING](code)`
20
+ */
21
+ export declare const WT_STOP_SENDING: unique symbol;
22
+ /** Stream that supports reset via WT_RESET. */
23
+ export type Resettable = {
24
+ [WT_RESET](code?: number): void;
25
+ };
26
+ /** Stream that supports stopSending via WT_STOP_SENDING. */
27
+ export type StopSendable = {
28
+ [WT_STOP_SENDING](code?: number): void;
29
+ };
30
+ type StreamHandleId = number;
31
+ export interface BidiStreamOptions extends DuplexOptions {
32
+ handleId: StreamHandleId;
33
+ nativeHandle?: any;
34
+ }
35
+ export declare class BidiStream extends Duplex implements Resettable, StopSendable {
36
+ #private;
37
+ private readonly _handleId;
38
+ constructor(opts: BidiStreamOptions);
39
+ _read(_size: number): void;
40
+ _write(chunk: Buffer, _encoding: BufferEncoding, callback: (error?: Error | null) => void): void;
41
+ _final(callback: (error?: Error | null) => void): void;
42
+ _destroy(error: Error | null, callback: (error?: Error | null) => void): void;
43
+ [WT_RESET](code?: number): void;
44
+ [WT_STOP_SENDING](code?: number): void;
45
+ }
46
+ export interface SendStreamOptions extends WritableOptions {
47
+ handleId: StreamHandleId;
48
+ nativeHandle?: any;
49
+ }
50
+ export declare class SendStream extends Writable implements Resettable {
51
+ #private;
52
+ private readonly _handleId;
53
+ constructor(opts: SendStreamOptions);
54
+ _write(chunk: Buffer, _encoding: BufferEncoding, callback: (error?: Error | null) => void): void;
55
+ _final(callback: (error?: Error | null) => void): void;
56
+ _destroy(error: Error | null, callback: (error?: Error | null) => void): void;
57
+ [WT_RESET](code?: number): void;
58
+ }
59
+ export interface RecvStreamOptions extends ReadableOptions {
60
+ handleId: StreamHandleId;
61
+ nativeHandle?: any;
62
+ }
63
+ export declare class RecvStream extends Readable implements StopSendable {
64
+ #private;
65
+ private readonly _handleId;
66
+ constructor(opts: RecvStreamOptions);
67
+ _read(_size: number): void;
68
+ _destroy(error: Error | null, callback: (error?: Error | null) => void): void;
69
+ [WT_STOP_SENDING](code?: number): void;
70
+ }
71
+ export {};
72
+ //# sourceMappingURL=streams.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"streams.d.ts","sourceRoot":"","sources":["../src/streams.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACzD,OAAO,KAAK,EAAE,aAAa,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAEnF;;;GAGG;AACH,eAAO,MAAM,QAAQ,EAAE,OAAO,MAA2B,CAAC;AAC1D;;;GAGG;AACH,eAAO,MAAM,eAAe,EAAE,OAAO,MAAkC,CAAC;AAExE,+CAA+C;AAC/C,MAAM,MAAM,UAAU,GAAG;IAAE,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;CAAE,CAAC;AAC7D,4DAA4D;AAC5D,MAAM,MAAM,YAAY,GAAG;IAAE,CAAC,eAAe,CAAC,CAAC,IAAI,CAAC,EAAE,MAAM,GAAG,IA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AI,GACzC,IAAI;IASE,MAAM,CAAC,QAAQ,EAAE,CAAC,KAAK,CAAC,EAAE,KAAK,GAAG,IAAI,KAAK,IAAI,GAAG,IAAI;IAKtD,QAAQ,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI,EAAE,QAAQ,EAAE,CAAC,KAAK,CAAC,EAAE,KAAK,GAAG,IAAI,KAAK,IAAI,GAAG,IAAI;IAWtF,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI;CAIlC;AAMD,MAAM,WAAW,iBAAkB,SAAQ,eAAe;IACtD,QAAQ,EAAE,cAAc,CAAC;IACzB,YAAY,CAAC,EAAE,GAAG,CAAC;CACtB;AAED,qBAAa,UAAW,SAAQ,QAAS,YAAW,YAAY;;IAC5D,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAiB;gBAI/B,IAAI,EAAE,iBAAiB;IAU1B,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAY1B,QAAQ,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI,EAAE,QAAQ,EAAE,CAAC,KAAK,CAAC,EAAE,KAAK,GAAG,IAAI,KAAK,IAAI,GAAG,IAAI;IAWtF,CAAC,eAAe,CAAC,CAAC,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI;CAGzC"}
@@ -0,0 +1,165 @@
1
+ /**
2
+ * Node stream wrappers for WebTransport QUIC streams.
3
+ *
4
+ * All bidi streams are exposed as Duplex.
5
+ * Outgoing uni streams are Writable, incoming uni streams are Readable.
6
+ *
7
+ * Stream control extensions (reset / stopSending) are attached via symbols
8
+ * to avoid name collisions with Node stream methods.
9
+ */
10
+ import { Duplex, Readable, Writable } from "node:stream";
11
+ /**
12
+ * Symbol to call stream reset (abort receiving). Use on BidiStream, SendStream, RecvStream.
13
+ * @example `(stream as Resettable)[WT_RESET](code)`
14
+ */
15
+ export const WT_RESET = Symbol("WT_RESET");
16
+ /**
17
+ * Symbol to send stopSending (abort sending). Use on BidiStream, RecvStream.
18
+ * @example `(stream as StopSendable)[WT_STOP_SENDING](code)`
19
+ */
20
+ export const WT_STOP_SENDING = Symbol("WT_STOP_SENDING");
21
+ export class BidiStream extends Duplex {
22
+ _handleId;
23
+ #nativeHandle;
24
+ #destroyed = false;
25
+ constructor(opts) {
26
+ super({
27
+ ...opts,
28
+ allowHalfOpen: true,
29
+ autoDestroy: false,
30
+ readableHighWaterMark: opts.readableHighWaterMark ?? 256 * 1024,
31
+ writableHighWaterMark: opts.writableHighWaterMark ?? 256 * 1024,
32
+ });
33
+ this._handleId = opts.handleId;
34
+ this.#nativeHandle = opts.nativeHandle;
35
+ }
36
+ // -- Node stream overrides -----------------------
37
+ _read(_size) {
38
+ const h = this.#nativeHandle;
39
+ if (!h || this.#destroyed) {
40
+ this.push(null);
41
+ return;
42
+ }
43
+ h.read()
44
+ .then((buf) => {
45
+ if (buf && !this.#destroyed)
46
+ this.push(buf);
47
+ else
48
+ this.push(null);
49
+ })
50
+ .catch((err) => this.destroy(err));
51
+ }
52
+ _write(chunk, _encoding, callback) {
53
+ const h = this.#nativeHandle;
54
+ if (!h || this.#destroyed) {
55
+ callback(new Error("E_STREAM_RESET"));
56
+ return;
57
+ }
58
+ h.write(chunk).then(() => callback()).catch(callback);
59
+ }
60
+ _final(callback) {
61
+ this.#nativeHandle?.finish?.();
62
+ callback();
63
+ }
64
+ _destroy(error, callback) {
65
+ if (!this.#destroyed) {
66
+ this.#destroyed = true;
67
+ try {
68
+ this.#nativeHandle?.reset?.(0);
69
+ }
70
+ catch { /* already closed */ }
71
+ this.#nativeHandle = null;
72
+ }
73
+ callback(error);
74
+ }
75
+ // -- Stream control extensions -------------------------------------------
76
+ [WT_RESET](code) {
77
+ this.#nativeHandle?.reset(code ?? 0);
78
+ this.destroy();
79
+ }
80
+ [WT_STOP_SENDING](code) {
81
+ this.#nativeHandle?.stopSending(code ?? 0);
82
+ }
83
+ }
84
+ export class SendStream extends Writable {
85
+ _handleId;
86
+ #nativeHandle;
87
+ #destroyed = false;
88
+ constructor(opts) {
89
+ super({
90
+ ...opts,
91
+ autoDestroy: true,
92
+ highWaterMark: opts.highWaterMark ?? 256 * 1024,
93
+ });
94
+ this._handleId = opts.handleId;
95
+ this.#nativeHandle = opts.nativeHandle;
96
+ }
97
+ _write(chunk, _encoding, callback) {
98
+ const h = this.#nativeHandle;
99
+ if (!h || this.#destroyed) {
100
+ callback(new Error("E_STREAM_RESET"));
101
+ return;
102
+ }
103
+ h.write(chunk).then(() => callback()).catch(callback);
104
+ }
105
+ _final(callback) {
106
+ this.#nativeHandle?.finish?.();
107
+ callback();
108
+ }
109
+ _destroy(error, callback) {
110
+ if (!this.#destroyed) {
111
+ this.#destroyed = true;
112
+ try {
113
+ this.#nativeHandle?.reset?.(0);
114
+ }
115
+ catch { /* already closed */ }
116
+ this.#nativeHandle = null;
117
+ }
118
+ callback(error);
119
+ }
120
+ [WT_RESET](code) {
121
+ this.#nativeHandle?.reset(code ?? 0);
122
+ this.destroy();
123
+ }
124
+ }
125
+ export class RecvStream extends Readable {
126
+ _handleId;
127
+ #nativeHandle;
128
+ #destroyed = false;
129
+ constructor(opts) {
130
+ super({
131
+ ...opts,
132
+ autoDestroy: true,
133
+ highWaterMark: opts.highWaterMark ?? 256 * 1024,
134
+ });
135
+ this._handleId = opts.handleId;
136
+ this.#nativeHandle = opts.nativeHandle;
137
+ }
138
+ _read(_size) {
139
+ const h = this.#nativeHandle;
140
+ if (!h || this.#destroyed) {
141
+ this.push(null);
142
+ return;
143
+ }
144
+ h.read().then((buf) => {
145
+ if (buf && !this.#destroyed)
146
+ this.push(buf);
147
+ else
148
+ this.push(null);
149
+ }).catch((err) => this.destroy(err));
150
+ }
151
+ _destroy(error, callback) {
152
+ if (!this.#destroyed) {
153
+ this.#destroyed = true;
154
+ try {
155
+ this.#nativeHandle?.stopSending?.(0);
156
+ }
157
+ catch { /* already closed */ }
158
+ this.#nativeHandle = null;
159
+ }
160
+ callback(error);
161
+ }
162
+ [WT_STOP_SENDING](code) {
163
+ this.#nativeHandle?.stopSending(code ?? 0);
164
+ }
165
+ }
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@webtransport-bun/webtransport",
3
+ "version": "0.1.0",
4
+ "description": "Production-focused WebTransport for Bun with datagrams and streams via a Rust napi-rs addon",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/vmeansdev/webtransport-bun.git"
9
+ },
10
+ "homepage": "https://github.com/vmeansdev/webtransport-bun#readme",
11
+ "bugs": {
12
+ "url": "https://github.com/vmeansdev/webtransport-bun/issues"
13
+ },
14
+ "keywords": ["bun", "webtransport", "quic", "napi-rs", "realtime"],
15
+ "type": "module",
16
+ "main": "./dist/index.js",
17
+ "types": "./dist/index.d.ts",
18
+ "exports": {
19
+ ".": {
20
+ "import": "./dist/index.js",
21
+ "types": "./dist/index.d.ts"
22
+ }
23
+ },
24
+ "files": [
25
+ "dist/**/*",
26
+ "prebuilds/**/*",
27
+ "README.md",
28
+ "LICENSE"
29
+ ],
30
+ "engines": {
31
+ "bun": ">=1.3.9"
32
+ },
33
+ "os": ["darwin", "linux"],
34
+ "cpu": ["arm64", "x64"],
35
+ "publishConfig": {
36
+ "access": "public"
37
+ },
38
+ "scripts": {
39
+ "clean": "rm -rf dist",
40
+ "build": "tsc -p tsconfig.build.json",
41
+ "prebuild:native": "cd ../.. && bun run build:native && mkdir -p packages/webtransport/prebuilds && cp crates/native/webtransport-native.*.node packages/webtransport/prebuilds/ 2>/dev/null || true",
42
+ "prebuild:checksums": "cd prebuilds 2>/dev/null && shasum -a 256 *.node > SHA256SUMS 2>/dev/null || true",
43
+ "pack:check": "npm pack --dry-run",
44
+ "prepublishOnly": "npm run clean && npm run build && npm run prebuild:native && npm run prebuild:checksums && npm run pack:check",
45
+ "test": "bun test",
46
+ "typecheck": "tsc --noEmit"
47
+ }
48
+ }
@@ -0,0 +1 @@
1
+ f42ef24018b0b6a19ceebc4cfc4989129c3bf6b651d03176ecb0809e5a9f90ea webtransport-native.darwin-arm64.node