@vite-hub/workspace 0.0.3 → 0.0.4

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 (86) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +13 -2
  3. package/dist/{ai-Ce3PtWFW.js → ai-BqCC8BpY.js} +2 -2
  4. package/dist/{ai-nmHdzjCM.d.ts → ai-CjxvGqM0.d.ts} +1 -1
  5. package/dist/ai.d.ts +2 -2
  6. package/dist/ai.js +1 -1
  7. package/dist/{asset-registry-CaG9b7tf.js → asset-registry-8QI3ML3K.js} +1 -1
  8. package/dist/chunk-2rV9d50f.js +35 -0
  9. package/dist/cloudflare.d.ts +1 -1
  10. package/dist/cloudflare.js +1 -1
  11. package/dist/collections.d.ts +2 -2
  12. package/dist/collections.js +1 -1
  13. package/dist/{config-CZLO0m9h.js → config-Bc3c4Su1.js} +213 -120
  14. package/dist/{config-CDoyls2X.js → config-CHLLWklZ.js} +9 -3
  15. package/dist/{config-3I1BnsdO.d.ts → config-DE9xlaIw.d.ts} +1 -1
  16. package/dist/{file-CHfsZpso.js → file-Bc1X7pco.js} +3 -3
  17. package/dist/{files-QRV55OCx.js → files-Q6xZjDHA.js} +20 -9
  18. package/dist/{github-CfA5vpJ5.js → github-DHnjxQIX.js} +24 -15
  19. package/dist/{glob-D0vD_wRY.js → glob-CEZKeZFC.js} +5 -4
  20. package/dist/host-DJH8cuoU.js +1030 -0
  21. package/dist/{hosted-C1-tTTTf.js → hosted-RPeJOCFz.js} +121 -31
  22. package/dist/hosted-vercel-blob-d6C8ND5j.js +23580 -0
  23. package/dist/hosted-vercel-blob.d.ts +1 -1
  24. package/dist/hosted-vercel-blob.js +1 -1
  25. package/dist/hosted.d.ts +1 -1
  26. package/dist/hosted.js +1 -1
  27. package/dist/index.d.ts +42 -51
  28. package/dist/index.js +124 -419
  29. package/dist/lifecycle-LrL3sA4n.js +316 -0
  30. package/dist/loader.d.ts +1 -1
  31. package/dist/loader.js +1 -1
  32. package/dist/metadata-target-B0tOmVW7.js +717 -0
  33. package/dist/mountx.d.ts +1 -1
  34. package/dist/nitro.d.ts +1 -1
  35. package/dist/nitro.js +1 -1
  36. package/dist/nuxt.d.ts +1 -1
  37. package/dist/nuxt.js +8 -2
  38. package/dist/{path-CNI6wKIU.js → path-Cp59koVz.js} +28 -4
  39. package/dist/{provider-C3_elpEh.js → provider-B1yLjCJJ.js} +194 -47
  40. package/dist/providers/github/store.d.ts +1 -1
  41. package/dist/providers/github/store.js +1 -1
  42. package/dist/publish.d.ts +1 -1
  43. package/dist/publish.js +2 -2
  44. package/dist/registry-D3yzt8HQ.js +127 -0
  45. package/dist/{registry-BIQVCBWI.d.ts → registry-Uj6IVxes.d.ts} +3 -3
  46. package/dist/{request-execution-u9mgUXKX.js → request-execution-B-dSFlGB.js} +8 -4
  47. package/dist/runtime/assets.d.ts +1 -1
  48. package/dist/runtime/assets.js +4 -2
  49. package/dist/runtime/empty-assets-registry.d.ts +1 -1
  50. package/dist/runtime/empty-registry.d.ts +1 -1
  51. package/dist/runtime/state.d.ts +4 -4
  52. package/dist/runtime/state.js +10 -7
  53. package/dist/runtime/workspace.d.ts +1 -1
  54. package/dist/runtime/workspace.js +1 -1
  55. package/dist/runtime-type-iEE6BjQJ.js +34 -0
  56. package/dist/runtime.d.ts +42 -7
  57. package/dist/runtime.js +275 -82
  58. package/dist/{search-C4REyHj9.js → search-BpiCF2Tw.js} +2 -2
  59. package/dist/server.d.ts +2 -2
  60. package/dist/server.js +23 -50
  61. package/dist/{shared-jfogBrd5.js → shared-DTR1Hhma.js} +97 -39
  62. package/dist/source-metadata.d.ts +9 -2
  63. package/dist/source-metadata.js +34 -2
  64. package/dist/{state-TTqa3x3Y.d.ts → state-tYcRWEh2.d.ts} +13 -12
  65. package/dist/store-Dt_Jyys3.js +608 -0
  66. package/dist/{sync-DiuDmSGk.js → sync-DnTQwt8r.js} +6 -5
  67. package/dist/test.d.ts +1 -1
  68. package/dist/test.js +1 -1
  69. package/dist/token-CVzURBxC.js +61 -0
  70. package/dist/{types-60Yn149G.d.ts → types-CYuwjJ4V.d.ts} +94 -27
  71. package/dist/{use-CsV3oIxH.js → use-BTNDR5Rd.js} +145 -31
  72. package/dist/{use-CUFSnFCS.d.ts → use-BiHzt0xA.d.ts} +21 -4
  73. package/dist/vite-BptCrpv2.js +4526 -0
  74. package/dist/vite-CtA9guIM.d.ts +66 -0
  75. package/dist/vite.d.ts +2 -50
  76. package/dist/vite.js +2 -2
  77. package/dist/{workspace-C4k92Ow8.js → workspace-Dq08Yn72.js} +224 -529
  78. package/dist/{workspace-66SKyiec.d.ts → workspace-Xddf4G1B.d.ts} +1 -1
  79. package/package.json +15 -18
  80. package/dist/chunk-8H4AJuhK.js +0 -14
  81. package/dist/host-CAnbugpI.js +0 -370
  82. package/dist/hosted-vercel-blob-CWBILDVo.js +0 -257
  83. package/dist/lifecycle-BYoa9hVb.js +0 -176
  84. package/dist/registry-CqyDTW9Y.js +0 -74
  85. package/dist/store-Bs1SLh55.js +0 -414
  86. package/dist/vite-tbfDxtlu.js +0 -2191
