@vite-hub/workspace 0.0.4 → 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.
Files changed (127) hide show
  1. package/README.md +225 -58
  2. package/dist/{ai-CjxvGqM0.d.ts → ai-BW2ZhizI.d.ts} +22 -7
  3. package/dist/{ai-BqCC8BpY.js → ai-BZyLwULx.js} +147 -66
  4. package/dist/ai.d.ts +2 -2
  5. package/dist/ai.js +1 -1
  6. package/dist/{asset-registry-8QI3ML3K.js → asset-registry-DfBV5kmw.js} +3 -2
  7. package/dist/association-Cm0dUTDU-CDDhJnIh-Cv3bWnVb.js +19 -0
  8. package/dist/blob-database.d.ts +521 -0
  9. package/dist/blob-database.js +601 -0
  10. package/dist/cloudflare.d.ts +5 -6
  11. package/dist/cloudflare.js +1 -1
  12. package/dist/config-B7dmzDX3.js +1112 -0
  13. package/dist/{config-DE9xlaIw.d.ts → config-DLKdaFWN.d.ts} +1 -2
  14. package/dist/error-diagnostics-A_8Kp-48.js +116 -0
  15. package/dist/error-diagnostics-DY5GP47p.js +96 -0
  16. package/dist/events-7EBoeIPd-A2RUkJi5-BxrICQ17.js +354 -0
  17. package/dist/events-CRaLy7U3-3PNNp5WZ-Gv9nd0cH.js +214 -0
  18. package/dist/events-CeChclTf-CX-gD8oy-4aL2j6V-.js +527 -0
  19. package/dist/feed-DsC7Vws1-M_de2nSK-tKc0Pwo8.js +248 -0
  20. package/dist/feed-Dyf730Ys-D0HgBba6-BUnCa5gS.js +179 -0
  21. package/dist/file-C0_aPWSS.js +26 -0
  22. package/dist/file-metadata-CxDy-E2H.js +58 -0
  23. package/dist/{files-Q6xZjDHA.js → files-C7Px8uaC.js} +3 -3
  24. package/dist/forgejo-BN8ItCfl-Bbn4vhDd.js +2278 -0
  25. package/dist/{github-DHnjxQIX.js → github-CPxHtFB7.js} +16 -23
  26. package/dist/github-DtncTfmv-Du2S8lGF.js +4159 -0
  27. package/dist/gitlab-BoDm8-BC-xzZsW9eW.js +2530 -0
  28. package/dist/glob--XcLlcml.js +22 -0
  29. package/dist/glob--kNYDbfU.js +30 -0
  30. package/dist/graphql-DG5aJBT3-DceyFYdM-C4saqX37.js +489 -0
  31. package/dist/{host-DJH8cuoU.js → host-CFnrTXTK.js} +97 -41
  32. package/dist/{hosted-RPeJOCFz.js → hosted-acMA5XyL.js} +102 -19
  33. package/dist/hosted-vercel-blob-DKntsHaf.js +12603 -0
  34. package/dist/hosted-vercel-blob.d.ts +5 -6
  35. package/dist/hosted-vercel-blob.js +1 -1
  36. package/dist/hosted.d.ts +6 -7
  37. package/dist/hosted.js +1 -1
  38. package/dist/index.d.ts +57 -36
  39. package/dist/index.js +56 -56
  40. package/dist/inspect-BoObD2cJ.js +167 -0
  41. package/dist/internal/path.d.ts +2 -0
  42. package/dist/internal/path.js +2 -0
  43. package/dist/lifecycle-BwZ7c9hb.js +596 -0
  44. package/dist/loader.d.ts +5 -6
  45. package/dist/loader.js +3 -2
  46. package/dist/mcp-resources-BREFCmtn.js +39 -0
  47. package/dist/memory-BbwOqDXl.js +248 -0
  48. package/dist/metadata-target-DZEZQGHA.js +1588 -0
  49. package/dist/mountx.d.ts +4 -6
  50. package/dist/mountx.js +10 -8
  51. package/dist/nitro.d.ts +1 -1
  52. package/dist/nitro.js +1 -1
  53. package/dist/nuxt.d.ts +13 -12
  54. package/dist/nuxt.js +4 -8
  55. package/dist/operations-BcpWr41m.js +159 -0
  56. package/dist/operations-CGqY1XeG.js +965 -0
  57. package/dist/operations-DnOwKsJT.d.ts +136 -0
  58. package/dist/operations.d.ts +53 -0
  59. package/dist/operations.js +2 -0
  60. package/dist/path-CNAwN7pw.js +114 -0
  61. package/dist/path-DrSIenjj.js +59 -0
  62. package/dist/path-zHxQUbqa.d.ts +9 -0
  63. package/dist/plugins-DqQ8b4T5.d.ts +10 -0
  64. package/dist/provider-kXjfli9u.js +1457 -0
  65. package/dist/providers/github/store.d.ts +3 -5
  66. package/dist/providers/github/store.js +1 -1
  67. package/dist/{registry-D3yzt8HQ.js → registry-Bp-8UUfG.js} +9 -7
  68. package/dist/{registry-Uj6IVxes.d.ts → registry-Cg1TJVVZ.d.ts} +1 -2
  69. package/dist/{request-execution-B-dSFlGB.js → request-execution-Dk9rY-Yh.js} +19 -19
  70. package/dist/{chunk-2rV9d50f.js → rolldown-runtime-B4iAMlE-.js} +2 -2
  71. package/dist/runtime/assets.d.ts +3 -5
  72. package/dist/runtime/assets.js +7 -6
  73. package/dist/runtime/empty-assets-registry.d.ts +1 -2
  74. package/dist/runtime/empty-registry.d.ts +1 -2
  75. package/dist/runtime/state.d.ts +3 -3
  76. package/dist/runtime/state.js +6 -6
  77. package/dist/runtime/workspace.d.ts +1 -1
  78. package/dist/runtime/workspace.js +1 -1
  79. package/dist/runtime.d.ts +51 -17
  80. package/dist/runtime.js +320 -60
  81. package/dist/{search-BpiCF2Tw.js → search-CrTmXj5h.js} +10 -9
  82. package/dist/server-BNQ-Nbjj.js +290 -0
  83. package/dist/server.d.ts +22 -34
  84. package/dist/server.js +2 -328
  85. package/dist/shared-DalXdP0i.js +2158 -0
  86. package/dist/source-metadata.d.ts +6 -7
  87. package/dist/source-metadata.js +7 -7
  88. package/dist/{state-tYcRWEh2.d.ts → state-B8zbLi5t.d.ts} +3 -3
  89. package/dist/store-CXgmu-uw.js +1729 -0
  90. package/dist/{sync-DnTQwt8r.js → sync-CN6ec-5M.js} +61 -41
  91. package/dist/templates-Bn8a-pd2-D9DhJEVD-CBfXNRzz.js +125 -0
  92. package/dist/test.d.ts +1 -1
  93. package/dist/test.js +1 -1
  94. package/dist/{token-CVzURBxC.js → token-CO62GglH.js} +3 -3
  95. package/dist/{types-CYuwjJ4V.d.ts → types-DeK9GIxy.d.ts} +256 -24
  96. package/dist/{use-BTNDR5Rd.js → use-BQj7nSz6.js} +82 -41
  97. package/dist/{use-BiHzt0xA.d.ts → use-DhwhbAVV.d.ts} +16 -11
  98. package/dist/utils-Blrh4N2n.js +545 -0
  99. package/dist/vite-BEvgU2GY.js +20909 -0
  100. package/dist/vite-CcZmMQ7S.d.ts +476 -0
  101. package/dist/vite.d.ts +2 -2
  102. package/dist/vite.js +2 -2
  103. package/dist/webhooks-DgHwxxYz-D1V2LOr1-azofFOiP.js +1565 -0
  104. package/dist/{workspace-Dq08Yn72.js → workspace-6pxkOuxr.js} +361 -230
  105. package/dist/{workspace-Xddf4G1B.d.ts → workspace-BWJQBK1u.d.ts} +4 -3
  106. package/package.json +89 -55
  107. package/dist/collections/client.d.ts +0 -39
  108. package/dist/collections/client.js +0 -137
  109. package/dist/collections.d.ts +0 -59
  110. package/dist/collections.js +0 -159
  111. package/dist/config-Bc3c4Su1.js +0 -7904
  112. package/dist/config-CHLLWklZ.js +0 -36
  113. package/dist/file-Bc1X7pco.js +0 -23
  114. package/dist/glob-CEZKeZFC.js +0 -18
  115. package/dist/hosted-vercel-blob-d6C8ND5j.js +0 -23580
  116. package/dist/lifecycle-LrL3sA4n.js +0 -316
  117. package/dist/metadata-target-B0tOmVW7.js +0 -717
  118. package/dist/path-Cp59koVz.js +0 -139
  119. package/dist/provider-B1yLjCJJ.js +0 -746
  120. package/dist/publish.d.ts +0 -35
  121. package/dist/publish.js +0 -112
  122. package/dist/runtime-options-BsPSv5O7.js +0 -13
  123. package/dist/runtime-type-iEE6BjQJ.js +0 -34
  124. package/dist/shared-DTR1Hhma.js +0 -331
  125. package/dist/store-Dt_Jyys3.js +0 -608
  126. package/dist/vite-BptCrpv2.js +0 -4526
  127. package/dist/vite-CtA9guIM.d.ts +0 -66
