crossws 0.0.1 → 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/README.md +154 -63
- package/adapters/bun.d.ts +2 -0
- package/adapters/cloudflare.d.ts +2 -0
- package/adapters/deno.d.ts +2 -0
- package/adapters/node.d.ts +2 -0
- package/dist/adapters/bun.cjs +44 -29
- package/dist/adapters/bun.d.cts +17 -36
- package/dist/adapters/bun.d.mts +17 -36
- package/dist/adapters/bun.d.ts +17 -36
- package/dist/adapters/bun.mjs +44 -26
- package/dist/adapters/cloudflare.cjs +52 -0
- package/dist/adapters/cloudflare.d.cts +12 -0
- package/dist/adapters/cloudflare.d.mts +12 -0
- package/dist/adapters/cloudflare.d.ts +12 -0
- package/dist/adapters/cloudflare.mjs +50 -0
- package/dist/adapters/deno.cjs +20 -29
- package/dist/adapters/deno.d.cts +2 -10
- package/dist/adapters/deno.d.mts +2 -10
- package/dist/adapters/deno.d.ts +2 -10
- package/dist/adapters/deno.mjs +20 -26
- package/dist/adapters/node.cjs +57 -3891
- package/dist/adapters/node.d.cts +36 -37
- package/dist/adapters/node.d.mts +36 -37
- package/dist/adapters/node.d.ts +36 -37
- package/dist/adapters/node.mjs +48 -3872
- package/dist/index.cjs +5 -5
- package/dist/index.d.cts +40 -10
- package/dist/index.d.mts +40 -10
- package/dist/index.d.ts +40 -10
- package/dist/index.mjs +4 -4
- package/dist/shared/{crossws.002417e5.cjs → crossws.21e14e0d.cjs} +7 -4
- package/dist/shared/crossws.2ed26345.cjs +3931 -0
- package/dist/shared/{crossws.08866fa6.mjs → crossws.9536f626.mjs} +7 -4
- package/dist/shared/crossws.a5db571c.mjs +3910 -0
- package/dist/websocket/index.cjs +5 -0
- package/dist/websocket/index.d.cts +10 -0
- package/dist/websocket/index.d.mts +10 -0
- package/dist/websocket/index.d.ts +10 -0
- package/dist/websocket/index.mjs +3 -0
- package/dist/websocket/node.cjs +17 -0
- package/dist/websocket/node.d.cts +10 -0
- package/dist/websocket/node.d.mts +10 -0
- package/dist/websocket/node.d.ts +10 -0
- package/dist/websocket/node.mjs +15 -0
- package/package.json +43 -16
- package/websocket.d.ts +2 -0
package/README.md
CHANGED
|
@@ -1,23 +1,22 @@
|
|
|
1
1
|
# ⛨ CrossWS
|
|
2
2
|
|
|
3
3
|
[![npm version][npm-version-src]][npm-version-href]
|
|
4
|
-
[![
|
|
4
|
+
[![npm downloads][npm-downloads-src]][npm-downloads-href]
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
👉 Elegant, typed, and simple interface to implement platform-agnostic WebSocket servers
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
🧩 Seamlessly integrates with, [Node.js](https://nodejs.org/en), [Bun](https://bun.sh/), [Deno](https://deno.com/) and [Cloudflare Workers](https://workers.cloudflare.com/)!
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
🚀 High-performance server hooks, avoiding heavy per-connection events API ([why](https://bun.sh/docs/api/websockets#lcYFjkFYJC-summary))
|
|
11
|
+
|
|
12
|
+
📦 No external dependencies, includes [ws](https://github.com/websockets/ws) for Node.js support
|
|
12
13
|
|
|
13
|
-
|
|
14
|
+
💡 Extremely lightweight and tree-shakable packaging with ESM and CJS support
|
|
14
15
|
|
|
15
|
-
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
- Super lightweight tree-shakable packaging
|
|
20
|
-
- Developer-Friendly logging
|
|
16
|
+
🔍 Developer-friendly object logging
|
|
17
|
+
|
|
18
|
+
> [!WARNING]
|
|
19
|
+
> This project and API are under development.
|
|
21
20
|
|
|
22
21
|
## Install
|
|
23
22
|
|
|
@@ -35,9 +34,91 @@ pnpm install crossws
|
|
|
35
34
|
bun install crossws
|
|
36
35
|
```
|
|
37
36
|
|
|
38
|
-
##
|
|
37
|
+
## Unified Hooks
|
|
38
|
+
|
|
39
|
+
CrossWS provides a cross-platform API to define WebSocket servers. An implementation with these hooks works across runtimes without needing you to go into details of any of them (while you always have the power to control low-level hooks). You can only define the life-cycle hooks that you only need and only those will be called run runtime.
|
|
40
|
+
|
|
41
|
+
> [!NOTE]
|
|
42
|
+
> For type support and IDE auto-completion, you can use `defineWebSocketHooks` utility or `WebSocketHooks` type export from the main.
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
import { defineWebSocketHooks } from "crossws";
|
|
46
|
+
|
|
47
|
+
const websocketHooks = defineWebSocketHooks({
|
|
48
|
+
open(peer) {
|
|
49
|
+
console.log("[ws] open", peer);
|
|
50
|
+
},
|
|
51
|
+
|
|
52
|
+
message(peer, message) {
|
|
53
|
+
console.log("[ws] message", peer, message);
|
|
54
|
+
if (message.text().includes("ping")) {
|
|
55
|
+
peer.send("pong");
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
|
|
59
|
+
close(peer, event) {
|
|
60
|
+
console.log("[ws] close", peer, event);
|
|
61
|
+
},
|
|
62
|
+
|
|
63
|
+
error(peer, error) {
|
|
64
|
+
console.log("[ws] error", peer, error);
|
|
65
|
+
},
|
|
66
|
+
|
|
67
|
+
// ... platform hooks such as bun:drain ...
|
|
68
|
+
});
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Peer Object
|
|
72
|
+
|
|
73
|
+
Websocket hooks always accept a peer instance as the first argument. `peer`, keeps the state of the connected client.
|
|
74
|
+
|
|
75
|
+
**Properties:**
|
|
76
|
+
|
|
77
|
+
- `peer.id?`: The peer address or unique id (might be `undefined`)
|
|
78
|
+
- `peer.readyState`: The connection status ([MDN](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/readyState))
|
|
79
|
+
- `peer.ctx[name]`: Keeps the state of native client connection
|
|
80
|
+
|
|
81
|
+
**Methods:**
|
|
82
|
+
|
|
83
|
+
- `send(message, compress)`: Send a message to the connected client
|
|
84
|
+
|
|
85
|
+
> [!TIP]
|
|
86
|
+
> You can safely log a peer instance to the console using `console.log` it will be automatically stringified with useful information including the remote address and connection status!
|
|
87
|
+
|
|
88
|
+
### Message Object
|
|
89
|
+
|
|
90
|
+
on `message` hook, you receive a message object containing an incoming message from the client.
|
|
91
|
+
|
|
92
|
+
**Properties:**
|
|
93
|
+
|
|
94
|
+
- `message.rawData`: Raw message data
|
|
95
|
+
- `message.isBinary`: Indicates if the message is binary (might be `undefined`)
|
|
96
|
+
|
|
97
|
+
**Methods:**
|
|
98
|
+
|
|
99
|
+
- `message.text()`: Get stringified version of the message
|
|
100
|
+
|
|
101
|
+
> [!TIP]
|
|
102
|
+
> You can safely log `message` object to the console using `console.log` it will be automatically stringified!
|
|
103
|
+
|
|
104
|
+
## Error handling
|
|
105
|
+
|
|
106
|
+
You can catch errors using `error` hook. The second argument is error wrapped into a `WebSocketError` class.
|
|
107
|
+
|
|
108
|
+
## Universal WebSocket client
|
|
109
|
+
|
|
110
|
+
CrossWS also exposes a universal way to use [WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) API in order to connect to the server. For all runtimes, except Node.js, native implementation is used, and for Node.js, a bundled version of [`ws.WebSocket`](https://www.npmjs.com/package/ws) is bundled.
|
|
111
|
+
|
|
112
|
+
```js
|
|
113
|
+
import WebSocket from "crossws/websocket";
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
> [!NOTE]
|
|
117
|
+
> Using export conditions, the correct version will be always used so you don't have to worry about picking the right build!
|
|
39
118
|
|
|
40
|
-
|
|
119
|
+
## Integrations
|
|
120
|
+
|
|
121
|
+
CrossWS allows integrating your WebSocket hooks with different runtimes and platforms using built-in adapters. Each runtime has a specific method of integrating WebSocket. Once integrated, your custom hooks (such as `message` and `close`) will work consistently even if you change the runtime!
|
|
41
122
|
|
|
42
123
|
### Integration with **Node.js**
|
|
43
124
|
|
|
@@ -53,26 +134,38 @@ const server = createServer((req, res) => {
|
|
|
53
134
|
);
|
|
54
135
|
}).listen(3000);
|
|
55
136
|
|
|
56
|
-
// Initialize WebSocket
|
|
137
|
+
// Initialize WebSocket Hooks
|
|
57
138
|
import nodeWSAdapter from "crossws/adapters/node";
|
|
58
139
|
|
|
59
|
-
const { handleUpgrade } = nodeWSAdapter({
|
|
140
|
+
const { handleUpgrade } = nodeWSAdapter({ message: console.log });
|
|
60
141
|
server.on("upgrade", handleUpgrade);
|
|
61
142
|
```
|
|
62
143
|
|
|
144
|
+
**Node-specific hooks:**
|
|
145
|
+
|
|
146
|
+
- `node:open (peer)`
|
|
147
|
+
- `node:message (peer, data, isBinary)`
|
|
148
|
+
- `node:close (peer, code, reason)`
|
|
149
|
+
- `node:error (peer, error)`
|
|
150
|
+
- `node:ping (peer)`
|
|
151
|
+
- `node:pong (peer)`
|
|
152
|
+
- `node:unexpected-response (peer, req, res)`
|
|
153
|
+
- `node:upgrade (peer, req)`
|
|
154
|
+
|
|
63
155
|
See [playground/node.ts](./playground/node.ts) for demo and [src/adapters/node.ts](./src/adapters/node.ts) for implementation.
|
|
64
156
|
|
|
65
|
-
|
|
157
|
+
### Integration with **Bun**
|
|
66
158
|
|
|
67
|
-
To integrate
|
|
159
|
+
To integrate CrossWS with your Bun server, you need to check for `server.upgrade` and also pass the `websocket` object returned from the adapter to server options. CrossWS leverages native Bun WebSocket API.
|
|
68
160
|
|
|
69
161
|
```ts
|
|
70
162
|
import bunAdapter from "./dist/adapters/bun";
|
|
71
163
|
|
|
72
|
-
const { websocket } = bunAdapter({
|
|
164
|
+
const { websocket } = bunAdapter({ message: console.log });
|
|
73
165
|
|
|
74
166
|
Bun.serve({
|
|
75
167
|
port: 3000,
|
|
168
|
+
websocket,
|
|
76
169
|
fetch(req, server) {
|
|
77
170
|
if (server.upgrade(req)) {
|
|
78
171
|
return;
|
|
@@ -82,20 +175,29 @@ Bun.serve({
|
|
|
82
175
|
{ headers: { "content-type": "text/html" } },
|
|
83
176
|
);
|
|
84
177
|
},
|
|
85
|
-
websocket,
|
|
86
178
|
});
|
|
87
179
|
```
|
|
88
180
|
|
|
181
|
+
**Bun-specific hooks:**
|
|
182
|
+
|
|
183
|
+
- `bun:message (peer, ws,message)`
|
|
184
|
+
- `bun:open (peer, ws)`
|
|
185
|
+
- `bun:close (peer, ws)`
|
|
186
|
+
- `bun:drain (peer)`
|
|
187
|
+
- `bun:error (peer, ws, error)`
|
|
188
|
+
- `bun:ping (peer, ws, data)`
|
|
189
|
+
- `bun:pong (peer, ws, data)`
|
|
190
|
+
|
|
89
191
|
See [playground/bun.ts](./playground/bun.ts) for demo and [src/adapters/bun.ts](./src/adapters/bun.ts) for implementation.
|
|
90
192
|
|
|
91
|
-
|
|
193
|
+
### Integration with **Deno**
|
|
92
194
|
|
|
93
195
|
To integrate CrossWS with your Deno server, you need to check for the `upgrade` header and then call `handleUpgrade` method from the adapter passing the incoming request object. The returned value is the server upgrade response.
|
|
94
196
|
|
|
95
197
|
```ts
|
|
96
198
|
import denoAdapter from "crossws/adapters/deno";
|
|
97
199
|
|
|
98
|
-
const { handleUpgrade } = denoAdapter({
|
|
200
|
+
const { handleUpgrade } = denoAdapter({ message: console.log });
|
|
99
201
|
|
|
100
202
|
Deno.serve({ port: 3000 }, (req) => {
|
|
101
203
|
if (req.headers.get("upgrade") === "websocket") {
|
|
@@ -108,63 +210,56 @@ Deno.serve({ port: 3000 }, (req) => {
|
|
|
108
210
|
});
|
|
109
211
|
```
|
|
110
212
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
## Integration with other runtimes
|
|
114
|
-
|
|
115
|
-
You can define your custom adapters using `defineWebSocketAdapter` wrapper.
|
|
213
|
+
**Deno-specific hooks:**
|
|
116
214
|
|
|
117
|
-
|
|
215
|
+
- `deno:open (peer)`
|
|
216
|
+
- `deno:message (peer, event)`
|
|
217
|
+
- `deno:close (peer)`
|
|
218
|
+
- `deno:error (peer, error)`
|
|
118
219
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
Previously you saw in the adapter examples that we pass `onMessage` option.
|
|
220
|
+
See [playground/deno.ts](./playground/deno.ts) for demo and [src/adapters/deno.ts](./src/adapters/deno.ts) for implementation.
|
|
122
221
|
|
|
123
|
-
|
|
222
|
+
### Integration with **Cloudflare Workers**
|
|
124
223
|
|
|
125
|
-
|
|
224
|
+
To integrate CrossWS with your Cloudflare Workers, you need to check for the `upgrade` header
|
|
126
225
|
|
|
127
226
|
```ts
|
|
128
|
-
import
|
|
227
|
+
import cloudflareAdapter from "crossws/adapters/cloudflare";
|
|
129
228
|
|
|
130
|
-
const
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
229
|
+
const { handleUpgrade } = cloudflareAdapter({ message: console.log });
|
|
230
|
+
|
|
231
|
+
export default {
|
|
232
|
+
async fetch(request, env, context) {
|
|
233
|
+
if (request.headers.get("upgrade") === "websocket") {
|
|
234
|
+
return handleUpgrade(request, env, context);
|
|
135
235
|
}
|
|
236
|
+
return new Response(
|
|
237
|
+
`<script>new WebSocket("ws://localhost:3000").addEventListener("open", (e) => e.target.send("Hello from client!"));</script>`,
|
|
238
|
+
{ headers: { "content-type": "text/html" } },
|
|
239
|
+
);
|
|
136
240
|
},
|
|
137
|
-
|
|
138
|
-
console.log("error", peer, error);
|
|
139
|
-
},
|
|
140
|
-
onOpen: (peer) => {
|
|
141
|
-
console.log("open", peer);
|
|
142
|
-
},
|
|
143
|
-
onClose: (peer, code, reason) => {
|
|
144
|
-
console.log("close", peer, code, reason);
|
|
145
|
-
},
|
|
146
|
-
onEvent: (event, ...args) => {
|
|
147
|
-
console.log("event", event);
|
|
148
|
-
},
|
|
149
|
-
});
|
|
241
|
+
};
|
|
150
242
|
```
|
|
151
243
|
|
|
152
|
-
|
|
244
|
+
**Cloudflare-specific hooks:**
|
|
153
245
|
|
|
154
|
-
|
|
246
|
+
- `cloudflare:accept(peer)`
|
|
247
|
+
- `cloudflare:message(peer, event)`
|
|
248
|
+
- `cloudflare:error(peer, event)`
|
|
249
|
+
- `cloudflare:close(peer, event)`
|
|
155
250
|
|
|
156
|
-
|
|
251
|
+
See [playground/cloudflare.ts](./playground/cloudflare.ts) for demo and [src/adapters/cloudflare.ts](./src/adapters/cloudflare.ts) for implementation.
|
|
157
252
|
|
|
158
|
-
###
|
|
253
|
+
### Integration with other runtimes
|
|
159
254
|
|
|
160
|
-
|
|
255
|
+
You can define your custom adapters using `defineWebSocketAdapter` wrapper.
|
|
161
256
|
|
|
162
|
-
|
|
257
|
+
See other adapter implementations in [./src/adapters](./src/adapters/) to get an idea of how adapters can be implemented and feel free to directly make a Pull Request to support your environment in CrossWS!
|
|
163
258
|
|
|
164
259
|
## Development
|
|
165
260
|
|
|
166
261
|
- Clone this repository
|
|
167
|
-
- Install latest LTS version of [Node.js](https://nodejs.org/en/)
|
|
262
|
+
- Install the latest LTS version of [Node.js](https://nodejs.org/en/)
|
|
168
263
|
- Enable [Corepack](https://github.com/nodejs/corepack) using `corepack enable`
|
|
169
264
|
- Install dependencies using `pnpm install`
|
|
170
265
|
- Run interactive tests using `pnpm dev`
|
|
@@ -181,7 +276,3 @@ Published under [MIT License](./LICENSE).
|
|
|
181
276
|
[npm-version-href]: https://npmjs.com/package/crossws
|
|
182
277
|
[npm-downloads-src]: https://img.shields.io/npm/dm/crossws?style=flat&colorA=18181B&colorB=F0DB4F
|
|
183
278
|
[npm-downloads-href]: https://npmjs.com/package/crossws
|
|
184
|
-
[codecov-src]: https://img.shields.io/codecov/c/gh/unjs/crossws/main?style=flat&colorA=18181B&colorB=F0DB4F
|
|
185
|
-
[codecov-href]: https://codecov.io/gh/unjs/crossws
|
|
186
|
-
[bundle-src]: https://img.shields.io/bundlephobia/minzip/crossws?style=flat&colorA=18181B&colorB=F0DB4F
|
|
187
|
-
[bundle-href]: https://bundlephobia.com/result?p=crossws
|
package/dist/adapters/bun.cjs
CHANGED
|
@@ -1,57 +1,72 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
const peer = require('../shared/crossws.21e14e0d.cjs');
|
|
4
4
|
|
|
5
|
-
const peer = require('../shared/crossws.002417e5.cjs');
|
|
6
|
-
|
|
7
|
-
const WebSocket = globalThis.WebSocket;
|
|
8
5
|
const bun = peer.defineWebSocketAdapter(
|
|
9
|
-
(
|
|
6
|
+
(hooks, opts = {}) => {
|
|
7
|
+
const getPeer = (ws) => {
|
|
8
|
+
if (ws.data?._peer) {
|
|
9
|
+
return ws.data._peer;
|
|
10
|
+
}
|
|
11
|
+
const peer = new WebSocketPeer({ bun: { ws } });
|
|
12
|
+
ws.data = ws.data || {};
|
|
13
|
+
ws.data._peer = peer;
|
|
14
|
+
return peer;
|
|
15
|
+
};
|
|
10
16
|
return {
|
|
11
17
|
websocket: {
|
|
12
18
|
message: (ws, message) => {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
19
|
+
const peer$1 = getPeer(ws);
|
|
20
|
+
hooks["bun:message"]?.(peer$1, ws, message);
|
|
21
|
+
hooks.message?.(peer$1, new peer.WebSocketMessage(message));
|
|
16
22
|
},
|
|
17
23
|
open: (ws) => {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
24
|
+
const peer = getPeer(ws);
|
|
25
|
+
hooks["bun:open"]?.(peer, ws);
|
|
26
|
+
hooks.open?.(peer);
|
|
21
27
|
},
|
|
22
28
|
close: (ws) => {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
29
|
+
const peer = getPeer(ws);
|
|
30
|
+
hooks["bun:close"]?.(peer, ws);
|
|
31
|
+
hooks.close?.(peer, {});
|
|
32
|
+
},
|
|
33
|
+
drain: (ws) => {
|
|
34
|
+
const peer = getPeer(ws);
|
|
35
|
+
hooks["bun:drain"]?.(peer);
|
|
26
36
|
},
|
|
37
|
+
// @ts-expect-error types unavailable but mentioned in docs
|
|
27
38
|
error: (ws, error) => {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
39
|
+
const peer$1 = getPeer(ws);
|
|
40
|
+
hooks["bun:error"]?.(peer$1, ws, error);
|
|
41
|
+
hooks.error?.(peer$1, new peer.WebSocketError(error));
|
|
31
42
|
},
|
|
32
|
-
|
|
33
|
-
|
|
43
|
+
ping(ws, data) {
|
|
44
|
+
const peer = getPeer(ws);
|
|
45
|
+
hooks["bun:ping"]?.(peer, ws, data);
|
|
46
|
+
},
|
|
47
|
+
pong(ws, data) {
|
|
48
|
+
const peer = getPeer(ws);
|
|
49
|
+
hooks["bun:pong"]?.(peer, ws, data);
|
|
34
50
|
}
|
|
35
51
|
}
|
|
36
52
|
};
|
|
37
53
|
}
|
|
38
54
|
);
|
|
39
|
-
class
|
|
40
|
-
constructor(_ws) {
|
|
41
|
-
super();
|
|
42
|
-
this._ws = _ws;
|
|
43
|
-
}
|
|
55
|
+
class WebSocketPeer extends peer.WebSocketPeerBase {
|
|
44
56
|
get id() {
|
|
45
|
-
|
|
57
|
+
let addr = this.ctx.bun.ws.remoteAddress;
|
|
58
|
+
if (addr.includes(":")) {
|
|
59
|
+
addr = `[${addr}]`;
|
|
60
|
+
}
|
|
61
|
+
return addr;
|
|
46
62
|
}
|
|
47
63
|
get readyState() {
|
|
48
|
-
return this.
|
|
64
|
+
return this.ctx.bun.ws.readyState;
|
|
49
65
|
}
|
|
50
66
|
send(message) {
|
|
51
|
-
this.
|
|
67
|
+
this.ctx.bun.ws.send(message);
|
|
52
68
|
return 0;
|
|
53
69
|
}
|
|
54
70
|
}
|
|
55
71
|
|
|
56
|
-
exports
|
|
57
|
-
exports.default = bun;
|
|
72
|
+
module.exports = bun;
|
package/dist/adapters/bun.d.cts
CHANGED
|
@@ -1,43 +1,24 @@
|
|
|
1
|
-
import { WebSocketAdapter } from '../index.cjs';
|
|
1
|
+
import { WebSocketAdapter, WebSocketPeerBase } from '../index.cjs';
|
|
2
|
+
import { WebSocketHandler, ServerWebSocket } from 'bun';
|
|
2
3
|
|
|
3
|
-
interface BunWSOptions {
|
|
4
|
-
message: (ws: BunServerWebSocket, message: string | ArrayBuffer | Uint8Array) => void;
|
|
5
|
-
open?: (ws: BunServerWebSocket) => void;
|
|
6
|
-
close?: (ws: BunServerWebSocket) => void;
|
|
7
|
-
error?: (ws: BunServerWebSocket, error: Error) => void;
|
|
8
|
-
drain?: (ws: BunServerWebSocket) => void;
|
|
9
|
-
perMessageDeflate?: boolean | {
|
|
10
|
-
compress?: boolean | BunWSCompressor;
|
|
11
|
-
decompress?: boolean | BunWSCompressor;
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
|
-
type BunWSCompressor = `"disable"` | `"shared"` | `"dedicated"` | `"3KB"` | `"4KB"` | `"8KB"` | `"16KB"` | `"32KB"` | `"64KB"` | `"128KB"` | `"256KB"`;
|
|
15
|
-
interface BunServerWebSocket {
|
|
16
|
-
readonly data: any;
|
|
17
|
-
readonly readyState: number;
|
|
18
|
-
readonly remoteAddress: string;
|
|
19
|
-
send(message: string | ArrayBuffer | Uint8Array, compress?: boolean): number;
|
|
20
|
-
close(code?: number, reason?: string): void;
|
|
21
|
-
subscribe(topic: string): void;
|
|
22
|
-
unsubscribe(topic: string): void;
|
|
23
|
-
publish(topic: string, message: string | ArrayBuffer | Uint8Array): void;
|
|
24
|
-
isSubscribed(topic: string): boolean;
|
|
25
|
-
cork(cb: (ws: BunServerWebSocket) => void): void;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
declare const WebSocket: {
|
|
29
|
-
new (url: string | URL, protocols?: string | string[] | undefined): WebSocket;
|
|
30
|
-
prototype: WebSocket;
|
|
31
|
-
readonly CONNECTING: 0;
|
|
32
|
-
readonly OPEN: 1;
|
|
33
|
-
readonly CLOSING: 2;
|
|
34
|
-
readonly CLOSED: 3;
|
|
35
|
-
};
|
|
36
4
|
interface AdapterOptions {
|
|
37
5
|
}
|
|
6
|
+
type ContextData = {
|
|
7
|
+
_peer?: WebSocketPeer;
|
|
8
|
+
};
|
|
38
9
|
interface Adapter {
|
|
39
|
-
websocket:
|
|
10
|
+
websocket: WebSocketHandler<ContextData>;
|
|
40
11
|
}
|
|
41
12
|
declare const _default: WebSocketAdapter<Adapter, AdapterOptions>;
|
|
42
13
|
|
|
43
|
-
|
|
14
|
+
declare class WebSocketPeer extends WebSocketPeerBase<{
|
|
15
|
+
bun: {
|
|
16
|
+
ws: ServerWebSocket<ContextData>;
|
|
17
|
+
};
|
|
18
|
+
}> {
|
|
19
|
+
get id(): string;
|
|
20
|
+
get readyState(): any;
|
|
21
|
+
send(message: string | ArrayBuffer): number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export { type Adapter, type AdapterOptions, _default as default };
|
package/dist/adapters/bun.d.mts
CHANGED
|
@@ -1,43 +1,24 @@
|
|
|
1
|
-
import { WebSocketAdapter } from '../index.mjs';
|
|
1
|
+
import { WebSocketAdapter, WebSocketPeerBase } from '../index.mjs';
|
|
2
|
+
import { WebSocketHandler, ServerWebSocket } from 'bun';
|
|
2
3
|
|
|
3
|
-
interface BunWSOptions {
|
|
4
|
-
message: (ws: BunServerWebSocket, message: string | ArrayBuffer | Uint8Array) => void;
|
|
5
|
-
open?: (ws: BunServerWebSocket) => void;
|
|
6
|
-
close?: (ws: BunServerWebSocket) => void;
|
|
7
|
-
error?: (ws: BunServerWebSocket, error: Error) => void;
|
|
8
|
-
drain?: (ws: BunServerWebSocket) => void;
|
|
9
|
-
perMessageDeflate?: boolean | {
|
|
10
|
-
compress?: boolean | BunWSCompressor;
|
|
11
|
-
decompress?: boolean | BunWSCompressor;
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
|
-
type BunWSCompressor = `"disable"` | `"shared"` | `"dedicated"` | `"3KB"` | `"4KB"` | `"8KB"` | `"16KB"` | `"32KB"` | `"64KB"` | `"128KB"` | `"256KB"`;
|
|
15
|
-
interface BunServerWebSocket {
|
|
16
|
-
readonly data: any;
|
|
17
|
-
readonly readyState: number;
|
|
18
|
-
readonly remoteAddress: string;
|
|
19
|
-
send(message: string | ArrayBuffer | Uint8Array, compress?: boolean): number;
|
|
20
|
-
close(code?: number, reason?: string): void;
|
|
21
|
-
subscribe(topic: string): void;
|
|
22
|
-
unsubscribe(topic: string): void;
|
|
23
|
-
publish(topic: string, message: string | ArrayBuffer | Uint8Array): void;
|
|
24
|
-
isSubscribed(topic: string): boolean;
|
|
25
|
-
cork(cb: (ws: BunServerWebSocket) => void): void;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
declare const WebSocket: {
|
|
29
|
-
new (url: string | URL, protocols?: string | string[] | undefined): WebSocket;
|
|
30
|
-
prototype: WebSocket;
|
|
31
|
-
readonly CONNECTING: 0;
|
|
32
|
-
readonly OPEN: 1;
|
|
33
|
-
readonly CLOSING: 2;
|
|
34
|
-
readonly CLOSED: 3;
|
|
35
|
-
};
|
|
36
4
|
interface AdapterOptions {
|
|
37
5
|
}
|
|
6
|
+
type ContextData = {
|
|
7
|
+
_peer?: WebSocketPeer;
|
|
8
|
+
};
|
|
38
9
|
interface Adapter {
|
|
39
|
-
websocket:
|
|
10
|
+
websocket: WebSocketHandler<ContextData>;
|
|
40
11
|
}
|
|
41
12
|
declare const _default: WebSocketAdapter<Adapter, AdapterOptions>;
|
|
42
13
|
|
|
43
|
-
|
|
14
|
+
declare class WebSocketPeer extends WebSocketPeerBase<{
|
|
15
|
+
bun: {
|
|
16
|
+
ws: ServerWebSocket<ContextData>;
|
|
17
|
+
};
|
|
18
|
+
}> {
|
|
19
|
+
get id(): string;
|
|
20
|
+
get readyState(): any;
|
|
21
|
+
send(message: string | ArrayBuffer): number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export { type Adapter, type AdapterOptions, _default as default };
|
package/dist/adapters/bun.d.ts
CHANGED
|
@@ -1,43 +1,24 @@
|
|
|
1
|
-
import { WebSocketAdapter } from '../index.js';
|
|
1
|
+
import { WebSocketAdapter, WebSocketPeerBase } from '../index.js';
|
|
2
|
+
import { WebSocketHandler, ServerWebSocket } from 'bun';
|
|
2
3
|
|
|
3
|
-
interface BunWSOptions {
|
|
4
|
-
message: (ws: BunServerWebSocket, message: string | ArrayBuffer | Uint8Array) => void;
|
|
5
|
-
open?: (ws: BunServerWebSocket) => void;
|
|
6
|
-
close?: (ws: BunServerWebSocket) => void;
|
|
7
|
-
error?: (ws: BunServerWebSocket, error: Error) => void;
|
|
8
|
-
drain?: (ws: BunServerWebSocket) => void;
|
|
9
|
-
perMessageDeflate?: boolean | {
|
|
10
|
-
compress?: boolean | BunWSCompressor;
|
|
11
|
-
decompress?: boolean | BunWSCompressor;
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
|
-
type BunWSCompressor = `"disable"` | `"shared"` | `"dedicated"` | `"3KB"` | `"4KB"` | `"8KB"` | `"16KB"` | `"32KB"` | `"64KB"` | `"128KB"` | `"256KB"`;
|
|
15
|
-
interface BunServerWebSocket {
|
|
16
|
-
readonly data: any;
|
|
17
|
-
readonly readyState: number;
|
|
18
|
-
readonly remoteAddress: string;
|
|
19
|
-
send(message: string | ArrayBuffer | Uint8Array, compress?: boolean): number;
|
|
20
|
-
close(code?: number, reason?: string): void;
|
|
21
|
-
subscribe(topic: string): void;
|
|
22
|
-
unsubscribe(topic: string): void;
|
|
23
|
-
publish(topic: string, message: string | ArrayBuffer | Uint8Array): void;
|
|
24
|
-
isSubscribed(topic: string): boolean;
|
|
25
|
-
cork(cb: (ws: BunServerWebSocket) => void): void;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
declare const WebSocket: {
|
|
29
|
-
new (url: string | URL, protocols?: string | string[] | undefined): WebSocket;
|
|
30
|
-
prototype: WebSocket;
|
|
31
|
-
readonly CONNECTING: 0;
|
|
32
|
-
readonly OPEN: 1;
|
|
33
|
-
readonly CLOSING: 2;
|
|
34
|
-
readonly CLOSED: 3;
|
|
35
|
-
};
|
|
36
4
|
interface AdapterOptions {
|
|
37
5
|
}
|
|
6
|
+
type ContextData = {
|
|
7
|
+
_peer?: WebSocketPeer;
|
|
8
|
+
};
|
|
38
9
|
interface Adapter {
|
|
39
|
-
websocket:
|
|
10
|
+
websocket: WebSocketHandler<ContextData>;
|
|
40
11
|
}
|
|
41
12
|
declare const _default: WebSocketAdapter<Adapter, AdapterOptions>;
|
|
42
13
|
|
|
43
|
-
|
|
14
|
+
declare class WebSocketPeer extends WebSocketPeerBase<{
|
|
15
|
+
bun: {
|
|
16
|
+
ws: ServerWebSocket<ContextData>;
|
|
17
|
+
};
|
|
18
|
+
}> {
|
|
19
|
+
get id(): string;
|
|
20
|
+
get readyState(): any;
|
|
21
|
+
send(message: string | ArrayBuffer): number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export { type Adapter, type AdapterOptions, _default as default };
|