@webjsdev/core 0.7.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 +55 -0
- package/index.d.ts +28 -0
- package/index.js +39 -0
- package/package.json +46 -0
- package/src/component.d.ts +120 -0
- package/src/component.js +1061 -0
- package/src/context.js +327 -0
- package/src/css.js +54 -0
- package/src/directives.js +454 -0
- package/src/escape.js +15 -0
- package/src/expose.js +90 -0
- package/src/html.js +30 -0
- package/src/lazy-loader.js +107 -0
- package/src/nav.js +45 -0
- package/src/registry.js +190 -0
- package/src/render-client.js +1717 -0
- package/src/render-server.js +1128 -0
- package/src/repeat.js +37 -0
- package/src/rich-fetch.js +70 -0
- package/src/router-client.js +1434 -0
- package/src/serialize.js +452 -0
- package/src/signal.js +684 -0
- package/src/slot.js +860 -0
- package/src/suspense.js +46 -0
- package/src/task.js +386 -0
- package/src/testing.js +157 -0
- package/src/webjs-frame.js +65 -0
- package/src/websocket-client.js +111 -0
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tiny client-side WebSocket helper with automatic reconnection, JSON
|
|
3
|
+
* parse/stringify, and the webjs CSRF-style matches-origin wss:// scheme
|
|
4
|
+
* rewriting.
|
|
5
|
+
*
|
|
6
|
+
* ```js
|
|
7
|
+
* import { connectWS } from '@webjsdev/core';
|
|
8
|
+
*
|
|
9
|
+
* const conn = connectWS('/api/chat', {
|
|
10
|
+
* onMessage: (msg) => { this.lines = [...this.lines, msg]; },
|
|
11
|
+
* onOpen: () => { this.connected = true; },
|
|
12
|
+
* onClose: () => { this.connected = false; },
|
|
13
|
+
* });
|
|
14
|
+
*
|
|
15
|
+
* conn.send({ type: 'say', text: 'hi' });
|
|
16
|
+
* conn.close();
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* Relative paths (`/api/chat`) are promoted to `ws://` or `wss://` based on
|
|
20
|
+
* `location.protocol`. Absolute `ws(s)://` URLs pass through unchanged.
|
|
21
|
+
*
|
|
22
|
+
* Reconnect: exponential backoff 1s, 2s, 4s, …, capped at 30s. Every
|
|
23
|
+
* successful open resets the counter. Call `.close()` to stop retrying.
|
|
24
|
+
*
|
|
25
|
+
* Messages are parsed as JSON when possible and passed raw (String) when not.
|
|
26
|
+
* Outgoing `send(data)` stringifies objects to JSON; strings and
|
|
27
|
+
* ArrayBuffer/Uint8Array are sent verbatim.
|
|
28
|
+
*
|
|
29
|
+
* @typedef {{
|
|
30
|
+
* onOpen?: (ev: Event) => void,
|
|
31
|
+
* onMessage?: (data: any, ev: MessageEvent) => void,
|
|
32
|
+
* onClose?: (ev: CloseEvent) => void,
|
|
33
|
+
* onError?: (ev: Event) => void,
|
|
34
|
+
* protocols?: string | string[],
|
|
35
|
+
* reconnect?: boolean,
|
|
36
|
+
* }} ConnectOptions
|
|
37
|
+
*
|
|
38
|
+
* @param {string} url
|
|
39
|
+
* @param {ConnectOptions} [opts]
|
|
40
|
+
*/
|
|
41
|
+
export function connectWS(url, opts = {}) {
|
|
42
|
+
const reconnect = opts.reconnect !== false;
|
|
43
|
+
let retries = 0;
|
|
44
|
+
let stopped = false;
|
|
45
|
+
/** @type {WebSocket | null} */
|
|
46
|
+
let ws = null;
|
|
47
|
+
/** @type {any[]} */
|
|
48
|
+
const queue = [];
|
|
49
|
+
|
|
50
|
+
function connect() {
|
|
51
|
+
if (stopped) return;
|
|
52
|
+
ws = new WebSocket(absoluteUrl(url), opts.protocols);
|
|
53
|
+
ws.onopen = (ev) => {
|
|
54
|
+
retries = 0;
|
|
55
|
+
// Flush anything queued while disconnected.
|
|
56
|
+
while (queue.length && ws && ws.readyState === WebSocket.OPEN) {
|
|
57
|
+
ws.send(/** @type any */ (queue.shift()));
|
|
58
|
+
}
|
|
59
|
+
opts.onOpen?.(ev);
|
|
60
|
+
};
|
|
61
|
+
ws.onmessage = (ev) => {
|
|
62
|
+
let data = ev.data;
|
|
63
|
+
if (typeof data === 'string') {
|
|
64
|
+
try { data = JSON.parse(data); } catch { /* leave as string */ }
|
|
65
|
+
}
|
|
66
|
+
opts.onMessage?.(data, ev);
|
|
67
|
+
};
|
|
68
|
+
ws.onerror = (ev) => {
|
|
69
|
+
opts.onError?.(ev);
|
|
70
|
+
};
|
|
71
|
+
ws.onclose = (ev) => {
|
|
72
|
+
opts.onClose?.(ev);
|
|
73
|
+
if (!stopped && reconnect) {
|
|
74
|
+
const delay = Math.min(1000 * 2 ** retries++, 30_000);
|
|
75
|
+
setTimeout(connect, delay);
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
connect();
|
|
81
|
+
|
|
82
|
+
return {
|
|
83
|
+
/** Send a message. Objects are JSON-stringified; strings and binary pass through.
|
|
84
|
+
* If the socket isn't open yet, the message is queued until it is. */
|
|
85
|
+
send(data) {
|
|
86
|
+
const payload =
|
|
87
|
+
typeof data === 'string' || data instanceof ArrayBuffer || ArrayBuffer.isView(data)
|
|
88
|
+
? data
|
|
89
|
+
: JSON.stringify(data);
|
|
90
|
+
if (ws && ws.readyState === WebSocket.OPEN) ws.send(/** @type any */ (payload));
|
|
91
|
+
else queue.push(payload);
|
|
92
|
+
},
|
|
93
|
+
/** Permanently close the socket. Disables reconnect. */
|
|
94
|
+
close(code, reason) {
|
|
95
|
+
stopped = true;
|
|
96
|
+
try { ws?.close(code, reason); } catch {}
|
|
97
|
+
},
|
|
98
|
+
/** Access the underlying socket (may be null while reconnecting). */
|
|
99
|
+
get socket() { return ws; },
|
|
100
|
+
/** Current ready state (0 CONNECTING, 1 OPEN, 2 CLOSING, 3 CLOSED). */
|
|
101
|
+
get readyState() { return ws?.readyState ?? 3; },
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** @param {string} url */
|
|
106
|
+
function absoluteUrl(url) {
|
|
107
|
+
if (/^wss?:\/\//i.test(url)) return url;
|
|
108
|
+
const scheme = typeof location !== 'undefined' && location.protocol === 'https:' ? 'wss:' : 'ws:';
|
|
109
|
+
const host = typeof location !== 'undefined' ? location.host : '';
|
|
110
|
+
return `${scheme}//${host}${url.startsWith('/') ? url : '/' + url}`;
|
|
111
|
+
}
|