@@ -0,0 +1,596 @@
1
+ import { n as __exportAll } from "./rolldown-runtime-B4iAMlE-.js";
2
+ import { s as workspaceError } from "./path-DrSIenjj.js";
3
+ import { f as sha256, l as normalizeWorkspacePath, t as contentStreamChunks } from "./path-CNAwN7pw.js";
4
+ import { a as normalizeWorkspaceSources, c as sourceMountIntersectsPath, f as prepareWorkspaceSource, n as createSourceContext } from "./config-B7dmzDX3.js";
5
+ import { s as invalidateWorkspaceSourceMaterialization } from "./workspace-6pxkOuxr.js";
6
+ import { A as removeWorkspaceOwnedFile, C as withWorkspaceStoreMutation, D as readWorkspaceFileOwner, M as registerWorkspaceStoreAlias, N as workspaceStoreIdentity, O as recordWorkspaceFileOwner, _ as reconcileRemovedStartupSources, d as invalidateSourceSnapshot, j as withWorkspaceFileCheckpoint, k as removeWorkspaceFileOwner, m as readCurrentSourceSnapshot, y as sourceSnapshotOwnsAnyPath } from "./metadata-target-DZEZQGHA.js";
7
+ import { n as useWorkspaceAssets } from "./asset-registry-DfBV5kmw.js";
8
+ import { t as files } from "./files-C7Px8uaC.js";
9
+ import { createHash } from "node:crypto";
10
+ import { getViteHubErrorShape } from "@vite-hub/runtime";
11
+ //#region src/lifecycle.ts
12
+ var lifecycle_exports = /* @__PURE__ */ __exportAll({ syncWorkspaceDefinition: () => syncWorkspaceDefinition });
13
+ const buildSourcesMetaKey = (workspace) => `workspace:${encodeURIComponent(workspace)}:build-sources`;
14
+ const buildFilesMetaKey = (workspace) => `workspace:${encodeURIComponent(workspace)}:build-files`;
15
+ const volatileBuildMetadata = /* @__PURE__ */ new WeakMap();
16
+ async function readBuildMetadata(store, key) {
17
+ const state = volatileBuildMetadata.get(workspaceStoreIdentity(store));
18
+ return state?.has(key) ? state.get(key) : await store.getMeta?.(key);
19
+ }
20
+ async function writeBuildMetadata(store, key, value) {
21
+ const identity = workspaceStoreIdentity(store);
22
+ let state = volatileBuildMetadata.get(identity);
23
+ if (!state) {
24
+ state = /* @__PURE__ */ new Map();
25
+ volatileBuildMetadata.set(identity, state);
26
+ }
27
+ state.set(key, value);
28
+ if (store.getMeta && store.setMeta) {
29
+ await store.setMeta(key, value);
30
+ state.delete(key);
31
+ }
32
+ }
33
+ const buildDirectoriesMetaKey = (workspace) => `workspace:${encodeURIComponent(workspace)}:build-directories`;
34
+ function parentPaths(path) {
35
+ const parts = normalizeWorkspacePath(path).split("/");
36
+ return parts.slice(0, -1).map((_part, index) => parts.slice(0, index + 1).join("/"));
37
+ }
38
+ async function readBuildDirectories(store, workspace) {
39
+ const value = await readBuildMetadata(store, buildDirectoriesMetaKey(workspace));
40
+ return Array.isArray(value) ? value.filter((path) => typeof path === "string" && !!path) : [];
41
+ }
42
+ const buildDirectoryUsersMetaKey = "workspace:build-directory-users";
43
+ async function readBuildDirectoryUsers(store) {
44
+ const value = await readBuildMetadata(store, buildDirectoryUsersMetaKey);
45
+ const users = Object.create(null);
46
+ if (!value || typeof value !== "object" || Array.isArray(value)) return users;
47
+ const entries = Object.entries(value).filter((entry) => Array.isArray(entry[1]) && entry[1].every((name) => typeof name === "string"));
48
+ const directories = /* @__PURE__ */ new Map();
49
+ for (const [path, names] of entries) {
50
+ const current = [];
51
+ for (const name of names) {
52
+ let owned = directories.get(name);
53
+ if (!owned) {
54
+ owned = await readBuildDirectories(store, name);
55
+ directories.set(name, owned);
56
+ }
57
+ if (owned.includes(path)) current.push(name);
58
+ }
59
+ if (current.length) users[path] = current;
60
+ }
61
+ return users;
62
+ }
63
+ async function missingBuildDirectories(store, paths) {
64
+ const missing = /* @__PURE__ */ new Set();
65
+ for (const path of paths) if (!await store.stat(path)) missing.add(path);
66
+ return missing;
67
+ }
68
+ async function recordBuildDirectories(store, workspace, paths, missing) {
69
+ const owned = new Set(await readBuildDirectories(store, workspace));
70
+ const users = await readBuildDirectoryUsers(store);
71
+ for (const path of paths) {
72
+ if (!missing.has(path) && !Object.hasOwn(users, path)) continue;
73
+ if ((await store.stat(path))?.type !== "directory") continue;
74
+ owned.add(path);
75
+ users[path] = [.../* @__PURE__ */ new Set([...Object.hasOwn(users, path) ? users[path] : [], workspace])];
76
+ }
77
+ await writeBuildMetadata(store, buildDirectoriesMetaKey(workspace), [...owned]);
78
+ await writeBuildMetadata(store, buildDirectoryUsersMetaKey, users);
79
+ }
80
+ async function pruneBuildDirectories(store, workspace) {
81
+ await withWorkspaceStoreMutation(store, async () => {
82
+ const owned = await readBuildDirectories(store, workspace);
83
+ const users = await readBuildDirectoryUsers(store);
84
+ const retained = [];
85
+ const pending = new Set(owned);
86
+ for (const path of owned.sort((a, b) => b.length - a.length)) {
87
+ const others = (Object.hasOwn(users, path) ? users[path] : []).filter((name) => name !== workspace);
88
+ if (others.length) {
89
+ users[path] = others;
90
+ pending.delete(path);
91
+ continue;
92
+ }
93
+ if ((await store.stat(path))?.type !== "directory") {
94
+ delete users[path];
95
+ pending.delete(path);
96
+ continue;
97
+ }
98
+ if ((await store.list(path)).length) {
99
+ retained.push(path);
100
+ continue;
101
+ }
102
+ if (!store.removeEmptyDirectory) {
103
+ retained.push(path);
104
+ continue;
105
+ }
106
+ const previousUsers = users[path];
107
+ delete users[path];
108
+ pending.delete(path);
109
+ await writeBuildMetadata(store, buildDirectoriesMetaKey(workspace), [...pending]);
110
+ await writeBuildMetadata(store, buildDirectoryUsersMetaKey, users);
111
+ try {
112
+ await store.removeEmptyDirectory(path);
113
+ } catch (error) {
114
+ pending.add(path);
115
+ if (previousUsers) users[path] = previousUsers;
116
+ await writeBuildMetadata(store, buildDirectoriesMetaKey(workspace), [...pending]);
117
+ await writeBuildMetadata(store, buildDirectoryUsersMetaKey, users);
118
+ const code = error && typeof error === "object" && "code" in error && typeof error.code === "string" ? error.code : void 0;
119
+ if (code === "ENOTEMPTY" || code === "WORKSPACE_FAILED" && error instanceof Error && error.message === `[vitehub] Workspace directory is not empty: ${path}.`) {
120
+ retained.push(path);
121
+ continue;
122
+ }
123
+ throw error;
124
+ }
125
+ }
126
+ await writeBuildMetadata(store, buildDirectoriesMetaKey(workspace), retained);
127
+ await writeBuildMetadata(store, buildDirectoryUsersMetaKey, users);
128
+ });
129
+ }
130
+ async function createBuildLoaderStore(workspace, store, sources, mutationStore = store, abortSignal, trackOperation) {
131
+ const records = await readBuildFiles(store, workspace);
132
+ const record = async (path, source, digest) => {
133
+ if (typeof source !== "string") return;
134
+ const normalized = normalizeWorkspacePath(path);
135
+ records[normalized] = {
136
+ source,
137
+ digest
138
+ };
139
+ try {
140
+ await recordWorkspaceFileOwner(mutationStore, normalized, {
141
+ workspace,
142
+ source,
143
+ digest
144
+ });
145
+ } finally {
146
+ await writeBuildMetadata(mutationStore, buildFilesMetaKey(workspace), records);
147
+ }
148
+ };
149
+ const checkpointWrite = async (path, write, checkpoint) => {
150
+ const normalized = normalizeWorkspacePath(path);
151
+ const previous = records[normalized];
152
+ return await withWorkspaceFileCheckpoint(mutationStore, path, write, checkpoint, async () => {
153
+ if (previous) records[normalized] = previous;
154
+ else delete records[normalized];
155
+ await writeBuildMetadata(mutationStore, buildFilesMetaKey(workspace), records);
156
+ });
157
+ };
158
+ let writes = Promise.resolve();
159
+ const mutate = (operation) => {
160
+ abortSignal?.throwIfAborted();
161
+ const pending = writes.then(() => withWorkspaceStoreMutation(mutationStore, operation));
162
+ writes = pending.then(() => {}, () => {});
163
+ return trackOperation ? trackOperation(pending) : pending;
164
+ };
165
+ const mounts = [...sources].sort((a, b) => b.mountPath.length - a.mountPath.length);
166
+ const tag = (path, file) => {
167
+ const normalized = normalizeWorkspacePath(path);
168
+ const candidates = mounts.filter((source) => !source.mountPath || normalized.startsWith(`${source.mountPath}/`));
169
+ const declared = candidates.find((source) => source.key === file.metadata?.source);
170
+ const closest = candidates.filter((source) => source.mountPath.length === candidates[0]?.mountPath.length);
171
+ if (!declared && closest.length > 1) throw workspaceError(`[vitehub] Loader output "${normalized}" matches multiple build Sources. Set metadata.source to the Source key that owns this output.`);
172
+ const source = declared ?? closest[0];
173
+ return source ? {
174
+ ...file,
175
+ metadata: {
176
+ ...file.metadata,
177
+ workspaceBuildSource: source.key,
178
+ workspaceSourceOwner: workspace
179
+ }
180
+ } : file;
181
+ };
182
+ return new Proxy(store, { get(target, property) {
183
+ if (property === "writeFile") return async (path, file) => await mutate(async () => {
184
+ const tagged = tag(path, file);
185
+ const directories = parentPaths(path);
186
+ const missing = await missingBuildDirectories(mutationStore, directories);
187
+ await checkpointWrite(path, () => mutationStore.writeFile(path, tagged), async () => {
188
+ try {
189
+ await record(path, tagged.metadata?.workspaceBuildSource, await sha256(file.content));
190
+ } finally {
191
+ await recordBuildDirectories(mutationStore, workspace, directories, missing);
192
+ }
193
+ });
194
+ });
195
+ if (property === "writeFileConditional" && target.writeFileConditional) return async (path, file, digest) => await mutate(async () => {
196
+ const tagged = tag(path, file);
197
+ const directories = parentPaths(path);
198
+ const missing = await missingBuildDirectories(mutationStore, directories);
199
+ await checkpointWrite(path, () => mutationStore.writeFileConditional(path, tagged, digest), async () => {
200
+ try {
201
+ await record(path, tagged.metadata?.workspaceBuildSource, await sha256(file.content));
202
+ } finally {
203
+ await recordBuildDirectories(mutationStore, workspace, directories, missing);
204
+ }
205
+ });
206
+ });
207
+ if (property === "writeFileStream" && target.writeFileStream) return async (path, file) => await mutate(async () => {
208
+ const tagged = tag(path, file);
209
+ const hash = createHash("sha256");
210
+ const content = (async function* () {
211
+ for await (const chunk of contentStreamChunks(tagged.content)) {
212
+ hash.update(chunk);
213
+ yield chunk;
214
+ }
215
+ })();
216
+ const directories = parentPaths(path);
217
+ const missing = await missingBuildDirectories(mutationStore, directories);
218
+ return await checkpointWrite(path, () => mutationStore.writeFileStream(path, {
219
+ ...tagged,
220
+ content
221
+ }), async () => {
222
+ try {
223
+ await record(path, tagged.metadata?.workspaceBuildSource, hash.digest("hex"));
224
+ } finally {
225
+ await recordBuildDirectories(mutationStore, workspace, directories, missing);
226
+ }
227
+ });
228
+ });
229
+ return Reflect.get(target, property, target)?.bind(target);
230
+ } });
231
+ }
232
+ const STORE_MUTATIONS = /* @__PURE__ */ new Set([
233
+ "mkdir",
234
+ "rebase",
235
+ "removeEmptyDirectory",
236
+ "rm",
237
+ "setMeta",
238
+ "snapshot",
239
+ "writeFile",
240
+ "writeFileConditional",
241
+ "writeFileStream"
242
+ ]);
243
+ function createAbortFencedStore(store, abortSignal) {
244
+ const active = /* @__PURE__ */ new Set();
245
+ let settleIdle;
246
+ const idle = () => active.size ? new Promise((resolve) => {
247
+ settleIdle = resolve;
248
+ }) : Promise.resolve();
249
+ const track = (operation) => {
250
+ active.add(operation);
251
+ operation.then(() => {
252
+ active.delete(operation);
253
+ if (!active.size) {
254
+ settleIdle?.();
255
+ settleIdle = void 0;
256
+ }
257
+ }, () => {
258
+ active.delete(operation);
259
+ if (!active.size) {
260
+ settleIdle?.();
261
+ settleIdle = void 0;
262
+ }
263
+ });
264
+ return operation;
265
+ };
266
+ const fenced = new Proxy(store, { get(target, property) {
267
+ const value = Reflect.get(target, property, target);
268
+ if (!STORE_MUTATIONS.has(String(property))) return value?.bind(target);
269
+ if (value === void 0) return void 0;
270
+ return (...args) => {
271
+ abortSignal.throwIfAborted();
272
+ const operation = Promise.resolve(Reflect.apply(value, target, args));
273
+ return track(operation);
274
+ };
275
+ } });
276
+ registerWorkspaceStoreAlias(fenced, store);
277
+ return {
278
+ fenced,
279
+ idle,
280
+ track
281
+ };
282
+ }
283
+ async function waitForFencedSync(operation, signal, idle) {
284
+ let removeAbortListener = () => {};
285
+ const aborted = new Promise((_resolve, reject) => {
286
+ const onAbort = () => {
287
+ idle().then(() => reject(signal.reason));
288
+ };
289
+ signal.addEventListener("abort", onAbort, { once: true });
290
+ removeAbortListener = () => signal.removeEventListener("abort", onAbort);
291
+ if (signal.aborted) onAbort();
292
+ });
293
+ try {
294
+ await Promise.race([operation, aborted]);
295
+ } finally {
296
+ removeAbortListener();
297
+ }
298
+ }
299
+ async function syncWorkspaceDefinition(definition, store, abortSignal) {
300
+ if (!abortSignal) return await syncWorkspaceDefinitionInternal(definition, store);
301
+ const { fenced, idle, track } = createAbortFencedStore(store, abortSignal);
302
+ await waitForFencedSync(syncWorkspaceDefinitionInternal(definition, fenced, abortSignal, store, track), abortSignal, idle);
303
+ }
304
+ async function syncWorkspaceDefinitionInternal(definition, store, abortSignal, materializationStore = store, trackOperation) {
305
+ abortSignal?.throwIfAborted();
306
+ const loaders = definition.loaders?.length ? definition.loaders : [files()];
307
+ const hasExplicitLoaders = !!definition.loaders?.length;
308
+ const ctxSource = createSourceContext(definition, void 0, store);
309
+ const sources = normalizeWorkspaceSources(definition.sources);
310
+ const buildSources = sources.filter((source) => source.materialize === "build");
311
+ const startupSources = sources.filter((source) => source.materialize === "startup");
312
+ await reconcileRemovedStartupSources(definition.name, store, startupSources, void 0, materializationStore);
313
+ abortSignal?.throwIfAborted();
314
+ const hasBuildSourceState = await reconcileBuildSourceMounts(definition, store, materializationStore, buildSources, startupSources, abortSignal, trackOperation);
315
+ abortSignal?.throwIfAborted();
316
+ const buildStore = await createBuildLoaderStore(definition.name, store, buildSources, materializationStore, abortSignal, trackOperation);
317
+ const bundledBuildSources = !hasExplicitLoaders ? await syncRuntimeBuildAssets(definition, buildStore, buildSources, abortSignal) : void 0;
318
+ abortSignal?.throwIfAborted();
319
+ if (bundledBuildSources && buildSources.every((source) => bundledBuildSources.has(source.key))) {
320
+ await invalidateOverwrittenStartupSnapshots(definition, store, materializationStore, startupSources, buildSources);
321
+ await store.snapshot({ name: "sync" });
322
+ abortSignal?.throwIfAborted();
323
+ return;
324
+ }
325
+ if (!hasBuildSourceState && !hasExplicitLoaders) return;
326
+ const normalizedSources = buildSources.filter((source) => !bundledBuildSources?.has(source.key)).map((source) => createMountedBuildSource(source, definition.name));
327
+ const ctx = {
328
+ abortSignal,
329
+ workspace: definition.name,
330
+ rootDir: ctxSource.rootDir,
331
+ sourceRootDir: ctxSource.sourceRootDir,
332
+ sources: normalizedSources,
333
+ store: buildStore,
334
+ parseData: async (input) => input.data,
335
+ generateDigest: (input) => JSON.stringify(input),
336
+ logger: console
337
+ };
338
+ for (const loader of loaders) {
339
+ abortSignal?.throwIfAborted();
340
+ await loader.load(ctx);
341
+ }
342
+ abortSignal?.throwIfAborted();
343
+ await invalidateOverwrittenStartupSnapshots(definition, store, materializationStore, startupSources, buildSources);
344
+ await store.snapshot({ name: "sync" });
345
+ abortSignal?.throwIfAborted();
346
+ }
347
+ async function invalidateOverwrittenStartupSnapshots(definition, store, materializationStore, startupSources, buildSources) {
348
+ for (const source of startupSources) {
349
+ const snapshot = await readCurrentSourceSnapshot(store, definition.name, source);
350
+ if (snapshot?.status !== "ready") continue;
351
+ for (const path of Object.keys(snapshot.items || {})) {
352
+ const file = await store.readFile(path);
353
+ if (!file) continue;
354
+ const owner = await readWorkspaceFileOwner(store, path);
355
+ if (!(owner ? owner.workspace === definition.name && buildSources.some((buildSource) => buildSource.key === owner.source) && await sha256(file.content) === owner.digest : (file.metadata?.workspaceSourceOwner === void 0 || file.metadata.workspaceSourceOwner === definition.name) && buildSources.some((buildSource) => buildSource.key === file.metadata?.source || buildSource.key === file.metadata?.workspaceBuildSource))) continue;
356
+ await invalidateWorkspaceSourceMaterialization(definition, materializationStore, [source.key]);
357
+ await invalidateSourceSnapshot(store, definition.name, source.key);
358
+ break;
359
+ }
360
+ }
361
+ }
362
+ async function reconcileBuildSourceMounts(definition, store, materializationStore, currentSources, startupSources, abortSignal, trackOperation) {
363
+ abortSignal?.throwIfAborted();
364
+ const previousSources = await readSyncedBuildSources(store, definition.name);
365
+ abortSignal?.throwIfAborted();
366
+ const hasBuildSourceState = previousSources.length > 0 || currentSources.length > 0;
367
+ const resetPaths = [.../* @__PURE__ */ new Set([...previousSources.map((source) => source.mountPath), ...currentSources.map((source) => source.mountPath)])];
368
+ const mutate = async (operation) => {
369
+ abortSignal?.throwIfAborted();
370
+ const pending = operation();
371
+ await (trackOperation ? trackOperation(pending) : pending);
372
+ };
373
+ for (const mountPath of resetPaths.filter(Boolean).sort((a, b) => b.length - a.length)) {
374
+ abortSignal?.throwIfAborted();
375
+ const buildKeys = [...previousSources, ...currentSources].filter((source) => source.mountPath === mountPath).map((source) => source.key);
376
+ await mutate(() => withWorkspaceStoreMutation(materializationStore, async () => {
377
+ const removedPaths = await buildSourceFilePaths(materializationStore, definition.name, mountPath, buildKeys);
378
+ const affected = [];
379
+ for (const startup of startupSources.filter((source) => sourceMountIntersectsPath(source, mountPath))) {
380
+ if (!(startup.mountPath === mountPath || startup.mountPath.startsWith(`${mountPath}/`)) && await sourceSnapshotOwnsAnyPath(store, definition.name, startup.key, removedPaths) === false) continue;
381
+ affected.push(startup);
382
+ }
383
+ await invalidateWorkspaceSourceMaterialization(definition, materializationStore, affected.map((source) => source.key));
384
+ for (const source of affected) await invalidateSourceSnapshot(store, definition.name, source.key);
385
+ abortSignal?.throwIfAborted();
386
+ await removeBuildSourceFiles(materializationStore, definition.name, removedPaths);
387
+ }));
388
+ abortSignal?.throwIfAborted();
389
+ }
390
+ const rootSourceKeys = new Set([...previousSources, ...currentSources].filter((source) => !source.mountPath).map((source) => source.key));
391
+ for (const key of rootSourceKeys) {
392
+ abortSignal?.throwIfAborted();
393
+ await mutate(() => withWorkspaceStoreMutation(materializationStore, async () => {
394
+ const removedPaths = await buildSourceFilePaths(materializationStore, definition.name, "", [key]);
395
+ const affected = [];
396
+ for (const startup of startupSources) {
397
+ if (!removedPaths.some((path) => sourceMountIntersectsPath(startup, path))) continue;
398
+ if (await sourceSnapshotOwnsAnyPath(store, definition.name, startup.key, removedPaths) === false) continue;
399
+ affected.push(startup);
400
+ }
401
+ await invalidateWorkspaceSourceMaterialization(definition, materializationStore, affected.map((startup) => startup.key));
402
+ for (const startup of affected) await invalidateSourceSnapshot(store, definition.name, startup.key);
403
+ abortSignal?.throwIfAborted();
404
+ await removeBuildSourceFiles(materializationStore, definition.name, removedPaths);
405
+ }));
406
+ abortSignal?.throwIfAborted();
407
+ }
408
+ await mutate(() => pruneBuildDirectories(materializationStore, definition.name));
409
+ for (const mountPath of [...new Set(currentSources.map((source) => source.mountPath))].filter(Boolean).sort((a, b) => a.length - b.length)) {
410
+ abortSignal?.throwIfAborted();
411
+ await mutate(() => withWorkspaceStoreMutation(materializationStore, async () => {
412
+ const directories = [...parentPaths(mountPath), mountPath];
413
+ const missing = await missingBuildDirectories(materializationStore, directories);
414
+ await materializationStore.mkdir(mountPath, { recursive: true });
415
+ await recordBuildDirectories(materializationStore, definition.name, directories, missing);
416
+ }));
417
+ abortSignal?.throwIfAborted();
418
+ }
419
+ abortSignal?.throwIfAborted();
420
+ await writeBuildMetadata(store, buildFilesMetaKey(definition.name), {});
421
+ await writeBuildMetadata(store, buildSourcesMetaKey(definition.name), currentSources.map(({ key, mountPath }) => ({
422
+ key,
423
+ mountPath
424
+ })));
425
+ return hasBuildSourceState;
426
+ }
427
+ async function syncRuntimeBuildAssets(definition, store, currentSources, abortSignal) {
428
+ abortSignal?.throwIfAborted();
429
+ if (!currentSources.length) return void 0;
430
+ let assets;
431
+ try {
432
+ assets = useWorkspaceAssets(definition.name);
433
+ } catch (error) {
434
+ if (getViteHubErrorShape(error)?.code === "WORKSPACE_NOT_FOUND") return void 0;
435
+ throw error;
436
+ }
437
+ const entries = await assets.list("", { recursive: true });
438
+ abortSignal?.throwIfAborted();
439
+ const files = entries.filter((entry) => entry.type === "file");
440
+ const bundledPaths = new Set(files.map((entry) => entry.path));
441
+ const bundledSourceByPath = /* @__PURE__ */ new Map();
442
+ const bundledBuildSources = /* @__PURE__ */ new Set();
443
+ for (const source of currentSources) {
444
+ abortSignal?.throwIfAborted();
445
+ if (await hasCompleteBundledBuildSource(definition, store, source, bundledPaths, files, bundledSourceByPath, abortSignal)) {
446
+ abortSignal?.throwIfAborted();
447
+ bundledBuildSources.add(source.key);
448
+ }
449
+ }
450
+ for (const entry of files) {
451
+ abortSignal?.throwIfAborted();
452
+ const sourceKey = typeof entry.metadata?.source === "string" ? entry.metadata.source : bundledSourceByPath.get(entry.path) || findBuildSourceForPath(entry.path, currentSources)?.key;
453
+ const content = await assets.readFile(entry.path, { encoding: "binary" });
454
+ abortSignal?.throwIfAborted();
455
+ await store.writeFile(entry.path, {
456
+ path: entry.path,
457
+ content,
458
+ mediaType: entry.mediaType,
459
+ metadata: sourceKey ? {
460
+ ...entry.metadata,
461
+ source: sourceKey,
462
+ workspaceSourceOwner: definition.name
463
+ } : entry.metadata
464
+ });
465
+ }
466
+ return bundledBuildSources;
467
+ }
468
+ async function hasCompleteBundledBuildSource(definition, store, source, bundledPaths, bundledFiles, bundledSourceByPath, abortSignal) {
469
+ const probeKeys = source.source.probeKeys;
470
+ if (!probeKeys?.length) {
471
+ const paths = bundledFiles?.filter((file) => file.metadata?.source === source.key).map((file) => file.path) ?? [];
472
+ const sourcePaths = await tryListBuildSourcePaths(definition, store, source, abortSignal);
473
+ if (sourcePaths && !sourcePaths.every((path) => paths.includes(path))) return false;
474
+ for (const path of paths) bundledSourceByPath?.set(path, source.key);
475
+ return paths.length > 0;
476
+ }
477
+ const paths = probeKeys.map((key) => normalizeWorkspacePath(`${source.mountPath}/${key}`));
478
+ if (!paths.every((path) => bundledPaths.has(path))) return false;
479
+ for (const path of paths) bundledSourceByPath?.set(path, source.key);
480
+ return true;
481
+ }
482
+ async function tryListBuildSourcePaths(definition, store, source, abortSignal) {
483
+ const ctx = createSourceContext(definition, {
484
+ key: source.key,
485
+ mountPath: source.mountPath
486
+ }, store, { abortSignal });
487
+ try {
488
+ await prepareWorkspaceSource(source.source, ctx);
489
+ return (await source.source.getKeys(ctx)).map((key) => normalizeWorkspacePath(`${source.mountPath}/${key}`));
490
+ } catch {
491
+ return;
492
+ }
493
+ }
494
+ function findBuildSourceForPath(path, sources) {
495
+ const matches = sources.filter((source) => !source.mountPath || path === source.mountPath || path.startsWith(`${source.mountPath}/`)).sort((left, right) => right.mountPath.length - left.mountPath.length)[0];
496
+ if (!matches?.mountPath && sources.filter((source) => !source.mountPath).length > 1) return void 0;
497
+ return matches;
498
+ }
499
+ async function readSyncedBuildSources(store, workspace) {
500
+ const value = await readBuildMetadata(store, buildSourcesMetaKey(workspace));
501
+ if (!Array.isArray(value)) return [];
502
+ return value.filter(isSyncedBuildSource);
503
+ }
504
+ async function readBuildFiles(store, workspace) {
505
+ const value = await readBuildMetadata(store, buildFilesMetaKey(workspace));
506
+ if (!value || typeof value !== "object" || Array.isArray(value)) return {};
507
+ return Object.fromEntries(Object.entries(value).filter((entry) => {
508
+ const record = entry[1];
509
+ return !!record && typeof record === "object" && "source" in record && typeof record.source === "string" && "digest" in record && typeof record.digest === "string";
510
+ }));
511
+ }
512
+ async function buildSourceFilePaths(store, workspace, mountPath, keys) {
513
+ const paths = [];
514
+ const records = await readBuildFiles(store, workspace);
515
+ for (const [path, record] of Object.entries(records)) if (keys.includes(record.source)) await readWorkspaceFileOwner(store, path, true);
516
+ for (const entry of await store.list(mountPath, { recursive: true })) {
517
+ if (entry.type !== "file") continue;
518
+ const file = entry.metadata ? void 0 : await store.readFile(entry.path);
519
+ const metadata = entry.metadata ?? file?.metadata;
520
+ if (metadata?.workspaceSourceOwner !== void 0 && metadata.workspaceSourceOwner !== workspace) continue;
521
+ const owner = await readWorkspaceFileOwner(store, entry.path);
522
+ const record = records[entry.path] ?? (owner?.workspace === workspace && owner.digest ? {
523
+ source: owner.source,
524
+ digest: owner.digest
525
+ } : void 0);
526
+ if (record && keys.includes(record.source)) {
527
+ if (metadata?.workspaceSourceOwner === void 0 && (metadata?.source !== void 0 || metadata?.workspaceBuildSource !== void 0)) continue;
528
+ if (owner?.workspace !== workspace || owner.source !== record.source || owner.digest !== record.digest) continue;
529
+ if (entry.digest !== record.digest) {
530
+ const current = file ?? await store.readFile(entry.path);
531
+ if (!current || await sha256(current.content) !== record.digest) {
532
+ await removeWorkspaceFileOwner(store, entry.path);
533
+ continue;
534
+ }
535
+ }
536
+ paths.push(entry.path);
537
+ } else if (metadata?.workspaceSourceOwner === workspace && keys.some((key) => metadata.source === key || metadata.workspaceBuildSource === key)) paths.push(entry.path);
538
+ }
539
+ return paths;
540
+ }
541
+ async function removeBuildSourceFiles(store, workspace, paths) {
542
+ const records = await readBuildFiles(store, workspace);
543
+ for (const path of paths) {
544
+ await removeWorkspaceOwnedFile(store, path);
545
+ delete records[path];
546
+ await writeBuildMetadata(store, buildFilesMetaKey(workspace), records);
547
+ }
548
+ }
549
+ function isSyncedBuildSource(value) {
550
+ return !!value && typeof value === "object" && typeof value.key === "string" && typeof value.mountPath === "string";
551
+ }
552
+ function createMountedBuildSource(source, workspace) {
553
+ const sourceContexts = /* @__PURE__ */ new WeakMap();
554
+ function getSourceContext(ctx) {
555
+ let sourceContext = sourceContexts.get(ctx);
556
+ if (sourceContext) return sourceContext;
557
+ sourceContext = {
558
+ ...ctx,
559
+ mountPath: source.mountPath,
560
+ source: source.key
561
+ };
562
+ sourceContexts.set(ctx, sourceContext);
563
+ return sourceContext;
564
+ }
565
+ return {
566
+ ...source.source,
567
+ key: source.key,
568
+ async resolveRevision(ctx) {
569
+ const sourceContext = getSourceContext(ctx);
570
+ const revision = await source.source.resolveRevision?.(sourceContext);
571
+ if (revision) sourceContext.revision = revision;
572
+ return revision;
573
+ },
574
+ async prepare(ctx) {
575
+ await prepareWorkspaceSource(source.source, getSourceContext(ctx));
576
+ },
577
+ async getKeys(ctx) {
578
+ return await source.source.getKeys(getSourceContext(ctx));
579
+ },
580
+ async getItem(key, ctx) {
581
+ const item = await source.source.getItem(key, getSourceContext(ctx));
582
+ const path = normalizeWorkspacePath(`${source.mountPath}/${item.path || item.key}`);
583
+ return {
584
+ ...item,
585
+ path,
586
+ metadata: {
587
+ ...item.metadata,
588
+ source: source.key,
589
+ workspaceSourceOwner: workspace
590
+ }
591
+ };
592
+ }
593
+ };
594
+ }
595
+ //#endregion
596
+ export { syncWorkspaceDefinition as n, lifecycle_exports as t };
package/dist/loader.d.ts CHANGED
@@ -1,17 +1,16 @@
1
- import { M as WorkspaceContent, Ot as WorkspaceSourceItem, X as WorkspaceLoader } from "./types-CYuwjJ4V.js";
2
-
1
+ import { M as WorkspaceContent, Yt as WorkspaceSourceItem, lt as WorkspaceLoader } from "./types-DeK9GIxy.js";
3
2
  //#region src/loaders/files.d.ts
