v8r 4.1.0 → 4.2.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.
package/CHANGELOG.md CHANGED
@@ -1,10 +1,22 @@
1
1
  # Changelog
2
2
 
3
+ ## 📦 [4.2.1](https://www.npmjs.com/package/v8r/v/4.2.1) - 2024-12-14
4
+
5
+ * Upgrade to flat-cache 6.
6
+ This release revamps how cache is stored and invalidated internally
7
+ but should have no user-visible impact
8
+
9
+ ## 📦 [4.2.0](https://www.npmjs.com/package/v8r/v/4.2.0) - 2024-10-24
10
+
11
+ * Add `V8R_CONFIG_FILE` environment variable.
12
+ This allows loading a config file from a location other than the directory v8r is invoked from.
13
+ More info: https://chris48s.github.io/v8r/configuration/
14
+
3
15
  ## 📦 [4.1.0](https://www.npmjs.com/package/v8r/v/4.1.0) - 2024-08-25
4
16
 
5
17
  * v8r can now parse and validate files that contain multiple yaml documents
6
18
  More info: https://chris48s.github.io/v8r/usage-examples/#files-containing-multiple-documents
7
- * The `parseInputFile()` plugin hook may now tionally return an array of `Document` objects
19
+ * The `parseInputFile()` plugin hook may now conditionally return an array of `Document` objects
8
20
  * The `ValidationResult` object now contains a `documentIndex` property.
9
21
  This identifies the document when a multi-doc file has been validated.
10
22
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "v8r",
3
- "version": "4.1.0",
3
+ "version": "4.2.1",
4
4
  "description": "A command-line JSON, YAML and TOML validator that's on your wavelength",
