express-memorize 1.0.0
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/dist/MemorizeStore.d.ts +46 -0
- package/dist/MemorizeStore.d.ts.map +1 -0
- package/dist/MemorizeStore.js +100 -0
- package/dist/MemorizeStore.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/memorize.d.ts +20 -0
- package/dist/memorize.d.ts.map +1 -0
- package/dist/memorize.js +40 -0
- package/dist/memorize.js.map +1 -0
- package/package.json +38 -0
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
export interface CacheEntry {
|
|
2
|
+
body: unknown;
|
|
3
|
+
statusCode: number;
|
|
4
|
+
contentType: string;
|
|
5
|
+
expiresAt: number | null;
|
|
6
|
+
}
|
|
7
|
+
export interface CacheInfo extends CacheEntry {
|
|
8
|
+
key: string;
|
|
9
|
+
remainingTtl: number | null;
|
|
10
|
+
}
|
|
11
|
+
export interface MemorizeSetEvent {
|
|
12
|
+
type: 'set';
|
|
13
|
+
key: string;
|
|
14
|
+
body: unknown;
|
|
15
|
+
statusCode: number;
|
|
16
|
+
contentType: string;
|
|
17
|
+
expiresAt: number | null;
|
|
18
|
+
}
|
|
19
|
+
export interface MemorizeDeleteEvent {
|
|
20
|
+
type: 'delete';
|
|
21
|
+
key: string;
|
|
22
|
+
}
|
|
23
|
+
export interface MemorizeExpireEvent {
|
|
24
|
+
type: 'expire';
|
|
25
|
+
key: string;
|
|
26
|
+
}
|
|
27
|
+
export type MemorizeEvent = MemorizeSetEvent | MemorizeDeleteEvent | MemorizeExpireEvent;
|
|
28
|
+
export type MemorizeEventType = MemorizeEvent['type'];
|
|
29
|
+
export declare class MemorizeStore {
|
|
30
|
+
private _store;
|
|
31
|
+
private _timers;
|
|
32
|
+
private _listeners;
|
|
33
|
+
on(event: 'set', handler: (e: MemorizeSetEvent) => void): void;
|
|
34
|
+
on(event: 'delete', handler: (e: MemorizeDeleteEvent) => void): void;
|
|
35
|
+
on(event: 'expire', handler: (e: MemorizeExpireEvent) => void): void;
|
|
36
|
+
set(key: string, entry: Omit<CacheEntry, 'expiresAt'>, ttl?: number | null): void;
|
|
37
|
+
get(key: string): CacheInfo | null;
|
|
38
|
+
getAll(): Record<string, CacheInfo>;
|
|
39
|
+
delete(key: string): boolean;
|
|
40
|
+
clear(): void;
|
|
41
|
+
getRaw(key: string): CacheEntry | null;
|
|
42
|
+
private _evict;
|
|
43
|
+
private _emit;
|
|
44
|
+
private _format;
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=MemorizeStore.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MemorizeStore.d.ts","sourceRoot":"","sources":["../src/MemorizeStore.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,OAAO,CAAC;IACd,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;CAC1B;AAED,MAAM,WAAW,SAAU,SAAQ,UAAU;IAC3C,GAAG,EAAE,MAAM,CAAC;IACZ,YAAY,EAAE,MAAM,GAAG,IAAI,CAAC;CAC7B;AAID,MAAM,WAAW,gBAAgB;IAC/B,IAAI,EAAE,KAAK,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,OAAO,CAAC;IACd,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;CAC1B;AAED,MAAM,WAAW,mBAAmB;IAClC,IAAI,EAAE,QAAQ,CAAC;IACf,GAAG,EAAE,MAAM,CAAC;CACb;AAED,MAAM,WAAW,mBAAmB;IAClC,IAAI,EAAE,QAAQ,CAAC;IACf,GAAG,EAAE,MAAM,CAAC;CACb;AA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
7
|
+
this._store = new Map();
|
|
8
|
+
this._timers = new Map();
|
|
9
|
+
this._listeners = { set: [], delete: [], expire: [] };
|
|
10
|
+
}
|
|
11
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
12
|
+
on(event, handler) {
|
|
13
|
+
this._listeners[event].push(handler);
|
|
14
|
+
}
|
|
15
|
+
set(key, entry, ttl) {
|
|
16
|
+
if (this._timers.has(key)) {
|
|
17
|
+
clearTimeout(this._timers.get(key));
|
|
18
|
+
this._timers.delete(key);
|
|
19
|
+
}
|
|
20
|
+
const expiresAt = ttl ? Date.now() + ttl : null;
|
|
21
|
+
const stored = { ...entry, expiresAt };
|
|
22
|
+
this._store.set(key, stored);
|
|
23
|
+
this._emit('set', { type: 'set', key, ...entry, expiresAt });
|
|
24
|
+
if (ttl) {
|
|
25
|
+
const timer = setTimeout(() => {
|
|
26
|
+
this._evict(key, 'expire');
|
|
27
|
+
}, ttl);
|
|
28
|
+
if (typeof timer === 'object' && 'unref' in timer)
|
|
29
|
+
timer.unref();
|
|
30
|
+
this._timers.set(key, timer);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
get(key) {
|
|
34
|
+
const entry = this._store.get(key);
|
|
35
|
+
if (!entry)
|
|
36
|
+
return null;
|
|
37
|
+
if (entry.expiresAt && Date.now() > entry.expiresAt) {
|
|
38
|
+
this._evict(key, 'expire');
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
return this._format(key, entry);
|
|
42
|
+
}
|
|
43
|
+
getAll() {
|
|
44
|
+
const result = {};
|
|
45
|
+
for (const [key, entry] of this._store) {
|
|
46
|
+
if (entry.expiresAt && Date.now() > entry.expiresAt) {
|
|
47
|
+
this._evict(key, 'expire');
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
result[key] = this._format(key, entry);
|
|
51
|
+
}
|
|
52
|
+
return result;
|
|
53
|
+
}
|
|
54
|
+
delete(key) {
|
|
55
|
+
if (!this._store.has(key))
|
|
56
|
+
return false;
|
|
57
|
+
this._evict(key, 'delete');
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
clear() {
|
|
61
|
+
for (const key of this._store.keys()) {
|
|
62
|
+
this._evict(key, 'delete');
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
getRaw(key) {
|
|
66
|
+
const entry = this._store.get(key);
|
|
67
|
+
if (!entry)
|
|
68
|
+
return null;
|
|
69
|
+
if (entry.expiresAt && Date.now() > entry.expiresAt) {
|
|
70
|
+
this._evict(key, 'expire');
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
return entry;
|
|
74
|
+
}
|
|
75
|
+
_evict(key, reason) {
|
|
76
|
+
if (this._timers.has(key)) {
|
|
77
|
+
clearTimeout(this._timers.get(key));
|
|
78
|
+
this._timers.delete(key);
|
|
79
|
+
}
|
|
80
|
+
this._store.delete(key);
|
|
81
|
+
this._emit(reason, { type: reason, key });
|
|
82
|
+
}
|
|
83
|
+
_emit(event, payload) {
|
|
84
|
+
for (const handler of this._listeners[event]) {
|
|
85
|
+
handler(payload);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
_format(key, entry) {
|
|
89
|
+
return {
|
|
90
|
+
key,
|
|
91
|
+
body: entry.body,
|
|
92
|
+
statusCode: entry.statusCode,
|
|
93
|
+
contentType: entry.contentType,
|
|
94
|
+
expiresAt: entry.expiresAt,
|
|
95
|
+
remainingTtl: entry.expiresAt ? Math.max(0, entry.expiresAt - Date.now()) : null,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
exports.MemorizeStore = MemorizeStore;
|
|
100
|
+
//# sourceMappingURL=MemorizeStore.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MemorizeStore.js","sourceRoot":"","sources":["../src/MemorizeStore.ts"],"names":[],"mappings":";;;AAyCA,gBAAgB;AAEhB,MAAa,aAAa;IAA1B;QACU,WAAM,GAAG,IAAI,GAAG,E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EAAE,KAAK,CAAC,UAAU;YAC5B,WAAW,EAAE,KAAK,CAAC,WAAW;YAC9B,SAAS,EAAE,KAAK,CAAC,SAAS;YAC1B,YAAY,EAAE,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,KAAK,CAAC,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI;SACjF,CAAC;IACJ,CAAC;CACF;AA9GD,sCA8GC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { memorize } from './memorize';
|
|
2
|
+
export type { MemorizeOptions, MemorizeCallOptions, Memorize } from './memorize';
|
|
3
|
+
export type { CacheEntry, CacheInfo, MemorizeEvent, MemorizeEventType, MemorizeSetEvent, MemorizeDeleteEvent, MemorizeExpireEvent, } from './MemorizeStore';
|
|
4
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,YAAY,EAAE,eAAe,EAAE,mBAAmB,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACjF,YAAY,EACV,UAAU,EACV,SAAS,EACT,aAAa,EACb,iBAAiB,EACjB,gBAAgB,EAChB,mBAAmB,EACnB,mBAAmB,GACpB,MAAM,iBAAiB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.memorize = void 0;
|
|
4
|
+
var memorize_1 = require("./memorize");
|
|
5
|
+
Object.defineProperty(exports, "memorize", { enumerable: true, get: function () { return memorize_1.memorize; } });
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,uCAAsC;AAA7B,oGAAA,QAAQ,OAAA"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { RequestHandler } from 'express';
|
|
2
|
+
import { CacheInfo, MemorizeSetEvent, MemorizeDeleteEvent, MemorizeExpireEvent } from './MemorizeStore';
|
|
3
|
+
export interface MemorizeOptions {
|
|
4
|
+
ttl?: number;
|
|
5
|
+
}
|
|
6
|
+
export interface MemorizeCallOptions {
|
|
7
|
+
ttl?: number;
|
|
8
|
+
}
|
|
9
|
+
export interface Memorize {
|
|
10
|
+
(options?: MemorizeCallOptions): RequestHandler;
|
|
11
|
+
get(key: string): CacheInfo | null;
|
|
12
|
+
getAll(): Record<string, CacheInfo>;
|
|
13
|
+
delete(key: string): boolean;
|
|
14
|
+
clear(): void;
|
|
15
|
+
on(event: 'set', handler: (e: MemorizeSetEvent) => void): void;
|
|
16
|
+
on(event: 'delete', handler: (e: MemorizeDeleteEvent) => void): void;
|
|
17
|
+
on(event: 'expire', handler: (e: MemorizeExpireEvent) => void): void;
|
|
18
|
+
}
|
|
19
|
+
export declare function memorize(options?: MemorizeOptions): Memorize;
|
|
20
|
+
//# sourceMappingURL=memorize.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memorize.d.ts","sourceRoot":"","sources":["../src/memorize.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmC,cAAc,EAAE,MAAM,SAAS,CAAC;AAC1E,OAAO,EAEL,SAAS,EACT,gBAAgB,EAChB,mBAAmB,EACnB,mBAAmB,EACpB,MAAM,iBAAiB,CAAC;AAEzB,MAAM,WAAW,eAAe;IAC9B,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,MAAM,WAAW,mBAAmB;IAClC,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,MAAM,WAAW,QAAQ;IACvB,CAAC,OAAO,CAAC,EAAE,mBAAmB,GAAG,cAAc,CAAC;IAChD,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,CAAC;IACnC,MAAM,IAAI,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;IACpC,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;IAC7B,KAAK,IAAI,IAAI,CAAC;IACd,EAAE,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,CAAC,EAAE,gBAAgB,KAAK,IAAI,GAAG,IAAI,CAAC;IAC/D,EAAE,CAAC,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC,CAAC,EAAE,mBAAmB,KAAK,IAAI,GAAG,IAAI,CAAC;IACrE,EAAE,CAAC,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC,CAAC,EAAE,mBAAmB,KAAK,IAAI,GAAG,IAAI,CAAC;CACtE;AAED,wBAAgB,QAAQ,CAAC,OAAO,GAAE,eAAoB,GAAG,QAAQ,CAwChE"}
|
package/dist/memorize.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.memorize = memorize;
|
|
4
|
+
const MemorizeStore_1 = require("./MemorizeStore");
|
|
5
|
+
function memorize(options = {}) {
|
|
6
|
+
const { ttl } = options;
|
|
7
|
+
const store = new MemorizeStore_1.MemorizeStore();
|
|
8
|
+
const cache = function (callOptions) {
|
|
9
|
+
var _a;
|
|
10
|
+
const effectiveTtl = (_a = callOptions === null || callOptions === void 0 ? void 0 : callOptions.ttl) !== null && _a !== void 0 ? _a : ttl;
|
|
11
|
+
return function (req, res, next) {
|
|
12
|
+
const key = req.originalUrl;
|
|
13
|
+
const cached = store.getRaw(key);
|
|
14
|
+
if (cached) {
|
|
15
|
+
res.setHeader('X-Cache', 'HIT');
|
|
16
|
+
res.setHeader('Content-Type', cached.contentType);
|
|
17
|
+
res.status(cached.statusCode).send(cached.body);
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
const originalSend = res.send.bind(res);
|
|
21
|
+
res.send = function (body) {
|
|
22
|
+
var _a;
|
|
23
|
+
if (res.statusCode >= 200 && res.statusCode < 300) {
|
|
24
|
+
const contentType = (_a = res.getHeader('Content-Type')) !== null && _a !== void 0 ? _a : 'application/octet-stream';
|
|
25
|
+
store.set(key, { body, statusCode: res.statusCode, contentType }, effectiveTtl);
|
|
26
|
+
}
|
|
27
|
+
res.setHeader('X-Cache', 'MISS');
|
|
28
|
+
return originalSend(body);
|
|
29
|
+
};
|
|
30
|
+
next();
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
cache.get = (key) => store.get(key);
|
|
34
|
+
cache.getAll = () => store.getAll();
|
|
35
|
+
cache.delete = (key) => store.delete(key);
|
|
36
|
+
cache.clear = () => store.clear();
|
|
37
|
+
cache.on = store.on.bind(store);
|
|
38
|
+
return cache;
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=memorize.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memorize.js","sourceRoot":"","sources":["../src/memorize.ts"],"names":[],"mappings":";;AA4BA,4BAwCC;AAnED,mDAMyB;AAqBzB,SAAgB,QAAQ,CAAC,UAA2B,EAAE;IACpD,MAAM,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC;IACxB,MAAM,KAAK,GAAG,IAAI,6BAAa,EAAE,CAAC;IAElC,MAAM,KAAK,GAAG,UAAU,WAAiC;;QACvD,MAAM,YAAY,GAAG,MAAA,WAAW,aAAX,WAAW,uBAAX,WAAW,CAAE,GAAG,mCAAI,GAAG,CAAC;QAE7C,OAAO,UAAU,GAAY,EAAE,GAAa,EAAE,IAAkB;YAC9D,MAAM,GAAG,GAAG,GAAG,CAAC,WAAW,CAAC;YAC5B,MAAM,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;YAEjC,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AAG,EAAE,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC;IAClC,KAAK,CAAC,EAAE,GAAG,KAAK,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,CAAmB,CAAC;IAElD,OAAO,KAAK,CAAC;AACf,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "express-memorize",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Express.js in-memory cache middleware with TTL support",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsc",
|
|
12
|
+
"dev": "tsc --watch",
|
|
13
|
+
"test": "jest",
|
|
14
|
+
"test:watch": "jest --watch",
|
|
15
|
+
"prepublishOnly": "npm test && npm run build"
|
|
16
|
+
},
|
|
17
|
+
"jest": {
|
|
18
|
+
"preset": "ts-jest",
|
|
19
|
+
"testEnvironment": "node",
|
|
20
|
+
"testMatch": ["**/__tests__/**/*.test.ts"],
|
|
21
|
+
"fakeTimers": {
|
|
22
|
+
"enableGlobally": false
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"keywords": ["express", "middleware", "cache", "memory", "ttl"],
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"peerDependencies": {
|
|
28
|
+
"express": ">=4.0.0"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@types/express": "^4.17.21",
|
|
32
|
+
"@types/jest": "^29.0.0",
|
|
33
|
+
"@types/node": "^20.0.0",
|
|
34
|
+
"jest": "^29.0.0",
|
|
35
|
+
"ts-jest": "^29.0.0",
|
|
36
|
+
"typescript": "^5.0.0"
|
|
37
|
+
}
|
|
38
|
+
}
|