@tesser-sh/gateway 0.60.0 → 0.62.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.
Files changed (2) hide show
  1. package/index.js +25 -10
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // ../front/src/page.ts
2
- var FAVICON = `data:image/svg+xml,${encodeURIComponent('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><rect width="24" height="24" rx="4" fill="#000000"/><g transform="translate(2.4 2.4) scale(0.8)"><path fill="#f0f1e9" d="M11.5 11.5H2A9.5 9.5 0 0 1 11.5 2ZM12.5 12.5H22A9.5 9.5 0 0 1 12.5 22Z"/><path fill="#fbffc9" d="M12.5 2H22V11.5H12.5Z"/></g></svg>')}`;
2
+ var FAVICON = `data:image/svg+xml,${encodeURIComponent('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"><rect width="16" height="16" rx="3" fill="#000000"/><path fill="#f0f1e9" d="M8 8H2A6 6 0 0 1 8 2ZM9 9H15A6 6 0 0 1 9 15Z"/><path fill="#fbffc9" d="M9 2H15V8H9Z"/></svg>')}`;
3
3
  function escapeHtml(text) {
4
4
  return text.replace(/[&<>"]/g, (c) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;" })[c]);
5
5
  }
@@ -102,6 +102,7 @@ var STATUS = {
102
102
  stopped: 502,
103
103
  idle: 502,
104
104
  "wake-failed": 503,
105
+ "out-of-credit": 402,
105
106
  "no-port": 404,
106
107
  removed: 410,
107
108
  "not-shared": 404,
@@ -126,6 +127,8 @@ function viewerText(state) {
126
127
  return `nothing is running on ${state.box.name}`;
127
128
  case "wake-failed":
128
129
  return `${state.box.name} didn't wake up: ${state.reason}`;
130
+ case "out-of-credit":
131
+ return `${state.box.name} can't wake up: ${state.reason}`;
129
132
  case "no-port":
130
133
  return `${state.box.name} has no port ${state.port}`;
131
134
  case "removed":
@@ -199,6 +202,15 @@ function body(state, owner) {
199
202
  retry: !1,
200
203
  rest: `${who(state.box)}<p class="bad">${escapeHtml(state.reason)}</p><a class="open" href="">Try again</a>`
201
204
  };
205
+ case "out-of-credit": {
206
+ let billing = /https?:\/\/\S+/.exec(state.reason)?.[0];
207
+ return {
208
+ headline: "This org is out of usage credit",
209
+ spinner: !1,
210
+ retry: !1,
211
+ rest: `${who(state.box)}<p class="sub">${escapeHtml(state.box.name)} can't wake up until someone adds credit.</p><p class="bad">${escapeHtml(state.reason)}</p>${billing === void 0 ? "" : `<a class="open" href="${escapeHtml(billing)}">Open billing</a>`}`
212
+ };
213
+ }
202
214
  case "no-port":
203
215
  return {
204
216
  headline: `${state.box.name} has no port ${state.port}`,
@@ -458,7 +470,7 @@ class DirectoryUnavailable extends Schema.TaggedError()("DirectoryUnavailable",
458
470
  class BoxAsleep extends Schema.TaggedError()("BoxAsleep", { boxId: Schema.String }) {
459
471
  }
460
472
 
461
- class WakeFailed extends Schema.TaggedError()("WakeFailed", { boxId: Schema.String, reason: Schema.String }) {
473
+ class WakeFailed extends Schema.TaggedError()("WakeFailed", { boxId: Schema.String, code: Schema.NullOr(Schema.String), reason: Schema.String }) {
462
474
  }
463
475
 
464
476
  class UpstreamRefused extends Schema.TaggedError()("UpstreamRefused", { boxId: Schema.String, port: Schema.Number }) {
@@ -491,7 +503,7 @@ function viewerBox(box) {
491
503
  var wakingPage = Effect2.fn("Proxy.wakingPage")(function* (request, box, port, stage, since) {
492
504
  let now = yield* Clock2.currentTimeMillis;
493
505
  return yield* page(request, { kind: "waking", box: viewerBox(box), stage, port, seconds: Math.max(0, Math.round((now - since) / 1000)) });
494
- }), notAnswering = (request, box, port) => page(request, { kind: "not-answering", box: viewerBox(box), port }), refused = Effect2.fn("Proxy.refused")(function* (request, box, port) {
506
+ }), wakeFailed = (request, box, { code, reason }) => page(request, code === "credit_exhausted" ? { kind: "out-of-credit", box, reason } : { kind: "wake-failed", box, reason }), notAnswering = (request, box, port) => page(request, { kind: "not-answering", box: viewerBox(box), port }), refused = Effect2.fn("Proxy.refused")(function* (request, box, port) {
495
507
  let status = yield* (yield* Waker).status(box.boxId), now = yield* Clock2.currentTimeMillis;
496
508
  if (box.app === "exited")
497
509
  return yield* page(request, { kind: "stopped", box: viewerBox(box) });
@@ -525,7 +537,7 @@ var wakingPage = Effect2.fn("Proxy.wakingPage")(function* (request, box, port, s
525
537
  yield* waker.wake(box.boxId);
526
538
  let status = yield* waker.status(box.boxId);
527
539
  if (box.power === "asleep" && status?.failure)
528
- return yield* page(request, { kind: "wake-failed", box: viewerBox(box), reason: status.failure });
540
+ return yield* wakeFailed(request, viewerBox(box), status.failure);
529
541
  if (box.power === "asleep" && status === null)
530
542
  return yield* page(request, { kind: "asleep", box: viewerBox(box) });
531
543
  if (!wantsDocument(request))
@@ -535,7 +547,7 @@ var wakingPage = Effect2.fn("Proxy.wakingPage")(function* (request, box, port, s
535
547
  return yield* wakingPage(request, box, port, "machine", status?.since ?? (yield* Clock2.currentTimeMillis));
536
548
  }), settled = (request, route, box) => serve(request, route, box).pipe(Effect2.catchTags({
537
549
  BoxAsleep: () => dormant(request, { ...box, power: "asleep" }, route.port, comingUp(request, box, route.port)),
538
- WakeFailed: (e) => page(request, { kind: "wake-failed", box: viewerBox(box), reason: e.reason })
550
+ WakeFailed: (e) => wakeFailed(request, viewerBox(box), e)
539
551
  })), handle = Effect2.fn("Proxy.handle")(function* (request) {
540
552
  let resolved = yield* (yield* Resolver).resolve(request);
541
553
  if (resolved instanceof Response)
@@ -550,7 +562,7 @@ var wakingPage = Effect2.fn("Proxy.wakingPage")(function* (request, box, port, s
550
562
  BoxNotShared: () => page(request, { kind: "not-shared" }),
551
563
  BoxRemoved: (e) => page(request, { kind: "removed", box: { boxId: e.boxId, name: e.name, sharedBy: null } }),
552
564
  DirectoryUnavailable: () => page(request, { kind: "unavailable" }),
553
- WakeFailed: (e) => page(request, { kind: "wake-failed", box: { boxId: e.boxId, name: e.boxId, sharedBy: null }, reason: e.reason })
565
+ WakeFailed: (e) => wakeFailed(request, { boxId: e.boxId, name: e.boxId, sharedBy: null }, e)
554
566
  })));
555
567
 
556
568
  // ../front/src/csp.ts
@@ -778,6 +790,9 @@ function upstreamHeaders(request, target, port, root) {
778
790
  let origin = request.headers.get("origin");
779
791
  if (origin !== null && parseUrl(origin)?.host === new URL(request.url).host)
780
792
  headers.set("origin", `http://${presentedHost(target.pageSub, root)}`);
793
+ let destination = parseUrl(request.headers.get("destination") ?? "");
794
+ if (destination?.host === new URL(request.url).host)
795
+ headers.set("destination", `http://${presentedHost(target.sub, port)}${destination.pathname}${destination.search}`);
781
796
  return headers;
782
797
  }
783
798
  function isHtmlDocument(request, response) {
@@ -1032,7 +1047,7 @@ var TICKET_HEADER = "x-tesser-ticket", WAKE_TIMEOUT = Duration2.minutes(5), WAKE
1032
1047
  power: Schema2.Literals(["launching", "awake", "asleep", "removing", "gone"]),
1033
1048
  app: Schema2.NullOr(Schema2.Literals(["none", "running", "unsupervised", "exited"]))
1034
1049
  })
1035
- }), ErrorBody = Schema2.Struct({ error: Schema2.Struct({ message: Schema2.String }) });
1050
+ }), ErrorBody = Schema2.Struct({ error: Schema2.Struct({ code: Schema2.String, message: Schema2.String }) });
1036
1051
  function plain(status, text) {
1037
1052
  return new Response(`${text}
1038
1053
  `, { status, headers: { "content-type": "text/plain; charset=utf-8", "cache-control": "no-store" } });
@@ -1282,7 +1297,7 @@ class BoxRelay extends DurableObject {
1282
1297
  if (status < 300 || status >= 500 || RETRYABLE.has(status))
1283
1298
  return;
1284
1299
  let message = Schema2.decodeUnknownOption(ErrorBody)(body);
1285
- waking.failure = message._tag === "Some" ? message.value.error.message : `tesser answered ${status}`;
1300
+ waking.failure = message._tag === "Some" ? { code: message.value.error.code, reason: message.value.error.message } : { code: null, reason: `tesser answered ${status}` };
1286
1301
  }), wake = (boxId) => kick(boxId, !0), lookup = Effect4.fn("Relay.lookup")(function* (boxId) {
1287
1302
  let ticket = self.live();
1288
1303
  if (ticket === null) {
@@ -1305,7 +1320,7 @@ class BoxRelay extends DurableObject {
1305
1320
  if (w === null || w.failure === null && now - w.since > Duration2.toMillis(WAKE_TIMEOUT))
1306
1321
  return null;
1307
1322
  return { since: w.since, failure: w.failure };
1308
- }), dialed = (boxId, within, reason) => Effect4.promise(() => new Promise((resolve) => self.connected.push(resolve))).pipe(Effect4.timeoutOrElse({ duration: within, orElse: () => Effect4.fail(new WakeFailed({ boxId, reason })) })), awake = Effect4.fn("Relay.awake")(function* (boxId) {
1323
+ }), dialed = (boxId, within, reason) => Effect4.promise(() => new Promise((resolve) => self.connected.push(resolve))).pipe(Effect4.timeoutOrElse({ duration: within, orElse: () => Effect4.fail(new WakeFailed({ boxId, code: null, reason })) })), awake = Effect4.fn("Relay.awake")(function* (boxId) {
1309
1324
  if (self.live() === null) {
1310
1325
  let box = yield* remote(boxId);
1311
1326
  if (box.power === "awake")
@@ -1313,7 +1328,7 @@ class BoxRelay extends DurableObject {
1313
1328
  else {
1314
1329
  let waking = yield* status(boxId);
1315
1330
  if (waking?.failure)
1316
- return yield* new WakeFailed({ boxId, reason: waking.failure });
1331
+ return yield* new WakeFailed({ boxId, ...waking.failure });
1317
1332
  if (box.power === "asleep" && waking === null)
1318
1333
  return yield* new BoxAsleep({ boxId });
1319
1334
  yield* dialed(boxId, WAKE_TIMEOUT, "it didn't wake up within 5 minutes");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tesser-sh/gateway",
3
- "version": "0.60.0",
3
+ "version": "0.62.0",
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",