@vite-hub/workspace 0.0.0 → 0.0.1
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 +89 -1
- package/dist/ai-Bk3LLnit.js +352 -0
- package/dist/ai.d.ts +86 -0
- package/dist/ai.js +2 -0
- package/dist/asset-registry-CMuQbyvl.js +14 -0
- package/dist/chunk-DrSxFLj_.js +14 -0
- package/dist/cloudflare-env-DL94Uvpz.js +12 -0
- package/dist/config-CvS-7sre.js +49 -0
- package/dist/errors-BUL8SoFS.js +21 -0
- package/dist/files-DbsIjBya.js +50 -0
- package/dist/index.d.ts +150 -0
- package/dist/index.js +656 -0
- package/dist/lifecycle-uD82JeGh.js +92 -0
- package/dist/loader.d.ts +17 -0
- package/dist/loader.js +22 -0
- package/dist/module-8LZUMEXC.js +922 -0
- package/dist/nitro.d.ts +31 -0
- package/dist/nitro.js +2 -0
- package/dist/path-BVv9OXhg.js +63 -0
- package/dist/provider-BZuKAV4e.js +494 -0
- package/dist/providers/cloudflare/artifacts-store.d.ts +6 -0
- package/dist/providers/cloudflare/artifacts-store.js +384 -0
- package/dist/providers/vercel/blob-store.d.ts +6 -0
- package/dist/providers/vercel/blob-store.js +176 -0
- package/dist/publish.d.ts +34 -0
- package/dist/publish.js +206 -0
- package/dist/registry-Cnr0NoKv.js +932 -0
- package/dist/registry-D4Qzq26I.d.ts +10 -0
- package/dist/runtime/assets.d.ts +11 -0
- package/dist/runtime/assets.js +138 -0
- package/dist/runtime/empty-assets-registry.d.ts +6 -0
- package/dist/runtime/empty-assets-registry.js +4 -0
- package/dist/runtime/empty-registry.d.ts +6 -0
- package/dist/runtime/empty-registry.js +4 -0
- package/dist/runtime/nitro-plugin.d.ts +6 -0
- package/dist/runtime/nitro-plugin.js +14 -0
- package/dist/runtime/state.d.ts +21 -0
- package/dist/runtime/state.js +12 -0
- package/dist/sandbox-DtGXCWRq.js +198 -0
- package/dist/search-DtaEyjpO.js +39 -0
- package/dist/test.d.ts +2 -0
- package/dist/test.js +2 -0
- package/dist/types-CwNcWTdt.d.ts +378 -0
- package/dist/utils-BtAm_K5P.js +51 -0
- package/dist/vite.d.ts +22 -0
- package/dist/vite.js +131 -0
- package/package.json +101 -5
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
import { t as WorkspaceError } from "../../errors-BUL8SoFS.js";
|
|
2
|
+
import { a as normalizeSafeWorkspacePattern, i as normalizeSafeWorkspacePath, l as sha256, o as normalizeWorkspacePath, r as matchesAny, t as contentToBytes } from "../../path-BVv9OXhg.js";
|
|
3
|
+
import { t as getActiveCloudflareBinding } from "../../cloudflare-env-DL94Uvpz.js";
|
|
4
|
+
import { n as diffSnapshots, t as createSnapshotFromEntries } from "../../utils-BtAm_K5P.js";
|
|
5
|
+
//#region src/storage/memory-fs.ts
|
|
6
|
+
var MemoryStats = class {
|
|
7
|
+
constructor(entry) {
|
|
8
|
+
this.entry = entry;
|
|
9
|
+
}
|
|
10
|
+
get size() {
|
|
11
|
+
return this.entry.kind === "file" ? this.entry.data.byteLength : 0;
|
|
12
|
+
}
|
|
13
|
+
get mtimeMs() {
|
|
14
|
+
return this.entry.mtimeMs;
|
|
15
|
+
}
|
|
16
|
+
get ctimeMs() {
|
|
17
|
+
return this.entry.mtimeMs;
|
|
18
|
+
}
|
|
19
|
+
get mode() {
|
|
20
|
+
return this.entry.kind === "file" ? 33188 : 16384;
|
|
21
|
+
}
|
|
22
|
+
isFile() {
|
|
23
|
+
return this.entry.kind === "file";
|
|
24
|
+
}
|
|
25
|
+
isDirectory() {
|
|
26
|
+
return this.entry.kind === "dir";
|
|
27
|
+
}
|
|
28
|
+
isSymbolicLink() {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
var MemoryFS = class {
|
|
33
|
+
#decoder = new TextDecoder();
|
|
34
|
+
#encoder = new TextEncoder();
|
|
35
|
+
entries = new Map([["/", {
|
|
36
|
+
kind: "dir",
|
|
37
|
+
children: /* @__PURE__ */ new Set(),
|
|
38
|
+
mtimeMs: Date.now()
|
|
39
|
+
}]]);
|
|
40
|
+
promises = {
|
|
41
|
+
lstat: this.lstat.bind(this),
|
|
42
|
+
mkdir: this.mkdir.bind(this),
|
|
43
|
+
readFile: this.readFile.bind(this),
|
|
44
|
+
readdir: this.readdir.bind(this),
|
|
45
|
+
rmdir: this.rmdir.bind(this),
|
|
46
|
+
stat: this.stat.bind(this),
|
|
47
|
+
unlink: this.unlink.bind(this),
|
|
48
|
+
writeFile: this.writeFile.bind(this)
|
|
49
|
+
};
|
|
50
|
+
normalize(input) {
|
|
51
|
+
const segments = [];
|
|
52
|
+
for (const part of input.split("/")) {
|
|
53
|
+
if (!part || part === ".") continue;
|
|
54
|
+
if (part === "..") segments.pop();
|
|
55
|
+
else segments.push(part);
|
|
56
|
+
}
|
|
57
|
+
return segments.length ? `/${segments.join("/")}` : "/";
|
|
58
|
+
}
|
|
59
|
+
parent(path) {
|
|
60
|
+
const normalized = this.normalize(path);
|
|
61
|
+
if (normalized === "/") return "/";
|
|
62
|
+
const parts = normalized.split("/").filter(Boolean);
|
|
63
|
+
parts.pop();
|
|
64
|
+
return parts.length ? `/${parts.join("/")}` : "/";
|
|
65
|
+
}
|
|
66
|
+
basename(path) {
|
|
67
|
+
return this.normalize(path).split("/").filter(Boolean).pop() || "";
|
|
68
|
+
}
|
|
69
|
+
async mkdir(path, options) {
|
|
70
|
+
const target = this.normalize(path);
|
|
71
|
+
if (target === "/") return;
|
|
72
|
+
const recursive = typeof options === "object" && Boolean(options?.recursive);
|
|
73
|
+
const parent = this.parent(target);
|
|
74
|
+
if (!this.entries.has(parent)) {
|
|
75
|
+
if (!recursive) throw new Error(`ENOENT: ${parent}`);
|
|
76
|
+
await this.mkdir(parent, { recursive: true });
|
|
77
|
+
}
|
|
78
|
+
if (this.entries.has(target)) return;
|
|
79
|
+
this.entries.set(target, {
|
|
80
|
+
kind: "dir",
|
|
81
|
+
children: /* @__PURE__ */ new Set(),
|
|
82
|
+
mtimeMs: Date.now()
|
|
83
|
+
});
|
|
84
|
+
this.#requireDir(parent).children.add(this.basename(target));
|
|
85
|
+
}
|
|
86
|
+
async writeFile(path, data) {
|
|
87
|
+
const target = this.normalize(path);
|
|
88
|
+
await this.mkdir(this.parent(target), { recursive: true });
|
|
89
|
+
const bytes = typeof data === "string" ? this.#encoder.encode(data) : data instanceof Uint8Array ? data : new Uint8Array(data);
|
|
90
|
+
this.entries.set(target, {
|
|
91
|
+
kind: "file",
|
|
92
|
+
data: bytes,
|
|
93
|
+
mtimeMs: Date.now()
|
|
94
|
+
});
|
|
95
|
+
this.#requireDir(this.parent(target)).children.add(this.basename(target));
|
|
96
|
+
}
|
|
97
|
+
async readFile(path, options) {
|
|
98
|
+
const entry = this.#requireEntry(path);
|
|
99
|
+
if (entry.kind !== "file") throw new Error(`EISDIR: ${path}`);
|
|
100
|
+
return (typeof options === "string" ? options : options?.encoding) ? this.#decoder.decode(entry.data) : entry.data;
|
|
101
|
+
}
|
|
102
|
+
async readdir(path) {
|
|
103
|
+
return [...this.#requireDir(path).children].sort();
|
|
104
|
+
}
|
|
105
|
+
async unlink(path) {
|
|
106
|
+
const target = this.normalize(path);
|
|
107
|
+
if (this.#requireEntry(target).kind !== "file") throw new Error(`EISDIR: ${path}`);
|
|
108
|
+
this.entries.delete(target);
|
|
109
|
+
this.#requireDir(this.parent(target)).children.delete(this.basename(target));
|
|
110
|
+
}
|
|
111
|
+
async rmdir(path) {
|
|
112
|
+
const target = this.normalize(path);
|
|
113
|
+
if (this.#requireDir(target).children.size) throw new Error(`ENOTEMPTY: ${path}`);
|
|
114
|
+
this.entries.delete(target);
|
|
115
|
+
this.#requireDir(this.parent(target)).children.delete(this.basename(target));
|
|
116
|
+
}
|
|
117
|
+
async stat(path) {
|
|
118
|
+
return new MemoryStats(this.#requireEntry(path));
|
|
119
|
+
}
|
|
120
|
+
async lstat(path) {
|
|
121
|
+
return this.stat(path);
|
|
122
|
+
}
|
|
123
|
+
deleteTree(path) {
|
|
124
|
+
const target = this.normalize(path);
|
|
125
|
+
const removed = [...this.entries.keys()].filter((key) => key === target || key.startsWith(`${target}/`));
|
|
126
|
+
for (const key of removed.sort((a, b) => b.length - a.length)) {
|
|
127
|
+
if (key === "/") continue;
|
|
128
|
+
this.entries.delete(key);
|
|
129
|
+
}
|
|
130
|
+
const parent = this.parent(target);
|
|
131
|
+
const parentEntry = this.entries.get(parent);
|
|
132
|
+
if (parentEntry?.kind === "dir") parentEntry.children.delete(this.basename(target));
|
|
133
|
+
return removed.length;
|
|
134
|
+
}
|
|
135
|
+
#requireEntry(path) {
|
|
136
|
+
const entry = this.entries.get(this.normalize(path));
|
|
137
|
+
if (!entry) throw new Error(`ENOENT: ${path}`);
|
|
138
|
+
return entry;
|
|
139
|
+
}
|
|
140
|
+
#requireDir(path) {
|
|
141
|
+
const entry = this.#requireEntry(path);
|
|
142
|
+
if (entry.kind !== "dir") throw new Error(`ENOTDIR: ${path}`);
|
|
143
|
+
return entry;
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
//#endregion
|
|
147
|
+
//#region src/providers/cloudflare/artifacts-store.ts
|
|
148
|
+
const dir = "/workspace";
|
|
149
|
+
function tokenSecret(token) {
|
|
150
|
+
return token.split("?expires=")[0] || token;
|
|
151
|
+
}
|
|
152
|
+
function repoName(options, workspaceName) {
|
|
153
|
+
return options.repo || `${options.repoPrefix || "vitehub-workspace-"}${workspaceName.replace(/[^a-zA-Z0-9_.-]/g, "-")}`;
|
|
154
|
+
}
|
|
155
|
+
var CloudflareArtifactsWorkspaceStore = class {
|
|
156
|
+
#baseline;
|
|
157
|
+
#fs;
|
|
158
|
+
#repo;
|
|
159
|
+
#token;
|
|
160
|
+
#ready;
|
|
161
|
+
constructor(options, workspaceName) {
|
|
162
|
+
this.options = options;
|
|
163
|
+
this.workspaceName = workspaceName;
|
|
164
|
+
}
|
|
165
|
+
async readFile(path) {
|
|
166
|
+
const normalized = normalizeSafeWorkspacePath(path);
|
|
167
|
+
await this.#ensure();
|
|
168
|
+
const filepath = this.#absolute(normalized);
|
|
169
|
+
const content = await this.#fs.promises.readFile(filepath).catch(() => void 0);
|
|
170
|
+
return content ? {
|
|
171
|
+
path: normalized,
|
|
172
|
+
content
|
|
173
|
+
} : void 0;
|
|
174
|
+
}
|
|
175
|
+
async writeFile(path, file) {
|
|
176
|
+
const normalized = normalizeSafeWorkspacePath(path);
|
|
177
|
+
await this.#ensure();
|
|
178
|
+
await this.#fs.promises.writeFile(this.#absolute(normalized), contentToBytes(file.content));
|
|
179
|
+
}
|
|
180
|
+
async list(prefix = "", options = {}) {
|
|
181
|
+
const normalizedPrefix = normalizeSafeWorkspacePath(prefix, { allowEmpty: true });
|
|
182
|
+
await this.#ensure();
|
|
183
|
+
const entries = [];
|
|
184
|
+
for (const [absolute, entry] of this.#fs.entries) {
|
|
185
|
+
if (absolute === dir || !absolute.startsWith(`${dir}/`)) continue;
|
|
186
|
+
const path = normalizeWorkspacePath(absolute.slice(`${dir}/`.length));
|
|
187
|
+
if (!path || path === ".git" || path.startsWith(".git/") || path.startsWith(".vitehub/")) continue;
|
|
188
|
+
if (normalizedPrefix) {
|
|
189
|
+
if (path === normalizedPrefix) continue;
|
|
190
|
+
if (!path.startsWith(`${normalizedPrefix}/`)) continue;
|
|
191
|
+
}
|
|
192
|
+
if (!options.recursive && path.slice(normalizedPrefix ? normalizedPrefix.length + 1 : 0).includes("/")) continue;
|
|
193
|
+
if (entry.kind === "file") entries.push({
|
|
194
|
+
digest: await sha256(entry.data),
|
|
195
|
+
mtime: entry.mtimeMs,
|
|
196
|
+
path,
|
|
197
|
+
size: entry.data.byteLength,
|
|
198
|
+
type: "file"
|
|
199
|
+
});
|
|
200
|
+
else entries.push({
|
|
201
|
+
mtime: entry.mtimeMs,
|
|
202
|
+
path,
|
|
203
|
+
type: "directory"
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
return entries.sort((a, b) => a.path.localeCompare(b.path));
|
|
207
|
+
}
|
|
208
|
+
async glob(pattern, _options = {}) {
|
|
209
|
+
const patterns = Array.isArray(pattern) ? pattern.map(normalizeSafeWorkspacePattern) : normalizeSafeWorkspacePattern(pattern);
|
|
210
|
+
return (await this.list("", { recursive: true })).filter((entry) => entry.type === "file" && matchesAny(entry.path, patterns));
|
|
211
|
+
}
|
|
212
|
+
async stat(path) {
|
|
213
|
+
const normalized = normalizeSafeWorkspacePath(path);
|
|
214
|
+
await this.#ensure();
|
|
215
|
+
const stat = await this.#fs.promises.stat(this.#absolute(normalized)).catch(() => void 0);
|
|
216
|
+
if (!stat) return void 0;
|
|
217
|
+
const bytes = stat.isFile() ? await this.#fs.promises.readFile(this.#absolute(normalized)) : void 0;
|
|
218
|
+
return {
|
|
219
|
+
digest: bytes ? await sha256(bytes) : void 0,
|
|
220
|
+
mtime: stat.mtimeMs,
|
|
221
|
+
path: normalized,
|
|
222
|
+
size: stat.isFile() ? stat.size : void 0,
|
|
223
|
+
type: stat.isDirectory() ? "directory" : "file"
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
async mkdir(path, options = {}) {
|
|
227
|
+
const normalized = normalizeSafeWorkspacePath(path);
|
|
228
|
+
await this.#ensure();
|
|
229
|
+
await this.#fs.promises.mkdir(this.#absolute(normalized), { recursive: options.recursive ?? true });
|
|
230
|
+
}
|
|
231
|
+
async rm(path, options = {}) {
|
|
232
|
+
const normalized = normalizeSafeWorkspacePath(path);
|
|
233
|
+
await this.#ensure();
|
|
234
|
+
const absolute = this.#absolute(normalized);
|
|
235
|
+
const stat = await this.#fs.promises.stat(absolute).catch(() => void 0);
|
|
236
|
+
if (!stat) {
|
|
237
|
+
if (options.force) return;
|
|
238
|
+
throw new WorkspaceError(`[vitehub] Workspace path does not exist: ${path}.`);
|
|
239
|
+
}
|
|
240
|
+
if (stat.isDirectory() && !options.recursive) {
|
|
241
|
+
if ((await this.#fs.promises.readdir(absolute)).length) throw new WorkspaceError(`[vitehub] Workspace directory is not empty: ${path}.`);
|
|
242
|
+
}
|
|
243
|
+
if (!this.#fs.deleteTree(absolute) && !options.force) throw new WorkspaceError(`[vitehub] Workspace path does not exist: ${path}.`);
|
|
244
|
+
}
|
|
245
|
+
async snapshot(options = {}) {
|
|
246
|
+
await this.#ensure();
|
|
247
|
+
const git = await import("isomorphic-git");
|
|
248
|
+
const http = await import("isomorphic-git/http/web");
|
|
249
|
+
const matrix = await git.statusMatrix({
|
|
250
|
+
dir,
|
|
251
|
+
fs: this.#fs
|
|
252
|
+
});
|
|
253
|
+
let changed = false;
|
|
254
|
+
for (const [filepath, head, workdir, stage] of matrix) {
|
|
255
|
+
if (filepath.startsWith(".git/")) continue;
|
|
256
|
+
if (workdir === 0 && (head !== 0 || stage !== 0)) {
|
|
257
|
+
await git.remove({
|
|
258
|
+
dir,
|
|
259
|
+
filepath,
|
|
260
|
+
fs: this.#fs
|
|
261
|
+
});
|
|
262
|
+
changed = true;
|
|
263
|
+
} else if (workdir !== stage) {
|
|
264
|
+
await git.add({
|
|
265
|
+
dir,
|
|
266
|
+
filepath,
|
|
267
|
+
fs: this.#fs
|
|
268
|
+
});
|
|
269
|
+
changed = true;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
let id;
|
|
273
|
+
if (changed) {
|
|
274
|
+
id = await git.commit({
|
|
275
|
+
author: {
|
|
276
|
+
email: "workspace@vitehub.dev",
|
|
277
|
+
name: "ViteHub Workspace"
|
|
278
|
+
},
|
|
279
|
+
dir,
|
|
280
|
+
fs: this.#fs,
|
|
281
|
+
message: options.name || "Update workspace"
|
|
282
|
+
});
|
|
283
|
+
await git.push({
|
|
284
|
+
dir,
|
|
285
|
+
fs: this.#fs,
|
|
286
|
+
http,
|
|
287
|
+
onAuth: () => ({
|
|
288
|
+
password: this.#token,
|
|
289
|
+
username: "x"
|
|
290
|
+
}),
|
|
291
|
+
ref: this.options.branch || "main",
|
|
292
|
+
url: this.#repo.remote
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
const snapshot = await createSnapshotFromEntries(await this.list("", { recursive: true }), options.name);
|
|
296
|
+
this.#baseline = {
|
|
297
|
+
...snapshot,
|
|
298
|
+
id: id || snapshot.id
|
|
299
|
+
};
|
|
300
|
+
return this.#baseline;
|
|
301
|
+
}
|
|
302
|
+
async diff(options = {}) {
|
|
303
|
+
return diffSnapshots(options.from || this.#baseline, await createSnapshotFromEntries(await this.list("", { recursive: true })));
|
|
304
|
+
}
|
|
305
|
+
async getMeta(key) {
|
|
306
|
+
const path = this.#metaAbsolute(key);
|
|
307
|
+
await this.#ensure();
|
|
308
|
+
const content = await this.#fs.promises.readFile(path).catch(() => void 0);
|
|
309
|
+
if (!content) return void 0;
|
|
310
|
+
return JSON.parse(new TextDecoder().decode(contentToBytes(content)));
|
|
311
|
+
}
|
|
312
|
+
async setMeta(key, value) {
|
|
313
|
+
const path = this.#metaAbsolute(key);
|
|
314
|
+
await this.#ensure();
|
|
315
|
+
await this.#fs.promises.writeFile(path, contentToBytes(JSON.stringify(value)));
|
|
316
|
+
}
|
|
317
|
+
#absolute(path) {
|
|
318
|
+
return `${dir}/${path}`;
|
|
319
|
+
}
|
|
320
|
+
#internalAbsolute(path) {
|
|
321
|
+
return `${dir}/${normalizeWorkspacePath(path)}`;
|
|
322
|
+
}
|
|
323
|
+
#metaAbsolute(key) {
|
|
324
|
+
const normalized = normalizeSafeWorkspacePath(key.endsWith(".json") ? key : `${key}.json`);
|
|
325
|
+
return this.#internalAbsolute(`.vitehub/meta/${normalized}`);
|
|
326
|
+
}
|
|
327
|
+
#getBinding() {
|
|
328
|
+
const bindingName = this.options.binding || "WORKSPACE_ARTIFACTS";
|
|
329
|
+
const binding = getActiveCloudflareBinding(bindingName) || globalThis.__env__?.[bindingName] || globalThis[bindingName];
|
|
330
|
+
if (!binding) throw new WorkspaceError(`[vitehub] Cloudflare Artifacts binding "${bindingName}" not found.`);
|
|
331
|
+
return binding;
|
|
332
|
+
}
|
|
333
|
+
async #ensure() {
|
|
334
|
+
this.#ready ||= this.#load();
|
|
335
|
+
await this.#ready;
|
|
336
|
+
}
|
|
337
|
+
async #load() {
|
|
338
|
+
const git = await import("isomorphic-git");
|
|
339
|
+
const http = await import("isomorphic-git/http/web");
|
|
340
|
+
const binding = this.#getBinding();
|
|
341
|
+
const name = repoName(this.options, this.workspaceName);
|
|
342
|
+
let createdToken;
|
|
343
|
+
try {
|
|
344
|
+
this.#repo = await binding.get(name);
|
|
345
|
+
} catch {
|
|
346
|
+
const created = await binding.create(name, {
|
|
347
|
+
description: `ViteHub workspace ${this.workspaceName}`,
|
|
348
|
+
readOnly: false,
|
|
349
|
+
setDefaultBranch: this.options.branch || "main"
|
|
350
|
+
});
|
|
351
|
+
this.#repo = created;
|
|
352
|
+
createdToken = created.token;
|
|
353
|
+
}
|
|
354
|
+
this.#token = tokenSecret(createdToken || (await this.#repo.createToken("write", 3600)).plaintext);
|
|
355
|
+
this.#fs = new MemoryFS();
|
|
356
|
+
await this.#fs.promises.mkdir(dir, { recursive: true });
|
|
357
|
+
try {
|
|
358
|
+
await git.clone({
|
|
359
|
+
depth: 1,
|
|
360
|
+
dir,
|
|
361
|
+
fs: this.#fs,
|
|
362
|
+
http,
|
|
363
|
+
onAuth: () => ({
|
|
364
|
+
password: this.#token,
|
|
365
|
+
username: "x"
|
|
366
|
+
}),
|
|
367
|
+
ref: this.options.branch || this.#repo.defaultBranch || "main",
|
|
368
|
+
singleBranch: true,
|
|
369
|
+
url: this.#repo.remote
|
|
370
|
+
});
|
|
371
|
+
} catch {
|
|
372
|
+
await git.init({
|
|
373
|
+
defaultBranch: this.options.branch || "main",
|
|
374
|
+
dir,
|
|
375
|
+
fs: this.#fs
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
};
|
|
380
|
+
function createCloudflareArtifactsWorkspaceStore(options, workspaceName) {
|
|
381
|
+
return new CloudflareArtifactsWorkspaceStore(options, workspaceName);
|
|
382
|
+
}
|
|
383
|
+
//#endregion
|
|
384
|
+
export { createCloudflareArtifactsWorkspaceStore };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { b as VercelBlobWorkspaceStoreOptions, ct as WorkspaceStore } from "../../types-CwNcWTdt.js";
|
|
2
|
+
|
|
3
|
+
//#region src/providers/vercel/blob-store.d.ts
|
|
4
|
+
declare function createVercelBlobWorkspaceStore(options: VercelBlobWorkspaceStoreOptions, workspaceName: string): WorkspaceStore;
|
|
5
|
+
//#endregion
|
|
6
|
+
export { createVercelBlobWorkspaceStore };
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { t as WorkspaceError } from "../../errors-BUL8SoFS.js";
|
|
2
|
+
import { a as normalizeSafeWorkspacePattern, i as normalizeSafeWorkspacePath, l as sha256, o as normalizeWorkspacePath, r as matchesAny, t as contentToBytes } from "../../path-BVv9OXhg.js";
|
|
3
|
+
import { r as resolveRuntimeVercelBlobWorkspaceStore } from "../../provider-BZuKAV4e.js";
|
|
4
|
+
import { n as diffSnapshots, t as createSnapshotFromEntries } from "../../utils-BtAm_K5P.js";
|
|
5
|
+
//#region src/providers/vercel/blob-store.ts
|
|
6
|
+
function joinBlobPath(...parts) {
|
|
7
|
+
return parts.map((part) => normalizeWorkspacePath(part)).filter(Boolean).join("/");
|
|
8
|
+
}
|
|
9
|
+
function contentType(path, fallback) {
|
|
10
|
+
if (fallback) return fallback;
|
|
11
|
+
if (path.endsWith(".json")) return "application/json; charset=utf-8";
|
|
12
|
+
if (path.endsWith(".md") || path.endsWith(".txt")) return "text/plain; charset=utf-8";
|
|
13
|
+
}
|
|
14
|
+
async function createVercelFiles(options) {
|
|
15
|
+
const [{ Files }, { vercelBlob }] = await Promise.all([import("files-sdk"), import("files-sdk/vercel-blob")]);
|
|
16
|
+
return new Files({ adapter: vercelBlob({
|
|
17
|
+
access: options.access || "private",
|
|
18
|
+
addRandomSuffix: false,
|
|
19
|
+
allowOverwrite: true,
|
|
20
|
+
token: options.token
|
|
21
|
+
}) });
|
|
22
|
+
}
|
|
23
|
+
var VercelBlobWorkspaceStore = class {
|
|
24
|
+
#baseline;
|
|
25
|
+
#options;
|
|
26
|
+
#files;
|
|
27
|
+
constructor(options, workspaceName) {
|
|
28
|
+
this.workspaceName = workspaceName;
|
|
29
|
+
this.#options = resolveRuntimeVercelBlobWorkspaceStore(options, typeof process !== "undefined" ? process.env : {});
|
|
30
|
+
}
|
|
31
|
+
get #root() {
|
|
32
|
+
return joinBlobPath(this.#options.prefix || ".vitehub/workspaces", this.workspaceName);
|
|
33
|
+
}
|
|
34
|
+
#fileKey(path, options = {}) {
|
|
35
|
+
return joinBlobPath(this.#root, "files", normalizeSafeWorkspacePath(path, { allowEmpty: options.allowEmpty }));
|
|
36
|
+
}
|
|
37
|
+
async #client() {
|
|
38
|
+
this.#files ||= createVercelFiles(this.#options);
|
|
39
|
+
return await this.#files;
|
|
40
|
+
}
|
|
41
|
+
#metaKey(key) {
|
|
42
|
+
return joinBlobPath(this.#root, ".vitehub/meta", normalizeSafeWorkspacePath(key.endsWith(".json") ? key : `${key}.json`));
|
|
43
|
+
}
|
|
44
|
+
#snapshotKey(id) {
|
|
45
|
+
return joinBlobPath(this.#root, ".vitehub/snapshots", `${id}.json`);
|
|
46
|
+
}
|
|
47
|
+
async readFile(path) {
|
|
48
|
+
const normalized = normalizeSafeWorkspacePath(path);
|
|
49
|
+
const pathname = this.#fileKey(normalized);
|
|
50
|
+
const file = await (await this.#client()).download(pathname).catch(() => null);
|
|
51
|
+
if (!file) return void 0;
|
|
52
|
+
const bytes = await file.arrayBuffer();
|
|
53
|
+
return {
|
|
54
|
+
path: normalized,
|
|
55
|
+
content: new Uint8Array(bytes)
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
async writeFile(path, file) {
|
|
59
|
+
const normalized = normalizeSafeWorkspacePath(path);
|
|
60
|
+
await (await this.#client()).upload(this.#fileKey(normalized), new Blob([contentToBytes(file.content)]), { contentType: contentType(normalized, file.mediaType) });
|
|
61
|
+
}
|
|
62
|
+
async list(prefix = "", options = {}) {
|
|
63
|
+
const normalizedPrefix = normalizeSafeWorkspacePath(prefix, { allowEmpty: true });
|
|
64
|
+
const filePrefix = this.#fileKey(normalizedPrefix, { allowEmpty: true });
|
|
65
|
+
const files = await this.#listBlobs(normalizedPrefix ? `${filePrefix}/` : `${this.#fileKey("", { allowEmpty: true })}/`);
|
|
66
|
+
const entries = /* @__PURE__ */ new Map();
|
|
67
|
+
for (const blob of files) {
|
|
68
|
+
const path = normalizeWorkspacePath(blob.key.slice(`${this.#fileKey("", { allowEmpty: true })}/`.length));
|
|
69
|
+
if (!path) continue;
|
|
70
|
+
if (normalizedPrefix && !path.startsWith(`${normalizedPrefix}/`)) continue;
|
|
71
|
+
if (!options.recursive && normalizedPrefix && path.slice(normalizedPrefix.length + 1).includes("/")) continue;
|
|
72
|
+
if (!options.recursive && !normalizedPrefix && path.includes("/")) {
|
|
73
|
+
entries.set(path.split("/")[0], {
|
|
74
|
+
path: path.split("/")[0],
|
|
75
|
+
type: "directory"
|
|
76
|
+
});
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
entries.set(path, {
|
|
80
|
+
mtime: blob.lastModified,
|
|
81
|
+
path,
|
|
82
|
+
size: blob.size,
|
|
83
|
+
type: "file"
|
|
84
|
+
});
|
|
85
|
+
if (options.recursive) {
|
|
86
|
+
const parts = path.split("/");
|
|
87
|
+
for (let index = 1; index < parts.length; index++) {
|
|
88
|
+
const dir = parts.slice(0, index).join("/");
|
|
89
|
+
entries.set(dir, {
|
|
90
|
+
path: dir,
|
|
91
|
+
type: "directory"
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return [...entries.values()].sort((a, b) => a.path.localeCompare(b.path));
|
|
97
|
+
}
|
|
98
|
+
async glob(pattern, _options = {}) {
|
|
99
|
+
const patterns = Array.isArray(pattern) ? pattern.map(normalizeSafeWorkspacePattern) : normalizeSafeWorkspacePattern(pattern);
|
|
100
|
+
return (await this.list("", { recursive: true })).filter((entry) => entry.type === "file" && matchesAny(entry.path, patterns));
|
|
101
|
+
}
|
|
102
|
+
async stat(path) {
|
|
103
|
+
const normalized = normalizeSafeWorkspacePath(path);
|
|
104
|
+
const file = await this.readFile(normalized);
|
|
105
|
+
if (file) {
|
|
106
|
+
const bytes = contentToBytes(file.content);
|
|
107
|
+
return {
|
|
108
|
+
digest: await sha256(bytes),
|
|
109
|
+
path: normalized,
|
|
110
|
+
size: bytes.byteLength,
|
|
111
|
+
type: "file"
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
return (await this.list(normalized, { recursive: false })).length ? {
|
|
115
|
+
path: normalized,
|
|
116
|
+
type: "directory"
|
|
117
|
+
} : void 0;
|
|
118
|
+
}
|
|
119
|
+
async mkdir(path, _options = {}) {
|
|
120
|
+
normalizeSafeWorkspacePath(path);
|
|
121
|
+
}
|
|
122
|
+
async rm(path, options = {}) {
|
|
123
|
+
const normalized = normalizeSafeWorkspacePath(path);
|
|
124
|
+
const client = await this.#client();
|
|
125
|
+
const targets = [];
|
|
126
|
+
if (await client.head(this.#fileKey(normalized)).catch(() => null)) targets.push(this.#fileKey(normalized));
|
|
127
|
+
else if (options.recursive) for (const blob of await this.#listBlobs(`${this.#fileKey(normalized)}/`)) targets.push(blob.key);
|
|
128
|
+
if (!targets.length) {
|
|
129
|
+
if (options.force) return;
|
|
130
|
+
throw new WorkspaceError(`[vitehub] Workspace path does not exist: ${path}.`);
|
|
131
|
+
}
|
|
132
|
+
await Promise.all(targets.map((target) => client.delete(target)));
|
|
133
|
+
}
|
|
134
|
+
async snapshot(options = {}) {
|
|
135
|
+
const snapshot = await createSnapshotFromEntries(await this.list("", { recursive: true }), options.name);
|
|
136
|
+
await (await this.#client()).upload(this.#snapshotKey(snapshot.id), JSON.stringify(snapshot), { contentType: "application/json; charset=utf-8" });
|
|
137
|
+
this.#baseline = snapshot;
|
|
138
|
+
return snapshot;
|
|
139
|
+
}
|
|
140
|
+
async diff(options = {}) {
|
|
141
|
+
return diffSnapshots(options.from || this.#baseline, await createSnapshotFromEntries(await this.list("", { recursive: true })));
|
|
142
|
+
}
|
|
143
|
+
async getMeta(key) {
|
|
144
|
+
return await this.#readJson(this.#metaKey(key));
|
|
145
|
+
}
|
|
146
|
+
async setMeta(key, value) {
|
|
147
|
+
await (await this.#client()).upload(this.#metaKey(key), JSON.stringify(value), { contentType: "application/json; charset=utf-8" });
|
|
148
|
+
}
|
|
149
|
+
async #listBlobs(prefix) {
|
|
150
|
+
const blobs = [];
|
|
151
|
+
let cursor;
|
|
152
|
+
do {
|
|
153
|
+
const result = await (await this.#client()).list({
|
|
154
|
+
cursor,
|
|
155
|
+
limit: 1e3,
|
|
156
|
+
prefix
|
|
157
|
+
});
|
|
158
|
+
blobs.push(...result.items);
|
|
159
|
+
cursor = result.cursor;
|
|
160
|
+
} while (cursor);
|
|
161
|
+
return blobs;
|
|
162
|
+
}
|
|
163
|
+
async #readBytes(path) {
|
|
164
|
+
const file = await this.readFile(path);
|
|
165
|
+
return file ? contentToBytes(file.content) : void 0;
|
|
166
|
+
}
|
|
167
|
+
async #readJson(pathname) {
|
|
168
|
+
const file = await (await this.#client()).download(pathname).catch(() => null);
|
|
169
|
+
return file ? JSON.parse(await file.text()) : void 0;
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
function createVercelBlobWorkspaceStore(options, workspaceName) {
|
|
173
|
+
return new VercelBlobWorkspaceStore(options, workspaceName);
|
|
174
|
+
}
|
|
175
|
+
//#endregion
|
|
176
|
+
export { createVercelBlobWorkspaceStore };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { J as WorkspacePublisher } from "./types-CwNcWTdt.js";
|
|
2
|
+
|
|
3
|
+
//#region src/publishers/manifest.d.ts
|
|
4
|
+
interface ManifestPublisherOptions {
|
|
5
|
+
path?: string;
|
|
6
|
+
}
|
|
7
|
+
declare function manifest(options?: ManifestPublisherOptions): WorkspacePublisher;
|
|
8
|
+
//#endregion
|
|
9
|
+
//#region src/publishers/github.d.ts
|
|
10
|
+
type GitHubPublisherOption = string | (() => string | undefined);
|
|
11
|
+
interface GitHubPublisherOptions {
|
|
12
|
+
branch?: GitHubPublisherOption;
|
|
13
|
+
message?: GitHubPublisherOption;
|
|
14
|
+
repo?: GitHubPublisherOption;
|
|
15
|
+
repository?: GitHubPublisherOption;
|
|
16
|
+
root?: GitHubPublisherOption;
|
|
17
|
+
token?: GitHubPublisherOption;
|
|
18
|
+
}
|
|
19
|
+
declare function github(options?: GitHubPublisherOptions): WorkspacePublisher;
|
|
20
|
+
//#endregion
|
|
21
|
+
//#region src/publishers/types.d.ts
|
|
22
|
+
interface TypesPublisherOptions {
|
|
23
|
+
path?: string;
|
|
24
|
+
}
|
|
25
|
+
declare function types(options?: TypesPublisherOptions): WorkspacePublisher;
|
|
26
|
+
//#endregion
|
|
27
|
+
//#region src/publishers/virtual-module.d.ts
|
|
28
|
+
interface VirtualModulePublisherOptions {
|
|
29
|
+
id: string;
|
|
30
|
+
includeContent?: boolean;
|
|
31
|
+
}
|
|
32
|
+
declare function virtualModule(options: VirtualModulePublisherOptions): WorkspacePublisher;
|
|
33
|
+
//#endregion
|
|
34
|
+
export { type GitHubPublisherOptions, type ManifestPublisherOptions, type TypesPublisherOptions, type VirtualModulePublisherOptions, github, manifest, types, virtualModule };
|