@vite-hub/workspace 0.0.0 → 0.0.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 +89 -1
- package/dist/ai-Bk3LLnit.js +352 -0
- package/dist/ai.d.ts +86 -0
- package/dist/ai.js +2 -0
- package/dist/asset-registry-CMuQbyvl.js +14 -0
- package/dist/chunk-DrSxFLj_.js +14 -0
- package/dist/cloudflare-env-DL94Uvpz.js +12 -0
- package/dist/config-CvS-7sre.js +49 -0
- package/dist/errors-BUL8SoFS.js +21 -0
- package/dist/files-DbsIjBya.js +50 -0
- package/dist/index.d.ts +150 -0
- package/dist/index.js +656 -0
- package/dist/lifecycle-uD82JeGh.js +92 -0
- package/dist/loader.d.ts +17 -0
- package/dist/loader.js +22 -0
- package/dist/module-8LZUMEXC.js +922 -0
- package/dist/nitro.d.ts +31 -0
- package/dist/nitro.js +2 -0
- package/dist/path-BVv9OXhg.js +63 -0
- package/dist/provider-BZuKAV4e.js +494 -0
- package/dist/providers/cloudflare/artifacts-store.d.ts +6 -0
- package/dist/providers/cloudflare/artifacts-store.js +384 -0
- package/dist/providers/vercel/blob-store.d.ts +6 -0
- package/dist/providers/vercel/blob-store.js +176 -0
- package/dist/publish.d.ts +34 -0
- package/dist/publish.js +206 -0
- package/dist/registry-Cnr0NoKv.js +932 -0
- package/dist/registry-D4Qzq26I.d.ts +10 -0
- package/dist/runtime/assets.d.ts +11 -0
- package/dist/runtime/assets.js +138 -0
- package/dist/runtime/empty-assets-registry.d.ts +6 -0
- package/dist/runtime/empty-assets-registry.js +4 -0
- package/dist/runtime/empty-registry.d.ts +6 -0
- package/dist/runtime/empty-registry.js +4 -0
- package/dist/runtime/nitro-plugin.d.ts +6 -0
- package/dist/runtime/nitro-plugin.js +14 -0
- package/dist/runtime/state.d.ts +21 -0
- package/dist/runtime/state.js +12 -0
- package/dist/sandbox-DtGXCWRq.js +198 -0
- package/dist/search-DtaEyjpO.js +39 -0
- package/dist/test.d.ts +2 -0
- package/dist/test.js +2 -0
- package/dist/types-CwNcWTdt.d.ts +378 -0
- package/dist/utils-BtAm_K5P.js +51 -0
- package/dist/vite.d.ts +22 -0
- package/dist/vite.js +131 -0
- package/package.json +101 -5
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { k as WorkspaceDefinitionInput } from "./types-CwNcWTdt.js";
|
|
2
|
+
|
|
3
|
+
//#region src/core/registry.d.ts
|
|
4
|
+
type WorkspaceRegistryModule = {
|
|
5
|
+
default?: WorkspaceDefinitionInput;
|
|
6
|
+
};
|
|
7
|
+
type WorkspaceRegistry = Record<string, () => Promise<WorkspaceRegistryModule>>;
|
|
8
|
+
declare function registerWorkspace(name: string, definition: WorkspaceDefinitionInput): void;
|
|
9
|
+
//#endregion
|
|
10
|
+
export { registerWorkspace as n, WorkspaceRegistry as t };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { C as WorkspaceAssets, D as WorkspaceContent } from "../types-CwNcWTdt.js";
|
|
2
|
+
|
|
3
|
+
//#region src/runtime/assets.d.ts
|
|
4
|
+
interface WorkspaceAssetFile {
|
|
5
|
+
load: () => Promise<WorkspaceContent>;
|
|
6
|
+
mediaType?: string;
|
|
7
|
+
}
|
|
8
|
+
type WorkspaceAssetFiles<TKey extends string = string> = Record<TKey, WorkspaceAssetFile>;
|
|
9
|
+
declare function createWorkspaceAssets<TKey extends string = string>(files: WorkspaceAssetFiles<TKey>): WorkspaceAssets<TKey>;
|
|
10
|
+
//#endregion
|
|
11
|
+
export { createWorkspaceAssets };
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { t as WorkspaceError } from "../errors-BUL8SoFS.js";
|
|
2
|
+
import { i as normalizeSafeWorkspacePath, l as sha256, n as decodeFile, o as normalizeWorkspacePath, r as matchesAny } from "../path-BVv9OXhg.js";
|
|
3
|
+
import { t as searchText } from "../search-DtaEyjpO.js";
|
|
4
|
+
//#region src/runtime/assets.ts
|
|
5
|
+
function createMissingPathError(path) {
|
|
6
|
+
return new WorkspaceError(`[vitehub] Workspace file does not exist: ${path}.`);
|
|
7
|
+
}
|
|
8
|
+
function createPathMap(files) {
|
|
9
|
+
const map = /* @__PURE__ */ new Map();
|
|
10
|
+
for (const path of Object.keys(files)) map.set(normalizeSafeWorkspacePath(path), files[path]);
|
|
11
|
+
return map;
|
|
12
|
+
}
|
|
13
|
+
function createDirectorySet(paths) {
|
|
14
|
+
const directories = /* @__PURE__ */ new Set();
|
|
15
|
+
for (const path of paths) {
|
|
16
|
+
const segments = normalizeWorkspacePath(path).split("/").filter(Boolean);
|
|
17
|
+
for (let index = 1; index < segments.length; index++) directories.add(segments.slice(0, index).join("/"));
|
|
18
|
+
}
|
|
19
|
+
return directories;
|
|
20
|
+
}
|
|
21
|
+
function contentSize(content) {
|
|
22
|
+
return typeof content === "string" ? new TextEncoder().encode(content).byteLength : content.byteLength;
|
|
23
|
+
}
|
|
24
|
+
function normalizeAssetPath(path = "") {
|
|
25
|
+
return normalizeSafeWorkspacePath(path, { allowEmpty: true });
|
|
26
|
+
}
|
|
27
|
+
function isNestedUnder(path, prefix) {
|
|
28
|
+
return !prefix || path.startsWith(`${prefix}/`);
|
|
29
|
+
}
|
|
30
|
+
function createWorkspaceAssets(files) {
|
|
31
|
+
const pathMap = createPathMap(files);
|
|
32
|
+
const paths = [...pathMap.keys()].sort();
|
|
33
|
+
const directorySet = createDirectorySet(paths);
|
|
34
|
+
const contentCache = /* @__PURE__ */ new Map();
|
|
35
|
+
const statCache = /* @__PURE__ */ new Map();
|
|
36
|
+
async function readContent(path) {
|
|
37
|
+
const entry = pathMap.get(path);
|
|
38
|
+
if (!entry) throw createMissingPathError(path);
|
|
39
|
+
const cached = contentCache.get(path);
|
|
40
|
+
if (cached) return await cached;
|
|
41
|
+
const next = entry.load();
|
|
42
|
+
contentCache.set(path, next);
|
|
43
|
+
return await next;
|
|
44
|
+
}
|
|
45
|
+
async function statFile(path) {
|
|
46
|
+
const cached = statCache.get(path);
|
|
47
|
+
if (cached) return await cached;
|
|
48
|
+
const entry = pathMap.get(path);
|
|
49
|
+
if (!entry) throw createMissingPathError(path);
|
|
50
|
+
const next = (async () => {
|
|
51
|
+
const content = await readContent(path);
|
|
52
|
+
return {
|
|
53
|
+
digest: await sha256(content),
|
|
54
|
+
mediaType: entry.mediaType,
|
|
55
|
+
path,
|
|
56
|
+
size: contentSize(content),
|
|
57
|
+
type: "file"
|
|
58
|
+
};
|
|
59
|
+
})();
|
|
60
|
+
statCache.set(path, next);
|
|
61
|
+
return await next;
|
|
62
|
+
}
|
|
63
|
+
async function statPath(path) {
|
|
64
|
+
if (pathMap.has(path)) return await statFile(path);
|
|
65
|
+
if (!path || directorySet.has(path)) return {
|
|
66
|
+
path,
|
|
67
|
+
type: "directory"
|
|
68
|
+
};
|
|
69
|
+
throw createMissingPathError(path);
|
|
70
|
+
}
|
|
71
|
+
function createDirectoryEntry(path) {
|
|
72
|
+
return {
|
|
73
|
+
path,
|
|
74
|
+
type: "directory"
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
async function listEntries(prefix = "", options = {}) {
|
|
78
|
+
const normalizedPrefix = normalizeAssetPath(prefix);
|
|
79
|
+
const result = /* @__PURE__ */ new Map();
|
|
80
|
+
for (const directory of directorySet) {
|
|
81
|
+
if (directory === normalizedPrefix) continue;
|
|
82
|
+
if (normalizedPrefix && !isNestedUnder(directory, normalizedPrefix)) continue;
|
|
83
|
+
if (!options.recursive && normalizeWorkspacePath(directory.slice(normalizedPrefix.length)).replace(/^\//, "").includes("/")) continue;
|
|
84
|
+
result.set(directory, createDirectoryEntry(directory));
|
|
85
|
+
}
|
|
86
|
+
for (const path of paths) {
|
|
87
|
+
if (path === normalizedPrefix) continue;
|
|
88
|
+
if (normalizedPrefix && !isNestedUnder(path, normalizedPrefix)) continue;
|
|
89
|
+
if (!options.recursive && normalizeWorkspacePath(path.slice(normalizedPrefix.length)).replace(/^\//, "").includes("/")) continue;
|
|
90
|
+
result.set(path, await statFile(path));
|
|
91
|
+
}
|
|
92
|
+
return [...result.values()].sort((left, right) => left.path.localeCompare(right.path));
|
|
93
|
+
}
|
|
94
|
+
return {
|
|
95
|
+
async readFile(path, options) {
|
|
96
|
+
return decodeFile(await readContent(normalizeAssetPath(path)), options);
|
|
97
|
+
},
|
|
98
|
+
async stat(path) {
|
|
99
|
+
return await statPath(normalizeAssetPath(path));
|
|
100
|
+
},
|
|
101
|
+
async exists(path) {
|
|
102
|
+
try {
|
|
103
|
+
await statPath(normalizeAssetPath(path));
|
|
104
|
+
return true;
|
|
105
|
+
} catch (error) {
|
|
106
|
+
if (error instanceof WorkspaceError) return false;
|
|
107
|
+
throw error;
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
async list(path, options) {
|
|
111
|
+
return await listEntries(path || "", options);
|
|
112
|
+
},
|
|
113
|
+
async glob(pattern, _options = {}) {
|
|
114
|
+
const entries = await listEntries("", { recursive: true });
|
|
115
|
+
const patterns = Array.isArray(pattern) ? pattern : [pattern];
|
|
116
|
+
return entries.filter((entry) => entry.type === "file" && patterns.some((item) => matchesAny(entry.path, item)));
|
|
117
|
+
},
|
|
118
|
+
async search(query) {
|
|
119
|
+
const result = [];
|
|
120
|
+
const limit = query.limit ?? 100;
|
|
121
|
+
const entries = await listEntries("", { recursive: true });
|
|
122
|
+
for (const entry of entries) {
|
|
123
|
+
if (entry.type !== "file") continue;
|
|
124
|
+
if (query.paths?.length && !query.paths.some((path) => entry.path === path || entry.path.startsWith(`${path}/`))) continue;
|
|
125
|
+
const content = await readContent(entry.path);
|
|
126
|
+
const text = typeof content === "string" ? content : new TextDecoder().decode(content);
|
|
127
|
+
result.push(...searchText(entry.path, text, {
|
|
128
|
+
...query,
|
|
129
|
+
limit: limit - result.length
|
|
130
|
+
}));
|
|
131
|
+
if (result.length >= limit) break;
|
|
132
|
+
}
|
|
133
|
+
return result.slice(0, limit);
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
//#endregion
|
|
138
|
+
export { createWorkspaceAssets };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { c as setWorkspaceRuntimeConfig } from "../provider-BZuKAV4e.js";
|
|
2
|
+
import { setWorkspaceRuntimeAssetsRegistry, setWorkspaceRuntimeRegistry } from "./state.js";
|
|
3
|
+
import workspaceAssetsRegistry from "#vitehub-workspace-assets-registry";
|
|
4
|
+
import workspaceRegistry from "#vitehub-workspace-registry";
|
|
5
|
+
import { definePlugin } from "nitro";
|
|
6
|
+
import { useRuntimeConfig } from "nitro/runtime-config";
|
|
7
|
+
//#region src/runtime/nitro-plugin.ts
|
|
8
|
+
const workspaceNitroPlugin = definePlugin(() => {
|
|
9
|
+
setWorkspaceRuntimeConfig(useRuntimeConfig().workspace || false);
|
|
10
|
+
setWorkspaceRuntimeRegistry(workspaceRegistry);
|
|
11
|
+
setWorkspaceRuntimeAssetsRegistry(workspaceAssetsRegistry);
|
|
12
|
+
});
|
|
13
|
+
//#endregion
|
|
14
|
+
export { workspaceNitroPlugin as default };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { b as VercelBlobWorkspaceStoreOptions, ct as WorkspaceStore, h as ResolvedWorkspaceModuleOptions, t as CloudflareArtifactsWorkspaceStoreOptions, w as WorkspaceAssetsRegistry } from "../types-CwNcWTdt.js";
|
|
2
|
+
import { t as WorkspaceRegistry } from "../registry-D4Qzq26I.js";
|
|
3
|
+
|
|
4
|
+
//#region src/core/workspace-cache.d.ts
|
|
5
|
+
declare function resetWorkspaceStoreCache(): void;
|
|
6
|
+
//#endregion
|
|
7
|
+
//#region src/runtime/hosted-store-loader.d.ts
|
|
8
|
+
type HostedWorkspaceStoreOptions = CloudflareArtifactsWorkspaceStoreOptions | VercelBlobWorkspaceStoreOptions;
|
|
9
|
+
type WorkspaceHostedStoreLoader = (options: HostedWorkspaceStoreOptions, workspaceName: string) => WorkspaceStore;
|
|
10
|
+
declare function setWorkspaceHostedStoreLoader(loader: WorkspaceHostedStoreLoader | undefined): void;
|
|
11
|
+
declare function getWorkspaceHostedStoreLoader(): WorkspaceHostedStoreLoader | undefined;
|
|
12
|
+
//#endregion
|
|
13
|
+
//#region src/runtime/config.d.ts
|
|
14
|
+
declare function setWorkspaceRuntimeConfig(config: false | ResolvedWorkspaceModuleOptions): void;
|
|
15
|
+
declare function getWorkspaceRuntimeConfig(): false | ResolvedWorkspaceModuleOptions;
|
|
16
|
+
//#endregion
|
|
17
|
+
//#region src/runtime/state.d.ts
|
|
18
|
+
declare function setWorkspaceRuntimeRegistry(registry: WorkspaceRegistry): void;
|
|
19
|
+
declare function setWorkspaceRuntimeAssetsRegistry(registry: WorkspaceAssetsRegistry): void;
|
|
20
|
+
//#endregion
|
|
21
|
+
export { getWorkspaceHostedStoreLoader, getWorkspaceRuntimeConfig, resetWorkspaceStoreCache, setWorkspaceHostedStoreLoader, setWorkspaceRuntimeAssetsRegistry, setWorkspaceRuntimeConfig, setWorkspaceRuntimeRegistry };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { i as resetWorkspaceStoreCache, n as setWorkspaceRegistry } from "../registry-Cnr0NoKv.js";
|
|
2
|
+
import { t as setWorkspaceAssetsRegistry } from "../asset-registry-CMuQbyvl.js";
|
|
3
|
+
import { a as getWorkspaceHostedStoreLoader, c as setWorkspaceRuntimeConfig, o as setWorkspaceHostedStoreLoader, s as getWorkspaceRuntimeConfig } from "../provider-BZuKAV4e.js";
|
|
4
|
+
//#region src/runtime/state.ts
|
|
5
|
+
function setWorkspaceRuntimeRegistry(registry) {
|
|
6
|
+
setWorkspaceRegistry(registry);
|
|
7
|
+
}
|
|
8
|
+
function setWorkspaceRuntimeAssetsRegistry(registry) {
|
|
9
|
+
setWorkspaceAssetsRegistry(registry);
|
|
10
|
+
}
|
|
11
|
+
//#endregion
|
|
12
|
+
export { getWorkspaceHostedStoreLoader, getWorkspaceRuntimeConfig, resetWorkspaceStoreCache, setWorkspaceHostedStoreLoader, setWorkspaceRuntimeAssetsRegistry, setWorkspaceRuntimeConfig, setWorkspaceRuntimeRegistry };
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { t as WorkspaceError } from "./errors-BUL8SoFS.js";
|
|
2
|
+
import { i as normalizeSafeWorkspacePath, l as sha256, n as decodeFile, o as normalizeWorkspacePath, t as contentToBytes } from "./path-BVv9OXhg.js";
|
|
3
|
+
import { n as diffSnapshots, t as createSnapshotFromEntries } from "./utils-BtAm_K5P.js";
|
|
4
|
+
import { posix } from "node:path";
|
|
5
|
+
//#region src/session/sandbox.ts
|
|
6
|
+
const sandboxCwd = "/workspace";
|
|
7
|
+
const sandboxPackageSpecifier = "@vite-hub/sandbox";
|
|
8
|
+
const sandboxRuntimeStateSpecifier = "@vite-hub/sandbox/runtime/state";
|
|
9
|
+
function normalizeSearchRoot(path) {
|
|
10
|
+
const normalized = posix.normalize(path.replace(/\\/g, "/"));
|
|
11
|
+
return normalized === "." ? "" : normalizeSafeWorkspacePath(normalized, { allowEmpty: true });
|
|
12
|
+
}
|
|
13
|
+
function toSandboxPath(path = "") {
|
|
14
|
+
const normalized = normalizeSafeWorkspacePath(path, { allowEmpty: true });
|
|
15
|
+
return normalized ? posix.join(sandboxCwd, normalized) : sandboxCwd;
|
|
16
|
+
}
|
|
17
|
+
function fromSandboxPath(path) {
|
|
18
|
+
const normalizedRoot = sandboxCwd.replace(/\/+$/, "");
|
|
19
|
+
const normalizedPath = path.replace(/\/+$/, "");
|
|
20
|
+
if (normalizedPath === normalizedRoot) return "";
|
|
21
|
+
if (!normalizedPath.startsWith(`${normalizedRoot}/`)) return normalizeWorkspacePath(normalizedPath);
|
|
22
|
+
return normalizeWorkspacePath(normalizedPath.slice(normalizedRoot.length + 1));
|
|
23
|
+
}
|
|
24
|
+
function toWorkspaceEntry(entry) {
|
|
25
|
+
return {
|
|
26
|
+
path: fromSandboxPath(entry.path),
|
|
27
|
+
size: entry.size,
|
|
28
|
+
type: entry.type
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
async function ensureSandboxParent(sandbox, path) {
|
|
32
|
+
const parent = posix.dirname(path);
|
|
33
|
+
if (parent && parent !== "." && parent !== "/") await sandbox.mkdir(parent, { recursive: true });
|
|
34
|
+
}
|
|
35
|
+
async function readSandboxFile(sandbox, path) {
|
|
36
|
+
if (!await sandbox.exists(path)) return void 0;
|
|
37
|
+
return {
|
|
38
|
+
content: await sandbox.readFile(path, { encoding: "binary" }),
|
|
39
|
+
path: fromSandboxPath(path)
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
async function listSandboxEntries(sandbox, path = "", recursive = false) {
|
|
43
|
+
const root = toSandboxPath(path);
|
|
44
|
+
return (await sandbox.listFiles(root, { recursive })).map(toWorkspaceEntry).filter((entry) => entry.path).sort((left, right) => left.path.localeCompare(right.path));
|
|
45
|
+
}
|
|
46
|
+
async function snapshotSandbox(sandbox, name) {
|
|
47
|
+
const entries = await listSandboxEntries(sandbox, "", true);
|
|
48
|
+
return await createSnapshotFromEntries(await Promise.all(entries.map(async (entry) => {
|
|
49
|
+
if (entry.type !== "file") return entry;
|
|
50
|
+
const content = await sandbox.readFile(toSandboxPath(entry.path), { encoding: "binary" });
|
|
51
|
+
return {
|
|
52
|
+
...entry,
|
|
53
|
+
digest: await sha256(content),
|
|
54
|
+
size: contentToBytes(content).byteLength
|
|
55
|
+
};
|
|
56
|
+
})), name);
|
|
57
|
+
}
|
|
58
|
+
async function resetSandboxWorkspaceRoot(sandbox) {
|
|
59
|
+
const result = await sandbox.exec("rm", ["-rf", sandboxCwd]);
|
|
60
|
+
if (!result.ok) throw new WorkspaceError(`[vitehub] Failed to reset sandbox workspace root: ${result.stderr || "rm failed"}`);
|
|
61
|
+
await sandbox.mkdir(sandboxCwd, { recursive: true });
|
|
62
|
+
}
|
|
63
|
+
async function materializeWorkspace(workspace, sandbox) {
|
|
64
|
+
await resetSandboxWorkspaceRoot(sandbox);
|
|
65
|
+
const entries = await workspace.list("", { recursive: true });
|
|
66
|
+
for (const entry of entries.filter((entry) => entry.type === "directory")) await sandbox.mkdir(toSandboxPath(entry.path), { recursive: true });
|
|
67
|
+
for (const entry of entries) {
|
|
68
|
+
if (entry.type !== "file") continue;
|
|
69
|
+
const content = await workspace.readFile(entry.path, { encoding: "binary" });
|
|
70
|
+
const target = toSandboxPath(entry.path);
|
|
71
|
+
await ensureSandboxParent(sandbox, target);
|
|
72
|
+
await sandbox.writeFile(target, content);
|
|
73
|
+
}
|
|
74
|
+
return await snapshotSandbox(sandbox, "sandbox-open");
|
|
75
|
+
}
|
|
76
|
+
async function commitSandboxChanges(sandbox, workspace, diff, mediaTypes) {
|
|
77
|
+
for (const entry of diff.entries) {
|
|
78
|
+
if (entry.after?.type === "directory") {
|
|
79
|
+
if (entry.before?.type === "file") await workspace.rm(entry.path, { force: true });
|
|
80
|
+
await workspace.mkdir(entry.path, { recursive: true });
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
if (entry.type === "removed") {
|
|
84
|
+
await workspace.rm(entry.path, {
|
|
85
|
+
force: true,
|
|
86
|
+
recursive: true
|
|
87
|
+
});
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
if (entry.after?.type === "file") {
|
|
91
|
+
if (entry.before?.type === "directory") await workspace.rm(entry.path, {
|
|
92
|
+
force: true,
|
|
93
|
+
recursive: true
|
|
94
|
+
});
|
|
95
|
+
const before = entry.before?.type === "file" ? await workspace.stat(entry.path).catch(() => void 0) : void 0;
|
|
96
|
+
const file = await readSandboxFile(sandbox, toSandboxPath(entry.path));
|
|
97
|
+
if (file) await workspace.writeFile(entry.path, file.content, { mediaType: file.mediaType || mediaTypes.get(entry.path) || before?.mediaType });
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
await workspace.snapshot({ name: "sandbox-commit" });
|
|
101
|
+
}
|
|
102
|
+
async function createSandboxWorkspaceSession(definition, workspace) {
|
|
103
|
+
const sandboxPackage = await import(
|
|
104
|
+
/* @vite-ignore */
|
|
105
|
+
sandboxPackageSpecifier
|
|
106
|
+
).catch((error) => {
|
|
107
|
+
throw new WorkspaceError(`[vitehub] Sandbox workspace runtime requires @vite-hub/sandbox. ${error instanceof Error ? error.message : String(error)}`);
|
|
108
|
+
});
|
|
109
|
+
const sandboxConfig = (await import(
|
|
110
|
+
/* @vite-ignore */
|
|
111
|
+
sandboxRuntimeStateSpecifier
|
|
112
|
+
)).getSandboxRuntimeConfig();
|
|
113
|
+
if (!sandboxConfig) throw new WorkspaceError("[vitehub] Workspace runtime `sandbox` requires app-level `sandbox` config.");
|
|
114
|
+
const sandbox = await sandboxPackage.createSandboxWithConfig(sandboxConfig);
|
|
115
|
+
let baseline = await materializeWorkspace(workspace, sandbox);
|
|
116
|
+
const mediaTypes = /* @__PURE__ */ new Map();
|
|
117
|
+
let closed = false;
|
|
118
|
+
function assertOpen() {
|
|
119
|
+
if (closed) throw new WorkspaceError("[vitehub] Workspace sandbox session is already closed.");
|
|
120
|
+
}
|
|
121
|
+
async function currentDiff() {
|
|
122
|
+
assertOpen();
|
|
123
|
+
return diffSnapshots(baseline, await snapshotSandbox(sandbox));
|
|
124
|
+
}
|
|
125
|
+
return {
|
|
126
|
+
async readFile(path, options) {
|
|
127
|
+
assertOpen();
|
|
128
|
+
const file = await readSandboxFile(sandbox, toSandboxPath(path));
|
|
129
|
+
if (!file) throw new WorkspaceError(`[vitehub] Workspace file does not exist: ${path}.`);
|
|
130
|
+
return decodeFile(file.content, options);
|
|
131
|
+
},
|
|
132
|
+
async writeFile(path, content, options) {
|
|
133
|
+
assertOpen();
|
|
134
|
+
const target = toSandboxPath(path);
|
|
135
|
+
await ensureSandboxParent(sandbox, target);
|
|
136
|
+
await sandbox.writeFile(target, content);
|
|
137
|
+
const workspacePath = fromSandboxPath(target);
|
|
138
|
+
if (options?.mediaType) mediaTypes.set(workspacePath, options.mediaType);
|
|
139
|
+
else mediaTypes.delete(workspacePath);
|
|
140
|
+
},
|
|
141
|
+
async list(path = "", options = {}) {
|
|
142
|
+
assertOpen();
|
|
143
|
+
return await listSandboxEntries(sandbox, path, options.recursive);
|
|
144
|
+
},
|
|
145
|
+
async glob(pattern, _options = {}) {
|
|
146
|
+
assertOpen();
|
|
147
|
+
const patterns = Array.isArray(pattern) ? pattern : [pattern];
|
|
148
|
+
const { matchesAny } = await import("./path-BVv9OXhg.js").then((n) => n.s);
|
|
149
|
+
return (await listSandboxEntries(sandbox, "", true)).filter((entry) => entry.type === "file" && patterns.some((item) => matchesAny(entry.path, item)));
|
|
150
|
+
},
|
|
151
|
+
async search(query) {
|
|
152
|
+
assertOpen();
|
|
153
|
+
const { searchText } = await import("./search-DtaEyjpO.js").then((n) => n.n);
|
|
154
|
+
const scopedSearchRoots = [...new Set((query.paths?.length ? query.paths : [query.cwd || ""]).map(normalizeSearchRoot))].filter(Boolean);
|
|
155
|
+
const limit = query.limit ?? 100;
|
|
156
|
+
const hits = [];
|
|
157
|
+
for (const entry of (await listSandboxEntries(sandbox, "", true)).filter((item) => item.type === "file")) {
|
|
158
|
+
if (scopedSearchRoots.length && !scopedSearchRoots.some((path) => entry.path === path || entry.path.startsWith(`${path}/`))) continue;
|
|
159
|
+
const text = await sandbox.readFile(toSandboxPath(entry.path));
|
|
160
|
+
hits.push(...searchText(entry.path, text, {
|
|
161
|
+
...query,
|
|
162
|
+
limit: limit - hits.length
|
|
163
|
+
}));
|
|
164
|
+
if (hits.length >= limit) break;
|
|
165
|
+
}
|
|
166
|
+
return hits;
|
|
167
|
+
},
|
|
168
|
+
async diff() {
|
|
169
|
+
return await currentDiff();
|
|
170
|
+
},
|
|
171
|
+
async commit(options) {
|
|
172
|
+
await commitSandboxChanges(sandbox, workspace, await currentDiff(), mediaTypes);
|
|
173
|
+
baseline = await snapshotSandbox(sandbox, options?.message || "sandbox-commit");
|
|
174
|
+
},
|
|
175
|
+
async exec(command, args = [], options = {}) {
|
|
176
|
+
assertOpen();
|
|
177
|
+
const result = await sandbox.exec(command, args, {
|
|
178
|
+
cwd: options.cwd || sandboxCwd,
|
|
179
|
+
env: options.env,
|
|
180
|
+
timeout: options.timeout
|
|
181
|
+
});
|
|
182
|
+
return {
|
|
183
|
+
args,
|
|
184
|
+
command,
|
|
185
|
+
exitCode: result.code ?? 0,
|
|
186
|
+
stderr: result.stderr,
|
|
187
|
+
stdout: result.stdout
|
|
188
|
+
};
|
|
189
|
+
},
|
|
190
|
+
async close() {
|
|
191
|
+
if (closed) return;
|
|
192
|
+
closed = true;
|
|
193
|
+
if (sandbox.provider === "vercel") await sandbox.stop().catch(() => {});
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
//#endregion
|
|
198
|
+
export { createSandboxWorkspaceSession };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { t as __exportAll } from "./chunk-DrSxFLj_.js";
|
|
2
|
+
import { o as normalizeWorkspacePath } from "./path-BVv9OXhg.js";
|
|
3
|
+
//#region src/core/search.ts
|
|
4
|
+
var search_exports = /* @__PURE__ */ __exportAll({
|
|
5
|
+
searchText: () => searchText,
|
|
6
|
+
toSearchRegExp: () => toSearchRegExp
|
|
7
|
+
});
|
|
8
|
+
function toSearchRegExp(query) {
|
|
9
|
+
const source = query.regex ? query.pattern : escapeRegExp(query.pattern);
|
|
10
|
+
return new RegExp(source, query.caseSensitive ? "g" : "gi");
|
|
11
|
+
}
|
|
12
|
+
function searchText(path, content, query) {
|
|
13
|
+
const result = [];
|
|
14
|
+
const expression = toSearchRegExp(query);
|
|
15
|
+
const lines = content.split(/\r?\n/);
|
|
16
|
+
const limit = query.limit ?? 100;
|
|
17
|
+
for (let index = 0; index < lines.length && result.length < limit; index++) {
|
|
18
|
+
const line = lines[index];
|
|
19
|
+
expression.lastIndex = 0;
|
|
20
|
+
let match = expression.exec(line);
|
|
21
|
+
while (match && result.length < limit) {
|
|
22
|
+
result.push({
|
|
23
|
+
path: normalizeWorkspacePath(path),
|
|
24
|
+
line: index + 1,
|
|
25
|
+
column: match.index + 1,
|
|
26
|
+
text: line
|
|
27
|
+
});
|
|
28
|
+
if (!expression.global) break;
|
|
29
|
+
if (match[0] === "") expression.lastIndex += 1;
|
|
30
|
+
match = expression.exec(line);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return result;
|
|
34
|
+
}
|
|
35
|
+
function escapeRegExp(input) {
|
|
36
|
+
return input.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
37
|
+
}
|
|
38
|
+
//#endregion
|
|
39
|
+
export { search_exports as n, searchText as t };
|
package/dist/test.d.ts
ADDED
package/dist/test.js
ADDED