@statewalker/webrun-streams-peerjs 0.1.1
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/LICENSE +21 -0
- package/README.md +16 -0
- package/dist/byte-channel.d.ts +9 -0
- package/dist/byte-channel.d.ts.map +1 -0
- package/dist/connect-serve.d.ts +13 -0
- package/dist/connect-serve.d.ts.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +127 -0
- package/package.json +51 -0
- package/src/byte-channel.ts +112 -0
- package/src/connect-serve.ts +49 -0
- package/src/index.ts +7 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022-2026 statewalker
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# @statewalker/webrun-streams-peerjs
|
|
2
|
+
|
|
3
|
+
PeerJS DataConnection-backed `Connect` / `Serve` adapter. Wraps a `DataConnection` (which itself wraps a WebRTC DataChannel) into a byte channel; `emulateMux` provides multi-stream. Requires `serialization: "raw"` on the connection.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { connect, serve } from "@statewalker/webrun-streams-peerjs";
|
|
7
|
+
|
|
8
|
+
const { call } = await connect({ conn }); // open DataConnection
|
|
9
|
+
const stop = await serve({ peer }, handler); // listens for inbound conns
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
Conformance is browser-gated; PeerJS's WebRTC handshake hangs under Node's wrtc polyfill. Run with `pnpm test:browser`.
|
|
13
|
+
|
|
14
|
+
## License
|
|
15
|
+
|
|
16
|
+
MIT
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { ByteChannel } from "@statewalker/webrun-streams";
|
|
2
|
+
import type { DataConnection } from "peerjs";
|
|
3
|
+
/**
|
|
4
|
+
* Wrap an open PeerJS `DataConnection` into a `ByteChannel`. The connection
|
|
5
|
+
* MUST be constructed with `{ serialization: "raw" }` so payloads arrive as
|
|
6
|
+
* `Uint8Array` rather than PeerJS's own JSON envelopes.
|
|
7
|
+
*/
|
|
8
|
+
export declare function byteChannelFromPeerJs(conn: DataConnection): ByteChannel;
|
|
9
|
+
//# sourceMappingURL=byte-channel.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"byte-channel.d.ts","sourceRoot":"","sources":["../src/byte-channel.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAC/D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,QAAQ,CAAC;AAE7C;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,cAAc,GAAG,WAAW,CAuGvE"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type Connect, type Serve } from "@statewalker/webrun-streams";
|
|
2
|
+
import type { DataConnection, Peer } from "peerjs";
|
|
3
|
+
export interface ConnectPeerJsParams {
|
|
4
|
+
/** Already-open `DataConnection` with `serialization: "raw"`. */
|
|
5
|
+
conn: DataConnection;
|
|
6
|
+
}
|
|
7
|
+
export interface ServePeerJsParams {
|
|
8
|
+
/** Connected `Peer`. The adapter listens for inbound `DataConnection`s. */
|
|
9
|
+
peer: Peer;
|
|
10
|
+
}
|
|
11
|
+
export declare const connect: Connect<ConnectPeerJsParams>;
|
|
12
|
+
export declare const serve: Serve<ServePeerJsParams>;
|
|
13
|
+
//# sourceMappingURL=connect-serve.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connect-serve.d.ts","sourceRoot":"","sources":["../src/connect-serve.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,OAAO,EAA2B,KAAK,KAAK,EAAE,MAAM,6BAA6B,CAAC;AAChG,OAAO,KAAK,EAAE,cAAc,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAGnD,MAAM,WAAW,mBAAmB;IAClC,iEAAiE;IACjE,IAAI,EAAE,cAAc,CAAC;CACtB;AAED,MAAM,WAAW,iBAAiB;IAChC,2EAA2E;IAC3E,IAAI,EAAE,IAAI,CAAC;CACZ;AAED,eAAO,MAAM,OAAO,EAAE,OAAO,CAAC,mBAAmB,CAShD,CAAC;AAEF,eAAO,MAAM,KAAK,EAAE,KAAK,CAAC,iBAAiB,CAuB1C,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAC;AAC1D,OAAO,EACL,KAAK,mBAAmB,EACxB,OAAO,EACP,KAAK,iBAAiB,EACtB,KAAK,GACN,MAAM,oBAAoB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { emulateMux } from "@statewalker/webrun-streams";
|
|
2
|
+
//#region src/byte-channel.ts
|
|
3
|
+
/**
|
|
4
|
+
* Wrap an open PeerJS `DataConnection` into a `ByteChannel`. The connection
|
|
5
|
+
* MUST be constructed with `{ serialization: "raw" }` so payloads arrive as
|
|
6
|
+
* `Uint8Array` rather than PeerJS's own JSON envelopes.
|
|
7
|
+
*/
|
|
8
|
+
function byteChannelFromPeerJs(conn) {
|
|
9
|
+
const serialization = conn.serialization;
|
|
10
|
+
if (serialization !== "raw") throw new TypeError(`byteChannelFromPeerJs: DataConnection serialization is '${serialization}', expected 'raw'`);
|
|
11
|
+
let closedResolve;
|
|
12
|
+
const closed = new Promise((r) => {
|
|
13
|
+
closedResolve = r;
|
|
14
|
+
});
|
|
15
|
+
let isClosed = false;
|
|
16
|
+
const queue = [];
|
|
17
|
+
let pending = null;
|
|
18
|
+
const deliver = (bytes) => {
|
|
19
|
+
if (isClosed) return;
|
|
20
|
+
if (pending) {
|
|
21
|
+
const r = pending;
|
|
22
|
+
pending = null;
|
|
23
|
+
r({
|
|
24
|
+
value: bytes,
|
|
25
|
+
done: false
|
|
26
|
+
});
|
|
27
|
+
} else queue.push(bytes);
|
|
28
|
+
};
|
|
29
|
+
const onData = (data) => {
|
|
30
|
+
if (data instanceof Uint8Array) {
|
|
31
|
+
deliver(new Uint8Array(data));
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
if (data instanceof ArrayBuffer) {
|
|
35
|
+
deliver(new Uint8Array(data));
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
if (ArrayBuffer.isView(data)) {
|
|
39
|
+
const view = data;
|
|
40
|
+
deliver(new Uint8Array(view.buffer.slice(view.byteOffset, view.byteOffset + view.byteLength)));
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
if (typeof Blob !== "undefined" && data instanceof Blob) data.arrayBuffer().then((buf) => deliver(new Uint8Array(buf)));
|
|
44
|
+
};
|
|
45
|
+
const onClose = () => {
|
|
46
|
+
if (isClosed) return;
|
|
47
|
+
isClosed = true;
|
|
48
|
+
conn.off("data", onData);
|
|
49
|
+
conn.off("close", onClose);
|
|
50
|
+
if (pending) {
|
|
51
|
+
const r = pending;
|
|
52
|
+
pending = null;
|
|
53
|
+
r({
|
|
54
|
+
value: void 0,
|
|
55
|
+
done: true
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
closedResolve();
|
|
59
|
+
};
|
|
60
|
+
conn.on("data", onData);
|
|
61
|
+
conn.on("close", onClose);
|
|
62
|
+
return {
|
|
63
|
+
send(bytes) {
|
|
64
|
+
if (isClosed) return;
|
|
65
|
+
if (!conn.open) return;
|
|
66
|
+
conn.send(bytes);
|
|
67
|
+
},
|
|
68
|
+
recv: { [Symbol.asyncIterator]() {
|
|
69
|
+
return { next() {
|
|
70
|
+
if (queue.length > 0) return Promise.resolve({
|
|
71
|
+
value: queue.shift(),
|
|
72
|
+
done: false
|
|
73
|
+
});
|
|
74
|
+
if (isClosed) return Promise.resolve({
|
|
75
|
+
value: void 0,
|
|
76
|
+
done: true
|
|
77
|
+
});
|
|
78
|
+
return new Promise((resolve) => {
|
|
79
|
+
pending = resolve;
|
|
80
|
+
});
|
|
81
|
+
} };
|
|
82
|
+
} },
|
|
83
|
+
closed,
|
|
84
|
+
close() {
|
|
85
|
+
try {
|
|
86
|
+
conn.close();
|
|
87
|
+
} catch {}
|
|
88
|
+
onClose();
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
//#endregion
|
|
93
|
+
//#region src/connect-serve.ts
|
|
94
|
+
const connect = async ({ conn }) => {
|
|
95
|
+
const channel = byteChannelFromPeerJs(conn);
|
|
96
|
+
const mux = emulateMux(channel, { side: "initiator" });
|
|
97
|
+
return {
|
|
98
|
+
call: mux.call,
|
|
99
|
+
async close() {
|
|
100
|
+
await mux.close();
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
};
|
|
104
|
+
const serve = async ({ peer }, handler) => {
|
|
105
|
+
const muxes = [];
|
|
106
|
+
const onConnection = (conn) => {
|
|
107
|
+
const ready = () => {
|
|
108
|
+
const channel = byteChannelFromPeerJs(conn);
|
|
109
|
+
const mux = emulateMux(channel, { side: "responder" });
|
|
110
|
+
mux.serve(handler);
|
|
111
|
+
muxes.push(mux);
|
|
112
|
+
channel.closed.then(() => mux.close());
|
|
113
|
+
};
|
|
114
|
+
if (conn.open) ready();
|
|
115
|
+
else conn.on("open", ready);
|
|
116
|
+
};
|
|
117
|
+
peer.on("connection", onConnection);
|
|
118
|
+
let torn = false;
|
|
119
|
+
return async () => {
|
|
120
|
+
if (torn) return;
|
|
121
|
+
torn = true;
|
|
122
|
+
peer.off("connection", onConnection);
|
|
123
|
+
await Promise.all(muxes.map((m) => m.close()));
|
|
124
|
+
};
|
|
125
|
+
};
|
|
126
|
+
//#endregion
|
|
127
|
+
export { byteChannelFromPeerJs, connect, serve };
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@statewalker/webrun-streams-peerjs",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"private": false,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"description": "PeerJS DataConnection-backed Connect/Serve adapter in the webrun-streams-* family",
|
|
7
|
+
"homepage": "https://github.com/statewalker/webrun-wire",
|
|
8
|
+
"author": {
|
|
9
|
+
"name": "Mikhail Kotelnikov",
|
|
10
|
+
"email": "mikhail.kotelnikov@gmail.com"
|
|
11
|
+
},
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git@github.com:statewalker/webrun-wire.git"
|
|
16
|
+
},
|
|
17
|
+
"exports": {
|
|
18
|
+
".": "./src/index.ts"
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist",
|
|
22
|
+
"src"
|
|
23
|
+
],
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@statewalker/webrun-streams": "0.1.1"
|
|
26
|
+
},
|
|
27
|
+
"peerDependencies": {
|
|
28
|
+
"peerjs": "^1.5.5"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@roamhq/wrtc": "^0.8.0",
|
|
32
|
+
"@types/node": "^26.2.0",
|
|
33
|
+
"peer": "^1.0.2",
|
|
34
|
+
"peerjs": "^1.5.5",
|
|
35
|
+
"rimraf": "^6.1.3",
|
|
36
|
+
"rolldown": "^1.2.4",
|
|
37
|
+
"typescript": "^7.0.2",
|
|
38
|
+
"vitest": "^4.1.10",
|
|
39
|
+
"@statewalker/webrun-streams-conformance": "0.1.1"
|
|
40
|
+
},
|
|
41
|
+
"sideEffects": false,
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"build": "rimraf dist && rolldown -c && tsc --emitDeclarationOnly --declaration",
|
|
47
|
+
"test": "vitest run",
|
|
48
|
+
"test:browser": "vitest run --config vitest.browser.config.ts",
|
|
49
|
+
"lint": "biome check src tests"
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import type { ByteChannel } from "@statewalker/webrun-streams";
|
|
2
|
+
import type { DataConnection } from "peerjs";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Wrap an open PeerJS `DataConnection` into a `ByteChannel`. The connection
|
|
6
|
+
* MUST be constructed with `{ serialization: "raw" }` so payloads arrive as
|
|
7
|
+
* `Uint8Array` rather than PeerJS's own JSON envelopes.
|
|
8
|
+
*/
|
|
9
|
+
export function byteChannelFromPeerJs(conn: DataConnection): ByteChannel {
|
|
10
|
+
const serialization = (conn as unknown as { serialization?: string }).serialization;
|
|
11
|
+
if (serialization !== "raw") {
|
|
12
|
+
throw new TypeError(
|
|
13
|
+
`byteChannelFromPeerJs: DataConnection serialization is '${serialization}', expected 'raw'`,
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
let closedResolve!: () => void;
|
|
18
|
+
const closed = new Promise<void>((r) => {
|
|
19
|
+
closedResolve = r;
|
|
20
|
+
});
|
|
21
|
+
let isClosed = false;
|
|
22
|
+
|
|
23
|
+
const queue: Uint8Array[] = [];
|
|
24
|
+
let pending: ((value: IteratorResult<Uint8Array>) => void) | null = null;
|
|
25
|
+
|
|
26
|
+
const deliver = (bytes: Uint8Array): void => {
|
|
27
|
+
if (isClosed) return;
|
|
28
|
+
if (pending) {
|
|
29
|
+
const r = pending;
|
|
30
|
+
pending = null;
|
|
31
|
+
r({ value: bytes, done: false });
|
|
32
|
+
} else {
|
|
33
|
+
queue.push(bytes);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const onData = (data: unknown): void => {
|
|
38
|
+
if (data instanceof Uint8Array) {
|
|
39
|
+
deliver(new Uint8Array(data));
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (data instanceof ArrayBuffer) {
|
|
43
|
+
deliver(new Uint8Array(data));
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
if (ArrayBuffer.isView(data)) {
|
|
47
|
+
const view = data as ArrayBufferView;
|
|
48
|
+
deliver(
|
|
49
|
+
new Uint8Array(view.buffer.slice(view.byteOffset, view.byteOffset + view.byteLength)),
|
|
50
|
+
);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
if (typeof Blob !== "undefined" && data instanceof Blob) {
|
|
54
|
+
void data.arrayBuffer().then((buf) => deliver(new Uint8Array(buf)));
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const onClose = (): void => {
|
|
59
|
+
if (isClosed) return;
|
|
60
|
+
isClosed = true;
|
|
61
|
+
conn.off("data", onData);
|
|
62
|
+
conn.off("close", onClose);
|
|
63
|
+
if (pending) {
|
|
64
|
+
const r = pending;
|
|
65
|
+
pending = null;
|
|
66
|
+
r({ value: undefined, done: true } as IteratorResult<Uint8Array>);
|
|
67
|
+
}
|
|
68
|
+
closedResolve();
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
conn.on("data", onData);
|
|
72
|
+
conn.on("close", onClose);
|
|
73
|
+
|
|
74
|
+
const recv: AsyncIterable<Uint8Array> = {
|
|
75
|
+
[Symbol.asyncIterator]() {
|
|
76
|
+
return {
|
|
77
|
+
next(): Promise<IteratorResult<Uint8Array>> {
|
|
78
|
+
if (queue.length > 0) {
|
|
79
|
+
return Promise.resolve({ value: queue.shift() as Uint8Array, done: false });
|
|
80
|
+
}
|
|
81
|
+
if (isClosed) {
|
|
82
|
+
return Promise.resolve({
|
|
83
|
+
value: undefined,
|
|
84
|
+
done: true,
|
|
85
|
+
} as IteratorResult<Uint8Array>);
|
|
86
|
+
}
|
|
87
|
+
return new Promise<IteratorResult<Uint8Array>>((resolve) => {
|
|
88
|
+
pending = resolve;
|
|
89
|
+
});
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
send(bytes) {
|
|
97
|
+
if (isClosed) return;
|
|
98
|
+
if (!conn.open) return;
|
|
99
|
+
conn.send(bytes);
|
|
100
|
+
},
|
|
101
|
+
recv,
|
|
102
|
+
closed,
|
|
103
|
+
close() {
|
|
104
|
+
try {
|
|
105
|
+
conn.close();
|
|
106
|
+
} catch {
|
|
107
|
+
/* ignore */
|
|
108
|
+
}
|
|
109
|
+
onClose();
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { type Connect, type Duplex, emulateMux, type Serve } from "@statewalker/webrun-streams";
|
|
2
|
+
import type { DataConnection, Peer } from "peerjs";
|
|
3
|
+
import { byteChannelFromPeerJs } from "./byte-channel.js";
|
|
4
|
+
|
|
5
|
+
export interface ConnectPeerJsParams {
|
|
6
|
+
/** Already-open `DataConnection` with `serialization: "raw"`. */
|
|
7
|
+
conn: DataConnection;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface ServePeerJsParams {
|
|
11
|
+
/** Connected `Peer`. The adapter listens for inbound `DataConnection`s. */
|
|
12
|
+
peer: Peer;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const connect: Connect<ConnectPeerJsParams> = async ({ conn }) => {
|
|
16
|
+
const channel = byteChannelFromPeerJs(conn);
|
|
17
|
+
const mux = emulateMux(channel, { side: "initiator" });
|
|
18
|
+
return {
|
|
19
|
+
call: mux.call,
|
|
20
|
+
async close() {
|
|
21
|
+
await mux.close();
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export const serve: Serve<ServePeerJsParams> = async ({ peer }, handler: Duplex) => {
|
|
27
|
+
const muxes: Array<{ close: () => Promise<void> }> = [];
|
|
28
|
+
|
|
29
|
+
const onConnection = (conn: DataConnection): void => {
|
|
30
|
+
const ready = (): void => {
|
|
31
|
+
const channel = byteChannelFromPeerJs(conn);
|
|
32
|
+
const mux = emulateMux(channel, { side: "responder" });
|
|
33
|
+
mux.serve(handler);
|
|
34
|
+
muxes.push(mux);
|
|
35
|
+
void channel.closed.then(() => mux.close());
|
|
36
|
+
};
|
|
37
|
+
if (conn.open) ready();
|
|
38
|
+
else conn.on("open", ready);
|
|
39
|
+
};
|
|
40
|
+
peer.on("connection", onConnection);
|
|
41
|
+
|
|
42
|
+
let torn = false;
|
|
43
|
+
return async () => {
|
|
44
|
+
if (torn) return;
|
|
45
|
+
torn = true;
|
|
46
|
+
peer.off("connection", onConnection);
|
|
47
|
+
await Promise.all(muxes.map((m) => m.close()));
|
|
48
|
+
};
|
|
49
|
+
};
|