4
3
  interface FilesLoaderOptions {
5
4
  include?: string | string[];
6
5
  exclude?: string | string[];
7
6
  transform?: (item: WorkspaceSourceItem) => WorkspaceContent | WorkspaceSourceItem | Promise<WorkspaceContent | WorkspaceSourceItem>;
8
7
  }
9
- declare function files(options?: FilesLoaderOptions): WorkspaceLoader;
8
+ export declare function files(options?: FilesLoaderOptions): WorkspaceLoader;
10
9
  //#endregion
11
10
  //#region src/loaders/json.d.ts
12
- declare function json(options?: FilesLoaderOptions): WorkspaceLoader;
11
+ export declare function json(options?: FilesLoaderOptions): WorkspaceLoader;
13
12
  //#endregion
14
13
  //#region src/loaders/text.d.ts
15
- declare function text(options?: FilesLoaderOptions): WorkspaceLoader;
14
+ export declare function text(options?: FilesLoaderOptions): WorkspaceLoader;
16
15
  //#endregion
17
- export { type FilesLoaderOptions, files, json, text };
16
+ export type { FilesLoaderOptions };
package/dist/loader.js CHANGED
@@ -1,11 +1,12 @@
1
- import { t as files } from "./files-Q6xZjDHA.js";
1
+ import { o as isWorkspaceBytes } from "./path-CNAwN7pw.js";
2
+ import { t as files } from "./files-C7Px8uaC.js";
2
3
  //#region src/loaders/json.ts
