@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.
Files changed (47) hide show
  1. package/README.md +89 -1
  2. package/dist/ai-Bk3LLnit.js +352 -0
  3. package/dist/ai.d.ts +86 -0
  4. package/dist/ai.js +2 -0
  5. package/dist/asset-registry-CMuQbyvl.js +14 -0
  6. package/dist/chunk-DrSxFLj_.js +14 -0
  7. package/dist/cloudflare-env-DL94Uvpz.js +12 -0
  8. package/dist/config-CvS-7sre.js +49 -0
  9. package/dist/errors-BUL8SoFS.js +21 -0
  10. package/dist/files-DbsIjBya.js +50 -0
  11. package/dist/index.d.ts +150 -0
  12. package/dist/index.js +656 -0
  13. package/dist/lifecycle-uD82JeGh.js +92 -0
  14. package/dist/loader.d.ts +17 -0
  15. package/dist/loader.js +22 -0
  16. package/dist/module-8LZUMEXC.js +922 -0
  17. package/dist/nitro.d.ts +31 -0
  18. package/dist/nitro.js +2 -0
  19. package/dist/path-BVv9OXhg.js +63 -0
  20. package/dist/provider-BZuKAV4e.js +494 -0
  21. package/dist/providers/cloudflare/artifacts-store.d.ts +6 -0
  22. package/dist/providers/cloudflare/artifacts-store.js +384 -0
  23. package/dist/providers/vercel/blob-store.d.ts +6 -0
  24. package/dist/providers/vercel/blob-store.js +176 -0
  25. package/dist/publish.d.ts +34 -0
  26. package/dist/publish.js +206 -0
  27. package/dist/registry-Cnr0NoKv.js +932 -0
  28. package/dist/registry-D4Qzq26I.d.ts +10 -0
  29. package/dist/runtime/assets.d.ts +11 -0
  30. package/dist/runtime/assets.js +138 -0
  31. package/dist/runtime/empty-assets-registry.d.ts +6 -0
  32. package/dist/runtime/empty-assets-registry.js +4 -0
  33. package/dist/runtime/empty-registry.d.ts +6 -0
  34. package/dist/runtime/empty-registry.js +4 -0
  35. package/dist/runtime/nitro-plugin.d.ts +6 -0
  36. package/dist/runtime/nitro-plugin.js +14 -0
  37. package/dist/runtime/state.d.ts +21 -0
  38. package/dist/runtime/state.js +12 -0
  39. package/dist/sandbox-DtGXCWRq.js +198 -0
  40. package/dist/search-DtaEyjpO.js +39 -0
  41. package/dist/test.d.ts +2 -0
  42. package/dist/test.js +2 -0
  43. package/dist/types-CwNcWTdt.d.ts +378 -0
  44. package/dist/utils-BtAm_K5P.js +51 -0
  45. package/dist/vite.d.ts +22 -0
  46. package/dist/vite.js +131 -0
  47. package/package.json +101 -5
