filegrc 0.3.4 → 0.5.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/src/mutation.js CHANGED
@@ -3,6 +3,7 @@ import { resolveWorkspaceRoot } from "./paths.js";
3
3
 
4
4
  const mutationQueues = new Map();
5
5
  const activeMutation = new AsyncLocalStorage();
6
+ const deferredValidation = new AsyncLocalStorage();
6
7
 
7
8
  export function serializeWorkspaceMutation(input, task) {
8
9
  const root = resolveWorkspaceRoot(input);
@@ -16,3 +17,44 @@ export function serializeWorkspaceMutation(input, task) {
16
17
  mutationQueues.set(root, tracked);
17
18
  return tracked;
18
19
  }
20
+
21
+ export function withDeferredWorkspaceValidation(task) {
22
+ return deferredValidation.run(true, task);
23
+ }
24
+
25
+ export function workspaceValidationDeferred() {
26
+ return deferredValidation.getStore() === true;
27
+ }
28
+
29
+ export function normalizeResourceMutation(value, options = {}) {
30
+ if (!value || Array.isArray(value) || typeof value !== "object") {
31
+ throw new Error("A { record, content, revision, contentRevisions } mutation object is required.");
32
+ }
33
+ if (!Object.hasOwn(value, "record")) {
34
+ throw new Error("A mutation envelope with a record property is required.");
35
+ }
36
+ if (!value.record || Array.isArray(value.record) || typeof value.record !== "object") {
37
+ throw new Error("Mutation record must be a JSON object.");
38
+ }
39
+ if (value.content !== undefined && (Array.isArray(value.content) || typeof value.content !== "object" || value.content === null)) {
40
+ throw new Error("Mutation content must be an object keyed by Markdown slot.");
41
+ }
42
+ if (value.revision !== undefined && typeof value.revision !== "string") {
43
+ throw new Error("Mutation revision must be a string.");
44
+ }
45
+ if (options.requireRevision && (typeof value.revision !== "string" || value.revision.length === 0)) {
46
+ throw new Error("Mutation revision is required when updating a resource.");
47
+ }
48
+ if (
49
+ value.contentRevisions !== undefined
50
+ && (Array.isArray(value.contentRevisions) || typeof value.contentRevisions !== "object" || value.contentRevisions === null)
51
+ ) {
52
+ throw new Error("Mutation contentRevisions must be an object keyed by data-relative Markdown path.");
53
+ }
54
+ return {
55
+ record: value.record,
56
+ content: value.content,
57
+ revision: value.revision,
58
+ contentRevisions: value.contentRevisions
59
+ };
60
+ }