Skip to content

Commit

Permalink
style: minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sjinks committed Apr 15, 2024
1 parent 997edcc commit 9d18a28
Showing 1 changed file with 26 additions and 23 deletions.
49 changes: 26 additions & 23 deletions lib/cache.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
'use strict';

// Modules
const fs = require('fs');
const jsonfile = require('jsonfile');
const {mkdirSync, unlinkSync} = require('node:fs');
const {readFileSync, writeFileSync} = require('jsonfile');
const Log = require('./logger');
const NodeCache = require('node-cache');
const os = require('os');
const path = require('path');
const {tmpdir} = require('node:os');
const {join} = require('node:path');

/*
/**
* Creates a new Cache instance.
*
* @property {Log} log A log instance
* @property {string} cacheDir The directory to store cache files in
*/
class Cache extends NodeCache {
constructor({log = new Log(), cacheDir = path.join(os.tmpdir(), '.cache')} = {}) {
module.exports = class Cache extends NodeCache {
/**
* @param {Object} [opts] Options to pass into the cache
* @param {Log} [opts.log] A log instance
* @param {string} [opts.cacheDir] The directory to store cache files in
*/
constructor({log = new Log(), cacheDir = join(tmpdir(), '.cache')} = {}) {
// Get the nodecache opts
super();
// Set some things
this.log = log;
this.cacheDir = cacheDir;
// Ensure the cache dir exists
fs.mkdirSync(this.cacheDir, {recursive: true});
mkdirSync(this.cacheDir, {recursive: true});
};

/**
Expand All @@ -45,18 +53,16 @@ class Cache extends NodeCache {
set(key, data, {persist = false, ttl = 0} = {}) {
// Unsafe cache key patterns
const patterns = {
controlRe: /[\x00-\x1f\x80-\x9f]/g,
illegalRe: /[/?<>\\*|":]/g,
controlRe: /[\x00-\x1f\x80-\x9f]/g, // NOSONAR
illegalRe: /[/?<>\\*|":]/g, // NOSONAR
reservedRe: /^\.+$/,
windowsReservedRe: /^(con|prn|aux|nul|com\d|lpt\d)(\..*)?$/i,
windowsTrailingRe: /[. ]+$/,
};

Object.values(patterns).forEach(pattern => {
if (pattern.test(key)) {
throw new Error(`Invalid cache key: ${key}`);
}
});
if (Object.values(patterns).some(pattern => pattern.test(key))) {
throw new Error(`Invalid cache key: ${key}`);
}

// Try to set cache
if (super.set(key, data, ttl)) {
Expand All @@ -66,7 +72,9 @@ class Cache extends NodeCache {
}

// And add to file if we have persistence
if (persist) jsonfile.writeFileSync(path.join(this.cacheDir, key), data);
if (persist) {
writeFileSync(join(this.cacheDir, key), data);
}
};

/**
Expand All @@ -91,7 +99,7 @@ class Cache extends NodeCache {
} else {
try {
this.log.debug('Trying to retrieve from file cache with key %s', key);
return jsonfile.readFileSync(path.join(this.cacheDir, key));
return readFileSync(join(this.cacheDir, key));
} catch (e) {
this.log.debug('File cache miss with key %s', key);
}
Expand All @@ -115,14 +123,9 @@ class Cache extends NodeCache {

// Also remove file if applicable
try {
fs.unlinkSync(path.join(this.cacheDir, key));
unlinkSync(join(this.cacheDir, key));
} catch (e) {
this.log.debug('No file cache with key %s', key);
}
};
};

/*
* Return the class
*/
module.exports = Cache;

0 comments on commit 9d18a28

Please sign in to comment.