@vite-hub/workspace 0.0.3 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +236 -58
- package/dist/{ai-nmHdzjCM.d.ts → ai-BW2ZhizI.d.ts} +22 -7
- package/dist/{ai-Ce3PtWFW.js → ai-BZyLwULx.js} +147 -66
- package/dist/ai.d.ts +2 -2
- package/dist/ai.js +1 -1
- package/dist/{asset-registry-CaG9b7tf.js → asset-registry-DfBV5kmw.js} +3 -2
- package/dist/association-Cm0dUTDU-CDDhJnIh-Cv3bWnVb.js +19 -0
- package/dist/blob-database.d.ts +521 -0
- package/dist/blob-database.js +601 -0
- package/dist/cloudflare.d.ts +5 -6
- package/dist/cloudflare.js +1 -1
- package/dist/config-B7dmzDX3.js +1112 -0
- package/dist/{config-3I1BnsdO.d.ts → config-DLKdaFWN.d.ts} +1 -2
- package/dist/error-diagnostics-A_8Kp-48.js +116 -0
- package/dist/error-diagnostics-DY5GP47p.js +96 -0
- package/dist/events-7EBoeIPd-A2RUkJi5-BxrICQ17.js +354 -0
- package/dist/events-CRaLy7U3-3PNNp5WZ-Gv9nd0cH.js +214 -0
- package/dist/events-CeChclTf-CX-gD8oy-4aL2j6V-.js +527 -0
- package/dist/feed-DsC7Vws1-M_de2nSK-tKc0Pwo8.js +248 -0
- package/dist/feed-Dyf730Ys-D0HgBba6-BUnCa5gS.js +179 -0
- package/dist/file-C0_aPWSS.js +26 -0
- package/dist/file-metadata-CxDy-E2H.js +58 -0
- package/dist/{files-QRV55OCx.js → files-C7Px8uaC.js} +21 -10
- package/dist/forgejo-BN8ItCfl-Bbn4vhDd.js +2278 -0
- package/dist/{github-CfA5vpJ5.js → github-CPxHtFB7.js} +36 -34
- package/dist/github-DtncTfmv-Du2S8lGF.js +4159 -0
- package/dist/gitlab-BoDm8-BC-xzZsW9eW.js +2530 -0
- package/dist/glob--XcLlcml.js +22 -0
- package/dist/glob--kNYDbfU.js +30 -0
- package/dist/graphql-DG5aJBT3-DceyFYdM-C4saqX37.js +489 -0
- package/dist/host-CFnrTXTK.js +1086 -0
- package/dist/{hosted-C1-tTTTf.js → hosted-acMA5XyL.js} +216 -43
- package/dist/hosted-vercel-blob-DKntsHaf.js +12603 -0
- package/dist/hosted-vercel-blob.d.ts +5 -6
- package/dist/hosted-vercel-blob.js +1 -1
- package/dist/hosted.d.ts +6 -7
- package/dist/hosted.js +1 -1
- package/dist/index.d.ts +76 -64
- package/dist/index.js +162 -457
- package/dist/inspect-BoObD2cJ.js +167 -0
- package/dist/internal/path.d.ts +2 -0
- package/dist/internal/path.js +2 -0
- package/dist/lifecycle-BwZ7c9hb.js +596 -0
- package/dist/loader.d.ts +5 -6
- package/dist/loader.js +3 -2
- package/dist/mcp-resources-BREFCmtn.js +39 -0
- package/dist/memory-BbwOqDXl.js +248 -0
- package/dist/metadata-target-DZEZQGHA.js +1588 -0
- package/dist/mountx.d.ts +4 -6
- package/dist/mountx.js +10 -8
- package/dist/nitro.d.ts +1 -1
- package/dist/nitro.js +1 -1
- package/dist/nuxt.d.ts +13 -12
- package/dist/nuxt.js +10 -8
- package/dist/operations-BcpWr41m.js +159 -0
- package/dist/operations-CGqY1XeG.js +965 -0
- package/dist/operations-DnOwKsJT.d.ts +136 -0
- package/dist/operations.d.ts +53 -0
- package/dist/operations.js +2 -0
- package/dist/{path-CNI6wKIU.js → path-CNAwN7pw.js} +34 -35
- package/dist/path-DrSIenjj.js +59 -0
- package/dist/path-zHxQUbqa.d.ts +9 -0
- package/dist/plugins-DqQ8b4T5.d.ts +10 -0
- package/dist/provider-kXjfli9u.js +1457 -0
- package/dist/providers/github/store.d.ts +3 -5
- package/dist/providers/github/store.js +1 -1
- package/dist/registry-Bp-8UUfG.js +129 -0
- package/dist/{registry-BIQVCBWI.d.ts → registry-Cg1TJVVZ.d.ts} +3 -4
- package/dist/{request-execution-u9mgUXKX.js → request-execution-Dk9rY-Yh.js} +24 -20
- package/dist/rolldown-runtime-B4iAMlE-.js +35 -0
- package/dist/runtime/assets.d.ts +3 -5
- package/dist/runtime/assets.js +9 -6
- package/dist/runtime/empty-assets-registry.d.ts +1 -2
- package/dist/runtime/empty-registry.d.ts +1 -2
- package/dist/runtime/state.d.ts +4 -4
- package/dist/runtime/state.js +10 -7
- package/dist/runtime/workspace.d.ts +1 -1
- package/dist/runtime/workspace.js +1 -1
- package/dist/runtime.d.ts +84 -15
- package/dist/runtime.js +561 -108
- package/dist/{search-C4REyHj9.js → search-CrTmXj5h.js} +10 -9
- package/dist/server-BNQ-Nbjj.js +290 -0
- package/dist/server.d.ts +22 -34
- package/dist/server.js +2 -355
- package/dist/shared-DalXdP0i.js +2158 -0
- package/dist/source-metadata.d.ts +7 -1
- package/dist/source-metadata.js +34 -2
- package/dist/{state-TTqa3x3Y.d.ts → state-B8zbLi5t.d.ts} +14 -13
- package/dist/store-CXgmu-uw.js +1729 -0
- package/dist/{sync-DiuDmSGk.js → sync-CN6ec-5M.js} +62 -41
- package/dist/templates-Bn8a-pd2-D9DhJEVD-CBfXNRzz.js +125 -0
- package/dist/test.d.ts +1 -1
- package/dist/test.js +1 -1
- package/dist/token-CO62GglH.js +61 -0
- package/dist/{types-60Yn149G.d.ts → types-DeK9GIxy.d.ts} +348 -49
- package/dist/{use-CsV3oIxH.js → use-BQj7nSz6.js} +200 -45
- package/dist/{use-CUFSnFCS.d.ts → use-DhwhbAVV.d.ts} +34 -12
- package/dist/utils-Blrh4N2n.js +545 -0
- package/dist/vite-BEvgU2GY.js +20909 -0
- package/dist/vite-CcZmMQ7S.d.ts +476 -0
- package/dist/vite.d.ts +2 -50
- package/dist/vite.js +2 -2
- package/dist/webhooks-DgHwxxYz-D1V2LOr1-azofFOiP.js +1565 -0
- package/dist/workspace-6pxkOuxr.js +1286 -0
- package/dist/workspace-BWJQBK1u.d.ts +7 -0
- package/package.json +90 -59
- package/dist/chunk-8H4AJuhK.js +0 -14
- package/dist/collections/client.d.ts +0 -39
- package/dist/collections/client.js +0 -137
- package/dist/collections.d.ts +0 -59
- package/dist/collections.js +0 -159
- package/dist/config-CDoyls2X.js +0 -30
- package/dist/config-CZLO0m9h.js +0 -7811
- package/dist/file-CHfsZpso.js +0 -23
- package/dist/glob-D0vD_wRY.js +0 -17
- package/dist/host-CAnbugpI.js +0 -370
- package/dist/hosted-vercel-blob-CWBILDVo.js +0 -257
- package/dist/lifecycle-BYoa9hVb.js +0 -176
- package/dist/provider-C3_elpEh.js +0 -599
- package/dist/publish.d.ts +0 -35
- package/dist/publish.js +0 -112
- package/dist/registry-CqyDTW9Y.js +0 -74
- package/dist/runtime-options-BsPSv5O7.js +0 -13
- package/dist/shared-jfogBrd5.js +0 -273
- package/dist/store-Bs1SLh55.js +0 -414
- package/dist/vite-tbfDxtlu.js +0 -2191
- package/dist/workspace-66SKyiec.d.ts +0 -6
- package/dist/workspace-C4k92Ow8.js +0 -1460
|
@@ -0,0 +1,1286 @@
|
|
|
1
|
+
import { s as workspaceError, t as normalizeSafeWorkspacePath } from "./path-DrSIenjj.js";
|
|
2
|
+
import { a as isExcludedWorkspacePath, f as sha256, i as decodeFile, l as normalizeWorkspacePath, n as contentStreamToBytes, r as contentToBytes, s as matchesAny } from "./path-CNAwN7pw.js";
|
|
3
|
+
import { t as forwardWorkspaceStoreTarget } from "./target-D4Bqb1eH.js";
|
|
4
|
+
import { a as normalizeWorkspaceSources, c as sourceMountIntersectsPath, f as prepareWorkspaceSource, n as createSourceContext, s as sourceMountContainsPath, v as workspaceSourceRequestDescriptorPath } from "./config-B7dmzDX3.js";
|
|
5
|
+
import { t as workspaceErrorDiagnostics } from "./error-diagnostics-DY5GP47p.js";
|
|
6
|
+
import { n as createWorkspaceSourceRequestExecution, t as attachWorkspaceSourceRequestExecution } from "./request-execution-Dk9rY-Yh.js";
|
|
7
|
+
import { t as createMemoryWorkspaceStore } from "./memory-BbwOqDXl.js";
|
|
8
|
+
import { t as copyJsonFileMetadata } from "./file-metadata-CxDy-E2H.js";
|
|
9
|
+
import { d as normalizeHistoryRef, i as createWorkspaceHistory, l as historyConflict, m as createWorkspaceWritePolicy } from "./utils-Blrh4N2n.js";
|
|
10
|
+
import { t as createWorkspaceGlobMatcher } from "./glob--kNYDbfU.js";
|
|
11
|
+
import { t as createWorkspaceStoreFromProvider } from "./provider-kXjfli9u.js";
|
|
12
|
+
import { n as forwardWorkspaceRevisionMaterializer } from "./store-CXgmu-uw.js";
|
|
13
|
+
import { t as searchText } from "./search-CrTmXj5h.js";
|
|
14
|
+
import { C as withWorkspaceStoreMutation, D as readWorkspaceFileOwner, _ as reconcileRemovedStartupSources, b as statVirtualSourcePath, c as workspaceInternalMetadataCapability, f as materializeWorkspaceSources, g as readSourceSnapshotPaths, h as readResolvedSourceFile, l as hasCurrentSourceSnapshot, m as readCurrentSourceSnapshot, n as createWorkspaceMetadataTarget, p as materializesCompleteSource, t as attachWorkspaceMetadataTarget, u as hasFreshSourceSnapshot, v as searchMaterializedStore } from "./metadata-target-DZEZQGHA.js";
|
|
15
|
+
import { hasRuntimeType } from "@vite-hub/runtime/internal/runtime-type";
|
|
16
|
+
import { noExecutionAuthority } from "@vite-hub/runtime";
|
|
17
|
+
//#region src/session/scope.ts
|
|
18
|
+
function isMissingWorkspacePathError(error) {
|
|
19
|
+
return error instanceof Error && error.message.includes("Workspace path does not exist:");
|
|
20
|
+
}
|
|
21
|
+
function pathInSessionScope(path, paths) {
|
|
22
|
+
return !paths || paths.some((scope) => path === scope || path.startsWith(`${scope}/`));
|
|
23
|
+
}
|
|
24
|
+
function pathIntersectsSessionScope(path, paths) {
|
|
25
|
+
return pathInSessionScope(path, paths) || Boolean(paths?.some((scope) => scope.startsWith(`${path}/`)));
|
|
26
|
+
}
|
|
27
|
+
function assertPathInSessionScope(path, paths, options = {}) {
|
|
28
|
+
if (!(options.mkdir ? pathIntersectsSessionScope(path, paths) : pathInSessionScope(path, paths))) throw workspaceError(options.masked ? `[vitehub] Workspace file does not exist: ${path}.` : `[vitehub] Workspace session path ${path} is outside the session scope.`);
|
|
29
|
+
return path;
|
|
30
|
+
}
|
|
31
|
+
function assertDiffInsideSessionPaths(diff, paths) {
|
|
32
|
+
if (!paths) return;
|
|
33
|
+
const entry = diff.entries.find((entry) => !diffEntryInSessionScope(entry, paths));
|
|
34
|
+
if (entry) throw workspaceError(`[vitehub] Workspace session path ${entry.path} is outside the session scope.`);
|
|
35
|
+
}
|
|
36
|
+
function filterSessionEntries(entries, paths) {
|
|
37
|
+
if (!paths) return entries;
|
|
38
|
+
return entries.filter((entry) => pathIntersectsSessionScope(entry.path, paths));
|
|
39
|
+
}
|
|
40
|
+
function filterSessionDiff(diff, paths) {
|
|
41
|
+
if (!paths) return diff;
|
|
42
|
+
return {
|
|
43
|
+
...diff,
|
|
44
|
+
entries: diff.entries.filter((entry) => diffEntryInSessionScope(entry, paths))
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
function diffEntryInSessionScope(entry, paths) {
|
|
48
|
+
if (pathInSessionScope(entry.path, paths)) return true;
|
|
49
|
+
return entry.after?.type === "directory" && pathIntersectsSessionScope(entry.path, paths);
|
|
50
|
+
}
|
|
51
|
+
function scopedSearchQuery(query, paths, normalizePath) {
|
|
52
|
+
if (!paths) return query;
|
|
53
|
+
const requested = query.paths?.length ? query.paths : [query.cwd || ""];
|
|
54
|
+
const scoped = /* @__PURE__ */ new Set();
|
|
55
|
+
for (const rawPath of requested) {
|
|
56
|
+
const path = normalizePath(rawPath);
|
|
57
|
+
for (const scope of paths) if (!path || pathInSessionScope(scope, [path])) scoped.add(scope);
|
|
58
|
+
else if (pathInSessionScope(path, [scope])) scoped.add(path);
|
|
59
|
+
}
|
|
60
|
+
if (!scoped.size) return void 0;
|
|
61
|
+
return {
|
|
62
|
+
...query,
|
|
63
|
+
cwd: void 0,
|
|
64
|
+
paths: [...scoped].sort((left, right) => left.localeCompare(right))
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
//#endregion
|
|
68
|
+
//#region src/session/basic.ts
|
|
69
|
+
function unsupportedExec() {
|
|
70
|
+
throw workspaceError("[vitehub] Workspace exec requires a Sandbox session host. Pass the active Sandbox session to workspace.startSession({ host }).");
|
|
71
|
+
}
|
|
72
|
+
function normalizeSessionPaths(options) {
|
|
73
|
+
const paths = [...new Set((options?.paths || []).map((path) => normalizeSafeWorkspacePath(path, {
|
|
74
|
+
allowEmpty: true,
|
|
75
|
+
allowReserved: true
|
|
76
|
+
})))];
|
|
77
|
+
if (!paths.length || paths.includes("")) return void 0;
|
|
78
|
+
return paths.sort((left, right) => left.length - right.length || left.localeCompare(right));
|
|
79
|
+
}
|
|
80
|
+
function normalizeSessionPath(path = "", options = {}) {
|
|
81
|
+
const normalized = path.replace(/\\/g, "/");
|
|
82
|
+
return normalizeSafeWorkspacePath(normalized === "." ? "" : normalized, {
|
|
83
|
+
allowEmpty: options.allowEmpty,
|
|
84
|
+
allowReserved: true
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
async function createBasicWorkspaceSession(workspace, options) {
|
|
88
|
+
const writeBackDisabled = options?.writeBack === false;
|
|
89
|
+
const sessionPaths = normalizeSessionPaths(options);
|
|
90
|
+
const overlay = createMemoryWorkspaceStore();
|
|
91
|
+
const initialEntries = filterSessionEntries(await workspace.list("", { recursive: true }), sessionPaths);
|
|
92
|
+
for (const entry of initialEntries.filter((entry) => entry.type === "directory")) await overlay.mkdir(entry.path, { recursive: true });
|
|
93
|
+
for (const entry of initialEntries.filter((entry) => entry.type === "file")) await overlay.writeFile(entry.path, {
|
|
94
|
+
content: await workspace.readFile(entry.path, { encoding: "binary" }),
|
|
95
|
+
mediaType: entry.mediaType,
|
|
96
|
+
metadata: entry.metadata,
|
|
97
|
+
path: entry.path
|
|
98
|
+
});
|
|
99
|
+
await overlay.snapshot({ name: "session-baseline" });
|
|
100
|
+
return {
|
|
101
|
+
executionAuthority: noExecutionAuthority,
|
|
102
|
+
async readFile(path, readOptions) {
|
|
103
|
+
const target = assertPathInSessionScope(normalizeSessionPath(path), sessionPaths, { masked: true });
|
|
104
|
+
const file = await overlay.readFile(target);
|
|
105
|
+
if (!file) throw workspaceError(`[vitehub] Workspace file does not exist: ${path}.`);
|
|
106
|
+
return decodeFile(file.content, readOptions);
|
|
107
|
+
},
|
|
108
|
+
async writeFile(path, content, writeOptions) {
|
|
109
|
+
const target = assertPathInSessionScope(normalizeSessionPath(path), sessionPaths);
|
|
110
|
+
await overlay.writeFile(target, {
|
|
111
|
+
content,
|
|
112
|
+
path: target,
|
|
113
|
+
...writeOptions
|
|
114
|
+
});
|
|
115
|
+
},
|
|
116
|
+
async mkdir(path, mkdirOptions) {
|
|
117
|
+
await overlay.mkdir(assertPathInSessionScope(normalizeSessionPath(path, { allowEmpty: true }), sessionPaths, { mkdir: true }), mkdirOptions);
|
|
118
|
+
},
|
|
119
|
+
async rm(path, rmOptions) {
|
|
120
|
+
await overlay.rm(assertPathInSessionScope(normalizeSessionPath(path), sessionPaths), rmOptions);
|
|
121
|
+
},
|
|
122
|
+
async list(path = "", listOptions = {}) {
|
|
123
|
+
return filterSessionEntries(await overlay.list(normalizeSessionPath(path, { allowEmpty: true }), listOptions), sessionPaths);
|
|
124
|
+
},
|
|
125
|
+
async glob(pattern, globOptions) {
|
|
126
|
+
const patterns = Array.isArray(pattern) ? pattern : [pattern];
|
|
127
|
+
const cwd = normalizeSessionPath(globOptions?.cwd || "", { allowEmpty: true });
|
|
128
|
+
return filterSessionEntries(await overlay.list("", { recursive: true }), sessionPaths).filter((entry) => {
|
|
129
|
+
if (entry.type !== "file") return false;
|
|
130
|
+
if (!cwd) return patterns.some((item) => matchesAny(entry.path, item));
|
|
131
|
+
if (!entry.path.startsWith(`${cwd}/`)) return false;
|
|
132
|
+
return patterns.some((item) => matchesAny(entry.path.slice(cwd.length + 1), item));
|
|
133
|
+
});
|
|
134
|
+
},
|
|
135
|
+
async search(query) {
|
|
136
|
+
const scoped = scopedSearchQuery(query, sessionPaths, (path) => normalizeSessionPath(path, { allowEmpty: true }));
|
|
137
|
+
if (!scoped) return [];
|
|
138
|
+
const roots = (scoped.paths?.length ? scoped.paths : [scoped.cwd || ""]).map((path) => normalizeSessionPath(path, { allowEmpty: true }));
|
|
139
|
+
const hits = [];
|
|
140
|
+
const limit = scoped.limit ?? 100;
|
|
141
|
+
for (const entry of filterSessionEntries(await overlay.list("", { recursive: true }), sessionPaths).filter((entry) => entry.type === "file")) {
|
|
142
|
+
if (!roots.some((root) => !root || entry.path === root || entry.path.startsWith(`${root}/`))) continue;
|
|
143
|
+
const file = await overlay.readFile(entry.path);
|
|
144
|
+
if (!file) continue;
|
|
145
|
+
hits.push(...searchText(entry.path, decodeFile(file.content), {
|
|
146
|
+
...scoped,
|
|
147
|
+
limit: limit - hits.length
|
|
148
|
+
}));
|
|
149
|
+
if (hits.length >= limit) break;
|
|
150
|
+
}
|
|
151
|
+
return hits;
|
|
152
|
+
},
|
|
153
|
+
async diff() {
|
|
154
|
+
if (writeBackDisabled) throw workspaceError("[vitehub] Workspace Session diff is unavailable when writeBack is false.");
|
|
155
|
+
return filterSessionDiff(await overlay.diff(), sessionPaths);
|
|
156
|
+
},
|
|
157
|
+
async commit(commitOptions) {
|
|
158
|
+
if (writeBackDisabled) throw workspaceError("[vitehub] Workspace Session commit is unavailable when writeBack is false.");
|
|
159
|
+
const diff = await overlay.diff();
|
|
160
|
+
assertDiffInsideSessionPaths(diff, sessionPaths);
|
|
161
|
+
assertDiffInsideSessionPaths(await workspace.diff(), sessionPaths);
|
|
162
|
+
for (const entry of [...diff.entries].sort((left, right) => right.path.length - left.path.length)) if (entry.type === "removed") await workspace.rm(entry.path, {
|
|
163
|
+
force: true,
|
|
164
|
+
recursive: true
|
|
165
|
+
});
|
|
166
|
+
for (const entry of diff.entries) {
|
|
167
|
+
if (!entry.after) continue;
|
|
168
|
+
if (entry.after.type === "directory") {
|
|
169
|
+
if (entry.before?.type === "file") await workspace.rm(entry.path, { force: true });
|
|
170
|
+
await workspace.mkdir(entry.path, { recursive: true });
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
if (entry.before?.type === "directory") await workspace.rm(entry.path, {
|
|
174
|
+
force: true,
|
|
175
|
+
recursive: true
|
|
176
|
+
});
|
|
177
|
+
const file = await overlay.readFile(entry.path);
|
|
178
|
+
if (file) await workspace.writeFile(entry.path, file.content, {
|
|
179
|
+
mediaType: file.mediaType,
|
|
180
|
+
metadata: file.metadata
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
await workspace.snapshot({ name: commitOptions?.message || "session-commit" });
|
|
184
|
+
await overlay.snapshot({ name: commitOptions?.message || "session-commit" });
|
|
185
|
+
},
|
|
186
|
+
async exec(_command, _args = [], _options) {
|
|
187
|
+
unsupportedExec();
|
|
188
|
+
},
|
|
189
|
+
async close() {}
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
//#endregion
|
|
193
|
+
//#region src/sources/resolver.ts
|
|
194
|
+
function resolveWorkspacePath(definition, path) {
|
|
195
|
+
const workspacePath = normalizeSafeWorkspacePath(path, { allowEmpty: true });
|
|
196
|
+
const sources = normalizeWorkspaceSources(definition.sources).filter((source) => source.materialize !== "none");
|
|
197
|
+
for (const source of sources) {
|
|
198
|
+
if (source.requestOnly) continue;
|
|
199
|
+
const liveSourcePath = source.livePaths?.[workspacePath];
|
|
200
|
+
if (typeof liveSourcePath === "string") return createSourceResolution(source, workspacePath, liveSourcePath);
|
|
201
|
+
if (workspacePath === source.mountPath) return createSourceResolution(source, workspacePath, "");
|
|
202
|
+
if (sourceMountContainsPath(source, workspacePath)) return createSourceResolution(source, workspacePath, source.mountPath ? workspacePath.slice(source.mountPath.length + 1) : workspacePath);
|
|
203
|
+
}
|
|
204
|
+
return {
|
|
205
|
+
type: "store",
|
|
206
|
+
workspacePath,
|
|
207
|
+
readonly: false
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
function createSourceResolution(source, workspacePath, sourcePath) {
|
|
211
|
+
return {
|
|
212
|
+
type: "source",
|
|
213
|
+
sourceKey: source.key,
|
|
214
|
+
source,
|
|
215
|
+
workspacePath,
|
|
216
|
+
sourcePath: normalizeWorkspacePath(sourcePath),
|
|
217
|
+
materialize: source.materialize,
|
|
218
|
+
cache: source.cache,
|
|
219
|
+
validate: source.validate,
|
|
220
|
+
readonly: true
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
//#endregion
|
|
224
|
+
//#region src/sources/sync-state.ts
|
|
225
|
+
function sourceSyncMetaKey(sourceKey, workspace) {
|
|
226
|
+
return workspace ? `workspace:${workspace}:source:${sourceKey}:sync` : `source:${sourceKey}:sync`;
|
|
227
|
+
}
|
|
228
|
+
/** Identifies the durable metadata record owned by Source Sync. */
|
|
229
|
+
function isWorkspaceSourceSyncMetaKey(key) {
|
|
230
|
+
return [key.toLowerCase(), normalizeWorkspacePath(key).toLowerCase()].some((form) => /^(?:workspace:.+:)?source:.*:sync$/.test(form));
|
|
231
|
+
}
|
|
232
|
+
function readWorkspaceSourceSyncState(value) {
|
|
233
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return;
|
|
234
|
+
const state = value;
|
|
235
|
+
if (typeof state.configHash !== "string") return;
|
|
236
|
+
if (typeof state.source !== "string" || typeof state.mountPath !== "string") return;
|
|
237
|
+
if (!state.paths || typeof state.paths !== "object" || Array.isArray(state.paths)) return;
|
|
238
|
+
for (const path of Object.keys(state.paths)) {
|
|
239
|
+
const entry = state.paths[path];
|
|
240
|
+
if (!entry || typeof entry !== "object" || Array.isArray(entry)) return;
|
|
241
|
+
if (typeof entry.digest !== "string" || typeof entry.sourcePath !== "string") return;
|
|
242
|
+
if (entry.mountPath !== void 0 && !hasRuntimeType(entry.mountPath, "string")) return;
|
|
243
|
+
}
|
|
244
|
+
if (state.claims !== void 0) {
|
|
245
|
+
if (!state.claims || !hasRuntimeType(state.claims, "object") || Array.isArray(state.claims)) return;
|
|
246
|
+
for (const [path, claims] of Object.entries(state.claims)) {
|
|
247
|
+
if (!state.paths[path] || !Array.isArray(claims) || claims.length === 0) return;
|
|
248
|
+
const mounts = /* @__PURE__ */ new Set();
|
|
249
|
+
for (const claim of claims) {
|
|
250
|
+
if (!claim || !hasRuntimeType(claim, "object") || Array.isArray(claim)) return;
|
|
251
|
+
if (!hasRuntimeType(claim.digest, "string") || !hasRuntimeType(claim.sourcePath, "string") || !hasRuntimeType(claim.mountPath, "string")) return;
|
|
252
|
+
if (mounts.has(claim.mountPath)) return;
|
|
253
|
+
mounts.add(claim.mountPath);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return state;
|
|
258
|
+
}
|
|
259
|
+
/** Legacy records contain one claim per path; new records retain overlapping mounts. */
|
|
260
|
+
function sourceSyncPathClaims(state, path) {
|
|
261
|
+
return state.claims?.[path] ?? [{
|
|
262
|
+
...state.paths[path],
|
|
263
|
+
mountPath: state.paths[path].mountPath ?? state.mountPath
|
|
264
|
+
}];
|
|
265
|
+
}
|
|
266
|
+
function workspaceSourceSyncStateEquals(left, right) {
|
|
267
|
+
if (!left) return false;
|
|
268
|
+
return JSON.stringify(canonicalSourceSyncState(left)) === JSON.stringify(canonicalSourceSyncState(right));
|
|
269
|
+
}
|
|
270
|
+
function canonicalSourceSyncState(state) {
|
|
271
|
+
return {
|
|
272
|
+
configHash: state.configHash,
|
|
273
|
+
claims: state.claims,
|
|
274
|
+
mountPath: state.mountPath,
|
|
275
|
+
paths: Object.fromEntries(Object.entries(state.paths).sort(([left], [right]) => left.localeCompare(right))),
|
|
276
|
+
source: state.source
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
//#endregion
|
|
280
|
+
//#region src/sources/view.ts
|
|
281
|
+
function assertPublicFileMetadata(path, metadata) {
|
|
282
|
+
metadata = copyJsonFileMetadata(path, metadata);
|
|
283
|
+
if (metadata && Object.hasOwn(metadata, "source")) throw workspaceError(`[vitehub] Invalid Workspace metadata for ${path}. metadata.source is reserved for Source materialization.`);
|
|
284
|
+
return metadata;
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Proof that one Source view checked one normalized Workspace path for writes.
|
|
288
|
+
* Only `assertWritable` creates a grant. A grant is request-scoped. Do not persist it.
|
|
289
|
+
*/
|
|
290
|
+
var WorkspaceSourceWriteGrant = class {
|
|
291
|
+
#path;
|
|
292
|
+
constructor(path) {
|
|
293
|
+
this.#path = path;
|
|
294
|
+
Object.freeze(this);
|
|
295
|
+
}
|
|
296
|
+
get path() {
|
|
297
|
+
return this.#path;
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
const materializationByStore = /* @__PURE__ */ new WeakMap();
|
|
301
|
+
async function invalidateWorkspaceSourceMaterialization(definition, store, sourceKeys) {
|
|
302
|
+
const state = materializationByStore.get(store)?.get(definition);
|
|
303
|
+
if (!state) return;
|
|
304
|
+
const pending = /* @__PURE__ */ new Set();
|
|
305
|
+
for (const sourceKey of sourceKeys) {
|
|
306
|
+
state.completedSources.delete(sourceKey);
|
|
307
|
+
state.materializedSources.delete(sourceKey);
|
|
308
|
+
state.generationBySource.set(sourceKey, (state.generationBySource.get(sourceKey) ?? 0) + 1);
|
|
309
|
+
const operation = state.pendingBySource.get(sourceKey);
|
|
310
|
+
if (operation) pending.add(operation.tail);
|
|
311
|
+
}
|
|
312
|
+
await Promise.all(pending);
|
|
313
|
+
}
|
|
314
|
+
async function waitForMaterialization(pending, signal) {
|
|
315
|
+
if (!signal) return await pending;
|
|
316
|
+
signal.throwIfAborted();
|
|
317
|
+
let removeAbortListener = () => {};
|
|
318
|
+
const aborted = new Promise((_resolve, reject) => {
|
|
319
|
+
const onAbort = () => reject(signal.reason);
|
|
320
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
321
|
+
removeAbortListener = () => signal.removeEventListener("abort", onAbort);
|
|
322
|
+
if (signal.aborted) onAbort();
|
|
323
|
+
});
|
|
324
|
+
try {
|
|
325
|
+
return await Promise.race([pending, aborted]);
|
|
326
|
+
} finally {
|
|
327
|
+
removeAbortListener();
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
function createWorkspaceSourceView(definition, store, options = {}) {
|
|
331
|
+
const sourceContext = createSourceContext(definition, void 0, store);
|
|
332
|
+
const allSources = normalizeWorkspaceSources(definition.sources);
|
|
333
|
+
const sources = allSources.filter((source) => !source.requestOnly && (source.materialize === "lazy" || source.materialize === "startup"));
|
|
334
|
+
const syncSources = allSources.filter((source) => source.sync);
|
|
335
|
+
const descriptorSources = allSources.filter((source) => source.requestDescriptor);
|
|
336
|
+
const writePolicy = createWorkspaceWritePolicy(definition);
|
|
337
|
+
const prepareBySource = /* @__PURE__ */ new Map();
|
|
338
|
+
const priorPathsBySource = /* @__PURE__ */ new Map();
|
|
339
|
+
let removedSourceReconciliation;
|
|
340
|
+
const sourceContexts = /* @__PURE__ */ new Map();
|
|
341
|
+
let materializationByDefinition = materializationByStore.get(store);
|
|
342
|
+
if (!materializationByDefinition) {
|
|
343
|
+
materializationByDefinition = /* @__PURE__ */ new WeakMap();
|
|
344
|
+
materializationByStore.set(store, materializationByDefinition);
|
|
345
|
+
}
|
|
346
|
+
const materializationState = materializationByDefinition.get(definition) ?? {
|
|
347
|
+
completedSources: /* @__PURE__ */ new Set(),
|
|
348
|
+
generationBySource: /* @__PURE__ */ new Map(),
|
|
349
|
+
materializedSources: /* @__PURE__ */ new Set(),
|
|
350
|
+
pendingBySource: /* @__PURE__ */ new Map()
|
|
351
|
+
};
|
|
352
|
+
if (!materializationByDefinition.has(definition)) materializationByDefinition.set(definition, materializationState);
|
|
353
|
+
const { completedSources, generationBySource, materializedSources, pendingBySource } = materializationState;
|
|
354
|
+
const reusedStartupSources = /* @__PURE__ */ new Set();
|
|
355
|
+
const uncachedMaterializedSources = /* @__PURE__ */ new Set();
|
|
356
|
+
function getSourceContext(source) {
|
|
357
|
+
let context = sourceContexts.get(source.key);
|
|
358
|
+
if (!context) {
|
|
359
|
+
context = createSourceContext(definition, source, store);
|
|
360
|
+
sourceContexts.set(source.key, context);
|
|
361
|
+
}
|
|
362
|
+
return context;
|
|
363
|
+
}
|
|
364
|
+
function isLazySourcePath(path) {
|
|
365
|
+
return sources.some((source) => sourceMountContainsPath(source, path));
|
|
366
|
+
}
|
|
367
|
+
function isSyncSourceMountPath(path) {
|
|
368
|
+
return syncSources.some((source) => source.mountPath && sourceMountContainsPath(source, path));
|
|
369
|
+
}
|
|
370
|
+
function descriptorSourceForPath(path) {
|
|
371
|
+
return descriptorSources.find((source) => workspaceSourceRequestDescriptorPath(source.key) === path);
|
|
372
|
+
}
|
|
373
|
+
function descriptorPathEntries(path = "", options = {}) {
|
|
374
|
+
if (!descriptorSources.length) return [];
|
|
375
|
+
const normalized = normalizeWorkspacePath(path);
|
|
376
|
+
const descriptors = descriptorSources.map((source) => workspaceSourceRequestDescriptorPath(source.key));
|
|
377
|
+
if (!normalized) return options.recursive ? [
|
|
378
|
+
{
|
|
379
|
+
path: ".vitehub",
|
|
380
|
+
type: "directory"
|
|
381
|
+
},
|
|
382
|
+
{
|
|
383
|
+
path: ".vitehub/sources",
|
|
384
|
+
type: "directory"
|
|
385
|
+
},
|
|
386
|
+
...descriptors.map((descriptor) => ({
|
|
387
|
+
path: descriptor,
|
|
388
|
+
type: "file"
|
|
389
|
+
}))
|
|
390
|
+
] : [{
|
|
391
|
+
path: ".vitehub",
|
|
392
|
+
type: "directory"
|
|
393
|
+
}];
|
|
394
|
+
if (normalized === ".vitehub") return options.recursive ? [{
|
|
395
|
+
path: ".vitehub/sources",
|
|
396
|
+
type: "directory"
|
|
397
|
+
}, ...descriptors.map((descriptor) => ({
|
|
398
|
+
path: descriptor,
|
|
399
|
+
type: "file"
|
|
400
|
+
}))] : [{
|
|
401
|
+
path: ".vitehub/sources",
|
|
402
|
+
type: "directory"
|
|
403
|
+
}];
|
|
404
|
+
if (normalized === ".vitehub/sources") return descriptors.map((descriptor) => ({
|
|
405
|
+
path: descriptor,
|
|
406
|
+
type: "file"
|
|
407
|
+
}));
|
|
408
|
+
if (descriptors.includes(normalized)) return [{
|
|
409
|
+
path: normalized,
|
|
410
|
+
type: "file"
|
|
411
|
+
}];
|
|
412
|
+
return [];
|
|
413
|
+
}
|
|
414
|
+
function descriptorStat(path) {
|
|
415
|
+
if (descriptorSourceForPath(path)) return {
|
|
416
|
+
path,
|
|
417
|
+
type: "file"
|
|
418
|
+
};
|
|
419
|
+
if (descriptorSources.length && (path === ".vitehub" || path === ".vitehub/sources")) return {
|
|
420
|
+
path,
|
|
421
|
+
type: "directory"
|
|
422
|
+
};
|
|
423
|
+
}
|
|
424
|
+
function isDescriptorPath(path) {
|
|
425
|
+
return descriptorSources.length > 0 && (path === ".vitehub" || path === ".vitehub/sources" || path.startsWith(".vitehub/sources/"));
|
|
426
|
+
}
|
|
427
|
+
function descriptorContent(source) {
|
|
428
|
+
return JSON.stringify({
|
|
429
|
+
...source.requestDescriptor,
|
|
430
|
+
sourceKey: source.key
|
|
431
|
+
}, null, 2);
|
|
432
|
+
}
|
|
433
|
+
function isLiveSource(sourceKey) {
|
|
434
|
+
const source = sources.find((item) => item.key === sourceKey);
|
|
435
|
+
return Boolean(source?.livePaths && source.materialize !== "startup");
|
|
436
|
+
}
|
|
437
|
+
function usesLiveProvider(source) {
|
|
438
|
+
return Boolean(source.livePaths && source.materialize !== "startup");
|
|
439
|
+
}
|
|
440
|
+
async function getLazySourcesForPath(path) {
|
|
441
|
+
if (!removedSourceReconciliation && sources.some((source) => !source.mountPath && source.materialize === "startup" && source.source.name === "file" && source.probeKeys?.length)) removedSourceReconciliation = reconcileRemovedStartupSources(definition.name, store, allSources.filter((source) => source.materialize === "startup")).catch((error) => {
|
|
442
|
+
removedSourceReconciliation = void 0;
|
|
443
|
+
throw error;
|
|
444
|
+
});
|
|
445
|
+
await removedSourceReconciliation;
|
|
446
|
+
const normalized = normalizeWorkspacePath(path);
|
|
447
|
+
const intersects = (filePath) => !normalized || normalized === filePath || filePath.startsWith(`${normalized}/`);
|
|
448
|
+
const matched = [];
|
|
449
|
+
for (const source of sources) {
|
|
450
|
+
if (source.mountPath || source.materialize !== "startup" || source.source.name !== "file" || !source.probeKeys?.length) {
|
|
451
|
+
if (sourceMountIntersectsPath(source, normalized)) matched.push(source);
|
|
452
|
+
continue;
|
|
453
|
+
}
|
|
454
|
+
if (source.probeKeys.some((key) => intersects(normalizeWorkspacePath(key)))) {
|
|
455
|
+
matched.push(source);
|
|
456
|
+
continue;
|
|
457
|
+
}
|
|
458
|
+
let priorPaths = priorPathsBySource.get(source.key);
|
|
459
|
+
if (!priorPaths) {
|
|
460
|
+
priorPaths = readSourceSnapshotPaths(store, definition.name, source.key).catch((error) => {
|
|
461
|
+
priorPathsBySource.delete(source.key);
|
|
462
|
+
throw error;
|
|
463
|
+
});
|
|
464
|
+
priorPathsBySource.set(source.key, priorPaths);
|
|
465
|
+
}
|
|
466
|
+
if ((await priorPaths).some(intersects)) matched.push(source);
|
|
467
|
+
}
|
|
468
|
+
return matched;
|
|
469
|
+
}
|
|
470
|
+
async function ensurePrepared(sourceKey) {
|
|
471
|
+
const source = sources.find((item) => item.key === sourceKey);
|
|
472
|
+
if (!source) return;
|
|
473
|
+
if (source.materialize === "startup" && completedSources.has(sourceKey)) {
|
|
474
|
+
if (await hasCurrentSourceSnapshot(store, definition.name, source, options.reuseStartupSnapshots, options.reuseStartupSnapshots ? sources : void 0)) return;
|
|
475
|
+
completedSources.delete(sourceKey);
|
|
476
|
+
}
|
|
477
|
+
if (source.materialize === "startup" && options.reuseStartupSnapshots && await hasCurrentSourceSnapshot(store, definition.name, source, true, sources)) {
|
|
478
|
+
reusedStartupSources.add(sourceKey);
|
|
479
|
+
return;
|
|
480
|
+
}
|
|
481
|
+
let pending = prepareBySource.get(sourceKey);
|
|
482
|
+
if (!pending) {
|
|
483
|
+
pending = prepareWorkspaceSource(source.source, getSourceContext(source)).then(() => void 0);
|
|
484
|
+
prepareBySource.set(sourceKey, pending);
|
|
485
|
+
}
|
|
486
|
+
await pending;
|
|
487
|
+
}
|
|
488
|
+
async function ensurePreparedSources(items = sources) {
|
|
489
|
+
await Promise.all(items.map(async (source) => await ensurePrepared(source.key)));
|
|
490
|
+
}
|
|
491
|
+
async function materializeSerialized(options = {}) {
|
|
492
|
+
const requestedPath = normalizeWorkspacePath(options.path || "");
|
|
493
|
+
const selectedSources = allSources.filter((source) => source.materialize === "lazy" || source.materialize === "startup" || source.materialize === "build" && Boolean(requestedPath)).filter((source) => (options.sources === void 0 || options.sources.includes(source.key)) && sourceMountIntersectsPath(source, requestedPath));
|
|
494
|
+
const predecessors = selectedSources.flatMap((source) => {
|
|
495
|
+
const pending = pendingBySource.get(source.key);
|
|
496
|
+
return pending ? [pending.tail] : [];
|
|
497
|
+
});
|
|
498
|
+
const previous = waitForMaterialization(Promise.all(predecessors), options.abortSignal);
|
|
499
|
+
const current = (async () => {
|
|
500
|
+
await previous;
|
|
501
|
+
const generations = new Map(selectedSources.map((source) => {
|
|
502
|
+
const generation = (generationBySource.get(source.key) ?? 0) + 1;
|
|
503
|
+
generationBySource.set(source.key, generation);
|
|
504
|
+
return [source.key, generation];
|
|
505
|
+
}));
|
|
506
|
+
const mutations = /* @__PURE__ */ new Set();
|
|
507
|
+
const isCurrent = () => selectedSources.every((source) => generationBySource.get(source.key) === generations.get(source.key));
|
|
508
|
+
async function trackMutation(operation) {
|
|
509
|
+
if (!isCurrent()) throw options.abortSignal?.reason ?? workspaceError("[vitehub] Workspace source materialization was superseded.");
|
|
510
|
+
const mutation = operation();
|
|
511
|
+
mutations.add(mutation);
|
|
512
|
+
try {
|
|
513
|
+
return await mutation;
|
|
514
|
+
} finally {
|
|
515
|
+
mutations.delete(mutation);
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
for (const source of selectedSources) {
|
|
519
|
+
if (!materializesCompleteSource(source, options)) continue;
|
|
520
|
+
materializedSources.delete(source.key);
|
|
521
|
+
if (source.materialize === "startup") {
|
|
522
|
+
completedSources.delete(source.key);
|
|
523
|
+
reusedStartupSources.delete(source.key);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
let result;
|
|
527
|
+
try {
|
|
528
|
+
result = await waitForMaterialization(materializeWorkspaceSources(definition, store, options, {
|
|
529
|
+
isCurrent,
|
|
530
|
+
async mutate(operation) {
|
|
531
|
+
options.abortSignal?.throwIfAborted();
|
|
532
|
+
return await trackMutation(operation);
|
|
533
|
+
},
|
|
534
|
+
async checkpoint(operation) {
|
|
535
|
+
return await trackMutation(operation);
|
|
536
|
+
}
|
|
537
|
+
}), options.abortSignal);
|
|
538
|
+
} catch (error) {
|
|
539
|
+
if (options.abortSignal?.aborted && mutations.size) await Promise.allSettled(mutations);
|
|
540
|
+
throw error;
|
|
541
|
+
}
|
|
542
|
+
for (const sourceResult of result.sources) {
|
|
543
|
+
const source = sources.find((item) => item.key === sourceResult.source);
|
|
544
|
+
if (sourceResult.status === "ready" && source && materializesCompleteSource(source, options)) {
|
|
545
|
+
materializedSources.add(source.key);
|
|
546
|
+
if (source.materialize === "lazy" && source.cache === false) uncachedMaterializedSources.add(source.key);
|
|
547
|
+
if (source.materialize === "startup") completedSources.add(source.key);
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
return result;
|
|
551
|
+
})();
|
|
552
|
+
const tail = current.then(() => void 0, async () => {
|
|
553
|
+
await Promise.allSettled(predecessors);
|
|
554
|
+
});
|
|
555
|
+
const entries = new Map(selectedSources.map((source) => [source.key, {
|
|
556
|
+
fullSource: materializesCompleteSource(source, options),
|
|
557
|
+
promise: current,
|
|
558
|
+
tail
|
|
559
|
+
}]));
|
|
560
|
+
for (const [sourceKey, entry] of entries) pendingBySource.set(sourceKey, entry);
|
|
561
|
+
tail.then(() => {
|
|
562
|
+
for (const [sourceKey, entry] of entries) if (pendingBySource.get(sourceKey) === entry) pendingBySource.delete(sourceKey);
|
|
563
|
+
});
|
|
564
|
+
return await current;
|
|
565
|
+
}
|
|
566
|
+
async function ensureMaterialized(sourceKey, verifyRootOwnership = true) {
|
|
567
|
+
const source = sources.find((item) => item.key === sourceKey);
|
|
568
|
+
if (!source) return;
|
|
569
|
+
const verifyOwnership = options.reuseStartupSnapshots ? true : verifyRootOwnership || source.mountPath || !sources.some((item) => !item.mountPath && item.materialize === "lazy") ? "workspace" : false;
|
|
570
|
+
const isUncachedLazySource = source.materialize === "lazy" && source.cache === false;
|
|
571
|
+
const pending = pendingBySource.get(sourceKey);
|
|
572
|
+
if (pending?.fullSource) {
|
|
573
|
+
try {
|
|
574
|
+
await pending.promise;
|
|
575
|
+
} catch {}
|
|
576
|
+
if (materializedSources.has(sourceKey) || completedSources.has(sourceKey)) {
|
|
577
|
+
if (isUncachedLazySource) uncachedMaterializedSources.add(sourceKey);
|
|
578
|
+
return;
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
if (isUncachedLazySource ? uncachedMaterializedSources.has(sourceKey) : materializedSources.has(sourceKey)) {
|
|
582
|
+
const maxAge = source.cache && source.cache.maxAge;
|
|
583
|
+
if (source.materialize === "startup" ? await hasCurrentSourceSnapshot(store, definition.name, source, verifyOwnership, options.reuseStartupSnapshots ? sources : void 0) : !Number.isFinite(maxAge) || await hasFreshSourceSnapshot(store, definition.name, source)) return;
|
|
584
|
+
materializedSources.delete(sourceKey);
|
|
585
|
+
}
|
|
586
|
+
if (completedSources.has(sourceKey) || reusedStartupSources.has(sourceKey)) {
|
|
587
|
+
if (await hasCurrentSourceSnapshot(store, definition.name, source, verifyOwnership, options.reuseStartupSnapshots ? sources : void 0)) return;
|
|
588
|
+
completedSources.delete(sourceKey);
|
|
589
|
+
reusedStartupSources.delete(sourceKey);
|
|
590
|
+
}
|
|
591
|
+
const recovery = pendingBySource.get(sourceKey);
|
|
592
|
+
if (recovery && recovery !== pending) return await ensureMaterialized(sourceKey, verifyRootOwnership);
|
|
593
|
+
await materializeSerialized({ sources: [sourceKey] });
|
|
594
|
+
}
|
|
595
|
+
async function ensureMaterializedSources(items = sources) {
|
|
596
|
+
await Promise.all(items.map(async (source) => await ensureMaterialized(source.key)));
|
|
597
|
+
}
|
|
598
|
+
async function listSourceAware(path = "", options = {}) {
|
|
599
|
+
const normalized = normalizeWorkspacePath(path);
|
|
600
|
+
if (!isDescriptorPath(normalized)) for (const source of await getLazySourcesForPath(normalized)) {
|
|
601
|
+
if (source.materialize !== "startup" || isExcludedWorkspacePath(source.mountPath, options.exclude)) continue;
|
|
602
|
+
await ensurePrepared(source.key);
|
|
603
|
+
if (!usesLiveProvider(source)) await ensureMaterialized(source.key);
|
|
604
|
+
}
|
|
605
|
+
const storeEntries = isDescriptorPath(normalized) ? [] : await store.list(path, options);
|
|
606
|
+
const result = new Map(storeEntries.map((entry) => [entry.path, entry]));
|
|
607
|
+
for (const entry of descriptorPathEntries(path, options)) if (!isExcludedWorkspacePath(entry.path, options.exclude)) result.set(entry.path, entry);
|
|
608
|
+
if (isDescriptorPath(normalized)) return [...result.values()].sort((left, right) => left.path.localeCompare(right.path));
|
|
609
|
+
for (const source of await getLazySourcesForPath(path)) {
|
|
610
|
+
if (isExcludedWorkspacePath(source.mountPath, options.exclude)) continue;
|
|
611
|
+
await ensurePrepared(source.key);
|
|
612
|
+
if (usesLiveProvider(source)) {
|
|
613
|
+
await pruneLiveSourceStoreEntries(result, source);
|
|
614
|
+
continue;
|
|
615
|
+
}
|
|
616
|
+
if (!normalized && options.recursive && source.materialize !== "startup" && !await hasCurrentSourceSnapshot(store, definition.name, source)) {
|
|
617
|
+
if ([...result.keys()].some((key) => sourceMountContainsPath(source, key))) {
|
|
618
|
+
const allowed = await currentSourceTreePaths(source, getSourceContext(source));
|
|
619
|
+
for (const key of result.keys()) if (sourceMountContainsPath(source, key) && !allowed.has(key)) result.delete(key);
|
|
620
|
+
result.set(source.mountPath, {
|
|
621
|
+
path: source.mountPath,
|
|
622
|
+
type: "directory"
|
|
623
|
+
});
|
|
624
|
+
continue;
|
|
625
|
+
}
|
|
626
|
+
for (const key of result.keys()) if (sourceMountContainsPath(source, key)) result.delete(key);
|
|
627
|
+
result.set(source.mountPath, {
|
|
628
|
+
path: source.mountPath,
|
|
629
|
+
type: "directory"
|
|
630
|
+
});
|
|
631
|
+
continue;
|
|
632
|
+
}
|
|
633
|
+
if (source.materialize === "startup") continue;
|
|
634
|
+
else if (sourceMountContainsPath(source, path) || !source.mountPath && normalized) {
|
|
635
|
+
await ensureMaterialized(source.key);
|
|
636
|
+
for (const entry of await store.list(path, options)) result.set(entry.path, entry);
|
|
637
|
+
} else if (!normalized && !result.has(source.mountPath)) result.set(source.mountPath, {
|
|
638
|
+
path: source.mountPath,
|
|
639
|
+
type: "directory"
|
|
640
|
+
});
|
|
641
|
+
}
|
|
642
|
+
addLiveSourceEntries(result, path, options, sources);
|
|
643
|
+
return [...result.values()].sort((left, right) => left.path.localeCompare(right.path));
|
|
644
|
+
}
|
|
645
|
+
async function searchWorkspace(query) {
|
|
646
|
+
const limit = query.limit ?? 100;
|
|
647
|
+
const storePaths = [];
|
|
648
|
+
const sourcePaths = /* @__PURE__ */ new Map();
|
|
649
|
+
const requestedPaths = query.paths?.length ? query.paths : [query.cwd || ""];
|
|
650
|
+
for (const requestedPath of requestedPaths) {
|
|
651
|
+
const resolution = resolveWorkspacePath(definition, normalizeWorkspacePath(requestedPath));
|
|
652
|
+
if (resolution.type === "store") {
|
|
653
|
+
storePaths.push(resolution.workspacePath);
|
|
654
|
+
continue;
|
|
655
|
+
}
|
|
656
|
+
const list = sourcePaths.get(resolution.sourceKey) || [];
|
|
657
|
+
list.push(resolution.workspacePath);
|
|
658
|
+
sourcePaths.set(resolution.sourceKey, list);
|
|
659
|
+
}
|
|
660
|
+
for (const source of sources) {
|
|
661
|
+
if (source.materialize !== "startup" || !requestedPaths.some((path) => sourceMountIntersectsPath(source, normalizeWorkspacePath(path)))) continue;
|
|
662
|
+
await ensurePrepared(source.key);
|
|
663
|
+
await ensureMaterialized(source.key);
|
|
664
|
+
}
|
|
665
|
+
const results = await searchMaterializedStore(store, {
|
|
666
|
+
...query,
|
|
667
|
+
paths: storePaths.filter(Boolean).length ? storePaths.filter(Boolean) : void 0,
|
|
668
|
+
limit
|
|
669
|
+
});
|
|
670
|
+
if (query.paths?.length && !storePaths.filter(Boolean).length) results.length = 0;
|
|
671
|
+
for (const source of descriptorSources) {
|
|
672
|
+
const entryPath = workspaceSourceRequestDescriptorPath(source.key);
|
|
673
|
+
if (query.paths?.length && !requestedPaths.some((path) => !normalizeWorkspacePath(path) || sourceMountContainsPath({ mountPath: normalizeWorkspacePath(path) }, entryPath))) continue;
|
|
674
|
+
results.push(...searchText(entryPath, descriptorContent(source), {
|
|
675
|
+
...query,
|
|
676
|
+
limit: limit - results.length
|
|
677
|
+
}));
|
|
678
|
+
if (results.length >= limit) return dedupeHits(results).slice(0, limit);
|
|
679
|
+
}
|
|
680
|
+
for (const source of sources) {
|
|
681
|
+
const paths = sourcePaths.get(source.key);
|
|
682
|
+
if (query.paths?.length && !paths?.length && !query.paths.some((path) => !normalizeWorkspacePath(path))) continue;
|
|
683
|
+
if (usesLiveProvider(source)) {
|
|
684
|
+
await ensurePrepared(source.key);
|
|
685
|
+
const liveEntries = liveSourceEntries(source).filter((entry) => entry.type === "file").filter((entry) => !paths?.length || paths.some((path) => !path || entry.path === path || entry.path.startsWith(`${path}/`)));
|
|
686
|
+
for (const entry of liveEntries) {
|
|
687
|
+
const sourcePath = source.livePaths[entry.path];
|
|
688
|
+
if (typeof sourcePath !== "string") continue;
|
|
689
|
+
const content = await sourceItemContent(await source.source.getItem(sourcePath, getSourceContext(source)));
|
|
690
|
+
const text = typeof content === "string" ? content : new TextDecoder().decode(content);
|
|
691
|
+
results.push(...searchText(entry.path, text, {
|
|
692
|
+
...query,
|
|
693
|
+
limit: limit - results.length
|
|
694
|
+
}));
|
|
695
|
+
if (results.length >= limit) break;
|
|
696
|
+
}
|
|
697
|
+
if (results.length >= limit) break;
|
|
698
|
+
continue;
|
|
699
|
+
}
|
|
700
|
+
await ensureMaterialized(source.key);
|
|
701
|
+
results.push(...await searchMaterializedStore(store, {
|
|
702
|
+
...query,
|
|
703
|
+
paths,
|
|
704
|
+
limit: limit - results.length
|
|
705
|
+
}));
|
|
706
|
+
if (results.length >= limit) break;
|
|
707
|
+
}
|
|
708
|
+
return dedupeHits(results).slice(0, limit);
|
|
709
|
+
}
|
|
710
|
+
function assertWritableStorePath(path, workspacePath, type) {
|
|
711
|
+
if (type === "source" || isLazySourcePath(workspacePath)) throw workspaceError(`[vitehub] Source-backed workspace paths are read-only: ${path}.`);
|
|
712
|
+
}
|
|
713
|
+
async function previousStat(path) {
|
|
714
|
+
return await store.stat(path);
|
|
715
|
+
}
|
|
716
|
+
async function materializeRootStartupSources(verifyRootOwnership = true) {
|
|
717
|
+
for (const source of sources.filter((source) => !source.mountPath && source.materialize === "startup")) {
|
|
718
|
+
await ensurePrepared(source.key);
|
|
719
|
+
await ensureMaterialized(source.key, verifyRootOwnership);
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
async function readOwnedRootStartupFile(path, sourceKey) {
|
|
723
|
+
if (!sources.some((source) => !source.mountPath && source.materialize === "startup")) return;
|
|
724
|
+
const owner = await readWorkspaceFileOwner(store, path);
|
|
725
|
+
if (owner?.workspace !== definition.name || !owner.digest) return;
|
|
726
|
+
if (sourceKey && owner.source !== sourceKey) return;
|
|
727
|
+
const source = sources.find((source) => !source.mountPath && source.materialize === "startup" && source.key === owner.source);
|
|
728
|
+
if (!source) return;
|
|
729
|
+
if ((await store.stat(path))?.type !== "file") return;
|
|
730
|
+
const file = await store.readFile(path);
|
|
731
|
+
if (!file) return;
|
|
732
|
+
if (file.metadata?.source && (file.metadata.workspaceSourceOwner !== definition.name || file.metadata.source !== owner.source)) return;
|
|
733
|
+
const snapshot = await readCurrentSourceSnapshot(store, definition.name, source);
|
|
734
|
+
if (snapshot?.status !== "ready" || !Object.hasOwn(snapshot.items || {}, path)) return;
|
|
735
|
+
if (await sha256(file.content) === owner.digest) return file;
|
|
736
|
+
}
|
|
737
|
+
async function materializeRootSourceForPath(path, verifyRootOwnership = true) {
|
|
738
|
+
for (const source of sources.filter((source) => !source.mountPath)) {
|
|
739
|
+
await ensurePrepared(source.key);
|
|
740
|
+
await ensureMaterialized(source.key, verifyRootOwnership);
|
|
741
|
+
if (source.materialize === "startup") {
|
|
742
|
+
if (await readOwnedRootStartupFile(path, source.key)) return source;
|
|
743
|
+
} else if ((await store.stat(path))?.metadata?.source === source.key) return source;
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
async function isSourceBackedStorePath(path) {
|
|
747
|
+
if (!allSources.length) return false;
|
|
748
|
+
for (const source of allSources) {
|
|
749
|
+
const snapshot = await readCurrentSourceSnapshot(store, definition.name, source);
|
|
750
|
+
if (Object.keys(snapshot?.items || {}).some((item) => item === path || !path || item.startsWith(`${path}/`))) return true;
|
|
751
|
+
}
|
|
752
|
+
const file = await store.readFile(path);
|
|
753
|
+
if (typeof file?.metadata?.source === "string" && allSources.some((source) => source.key === file.metadata?.source)) return true;
|
|
754
|
+
if ((await store.stat(path))?.type !== "directory") return false;
|
|
755
|
+
const entries = await store.list(path, { recursive: true });
|
|
756
|
+
for (const entry of entries) {
|
|
757
|
+
if (entry.type !== "file") continue;
|
|
758
|
+
const child = await store.readFile(entry.path);
|
|
759
|
+
if (typeof child?.metadata?.source === "string" && allSources.some((source) => source.key === child.metadata?.source)) return true;
|
|
760
|
+
}
|
|
761
|
+
return false;
|
|
762
|
+
}
|
|
763
|
+
async function isSyncedStatePath(path) {
|
|
764
|
+
if (!store.getMeta) return false;
|
|
765
|
+
for (const source of syncSources) {
|
|
766
|
+
const state = readWorkspaceSourceSyncState(await store.getMeta(sourceSyncMetaKey(source.key, definition.name)));
|
|
767
|
+
if (!state) continue;
|
|
768
|
+
if (state.paths[path]) return true;
|
|
769
|
+
if (Object.keys(state.paths).some((item) => item.startsWith(`${path}/`))) return true;
|
|
770
|
+
}
|
|
771
|
+
return false;
|
|
772
|
+
}
|
|
773
|
+
async function assertWritableCurrentPath(path) {
|
|
774
|
+
if (isDescriptorPath(path)) throw workspaceError(`[vitehub] Source-backed workspace paths are read-only: ${path}.`);
|
|
775
|
+
const resolution = resolveWorkspacePath(definition, path);
|
|
776
|
+
if (resolution.type === "source" || isLazySourcePath(resolution.workspacePath) || isSyncSourceMountPath(resolution.workspacePath) || await isSyncedStatePath(resolution.workspacePath) || await isSourceBackedStorePath(resolution.workspacePath)) throw workspaceError(`[vitehub] Source-backed workspace paths are read-only: ${path}.`);
|
|
777
|
+
}
|
|
778
|
+
async function assertWritableResolvedStorePath(path, workspacePath, type) {
|
|
779
|
+
assertWritableStorePath(path, workspacePath, type);
|
|
780
|
+
await materializeRootSourceForPath(workspacePath);
|
|
781
|
+
if (isSyncSourceMountPath(workspacePath) || await isSyncedStatePath(workspacePath) || await isSourceBackedStorePath(workspacePath)) throw workspaceError(`[vitehub] Source-backed workspace paths are read-only: ${path}.`);
|
|
782
|
+
}
|
|
783
|
+
async function assertWritablePath(path) {
|
|
784
|
+
if (isDescriptorPath(normalizeWorkspacePath(path))) throw workspaceError(`[vitehub] Source-backed workspace paths are read-only: ${path}.`);
|
|
785
|
+
const resolution = resolveWorkspacePath(definition, path);
|
|
786
|
+
if (isDescriptorPath(resolution.workspacePath) || allSources.some((source) => (source.materialize === "lazy" || source.materialize === "startup") && sourceMountContainsPath(source, resolution.workspacePath))) throw workspaceError(`[vitehub] Source-backed workspace paths are read-only: ${path}.`);
|
|
787
|
+
await assertWritableResolvedStorePath(path, resolution.workspacePath, resolution.type);
|
|
788
|
+
return resolution;
|
|
789
|
+
}
|
|
790
|
+
const writeGrants = /* @__PURE__ */ new WeakMap();
|
|
791
|
+
async function grantWritablePath(path) {
|
|
792
|
+
const { workspacePath } = await assertWritablePath(path);
|
|
793
|
+
const grant = new WorkspaceSourceWriteGrant(workspacePath);
|
|
794
|
+
writeGrants.set(grant, workspacePath);
|
|
795
|
+
return grant;
|
|
796
|
+
}
|
|
797
|
+
async function grantPolicyPath(path) {
|
|
798
|
+
return await grantWritablePath(path);
|
|
799
|
+
}
|
|
800
|
+
function requireWriteGrant(write) {
|
|
801
|
+
return async (grant, path, ...args) => {
|
|
802
|
+
const workspacePath = normalizeWorkspacePath(path);
|
|
803
|
+
if (writeGrants.get(grant) !== workspacePath) throw workspaceError(`[vitehub] Workspace write to ${path} requires a Source write grant for that path.`);
|
|
804
|
+
return await withWorkspaceStoreMutation(store, async () => {
|
|
805
|
+
await assertWritableCurrentPath(workspacePath);
|
|
806
|
+
return await write(workspacePath, ...args);
|
|
807
|
+
});
|
|
808
|
+
};
|
|
809
|
+
}
|
|
810
|
+
function requireRebaseGrants(rebase) {
|
|
811
|
+
return async (grants, options) => {
|
|
812
|
+
const takeRemote = options?.takeRemote ?? [];
|
|
813
|
+
if (!Array.isArray(grants) || grants.length !== takeRemote.length) throw workspaceError("[vitehub] Workspace rebase requires one Source write grant for each takeRemote path.");
|
|
814
|
+
for (const [index, path] of takeRemote.entries()) {
|
|
815
|
+
const grant = grants[index];
|
|
816
|
+
const normalizedPath = normalizeWorkspacePath(path);
|
|
817
|
+
if (!grant || writeGrants.get(grant) !== normalizedPath) throw workspaceError(`[vitehub] Workspace rebase to take remote ${path} requires a Source write grant for that path.`);
|
|
818
|
+
if (allSources.some((source) => (source.materialize === "lazy" || source.materialize === "startup") && sourceMountIntersectsPath(source, normalizedPath))) throw workspaceError(`[vitehub] Source-backed workspace paths are read-only: ${path}.`);
|
|
819
|
+
}
|
|
820
|
+
await withWorkspaceStoreMutation(store, async () => {
|
|
821
|
+
for (const grant of grants) await assertWritableCurrentPath(grant.path);
|
|
822
|
+
await rebase(options?.takeRemote ? {
|
|
823
|
+
...options,
|
|
824
|
+
takeRemote: grants.map((grant) => grant.path)
|
|
825
|
+
} : options);
|
|
826
|
+
});
|
|
827
|
+
};
|
|
828
|
+
}
|
|
829
|
+
const grantedStore = {
|
|
830
|
+
mkdir: requireWriteGrant(async (path, options) => await store.mkdir(path, options)),
|
|
831
|
+
rm: requireWriteGrant(async (path, options) => await store.rm(path, options)),
|
|
832
|
+
writeFile: requireWriteGrant(async (path, file, ifDigest) => {
|
|
833
|
+
if (ifDigest === void 0) return await store.writeFile(path, {
|
|
834
|
+
...file,
|
|
835
|
+
path
|
|
836
|
+
});
|
|
837
|
+
if (!store.writeFileConditional) throw workspaceError("[vitehub] This Workspace Store does not support conditional writes.");
|
|
838
|
+
await store.writeFileConditional(path, {
|
|
839
|
+
...file,
|
|
840
|
+
path
|
|
841
|
+
}, ifDigest);
|
|
842
|
+
})
|
|
843
|
+
};
|
|
844
|
+
return {
|
|
845
|
+
requireHistoryGrants(history, apply = (options) => history.commit(options)) {
|
|
846
|
+
return async (grants, options) => {
|
|
847
|
+
return await withWorkspaceStoreMutation(store, async () => {
|
|
848
|
+
const ref = normalizeHistoryRef(options.ref);
|
|
849
|
+
const head = await history.head({ ref });
|
|
850
|
+
if ((head?.id ?? null) !== options.ifHead) throw historyConflict(ref, options.ifHead, head?.id ?? null);
|
|
851
|
+
const forkRef = head || options.startFrom ? void 0 : await history.forksFrom?.(ref);
|
|
852
|
+
const startId = head?.id ?? options.startFrom ?? (forkRef ? (await history.head({ ref: forkRef }))?.id : void 0);
|
|
853
|
+
const previous = startId ? await history.open(startId) : void 0;
|
|
854
|
+
const entries = /* @__PURE__ */ new Map();
|
|
855
|
+
for (const entry of previous ? await previous.list("", { recursive: true }) : []) if (entry.type === "file") entries.set(entry.path, entry);
|
|
856
|
+
const historyDigest = async (content) => history.digest ? await history.digest(contentToBytes(content)) : await sha256(content);
|
|
857
|
+
const digestOf = async (path, entry) => entry.digest ?? await historyDigest(await previous.readFile(path, { encoding: "binary" }));
|
|
858
|
+
const sameFile = async (file, digest, entry, compute = historyDigest) => digest === await compute(file.content) && (file.mediaType === void 0 || entry.mediaType === file.mediaType) && (file.metadata === void 0 || JSON.stringify(entry.metadata) === JSON.stringify(file.metadata));
|
|
859
|
+
const next = (path) => {
|
|
860
|
+
if (options.files) return options.files[path];
|
|
861
|
+
if (options.changes.put && Object.hasOwn(options.changes.put, path)) return options.changes.put[path];
|
|
862
|
+
return options.changes.delete?.includes(path) ? void 0 : entries.get(path);
|
|
863
|
+
};
|
|
864
|
+
const candidates = options.files ? /* @__PURE__ */ new Set([...Object.keys(options.files), ...entries.keys()]) : /* @__PURE__ */ new Set([...Object.keys(options.changes.put ?? {}), ...options.changes.delete ?? []]);
|
|
865
|
+
const paths = [];
|
|
866
|
+
for (const path of candidates) {
|
|
867
|
+
const file = next(path);
|
|
868
|
+
const before = entries.get(path);
|
|
869
|
+
if (!file || !before || !("content" in file) || !await sameFile(file, await digestOf(path, before), before)) paths.push(path);
|
|
870
|
+
}
|
|
871
|
+
const authorized = new Set(grants.map((grant) => writeGrants.get(grant)));
|
|
872
|
+
if (authorized.has(void 0) || paths.some((path) => !authorized.has(path))) throw workspaceError("[vitehub] History publication requires a Source write grant for every affected path.");
|
|
873
|
+
for (const path of paths) await assertWritableCurrentPath(path);
|
|
874
|
+
if (history === store.history && ref === "main" && allSources.length) for (const entry of await store.list("", { recursive: true })) {
|
|
875
|
+
if (entry.type !== "file" || !allSources.some((source) => source.key === entry.metadata?.source)) continue;
|
|
876
|
+
const file = next(entry.path);
|
|
877
|
+
const digest = entry.digest ?? await sha256((await store.readFile(entry.path)).content);
|
|
878
|
+
if (!(file && ("content" in file ? await sameFile(file, digest, entry, sha256) : await digestOf(entry.path, file) === digest && file.mediaType === entry.mediaType && JSON.stringify(file.metadata) === JSON.stringify(entry.metadata)))) await assertWritableCurrentPath(entry.path);
|
|
879
|
+
}
|
|
880
|
+
return await apply({
|
|
881
|
+
...options,
|
|
882
|
+
ref,
|
|
883
|
+
startFrom: head ? options.startFrom : startId
|
|
884
|
+
});
|
|
885
|
+
});
|
|
886
|
+
};
|
|
887
|
+
},
|
|
888
|
+
async assertWritable(path) {
|
|
889
|
+
return await grantWritablePath(path);
|
|
890
|
+
},
|
|
891
|
+
assertWritableCurrentPath,
|
|
892
|
+
requireWriteGrant,
|
|
893
|
+
requireRebaseGrants,
|
|
894
|
+
async readFile(path, options) {
|
|
895
|
+
const descriptorSource = descriptorSourceForPath(normalizeWorkspacePath(path));
|
|
896
|
+
if (descriptorSource) return decodeFile(descriptorContent(descriptorSource), options);
|
|
897
|
+
const resolution = resolveWorkspacePath(definition, path);
|
|
898
|
+
if (resolution.type === "source") {
|
|
899
|
+
await ensurePrepared(resolution.sourceKey);
|
|
900
|
+
if (isLiveSource(resolution.sourceKey)) {
|
|
901
|
+
const item = await resolution.source.source.getItem(resolution.sourcePath, getSourceContext(resolution.source));
|
|
902
|
+
return decodeFile(await sourceItemContent(item), options);
|
|
903
|
+
}
|
|
904
|
+
if (resolution.source.materialize === "startup") await ensureMaterialized(resolution.sourceKey);
|
|
905
|
+
const cacheMaxAge = resolution.source.cache && resolution.source.cache.maxAge;
|
|
906
|
+
let shouldRefreshCachedLazy = false;
|
|
907
|
+
if (resolution.source.materialize === "lazy" && Number.isFinite(cacheMaxAge)) shouldRefreshCachedLazy = materializedSources.has(resolution.sourceKey) || Boolean(await readCurrentSourceSnapshot(store, definition.name, resolution.source));
|
|
908
|
+
if (shouldRefreshCachedLazy) await ensureMaterialized(resolution.sourceKey);
|
|
909
|
+
const file = await store.readFile(resolution.workspacePath);
|
|
910
|
+
if (file) return decodeFile(file.content, options);
|
|
911
|
+
await ensureMaterialized(resolution.sourceKey);
|
|
912
|
+
return await readResolvedSourceFile(resolution, store, sourceContext, options);
|
|
913
|
+
}
|
|
914
|
+
await materializeRootStartupSources(false);
|
|
915
|
+
const startupFile = await readOwnedRootStartupFile(resolution.workspacePath);
|
|
916
|
+
if (startupFile) return decodeFile(startupFile.content, options);
|
|
917
|
+
if (!await materializeRootSourceForPath(resolution.workspacePath, false)) for (const source of sources.filter((source) => !source.mountPath && source.materialize === "startup")) await ensureMaterialized(source.key, true);
|
|
918
|
+
const file = await store.readFile(resolution.workspacePath);
|
|
919
|
+
if (!file) throw workspaceError(`[vitehub] Workspace file does not exist: ${path}.`);
|
|
920
|
+
return decodeFile(file.content, options);
|
|
921
|
+
},
|
|
922
|
+
async writeFile(path, content, options) {
|
|
923
|
+
const metadata = assertPublicFileMetadata(path, options?.metadata);
|
|
924
|
+
const requested = await grantWritablePath(path);
|
|
925
|
+
const input = await writePolicy.before({
|
|
926
|
+
content,
|
|
927
|
+
mediaType: options?.mediaType,
|
|
928
|
+
metadata,
|
|
929
|
+
operation: "writeFile",
|
|
930
|
+
path: requested.path,
|
|
931
|
+
previous: await previousStat(requested.path),
|
|
932
|
+
workspace: definition.name
|
|
933
|
+
});
|
|
934
|
+
try {
|
|
935
|
+
if (options?.preservePath && input.path !== requested.path) throw workspaceError(`[vitehub] Workspace validator cannot rewrite preserved path: ${requested.path} -> ${input.path}.`);
|
|
936
|
+
const grant = await grantPolicyPath(input.path);
|
|
937
|
+
await grantedStore.writeFile(grant, input.path, {
|
|
938
|
+
content: input.content ?? content,
|
|
939
|
+
mediaType: input.mediaType,
|
|
940
|
+
metadata: assertPublicFileMetadata(input.path, input.metadata)
|
|
941
|
+
}, options?.ifDigest);
|
|
942
|
+
await writePolicy.after(input);
|
|
943
|
+
return grant.path;
|
|
944
|
+
} catch (error) {
|
|
945
|
+
await writePolicy.error(input, error);
|
|
946
|
+
throw error;
|
|
947
|
+
}
|
|
948
|
+
},
|
|
949
|
+
async list(path, options) {
|
|
950
|
+
return await listSourceAware(path || "", options || {});
|
|
951
|
+
},
|
|
952
|
+
async glob(pattern, options) {
|
|
953
|
+
const patterns = Array.isArray(pattern) ? pattern : [pattern];
|
|
954
|
+
const { cwd, matches } = createWorkspaceGlobMatcher(patterns, options);
|
|
955
|
+
await ensurePreparedSources();
|
|
956
|
+
await ensureMaterializedSources(sources.filter((source) => !usesLiveProvider(source)));
|
|
957
|
+
const result = /* @__PURE__ */ new Map();
|
|
958
|
+
if (cwd !== ".vitehub/sources") {
|
|
959
|
+
for (const entry of await store.glob(patterns, options)) if (matches(entry.path)) result.set(entry.path, entry);
|
|
960
|
+
}
|
|
961
|
+
for (const entry of descriptorPathEntries(cwd, { recursive: true })) if (entry.type === "file" && matches(entry.path)) result.set(entry.path, entry);
|
|
962
|
+
for (const source of sources.filter(usesLiveProvider)) {
|
|
963
|
+
await pruneLiveSourceStoreEntries(result, source);
|
|
964
|
+
for (const entry of liveSourceEntries(source)) if (entry.type === "file" && matches(entry.path)) result.set(entry.path, entry);
|
|
965
|
+
}
|
|
966
|
+
return [...result.values()].sort((left, right) => left.path.localeCompare(right.path));
|
|
967
|
+
},
|
|
968
|
+
async search(query) {
|
|
969
|
+
return await searchWorkspace(query);
|
|
970
|
+
},
|
|
971
|
+
async stat(path) {
|
|
972
|
+
const descriptor = descriptorStat(normalizeWorkspacePath(path));
|
|
973
|
+
if (descriptor) return descriptor;
|
|
974
|
+
const resolution = resolveWorkspacePath(definition, path);
|
|
975
|
+
if (resolution.type === "source") {
|
|
976
|
+
await ensurePrepared(resolution.sourceKey);
|
|
977
|
+
if (isLiveSource(resolution.sourceKey)) {
|
|
978
|
+
if (!resolution.workspacePath) return {
|
|
979
|
+
path: "",
|
|
980
|
+
type: "directory"
|
|
981
|
+
};
|
|
982
|
+
const result = liveSourceStat(resolution.source, resolution.workspacePath);
|
|
983
|
+
if (!result) throw workspaceError(`[vitehub] Workspace path does not exist: ${path}.`);
|
|
984
|
+
return result;
|
|
985
|
+
}
|
|
986
|
+
if (resolution.source.materialize === "startup") await ensureMaterialized(resolution.sourceKey);
|
|
987
|
+
const stored = await store.stat(resolution.workspacePath);
|
|
988
|
+
if (stored) return stored;
|
|
989
|
+
if (resolution.source.materialize === "startup" && (completedSources.has(resolution.sourceKey) || reusedStartupSources.has(resolution.sourceKey))) throw workspaceError(`[vitehub] Workspace path does not exist: ${path}.`);
|
|
990
|
+
await ensureMaterialized(resolution.sourceKey);
|
|
991
|
+
const result = await statVirtualSourcePath(resolution.source, resolution.workspacePath, store, getSourceContext(resolution.source));
|
|
992
|
+
if (!result) throw workspaceError(`[vitehub] Workspace path does not exist: ${path}.`);
|
|
993
|
+
return result;
|
|
994
|
+
}
|
|
995
|
+
await materializeRootStartupSources();
|
|
996
|
+
let result = await store.stat(resolution.workspacePath);
|
|
997
|
+
if (!result) {
|
|
998
|
+
if (!resolution.workspacePath && sources.some((source) => !source.mountPath && usesLiveProvider(source))) return {
|
|
999
|
+
path: "",
|
|
1000
|
+
type: "directory"
|
|
1001
|
+
};
|
|
1002
|
+
await materializeRootSourceForPath(resolution.workspacePath);
|
|
1003
|
+
result = await store.stat(resolution.workspacePath);
|
|
1004
|
+
}
|
|
1005
|
+
if (!result) throw workspaceError(`[vitehub] Workspace path does not exist: ${path}.`);
|
|
1006
|
+
return result;
|
|
1007
|
+
},
|
|
1008
|
+
async materializeSources(options) {
|
|
1009
|
+
return await materializeSerialized(options);
|
|
1010
|
+
},
|
|
1011
|
+
async exists(path) {
|
|
1012
|
+
if (descriptorStat(normalizeWorkspacePath(path))) return true;
|
|
1013
|
+
const resolution = resolveWorkspacePath(definition, path);
|
|
1014
|
+
if (resolution.type === "source") {
|
|
1015
|
+
await ensurePrepared(resolution.sourceKey);
|
|
1016
|
+
if (isLiveSource(resolution.sourceKey)) {
|
|
1017
|
+
if (!resolution.workspacePath) return true;
|
|
1018
|
+
return Boolean(liveSourceStat(resolution.source, resolution.workspacePath));
|
|
1019
|
+
}
|
|
1020
|
+
if (resolution.source.materialize === "startup") await ensureMaterialized(resolution.sourceKey);
|
|
1021
|
+
if (await store.stat(resolution.workspacePath)) return true;
|
|
1022
|
+
if (resolution.source.materialize === "startup" && (completedSources.has(resolution.sourceKey) || reusedStartupSources.has(resolution.sourceKey))) return false;
|
|
1023
|
+
await ensureMaterialized(resolution.sourceKey);
|
|
1024
|
+
return Boolean(await statVirtualSourcePath(resolution.source, resolution.workspacePath, store, getSourceContext(resolution.source)));
|
|
1025
|
+
}
|
|
1026
|
+
await materializeRootStartupSources();
|
|
1027
|
+
if (await store.stat(resolution.workspacePath)) return true;
|
|
1028
|
+
if (!resolution.workspacePath && sources.some((source) => !source.mountPath && usesLiveProvider(source))) return true;
|
|
1029
|
+
await materializeRootSourceForPath(resolution.workspacePath);
|
|
1030
|
+
return Boolean(await store.stat(resolution.workspacePath));
|
|
1031
|
+
},
|
|
1032
|
+
async mkdir(path, options) {
|
|
1033
|
+
const requested = await grantWritablePath(path);
|
|
1034
|
+
const input = await writePolicy.before({
|
|
1035
|
+
operation: "mkdir",
|
|
1036
|
+
path: requested.path,
|
|
1037
|
+
previous: await previousStat(requested.path),
|
|
1038
|
+
workspace: definition.name
|
|
1039
|
+
});
|
|
1040
|
+
try {
|
|
1041
|
+
await grantedStore.mkdir(await grantPolicyPath(input.path), input.path, options);
|
|
1042
|
+
await writePolicy.after(input);
|
|
1043
|
+
} catch (error) {
|
|
1044
|
+
await writePolicy.error(input, error);
|
|
1045
|
+
throw error;
|
|
1046
|
+
}
|
|
1047
|
+
},
|
|
1048
|
+
async rm(path, options) {
|
|
1049
|
+
const requested = await grantWritablePath(path);
|
|
1050
|
+
const input = await writePolicy.before({
|
|
1051
|
+
operation: "rm",
|
|
1052
|
+
path: requested.path,
|
|
1053
|
+
previous: await previousStat(requested.path),
|
|
1054
|
+
workspace: definition.name
|
|
1055
|
+
});
|
|
1056
|
+
try {
|
|
1057
|
+
await grantedStore.rm(await grantPolicyPath(input.path), input.path, options);
|
|
1058
|
+
await writePolicy.after(input);
|
|
1059
|
+
} catch (error) {
|
|
1060
|
+
await writePolicy.error(input, error);
|
|
1061
|
+
throw error;
|
|
1062
|
+
}
|
|
1063
|
+
}
|
|
1064
|
+
};
|
|
1065
|
+
async function pruneLiveSourceStoreEntries(result, source) {
|
|
1066
|
+
const allowed = new Set(liveSourceEntries(source).map((entry) => entry.path));
|
|
1067
|
+
for (const [path, entry] of result) {
|
|
1068
|
+
if (!isWithinLiveSourceMount(source, path) || allowed.has(path)) continue;
|
|
1069
|
+
if (entry.type === "file") {
|
|
1070
|
+
if ((await store.readFile(path))?.metadata?.source === source.key) result.delete(path);
|
|
1071
|
+
continue;
|
|
1072
|
+
}
|
|
1073
|
+
if (!source.mountPath) continue;
|
|
1074
|
+
if (![...allowed].some((allowedPath) => allowedPath.startsWith(`${path}/`))) result.delete(path);
|
|
1075
|
+
}
|
|
1076
|
+
}
|
|
1077
|
+
}
|
|
1078
|
+
function isWithinLiveSourceMount(source, path) {
|
|
1079
|
+
return source.mountPath ? sourceMountContainsPath(source, path) : Boolean(path);
|
|
1080
|
+
}
|
|
1081
|
+
function liveSourceEntries(source) {
|
|
1082
|
+
const paths = Object.keys(source.livePaths || {});
|
|
1083
|
+
const entries = /* @__PURE__ */ new Map();
|
|
1084
|
+
for (const path of paths) {
|
|
1085
|
+
const parts = path.split("/").filter(Boolean);
|
|
1086
|
+
for (let index = 1; index < parts.length; index++) {
|
|
1087
|
+
const directory = parts.slice(0, index).join("/");
|
|
1088
|
+
entries.set(directory, {
|
|
1089
|
+
path: directory,
|
|
1090
|
+
type: "directory"
|
|
1091
|
+
});
|
|
1092
|
+
}
|
|
1093
|
+
entries.set(path, {
|
|
1094
|
+
path,
|
|
1095
|
+
type: "file"
|
|
1096
|
+
});
|
|
1097
|
+
}
|
|
1098
|
+
return [...entries.values()];
|
|
1099
|
+
}
|
|
1100
|
+
async function sourceItemContent(item) {
|
|
1101
|
+
if (item.contentStream) {
|
|
1102
|
+
if (typeof item.content !== "undefined" || typeof item.data !== "undefined") throw workspaceError("[vitehub] Workspace source items cannot define contentStream with content or data.");
|
|
1103
|
+
return await contentStreamToBytes(item.contentStream);
|
|
1104
|
+
}
|
|
1105
|
+
return item.content ?? (typeof item.data === "undefined" ? "" : JSON.stringify(item.data, null, 2));
|
|
1106
|
+
}
|
|
1107
|
+
function addLiveSourceEntries(result, path, options, sources) {
|
|
1108
|
+
const normalized = normalizeWorkspacePath(path);
|
|
1109
|
+
for (const source of sources.filter((source) => source.livePaths)) for (const entry of liveSourceEntries(source)) {
|
|
1110
|
+
if (!isListedLiveEntry(entry, normalized, options)) continue;
|
|
1111
|
+
if (isExcludedWorkspacePath(entry.path, options.exclude)) continue;
|
|
1112
|
+
result.set(entry.path, entry);
|
|
1113
|
+
}
|
|
1114
|
+
}
|
|
1115
|
+
function isListedLiveEntry(entry, path, options) {
|
|
1116
|
+
if (!path) {
|
|
1117
|
+
if (options.recursive) return true;
|
|
1118
|
+
return !entry.path.includes("/");
|
|
1119
|
+
}
|
|
1120
|
+
if (entry.path === path) return false;
|
|
1121
|
+
if (!entry.path.startsWith(`${path}/`)) return false;
|
|
1122
|
+
if (options.recursive) return true;
|
|
1123
|
+
return !entry.path.slice(path.length + 1).includes("/");
|
|
1124
|
+
}
|
|
1125
|
+
function liveSourceStat(source, workspacePath) {
|
|
1126
|
+
const paths = source.livePaths;
|
|
1127
|
+
if (!paths) return;
|
|
1128
|
+
if (Object.hasOwn(paths, workspacePath)) return {
|
|
1129
|
+
path: workspacePath,
|
|
1130
|
+
type: "file"
|
|
1131
|
+
};
|
|
1132
|
+
if (Object.keys(paths).some((path) => path.startsWith(`${workspacePath}/`))) return {
|
|
1133
|
+
path: workspacePath,
|
|
1134
|
+
type: "directory"
|
|
1135
|
+
};
|
|
1136
|
+
}
|
|
1137
|
+
async function currentSourceTreePaths(source, sourceContext) {
|
|
1138
|
+
const allowed = /* @__PURE__ */ new Set([source.mountPath]);
|
|
1139
|
+
const keys = await source.source.getKeys(sourceContext);
|
|
1140
|
+
for (const key of keys) {
|
|
1141
|
+
const parts = `${source.mountPath}/${key}`.replace(/\/+/g, "/").split("/");
|
|
1142
|
+
for (let index = 1; index <= parts.length; index++) allowed.add(parts.slice(0, index).join("/"));
|
|
1143
|
+
}
|
|
1144
|
+
return allowed;
|
|
1145
|
+
}
|
|
1146
|
+
function dedupeHits(hits) {
|
|
1147
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1148
|
+
return hits.filter((hit) => {
|
|
1149
|
+
const key = `${hit.path}:${hit.line}:${hit.column}:${hit.text}`;
|
|
1150
|
+
if (seen.has(key)) return false;
|
|
1151
|
+
seen.add(key);
|
|
1152
|
+
return true;
|
|
1153
|
+
});
|
|
1154
|
+
}
|
|
1155
|
+
//#endregion
|
|
1156
|
+
//#region src/storage/metadata-keys.ts
|
|
1157
|
+
const internalMetaKeyPrefixes = [
|
|
1158
|
+
"source:",
|
|
1159
|
+
"workspace:",
|
|
1160
|
+
"workspace-file-",
|
|
1161
|
+
"loader:"
|
|
1162
|
+
];
|
|
1163
|
+
function isInternalWorkspaceMetaKey(key) {
|
|
1164
|
+
const forms = [key.toLowerCase(), normalizeWorkspacePath(key).toLowerCase()];
|
|
1165
|
+
return internalMetaKeyPrefixes.some((prefix) => forms.some((form) => form.startsWith(prefix)));
|
|
1166
|
+
}
|
|
1167
|
+
/** Rejects keys that Workspace internals own. Public `setMeta` calls this before it writes. */
|
|
1168
|
+
function assertPublicWorkspaceMetaKey(key) {
|
|
1169
|
+
if (!hasRuntimeType(key, "string")) throw workspaceError("[vitehub] Workspace metadata keys must be strings.");
|
|
1170
|
+
if (isInternalWorkspaceMetaKey(key)) throw workspaceError(`[vitehub] Workspace metadata key ${JSON.stringify(key)} is reserved for Workspace internals.`);
|
|
1171
|
+
return key;
|
|
1172
|
+
}
|
|
1173
|
+
//#endregion
|
|
1174
|
+
//#region src/core/workspace-cache.ts
|
|
1175
|
+
let storeByDefinition = /* @__PURE__ */ new WeakMap();
|
|
1176
|
+
function getCachedWorkspaceStore(definition, create) {
|
|
1177
|
+
let store = storeByDefinition.get(definition);
|
|
1178
|
+
if (!store) {
|
|
1179
|
+
store = create();
|
|
1180
|
+
storeByDefinition.set(definition, store);
|
|
1181
|
+
}
|
|
1182
|
+
return store;
|
|
1183
|
+
}
|
|
1184
|
+
function invalidateCachedWorkspaceStore(definition) {
|
|
1185
|
+
storeByDefinition.delete(definition);
|
|
1186
|
+
}
|
|
1187
|
+
function resetWorkspaceStoreCache() {
|
|
1188
|
+
storeByDefinition = /* @__PURE__ */ new WeakMap();
|
|
1189
|
+
}
|
|
1190
|
+
//#endregion
|
|
1191
|
+
//#region src/core/workspace.ts
|
|
1192
|
+
function getStore(definition) {
|
|
1193
|
+
return getCachedWorkspaceStore(definition, () => createWorkspaceStoreFromProvider(definition));
|
|
1194
|
+
}
|
|
1195
|
+
function createWorkspace(definition, options = {}) {
|
|
1196
|
+
const store = getStore(definition);
|
|
1197
|
+
const files = createWorkspaceSourceView(definition, store, options);
|
|
1198
|
+
const metadata = createWorkspaceMetadataTarget(store, definition.name);
|
|
1199
|
+
const rebaseStore = files.requireRebaseGrants(async (options) => await store.rebase?.(options));
|
|
1200
|
+
const workspace = {
|
|
1201
|
+
name: definition.name,
|
|
1202
|
+
history: createWorkspaceHistory(definition, store, files),
|
|
1203
|
+
async capabilities() {
|
|
1204
|
+
return {
|
|
1205
|
+
conditionalWrites: hasRuntimeType(store.writeFileConditional, "function"),
|
|
1206
|
+
retainedHistory: Boolean(store.history)
|
|
1207
|
+
};
|
|
1208
|
+
},
|
|
1209
|
+
async sync(options) {
|
|
1210
|
+
const { syncWorkspaceSources } = await import("./sync-CN6ec-5M.js");
|
|
1211
|
+
return await syncWorkspaceSources(definition, store, options);
|
|
1212
|
+
},
|
|
1213
|
+
async materializeSources(options) {
|
|
1214
|
+
return await files.materializeSources(options);
|
|
1215
|
+
},
|
|
1216
|
+
async getMeta(key) {
|
|
1217
|
+
return await store.getMeta?.(key);
|
|
1218
|
+
},
|
|
1219
|
+
async setMeta(key, value, capability) {
|
|
1220
|
+
const metadataKey = capability === workspaceInternalMetadataCapability ? key : assertPublicWorkspaceMetaKey(key);
|
|
1221
|
+
await store.setMeta?.(metadataKey, value);
|
|
1222
|
+
},
|
|
1223
|
+
async readFile(path, options) {
|
|
1224
|
+
return await files.readFile(path, options);
|
|
1225
|
+
},
|
|
1226
|
+
async writeFile(path, content, options) {
|
|
1227
|
+
return await files.writeFile(path, content, options);
|
|
1228
|
+
},
|
|
1229
|
+
async list(path, options) {
|
|
1230
|
+
return await files.list(path, options);
|
|
1231
|
+
},
|
|
1232
|
+
async glob(pattern, options) {
|
|
1233
|
+
return await files.glob(pattern, options);
|
|
1234
|
+
},
|
|
1235
|
+
async search(query) {
|
|
1236
|
+
return await files.search(query);
|
|
1237
|
+
},
|
|
1238
|
+
async stat(path) {
|
|
1239
|
+
return await files.stat(path);
|
|
1240
|
+
},
|
|
1241
|
+
async exists(path) {
|
|
1242
|
+
return await files.exists(path);
|
|
1243
|
+
},
|
|
1244
|
+
async mkdir(path, options) {
|
|
1245
|
+
await files.mkdir(path, options);
|
|
1246
|
+
},
|
|
1247
|
+
async rm(path, options) {
|
|
1248
|
+
await files.rm(path, options);
|
|
1249
|
+
},
|
|
1250
|
+
async snapshot(options) {
|
|
1251
|
+
return await store.snapshot(options);
|
|
1252
|
+
},
|
|
1253
|
+
async rebase(options) {
|
|
1254
|
+
if (!store.rebase) throw workspaceErrorDiagnostics.WORKSPACE_R0026({ message: "[vitehub] Workspace Store does not support rebasing." });
|
|
1255
|
+
const grants = [];
|
|
1256
|
+
for (const path of options?.takeRemote ?? []) grants.push(await files.assertWritable(path));
|
|
1257
|
+
await rebaseStore(grants, options);
|
|
1258
|
+
},
|
|
1259
|
+
async diff(options) {
|
|
1260
|
+
return await store.diff(options);
|
|
1261
|
+
},
|
|
1262
|
+
async startSession(options) {
|
|
1263
|
+
const host = options?.host;
|
|
1264
|
+
if (host) {
|
|
1265
|
+
const { createHostedWorkspaceSession } = await import("./host-CFnrTXTK.js").then((n) => n.n);
|
|
1266
|
+
return await createHostedWorkspaceSession(workspace, {
|
|
1267
|
+
...options,
|
|
1268
|
+
host
|
|
1269
|
+
});
|
|
1270
|
+
}
|
|
1271
|
+
return await createBasicWorkspaceSession(workspace, options);
|
|
1272
|
+
}
|
|
1273
|
+
};
|
|
1274
|
+
attachWorkspaceMetadataTarget(workspace, () => metadata);
|
|
1275
|
+
forwardWorkspaceStoreTarget(store, workspace);
|
|
1276
|
+
if (!normalizeWorkspaceSources(definition.sources).some((source) => source.requestDescriptor || source.livePaths)) forwardWorkspaceRevisionMaterializer(store, workspace);
|
|
1277
|
+
const synchronizedWorkspace = workspace;
|
|
1278
|
+
synchronizedWorkspace.__workspaceDefinitionSyncKey = definition;
|
|
1279
|
+
synchronizedWorkspace.__syncWorkspaceDefinition = async (abortSignal) => {
|
|
1280
|
+
const { syncWorkspaceDefinition } = await import("./lifecycle-BwZ7c9hb.js").then((n) => n.t);
|
|
1281
|
+
await syncWorkspaceDefinition(definition, store, abortSignal);
|
|
1282
|
+
};
|
|
1283
|
+
return attachWorkspaceSourceRequestExecution(workspace, createWorkspaceSourceRequestExecution(definition));
|
|
1284
|
+
}
|
|
1285
|
+
//#endregion
|
|
1286
|
+
export { filterSessionDiff as _, isInternalWorkspaceMetaKey as a, scopedSearchQuery as b, isWorkspaceSourceSyncMetaKey as c, sourceSyncPathClaims as d, workspaceSourceSyncStateEquals as f, assertPathInSessionScope as g, assertDiffInsideSessionPaths as h, assertPublicWorkspaceMetaKey as i, readWorkspaceSourceSyncState as l, createBasicWorkspaceSession as m, invalidateCachedWorkspaceStore as n, createWorkspaceSourceView as o, resolveWorkspacePath as p, resetWorkspaceStoreCache as r, invalidateWorkspaceSourceMaterialization as s, createWorkspace as t, sourceSyncMetaKey as u, filterSessionEntries as v, isMissingWorkspacePathError as y };
|