@fluixi/realtime 0.0.0-stage → 1.0.0-alpha.110

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 (62) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +24 -2
  3. package/dist/bus.cjs +127 -0
  4. package/dist/bus.d.ts +58 -0
  5. package/dist/bus.d.ts.map +1 -0
  6. package/dist/bus.js +118 -0
  7. package/dist/bus.mjs +8 -0
  8. package/dist/channel-protocol.cjs +42 -0
  9. package/dist/channel-protocol.d.ts +40 -0
  10. package/dist/channel-protocol.d.ts.map +1 -0
  11. package/dist/channel-protocol.js +24 -0
  12. package/dist/channel-protocol.mjs +8 -0
  13. package/dist/channel-server.cjs +291 -0
  14. package/dist/channel-server.d.ts +29 -0
  15. package/dist/channel-server.d.ts.map +1 -0
  16. package/dist/channel-server.js +122 -0
  17. package/dist/channel-server.mjs +9 -0
  18. package/dist/channels.cjs +426 -0
  19. package/dist/channels.d.ts +38 -0
  20. package/dist/channels.d.ts.map +1 -0
  21. package/dist/channels.js +82 -0
  22. package/dist/channels.mjs +9 -0
  23. package/dist/chunk-4CEAXWJN.mjs +31 -0
  24. package/dist/chunk-AWS4MP5D.mjs +107 -0
  25. package/dist/chunk-DNVSSCJA.mjs +22 -0
  26. package/dist/chunk-GAAZQJJ4.mjs +145 -0
  27. package/dist/chunk-MGON7IDW.mjs +77 -0
  28. package/dist/chunk-Q3NDGSX6.mjs +107 -0
  29. package/dist/chunk-UAZIXYV5.mjs +315 -0
  30. package/dist/chunk-X73D2DE2.mjs +19 -0
  31. package/dist/endpoint.cjs +165 -0
  32. package/dist/endpoint.d.ts +80 -0
  33. package/dist/endpoint.d.ts.map +1 -0
  34. package/dist/endpoint.js +168 -0
  35. package/dist/endpoint.mjs +8 -0
  36. package/dist/index.cjs +445 -0
  37. package/dist/index.d.ts +6 -0
  38. package/dist/index.d.ts.map +1 -0
  39. package/dist/index.js +4 -0
  40. package/dist/index.mjs +29 -0
  41. package/dist/serializers.cjs +39 -0
  42. package/dist/serializers.d.ts +12 -0
  43. package/dist/serializers.d.ts.map +1 -0
  44. package/dist/serializers.js +16 -0
  45. package/dist/serializers.mjs +8 -0
  46. package/dist/server.cjs +385 -0
  47. package/dist/server.d.ts +5 -0
  48. package/dist/server.d.ts.map +1 -0
  49. package/dist/server.js +3 -0
  50. package/dist/server.mjs +20 -0
  51. package/dist/socket.cjs +362 -0
  52. package/dist/socket.d.ts +137 -0
  53. package/dist/socket.d.ts.map +1 -0
  54. package/dist/socket.js +351 -0
  55. package/dist/socket.mjs +16 -0
  56. package/dist/transport.cjs +51 -0
  57. package/dist/transport.d.ts +33 -0
  58. package/dist/transport.d.ts.map +1 -0
  59. package/dist/transport.js +36 -0
  60. package/dist/transport.mjs +8 -0
  61. package/dist/tsconfig.lib.tsbuildinfo +1 -0
  62. package/package.json +65 -3