3
4
  function json(options = {}) {
4
5
  return files({
5
6
  ...options,
6
7
  transform: async (item) => {
7
8
  const transformed = options.transform ? await options.transform(item) : item;
8
- if (typeof transformed === "string" || transformed instanceof Uint8Array) return transformed;
9
+ if (typeof transformed === "string" || isWorkspaceBytes(transformed)) return transformed;
9
10
  return {
10
11
  ...transformed,
11
12
  mediaType: transformed.mediaType || "application/json"
@@ -0,0 +1,39 @@
1
+ import { n as __exportAll } from "./rolldown-runtime-B4iAMlE-.js";
2
+ import { t as normalizeSafeWorkspacePath } from "./path-DrSIenjj.js";
3
+ import { b as markLiveWorkspaceSource, d as withWorkspaceRuntimeOptions, f as prepareWorkspaceSource, u as ownSourceOption } from "./config-B7dmzDX3.js";
4
+ import { mcpResources } from "@vite-hub/source/mcp";
5
+ //#region src/sources/mcp-resources.ts
6
+ var mcp_resources_exports = /* @__PURE__ */ __exportAll({ mcpResources: () => mcpResources$1 });
7
+ function mcpResources$1(options) {
8
+ const livePaths = {};
9
+ const baseSource = mcpResources({
10
+ ...options,
11
+ cache: ownSourceOption(options, "cache")
12
+ });
13
+ const source = withWorkspaceRuntimeOptions({
14
+ ...baseSource,
15
+ async prepare(ctx) {
16
+ await prepareWorkspaceSource(baseSource, ctx);
17
+ const mountPath = resolveMountPath(ownSourceOption(options, "mount"), ctx);
18
+ const keys = await baseSource.getKeys(ctx);
19
+ resetLivePaths(livePaths, mountPath, keys);
20
+ }
21
+ }, {
22
+ ...options,
23
+ materialize: ownSourceOption(options, "materialize") || (ownSourceOption(options, "sync") ? "none" : "lazy")
24
+ });
25
+ return markLiveWorkspaceSource(source, livePaths);
26
+ }
27
+ function resolveMountPath(mount, ctx) {
28
+ const explicit = typeof mount === "string" ? mount : mount && ownSourceOption(mount, "path");
29
+ return normalizeSafeWorkspacePath(explicit ?? ctx.mountPath ?? ctx.source ?? "", { allowEmpty: true });
30
+ }
31
+ function resetLivePaths(paths, mountPath, keys) {
32
+ for (const path of Object.keys(paths)) delete paths[path];
33
+ for (const key of keys) {
34
+ const path = normalizeSafeWorkspacePath(`${mountPath}/${key}`.replace(/\/+/g, "/"), { allowEmpty: false });
35
+ paths[path] = key;
36
+ }
37
+ }
38
+ //#endregion
39
+ export { mcp_resources_exports as n, mcpResources$1 as t };