5
5
  "scripts": {
6
6
  "test": "V8R_CACHE_NAME=v8r-test c8 --reporter=text mocha \"src/**/*.spec.js\"",
@@ -33,7 +33,7 @@
33
33
  "chalk": "^5.0.0",
34
34
  "cosmiconfig": "^9.0.0",
35
35
  "decamelize": "^6.0.0",
36
- "flat-cache": "^5.0.0",
36
+ "flat-cache": "^6.1.4",
37
37
  "glob": "^10.1.0",
38
38
  "global-agent": "^3.0.0",
39
39
  "got": "^13.0.0",
@@ -51,7 +51,7 @@
51
51
  "eslint-plugin-jsdoc": "^50.2.2",
52
52
  "eslint-plugin-mocha": "^10.0.3",
53
53
  "eslint-plugin-prettier": "^5.0.0",
54
- "mocha": "^10.7.3",
54
+ "mocha": "^11.0.1",
55
55
  "mock-cwd": "^1.0.0",
56
56
  "nock": "^13.0.4",
57
57
  "prettier": "^3.0.0",
package/src/ajv.js CHANGED
@@ -1,4 +1,4 @@
1
- import { createRequire } from "module";
1
+ import { createRequire } from "node:module";
2
2
  // TODO: once JSON modules is stable these requires could become imports
3
3
  // https://nodejs.org/api/esm.html#esm_experimental_json_modules
4
4
  const require = createRequire(import.meta.url);
package/src/bootstrap.js CHANGED
@@ -1,12 +1,12 @@
1
- import { createRequire } from "module";
1
+ import { createRequire } from "node:module";
2
2
  // TODO: once JSON modules is stable these requires could become imports
3
3
  // https://nodejs.org/api/esm.html#esm_experimental_json_modules
4
4
  const require = createRequire(import.meta.url);
5
5
 
6
+ import fs from "node:fs";
7
+ import path from "node:path";
6
8
  import { cosmiconfig } from "cosmiconfig";
7
9
  import decamelize from "decamelize";
8
- import isUrl from "is-url";
9
- import path from "path";
10
10
  import yargs from "yargs";
11
11
  import { hideBin } from "yargs/helpers";
12
12
  import {
@@ -17,27 +17,28 @@ import {
17
17
  import logger from "./logger.js";
18
18
  import { loadAllPlugins, resolveUserPlugins } from "./plugins.js";
19
19
 
20
- function preProcessConfig(configFile) {
21
- if (!configFile?.config?.customCatalog?.schemas) {
22
- return;
23
- }
24
- for (const schema of configFile.config.customCatalog.schemas) {
25
- if (!path.isAbsolute(schema.location) && !isUrl(schema.location)) {
26
- schema.location = path.join(
27
- path.dirname(configFile.filepath),
28
- schema.location,
29
- );
20
+ async function getCosmiConfig(cosmiconfigOptions) {
21
+ let configFile;
22
+
23
+ if (process.env.V8R_CONFIG_FILE) {
24
+ if (!fs.existsSync(process.env.V8R_CONFIG_FILE)) {
25
+ throw new Error(`File ${process.env.V8R_CONFIG_FILE} does not exist.`);
30
26
  }
27
+ configFile = await cosmiconfig("v8r", cosmiconfigOptions).load(
28
+ process.env.V8R_CONFIG_FILE,
29
+ );
30
+ } else {
31
+ cosmiconfigOptions.stopDir = process.cwd();
32
+ configFile = (await cosmiconfig("v8r", cosmiconfigOptions).search(
33
+ process.cwd(),
34
+ )) || { config: {} };
31
35
  }
32
- }
33
36
 
34
- async function getCosmiConfig(cosmiconfigOptions) {
35
- cosmiconfigOptions.stopDir = process.cwd();
36
- const configFile = (await cosmiconfig("v8r", cosmiconfigOptions).search(
37
- process.cwd(),
38
- )) || { config: {} };
39
37
  if (configFile.filepath) {
40
38
  logger.info(`Loaded config file from ${getRelativeFilePath(configFile)}`);
39
+ logger.info(
40
+ `Patterns and relative paths will be resolved relative to current working directory: ${process.cwd()}`,
41
+ );
41
42
  } else {
42
43
  logger.info(`No config file found`);
43
44
  }
@@ -200,7 +201,6 @@ async function bootstrap(argv, config, cosmiconfigOptions = {}) {
200
201
  // we can finish validating and processing the config
201
202
  validateConfigDocumentParsers(configFile, documentFormats);
202
203
  validateConfigOutputFormats(configFile, outputFormats);
203
- preProcessConfig(configFile);
204
204
 
205
205
  // parse command line arguments
206
206
  const args = parseArgs(argv, configFile, documentFormats, outputFormats);
@@ -213,10 +213,4 @@ async function bootstrap(argv, config, cosmiconfigOptions = {}) {
213
213
  };
214
214
  }
215
215
 
216
- export {
217
- bootstrap,
218
- getDocumentFormats,
219
- getOutputFormats,
220
- parseArgs,
221
- preProcessConfig,
222
- };
216
+ export { bootstrap, getDocumentFormats, getOutputFormats, parseArgs };
package/src/cache.js CHANGED
@@ -3,28 +3,13 @@ import logger from "./logger.js";
3
3
  import { parseSchema } from "./parser.js";
4
4
 
5
5
  class Cache {
6
- constructor(flatCache, ttl) {
6
+ constructor(flatCache) {
7
7
  this.cache = flatCache;
8
- this.ttl = ttl;
8
+ this.ttl = this.cache._cache.ttl || 0;
9
9
  this.callCounter = {};
10
10
  this.callLimit = 10;
11
11
  }
12
12
 
13
- expire() {
14
- Object.entries(this.cache.all()).forEach(
15
- function ([url, cachedResponse]) {
16
- if (!("timestamp" in cachedResponse) || !("body" in cachedResponse)) {
17
- logger.debug(`Cache error: deleting malformed response`);
18
- this.cache.removeKey(url);
19
- } else if (Date.now() > cachedResponse.timestamp + this.ttl) {
20
- logger.debug(`Cache stale: deleting cached response from ${url}`);
21
- this.cache.removeKey(url);
22
- }
23
- this.cache.save(true);
24
- }.bind(this),
25
- );
26
- }
27
-
28
13
  limitDepth(url) {
29
14
  /*
30
15
  It is possible to create cyclic dependencies with external references
@@ -51,7 +36,6 @@ class Cache {
51
36
 
52
37
  async fetch(url) {
53
38
  this.limitDepth(url);
54
- this.expire();
55
39
  const cachedResponse = this.cache.getKey(url);
56
40
  if (cachedResponse !== undefined) {
57
41
  logger.debug(`Cache hit: using cached response from ${url}`);
@@ -63,7 +47,7 @@ class Cache {
63
47
  const resp = await got(url);
64
48
  const parsedBody = parseSchema(resp.body, url);
65
49
  if (this.ttl > 0) {
66
- this.cache.setKey(url, { timestamp: Date.now(), body: parsedBody });
50
+ this.cache.setKey(url, { body: parsedBody });
67
51
  this.cache.save(true);
68
52
  }
69
53
  return parsedBody;
package/src/catalogs.js CHANGED
@@ -1,5 +1,5 @@
1
+ import path from "node:path";
1
2
  import { minimatch } from "minimatch";
2
- import path from "path";
3
3
  import { validate } from "./ajv.js";
4
4
  import { getFromUrlOrFile } from "./io.js";
5
5
  import logger from "./logger.js";
package/src/cli.js CHANGED
@@ -1,7 +1,7 @@
1
- import flatCache from "flat-cache";
2
- import fs from "fs";
3
- import os from "os";
4
- import path from "path";
1
+ import fs from "node:fs";
2
+ import os from "node:os";
3
+ import path from "node:path";
4
+ import { FlatCache } from "flat-cache";
5
5
  import isUrl from "is-url";
6
6
  import { validate } from "./ajv.js";
7
7
  import { bootstrap } from "./bootstrap.js";
@@ -27,11 +27,15 @@ function secondsToMilliseconds(seconds) {
27
27
  return seconds * 1000;
28
28
  }
29
29
 
30
- function getFlatCache() {
30
+ function getFlatCache(ttl) {
31
+ let cache;
31
32
  if (process.env.V8R_CACHE_NAME) {
32
- return flatCache.load(process.env.V8R_CACHE_NAME);
33
+ cache = new FlatCache({ cacheId: process.env.V8R_CACHE_NAME, ttl: ttl });
34
+ } else {
35
+ cache = new FlatCache({ cacheId: "v8rv2", cacheDir: CACHE_DIR, ttl: ttl });
33
36
  }
34
- return flatCache.load("v8r", CACHE_DIR);
37
+ cache.load();
38
+ return cache;
35
39
  }
36
40
 
37
41
  async function validateDocument(
@@ -180,7 +184,7 @@ function Validator() {
180
184
  filenames.sort((a, b) => a.localeCompare(b));
181
185
 
182
186
  const ttl = secondsToMilliseconds(config.cacheTtl || 0);
183
- const cache = new Cache(getFlatCache(), ttl);
187
+ const cache = new Cache(getFlatCache(ttl));
184
188
 
185
189
  let results = [];
186
190
  for (const filename of filenames) {
@@ -1,4 +1,4 @@
1
- import { createRequire } from "module";
1
+ import { createRequire } from "node:module";
2
2
  // TODO: once JSON modules is stable these requires could become imports
3
3
  // https://nodejs.org/api/esm.html#esm_experimental_json_modules
4
4
  const require = createRequire(import.meta.url);
package/src/io.js CHANGED
@@ -1,5 +1,5 @@
1
- import fs from "fs";
2
- import path from "path";
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
3
  import isUrl from "is-url";
4
4
  import { parseSchema } from "./parser.js";
5
5
 
package/src/parser.js CHANGED
@@ -1,4 +1,4 @@
1
- import path from "path";
1
+ import path from "node:path";
2
2
  import yaml from "js-yaml";
3
3
  import { Document } from "./plugins.js";
4
4
 
package/src/plugins.js CHANGED
@@ -1,4 +1,4 @@
1
- import path from "path";
1
+ import path from "node:path";
2
2
 
3
3
  /**
4
4
  * Base class for all v8r plugins.
@@ -1,24 +1,27 @@
1
- import flatCache from "flat-cache";
1
+ import { clearCacheById } from "flat-cache";
2
2
  import logger from "./logger.js";
3
3
 
4
4
  const origWriteOut = logger.writeOut;
5
5
  const origWriteErr = logger.writeErr;
6
6
  const testCacheName = process.env.V8R_CACHE_NAME;
7
+ const env = process.env;
7
8
 
8
9
  function setUp() {
9
- flatCache.clearCacheById(testCacheName);
10
+ clearCacheById(testCacheName);
10
11
  logger.resetStdout();
11
12
  logger.resetStderr();
12
13
  logger.writeOut = function () {};
13
14
  logger.writeErr = function () {};
15
+ process.env = { ...env };
14
16
  }
15
17
 
16
18
  function tearDown() {
17
- flatCache.clearCacheById(testCacheName);
19
+ clearCacheById(testCacheName);
18
20
  logger.resetStdout();
19
21
  logger.resetStderr();
20
22
  logger.writeOut = origWriteOut;
21
23
  logger.writeErr = origWriteErr;
24
+ process.env = env;
22
25
  }
23
26
 
24
27
  function isString(el) {