Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@axah/cache

Package Overview
Dependencies
Maintainers
9
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@axah/cache - npm Package Compare versions

Comparing version 2.1.0 to 3.0.0

lib/backend/disabled.d.ts

42

lib/backend/disabled.js
"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"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc