@vltpkg/graph 0.0.0-9 → 1.0.0-rc.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 (177) hide show
  1. package/README.md +136 -1
  2. package/dist/esm/actual/load.d.ts +49 -3
  3. package/dist/esm/actual/load.d.ts.map +1 -1
  4. package/dist/esm/actual/load.js +124 -73
  5. package/dist/esm/actual/load.js.map +1 -1
  6. package/dist/esm/browser.d.ts +8 -4
  7. package/dist/esm/browser.d.ts.map +1 -1
  8. package/dist/esm/browser.js +6 -2
  9. package/dist/esm/browser.js.map +1 -1
  10. package/dist/esm/build.d.ts +29 -0
  11. package/dist/esm/build.d.ts.map +1 -0
  12. package/dist/esm/build.js +79 -0
  13. package/dist/esm/build.js.map +1 -0
  14. package/dist/esm/dependencies.d.ts +10 -3
  15. package/dist/esm/dependencies.d.ts.map +1 -1
  16. package/dist/esm/dependencies.js +63 -0
  17. package/dist/esm/dependencies.js.map +1 -1
  18. package/dist/esm/diff.d.ts +67 -0
  19. package/dist/esm/diff.d.ts.map +1 -1
  20. package/dist/esm/diff.js +25 -0
  21. package/dist/esm/diff.js.map +1 -1
  22. package/dist/esm/edge.d.ts +7 -2
  23. package/dist/esm/edge.d.ts.map +1 -1
  24. package/dist/esm/edge.js +8 -0
  25. package/dist/esm/edge.js.map +1 -1
  26. package/dist/esm/graph.d.ts +20 -10
  27. package/dist/esm/graph.d.ts.map +1 -1
  28. package/dist/esm/graph.js +124 -21
  29. package/dist/esm/graph.js.map +1 -1
  30. package/dist/esm/ideal/add-nodes.d.ts +17 -2
  31. package/dist/esm/ideal/add-nodes.d.ts.map +1 -1
  32. package/dist/esm/ideal/add-nodes.js +9 -2
  33. package/dist/esm/ideal/add-nodes.js.map +1 -1
  34. package/dist/esm/ideal/append-nodes.d.ts +11 -1
  35. package/dist/esm/ideal/append-nodes.d.ts.map +1 -1
  36. package/dist/esm/ideal/append-nodes.js +176 -31
  37. package/dist/esm/ideal/append-nodes.js.map +1 -1
  38. package/dist/esm/ideal/build-ideal-from-starting-graph.d.ts.map +1 -1
  39. package/dist/esm/ideal/build-ideal-from-starting-graph.js +9 -5
  40. package/dist/esm/ideal/build-ideal-from-starting-graph.js.map +1 -1
  41. package/dist/esm/ideal/build.d.ts +9 -0
  42. package/dist/esm/ideal/build.d.ts.map +1 -1
  43. package/dist/esm/ideal/build.js +4 -1
  44. package/dist/esm/ideal/build.js.map +1 -1
  45. package/dist/esm/ideal/get-importer-specs.d.ts +3 -2
  46. package/dist/esm/ideal/get-importer-specs.d.ts.map +1 -1
  47. package/dist/esm/ideal/get-importer-specs.js +7 -5
  48. package/dist/esm/ideal/get-importer-specs.js.map +1 -1
  49. package/dist/esm/index.d.ts +8 -4
  50. package/dist/esm/index.d.ts.map +1 -1
  51. package/dist/esm/index.js +4 -1
  52. package/dist/esm/index.js.map +1 -1
  53. package/dist/esm/install.d.ts +8 -3
  54. package/dist/esm/install.d.ts.map +1 -1
  55. package/dist/esm/install.js +167 -20
  56. package/dist/esm/install.js.map +1 -1
  57. package/dist/esm/lockfile/load-edges.d.ts +8 -1
  58. package/dist/esm/lockfile/load-edges.d.ts.map +1 -1
  59. package/dist/esm/lockfile/load-edges.js +79 -15
  60. package/dist/esm/lockfile/load-edges.js.map +1 -1
  61. package/dist/esm/lockfile/load-nodes.d.ts +3 -2
  62. package/dist/esm/lockfile/load-nodes.d.ts.map +1 -1
  63. package/dist/esm/lockfile/load-nodes.js +72 -12
  64. package/dist/esm/lockfile/load-nodes.js.map +1 -1
  65. package/dist/esm/lockfile/load.d.ts +18 -5
  66. package/dist/esm/lockfile/load.d.ts.map +1 -1
  67. package/dist/esm/lockfile/load.js +53 -22
  68. package/dist/esm/lockfile/load.js.map +1 -1
  69. package/dist/esm/lockfile/save.d.ts +12 -3
  70. package/dist/esm/lockfile/save.d.ts.map +1 -1
  71. package/dist/esm/lockfile/save.js +49 -16
  72. package/dist/esm/lockfile/save.js.map +1 -1
  73. package/dist/esm/lockfile/types.d.ts +34 -4
  74. package/dist/esm/lockfile/types.d.ts.map +1 -1
  75. package/dist/esm/lockfile/types.js +31 -0
  76. package/dist/esm/lockfile/types.js.map +1 -1
  77. package/dist/esm/modifiers.d.ts +189 -0
  78. package/dist/esm/modifiers.d.ts.map +1 -0
  79. package/dist/esm/modifiers.js +330 -0
  80. package/dist/esm/modifiers.js.map +1 -0
  81. package/dist/esm/node.d.ts +77 -6
  82. package/dist/esm/node.d.ts.map +1 -1
  83. package/dist/esm/node.js +98 -5
  84. package/dist/esm/node.js.map +1 -1
  85. package/dist/esm/reify/add-edge.d.ts +1 -2
  86. package/dist/esm/reify/add-edge.d.ts.map +1 -1
  87. package/dist/esm/reify/add-edge.js +29 -18
  88. package/dist/esm/reify/add-edge.js.map +1 -1
  89. package/dist/esm/reify/add-edges.d.ts +1 -2
  90. package/dist/esm/reify/add-edges.d.ts.map +1 -1
  91. package/dist/esm/reify/add-edges.js +2 -3
  92. package/dist/esm/reify/add-edges.js.map +1 -1
  93. package/dist/esm/reify/add-nodes.d.ts.map +1 -1
  94. package/dist/esm/reify/add-nodes.js +4 -27
  95. package/dist/esm/reify/add-nodes.js.map +1 -1
  96. package/dist/esm/reify/bin-chmod.d.ts +11 -0
  97. package/dist/esm/reify/bin-chmod.d.ts.map +1 -0
  98. package/dist/esm/reify/bin-chmod.js +39 -0
  99. package/dist/esm/reify/bin-chmod.js.map +1 -0
  100. package/dist/esm/reify/build.d.ts +10 -1
  101. package/dist/esm/reify/build.d.ts.map +1 -1
  102. package/dist/esm/reify/build.js +36 -23
  103. package/dist/esm/reify/build.js.map +1 -1
  104. package/dist/esm/reify/calculate-save-value.d.ts +3 -0
  105. package/dist/esm/reify/calculate-save-value.d.ts.map +1 -0
  106. package/dist/esm/reify/calculate-save-value.js +45 -0
  107. package/dist/esm/reify/calculate-save-value.js.map +1 -0
  108. package/dist/esm/reify/check-needed-build.d.ts +25 -0
  109. package/dist/esm/reify/check-needed-build.d.ts.map +1 -0
  110. package/dist/esm/reify/check-needed-build.js +50 -0
  111. package/dist/esm/reify/check-needed-build.js.map +1 -0
  112. package/dist/esm/reify/delete-edge.d.ts.map +1 -1
  113. package/dist/esm/reify/delete-edge.js +3 -4
  114. package/dist/esm/reify/delete-edge.js.map +1 -1
  115. package/dist/esm/reify/extract-node.d.ts +24 -0
  116. package/dist/esm/reify/extract-node.d.ts.map +1 -0
  117. package/dist/esm/reify/extract-node.js +76 -0
  118. package/dist/esm/reify/extract-node.js.map +1 -0
  119. package/dist/esm/reify/index.d.ts +18 -1
  120. package/dist/esm/reify/index.d.ts.map +1 -1
  121. package/dist/esm/reify/index.js +84 -14
  122. package/dist/esm/reify/index.js.map +1 -1
  123. package/dist/esm/reify/internal-hoist.d.ts +9 -0
  124. package/dist/esm/reify/internal-hoist.d.ts.map +1 -0
  125. package/dist/esm/reify/internal-hoist.js +134 -0
  126. package/dist/esm/reify/internal-hoist.js.map +1 -0
  127. package/dist/esm/reify/update-importers-package-json.d.ts.map +1 -1
  128. package/dist/esm/reify/update-importers-package-json.js +15 -9
  129. package/dist/esm/reify/update-importers-package-json.js.map +1 -1
  130. package/dist/esm/remove-optional-subgraph.js +1 -1
  131. package/dist/esm/remove-optional-subgraph.js.map +1 -1
  132. package/dist/esm/resolve-save-type.d.ts +1 -2
  133. package/dist/esm/resolve-save-type.d.ts.map +1 -1
  134. package/dist/esm/resolve-save-type.js.map +1 -1
  135. package/dist/esm/stringify-node.d.ts +1 -1
  136. package/dist/esm/stringify-node.d.ts.map +1 -1
  137. package/dist/esm/stringify-node.js.map +1 -1
  138. package/dist/esm/transfer-data/load.d.ts +44 -0
  139. package/dist/esm/transfer-data/load.d.ts.map +1 -0
  140. package/dist/esm/transfer-data/load.js +176 -0
  141. package/dist/esm/transfer-data/load.js.map +1 -0
  142. package/dist/esm/uninstall.d.ts +5 -4
  143. package/dist/esm/uninstall.d.ts.map +1 -1
  144. package/dist/esm/uninstall.js +51 -19
  145. package/dist/esm/uninstall.js.map +1 -1
  146. package/dist/esm/update.d.ts +13 -0
  147. package/dist/esm/update.d.ts.map +1 -0
  148. package/dist/esm/update.js +63 -0
  149. package/dist/esm/update.js.map +1 -0
  150. package/dist/esm/virtual-root.d.ts +16 -0
  151. package/dist/esm/virtual-root.d.ts.map +1 -0
  152. package/dist/esm/virtual-root.js +79 -0
  153. package/dist/esm/virtual-root.js.map +1 -0
  154. package/dist/esm/visualization/human-readable-output.d.ts +4 -5
  155. package/dist/esm/visualization/human-readable-output.d.ts.map +1 -1
  156. package/dist/esm/visualization/human-readable-output.js +41 -18
  157. package/dist/esm/visualization/human-readable-output.js.map +1 -1
  158. package/dist/esm/visualization/json-output.d.ts +7 -3
  159. package/dist/esm/visualization/json-output.d.ts.map +1 -1
  160. package/dist/esm/visualization/json-output.js +35 -12
  161. package/dist/esm/visualization/json-output.js.map +1 -1
  162. package/dist/esm/visualization/mermaid-output.d.ts +7 -1
  163. package/dist/esm/visualization/mermaid-output.d.ts.map +1 -1
  164. package/dist/esm/visualization/mermaid-output.js +64 -9
  165. package/dist/esm/visualization/mermaid-output.js.map +1 -1
  166. package/dist/esm/visualization/object-like-output.d.ts +1 -1
  167. package/dist/esm/visualization/object-like-output.d.ts.map +1 -1
  168. package/dist/esm/visualization/object-like-output.js.map +1 -1
  169. package/package.json +30 -24
  170. package/dist/esm/reify/bin-paths.d.ts +0 -4
  171. package/dist/esm/reify/bin-paths.d.ts.map +0 -1
  172. package/dist/esm/reify/bin-paths.js +0 -23
  173. package/dist/esm/reify/bin-paths.js.map +0 -1
  174. package/dist/esm/types.d.ts +0 -42
  175. package/dist/esm/types.d.ts.map +0 -1
  176. package/dist/esm/types.js +0 -2
  177. package/dist/esm/types.js.map +0 -1
