@vite-hub/workspace 0.0.1 → 0.0.2
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 +190 -17
- package/dist/{ai-Bk3LLnit.js → ai-Cztw4eLk.js} +149 -29
- package/dist/ai-hFdsvDHC.d.ts +162 -0
- package/dist/ai.d.ts +2 -85
- package/dist/ai.js +1 -1
- package/dist/artifacts-store-BAV9jZpd.js +555 -0
- package/dist/asset-registry-CaG9b7tf.js +19 -0
- package/dist/cli.d.ts +31 -0
- package/dist/cli.js +303 -0
- package/dist/cloudflare-env-CKxUs2fa.js +20 -0
- package/dist/cloudflare.d.ts +16 -0
- package/dist/cloudflare.js +3 -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--r5unwPc.js +7811 -0
- package/dist/config-Ca5DM8fF.d.ts +28 -0
- package/dist/config-DCQ6y30R.js +30 -0
- package/dist/file-B2q9NE18.js +24 -0
- package/dist/{files-DbsIjBya.js → files-xy0le-_U.js} +6 -5
- package/dist/github-lIO6-2sH.js +194 -0
- package/dist/glob-DeH0dzZX.js +23 -0
- package/dist/host-I-_vDs05.js +370 -0
- package/dist/hosted-vercel-blob.d.ts +15 -0
- package/dist/hosted-vercel-blob.js +26 -0
- package/dist/hosted.d.ts +18 -0
- package/dist/hosted.js +30 -0
- package/dist/index.d.ts +137 -102
- package/dist/index.js +479 -594
- package/dist/lifecycle-Lhk3dAMt.js +176 -0
- package/dist/loader.d.ts +1 -1
- package/dist/loader.js +1 -1
- 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-CeQciesl.js} +142 -37
- package/dist/providers/cloudflare/artifacts-store.d.ts +1 -1
- package/dist/providers/cloudflare/artifacts-store.js +1 -383
- package/dist/providers/github/store.d.ts +6 -0
- package/dist/providers/github/store.js +362 -0
- package/dist/providers/vercel/blob-store.d.ts +1 -1
- package/dist/providers/vercel/blob-store.js +77 -18
- package/dist/publish.d.ts +3 -2
- package/dist/publish.js +48 -142
- package/dist/registry-C7DMdBXN.js +74 -0
- package/dist/{registry-D4Qzq26I.d.ts → registry-CoC9JxVt.d.ts} +3 -2
- package/dist/request-execution-C8qRcReq.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.d.ts +38 -0
- package/dist/runtime.js +643 -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-BRycRSbo.js +255 -0
- package/dist/source-metadata.d.ts +2 -0
- package/dist/source-metadata.js +2 -0
- package/dist/state-DSAHEsq6.d.ts +25 -0
- package/dist/sync-DO42ypji.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-DHkae7Cb.d.ts +628 -0
- package/dist/use-D16tf8Ph.d.ts +86 -0
- package/dist/use-d_PlAdOu.js +353 -0
- package/dist/{utils-BtAm_K5P.js → utils-BPdaCyI6.js} +7 -3
- package/dist/vite-NWC9mFjI.js +1882 -0
- package/dist/vite.d.ts +33 -5
- package/dist/vite.js +2 -131
- package/dist/workspace-f2DBLmPZ.d.ts +6 -0
- package/dist/workspace-n4wBmdaM.js +1476 -0
- package/package.json +158 -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/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/{chunk-DrSxFLj_.js → chunk-8H4AJuhK.js} +0 -0
|
@@ -1,932 +0,0 @@
|
|
|
1
|
-
import { n as WorkspaceNotFoundError, t as WorkspaceError } from "./errors-BUL8SoFS.js";
|
|
2
|
-
import { i as normalizeSafeWorkspacePath, l as sha256, n as decodeFile, o as normalizeWorkspacePath, r as matchesAny, t as contentToBytes } from "./path-BVv9OXhg.js";
|
|
3
|
-
import { a as sourceMountIntersectsPath, i as sourceMountContainsPath, r as normalizeWorkspaceSources, t as createSourceContext } from "./config-CvS-7sre.js";
|
|
4
|
-
import { t as searchText } from "./search-DtaEyjpO.js";
|
|
5
|
-
import { t as createWorkspaceStoreFromProvider } from "./provider-BZuKAV4e.js";
|
|
6
|
-
import "node:path";
|
|
7
|
-
import { minimatch } from "minimatch";
|
|
8
|
-
import workspaceRegistry from "#vitehub-workspace-registry";
|
|
9
|
-
//#region src/core/rules.ts
|
|
10
|
-
function normalizeMaxBytes(value) {
|
|
11
|
-
if (value === void 0) return void 0;
|
|
12
|
-
if (typeof value === "number") return value;
|
|
13
|
-
const match = /^(\d+)(kb|mb)$/.exec(value.toLowerCase());
|
|
14
|
-
if (!match) throw new TypeError(`[vitehub] Invalid workspace rule maxBytes value "${value}".`);
|
|
15
|
-
const count = Number(match[1]);
|
|
16
|
-
return match[2] === "kb" ? count * 1024 : count * 1024 * 1024;
|
|
17
|
-
}
|
|
18
|
-
function toArray(value) {
|
|
19
|
-
if (value === void 0) return [];
|
|
20
|
-
return Array.isArray(value) ? value : [value];
|
|
21
|
-
}
|
|
22
|
-
function mergeHooks(left = {}, right = {}) {
|
|
23
|
-
return {
|
|
24
|
-
"write:after": [...toArray(left["write:after"]), ...toArray(right["write:after"])],
|
|
25
|
-
"write:before": [...toArray(left["write:before"]), ...toArray(right["write:before"])],
|
|
26
|
-
"write:error": [...toArray(left["write:error"]), ...toArray(right["write:error"])],
|
|
27
|
-
"write:validate": [...toArray(left["write:validate"]), ...toArray(right["write:validate"])]
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
function normalizeRules(rules = {}) {
|
|
31
|
-
return Object.entries(rules).map(([pattern, rule]) => {
|
|
32
|
-
const normalizedPattern = normalizeWorkspacePath(pattern);
|
|
33
|
-
return {
|
|
34
|
-
...rule,
|
|
35
|
-
maxBytes: normalizeMaxBytes(rule.maxBytes),
|
|
36
|
-
pattern: normalizedPattern,
|
|
37
|
-
specificity: normalizedPattern.replace(/\*/g, "").length,
|
|
38
|
-
validate: toArray(rule.validate)
|
|
39
|
-
};
|
|
40
|
-
}).sort((left, right) => left.specificity - right.specificity || left.pattern.localeCompare(right.pattern));
|
|
41
|
-
}
|
|
42
|
-
function normalizePolicy(definition) {
|
|
43
|
-
let hooks = {};
|
|
44
|
-
let rules = {};
|
|
45
|
-
for (const plugin of definition.plugins || []) {
|
|
46
|
-
if (!plugin?.id) throw new TypeError("[vitehub] Workspace plugins require an id.");
|
|
47
|
-
rules = {
|
|
48
|
-
...rules,
|
|
49
|
-
...plugin.rules
|
|
50
|
-
};
|
|
51
|
-
hooks = mergeHooks(hooks, plugin.hooks);
|
|
52
|
-
}
|
|
53
|
-
rules = {
|
|
54
|
-
...rules,
|
|
55
|
-
...definition.rules
|
|
56
|
-
};
|
|
57
|
-
hooks = mergeHooks(hooks, definition.hooks);
|
|
58
|
-
return {
|
|
59
|
-
hooks,
|
|
60
|
-
rules: normalizeRules(rules)
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
function matchRule(policy, path) {
|
|
64
|
-
const normalized = normalizeWorkspacePath(path);
|
|
65
|
-
let matched;
|
|
66
|
-
for (const rule of policy.rules) if (minimatch(normalized, rule.pattern, { dot: true })) matched = rule;
|
|
67
|
-
return matched;
|
|
68
|
-
}
|
|
69
|
-
function writeAllowed(write, operation, exists) {
|
|
70
|
-
if (write === void 0 || write === true) return true;
|
|
71
|
-
if (write === false) return false;
|
|
72
|
-
if (write === "delete") return operation === "rm";
|
|
73
|
-
if (write === "create") return operation !== "rm" && !exists;
|
|
74
|
-
if (write === "update") return operation === "writeFile" && exists;
|
|
75
|
-
return true;
|
|
76
|
-
}
|
|
77
|
-
function sizeOf(content) {
|
|
78
|
-
return content === void 0 ? 0 : contentToBytes(content).byteLength;
|
|
79
|
-
}
|
|
80
|
-
function assertRuleAllows(input) {
|
|
81
|
-
const rule = input.rule;
|
|
82
|
-
if (!rule) return;
|
|
83
|
-
const exists = Boolean(input.previous);
|
|
84
|
-
if (!writeAllowed(rule.write, input.operation, exists)) throw new WorkspaceError(`[vitehub] Workspace rule "${rule.pattern}" does not allow ${input.operation} for ${input.path}.`);
|
|
85
|
-
if (rule.maxBytes !== void 0 && sizeOf(input.content) > rule.maxBytes) throw new WorkspaceError(`[vitehub] Workspace rule "${rule.pattern}" limits writes to ${rule.maxBytes} bytes: ${input.path}.`);
|
|
86
|
-
if (rule.mediaType && input.mediaType) {
|
|
87
|
-
if (!(Array.isArray(rule.mediaType) ? rule.mediaType : [rule.mediaType]).includes(input.mediaType)) throw new WorkspaceError(`[vitehub] Workspace rule "${rule.pattern}" does not allow media type ${input.mediaType}: ${input.path}.`);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
async function runHooks(hooks, name, input) {
|
|
91
|
-
for (const hook of toArray(hooks[name])) await hook(input);
|
|
92
|
-
}
|
|
93
|
-
async function runValidators(validators, input) {
|
|
94
|
-
let current = input;
|
|
95
|
-
for (const validate of validators) {
|
|
96
|
-
const result = await validate(current);
|
|
97
|
-
if (result === false) throw new WorkspaceError(`[vitehub] Workspace validator rejected ${current.operation} for ${current.path}.`);
|
|
98
|
-
if (typeof result === "object" && result !== null) current = result;
|
|
99
|
-
}
|
|
100
|
-
return current;
|
|
101
|
-
}
|
|
102
|
-
function createWorkspaceWritePolicy(definition) {
|
|
103
|
-
const policy = normalizePolicy(definition);
|
|
104
|
-
return {
|
|
105
|
-
async before(input) {
|
|
106
|
-
let current = {
|
|
107
|
-
...input,
|
|
108
|
-
rule: matchRule(policy, input.path)
|
|
109
|
-
};
|
|
110
|
-
await runHooks(policy.hooks, "write:before", current);
|
|
111
|
-
assertRuleAllows(current);
|
|
112
|
-
if (current.rule?.validate.length) current = await runValidators(current.rule.validate, current);
|
|
113
|
-
await runHooks(policy.hooks, "write:validate", current);
|
|
114
|
-
return current;
|
|
115
|
-
},
|
|
116
|
-
async after(input) {
|
|
117
|
-
await runHooks(policy.hooks, "write:after", input);
|
|
118
|
-
},
|
|
119
|
-
async error(input, error) {
|
|
120
|
-
for (const hook of toArray(policy.hooks["write:error"])) await hook({
|
|
121
|
-
...input,
|
|
122
|
-
error
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
};
|
|
126
|
-
}
|
|
127
|
-
function resolveWorkspaceAutoCommit(definition, diff) {
|
|
128
|
-
if (!diff.entries.length) return;
|
|
129
|
-
const policy = normalizePolicy(definition);
|
|
130
|
-
const paths = diff.entries.map((entry) => normalizeWorkspacePath(entry.path));
|
|
131
|
-
const rules = paths.map((path) => matchRule(policy, path));
|
|
132
|
-
if (!rules.length || rules.some((rule) => !rule?.commit)) return;
|
|
133
|
-
return {
|
|
134
|
-
message: rules.map((rule) => rule?.commit).find((commit) => typeof commit === "string" && commit.trim().length > 0)?.trim() || `chore: update ${definition.name} workspace`,
|
|
135
|
-
paths
|
|
136
|
-
};
|
|
137
|
-
}
|
|
138
|
-
//#endregion
|
|
139
|
-
//#region src/session/basic.ts
|
|
140
|
-
function unsupportedExec() {
|
|
141
|
-
throw new WorkspaceError("[vitehub] Workspace does not configure an executable runtime. Set `runtime: 'sandbox'` in the workspace definition.");
|
|
142
|
-
}
|
|
143
|
-
function createBasicWorkspaceSession(workspace) {
|
|
144
|
-
return {
|
|
145
|
-
readFile: workspace.readFile,
|
|
146
|
-
writeFile: workspace.writeFile,
|
|
147
|
-
list: workspace.list,
|
|
148
|
-
glob: workspace.glob,
|
|
149
|
-
search: workspace.search,
|
|
150
|
-
diff: () => workspace.diff(),
|
|
151
|
-
async commit(options) {
|
|
152
|
-
await workspace.snapshot({ name: options?.message || "session-commit" });
|
|
153
|
-
},
|
|
154
|
-
async exec(_command, _args = [], _options) {
|
|
155
|
-
unsupportedExec();
|
|
156
|
-
},
|
|
157
|
-
async close() {}
|
|
158
|
-
};
|
|
159
|
-
}
|
|
160
|
-
//#endregion
|
|
161
|
-
//#region src/sources/materialization.ts
|
|
162
|
-
function sourceSnapshotMetaKey(sourceKey) {
|
|
163
|
-
return `source:${sourceKey}:snapshot`;
|
|
164
|
-
}
|
|
165
|
-
function sourceConfigFingerprint(source) {
|
|
166
|
-
return {
|
|
167
|
-
cache: source.cache,
|
|
168
|
-
key: source.key,
|
|
169
|
-
materialize: source.materialize,
|
|
170
|
-
mountPath: source.mountPath,
|
|
171
|
-
source: source.source.fingerprint
|
|
172
|
-
};
|
|
173
|
-
}
|
|
174
|
-
async function sourceConfigHash(source) {
|
|
175
|
-
return await sha256(sourceConfigFingerprint(source));
|
|
176
|
-
}
|
|
177
|
-
function isSnapshotFresh(meta, source, configHash) {
|
|
178
|
-
if (!meta || meta.status !== "ready" || meta.configHash !== configHash) return false;
|
|
179
|
-
if (!source.cache || typeof source.cache.maxAge !== "number") return false;
|
|
180
|
-
if (!meta.materializedAt) return false;
|
|
181
|
-
return Date.now() - Date.parse(meta.materializedAt) <= source.cache.maxAge * 1e3;
|
|
182
|
-
}
|
|
183
|
-
async function readSourceSnapshotMetadata(store, sourceKey) {
|
|
184
|
-
return await store.getMeta?.(sourceSnapshotMetaKey(sourceKey));
|
|
185
|
-
}
|
|
186
|
-
async function hasCurrentSourceSnapshot(store, source) {
|
|
187
|
-
const configHash = await sourceConfigHash(source);
|
|
188
|
-
const meta = await readSourceSnapshotMetadata(store, source.key);
|
|
189
|
-
return meta?.status === "ready" && meta.configHash === configHash;
|
|
190
|
-
}
|
|
191
|
-
async function writeSourceSnapshotMetadata(store, metadata) {
|
|
192
|
-
await store.setMeta?.(sourceSnapshotMetaKey(metadata.source), metadata);
|
|
193
|
-
}
|
|
194
|
-
function contentSize(content) {
|
|
195
|
-
return typeof content === "string" ? new TextEncoder().encode(content).byteLength : content.byteLength;
|
|
196
|
-
}
|
|
197
|
-
function sourcePathMatches(path, source, options) {
|
|
198
|
-
if (options?.sources?.length && !options.sources.includes(source.key)) return false;
|
|
199
|
-
const requested = normalizeWorkspacePath(options?.path || "");
|
|
200
|
-
if (!requested) return true;
|
|
201
|
-
return sourceMountIntersectsPath(source, requested);
|
|
202
|
-
}
|
|
203
|
-
function parentDirectoryPaths(path) {
|
|
204
|
-
const parts = normalizeWorkspacePath(path).split("/").filter(Boolean);
|
|
205
|
-
const paths = [];
|
|
206
|
-
for (let index = 1; index < parts.length; index++) paths.push(parts.slice(0, index).join("/"));
|
|
207
|
-
return paths;
|
|
208
|
-
}
|
|
209
|
-
async function removeStaleMaterializedSourceFiles(store, source, nextPaths) {
|
|
210
|
-
const entries = await store.list(source.mountPath, { recursive: true });
|
|
211
|
-
const nextDirectories = new Set([...nextPaths].flatMap((path) => parentDirectoryPaths(path)));
|
|
212
|
-
const staleDirectories = /* @__PURE__ */ new Set();
|
|
213
|
-
await Promise.all(entries.map(async (entry) => {
|
|
214
|
-
if (nextPaths.has(entry.path) || entry.type !== "file") return;
|
|
215
|
-
if ((await store.readFile(entry.path))?.metadata?.source === source.key) {
|
|
216
|
-
for (const directory of parentDirectoryPaths(entry.path)) staleDirectories.add(directory);
|
|
217
|
-
await store.rm(entry.path, { force: true });
|
|
218
|
-
}
|
|
219
|
-
}));
|
|
220
|
-
for (const entry of entries.filter((entry) => entry.type === "directory" && staleDirectories.has(entry.path) && !nextDirectories.has(entry.path)).sort((a, b) => b.path.length - a.path.length)) try {
|
|
221
|
-
await store.rm(entry.path, { force: true });
|
|
222
|
-
} catch {}
|
|
223
|
-
}
|
|
224
|
-
async function materializeWorkspaceSources(definition, store, options = {}) {
|
|
225
|
-
const started = Date.now();
|
|
226
|
-
const ctx = createSourceContext(definition);
|
|
227
|
-
const sources = normalizeWorkspaceSources(definition.sources).filter((source) => source.materialize === "lazy" && sourcePathMatches("", source, options));
|
|
228
|
-
const resultSources = [];
|
|
229
|
-
let files = 0;
|
|
230
|
-
let directories = 0;
|
|
231
|
-
let bytes = 0;
|
|
232
|
-
for (const source of sources) {
|
|
233
|
-
const configHash = await sourceConfigHash(source);
|
|
234
|
-
const existing = await readSourceSnapshotMetadata(store, source.key);
|
|
235
|
-
if (isSnapshotFresh(existing, source, configHash)) {
|
|
236
|
-
resultSources.push({
|
|
237
|
-
source: source.key,
|
|
238
|
-
mountPath: source.mountPath,
|
|
239
|
-
status: "ready",
|
|
240
|
-
commit: existing?.commit,
|
|
241
|
-
materializedAt: existing?.materializedAt,
|
|
242
|
-
files: existing?.files,
|
|
243
|
-
bytes: existing?.bytes
|
|
244
|
-
});
|
|
245
|
-
files += existing?.files || 0;
|
|
246
|
-
bytes += existing?.bytes || 0;
|
|
247
|
-
continue;
|
|
248
|
-
}
|
|
249
|
-
await writeSourceSnapshotMetadata(store, {
|
|
250
|
-
configHash,
|
|
251
|
-
source: source.key,
|
|
252
|
-
mountPath: source.mountPath,
|
|
253
|
-
status: "updating",
|
|
254
|
-
cacheMaxAge: source.cache ? source.cache.maxAge : void 0
|
|
255
|
-
});
|
|
256
|
-
try {
|
|
257
|
-
await source.source.prepare?.(ctx);
|
|
258
|
-
const items = source.source.getItems ? await source.source.getItems(ctx) : await Promise.all((await source.source.getKeys(ctx)).map(async (key) => await source.source.getItem(key, ctx)));
|
|
259
|
-
if (source.mountPath) {
|
|
260
|
-
await store.rm(source.mountPath, {
|
|
261
|
-
recursive: true,
|
|
262
|
-
force: true
|
|
263
|
-
});
|
|
264
|
-
await store.mkdir(source.mountPath, { recursive: true });
|
|
265
|
-
}
|
|
266
|
-
let sourceFiles = 0;
|
|
267
|
-
let sourceBytes = 0;
|
|
268
|
-
let commit;
|
|
269
|
-
const directorySet = new Set(source.mountPath ? [source.mountPath] : []);
|
|
270
|
-
const nextPaths = /* @__PURE__ */ new Set();
|
|
271
|
-
for (const item of items) {
|
|
272
|
-
const content = item.content ?? (typeof item.data === "undefined" ? "" : JSON.stringify(item.data, null, 2));
|
|
273
|
-
const path = normalizeWorkspacePath(`${source.mountPath}/${item.path || item.key}`);
|
|
274
|
-
nextPaths.add(path);
|
|
275
|
-
const parts = path.split("/");
|
|
276
|
-
for (let index = 1; index < parts.length; index++) directorySet.add(parts.slice(0, index).join("/"));
|
|
277
|
-
const metadata = item.metadata || {};
|
|
278
|
-
commit ||= readStringMeta(metadata, "ref") || readStringMeta(metadata, "sha");
|
|
279
|
-
await store.writeFile(path, {
|
|
280
|
-
path,
|
|
281
|
-
content,
|
|
282
|
-
mediaType: item.mediaType,
|
|
283
|
-
metadata: {
|
|
284
|
-
...metadata,
|
|
285
|
-
source: source.key
|
|
286
|
-
}
|
|
287
|
-
});
|
|
288
|
-
sourceFiles++;
|
|
289
|
-
sourceBytes += contentSize(content);
|
|
290
|
-
}
|
|
291
|
-
if (!source.mountPath) await removeStaleMaterializedSourceFiles(store, source, nextPaths);
|
|
292
|
-
const ready = {
|
|
293
|
-
configHash,
|
|
294
|
-
source: source.key,
|
|
295
|
-
mountPath: source.mountPath,
|
|
296
|
-
status: "ready",
|
|
297
|
-
commit,
|
|
298
|
-
materializedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
299
|
-
files: sourceFiles,
|
|
300
|
-
bytes: sourceBytes,
|
|
301
|
-
cacheMaxAge: source.cache ? source.cache.maxAge : void 0
|
|
302
|
-
};
|
|
303
|
-
await writeSourceSnapshotMetadata(store, ready);
|
|
304
|
-
resultSources.push(ready);
|
|
305
|
-
files += sourceFiles;
|
|
306
|
-
bytes += sourceBytes;
|
|
307
|
-
directories += directorySet.size;
|
|
308
|
-
} catch (error) {
|
|
309
|
-
const failed = {
|
|
310
|
-
configHash,
|
|
311
|
-
source: source.key,
|
|
312
|
-
mountPath: source.mountPath,
|
|
313
|
-
status: "error",
|
|
314
|
-
error: error instanceof Error ? error.message : String(error),
|
|
315
|
-
cacheMaxAge: source.cache ? source.cache.maxAge : void 0
|
|
316
|
-
};
|
|
317
|
-
await writeSourceSnapshotMetadata(store, failed);
|
|
318
|
-
resultSources.push(failed);
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
return {
|
|
322
|
-
bytes,
|
|
323
|
-
directories,
|
|
324
|
-
durationMs: Date.now() - started,
|
|
325
|
-
files,
|
|
326
|
-
path: normalizeWorkspacePath(options.path || ""),
|
|
327
|
-
sources: resultSources
|
|
328
|
-
};
|
|
329
|
-
}
|
|
330
|
-
async function readResolvedSourceFile(resolution, store, ctx, options) {
|
|
331
|
-
const file = await store.readFile(resolution.workspacePath);
|
|
332
|
-
if (!file) throw new WorkspaceError(`[vitehub] Workspace file does not exist: ${resolution.workspacePath}.`);
|
|
333
|
-
return decodeFile(file.content, options);
|
|
334
|
-
}
|
|
335
|
-
async function statVirtualSourcePath(source, workspacePath, store, ctx) {
|
|
336
|
-
const stored = await store.stat(workspacePath);
|
|
337
|
-
if (stored) return stored;
|
|
338
|
-
const keys = await source.source.getKeys(ctx);
|
|
339
|
-
const sourcePath = normalizeSourcePath(source, workspacePath);
|
|
340
|
-
if (!sourcePath && keys.length > 0) return {
|
|
341
|
-
path: source.mountPath,
|
|
342
|
-
type: "directory"
|
|
343
|
-
};
|
|
344
|
-
if (keys.includes(sourcePath)) return {
|
|
345
|
-
path: workspacePath,
|
|
346
|
-
type: "file",
|
|
347
|
-
digest: readDigest(await source.source.getMeta?.(sourcePath, ctx))
|
|
348
|
-
};
|
|
349
|
-
if (keys.some((key) => key.startsWith(`${sourcePath}/`))) return {
|
|
350
|
-
path: workspacePath,
|
|
351
|
-
type: "directory"
|
|
352
|
-
};
|
|
353
|
-
}
|
|
354
|
-
async function searchMaterializedStore(store, query) {
|
|
355
|
-
const limit = query.limit ?? 100;
|
|
356
|
-
const result = [];
|
|
357
|
-
const entries = query.paths?.length ? (await Promise.all(query.paths.map(async (path) => {
|
|
358
|
-
const [stat, nested] = await Promise.all([store.stat(path), store.glob(`${path}/**/*`)]);
|
|
359
|
-
return [...stat?.type === "file" ? [stat] : [], ...nested];
|
|
360
|
-
}))).flat() : await store.glob("**/*");
|
|
361
|
-
for (const entry of entries) {
|
|
362
|
-
if (entry.type !== "file") continue;
|
|
363
|
-
const file = await store.readFile(entry.path);
|
|
364
|
-
if (!file) continue;
|
|
365
|
-
const text = typeof file.content === "string" ? file.content : new TextDecoder().decode(file.content);
|
|
366
|
-
result.push(...searchText(entry.path, text, {
|
|
367
|
-
...query,
|
|
368
|
-
limit: limit - result.length
|
|
369
|
-
}));
|
|
370
|
-
if (result.length >= limit) break;
|
|
371
|
-
}
|
|
372
|
-
return result.slice(0, limit);
|
|
373
|
-
}
|
|
374
|
-
function normalizeSourcePath(source, workspacePath) {
|
|
375
|
-
if (workspacePath === source.mountPath) return "";
|
|
376
|
-
return source.mountPath && workspacePath.startsWith(`${source.mountPath}/`) ? normalizeWorkspacePath(workspacePath.slice(source.mountPath.length + 1)) : normalizeWorkspacePath(workspacePath);
|
|
377
|
-
}
|
|
378
|
-
function readStringMeta(meta, key) {
|
|
379
|
-
return typeof meta?.[key] === "string" ? meta[key] : void 0;
|
|
380
|
-
}
|
|
381
|
-
function readDigest(meta) {
|
|
382
|
-
return readStringMeta(meta, "digest") || readStringMeta(meta, "sha");
|
|
383
|
-
}
|
|
384
|
-
//#endregion
|
|
385
|
-
//#region src/sources/resolver.ts
|
|
386
|
-
function resolveWorkspacePath(definition, path) {
|
|
387
|
-
const workspacePath = normalizeSafeWorkspacePath(path, { allowEmpty: true });
|
|
388
|
-
const sources = normalizeWorkspaceSources(definition.sources);
|
|
389
|
-
for (const source of sources) {
|
|
390
|
-
const liveSourcePath = source.livePaths?.[workspacePath];
|
|
391
|
-
if (typeof liveSourcePath === "string") return createSourceResolution(source, workspacePath, liveSourcePath);
|
|
392
|
-
if (workspacePath === source.mountPath) return createSourceResolution(source, workspacePath, "");
|
|
393
|
-
if (sourceMountContainsPath(source, workspacePath)) return createSourceResolution(source, workspacePath, source.mountPath ? workspacePath.slice(source.mountPath.length + 1) : workspacePath);
|
|
394
|
-
}
|
|
395
|
-
return {
|
|
396
|
-
type: "store",
|
|
397
|
-
workspacePath,
|
|
398
|
-
readonly: false
|
|
399
|
-
};
|
|
400
|
-
}
|
|
401
|
-
function createSourceResolution(source, workspacePath, sourcePath) {
|
|
402
|
-
return {
|
|
403
|
-
type: "source",
|
|
404
|
-
sourceKey: source.key,
|
|
405
|
-
source,
|
|
406
|
-
workspacePath,
|
|
407
|
-
sourcePath: normalizeWorkspacePath(sourcePath),
|
|
408
|
-
materialize: source.materialize,
|
|
409
|
-
cache: source.cache,
|
|
410
|
-
validate: source.validate,
|
|
411
|
-
readonly: true
|
|
412
|
-
};
|
|
413
|
-
}
|
|
414
|
-
//#endregion
|
|
415
|
-
//#region src/sources/view.ts
|
|
416
|
-
function createWorkspaceSourceView(definition, store) {
|
|
417
|
-
const sourceContext = createSourceContext(definition);
|
|
418
|
-
const sources = normalizeWorkspaceSources(definition.sources).filter((source) => source.materialize === "lazy");
|
|
419
|
-
const writePolicy = createWorkspaceWritePolicy(definition);
|
|
420
|
-
const prepareBySource = /* @__PURE__ */ new Map();
|
|
421
|
-
const materializeBySource = /* @__PURE__ */ new Map();
|
|
422
|
-
function isLazySourcePath(path) {
|
|
423
|
-
return sources.some((source) => sourceMountContainsPath(source, path));
|
|
424
|
-
}
|
|
425
|
-
function isLiveSource(sourceKey) {
|
|
426
|
-
return Boolean(sources.find((item) => item.key === sourceKey)?.livePaths);
|
|
427
|
-
}
|
|
428
|
-
function getLazySourcesForPath(path) {
|
|
429
|
-
return sources.filter((source) => sourceMountIntersectsPath(source, path));
|
|
430
|
-
}
|
|
431
|
-
async function ensurePrepared(sourceKey) {
|
|
432
|
-
const source = sources.find((item) => item.key === sourceKey)?.source;
|
|
433
|
-
if (!source?.prepare) return;
|
|
434
|
-
let pending = prepareBySource.get(sourceKey);
|
|
435
|
-
if (!pending) {
|
|
436
|
-
pending = source.prepare(sourceContext);
|
|
437
|
-
prepareBySource.set(sourceKey, pending);
|
|
438
|
-
}
|
|
439
|
-
await pending;
|
|
440
|
-
}
|
|
441
|
-
async function ensurePreparedSources(items = sources) {
|
|
442
|
-
await Promise.all(items.map(async (source) => await ensurePrepared(source.key)));
|
|
443
|
-
}
|
|
444
|
-
async function ensureMaterialized(sourceKey) {
|
|
445
|
-
let pending = materializeBySource.get(sourceKey);
|
|
446
|
-
if (!pending) {
|
|
447
|
-
pending = materializeWorkspaceSources(definition, store, { sources: [sourceKey] }).then(() => void 0);
|
|
448
|
-
materializeBySource.set(sourceKey, pending);
|
|
449
|
-
}
|
|
450
|
-
await pending;
|
|
451
|
-
}
|
|
452
|
-
async function ensureMaterializedSources(items = sources) {
|
|
453
|
-
await Promise.all(items.map(async (source) => await ensureMaterialized(source.key)));
|
|
454
|
-
}
|
|
455
|
-
async function listSourceAware(path = "", options = {}) {
|
|
456
|
-
const storeEntries = await store.list(path, options);
|
|
457
|
-
const result = new Map(storeEntries.map((entry) => [entry.path, entry]));
|
|
458
|
-
for (const source of getLazySourcesForPath(path)) {
|
|
459
|
-
await ensurePrepared(source.key);
|
|
460
|
-
if (source.livePaths) {
|
|
461
|
-
await pruneLiveSourceStoreEntries(result, source);
|
|
462
|
-
continue;
|
|
463
|
-
}
|
|
464
|
-
if (!path && options.recursive && !await hasCurrentSourceSnapshot(store, source)) {
|
|
465
|
-
if ([...result.keys()].some((key) => sourceMountContainsPath(source, key))) {
|
|
466
|
-
const allowed = await currentSourceTreePaths(source, sourceContext);
|
|
467
|
-
for (const key of result.keys()) if (sourceMountContainsPath(source, key) && !allowed.has(key)) result.delete(key);
|
|
468
|
-
result.set(source.mountPath, {
|
|
469
|
-
path: source.mountPath,
|
|
470
|
-
type: "directory"
|
|
471
|
-
});
|
|
472
|
-
continue;
|
|
473
|
-
}
|
|
474
|
-
for (const key of result.keys()) if (sourceMountContainsPath(source, key)) result.delete(key);
|
|
475
|
-
result.set(source.mountPath, {
|
|
476
|
-
path: source.mountPath,
|
|
477
|
-
type: "directory"
|
|
478
|
-
});
|
|
479
|
-
continue;
|
|
480
|
-
}
|
|
481
|
-
if (sourceMountContainsPath(source, path) || !source.mountPath && path) {
|
|
482
|
-
await ensureMaterialized(source.key);
|
|
483
|
-
for (const entry of await store.list(path, options)) result.set(entry.path, entry);
|
|
484
|
-
} else if (!path && !result.has(source.mountPath)) result.set(source.mountPath, {
|
|
485
|
-
path: source.mountPath,
|
|
486
|
-
type: "directory"
|
|
487
|
-
});
|
|
488
|
-
}
|
|
489
|
-
addLiveSourceEntries(result, path, options, sources);
|
|
490
|
-
return [...result.values()].sort((left, right) => left.path.localeCompare(right.path));
|
|
491
|
-
}
|
|
492
|
-
async function searchWorkspace(query) {
|
|
493
|
-
const limit = query.limit ?? 100;
|
|
494
|
-
const storePaths = [];
|
|
495
|
-
const sourcePaths = /* @__PURE__ */ new Map();
|
|
496
|
-
const requestedPaths = query.paths?.length ? query.paths : [query.cwd || ""];
|
|
497
|
-
for (const requestedPath of requestedPaths) {
|
|
498
|
-
const resolution = resolveWorkspacePath(definition, normalizeWorkspacePath(requestedPath));
|
|
499
|
-
if (resolution.type === "store") {
|
|
500
|
-
storePaths.push(resolution.workspacePath);
|
|
501
|
-
continue;
|
|
502
|
-
}
|
|
503
|
-
const list = sourcePaths.get(resolution.sourceKey) || [];
|
|
504
|
-
list.push(resolution.workspacePath);
|
|
505
|
-
sourcePaths.set(resolution.sourceKey, list);
|
|
506
|
-
}
|
|
507
|
-
const results = await searchMaterializedStore(store, {
|
|
508
|
-
...query,
|
|
509
|
-
paths: storePaths.filter(Boolean).length ? storePaths.filter(Boolean) : void 0,
|
|
510
|
-
limit
|
|
511
|
-
});
|
|
512
|
-
if (query.paths?.length && !storePaths.filter(Boolean).length) results.length = 0;
|
|
513
|
-
for (const source of sources) {
|
|
514
|
-
const paths = sourcePaths.get(source.key);
|
|
515
|
-
if (query.paths?.length && !paths?.length && !query.paths.some((path) => !normalizeWorkspacePath(path))) continue;
|
|
516
|
-
if (source.livePaths) {
|
|
517
|
-
await ensurePrepared(source.key);
|
|
518
|
-
const liveEntries = liveSourceEntries(source).filter((entry) => entry.type === "file").filter((entry) => !paths?.length || paths.some((path) => !path || entry.path === path || entry.path.startsWith(`${path}/`)));
|
|
519
|
-
for (const entry of liveEntries) {
|
|
520
|
-
const sourcePath = source.livePaths[entry.path];
|
|
521
|
-
if (typeof sourcePath !== "string") continue;
|
|
522
|
-
const item = await source.source.getItem(sourcePath, sourceContext);
|
|
523
|
-
const content = item.content ?? (typeof item.data === "undefined" ? "" : JSON.stringify(item.data, null, 2));
|
|
524
|
-
const text = typeof content === "string" ? content : new TextDecoder().decode(content);
|
|
525
|
-
results.push(...searchText(entry.path, text, {
|
|
526
|
-
...query,
|
|
527
|
-
limit: limit - results.length
|
|
528
|
-
}));
|
|
529
|
-
if (results.length >= limit) break;
|
|
530
|
-
}
|
|
531
|
-
if (results.length >= limit) break;
|
|
532
|
-
continue;
|
|
533
|
-
}
|
|
534
|
-
await ensureMaterialized(source.key);
|
|
535
|
-
results.push(...await searchMaterializedStore(store, {
|
|
536
|
-
...query,
|
|
537
|
-
paths,
|
|
538
|
-
limit: limit - results.length
|
|
539
|
-
}));
|
|
540
|
-
if (results.length >= limit) break;
|
|
541
|
-
}
|
|
542
|
-
return dedupeHits(results).slice(0, limit);
|
|
543
|
-
}
|
|
544
|
-
function assertWritableStorePath(path, workspacePath, type) {
|
|
545
|
-
if (type === "source" || isLazySourcePath(workspacePath)) throw new WorkspaceError(`[vitehub] Source-backed workspace paths are read-only: ${path}.`);
|
|
546
|
-
}
|
|
547
|
-
async function previousStat(path) {
|
|
548
|
-
return await store.stat(path);
|
|
549
|
-
}
|
|
550
|
-
async function materializeRootSourceForPath(path) {
|
|
551
|
-
for (const source of sources.filter((source) => !source.mountPath)) {
|
|
552
|
-
await ensurePrepared(source.key);
|
|
553
|
-
await ensureMaterialized(source.key);
|
|
554
|
-
if ((await store.readFile(path))?.metadata?.source === source.key) return source;
|
|
555
|
-
}
|
|
556
|
-
}
|
|
557
|
-
async function isSourceBackedStorePath(path) {
|
|
558
|
-
const file = await store.readFile(path);
|
|
559
|
-
if (typeof file?.metadata?.source === "string" && sources.some((source) => source.key === file.metadata?.source)) return true;
|
|
560
|
-
if ((await store.stat(path))?.type !== "directory") return false;
|
|
561
|
-
const entries = await store.list(path, { recursive: true });
|
|
562
|
-
for (const entry of entries) {
|
|
563
|
-
if (entry.type !== "file") continue;
|
|
564
|
-
const child = await store.readFile(entry.path);
|
|
565
|
-
if (typeof child?.metadata?.source === "string" && sources.some((source) => source.key === child.metadata?.source)) return true;
|
|
566
|
-
}
|
|
567
|
-
return false;
|
|
568
|
-
}
|
|
569
|
-
async function assertWritableResolvedStorePath(path, workspacePath, type) {
|
|
570
|
-
assertWritableStorePath(path, workspacePath, type);
|
|
571
|
-
await materializeRootSourceForPath(workspacePath);
|
|
572
|
-
if (await isSourceBackedStorePath(workspacePath)) throw new WorkspaceError(`[vitehub] Source-backed workspace paths are read-only: ${path}.`);
|
|
573
|
-
}
|
|
574
|
-
return {
|
|
575
|
-
async readFile(path, options) {
|
|
576
|
-
const resolution = resolveWorkspacePath(definition, path);
|
|
577
|
-
if (resolution.type === "source") {
|
|
578
|
-
await ensurePrepared(resolution.sourceKey);
|
|
579
|
-
if (isLiveSource(resolution.sourceKey)) {
|
|
580
|
-
const item = await resolution.source.source.getItem(resolution.sourcePath, sourceContext);
|
|
581
|
-
return decodeFile(item.content ?? (typeof item.data === "undefined" ? "" : JSON.stringify(item.data, null, 2)), options);
|
|
582
|
-
}
|
|
583
|
-
await ensureMaterialized(resolution.sourceKey);
|
|
584
|
-
return await readResolvedSourceFile(resolution, store, sourceContext, options);
|
|
585
|
-
}
|
|
586
|
-
await materializeRootSourceForPath(resolution.workspacePath);
|
|
587
|
-
const file = await store.readFile(resolution.workspacePath);
|
|
588
|
-
if (!file) throw new WorkspaceError(`[vitehub] Workspace file does not exist: ${path}.`);
|
|
589
|
-
return decodeFile(file.content, options);
|
|
590
|
-
},
|
|
591
|
-
async writeFile(path, content, options) {
|
|
592
|
-
const resolution = resolveWorkspacePath(definition, path);
|
|
593
|
-
await assertWritableResolvedStorePath(path, resolution.workspacePath, resolution.type);
|
|
594
|
-
const input = await writePolicy.before({
|
|
595
|
-
content,
|
|
596
|
-
mediaType: options?.mediaType,
|
|
597
|
-
operation: "writeFile",
|
|
598
|
-
path: resolution.workspacePath,
|
|
599
|
-
previous: await previousStat(resolution.workspacePath),
|
|
600
|
-
workspace: definition.name
|
|
601
|
-
});
|
|
602
|
-
try {
|
|
603
|
-
await store.writeFile(input.path, {
|
|
604
|
-
path: input.path,
|
|
605
|
-
content: input.content ?? content,
|
|
606
|
-
mediaType: input.mediaType
|
|
607
|
-
});
|
|
608
|
-
await writePolicy.after(input);
|
|
609
|
-
} catch (error) {
|
|
610
|
-
await writePolicy.error(input, error);
|
|
611
|
-
throw error;
|
|
612
|
-
}
|
|
613
|
-
},
|
|
614
|
-
async list(path, options) {
|
|
615
|
-
return await listSourceAware(path || "", options || {});
|
|
616
|
-
},
|
|
617
|
-
async glob(pattern, options) {
|
|
618
|
-
const patterns = Array.isArray(pattern) ? pattern : [pattern];
|
|
619
|
-
await ensurePreparedSources();
|
|
620
|
-
await ensureMaterializedSources(sources.filter((source) => !source.livePaths));
|
|
621
|
-
const result = /* @__PURE__ */ new Map();
|
|
622
|
-
for (const entry of await store.glob(patterns, options)) result.set(entry.path, entry);
|
|
623
|
-
for (const source of sources.filter((source) => source.livePaths)) {
|
|
624
|
-
await pruneLiveSourceStoreEntries(result, source);
|
|
625
|
-
for (const entry of liveSourceEntries(source)) if (entry.type === "file" && patterns.some((pattern) => matchesAny(entry.path, pattern))) result.set(entry.path, entry);
|
|
626
|
-
}
|
|
627
|
-
return [...result.values()].sort((left, right) => left.path.localeCompare(right.path));
|
|
628
|
-
},
|
|
629
|
-
async search(query) {
|
|
630
|
-
return await searchWorkspace(query);
|
|
631
|
-
},
|
|
632
|
-
async stat(path) {
|
|
633
|
-
const resolution = resolveWorkspacePath(definition, path);
|
|
634
|
-
if (resolution.type === "source") {
|
|
635
|
-
await ensurePrepared(resolution.sourceKey);
|
|
636
|
-
if (isLiveSource(resolution.sourceKey)) {
|
|
637
|
-
if (!resolution.workspacePath) return {
|
|
638
|
-
path: "",
|
|
639
|
-
type: "directory"
|
|
640
|
-
};
|
|
641
|
-
const result = liveSourceStat(resolution.source, resolution.workspacePath);
|
|
642
|
-
if (!result) throw new WorkspaceError(`[vitehub] Workspace path does not exist: ${path}.`);
|
|
643
|
-
return result;
|
|
644
|
-
}
|
|
645
|
-
await ensureMaterialized(resolution.sourceKey);
|
|
646
|
-
const result = await statVirtualSourcePath(resolution.source, resolution.workspacePath, store, sourceContext);
|
|
647
|
-
if (!result) throw new WorkspaceError(`[vitehub] Workspace path does not exist: ${path}.`);
|
|
648
|
-
return result;
|
|
649
|
-
}
|
|
650
|
-
let result = await store.stat(resolution.workspacePath);
|
|
651
|
-
if (!result) {
|
|
652
|
-
if (!resolution.workspacePath && sources.some((source) => !source.mountPath && source.livePaths)) return {
|
|
653
|
-
path: "",
|
|
654
|
-
type: "directory"
|
|
655
|
-
};
|
|
656
|
-
await materializeRootSourceForPath(resolution.workspacePath);
|
|
657
|
-
result = await store.stat(resolution.workspacePath);
|
|
658
|
-
}
|
|
659
|
-
if (!result) throw new WorkspaceError(`[vitehub] Workspace path does not exist: ${path}.`);
|
|
660
|
-
return result;
|
|
661
|
-
},
|
|
662
|
-
async materializeSources(options) {
|
|
663
|
-
return await materializeWorkspaceSources(definition, store, options);
|
|
664
|
-
},
|
|
665
|
-
async exists(path) {
|
|
666
|
-
const resolution = resolveWorkspacePath(definition, path);
|
|
667
|
-
if (resolution.type === "source") {
|
|
668
|
-
await ensurePrepared(resolution.sourceKey);
|
|
669
|
-
if (isLiveSource(resolution.sourceKey)) {
|
|
670
|
-
if (!resolution.workspacePath) return true;
|
|
671
|
-
return Boolean(liveSourceStat(resolution.source, resolution.workspacePath));
|
|
672
|
-
}
|
|
673
|
-
await ensureMaterialized(resolution.sourceKey);
|
|
674
|
-
return Boolean(await statVirtualSourcePath(resolution.source, resolution.workspacePath, store, sourceContext));
|
|
675
|
-
}
|
|
676
|
-
if (await store.stat(resolution.workspacePath)) return true;
|
|
677
|
-
if (!resolution.workspacePath && sources.some((source) => !source.mountPath && source.livePaths)) return true;
|
|
678
|
-
await materializeRootSourceForPath(resolution.workspacePath);
|
|
679
|
-
return Boolean(await store.stat(resolution.workspacePath));
|
|
680
|
-
},
|
|
681
|
-
async mkdir(path, options) {
|
|
682
|
-
const resolution = resolveWorkspacePath(definition, path);
|
|
683
|
-
await assertWritableResolvedStorePath(path, resolution.workspacePath, resolution.type);
|
|
684
|
-
const input = await writePolicy.before({
|
|
685
|
-
operation: "mkdir",
|
|
686
|
-
path: resolution.workspacePath,
|
|
687
|
-
previous: await previousStat(resolution.workspacePath),
|
|
688
|
-
workspace: definition.name
|
|
689
|
-
});
|
|
690
|
-
try {
|
|
691
|
-
await store.mkdir(input.path, options);
|
|
692
|
-
await writePolicy.after(input);
|
|
693
|
-
} catch (error) {
|
|
694
|
-
await writePolicy.error(input, error);
|
|
695
|
-
throw error;
|
|
696
|
-
}
|
|
697
|
-
},
|
|
698
|
-
async rm(path, options) {
|
|
699
|
-
const resolution = resolveWorkspacePath(definition, path);
|
|
700
|
-
await assertWritableResolvedStorePath(path, resolution.workspacePath, resolution.type);
|
|
701
|
-
const input = await writePolicy.before({
|
|
702
|
-
operation: "rm",
|
|
703
|
-
path: resolution.workspacePath,
|
|
704
|
-
previous: await previousStat(resolution.workspacePath),
|
|
705
|
-
workspace: definition.name
|
|
706
|
-
});
|
|
707
|
-
try {
|
|
708
|
-
await store.rm(input.path, options);
|
|
709
|
-
await writePolicy.after(input);
|
|
710
|
-
} catch (error) {
|
|
711
|
-
await writePolicy.error(input, error);
|
|
712
|
-
throw error;
|
|
713
|
-
}
|
|
714
|
-
}
|
|
715
|
-
};
|
|
716
|
-
async function pruneLiveSourceStoreEntries(result, source) {
|
|
717
|
-
const allowed = new Set(liveSourceEntries(source).map((entry) => entry.path));
|
|
718
|
-
for (const [path, entry] of result) {
|
|
719
|
-
if (!isWithinLiveSourceMount(source, path) || allowed.has(path)) continue;
|
|
720
|
-
if (entry.type === "file") {
|
|
721
|
-
if ((await store.readFile(path))?.metadata?.source === source.key) result.delete(path);
|
|
722
|
-
continue;
|
|
723
|
-
}
|
|
724
|
-
if (!source.mountPath) continue;
|
|
725
|
-
if (![...allowed].some((allowedPath) => allowedPath.startsWith(`${path}/`))) result.delete(path);
|
|
726
|
-
}
|
|
727
|
-
}
|
|
728
|
-
}
|
|
729
|
-
function isWithinLiveSourceMount(source, path) {
|
|
730
|
-
return source.mountPath ? sourceMountContainsPath(source, path) : Boolean(path);
|
|
731
|
-
}
|
|
732
|
-
function liveSourceEntries(source) {
|
|
733
|
-
const paths = Object.keys(source.livePaths || {});
|
|
734
|
-
const entries = /* @__PURE__ */ new Map();
|
|
735
|
-
for (const path of paths) {
|
|
736
|
-
const parts = path.split("/").filter(Boolean);
|
|
737
|
-
for (let index = 1; index < parts.length; index++) {
|
|
738
|
-
const directory = parts.slice(0, index).join("/");
|
|
739
|
-
entries.set(directory, {
|
|
740
|
-
path: directory,
|
|
741
|
-
type: "directory"
|
|
742
|
-
});
|
|
743
|
-
}
|
|
744
|
-
entries.set(path, {
|
|
745
|
-
path,
|
|
746
|
-
type: "file"
|
|
747
|
-
});
|
|
748
|
-
}
|
|
749
|
-
return [...entries.values()];
|
|
750
|
-
}
|
|
751
|
-
function addLiveSourceEntries(result, path, options, sources) {
|
|
752
|
-
const normalized = normalizeWorkspacePath(path);
|
|
753
|
-
for (const source of sources.filter((source) => source.livePaths)) for (const entry of liveSourceEntries(source)) {
|
|
754
|
-
if (!isListedLiveEntry(entry, normalized, options)) continue;
|
|
755
|
-
result.set(entry.path, entry);
|
|
756
|
-
}
|
|
757
|
-
}
|
|
758
|
-
function isListedLiveEntry(entry, path, options) {
|
|
759
|
-
if (!path) {
|
|
760
|
-
if (options.recursive) return true;
|
|
761
|
-
return !entry.path.includes("/");
|
|
762
|
-
}
|
|
763
|
-
if (entry.path === path) return false;
|
|
764
|
-
if (!entry.path.startsWith(`${path}/`)) return false;
|
|
765
|
-
if (options.recursive) return true;
|
|
766
|
-
return !entry.path.slice(path.length + 1).includes("/");
|
|
767
|
-
}
|
|
768
|
-
function liveSourceStat(source, workspacePath) {
|
|
769
|
-
return liveSourceEntries(source).find((entry) => entry.path === workspacePath);
|
|
770
|
-
}
|
|
771
|
-
async function currentSourceTreePaths(source, sourceContext) {
|
|
772
|
-
const allowed = new Set([source.mountPath]);
|
|
773
|
-
const keys = await source.source.getKeys(sourceContext);
|
|
774
|
-
for (const key of keys) {
|
|
775
|
-
const parts = `${source.mountPath}/${key}`.replace(/\/+/g, "/").split("/");
|
|
776
|
-
for (let index = 1; index <= parts.length; index++) allowed.add(parts.slice(0, index).join("/"));
|
|
777
|
-
}
|
|
778
|
-
return allowed;
|
|
779
|
-
}
|
|
780
|
-
function dedupeHits(hits) {
|
|
781
|
-
const seen = /* @__PURE__ */ new Set();
|
|
782
|
-
return hits.filter((hit) => {
|
|
783
|
-
const key = `${hit.path}:${hit.line}:${hit.column}:${hit.text}`;
|
|
784
|
-
if (seen.has(key)) return false;
|
|
785
|
-
seen.add(key);
|
|
786
|
-
return true;
|
|
787
|
-
});
|
|
788
|
-
}
|
|
789
|
-
//#endregion
|
|
790
|
-
//#region src/core/workspace-cache.ts
|
|
791
|
-
let storeByDefinition = /* @__PURE__ */ new WeakMap();
|
|
792
|
-
function getCachedWorkspaceStore(definition, create) {
|
|
793
|
-
let store = storeByDefinition.get(definition);
|
|
794
|
-
if (!store) {
|
|
795
|
-
store = create();
|
|
796
|
-
storeByDefinition.set(definition, store);
|
|
797
|
-
}
|
|
798
|
-
return store;
|
|
799
|
-
}
|
|
800
|
-
function resetWorkspaceStoreCache() {
|
|
801
|
-
storeByDefinition = /* @__PURE__ */ new WeakMap();
|
|
802
|
-
}
|
|
803
|
-
//#endregion
|
|
804
|
-
//#region src/core/workspace.ts
|
|
805
|
-
function getStore(definition) {
|
|
806
|
-
return getCachedWorkspaceStore(definition, () => createWorkspaceStoreFromProvider(definition));
|
|
807
|
-
}
|
|
808
|
-
function createWorkspace(definition) {
|
|
809
|
-
const store = getStore(definition);
|
|
810
|
-
const files = createWorkspaceSourceView(definition, store);
|
|
811
|
-
const workspace = {
|
|
812
|
-
name: definition.name,
|
|
813
|
-
async sync() {
|
|
814
|
-
const { syncWorkspaceDefinition } = await import("./lifecycle-uD82JeGh.js").then((n) => n.t);
|
|
815
|
-
await syncWorkspaceDefinition(definition, store);
|
|
816
|
-
},
|
|
817
|
-
async materializeSources(options) {
|
|
818
|
-
return await files.materializeSources(options);
|
|
819
|
-
},
|
|
820
|
-
async readFile(path, options) {
|
|
821
|
-
return await files.readFile(path, options);
|
|
822
|
-
},
|
|
823
|
-
async writeFile(path, content, options) {
|
|
824
|
-
await files.writeFile(path, content, options);
|
|
825
|
-
},
|
|
826
|
-
async list(path, options) {
|
|
827
|
-
return await files.list(path, options);
|
|
828
|
-
},
|
|
829
|
-
async glob(pattern, options) {
|
|
830
|
-
return await files.glob(pattern, options);
|
|
831
|
-
},
|
|
832
|
-
async search(query) {
|
|
833
|
-
return await files.search(query);
|
|
834
|
-
},
|
|
835
|
-
async stat(path) {
|
|
836
|
-
return await files.stat(path);
|
|
837
|
-
},
|
|
838
|
-
async exists(path) {
|
|
839
|
-
return await files.exists(path);
|
|
840
|
-
},
|
|
841
|
-
async mkdir(path, options) {
|
|
842
|
-
await files.mkdir(path, options);
|
|
843
|
-
},
|
|
844
|
-
async rm(path, options) {
|
|
845
|
-
await files.rm(path, options);
|
|
846
|
-
},
|
|
847
|
-
async snapshot(options) {
|
|
848
|
-
const snapshot = await store.snapshot(options);
|
|
849
|
-
const { publishWorkspaceSnapshot } = await import("./lifecycle-uD82JeGh.js").then((n) => n.t);
|
|
850
|
-
await publishWorkspaceSnapshot(definition, store, snapshot);
|
|
851
|
-
return snapshot;
|
|
852
|
-
},
|
|
853
|
-
async diff(options) {
|
|
854
|
-
return await store.diff(options);
|
|
855
|
-
},
|
|
856
|
-
async startSession(_options) {
|
|
857
|
-
if (definition.runtime === "sandbox") {
|
|
858
|
-
const { createSandboxWorkspaceSession } = await import("./sandbox-DtGXCWRq.js");
|
|
859
|
-
return await createSandboxWorkspaceSession(definition, workspace);
|
|
860
|
-
}
|
|
861
|
-
return createBasicWorkspaceSession(workspace);
|
|
862
|
-
},
|
|
863
|
-
mount(options) {
|
|
864
|
-
return {
|
|
865
|
-
workspace,
|
|
866
|
-
mode: options?.mode || "read-only",
|
|
867
|
-
target: options?.target || "/workspace",
|
|
868
|
-
async diff() {
|
|
869
|
-
return await workspace.diff();
|
|
870
|
-
},
|
|
871
|
-
async commit() {
|
|
872
|
-
await workspace.snapshot({ name: "mount-commit" });
|
|
873
|
-
},
|
|
874
|
-
async export() {
|
|
875
|
-
return await workspace.snapshot({ name: "mount-export" });
|
|
876
|
-
}
|
|
877
|
-
};
|
|
878
|
-
}
|
|
879
|
-
};
|
|
880
|
-
return workspace;
|
|
881
|
-
}
|
|
882
|
-
//#endregion
|
|
883
|
-
//#region src/core/registry.ts
|
|
884
|
-
const registeredDefinitions = /* @__PURE__ */ new Map();
|
|
885
|
-
const loadedDefinitions = /* @__PURE__ */ new Map();
|
|
886
|
-
let loaders = workspaceRegistry;
|
|
887
|
-
function pickWorkspaceFields(definition) {
|
|
888
|
-
const { __vitehubWorkspaceAgent: _agent, __vitehubWorkspaceAgentDefaults: _agentDefaults, __vitehubWorkspaceAgentOptions: _agentOptions, description: _description, fallback: _fallback, instructions: _instructions, model: _model, name: _agentName, resolve: _resolve, run: _run, stepLimit: _stepLimit, workspace: _workspace, ...workspace } = definition;
|
|
889
|
-
return workspace;
|
|
890
|
-
}
|
|
891
|
-
function normalizeWorkspaceDefinition(name, definition) {
|
|
892
|
-
if (!definition) throw new WorkspaceNotFoundError(name);
|
|
893
|
-
const workspaceAgentOptions = definition.__vitehubWorkspaceAgentOptions;
|
|
894
|
-
if (workspaceAgentOptions?.workspace) {
|
|
895
|
-
const injectedWorkspace = pickWorkspaceFields(definition);
|
|
896
|
-
const configuredWorkspace = typeof workspaceAgentOptions.workspace === "string" ? {} : workspaceAgentOptions.workspace;
|
|
897
|
-
return {
|
|
898
|
-
...injectedWorkspace,
|
|
899
|
-
...configuredWorkspace,
|
|
900
|
-
rootDir: configuredWorkspace.rootDir ?? injectedWorkspace.rootDir,
|
|
901
|
-
sourceRootDir: configuredWorkspace.sourceRootDir ?? injectedWorkspace.sourceRootDir,
|
|
902
|
-
name
|
|
903
|
-
};
|
|
904
|
-
}
|
|
905
|
-
if ("name" in definition) throw new TypeError(`[vitehub] Workspace definition "${name}" must not declare a name. Workspace names are inferred from filenames.`);
|
|
906
|
-
return {
|
|
907
|
-
...pickWorkspaceFields(definition),
|
|
908
|
-
name
|
|
909
|
-
};
|
|
910
|
-
}
|
|
911
|
-
function registerWorkspace(name, definition) {
|
|
912
|
-
if (!name || typeof name !== "string") throw new TypeError("[vitehub] registerWorkspace requires a string name.");
|
|
913
|
-
registeredDefinitions.set(name, normalizeWorkspaceDefinition(name, definition));
|
|
914
|
-
}
|
|
915
|
-
function setWorkspaceRegistry(registry) {
|
|
916
|
-
loaders = registry;
|
|
917
|
-
loadedDefinitions.clear();
|
|
918
|
-
}
|
|
919
|
-
async function resolveWorkspaceDefinition(name) {
|
|
920
|
-
const existing = registeredDefinitions.get(name) || loadedDefinitions.get(name);
|
|
921
|
-
if (existing) return existing;
|
|
922
|
-
const load = loaders[name];
|
|
923
|
-
if (!load) throw new WorkspaceNotFoundError(name);
|
|
924
|
-
const definition = normalizeWorkspaceDefinition(name, (await load()).default);
|
|
925
|
-
loadedDefinitions.set(name, definition);
|
|
926
|
-
return definition;
|
|
927
|
-
}
|
|
928
|
-
async function useRegisteredWorkspace(name) {
|
|
929
|
-
return createWorkspace(await resolveWorkspaceDefinition(name));
|
|
930
|
-
}
|
|
931
|
-
//#endregion
|
|
932
|
-
export { resolveWorkspaceAutoCommit as a, resetWorkspaceStoreCache as i, setWorkspaceRegistry as n, useRegisteredWorkspace as r, registerWorkspace as t };
|