@freedome/client 0.0.0-stage → 1.0.0
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/CHANGELOG.md +5 -0
- package/LICENSE +289 -0
- package/README.md +20 -2
- package/dist/chunk-CAEBM4AN.js +180 -0
- package/dist/chunk-HYGH4JIY.js +30 -0
- package/dist/chunk-J7A5LF32.js +180 -0
- package/dist/chunk-K3G5AG65.js +211 -0
- package/dist/chunk-YB5V2MMF.js +81 -0
- package/dist/codecs.cjs +1608 -0
- package/dist/codecs.d.cts +204 -0
- package/dist/codecs.d.ts +204 -0
- package/dist/codecs.js +1364 -0
- package/dist/discovery-BsEQfHam.d.cts +419 -0
- package/dist/discovery-DTV9yp9S.d.ts +419 -0
- package/dist/errors-L_bIF-OR.d.cts +108 -0
- package/dist/errors-L_bIF-OR.d.ts +108 -0
- package/dist/index.cjs +4753 -0
- package/dist/index.d.cts +1124 -0
- package/dist/index.d.ts +1124 -0
- package/dist/index.js +4084 -0
- package/dist/input-CxW5zgc-.d.cts +27 -0
- package/dist/input-CxW5zgc-.d.ts +27 -0
- package/dist/node.cjs +465 -0
- package/dist/node.d.cts +31 -0
- package/dist/node.d.ts +31 -0
- package/dist/node.js +136 -0
- package/dist/testing.cjs +1622 -0
- package/dist/testing.d.cts +184 -0
- package/dist/testing.d.ts +184 -0
- package/dist/testing.js +1251 -0
- package/dist/transport-dvmDQyvB.d.cts +130 -0
- package/dist/transport-dvmDQyvB.d.ts +130 -0
- package/package.json +78 -3
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
// src/sha256.ts
|
|
2
|
+
var K = new Uint32Array([
|
|
3
|
+
1116352408,
|
|
4
|
+
1899447441,
|
|
5
|
+
3049323471,
|
|
6
|
+
3921009573,
|
|
7
|
+
961987163,
|
|
8
|
+
1508970993,
|
|
9
|
+
2453635748,
|
|
10
|
+
2870763221,
|
|
11
|
+
3624381080,
|
|
12
|
+
310598401,
|
|
13
|
+
607225278,
|
|
14
|
+
1426881987,
|
|
15
|
+
1925078388,
|
|
16
|
+
2162078206,
|
|
17
|
+
2614888103,
|
|
18
|
+
3248222580,
|
|
19
|
+
3835390401,
|
|
20
|
+
4022224774,
|
|
21
|
+
264347078,
|
|
22
|
+
604807628,
|
|
23
|
+
770255983,
|
|
24
|
+
1249150122,
|
|
25
|
+
1555081692,
|
|
26
|
+
1996064986,
|
|
27
|
+
2554220882,
|
|
28
|
+
2821834349,
|
|
29
|
+
2952996808,
|
|
30
|
+
3210313671,
|
|
31
|
+
3336571891,
|
|
32
|
+
3584528711,
|
|
33
|
+
113926993,
|
|
34
|
+
338241895,
|
|
35
|
+
666307205,
|
|
36
|
+
773529912,
|
|
37
|
+
1294757372,
|
|
38
|
+
1396182291,
|
|
39
|
+
1695183700,
|
|
40
|
+
1986661051,
|
|
41
|
+
2177026350,
|
|
42
|
+
2456956037,
|
|
43
|
+
2730485921,
|
|
44
|
+
2820302411,
|
|
45
|
+
3259730800,
|
|
46
|
+
3345764771,
|
|
47
|
+
3516065817,
|
|
48
|
+
3600352804,
|
|
49
|
+
4094571909,
|
|
50
|
+
275423344,
|
|
51
|
+
430227734,
|
|
52
|
+
506948616,
|
|
53
|
+
659060556,
|
|
54
|
+
883997877,
|
|
55
|
+
958139571,
|
|
56
|
+
1322822218,
|
|
57
|
+
1537002063,
|
|
58
|
+
1747873779,
|
|
59
|
+
1955562222,
|
|
60
|
+
2024104815,
|
|
61
|
+
2227730452,
|
|
62
|
+
2361852424,
|
|
63
|
+
2428436474,
|
|
64
|
+
2756734187,
|
|
65
|
+
3204031479,
|
|
66
|
+
3329325298
|
|
67
|
+
]);
|
|
68
|
+
var Sha256 = class {
|
|
69
|
+
h = new Uint32Array([
|
|
70
|
+
1779033703,
|
|
71
|
+
3144134277,
|
|
72
|
+
1013904242,
|
|
73
|
+
2773480762,
|
|
74
|
+
1359893119,
|
|
75
|
+
2600822924,
|
|
76
|
+
528734635,
|
|
77
|
+
1541459225
|
|
78
|
+
]);
|
|
79
|
+
buf = new Uint8Array(64);
|
|
80
|
+
w = new Uint32Array(64);
|
|
81
|
+
bufLen = 0;
|
|
82
|
+
total = 0;
|
|
83
|
+
finished = false;
|
|
84
|
+
update(data) {
|
|
85
|
+
if (this.finished) throw new Error("sha256: already finished");
|
|
86
|
+
let i = 0;
|
|
87
|
+
this.total += data.length;
|
|
88
|
+
if (this.bufLen) {
|
|
89
|
+
const n = Math.min(64 - this.bufLen, data.length);
|
|
90
|
+
this.buf.set(data.subarray(0, n), this.bufLen);
|
|
91
|
+
this.bufLen += n;
|
|
92
|
+
i = n;
|
|
93
|
+
if (this.bufLen === 64) {
|
|
94
|
+
this.block(this.buf, 0);
|
|
95
|
+
this.bufLen = 0;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
for (; i + 64 <= data.length; i += 64) this.block(data, i);
|
|
99
|
+
if (i < data.length) {
|
|
100
|
+
this.buf.set(data.subarray(i), 0);
|
|
101
|
+
this.bufLen = data.length - i;
|
|
102
|
+
}
|
|
103
|
+
return this;
|
|
104
|
+
}
|
|
105
|
+
digest() {
|
|
106
|
+
if (!this.finished) {
|
|
107
|
+
const bits = this.total * 8;
|
|
108
|
+
const pad = new Uint8Array((this.bufLen < 56 ? 56 : 120) - this.bufLen + 8);
|
|
109
|
+
pad[0] = 128;
|
|
110
|
+
const dv2 = new DataView(pad.buffer);
|
|
111
|
+
dv2.setUint32(pad.length - 8, Math.floor(bits / 2 ** 32));
|
|
112
|
+
dv2.setUint32(pad.length - 4, bits >>> 0);
|
|
113
|
+
const t = this.total;
|
|
114
|
+
this.update(pad);
|
|
115
|
+
this.total = t;
|
|
116
|
+
this.finished = true;
|
|
117
|
+
}
|
|
118
|
+
const out = new Uint8Array(32);
|
|
119
|
+
const dv = new DataView(out.buffer);
|
|
120
|
+
for (let j = 0; j < 8; j++) dv.setUint32(j * 4, this.h[j]);
|
|
121
|
+
return out;
|
|
122
|
+
}
|
|
123
|
+
hex() {
|
|
124
|
+
return Array.from(this.digest(), (b) => b.toString(16).padStart(2, "0")).join("");
|
|
125
|
+
}
|
|
126
|
+
block(d, o) {
|
|
127
|
+
const w = this.w;
|
|
128
|
+
for (let j = 0; j < 16; j++)
|
|
129
|
+
w[j] = d[o + j * 4] << 24 | d[o + j * 4 + 1] << 16 | d[o + j * 4 + 2] << 8 | d[o + j * 4 + 3];
|
|
130
|
+
for (let j = 16; j < 64; j++) {
|
|
131
|
+
const a2 = w[j - 15];
|
|
132
|
+
const b2 = w[j - 2];
|
|
133
|
+
const s0 = (a2 >>> 7 | a2 << 25) ^ (a2 >>> 18 | a2 << 14) ^ a2 >>> 3;
|
|
134
|
+
const s1 = (b2 >>> 17 | b2 << 15) ^ (b2 >>> 19 | b2 << 13) ^ b2 >>> 10;
|
|
135
|
+
w[j] = w[j - 16] + s0 + w[j - 7] + s1 | 0;
|
|
136
|
+
}
|
|
137
|
+
let [a, b, c, dd, e, f, g, h] = this.h;
|
|
138
|
+
for (let j = 0; j < 64; j++) {
|
|
139
|
+
const S1 = (e >>> 6 | e << 26) ^ (e >>> 11 | e << 21) ^ (e >>> 25 | e << 7);
|
|
140
|
+
const ch = e & f ^ ~e & g;
|
|
141
|
+
const t1 = h + S1 + ch + K[j] + w[j] | 0;
|
|
142
|
+
const S0 = (a >>> 2 | a << 30) ^ (a >>> 13 | a << 19) ^ (a >>> 22 | a << 10);
|
|
143
|
+
const maj = a & b ^ a & c ^ b & c;
|
|
144
|
+
const t2 = S0 + maj | 0;
|
|
145
|
+
h = g;
|
|
146
|
+
g = f;
|
|
147
|
+
f = e;
|
|
148
|
+
e = dd + t1 | 0;
|
|
149
|
+
dd = c;
|
|
150
|
+
c = b;
|
|
151
|
+
b = a;
|
|
152
|
+
a = t1 + t2 | 0;
|
|
153
|
+
}
|
|
154
|
+
const H = this.h;
|
|
155
|
+
H[0] = H[0] + a | 0;
|
|
156
|
+
H[1] = H[1] + b | 0;
|
|
157
|
+
H[2] = H[2] + c | 0;
|
|
158
|
+
H[3] = H[3] + dd | 0;
|
|
159
|
+
H[4] = H[4] + e | 0;
|
|
160
|
+
H[5] = H[5] + f | 0;
|
|
161
|
+
H[6] = H[6] + g | 0;
|
|
162
|
+
H[7] = H[7] + h | 0;
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
var sha256Hex = (data) => new Sha256().update(data).hex();
|
|
166
|
+
function base64(bytes) {
|
|
167
|
+
const abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
168
|
+
let s = "";
|
|
169
|
+
for (let i = 0; i < bytes.length; i += 3) {
|
|
170
|
+
const n = bytes[i] << 16 | (bytes[i + 1] ?? 0) << 8 | (bytes[i + 2] ?? 0);
|
|
171
|
+
s += abc[n >> 18 & 63] + abc[n >> 12 & 63] + (i + 1 < bytes.length ? abc[n >> 6 & 63] : "=") + (i + 2 < bytes.length ? abc[n & 63] : "=");
|
|
172
|
+
}
|
|
173
|
+
return s;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export {
|
|
177
|
+
Sha256,
|
|
178
|
+
sha256Hex,
|
|
179
|
+
base64
|
|
180
|
+
};
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import {
|
|
2
|
+
NetworkError,
|
|
3
|
+
RequestTimeoutError
|
|
4
|
+
} from "./chunk-CAEBM4AN.js";
|
|
5
|
+
|
|
6
|
+
// src/transport.ts
|
|
7
|
+
var FreedomeResponse = class _FreedomeResponse {
|
|
8
|
+
constructor(status, headers = {}, body = new Uint8Array(0)) {
|
|
9
|
+
this.status = status;
|
|
10
|
+
this.headers = headers;
|
|
11
|
+
this.body = body;
|
|
12
|
+
}
|
|
13
|
+
status;
|
|
14
|
+
headers;
|
|
15
|
+
body;
|
|
16
|
+
get ok() {
|
|
17
|
+
return this.status >= 200 && this.status < 300;
|
|
18
|
+
}
|
|
19
|
+
get text() {
|
|
20
|
+
return new TextDecoder().decode(this.body);
|
|
21
|
+
}
|
|
22
|
+
/** Parsed JSON body (`undefined` when empty). */
|
|
23
|
+
get json() {
|
|
24
|
+
if (this.body.length === 0) return void 0;
|
|
25
|
+
return JSON.parse(this.text);
|
|
26
|
+
}
|
|
27
|
+
static json(status, value, headers = {}) {
|
|
28
|
+
return new _FreedomeResponse(
|
|
29
|
+
status,
|
|
30
|
+
{ "content-type": "application/json", ...headers },
|
|
31
|
+
new TextEncoder().encode(JSON.stringify(value))
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
function requestUrl(endpoint, r) {
|
|
36
|
+
const base = r.rootRelative ? endpoint.baseUrl : endpoint.apiBase;
|
|
37
|
+
const q = r.query && Object.keys(r.query).length ? `?${new URLSearchParams(r.query).toString()}` : "";
|
|
38
|
+
return `${base}${r.path}${q}`;
|
|
39
|
+
}
|
|
40
|
+
var FetchTransport = class {
|
|
41
|
+
constructor(endpoint, fetchImpl = globalThis.fetch.bind(globalThis)) {
|
|
42
|
+
this.endpoint = endpoint;
|
|
43
|
+
this.fetchImpl = fetchImpl;
|
|
44
|
+
}
|
|
45
|
+
endpoint;
|
|
46
|
+
fetchImpl;
|
|
47
|
+
controllers = /* @__PURE__ */ new Set();
|
|
48
|
+
async send(r, timeoutMs) {
|
|
49
|
+
const ac = new AbortController();
|
|
50
|
+
this.controllers.add(ac);
|
|
51
|
+
let timedOut = false;
|
|
52
|
+
const timer = setTimeout(() => {
|
|
53
|
+
timedOut = true;
|
|
54
|
+
ac.abort();
|
|
55
|
+
}, timeoutMs);
|
|
56
|
+
try {
|
|
57
|
+
const res = await this.fetchImpl(requestUrl(this.endpoint, r), {
|
|
58
|
+
method: r.method,
|
|
59
|
+
headers: r.headers,
|
|
60
|
+
...r.body ? { body: r.body } : {},
|
|
61
|
+
signal: ac.signal
|
|
62
|
+
});
|
|
63
|
+
const body = new Uint8Array(await res.arrayBuffer());
|
|
64
|
+
const headers = {};
|
|
65
|
+
res.headers.forEach((v, k) => headers[k.toLowerCase()] = v);
|
|
66
|
+
return new FreedomeResponse(res.status, headers, body);
|
|
67
|
+
} catch (e) {
|
|
68
|
+
if (timedOut)
|
|
69
|
+
throw new RequestTimeoutError(
|
|
70
|
+
`${r.method} ${r.path}: no answer within ${timeoutMs} ms`,
|
|
71
|
+
timeoutMs
|
|
72
|
+
);
|
|
73
|
+
throw new NetworkError(`${r.method} ${r.path}: ${e?.message ?? e}`, { cause: e });
|
|
74
|
+
} finally {
|
|
75
|
+
clearTimeout(timer);
|
|
76
|
+
this.controllers.delete(ac);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
async close() {
|
|
80
|
+
for (const c of this.controllers) c.abort();
|
|
81
|
+
this.controllers.clear();
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
// src/endpoint.ts
|
|
86
|
+
var FreedomeEndpoint = class _FreedomeEndpoint {
|
|
87
|
+
constructor(baseUrl, opts = {}) {
|
|
88
|
+
this.opts = opts;
|
|
89
|
+
const u = new URL(String(baseUrl));
|
|
90
|
+
this.baseUrl = `${u.protocol}//${u.host}`;
|
|
91
|
+
}
|
|
92
|
+
opts;
|
|
93
|
+
baseUrl;
|
|
94
|
+
/** `host[:port]`, `http(s)://…` or a `freedome://pair?…` link (default port 8080 / 443). */
|
|
95
|
+
static parse(text) {
|
|
96
|
+
const t = text.trim();
|
|
97
|
+
if (t.startsWith("freedome://")) return PairingLink.parse(t).endpoint;
|
|
98
|
+
const u = new URL(t.includes("://") ? t : `http://${t}`);
|
|
99
|
+
if (u.protocol !== "http:" && u.protocol !== "https:")
|
|
100
|
+
throw new TypeError(`unsupported scheme "${u.protocol}"`);
|
|
101
|
+
if (!u.hostname) throw new TypeError("no host");
|
|
102
|
+
if (!u.port) u.port = u.protocol === "https:" ? "443" : "8080";
|
|
103
|
+
return new _FreedomeEndpoint(u);
|
|
104
|
+
}
|
|
105
|
+
get id() {
|
|
106
|
+
return this.opts.id;
|
|
107
|
+
}
|
|
108
|
+
get tlsFingerprint() {
|
|
109
|
+
return this.opts.tlsFingerprint;
|
|
110
|
+
}
|
|
111
|
+
get pairingCode() {
|
|
112
|
+
return this.opts.pairingCode;
|
|
113
|
+
}
|
|
114
|
+
/** `<base>/api/v1`. */
|
|
115
|
+
get apiBase() {
|
|
116
|
+
return `${this.baseUrl}/api/v1`;
|
|
117
|
+
}
|
|
118
|
+
/** WebSocket base (`ws://` / `wss://`). */
|
|
119
|
+
get wsBase() {
|
|
120
|
+
return this.baseUrl.replace(/^http/, "ws");
|
|
121
|
+
}
|
|
122
|
+
/** Key of the stored tokens / caches (server instance id once known). */
|
|
123
|
+
get storageKey() {
|
|
124
|
+
return this.opts.id ?? this.baseUrl;
|
|
125
|
+
}
|
|
126
|
+
withId(id) {
|
|
127
|
+
return new _FreedomeEndpoint(this.baseUrl, { ...this.opts, id });
|
|
128
|
+
}
|
|
129
|
+
toString() {
|
|
130
|
+
return `FreedomeEndpoint(${this.baseUrl}${this.opts.id ? `, id: ${this.opts.id}` : ""})`;
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
var HOST = /^(?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?(?:\.[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?)*|\[[0-9A-Fa-f:.]+\]|[0-9A-Fa-f:]+:[0-9A-Fa-f:.]*)$/;
|
|
134
|
+
var PairingLink = class _PairingLink {
|
|
135
|
+
constructor(host, port = 8080, opts = {}) {
|
|
136
|
+
this.host = host;
|
|
137
|
+
this.port = port;
|
|
138
|
+
this.opts = opts;
|
|
139
|
+
}
|
|
140
|
+
host;
|
|
141
|
+
port;
|
|
142
|
+
opts;
|
|
143
|
+
static parse(text) {
|
|
144
|
+
let u;
|
|
145
|
+
try {
|
|
146
|
+
u = new URL(text.trim());
|
|
147
|
+
} catch {
|
|
148
|
+
throw new TypeError("not a pairing link");
|
|
149
|
+
}
|
|
150
|
+
if (u.protocol !== "freedome:" || u.hostname !== "pair" && u.host !== "pair")
|
|
151
|
+
throw new TypeError("not a freedome://pair link");
|
|
152
|
+
const q = u.searchParams;
|
|
153
|
+
const host = q.get("host");
|
|
154
|
+
if (!host || !HOST.test(host)) throw new TypeError("pairing link: bad or missing host");
|
|
155
|
+
const portText = q.get("port");
|
|
156
|
+
const port = portText === null ? 8080 : Number(portText);
|
|
157
|
+
if (!Number.isInteger(port) || port < 1 || port > 65535)
|
|
158
|
+
throw new TypeError("pairing link: bad port");
|
|
159
|
+
const id = q.get("id");
|
|
160
|
+
if (id !== null && !/^[A-Za-z0-9._-]{1,64}$/.test(id))
|
|
161
|
+
throw new TypeError("pairing link: bad id");
|
|
162
|
+
const code = q.get("code");
|
|
163
|
+
if (code !== null && !/^[0-9]{6}$/.test(code))
|
|
164
|
+
throw new TypeError("pairing link: code must be 6 digits");
|
|
165
|
+
const fp = q.get("fp");
|
|
166
|
+
if (fp !== null && !/^(?:[0-9A-Fa-f]{2}:?){8,32}$/.test(fp))
|
|
167
|
+
throw new TypeError("pairing link: fp must be \u2265 16 hex digits");
|
|
168
|
+
const name = q.get("name");
|
|
169
|
+
if (name !== null && name.length > 128) throw new TypeError("pairing link: name too long");
|
|
170
|
+
return new _PairingLink(host, port, {
|
|
171
|
+
...id !== null ? { id } : {},
|
|
172
|
+
...code !== null ? { code } : {},
|
|
173
|
+
...fp !== null ? { fingerprint: fp } : {},
|
|
174
|
+
tls: q.get("tls") === "1" || fp !== null,
|
|
175
|
+
...name !== null ? { name } : {}
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
static tryParse(text) {
|
|
179
|
+
try {
|
|
180
|
+
return _PairingLink.parse(text);
|
|
181
|
+
} catch {
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
get endpoint() {
|
|
186
|
+
const h = this.host.includes(":") && !this.host.startsWith("[") ? `[${this.host}]` : this.host;
|
|
187
|
+
return new FreedomeEndpoint(`${this.opts.tls ? "https" : "http"}://${h}:${this.port}`, {
|
|
188
|
+
...this.opts.id ? { id: this.opts.id } : {},
|
|
189
|
+
...this.opts.name ? { name: this.opts.name } : {},
|
|
190
|
+
...this.opts.fingerprint ? { tlsFingerprint: this.opts.fingerprint } : {},
|
|
191
|
+
...this.opts.code ? { pairingCode: this.opts.code } : {}
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
toString() {
|
|
195
|
+
const q = new URLSearchParams({ host: this.host, port: String(this.port) });
|
|
196
|
+
if (this.opts.id) q.set("id", this.opts.id);
|
|
197
|
+
if (this.opts.code) q.set("code", this.opts.code);
|
|
198
|
+
if (this.opts.fingerprint) q.set("fp", this.opts.fingerprint);
|
|
199
|
+
else if (this.opts.tls) q.set("tls", "1");
|
|
200
|
+
if (this.opts.name) q.set("name", this.opts.name);
|
|
201
|
+
return `freedome://pair?${q.toString()}`;
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
export {
|
|
206
|
+
FreedomeResponse,
|
|
207
|
+
requestUrl,
|
|
208
|
+
FetchTransport,
|
|
209
|
+
FreedomeEndpoint,
|
|
210
|
+
PairingLink
|
|
211
|
+
};
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import {
|
|
2
|
+
NetworkError,
|
|
3
|
+
RequestTimeoutError,
|
|
4
|
+
UnauthorizedError
|
|
5
|
+
} from "./chunk-CAEBM4AN.js";
|
|
6
|
+
|
|
7
|
+
// src/realtime/socket.ts
|
|
8
|
+
function webSocketConnector(impl) {
|
|
9
|
+
return (endpoint, path, protocols, timeoutMs) => new Promise((resolve, reject) => {
|
|
10
|
+
const Ctor = impl ?? globalThis.WebSocket;
|
|
11
|
+
if (!Ctor) {
|
|
12
|
+
reject(
|
|
13
|
+
new NetworkError(
|
|
14
|
+
"no WebSocket implementation (Node < 22: use createNodeTransports from @freedome/client/node)"
|
|
15
|
+
)
|
|
16
|
+
);
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
let ws;
|
|
20
|
+
try {
|
|
21
|
+
ws = new Ctor(`${endpoint.wsBase}${path}`, protocols);
|
|
22
|
+
} catch (e) {
|
|
23
|
+
reject(new NetworkError(`websocket: ${e.message}`, { cause: e }));
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
let settled = false;
|
|
27
|
+
const timer = setTimeout(() => {
|
|
28
|
+
if (settled) return;
|
|
29
|
+
settled = true;
|
|
30
|
+
try {
|
|
31
|
+
ws.close();
|
|
32
|
+
} catch {
|
|
33
|
+
}
|
|
34
|
+
reject(
|
|
35
|
+
new RequestTimeoutError(`websocket ${path}: not open within ${timeoutMs} ms`, timeoutMs)
|
|
36
|
+
);
|
|
37
|
+
}, timeoutMs);
|
|
38
|
+
let onMsg;
|
|
39
|
+
let onClose;
|
|
40
|
+
const buffered = [];
|
|
41
|
+
ws.onmessage = (ev) => {
|
|
42
|
+
const t = typeof ev.data === "string" ? ev.data : new TextDecoder().decode(ev.data);
|
|
43
|
+
if (onMsg) onMsg(t);
|
|
44
|
+
else buffered.push(t);
|
|
45
|
+
};
|
|
46
|
+
ws.onclose = (ev) => {
|
|
47
|
+
clearTimeout(timer);
|
|
48
|
+
if (!settled) {
|
|
49
|
+
settled = true;
|
|
50
|
+
reject(
|
|
51
|
+
ev.code === 4401 || ev.code === 4403 ? new UnauthorizedError(`websocket ${path}: ${ev.reason || "unauthorized"}`) : new NetworkError(
|
|
52
|
+
`websocket ${path} closed during handshake (${ev.code ?? "network"})`
|
|
53
|
+
)
|
|
54
|
+
);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
onClose?.(ev.code, ev.reason || void 0);
|
|
58
|
+
};
|
|
59
|
+
ws.onerror = () => {
|
|
60
|
+
};
|
|
61
|
+
ws.onopen = () => {
|
|
62
|
+
clearTimeout(timer);
|
|
63
|
+
if (settled) return;
|
|
64
|
+
settled = true;
|
|
65
|
+
resolve({
|
|
66
|
+
protocol: ws.protocol,
|
|
67
|
+
send: (t) => ws.send(t),
|
|
68
|
+
close: (code, reason) => ws.close(code ?? 1e3, reason),
|
|
69
|
+
listen(m, c) {
|
|
70
|
+
onMsg = m;
|
|
71
|
+
onClose = c;
|
|
72
|
+
for (const t of buffered.splice(0)) m(t);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
};
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export {
|
|
80
|
+
webSocketConnector
|
|
81
|
+
};
|