@vite-hub/workspace 0.0.4 → 0.3.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 +339 -115
- package/dist/abort-BCwpdrN0.js +32 -0
- package/dist/{asset-registry-8QI3ML3K.js → asset-registry-DfBV5kmw.js} +3 -2
- package/dist/blob-database.d.ts +474 -0
- package/dist/blob-database.js +608 -0
- package/dist/checkout.d.ts +128 -0
- package/dist/checkout.js +849 -0
- package/dist/cloudflare.d.ts +6 -7
- package/dist/cloudflare.js +1 -1
- package/dist/config-B-863EgC.js +15 -0
- package/dist/config-BAgL2dtB.js +1076 -0
- package/dist/{config-DE9xlaIw.d.ts → config-BQKcPawI.d.ts} +1 -2
- package/dist/discard-CNNtCozM.js +8 -0
- package/dist/durability.d.ts +22 -0
- package/dist/durability.js +70 -0
- package/dist/error-diagnostics-CtKjGtGb.js +188 -0
- package/dist/error-diagnostics-DSkCJC_L.js +266 -0
- package/dist/file-2pFYNtsU.js +22 -0
- package/dist/{files-Q6xZjDHA.js → files-BOo4SbwE.js} +3 -3
- package/dist/files.d.ts +38 -0
- package/dist/files.js +6 -0
- package/dist/{github-DHnjxQIX.js → github-CrYp4v8k.js} +44 -24
- package/dist/glob-B8YGvTrZ.js +21 -0
- package/dist/glob-Cjxms1t7.js +30 -0
- package/dist/{host-DJH8cuoU.js → host-BFBpXn0N.js} +144 -77
- package/dist/{hosted-RPeJOCFz.js → hosted-KcUR_jPv.js} +303 -97
- package/dist/hosted-vercel-blob-B3-wRdUm.js +410 -0
- package/dist/hosted-vercel-blob.d.ts +6 -7
- package/dist/hosted-vercel-blob.js +1 -1
- package/dist/hosted.d.ts +10 -9
- package/dist/hosted.js +2 -2
- package/dist/index.d.ts +26 -47
- package/dist/index.js +30 -100
- package/dist/internal/path.d.ts +2 -0
- package/dist/internal/path.js +2 -0
- package/dist/lifecycle-B-enk_4A.js +596 -0
- package/dist/lineage.d.ts +26 -0
- package/dist/lineage.js +76 -0
- package/dist/loader.d.ts +5 -6
- package/dist/loader.js +3 -2
- package/dist/mcp-resources-DF0F5rcL.js +39 -0
- package/dist/memory-BOexthyW.js +247 -0
- package/dist/metadata-target-CWz6KHKq.js +1589 -0
- package/dist/object-Da1zyPCR.js +11 -0
- package/dist/operation-definition-8gOe-ubD.d.ts +234 -0
- package/dist/operations-C6FpR8Rr.js +389 -0
- package/dist/operations-CE2drFih.js +1234 -0
- package/dist/operations.d.ts +90 -0
- package/dist/operations.js +3 -0
- package/dist/path--wrT_Azm.js +113 -0
- package/dist/path-AxMho1wo.d.ts +7 -0
- package/dist/path-DrSIenjj.js +59 -0
- package/dist/path-zHxQUbqa.d.ts +9 -0
- package/dist/plugins-C8C0auoW.d.ts +10 -0
- package/dist/provider-9s4Ufkm5.js +1479 -0
- package/dist/providers/github/store.d.ts +3 -5
- package/dist/providers/github/store.js +1 -1
- package/dist/registry-B3BBm4Ts.d.ts +19 -0
- package/dist/{registry-D3yzt8HQ.js → registry-CNphafBx.js} +60 -23
- package/dist/registry-CYNN623b.d.ts +9 -0
- package/dist/request-scope-Dw5n8bMC.js +83 -0
- package/dist/results-DgkZUmTL.js +8 -0
- package/dist/{chunk-2rV9d50f.js → rolldown-runtime-rQ84J-ij.js} +3 -4
- package/dist/runtime/assets.d.ts +3 -5
- package/dist/runtime/assets.js +7 -6
- package/dist/runtime/empty-assets-registry.d.ts +1 -2
- package/dist/runtime/empty-registry.d.ts +2 -2
- package/dist/runtime/registry.d.ts +2 -0
- package/dist/runtime/registry.js +23 -0
- package/dist/runtime/request.d.ts +4 -0
- package/dist/runtime/request.js +8 -0
- package/dist/runtime/state.d.ts +5 -4
- package/dist/runtime/state.js +8 -18
- package/dist/runtime/workspace.d.ts +1 -1
- package/dist/runtime/workspace.js +1 -1
- package/dist/runtime.d.ts +52 -17
- package/dist/runtime.js +324 -67
- package/dist/{search-BpiCF2Tw.js → search-C_RfoM2j.js} +10 -9
- package/dist/server-8tGltlGz.js +303 -0
- package/dist/server.d.ts +36 -39
- package/dist/server.js +2 -328
- package/dist/shared-B3_EUk89.js +848 -0
- package/dist/source-metadata.d.ts +10 -7
- package/dist/source-metadata.js +12 -12
- package/dist/state-BISnUf1s.d.ts +35 -0
- package/dist/{ai-BqCC8BpY.js → state-DbrW-njQ.js} +237 -114
- package/dist/store-ybhyD38Z.js +1873 -0
- package/dist/{sync-DnTQwt8r.js → sync-CCD_VYC8.js} +61 -41
- package/dist/target-jWhTdIuF.js +89 -0
- package/dist/test.d.ts +1 -1
- package/dist/test.js +1 -1
- package/dist/{types-CYuwjJ4V.d.ts → types-BbtwmBZ5.d.ts} +321 -35
- package/dist/use-C6lV3-wC.d.ts +321 -0
- package/dist/{use-BTNDR5Rd.js → use-CSpT5W87.js} +141 -107
- package/dist/utils-CgMZCHsX.js +616 -0
- package/dist/vite.d.ts +585 -2
- package/dist/vite.js +21369 -2
- package/dist/workspace-CYB2xZ1T.d.ts +7 -0
- package/dist/{workspace-Dq08Yn72.js → workspace-sPWn2Jxj.js} +441 -225
- package/package.json +97 -72
- package/dist/ai-CjxvGqM0.d.ts +0 -162
- package/dist/ai.d.ts +0 -3
- package/dist/ai.js +0 -2
- package/dist/collections/client.d.ts +0 -39
- package/dist/collections/client.js +0 -137
- package/dist/collections.d.ts +0 -59
- package/dist/collections.js +0 -159
- package/dist/config-Bc3c4Su1.js +0 -7904
- package/dist/config-CHLLWklZ.js +0 -36
- package/dist/file-Bc1X7pco.js +0 -23
- package/dist/glob-CEZKeZFC.js +0 -18
- package/dist/hosted-vercel-blob-d6C8ND5j.js +0 -23580
- package/dist/lifecycle-LrL3sA4n.js +0 -316
- package/dist/metadata-target-B0tOmVW7.js +0 -717
- package/dist/mountx.d.ts +0 -10
- package/dist/mountx.js +0 -143
- package/dist/nitro.d.ts +0 -2
- package/dist/nitro.js +0 -2
- package/dist/nuxt.d.ts +0 -26
- package/dist/nuxt.js +0 -42
- package/dist/path-Cp59koVz.js +0 -139
- package/dist/provider-B1yLjCJJ.js +0 -746
- package/dist/publish.d.ts +0 -35
- package/dist/publish.js +0 -112
- package/dist/registry-Uj6IVxes.d.ts +0 -11
- package/dist/request-execution-B-dSFlGB.js +0 -84
- package/dist/runtime-options-BsPSv5O7.js +0 -13
- package/dist/runtime-type-iEE6BjQJ.js +0 -34
- package/dist/shared-DTR1Hhma.js +0 -331
- package/dist/state-tYcRWEh2.d.ts +0 -26
- package/dist/store-Dt_Jyys3.js +0 -608
- package/dist/target-D4Bqb1eH.js +0 -11
- package/dist/token-CVzURBxC.js +0 -61
- package/dist/use-BiHzt0xA.d.ts +0 -103
- package/dist/vite-BptCrpv2.js +0 -4526
- package/dist/vite-CtA9guIM.d.ts +0 -66
- package/dist/workspace-Xddf4G1B.d.ts +0 -6
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
import { n as __exportAll } from "./
|
|
2
|
-
import { l as normalizeWorkspacePath } from "./path
|
|
1
|
+
import { n as __exportAll } from "./rolldown-runtime-rQ84J-ij.js";
|
|
2
|
+
import { l as normalizeWorkspacePath } from "./path--wrT_Azm.js";
|
|
3
3
|
//#region src/core/search.ts
|
|
4
|
-
var search_exports = /* @__PURE__ */ __exportAll({
|
|
5
|
-
searchText: () => searchText,
|
|
6
|
-
toSearchRegExp: () => toSearchRegExp
|
|
7
|
-
});
|
|
4
|
+
var search_exports = /* @__PURE__ */ __exportAll({ searchText: () => searchText });
|
|
8
5
|
function toSearchRegExp(query) {
|
|
9
6
|
const source = query.regex ? query.pattern : escapeRegExp(query.pattern);
|
|
10
7
|
return new RegExp(source, query.caseSensitive ? "g" : "gi");
|
|
@@ -12,10 +9,12 @@ function toSearchRegExp(query) {
|
|
|
12
9
|
function searchText(path, content, query) {
|
|
13
10
|
const result = [];
|
|
14
11
|
const expression = toSearchRegExp(query);
|
|
15
|
-
const lines = content.split(/\r?\n/);
|
|
16
12
|
const limit = query.limit ?? 100;
|
|
17
|
-
|
|
18
|
-
|
|
13
|
+
let start = 0;
|
|
14
|
+
for (let index = 0; start <= content.length && result.length < limit; index++) {
|
|
15
|
+
const newline = content.indexOf("\n", start);
|
|
16
|
+
const end = newline === -1 ? content.length : newline;
|
|
17
|
+
const line = content.slice(start, newline !== -1 && content[end - 1] === "\r" ? end - 1 : end);
|
|
19
18
|
expression.lastIndex = 0;
|
|
20
19
|
let match = expression.exec(line);
|
|
21
20
|
while (match && result.length < limit) {
|
|
@@ -29,6 +28,8 @@ function searchText(path, content, query) {
|
|
|
29
28
|
if (match[0] === "") expression.lastIndex += 1;
|
|
30
29
|
match = expression.exec(line);
|
|
31
30
|
}
|
|
31
|
+
if (newline === -1) break;
|
|
32
|
+
start = newline + 1;
|
|
32
33
|
}
|
|
33
34
|
return result;
|
|
34
35
|
}
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import { t as normalizeSafeWorkspacePath } from "./path-DrSIenjj.js";
|
|
2
|
+
import { s as matchesAny } from "./path--wrT_Azm.js";
|
|
3
|
+
import { t as workspaceErrorDiagnostics } from "./error-diagnostics-CtKjGtGb.js";
|
|
4
|
+
import { _ as workspaceDefinitionPlugins, h as resolveWorkspaceAutoCommit } from "./utils-CgMZCHsX.js";
|
|
5
|
+
import { r as useWorkspaceFiles } from "./use-CSpT5W87.js";
|
|
6
|
+
import { r as withWorkspaceProgress } from "./host-BFBpXn0N.js";
|
|
7
|
+
import { getViteHubErrorShape } from "@vite-hub/runtime";
|
|
8
|
+
import { lookup } from "mrmime";
|
|
9
|
+
//#region ../internal/src/secret.ts
|
|
10
|
+
const secretEncoder = new TextEncoder();
|
|
11
|
+
/**
|
|
12
|
+
* Compares a request value with an expected secret in constant time for the length of `expected`. Returns `false`
|
|
13
|
+
* when either value is missing or empty. It uses no Node API, so it also runs in Worker runtimes.
|
|
14
|
+
*/
|
|
15
|
+
function isViteHubSecretEqual(actual, expected) {
|
|
16
|
+
if (!actual || !expected) return false;
|
|
17
|
+
const left = secretEncoder.encode(actual);
|
|
18
|
+
const right = secretEncoder.encode(expected);
|
|
19
|
+
let difference = left.length ^ right.length;
|
|
20
|
+
for (let index = 0; index < right.length; index += 1) difference |= (left[index] ?? 0) ^ right[index];
|
|
21
|
+
return difference === 0;
|
|
22
|
+
}
|
|
23
|
+
Object.freeze({ prefix: "sha256=" });
|
|
24
|
+
//#endregion
|
|
25
|
+
//#region src/server.ts
|
|
26
|
+
const workspaceDevRoute = "/__vitehub/workspace/dev";
|
|
27
|
+
const workspaceDevHeader = "x-vitehub-workspace-dev";
|
|
28
|
+
const workspaceDevHeaderValue = "1";
|
|
29
|
+
const workspaceDevTokenHeader = "x-vitehub-dev-token";
|
|
30
|
+
function hasWorkspaceCommitRules(definition) {
|
|
31
|
+
return [...Object.values(definition.rules || {}), ...workspaceDefinitionPlugins(definition).flatMap((plugin) => Object.values(plugin.rules || {}))].some((rule) => rule.commit !== void 0);
|
|
32
|
+
}
|
|
33
|
+
const workspaceDevTokensKey = Symbol.for("vitehub.workspace.devTokens");
|
|
34
|
+
function workspaceDevTokens() {
|
|
35
|
+
const scope = globalThis;
|
|
36
|
+
scope[workspaceDevTokensKey] ??= /* @__PURE__ */ new Map();
|
|
37
|
+
return scope[workspaceDevTokensKey];
|
|
38
|
+
}
|
|
39
|
+
function workspaceDevTokenServerId(port) {
|
|
40
|
+
return `${process.pid}:${port ?? "unknown"}`;
|
|
41
|
+
}
|
|
42
|
+
async function workspaceDevTokenRoot(rootDir, options = {}) {
|
|
43
|
+
const [{ createHash }, { tmpdir }, { join, resolve }] = await Promise.all([
|
|
44
|
+
import("node:crypto"),
|
|
45
|
+
import("node:os"),
|
|
46
|
+
import("node:path")
|
|
47
|
+
]);
|
|
48
|
+
const resolvedRoot = resolve(rootDir);
|
|
49
|
+
const rootKey = createHash("sha256").update(resolvedRoot).digest("hex");
|
|
50
|
+
const serverKey = createHash("sha256").update(options.serverId || "default").digest("hex");
|
|
51
|
+
return {
|
|
52
|
+
file: join(tmpdir(), "vitehub-workspace-dev", rootKey, serverKey, "dev-token"),
|
|
53
|
+
key: `${rootKey}:${serverKey}`
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
function headerValue(headers, name, expectedToken) {
|
|
57
|
+
try {
|
|
58
|
+
const prototype = Object.getPrototypeOf(headers);
|
|
59
|
+
const constructor = prototype ? Object.getOwnPropertyDescriptor(prototype, "constructor")?.value : void 0;
|
|
60
|
+
const constructorSource = typeof constructor === "function" ? Function.prototype.toString.call(constructor) : "";
|
|
61
|
+
const prototypeGet = prototype ? Object.getOwnPropertyDescriptor(prototype, "get")?.value : void 0;
|
|
62
|
+
const realmHeaders = typeof Headers === "function" ? Headers : void 0;
|
|
63
|
+
if (typeof constructor === "function" && Reflect.get(constructor, "name") === "Headers" && (constructor === realmHeaders || /^class\s+Headers\b/.test(constructorSource) && constructorSource.includes("#") || /^function\s+Headers\(\)\s*\{\s*\[native code\]\s*\}$/.test(constructorSource) || constructorSource.includes("webidl.brandCheck") || constructorSource.includes("#")) && Reflect.get(constructor, "prototype") === prototype && (() => {
|
|
64
|
+
try {
|
|
65
|
+
return headers instanceof constructor;
|
|
66
|
+
} catch {
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
})() && typeof prototypeGet === "function") {
|
|
70
|
+
const get = typeof Headers === "function" ? Reflect.get(Headers.prototype, "get") : void 0;
|
|
71
|
+
for (const getter of [get, prototypeGet]) {
|
|
72
|
+
if (typeof getter !== "function") continue;
|
|
73
|
+
try {
|
|
74
|
+
const value = Reflect.apply(getter, headers, [name]);
|
|
75
|
+
return typeof value === "string" && value ? value : void 0;
|
|
76
|
+
} catch {}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
try {
|
|
80
|
+
const ownGet = Object.hasOwn(headers, "get") ? Reflect.get(headers, "get") : void 0;
|
|
81
|
+
if (typeof ownGet === "function") {
|
|
82
|
+
const value = Reflect.apply(ownGet, headers, [name]);
|
|
83
|
+
return typeof value === "string" && value ? value : void 0;
|
|
84
|
+
}
|
|
85
|
+
if (!Object.hasOwn(headers, name)) return void 0;
|
|
86
|
+
const value = Reflect.get(headers, name);
|
|
87
|
+
if (Array.isArray(value)) return typeof value[0] === "string" ? value[0] : void 0;
|
|
88
|
+
return typeof value === "string" ? value : void 0;
|
|
89
|
+
} catch {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
} catch {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
function randomToken() {
|
|
97
|
+
return [...globalThis.crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(32))].map((byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
98
|
+
}
|
|
99
|
+
async function refreshWorkspaceDevToken(rootDir, options = {}) {
|
|
100
|
+
const token = randomToken();
|
|
101
|
+
const tokenRoot = await workspaceDevTokenRoot(rootDir, options);
|
|
102
|
+
workspaceDevTokens().set(tokenRoot.key, token);
|
|
103
|
+
const [{ mkdir, writeFile }, { dirname }] = await Promise.all([import("node:fs/promises"), import("node:path")]);
|
|
104
|
+
const file = tokenRoot.file;
|
|
105
|
+
await mkdir(dirname(file), {
|
|
106
|
+
mode: 448,
|
|
107
|
+
recursive: true
|
|
108
|
+
});
|
|
109
|
+
await writeFile(file, `${token}\n`, { mode: 384 });
|
|
110
|
+
return token;
|
|
111
|
+
}
|
|
112
|
+
async function ensureWorkspaceDevToken(rootDir, options = {}) {
|
|
113
|
+
return workspaceDevTokens().get((await workspaceDevTokenRoot(rootDir, options)).key) || await refreshWorkspaceDevToken(rootDir, options);
|
|
114
|
+
}
|
|
115
|
+
async function readWorkspaceDevToken(rootDir, options = {}) {
|
|
116
|
+
try {
|
|
117
|
+
const { readFile } = await import("node:fs/promises");
|
|
118
|
+
return (await readFile((await workspaceDevTokenRoot(rootDir, options)).file, "utf8")).trim() || void 0;
|
|
119
|
+
} catch (error) {
|
|
120
|
+
if (error && typeof error === "object" && "code" in error && error.code === "ENOENT") return;
|
|
121
|
+
throw error;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
async function validateWorkspaceDevToken(rootDir, headers, options = {}) {
|
|
125
|
+
const expectedToken = await ensureWorkspaceDevToken(rootDir, options);
|
|
126
|
+
return isViteHubSecretEqual(headerValue(headers, workspaceDevTokenHeader, expectedToken), expectedToken);
|
|
127
|
+
}
|
|
128
|
+
async function resolveWorkspace(workspace) {
|
|
129
|
+
if (typeof workspace === "string") return useWorkspaceFiles(workspace);
|
|
130
|
+
if (typeof workspace === "function") return await workspace();
|
|
131
|
+
return workspace;
|
|
132
|
+
}
|
|
133
|
+
function resolveWorkspaceFilePath(path, root) {
|
|
134
|
+
const normalizedPath = normalizeSafeWorkspacePath(path, { allowEmpty: false });
|
|
135
|
+
if (!root) return normalizedPath;
|
|
136
|
+
return `${normalizeSafeWorkspacePath(root, { allowEmpty: false })}/${normalizedPath}`;
|
|
137
|
+
}
|
|
138
|
+
function resolveContentType(path, mediaType, contentTypes) {
|
|
139
|
+
if (mediaType) return mediaType;
|
|
140
|
+
if (contentTypes && Object.hasOwn(contentTypes, path) && contentTypes[path]) return contentTypes[path];
|
|
141
|
+
const extension = path.includes(".") ? `.${path.split(".").at(-1)}` : "";
|
|
142
|
+
if (extension && contentTypes && Object.hasOwn(contentTypes, extension) && contentTypes[extension]) return contentTypes[extension];
|
|
143
|
+
return lookup(path) || "application/octet-stream";
|
|
144
|
+
}
|
|
145
|
+
function errorMessage(error) {
|
|
146
|
+
if (Object(error) !== error) return;
|
|
147
|
+
try {
|
|
148
|
+
const message = Object.getOwnPropertyDescriptor(error, "message")?.value;
|
|
149
|
+
return message === String(message) ? String(message) : void 0;
|
|
150
|
+
} catch {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
function isNotFoundError(error) {
|
|
155
|
+
const code = getViteHubErrorShape(error)?.code;
|
|
156
|
+
const message = errorMessage(error);
|
|
157
|
+
return code === "WORKSPACE_NOT_FOUND" || code === "WORKSPACE_PATH_INVALID" || message?.includes("Workspace file does not exist:") === true || message?.includes("Workspace path does not exist:") === true;
|
|
158
|
+
}
|
|
159
|
+
function workspaceSessionStarter(input) {
|
|
160
|
+
return input && typeof input === "object" && typeof input.startSession === "function" ? input : void 0;
|
|
161
|
+
}
|
|
162
|
+
function workspaceSourceMaterializer(input) {
|
|
163
|
+
return input && typeof input === "object" && typeof input.materializeSources === "function" ? input.materializeSources.bind(input) : void 0;
|
|
164
|
+
}
|
|
165
|
+
async function materializeWorkspaceDevSources(workspace, input) {
|
|
166
|
+
const materialize = workspaceSourceMaterializer(workspace) ?? workspaceSourceMaterializer(workspace.fs);
|
|
167
|
+
if (!materialize) return;
|
|
168
|
+
const paths = input.paths?.length ? input.paths : [""];
|
|
169
|
+
await withWorkspaceProgress(input.onProgress, {
|
|
170
|
+
data: { paths },
|
|
171
|
+
id: "workspace.dev.materialize",
|
|
172
|
+
label: "Materializing workspace sources"
|
|
173
|
+
}, async () => {
|
|
174
|
+
await Promise.all(paths.map(async (path) => {
|
|
175
|
+
await materialize({
|
|
176
|
+
abortSignal: input.abortSignal,
|
|
177
|
+
onProgress: async (event) => await input.onProgress?.({
|
|
178
|
+
data: { ...event },
|
|
179
|
+
durationMs: event.durationMs,
|
|
180
|
+
error: event.error,
|
|
181
|
+
id: `workspace.dev.materialize.${event.source}`,
|
|
182
|
+
label: `Materializing source ${event.source}`,
|
|
183
|
+
status: event.status
|
|
184
|
+
}),
|
|
185
|
+
path
|
|
186
|
+
}).catch((error) => {
|
|
187
|
+
if (path && isNotFoundError(error)) return;
|
|
188
|
+
throw error;
|
|
189
|
+
});
|
|
190
|
+
}));
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
function workspaceFileNotFound() {
|
|
194
|
+
return Response.json({
|
|
195
|
+
status: 404,
|
|
196
|
+
message: "Workspace file not found."
|
|
197
|
+
}, { status: 404 });
|
|
198
|
+
}
|
|
199
|
+
/** Read a Workspace file as a Response. A missing, disallowed, or unsafe path gets a 404 Response. */
|
|
200
|
+
async function readWorkspaceFileResponse(options) {
|
|
201
|
+
let filePath;
|
|
202
|
+
try {
|
|
203
|
+
filePath = resolveWorkspaceFilePath(options.path, options.root);
|
|
204
|
+
} catch (error) {
|
|
205
|
+
if (isNotFoundError(error)) return workspaceFileNotFound();
|
|
206
|
+
throw error;
|
|
207
|
+
}
|
|
208
|
+
if (!matchesAny(filePath, options.allow)) return workspaceFileNotFound();
|
|
209
|
+
const workspace = await resolveWorkspace(options.workspace);
|
|
210
|
+
try {
|
|
211
|
+
const [content, stat] = await Promise.all([workspace.fs.readFile(filePath, { encoding: "binary" }), workspace.fs.stat(filePath)]);
|
|
212
|
+
if (stat.type !== "file") return workspaceFileNotFound();
|
|
213
|
+
const headers = new Headers(options.headers);
|
|
214
|
+
headers.set("content-type", resolveContentType(filePath, stat.mediaType, options.contentTypes));
|
|
215
|
+
headers.set("x-content-type-options", "nosniff");
|
|
216
|
+
if (options.cacheControl) headers.set("cache-control", options.cacheControl);
|
|
217
|
+
return new Response(content, { headers });
|
|
218
|
+
} catch (error) {
|
|
219
|
+
if (isNotFoundError(error)) return workspaceFileNotFound();
|
|
220
|
+
throw error;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Serve Workspace files from a route. The file path is the part of the URL path after `base`, percent-decoded, so
|
|
225
|
+
* `/files/my%20file.md` serves `my file.md`. A path that is not valid percent-encoding gets a 400 Response.
|
|
226
|
+
*/
|
|
227
|
+
function defineWorkspaceFileHandler(options) {
|
|
228
|
+
const { base, baseURL, ...fileOptions } = options;
|
|
229
|
+
const segments = [baseURL ?? "", base].map((part) => part.replace(/^\/+|\/+$/g, "")).filter(Boolean).join("/");
|
|
230
|
+
const prefix = segments ? `/${segments}/` : "/";
|
|
231
|
+
return { async fetch(request) {
|
|
232
|
+
const pathname = new URL(request.url).pathname;
|
|
233
|
+
if (baseURL !== void 0 && !pathname.startsWith(prefix)) return workspaceFileNotFound();
|
|
234
|
+
const start = baseURL === void 0 ? pathname.indexOf(prefix) : 0;
|
|
235
|
+
if (start < 0) return workspaceFileNotFound();
|
|
236
|
+
let path;
|
|
237
|
+
try {
|
|
238
|
+
path = decodeURIComponent(pathname.slice(start + prefix.length));
|
|
239
|
+
} catch {
|
|
240
|
+
return Response.json({
|
|
241
|
+
status: 400,
|
|
242
|
+
message: "Workspace file path is not valid percent-encoding."
|
|
243
|
+
}, { status: 400 });
|
|
244
|
+
}
|
|
245
|
+
return await readWorkspaceFileResponse({
|
|
246
|
+
...fileOptions,
|
|
247
|
+
path
|
|
248
|
+
});
|
|
249
|
+
} };
|
|
250
|
+
}
|
|
251
|
+
async function runWorkspaceDevCommand(input) {
|
|
252
|
+
const command = input.command.trim();
|
|
253
|
+
if (!command) throw workspaceErrorDiagnostics.WORKSPACE_R0040({ message: "Workspace Dev command cannot be empty." });
|
|
254
|
+
const definition = input.definition;
|
|
255
|
+
const workspace = typeof input.workspace === "string" ? await useWorkspaceFiles(input.workspace, definition ? {
|
|
256
|
+
definition,
|
|
257
|
+
mode: "write"
|
|
258
|
+
} : { mode: "write" }) : input.workspace;
|
|
259
|
+
const starter = workspaceSessionStarter(workspace) ?? workspaceSessionStarter(workspace.fs);
|
|
260
|
+
const startSession = starter?.startSession.bind(starter);
|
|
261
|
+
if (!startSession) throw workspaceErrorDiagnostics.WORKSPACE_R0041({ message: "Workspace Dev command requires a Workspace Session." });
|
|
262
|
+
await materializeWorkspaceDevSources(workspace, input);
|
|
263
|
+
let session;
|
|
264
|
+
const execOptions = {
|
|
265
|
+
abortSignal: input.abortSignal,
|
|
266
|
+
timeout: input.timeout
|
|
267
|
+
};
|
|
268
|
+
let result;
|
|
269
|
+
try {
|
|
270
|
+
await withWorkspaceProgress(input.onProgress, {
|
|
271
|
+
data: { paths: input.paths ?? null },
|
|
272
|
+
id: "workspace.dev.start-session",
|
|
273
|
+
label: "Starting workspace session"
|
|
274
|
+
}, async () => {
|
|
275
|
+
session = await startSession({
|
|
276
|
+
abortSignal: input.abortSignal,
|
|
277
|
+
host: input.host,
|
|
278
|
+
paths: input.paths
|
|
279
|
+
});
|
|
280
|
+
return session;
|
|
281
|
+
});
|
|
282
|
+
if (!session) throw workspaceErrorDiagnostics.WORKSPACE_R0042({ message: "Workspace Dev Session did not start." });
|
|
283
|
+
result = input.args ? await session.exec(command, input.args, execOptions) : await session.exec("sh", ["-c", command], execOptions);
|
|
284
|
+
const diff = result.exitCode === 0 ? await session.diff() : void 0;
|
|
285
|
+
if (diff?.entries.length) {
|
|
286
|
+
const commit = definition ? resolveWorkspaceAutoCommit(definition, diff) : void 0;
|
|
287
|
+
if (!definition || !hasWorkspaceCommitRules(definition) || commit) await session.commit({ message: commit?.message || "workspace dev command" });
|
|
288
|
+
}
|
|
289
|
+
} catch (error) {
|
|
290
|
+
if (!session) throw error;
|
|
291
|
+
try {
|
|
292
|
+
await session.close();
|
|
293
|
+
} catch (closeError) {
|
|
294
|
+
throw new AggregateError([error, closeError], "[vitehub] Workspace Dev command failed and session cleanup also failed.");
|
|
295
|
+
}
|
|
296
|
+
throw error;
|
|
297
|
+
}
|
|
298
|
+
if (!session) throw workspaceErrorDiagnostics.WORKSPACE_R0043({ message: "Workspace Dev Session did not start." });
|
|
299
|
+
await session.close();
|
|
300
|
+
return result;
|
|
301
|
+
}
|
|
302
|
+
//#endregion
|
|
303
|
+
export { refreshWorkspaceDevToken as a, workspaceDevHeader as c, workspaceDevTokenHeader as d, workspaceDevTokenServerId as f, readWorkspaceFileResponse as i, workspaceDevHeaderValue as l, ensureWorkspaceDevToken as n, runWorkspaceDevCommand as o, isViteHubSecretEqual as p, readWorkspaceDevToken as r, validateWorkspaceDevToken as s, defineWorkspaceFileHandler as t, workspaceDevRoute as u };
|
package/dist/server.d.ts
CHANGED
|
@@ -1,19 +1,16 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { WorkspaceCollectionOptions, WorkspaceCollectionSort } from "./collections.js";
|
|
4
|
-
import { defineEventHandler } from "h3";
|
|
5
|
-
|
|
1
|
+
import { Bt as WorkspaceSessionHost, Dt as WorkspacePrepareSessionProgressEvent, I as WorkspaceDefinition, St as WorkspaceName, Ut as WorkspaceSessionOptions, i as ExecResult, zt as WorkspaceSession } from "./types-BbtwmBZ5.js";
|
|
2
|
+
import { _ as WritableWorkspaceFiles, a as ReadonlyWorkspaceFiles } from "./use-C6lV3-wC.js";
|
|
6
3
|
//#region src/server.d.ts
|
|
7
|
-
declare const workspaceDevRoute = "/__vitehub/workspace/dev";
|
|
8
|
-
declare const workspaceDevHeader = "x-vitehub-workspace-dev";
|
|
9
|
-
declare const workspaceDevHeaderValue = "1";
|
|
10
|
-
declare const workspaceDevTokenHeader = "x-vitehub-dev-token";
|
|
11
|
-
type WorkspaceInput<Name extends WorkspaceName> = Name |
|
|
4
|
+
export declare const workspaceDevRoute = "/__vitehub/workspace/dev";
|
|
5
|
+
export declare const workspaceDevHeader = "x-vitehub-workspace-dev";
|
|
6
|
+
export declare const workspaceDevHeaderValue = "1";
|
|
7
|
+
export declare const workspaceDevTokenHeader = "x-vitehub-dev-token";
|
|
8
|
+
type WorkspaceInput<Name extends WorkspaceName> = Name | ReadonlyWorkspaceFiles<Name> | WritableWorkspaceFiles<Name> | (() => ReadonlyWorkspaceFiles<Name> | WritableWorkspaceFiles<Name> | Promise<ReadonlyWorkspaceFiles<Name> | WritableWorkspaceFiles<Name>>);
|
|
12
9
|
type WorkspaceSessionStarter = {
|
|
13
10
|
startSession(options?: WorkspaceSessionOptions): Promise<WorkspaceSession>;
|
|
14
11
|
};
|
|
15
12
|
type ResponseHeaders = ConstructorParameters<typeof Headers>[0];
|
|
16
|
-
interface WorkspaceFileResponseOptions<Name extends WorkspaceName = WorkspaceName> {
|
|
13
|
+
export interface WorkspaceFileResponseOptions<Name extends WorkspaceName = WorkspaceName> {
|
|
17
14
|
allow?: string[];
|
|
18
15
|
cacheControl?: string;
|
|
19
16
|
contentTypes?: Record<string, string>;
|
|
@@ -22,21 +19,13 @@ interface WorkspaceFileResponseOptions<Name extends WorkspaceName = WorkspaceNam
|
|
|
22
19
|
root?: string;
|
|
23
20
|
workspace: WorkspaceInput<Name>;
|
|
24
21
|
}
|
|
25
|
-
interface WorkspaceFileHandlerOptions<Name extends WorkspaceName = WorkspaceName> extends Omit<WorkspaceFileResponseOptions<Name>, "path"> {
|
|
26
|
-
|
|
22
|
+
export interface WorkspaceFileHandlerOptions<Name extends WorkspaceName = WorkspaceName> extends Omit<WorkspaceFileResponseOptions<Name>, "path"> {
|
|
23
|
+
/** The route path, such as `"/api/data/"`, relative to `baseURL`. The rest of the URL path is the file path. */
|
|
24
|
+
base: string;
|
|
25
|
+
/** The retained application prefix, such as Nitro's `baseURL`. Set this when the app is mounted below `/`. */
|
|
26
|
+
baseURL?: string;
|
|
27
27
|
}
|
|
28
|
-
interface
|
|
29
|
-
facets?: string[];
|
|
30
|
-
filters?: string[];
|
|
31
|
-
item?: {
|
|
32
|
-
key: string;
|
|
33
|
-
select?: string[];
|
|
34
|
-
};
|
|
35
|
-
searchFields?: string[];
|
|
36
|
-
select?: string[];
|
|
37
|
-
sort?: WorkspaceCollectionSort;
|
|
38
|
-
}
|
|
39
|
-
interface WorkspaceDevCommandInput<Name extends WorkspaceName = WorkspaceName> {
|
|
28
|
+
export interface WorkspaceDevCommandInput<Name extends WorkspaceName = WorkspaceName> {
|
|
40
29
|
abortSignal?: AbortSignal;
|
|
41
30
|
args?: string[];
|
|
42
31
|
command: string;
|
|
@@ -45,21 +34,29 @@ interface WorkspaceDevCommandInput<Name extends WorkspaceName = WorkspaceName> {
|
|
|
45
34
|
onProgress?: (event: WorkspacePrepareSessionProgressEvent) => void | Promise<void>;
|
|
46
35
|
paths?: readonly string[];
|
|
47
36
|
timeout?: number;
|
|
48
|
-
workspace: Name |
|
|
49
|
-
fs:
|
|
37
|
+
workspace: Name | WritableWorkspaceFiles<Name> | (ReadonlyWorkspaceFiles<Name> & {
|
|
38
|
+
fs: ReadonlyWorkspaceFiles<Name>["fs"] & Partial<WorkspaceSessionStarter>;
|
|
50
39
|
});
|
|
51
40
|
}
|
|
52
|
-
interface WorkspaceDevTokenOptions {
|
|
41
|
+
export interface WorkspaceDevTokenOptions {
|
|
53
42
|
serverId?: string;
|
|
54
43
|
}
|
|
55
|
-
declare function workspaceDevTokenServerId(port?: number | string | null): string;
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
declare function
|
|
60
|
-
declare function
|
|
61
|
-
declare function
|
|
62
|
-
declare function
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
44
|
+
export declare function workspaceDevTokenServerId(port?: number | string | null): string;
|
|
45
|
+
type WorkspaceDevHeaders = Headers | Record<string, string | string[] | undefined> | {
|
|
46
|
+
get(name: string): string | null;
|
|
47
|
+
};
|
|
48
|
+
export declare function refreshWorkspaceDevToken(rootDir: string, options?: WorkspaceDevTokenOptions): Promise<string>;
|
|
49
|
+
export declare function ensureWorkspaceDevToken(rootDir: string, options?: WorkspaceDevTokenOptions): Promise<string>;
|
|
50
|
+
export declare function readWorkspaceDevToken(rootDir: string, options?: WorkspaceDevTokenOptions): Promise<string | undefined>;
|
|
51
|
+
export declare function validateWorkspaceDevToken(rootDir: string, headers: WorkspaceDevHeaders, options?: WorkspaceDevTokenOptions): Promise<boolean>;
|
|
52
|
+
/** Read a Workspace file as a Response. A missing, disallowed, or unsafe path gets a 404 Response. */
|
|
53
|
+
export declare function readWorkspaceFileResponse<Name extends WorkspaceName>(options: WorkspaceFileResponseOptions<Name>): Promise<Response>;
|
|
54
|
+
/**
|
|
55
|
+
* Serve Workspace files from a route. The file path is the part of the URL path after `base`, percent-decoded, so
|
|
56
|
+
* `/files/my%20file.md` serves `my file.md`. A path that is not valid percent-encoding gets a 400 Response.
|
|
57
|
+
*/
|
|
58
|
+
export declare function defineWorkspaceFileHandler<Name extends WorkspaceName>(options: WorkspaceFileHandlerOptions<Name>): {
|
|
59
|
+
fetch(request: Request): Promise<Response>;
|
|
60
|
+
};
|
|
61
|
+
export declare function runWorkspaceDevCommand<Name extends WorkspaceName>(input: WorkspaceDevCommandInput<Name>): Promise<ExecResult>;
|
|
62
|
+
//#endregion
|