v8r 4.2.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,5 +1,11 @@
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
+
3
9
  ## 📦 [4.2.0](https://www.npmjs.com/package/v8r/v/4.2.0) - 2024-10-24
4
10
 
5
11
  * Add `V8R_CONFIG_FILE` environment variable.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "v8r",
3
- "version": "4.2.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/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/cli.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import fs from "node:fs";
2
2
  import os from "node:os";
3
3
  import path from "node:path";
4
- import flatCache from "flat-cache";
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 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;
@@ -7,7 +7,7 @@ const testCacheName = process.env.V8R_CACHE_NAME;
7
7
  const env = process.env;
8
8
 
9
9
  function setUp() {
10
- flatCache.clearCacheById(testCacheName);
10
+ clearCacheById(testCacheName);
11
11
  logger.resetStdout();
12
12
  logger.resetStderr();
13
13
  logger.writeOut = function () {};
@@ -16,7 +16,7 @@ function setUp() {
16
16
  }
17
17
 
18
18
  function tearDown() {
19
- flatCache.clearCacheById(testCacheName);
19
+ clearCacheById(testCacheName);
20
20
  logger.resetStdout();
21
21
  logger.resetStderr();
22
22
  logger.writeOut = origWriteOut;