agent-yes 1.98.0 → 1.100.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.
@@ -1,97 +1,105 @@
1
- function W() {
1
+ function k() {
2
2
  return crypto.randomUUID();
3
3
  }
4
- var E = 1e4,
5
- T = 1e4;
6
- class U {
4
+ var g = 60000,
5
+ u = 1e4,
6
+ L = 1000,
7
+ b = 120000,
8
+ O = 25000;
9
+ class j {
7
10
  opts;
8
11
  peerId;
9
12
  ws = null;
10
13
  closed = !1;
11
- reconnectDelay = 1000;
14
+ reconnectDelay = L;
12
15
  reconnectTimer = null;
16
+ dormant = !1;
13
17
  heartbeat = null;
14
18
  stableTimer = null;
15
19
  openedAt = 0;
16
- constructor(q) {
17
- this.opts = q;
18
- this.peerId = q.peerId ?? W();
20
+ constructor(z) {
21
+ this.opts = z;
22
+ this.peerId = z.peerId ?? k();
19
23
  }
20
24
  connect() {
21
25
  ((this.closed = !1), this.attachWakeListeners(), this.open());
22
26
  }
23
27
  onWake = () => {
24
28
  if (this.closed) return;
25
- let q = this.ws?.readyState;
26
- if (q === 1) return;
27
- if (q === 0) {
29
+ let z = this.ws?.readyState;
30
+ if (z === 1) return;
31
+ if (z === 0) {
28
32
  try {
29
33
  this.ws?.close();
30
34
  } catch {}
31
35
  return;
32
36
  }
33
- if (this.reconnectTimer != null) (this.clearReconnectTimer(), this.open());
37
+ if (this.dormant || this.reconnectTimer != null)
38
+ ((this.dormant = !1), this.clearReconnectTimer(), this.open());
34
39
  };
40
+ hidden() {
41
+ return globalThis.document?.visibilityState === "hidden";
42
+ }
35
43
  attachWakeListeners() {
36
44
  globalThis.document?.addEventListener("visibilitychange", this.onWake);
37
- let z = globalThis.window;
38
- (z?.addEventListener("focus", this.onWake), z?.addEventListener("online", this.onWake));
45
+ let Q = globalThis.window;
46
+ (Q?.addEventListener("focus", this.onWake), Q?.addEventListener("online", this.onWake));
39
47
  }
40
48
  detachWakeListeners() {
41
49
  globalThis.document?.removeEventListener("visibilitychange", this.onWake);
42
- let z = globalThis.window;
43
- (z?.removeEventListener("focus", this.onWake), z?.removeEventListener("online", this.onWake));
50
+ let Q = globalThis.window;
51
+ (Q?.removeEventListener("focus", this.onWake), Q?.removeEventListener("online", this.onWake));
44
52
  }
45
53
  roomUrl() {
46
54
  return `${this.opts.url.replace(/\/+$/, "")}/room/${encodeURIComponent(this.opts.token)}`;
47
55
  }
48
56
  open() {
49
- let q = new WebSocket(this.roomUrl());
50
- this.ws = q;
51
- let z = setTimeout(() => {
52
- if (q.readyState === 0)
57
+ let z = new WebSocket(this.roomUrl());
58
+ this.ws = z;
59
+ let Q = setTimeout(() => {
60
+ if (z.readyState === 0)
53
61
  try {
54
- q.close();
62
+ z.close();
55
63
  } catch {}
56
- }, T);
57
- ((q.onopen = () => {
58
- (clearTimeout(z),
64
+ }, u);
65
+ ((z.onopen = () => {
66
+ (clearTimeout(Q),
59
67
  (this.openedAt = Date.now()),
60
68
  this.clearStableTimer(),
61
69
  (this.stableTimer = setTimeout(() => {
62
- this.reconnectDelay = 1000;
63
- }, E)));
64
- let K = {
70
+ this.reconnectDelay = L;
71
+ }, g)));
72
+ let Y = {
65
73
  type: "hello",
66
74
  role: this.opts.role,
67
75
  peerId: this.peerId,
68
76
  ...(this.opts.meta ? { meta: this.opts.meta } : {}),
69
77
  };
70
- (q.send(JSON.stringify(K)), this.startHeartbeat(), this.opts.onOpen?.());
78
+ (z.send(JSON.stringify(Y)), this.startHeartbeat(), this.opts.onOpen?.());
71
79
  }),
72
- (q.onmessage = (K) => {
73
- let Q;
80
+ (z.onmessage = (Y) => {
81
+ let Z;
74
82
  try {
75
- Q = JSON.parse(String(K.data));
83
+ Z = JSON.parse(String(Y.data));
76
84
  } catch {
77
85
  return;
78
86
  }
79
- if (Q.type === "peers") this.opts.onPeers?.(Q.peers);
80
- else if (Q.type === "signal") this.opts.onSignal?.(Q.from, Q.data);
87
+ if (Z.type === "peers") this.opts.onPeers?.(Z.peers);
88
+ else if (Z.type === "signal") this.opts.onSignal?.(Z.from, Z.data);
81
89
  }),
82
- (q.onclose = (K) => {
83
- (clearTimeout(z), this.clearStableTimer(), this.stopHeartbeat());
84
- let Q = this.openedAt ? Date.now() - this.openedAt : 0;
90
+ (z.onclose = (Y) => {
91
+ (clearTimeout(Q), this.clearStableTimer(), this.stopHeartbeat());
92
+ let Z = this.openedAt ? Date.now() - this.openedAt : 0;
85
93
  if (
86
94
  ((this.openedAt = 0),
87
- this.opts.onClose?.({ code: K?.code ?? 0, reason: K?.reason ?? "", ms: Q }),
95
+ this.opts.onClose?.({ code: Y?.code ?? 0, reason: Y?.reason ?? "", ms: Z }),
88
96
  !this.closed)
89
97
  )
90
98
  this.scheduleReconnect();
91
99
  }),
92
- (q.onerror = () => {
100
+ (z.onerror = () => {
93
101
  try {
94
- q.close();
102
+ z.close();
95
103
  } catch {}
96
104
  }));
97
105
  }
@@ -101,7 +109,7 @@ class U {
101
109
  try {
102
110
  this.ws?.send(JSON.stringify({ type: "ping" }));
103
111
  } catch {}
104
- }, 1e4)));
112
+ }, O)));
105
113
  }
106
114
  stopHeartbeat() {
107
115
  if (this.heartbeat != null) (clearInterval(this.heartbeat), (this.heartbeat = null));
@@ -110,29 +118,39 @@ class U {
110
118
  if (this.stableTimer != null) (clearTimeout(this.stableTimer), (this.stableTimer = null));
111
119
  }
112
120
  scheduleReconnect() {
113
- let q = this.reconnectDelay;
114
- ((this.reconnectDelay = Math.min(q * 2, 15000)),
121
+ if (this.hidden()) {
122
+ this.dormant = !0;
123
+ return;
124
+ }
125
+ let z = Math.round(this.reconnectDelay * (0.75 + Math.random() * 0.5));
126
+ ((this.reconnectDelay = Math.min(this.reconnectDelay * 2, b)),
115
127
  this.clearReconnectTimer(),
116
128
  (this.reconnectTimer = setTimeout(() => {
117
- if (((this.reconnectTimer = null), !this.closed)) this.open();
118
- }, q)));
129
+ if (((this.reconnectTimer = null), this.closed)) return;
130
+ if (this.hidden()) {
131
+ this.dormant = !0;
132
+ return;
133
+ }
134
+ this.open();
135
+ }, z)));
119
136
  }
120
137
  clearReconnectTimer() {
121
138
  if (this.reconnectTimer != null)
122
139
  (clearTimeout(this.reconnectTimer), (this.reconnectTimer = null));
123
140
  }
124
- sendSignal(q, z) {
125
- let K = { type: "signal", to: q, data: z };
126
- this.ws?.send(JSON.stringify(K));
141
+ sendSignal(z, Q) {
142
+ let Y = { type: "signal", to: z, data: Q };
143
+ this.ws?.send(JSON.stringify(Y));
127
144
  }
128
- updateMeta(q) {
129
- if (((this.opts.meta = q), this.ws?.readyState === 1)) {
130
- let z = { type: "meta", meta: q };
131
- this.ws.send(JSON.stringify(z));
145
+ updateMeta(z) {
146
+ if (((this.opts.meta = z), this.ws?.readyState === 1)) {
147
+ let Q = { type: "meta", meta: z };
148
+ this.ws.send(JSON.stringify(Q));
132
149
  }
133
150
  }
134
151
  close() {
135
152
  ((this.closed = !0),
153
+ (this.dormant = !1),
136
154
  this.detachWakeListeners(),
137
155
  this.clearReconnectTimer(),
138
156
  this.stopHeartbeat(),
@@ -143,20 +161,22 @@ class U {
143
161
  }
144
162
  }
145
163
  var A = ["stun:stun.l.google.com:19302", "stun:stun1.l.google.com:19302"],
146
- _ = "codehost";
164
+ N = "codehost",
165
+ _ = "codehost-bulk";
147
166
  class B {
148
167
  opts;
149
168
  pc;
150
169
  channel = null;
151
- constructor(q) {
152
- this.opts = q;
153
- ((this.pc = new RTCPeerConnection({ iceServers: A.map((z) => ({ urls: z })) })),
154
- (this.pc.onicecandidate = (z) => {
155
- if (z.candidate)
170
+ bulk = null;
171
+ constructor(z) {
172
+ this.opts = z;
173
+ ((this.pc = new RTCPeerConnection({ iceServers: A.map((Q) => ({ urls: Q })) })),
174
+ (this.pc.onicecandidate = (Q) => {
175
+ if (Q.candidate)
156
176
  this.opts.sendSignal({
157
177
  kind: "candidate",
158
- candidate: z.candidate.candidate,
159
- mid: z.candidate.sdpMid ?? "0",
178
+ candidate: Q.candidate.candidate,
179
+ mid: Q.candidate.sdpMid ?? "0",
160
180
  });
161
181
  }),
162
182
  (this.pc.onconnectionstatechange = () => {
@@ -164,286 +184,355 @@ class B {
164
184
  }));
165
185
  }
166
186
  async start() {
167
- let q = this.pc.createDataChannel(_, { ordered: !0 });
168
- ((q.binaryType = "arraybuffer"),
169
- (this.channel = q),
170
- (q.onopen = () => this.opts.onOpen?.(q)),
171
- (q.onclose = () => this.opts.onClose?.()));
172
- let z = await this.pc.createOffer();
173
- (await this.pc.setLocalDescription(z),
174
- this.opts.sendSignal({ kind: "offer", type: "offer", sdp: z.sdp ?? "" }));
175
- }
176
- async handleSignal(q) {
177
- let z = q;
178
- if (!z || typeof z !== "object") return;
179
- if (z.kind === "answer") await this.pc.setRemoteDescription({ type: "answer", sdp: z.sdp });
180
- else if (z.kind === "candidate")
187
+ let z = this.pc.createDataChannel(N, { ordered: !0 });
188
+ ((z.binaryType = "arraybuffer"),
189
+ (this.channel = z),
190
+ (z.onopen = () => this.opts.onOpen?.(z)),
191
+ (z.onclose = () => this.opts.onClose?.()));
192
+ let Q = this.pc.createDataChannel(_, { ordered: !0 });
193
+ ((Q.binaryType = "arraybuffer"), (this.bulk = Q));
194
+ let Y = await this.pc.createOffer();
195
+ (await this.pc.setLocalDescription(Y),
196
+ this.opts.sendSignal({ kind: "offer", type: "offer", sdp: Y.sdp ?? "" }));
197
+ }
198
+ async handleSignal(z) {
199
+ let Q = z;
200
+ if (!Q || typeof Q !== "object") return;
201
+ if (Q.kind === "answer") await this.pc.setRemoteDescription({ type: "answer", sdp: Q.sdp });
202
+ else if (Q.kind === "candidate")
181
203
  try {
182
- await this.pc.addIceCandidate({ candidate: z.candidate, sdpMid: z.mid });
183
- } catch (K) {
184
- console.error("[rtc] addIceCandidate failed:", K);
204
+ await this.pc.addIceCandidate({ candidate: Q.candidate, sdpMid: Q.mid });
205
+ } catch (Y) {
206
+ console.error("[rtc] addIceCandidate failed:", Y);
185
207
  }
186
208
  }
187
209
  get dataChannel() {
188
210
  return this.channel;
189
211
  }
212
+ get bulkChannel() {
213
+ return this.bulk;
214
+ }
215
+ async selectedPath() {
216
+ try {
217
+ let z = await this.pc.getStats(),
218
+ Q = null;
219
+ z.forEach((q) => {
220
+ if (q.type === "transport" && q.selectedCandidatePairId) Q = q.selectedCandidatePairId;
221
+ });
222
+ let Y = null;
223
+ if (
224
+ (z.forEach((q) => {
225
+ if (
226
+ Q ? q.id === Q : q.type === "candidate-pair" && q.state === "succeeded" && q.nominated
227
+ )
228
+ Y = q;
229
+ }),
230
+ !Y)
231
+ )
232
+ return null;
233
+ let { localCandidateId: Z, remoteCandidateId: $ } = Y,
234
+ G = !0,
235
+ V = 0;
236
+ if (
237
+ (z.forEach((q) => {
238
+ if (q.id === Z || q.id === $) {
239
+ if ((V++, q.candidateType !== "host")) G = !1;
240
+ }
241
+ }),
242
+ V < 2)
243
+ )
244
+ return null;
245
+ return G ? "lan" : "p2p";
246
+ } catch {
247
+ return null;
248
+ }
249
+ }
190
250
  close() {
191
251
  try {
192
252
  this.channel?.close();
193
253
  } catch {}
254
+ try {
255
+ this.bulk?.close();
256
+ } catch {}
194
257
  try {
195
258
  this.pc.close();
196
259
  } catch {}
197
260
  }
198
261
  }
199
- var F = new TextEncoder(),
200
- x = new TextDecoder();
201
- function G(q, z, K) {
202
- let Q = K?.byteLength ?? 0,
203
- X = new Uint8Array(5 + Q);
204
- if (((X[0] = q), new DataView(X.buffer).setUint32(1, z >>> 0, !1), K && Q)) X.set(K, 5);
205
- return X;
262
+ var y = new TextEncoder(),
263
+ S = new TextDecoder();
264
+ function P(z, Q, Y) {
265
+ let Z = Y?.byteLength ?? 0,
266
+ $ = new Uint8Array(5 + Z);
267
+ if ((($[0] = z), new DataView($.buffer).setUint32(1, Q >>> 0, !1), Y && Z)) $.set(Y, 5);
268
+ return $;
206
269
  }
207
- function P(q, z, K) {
208
- return G(q, z, F.encode(JSON.stringify(K)));
270
+ function J(z, Q, Y) {
271
+ return P(z, Q, y.encode(JSON.stringify(Y)));
209
272
  }
210
- function J(q) {
211
- let z = q instanceof Uint8Array ? q : new Uint8Array(q),
212
- K = z[0],
213
- Q = new DataView(z.buffer, z.byteOffset, z.byteLength).getUint32(1, !1),
214
- X = z.subarray(5);
215
- return { op: K, streamId: Q, payload: X };
273
+ function T(z) {
274
+ let Q = z instanceof Uint8Array ? z : new Uint8Array(z),
275
+ Y = Q[0],
276
+ Z = new DataView(Q.buffer, Q.byteOffset, Q.byteLength).getUint32(1, !1),
277
+ $ = Q.subarray(5);
278
+ return { op: Y, streamId: Z, payload: $ };
216
279
  }
217
- function H(q) {
218
- return JSON.parse(x.decode(q));
280
+ function U(z) {
281
+ return JSON.parse(S.decode(z));
219
282
  }
220
- function L(q) {
221
- return x.decode(q);
283
+ function E(z) {
284
+ return S.decode(z);
222
285
  }
223
- function* S(q) {
224
- for (let z = 0; z < q.byteLength; z += 16379) yield q.slice(z, Math.min(z + 16379, q.byteLength));
286
+ function* C(z) {
287
+ for (let Q = 0; Q < z.byteLength; Q += 65531) yield z.slice(Q, Math.min(Q + 65531, z.byteLength));
225
288
  }
226
- function C(q) {
227
- if (q.length === 1) return q[0];
228
- let z = q.reduce((X, Z) => X + Z.byteLength, 0),
229
- K = new Uint8Array(z),
230
- Q = 0;
231
- for (let X of q) (K.set(X, Q), (Q += X.byteLength));
232
- return K;
289
+ function f(z) {
290
+ if (z.length === 1) return z[0];
291
+ let Q = z.reduce(($, G) => $ + G.byteLength, 0),
292
+ Y = new Uint8Array(Q),
293
+ Z = 0;
294
+ for (let $ of z) (Y.set($, Z), (Z += $.byteLength));
295
+ return Y;
233
296
  }
234
- function* M(q, z, K) {
235
- let Q = 0;
236
- while (K.byteLength - Q > 16379) (yield G(13, z, K.subarray(Q, Q + 16379)), (Q += 16379));
237
- yield G(q, z, K.subarray(Q));
297
+ function* H(z, Q, Y) {
298
+ let Z = 0;
299
+ while (Y.byteLength - Z > 65531) (yield P(13, Q, Y.subarray(Z, Z + 65531)), (Z += 65531));
300
+ yield P(z, Q, Y.subarray(Z));
238
301
  }
239
- class k {
302
+ class F {
240
303
  pending = new Map();
241
- cont(q, z) {
242
- let K = this.pending.get(q);
243
- if (K) K.push(z.slice());
244
- else this.pending.set(q, [z.slice()]);
304
+ cont(z, Q) {
305
+ let Y = this.pending.get(z);
306
+ if (Y) Y.push(Q.slice());
307
+ else this.pending.set(z, [Q.slice()]);
245
308
  }
246
- finish(q, z) {
247
- let K = this.pending.get(q);
248
- if (!K) return z;
249
- return (this.pending.delete(q), K.push(z), C(K));
309
+ finish(z, Q) {
310
+ let Y = this.pending.get(z);
311
+ if (!Y) return Q;
312
+ return (this.pending.delete(z), Y.push(Q), f(Y));
250
313
  }
251
- drop(q) {
252
- this.pending.delete(q);
314
+ drop(z) {
315
+ this.pending.delete(z);
253
316
  }
254
317
  }
255
- class N {
318
+ class R {
256
319
  channel;
320
+ bulk;
257
321
  nextStreamId = 1;
258
322
  https = new Map();
259
323
  wss = new Map();
260
- wsRx = new k();
324
+ wsRx = new F();
261
325
  textEncoder = new TextEncoder();
262
- constructor(q) {
263
- this.channel = q;
264
- ((q.binaryType = "arraybuffer"), q.addEventListener("message", (z) => this.onFrame(z.data)));
326
+ constructor(z, Q = null) {
327
+ this.channel = z;
328
+ this.bulk = Q;
329
+ if (
330
+ ((z.binaryType = "arraybuffer"),
331
+ z.addEventListener("message", (Y) => this.onFrame(Y.data)),
332
+ Q)
333
+ )
334
+ ((Q.binaryType = "arraybuffer"), Q.addEventListener("message", (Y) => this.onFrame(Y.data)));
265
335
  }
266
336
  allocId() {
267
- let q = this.nextStreamId;
268
- return ((this.nextStreamId = (this.nextStreamId + 1) >>> 0 || 1), q);
337
+ let z = this.nextStreamId;
338
+ return ((this.nextStreamId = (this.nextStreamId + 1) >>> 0 || 1), z);
269
339
  }
270
- onFrame(q) {
271
- if (typeof q === "string") return;
272
- let { op: z, streamId: K, payload: Q } = J(q);
273
- switch (z) {
340
+ onFrame(z) {
341
+ if (typeof z === "string") return;
342
+ let { op: Q, streamId: Y, payload: Z } = T(z);
343
+ switch (Q) {
274
344
  case 4:
275
- this.https.get(K)?.onHead(H(Q));
345
+ this.https.get(Y)?.onHead(U(Z));
276
346
  break;
277
347
  case 5:
278
- this.https.get(K)?.onBody(Q.slice());
348
+ this.https.get(Y)?.onBody(Z.slice());
279
349
  break;
280
350
  case 6:
281
- (this.https.get(K)?.onEnd(), this.https.delete(K));
351
+ (this.https.get(Y)?.onEnd(), this.https.delete(Y));
282
352
  break;
283
353
  case 12: {
284
- let X = this.https.get(K);
285
- if (X) (X.onError(H(Q).message), this.https.delete(K));
354
+ let $ = this.https.get(Y);
355
+ if ($) ($.onError(U(Z).message), this.https.delete(Y));
286
356
  break;
287
357
  }
288
358
  case 8: {
289
- let X = H(Q);
290
- this.wss.get(K)?.onOpenAck(X.ok, X.protocol);
359
+ let $ = U(Z);
360
+ this.wss.get(Y)?.onOpenAck($.ok, $.protocol);
291
361
  break;
292
362
  }
293
363
  case 13:
294
- this.wsRx.cont(K, Q);
364
+ this.wsRx.cont(Y, Z);
295
365
  break;
296
366
  case 9:
297
- this.wss.get(K)?.onText(L(this.wsRx.finish(K, Q)));
367
+ this.wss.get(Y)?.onText(E(this.wsRx.finish(Y, Z)));
298
368
  break;
299
369
  case 10:
300
- this.wss.get(K)?.onBin(this.wsRx.finish(K, Q).slice());
370
+ this.wss.get(Y)?.onBin(this.wsRx.finish(Y, Z).slice());
301
371
  break;
302
372
  case 11: {
303
- let X = H(Q);
304
- (this.wsRx.drop(K),
305
- this.wss.get(K)?.onClose(X.code ?? 1000, X.reason ?? ""),
306
- this.wss.delete(K));
373
+ let $ = U(Z);
374
+ (this.wsRx.drop(Y),
375
+ this.wss.get(Y)?.onClose($.code ?? 1000, $.reason ?? ""),
376
+ this.wss.delete(Y));
307
377
  break;
308
378
  }
309
379
  }
310
380
  }
311
- fetch(q, z, K, Q) {
312
- let X = this.allocId();
313
- return new Promise((Z, V) => {
314
- let D = null,
315
- $ = null,
316
- j = new ReadableStream({
317
- start: (Y) => {
318
- $ = Y;
319
- },
320
- });
321
- if (
322
- (this.https.set(X, {
323
- onHead: (Y) => {
324
- ((D = Y),
325
- Z(
326
- new Response(j, {
327
- status: Y.status === 204 || Y.status === 304 ? Y.status : Y.status,
328
- statusText: Y.statusText,
329
- headers: Y.headers,
330
- }),
331
- ));
332
- },
333
- onBody: (Y) => {
334
- try {
335
- $?.enqueue(Y);
336
- } catch {}
337
- },
338
- onEnd: () => {
339
- try {
340
- $?.close();
341
- } catch {}
342
- if (!D) V(Error("stream ended before head"));
343
- },
344
- onError: (Y) => {
345
- try {
346
- $?.error(Error(Y));
347
- } catch {}
348
- if (!D) V(Error(Y));
381
+ fetch(z, Q, Y, Z) {
382
+ let $ = this.allocId();
383
+ return new Promise((G, V) => {
384
+ let q = null,
385
+ D = null,
386
+ X = new ReadableStream({
387
+ start: (K) => {
388
+ D = K;
349
389
  },
350
390
  }),
351
- this.send(P(1, X, { method: q, path: z, headers: K })),
352
- Q && Q.byteLength)
353
- )
354
- for (let Y of S(Q)) this.send(G(2, X, Y));
355
- this.send(G(3, X));
391
+ v = typeof DecompressionStream < "u" ? { ...Y, "x-codehost-accept-gzip": "1" } : Y;
392
+ this.https.set($, {
393
+ onHead: (K) => {
394
+ q = K;
395
+ let x = new Headers(K.headers),
396
+ M = X;
397
+ if (x.get("content-encoding") === "gzip")
398
+ ((M = X.pipeThrough(new DecompressionStream("gzip"))),
399
+ x.delete("content-encoding"),
400
+ x.delete("content-length"));
401
+ G(
402
+ new Response(M, {
403
+ status: K.status === 204 || K.status === 304 ? K.status : K.status,
404
+ statusText: K.statusText,
405
+ headers: x,
406
+ }),
407
+ );
408
+ },
409
+ onBody: (K) => {
410
+ try {
411
+ D?.enqueue(K);
412
+ } catch {}
413
+ },
414
+ onEnd: () => {
415
+ try {
416
+ D?.close();
417
+ } catch {}
418
+ if (!q) V(Error("stream ended before head"));
419
+ },
420
+ onError: (K) => {
421
+ try {
422
+ D?.error(Error(K));
423
+ } catch {}
424
+ if (!q) V(Error(K));
425
+ },
426
+ });
427
+ let W = this.bulk?.readyState === "open" ? this.bulk : this.channel;
428
+ if ((this.sendOn(W, J(1, $, { method: z, path: Q, headers: v })), Z && Z.byteLength))
429
+ for (let K of C(Z)) this.sendOn(W, P(2, $, K));
430
+ this.sendOn(W, P(3, $));
356
431
  });
357
432
  }
358
- openWs(q, z, K) {
359
- let Q = this.allocId();
433
+ openWs(z, Q, Y) {
434
+ let Z = this.allocId();
360
435
  return (
361
- this.wss.set(Q, K),
362
- this.send(P(7, Q, { path: q, protocols: z })),
436
+ this.wss.set(Z, Y),
437
+ this.send(J(7, Z, { path: z, protocols: Q })),
363
438
  {
364
- sendText: (X) => {
365
- for (let Z of M(9, Q, this.textEncoder.encode(X))) this.send(Z);
439
+ sendText: ($) => {
440
+ for (let G of H(9, Z, this.textEncoder.encode($))) this.send(G);
366
441
  },
367
- sendBin: (X) => {
368
- for (let Z of M(10, Q, X)) this.send(Z);
442
+ sendBin: ($) => {
443
+ for (let G of H(10, Z, $)) this.send(G);
369
444
  },
370
- close: (X, Z) => {
371
- (this.send(P(11, Q, { code: X, reason: Z })), this.wss.delete(Q));
445
+ close: ($, G) => {
446
+ (this.send(J(11, Z, { code: $, reason: G })), this.wss.delete(Z));
372
447
  },
373
448
  }
374
449
  );
375
450
  }
376
- send(q) {
377
- if (this.channel.readyState === "open") {
378
- let z = new Uint8Array(q.byteLength);
379
- (z.set(q), this.channel.send(z.buffer));
451
+ send(z) {
452
+ this.sendOn(this.channel, z);
453
+ }
454
+ sendOn(z, Q) {
455
+ if (z.readyState === "open") {
456
+ let Y = new Uint8Array(Q.byteLength);
457
+ (Y.set(Q), z.send(Y.buffer));
380
458
  }
381
459
  }
382
460
  get ready() {
383
461
  return this.channel.readyState === "open";
384
462
  }
385
463
  }
386
- var w = "wss://signal.codehost.dev";
387
- class R {
464
+ var I = "wss://signal.codehost.dev",
465
+ h = 1e4;
466
+ class w {
388
467
  peers = [];
389
468
  signaling;
390
469
  rtcs = new Map();
391
470
  tunnels = new Map();
471
+ dialFailedAt = new Map();
392
472
  closed = !1;
393
- constructor(q) {
394
- ((this.signaling = new U({
395
- url: q.signalUrl ?? w,
396
- token: q.token,
473
+ constructor(z) {
474
+ ((this.signaling = new j({
475
+ url: z.signalUrl ?? I,
476
+ token: z.token,
397
477
  role: "viewer",
398
- onOpen: () => q.onStatus?.(!0),
399
- onClose: () => q.onStatus?.(!1),
400
- onPeers: (z) => {
401
- ((this.peers = z.filter((K) => K.role === "server")), q.onPeers?.(this.peers));
478
+ onOpen: () => z.onStatus?.(!0),
479
+ onClose: () => z.onStatus?.(!1),
480
+ onPeers: (Q) => {
481
+ ((this.peers = Q.filter((Y) => Y.role === "server")), z.onPeers?.(this.peers));
402
482
  },
403
- onSignal: (z, K) => void this.rtcs.get(z)?.handleSignal(K),
483
+ onSignal: (Q, Y) => void this.rtcs.get(Q)?.handleSignal(Y),
404
484
  })),
405
485
  this.signaling.connect());
406
486
  }
407
- async fetch(q, z, K, Q = {}) {
408
- let X = await this.dial(q),
409
- Z = typeof Q.body === "string" ? new TextEncoder().encode(Q.body) : Q.body;
410
- return X.fetch(z, K, Q.headers ?? {}, Z);
411
- }
412
- dial(q) {
413
- let z = this.tunnels.get(q);
414
- if (z) return z;
415
- let K = () => {
416
- (this.tunnels.delete(q), this.rtcs.get(q)?.close(), this.rtcs.delete(q));
487
+ async fetch(z, Q, Y, Z = {}) {
488
+ let $ = await this.dial(z),
489
+ G = typeof Z.body === "string" ? new TextEncoder().encode(Z.body) : Z.body;
490
+ return $.fetch(Q, Y, Z.headers ?? {}, G);
491
+ }
492
+ dial(z) {
493
+ let Q = this.tunnels.get(z);
494
+ if (Q) return Q;
495
+ let Y = this.dialFailedAt.get(z);
496
+ if (Y != null && Date.now() - Y < h)
497
+ return Promise.reject(Error("dial failed recently; cooling down"));
498
+ let Z = () => {
499
+ (this.tunnels.delete(z), this.rtcs.get(z)?.close(), this.rtcs.delete(z));
417
500
  },
418
- Q = new Promise((X, Z) => {
419
- let V = setTimeout(() => {
420
- (K(), Z(Error("dial timed out")));
501
+ $ = new Promise((G, V) => {
502
+ let q = setTimeout(() => {
503
+ (Z(), V(Error("dial timed out")));
421
504
  }, 15000),
422
505
  D = new B({
423
- sendSignal: ($) => this.signaling.sendSignal(q, $),
424
- onOpen: ($) => {
425
- (clearTimeout(V), X(new N($)));
506
+ sendSignal: (X) => this.signaling.sendSignal(z, X),
507
+ onOpen: (X) => {
508
+ (clearTimeout(q), this.dialFailedAt.delete(z), G(new R(X, D.bulkChannel)));
426
509
  },
427
- onClose: K,
428
- onState: ($) => {
429
- if ($ === "failed" || $ === "disconnected") K();
510
+ onClose: Z,
511
+ onState: (X) => {
512
+ if (X === "failed" || X === "disconnected") Z();
430
513
  },
431
514
  });
432
- (this.rtcs.set(q, D),
433
- D.start().catch(($) => {
434
- (clearTimeout(V), K(), Z($));
515
+ (this.rtcs.set(z, D),
516
+ D.start().catch((X) => {
517
+ (clearTimeout(q), Z(), V(X));
435
518
  }));
436
519
  });
437
- return (this.tunnels.set(q, Q), Q.catch(() => this.tunnels.delete(q)), Q);
520
+ return (
521
+ this.tunnels.set(z, $),
522
+ $.catch(() => {
523
+ (this.dialFailedAt.set(z, Date.now()), this.tunnels.delete(z));
524
+ }),
525
+ $
526
+ );
438
527
  }
439
528
  close() {
440
529
  if (this.closed) return;
441
530
  this.closed = !0;
442
- for (let q of this.rtcs.values()) q.close();
531
+ for (let z of this.rtcs.values()) z.close();
443
532
  (this.rtcs.clear(), this.tunnels.clear(), this.signaling.close());
444
533
  }
445
534
  }
446
- function n(q) {
447
- return new R(q);
535
+ function zz(z) {
536
+ return new w(z);
448
537
  }
449
- export { n as joinRoom, w as DEFAULT_SIGNAL_URL, R as CodehostRoom };
538
+ export { zz as joinRoom, I as DEFAULT_SIGNAL_URL, w as CodehostRoom };