@@ -0,0 +1,165 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/endpoint.ts
21
+ var endpoint_exports = {};
22
+ __export(endpoint_exports, {
23
+ CLOSE_REAUTHENTICATE: () => CLOSE_REAUTHENTICATE,
24
+ createWebSocketServer: () => createWebSocketServer
25
+ });
26
+ module.exports = __toCommonJS(endpoint_exports);
27
+ var import_ws = require("ws");
28
+ var DRAIN_POLL_MS = 20;
29
+ var CLOSE_REAUTHENTICATE = 4100;
30
+ var REFUSALS = {
31
+ 401: "Unauthorized",
32
+ 403: "Forbidden",
33
+ 503: "Service Unavailable"
34
+ };
35
+ function createWebSocketServer(options) {
36
+ const maxConnections = options.maxConnections ?? 1e4;
37
+ const maxMessageSize = options.maxMessageSize ?? 1024 * 1024;
38
+ const maxBuffered = options.maxBuffered ?? 4 * 1024 * 1024;
39
+ const pingInterval = options.pingInterval ?? 25e3;
40
+ const wss = new import_ws.WebSocketServer({ noServer: true, maxPayload: maxMessageSize });
41
+ const alive = /* @__PURE__ */ new WeakSet();
42
+ let pending = 0;
43
+ let closing = false;
44
+ const ping = setInterval(() => {
45
+ for (const ws of wss.clients) {
46
+ if (!alive.has(ws)) {
47
+ ws.terminate();
48
+ continue;
49
+ }
50
+ alive.delete(ws);
51
+ ws.ping();
52
+ if (options.keepAlive !== void 0) ws.send(options.keepAlive);
53
+ }
54
+ }, pingInterval);
55
+ ping.unref?.();
56
+ const allowedOrigin = (request) => {
57
+ const origin = request.headers.origin;
58
+ if (!origin) return true;
59
+ if (typeof options.origins === "function") return options.origins(origin, request);
60
+ if (options.origins) return options.origins.includes(origin);
61
+ try {
62
+ return new URL(origin).host === request.headers.host;
63
+ } catch {
64
+ return false;
65
+ }
66
+ };
67
+ const refuse = (socket, status) => {
68
+ if (socket.destroyed) return;
69
+ socket.end(`HTTP/1.1 ${status} ${REFUSALS[status]}\r
70
+ Connection: close\r
71
+ Content-Length: 0\r
72
+ \r
73
+ `);
74
+ };
75
+ const accept = async (request, socket, head) => {
76
+ if (closing || wss.clients.size + pending >= maxConnections) return refuse(socket, 503);
77
+ if (!allowedOrigin(request)) return refuse(socket, 403);
78
+ pending += 1;
79
+ let context;
80
+ try {
81
+ context = await options.authenticate(request);
82
+ } catch (error) {
83
+ console.error(error);
84
+ context = null;
85
+ } finally {
86
+ pending -= 1;
87
+ }
88
+ if (context === null) return refuse(socket, 401);
89
+ if (closing || socket.destroyed) return refuse(socket, 503);
90
+ wss.handleUpgrade(request, socket, head, (ws) => {
91
+ alive.add(ws);
92
+ ws.on("pong", () => alive.add(ws));
93
+ ws.on("error", () => {
94
+ });
95
+ if (options.maxAge) {
96
+ const timer = setTimeout(() => ws.close(CLOSE_REAUTHENTICATE, "reauthenticate"), options.maxAge);
97
+ timer.unref?.();
98
+ ws.once("close", () => clearTimeout(timer));
99
+ }
100
+ options.onConnection(wrap(ws, context, maxBuffered));
101
+ });
102
+ };
103
+ return {
104
+ upgrade(request, socket, head) {
105
+ const path = new URL(request.url ?? "/", "http://localhost").pathname;
106
+ if (path !== options.path) return false;
107
+ void accept(request, socket, head);
108
+ return true;
109
+ },
110
+ size: () => wss.clients.size,
111
+ close(timeout = 5e3) {
112
+ closing = true;
113
+ clearInterval(ping);
114
+ const open = [...wss.clients];
115
+ for (const ws of open) ws.close(1001, "server shutting down");
116
+ return new Promise((resolve) => {
117
+ const timer = setTimeout(() => {
118
+ for (const ws of wss.clients) ws.terminate();
119
+ }, timeout);
120
+ timer.unref?.();
121
+ const done = () => {
122
+ if (wss.clients.size) return;
123
+ clearTimeout(timer);
124
+ wss.close(() => resolve());
125
+ };
126
+ for (const ws of open) ws.once("close", done);
127
+ done();
128
+ });
129
+ }
130
+ };
131
+ }
132
+ function wrap(ws, context, maxBuffered) {
133
+ return {
134
+ context,
135
+ get buffered() {
136
+ return ws.bufferedAmount;
137
+ },
138
+ send(data) {
139
+ if (ws.readyState !== ws.OPEN) return "closed";
140
+ if (ws.bufferedAmount > maxBuffered) return "buffer-full";
141
+ ws.send(data);
142
+ return "sent";
143
+ },
144
+ drained(bytes = maxBuffered / 2) {
145
+ return new Promise((resolve) => {
146
+ const check = () => {
147
+ if (ws.readyState !== ws.OPEN || ws.bufferedAmount <= bytes) return resolve();
148
+ setTimeout(check, DRAIN_POLL_MS);
149
+ };
150
+ check();
151
+ });
152
+ },
153
+ close(code = 1e3, reason = "") {
154
+ ws.close(code, reason);
155
+ },
156
+ on(event, handler) {
157
+ const listener = event === "message" ? (data, isBinary) => {
158
+ const bytes = Array.isArray(data) ? Buffer.concat(data) : Buffer.from(data);
159
+ handler(isBinary ? new Uint8Array(bytes) : bytes.toString("utf8"));
160
+ } : (code, reason) => handler(code, reason.toString("utf8"));
161
+ ws.on(event, listener);
162
+ return () => void ws.off(event, listener);
163
+ }
164
+ };
165
+ }
@@ -0,0 +1,80 @@
1
+ import type { IncomingMessage } from 'node:http';
2
+ import type { Duplex } from 'node:stream';
3
+ /** What a server connection can send. */
4
+ export type ServerSendData = string | Uint8Array | ArrayBuffer;
5
+ /** What `send` did: only `sent` means the frame went to the socket. */
6
+ export type ServerSendResult = 'sent' | 'buffer-full' | 'closed';
7
+ export interface ServerConnection<Context> {
8
+ /** What `authenticate` returned for this connection. */
9
+ readonly context: Context;
10
+ /** Bytes queued in the socket and not yet written. */
11
+ readonly buffered: number;
12
+ send(data: ServerSendData): ServerSendResult;
13
+ /**
14
+ * Settles once at most `bytes` wait unsent (half of `maxBuffered` by default), or the
15
+ * connection closed: after `buffer-full`, wait on it before sending more. Node's socket gives
16
+ * no event for this through `ws`, so it is checked every 20 ms while it waits.
17
+ */
18
+ drained(bytes?: number): Promise<void>;
19
+ close(code?: number, reason?: string): void;
20
+ /** Text frames arrive as strings, binary ones as bytes. */
21
+ on(event: 'message', handler: (data: string | Uint8Array) => void): () => void;
22
+ on(event: 'close', handler: (code: number, reason: string) => void): () => void;
23
+ }
24
+ export interface WebSocketServerOptions<Context> {
25
+ /** The address path this server answers; other upgrades are left to the next hook. */
26
+ path: string;
27
+ /**
28
+ * Who connects, from the upgrade request (its cookie, a ticket in the address); null answers
29
+ * 401. Runs after the origin check.
30
+ */
31
+ authenticate: (request: IncomingMessage) => Context | null | Promise<Context | null>;
32
+ /**
33
+ * Pages allowed to connect, as origins (`https://app.example.test`) or a function. A browser
34
+ * sends its cookies with any socket it opens, so without this check another site could open
35
+ * one as your user. Default: the request's own host. A request without an `Origin` header
36
+ * does not come from a browser page and is let through to `authenticate`.
37
+ */
38
+ origins?: string[] | ((origin: string, request: IncomingMessage) => boolean);
39
+ onConnection: (connection: ServerConnection<Context>) => void;
40
+ /** Open connections at most; more answer 503. Default: 10000 */
41
+ maxConnections?: number;
42
+ /** Largest incoming message in bytes; a larger one closes the connection with 1009. Default: 1 MiB */
43
+ maxMessageSize?: number;
44
+ /** Bytes a connection may hold unsent before `send` answers `buffer-full`. Default: 4 MiB */
45
+ maxBuffered?: number;
46
+ /**
47
+ * Protocol ping interval in ms; a connection that has not answered the previous ping is
48
+ * dropped. Pings are invisible to browser code, so `keepAlive` sends a frame it can see.
49
+ * Default: 25000
50
+ */
51
+ pingInterval?: number;
52
+ /** A text frame sent every `pingInterval`, for clients that watch for silence. */
53
+ keepAlive?: string;
54
+ /**
55
+ * Longest life of a connection, in ms. Past it the server closes with `CLOSE_REAUTHENTICATE`,
56
+ * which the client answers by reconnecting, through `authenticate` again: a person whose
57
+ * access ended stops receiving by then. Default: none
58
+ */
59
+ maxAge?: number;
60
+ }
61
+ /** The close code for a connection past `maxAge`: reconnect, authenticating again. */
62
+ export declare const CLOSE_REAUTHENTICATE = 4100;
63
+ export interface RealtimeServer {
64
+ /** For `defineServer({ upgrade })`: true when the request was for `path`. */
65
+ upgrade(request: IncomingMessage, socket: Duplex, head: Buffer): boolean;
66
+ /** Open connections. */
67
+ size(): number;
68
+ /** Refuses new connections, closes open ones with 1001, and drops what is left after `timeout` ms. */
69
+ close(timeout?: number): Promise<void>;
70
+ }
71
+ /**
72
+ * A WebSocket endpoint for the app's server module, on the `ws` library.
73
+ *
74
+ * ```ts
75
+ * const live = createWebSocketServer({ path: '/live', authenticate: sessionFrom, onConnection });
76
+ * export default defineServer({ upgrade: live.upgrade, shutdown: () => live.close() });
77
+ * ```
78
+ */
79
+ export declare function createWebSocketServer<Context>(options: WebSocketServerOptions<Context>): RealtimeServer;
80
+ //# sourceMappingURL=endpoint.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"endpoint.d.ts","sourceRoot":"","sources":["../src/endpoint.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AACjD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAG1C,yCAAyC;AACzC,MAAM,MAAM,cAAc,GAAG,MAAM,GAAG,UAAU,GAAG,WAAW,CAAC;AAE/D,uEAAuE;AACvE,MAAM,MAAM,gBAAgB,GAAG,MAAM,GAAG,aAAa,GAAG,QAAQ,CAAC;AAEjE,MAAM,WAAW,gBAAgB,CAAC,OAAO;IACvC,wDAAwD;IACxD,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,sDAAsD;IACtD,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,IAAI,CAAC,IAAI,EAAE,cAAc,GAAG,gBAAgB,CAAC;IAC7C;;;;OAIG;IACH,OAAO,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACvC,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5C,2DAA2D;IAC3D,EAAE,CAAC,KAAK,EAAE,SAAS,EAAE,OAAO,EAAE,CAAC,IAAI,EAAE,MAAM,GAAG,UAAU,KAAK,IAAI,GAAG,MAAM,IAAI,CAAC;IAC/E,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,KAAK,IAAI,GAAG,MAAM,IAAI,CAAC;CACjF;AAED,MAAM,WAAW,sBAAsB,CAAC,OAAO;IAC7C,sFAAsF;IACtF,IAAI,EAAE,MAAM,CAAC;IACb;;;OAGG;IACH,YAAY,EAAE,CAAC,OAAO,EAAE,eAAe,KAAK,OAAO,GAAG,IAAI,GAAG,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC,CAAC;IACrF;;;;;OAKG;IACH,OAAO,CAAC,EAAE,MAAM,EAAE,GAAG,CAAC,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,eAAe,KAAK,OAAO,CAAC,CAAC;IAC7E,YAAY,EAAE,CAAC,UAAU,EAAE,gBAAgB,CAAC,OAAO,CAAC,KAAK,IAAI,CAAC;IAC9D,gEAAgE;IAChE,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,sGAAsG;IACtG,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,6FAA6F;IAC7F,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;;;OAIG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,kFAAkF;IAClF,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;;;OAIG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAID,sFAAsF;AACtF,eAAO,MAAM,oBAAoB,OAAO,CAAC;AAEzC,MAAM,WAAW,cAAc;IAC7B,6EAA6E;IAC7E,OAAO,CAAC,OAAO,EAAE,eAAe,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC;IACzE,wBAAwB;IACxB,IAAI,IAAI,MAAM,CAAC;IACf,sGAAsG;IACtG,KAAK,CAAC,OAAO,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACxC;AAQD;;;;;;;GAOG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,OAAO,EAAE,sBAAsB,CAAC,OAAO,CAAC,GAAG,cAAc,CAmGvG"}
@@ -0,0 +1,168 @@
1
+ import { WebSocketServer } from 'ws';
2
+ const DRAIN_POLL_MS = 20;
3
+ /** The close code for a connection past `maxAge`: reconnect, authenticating again. */
4
+ export const CLOSE_REAUTHENTICATE = 4100;
5
+ const REFUSALS = {
6
+ 401: 'Unauthorized',
7
+ 403: 'Forbidden',
8
+ 503: 'Service Unavailable',
9
+ };
10
+ /**
11
+ * A WebSocket endpoint for the app's server module, on the `ws` library.
12
+ *
13
+ * ```ts
14
+ * const live = createWebSocketServer({ path: '/live', authenticate: sessionFrom, onConnection });
15
+ * export default defineServer({ upgrade: live.upgrade, shutdown: () => live.close() });
16
+ * ```
17
+ */
18
+ export function createWebSocketServer(options) {
19
+ const maxConnections = options.maxConnections ?? 10_000;
20
+ const maxMessageSize = options.maxMessageSize ?? 1024 * 1024;
21
+ const maxBuffered = options.maxBuffered ?? 4 * 1024 * 1024;
22
+ const pingInterval = options.pingInterval ?? 25_000;
23
+ const wss = new WebSocketServer({ noServer: true, maxPayload: maxMessageSize });
24
+ const alive = new WeakSet();
25
+ /** Handshakes past the path check, counted with open connections against the limit. */
26
+ let pending = 0;
27
+ let closing = false;
28
+ const ping = setInterval(() => {
29
+ for (const ws of wss.clients) {
30
+ if (!alive.has(ws)) {
31
+ ws.terminate();
32
+ continue;
33
+ }
34
+ alive.delete(ws);
35
+ ws.ping();
36
+ if (options.keepAlive !== undefined)
37
+ ws.send(options.keepAlive);
38
+ }
39
+ }, pingInterval);
40
+ ping.unref?.();
41
+ const allowedOrigin = (request) => {
42
+ const origin = request.headers.origin;
43
+ if (!origin)
44
+ return true;
45
+ if (typeof options.origins === 'function')
46
+ return options.origins(origin, request);
47
+ if (options.origins)
48
+ return options.origins.includes(origin);
49
+ try {
50
+ return new URL(origin).host === request.headers.host;
51
+ }
52
+ catch {
53
+ return false;
54
+ }
55
+ };
56
+ const refuse = (socket, status) => {
57
+ if (socket.destroyed)
58
+ return;
59
+ socket.end(`HTTP/1.1 ${status} ${REFUSALS[status]}\r\nConnection: close\r\nContent-Length: 0\r\n\r\n`);
60
+ };
61
+ const accept = async (request, socket, head) => {
62
+ if (closing || wss.clients.size + pending >= maxConnections)
63
+ return refuse(socket, 503);
64
+ if (!allowedOrigin(request))
65
+ return refuse(socket, 403);
66
+ pending += 1;
67
+ let context;
68
+ try {
69
+ context = await options.authenticate(request);
70
+ }
71
+ catch (error) {
72
+ console.error(error);
73
+ context = null;
74
+ }
75
+ finally {
76
+ pending -= 1;
77
+ }
78
+ if (context === null)
79
+ return refuse(socket, 401);
80
+ if (closing || socket.destroyed)
81
+ return refuse(socket, 503);
82
+ wss.handleUpgrade(request, socket, head, (ws) => {
83
+ alive.add(ws);
84
+ ws.on('pong', () => alive.add(ws));
85
+ // A protocol error (an oversized or malformed frame) closes the socket on its own; without
86
+ // a listener the error would end the process.
87
+ ws.on('error', () => { });
88
+ if (options.maxAge) {
89
+ const timer = setTimeout(() => ws.close(CLOSE_REAUTHENTICATE, 'reauthenticate'), options.maxAge);
90
+ timer.unref?.();
91
+ ws.once('close', () => clearTimeout(timer));
92
+ }
93
+ options.onConnection(wrap(ws, context, maxBuffered));
94
+ });
95
+ };
96
+ return {
97
+ upgrade(request, socket, head) {
98
+ const path = new URL(request.url ?? '/', 'http://localhost').pathname;
99
+ if (path !== options.path)
100
+ return false;
101
+ void accept(request, socket, head);
102
+ return true;
103
+ },
104
+ size: () => wss.clients.size,
105
+ close(timeout = 5000) {
106
+ closing = true;
107
+ clearInterval(ping);
108
+ const open = [...wss.clients];
109
+ for (const ws of open)
110
+ ws.close(1001, 'server shutting down');
111
+ return new Promise((resolve) => {
112
+ const timer = setTimeout(() => {
113
+ for (const ws of wss.clients)
114
+ ws.terminate();
115
+ }, timeout);
116
+ timer.unref?.();
117
+ const done = () => {
118
+ if (wss.clients.size)
119
+ return;
120
+ clearTimeout(timer);
121
+ wss.close(() => resolve());
122
+ };
123
+ for (const ws of open)
124
+ ws.once('close', done);
125
+ done();
126
+ });
127
+ },
128
+ };
129
+ }
130
+ function wrap(ws, context, maxBuffered) {
131
+ return {
132
+ context,
133
+ get buffered() {
134
+ return ws.bufferedAmount;
135
+ },
136
+ send(data) {
137
+ if (ws.readyState !== ws.OPEN)
138
+ return 'closed';
139
+ if (ws.bufferedAmount > maxBuffered)
140
+ return 'buffer-full';
141
+ ws.send(data);
142
+ return 'sent';
143
+ },
144
+ drained(bytes = maxBuffered / 2) {
145
+ return new Promise((resolve) => {
146
+ const check = () => {
147
+ if (ws.readyState !== ws.OPEN || ws.bufferedAmount <= bytes)
148
+ return resolve();
149
+ setTimeout(check, DRAIN_POLL_MS);
150
+ };
151
+ check();
152
+ });
153
+ },
154
+ close(code = 1000, reason = '') {
155
+ ws.close(code, reason);
156
+ },
157
+ on(event, handler) {
158
+ const listener = event === 'message'
159
+ ? (data, isBinary) => {
160
+ const bytes = Array.isArray(data) ? Buffer.concat(data) : Buffer.from(data);
161
+ handler(isBinary ? new Uint8Array(bytes) : bytes.toString('utf8'));
162
+ }
163
+ : (code, reason) => handler(code, reason.toString('utf8'));
164
+ ws.on(event, listener);
165
+ return () => void ws.off(event, listener);
166
+ },
167
+ };
168
+ }
@@ -0,0 +1,8 @@
1
+ import {
2
+ CLOSE_REAUTHENTICATE,
3
+ createWebSocketServer
4
+ } from "./chunk-GAAZQJJ4.mjs";
5
+ export {
6
+ CLOSE_REAUTHENTICATE,
7
+ createWebSocketServer
8
+ };