@vite-hub/runtime 0.1.0 → 0.3.1
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 +151 -117
- package/dist/durability.d.ts +107 -0
- package/dist/durability.js +168 -0
- package/dist/error-diagnostics-BKICpwZp.js +85 -0
- package/dist/error-diagnostics-BsJr9VdW.d.ts +15 -0
- package/dist/{errors-CbqHQTvn.js → errors-BEomN3fX.js} +1 -1
- package/dist/{execution-budget-D8SwJE8_.d.ts → execution-budget-BPXbISEt.d.ts} +9 -6
- package/dist/index-Bcl-Hr-a.d.ts +678 -0
- package/dist/index.d.ts +5 -4
- package/dist/index.js +5 -4
- package/dist/internal/drain.d.ts +17 -0
- package/dist/internal/drain.js +75 -0
- package/dist/internal/execution-budget.d.ts +1 -1
- package/dist/internal/grant.js +2 -2
- package/dist/internal/h3.d.ts +42 -0
- package/dist/internal/h3.js +181 -0
- package/dist/{node.d.ts → internal/node.d.ts} +2 -2
- package/dist/{node.js → internal/node.js} +4 -4
- package/dist/internal/public-url.d.ts +2 -0
- package/dist/internal/public-url.js +2 -0
- package/dist/internal/run-scope.d.ts +11 -0
- package/dist/internal/run-scope.js +30 -0
- package/dist/internal/runtime-type.d.ts +9 -2
- package/dist/internal/runtime-type.js +3 -2
- package/dist/{shutdown.d.ts → internal/shutdown.d.ts} +1 -1
- package/dist/{shutdown.js → internal/shutdown.js} +1 -1
- package/dist/internal/test-mode-setup.d.ts +7 -0
- package/dist/internal/test-mode-setup.js +114 -0
- package/dist/internal/test-mode.d.ts +32 -0
- package/dist/internal/test-mode.js +9 -0
- package/dist/operations.d.ts +167 -0
- package/dist/operations.js +406 -0
- package/dist/public-url-1WeOw54Z.d.ts +34 -0
- package/dist/public-url-B6x0HMgY.js +140 -0
- package/dist/{src-jy-gyPPu.js → request-context-CP9NhgLL.js} +635 -551
- package/dist/runtime-type-BftydMAf.js +88 -0
- package/package.json +61 -26
- package/dist/drain.d.ts +0 -1
- package/dist/drain.js +0 -69
- package/dist/error-diagnostics-Bkym4e6y.d.ts +0 -12
- package/dist/h3.d.ts +0 -49
- package/dist/h3.js +0 -404
- package/dist/index-Rl4DbP8B.d.ts +0 -574
- package/dist/runtime-type-CgfU6SWl.js +0 -52
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { t as runtimeErrorDiagnostics } from "./error-diagnostics-BKICpwZp.js";
|
|
2
|
+
//#region src/route-segment.ts
|
|
3
|
+
const plainSegment = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
4
|
+
function encodeRouteSegment(value) {
|
|
5
|
+
if (plainSegment.test(value)) return value;
|
|
6
|
+
let encoded = "~";
|
|
7
|
+
for (let index = 0; index < value.length; index++) encoded += value.charCodeAt(index).toString(16).padStart(4, "0");
|
|
8
|
+
return encoded;
|
|
9
|
+
}
|
|
10
|
+
function decodeRouteSegment(segment) {
|
|
11
|
+
if (plainSegment.test(segment)) return segment;
|
|
12
|
+
if (!/^~(?:[0-9a-f]{4})+$/.test(segment)) return;
|
|
13
|
+
let value = "";
|
|
14
|
+
for (let index = 1; index < segment.length; index += 4) value += String.fromCharCode(Number.parseInt(segment.slice(index, index + 4), 16));
|
|
15
|
+
return encodeRouteSegment(value) === segment ? value : void 0;
|
|
16
|
+
}
|
|
17
|
+
//#endregion
|
|
18
|
+
//#region src/hosted-console.ts
|
|
19
|
+
/** The origin of the hosted Console. */
|
|
20
|
+
const hostedConsoleOrigin = "https://console.vitehub.dev";
|
|
21
|
+
const loopbackHosts = /* @__PURE__ */ new Set([
|
|
22
|
+
"localhost",
|
|
23
|
+
"127.0.0.1",
|
|
24
|
+
"[::1]"
|
|
25
|
+
]);
|
|
26
|
+
const pathBase = "https://console.invalid";
|
|
27
|
+
/**
|
|
28
|
+
* The Console path of a link, such as `/agents/bot/invocations/abc?tab=trace`, or `undefined` when the value is not a
|
|
29
|
+
* Console path. A Console path starts with one `/`. Absolute URLs, protocol-relative URLs, and backslashes are not
|
|
30
|
+
* Console paths, so a link cannot send the browser to another site. A path that starts with `/_vitehub`, as on a
|
|
31
|
+
* Console that the app serves, loses that prefix.
|
|
32
|
+
*/
|
|
33
|
+
function normalizeConsolePath(path) {
|
|
34
|
+
if (!path.startsWith("/") || path.startsWith("//") || path.includes("\\")) return void 0;
|
|
35
|
+
let url;
|
|
36
|
+
try {
|
|
37
|
+
url = new URL(path, pathBase);
|
|
38
|
+
} catch {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
if (url.origin !== pathBase) return void 0;
|
|
42
|
+
const pathname = url.pathname.replace(/^\/_vitehub(?=\/|$)/, "") || "/";
|
|
43
|
+
if (pathname.startsWith("//")) return void 0;
|
|
44
|
+
return `${pathname}${url.search}${url.hash}`;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* The project URL of a link: the app origin and its application mount, such as `https://app.example.com/portal`, or
|
|
48
|
+
* `undefined` when the URL cannot be a project. A project URL is `https:`, or `http:` on a loopback host, and has no
|
|
49
|
+
* user name or password.
|
|
50
|
+
*/
|
|
51
|
+
function normalizeConsoleProjectUrl(projectUrl) {
|
|
52
|
+
let url;
|
|
53
|
+
try {
|
|
54
|
+
url = new URL(projectUrl);
|
|
55
|
+
} catch {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (url.username || url.password) return void 0;
|
|
59
|
+
if (url.protocol !== "https:" && !(url.protocol === "http:" && loopbackHosts.has(url.hostname))) return void 0;
|
|
60
|
+
const mount = url.pathname.search(/\/_vitehub(?=\/|$)/);
|
|
61
|
+
const base = (mount === -1 ? url.pathname : url.pathname.slice(0, mount)).replace(/\/+$/, "");
|
|
62
|
+
return `${url.origin}${base}`;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The hosted Console URL that opens one page of a project, such as
|
|
66
|
+
* `https://console.vitehub.dev/open?project=https%3A%2F%2Fapp.example.com&path=%2Fkv`.
|
|
67
|
+
*
|
|
68
|
+
* `projectUrl` is the app origin with its application mount, such as `https://app.example.com/portal`. `path` is the
|
|
69
|
+
* Console path, such as `/agents/bot/invocations/abc`. The hosted Console switches to the project when this browser
|
|
70
|
+
* linked it, or asks the person to link it first. The URL holds no secret.
|
|
71
|
+
*
|
|
72
|
+
* Throws `RUNTIME_CONSOLE_PROJECT_URL_INVALID` when `projectUrl` is not `https:` (or `http:` on a loopback host), and
|
|
73
|
+
* `RUNTIME_CONSOLE_PATH_INVALID` when `path` is not a Console path.
|
|
74
|
+
*/
|
|
75
|
+
function hostedConsoleUrl(projectUrl, path, consoleOrigin = hostedConsoleOrigin) {
|
|
76
|
+
const project = normalizeConsoleProjectUrl(projectUrl);
|
|
77
|
+
if (!project) throw runtimeErrorDiagnostics.RUNTIME_CONSOLE_PROJECT_URL_INVALID({ value: projectUrl });
|
|
78
|
+
const consolePath = normalizeConsolePath(path);
|
|
79
|
+
if (!consolePath) throw runtimeErrorDiagnostics.RUNTIME_CONSOLE_PATH_INVALID({ value: path });
|
|
80
|
+
const url = new URL("/open", consoleOrigin);
|
|
81
|
+
url.search = new URLSearchParams({
|
|
82
|
+
project,
|
|
83
|
+
path: consolePath
|
|
84
|
+
}).toString();
|
|
85
|
+
return url.href;
|
|
86
|
+
}
|
|
87
|
+
//#endregion
|
|
88
|
+
//#region src/public-url.ts
|
|
89
|
+
const publicUrlAgentNames = /* @__PURE__ */ new Map();
|
|
90
|
+
/** Replace aliases when a generated Agent registry is installed. */
|
|
91
|
+
function resetPublicUrlAgentNames() {
|
|
92
|
+
publicUrlAgentNames.clear();
|
|
93
|
+
}
|
|
94
|
+
/** Associate an evaluated Agent name with the identity used by build-time URL configuration. */
|
|
95
|
+
function registerPublicUrlAgentName(name, discoveredName) {
|
|
96
|
+
const previous = publicUrlAgentNames.get(name);
|
|
97
|
+
publicUrlAgentNames.set(name, previous === void 0 || previous === discoveredName ? discoveredName : null);
|
|
98
|
+
}
|
|
99
|
+
function publicUrlConfig() {
|
|
100
|
+
return typeof __VITEHUB_PUBLIC_URL__ === "undefined" ? {} : __VITEHUB_PUBLIC_URL__;
|
|
101
|
+
}
|
|
102
|
+
function applicationBasePath() {
|
|
103
|
+
const segments = (typeof __VITEHUB_APP_BASE_URL__ === "undefined" ? "/" : __VITEHUB_APP_BASE_URL__).split("/").filter(Boolean);
|
|
104
|
+
return segments.length ? `/${segments.join("/")}` : "";
|
|
105
|
+
}
|
|
106
|
+
function configuredAgentUrl(config, name) {
|
|
107
|
+
return config.agents && Object.hasOwn(config.agents, name) ? config.agents[name] : void 0;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Resolve the public origin of this deployment.
|
|
111
|
+
*
|
|
112
|
+
* A configured origin always wins over the request: the single `vitehub({ publicUrl })` origin, then the origin of
|
|
113
|
+
* the Agent, then, for a request, the configured origin with the same host as the request or the only configured origin. When
|
|
114
|
+
* `publicUrl` configures several origins and none of them fits, the result is `undefined`: the request Host cannot
|
|
115
|
+
* name a new origin. Only when nothing is configured, such as in `vite dev`, the request origin is the result.
|
|
116
|
+
* Set `configuredOnly` when the request must not supply it in that case either.
|
|
117
|
+
*/
|
|
118
|
+
function resolvePublicUrl(options = {}) {
|
|
119
|
+
const config = publicUrlConfig();
|
|
120
|
+
if (config.url) return config.url;
|
|
121
|
+
const discoveredName = options.agentName ? publicUrlAgentNames.get(options.agentName) : void 0;
|
|
122
|
+
const agentUrl = options.agentName ? configuredAgentUrl(config, options.agentName) ?? (discoveredName ? configuredAgentUrl(config, discoveredName) : void 0) : void 0;
|
|
123
|
+
if (agentUrl || !options.request) return agentUrl;
|
|
124
|
+
const request = new URL(options.request.url);
|
|
125
|
+
const configured = [...new Set(Object.values(config.agents ?? {}))];
|
|
126
|
+
if (configured.length) return configured.find((url) => new URL(url).host === request.host) ?? (configured.length === 1 ? configured[0] : void 0);
|
|
127
|
+
return options.configuredOnly ? void 0 : request.origin;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Build the hosted Console URL that opens one Agent invocation, such as
|
|
131
|
+
* `https://console.vitehub.dev/open?project=https%3A%2F%2Fapp.example.com&path=%2Fagents%2Fbot%2Finvocations%2Fid`.
|
|
132
|
+
* `origin` must not include the application base path. The hosted Console checks the project URL when it opens the link.
|
|
133
|
+
*/
|
|
134
|
+
function consoleInvocationUrl(origin, agentName, invocationId) {
|
|
135
|
+
const project = `${new URL(origin).origin}${applicationBasePath()}`;
|
|
136
|
+
const path = `/agents/${encodeRouteSegment(agentName)}/invocations/${encodeURIComponent(invocationId)}`;
|
|
137
|
+
return `${hostedConsoleOrigin}/open?project=${encodeURIComponent(project)}&path=${encodeURIComponent(path)}`;
|
|
138
|
+
}
|
|
139
|
+
//#endregion
|
|
140
|
+
export { hostedConsoleOrigin as a, normalizeConsoleProjectUrl as c, resolvePublicUrl as i, decodeRouteSegment as l, registerPublicUrlAgentName as n, hostedConsoleUrl as o, resetPublicUrlAgentNames as r, normalizeConsolePath as s, consoleInvocationUrl as t, encodeRouteSegment as u };
|