@@ -0,0 +1,189 @@
1
+ import { Spec } from '@vltpkg/spec';
2
+ import type { ModifierBreadcrumb, ModifierInteractiveBreadcrumb } from '@vltpkg/dss-breadcrumb';
3
+ import type { SpecOptions } from '@vltpkg/spec';
4
+ import type { NormalizedManifest } from '@vltpkg/types';
5
+ import type { Edge } from './edge.ts';
6
+ import type { Node } from './node.ts';
7
+ import type { Dependency } from './dependencies.ts';
8
+ /**
9
+ * Loaded modifiers configuration as described in the `vlt.json` file.
10
+ */
11
+ export type GraphModifierLoadedConfig = {
12
+ modifiers: GraphModifierConfigObject;
13
+ };
14
+ /**
15
+ * Type definition for the modifiers configuration object
16
+ */
17
+ export type GraphModifierConfigObject = Record<string, string>;
18
+ /**
19
+ * Info needed to define a graph modifier.
20
+ */
21
+ export type BaseModifierEntry = {
22
+ type: 'edge' | 'node';
23
+ query: string;
24
+ breadcrumb: ModifierBreadcrumb;
25
+ value: string | NormalizedManifest;
26
+ refs: Set<{
27
+ name: string;
28
+ from: Node;
29
+ }>;
30
+ };
31
+ /**
32
+ * Extra info to define specifically a graph edge modifier.
33
+ */
34
+ export type EdgeModifierEntry = BaseModifierEntry & {
35
+ type: 'edge';
36
+ spec: Spec;
37
+ value: string;
38
+ };
39
+ /**
40
+ * Extra info to define the graph node modifier.
41
+ */
42
+ export type NodeModifierEntry = BaseModifierEntry & {
43
+ type: 'node';
44
+ manifest: NormalizedManifest;
45
+ };
46
+ /**
47
+ * A graph modifier entry, which can be either an edge or a node modifier.
48
+ */
49
+ export type ModifierEntry = EdgeModifierEntry | NodeModifierEntry;
50
+ /**
51
+ * An object to track modifiers that have matched an initial part of the
52
+ * breadcrumb. It holds pointers to both nodes and edges matched in the
53
+ * current traversed graph on top of the modifier info and the breadcrumb
54
+ * state that is used to track the current state of the parsing.
55
+ */
56
+ export type ModifierActiveEntry = {
57
+ /**
58
+ * The modifier this active entry is working with.
59
+ */
60
+ modifier: ModifierEntry;
61
+ /**
62
+ * The breadcrumb that is used to track the current state of the parsing.
63
+ */
64
+ interactiveBreadcrumb: ModifierInteractiveBreadcrumb;
65
+ /**
66
+ * The first node to be affected by this modifier.
67
+ */
68
+ originalFrom: Node;
69
+ /**
70
+ * The original edge that is being replaced with this entry.
71
+ */
72
+ originalEdge?: Edge;
73
+ /**
74
+ * The modified edge that is being used to replace the original edge.
75
+ */
76
+ modifiedEdge?: Edge;
77
+ };
78
+ /**
79
+ * Class representing loaded modifiers configuration for a project.
80
+ *
81
+ * Instances of this class can be used as a helper to modify the graph
82
+ * during the graph build ideal traversal time.
83
+ *
84
+ * ```
85
+ * const modifier = new GraphModifier(options)
86
+ * modifier.load(options)
87
+ * ```
88
+ *
89
+ * The `tryImporter` method can be used to register the initial importer
90
+ * node along with any modifier that includes an importer selector, e.g:
91
+ * `modifier.tryImporter(graph.mainImporter)`
92
+ *
93
+ * When traversing the graph, use the `tryNewDependency` method to check
94
+ * if a given dependency spec to the current traversed node has matching
95
+ * registered modifiers, e.g:
96
+ * `const entries = modifier.tryNewDependency(fromNode, depSpec)`
97
+ *
98
+ * Use `updateActiveEntry` to update a given active modifier entry state
99
+ * with the current node of the graph being traversed. e.g:
100
+ * ```
101
+ * for (const entry of entries)
102
+ * modifier.updateActiveEntry(fromNode, entry)
103
+ * ```
104
+ */
105
+ export declare class GraphModifier {
106
+ #private;
107
+ /**
108
+ * A set of currently active modifiers, which are being parsed.
109
+ */
110
+ activeModifiers: Set<ModifierActiveEntry>;
111
+ /** A set of all modifier string values loaded from vlt.json */
112
+ modifierNames: Set<string>;
113
+ constructor(options: SpecOptions);
114
+ /**
115
+ * Load the modifiers definitions from vlt.json,
116
+ * converting the result into a GraphModifierConfigObject
117
+ */
118
+ get config(): GraphModifierConfigObject;
119
+ /**
120
+ * Loads the modifiers defined in `vlt.json` into memory.
121
+ */
122
+ load(options: SpecOptions): void;
123
+ /**
124
+ * Try matching the provided node against the top-level selectors. In case
125
+ * a match is found it will also register the active entry modifier and
126
+ * update the active entry to the current importer node.
127
+ */
128
+ tryImporter(importer: Node): void;
129
+ /**
130
+ * Try matching the provided node and spec to the current
131
+ * active parsing modifier entries along with possible starting-level
132
+ * modifiers.
133
+ *
134
+ * Any entries in which the breachcrumb have already reached its last
135
+ * element will be prioritized, along with checking for specificity,
136
+ * the complete entry with the highest specificity will be returned or just
137
+ * the entry with the highest specificity if no complete entry is found.
138
+ * Returns `undefined` if no matching entry is found.
139
+ *
140
+ * This method works with the assumption that it's going to be called
141
+ * during a graph traversal, such that any ascendent has been checked
142
+ * and the active modifier entry state has been updated in the previous
143
+ * iteration.
144
+ */
145
+ tryNewDependency(from: Node, spec: Spec): ModifierActiveEntry | undefined;
146
+ /**
147
+ * Returns the set of {@link ModifierActiveEntry} instances that matches
148
+ * the provided {@link Dependency} specs for a given node.
149
+ *
150
+ * This method is mostly a helper to {@link GraphModifier.tryNewDependency}
151
+ * that handles the registered modifiers traversal lookup.
152
+ */
153
+ tryDependencies(from: Node, dependencies: Dependency[] | Edge[]): Map<string, ModifierActiveEntry>;
154
+ /**
155
+ * Updates an active entry state keeping track of items in the multi-level
156
+ * active entries map. If the current breadcrumb state shows there's no more
157
+ * items left, then we deregister the modifier.
158
+ */
159
+ updateActiveEntry(from: Node, active: ModifierActiveEntry): void;
160
+ /**
161
+ * Creates a new active modifier.
162
+ */
163
+ newModifier(from: Node, modifier: ModifierEntry): ModifierActiveEntry;
164
+ /**
165
+ * Removes a previously registered modifier from the active entries.
166
+ */
167
+ deregisterModifier(modifier: ModifierEntry): void;
168
+ /**
169
+ * Operates in previously registered nodes and edges in order to put
170
+ * back in place any of the original edges that were referenced to in
171
+ * active (ongoing) breadcrumb parsing entries that were never completed.
172
+ *
173
+ * This method can be used to easily rollback any pending operations
174
+ * once the graph traversal is done.
175
+ */
176
+ rollbackActiveEntries(): void;
177
+ /**
178
+ * Convenience method to instantiate and load in one call.
179
+ * Returns undefined if the project does not have a vlt.json file,
180
+ * otherwise returns the loaded Modifiers instance.
181
+ */
182
+ static maybeLoad(options: SpecOptions): GraphModifier | undefined;
183
+ /**
184
+ * Convenience method to instantiate and load in one call.
185
+ * Throws if called on a directory that does not have a vlt.json file.
186
+ */
187
+ static load(options: SpecOptions): GraphModifier;
188
+ }
189
+ //# sourceMappingURL=modifiers.d.ts.map
@@ -0,0 +1 @@
1
+ {"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,IAAI;IAc7B;;;;OAIG;IACH,MAAM,CAAC,SAAS,CAAC,OAAO,EAAE,WAAW;IAMrC;;;OAGG;IACH,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,WAAW;CAGjC"}
@@ -0,0 +1,330 @@
1
+ import { parseBreadcrumb, specificitySort, } from '@vltpkg/dss-breadcrumb';
2
+ import { error } from '@vltpkg/error-cause';
3
+ import { Spec } from '@vltpkg/spec';
4
+ import { asNormalizedManifest, assertRecordStringString, normalizeManifest, } from '@vltpkg/types';
5
+ import { load } from '@vltpkg/vlt-json';
6
+ /**
7
+ * Class representing loaded modifiers configuration for a project.
8
+ *
9
+ * Instances of this class can be used as a helper to modify the graph
10
+ * during the graph build ideal traversal time.
11
+ *
12
+ * ```
13
+ * const modifier = new GraphModifier(options)
14
+ * modifier.load(options)
15
+ * ```
16
+ *
17
+ * The `tryImporter` method can be used to register the initial importer
18
+ * node along with any modifier that includes an importer selector, e.g:
19
+ * `modifier.tryImporter(graph.mainImporter)`
20
+ *
21
+ * When traversing the graph, use the `tryNewDependency` method to check
22
+ * if a given dependency spec to the current traversed node has matching
23
+ * registered modifiers, e.g:
24
+ * `const entries = modifier.tryNewDependency(fromNode, depSpec)`
25
+ *
26
+ * Use `updateActiveEntry` to update a given active modifier entry state
27
+ * with the current node of the graph being traversed. e.g:
28
+ * ```
29
+ * for (const entry of entries)
30
+ * modifier.updateActiveEntry(fromNode, entry)
31
+ * ```
32
+ */
33
+ export class GraphModifier {
34
+ /** The loaded modifiers configuration */
35
+ #config;
36
+ /** A set of all modifiers loaded from vlt.json */
37
+ #modifiers = new Set();
38
+ /** A set of all edge modifiers loaded from vlt.json */
39
+ #edgeModifiers = new Set();
40
+ /** A set of all node modifiers loaded from vlt.json */
41
+ #nodeModifiers = new Set();
42
+ /**
43
+ * A map of initial entries, keyed by the name of the first breadcrumb
44
+ * item to its modifier entry. Useful for checking for non-importer
45
+ * starting breadcrumbs, e.g: `#a > #b`
46
+ */
47
+ #initialEntries = new Map();
48
+ /**
49
+ * A multi-level map of active entries, keyed by:
50
+ * - modifiers
51
+ * - edge name
52
+ * - from node
53
+ * that allows for retrieving seen {@link ModifierActiveEntry} instances
54
+ * in constant time.
55
+ */
56
+ #activeEntries = new Map();
57
+ /**
58
+ * A set of currently active modifiers, which are being parsed.
59
+ */
60
+ activeModifiers = new Set();
61
+ /** A set of all modifier string values loaded from vlt.json */
62
+ modifierNames = new Set();
63
+ constructor(options) {
64
+ this.load(options);
65
+ }
66
+ /**
67
+ * Load the modifiers definitions from vlt.json,
68
+ * converting the result into a GraphModifierConfigObject
69
+ */
70
+ get config() {
71
+ if (this.#config)
72
+ return this.#config;
73
+ return (this.#config =
74
+ load('modifiers', assertRecordStringString) ?? {});
75
+ }
76
+ /**
77
+ * Loads the modifiers defined in `vlt.json` into memory.
78
+ */
79
+ load(options) {
80
+ for (const [key, value] of Object.entries(this.config)) {
81
+ this.modifierNames.add(key);
82
+ const breadcrumb = parseBreadcrumb(key);
83
+ /* c8 ignore start - should not be possible */
84
+ if (!breadcrumb.last.name) {
85
+ throw error('Could not find name in breadcrumb', {
86
+ found: key,
87
+ });
88
+ }
89
+ /* c8 ignore stop */
90
+ let mod;
91
+ if (typeof value === 'string') {
92
+ mod = {
93
+ breadcrumb,
94
+ query: key,
95
+ refs: new Set(),
96
+ spec: Spec.parse(breadcrumb.last.name, value, options),
97
+ type: 'edge',
98
+ value,
99
+ };
100
+ this.#edgeModifiers.add(mod);
101
+ /* c8 ignore start - TODO */
102
+ }
103
+ else {
104
+ const manifest = asNormalizedManifest(normalizeManifest(value));
105
+ mod = {
106
+ breadcrumb,
107
+ query: key,
108
+ manifest,
109
+ refs: new Set(),
110
+ type: 'node',
111
+ value: manifest,
112
+ };
113
+ this.#nodeModifiers.add(mod);
114
+ }
115
+ /* c8 ignore end */
116
+ this.#modifiers.add(mod);
117
+ // if the breadcrumb starts with an id, then add it to the
118
+ // map of initial entries, so that we can use it to match
119
+ if (breadcrumb.first.name) {
120
+ const initialSet = this.#initialEntries.get(breadcrumb.first.name) ?? new Set();
121
+ initialSet.add(mod);
122
+ this.#initialEntries.set(breadcrumb.first.name, initialSet);
123
+ }
124
+ }
125
+ }
126
+ /**
127
+ * Try matching the provided node against the top-level selectors. In case
128
+ * a match is found it will also register the active entry modifier and
129
+ * update the active entry to the current importer node.
130
+ */
131
+ tryImporter(importer) {
132
+ for (const modifier of this.#modifiers) {
133
+ // if the first item in the breadcrumb is an importer and it matches
134
+ // any of the valid top-level selectors, then register the modifier
135
+ const { first } = modifier.breadcrumb;
136
+ const matchRoot = first.value === ':root' && importer.mainImporter;
137
+ const matchWorkspace = first.value === ':workspace' && importer.importer;
138
+ const matchAny = first.value === ':project' || matchRoot || matchWorkspace;
139
+ if (first.importer && matchAny) {
140
+ const active = this.newModifier(importer, modifier);
141
+ const single = active.modifier.breadcrumb.single;
142
+ // only the importers will update the active entry right after
143
+ // registering it since tryImporter doesn't try to match from
144
+ // active dependencies
145
+ if (!single) {
146
+ this.updateActiveEntry(importer, active);
147
+ }
148
+ }
149
+ }
150
+ }
151
+ /**
152
+ * Try matching the provided node and spec to the current
153
+ * active parsing modifier entries along with possible starting-level
154
+ * modifiers.
155
+ *
156
+ * Any entries in which the breachcrumb have already reached its last
157
+ * element will be prioritized, along with checking for specificity,
158
+ * the complete entry with the highest specificity will be returned or just
159
+ * the entry with the highest specificity if no complete entry is found.
160
+ * Returns `undefined` if no matching entry is found.
161
+ *
162
+ * This method works with the assumption that it's going to be called
163
+ * during a graph traversal, such that any ascendent has been checked
164
+ * and the active modifier entry state has been updated in the previous
165
+ * iteration.
166
+ */
167
+ tryNewDependency(from, spec) {
168
+ const { name, semver } = spec;
169
+ // here we use a map instead of a set so that we can associate each
170
+ // modifier active entry with its breadcrumb so that it's easier to
171
+ // pick the correct entry when we sort breadcrbumbs by specificity
172
+ const all = new Map();
173
+ for (const modifier of this.#modifiers) {
174
+ // if an active entry is found then returns that
175
+ const entry = this.#activeEntries
176
+ .get(modifier)
177
+ ?.get(name)
178
+ ?.get(from);
179
+ if (entry) {
180
+ all.set(entry.modifier.breadcrumb, entry);
181
+ }
182
+ }
183
+ // matches the name against the initial entries, this will make it so
184
+ // that modifier queries that start with a name (e.g: #a > #b) can
185
+ // match at any point of the graph traversal
186
+ const initialSet = this.#initialEntries.get(name) ?? new Set();
187
+ for (const initial of initialSet) {
188
+ const initialEntry =
189
+ /* c8 ignore next - difficult to test branch */
190
+ this.#activeEntries.get(initial)?.get(name)?.get(from) ??
191
+ this.newModifier(from, initial);
192
+ all.set(initialEntry.modifier.breadcrumb, initialEntry);
193
+ }
194
+ // selects the active entry that should apply to this dependency,
195
+ // any active entry that is done parsing has the priority, if we
196
+ // find multiple entries then we use css specificity to pick a winner
197
+ // if we have multiple matches but no active entry is complete, then
198
+ // we pick the one with the highest specificity breadcrumb
199
+ const arr = [...all.values()];
200
+ const completeEntries = arr.filter(active => active.interactiveBreadcrumb.current ===
201
+ active.modifier.breadcrumb.last);
202
+ // deregister completed entries
203
+ for (const entry of completeEntries) {
204
+ this.deregisterModifier(entry.modifier);
205
+ }
206
+ // returns the highest specificity entry from either the complete entries
207
+ // if any were found or from any of the entries if available, otherwise
208
+ // it will return undefined as no entry is found in the `all` map
209
+ const entries = completeEntries.length ? completeEntries : arr;
210
+ return all.get(specificitySort(entries
211
+ // here we filter out any entries that do not match the
212
+ // pseudo selector comparators used in the breadcrumb item
213
+ .filter(i => i.interactiveBreadcrumb.current?.comparator({
214
+ semver,
215
+ }))
216
+ .map(i => i.modifier.breadcrumb))[0]);
217
+ }
218
+ /**
219
+ * Returns the set of {@link ModifierActiveEntry} instances that matches
220
+ * the provided {@link Dependency} specs for a given node.
221
+ *
222
+ * This method is mostly a helper to {@link GraphModifier.tryNewDependency}
223
+ * that handles the registered modifiers traversal lookup.
224
+ */
225
+ tryDependencies(from, dependencies) {
226
+ const modifierRefs = new Map();
227
+ for (const { spec } of dependencies) {
228
+ const active = this.tryNewDependency(from, spec);
229
+ if (active) {
230
+ modifierRefs.set(spec.name, active);
231
+ }
232
+ }
233
+ return modifierRefs;
234
+ }
235
+ /**
236
+ * Updates an active entry state keeping track of items in the multi-level
237
+ * active entries map. If the current breadcrumb state shows there's no more
238
+ * items left, then we deregister the modifier.
239
+ */
240
+ updateActiveEntry(from, active) {
241
+ const { modifier } = active;
242
+ const interactiveBreadcrumb = active.interactiveBreadcrumb.next();
243
+ const name = interactiveBreadcrumb.current?.name;
244
+ // if there's no name, or we're done parsing then we
245
+ // deregister the modifier instead of updating the entry
246
+ if (interactiveBreadcrumb.done || !name) {
247
+ this.deregisterModifier(modifier);
248
+ return;
249
+ }
250
+ // register the active modifier
251
+ this.activeModifiers.add(active);
252
+ active.modifier.refs.add({ from, name });
253
+ // optionally read or create the nested maps
254
+ const nameMap = this.#activeEntries.get(modifier) ??
255
+ new Map();
256
+ this.#activeEntries.set(modifier, nameMap);
257
+ const nodeMap = nameMap.get(name) ?? new Map();
258
+ nameMap.set(name, nodeMap);
259
+ // sets the active entry in the map
260
+ nodeMap.set(from, active);
261
+ }
262
+ /**
263
+ * Creates a new active modifier.
264
+ */
265
+ newModifier(from, modifier) {
266
+ return {
267
+ modifier,
268
+ interactiveBreadcrumb: modifier.breadcrumb.interactive(),
269
+ originalFrom: from,
270
+ };
271
+ }
272
+ /**
273
+ * Removes a previously registered modifier from the active entries.
274
+ */
275
+ deregisterModifier(modifier) {
276
+ for (const { from, name } of modifier.refs) {
277
+ const nodeMap = this.#activeEntries.get(modifier)?.get(name);
278
+ if (nodeMap) {
279
+ // if an entry is found, we remove it from the active set
280
+ const entry = nodeMap.get(from);
281
+ if (entry) {
282
+ this.activeModifiers.delete(entry);
283
+ }
284
+ // then we remove the entry from the map
285
+ nodeMap.delete(from);
286
+ // if the map is empty, we remove it from the active entries map
287
+ if (!nodeMap.size) {
288
+ this.#activeEntries.get(modifier)?.delete(name);
289
+ }
290
+ }
291
+ }
292
+ }
293
+ /**
294
+ * Operates in previously registered nodes and edges in order to put
295
+ * back in place any of the original edges that were referenced to in
296
+ * active (ongoing) breadcrumb parsing entries that were never completed.
297
+ *
298
+ * This method can be used to easily rollback any pending operations
299
+ * once the graph traversal is done.
300
+ */
301
+ rollbackActiveEntries() {
302
+ for (const modifier of this.activeModifiers) {
303
+ // if the modifier has an original edge, we can put it back in place
304
+ if (modifier.originalEdge) {
305
+ modifier.originalFrom.edgesOut.set(modifier.originalEdge.spec.name, modifier.originalEdge);
306
+ }
307
+ // then we deregister the modifier
308
+ this.deregisterModifier(modifier.modifier);
309
+ }
310
+ }
311
+ /**
312
+ * Convenience method to instantiate and load in one call.
313
+ * Returns undefined if the project does not have a vlt.json file,
314
+ * otherwise returns the loaded Modifiers instance.
315
+ */
316
+ static maybeLoad(options) {
317
+ const config = load('modifiers', assertRecordStringString);
318
+ if (!config)
319
+ return;
320
+ return new GraphModifier(options);
321
+ }
322
+ /**
323
+ * Convenience method to instantiate and load in one call.
324
+ * Throws if called on a directory that does not have a vlt.json file.
325
+ */
326
+ static load(options) {
327
+ return new GraphModifier(options);
328
+ }
329
+ }
330
+ //# sourceMappingURL=modifiers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"modifiers.js","sourceRoot":"","sources":["../../src/modifiers.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EACf,eAAe,GAChB,MAAM,wBAAwB,CAAA;AAC/B,OAAO,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAA;AAC3C,OAAO,EAAE,IAAI,EAAE,MAAM,cAAc,CAAA;AACnC,OAAO,EACL,oBAAoB,EACpB,wBAAwB,EACxB,iBAAiB,GAClB,MAAM,eAAe,CAAA;AACtB,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAA;AAyFvC;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,OAAO,aAAa;IACxB,yCAAyC;IACzC,OAAO,CAA4B;IACnC,kDAAkD;IAClD,UAAU,GAAG,IAAI,GAAG,EAAiB,CAAA;IACrC,uDAAuD;I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M,CAAC,IAAI,CAAC,CAAA;gBACjD,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;IAED;;;;;;;OAOG;IACH,qBAAqB;QACnB,KAAK,MAAM,QAAQ,IAAI,IAAI,CAAC,eAAe,EAAE,CAAC;YAC5C,oEAAoE;YACpE,IAAI,QAAQ,CAAC,YAAY,EAAE,CAAC;gBAC1B,QAAQ,CAAC,YAAY,CAAC,QAAQ,CAAC,GAAG,CAChC,QAAQ,CAAC,YAAY,CAAC,IAAI,CAAC,IAAI,EAC/B,QAAQ,CAAC,YAAY,CACtB,CAAA;YACH,CAAC;YACD,kCAAkC;YAClC,IAAI,CAAC,kBAAkB,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAA;QAC5C,CAAC;IACH,CAAC;IAED;;;;OAIG;IACH,MAAM,CAAC,SAAS,CAAC,OAAoB;QACnC,MAAM,MAAM,GAAG,IAAI,CAAC,WAAW,EAAE,wBAAwB,CAAC,CAAA;QAC1D,IAAI,CAAC,MAAM;YAAE,OAAM;QACnB,OAAO,IAAI,aAAa,CAAC,OAAO,CAAC,CAAA;IACnC,CAAC;IAED;;;OAGG;IACH,MAAM,CAAC,IAAI,CAAC,OAAoB;QAC9B,OAAO,IAAI,aAAa,CAAC,OAAO,CAAC,CAAA;IACnC,CAAC;CACF","sourcesContent":["import {\n parseBreadcrumb,\n specificitySort,\n} from '@vltpkg/dss-breadcrumb'\nimport { error } from '@vltpkg/error-cause'\nimport { Spec } from '@vltpkg/spec'\nimport {\n asNormalizedManifest,\n assertRecordStringString,\n normalizeManifest,\n} from '@vltpkg/types'\nimport { load } from '@vltpkg/vlt-json'\nimport type {\n ModifierBreadcrumb,\n ModifierInteractiveBreadcrumb,\n} from '@vltpkg/dss-breadcrumb'\nimport type { SpecOptions } from '@vltpkg/spec'\nimport type { NormalizedManifest } from '@vltpkg/types'\nimport type { Edge } from './edge.ts'\nimport type { Node } from './node.ts'\nimport type { Dependency } from './dependencies.ts'\n\n/**\n * Loaded modifiers configuration as described in the `vlt.json` file.\n */\nexport type GraphModifierLoadedConfig = {\n modifiers: GraphModifierConfigObject\n}\n\n/**\n * Type definition for the modifiers configuration object\n */\n// TODO: subtype string into a more specific type for Queries\nexport type GraphModifierConfigObject = Record<string, string>\n\n/**\n * Info needed to define a graph modifier.\n */\nexport type BaseModifierEntry = {\n type: 'edge' | 'node'\n query: string\n breadcrumb: ModifierBreadcrumb\n value: string | NormalizedManifest\n refs: Set<{\n name: string\n from: Node\n }>\n}\n\n/**\n * Extra info to define specifically a graph edge modifier.\n */\nexport type EdgeModifierEntry = BaseModifierEntry & {\n type: 'edge'\n spec: Spec\n value: string\n}\n\n/**\n * Extra info to define the graph node modifier.\n */\nexport type NodeModifierEntry = BaseModifierEntry & {\n type: 'node'\n manifest: NormalizedManifest\n}\n\n/**\n * A graph modifier entry, which can be either an edge or a node modifier.\n */\nexport type ModifierEntry = EdgeModifierEntry | NodeModifierEntry\n\n/**\n * An object to track modifiers that have matched an initial part of the\n * breadcrumb. It holds pointers to both nodes and edges matched in the\n * current traversed graph on top of the modifier info and the breadcrumb\n * state that is used to track the current state of the parsing.\n */\nexport type ModifierActiveEntry = {\n /**\n * The modifier this active entry is working with.\n */\n modifier: ModifierEntry\n /**\n * The breadcrumb that is used to track the current state of the parsing.\n */\n interactiveBreadcrumb: ModifierInteractiveBreadcrumb\n /**\n * The first node to be affected by this modifier.\n */\n originalFrom: Node\n /**\n * The original edge that is being replaced with this entry.\n */\n originalEdge?: Edge\n /**\n * The modified edge that is being used to replace the original edge.\n */\n modifiedEdge?: Edge\n}\n\n/**\n * Class representing loaded modifiers configuration for a project.\n *\n * Instances of this class can be used as a helper to modify the graph\n * during the graph build ideal traversal time.\n *\n * ```\n * const modifier = new GraphModifier(options)\n * modifier.load(options)\n * ```\n *\n * The `tryImporter` method can be used to register the initial importer\n * node along with any modifier that includes an importer selector, e.g:\n * `modifier.tryImporter(graph.mainImporter)`\n *\n * When traversing the graph, use the `tryNewDependency` method to check\n * if a given dependency spec to the current traversed node has matching\n * registered modifiers, e.g:\n * `const entries = modifier.tryNewDependency(fromNode, depSpec)`\n *\n * Use `updateActiveEntry` to update a given active modifier entry state\n * with the current node of the graph being traversed. e.g:\n * ```\n * for (const entry of entries)\n * modifier.updateActiveEntry(fromNode, entry)\n * ```\n */\nexport class GraphModifier {\n /** The loaded modifiers configuration */\n #config?: GraphModifierConfigObject\n /** A set of all modifiers loaded from vlt.json */\n #modifiers = new Set<ModifierEntry>()\n /** A set of all edge modifiers loaded from vlt.json */\n #edgeModifiers = new Set<EdgeModifierEntry>()\n /** A set of all node modifiers loaded from vlt.json */\n #nodeModifiers = new Set<NodeModifierEntry>()\n /**\n * A map of initial entries, keyed by the name of the first breadcrumb\n * item to its modifier entry. Useful for checking for non-importer\n * starting breadcrumbs, e.g: `#a > #b`\n */\n #initialEntries = new Map<string, Set<ModifierEntry>>()\n /**\n * A multi-level map of active entries, keyed by:\n * - modifiers\n * - edge name\n * - from node\n * that allows for retrieving seen {@link ModifierActiveEntry} instances\n * in constant time.\n */\n #activeEntries = new Map<\n ModifierEntry,\n Map<string, Map<Node, ModifierActiveEntry>>\n >()\n /**\n * A set of currently active modifiers, which are being parsed.\n */\n activeModifiers = new Set<ModifierActiveEntry>()\n /** A set of all modifier string values loaded from vlt.json */\n modifierNames = new Set<string>()\n\n constructor(options: SpecOptions) {\n this.load(options)\n }\n\n /**\n * Load the modifiers definitions from vlt.json,\n * converting the result into a GraphModifierConfigObject\n */\n get config(): GraphModifierConfigObject {\n if (this.#config) return this.#config\n return (this.#config =\n load('modifiers', assertRecordStringString) ?? {})\n }\n\n /**\n * Loads the modifiers defined in `vlt.json` into memory.\n */\n load(options: SpecOptions) {\n for (const [key, value] of Object.entries(this.config)) {\n this.modifierNames.add(key)\n const breadcrumb = parseBreadcrumb(key)\n /* c8 ignore start - should not be possible */\n if (!breadcrumb.last.name) {\n throw error('Could not find name in breadcrumb', {\n found: key,\n })\n }\n /* c8 ignore stop */\n let mod: ModifierEntry\n if (typeof value === 'string') {\n mod = {\n breadcrumb,\n query: key,\n refs: new Set(),\n spec: Spec.parse(breadcrumb.last.name, value, options),\n type: 'edge',\n value,\n } satisfies EdgeModifierEntry\n this.#edgeModifiers.add(mod)\n /* c8 ignore start - TODO */\n } else {\n const manifest = asNormalizedManifest(\n normalizeManifest(value),\n )\n mod = {\n breadcrumb,\n query: key,\n manifest,\n refs: new Set(),\n type: 'node',\n value: manifest,\n } satisfies NodeModifierEntry\n this.#nodeModifiers.add(mod)\n }\n /* c8 ignore end */\n this.#modifiers.add(mod)\n // if the breadcrumb starts with an id, then add it to the\n // map of initial entries, so that we can use it to match\n if (breadcrumb.first.name) {\n const initialSet =\n this.#initialEntries.get(breadcrumb.first.name) ?? new Set()\n initialSet.add(mod)\n this.#initialEntries.set(breadcrumb.first.name, initialSet)\n }\n }\n }\n\n /**\n * Try matching the provided node against the top-level selectors. In case\n * a match is found it will also register the active entry modifier and\n * update the active entry to the current importer node.\n */\n tryImporter(importer: Node) {\n for (const modifier of this.#modifiers) {\n // if the first item in the breadcrumb is an importer and it matches\n // any of the valid top-level selectors, then register the modifier\n const { first } = modifier.breadcrumb\n const matchRoot =\n first.value === ':root' && importer.mainImporter\n const matchWorkspace =\n first.value === ':workspace' && importer.importer\n const matchAny =\n first.value === ':project' || matchRoot || matchWorkspace\n if (first.importer && matchAny) {\n const active = this.newModifier(importer, modifier)\n const single = active.modifier.breadcrumb.single\n // only the importers will update the active entry right after\n // registering it since tryImporter doesn't try to match from\n // active dependencies\n if (!single) {\n this.updateActiveEntry(importer, active)\n }\n }\n }\n }\n\n /**\n * Try matching the provided node and spec to the current\n * active parsing modifier entries along with possible starting-level\n * modifiers.\n *\n * Any entries in which the breachcrumb have already reached its last\n * element will be prioritized, along with checking for specificity,\n * the complete entry with the highest specificity will be returned or just\n * the entry with the highest specificity if no complete entry is found.\n * Returns `undefined` if no matching entry is found.\n *\n * This method works with the assumption that it's going to be called\n * during a graph traversal, such that any ascendent has been checked\n * and the active modifier entry state has been updated in the previous\n * iteration.\n */\n tryNewDependency(\n from: Node,\n spec: Spec,\n ): ModifierActiveEntry | undefined {\n const { name, semver } = spec\n // here we use a map instead of a set so that we can associate each\n // modifier active entry with its breadcrumb so that it's easier to\n // pick the correct entry when we sort breadcrbumbs by specificity\n const all = new Map<\n ModifierBreadcrumb | undefined,\n ModifierActiveEntry\n >()\n for (const modifier of this.#modifiers) {\n // if an active entry is found then returns that\n const entry = this.#activeEntries\n .get(modifier)\n ?.get(name)\n ?.get(from)\n if (entry) {\n all.set(entry.modifier.breadcrumb, entry)\n }\n }\n // matches the name against the initial entries, this will make it so\n // that modifier queries that start with a name (e.g: #a > #b) can\n // match at any point of the graph traversal\n const initialSet =\n this.#initialEntries.get(name) ?? new Set<ModifierEntry>()\n for (const initial of initialSet) {\n const initialEntry =\n /* c8 ignore next - difficult to test branch */\n this.#activeEntries.get(initial)?.get(name)?.get(from) ??\n this.newModifier(from, initial)\n all.set(initialEntry.modifier.breadcrumb, initialEntry)\n }\n // selects the active entry that should apply to this dependency,\n // any active entry that is done parsing has the priority, if we\n // find multiple entries then we use css specificity to pick a winner\n // if we have multiple matches but no active entry is complete, then\n // we pick the one with the highest specificity breadcrumb\n const arr = [...all.values()]\n const completeEntries = arr.filter(\n active =>\n active.interactiveBreadcrumb.current ===\n active.modifier.breadcrumb.last,\n )\n // deregister completed entries\n for (const entry of completeEntries) {\n this.deregisterModifier(entry.modifier)\n }\n // returns the highest specificity entry from either the complete entries\n // if any were found or from any of the entries if available, otherwise\n // it will return undefined as no entry is found in the `all` map\n const entries = completeEntries.length ? completeEntries : arr\n return all.get(\n specificitySort(\n entries\n // here we filter out any entries that do not match the\n // pseudo selector comparators used in the breadcrumb item\n .filter(i =>\n i.interactiveBreadcrumb.current?.comparator({\n semver,\n }),\n )\n .map(i => i.modifier.breadcrumb),\n )[0],\n )\n }\n\n /**\n * Returns the set of {@link ModifierActiveEntry} instances that matches\n * the provided {@link Dependency} specs for a given node.\n *\n * This method is mostly a helper to {@link GraphModifier.tryNewDependency}\n * that handles the registered modifiers traversal lookup.\n */\n tryDependencies(\n from: Node,\n dependencies: Dependency[] | Edge[],\n ): Map<string, ModifierActiveEntry> {\n const modifierRefs = new Map<string, ModifierActiveEntry>()\n for (const { spec } of dependencies) {\n const active = this.tryNewDependency(from, spec)\n if (active) {\n modifierRefs.set(spec.name, active)\n }\n }\n return modifierRefs\n }\n\n /**\n * Updates an active entry state keeping track of items in the multi-level\n * active entries map. If the current breadcrumb state shows there's no more\n * items left, then we deregister the modifier.\n */\n updateActiveEntry(from: Node, active: ModifierActiveEntry): void {\n const { modifier } = active\n const interactiveBreadcrumb = active.interactiveBreadcrumb.next()\n const name = interactiveBreadcrumb.current?.name\n\n // if there's no name, or we're done parsing then we\n // deregister the modifier instead of updating the entry\n if (interactiveBreadcrumb.done || !name) {\n this.deregisterModifier(modifier)\n return\n }\n\n // register the active modifier\n this.activeModifiers.add(active)\n active.modifier.refs.add({ from, name })\n\n // optionally read or create the nested maps\n const nameMap =\n this.#activeEntries.get(modifier) ??\n new Map<string, Map<Node, ModifierActiveEntry>>()\n this.#activeEntries.set(modifier, nameMap)\n const nodeMap =\n nameMap.get(name) ?? new Map<Node, ModifierActiveEntry>()\n nameMap.set(name, nodeMap)\n\n // sets the active entry in the map\n nodeMap.set(from, active)\n }\n\n /**\n * Creates a new active modifier.\n */\n newModifier(\n from: Node,\n modifier: ModifierEntry,\n ): ModifierActiveEntry {\n return {\n modifier,\n interactiveBreadcrumb: modifier.breadcrumb.interactive(),\n originalFrom: from,\n }\n }\n\n /**\n * Removes a previously registered modifier from the active entries.\n */\n deregisterModifier(modifier: ModifierEntry): void {\n for (const { from, name } of modifier.refs) {\n const nodeMap = this.#activeEntries.get(modifier)?.get(name)\n if (nodeMap) {\n // if an entry is found, we remove it from the active set\n const entry = nodeMap.get(from)\n if (entry) {\n this.activeModifiers.delete(entry)\n }\n // then we remove the entry from the map\n nodeMap.delete(from)\n // if the map is empty, we remove it from the active entries map\n if (!nodeMap.size) {\n this.#activeEntries.get(modifier)?.delete(name)\n }\n }\n }\n }\n\n /**\n * Operates in previously registered nodes and edges in order to put\n * back in place any of the original edges that were referenced to in\n * active (ongoing) breadcrumb parsing entries that were never completed.\n *\n * This method can be used to easily rollback any pending operations\n * once the graph traversal is done.\n */\n rollbackActiveEntries(): void {\n for (const modifier of this.activeModifiers) {\n // if the modifier has an original edge, we can put it back in place\n if (modifier.originalEdge) {\n modifier.originalFrom.edgesOut.set(\n modifier.originalEdge.spec.name,\n modifier.originalEdge,\n )\n }\n // then we deregister the modifier\n this.deregisterModifier(modifier.modifier)\n }\n }\n\n /**\n * Convenience method to instantiate and load in one call.\n * Returns undefined if the project does not have a vlt.json file,\n * otherwise returns the loaded Modifiers instance.\n */\n static maybeLoad(options: SpecOptions) {\n const config = load('modifiers', assertRecordStringString)\n if (!config) return\n return new GraphModifier(options)\n }\n\n /**\n * Convenience method to instantiate and load in one call.\n * Throws if called on a directory that does not have a vlt.json file.\n */\n static load(options: SpecOptions) {\n return new GraphModifier(options)\n }\n}\n"]}