@vite-hub/workspace 0.0.2 → 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 (95) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +48 -2
  3. package/dist/{ai-Cztw4eLk.js → ai-BqCC8BpY.js} +15 -4
  4. package/dist/{ai-hFdsvDHC.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 +4 -2
  10. package/dist/cloudflare.js +1 -2
  11. package/dist/collections.d.ts +2 -2
  12. package/dist/collections.js +1 -1
  13. package/dist/{config--r5unwPc.js → config-Bc3c4Su1.js} +213 -120
  14. package/dist/{config-DCQ6y30R.js → config-CHLLWklZ.js} +9 -3
  15. package/dist/{config-Ca5DM8fF.d.ts → config-DE9xlaIw.d.ts} +1 -1
  16. package/dist/{file-B2q9NE18.js → file-Bc1X7pco.js} +9 -10
  17. package/dist/{files-xy0le-_U.js → files-Q6xZjDHA.js} +20 -9
  18. package/dist/{github-lIO6-2sH.js → github-DHnjxQIX.js} +27 -23
  19. package/dist/glob-CEZKeZFC.js +18 -0
  20. package/dist/host-DJH8cuoU.js +1030 -0
  21. package/dist/{artifacts-store-BAV9jZpd.js → hosted-RPeJOCFz.js} +146 -30
  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 -25
  25. package/dist/hosted.d.ts +1 -1
  26. package/dist/hosted.js +1 -29
  27. package/dist/index.d.ts +61 -78
  28. package/dist/index.js +133 -435
  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 +10 -0
  34. package/dist/mountx.js +143 -0
  35. package/dist/nitro.d.ts +1 -1
  36. package/dist/nitro.js +1 -1
  37. package/dist/nuxt.d.ts +1 -1
  38. package/dist/nuxt.js +8 -2
  39. package/dist/{path-CNI6wKIU.js → path-Cp59koVz.js} +28 -4
  40. package/dist/{provider-CeQciesl.js → provider-B1yLjCJJ.js} +194 -47
  41. package/dist/providers/github/store.d.ts +1 -1
  42. package/dist/providers/github/store.js +1 -361
  43. package/dist/publish.d.ts +1 -1
  44. package/dist/publish.js +2 -2
  45. package/dist/registry-D3yzt8HQ.js +127 -0
  46. package/dist/{registry-CoC9JxVt.d.ts → registry-Uj6IVxes.d.ts} +3 -3
  47. package/dist/{request-execution-C8qRcReq.js → request-execution-B-dSFlGB.js} +8 -4
  48. package/dist/runtime/assets.d.ts +1 -1
  49. package/dist/runtime/assets.js +4 -2
  50. package/dist/runtime/empty-assets-registry.d.ts +1 -1
  51. package/dist/runtime/empty-registry.d.ts +1 -1
  52. package/dist/runtime/state.d.ts +4 -4
  53. package/dist/runtime/state.js +10 -7
  54. package/dist/runtime/workspace.d.ts +1 -1
  55. package/dist/runtime/workspace.js +1 -1
  56. package/dist/runtime-options-BsPSv5O7.js +13 -0
  57. package/dist/runtime-type-iEE6BjQJ.js +34 -0
  58. package/dist/runtime.d.ts +42 -7
  59. package/dist/runtime.js +276 -100
  60. package/dist/{search-C4REyHj9.js → search-BpiCF2Tw.js} +2 -2
  61. package/dist/server.d.ts +2 -2
  62. package/dist/server.js +24 -51
  63. package/dist/{shared-BRycRSbo.js → shared-DTR1Hhma.js} +115 -39
  64. package/dist/source-metadata.d.ts +9 -2
  65. package/dist/source-metadata.js +34 -2
  66. package/dist/{state-DSAHEsq6.d.ts → state-tYcRWEh2.d.ts} +13 -12
  67. package/dist/store-Dt_Jyys3.js +608 -0
  68. package/dist/{sync-DO42ypji.js → sync-DnTQwt8r.js} +6 -5
  69. package/dist/test.d.ts +1 -1
  70. package/dist/test.js +1 -1
  71. package/dist/token-CVzURBxC.js +61 -0
  72. package/dist/{types-DHkae7Cb.d.ts → types-CYuwjJ4V.d.ts} +94 -43
  73. package/dist/{use-d_PlAdOu.js → use-BTNDR5Rd.js} +147 -50
  74. package/dist/{use-D16tf8Ph.d.ts → use-BiHzt0xA.d.ts} +21 -4
  75. package/dist/vite-BptCrpv2.js +4526 -0
  76. package/dist/vite-CtA9guIM.d.ts +66 -0
  77. package/dist/vite.d.ts +2 -50
  78. package/dist/vite.js +2 -2
  79. package/dist/{workspace-n4wBmdaM.js → workspace-Dq08Yn72.js} +224 -545
  80. package/dist/{workspace-f2DBLmPZ.d.ts → workspace-Xddf4G1B.d.ts} +1 -1
  81. package/package.json +20 -38
  82. package/dist/chunk-8H4AJuhK.js +0 -14
  83. package/dist/cli.d.ts +0 -31
  84. package/dist/cli.js +0 -303
  85. package/dist/cloudflare-env-CKxUs2fa.js +0 -20
  86. package/dist/glob-DeH0dzZX.js +0 -23
  87. package/dist/host-I-_vDs05.js +0 -370
  88. package/dist/lifecycle-Lhk3dAMt.js +0 -176
  89. package/dist/providers/cloudflare/artifacts-store.d.ts +0 -6
  90. package/dist/providers/cloudflare/artifacts-store.js +0 -2
  91. package/dist/providers/vercel/blob-store.d.ts +0 -6
  92. package/dist/providers/vercel/blob-store.js +0 -235
  93. package/dist/registry-C7DMdBXN.js +0 -74
  94. package/dist/utils-BPdaCyI6.js +0 -55
  95. package/dist/vite-NWC9mFjI.js +0 -1882
@@ -1,7 +1,14 @@
1
- import { a as matchesAny, c as normalizeWorkspacePath, d as sha256, o as normalizeSafeWorkspacePath, p as workspaceError, r as contentToBytes, s as normalizeSafeWorkspacePattern } from "./path-CNI6wKIU.js";
2
- import { n as createSnapshotFromEntries, r as diffSnapshots } from "./utils-BPdaCyI6.js";
3
- import { t as getActiveCloudflareBinding } from "./cloudflare-env-CKxUs2fa.js";
1
+ import { _ as workspaceError, a as isExcludedWorkspacePath, c as normalizeSafeWorkspacePattern, f as sha256, g as workspaceConflict, l as normalizeWorkspacePath, o as matchesAny, p as assertWorkspaceDigest, r as contentToBytes, s as normalizeSafeWorkspacePath } from "./path-Cp59koVz.js";
2
+ import { n as isPlainObject } from "./config-CHLLWklZ.js";
3
+ import { c as getWorkspaceHostedStoreLoader, d as setWorkspaceRuntimeConfig, i as resolveGitHubWorkspaceStore, l as setWorkspaceHostedStoreLoader, r as resolveCloudflareArtifactsStore } from "./provider-B1yLjCJJ.js";
4
+ import { a as createSnapshotFromEntries, o as diffSnapshots, t as createGitHubWorkspaceStore } from "./store-Dt_Jyys3.js";
5
+ import { _ as getActiveCloudflareBinding } from "./shared-DTR1Hhma.js";
6
+ import { resolve } from "node:path";
4
7
  //#region src/storage/memory-fs.ts
8
+ function copyBinaryData(data) {
9
+ if (ArrayBuffer.isView(data)) return Uint8Array.from(new Uint8Array(data.buffer, data.byteOffset, data.byteLength));
10
+ if (Object.prototype.toString.call(data) === "[object ArrayBuffer]") return Uint8Array.from(new Uint8Array(data));
11
+ }
5
12
  var MemoryStats = class {
6
13
  entry;
7
14
  constructor(entry) {
@@ -70,49 +77,61 @@ var MemoryFS = class {
70
77
  }
71
78
  async mkdir(path, options) {
72
79
  const target = this.normalize(path);
73
- if (target === "/") return;
74
- const recursive = typeof options === "object" && Boolean(options?.recursive);
80
+ const recursive = isPlainObject(options) && options.recursive === true;
81
+ if (target === "/") {
82
+ if (recursive) return;
83
+ throw memoryFsError("EEXIST", path);
84
+ }
75
85
  const parent = this.parent(target);
76
86
  if (!this.entries.has(parent)) {
77
- if (!recursive) throw new Error(`ENOENT: ${parent}`);
87
+ if (!recursive) throw memoryFsError("ENOENT", parent);
78
88
  await this.mkdir(parent, { recursive: true });
79
89
  }
80
- if (this.entries.has(target)) return;
90
+ const existing = this.entries.get(target);
91
+ if (existing) {
92
+ if (existing.kind === "dir" && recursive) return;
93
+ throw memoryFsError("EEXIST", path);
94
+ }
95
+ const parentEntry = this.#requireDir(parent);
81
96
  this.entries.set(target, {
82
97
  kind: "dir",
83
98
  children: /* @__PURE__ */ new Set(),
84
99
  mtimeMs: Date.now()
85
100
  });
86
- this.#requireDir(parent).children.add(this.basename(target));
101
+ parentEntry.children.add(this.basename(target));
87
102
  }
88
103
  async writeFile(path, data) {
89
104
  const target = this.normalize(path);
90
- await this.mkdir(this.parent(target), { recursive: true });
91
- const bytes = typeof data === "string" ? this.#encoder.encode(data) : data instanceof Uint8Array ? data : new Uint8Array(data);
105
+ if (this.entries.get(target)?.kind === "dir") throw memoryFsError("EISDIR", path);
106
+ const parentPath = this.parent(target);
107
+ if (!this.entries.has(parentPath)) await this.mkdir(parentPath, { recursive: true });
108
+ const parent = this.#requireDir(parentPath);
109
+ const bytes = copyBinaryData(data) ?? this.#encoder.encode(String(data));
92
110
  this.entries.set(target, {
93
111
  kind: "file",
94
112
  data: bytes,
95
113
  mtimeMs: Date.now()
96
114
  });
97
- this.#requireDir(this.parent(target)).children.add(this.basename(target));
115
+ parent.children.add(this.basename(target));
98
116
  }
99
117
  async readFile(path, options) {
100
118
  const entry = this.#requireEntry(path);
101
- if (entry.kind !== "file") throw new Error(`EISDIR: ${path}`);
102
- return (typeof options === "string" ? options : options?.encoding) ? this.#decoder.decode(entry.data) : entry.data;
119
+ if (entry.kind !== "file") throw memoryFsError("EISDIR", path);
120
+ return (isPlainObject(options) ? options.encoding : options) ? this.#decoder.decode(entry.data) : Uint8Array.from(entry.data);
103
121
  }
104
122
  async readdir(path) {
105
123
  return [...this.#requireDir(path).children].sort();
106
124
  }
107
125
  async unlink(path) {
108
126
  const target = this.normalize(path);
109
- if (this.#requireEntry(target).kind !== "file") throw new Error(`EISDIR: ${path}`);
127
+ if (this.#requireEntry(target).kind !== "file") throw memoryFsError("EISDIR", path);
110
128
  this.entries.delete(target);
111
129
  this.#requireDir(this.parent(target)).children.delete(this.basename(target));
112
130
  }
113
131
  async rmdir(path) {
114
132
  const target = this.normalize(path);
115
- if (this.#requireDir(target).children.size) throw new Error(`ENOTEMPTY: ${path}`);
133
+ if (target === "/") throw memoryFsError("EBUSY", path);
134
+ if (this.#requireDir(target).children.size) throw memoryFsError("ENOTEMPTY", path);
116
135
  this.entries.delete(target);
117
136
  this.#requireDir(this.parent(target)).children.delete(this.basename(target));
118
137
  }
@@ -148,7 +167,7 @@ var MemoryFS = class {
148
167
  }
149
168
  #requireDir(path) {
150
169
  const entry = this.#requireEntry(path);
151
- if (entry.kind !== "dir") throw new Error(`ENOTDIR: ${path}`);
170
+ if (entry.kind !== "dir") throw memoryFsError("ENOTDIR", path);
152
171
  return entry;
153
172
  }
154
173
  };
@@ -160,6 +179,9 @@ function memoryFsError(code, path) {
160
179
  const dir = "/workspace";
161
180
  const fileMetadataPath = ".vitehub/files.json";
162
181
  const tokenRefreshWindow = 6e4;
182
+ function sameSnapshotEntry(left, right) {
183
+ return left?.digest === right?.digest && left?.type === right?.type && left?.size === right?.size && JSON.stringify(left?.metadata) === JSON.stringify(right?.metadata);
184
+ }
163
185
  function tokenSecret(token) {
164
186
  return token.split("?expires=")[0] || token;
165
187
  }
@@ -219,6 +241,9 @@ var CloudflareArtifactsWorkspaceStore = class {
219
241
  async readFile(path) {
220
242
  const normalized = normalizeSafeWorkspacePath(path);
221
243
  await this.#ensure();
244
+ return await this.#mutate(() => this.#readFile(normalized));
245
+ }
246
+ async #readFile(normalized) {
222
247
  const filepath = this.#absolute(normalized);
223
248
  const content = await this.#fs.promises.readFile(filepath).catch(() => void 0);
224
249
  if (!content) return void 0;
@@ -231,21 +256,22 @@ var CloudflareArtifactsWorkspaceStore = class {
231
256
  };
232
257
  }
233
258
  async writeFile(path, file) {
259
+ const normalized = normalizeSafeWorkspacePath(path);
260
+ await this.#ensure();
261
+ await this.#mutate(() => this.#writeFile(normalized, file));
262
+ }
263
+ async writeFileConditional(path, file, ifDigest) {
234
264
  const normalized = normalizeSafeWorkspacePath(path);
235
265
  await this.#ensure();
236
266
  await this.#mutate(async () => {
237
- await this.#fs.promises.writeFile(this.#absolute(normalized), contentToBytes(file.content));
238
- if (file.mediaType !== void 0 || file.metadata !== void 0) this.#files.set(normalized, {
239
- mediaType: file.mediaType,
240
- metadata: file.metadata
241
- });
242
- else this.#files.delete(normalized);
243
- await this.#writeFileMetadata();
267
+ const current = await this.#stat(normalized).catch(() => void 0);
268
+ assertWorkspaceDigest(normalized, ifDigest, current?.type === "file" ? current.digest : void 0);
269
+ await this.#writeFile(normalized, file);
244
270
  });
245
271
  }
246
272
  async list(prefix = "", options = {}) {
247
273
  await this.#ensure();
248
- return this.#listEntries(prefix, options);
274
+ return await this.#mutate(() => this.#listEntries(prefix, options));
249
275
  }
250
276
  async #listEntries(prefix = "", options = {}) {
251
277
  const normalizedPrefix = normalizeSafeWorkspacePath(prefix, { allowEmpty: true });
@@ -254,6 +280,7 @@ var CloudflareArtifactsWorkspaceStore = class {
254
280
  if (absolute === dir || !absolute.startsWith(`${dir}/`)) continue;
255
281
  const path = normalizeWorkspacePath(absolute.slice(`${dir}/`.length));
256
282
  if (!path || path === ".git" || path.startsWith(".git/") || path === ".vitehub" || path.startsWith(".vitehub/")) continue;
283
+ if (isExcludedWorkspacePath(path, options.exclude)) continue;
257
284
  if (normalizedPrefix) {
258
285
  if (path === normalizedPrefix) continue;
259
286
  if (!path.startsWith(`${normalizedPrefix}/`)) continue;
@@ -285,6 +312,9 @@ var CloudflareArtifactsWorkspaceStore = class {
285
312
  async stat(path) {
286
313
  const normalized = normalizeSafeWorkspacePath(path);
287
314
  await this.#ensure();
315
+ return await this.#mutate(() => this.#stat(normalized));
316
+ }
317
+ async #stat(normalized) {
288
318
  const stat = await this.#fs.promises.stat(this.#absolute(normalized)).catch(() => void 0);
289
319
  if (!stat) return void 0;
290
320
  const bytes = stat.isFile() ? await this.#fs.promises.readFile(this.#absolute(normalized)) : void 0;
@@ -326,6 +356,55 @@ var CloudflareArtifactsWorkspaceStore = class {
326
356
  await this.#ensure();
327
357
  return this.#mutate(() => this.#createSnapshot(options));
328
358
  }
359
+ async rebase(options = {}) {
360
+ await this.#ensure();
361
+ await this.#mutate(async () => {
362
+ const baseline = this.#baseline;
363
+ const current = await createSnapshotFromEntries(await this.#listEntries("", { recursive: true }));
364
+ const changes = diffSnapshots(baseline, current).entries.filter((change) => change.before?.type === "file" || change.after?.type === "file");
365
+ const local = /* @__PURE__ */ new Map();
366
+ for (const change of changes) local.set(change.path, change.after ? await this.#readFile(change.path) : void 0);
367
+ const previous = {
368
+ baseline: this.#baseline,
369
+ branch: this.#branch,
370
+ files: this.#files,
371
+ fs: this.#fs,
372
+ head: this.#head,
373
+ pendingCommit: this.#pendingCommit,
374
+ remote: this.#remote
375
+ };
376
+ const restore = () => {
377
+ this.#baseline = previous.baseline;
378
+ this.#branch = previous.branch;
379
+ this.#files = previous.files;
380
+ this.#fs = previous.fs;
381
+ this.#head = previous.head;
382
+ this.#pendingCommit = previous.pendingCommit;
383
+ this.#remote = previous.remote;
384
+ };
385
+ try {
386
+ this.#files = /* @__PURE__ */ new Map();
387
+ this.#pendingCommit = void 0;
388
+ await this.#load();
389
+ const remoteChanges = new Map(diffSnapshots(baseline, this.#baseline).entries.map((change) => [change.path, change]));
390
+ const takeRemote = new Set(options.takeRemote || []);
391
+ for (const change of changes) if (remoteChanges.has(change.path) && !takeRemote.has(change.path) && !sameSnapshotEntry(current.entries[change.path], this.#baseline?.entries[change.path])) throw workspaceConflict(`[vitehub] Cloudflare Artifacts Workspace Store rebase conflict: ${change.path} changed locally and remotely.`);
392
+ for (const change of changes) {
393
+ if (remoteChanges.has(change.path) && takeRemote.has(change.path)) continue;
394
+ const file = local.get(change.path);
395
+ if (file) await this.#writeFile(change.path, file);
396
+ else {
397
+ this.#fs.deleteTree(this.#absolute(change.path));
398
+ this.#files.delete(change.path);
399
+ await this.#writeFileMetadata();
400
+ }
401
+ }
402
+ } catch (error) {
403
+ restore();
404
+ throw error;
405
+ }
406
+ });
407
+ }
329
408
  async #createSnapshot(options) {
330
409
  const git = await import("isomorphic-git");
331
410
  const http = await import("isomorphic-git/http/web");
@@ -379,7 +458,7 @@ var CloudflareArtifactsWorkspaceStore = class {
379
458
  url: this.#remote
380
459
  });
381
460
  } catch (error) {
382
- if (isNonFastForward(error)) throw workspaceError(`[vitehub] Workspace "${this.workspaceName}" changed remotely while snapshotting branch "${this.#branch}". Reload the Workspace before retrying.`, { cause: error });
461
+ if (isNonFastForward(error)) throw workspaceConflict(`[vitehub] Workspace "${this.workspaceName}" changed remotely while snapshotting branch "${this.#branch}". Reload the Workspace before retrying.`, { cause: error });
383
462
  throw error;
384
463
  }
385
464
  this.#head = id;
@@ -394,20 +473,33 @@ var CloudflareArtifactsWorkspaceStore = class {
394
473
  }
395
474
  async diff(options = {}) {
396
475
  await this.#ensure();
397
- return diffSnapshots(options.from || this.#baseline, await createSnapshotFromEntries(await this.#listEntries("", { recursive: true })));
476
+ return await this.#mutate(async () => {
477
+ return diffSnapshots(options.from || this.#baseline, await createSnapshotFromEntries(await this.#listEntries("", { recursive: true })));
478
+ });
398
479
  }
399
480
  async getMeta(key) {
400
481
  const path = this.#metaAbsolute(key);
401
482
  await this.#ensure();
402
- const content = await this.#fs.promises.readFile(path).catch(() => void 0);
403
- if (!content) return void 0;
404
- return JSON.parse(new TextDecoder().decode(contentToBytes(content)));
483
+ return await this.#mutate(async () => {
484
+ const content = await this.#fs.promises.readFile(path).catch(() => void 0);
485
+ if (!content) return void 0;
486
+ return JSON.parse(new TextDecoder().decode(contentToBytes(content)));
487
+ });
405
488
  }
406
489
  async setMeta(key, value) {
407
490
  const path = this.#metaAbsolute(key);
408
491
  await this.#ensure();
409
492
  await this.#mutate(() => this.#fs.promises.writeFile(path, contentToBytes(JSON.stringify(value))));
410
493
  }
494
+ async #writeFile(path, file) {
495
+ await this.#fs.promises.writeFile(this.#absolute(path), contentToBytes(file.content));
496
+ if (file.mediaType !== void 0 || file.metadata !== void 0) this.#files.set(path, {
497
+ mediaType: file.mediaType,
498
+ metadata: file.metadata
499
+ });
500
+ else this.#files.delete(path);
501
+ await this.#writeFileMetadata();
502
+ }
411
503
  #absolute(path) {
412
504
  return `${dir}/${path}`;
413
505
  }
@@ -552,4 +644,28 @@ function createCloudflareArtifactsWorkspaceStore(options, workspaceName) {
552
644
  return new CloudflareArtifactsWorkspaceStore(options, workspaceName);
553
645
  }
554
646
  //#endregion
555
- export { createCloudflareArtifactsWorkspaceStore as t };
647
+ //#region src/hosted.ts
648
+ function installHostedWorkspaceRuntime() {
649
+ const existingWorkspaceHostedStoreLoader = getWorkspaceHostedStoreLoader();
650
+ setWorkspaceHostedStoreLoader((storeOptions, workspaceName) => {
651
+ if (storeOptions.provider === "cloudflare-artifacts") return createCloudflareArtifactsWorkspaceStore(storeOptions, workspaceName);
652
+ if (storeOptions.provider === "github") return createGitHubWorkspaceStore(storeOptions, workspaceName);
653
+ if (existingWorkspaceHostedStoreLoader) return existingWorkspaceHostedStoreLoader(storeOptions, workspaceName);
654
+ throw workspaceError("[vitehub] Hosted workspace runtime cannot load unsupported hosted store.");
655
+ });
656
+ }
657
+ function configureHostedWorkspaceRuntime(options = {}) {
658
+ const root = resolve(process.cwd(), options.root || ".vitehub/workspaces");
659
+ const store = options.store?.provider === "github" ? resolveGitHubWorkspaceStore(options.store, options.env || {}, { runtime: true }) : options.store?.provider === "vercel-blob" ? void 0 : resolveCloudflareArtifactsStore(options.store, options.env);
660
+ if (!store) throw workspaceError("[vitehub] Vercel Blob hosted workspace runtime must use @vite-hub/workspace/internal/runtime/hosted-vercel-blob.");
661
+ const config = {
662
+ assets: options.assets,
663
+ root,
664
+ store
665
+ };
666
+ installHostedWorkspaceRuntime();
667
+ setWorkspaceRuntimeConfig(config);
668
+ return config;
669
+ }
670
+ //#endregion
671
+ export { installHostedWorkspaceRuntime as n, createCloudflareArtifactsWorkspaceStore as r, configureHostedWorkspaceRuntime as t };