@axah/cache
Advanced tools
Comparing version 2.1.0 to 3.0.0
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
async function createBackend() { | ||
return { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
async get(_key) { | ||
return undefined; | ||
}, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
async set(_key, value, _ttl) { | ||
return value; | ||
}, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
async delete(_key) { | ||
// no-op | ||
}, | ||
}; | ||
} | ||
exports.default = createBackend; | ||
/* eslint-disable flowtype/no-weak-types */ | ||
async function createBackend() { | ||
return { | ||
// eslint-disable-next-line no-unused-vars | ||
async get(key) { | ||
return undefined; | ||
}, | ||
// eslint-disable-next-line no-unused-vars | ||
async set(key, value, ttl) { | ||
return value; | ||
}, | ||
// eslint-disable-next-line no-unused-vars | ||
async delete(key) {// no-op | ||
} | ||
}; | ||
} | ||
//# sourceMappingURL=disabled.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
async function createBackend() { | ||
const cache = new Map(); | ||
return { | ||
async get(key) { | ||
const entry = cache.get(key); | ||
if (entry) { | ||
return entry.value; | ||
} | ||
return undefined; | ||
}, | ||
async set(key, value, ttl) { | ||
const existing = cache.get(key); | ||
if (existing) { | ||
clearTimeout(existing.timeout); | ||
} | ||
const entry = { | ||
value, | ||
timeout: setTimeout(() => { | ||
cache.delete(key); | ||
}, 1000 * ttl), | ||
}; | ||
cache.set(key, entry); | ||
return value; | ||
}, | ||
async delete(key) { | ||
cache.delete(key); | ||
}, | ||
}; | ||
} | ||
exports.default = createBackend; | ||
/* eslint-disable flowtype/no-weak-types */ | ||
async function createBackend() { | ||
const cache = new Map(); | ||
return { | ||
async get(key) { | ||
const entry = cache.get(key); | ||
if (entry) { | ||
return entry.value; | ||
} | ||
return undefined; | ||
}, | ||
async set(key, value, ttl) { | ||
const existing = cache.get(key); | ||
if (existing) { | ||
clearTimeout(existing.timeout); | ||
} | ||
const entry = { | ||
value, | ||
timeout: setTimeout(() => { | ||
cache.delete(key); | ||
}, 1000 * ttl) | ||
}; | ||
cache.set(key, entry); | ||
return value; | ||
}, | ||
async delete(key) { | ||
cache.delete(key); | ||
} | ||
}; | ||
} | ||
//# sourceMappingURL=memory.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = createBackend; | ||
var _redis = require("redis"); | ||
var _readSecret = _interopRequireDefault(require("../utils/read-secret")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/* eslint-disable flowtype/no-weak-types */ | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const redis_1 = require("redis"); | ||
const read_secret_1 = __importDefault(require("../utils/read-secret")); | ||
async function createBackend(opts) { | ||
let password; | ||
const { | ||
passwordFile | ||
} = opts; | ||
if (passwordFile) { | ||
password = await (0, _readSecret.default)(passwordFile); | ||
} | ||
const client = await new Promise((res, rej) => { | ||
// $FlowFixMe | ||
const options = passwordFile ? { ...opts, | ||
password, | ||
passwordFile: undefined | ||
} : opts; | ||
const c = (0, _redis.createClient)(options); | ||
const onError = err => { | ||
c.off('connect', onConnect); // eslint-disable-line no-use-before-define | ||
rej(err); | ||
let password; | ||
const { passwordFile } = opts; | ||
if (passwordFile) { | ||
password = await (0, read_secret_1.default)(passwordFile); | ||
} | ||
const options = passwordFile | ||
? { | ||
...opts, | ||
password, | ||
passwordFile: undefined, | ||
} | ||
: opts; | ||
const client = (0, redis_1.createClient)(options); | ||
await client.connect(); | ||
return { | ||
async get(key) { | ||
const value = await client.get(key); | ||
return value !== null ? JSON.parse(value) : undefined; | ||
}, | ||
async set(key, value, ttl) { | ||
if (value == null) { | ||
return this.delete(key).then(() => value); | ||
} | ||
const redisTtlKey = ttl < 1 ? 'PX' : 'EX'; | ||
const redisTtl = ttl < 1 ? Math.round(ttl * 1000) : ttl; | ||
await client.set(key, JSON.stringify(value), { | ||
[redisTtlKey]: redisTtl, | ||
}); | ||
return value; | ||
}, | ||
async delete(key) { | ||
await client.del(key); | ||
}, | ||
}; | ||
const onConnect = () => { | ||
c.off('error', onError); | ||
res(c); | ||
}; | ||
c.once('connect', onConnect); | ||
c.once('error', onError); | ||
}); | ||
return { | ||
get(key) { | ||
return new Promise((res, rej) => { | ||
client.get(key, (err, value) => { | ||
if (err) { | ||
rej(err); | ||
} else { | ||
res(value != null ? JSON.parse(value) : undefined); | ||
} | ||
}); | ||
}); | ||
}, | ||
set(key, value, ttl) { | ||
if (value == null) { | ||
return this.delete(key).then(() => value); | ||
} | ||
const redisTtlKey = ttl < 1 ? 'PX' : 'EX'; | ||
const redisTtl = ttl < 1 ? Math.round(ttl * 1000) : ttl; | ||
return new Promise((res, rej) => { | ||
client.set(key, // $FlowFixMe | ||
JSON.stringify(value), redisTtlKey, redisTtl, err => { | ||
if (err) { | ||
rej(err); | ||
} else { | ||
res(value); | ||
} | ||
}); | ||
}); | ||
}, | ||
delete(key) { | ||
return new Promise((res, rej) => { | ||
client.del(key, err => { | ||
if (err) { | ||
rej(err); | ||
} else { | ||
res(); | ||
} | ||
}); | ||
}); | ||
} | ||
}; | ||
} | ||
} | ||
exports.default = createBackend; | ||
//# sourceMappingURL=redis.js.map |
@@ -1,1 +0,3 @@ | ||
"use strict"; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
//# sourceMappingURL=types.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.cached = void 0; | ||
const memory_1 = __importDefault(require("./backend/memory")); | ||
const disabled_1 = __importDefault(require("./backend/disabled")); | ||
const redis_1 = __importDefault(require("./backend/redis")); | ||
function createCache(options, log) { | ||
if (!options.enabled) { | ||
log.warn('Cache %s is disabled', options.name); | ||
return (0, disabled_1.default)(); | ||
} | ||
switch (options.type) { | ||
case 'memory': | ||
return (0, memory_1.default)(); | ||
case 'redis': | ||
return (0, redis_1.default)(options.redis); | ||
default: | ||
throw new Error(`Unknown cache type ${options.type}`); | ||
} | ||
} | ||
exports.default = createCache; | ||
function cached(options, log, ttl, fn, keyCreator) { | ||
const cacheAsync = createCache(options, log); | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
cacheAsync.catch(() => { }); // prevent unhandled promise rejection... | ||
return async function doCached(...args) { | ||
const cache = await cacheAsync; | ||
const key = keyCreator ? keyCreator(...args) : JSON.stringify(args); | ||
let result = await cache.get(key); | ||
if (!result) { | ||
result = await cache.set(key, await fn(...args), ttl); | ||
} | ||
return result; | ||
}; | ||
} | ||
exports.cached = cached; | ||
var _memory = _interopRequireDefault(require("./backend/memory")); | ||
var _disabled = _interopRequireDefault(require("./backend/disabled")); | ||
var _redis = _interopRequireDefault(require("./backend/redis")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/* eslint "import/no-unused-modules": "error" */ | ||
function createCache(options, log) { | ||
if (!options.enabled) { | ||
log.warn('Cache %s is disabled', options.name); | ||
return (0, _disabled.default)(); | ||
} | ||
switch (options.type) { | ||
case 'memory': | ||
return (0, _memory.default)(); | ||
case 'redis': | ||
return (0, _redis.default)(options.redis); | ||
default: | ||
throw new Error(`Unknown cache type ${options.type}`); | ||
} | ||
} // eslint-disable-next-line flowtype/no-weak-types | ||
function cached(options, log, ttl, fn, keyCreator) { | ||
const cacheAsync = createCache(options, log); | ||
cacheAsync.catch(() => {}); // prevent unhandled promise rejection... | ||
return async function doCached(...args) { | ||
const cache = await cacheAsync; | ||
const key = keyCreator ? keyCreator(...args) : JSON.stringify(args); | ||
let result = await cache.get(key); | ||
if (!result) { | ||
result = await cache.set(key, (await fn(...args)), ttl); | ||
} | ||
return result; | ||
}; | ||
} | ||
//# sourceMappingURL=index.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const fs_1 = __importDefault(require("fs")); | ||
const util_1 = require("util"); | ||
const readFile = (0, util_1.promisify)(fs_1.default.readFile); | ||
async function readSecret(secretFile) { | ||
const text = await readFile(secretFile, 'utf-8'); | ||
return text.trim(); // because of these freaking EOL chars | ||
} | ||
exports.default = readSecret; | ||
var _fs = _interopRequireDefault(require("fs")); | ||
var _util = require("util"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
const readFile = (0, _util.promisify)(_fs.default.readFile); | ||
async function readSecret(secretFile) { | ||
const text = await readFile(secretFile, 'utf-8'); | ||
return text.trim(); // because of these freaking EOL chars | ||
} | ||
//# sourceMappingURL=read-secret.js.map |
{ | ||
"name": "@axah/cache", | ||
"version": "2.1.0", | ||
"version": "3.0.0", | ||
"description": "Provides a cache either backed by RAM or redis", | ||
@@ -11,31 +11,31 @@ "main": "lib/index.js", | ||
"scripts": { | ||
"dev": "nodemon -w src -e js --exec yarn build", | ||
"lint": "eslint . && flow check", | ||
"dev": "ts-node-dev --respawn src/index.ts", | ||
"lint": " tsc --noEmit && eslint .", | ||
"prepublishOnly": "yarn build", | ||
"build": "yarn lint && yarn test && yarn clean && npm-run-all -p build:*", | ||
"build": "yarn lint && yarn test && yarn clean && yarn build:typescript", | ||
"build-with-lcov": "yarn lint && yarn test --coverage --coverageReporters lcov && yarn clean && yarn build:typescript", | ||
"clean": "rimraf lib", | ||
"build:babel": "babel src/ -d lib/ --ignore '**/*.test.js,**/__mocks__/**'", | ||
"build:flow": "flow-copy-source -i '**/*.test.js' -i '**/__mocks__/**' src lib/", | ||
"build:typescript": "tsc --build tsconfig.build.json", | ||
"test": "jest", | ||
"ci": "yarn build && yarn flow stop" | ||
"ci": "yarn build" | ||
}, | ||
"devDependencies": { | ||
"@axah/log": "^1.0.2", | ||
"@babel/cli": "^7.2.3", | ||
"@babel/core": "^7.2.2", | ||
"@babel/preset-env": "^7.3.1", | ||
"@babel/preset-flow": "^7.0.0", | ||
"babel-eslint": "^10.0.1", | ||
"eslint": "^6.8.0", | ||
"eslint-config-airbnb-base": "^14.0.0", | ||
"eslint-plugin-flowtype": "^4.6.0", | ||
"eslint-plugin-import": "^2.15.0", | ||
"flow-bin": "0.117.0", | ||
"flow-copy-source": "^2.0.2", | ||
"flow-typed": "^2.5.1", | ||
"jest": "^25.1.0", | ||
"nodemon": "^2.0.2", | ||
"@types/jest": "^27.4.0", | ||
"@types/pino": "^6.3.12", | ||
"@typescript-eslint/eslint-plugin": "^5.10.1", | ||
"@typescript-eslint/parser": "^5.10.1", | ||
"eslint": "^8.7.0", | ||
"eslint-config-airbnb-base": "^15.0.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-plugin-import": "^2.25.4", | ||
"eslint-plugin-prettier": "^4.0.0", | ||
"jest": "^27.4.7", | ||
"npm-run-all": "^4.1.5", | ||
"pino": "^5.0.0", | ||
"pino-pretty": "^3.5.0" | ||
"pino": "^6.2.1", | ||
"pino-pretty": "^5.0.0", | ||
"prettier": "^2.5.1", | ||
"ts-jest": "^27.1.3", | ||
"ts-node-dev": "^1.1.8", | ||
"typescript": "^4.5.5" | ||
}, | ||
@@ -46,4 +46,4 @@ "peerDependencies": { | ||
"dependencies": { | ||
"redis": "^2.8.0" | ||
"redis": "^4.0.2" | ||
} | ||
} |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
13440
20
181
0
22
2
+ Added@redis/bloom@1.2.0(transitive)
+ Added@redis/client@1.6.0(transitive)
+ Added@redis/graph@1.1.1(transitive)
+ Added@redis/json@1.0.7(transitive)
+ Added@redis/search@1.2.0(transitive)
+ Added@redis/time-series@1.1.0(transitive)
+ Addedcluster-key-slot@1.1.2(transitive)
+ Addedgeneric-pool@3.9.0(transitive)
+ Addedredis@4.7.0(transitive)
+ Addedyallist@4.0.0(transitive)
- Removeddouble-ended-queue@2.1.0-0(transitive)
- Removedredis@2.8.0(transitive)
- Removedredis-commands@1.7.0(transitive)
- Removedredis-parser@2.6.0(transitive)
Updatedredis@^4.0.2