@tesser-sh/gateway 0.56.2 → 0.56.3

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 (2) hide show
  1. package/index.js +24 -8
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -247,7 +247,15 @@ function gatewayHostBox(gatewayHost, hostname) {
247
247
 
248
248
  // ../protocol/src/mux.ts
249
249
  var MUX_WINDOW = 262144, MUX_CHUNK = 65536;
250
- var MUX_PING = "tesser:ping", MUX_PONG = "tesser:pong", CODES = { open: 1, head: 2, data: 3, end: 4, reset: 5, credit: 6, text: 7, binary: 8, close: 9 }, TYPES = Object.fromEntries(Object.entries(CODES).map(([k, v]) => [v, k])), encoder = /* @__PURE__ */ new TextEncoder, decoder = new TextDecoder("utf-8", { fatal: !0, ignoreBOM: !1 });
250
+ var MUX_PING = "tesser:ping", MUX_PONG = "tesser:pong";
251
+ function closeCode(code) {
252
+ if (code === 1005)
253
+ return 1000;
254
+ if (code >= 1000 && code <= 1014 && code !== 1004 && code !== 1006 || code >= 3000 && code <= 4999)
255
+ return code;
256
+ return code >= 1016 && code <= 2999 ? 1011 : 1001;
257
+ }
258
+ var CODES = { open: 1, head: 2, data: 3, end: 4, reset: 5, credit: 6, text: 7, binary: 8, close: 9 }, TYPES = Object.fromEntries(Object.entries(CODES).map(([k, v]) => [v, k])), encoder = /* @__PURE__ */ new TextEncoder, decoder = new TextDecoder("utf-8", { fatal: !0, ignoreBOM: !1 });
251
259
  function frame(type, stream, payload) {
252
260
  let out = new Uint8Array(5 + payload.byteLength);
253
261
  return out[0] = CODES[type], new DataView(out.buffer).setUint32(1, stream), out.set(payload, 5), out;
@@ -448,12 +456,19 @@ class WakeFailed extends Schema.TaggedError()("WakeFailed", { boxId: Schema.Stri
448
456
  class UpstreamRefused extends Schema.TaggedError()("UpstreamRefused", { boxId: Schema.String, port: Schema.Number }) {
449
457
  }
450
458
 
459
+ class UpstreamTimedOut extends Schema.TaggedError()("UpstreamTimedOut", { boxId: Schema.String, port: Schema.Number }) {
460
+ }
461
+
451
462
  class Resolver extends Context.Service()("@tesser/proxy/Resolver") {
452
463
  }
453
464
 
454
465
  class Waker extends Context.Service()("@tesser/proxy/Waker") {
455
466
  }
456
- var WAKING_WINDOW = Duration.minutes(5), startingRetry = { schedule: Schedule.spaced(Duration.seconds(1)), times: 60 }, respond = (response) => ({ _tag: "response", response });
467
+ var WAKING_WINDOW = Duration.minutes(5), startingRetry = {
468
+ schedule: Schedule.spaced(Duration.seconds(1)),
469
+ times: 60,
470
+ while: (error) => error._tag === "UpstreamRefused"
471
+ }, respond = (response) => ({ _tag: "response", response });
457
472
  function viewerResponse(request, state, owner = !1) {
458
473
  let status = viewerStatus(state);
459
474
  if (wantsDocument(request))
@@ -468,7 +483,7 @@ function viewerBox(box) {
468
483
  var wakingPage = Effect2.fn("Proxy.wakingPage")(function* (request, box, port, stage, since) {
469
484
  let now = yield* Clock2.currentTimeMillis;
470
485
  return yield* page(request, { kind: "waking", box: viewerBox(box), stage, port, seconds: Math.max(0, Math.round((now - since) / 1000)) });
471
- }), refused = Effect2.fn("Proxy.refused")(function* (request, box, port) {
486
+ }), notAnswering = (request, box, port) => page(request, { kind: "not-answering", box: viewerBox(box), port }), refused = Effect2.fn("Proxy.refused")(function* (request, box, port) {
472
487
  let status = yield* (yield* Waker).status(box.boxId), now = yield* Clock2.currentTimeMillis;
473
488
  if (box.app === "exited")
474
489
  return yield* page(request, { kind: "stopped", box: viewerBox(box) });
@@ -476,7 +491,7 @@ var wakingPage = Effect2.fn("Proxy.wakingPage")(function* (request, box, port, s
476
491
  return yield* wakingPage(request, box, port, box.app === "running" ? "port" : "app", status.since);
477
492
  if (box.app === "none")
478
493
  return yield* page(request, { kind: "idle", box: viewerBox(box) });
479
- return yield* page(request, { kind: "not-answering", box: viewerBox(box), port });
494
+ return yield* notAnswering(request, box, port);
480
495
  }), serve = Effect2.fn("Proxy.serve")(function* (request, route, box) {
481
496
  let { port, path, headers } = route;
482
497
  if (request.headers.get("upgrade")?.toLowerCase() === "websocket")
@@ -489,7 +504,10 @@ var wakingPage = Effect2.fn("Proxy.wakingPage")(function* (request, box, port, s
489
504
  signal: request.signal,
490
505
  ...replayable ? {} : { duplex: "half" }
491
506
  })));
492
- return yield* (document || !replayable ? attempt : attempt.pipe(Effect2.retry(startingRetry))).pipe(Effect2.flatMap((response) => Effect2.map(route.present(box, response), respond)), Effect2.catchTag("UpstreamRefused", () => document ? refused(request, box, port) : page(request, { kind: "not-answering", box: viewerBox(box), port })));
507
+ return yield* (document || !replayable ? attempt : attempt.pipe(Effect2.retry(startingRetry))).pipe(Effect2.flatMap((response) => Effect2.map(route.present(box, response), respond)), Effect2.catchTags({
508
+ UpstreamRefused: () => document ? refused(request, box, port) : notAnswering(request, box, port),
509
+ UpstreamTimedOut: () => notAnswering(request, box, port)
510
+ }));
493
511
  }), handle = Effect2.fn("Proxy.handle")(function* (request) {
494
512
  let resolved = yield* (yield* Resolver).resolve(request);
495
513
  if (resolved instanceof Response)
@@ -1191,9 +1209,7 @@ class BoxRelay extends DurableObject {
1191
1209
  return;
1192
1210
  }
1193
1211
  let { stream } = ws.deserializeAttachment();
1194
- this.unstored.delete(stream);
1195
- let sendable = code === 1005 ? 1000 : code === 1004 || code === 1006 || code === 1015 || code < 1000 || code > 4999 ? 1001 : code;
1196
- this.tunnel.send({ type: "close", stream, code: sendable, reason: reason.slice(0, 120) });
1212
+ this.unstored.delete(stream), this.tunnel.send({ type: "close", stream, code: closeCode(code), reason: reason.slice(0, 120) });
1197
1213
  }
1198
1214
  webSocketError(ws) {
1199
1215
  this.webSocketClose(ws, 1006, "socket error");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tesser-sh/gateway",
3
- "version": "0.56.2",
3
+ "version": "0.56.3",
4
4
  "description": "Tesser's share gateway as a Cloudflare Worker and Durable Object, deployed in your own account",
5
5
  "license": "MIT",
6
6
  "type": "module",