@vite-hub/workspace 0.0.4 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +225 -58
- package/dist/{ai-CjxvGqM0.d.ts → ai-BW2ZhizI.d.ts} +22 -7
- package/dist/{ai-BqCC8BpY.js → ai-BZyLwULx.js} +147 -66
- package/dist/ai.d.ts +2 -2
- package/dist/ai.js +1 -1
- package/dist/{asset-registry-8QI3ML3K.js → asset-registry-DfBV5kmw.js} +3 -2
- package/dist/association-Cm0dUTDU-CDDhJnIh-Cv3bWnVb.js +19 -0
- package/dist/blob-database.d.ts +521 -0
- package/dist/blob-database.js +601 -0
- package/dist/cloudflare.d.ts +5 -6
- package/dist/cloudflare.js +1 -1
- package/dist/config-B7dmzDX3.js +1112 -0
- package/dist/{config-DE9xlaIw.d.ts → config-DLKdaFWN.d.ts} +1 -2
- package/dist/error-diagnostics-A_8Kp-48.js +116 -0
- package/dist/error-diagnostics-DY5GP47p.js +96 -0
- package/dist/events-7EBoeIPd-A2RUkJi5-BxrICQ17.js +354 -0
- package/dist/events-CRaLy7U3-3PNNp5WZ-Gv9nd0cH.js +214 -0
- package/dist/events-CeChclTf-CX-gD8oy-4aL2j6V-.js +527 -0
- package/dist/feed-DsC7Vws1-M_de2nSK-tKc0Pwo8.js +248 -0
- package/dist/feed-Dyf730Ys-D0HgBba6-BUnCa5gS.js +179 -0
- package/dist/file-C0_aPWSS.js +26 -0
- package/dist/file-metadata-CxDy-E2H.js +58 -0
- package/dist/{files-Q6xZjDHA.js → files-C7Px8uaC.js} +3 -3
- package/dist/forgejo-BN8ItCfl-Bbn4vhDd.js +2278 -0
- package/dist/{github-DHnjxQIX.js → github-CPxHtFB7.js} +16 -23
- package/dist/github-DtncTfmv-Du2S8lGF.js +4159 -0
- package/dist/gitlab-BoDm8-BC-xzZsW9eW.js +2530 -0
- package/dist/glob--XcLlcml.js +22 -0
- package/dist/glob--kNYDbfU.js +30 -0
- package/dist/graphql-DG5aJBT3-DceyFYdM-C4saqX37.js +489 -0
- package/dist/{host-DJH8cuoU.js → host-CFnrTXTK.js} +97 -41
- package/dist/{hosted-RPeJOCFz.js → hosted-acMA5XyL.js} +102 -19
- package/dist/hosted-vercel-blob-DKntsHaf.js +12603 -0
- package/dist/hosted-vercel-blob.d.ts +5 -6
- package/dist/hosted-vercel-blob.js +1 -1
- package/dist/hosted.d.ts +6 -7
- package/dist/hosted.js +1 -1
- package/dist/index.d.ts +57 -36
- package/dist/index.js +56 -56
- package/dist/inspect-BoObD2cJ.js +167 -0
- package/dist/internal/path.d.ts +2 -0
- package/dist/internal/path.js +2 -0
- package/dist/lifecycle-BwZ7c9hb.js +596 -0
- package/dist/loader.d.ts +5 -6
- package/dist/loader.js +3 -2
- package/dist/mcp-resources-BREFCmtn.js +39 -0
- package/dist/memory-BbwOqDXl.js +248 -0
- package/dist/metadata-target-DZEZQGHA.js +1588 -0
- package/dist/mountx.d.ts +4 -6
- package/dist/mountx.js +10 -8
- package/dist/nitro.d.ts +1 -1
- package/dist/nitro.js +1 -1
- package/dist/nuxt.d.ts +13 -12
- package/dist/nuxt.js +4 -8
- package/dist/operations-BcpWr41m.js +159 -0
- package/dist/operations-CGqY1XeG.js +965 -0
- package/dist/operations-DnOwKsJT.d.ts +136 -0
- package/dist/operations.d.ts +53 -0
- package/dist/operations.js +2 -0
- package/dist/path-CNAwN7pw.js +114 -0
- package/dist/path-DrSIenjj.js +59 -0
- package/dist/path-zHxQUbqa.d.ts +9 -0
- package/dist/plugins-DqQ8b4T5.d.ts +10 -0
- package/dist/provider-kXjfli9u.js +1457 -0
- package/dist/providers/github/store.d.ts +3 -5
- package/dist/providers/github/store.js +1 -1
- package/dist/{registry-D3yzt8HQ.js → registry-Bp-8UUfG.js} +9 -7
- package/dist/{registry-Uj6IVxes.d.ts → registry-Cg1TJVVZ.d.ts} +1 -2
- package/dist/{request-execution-B-dSFlGB.js → request-execution-Dk9rY-Yh.js} +19 -19
- package/dist/{chunk-2rV9d50f.js → rolldown-runtime-B4iAMlE-.js} +2 -2
- package/dist/runtime/assets.d.ts +3 -5
- package/dist/runtime/assets.js +7 -6
- package/dist/runtime/empty-assets-registry.d.ts +1 -2
- package/dist/runtime/empty-registry.d.ts +1 -2
- package/dist/runtime/state.d.ts +3 -3
- package/dist/runtime/state.js +6 -6
- package/dist/runtime/workspace.d.ts +1 -1
- package/dist/runtime/workspace.js +1 -1
- package/dist/runtime.d.ts +51 -17
- package/dist/runtime.js +320 -60
- package/dist/{search-BpiCF2Tw.js → search-CrTmXj5h.js} +10 -9
- package/dist/server-BNQ-Nbjj.js +290 -0
- package/dist/server.d.ts +22 -34
- package/dist/server.js +2 -328
- package/dist/shared-DalXdP0i.js +2158 -0
- package/dist/source-metadata.d.ts +6 -7
- package/dist/source-metadata.js +7 -7
- package/dist/{state-tYcRWEh2.d.ts → state-B8zbLi5t.d.ts} +3 -3
- package/dist/store-CXgmu-uw.js +1729 -0
- package/dist/{sync-DnTQwt8r.js → sync-CN6ec-5M.js} +61 -41
- package/dist/templates-Bn8a-pd2-D9DhJEVD-CBfXNRzz.js +125 -0
- package/dist/test.d.ts +1 -1
- package/dist/test.js +1 -1
- package/dist/{token-CVzURBxC.js → token-CO62GglH.js} +3 -3
- package/dist/{types-CYuwjJ4V.d.ts → types-DeK9GIxy.d.ts} +256 -24
- package/dist/{use-BTNDR5Rd.js → use-BQj7nSz6.js} +82 -41
- package/dist/{use-BiHzt0xA.d.ts → use-DhwhbAVV.d.ts} +16 -11
- package/dist/utils-Blrh4N2n.js +545 -0
- package/dist/vite-BEvgU2GY.js +20909 -0
- package/dist/vite-CcZmMQ7S.d.ts +476 -0
- package/dist/vite.d.ts +2 -2
- package/dist/vite.js +2 -2
- package/dist/webhooks-DgHwxxYz-D1V2LOr1-azofFOiP.js +1565 -0
- package/dist/{workspace-Dq08Yn72.js → workspace-6pxkOuxr.js} +361 -230
- package/dist/{workspace-Xddf4G1B.d.ts → workspace-BWJQBK1u.d.ts} +4 -3
- package/package.json +89 -55
- package/dist/collections/client.d.ts +0 -39
- package/dist/collections/client.js +0 -137
- package/dist/collections.d.ts +0 -59
- package/dist/collections.js +0 -159
- package/dist/config-Bc3c4Su1.js +0 -7904
- package/dist/config-CHLLWklZ.js +0 -36
- package/dist/file-Bc1X7pco.js +0 -23
- package/dist/glob-CEZKeZFC.js +0 -18
- package/dist/hosted-vercel-blob-d6C8ND5j.js +0 -23580
- package/dist/lifecycle-LrL3sA4n.js +0 -316
- package/dist/metadata-target-B0tOmVW7.js +0 -717
- package/dist/path-Cp59koVz.js +0 -139
- package/dist/provider-B1yLjCJJ.js +0 -746
- package/dist/publish.d.ts +0 -35
- package/dist/publish.js +0 -112
- package/dist/runtime-options-BsPSv5O7.js +0 -13
- package/dist/runtime-type-iEE6BjQJ.js +0 -34
- package/dist/shared-DTR1Hhma.js +0 -331
- package/dist/store-Dt_Jyys3.js +0 -608
- package/dist/vite-BptCrpv2.js +0 -4526
- package/dist/vite-CtA9guIM.d.ts +0 -66
|
@@ -0,0 +1,545 @@
|
|
|
1
|
+
import { a as workspaceConflict, s as workspaceError, t as normalizeSafeWorkspacePath } from "./path-DrSIenjj.js";
|
|
2
|
+
import { f as sha256, l as normalizeWorkspacePath, o as isWorkspaceBytes, r as contentToBytes } from "./path-CNAwN7pw.js";
|
|
3
|
+
import { t as workspaceErrorDiagnostics } from "./error-diagnostics-DY5GP47p.js";
|
|
4
|
+
import { n as copyJsonWorkspaceMetadata, t as copyJsonFileMetadata } from "./file-metadata-CxDy-E2H.js";
|
|
5
|
+
import { minimatch } from "minimatch";
|
|
6
|
+
import { hasRuntimeType, isRuntimeObject } from "@vite-hub/runtime/internal/runtime-type";
|
|
7
|
+
import { lookup } from "mrmime";
|
|
8
|
+
//#region src/core/plugins.ts
|
|
9
|
+
const contributedPluginsKey = Symbol.for("vitehub.workspace.contributedPlugins");
|
|
10
|
+
function contributedPluginState() {
|
|
11
|
+
const scope = globalThis;
|
|
12
|
+
scope[contributedPluginsKey] ??= {
|
|
13
|
+
plugins: /* @__PURE__ */ new Map(),
|
|
14
|
+
version: 0
|
|
15
|
+
};
|
|
16
|
+
return scope[contributedPluginsKey];
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Add a plugin's rules and hooks to a Workspace by name, including discovered Workspaces the caller does not define.
|
|
20
|
+
* Registering the same plugin id again replaces the earlier registration. A plugin declared by the Workspace Definition
|
|
21
|
+
* with the same id wins. The returned function removes this registration.
|
|
22
|
+
*/
|
|
23
|
+
function registerWorkspacePlugin(workspace, plugin) {
|
|
24
|
+
if (!hasRuntimeType(workspace, "string") || !workspace) throw workspaceErrorDiagnostics.WORKSPACE_R0022({ message: "[vitehub] registerWorkspacePlugin requires a Workspace name." });
|
|
25
|
+
if (!plugin || !hasRuntimeType(plugin.id, "string") || !plugin.id) throw workspaceErrorDiagnostics.WORKSPACE_R0024({ message: "[vitehub] Workspace plugins require an id." });
|
|
26
|
+
const state = contributedPluginState();
|
|
27
|
+
let plugins = state.plugins.get(workspace);
|
|
28
|
+
if (!plugins) state.plugins.set(workspace, plugins = /* @__PURE__ */ new Map());
|
|
29
|
+
plugins.set(plugin.id, plugin);
|
|
30
|
+
state.version++;
|
|
31
|
+
return () => {
|
|
32
|
+
if (state.plugins.get(workspace)?.get(plugin.id) !== plugin) return;
|
|
33
|
+
plugins.delete(plugin.id);
|
|
34
|
+
if (!plugins.size) state.plugins.delete(workspace);
|
|
35
|
+
state.version++;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
/** Changes whenever a contributed plugin is added, replaced, or removed. */
|
|
39
|
+
function workspacePluginsVersion() {
|
|
40
|
+
return contributedPluginState().version;
|
|
41
|
+
}
|
|
42
|
+
/** The Definition's own plugins followed by contributed plugins with other ids. */
|
|
43
|
+
function workspaceDefinitionPlugins(definition) {
|
|
44
|
+
const own = definition.plugins ?? [];
|
|
45
|
+
const contributed = contributedPluginState().plugins.get(definition.name);
|
|
46
|
+
if (!contributed?.size) return own;
|
|
47
|
+
const ids = new Set(own.map((plugin) => plugin?.id));
|
|
48
|
+
return [...own, ...[...contributed.values()].filter((plugin) => !ids.has(plugin.id))];
|
|
49
|
+
}
|
|
50
|
+
//#endregion
|
|
51
|
+
//#region src/core/rules.ts
|
|
52
|
+
function normalizeMaxBytes(value) {
|
|
53
|
+
if (value === void 0) return void 0;
|
|
54
|
+
if (typeof value === "number") return value;
|
|
55
|
+
const match = /^(\d+)(kb|mb)$/.exec(value.toLowerCase());
|
|
56
|
+
if (!match) throw workspaceErrorDiagnostics.WORKSPACE_R0023({ message: `[vitehub] Invalid workspace rule maxBytes value "${value}".` });
|
|
57
|
+
const count = Number(match[1]);
|
|
58
|
+
return match[2] === "kb" ? count * 1024 : count * 1024 * 1024;
|
|
59
|
+
}
|
|
60
|
+
function toArray(value) {
|
|
61
|
+
if (value === void 0) return [];
|
|
62
|
+
return Array.isArray(value) ? value : [value];
|
|
63
|
+
}
|
|
64
|
+
function mergeHooks(left = {}, right = {}) {
|
|
65
|
+
return {
|
|
66
|
+
"write:after": [...toArray(left["write:after"]), ...toArray(right["write:after"])],
|
|
67
|
+
"write:before": [...toArray(left["write:before"]), ...toArray(right["write:before"])],
|
|
68
|
+
"write:error": [...toArray(left["write:error"]), ...toArray(right["write:error"])],
|
|
69
|
+
"write:validate": [...toArray(left["write:validate"]), ...toArray(right["write:validate"])]
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
function normalizeRules(rules = {}) {
|
|
73
|
+
return Object.entries(rules).map(([pattern, rule]) => {
|
|
74
|
+
const normalizedPattern = normalizeWorkspacePath(pattern);
|
|
75
|
+
return {
|
|
76
|
+
...rule,
|
|
77
|
+
maxBytes: normalizeMaxBytes(rule.maxBytes),
|
|
78
|
+
pattern: normalizedPattern,
|
|
79
|
+
specificity: normalizedPattern.replace(/\*/g, "").length,
|
|
80
|
+
validate: toArray(rule.validate)
|
|
81
|
+
};
|
|
82
|
+
}).sort((left, right) => left.specificity - right.specificity || left.pattern.localeCompare(right.pattern));
|
|
83
|
+
}
|
|
84
|
+
function normalizePolicy(definition) {
|
|
85
|
+
let hooks = {};
|
|
86
|
+
let rules = {};
|
|
87
|
+
for (const plugin of workspaceDefinitionPlugins(definition)) {
|
|
88
|
+
if (!plugin?.id) throw workspaceErrorDiagnostics.WORKSPACE_R0024({ message: "[vitehub] Workspace plugins require an id." });
|
|
89
|
+
rules = {
|
|
90
|
+
...rules,
|
|
91
|
+
...plugin.rules
|
|
92
|
+
};
|
|
93
|
+
hooks = mergeHooks(hooks, plugin.hooks);
|
|
94
|
+
}
|
|
95
|
+
rules = {
|
|
96
|
+
...rules,
|
|
97
|
+
...definition.rules
|
|
98
|
+
};
|
|
99
|
+
hooks = mergeHooks(hooks, definition.hooks);
|
|
100
|
+
return {
|
|
101
|
+
hooks,
|
|
102
|
+
rules: normalizeRules(rules)
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
function matchRule(policy, path) {
|
|
106
|
+
const normalized = normalizeWorkspacePath(path);
|
|
107
|
+
let matched;
|
|
108
|
+
for (const rule of policy.rules) {
|
|
109
|
+
const globstarDirectory = rule.pattern.endsWith("/**") ? rule.pattern.slice(0, -3) : void 0;
|
|
110
|
+
if (minimatch(normalized, rule.pattern, { dot: true }) || normalized === globstarDirectory) matched = rule;
|
|
111
|
+
}
|
|
112
|
+
return matched;
|
|
113
|
+
}
|
|
114
|
+
function writeAllowed(write, operation, exists) {
|
|
115
|
+
if (write === void 0 || write === true) return true;
|
|
116
|
+
if (write === false) return false;
|
|
117
|
+
if (write === "delete") return operation === "rm";
|
|
118
|
+
if (write === "create") return operation !== "rm" && !exists;
|
|
119
|
+
if (write === "update") return operation === "writeFile" && exists;
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
function sizeOf(content) {
|
|
123
|
+
return content === void 0 ? 0 : contentToBytes(content).byteLength;
|
|
124
|
+
}
|
|
125
|
+
function assertRuleAllows(input) {
|
|
126
|
+
const rule = input.rule;
|
|
127
|
+
if (!rule) return;
|
|
128
|
+
const exists = Boolean(input.previous);
|
|
129
|
+
if (!writeAllowed(rule.write, input.operation, exists)) throw workspaceError(`[vitehub] Workspace rule "${rule.pattern}" does not allow ${input.operation} for ${input.path}.`);
|
|
130
|
+
if (rule.maxBytes !== void 0 && sizeOf(input.content) > rule.maxBytes) throw workspaceError(`[vitehub] Workspace rule "${rule.pattern}" limits writes to ${rule.maxBytes} bytes: ${input.path}.`);
|
|
131
|
+
if (rule.mediaType && input.mediaType) {
|
|
132
|
+
if (!(Array.isArray(rule.mediaType) ? rule.mediaType : [rule.mediaType]).includes(input.mediaType)) throw workspaceError(`[vitehub] Workspace rule "${rule.pattern}" does not allow media type ${input.mediaType}: ${input.path}.`);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
async function runHooks(hooks, name, input) {
|
|
136
|
+
for (const hook of toArray(hooks[name])) await hook(input);
|
|
137
|
+
}
|
|
138
|
+
async function runValidators(validators, input) {
|
|
139
|
+
let current = input;
|
|
140
|
+
for (const validate of validators) {
|
|
141
|
+
const result = await validate(current);
|
|
142
|
+
if (result === false) throw workspaceError(`[vitehub] Workspace validator rejected ${current.operation} for ${current.path}.`);
|
|
143
|
+
if (typeof result === "object" && result !== null) current = result;
|
|
144
|
+
}
|
|
145
|
+
return current;
|
|
146
|
+
}
|
|
147
|
+
function createWorkspaceWritePolicy(definition) {
|
|
148
|
+
let cached = {
|
|
149
|
+
version: workspacePluginsVersion(),
|
|
150
|
+
policy: normalizePolicy(definition)
|
|
151
|
+
};
|
|
152
|
+
const currentPolicy = () => {
|
|
153
|
+
const version = workspacePluginsVersion();
|
|
154
|
+
if (cached.version !== version) cached = {
|
|
155
|
+
version,
|
|
156
|
+
policy: normalizePolicy(definition)
|
|
157
|
+
};
|
|
158
|
+
return cached.policy;
|
|
159
|
+
};
|
|
160
|
+
return {
|
|
161
|
+
check(input) {
|
|
162
|
+
const checked = {
|
|
163
|
+
...input,
|
|
164
|
+
rule: matchRule(currentPolicy(), input.path)
|
|
165
|
+
};
|
|
166
|
+
assertRuleAllows(checked);
|
|
167
|
+
return checked;
|
|
168
|
+
},
|
|
169
|
+
async before(input) {
|
|
170
|
+
const policy = currentPolicy();
|
|
171
|
+
const rule = matchRule(policy, input.path);
|
|
172
|
+
let current = {
|
|
173
|
+
...input,
|
|
174
|
+
rule
|
|
175
|
+
};
|
|
176
|
+
await runHooks(policy.hooks, "write:before", current);
|
|
177
|
+
current = {
|
|
178
|
+
...current,
|
|
179
|
+
rule
|
|
180
|
+
};
|
|
181
|
+
assertRuleAllows(current);
|
|
182
|
+
if (current.rule?.validate.length) current = await runValidators(current.rule.validate, current);
|
|
183
|
+
await runHooks(policy.hooks, "write:validate", current);
|
|
184
|
+
current = {
|
|
185
|
+
...current,
|
|
186
|
+
rule
|
|
187
|
+
};
|
|
188
|
+
assertRuleAllows(current);
|
|
189
|
+
return current;
|
|
190
|
+
},
|
|
191
|
+
async after(input) {
|
|
192
|
+
await runHooks(currentPolicy().hooks, "write:after", input);
|
|
193
|
+
},
|
|
194
|
+
async error(input, error) {
|
|
195
|
+
for (const hook of toArray(currentPolicy().hooks["write:error"])) await hook({
|
|
196
|
+
...input,
|
|
197
|
+
error
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
function resolveWorkspaceAutoCommit(definition, diff) {
|
|
203
|
+
if (!diff.entries.length) return;
|
|
204
|
+
if (definition.commit === true || typeof definition.commit === "string") return {
|
|
205
|
+
message: (typeof definition.commit === "string" ? definition.commit.trim() : "") || `chore: update ${definition.name} workspace`,
|
|
206
|
+
paths: diff.entries.map((entry) => normalizeWorkspacePath(entry.path))
|
|
207
|
+
};
|
|
208
|
+
const policy = normalizePolicy(definition);
|
|
209
|
+
const paths = diff.entries.map((entry) => normalizeWorkspacePath(entry.path));
|
|
210
|
+
const rules = paths.map((path) => matchRule(policy, path));
|
|
211
|
+
if (!rules.length || rules.some((rule) => !rule?.commit)) return;
|
|
212
|
+
return {
|
|
213
|
+
message: rules.map((rule) => rule?.commit).find((commit) => typeof commit === "string" && commit.trim().length > 0)?.trim() || `chore: update ${definition.name} workspace`,
|
|
214
|
+
paths
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
//#endregion
|
|
218
|
+
//#region src/core/history.ts
|
|
219
|
+
/** The ref used when a history call omits `ref`. A Store's working tree follows this ref. */
|
|
220
|
+
const defaultWorkspaceHistoryRef = "main";
|
|
221
|
+
const historyRefPattern = /^[\w-]+(?:[./][\w-]+)*$/;
|
|
222
|
+
const forwardedHistoryCommits = /* @__PURE__ */ new WeakSet();
|
|
223
|
+
const forwardedHistoryMerges = /* @__PURE__ */ new WeakMap();
|
|
224
|
+
/** Let a resolved facade check a prepared merge before the base facade checks its own grants and rules. */
|
|
225
|
+
function forwardWorkspaceHistoryMerge(options, commit) {
|
|
226
|
+
const forwarded = { ...options };
|
|
227
|
+
forwardedHistoryMerges.set(forwarded, commit);
|
|
228
|
+
return forwarded;
|
|
229
|
+
}
|
|
230
|
+
function forwardWorkspaceHistoryFiles(options) {
|
|
231
|
+
const forwardFiles = (files = {}) => Object.fromEntries(Object.entries(files).map(([path, file]) => [path, {
|
|
232
|
+
content: file.content,
|
|
233
|
+
mediaType: file.mediaType,
|
|
234
|
+
metadata: file.metadata
|
|
235
|
+
}]));
|
|
236
|
+
const base = {
|
|
237
|
+
ref: options.ref,
|
|
238
|
+
ifHead: options.ifHead,
|
|
239
|
+
ifHeads: options.ifHeads,
|
|
240
|
+
startFrom: options.startFrom,
|
|
241
|
+
message: options.message,
|
|
242
|
+
metadata: options.metadata
|
|
243
|
+
};
|
|
244
|
+
const forwarded = options.changes ? {
|
|
245
|
+
...base,
|
|
246
|
+
changes: {
|
|
247
|
+
put: forwardFiles(options.changes.put),
|
|
248
|
+
delete: [...options.changes.delete ?? []]
|
|
249
|
+
}
|
|
250
|
+
} : {
|
|
251
|
+
...base,
|
|
252
|
+
files: forwardFiles(options.files)
|
|
253
|
+
};
|
|
254
|
+
forwardedHistoryCommits.add(forwarded);
|
|
255
|
+
return forwarded;
|
|
256
|
+
}
|
|
257
|
+
function requireWorkspaceHistory(workspace) {
|
|
258
|
+
if (!workspace.history) throw historyUnavailable();
|
|
259
|
+
return workspace.history;
|
|
260
|
+
}
|
|
261
|
+
function historyUnavailable() {
|
|
262
|
+
return workspaceErrorDiagnostics.WORKSPACE_R0069({ message: "[vitehub] This Workspace Store does not support retained history. Configure a Store with the history capability." });
|
|
263
|
+
}
|
|
264
|
+
/** Forward history only when the caller can inspect the complete folder. */
|
|
265
|
+
function createWorkspaceHistoryReader(history, complete) {
|
|
266
|
+
function requireHistory() {
|
|
267
|
+
if (!complete) throw workspaceErrorDiagnostics.WORKSPACE_R0069({ message: "[vitehub] Retained folder history requires access to the complete Workspace." });
|
|
268
|
+
if (!history) throw historyUnavailable();
|
|
269
|
+
return history;
|
|
270
|
+
}
|
|
271
|
+
return {
|
|
272
|
+
...history?.digest ? { digest: async (bytes) => await history.digest(bytes) } : {},
|
|
273
|
+
...history?.forksFrom ? { forksFrom: async (ref) => await history.forksFrom(ref) } : {},
|
|
274
|
+
...history?.pullRequests ? { pullRequests: async (options) => await requireHistory().pullRequests(options) } : {},
|
|
275
|
+
head: async (options) => await requireHistory().head(options),
|
|
276
|
+
list: async (options) => await requireHistory().list(options),
|
|
277
|
+
refs: async () => await requireHistory().refs(),
|
|
278
|
+
open: async (id) => await requireHistory().open(id),
|
|
279
|
+
usage: async () => await requireHistory().usage()
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
function validateHistoryMessage(message) {
|
|
283
|
+
if (message !== void 0 && !hasRuntimeType(message, "string")) throw workspaceError("[vitehub] History revision message must be a string.");
|
|
284
|
+
}
|
|
285
|
+
/** Resolve an optional ref name. Names use letters, numbers, `_` and `-`, joined by single `.` or `/`. */
|
|
286
|
+
function normalizeHistoryRef(ref) {
|
|
287
|
+
if (ref === void 0) return defaultWorkspaceHistoryRef;
|
|
288
|
+
if (!hasRuntimeType(ref, "string") || ref.length > 128 || !historyRefPattern.test(ref)) throw workspaceErrorDiagnostics.WORKSPACE_R0071({ message: `[vitehub] Invalid history ref${hasRuntimeType(ref, "string") ? ` ${JSON.stringify(ref.slice(0, 128))}` : ""}. Use up to 128 letters, numbers, underscores, and dashes, separated by single dots or slashes.` });
|
|
289
|
+
return ref;
|
|
290
|
+
}
|
|
291
|
+
function normalizeHistoryPath(path) {
|
|
292
|
+
return normalizeSafeWorkspacePath(path).split("/").filter(Boolean).join("/");
|
|
293
|
+
}
|
|
294
|
+
function historyConflict(ref, expected, actual, details = {}) {
|
|
295
|
+
return workspaceConflict(`[vitehub] History ref "${ref}" changed before the commit.`, { details: {
|
|
296
|
+
...details,
|
|
297
|
+
ref,
|
|
298
|
+
expected,
|
|
299
|
+
actual
|
|
300
|
+
} });
|
|
301
|
+
}
|
|
302
|
+
function readHistoryFiles(files, label) {
|
|
303
|
+
if (!isRuntimeObject(files) || Array.isArray(files)) throw workspaceError(`[vitehub] History ${label} must be an object keyed by file path.`);
|
|
304
|
+
const result = /* @__PURE__ */ new Map();
|
|
305
|
+
for (const [path, value] of Object.entries(files)) {
|
|
306
|
+
const normalized = normalizeHistoryPath(path);
|
|
307
|
+
if (result.has(normalized)) throw workspaceError(`[vitehub] Duplicate history file path: ${normalized}.`);
|
|
308
|
+
const input = !hasRuntimeType(value, "string") && !isWorkspaceBytes(value) && isRuntimeObject(value) ? value : void 0;
|
|
309
|
+
const content = input ? input.content : value;
|
|
310
|
+
if (!hasRuntimeType(content, "string") && !isWorkspaceBytes(content)) throw workspaceError(`[vitehub] Invalid history file content: ${normalized}.`);
|
|
311
|
+
if (input?.mediaType !== void 0 && (!hasRuntimeType(input.mediaType, "string") || !input.mediaType)) throw workspaceError(`[vitehub] Invalid history file media type: ${normalized}.`);
|
|
312
|
+
result.set(normalized, {
|
|
313
|
+
file: {
|
|
314
|
+
path: normalized,
|
|
315
|
+
content: isWorkspaceBytes(content) ? new Uint8Array(content) : content,
|
|
316
|
+
mediaType: input?.mediaType ?? (lookup(normalized) || "application/octet-stream"),
|
|
317
|
+
metadata: copyJsonFileMetadata(normalized, input?.metadata)
|
|
318
|
+
},
|
|
319
|
+
explicitMediaType: Boolean(input && Object.hasOwn(input, "mediaType")),
|
|
320
|
+
explicitMetadata: Boolean(input && Object.hasOwn(input, "metadata"))
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
return result;
|
|
324
|
+
}
|
|
325
|
+
function readHistoryCommit(options) {
|
|
326
|
+
if (options.files === void 0 === (options.changes === void 0)) throw workspaceError("[vitehub] History commit requires exactly one of files or changes.");
|
|
327
|
+
if (options.files !== void 0) return {
|
|
328
|
+
mode: "files",
|
|
329
|
+
puts: readHistoryFiles(options.files, "files"),
|
|
330
|
+
deletes: /* @__PURE__ */ new Set()
|
|
331
|
+
};
|
|
332
|
+
const changes = options.changes;
|
|
333
|
+
if (!isRuntimeObject(changes) || Array.isArray(changes)) throw workspaceError("[vitehub] History changes must be an object with put and delete.");
|
|
334
|
+
const { put, delete: remove } = changes;
|
|
335
|
+
const puts = readHistoryFiles(put ?? {}, "changes.put");
|
|
336
|
+
if (remove !== void 0 && !Array.isArray(remove)) throw workspaceError("[vitehub] History changes.delete must be an array of file paths.");
|
|
337
|
+
const deletes = /* @__PURE__ */ new Set();
|
|
338
|
+
for (const path of remove ?? []) {
|
|
339
|
+
if (!hasRuntimeType(path, "string")) throw workspaceError("[vitehub] History changes.delete must be an array of file paths.");
|
|
340
|
+
const normalized = normalizeHistoryPath(path);
|
|
341
|
+
if (deletes.has(normalized) || puts.has(normalized)) throw workspaceError(`[vitehub] Duplicate history file path: ${normalized}.`);
|
|
342
|
+
deletes.add(normalized);
|
|
343
|
+
}
|
|
344
|
+
return {
|
|
345
|
+
mode: "changes",
|
|
346
|
+
puts,
|
|
347
|
+
deletes
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
function createWorkspaceHistory(definition, store, files) {
|
|
351
|
+
const policy = createWorkspaceWritePolicy(definition);
|
|
352
|
+
const history = () => {
|
|
353
|
+
if (!store.history) throw historyUnavailable();
|
|
354
|
+
return store.history;
|
|
355
|
+
};
|
|
356
|
+
const digest = async (bytes) => store.history?.digest ? await store.history.digest(bytes) : await sha256(bytes);
|
|
357
|
+
const digestOf = async (content) => await digest(new Uint8Array(contentToBytes(content)));
|
|
358
|
+
async function commit(options, apply) {
|
|
359
|
+
const retained = history();
|
|
360
|
+
validateHistoryMessage(options.message);
|
|
361
|
+
const ref = normalizeHistoryRef(options.ref);
|
|
362
|
+
if (options.ifHead !== null && (!hasRuntimeType(options.ifHead, "string") || !options.ifHead)) throw workspaceError("[vitehub] History commit requires ifHead to be a revision id or null.");
|
|
363
|
+
const forwarded = forwardedHistoryCommits.has(options);
|
|
364
|
+
const beforeWrite = forwarded ? policy.check : policy.before;
|
|
365
|
+
const commit = readHistoryCommit(options);
|
|
366
|
+
const metadata = copyJsonWorkspaceMetadata("history revision", options.metadata);
|
|
367
|
+
const current = await retained.head({ ref });
|
|
368
|
+
if ((current?.id ?? null) !== options.ifHead) throw historyConflict(ref, options.ifHead, current?.id ?? null);
|
|
369
|
+
if (options.startFrom !== void 0 && (!hasRuntimeType(options.startFrom, "string") || !options.startFrom || options.ifHead !== null || !store.history?.forksFrom)) throw workspaceError("[vitehub] History commit startFrom needs a revision id, ifHead null, and a Store that starts missing refs from another ref.");
|
|
370
|
+
const forkRef = current || options.startFrom ? void 0 : await store.history?.forksFrom?.(ref);
|
|
371
|
+
const startId = current?.id ?? options.startFrom ?? (forkRef ? (await retained.head({ ref: forkRef }))?.id : void 0);
|
|
372
|
+
const previous = startId ? await retained.open(startId) : void 0;
|
|
373
|
+
const previousEntries = /* @__PURE__ */ new Map();
|
|
374
|
+
for (const entry of previous ? await previous.list("", { recursive: true }) : []) if (entry.type === "file") previousEntries.set(entry.path, entry);
|
|
375
|
+
for (const path of commit.deletes) if (!previousEntries.has(path)) throw workspaceError(`[vitehub] Workspace file does not exist: ${path}.`);
|
|
376
|
+
const candidates = commit.mode === "files" ? /* @__PURE__ */ new Set([...commit.puts.keys(), ...previousEntries.keys()]) : /* @__PURE__ */ new Set([...commit.puts.keys(), ...commit.deletes]);
|
|
377
|
+
const desired = Object.create(null);
|
|
378
|
+
const changed = /* @__PURE__ */ new Set();
|
|
379
|
+
const deleted = [];
|
|
380
|
+
const inputs = [];
|
|
381
|
+
const grants = [];
|
|
382
|
+
for (const path of [...candidates].sort()) {
|
|
383
|
+
const put = commit.puts.get(path);
|
|
384
|
+
const file = put?.file;
|
|
385
|
+
const before = previousEntries.get(path);
|
|
386
|
+
const digest = before?.digest ?? (before && previous ? await digestOf(await previous.readFile(path, { encoding: "binary" })) : void 0);
|
|
387
|
+
if (put && file && before && digest === await digestOf(file.content) && (!put.explicitMediaType || file.mediaType === before.mediaType) && (!put.explicitMetadata || JSON.stringify(file.metadata) === JSON.stringify(before.metadata))) {
|
|
388
|
+
desired[path] = {
|
|
389
|
+
...file,
|
|
390
|
+
mediaType: before.mediaType,
|
|
391
|
+
metadata: before.metadata
|
|
392
|
+
};
|
|
393
|
+
continue;
|
|
394
|
+
}
|
|
395
|
+
const requested = await files.assertWritable(path);
|
|
396
|
+
const input = await beforeWrite({
|
|
397
|
+
content: file?.content,
|
|
398
|
+
mediaType: file?.mediaType,
|
|
399
|
+
metadata: file?.metadata,
|
|
400
|
+
operation: file ? "writeFile" : "rm",
|
|
401
|
+
path: requested.path,
|
|
402
|
+
previous: before,
|
|
403
|
+
ref,
|
|
404
|
+
workspace: definition.name
|
|
405
|
+
});
|
|
406
|
+
if (!forwarded) inputs.push(input);
|
|
407
|
+
try {
|
|
408
|
+
if (input.path !== requested.path || input.operation !== (file ? "writeFile" : "rm")) throw workspaceError(`[vitehub] History validators cannot rewrite file paths or operations: ${path}.`);
|
|
409
|
+
grants.push(await files.assertWritable(input.path));
|
|
410
|
+
if (file) {
|
|
411
|
+
if (input.metadata && Object.hasOwn(input.metadata, "source")) throw workspaceError("[vitehub] metadata.source is reserved for Source materialization.");
|
|
412
|
+
desired[path] = {
|
|
413
|
+
...file,
|
|
414
|
+
content: input.content ?? file.content,
|
|
415
|
+
mediaType: input.mediaType,
|
|
416
|
+
metadata: copyJsonFileMetadata(path, input.metadata)
|
|
417
|
+
};
|
|
418
|
+
changed.add(path);
|
|
419
|
+
} else deleted.push(path);
|
|
420
|
+
} catch (error) {
|
|
421
|
+
if (!forwarded) await policy.error(input, error);
|
|
422
|
+
throw error;
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
const base = {
|
|
426
|
+
ref,
|
|
427
|
+
ifHead: options.ifHead,
|
|
428
|
+
ifHeads: options.ifHeads,
|
|
429
|
+
startFrom: current ? options.startFrom : startId,
|
|
430
|
+
message: options.message,
|
|
431
|
+
metadata
|
|
432
|
+
};
|
|
433
|
+
const storeOptions = commit.mode === "files" ? {
|
|
434
|
+
...base,
|
|
435
|
+
files: desired
|
|
436
|
+
} : {
|
|
437
|
+
...base,
|
|
438
|
+
changes: {
|
|
439
|
+
put: Object.fromEntries([...changed].map((path) => [path, desired[path]])),
|
|
440
|
+
delete: deleted
|
|
441
|
+
}
|
|
442
|
+
};
|
|
443
|
+
let revision;
|
|
444
|
+
try {
|
|
445
|
+
revision = await files.requireHistoryGrants(retained, apply)(grants, storeOptions);
|
|
446
|
+
} catch (error) {
|
|
447
|
+
for (const input of inputs) await policy.error(input, error);
|
|
448
|
+
throw error;
|
|
449
|
+
}
|
|
450
|
+
for (const input of inputs) await policy.after(input);
|
|
451
|
+
return revision;
|
|
452
|
+
}
|
|
453
|
+
return {
|
|
454
|
+
digest,
|
|
455
|
+
...store.history?.forksFrom ? { forksFrom: async (ref) => await store.history.forksFrom(ref) } : {},
|
|
456
|
+
...store.history?.pullRequests ? { pullRequests: async (options) => await store.history.pullRequests(options) } : {},
|
|
457
|
+
...store.history?.merge ? { merge: async (options) => {
|
|
458
|
+
validateHistoryMessage(options.message);
|
|
459
|
+
normalizeHistoryRef(options.from);
|
|
460
|
+
normalizeHistoryRef(options.into);
|
|
461
|
+
const forwarded = forwardedHistoryMerges.get(options);
|
|
462
|
+
return await history().merge(options, async (commitOptions, apply) => {
|
|
463
|
+
const check = async (checked) => await commit(forwarded ? forwardWorkspaceHistoryFiles(checked) : checked, apply);
|
|
464
|
+
return forwarded ? await forwarded(commitOptions, check) : await check(commitOptions);
|
|
465
|
+
});
|
|
466
|
+
} } : {},
|
|
467
|
+
...store.history?.openPullRequest ? { openPullRequest: async (options) => await store.history.openPullRequest(options) } : {},
|
|
468
|
+
head: async (options) => await history().head(options),
|
|
469
|
+
list: async (options) => await history().list(options),
|
|
470
|
+
refs: async () => await history().refs(),
|
|
471
|
+
open: async (id) => await history().open(id),
|
|
472
|
+
usage: async () => await history().usage(),
|
|
473
|
+
commit
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
//#endregion
|
|
477
|
+
//#region src/storage/utils.ts
|
|
478
|
+
const snapshotStatConcurrency = 16;
|
|
479
|
+
async function mapWithConcurrency(values, concurrency, visit) {
|
|
480
|
+
const results = [];
|
|
481
|
+
let next = 0;
|
|
482
|
+
await Promise.all(Array.from({ length: Math.min(concurrency, values.length) }, async () => {
|
|
483
|
+
while (next < values.length) {
|
|
484
|
+
const index = next++;
|
|
485
|
+
results[index] = await visit(values[index]);
|
|
486
|
+
}
|
|
487
|
+
}));
|
|
488
|
+
return results;
|
|
489
|
+
}
|
|
490
|
+
async function createCurrentSnapshotFromStore(store, name) {
|
|
491
|
+
return await createSnapshotFromEntries(await mapWithConcurrency(await store.list("", { recursive: true }), snapshotStatConcurrency, async (entry) => {
|
|
492
|
+
if (entry.type !== "file" || entry.digest) return entry;
|
|
493
|
+
return await store.stat(entry.path) || entry;
|
|
494
|
+
}), name);
|
|
495
|
+
}
|
|
496
|
+
async function createSnapshotFromEntries(entries, name) {
|
|
497
|
+
const snapshotEntries = Object.create(null);
|
|
498
|
+
for (const entry of entries) snapshotEntries[entry.path] = {
|
|
499
|
+
digest: entry.digest,
|
|
500
|
+
metadata: entry.metadata,
|
|
501
|
+
size: entry.size,
|
|
502
|
+
type: entry.type
|
|
503
|
+
};
|
|
504
|
+
return {
|
|
505
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
506
|
+
entries: snapshotEntries,
|
|
507
|
+
id: await sha256({
|
|
508
|
+
entries: snapshotEntries,
|
|
509
|
+
name,
|
|
510
|
+
createdAt: Date.now()
|
|
511
|
+
}),
|
|
512
|
+
name
|
|
513
|
+
};
|
|
514
|
+
}
|
|
515
|
+
function diffSnapshots(from, to) {
|
|
516
|
+
const entries = [];
|
|
517
|
+
const keys = /* @__PURE__ */ new Set([...Object.keys(from?.entries || {}), ...Object.keys(to.entries)]);
|
|
518
|
+
for (const path of [...keys].sort()) {
|
|
519
|
+
const before = from && Object.hasOwn(from.entries, path) ? from.entries[path] : void 0;
|
|
520
|
+
const after = Object.hasOwn(to.entries, path) ? to.entries[path] : void 0;
|
|
521
|
+
if (!before && after) entries.push({
|
|
522
|
+
path,
|
|
523
|
+
type: "added",
|
|
524
|
+
after
|
|
525
|
+
});
|
|
526
|
+
else if (before && !after) entries.push({
|
|
527
|
+
path,
|
|
528
|
+
type: "removed",
|
|
529
|
+
before
|
|
530
|
+
});
|
|
531
|
+
else if (before && after && (before.digest !== void 0 && after.digest !== void 0 && before.digest !== after.digest || before.type !== after.type || before.size !== after.size || JSON.stringify(before.metadata) !== JSON.stringify(after.metadata))) entries.push({
|
|
532
|
+
path,
|
|
533
|
+
type: "modified",
|
|
534
|
+
before,
|
|
535
|
+
after
|
|
536
|
+
});
|
|
537
|
+
}
|
|
538
|
+
return {
|
|
539
|
+
from: from?.id,
|
|
540
|
+
to: to.id,
|
|
541
|
+
entries
|
|
542
|
+
};
|
|
543
|
+
}
|
|
544
|
+
//#endregion
|
|
545
|
+
export { workspaceDefinitionPlugins as _, createWorkspaceHistoryReader as a, forwardWorkspaceHistoryMerge as c, normalizeHistoryRef as d, requireWorkspaceHistory as f, registerWorkspacePlugin as g, resolveWorkspaceAutoCommit as h, createWorkspaceHistory as i, historyConflict as l, createWorkspaceWritePolicy as m, createSnapshotFromEntries as n, defaultWorkspaceHistoryRef as o, validateHistoryMessage as p, diffSnapshots as r, forwardWorkspaceHistoryFiles as s, createCurrentSnapshotFromStore as t, normalizeHistoryPath as u };
|