@vite-hub/workspace 0.0.4 → 0.3.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/README.md +339 -115
- package/dist/abort-BCwpdrN0.js +32 -0
- package/dist/{asset-registry-8QI3ML3K.js → asset-registry-DfBV5kmw.js} +3 -2
- package/dist/blob-database.d.ts +474 -0
- package/dist/blob-database.js +608 -0
- package/dist/checkout.d.ts +128 -0
- package/dist/checkout.js +849 -0
- package/dist/cloudflare.d.ts +6 -7
- package/dist/cloudflare.js +1 -1
- package/dist/config-B-863EgC.js +15 -0
- package/dist/config-BAgL2dtB.js +1076 -0
- package/dist/{config-DE9xlaIw.d.ts → config-BQKcPawI.d.ts} +1 -2
- package/dist/discard-CNNtCozM.js +8 -0
- package/dist/durability.d.ts +22 -0
- package/dist/durability.js +70 -0
- package/dist/error-diagnostics-CtKjGtGb.js +188 -0
- package/dist/error-diagnostics-DSkCJC_L.js +266 -0
- package/dist/file-2pFYNtsU.js +22 -0
- package/dist/{files-Q6xZjDHA.js → files-BOo4SbwE.js} +3 -3
- package/dist/files.d.ts +38 -0
- package/dist/files.js +6 -0
- package/dist/{github-DHnjxQIX.js → github-CrYp4v8k.js} +44 -24
- package/dist/glob-B8YGvTrZ.js +21 -0
- package/dist/glob-Cjxms1t7.js +30 -0
- package/dist/{host-DJH8cuoU.js → host-BFBpXn0N.js} +144 -77
- package/dist/{hosted-RPeJOCFz.js → hosted-KcUR_jPv.js} +303 -97
- package/dist/hosted-vercel-blob-B3-wRdUm.js +410 -0
- package/dist/hosted-vercel-blob.d.ts +6 -7
- package/dist/hosted-vercel-blob.js +1 -1
- package/dist/hosted.d.ts +10 -9
- package/dist/hosted.js +2 -2
- package/dist/index.d.ts +26 -47
- package/dist/index.js +30 -100
- package/dist/internal/path.d.ts +2 -0
- package/dist/internal/path.js +2 -0
- package/dist/lifecycle-B-enk_4A.js +596 -0
- package/dist/lineage.d.ts +26 -0
- package/dist/lineage.js +76 -0
- package/dist/loader.d.ts +5 -6
- package/dist/loader.js +3 -2
- package/dist/mcp-resources-DF0F5rcL.js +39 -0
- package/dist/memory-BOexthyW.js +247 -0
- package/dist/metadata-target-CWz6KHKq.js +1589 -0
- package/dist/object-Da1zyPCR.js +11 -0
- package/dist/operation-definition-8gOe-ubD.d.ts +234 -0
- package/dist/operations-C6FpR8Rr.js +389 -0
- package/dist/operations-CE2drFih.js +1234 -0
- package/dist/operations.d.ts +90 -0
- package/dist/operations.js +3 -0
- package/dist/path--wrT_Azm.js +113 -0
- package/dist/path-AxMho1wo.d.ts +7 -0
- package/dist/path-DrSIenjj.js +59 -0
- package/dist/path-zHxQUbqa.d.ts +9 -0
- package/dist/plugins-C8C0auoW.d.ts +10 -0
- package/dist/provider-9s4Ufkm5.js +1479 -0
- package/dist/providers/github/store.d.ts +3 -5
- package/dist/providers/github/store.js +1 -1
- package/dist/registry-B3BBm4Ts.d.ts +19 -0
- package/dist/{registry-D3yzt8HQ.js → registry-CNphafBx.js} +60 -23
- package/dist/registry-CYNN623b.d.ts +9 -0
- package/dist/request-scope-Dw5n8bMC.js +83 -0
- package/dist/results-DgkZUmTL.js +8 -0
- package/dist/{chunk-2rV9d50f.js → rolldown-runtime-rQ84J-ij.js} +3 -4
- package/dist/runtime/assets.d.ts +3 -5
- package/dist/runtime/assets.js +7 -6
- package/dist/runtime/empty-assets-registry.d.ts +1 -2
- package/dist/runtime/empty-registry.d.ts +2 -2
- package/dist/runtime/registry.d.ts +2 -0
- package/dist/runtime/registry.js +23 -0
- package/dist/runtime/request.d.ts +4 -0
- package/dist/runtime/request.js +8 -0
- package/dist/runtime/state.d.ts +5 -4
- package/dist/runtime/state.js +8 -18
- package/dist/runtime/workspace.d.ts +1 -1
- package/dist/runtime/workspace.js +1 -1
- package/dist/runtime.d.ts +52 -17
- package/dist/runtime.js +324 -67
- package/dist/{search-BpiCF2Tw.js → search-C_RfoM2j.js} +10 -9
- package/dist/server-8tGltlGz.js +303 -0
- package/dist/server.d.ts +36 -39
- package/dist/server.js +2 -328
- package/dist/shared-B3_EUk89.js +848 -0
- package/dist/source-metadata.d.ts +10 -7
- package/dist/source-metadata.js +12 -12
- package/dist/state-BISnUf1s.d.ts +35 -0
- package/dist/{ai-BqCC8BpY.js → state-DbrW-njQ.js} +237 -114
- package/dist/store-ybhyD38Z.js +1873 -0
- package/dist/{sync-DnTQwt8r.js → sync-CCD_VYC8.js} +61 -41
- package/dist/target-jWhTdIuF.js +89 -0
- package/dist/test.d.ts +1 -1
- package/dist/test.js +1 -1
- package/dist/{types-CYuwjJ4V.d.ts → types-BbtwmBZ5.d.ts} +321 -35
- package/dist/use-C6lV3-wC.d.ts +321 -0
- package/dist/{use-BTNDR5Rd.js → use-CSpT5W87.js} +141 -107
- package/dist/utils-CgMZCHsX.js +616 -0
- package/dist/vite.d.ts +585 -2
- package/dist/vite.js +21369 -2
- package/dist/workspace-CYB2xZ1T.d.ts +7 -0
- package/dist/{workspace-Dq08Yn72.js → workspace-sPWn2Jxj.js} +441 -225
- package/package.json +97 -72
- package/dist/ai-CjxvGqM0.d.ts +0 -162
- package/dist/ai.d.ts +0 -3
- package/dist/ai.js +0 -2
- 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-Bc3c4Su1.js +0 -7904
- package/dist/config-CHLLWklZ.js +0 -36
- package/dist/file-Bc1X7pco.js +0 -23
- package/dist/glob-CEZKeZFC.js +0 -18
- package/dist/hosted-vercel-blob-d6C8ND5j.js +0 -23580
- package/dist/lifecycle-LrL3sA4n.js +0 -316
- package/dist/metadata-target-B0tOmVW7.js +0 -717
- package/dist/mountx.d.ts +0 -10
- package/dist/mountx.js +0 -143
- package/dist/nitro.d.ts +0 -2
- package/dist/nitro.js +0 -2
- package/dist/nuxt.d.ts +0 -26
- package/dist/nuxt.js +0 -42
- package/dist/path-Cp59koVz.js +0 -139
- package/dist/provider-B1yLjCJJ.js +0 -746
- package/dist/publish.d.ts +0 -35
- package/dist/publish.js +0 -112
- package/dist/registry-Uj6IVxes.d.ts +0 -11
- package/dist/request-execution-B-dSFlGB.js +0 -84
- package/dist/runtime-options-BsPSv5O7.js +0 -13
- package/dist/runtime-type-iEE6BjQJ.js +0 -34
- package/dist/shared-DTR1Hhma.js +0 -331
- package/dist/state-tYcRWEh2.d.ts +0 -26
- package/dist/store-Dt_Jyys3.js +0 -608
- package/dist/target-D4Bqb1eH.js +0 -11
- package/dist/token-CVzURBxC.js +0 -61
- package/dist/use-BiHzt0xA.d.ts +0 -103
- package/dist/vite-BptCrpv2.js +0 -4526
- package/dist/vite-CtA9guIM.d.ts +0 -66
- package/dist/workspace-Xddf4G1B.d.ts +0 -6
package/dist/lineage.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { parse } from "yaml";
|
|
3
|
+
//#region src/lineage.ts
|
|
4
|
+
/** The digest that `derivedFrom.digest` holds: `sha256-` and the hex SHA-256 of the file bytes. */
|
|
5
|
+
function lineageDigest(bytes) {
|
|
6
|
+
return `sha256-${createHash("sha256").update(bytes).digest("hex")}`;
|
|
7
|
+
}
|
|
8
|
+
const frontmatter = /^---\r?\n([\s\S]*?)\r?\n---(?:\r?\n|$)/;
|
|
9
|
+
const markdown = /* @__PURE__ */ new Set([
|
|
10
|
+
"markdown",
|
|
11
|
+
"md",
|
|
12
|
+
"mdx"
|
|
13
|
+
]);
|
|
14
|
+
function derivedFrom(parsed) {
|
|
15
|
+
const value = Object(parsed) === parsed ? Reflect.get(Object(parsed), "derivedFrom") : void 0;
|
|
16
|
+
if (Object(value) !== value) return void 0;
|
|
17
|
+
const path = Reflect.get(Object(value), "path");
|
|
18
|
+
const digest = Reflect.get(Object(value), "digest");
|
|
19
|
+
return typeof path === "string" && path && typeof digest === "string" && digest ? {
|
|
20
|
+
digest,
|
|
21
|
+
path
|
|
22
|
+
} : void 0;
|
|
23
|
+
}
|
|
24
|
+
function parsed(parse, source) {
|
|
25
|
+
try {
|
|
26
|
+
return derivedFrom(parse(source));
|
|
27
|
+
} catch {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Reads `derivedFrom` of a file: the YAML frontmatter of Markdown (`.md`, `.markdown`, `.mdx`),
|
|
33
|
+
* a YAML file (`.yaml`, `.yml`), or the top-level key of a JSON file (`.json`).
|
|
34
|
+
* Returns undefined for other files and for files without it.
|
|
35
|
+
*/
|
|
36
|
+
function readDerivedFrom(path, bytes) {
|
|
37
|
+
const extension = path.slice(path.lastIndexOf(".") + 1).toLowerCase();
|
|
38
|
+
if (extension !== "json" && extension !== "yaml" && extension !== "yml" && !markdown.has(extension)) return void 0;
|
|
39
|
+
let text;
|
|
40
|
+
try {
|
|
41
|
+
text = new TextDecoder("utf-8", { fatal: true }).decode(bytes);
|
|
42
|
+
} catch {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
if (extension === "json") return parsed(JSON.parse, text);
|
|
46
|
+
if (extension === "yaml" || extension === "yml") return parsed(parse, text);
|
|
47
|
+
const head = frontmatter.exec(text);
|
|
48
|
+
return head ? parsed(parse, head[1]) : void 0;
|
|
49
|
+
}
|
|
50
|
+
/** The derived files of a file set whose source changed or is gone, sorted by path. */
|
|
51
|
+
function staleDerivedFiles(files) {
|
|
52
|
+
const stale = [];
|
|
53
|
+
for (const [path, bytes] of files) {
|
|
54
|
+
const derivedFrom = readDerivedFrom(path, bytes);
|
|
55
|
+
if (!derivedFrom) continue;
|
|
56
|
+
const source = files.get(derivedFrom.path);
|
|
57
|
+
if (!source) {
|
|
58
|
+
stale.push({
|
|
59
|
+
derivedFrom,
|
|
60
|
+
path,
|
|
61
|
+
reason: "missing"
|
|
62
|
+
});
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
const sourceDigest = lineageDigest(source);
|
|
66
|
+
if (sourceDigest !== derivedFrom.digest) stale.push({
|
|
67
|
+
derivedFrom,
|
|
68
|
+
path,
|
|
69
|
+
reason: "changed",
|
|
70
|
+
sourceDigest
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
return stale.sort((left, right) => left.path < right.path ? -1 : 1);
|
|
74
|
+
}
|
|
75
|
+
//#endregion
|
|
76
|
+
export { lineageDigest, readDerivedFrom, staleDerivedFiles };
|
package/dist/loader.d.ts
CHANGED
|
@@ -1,17 +1,16 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { N as WorkspaceContent, Xt as WorkspaceSourceItem, ut as WorkspaceLoader } from "./types-BbtwmBZ5.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 {
|
|
16
|
+
export type { FilesLoaderOptions };
|
package/dist/loader.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { o as isWorkspaceBytes } from "./path--wrT_Azm.js";
|
|
2
|
+
import { t as files } from "./files-BOo4SbwE.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
|
|
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-rQ84J-ij.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-BAgL2dtB.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 };
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import { n as assertWorkspaceDigest, o as workspaceConflictError, s as workspaceError } from "./path-DrSIenjj.js";
|
|
2
|
+
import { o as copyJsonFileMetadata, r as workspaceStoreTarget } from "./target-jWhTdIuF.js";
|
|
3
|
+
import { a as isExcludedWorkspacePath, f as sha256, l as normalizeWorkspacePath, o as isWorkspaceBytes } from "./path--wrT_Azm.js";
|
|
4
|
+
import { t as createWorkspaceGlobMatcher } from "./glob-Cjxms1t7.js";
|
|
5
|
+
//#region ../internal/src/owned-paths.ts
|
|
6
|
+
/** Every Blob path that ViteHub writes for itself starts with this prefix. */
|
|
7
|
+
const vitehubOwnedPrefix = "_vitehub/";
|
|
8
|
+
//#endregion
|
|
9
|
+
//#region src/storage/memory.ts
|
|
10
|
+
function now() {
|
|
11
|
+
return Date.now();
|
|
12
|
+
}
|
|
13
|
+
var MemoryWorkspaceStore = class MemoryWorkspaceStore {
|
|
14
|
+
[workspaceStoreTarget]() {
|
|
15
|
+
return { provider: "memory" };
|
|
16
|
+
}
|
|
17
|
+
#nodes = /* @__PURE__ */ new Map([["", {
|
|
18
|
+
type: "directory",
|
|
19
|
+
mtime: now()
|
|
20
|
+
}]]);
|
|
21
|
+
#meta = /* @__PURE__ */ new Map();
|
|
22
|
+
#baseline;
|
|
23
|
+
#mutationQueue = Promise.resolve();
|
|
24
|
+
fork() {
|
|
25
|
+
const fork = new MemoryWorkspaceStore();
|
|
26
|
+
fork.#nodes = structuredClone(this.#nodes);
|
|
27
|
+
fork.#meta = structuredClone(this.#meta);
|
|
28
|
+
fork.#baseline = structuredClone(this.#baseline);
|
|
29
|
+
return fork;
|
|
30
|
+
}
|
|
31
|
+
async readFile(path) {
|
|
32
|
+
const normalized = normalizeWorkspacePath(path);
|
|
33
|
+
const node = this.#nodes.get(normalized);
|
|
34
|
+
if (!node || node.type !== "file") return void 0;
|
|
35
|
+
return {
|
|
36
|
+
path: normalized,
|
|
37
|
+
content: isWorkspaceBytes(node.content) ? new Uint8Array(node.content) : node.content || "",
|
|
38
|
+
mediaType: node.mediaType,
|
|
39
|
+
metadata: structuredClone(node.metadata)
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
async writeFile(path, file) {
|
|
43
|
+
await this.#mutate(() => this.#writeFile(path, file));
|
|
44
|
+
}
|
|
45
|
+
async writeFileConditional(path, file, ifDigest) {
|
|
46
|
+
await this.#mutate(async () => {
|
|
47
|
+
const normalized = normalizeWorkspacePath(path);
|
|
48
|
+
const current = this.#nodes.get(normalized);
|
|
49
|
+
assertWorkspaceDigest(normalized, ifDigest, current?.type === "file" ? (await this.#entry(normalized, current)).digest : void 0);
|
|
50
|
+
this.#writeFile(normalized, file);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
async list(prefix = "", options = {}) {
|
|
54
|
+
const normalizedPrefix = normalizeWorkspacePath(prefix);
|
|
55
|
+
const result = [];
|
|
56
|
+
for (const [path, node] of this.#nodes) {
|
|
57
|
+
if (!path || path === normalizedPrefix) continue;
|
|
58
|
+
if (isExcludedWorkspacePath(path, options.exclude)) continue;
|
|
59
|
+
if (normalizedPrefix && !path.startsWith(`${normalizedPrefix}/`)) continue;
|
|
60
|
+
if (!options.recursive && normalizeWorkspacePath(path.slice(normalizedPrefix.length)).replace(/^\//, "").includes("/")) continue;
|
|
61
|
+
result.push(await this.#entry(path, node));
|
|
62
|
+
}
|
|
63
|
+
return result.sort((a, b) => a.path.localeCompare(b.path));
|
|
64
|
+
}
|
|
65
|
+
async glob(pattern, options = {}) {
|
|
66
|
+
const { cwd, matches } = createWorkspaceGlobMatcher(pattern, options);
|
|
67
|
+
return (await this.list(cwd, { recursive: true })).filter((entry) => entry.type === "file" && matches(entry.path));
|
|
68
|
+
}
|
|
69
|
+
async stat(path) {
|
|
70
|
+
const normalized = normalizeWorkspacePath(path);
|
|
71
|
+
const node = this.#nodes.get(normalized);
|
|
72
|
+
return node ? {
|
|
73
|
+
...await this.#entry(normalized, node),
|
|
74
|
+
revision: node.revision
|
|
75
|
+
} : void 0;
|
|
76
|
+
}
|
|
77
|
+
async mkdir(path, _options = {}) {
|
|
78
|
+
await this.#mutate(() => {
|
|
79
|
+
const normalized = normalizeWorkspacePath(path);
|
|
80
|
+
const existing = this.#nodes.get(normalized);
|
|
81
|
+
const recursive = _options.recursive ?? true;
|
|
82
|
+
if (existing) {
|
|
83
|
+
if (!recursive || existing.type === "file") throw workspaceError(`[vitehub] Workspace path already exists: ${path}.`);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
if (recursive) this.#ensureParents(normalized);
|
|
87
|
+
else {
|
|
88
|
+
const separator = normalized.lastIndexOf("/");
|
|
89
|
+
const parent = separator === -1 ? "" : normalized.slice(0, separator);
|
|
90
|
+
if (parent && this.#nodes.get(parent)?.type !== "directory") throw workspaceError(`[vitehub] Workspace parent does not exist: ${path}.`);
|
|
91
|
+
}
|
|
92
|
+
this.#nodes.set(normalized, {
|
|
93
|
+
type: "directory",
|
|
94
|
+
mtime: now()
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
async rm(path, options = {}) {
|
|
99
|
+
await this.#mutate(async () => {
|
|
100
|
+
const normalized = normalizeWorkspacePath(path);
|
|
101
|
+
const node = this.#nodes.get(normalized);
|
|
102
|
+
if (options.ifDigest !== void 0) {
|
|
103
|
+
const actual = node?.type === "file" ? (await this.#entry(normalized, node)).digest : void 0;
|
|
104
|
+
if (options.ifDigest === null ? node !== void 0 : actual !== options.ifDigest) throw workspaceConflictError(path, options.ifDigest, actual);
|
|
105
|
+
}
|
|
106
|
+
if (!node) {
|
|
107
|
+
if (options.force) return;
|
|
108
|
+
throw workspaceError(`[vitehub] Workspace path does not exist: ${path}.`);
|
|
109
|
+
}
|
|
110
|
+
if (node.type === "directory" && !options.recursive) {
|
|
111
|
+
for (const key of this.#nodes.keys()) if (key.startsWith(`${normalized}/`)) throw workspaceError(`[vitehub] Workspace directory is not empty: ${path}.`);
|
|
112
|
+
}
|
|
113
|
+
for (const key of this.#nodes.keys()) if (key === normalized || key.startsWith(`${normalized}/`)) this.#nodes.delete(key);
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
async removeEmptyDirectory(path) {
|
|
117
|
+
await this.#mutate(() => {
|
|
118
|
+
const normalized = normalizeWorkspacePath(path);
|
|
119
|
+
if (!normalized) return;
|
|
120
|
+
if (this.#nodes.get(normalized)?.type !== "directory") return;
|
|
121
|
+
for (const key of this.#nodes.keys()) if (key.startsWith(`${normalized}/`)) throw workspaceError(`[vitehub] Workspace directory is not empty: ${path}.`);
|
|
122
|
+
this.#nodes.delete(normalized);
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
async snapshot(options = {}) {
|
|
126
|
+
const snapshot = await this.#createSnapshot(options.name);
|
|
127
|
+
this.#baseline = snapshot;
|
|
128
|
+
return snapshot;
|
|
129
|
+
}
|
|
130
|
+
async diff(options = {}) {
|
|
131
|
+
const from = options.from || this.#baseline;
|
|
132
|
+
const to = await this.#createSnapshot();
|
|
133
|
+
const entries = [];
|
|
134
|
+
const keys = /* @__PURE__ */ new Set([...Object.keys(from?.entries || {}), ...Object.keys(to.entries)]);
|
|
135
|
+
for (const path of [...keys].sort()) {
|
|
136
|
+
const before = from && Object.hasOwn(from.entries, path) ? from.entries[path] : void 0;
|
|
137
|
+
const after = Object.hasOwn(to.entries, path) ? to.entries[path] : void 0;
|
|
138
|
+
if (!before && after) entries.push({
|
|
139
|
+
path,
|
|
140
|
+
type: "added",
|
|
141
|
+
after
|
|
142
|
+
});
|
|
143
|
+
else if (before && !after) entries.push({
|
|
144
|
+
path,
|
|
145
|
+
type: "removed",
|
|
146
|
+
before
|
|
147
|
+
});
|
|
148
|
+
else if (before && after && (before.digest !== after.digest || before.type !== after.type || before.size !== after.size || JSON.stringify(before.metadata) !== JSON.stringify(after.metadata))) entries.push({
|
|
149
|
+
path,
|
|
150
|
+
type: "modified",
|
|
151
|
+
before,
|
|
152
|
+
after
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
return {
|
|
156
|
+
from: from?.id,
|
|
157
|
+
to: to.id,
|
|
158
|
+
entries
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
async getMeta(key) {
|
|
162
|
+
return this.#meta.get(key);
|
|
163
|
+
}
|
|
164
|
+
async setMeta(key, value) {
|
|
165
|
+
this.#meta.set(key, value);
|
|
166
|
+
}
|
|
167
|
+
#writeFile(path, file) {
|
|
168
|
+
file = {
|
|
169
|
+
...file,
|
|
170
|
+
metadata: copyJsonFileMetadata(path, file.metadata)
|
|
171
|
+
};
|
|
172
|
+
const normalized = normalizeWorkspacePath(path);
|
|
173
|
+
if (this.#nodes.get(normalized)?.type === "directory") throw workspaceError(`[vitehub] Cannot write a file over directory: ${normalized}.`);
|
|
174
|
+
this.#ensureParents(normalized);
|
|
175
|
+
this.#nodes.set(normalized, {
|
|
176
|
+
type: "file",
|
|
177
|
+
content: isWorkspaceBytes(file.content) ? new Uint8Array(file.content) : file.content,
|
|
178
|
+
revision: crypto.randomUUID(),
|
|
179
|
+
mediaType: file.mediaType,
|
|
180
|
+
metadata: structuredClone(file.metadata),
|
|
181
|
+
mtime: now()
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
#mutate(operation) {
|
|
185
|
+
const result = this.#mutationQueue.then(operation);
|
|
186
|
+
this.#mutationQueue = result.then(() => void 0, () => void 0);
|
|
187
|
+
return result;
|
|
188
|
+
}
|
|
189
|
+
#ensureParents(path) {
|
|
190
|
+
const parts = normalizeWorkspacePath(path).split("/").filter(Boolean);
|
|
191
|
+
for (let index = 1; index < parts.length; index++) {
|
|
192
|
+
const dir = parts.slice(0, index).join("/");
|
|
193
|
+
const node = this.#nodes.get(dir);
|
|
194
|
+
if (node?.type === "file") throw workspaceError(`[vitehub] Workspace parent is not a directory: ${path}.`);
|
|
195
|
+
if (!node) this.#nodes.set(dir, {
|
|
196
|
+
type: "directory",
|
|
197
|
+
mtime: now()
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
async #entry(path, node) {
|
|
202
|
+
const content = node.content || "";
|
|
203
|
+
const size = node.type === "file" ? typeof content === "string" ? new TextEncoder().encode(content).byteLength : content.byteLength : void 0;
|
|
204
|
+
return {
|
|
205
|
+
path,
|
|
206
|
+
type: node.type,
|
|
207
|
+
size,
|
|
208
|
+
mtime: node.mtime,
|
|
209
|
+
mediaType: node.mediaType,
|
|
210
|
+
metadata: structuredClone(node.metadata),
|
|
211
|
+
digest: node.type === "file" ? node.digest ??= await sha256(content) : void 0
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
async #createSnapshot(name) {
|
|
215
|
+
const entries = Object.create(null);
|
|
216
|
+
for (const [path, node] of this.#nodes) {
|
|
217
|
+
if (!path) continue;
|
|
218
|
+
const entry = await this.#entry(path, node);
|
|
219
|
+
entries[path] = {
|
|
220
|
+
type: entry.type,
|
|
221
|
+
digest: entry.digest,
|
|
222
|
+
metadata: entry.metadata,
|
|
223
|
+
size: entry.size
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
return {
|
|
227
|
+
id: await sha256({
|
|
228
|
+
name,
|
|
229
|
+
entries,
|
|
230
|
+
createdAt: Date.now()
|
|
231
|
+
}),
|
|
232
|
+
name,
|
|
233
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
234
|
+
entries
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
function createMemoryWorkspaceStore() {
|
|
239
|
+
return new MemoryWorkspaceStore();
|
|
240
|
+
}
|
|
241
|
+
/** Internal copy for Store adapters that publish draft mutations only after validation. */
|
|
242
|
+
function forkMemoryWorkspaceStore(store) {
|
|
243
|
+
if (!(store instanceof MemoryWorkspaceStore)) throw workspaceError("[vitehub] Only memory Workspace Stores can be forked.");
|
|
244
|
+
return store.fork();
|
|
245
|
+
}
|
|
246
|
+
//#endregion
|
|
247
|
+
export { forkMemoryWorkspaceStore as n, vitehubOwnedPrefix as r, createMemoryWorkspaceStore as t };
|