package/dist/mountx.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { ht as WorkspaceSession } from "./types-60Yn149G.js";
1
+ import { _t as WorkspaceSession } from "./types-CYuwjJ4V.js";
2
2
  import { FsDriver } from "mountx";
3
3
 
4
4
  //#region src/mountx.d.ts
package/dist/nitro.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- import { NitroConfig, WorkspaceNitroConfigOptions, createWorkspaceNitroConfig } from "./vite.js";
1
+ import { i as WorkspaceNitroConfigOptions, r as NitroConfig, s as createWorkspaceNitroConfig } from "./vite-CtA9guIM.js";
2
2
  export { type NitroConfig, type WorkspaceNitroConfigOptions, createWorkspaceNitroConfig };
package/dist/nitro.js CHANGED
@@ -1,2 +1,2 @@
1
- import { t as createWorkspaceNitroConfig } from "./vite-tbfDxtlu.js";
1
+ import { t as createWorkspaceNitroConfig } from "./vite-BptCrpv2.js";
2
2
  export { createWorkspaceNitroConfig };
package/dist/nuxt.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { nt as WorkspaceModuleOptions } from "./types-60Yn149G.js";
1
+ import { rt as WorkspaceModuleOptions } from "./types-CYuwjJ4V.js";
2
2
 
3
3
  //#region src/nuxt.d.ts
4
4
  interface WorkspaceNuxtModuleOptions extends WorkspaceModuleOptions {}
package/dist/nuxt.js CHANGED
@@ -1,4 +1,4 @@
1
- import { n as hubWorkspace, t as createWorkspaceNitroConfig } from "./vite-tbfDxtlu.js";
1
+ import { n as hubWorkspace, r as setWorkspaceVitePluginHosting, t as createWorkspaceNitroConfig } from "./vite-BptCrpv2.js";
2
2
  //#region src/nuxt.ts
3
3
  function isWorkspaceVitePlugin(value) {
4
4
  return Boolean(value && typeof value === "object" && value.name === "@vite-hub/workspace/vite");
@@ -17,10 +17,16 @@ function viteHubWorkspaceNuxtModule(options = {}, nuxt) {
17
17
  });
18
18
  nuxt.options.vite ??= {};
19
19
  const workspaceOptions = resolveWorkspaceOptions(options, nuxt.options.vite.workspace);
20
+ let resolvedNitroHosting;
20
21
  const plugins = Array.isArray(nuxt.options.vite.plugins) ? nuxt.options.vite.plugins : [];
21
- if (!plugins.some(isWorkspaceVitePlugin)) plugins.push(hubWorkspace(workspaceOptions === false ? void 0 : workspaceOptions));
22
+ if (!plugins.some(isWorkspaceVitePlugin)) plugins.push(hubWorkspace({
23
+ ...workspaceOptions || {},
24
+ hosting: () => resolvedNitroHosting
25
+ }));
26
+ for (const plugin of plugins) if (isWorkspaceVitePlugin(plugin)) setWorkspaceVitePluginHosting(plugin, () => resolvedNitroHosting);
22
27
  nuxt.options.vite.plugins = plugins;
