@edgehero/pi-dispatch-sandbox-gateway 0.0.0-stage → 5.1.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/README.md +35 -2
- package/dist/cli.mjs +104781 -0
- package/dist/static/gateway.css +5215 -0
- package/dist/static/gateway.js +151 -0
- package/dist/static/terminal.js +27874 -0
- package/package.json +50 -4
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
(() => {
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
9
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
10
|
+
}) : x)(function(x) {
|
|
11
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
12
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
13
|
+
});
|
|
14
|
+
var __copyProps = (to, from, except, desc) => {
|
|
15
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
16
|
+
for (let key of __getOwnPropNames(from))
|
|
17
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
18
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
23
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
24
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
25
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
26
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
27
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
28
|
+
mod
|
|
29
|
+
));
|
|
30
|
+
|
|
31
|
+
// src/bootstrap.mjs
|
|
32
|
+
var PUBLIC_ID = /^[A-Za-z0-9_-]{43}$/;
|
|
33
|
+
var GATEWAY_BROWSER_MIN_WIDTH = 761;
|
|
34
|
+
var GATEWAY_BOOT_TIMEOUT_MS = 1e4;
|
|
35
|
+
var GATEWAY_BOOT_WORDS = Object.freeze({
|
|
36
|
+
phone: ["Use your own terminal", "Browser terminals require a window wider than 760 pixels. Return to Sandboxes and copy the command."],
|
|
37
|
+
invalid: ["This launch is not usable", "Return to Sandboxes and start a new browser launch. A public request id alone cannot open a terminal."],
|
|
38
|
+
unavailable: ["The gateway could not authorize this launch", "No terminal opened. Return to Sandboxes, check the gateway and sign in again for a new launch."],
|
|
39
|
+
expired: ["This launch has ended", "The one-use authorization expired or was already used. Return to Sandboxes and start a new browser launch."],
|
|
40
|
+
loading: ["Opening the terminal", "Authorization was accepted. The terminal connects only to this gateway."],
|
|
41
|
+
coordination: ["Use your own terminal", "This browser cannot coordinate secure gateway launches. Return to Sandboxes and use Open in Terminal or copy the command."],
|
|
42
|
+
busy: ["Already open", "This exact browser attachment is already open. Use its existing workspace window, or disconnect it before authorizing a fresh launch."],
|
|
43
|
+
runtime: ["Sandbox runtime unavailable", "The gateway could not verify this existing shell. Inspect its host before authorizing a fresh launch. No container was started."],
|
|
44
|
+
session: ["This workspace uses another sign-in", "An existing workspace keeps its original sign-in even when all its tabs are closed. Close that workspace before opening a terminal with a different sign-in."],
|
|
45
|
+
limited: ["The gateway is at its connection limit", "No terminal opened. Disconnect an existing browser attachment or wait, then start a fresh launch from Sandboxes."]
|
|
46
|
+
});
|
|
47
|
+
function readGatewayLaunch(location, history) {
|
|
48
|
+
const url = new URL(location.href);
|
|
49
|
+
const fragment = url.hash;
|
|
50
|
+
if (fragment) history.replaceState(null, "", `${url.pathname}${url.search}`);
|
|
51
|
+
const match = /^\/s\/([A-Za-z0-9_-]{43})$/.exec(url.pathname);
|
|
52
|
+
if (!match || url.search) return null;
|
|
53
|
+
const values = new URLSearchParams(fragment.slice(1));
|
|
54
|
+
if ([...values.keys()].some((key) => key !== "handoff") || values.getAll("handoff").length > 1) return null;
|
|
55
|
+
const handoff = values.get("handoff");
|
|
56
|
+
if (fragment && (!handoff || !PUBLIC_ID.test(handoff))) return null;
|
|
57
|
+
return { intentId: match[1], handoff };
|
|
58
|
+
}
|
|
59
|
+
function gatewayAuthorizeUrl(raw, consoleOrigin, intentId) {
|
|
60
|
+
try {
|
|
61
|
+
const url = new URL(raw);
|
|
62
|
+
return url.origin === consoleOrigin && !url.username && !url.password && url.pathname === "/sandbox/gateway/authorize" && !url.hash && url.search === `?intent=${intentId}` ? url.href : null;
|
|
63
|
+
} catch {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
function gatewayBootRefusal(status, reason) {
|
|
68
|
+
if (reason === "workbench-session-changed") return "session";
|
|
69
|
+
if (reason === "window-busy") return "busy";
|
|
70
|
+
if (reason === "runtime-unavailable" || reason === "runtime-environment") return "runtime";
|
|
71
|
+
if (status === 429) return "limited";
|
|
72
|
+
return status === 409 || status === 410 ? "expired" : "unavailable";
|
|
73
|
+
}
|
|
74
|
+
async function boundedGatewayCall(window2, action) {
|
|
75
|
+
const controller = new window2.AbortController();
|
|
76
|
+
let timer;
|
|
77
|
+
try {
|
|
78
|
+
return await Promise.race([action(controller.signal), new Promise((_, reject) => {
|
|
79
|
+
timer = window2.setTimeout(() => {
|
|
80
|
+
controller.abort();
|
|
81
|
+
reject(new Error("gateway-request-timeout"));
|
|
82
|
+
}, GATEWAY_BOOT_TIMEOUT_MS);
|
|
83
|
+
})]);
|
|
84
|
+
} finally {
|
|
85
|
+
window2.clearTimeout(timer);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
async function requestGateway(fetch, path, body, signal) {
|
|
89
|
+
const response = await fetch(path, { method: "POST", credentials: "same-origin", cache: "no-store", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body), signal });
|
|
90
|
+
let data;
|
|
91
|
+
try {
|
|
92
|
+
data = await response.json();
|
|
93
|
+
} catch {
|
|
94
|
+
data = null;
|
|
95
|
+
}
|
|
96
|
+
return { ok: response.ok, status: response.status, data };
|
|
97
|
+
}
|
|
98
|
+
async function startGatewayPage(options = {}) {
|
|
99
|
+
var _a, _b, _c, _d;
|
|
100
|
+
const window2 = options.window, document2 = options.document, fetch = options.fetch;
|
|
101
|
+
const importTerminal = options.importTerminal ?? (() => import("/static/terminal.js"));
|
|
102
|
+
const launch = readGatewayLaunch(window2.location, window2.history);
|
|
103
|
+
const say = (key) => {
|
|
104
|
+
const words = GATEWAY_BOOT_WORDS[key] ?? GATEWAY_BOOT_WORDS.unavailable;
|
|
105
|
+
document2.getElementById("gateway-state-title").textContent = words[0];
|
|
106
|
+
document2.getElementById("gateway-state-text").textContent = words[1];
|
|
107
|
+
};
|
|
108
|
+
if (!launch) {
|
|
109
|
+
say("invalid");
|
|
110
|
+
return { state: "invalid" };
|
|
111
|
+
}
|
|
112
|
+
if (window2.matchMedia(`(max-width: ${GATEWAY_BROWSER_MIN_WIDTH - 1}px)`).matches) {
|
|
113
|
+
say("phone");
|
|
114
|
+
return { state: "phone" };
|
|
115
|
+
}
|
|
116
|
+
const consoleOrigin = (_a = document2.querySelector('meta[name="gateway-console-origin"]')) == null ? void 0 : _a.getAttribute("content");
|
|
117
|
+
try {
|
|
118
|
+
if (launch.handoff) {
|
|
119
|
+
if (typeof ((_c = (_b = window2.navigator) == null ? void 0 : _b.locks) == null ? void 0 : _c.request) !== "function") {
|
|
120
|
+
say("coordination");
|
|
121
|
+
return { state: "coordination" };
|
|
122
|
+
}
|
|
123
|
+
const exchange = await boundedGatewayCall(window2, (signal) => window2.navigator.locks.request("pi-dispatch-sandbox-bootstrap", { mode: "exclusive", signal }, () => requestGateway(fetch, "/api/bootstrap", launch, signal)));
|
|
124
|
+
if (!exchange.ok) {
|
|
125
|
+
say(gatewayBootRefusal(exchange.status, (_d = exchange.data) == null ? void 0 : _d.error));
|
|
126
|
+
return { state: "refused" };
|
|
127
|
+
}
|
|
128
|
+
const result = exchange.data;
|
|
129
|
+
const destination = gatewayAuthorizeUrl(result.authorizeUrl, consoleOrigin, launch.intentId);
|
|
130
|
+
if (!destination || !/^[A-Za-z0-9_-]{43}$/.test(result.receipt ?? "")) {
|
|
131
|
+
say("invalid");
|
|
132
|
+
return { state: "invalid" };
|
|
133
|
+
}
|
|
134
|
+
window2.location.replace(`${destination}#receipt=${result.receipt}`);
|
|
135
|
+
return { state: "authorize" };
|
|
136
|
+
}
|
|
137
|
+
const deadline = window2.performance.now() + 15e3;
|
|
138
|
+
say("loading");
|
|
139
|
+
const terminal = await boundedGatewayCall(window2, () => importTerminal());
|
|
140
|
+
if (typeof terminal.startWorkbenchLaunch !== "function") {
|
|
141
|
+
say("unavailable");
|
|
142
|
+
return { state: "unavailable" };
|
|
143
|
+
}
|
|
144
|
+
return await terminal.startWorkbenchLaunch({ window: window2, document: document2, intentId: launch.intentId, fetch, deadline, onRefusal: (status, reason) => say(gatewayBootRefusal(status, reason)) });
|
|
145
|
+
} catch {
|
|
146
|
+
say("unavailable");
|
|
147
|
+
return { state: "unavailable" };
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (typeof window !== "undefined" && typeof document !== "undefined") void startGatewayPage({ window, document, fetch: window.fetch.bind(window) });
|
|
151
|
+
})();
|