@@ -0,0 +1,206 @@
1
+ import { t as WorkspaceError } from "./errors-BUL8SoFS.js";
2
+ import { o as normalizeWorkspacePath, t as contentToBytes } from "./path-BVv9OXhg.js";
3
+ import { t as getActiveCloudflareBinding } from "./cloudflare-env-DL94Uvpz.js";
4
+ import { dirname, resolve } from "node:path";
5
+ import { mkdir, writeFile } from "node:fs/promises";
6
+ //#region src/publishers/manifest.ts
7
+ function manifest(options = {}) {
8
+ return {
9
+ name: "manifest",
10
+ async publish(ctx) {
11
+ const path = resolve(ctx.rootDir, options.path || `.vitehub/workspaces/${ctx.workspace.name}.json`);
12
+ const entries = await ctx.store.list("", { recursive: true });
13
+ await mkdir(dirname(path), { recursive: true });
14
+ await writeFile(path, `${JSON.stringify({
15
+ name: ctx.workspace.name,
16
+ entries
17
+ }, null, 2)}\n`);
18
+ }
19
+ };
20
+ }
21
+ //#endregion
22
+ //#region src/publishers/github.ts
23
+ function processEnv(...keys) {
24
+ const env = typeof process !== "undefined" ? process.env : void 0;
25
+ return keys.map((key) => env?.[key]).find((value) => typeof value === "string" && value.length > 0);
26
+ }
27
+ function resolveOption(value) {
28
+ return typeof value === "function" ? value() : value;
29
+ }
30
+ function requireOption(label, value) {
31
+ if (!value) throw new WorkspaceError(`[vitehub] GitHub workspace publisher requires ${label}.`);
32
+ return value;
33
+ }
34
+ function splitRepository(repository) {
35
+ const [owner, repo] = repository.split("/");
36
+ if (!owner || !repo) throw new WorkspaceError("[vitehub] GitHub workspace publisher requires a repository in owner/repo format.");
37
+ return {
38
+ owner,
39
+ repo
40
+ };
41
+ }
42
+ function joinGitPath(...parts) {
43
+ return parts.join("/").replaceAll("\\", "/").split("/").filter(Boolean).join("/");
44
+ }
45
+ function workspacePathFromGitPath(path, root) {
46
+ const normalized = joinGitPath(path);
47
+ const normalizedRoot = joinGitPath(root);
48
+ if (!normalizedRoot) return normalizeWorkspacePath(normalized);
49
+ if (!normalized.startsWith(`${normalizedRoot}/`)) return void 0;
50
+ return normalizeWorkspacePath(normalized.slice(normalizedRoot.length + 1));
51
+ }
52
+ function isFileEntry(entry) {
53
+ return entry.type === "file";
54
+ }
55
+ function toHex(bytes) {
56
+ return [...bytes].map((byte) => byte.toString(16).padStart(2, "0")).join("");
57
+ }
58
+ async function gitBlobSha(bytes) {
59
+ const prefix = new TextEncoder().encode(`blob ${bytes.byteLength}\0`);
60
+ const input = new Uint8Array(prefix.byteLength + bytes.byteLength);
61
+ input.set(prefix);
62
+ input.set(bytes, prefix.byteLength);
63
+ return toHex(new Uint8Array(await globalThis.crypto.subtle.digest("SHA-1", input)));
64
+ }
65
+ function toBase64(bytes) {
66
+ let binary = "";
67
+ for (let index = 0; index < bytes.length; index += 32768) binary += String.fromCharCode(...bytes.subarray(index, index + 32768));
68
+ const encode = globalThis.btoa;
69
+ return encode ? encode(binary) : Buffer.from(bytes).toString("base64");
70
+ }
71
+ function resolveRepository(options) {
72
+ return resolveOption(options.repository) || resolveOption(options.repo) || processEnv("WORKSPACE_GITHUB_REPOSITORY", "VITEHUB_WORKSPACE_GITHUB_REPOSITORY", "GITHUB_REPOSITORY");
73
+ }
74
+ function resolveBranch(options) {
75
+ return resolveOption(options.branch) || processEnv("WORKSPACE_GITHUB_BRANCH", "VITEHUB_WORKSPACE_GITHUB_BRANCH", "GITHUB_BRANCH") || "main";
76
+ }
77
+ function resolveRoot(options, workspaceName) {
78
+ return resolveOption(options.root) || processEnv("WORKSPACE_GITHUB_ROOT", "VITEHUB_WORKSPACE_GITHUB_ROOT") || `.vitehub/workspaces/${workspaceName}`;
79
+ }
80
+ function resolveToken(options) {
81
+ return resolveOption(options.token) || getActiveCloudflareBinding("GITHUB_TOKEN") || processEnv("WORKSPACE_GITHUB_TOKEN", "VITEHUB_WORKSPACE_GITHUB_TOKEN", "GITHUB_TOKEN");
82
+ }
83
+ function resolveMessage(options, ctx) {
84
+ return ctx.snapshot?.name || resolveOption(options.message) || "chore: update workspace snapshot";
85
+ }
86
+ async function readFiles(ctx, root) {
87
+ const entries = await ctx.store.list("", { recursive: true });
88
+ return await Promise.all(entries.filter(isFileEntry).map(async (entry) => {
89
+ const file = await ctx.store.readFile(entry.path);
90
+ if (!file) throw new WorkspaceError(`[vitehub] Workspace file disappeared before GitHub publish: ${entry.path}.`);
91
+ const bytes = contentToBytes(file.content);
92
+ return {
93
+ ...file,
94
+ bytes,
95
+ path: normalizeWorkspacePath(entry.path),
96
+ fullPath: joinGitPath(root, entry.path),
97
+ gitSha: await gitBlobSha(bytes)
98
+ };
99
+ }));
100
+ }
101
+ async function requestGitHubJson(repo, token, path, init) {
102
+ const response = await fetch(`https://api.github.com${path}`, {
103
+ ...init,
104
+ headers: {
105
+ accept: "application/vnd.github+json",
106
+ authorization: `Bearer ${token}`,
107
+ "content-type": "application/json",
108
+ "user-agent": "vitehub-workspace",
109
+ "x-github-api-version": "2022-11-28",
110
+ ...init?.headers
111
+ }
112
+ });
113
+ if (!response.ok) throw new WorkspaceError(`[vitehub] GitHub workspace publisher request failed for ${repo}: ${response.status} ${response.statusText} ${await response.text().catch(() => "")}`);
114
+ return await response.json();
115
+ }
116
+ function findRemoteFiles(tree, root) {
117
+ if (tree.truncated) throw new WorkspaceError("[vitehub] GitHub workspace publisher could not compare the remote tree because GitHub returned a truncated tree.");
118
+ const files = /* @__PURE__ */ new Map();
119
+ for (const entry of tree.tree) {
120
+ if (entry.type !== "blob" || !entry.sha) continue;
121
+ const path = workspacePathFromGitPath(entry.path, root);
122
+ if (path) files.set(path, entry);
123
+ }
124
+ return files;
125
+ }
126
+ function github(options = {}) {
127
+ return {
128
+ name: "github",
129
+ async publish(ctx) {
130
+ const repository = requireOption("a repository", resolveRepository(options));
131
+ const branch = resolveBranch(options);
132
+ const root = resolveRoot(options, ctx.workspace.name);
133
+ const token = requireOption("a token", resolveToken(options));
134
+ const files = await readFiles(ctx, root);
135
+ const nextPaths = new Set(files.map((file) => file.path));
136
+ const { owner, repo } = splitRepository(repository);
137
+ const refPath = `/repos/${owner}/${repo}/git/ref/heads/${branch}`;
138
+ const refsPath = `/repos/${owner}/${repo}/git/refs/heads/${branch}`;
139
+ const ref = await requestGitHubJson(repository, token, refPath);
140
+ const current = await requestGitHubJson(repository, token, `/repos/${owner}/${repo}/git/commits/${ref.object.sha}`);
141
+ const remoteFiles = findRemoteFiles(await requestGitHubJson(repository, token, `/repos/${owner}/${repo}/git/trees/${current.tree.sha}?recursive=1`), root);
142
+ const changedFiles = files.filter((file) => remoteFiles.get(file.path)?.sha !== file.gitSha);
143
+ const deletedEntries = [...remoteFiles].filter(([path]) => !nextPaths.has(path)).map(([, entry]) => ({
144
+ mode: "100644",
145
+ path: entry.path,
146
+ sha: null,
147
+ type: "blob"
148
+ }));
149
+ if (!changedFiles.length && !deletedEntries.length) return;
150
+ const blobs = await Promise.all(changedFiles.map((file) => requestGitHubJson(repository, token, `/repos/${owner}/${repo}/git/blobs`, {
151
+ body: JSON.stringify({
152
+ content: toBase64(file.bytes),
153
+ encoding: "base64"
154
+ }),
155
+ method: "POST"
156
+ })));
157
+ const tree = await requestGitHubJson(repository, token, `/repos/${owner}/${repo}/git/trees`, {
158
+ body: JSON.stringify({
159
+ base_tree: current.tree.sha,
160
+ tree: [...changedFiles.map((file, index) => ({
161
+ mode: "100644",
162
+ path: file.fullPath,
163
+ sha: blobs[index].sha,
164
+ type: "blob"
165
+ })), ...deletedEntries]
166
+ }),
167
+ method: "POST"
168
+ });
169
+ const commit = await requestGitHubJson(repository, token, `/repos/${owner}/${repo}/git/commits`, {
170
+ body: JSON.stringify({
171
+ message: resolveMessage(options, ctx),
172
+ parents: [ref.object.sha],
173
+ tree: tree.sha
174
+ }),
175
+ method: "POST"
176
+ });
177
+ await requestGitHubJson(repository, token, refsPath, {
178
+ body: JSON.stringify({ sha: commit.sha }),
179
+ method: "PATCH"
180
+ });
181
+ }
182
+ };
183
+ }
184
+ //#endregion
185
+ //#region src/publishers/types.ts
186
+ function types(options = {}) {
187
+ return {
188
+ name: "types",
189
+ async publish(ctx) {
190
+ const path = resolve(ctx.rootDir, options.path || `.vitehub/workspaces/${ctx.workspace.name}.d.ts`);
191
+ await mkdir(dirname(path), { recursive: true });
192
+ await writeFile(path, `declare module "#vitehub/workspaces/${ctx.workspace.name}" {\n const manifest: { name: ${JSON.stringify(ctx.workspace.name)}, entries: Array<{ path: string, type: "file" | "directory" }> }\n export default manifest\n}\n`);
193
+ }
194
+ };
195
+ }
196
+ //#endregion
197
+ //#region src/publishers/virtual-module.ts
198
+ function virtualModule(options) {
199
+ return {
200
+ name: `virtual-module:${options.id}`,
201
+ clientSafe: options.includeContent === true,
202
+ async publish() {}
203
+ };
204
+ }
205
+ //#endregion
206
+ export { github, manifest, types, virtualModule };