23
28
  nuxt.hook?.("nitro:config", async (nitroConfig) => {
29
+ resolvedNitroHosting = typeof nitroConfig.preset === "string" ? nitroConfig.preset : void 0;
24
30
  const rootDir = nuxt.options.rootDir || process.cwd();
25
31
  const nitro = await createWorkspaceNitroConfig({
26
32
  aliases: nuxt.options.alias,
@@ -1,4 +1,4 @@
1
- import { t as __exportAll } from "./chunk-8H4AJuhK.js";
1
+ import { n as __exportAll } from "./chunk-2rV9d50f.js";
2
2
  import { relative, resolve, sep } from "node:path";
3
3
  import { createHash } from "node:crypto";
4
4
  import { minimatch } from "minimatch";
@@ -7,6 +7,19 @@ import { ViteHubError, getViteHubErrorShape } from "@vite-hub/runtime";
7
7
  function workspaceError(message, options) {
8
8
  return new ViteHubError("WORKSPACE_FAILED", message, options);
9
9
  }
10
+ function workspaceConflict(message, options) {
11
+ return new ViteHubError("WORKSPACE_CONFLICT", message, options);
12
+ }
13
+ function workspaceConflictError(path, expected, actual) {
14
+ return workspaceConflict(`[vitehub] Workspace path changed before the conditional write: ${path}.`, { details: {
15
+ actual,
16
+ expected,
17
+ path
18
+ } });
19
+ }
20
+ function assertWorkspaceDigest(path, expected, actual) {
21
+ if (expected === null ? actual !== void 0 : actual !== expected) throw workspaceConflictError(path, expected, actual);
22
+ }
10
23
  function workspaceNotFoundError(name) {
11
24
  return new ViteHubError("WORKSPACE_NOT_FOUND", `[vitehub] Workspace "${name}" is not registered.`, { details: { name } });
12
25
  }
@@ -16,7 +29,10 @@ function workspacePathError(path) {
16
29
  }
17
30
  function isWorkspaceError(value) {
18
31
  const code = getViteHubErrorShape(value)?.code;
19
- return code === "WORKSPACE_COLLECTION_CURSOR_INVALID" || code === "WORKSPACE_FAILED" || code === "WORKSPACE_NOT_FOUND" || code === "WORKSPACE_PATH_INVALID";
32
+ return code === "WORKSPACE_COLLECTION_CURSOR_INVALID" || code === "WORKSPACE_CONFLICT" || code === "WORKSPACE_FAILED" || code === "WORKSPACE_NOT_FOUND" || code === "WORKSPACE_PATH_INVALID";
33
+ }
34
+ function isWorkspaceConflict(value) {
35
+ return getViteHubErrorShape(value)?.code === "WORKSPACE_CONFLICT";
20
36
  }
21
37
  //#endregion
22
38
  //#region src/core/path.ts
@@ -25,6 +41,7 @@ var path_exports = /* @__PURE__ */ __exportAll({
25
41
  contentStreamToBytes: () => contentStreamToBytes,
26
42
  contentToBytes: () => contentToBytes,
27
43
  decodeFile: () => decodeFile,
44
+ isExcludedWorkspacePath: () => isExcludedWorkspacePath,
28
45
  matchesAny: () => matchesAny,
29
46
  normalizeSafeWorkspacePath: () => normalizeSafeWorkspacePath,
30
47
  normalizeSafeWorkspacePattern: () => normalizeSafeWorkspacePattern,
@@ -35,13 +52,20 @@ var path_exports = /* @__PURE__ */ __exportAll({
35
52
  function normalizeWorkspacePath(path = "") {
36
53
  return path.replace(/\\/g, "/").replace(/^\/+/, "").replace(/\/+$/, "");
37
54
  }
55
+ function isExcludedWorkspacePath(path, excluded = []) {
56
+ const normalized = normalizeWorkspacePath(path);
57
+ return excluded.some((item) => {
58
+ const excludedPath = normalizeWorkspacePath(item);
59
+ return excludedPath === "" || normalized === excludedPath || normalized.startsWith(`${excludedPath}/`);
60
+ });
61
+ }
38
62
  function normalizeSafeWorkspacePath(path = "", options = {}) {
39
63
  const raw = path.replace(/\\/g, "/");
40
64
  const normalized = normalizeWorkspacePath(path);
41
65
  const parts = normalized.split("/").filter(Boolean);
42
66
  if (!options.allowEmpty && !normalized) throw workspacePathError(path);
43
67
  if (raw.startsWith("/") || parts.some((part) => part === "." || part === "..")) throw workspacePathError(path);
44
- if (!options.allowReserved && (parts[0] === ".git" || parts[0] === ".vitehub")) throw workspacePathError(path);
68
+ if (!options.allowReserved && (parts.some((part) => part.toLowerCase() === ".git") || parts[0] === ".vitehub")) throw workspacePathError(path);
45
69
  return normalized;
46
70
  }
47
71
  function normalizeSafeWorkspacePattern(pattern) {
@@ -112,4 +136,4 @@ async function sha256(input) {
112
136
  return createHash("sha256").update(bytes).digest("hex");
113
137
  }
114
138
  //#endregion
115
- export { matchesAny as a, normalizeWorkspacePath as c, sha256 as d, isWorkspaceError as f, decodeFile as i, path_exports as l, workspaceNotFoundError as m, contentStreamToBytes as n, normalizeSafeWorkspacePath as o, workspaceError as p, contentToBytes as r, normalizeSafeWorkspacePattern as s, contentStreamChunks as t, resolveInside as u };
139
+ export { workspaceError as _, isExcludedWorkspacePath as a, normalizeSafeWorkspacePattern as c, resolveInside as d, sha256 as f, workspaceConflict as g, isWorkspaceError as h, decodeFile as i, normalizeWorkspacePath as l, isWorkspaceConflict as m, contentStreamToBytes as n, matchesAny as o, assertWorkspaceDigest as p, contentToBytes as r, normalizeSafeWorkspacePath as s, contentStreamChunks as t, path_exports as u, workspaceNotFoundError as v };
@@ -1,11 +1,13 @@
1
- import { a as matchesAny, c as normalizeWorkspacePath, d as sha256, p as workspaceError, r as contentToBytes, t as contentStreamChunks, u as resolveInside } from "./path-CNI6wKIU.js";
2
- import { t as createGitHubWorkspaceStore } from "./store-Bs1SLh55.js";
1
+ import { _ as workspaceError, a as isExcludedWorkspacePath, d as resolveInside, f as sha256, l as normalizeWorkspacePath, o as matchesAny, p as assertWorkspaceDigest, r as contentToBytes, t as contentStreamChunks } from "./path-Cp59koVz.js";
2
+ import { t as createGitHubWorkspaceStore } from "./store-Dt_Jyys3.js";
3
+ import { r as workspaceStoreTarget } from "./target-D4Bqb1eH.js";
3
4
  import { resolve } from "node:path";
4
5
  import { createHash, randomUUID } from "node:crypto";
5
6
  import { defu } from "defu";
6
7
  import { createReadStream, createWriteStream } from "node:fs";
7
8
  import { Readable, Transform } from "node:stream";
8
9
  import { pipeline } from "node:stream/promises";
10
+ import { setTimeout } from "node:timers/promises";
9
11
  //#region ../internal/src/env.ts
10
12
  function trimmed(value) {
11
13
  return typeof value === "string" && value.trim().length > 0 ? value.trim() : void 0;
@@ -44,17 +46,102 @@ function getWorkspaceHostedStoreLoader() {
44
46
  }
45
47
  //#endregion
46
48
  //#region src/storage/local.ts
47
- async function walk(root, current = root) {
49
+ async function withFilesystemLock(lock, description, operation) {
50
+ const { mkdir, open, readFile, rename, rm, stat } = await import("node:fs/promises");
51
+ const { dirname } = await import("node:path");
52
+ const owner = randomUUID();
53
+ const ownerPath = `${lock}/owner`;
54
+ await mkdir(dirname(lock), { recursive: true });
55
+ const deadline = Date.now() + 1e4;
56
+ while (true) try {
57
+ await mkdir(lock);
58
+ const ownerFile = await open(ownerPath, "wx");
59
+ await ownerFile.writeFile(owner);
60
+ await ownerFile.close();
61
+ break;
62
+ } catch (error) {
63
+ if (Reflect.get(Object(error), "code") !== "EEXIST") throw error;
64
+ const info = await stat(lock).catch(() => void 0);
65
+ if (info && Date.now() - info.mtimeMs > 3e5) {
66
+ const stale = `${lock}.stale-${randomUUID()}`;
67
+ if (await rename(lock, stale).then(() => true, (renameError) => {
68
+ if (renameError.code === "ENOENT") return false;
69
+ throw renameError;
70
+ })) await rm(stale, {
71
+ force: true,
72
+ recursive: true
73
+ });
74
+ } else if (Date.now() >= deadline) throw workspaceError(`[vitehub] Timed out waiting to write Workspace ${description}.`);
75
+ else await setTimeout(25);
76
+ }
77
+ try {
78
+ return await operation();
79
+ } finally {
80
+ if (await readFile(ownerPath, "utf8").catch(() => void 0) === owner) await rm(lock, {
81
+ force: true,
82
+ recursive: true
83
+ });
84
+ }
85
+ }
86
+ async function withFilesystemReadLock(lock, description, operation) {
87
+ const { mkdir, open, rm } = await import("node:fs/promises");
88
+ const reader = `${lock}.readers/${randomUUID()}`;
89
+ await withFilesystemLock(`${lock}.gate`, description, async () => {
90
+ await mkdir(`${lock}.readers`, { recursive: true });
91
+ await (await open(reader, "wx")).close();
92
+ });
93
+ try {
94
+ return await operation();
95
+ } finally {
96
+ await rm(reader, { force: true });
97
+ }
98
+ }
99
+ async function withFilesystemWriteLock(lock, description, operation) {
100
+ const { readdir, rm, stat } = await import("node:fs/promises");
101
+ return await withFilesystemLock(`${lock}.gate`, description, async () => {
102
+ const readers = `${lock}.readers`;
103
+ const deadline = Date.now() + 1e4;
104
+ while (true) {
105
+ const active = await readdir(readers).catch((error) => {
106
+ if (error.code === "ENOENT") return [];
107
+ throw error;
108
+ });
109
+ if (active.length === 0) return await operation();
110
+ for (const owner of active) {
111
+ const path = `${readers}/${owner}`;
112
+ const info = await stat(path).catch(() => void 0);
113
+ if (info && Date.now() - info.mtimeMs > 3e5) await rm(path, { force: true });
114
+ }
115
+ if (Date.now() >= deadline) throw workspaceError(`[vitehub] Timed out waiting to write Workspace ${description}.`);
116
+ await setTimeout(25);
117
+ }
118
+ });
119
+ }
120
+ async function withWorkspacePathLock(root, path, operation) {
121
+ const parts = normalizeWorkspacePath(path).split("/").filter(Boolean);
122
+ const paths = parts.map((_, index) => parts.slice(0, index + 1).join("/"));
123
+ const lock = async (index) => {
124
+ if (index === paths.length) return await operation();
125
+ const lockedPath = paths[index];
126
+ const lockPath = `${root}.vitehub-locks/${createHash("sha256").update(lockedPath).digest("hex")}`;
127
+ const next = () => lock(index + 1);
128
+ return index === paths.length - 1 ? await withFilesystemWriteLock(lockPath, `path: ${lockedPath}.`, next) : await withFilesystemReadLock(lockPath, `path: ${lockedPath}.`, next);
129
+ };
130
+ return await lock(0);
131
+ }
132
+ async function walk(root, current = root, excluded = [], recursive = true, includeDigest = false) {
48
133
  const { readdir } = await import("node:fs/promises");
49
134
  const { relative } = await import("node:path");
50
135
  const entries = [];
51
136
  const dirents = await readdir(current, { withFileTypes: true }).catch((error) => {
52
- if (error.code === "ENOENT") return [];
137
+ if (error.code === "ENOENT" || error.code === "ENOTDIR") return [];
53
138
  throw error;
54
139
  });
55
140
  for (const dirent of dirents) {
56
141
  const absolute = `${current}/${dirent.name}`;
57
142
  const path = normalizeWorkspacePath(relative(root, absolute));
143
+ if (path === ".vitehub" || path.startsWith(".vitehub/")) continue;
144
+ if (isExcludedWorkspacePath(path, excluded)) continue;
58
145
  const { stat } = await import("node:fs/promises");
59
146
  const info = await stat(absolute).catch((error) => {
60
147
  if (error.code === "ENOENT") return void 0;
@@ -67,16 +154,19 @@ async function walk(root, current = root) {
67
154
  type: "directory",
68
155
  mtime: info.mtimeMs
69
156
  });
70
- entries.push(...await walk(root, absolute));
157
+ if (recursive) entries.push(...await walk(root, absolute, excluded, true, includeDigest));
71
158
  continue;
72
159
  }
73
- if (dirent.isFile()) entries.push({
74
- path,
75
- type: "file",
76
- size: info.size,
77
- mtime: info.mtimeMs,
78
- digest: await fileDigest(absolute)
79
- });
160
+ if (dirent.isFile()) {
161
+ const entry = {
162
+ path,
163
+ type: "file",
164
+ size: info.size,
165
+ mtime: info.mtimeMs
166
+ };
167
+ if (includeDigest) entry.digest = await fileDigest(absolute);
168
+ entries.push(entry);
169
+ }
80
170
  }
81
171
  return entries;
82
172
  }
@@ -117,9 +207,22 @@ var LocalWorkspaceStore = class {
117
207
  };
118
208
  }
119
209
  async writeFile(path, file) {
210
+ await withWorkspacePathLock(this.root, path, () => this.#writeFile(path, file));
211
+ }
212
+ async writeFileConditional(path, file, ifDigest) {
213
+ await withWorkspacePathLock(this.root, path, async () => {
214
+ const normalized = normalizeWorkspacePath(path);
215
+ const current = await this.stat(normalized);
216
+ assertWorkspaceDigest(normalized, ifDigest, current?.type === "file" ? current.digest : void 0);
217
+ await this.#writeFile(normalized, file);
218
+ });
219
+ }
220
+ async #writeFile(path, file) {
120
221
  const { dirname } = await import("node:path");
121
- const { mkdir, writeFile } = await import("node:fs/promises");
222
+ const { mkdir, rename, rm, writeFile } = await import("node:fs/promises");
122
223
  const absolute = resolveInside(this.root, path);
224
+ const tempRoot = `${this.root}/.vitehub/tmp`;
225
+ const temp = `${tempRoot}/${randomUUID()}.tmp`;
123
226
  const normalized = normalizeWorkspacePath(path);
124
227
  const bytes = contentToBytes(file.content);
125
228
  const digest = await sha256(bytes);
@@ -131,22 +234,32 @@ var LocalWorkspaceStore = class {
131
234
  });
132
235
  return;
133
236
  }
134
- await mkdir(dirname(absolute), { recursive: true });
135
- await writeFile(absolute, bytes);
237
+ await Promise.all([mkdir(dirname(absolute), { recursive: true }), mkdir(tempRoot, { recursive: true })]);
238
+ try {
239
+ await writeFile(temp, bytes);
240
+ await rename(temp, absolute);
241
+ } catch (error) {
242
+ await rm(temp, { force: true }).catch(() => void 0);
243
+ throw error;
244
+ }
136
245
  this.#files.set(normalized, {
137
246
  mediaType: file.mediaType,
138
247
  metadata: file.metadata
139
248
  });
140
249
  }
141
250
  async writeFileStream(path, file) {
251
+ return await withWorkspacePathLock(this.root, path, () => this.#writeFileStream(path, file));
252
+ }
253
+ async #writeFileStream(path, file) {
142
254
  const { dirname } = await import("node:path");
143
255
  const { mkdir, rename, rm } = await import("node:fs/promises");
144
256
  const normalized = normalizeWorkspacePath(path);
145
257
  const absolute = resolveInside(this.root, path);
146
- const temp = `${absolute}.${process.pid}.${Date.now()}.tmp`;
258
+ const tempRoot = `${this.root}/.vitehub/tmp`;
259
+ const temp = `${tempRoot}/${randomUUID()}.tmp`;
147
260
  const hash = createHash("sha256");
148
261
  let size = 0;
149
- await mkdir(dirname(absolute), { recursive: true });
262
+ await Promise.all([mkdir(dirname(absolute), { recursive: true }), mkdir(tempRoot, { recursive: true })]);
150
263
  try {
151
264
  const hashing = new Transform({ transform(chunk, _encoding, callback) {
152
265
  hash.update(chunk);
@@ -189,8 +302,12 @@ var LocalWorkspaceStore = class {
189
302
  }
190
303
  }
191
304
  async list(prefix = "", options = {}) {
305
+ return await this.#list(prefix, options, false);
306
+ }
307
+ async #list(prefix, options, includeDigest) {
192
308
  const normalizedPrefix = normalizeWorkspacePath(prefix);
193
- return (await walk(this.root)).filter((entry) => {
309
+ const current = normalizedPrefix ? resolveInside(this.root, normalizedPrefix) : this.root;
310
+ return (await walk(this.root, current, options.exclude, options.recursive === true, includeDigest)).filter((entry) => {
194
311
  if (!normalizedPrefix) return options.recursive || !entry.path.includes("/");
195
312
  if (entry.path === normalizedPrefix) return false;
196
313
  if (!entry.path.startsWith(`${normalizedPrefix}/`)) return false;
@@ -229,6 +346,9 @@ var LocalWorkspaceStore = class {
229
346
  await mkdir(resolveInside(this.root, path), { recursive: options.recursive ?? true });
230
347
  }
231
348
  async rm(path, options = {}) {
349
+ await withWorkspacePathLock(this.root, path, () => this.#rm(path, options));
350
+ }
351
+ async #rm(path, options = {}) {
232
352
  const { rm } = await import("node:fs/promises");
233
353
  const normalized = normalizeWorkspacePath(path);
234
354
  await rm(resolveInside(this.root, path), {
@@ -289,7 +409,7 @@ var LocalWorkspaceStore = class {
289
409
  }
290
410
  async #createSnapshot(name) {
291
411
  const entries = {};
292
- for (const entry of await this.list("", { recursive: true })) entries[entry.path] = {
412
+ for (const entry of await this.#list("", { recursive: true }, true)) entries[entry.path] = {
293
413
  type: entry.type,
294
414
  digest: entry.digest,
295
415
  metadata: entry.metadata,
@@ -316,8 +436,8 @@ var LocalWorkspaceStore = class {
316
436
  });
317
437
  if (!content) return;
318
438
  const value = JSON.parse(content);
319
- if (!value || typeof value !== "object" || Array.isArray(value)) return;
320
- this.#meta = new Map(Object.entries(value));
439
+ if (!value || Object(value) !== value || Array.isArray(value)) return;
440
+ this.#meta = new Map(Object.entries(Object(value)));
321
441
  }
322
442
  async #writeMeta() {
323
443
  const { dirname } = await import("node:path");
@@ -343,12 +463,16 @@ function now() {
343
463
  return Date.now();
344
464
  }
345
465
  var MemoryWorkspaceStore = class {
466
+ [workspaceStoreTarget]() {
467
+ return { provider: "memory" };
468
+ }
346
469
  #nodes = new Map([["", {
347
470
  type: "directory",
348
471
  mtime: now()
349
472
  }]]);
350
473
  #meta = /* @__PURE__ */ new Map();
351
474
  #baseline;
475
+ #mutationQueue = Promise.resolve();
352
476
  async readFile(path) {
353
477
  const normalized = normalizeWorkspacePath(path);
354
478
  const node = this.#nodes.get(normalized);
@@ -361,14 +485,14 @@ var MemoryWorkspaceStore = class {
361
485
  };
362
486
  }
363
487
  async writeFile(path, file) {
364
- const normalized = normalizeWorkspacePath(path);
365
- this.#ensureParents(normalized);
366
- this.#nodes.set(normalized, {
367
- type: "file",
368
- content: file.content,
369
- mediaType: file.mediaType,
370
- metadata: file.metadata,
371
- mtime: now()
488
+ await this.#mutate(() => this.#writeFile(path, file));
489
+ }
490
+ async writeFileConditional(path, file, ifDigest) {
491
+ await this.#mutate(async () => {
492
+ const normalized = normalizeWorkspacePath(path);
493
+ const current = this.#nodes.get(normalized);
494
+ assertWorkspaceDigest(normalized, ifDigest, current?.type === "file" ? (await this.#entry(normalized, current)).digest : void 0);
495
+ this.#writeFile(normalized, file);
372
496
  });
373
497
  }
374
498
  async list(prefix = "", options = {}) {
@@ -376,6 +500,7 @@ var MemoryWorkspaceStore = class {
376
500
  const result = [];
377
501
  for (const [path, node] of this.#nodes) {
378
502
  if (!path || path === normalizedPrefix) continue;
503
+ if (isExcludedWorkspacePath(path, options.exclude)) continue;
379
504
  if (normalizedPrefix && !path.startsWith(`${normalizedPrefix}/`)) continue;
380
505
  if (!options.recursive && normalizeWorkspacePath(path.slice(normalizedPrefix.length)).replace(/^\//, "").includes("/")) continue;
381
506
  result.push(await this.#entry(path, node));
@@ -393,24 +518,28 @@ var MemoryWorkspaceStore = class {
393
518
  return node ? await this.#entry(normalized, node) : void 0;
394
519
  }
395
520
  async mkdir(path, _options = {}) {
396
- const normalized = normalizeWorkspacePath(path);
397
- this.#ensureParents(normalized);
398
- this.#nodes.set(normalized, {
399
- type: "directory",
400
- mtime: now()
521
+ await this.#mutate(() => {
522
+ const normalized = normalizeWorkspacePath(path);
523
+ this.#ensureParents(normalized);
524
+ this.#nodes.set(normalized, {
525
+ type: "directory",
526
+ mtime: now()
527
+ });
401
528
  });
402
529
  }
403
530
  async rm(path, options = {}) {
404
- const normalized = normalizeWorkspacePath(path);
405
- const node = this.#nodes.get(normalized);
406
- if (!node) {
407
- if (options.force) return;
408
- throw workspaceError(`[vitehub] Workspace path does not exist: ${path}.`);
409
- }
410
- if (node.type === "directory" && !options.recursive) {
411
- for (const key of this.#nodes.keys()) if (key.startsWith(`${normalized}/`)) throw workspaceError(`[vitehub] Workspace directory is not empty: ${path}.`);
412
- }
413
- for (const key of this.#nodes.keys()) if (key === normalized || key.startsWith(`${normalized}/`)) this.#nodes.delete(key);
531
+ await this.#mutate(() => {
532
+ const normalized = normalizeWorkspacePath(path);
533
+ const node = this.#nodes.get(normalized);
534
+ if (!node) {
535
+ if (options.force) return;
536
+ throw workspaceError(`[vitehub] Workspace path does not exist: ${path}.`);
537
+ }
538
+ if (node.type === "directory" && !options.recursive) {
539
+ for (const key of this.#nodes.keys()) if (key.startsWith(`${normalized}/`)) throw workspaceError(`[vitehub] Workspace directory is not empty: ${path}.`);
540
+ }
541
+ for (const key of this.#nodes.keys()) if (key === normalized || key.startsWith(`${normalized}/`)) this.#nodes.delete(key);
542
+ });
414
543
  }
415
544
  async snapshot(options = {}) {
416
545
  const snapshot = await this.#createSnapshot(options.name);
@@ -454,6 +583,22 @@ var MemoryWorkspaceStore = class {
454
583
  async setMeta(key, value) {
455
584
  this.#meta.set(key, value);
456
585
  }
586
+ #writeFile(path, file) {
587
+ const normalized = normalizeWorkspacePath(path);
588
+ this.#ensureParents(normalized);
589
+ this.#nodes.set(normalized, {
590
+ type: "file",
591
+ content: file.content,
592
+ mediaType: file.mediaType,
593
+ metadata: file.metadata,
594
+ mtime: now()
595
+ });
596
+ }
597
+ #mutate(operation) {
598
+ const result = this.#mutationQueue.then(operation);
599
+ this.#mutationQueue = result.then(() => void 0, () => void 0);
600
+ return result;
601
+ }
457
602
  #ensureParents(path) {
458
603
  const parts = normalizeWorkspacePath(path).split("/").filter(Boolean);
459
604
  for (let index = 1; index < parts.length; index++) {
@@ -537,12 +682,14 @@ function resolveVercelBlobWorkspaceStore(config = {}, env = process.env) {
537
682
  };
538
683
  }
539
684
  function resolveGitHubWorkspaceStore(config = {}, env = process.env, input = {}) {
685
+ const branch = gitHubWorkspaceOption(config.branch) ?? readEnv(env, "WORKSPACE_GITHUB_BRANCH", "VITEHUB_WORKSPACE_GITHUB_BRANCH", "GITHUB_BRANCH");
686
+ const root = gitHubWorkspaceOption(config.root) ?? readEnv(env, "WORKSPACE_GITHUB_ROOT", "VITEHUB_WORKSPACE_GITHUB_ROOT");
540
687
  return {
541
- branch: gitHubWorkspaceOption(config.branch) ?? readEnv(env, "WORKSPACE_GITHUB_BRANCH", "VITEHUB_WORKSPACE_GITHUB_BRANCH", "GITHUB_BRANCH") ?? "main",
688
+ branch: branch ?? (input.runtime ? void 0 : "main"),
542
689
  provider: "github",
543
690
  repository: gitHubWorkspaceOption(config.repository) ?? gitHubWorkspaceOption(config.repo) ?? readEnv(env, "WORKSPACE_GITHUB_REPOSITORY", "VITEHUB_WORKSPACE_GITHUB_REPOSITORY", "GITHUB_REPOSITORY"),
544
- root: gitHubWorkspaceOption(config.root) ?? readEnv(env, "WORKSPACE_GITHUB_ROOT", "VITEHUB_WORKSPACE_GITHUB_ROOT") ?? ".vitehub/workspaces/<workspace>",
545
- token: input.runtime ? gitHubWorkspaceOption(config.token) ?? "********" : MASKED_WORKSPACE_RUNTIME_VALUE
691
+ root: root ?? (input.runtime ? void 0 : ".vitehub/workspaces/<workspace>"),
692
+ token: input.runtime ? gitHubWorkspaceOption(config.token) ?? readEnv(env, "WORKSPACE_GITHUB_TOKEN", "VITEHUB_WORKSPACE_GITHUB_TOKEN", "GITHUB_TOKEN", "GH_TOKEN") ?? "********" : MASKED_WORKSPACE_RUNTIME_VALUE
546
693
  };
547
694
  }
548
695
  function hasVercelWorkspaceBlobEnv(env) {
@@ -1,4 +1,4 @@
1
- import { Jt as WorkspaceStore, o as GitHubWorkspaceStoreOptions } from "../../types-60Yn149G.js";
1
+ import { $t as WorkspaceStore, o as GitHubWorkspaceStoreOptions } from "../../types-CYuwjJ4V.js";
2
2
 
3
3
  //#region src/providers/github/store.d.ts
4
4
  declare function createGitHubWorkspaceStore(options: GitHubWorkspaceStoreOptions, workspaceName: string): WorkspaceStore;
@@ -1,2 +1,2 @@
1
- import { t as createGitHubWorkspaceStore } from "../../store-Bs1SLh55.js";
1
+ import { t as createGitHubWorkspaceStore } from "../../store-Dt_Jyys3.js";
2
2
  export { createGitHubWorkspaceStore };
package/dist/publish.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { a as GitHubWorkspaceOption, ct as WorkspacePublisher } from "./types-60Yn149G.js";
1
+ import { a as GitHubWorkspaceOption, lt as WorkspacePublisher } from "./types-CYuwjJ4V.js";
2
2
 
3
3
  //#region src/publishers/manifest.d.ts
4
4
  interface ManifestPublisherOptions {
package/dist/publish.js CHANGED
@@ -1,6 +1,6 @@
1
- import { c as normalizeWorkspacePath, p as workspaceError } from "./path-CNI6wKIU.js";
1
+ import { _ as workspaceError, l as normalizeWorkspacePath } from "./path-Cp59koVz.js";
2
2
  import { n as resolveWorkspaceStoreTarget } from "./target-D4Bqb1eH.js";
3
- import { a as isWorkspaceFileEntry, d as resolveGitHubBranchOption, f as resolveGitHubOption, h as resolveGitHubTokenOption, l as readGitHubBranchState, m as resolveGitHubRootOption, p as resolveGitHubRepositoryOption, r as createGitHubFileUpdate, t as commitGitHubChanges, u as requireGitHubOption } from "./shared-jfogBrd5.js";
3
+ import { a as isWorkspaceFileEntry, d as requireGitHubOption, f as resolveGitHubBranchOption, g as resolveGitHubTokenOption, h as resolveGitHubRootOption, l as readGitHubBranchState, m as resolveGitHubRepositoryOption, p as resolveGitHubOption, r as createGitHubFileUpdate, t as commitGitHubChanges } from "./shared-DTR1Hhma.js";
4
4
  import { dirname, resolve } from "node:path";
5
5
  import { mkdir, writeFile } from "node:fs/promises";
6
6
  //#region src/publishers/manifest.ts