@fluixi/realtime 0.0.0-stage → 1.0.0-alpha.109
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 +24 -2
- package/dist/bus.cjs +127 -0
- package/dist/bus.d.ts +58 -0
- package/dist/bus.d.ts.map +1 -0
- package/dist/bus.js +118 -0
- package/dist/bus.mjs +8 -0
- package/dist/channel-protocol.cjs +42 -0
- package/dist/channel-protocol.d.ts +40 -0
- package/dist/channel-protocol.d.ts.map +1 -0
- package/dist/channel-protocol.js +24 -0
- package/dist/channel-protocol.mjs +8 -0
- package/dist/channel-server.cjs +291 -0
- package/dist/channel-server.d.ts +29 -0
- package/dist/channel-server.d.ts.map +1 -0
- package/dist/channel-server.js +122 -0
- package/dist/channel-server.mjs +9 -0
- package/dist/channels.cjs +426 -0
- package/dist/channels.d.ts +38 -0
- package/dist/channels.d.ts.map +1 -0
- package/dist/channels.js +82 -0
- package/dist/channels.mjs +9 -0
- package/dist/chunk-4CEAXWJN.mjs +31 -0
- package/dist/chunk-AWS4MP5D.mjs +107 -0
- package/dist/chunk-DNVSSCJA.mjs +22 -0
- package/dist/chunk-GAAZQJJ4.mjs +145 -0
- package/dist/chunk-MGON7IDW.mjs +77 -0
- package/dist/chunk-Q3NDGSX6.mjs +107 -0
- package/dist/chunk-UAZIXYV5.mjs +315 -0
- package/dist/chunk-X73D2DE2.mjs +19 -0
- package/dist/endpoint.cjs +165 -0
- package/dist/endpoint.d.ts +80 -0
- package/dist/endpoint.d.ts.map +1 -0
- package/dist/endpoint.js +168 -0
- package/dist/endpoint.mjs +8 -0
- package/dist/index.cjs +445 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.mjs +29 -0
- package/dist/serializers.cjs +39 -0
- package/dist/serializers.d.ts +12 -0
- package/dist/serializers.d.ts.map +1 -0
- package/dist/serializers.js +16 -0
- package/dist/serializers.mjs +8 -0
- package/dist/server.cjs +385 -0
- package/dist/server.d.ts +5 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +3 -0
- package/dist/server.mjs +20 -0
- package/dist/socket.cjs +362 -0
- package/dist/socket.d.ts +137 -0
- package/dist/socket.d.ts.map +1 -0
- package/dist/socket.js +351 -0
- package/dist/socket.mjs +16 -0
- package/dist/transport.cjs +51 -0
- package/dist/transport.d.ts +33 -0
- package/dist/transport.d.ts.map +1 -0
- package/dist/transport.js +36 -0
- package/dist/transport.mjs +8 -0
- package/dist/tsconfig.lib.tsbuildinfo +1 -0
- package/package.json +65 -3
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,445 @@
|
|
|
1
|
+
/*! @fluixi/realtime v1.0.0-alpha.109 | (c) 2026 Ibrahima Touré and Fluixi contributors | MIT */
|
|
2
|
+
"use strict";
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// src/index.ts
|
|
22
|
+
var index_exports = {};
|
|
23
|
+
__export(index_exports, {
|
|
24
|
+
CONNECTION_STATUSES: () => CONNECTION_STATUSES,
|
|
25
|
+
REALTIME_ERROR_CODES: () => REALTIME_ERROR_CODES,
|
|
26
|
+
RealtimeError: () => RealtimeError,
|
|
27
|
+
backoff: () => backoff,
|
|
28
|
+
createChannelClient: () => createChannelClient,
|
|
29
|
+
createWebSocket: () => createWebSocket,
|
|
30
|
+
globalTransport: () => globalTransport,
|
|
31
|
+
json: () => json,
|
|
32
|
+
raw: () => raw
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(index_exports);
|
|
35
|
+
|
|
36
|
+
// src/socket.ts
|
|
37
|
+
var import_reactive = require("@fluixi/reactive");
|
|
38
|
+
|
|
39
|
+
// src/serializers.ts
|
|
40
|
+
var raw = {
|
|
41
|
+
encode: (message) => message,
|
|
42
|
+
decode: (data) => data
|
|
43
|
+
};
|
|
44
|
+
function json() {
|
|
45
|
+
return {
|
|
46
|
+
encode: (message) => JSON.stringify(message),
|
|
47
|
+
decode: (data) => {
|
|
48
|
+
if (typeof data !== "string") throw new TypeError("json(): received a binary frame");
|
|
49
|
+
return JSON.parse(data);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// src/transport.ts
|
|
55
|
+
function globalTransport() {
|
|
56
|
+
const Native = globalThis.WebSocket;
|
|
57
|
+
return Native ? (url, protocols) => new Native(url, protocols) : void 0;
|
|
58
|
+
}
|
|
59
|
+
function byteSize(data) {
|
|
60
|
+
if (typeof data === "string") {
|
|
61
|
+
return data.length < 1024 ? new TextEncoder().encode(data).length : utf8Length(data);
|
|
62
|
+
}
|
|
63
|
+
if (data instanceof ArrayBuffer || ArrayBuffer.isView(data)) return data.byteLength;
|
|
64
|
+
if (typeof Blob !== "undefined" && data instanceof Blob) return data.size;
|
|
65
|
+
return data.byteLength ?? 0;
|
|
66
|
+
}
|
|
67
|
+
function utf8Length(text) {
|
|
68
|
+
let bytes = 0;
|
|
69
|
+
for (let i = 0; i < text.length; i++) {
|
|
70
|
+
const unit = text.charCodeAt(i);
|
|
71
|
+
if (unit < 128) bytes += 1;
|
|
72
|
+
else if (unit < 2048) bytes += 2;
|
|
73
|
+
else if (unit >= 55296 && unit <= 56319 && i + 1 < text.length) {
|
|
74
|
+
bytes += 4;
|
|
75
|
+
i++;
|
|
76
|
+
} else bytes += 3;
|
|
77
|
+
}
|
|
78
|
+
return bytes;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// src/socket.ts
|
|
82
|
+
var CONNECTION_STATUSES = ["idle", "connecting", "open", "reconnecting", "closing", "closed"];
|
|
83
|
+
var REALTIME_ERROR_CODES = [
|
|
84
|
+
"connect-failed",
|
|
85
|
+
"connect-timeout",
|
|
86
|
+
"connection-error",
|
|
87
|
+
"heartbeat-timeout",
|
|
88
|
+
"message-too-large",
|
|
89
|
+
"decode-failed",
|
|
90
|
+
"reconnect-exhausted"
|
|
91
|
+
];
|
|
92
|
+
var RealtimeError = class extends Error {
|
|
93
|
+
constructor(code, message, options) {
|
|
94
|
+
super(message);
|
|
95
|
+
this.name = "RealtimeError";
|
|
96
|
+
this.code = code;
|
|
97
|
+
if (options && "cause" in options) this.cause = options.cause;
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
var NO_RECONNECT_CODES = /* @__PURE__ */ new Set([1e3, 1008]);
|
|
101
|
+
var CLOSE_TOO_LARGE = 1009;
|
|
102
|
+
var CLOSE_HEARTBEAT = 4e3;
|
|
103
|
+
var CLOSE_TIMEOUT = 4001;
|
|
104
|
+
var BUFFER_POLL_MS = 100;
|
|
105
|
+
function createWebSocket(url, options = {}) {
|
|
106
|
+
const serializer = options.serializer ?? raw;
|
|
107
|
+
const policy = reconnectPolicy(options.reconnect);
|
|
108
|
+
const connectTimeout = options.connectTimeout ?? 1e4;
|
|
109
|
+
const queueLimit = options.queueLimit ?? 100;
|
|
110
|
+
const maxMessageSize = options.maxMessageSize ?? 1024 * 1024;
|
|
111
|
+
const keepLatest = options.keepLatest ?? true;
|
|
112
|
+
const transport = options.transport ?? (typeof document === "undefined" ? void 0 : globalTransport());
|
|
113
|
+
const status = (0, import_reactive.signal)("idle");
|
|
114
|
+
const data = (0, import_reactive.signal)(void 0, { equals: false });
|
|
115
|
+
const error = (0, import_reactive.signal)(void 0);
|
|
116
|
+
const info = (0, import_reactive.signal)({ url: void 0, protocol: "", attempt: 0, closeCode: void 0, closeReason: void 0 });
|
|
117
|
+
const buffered = (0, import_reactive.signal)(0);
|
|
118
|
+
const subscribers = /* @__PURE__ */ new Set();
|
|
119
|
+
const listeners = {
|
|
120
|
+
open: /* @__PURE__ */ new Set(),
|
|
121
|
+
close: /* @__PURE__ */ new Set(),
|
|
122
|
+
error: /* @__PURE__ */ new Set(),
|
|
123
|
+
discard: /* @__PURE__ */ new Set()
|
|
124
|
+
};
|
|
125
|
+
const queue = [];
|
|
126
|
+
let socket;
|
|
127
|
+
let generation = 0;
|
|
128
|
+
let failures = 0;
|
|
129
|
+
let disposed = false;
|
|
130
|
+
let autoConnect = options.connect !== false && !!transport;
|
|
131
|
+
let connectTimer;
|
|
132
|
+
let retryTimer;
|
|
133
|
+
let beatTimer;
|
|
134
|
+
let silenceTimer;
|
|
135
|
+
let bufferTimer;
|
|
136
|
+
const emit = (event, ...args) => {
|
|
137
|
+
for (const listener of [...listeners[event]]) call(listener, args);
|
|
138
|
+
};
|
|
139
|
+
const fail = (code, message, cause) => {
|
|
140
|
+
const failure = new RealtimeError(code, message, cause === void 0 ? void 0 : { cause });
|
|
141
|
+
error.set(failure);
|
|
142
|
+
emit("error", failure);
|
|
143
|
+
};
|
|
144
|
+
const patchInfo = (patch) => info.set((current) => ({ ...current, ...patch }));
|
|
145
|
+
const clearTimers = () => {
|
|
146
|
+
clearTimeout(connectTimer);
|
|
147
|
+
clearInterval(beatTimer);
|
|
148
|
+
clearTimeout(silenceTimer);
|
|
149
|
+
clearTimeout(bufferTimer);
|
|
150
|
+
connectTimer = beatTimer = silenceTimer = bufferTimer = void 0;
|
|
151
|
+
};
|
|
152
|
+
const watchSilence = () => {
|
|
153
|
+
if (!options.heartbeat) return;
|
|
154
|
+
clearTimeout(silenceTimer);
|
|
155
|
+
const timeout = options.heartbeat.timeout ?? options.heartbeat.interval * 2;
|
|
156
|
+
silenceTimer = setTimeout(() => {
|
|
157
|
+
fail("heartbeat-timeout", `No frame for ${timeout} ms`);
|
|
158
|
+
drop(CLOSE_HEARTBEAT, "heartbeat timeout");
|
|
159
|
+
}, timeout);
|
|
160
|
+
};
|
|
161
|
+
const trackBuffer = () => {
|
|
162
|
+
clearTimeout(bufferTimer);
|
|
163
|
+
const amount = socket?.bufferedAmount ?? 0;
|
|
164
|
+
buffered.set(amount);
|
|
165
|
+
if (amount > 0) bufferTimer = setTimeout(trackBuffer, BUFFER_POLL_MS);
|
|
166
|
+
};
|
|
167
|
+
const transmit = (frame) => {
|
|
168
|
+
socket.send(frame);
|
|
169
|
+
trackBuffer();
|
|
170
|
+
};
|
|
171
|
+
const discardQueue = () => {
|
|
172
|
+
if (!queue.length) return;
|
|
173
|
+
const lost2 = queue.splice(0).map((entry) => entry.message);
|
|
174
|
+
emit("discard", lost2);
|
|
175
|
+
};
|
|
176
|
+
const lost = (close) => {
|
|
177
|
+
clearTimers();
|
|
178
|
+
socket = void 0;
|
|
179
|
+
patchInfo({ closeCode: close.code, closeReason: close.reason });
|
|
180
|
+
const retry = policy && !disposed && policy.when(close);
|
|
181
|
+
if (retry && failures < policy.attempts) {
|
|
182
|
+
const delay = backoff(policy, failures);
|
|
183
|
+
failures += 1;
|
|
184
|
+
patchInfo({ attempt: failures });
|
|
185
|
+
status.set("reconnecting");
|
|
186
|
+
emit("close", close);
|
|
187
|
+
retryTimer = setTimeout(open, delay);
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
status.set("closed");
|
|
191
|
+
if (retry) fail("reconnect-exhausted", `Gave up after ${failures} attempts`);
|
|
192
|
+
discardQueue();
|
|
193
|
+
emit("close", close);
|
|
194
|
+
};
|
|
195
|
+
const drop = (code, reason) => {
|
|
196
|
+
const current = socket;
|
|
197
|
+
generation += 1;
|
|
198
|
+
if (current) {
|
|
199
|
+
current.onopen = current.onmessage = current.onerror = current.onclose = null;
|
|
200
|
+
try {
|
|
201
|
+
current.close(code, reason);
|
|
202
|
+
} catch {
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
lost({ code, reason, wasClean: false });
|
|
206
|
+
};
|
|
207
|
+
const open = async () => {
|
|
208
|
+
retryTimer = void 0;
|
|
209
|
+
const attempt = ++generation;
|
|
210
|
+
status.set("connecting");
|
|
211
|
+
let address;
|
|
212
|
+
try {
|
|
213
|
+
address = typeof url === "function" ? await url() : url;
|
|
214
|
+
} catch (cause) {
|
|
215
|
+
if (attempt !== generation) return;
|
|
216
|
+
fail("connect-failed", "Could not get the connection address", cause);
|
|
217
|
+
return lost({ code: 1006, reason: "", wasClean: false });
|
|
218
|
+
}
|
|
219
|
+
if (attempt !== generation) return;
|
|
220
|
+
patchInfo({ url: address });
|
|
221
|
+
let next;
|
|
222
|
+
try {
|
|
223
|
+
next = transport(address, options.protocols);
|
|
224
|
+
} catch (cause) {
|
|
225
|
+
fail("connect-failed", `Could not open ${address}`, cause);
|
|
226
|
+
status.set("closed");
|
|
227
|
+
discardQueue();
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
socket = next;
|
|
231
|
+
next.binaryType = options.binaryType ?? "arraybuffer";
|
|
232
|
+
connectTimer = setTimeout(() => {
|
|
233
|
+
fail("connect-timeout", `Not open after ${connectTimeout} ms`);
|
|
234
|
+
drop(CLOSE_TIMEOUT, "connect timeout");
|
|
235
|
+
}, connectTimeout);
|
|
236
|
+
next.onopen = () => {
|
|
237
|
+
if (attempt !== generation) return;
|
|
238
|
+
clearTimeout(connectTimer);
|
|
239
|
+
failures = 0;
|
|
240
|
+
(0, import_reactive.batch)(() => {
|
|
241
|
+
error.set(void 0);
|
|
242
|
+
patchInfo({ protocol: next.protocol, attempt: 0, closeCode: void 0, closeReason: void 0 });
|
|
243
|
+
status.set("open");
|
|
244
|
+
});
|
|
245
|
+
for (const entry of queue.splice(0)) transmit(entry.frame);
|
|
246
|
+
if (options.heartbeat) {
|
|
247
|
+
const { interval, message } = options.heartbeat;
|
|
248
|
+
if (message !== void 0) beatTimer = setInterval(() => socket && transmit(serializer.encode(message)), interval);
|
|
249
|
+
watchSilence();
|
|
250
|
+
}
|
|
251
|
+
emit("open");
|
|
252
|
+
};
|
|
253
|
+
next.onmessage = (event) => {
|
|
254
|
+
if (attempt !== generation) return;
|
|
255
|
+
watchSilence();
|
|
256
|
+
const frame = event.data;
|
|
257
|
+
if (byteSize(frame) > maxMessageSize) {
|
|
258
|
+
fail("message-too-large", `Received a message over ${maxMessageSize} bytes`);
|
|
259
|
+
return drop(CLOSE_TOO_LARGE, "message too large");
|
|
260
|
+
}
|
|
261
|
+
let message;
|
|
262
|
+
try {
|
|
263
|
+
message = serializer.decode(frame);
|
|
264
|
+
} catch (cause) {
|
|
265
|
+
return fail("decode-failed", "Could not decode a message", cause);
|
|
266
|
+
}
|
|
267
|
+
if (keepLatest) data.set(() => message);
|
|
268
|
+
for (const subscriber of [...subscribers]) call(subscriber, [message]);
|
|
269
|
+
};
|
|
270
|
+
next.onerror = () => attempt === generation && fail("connection-error", "The connection failed");
|
|
271
|
+
next.onclose = (close) => {
|
|
272
|
+
if (attempt !== generation) return;
|
|
273
|
+
lost({ code: close.code, reason: close.reason, wasClean: close.wasClean });
|
|
274
|
+
};
|
|
275
|
+
};
|
|
276
|
+
const connection = {
|
|
277
|
+
status,
|
|
278
|
+
connected: () => status() === "open",
|
|
279
|
+
data,
|
|
280
|
+
error,
|
|
281
|
+
info,
|
|
282
|
+
buffered,
|
|
283
|
+
connect() {
|
|
284
|
+
autoConnect = false;
|
|
285
|
+
if (disposed || !transport) return;
|
|
286
|
+
const now = (0, import_reactive.untrack)(status);
|
|
287
|
+
if (now === "open" || now === "connecting") return;
|
|
288
|
+
if (now === "closing") drop(1e3, "");
|
|
289
|
+
clearTimeout(retryTimer);
|
|
290
|
+
failures = 0;
|
|
291
|
+
void open();
|
|
292
|
+
},
|
|
293
|
+
send(message) {
|
|
294
|
+
const now = (0, import_reactive.untrack)(status);
|
|
295
|
+
if (disposed || now === "idle" && !autoConnect || now === "closing" || now === "closed") return "closed";
|
|
296
|
+
const frame = serializer.encode(message);
|
|
297
|
+
if (byteSize(frame) > maxMessageSize) return "too-large";
|
|
298
|
+
if (now === "open") {
|
|
299
|
+
transmit(frame);
|
|
300
|
+
return "sent";
|
|
301
|
+
}
|
|
302
|
+
if (queue.length >= queueLimit) return "queue-full";
|
|
303
|
+
queue.push({ message, frame });
|
|
304
|
+
return "queued";
|
|
305
|
+
},
|
|
306
|
+
subscribe(handler) {
|
|
307
|
+
subscribers.add(handler);
|
|
308
|
+
const stop = () => void subscribers.delete(handler);
|
|
309
|
+
if ((0, import_reactive.getOwner)()) (0, import_reactive.onCleanup)(stop);
|
|
310
|
+
return stop;
|
|
311
|
+
},
|
|
312
|
+
on(event, handler) {
|
|
313
|
+
const set = listeners[event];
|
|
314
|
+
set.add(handler);
|
|
315
|
+
const stop = () => void set.delete(handler);
|
|
316
|
+
if ((0, import_reactive.getOwner)()) (0, import_reactive.onCleanup)(stop);
|
|
317
|
+
return stop;
|
|
318
|
+
},
|
|
319
|
+
close(code = 1e3, reason = "") {
|
|
320
|
+
autoConnect = false;
|
|
321
|
+
clearTimeout(retryTimer);
|
|
322
|
+
retryTimer = void 0;
|
|
323
|
+
const current = socket;
|
|
324
|
+
generation += 1;
|
|
325
|
+
clearTimers();
|
|
326
|
+
if (!current) {
|
|
327
|
+
if ((0, import_reactive.untrack)(status) !== "idle") status.set("closed");
|
|
328
|
+
discardQueue();
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
status.set("closing");
|
|
332
|
+
current.onopen = current.onmessage = current.onerror = null;
|
|
333
|
+
current.onclose = (close) => {
|
|
334
|
+
socket = void 0;
|
|
335
|
+
patchInfo({ closeCode: close.code, closeReason: close.reason });
|
|
336
|
+
status.set("closed");
|
|
337
|
+
emit("close", close);
|
|
338
|
+
};
|
|
339
|
+
discardQueue();
|
|
340
|
+
current.close(code, reason);
|
|
341
|
+
},
|
|
342
|
+
dispose() {
|
|
343
|
+
if (disposed) return;
|
|
344
|
+
connection.close();
|
|
345
|
+
disposed = true;
|
|
346
|
+
subscribers.clear();
|
|
347
|
+
for (const set of Object.values(listeners)) set.clear();
|
|
348
|
+
}
|
|
349
|
+
};
|
|
350
|
+
if ((0, import_reactive.getOwner)()) (0, import_reactive.onCleanup)(() => connection.dispose());
|
|
351
|
+
if (autoConnect) queueMicrotask(() => autoConnect && connection.connect());
|
|
352
|
+
return connection;
|
|
353
|
+
}
|
|
354
|
+
function reconnectPolicy(option) {
|
|
355
|
+
if (option === false) return void 0;
|
|
356
|
+
const given = option === true || option === void 0 ? {} : option;
|
|
357
|
+
return {
|
|
358
|
+
attempts: given.attempts ?? Infinity,
|
|
359
|
+
delay: given.delay ?? 500,
|
|
360
|
+
maxDelay: given.maxDelay ?? 3e4,
|
|
361
|
+
factor: given.factor ?? 2,
|
|
362
|
+
jitter: given.jitter ?? true,
|
|
363
|
+
when: given.when ?? ((close) => !NO_RECONNECT_CODES.has(close.code))
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
function backoff(policy, failures) {
|
|
367
|
+
const full = Math.min(policy.maxDelay, policy.delay * policy.factor ** failures);
|
|
368
|
+
return policy.jitter ? full / 2 + Math.random() * (full / 2) : full;
|
|
369
|
+
}
|
|
370
|
+
function call(handler, args) {
|
|
371
|
+
try {
|
|
372
|
+
handler(...args);
|
|
373
|
+
} catch (failure) {
|
|
374
|
+
queueMicrotask(() => {
|
|
375
|
+
throw failure;
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// src/channels.ts
|
|
381
|
+
var import_reactive2 = require("@fluixi/reactive");
|
|
382
|
+
function createChannelClient(url, options = {}) {
|
|
383
|
+
const { silenceTimeout = 6e4, ...socketOptions } = options;
|
|
384
|
+
const connection = createWebSocket(url, {
|
|
385
|
+
...socketOptions,
|
|
386
|
+
serializer: json(),
|
|
387
|
+
heartbeat: { interval: silenceTimeout / 2, timeout: silenceTimeout }
|
|
388
|
+
});
|
|
389
|
+
const channels = /* @__PURE__ */ new Map();
|
|
390
|
+
const join = (name, channel) => {
|
|
391
|
+
if (channel.joined || !(0, import_reactive2.untrack)(connection.connected)) return;
|
|
392
|
+
channel.joined = true;
|
|
393
|
+
connection.send(channel.last === void 0 ? { t: "join", c: name } : { t: "join", c: name, since: channel.last });
|
|
394
|
+
};
|
|
395
|
+
connection.on("open", () => {
|
|
396
|
+
for (const [name, channel] of channels) {
|
|
397
|
+
channel.joined = false;
|
|
398
|
+
join(name, channel);
|
|
399
|
+
}
|
|
400
|
+
});
|
|
401
|
+
connection.subscribe((frame) => {
|
|
402
|
+
if (frame.t === "ping" || frame.t === "joined") return;
|
|
403
|
+
const channel = channels.get(frame.c);
|
|
404
|
+
if (!channel) return;
|
|
405
|
+
const subscribers = [...channel.subscribers];
|
|
406
|
+
if (frame.t === "msg") {
|
|
407
|
+
channel.last = frame.m.id;
|
|
408
|
+
for (const subscriber of subscribers) subscriber.handler(frame.m);
|
|
409
|
+
} else if (frame.t === "resync") {
|
|
410
|
+
for (const subscriber of subscribers) subscriber.onResync?.();
|
|
411
|
+
} else if (frame.t === "denied") {
|
|
412
|
+
channels.delete(frame.c);
|
|
413
|
+
for (const subscriber of subscribers) subscriber.onDenied?.();
|
|
414
|
+
}
|
|
415
|
+
});
|
|
416
|
+
const client = {
|
|
417
|
+
status: connection.status,
|
|
418
|
+
connected: connection.connected,
|
|
419
|
+
error: connection.error,
|
|
420
|
+
connection,
|
|
421
|
+
subscribe(name, handler, handlers = {}) {
|
|
422
|
+
let channel = channels.get(name);
|
|
423
|
+
if (!channel) {
|
|
424
|
+
channel = { subscribers: /* @__PURE__ */ new Set(), last: void 0, joined: false };
|
|
425
|
+
channels.set(name, channel);
|
|
426
|
+
}
|
|
427
|
+
const subscriber = { handler, ...handlers };
|
|
428
|
+
channel.subscribers.add(subscriber);
|
|
429
|
+
join(name, channel);
|
|
430
|
+
const stop = () => {
|
|
431
|
+
const current = channels.get(name);
|
|
432
|
+
if (!current?.subscribers.delete(subscriber) || current.subscribers.size) return;
|
|
433
|
+
channels.delete(name);
|
|
434
|
+
if (current.joined && (0, import_reactive2.untrack)(connection.connected)) connection.send({ t: "leave", c: name });
|
|
435
|
+
};
|
|
436
|
+
if ((0, import_reactive2.getOwner)()) (0, import_reactive2.onCleanup)(stop);
|
|
437
|
+
return stop;
|
|
438
|
+
},
|
|
439
|
+
dispose() {
|
|
440
|
+
channels.clear();
|
|
441
|
+
connection.dispose();
|
|
442
|
+
}
|
|
443
|
+
};
|
|
444
|
+
return client;
|
|
445
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { createWebSocket, backoff, RealtimeError, CONNECTION_STATUSES, REALTIME_ERROR_CODES, type ConnectionEvents, type ConnectionInfo, type ConnectionStatus, type Heartbeat, type RealtimeErrorCode, type ReconnectPolicy, type SendResult, type WebSocketConnection, type WebSocketOptions, } from './socket.js';
|
|
2
|
+
export { json, raw, type Serializer } from './serializers.js';
|
|
3
|
+
export { globalTransport, type CloseInfo, type RawData, type SendData, type WebSocketLike, type WebSocketTransport } from './transport.js';
|
|
4
|
+
export { createChannelClient, type ChannelClient, type ChannelClientOptions, type ChannelHandlers } from './channels.js';
|
|
5
|
+
export type { ChannelMessage, ClientFrame, ServerFrame } from './channel-protocol.js';
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EACf,OAAO,EACP,aAAa,EACb,mBAAmB,EACnB,oBAAoB,EACpB,KAAK,gBAAgB,EACrB,KAAK,cAAc,EACnB,KAAK,gBAAgB,EACrB,KAAK,SAAS,EACd,KAAK,iBAAiB,EACtB,KAAK,eAAe,EACpB,KAAK,UAAU,EACf,KAAK,mBAAmB,EACxB,KAAK,gBAAgB,GACtB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,IAAI,EAAE,GAAG,EAAE,KAAK,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9D,OAAO,EAAE,eAAe,EAAE,KAAK,SAAS,EAAE,KAAK,OAAO,EAAE,KAAK,QAAQ,EAAE,KAAK,aAAa,EAAE,KAAK,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AAC3I,OAAO,EAAE,mBAAmB,EAAE,KAAK,aAAa,EAAE,KAAK,oBAAoB,EAAE,KAAK,eAAe,EAAE,MAAM,eAAe,CAAC;AACzH,YAAY,EAAE,cAAc,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAC"}
|
package/dist/index.js
ADDED
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/*! @fluixi/realtime v1.0.0-alpha.109 | (c) 2026 Ibrahima Touré and Fluixi contributors | MIT */
|
|
2
|
+
import {
|
|
3
|
+
createChannelClient
|
|
4
|
+
} from "./chunk-MGON7IDW.mjs";
|
|
5
|
+
import {
|
|
6
|
+
CONNECTION_STATUSES,
|
|
7
|
+
REALTIME_ERROR_CODES,
|
|
8
|
+
RealtimeError,
|
|
9
|
+
backoff,
|
|
10
|
+
createWebSocket
|
|
11
|
+
} from "./chunk-UAZIXYV5.mjs";
|
|
12
|
+
import {
|
|
13
|
+
globalTransport
|
|
14
|
+
} from "./chunk-4CEAXWJN.mjs";
|
|
15
|
+
import {
|
|
16
|
+
json,
|
|
17
|
+
raw
|
|
18
|
+
} from "./chunk-X73D2DE2.mjs";
|
|
19
|
+
export {
|
|
20
|
+
CONNECTION_STATUSES,
|
|
21
|
+
REALTIME_ERROR_CODES,
|
|
22
|
+
RealtimeError,
|
|
23
|
+
backoff,
|
|
24
|
+
createChannelClient,
|
|
25
|
+
createWebSocket,
|
|
26
|
+
globalTransport,
|
|
27
|
+
json,
|
|
28
|
+
raw
|
|
29
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
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/serializers.ts
|
|
21
|
+
var serializers_exports = {};
|
|
22
|
+
__export(serializers_exports, {
|
|
23
|
+
json: () => json,
|
|
24
|
+
raw: () => raw
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(serializers_exports);
|
|
27
|
+
var raw = {
|
|
28
|
+
encode: (message) => message,
|
|
29
|
+
decode: (data) => data
|
|
30
|
+
};
|
|
31
|
+
function json() {
|
|
32
|
+
return {
|
|
33
|
+
encode: (message) => JSON.stringify(message),
|
|
34
|
+
decode: (data) => {
|
|
35
|
+
if (typeof data !== "string") throw new TypeError("json(): received a binary frame");
|
|
36
|
+
return JSON.parse(data);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { RawData, SendData } from './transport.js';
|
|
2
|
+
/** Turns outgoing messages into frames and incoming frames into messages. */
|
|
3
|
+
export interface Serializer<In, Out> {
|
|
4
|
+
encode(message: Out): SendData;
|
|
5
|
+
/** Throwing rejects the frame: the connection reports a `decode-failed` error and stays open. */
|
|
6
|
+
decode(data: RawData): In;
|
|
7
|
+
}
|
|
8
|
+
/** Frames as they are: strings for text, `ArrayBuffer` or `Blob` for binary. The default. */
|
|
9
|
+
export declare const raw: Serializer<RawData, SendData>;
|
|
10
|
+
/** JSON over text frames. A binary frame fails to decode. */
|
|
11
|
+
export declare function json<In = unknown, Out = unknown>(): Serializer<In, Out>;
|
|
12
|
+
//# sourceMappingURL=serializers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serializers.d.ts","sourceRoot":"","sources":["../src/serializers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAExD,6EAA6E;AAC7E,MAAM,WAAW,UAAU,CAAC,EAAE,EAAE,GAAG;IACjC,MAAM,CAAC,OAAO,EAAE,GAAG,GAAG,QAAQ,CAAC;IAC/B,iGAAiG;IACjG,MAAM,CAAC,IAAI,EAAE,OAAO,GAAG,EAAE,CAAC;CAC3B;AAED,6FAA6F;AAC7F,eAAO,MAAM,GAAG,EAAE,UAAU,CAAC,OAAO,EAAE,QAAQ,CAG7C,CAAC;AAEF,6DAA6D;AAC7D,wBAAgB,IAAI,CAAC,EAAE,GAAG,OAAO,EAAE,GAAG,GAAG,OAAO,KAAK,UAAU,CAAC,EAAE,EAAE,GAAG,CAAC,CAQvE"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** Frames as they are: strings for text, `ArrayBuffer` or `Blob` for binary. The default. */
|
|
2
|
+
export const raw = {
|
|
3
|
+
encode: (message) => message,
|
|
4
|
+
decode: (data) => data,
|
|
5
|
+
};
|
|
6
|
+
/** JSON over text frames. A binary frame fails to decode. */
|
|
7
|
+
export function json() {
|
|
8
|
+
return {
|
|
9
|
+
encode: (message) => JSON.stringify(message),
|
|
10
|
+
decode: (data) => {
|
|
11
|
+
if (typeof data !== 'string')
|
|
12
|
+
throw new TypeError('json(): received a binary frame');
|
|
13
|
+
return JSON.parse(data);
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
}
|