@vite-hub/workspace 0.0.1 → 0.0.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.
- package/README.md +225 -17
- package/dist/{ai-Bk3LLnit.js → ai-Ce3PtWFW.js} +160 -29
- package/dist/ai-nmHdzjCM.d.ts +162 -0
- package/dist/ai.d.ts +2 -85
- package/dist/ai.js +1 -1
- package/dist/asset-registry-CaG9b7tf.js +19 -0
- package/dist/cloudflare.d.ts +18 -0
- package/dist/cloudflare.js +2 -0
- package/dist/collections/client.d.ts +39 -0
- package/dist/collections/client.js +137 -0
- package/dist/collections.d.ts +59 -0
- package/dist/collections.js +159 -0
- package/dist/config-3I1BnsdO.d.ts +28 -0
- package/dist/config-CDoyls2X.js +30 -0
- package/dist/config-CZLO0m9h.js +7811 -0
- package/dist/file-CHfsZpso.js +23 -0
- package/dist/{files-DbsIjBya.js → files-QRV55OCx.js} +6 -5
- package/dist/github-CfA5vpJ5.js +189 -0
- package/dist/glob-D0vD_wRY.js +17 -0
- package/dist/host-CAnbugpI.js +370 -0
- package/dist/hosted-C1-tTTTf.js +581 -0
- package/dist/{providers/vercel/blob-store.js → hosted-vercel-blob-CWBILDVo.js} +100 -19
- package/dist/hosted-vercel-blob.d.ts +15 -0
- package/dist/hosted-vercel-blob.js +2 -0
- package/dist/hosted.d.ts +18 -0
- package/dist/hosted.js +2 -0
- package/dist/index.d.ts +129 -102
- package/dist/index.js +477 -599
- package/dist/lifecycle-BYoa9hVb.js +176 -0
- package/dist/loader.d.ts +1 -1
- package/dist/loader.js +1 -1
- package/dist/mountx.d.ts +10 -0
- package/dist/mountx.js +143 -0
- package/dist/nitro.d.ts +2 -31
- package/dist/nitro.js +2 -2
- package/dist/nuxt.d.ts +26 -0
- package/dist/nuxt.js +36 -0
- package/dist/path-CNI6wKIU.js +115 -0
- package/dist/{provider-BZuKAV4e.js → provider-C3_elpEh.js} +142 -37
- package/dist/providers/github/store.d.ts +6 -0
- package/dist/providers/github/store.js +2 -0
- package/dist/publish.d.ts +3 -2
- package/dist/publish.js +48 -142
- package/dist/{registry-D4Qzq26I.d.ts → registry-BIQVCBWI.d.ts} +3 -2
- package/dist/registry-CqyDTW9Y.js +74 -0
- package/dist/request-execution-u9mgUXKX.js +80 -0
- package/dist/runtime/assets.d.ts +2 -1
- package/dist/runtime/assets.js +5 -5
- package/dist/runtime/empty-assets-registry.d.ts +1 -1
- package/dist/runtime/empty-registry.d.ts +1 -1
- package/dist/runtime/state.d.ts +4 -21
- package/dist/runtime/state.js +7 -4
- package/dist/runtime/workspace.d.ts +2 -0
- package/dist/runtime/workspace.js +2 -0
- package/dist/runtime-options-BsPSv5O7.js +13 -0
- package/dist/runtime.d.ts +38 -0
- package/dist/runtime.js +626 -0
- package/dist/{search-DtaEyjpO.js → search-C4REyHj9.js} +2 -2
- package/dist/server.d.ts +65 -0
- package/dist/server.js +355 -0
- package/dist/shared-jfogBrd5.js +273 -0
- package/dist/source-metadata.d.ts +2 -0
- package/dist/source-metadata.js +2 -0
- package/dist/state-TTqa3x3Y.d.ts +25 -0
- package/dist/store-Bs1SLh55.js +414 -0
- package/dist/sync-DiuDmSGk.js +269 -0
- package/dist/target-D4Bqb1eH.js +11 -0
- package/dist/test.d.ts +1 -1
- package/dist/test.js +1 -1
- package/dist/types-60Yn149G.d.ts +612 -0
- package/dist/use-CUFSnFCS.d.ts +86 -0
- package/dist/use-CsV3oIxH.js +336 -0
- package/dist/vite-tbfDxtlu.js +2191 -0
- package/dist/vite.d.ts +33 -5
- package/dist/vite.js +2 -131
- package/dist/workspace-66SKyiec.d.ts +6 -0
- package/dist/workspace-C4k92Ow8.js +1460 -0
- package/package.json +143 -62
- package/dist/asset-registry-CMuQbyvl.js +0 -14
- package/dist/cloudflare-env-DL94Uvpz.js +0 -12
- package/dist/config-CvS-7sre.js +0 -49
- package/dist/errors-BUL8SoFS.js +0 -21
- package/dist/lifecycle-uD82JeGh.js +0 -92
- package/dist/module-8LZUMEXC.js +0 -922
- package/dist/path-BVv9OXhg.js +0 -63
- package/dist/providers/cloudflare/artifacts-store.d.ts +0 -6
- package/dist/providers/cloudflare/artifacts-store.js +0 -384
- package/dist/providers/vercel/blob-store.d.ts +0 -6
- package/dist/registry-Cnr0NoKv.js +0 -932
- package/dist/runtime/nitro-plugin.d.ts +0 -6
- package/dist/runtime/nitro-plugin.js +0 -14
- package/dist/sandbox-DtGXCWRq.js +0 -198
- package/dist/types-CwNcWTdt.d.ts +0 -378
- package/dist/utils-BtAm_K5P.js +0 -51
- /package/dist/{chunk-DrSxFLj_.js → chunk-8H4AJuhK.js} +0 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { t as __exportAll } from "./chunk-8H4AJuhK.js";
|
|
2
|
+
import { o as normalizeSafeWorkspacePath } from "./path-CNI6wKIU.js";
|
|
3
|
+
import { t as withWorkspaceRuntimeOptions } from "./runtime-options-BsPSv5O7.js";
|
|
4
|
+
import { file } from "@vite-hub/source";
|
|
5
|
+
//#region src/sources/file.ts
|
|
6
|
+
var file_exports = /* @__PURE__ */ __exportAll({ file: () => file$1 });
|
|
7
|
+
function file$1(input) {
|
|
8
|
+
const options = typeof input === "string" ? { path: input } : input;
|
|
9
|
+
const key = normalizeSafeWorkspacePath(options.workspacePath || options.path || "");
|
|
10
|
+
const mount = typeof options.mount === "object" && options.mount && !("path" in options.mount) ? {
|
|
11
|
+
...options.mount,
|
|
12
|
+
path: ""
|
|
13
|
+
} : options.mount ?? "";
|
|
14
|
+
return {
|
|
15
|
+
...withWorkspaceRuntimeOptions(file(options), {
|
|
16
|
+
...options,
|
|
17
|
+
mount
|
|
18
|
+
}),
|
|
19
|
+
probeKeys: options.probeKeys || [key]
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
export { file_exports as n, file$1 as t };
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { a as matchesAny, c as normalizeWorkspacePath } from "./path-CNI6wKIU.js";
|
|
2
|
+
import { n as createSourceContext } from "./config-CZLO0m9h.js";
|
|
2
3
|
//#region src/loaders/files.ts
|
|
3
4
|
function shouldLoad(path, options) {
|
|
4
5
|
if (options.include && !matchesAny(path, options.include)) return false;
|
|
@@ -9,11 +10,11 @@ function files(options = {}) {
|
|
|
9
10
|
return {
|
|
10
11
|
name: "files",
|
|
11
12
|
async load(ctx) {
|
|
12
|
-
const sourceContext = {
|
|
13
|
+
const sourceContext = createSourceContext({
|
|
14
|
+
name: ctx.workspace,
|
|
13
15
|
rootDir: ctx.rootDir,
|
|
14
|
-
sourceRootDir: ctx.sourceRootDir
|
|
15
|
-
|
|
16
|
-
};
|
|
16
|
+
sourceRootDir: ctx.sourceRootDir
|
|
17
|
+
}, void 0, ctx.store);
|
|
17
18
|
for (const source of ctx.sources) {
|
|
18
19
|
await source.prepare?.(sourceContext);
|
|
19
20
|
for (const key of await source.getKeys(sourceContext)) {
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { t as __exportAll } from "./chunk-8H4AJuhK.js";
|
|
2
|
+
import { g as getActiveCloudflareBinding, h as resolveGitHubTokenOption, s as processEnv } from "./shared-jfogBrd5.js";
|
|
3
|
+
import { t as withWorkspaceRuntimeOptions } from "./runtime-options-BsPSv5O7.js";
|
|
4
|
+
import { github } from "@vite-hub/source";
|
|
5
|
+
//#region src/env.ts
|
|
6
|
+
let viteLoadEnv;
|
|
7
|
+
async function resolveWorkspaceEnv(rootDir, name) {
|
|
8
|
+
return (await loadWorkspaceEnv(rootDir))?.[name];
|
|
9
|
+
}
|
|
10
|
+
async function loadWorkspaceEnv(rootDir) {
|
|
11
|
+
return await loadViteEnv(rootDir);
|
|
12
|
+
}
|
|
13
|
+
async function loadViteEnv(rootDir) {
|
|
14
|
+
if (viteLoadEnv === null) return;
|
|
15
|
+
if (!viteLoadEnv) try {
|
|
16
|
+
viteLoadEnv = (await import(
|
|
17
|
+
/* @vite-ignore */
|
|
18
|
+
"vite"
|
|
19
|
+
)).loadEnv;
|
|
20
|
+
} catch {
|
|
21
|
+
viteLoadEnv = null;
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
return viteLoadEnv("", rootDir, "");
|
|
25
|
+
}
|
|
26
|
+
//#endregion
|
|
27
|
+
//#region src/sources/github.ts
|
|
28
|
+
var github_exports = /* @__PURE__ */ __exportAll({ github: () => github$1 });
|
|
29
|
+
const githubTokenEnvNames = [
|
|
30
|
+
"WORKSPACE_GITHUB_TOKEN",
|
|
31
|
+
"VITEHUB_WORKSPACE_GITHUB_TOKEN",
|
|
32
|
+
"GITHUB_TOKEN",
|
|
33
|
+
"GH_TOKEN"
|
|
34
|
+
];
|
|
35
|
+
function github$1(input) {
|
|
36
|
+
if (typeof input === "function") return resolvableGitHubSource(input);
|
|
37
|
+
const resolvedOptions = { ...input };
|
|
38
|
+
const baseSource = github({
|
|
39
|
+
...resolvedOptions,
|
|
40
|
+
auth: createGitHubAuthResolver(resolvedOptions.auth)
|
|
41
|
+
});
|
|
42
|
+
const sourceByRootAndToken = /* @__PURE__ */ new Map();
|
|
43
|
+
async function getSourceForRoot(rootDir) {
|
|
44
|
+
const token = await resolveGitHubAuth(resolvedOptions.auth, rootDir);
|
|
45
|
+
const cacheKey = `${rootDir}\0${token || ""}`;
|
|
46
|
+
const cachedSource = sourceByRootAndToken.get(cacheKey);
|
|
47
|
+
if (cachedSource) return cachedSource;
|
|
48
|
+
const source = github({
|
|
49
|
+
...resolvedOptions,
|
|
50
|
+
auth: token
|
|
51
|
+
});
|
|
52
|
+
sourceByRootAndToken.set(cacheKey, source);
|
|
53
|
+
return source;
|
|
54
|
+
}
|
|
55
|
+
return withWorkspaceRuntimeOptions({
|
|
56
|
+
...baseSource,
|
|
57
|
+
fingerprint: {
|
|
58
|
+
exclude: resolvedOptions.exclude,
|
|
59
|
+
include: resolvedOptions.include,
|
|
60
|
+
ref: resolvedOptions.ref,
|
|
61
|
+
repo: resolvedOptions.repo,
|
|
62
|
+
root: resolvedOptions.root
|
|
63
|
+
},
|
|
64
|
+
async prepare(ctx) {
|
|
65
|
+
await (await getSourceForRoot(ctx.rootDir)).prepare?.(ctx);
|
|
66
|
+
},
|
|
67
|
+
async getKeys(ctx) {
|
|
68
|
+
return await (await getSourceForRoot(ctx.rootDir)).getKeys(ctx);
|
|
69
|
+
},
|
|
70
|
+
async getItem(key, ctx) {
|
|
71
|
+
return await (await getSourceForRoot(ctx.rootDir)).getItem(key, ctx);
|
|
72
|
+
},
|
|
73
|
+
async getItems(ctx) {
|
|
74
|
+
const source = await getSourceForRoot(ctx.rootDir);
|
|
75
|
+
return await source.getItems?.(ctx) ?? await Promise.all((await source.getKeys(ctx)).map((key) => source.getItem(key, ctx)));
|
|
76
|
+
},
|
|
77
|
+
async getMeta(key, ctx) {
|
|
78
|
+
return await (await getSourceForRoot(ctx.rootDir)).getMeta?.(key, ctx);
|
|
79
|
+
},
|
|
80
|
+
async search(query, ctx) {
|
|
81
|
+
return await (await getSourceForRoot(ctx.rootDir)).search?.(query, ctx) ?? [];
|
|
82
|
+
}
|
|
83
|
+
}, resolvedOptions);
|
|
84
|
+
}
|
|
85
|
+
function resolvableGitHubSource(resolve) {
|
|
86
|
+
return {
|
|
87
|
+
fingerprint: { sourceResolution: "github" },
|
|
88
|
+
materialize: "lazy",
|
|
89
|
+
async getKeys() {
|
|
90
|
+
return [];
|
|
91
|
+
},
|
|
92
|
+
async getItem(key) {
|
|
93
|
+
throw new Error(`[vitehub] github() resolver did not resolve before reading ${JSON.stringify(key)}.`);
|
|
94
|
+
},
|
|
95
|
+
async getItems() {
|
|
96
|
+
return [];
|
|
97
|
+
},
|
|
98
|
+
async search() {
|
|
99
|
+
return [];
|
|
100
|
+
},
|
|
101
|
+
async resolve(ctx) {
|
|
102
|
+
const options = await resolve(ctx);
|
|
103
|
+
return options ? github$1(githubResolutionDefaults(options, ctx)) : false;
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
function githubResolutionDefaults(options, ctx) {
|
|
108
|
+
const source = pullRequestSource(ctx.invocation.context.get("pullRequest"));
|
|
109
|
+
const repo = options.repo || source?.repo;
|
|
110
|
+
if (!repo) throw new Error("[vitehub] github() resolver requires repo or typed pullRequest source context.");
|
|
111
|
+
return {
|
|
112
|
+
...options,
|
|
113
|
+
mount: options.mount ?? ctx.source.mountPath,
|
|
114
|
+
ref: options.ref ?? source?.ref,
|
|
115
|
+
repo
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
function pullRequestSource(value) {
|
|
119
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return;
|
|
120
|
+
const flatRef = value.headRef;
|
|
121
|
+
const directSource = value.source;
|
|
122
|
+
if (directSource && typeof directSource === "object" && !Array.isArray(directSource)) {
|
|
123
|
+
const ref = directSource.ref;
|
|
124
|
+
const repo = directSource.repo;
|
|
125
|
+
if (typeof repo === "string" && repo) {
|
|
126
|
+
const resolvedRef = typeof ref === "string" && ref ? ref : typeof flatRef === "string" && flatRef ? flatRef : void 0;
|
|
127
|
+
return {
|
|
128
|
+
...resolvedRef ? { ref: resolvedRef } : {},
|
|
129
|
+
repo
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
const flatRepo = value.repository;
|
|
134
|
+
if (typeof flatRepo === "string" && flatRepo) return {
|
|
135
|
+
...typeof flatRef === "string" && flatRef ? { ref: flatRef } : {},
|
|
136
|
+
repo: flatRepo
|
|
137
|
+
};
|
|
138
|
+
const pullRequest = value.pullRequest;
|
|
139
|
+
if (!pullRequest || typeof pullRequest !== "object" || Array.isArray(pullRequest)) return;
|
|
140
|
+
const source = pullRequest.source;
|
|
141
|
+
if (!source || typeof source !== "object" || Array.isArray(source)) return;
|
|
142
|
+
const ref = source.ref;
|
|
143
|
+
const repo = source.repo;
|
|
144
|
+
return {
|
|
145
|
+
...typeof ref === "string" && ref ? { ref } : {},
|
|
146
|
+
...typeof repo === "string" && repo ? { repo } : {}
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
function createGitHubAuthResolver(auth) {
|
|
150
|
+
if (auth === false) return false;
|
|
151
|
+
return () => {
|
|
152
|
+
return resolveExplicitGitHubAuth(auth) || getActiveCloudflareBinding("GITHUB_TOKEN") || processEnv(process.env, ...githubTokenEnvNames);
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
async function resolveGitHubAuth(auth, rootDir) {
|
|
156
|
+
if (auth === false) return false;
|
|
157
|
+
return resolveExplicitGitHubAuth(auth) || resolveGitHubTokenOption({}) || await resolveGitHubEnvFileToken(rootDir) || await resolveGitHubCliToken();
|
|
158
|
+
}
|
|
159
|
+
function resolveExplicitGitHubAuth(auth) {
|
|
160
|
+
if (auth === false) return void 0;
|
|
161
|
+
if (typeof auth === "function") return auth();
|
|
162
|
+
return typeof auth === "string" ? auth : void 0;
|
|
163
|
+
}
|
|
164
|
+
async function resolveGitHubEnvFileToken(rootDir) {
|
|
165
|
+
return processEnv(Object.fromEntries(await Promise.all(githubTokenEnvNames.map(async (name) => [name, await resolveWorkspaceEnv(rootDir, name)]))), ...githubTokenEnvNames);
|
|
166
|
+
}
|
|
167
|
+
async function resolveGitHubCliToken() {
|
|
168
|
+
try {
|
|
169
|
+
const { execFileSync } = await import("node:child_process");
|
|
170
|
+
return execFileSync("gh", [
|
|
171
|
+
"auth",
|
|
172
|
+
"token",
|
|
173
|
+
"--hostname",
|
|
174
|
+
"github.com"
|
|
175
|
+
], {
|
|
176
|
+
encoding: "utf8",
|
|
177
|
+
stdio: [
|
|
178
|
+
"ignore",
|
|
179
|
+
"pipe",
|
|
180
|
+
"ignore"
|
|
181
|
+
],
|
|
182
|
+
timeout: 2e3
|
|
183
|
+
}).trim() || void 0;
|
|
184
|
+
} catch {
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
//#endregion
|
|
189
|
+
export { github_exports as n, github$1 as t };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { t as __exportAll } from "./chunk-8H4AJuhK.js";
|
|
2
|
+
import { t as withWorkspaceRuntimeOptions } from "./runtime-options-BsPSv5O7.js";
|
|
3
|
+
import { glob } from "@vite-hub/source";
|
|
4
|
+
//#region src/sources/glob.ts
|
|
5
|
+
var glob_exports = /* @__PURE__ */ __exportAll({ glob: () => glob$1 });
|
|
6
|
+
function glob$1(options) {
|
|
7
|
+
return withWorkspaceRuntimeOptions(glob({
|
|
8
|
+
...options,
|
|
9
|
+
keyCache: options.keyCache ?? !isLazySource(options)
|
|
10
|
+
}), options);
|
|
11
|
+
}
|
|
12
|
+
function isLazySource(options) {
|
|
13
|
+
if (options.materialize === "lazy") return true;
|
|
14
|
+
return typeof options.mount === "object" && options.mount?.materialize === "lazy";
|
|
15
|
+
}
|
|
16
|
+
//#endregion
|
|
17
|
+
export { glob_exports as n, glob$1 as t };
|
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
import { c as normalizeWorkspacePath, d as sha256, i as decodeFile, o as normalizeSafeWorkspacePath, p as workspaceError, r as contentToBytes } from "./path-CNI6wKIU.js";
|
|
2
|
+
import { i as diffSnapshots, r as createSnapshotFromEntries } from "./store-Bs1SLh55.js";
|
|
3
|
+
import { _ as scopedSearchQuery, f as assertDiffInsideSessionPaths, g as isMissingWorkspacePathError, h as filterSessionEntries, m as filterSessionDiff, p as assertPathInSessionScope } from "./workspace-C4k92Ow8.js";
|
|
4
|
+
import { posix } from "node:path";
|
|
5
|
+
import { normalizeExecutionAuthority } from "@vite-hub/runtime";
|
|
6
|
+
//#region src/session/host.ts
|
|
7
|
+
function normalizeTarget(target = "/workspace") {
|
|
8
|
+
const normalized = posix.resolve("/", target.replace(/\\/g, "/"));
|
|
9
|
+
if (normalized === "/") throw workspaceError("[vitehub] Workspace session target cannot be the host root.");
|
|
10
|
+
return normalized;
|
|
11
|
+
}
|
|
12
|
+
function normalizeSearchRoot(path) {
|
|
13
|
+
const normalized = posix.normalize(path.replace(/\\/g, "/"));
|
|
14
|
+
return normalized === "." ? "" : normalizeSafeWorkspacePath(normalized, { allowEmpty: true });
|
|
15
|
+
}
|
|
16
|
+
function toHostPath(root, path = "") {
|
|
17
|
+
const normalized = normalizeSafeWorkspacePath(path, { allowEmpty: true });
|
|
18
|
+
return normalized ? posix.join(root, normalized) : root;
|
|
19
|
+
}
|
|
20
|
+
function toHostCwd(root, cwd) {
|
|
21
|
+
if (cwd === void 0) return root;
|
|
22
|
+
if (!posix.isAbsolute(cwd)) return toHostPath(root, cwd);
|
|
23
|
+
const normalized = posix.normalize(cwd);
|
|
24
|
+
if (normalized === root || normalized.startsWith(`${root}/`)) return normalized;
|
|
25
|
+
if (normalized === "/workspace" || normalized.startsWith("/workspace/")) return toHostPath(root, normalized.slice(10));
|
|
26
|
+
throw workspaceError(`[vitehub] Workspace exec cwd must stay inside ${root}: ${cwd}.`);
|
|
27
|
+
}
|
|
28
|
+
function fromHostPath(root, path) {
|
|
29
|
+
const normalizedRoot = root.replace(/\/+$/, "");
|
|
30
|
+
const normalizedPath = posix.resolve(root, path).replace(/\/+$/, "");
|
|
31
|
+
if (normalizedPath === normalizedRoot) return "";
|
|
32
|
+
if (!normalizedPath.startsWith(`${normalizedRoot}/`)) throw workspaceError(`[vitehub] Workspace host returned a path outside ${root}: ${path}.`);
|
|
33
|
+
return normalizeWorkspacePath(normalizedPath.slice(normalizedRoot.length + 1));
|
|
34
|
+
}
|
|
35
|
+
function isGitSymlinkEntry(entry) {
|
|
36
|
+
return entry.metadata?.gitMode === "120000";
|
|
37
|
+
}
|
|
38
|
+
function hasSymlinkParent(path, symlinks) {
|
|
39
|
+
let parent = posix.dirname(path);
|
|
40
|
+
while (parent && parent !== "." && parent !== "/") {
|
|
41
|
+
if (symlinks.has(parent)) return true;
|
|
42
|
+
parent = posix.dirname(parent);
|
|
43
|
+
}
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
function isSafeHostSymlink(root, path, target) {
|
|
47
|
+
if (posix.isAbsolute(target)) return false;
|
|
48
|
+
const resolved = posix.resolve(posix.dirname(toHostPath(root, path)), target);
|
|
49
|
+
return resolved === root || resolved.startsWith(`${root}/`);
|
|
50
|
+
}
|
|
51
|
+
async function assertNoHostSymlinkParent(host, root, target) {
|
|
52
|
+
const path = fromHostPath(root, target);
|
|
53
|
+
if (hasSymlinkParent(path, new Set((await listHostEntries(host, root, "", true)).filter(isGitSymlinkEntry).map((entry) => entry.path)))) throw workspaceError(`[vitehub] Workspace host path crosses a symlink parent: ${path}.`);
|
|
54
|
+
}
|
|
55
|
+
function toWorkspaceEntry(root, entry) {
|
|
56
|
+
return {
|
|
57
|
+
metadata: entry.type === "symlink" ? { gitMode: "120000" } : void 0,
|
|
58
|
+
path: fromHostPath(root, entry.path),
|
|
59
|
+
size: entry.size,
|
|
60
|
+
type: entry.type === "directory" ? "directory" : "file"
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
async function ensureHostParent(host, path) {
|
|
64
|
+
const parent = posix.dirname(path);
|
|
65
|
+
if (parent && parent !== "." && parent !== "/") await host.files.mkdir(parent, { recursive: true });
|
|
66
|
+
}
|
|
67
|
+
async function readHostSymlinkTarget(host, root, path) {
|
|
68
|
+
const result = await host.exec("readlink", [fromHostPath(root, path)], { cwd: root });
|
|
69
|
+
if (result.code !== 0) throw workspaceError(`[vitehub] Failed to read workspace symlink: ${path}.`, { cause: new Error(result.stderr || "readlink failed") });
|
|
70
|
+
return result.stdout.replace(/\n$/, "");
|
|
71
|
+
}
|
|
72
|
+
async function writeHostSymlink(host, root, path, target) {
|
|
73
|
+
await host.files.remove(path).catch(() => void 0);
|
|
74
|
+
const result = await host.exec("ln", [
|
|
75
|
+
"-s",
|
|
76
|
+
target,
|
|
77
|
+
fromHostPath(root, path)
|
|
78
|
+
], { cwd: root });
|
|
79
|
+
if (result.code !== 0) throw workspaceError(`[vitehub] Failed to create workspace symlink: ${path}. ${result.stderr || "ln failed"}`);
|
|
80
|
+
}
|
|
81
|
+
async function readHostFile(host, root, path) {
|
|
82
|
+
if (!await host.files.exists(path)) return void 0;
|
|
83
|
+
const content = await host.files.read(path);
|
|
84
|
+
return content ? {
|
|
85
|
+
content,
|
|
86
|
+
path: fromHostPath(root, path)
|
|
87
|
+
} : void 0;
|
|
88
|
+
}
|
|
89
|
+
async function listHostEntries(host, root, path = "", recursive = false) {
|
|
90
|
+
const entries = await host.files.list(toHostPath(root, path), { recursive });
|
|
91
|
+
return (await Promise.all(entries.map(async (entry) => {
|
|
92
|
+
const workspaceEntry = toWorkspaceEntry(root, entry);
|
|
93
|
+
if (workspaceEntry.type !== "file" || isGitSymlinkEntry(workspaceEntry)) return workspaceEntry;
|
|
94
|
+
return (await host.exec("test", ["-x", fromHostPath(root, entry.path)], { cwd: root })).code === 0 ? {
|
|
95
|
+
...workspaceEntry,
|
|
96
|
+
metadata: {
|
|
97
|
+
...workspaceEntry.metadata,
|
|
98
|
+
gitMode: "100755"
|
|
99
|
+
}
|
|
100
|
+
} : workspaceEntry;
|
|
101
|
+
}))).filter((entry) => entry.path && entry.path !== ".git" && !entry.path.startsWith(".git/")).sort((left, right) => left.path.localeCompare(right.path));
|
|
102
|
+
}
|
|
103
|
+
async function makeHostFileExecutable(host, root, path) {
|
|
104
|
+
const result = await host.exec("chmod", ["+x", fromHostPath(root, path)], { cwd: root });
|
|
105
|
+
if (result.code !== 0) throw workspaceError(`[vitehub] Failed to preserve executable Workspace file: ${path}. ${result.stderr || "chmod failed"}`);
|
|
106
|
+
}
|
|
107
|
+
async function snapshotHost(host, root, name) {
|
|
108
|
+
return (await captureHostState(host, root, name)).snapshot;
|
|
109
|
+
}
|
|
110
|
+
async function captureHostState(host, root, name) {
|
|
111
|
+
const entries = await listHostEntries(host, root, "", true);
|
|
112
|
+
const contents = /* @__PURE__ */ new Map();
|
|
113
|
+
return {
|
|
114
|
+
contents,
|
|
115
|
+
snapshot: await createSnapshotFromEntries(await Promise.all(entries.map(async (entry) => {
|
|
116
|
+
if (entry.type !== "file") return entry;
|
|
117
|
+
const content = isGitSymlinkEntry(entry) ? await readHostSymlinkTarget(host, root, toHostPath(root, entry.path)) : await host.files.read(toHostPath(root, entry.path));
|
|
118
|
+
if (content === null) throw workspaceError(`[vitehub] Workspace host file disappeared while snapshotting: ${entry.path}.`);
|
|
119
|
+
contents.set(entry.path, content);
|
|
120
|
+
return {
|
|
121
|
+
...entry,
|
|
122
|
+
digest: await sha256(content),
|
|
123
|
+
size: contentToBytes(content).byteLength
|
|
124
|
+
};
|
|
125
|
+
})), name)
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
async function restoreAttachedHost(host, root, diff, state) {
|
|
129
|
+
const changed = [...new Set(diff.entries.map((entry) => entry.path))];
|
|
130
|
+
for (const path of changed.sort((left, right) => right.length - left.length)) await host.files.remove(toHostPath(root, path), { recursive: true }).catch(() => void 0);
|
|
131
|
+
const baselineEntries = changed.map((path) => [path, state.snapshot.entries[path]]).filter((entry) => Boolean(entry[1])).sort(([left], [right]) => left.split("/").length - right.split("/").length);
|
|
132
|
+
for (const [path, entry] of baselineEntries) {
|
|
133
|
+
const target = toHostPath(root, path);
|
|
134
|
+
if (entry.type === "directory") {
|
|
135
|
+
await host.files.mkdir(target, { recursive: true });
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
const content = state.contents.get(path);
|
|
139
|
+
if (content === void 0) continue;
|
|
140
|
+
await ensureHostParent(host, target);
|
|
141
|
+
if (entry.metadata?.gitMode === "120000") await writeHostSymlink(host, root, target, String(content));
|
|
142
|
+
else {
|
|
143
|
+
await host.files.write(target, contentToBytes(content));
|
|
144
|
+
if (entry.metadata?.gitMode === "100755") await makeHostFileExecutable(host, root, target);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
async function resetHostWorkspaceRoot(host, root) {
|
|
149
|
+
await host.files.remove(root, { recursive: true }).catch(() => void 0);
|
|
150
|
+
await host.files.mkdir(root, { recursive: true });
|
|
151
|
+
}
|
|
152
|
+
function normalizeSessionPaths(options) {
|
|
153
|
+
const paths = [...new Set((options?.paths || []).map((path) => normalizeSafeWorkspacePath(path, {
|
|
154
|
+
allowEmpty: true,
|
|
155
|
+
allowReserved: true
|
|
156
|
+
})))];
|
|
157
|
+
if (!paths.length || paths.includes("")) return void 0;
|
|
158
|
+
return paths.sort((left, right) => left.length - right.length || left.localeCompare(right));
|
|
159
|
+
}
|
|
160
|
+
async function sessionEntries(workspace, options) {
|
|
161
|
+
const paths = normalizeSessionPaths(options);
|
|
162
|
+
if (!paths) return await workspace.list("", { recursive: true });
|
|
163
|
+
const entries = /* @__PURE__ */ new Map();
|
|
164
|
+
for (const path of paths) {
|
|
165
|
+
const stat = await workspace.stat(path).catch((error) => {
|
|
166
|
+
if (isMissingWorkspacePathError(error)) return void 0;
|
|
167
|
+
throw error;
|
|
168
|
+
});
|
|
169
|
+
if (!stat) continue;
|
|
170
|
+
entries.set(stat.path, stat);
|
|
171
|
+
if (stat.type === "directory") for (const entry of await workspace.list(path, { recursive: true })) entries.set(entry.path, entry);
|
|
172
|
+
}
|
|
173
|
+
return [...entries.values()].sort((left, right) => left.path.localeCompare(right.path));
|
|
174
|
+
}
|
|
175
|
+
async function materializeWorkspace(workspace, host, root, options) {
|
|
176
|
+
await resetHostWorkspaceRoot(host, root);
|
|
177
|
+
const entries = await sessionEntries(workspace, options);
|
|
178
|
+
const symlinks = new Set(entries.filter(isGitSymlinkEntry).map((entry) => entry.path));
|
|
179
|
+
const nested = entries.find((entry) => hasSymlinkParent(entry.path, symlinks));
|
|
180
|
+
if (nested) throw workspaceError(`[vitehub] Workspace path crosses a symlink parent: ${nested.path}.`);
|
|
181
|
+
for (const entry of entries.filter((entry) => entry.type === "directory")) await host.files.mkdir(toHostPath(root, entry.path), { recursive: true });
|
|
182
|
+
for (const entry of entries) {
|
|
183
|
+
if (entry.type !== "file") continue;
|
|
184
|
+
const target = toHostPath(root, entry.path);
|
|
185
|
+
await ensureHostParent(host, target);
|
|
186
|
+
if (isGitSymlinkEntry(entry)) {
|
|
187
|
+
const symlinkTarget = typeof entry.metadata?.symlinkTarget === "string" ? entry.metadata.symlinkTarget : new TextDecoder().decode(contentToBytes(await workspace.readFile(entry.path, { encoding: "binary" })));
|
|
188
|
+
if (isSafeHostSymlink(root, entry.path, symlinkTarget)) await writeHostSymlink(host, root, target, symlinkTarget);
|
|
189
|
+
else await host.files.write(target, contentToBytes(symlinkTarget));
|
|
190
|
+
} else {
|
|
191
|
+
await host.files.write(target, contentToBytes(await workspace.readFile(entry.path, { encoding: "binary" })));
|
|
192
|
+
if (entry.metadata?.gitMode === "100755") await makeHostFileExecutable(host, root, target);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return await snapshotHost(host, root, "host-open");
|
|
196
|
+
}
|
|
197
|
+
async function commitHostChanges(host, root, workspace, diff, mediaTypes, message) {
|
|
198
|
+
for (const entry of diff.entries) {
|
|
199
|
+
if (entry.after?.type === "directory") {
|
|
200
|
+
if (entry.before?.type === "file") await workspace.rm(entry.path, { force: true });
|
|
201
|
+
await workspace.mkdir(entry.path, { recursive: true });
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
if (entry.type === "removed") {
|
|
205
|
+
await workspace.rm(entry.path, {
|
|
206
|
+
force: true,
|
|
207
|
+
recursive: true
|
|
208
|
+
});
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
if (entry.after?.type !== "file") continue;
|
|
212
|
+
if (entry.before?.type === "directory") await workspace.rm(entry.path, {
|
|
213
|
+
force: true,
|
|
214
|
+
recursive: true
|
|
215
|
+
});
|
|
216
|
+
const before = entry.before?.type === "file" ? await workspace.stat(entry.path).catch(() => void 0) : void 0;
|
|
217
|
+
const target = toHostPath(root, entry.path);
|
|
218
|
+
const symlinkTarget = entry.after.metadata?.gitMode === "120000" ? await readHostSymlinkTarget(host, root, target) : void 0;
|
|
219
|
+
const file = symlinkTarget === void 0 ? await readHostFile(host, root, target) : isSafeHostSymlink(root, entry.path, symlinkTarget) ? {
|
|
220
|
+
content: symlinkTarget,
|
|
221
|
+
metadata: entry.after.metadata,
|
|
222
|
+
path: entry.path
|
|
223
|
+
} : {
|
|
224
|
+
content: symlinkTarget,
|
|
225
|
+
path: entry.path
|
|
226
|
+
};
|
|
227
|
+
if (file) await workspace.writeFile(entry.path, file.content, {
|
|
228
|
+
mediaType: file.mediaType || mediaTypes.get(entry.path) || before?.mediaType,
|
|
229
|
+
metadata: symlinkTarget === void 0 ? entry.after.metadata : file.metadata
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
await workspace.snapshot({ name: message || "host-commit" });
|
|
233
|
+
}
|
|
234
|
+
async function createHostedWorkspaceSession(workspace, options) {
|
|
235
|
+
const host = options.host;
|
|
236
|
+
let executionAuthority;
|
|
237
|
+
try {
|
|
238
|
+
executionAuthority = normalizeExecutionAuthority(host.executionAuthority);
|
|
239
|
+
} catch {
|
|
240
|
+
throw new TypeError("[vitehub] Workspace session host must declare executionAuthority.");
|
|
241
|
+
}
|
|
242
|
+
const root = normalizeTarget(options.target);
|
|
243
|
+
const sessionPaths = normalizeSessionPaths(options);
|
|
244
|
+
let closed = false;
|
|
245
|
+
let attachedState = options.attach ? await captureHostState(host, root, "host-attach") : void 0;
|
|
246
|
+
let baseline = attachedState?.snapshot || await materializeWorkspace(workspace, host, root, options);
|
|
247
|
+
const mediaTypes = /* @__PURE__ */ new Map();
|
|
248
|
+
function assertOpen() {
|
|
249
|
+
if (closed) throw workspaceError("[vitehub] Workspace host session is already closed.");
|
|
250
|
+
}
|
|
251
|
+
async function currentDiff() {
|
|
252
|
+
assertOpen();
|
|
253
|
+
return diffSnapshots(baseline, await snapshotHost(host, root));
|
|
254
|
+
}
|
|
255
|
+
return {
|
|
256
|
+
executionAuthority,
|
|
257
|
+
async readFile(path, readOptions) {
|
|
258
|
+
assertOpen();
|
|
259
|
+
const file = await readHostFile(host, root, toHostPath(root, assertPathInSessionScope(normalizeSafeWorkspacePath(path), sessionPaths, { masked: true })));
|
|
260
|
+
if (!file) throw workspaceError(`[vitehub] Workspace file does not exist: ${path}.`);
|
|
261
|
+
return decodeFile(file.content, readOptions);
|
|
262
|
+
},
|
|
263
|
+
async writeFile(path, content, writeOptions) {
|
|
264
|
+
assertOpen();
|
|
265
|
+
const target = toHostPath(root, assertPathInSessionScope(normalizeSafeWorkspacePath(path), sessionPaths));
|
|
266
|
+
await assertNoHostSymlinkParent(host, root, target);
|
|
267
|
+
await ensureHostParent(host, target);
|
|
268
|
+
await host.files.remove(target, { recursive: true }).catch(() => void 0);
|
|
269
|
+
const symlinkTarget = writeOptions?.metadata?.gitMode === "120000" ? new TextDecoder().decode(contentToBytes(content)) : void 0;
|
|
270
|
+
if (symlinkTarget !== void 0 && isSafeHostSymlink(root, fromHostPath(root, target), symlinkTarget)) await writeHostSymlink(host, root, target, symlinkTarget);
|
|
271
|
+
else {
|
|
272
|
+
await host.files.write(target, contentToBytes(content));
|
|
273
|
+
const workspacePath = fromHostPath(root, target);
|
|
274
|
+
if (writeOptions?.metadata?.gitMode === "100755" || baseline.entries[workspacePath]?.metadata?.gitMode === "100755") await makeHostFileExecutable(host, root, target);
|
|
275
|
+
}
|
|
276
|
+
const workspacePath = fromHostPath(root, target);
|
|
277
|
+
if (writeOptions?.mediaType) mediaTypes.set(workspacePath, writeOptions.mediaType);
|
|
278
|
+
else mediaTypes.delete(workspacePath);
|
|
279
|
+
},
|
|
280
|
+
async mkdir(path, mkdirOptions = {}) {
|
|
281
|
+
assertOpen();
|
|
282
|
+
const hostTarget = toHostPath(root, assertPathInSessionScope(normalizeSafeWorkspacePath(path, { allowEmpty: true }), sessionPaths, { mkdir: true }));
|
|
283
|
+
await assertNoHostSymlinkParent(host, root, hostTarget);
|
|
284
|
+
await host.files.mkdir(hostTarget, { recursive: mkdirOptions.recursive });
|
|
285
|
+
},
|
|
286
|
+
async rm(path, rmOptions) {
|
|
287
|
+
assertOpen();
|
|
288
|
+
const workspacePath = assertPathInSessionScope(normalizeSafeWorkspacePath(path), sessionPaths);
|
|
289
|
+
const target = toHostPath(root, workspacePath);
|
|
290
|
+
await assertNoHostSymlinkParent(host, root, target);
|
|
291
|
+
await host.files.remove(target, { recursive: rmOptions?.recursive });
|
|
292
|
+
mediaTypes.delete(normalizeWorkspacePath(workspacePath));
|
|
293
|
+
},
|
|
294
|
+
async list(path = "", listOptions = {}) {
|
|
295
|
+
assertOpen();
|
|
296
|
+
return filterSessionEntries(await listHostEntries(host, root, path, listOptions.recursive), sessionPaths);
|
|
297
|
+
},
|
|
298
|
+
async glob(pattern, globOptions) {
|
|
299
|
+
assertOpen();
|
|
300
|
+
const patterns = Array.isArray(pattern) ? pattern : [pattern];
|
|
301
|
+
const cwd = normalizeSearchRoot(globOptions?.cwd || "");
|
|
302
|
+
const { matchesAny } = await import("./path-CNI6wKIU.js").then((n) => n.l);
|
|
303
|
+
return filterSessionEntries(await listHostEntries(host, root, "", true), sessionPaths).filter((entry) => {
|
|
304
|
+
if (entry.type !== "file") return false;
|
|
305
|
+
if (!cwd) return patterns.some((item) => matchesAny(entry.path, item));
|
|
306
|
+
if (!entry.path.startsWith(`${cwd}/`)) return false;
|
|
307
|
+
const relative = entry.path.slice(cwd.length + 1);
|
|
308
|
+
return patterns.some((item) => matchesAny(relative, item));
|
|
309
|
+
});
|
|
310
|
+
},
|
|
311
|
+
async search(query) {
|
|
312
|
+
assertOpen();
|
|
313
|
+
const { searchText } = await import("./search-C4REyHj9.js").then((n) => n.n);
|
|
314
|
+
const scoped = scopedSearchQuery(query, sessionPaths, normalizeSearchRoot);
|
|
315
|
+
if (!scoped) return [];
|
|
316
|
+
const scopedSearchRoots = [...new Set((scoped.paths?.length ? scoped.paths : [scoped.cwd || ""]).map(normalizeSearchRoot))].filter(Boolean);
|
|
317
|
+
const limit = scoped.limit ?? 100;
|
|
318
|
+
const hits = [];
|
|
319
|
+
for (const entry of filterSessionEntries(await listHostEntries(host, root, "", true), sessionPaths).filter((item) => item.type === "file")) {
|
|
320
|
+
if (scopedSearchRoots.length && !scopedSearchRoots.some((path) => entry.path === path || entry.path.startsWith(`${path}/`))) continue;
|
|
321
|
+
const content = await host.files.read(toHostPath(root, entry.path));
|
|
322
|
+
if (content === null) continue;
|
|
323
|
+
const text = new TextDecoder().decode(content);
|
|
324
|
+
hits.push(...searchText(entry.path, text, {
|
|
325
|
+
...scoped,
|
|
326
|
+
limit: limit - hits.length
|
|
327
|
+
}));
|
|
328
|
+
if (hits.length >= limit) break;
|
|
329
|
+
}
|
|
330
|
+
return hits;
|
|
331
|
+
},
|
|
332
|
+
async diff() {
|
|
333
|
+
return filterSessionDiff(await currentDiff(), sessionPaths);
|
|
334
|
+
},
|
|
335
|
+
async commit(commitOptions) {
|
|
336
|
+
const diff = await currentDiff();
|
|
337
|
+
if (!diff.entries.length) return;
|
|
338
|
+
assertDiffInsideSessionPaths(diff, sessionPaths);
|
|
339
|
+
await commitHostChanges(host, root, workspace, diff, mediaTypes, commitOptions?.message);
|
|
340
|
+
if (options.attach) {
|
|
341
|
+
attachedState = await captureHostState(host, root, commitOptions?.message || "host-commit");
|
|
342
|
+
baseline = attachedState.snapshot;
|
|
343
|
+
} else baseline = await snapshotHost(host, root, commitOptions?.message || "host-commit");
|
|
344
|
+
},
|
|
345
|
+
async exec(command, args = [], execOptions = {}) {
|
|
346
|
+
assertOpen();
|
|
347
|
+
const result = await host.exec(command, args, {
|
|
348
|
+
cwd: toHostCwd(root, execOptions.cwd),
|
|
349
|
+
env: execOptions.env,
|
|
350
|
+
signal: execOptions.abortSignal,
|
|
351
|
+
timeout: execOptions.timeout
|
|
352
|
+
});
|
|
353
|
+
return {
|
|
354
|
+
args,
|
|
355
|
+
command,
|
|
356
|
+
exitCode: result.code,
|
|
357
|
+
stderr: result.stderr,
|
|
358
|
+
stdout: result.stdout
|
|
359
|
+
};
|
|
360
|
+
},
|
|
361
|
+
async close() {
|
|
362
|
+
if (closed) return;
|
|
363
|
+
if (options.attach && attachedState) await restoreAttachedHost(host, root, filterSessionDiff(diffSnapshots(baseline, await snapshotHost(host, root)), sessionPaths), attachedState);
|
|
364
|
+
closed = true;
|
|
365
|
+
if (!options.attach) await materializeWorkspace(workspace, host, root, options);
|
|
366
|
+
}
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
//#endregion
|
|
370
|
+
export { createHostedWorkspaceSession };
|