yamlctl 0.1.6 → 0.1.7
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 +1 -0
- package/package.json +1 -1
- package/src/schema.js +17 -6
- package/src/store.js +34 -9
package/README.md
CHANGED
|
@@ -193,6 +193,7 @@ Without any schema, it still edits the file, checks nothing, and says so.
|
|
|
193
193
|
A file naming a schema that cannot be loaded is read with a warning and never written, since checking nothing where the file asked for a check is worse than stopping.
|
|
194
194
|
|
|
195
195
|
Drafts 07, 2019-09 and 2020-12 are supported, and a schema can be split across files and addresses: every `$ref` is followed, relative to the document's `$id` when it has one.
|
|
196
|
+
A local copy of a published schema works as the published schema, and its relative `$ref`s are read from the files beside it, without the network.
|
|
196
197
|
|
|
197
198
|
A schema named by an `https://` address, the ones [SchemaStore](https://www.schemastore.org/) publishes for instance, is fetched along with every remote document its `$ref`s reach:
|
|
198
199
|
|
package/package.json
CHANGED
package/src/schema.js
CHANGED
|
@@ -80,8 +80,9 @@ export class Schema {
|
|
|
80
80
|
this.root = root;
|
|
81
81
|
this.uri = uri;
|
|
82
82
|
this.path = displayUri(uri);
|
|
83
|
-
this.documents = new Map(
|
|
84
|
-
this.bases = new Map(
|
|
83
|
+
this.documents = new Map();
|
|
84
|
+
this.bases = new Map();
|
|
85
|
+
this.keep(root, uri);
|
|
85
86
|
this.missing = [];
|
|
86
87
|
const draft = String(root.$schema ?? '');
|
|
87
88
|
this.Validator = validatorOf(draft);
|
|
@@ -90,7 +91,7 @@ export class Schema {
|
|
|
90
91
|
|
|
91
92
|
// Every document a reference can reach is given to the validator up front, since it compiles synchronously and cannot go looking for one halfway through.
|
|
92
93
|
const added = new Set([root]);
|
|
93
|
-
const queue = referencedDocuments(root,
|
|
94
|
+
const queue = referencedDocuments(root, uri);
|
|
94
95
|
while (queue.length) {
|
|
95
96
|
const next = queue.shift();
|
|
96
97
|
if (this.documents.has(next)) continue;
|
|
@@ -100,14 +101,24 @@ export class Schema {
|
|
|
100
101
|
continue;
|
|
101
102
|
}
|
|
102
103
|
const document = documentAt(next);
|
|
103
|
-
this.
|
|
104
|
-
this.bases.set(next, baseOf(document, next));
|
|
104
|
+
this.keep(document, next);
|
|
105
105
|
if (!added.has(document)) {
|
|
106
106
|
added.add(document);
|
|
107
107
|
this.added.push([document, next]);
|
|
108
108
|
this.ajv.addSchema(document, next);
|
|
109
109
|
}
|
|
110
|
-
queue.push(...referencedDocuments(document,
|
|
110
|
+
queue.push(...referencedDocuments(document, next));
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// keeps a document under its address and under its `$id`, because a `#` reference in it resolves against the `$id`
|
|
115
|
+
keep(document, uri) {
|
|
116
|
+
const base = baseOf(document, uri);
|
|
117
|
+
this.documents.set(uri, document);
|
|
118
|
+
this.bases.set(uri, base);
|
|
119
|
+
if (!this.documents.has(base)) {
|
|
120
|
+
this.documents.set(base, document);
|
|
121
|
+
this.bases.set(base, base);
|
|
111
122
|
}
|
|
112
123
|
}
|
|
113
124
|
|
package/src/store.js
CHANGED
|
@@ -10,6 +10,8 @@ import { fileURLToPath } from 'node:url';
|
|
|
10
10
|
|
|
11
11
|
const documents = new Map();
|
|
12
12
|
const failures = new Map();
|
|
13
|
+
// maps a published address to the local file that replaces it, so that a schema copied to disk works without the network
|
|
14
|
+
const copies = new Map();
|
|
13
15
|
|
|
14
16
|
// A day, long enough that a pipeline running every few minutes does not download the same schema each time, short enough that a published fix arrives the next day.
|
|
15
17
|
export const CACHE_TTL_MS = 24 * 60 * 60 * 1000;
|
|
@@ -25,8 +27,9 @@ export function cacheDir(env = process.env) {
|
|
|
25
27
|
// The document at a URI without its fragment, or an error saying why it is not there.
|
|
26
28
|
export function documentAt(uri) {
|
|
27
29
|
if (documents.has(uri)) return documents.get(uri);
|
|
28
|
-
|
|
29
|
-
|
|
30
|
+
const at = locationOf(uri);
|
|
31
|
+
if (at.startsWith('file:')) {
|
|
32
|
+
const path = fileURLToPath(at);
|
|
30
33
|
let parsed;
|
|
31
34
|
try {
|
|
32
35
|
parsed = JSON.parse(readFileSync(path, 'utf8'));
|
|
@@ -43,10 +46,16 @@ export function documentAt(uri) {
|
|
|
43
46
|
|
|
44
47
|
export function hasDocument(uri) {
|
|
45
48
|
if (documents.has(uri)) return true;
|
|
46
|
-
|
|
49
|
+
const at = locationOf(uri);
|
|
50
|
+
if (at.startsWith('file:')) return existsSync(fileURLToPath(at));
|
|
47
51
|
return false;
|
|
48
52
|
}
|
|
49
53
|
|
|
54
|
+
// gives the local copy of a URI when there is one, because a published schema can be read from disk
|
|
55
|
+
export function locationOf(uri) {
|
|
56
|
+
return copies.get(uri) ?? uri;
|
|
57
|
+
}
|
|
58
|
+
|
|
50
59
|
// For a test, or a caller holding a schema that is not in a file.
|
|
51
60
|
export function registerDocument(uri, document) {
|
|
52
61
|
documents.set(uri, document);
|
|
@@ -55,6 +64,7 @@ export function registerDocument(uri, document) {
|
|
|
55
64
|
export function forget() {
|
|
56
65
|
documents.clear();
|
|
57
66
|
failures.clear();
|
|
67
|
+
copies.clear();
|
|
58
68
|
}
|
|
59
69
|
|
|
60
70
|
// The URI a document's relative references resolve against: its own `$id` when it has one, as the specification says, else where it was read from.
|
|
@@ -69,8 +79,14 @@ export function baseOf(document, uri) {
|
|
|
69
79
|
}
|
|
70
80
|
}
|
|
71
81
|
|
|
72
|
-
//
|
|
73
|
-
|
|
82
|
+
// finds every `$ref` in a document, resolved against its base and without the fragment: these are the documents that a validation can reach.
|
|
83
|
+
// `uri` is the address of the document, and `location` is where it was read from.
|
|
84
|
+
// treats a document on disk with an `$id` at another address as a copy of a published schema, as a package ships it.
|
|
85
|
+
// reads a relative reference from the file beside the copy when that file exists, because it has the same version as the copy and a copy is often used without the network.
|
|
86
|
+
export function referencedDocuments(document, uri, location = locationOf(uri)) {
|
|
87
|
+
const base = baseOf(document, uri);
|
|
88
|
+
const local = location.startsWith('file:') && base !== location;
|
|
89
|
+
if (local && !copies.has(base)) copies.set(base, location);
|
|
74
90
|
const found = new Set();
|
|
75
91
|
const visit = (node) => {
|
|
76
92
|
if (Array.isArray(node)) return node.forEach(visit);
|
|
@@ -78,7 +94,9 @@ export function referencedDocuments(document, base) {
|
|
|
78
94
|
for (const [key, value] of Object.entries(node)) {
|
|
79
95
|
if (key === '$ref' && typeof value === 'string' && !value.startsWith('#')) {
|
|
80
96
|
try {
|
|
81
|
-
|
|
97
|
+
const target = new URL(value, base).href.split('#', 1)[0];
|
|
98
|
+
found.add(target);
|
|
99
|
+
if (local) copyBeside(target, value, location);
|
|
82
100
|
} catch {
|
|
83
101
|
// A reference that is not a URL cannot be followed, and validation says so when it reaches it.
|
|
84
102
|
}
|
|
@@ -89,6 +107,13 @@ export function referencedDocuments(document, base) {
|
|
|
89
107
|
return [...found];
|
|
90
108
|
}
|
|
91
109
|
|
|
110
|
+
// records the file beside a local copy as the copy of a published address, but only when that file exists, so that a missing file is still reported under the published address
|
|
111
|
+
function copyBeside(target, reference, location) {
|
|
112
|
+
if (copies.has(target)) return;
|
|
113
|
+
const beside = new URL(reference, location).href.split('#', 1)[0];
|
|
114
|
+
if (beside !== target && beside.startsWith('file:') && existsSync(fileURLToPath(beside))) copies.set(target, beside);
|
|
115
|
+
}
|
|
116
|
+
|
|
92
117
|
// Fetches every remote document reachable from the URIs given, following `$ref`s through local and remote documents alike, and keeps each one in the cache.
|
|
93
118
|
// A document the cache holds and that is younger than the TTL is not fetched again; `refresh` fetches regardless, `offline` never fetches.
|
|
94
119
|
// A fetch that fails falls back to a cached copy of any age, said in a warning.
|
|
@@ -104,18 +129,18 @@ async function prefetchLevel(uris, seen, options) {
|
|
|
104
129
|
const warnings = new Map(uris.map((uri) => [uri, []]));
|
|
105
130
|
const reached = await Promise.all(uris.map((uri) => reachableDocument(uri, { ...options, warn: (text) => warnings.get(uri).push(text) })));
|
|
106
131
|
for (const text of [...warnings.values()].flat()) options.warn(text);
|
|
107
|
-
const next = new Set(reached.flatMap((document, i) => (document ? referencedDocuments(document,
|
|
132
|
+
const next = new Set(reached.flatMap((document, i) => (document ? referencedDocuments(document, uris[i]) : [])).filter((uri) => !seen.has(uri)));
|
|
108
133
|
for (const uri of next) seen.add(uri);
|
|
109
134
|
await prefetchLevel([...next], seen, options);
|
|
110
135
|
}
|
|
111
136
|
|
|
112
137
|
async function reachableDocument(uri, options) {
|
|
113
|
-
if (isRemote(uri)) {
|
|
138
|
+
if (isRemote(locationOf(uri))) {
|
|
114
139
|
const document = await remoteDocument(uri, options);
|
|
115
140
|
if (document) documents.set(uri, document);
|
|
116
141
|
return document;
|
|
117
142
|
}
|
|
118
|
-
if (!uri.startsWith('file:') || !hasDocument(uri)) return null;
|
|
143
|
+
if (!locationOf(uri).startsWith('file:') || !hasDocument(uri)) return null;
|
|
119
144
|
try {
|
|
120
145
|
return documentAt(uri);
|
|
121
146
|
} catch {
|