express-memorize 1.0.0 → 1.3.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/CHANGELOG.md +53 -0
- package/LICENSE +21 -0
- package/README.md +230 -0
- package/dist/MemorizeStore.d.ts +83 -31
- package/dist/MemorizeStore.d.ts.map +1 -1
- package/dist/MemorizeStore.js +87 -10
- package/dist/MemorizeStore.js.map +1 -1
- package/dist/domain/CacheEntry.d.ts +14 -0
- package/dist/domain/CacheEntry.d.ts.map +1 -0
- package/dist/domain/CacheEntry.js +3 -0
- package/dist/domain/CacheEntry.js.map +1 -0
- package/dist/domain/CacheInfo.d.ts +12 -0
- package/dist/domain/CacheInfo.d.ts.map +1 -0
- package/dist/domain/CacheInfo.js +3 -0
- package/dist/domain/CacheInfo.js.map +1 -0
- package/dist/domain/Memorize.d.ts +148 -0
- package/dist/domain/Memorize.d.ts.map +1 -0
- package/dist/domain/Memorize.js +3 -0
- package/dist/domain/Memorize.js.map +1 -0
- package/dist/domain/MemorizeCallOptions.d.ts +24 -0
- package/dist/domain/MemorizeCallOptions.d.ts.map +1 -0
- package/dist/domain/MemorizeCallOptions.js +3 -0
- package/dist/domain/MemorizeCallOptions.js.map +1 -0
- package/dist/domain/MemorizeDeleteEvent.d.ts +18 -0
- package/dist/domain/MemorizeDeleteEvent.d.ts.map +1 -0
- package/dist/domain/MemorizeDeleteEvent.js +3 -0
- package/dist/domain/MemorizeDeleteEvent.js.map +1 -0
- package/dist/domain/MemorizeEmptyEvent.d.ts +16 -0
- package/dist/domain/MemorizeEmptyEvent.d.ts.map +1 -0
- package/dist/domain/MemorizeEmptyEvent.js +3 -0
- package/dist/domain/MemorizeEmptyEvent.js.map +1 -0
- package/dist/domain/MemorizeEvent.d.ts +7 -0
- package/dist/domain/MemorizeEvent.d.ts.map +1 -0
- package/dist/domain/MemorizeEvent.js +3 -0
- package/dist/domain/MemorizeEvent.js.map +1 -0
- package/dist/domain/MemorizeEventType.d.ts +22 -0
- package/dist/domain/MemorizeEventType.d.ts.map +1 -0
- package/dist/domain/MemorizeEventType.js +26 -0
- package/dist/domain/MemorizeEventType.js.map +1 -0
- package/dist/domain/MemorizeExpireEvent.d.ts +17 -0
- package/dist/domain/MemorizeExpireEvent.d.ts.map +1 -0
- package/dist/domain/MemorizeExpireEvent.js +3 -0
- package/dist/domain/MemorizeExpireEvent.js.map +1 -0
- package/dist/domain/MemorizeOptions.d.ts +16 -0
- package/dist/domain/MemorizeOptions.d.ts.map +1 -0
- package/dist/domain/MemorizeOptions.js +3 -0
- package/dist/domain/MemorizeOptions.js.map +1 -0
- package/dist/domain/MemorizeSetEvent.d.ts +25 -0
- package/dist/domain/MemorizeSetEvent.d.ts.map +1 -0
- package/dist/domain/MemorizeSetEvent.js +3 -0
- package/dist/domain/MemorizeSetEvent.js.map +1 -0
- package/dist/domain/index.d.ts +12 -0
- package/dist/domain/index.d.ts.map +1 -0
- package/dist/domain/index.js +6 -0
- package/dist/domain/index.js.map +1 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/memorize.d.ts +51 -18
- package/dist/memorize.d.ts.map +1 -1
- package/dist/memorize.js +52 -0
- package/dist/memorize.js.map +1 -1
- package/dist/utils/globToRegex.d.ts +12 -0
- package/dist/utils/globToRegex.d.ts.map +1 -0
- package/dist/utils/globToRegex.js +41 -0
- package/dist/utils/globToRegex.js.map +1 -0
- package/package.json +30 -6
package/dist/memorize.js
CHANGED
|
@@ -2,6 +2,53 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.memorize = memorize;
|
|
4
4
|
const MemorizeStore_1 = require("./MemorizeStore");
|
|
5
|
+
/**
|
|
6
|
+
* Creates an in-memory cache for an Express application.
|
|
7
|
+
*
|
|
8
|
+
* Returns a {@link Memorize} instance that can be used as per-route middleware,
|
|
9
|
+
* a global `app.use()` middleware, or a cache management API — all sharing the
|
|
10
|
+
* same underlying store.
|
|
11
|
+
*
|
|
12
|
+
* **Only `GET` requests are cached.** Responses are cached only when the HTTP
|
|
13
|
+
* status code is in the `2xx` range. The cache key is `req.originalUrl`, which
|
|
14
|
+
* includes the query string.
|
|
15
|
+
*
|
|
16
|
+
* @param options - Global configuration for the cache instance.
|
|
17
|
+
*
|
|
18
|
+
* @example Per-route middleware
|
|
19
|
+
* ```ts
|
|
20
|
+
* const cache = memorize({ ttl: 30_000 });
|
|
21
|
+
*
|
|
22
|
+
* app.get('/users', cache(), (req, res) => {
|
|
23
|
+
* res.json({ data: users });
|
|
24
|
+
* });
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @example Global middleware
|
|
28
|
+
* ```ts
|
|
29
|
+
* const cache = memorize({ ttl: 60_000 });
|
|
30
|
+
* app.use(cache()); // caches all GET routes
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* @example Cache invalidation
|
|
34
|
+
* ```ts
|
|
35
|
+
* const cache = memorize({ ttl: 30_000 });
|
|
36
|
+
*
|
|
37
|
+
* app.post('/users', (req, res) => {
|
|
38
|
+
* users.push(req.body);
|
|
39
|
+
* cache.delete('/users');
|
|
40
|
+
* res.status(201).json(req.body);
|
|
41
|
+
* });
|
|
42
|
+
* ```
|
|
43
|
+
*
|
|
44
|
+
* @example Event hooks
|
|
45
|
+
* ```ts
|
|
46
|
+
* cache.on(MemorizeEventType.Set, (e) => console.log('stored', e.key));
|
|
47
|
+
* cache.on(MemorizeEventType.Delete, (e) => console.log('deleted', e.key));
|
|
48
|
+
* cache.on(MemorizeEventType.Expire, (e) => console.log('expired', e.key));
|
|
49
|
+
* cache.on(MemorizeEventType.Empty, () => console.log('cache is empty'));
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
5
52
|
function memorize(options = {}) {
|
|
6
53
|
const { ttl } = options;
|
|
7
54
|
const store = new MemorizeStore_1.MemorizeStore();
|
|
@@ -9,6 +56,10 @@ function memorize(options = {}) {
|
|
|
9
56
|
var _a;
|
|
10
57
|
const effectiveTtl = (_a = callOptions === null || callOptions === void 0 ? void 0 : callOptions.ttl) !== null && _a !== void 0 ? _a : ttl;
|
|
11
58
|
return function (req, res, next) {
|
|
59
|
+
if (req.method !== 'GET' || (callOptions === null || callOptions === void 0 ? void 0 : callOptions.noCache)) {
|
|
60
|
+
next();
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
12
63
|
const key = req.originalUrl;
|
|
13
64
|
const cached = store.getRaw(key);
|
|
14
65
|
if (cached) {
|
|
@@ -33,6 +84,7 @@ function memorize(options = {}) {
|
|
|
33
84
|
cache.get = (key) => store.get(key);
|
|
34
85
|
cache.getAll = () => store.getAll();
|
|
35
86
|
cache.delete = (key) => store.delete(key);
|
|
87
|
+
cache.deleteMatching = (pattern) => store.deleteMatching(pattern);
|
|
36
88
|
cache.clear = () => store.clear();
|
|
37
89
|
cache.on = store.on.bind(store);
|
|
38
90
|
return cache;
|
package/dist/memorize.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"memorize.js","sourceRoot":"","sources":["../src/memorize.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"memorize.js","sourceRoot":"","sources":["../src/memorize.ts"],"names":[],"mappings":";;AAuDA,4BA8CC;AApGD,mDAAgD;AAOhD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;AACH,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,IAAI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,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IACvD,KAAK,CAAC,MAAM,GAAW,GAAG,EAAE,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC;IAC5C,KAAK,CAAC,MAAM,GAAW,CAAC,GAAW,EAAE,EAAE,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IAC1D,KAAK,CAAC,cAAc,GAAG,CAAC,OAAe,EAAE,EAAE,CAAC,KAAK,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;IAC1E,KAAK,CAAC,KAAK,GAAY,GAAG,EAAE,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC;IAC3C,KAAK,CAAC,EAAE,GAAe,KAAK,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,CAAmB,CAAC;IAE9D,OAAO,KAAK,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts a glob pattern to a `RegExp`.
|
|
3
|
+
*
|
|
4
|
+
* Glob rules:
|
|
5
|
+
* - `**` — matches any character sequence across path segments (crosses `/`).
|
|
6
|
+
* - `*` — matches any character sequence within a single path segment (does not cross `/`).
|
|
7
|
+
* - `?` — matches any single character except `/`.
|
|
8
|
+
*
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
export declare function globToRegex(pattern: string): RegExp;
|
|
12
|
+
//# sourceMappingURL=globToRegex.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"globToRegex.d.ts","sourceRoot":"","sources":["../../src/utils/globToRegex.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,wBAAgB,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAsBnD"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.globToRegex = globToRegex;
|
|
4
|
+
/**
|
|
5
|
+
* Converts a glob pattern to a `RegExp`.
|
|
6
|
+
*
|
|
7
|
+
* Glob rules:
|
|
8
|
+
* - `**` — matches any character sequence across path segments (crosses `/`).
|
|
9
|
+
* - `*` — matches any character sequence within a single path segment (does not cross `/`).
|
|
10
|
+
* - `?` — matches any single character except `/`.
|
|
11
|
+
*
|
|
12
|
+
* @internal
|
|
13
|
+
*/
|
|
14
|
+
function globToRegex(pattern) {
|
|
15
|
+
let result = '^';
|
|
16
|
+
let i = 0;
|
|
17
|
+
while (i < pattern.length) {
|
|
18
|
+
const ch = pattern[i];
|
|
19
|
+
if (ch === '*' && pattern[i + 1] === '*') {
|
|
20
|
+
result += '.*';
|
|
21
|
+
i += 2;
|
|
22
|
+
if (pattern[i] === '/')
|
|
23
|
+
i++;
|
|
24
|
+
}
|
|
25
|
+
else if (ch === '*') {
|
|
26
|
+
result += '[^/]*';
|
|
27
|
+
i++;
|
|
28
|
+
}
|
|
29
|
+
else if (ch === '?') {
|
|
30
|
+
result += '[^/]';
|
|
31
|
+
i++;
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
result += ch.replace(/[.+^${}()|[\]\\]/g, '\\$&');
|
|
35
|
+
i++;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
result += '$';
|
|
39
|
+
return new RegExp(result);
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=globToRegex.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"globToRegex.js","sourceRoot":"","sources":["../../src/utils/globToRegex.ts"],"names":[],"mappings":";;AAUA,kCAsBC;AAhCD;;;;;;;;;GASG;AACH,SAAgB,WAAW,CAAC,OAAe;IACzC,IAAI,MAAM,GAAG,GAAG,CAAC;IACjB,IAAI,CAAC,GAAG,CAAC,CAAC;IACV,OAAO,CAAC,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;QAC1B,MAAM,EAAE,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;QACtB,IAAI,EAAE,KAAK,GAAG,IAAI,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,GAAG,EAAE,CAAC;YACzC,MAAM,IAAI,IAAI,CAAC;YACf,CAAC,IAAI,CAAC,CAAC;YACP,IAAI,OAAO,CAAC,CAAC,CAAC,KAAK,GAAG;gBAAE,CAAC,EAAE,CAAC;QAC9B,CAAC;aAAM,IAAI,EAAE,KAAK,GAAG,EAAE,CAAC;YACtB,MAAM,IAAI,OAAO,CAAC;YAClB,CAAC,EAAE,CAAC;QACN,CAAC;aAAM,IAAI,EAAE,KAAK,GAAG,EAAE,CAAC;YACtB,MAAM,IAAI,MAAM,CAAC;YACjB,CAAC,EAAE,CAAC;QACN,CAAC;aAAM,CAAC;YACN,MAAM,IAAI,EAAE,CAAC,OAAO,CAAC,mBAAmB,EAAE,MAAM,CAAC,CAAC;YAClD,CAAC,EAAE,CAAC;QACN,CAAC;IACH,CAAC;IACD,MAAM,IAAI,GAAG,CAAC;IACd,OAAO,IAAI,MAAM,CAAC,MAAM,CAAC,CAAC;AAC5B,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,28 +1,50 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "express-memorize",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.3.0",
|
|
4
|
+
"description": "In-memory cache middleware for Express.js. Caches GET responses with optional TTL — zero dependencies, fully typed.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
|
+
"homepage": "https://github.com/ElJijuna/express-memorize#readme",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "https://github.com/ElJijuna/express-memorize.git"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/ElJijuna/express-memorize/issues"
|
|
14
|
+
},
|
|
7
15
|
"files": [
|
|
8
|
-
"dist"
|
|
16
|
+
"dist",
|
|
17
|
+
"CHANGELOG.md"
|
|
9
18
|
],
|
|
10
19
|
"scripts": {
|
|
11
20
|
"build": "tsc",
|
|
12
21
|
"dev": "tsc --watch",
|
|
13
22
|
"test": "jest",
|
|
14
23
|
"test:watch": "jest --watch",
|
|
15
|
-
"prepublishOnly": "npm test && npm run build"
|
|
24
|
+
"prepublishOnly": "npm test && npm run build",
|
|
25
|
+
"docs": "typedoc",
|
|
26
|
+
"docs:watch": "typedoc --watch"
|
|
16
27
|
},
|
|
17
28
|
"jest": {
|
|
18
29
|
"preset": "ts-jest",
|
|
19
30
|
"testEnvironment": "node",
|
|
20
|
-
"testMatch": [
|
|
31
|
+
"testMatch": [
|
|
32
|
+
"**/__tests__/**/*.test.ts"
|
|
33
|
+
],
|
|
21
34
|
"fakeTimers": {
|
|
22
35
|
"enableGlobally": false
|
|
23
36
|
}
|
|
24
37
|
},
|
|
25
|
-
"keywords": [
|
|
38
|
+
"keywords": [
|
|
39
|
+
"express",
|
|
40
|
+
"middleware",
|
|
41
|
+
"cache",
|
|
42
|
+
"memory",
|
|
43
|
+
"ttl",
|
|
44
|
+
"in-memory",
|
|
45
|
+
"http-cache",
|
|
46
|
+
"expressjs"
|
|
47
|
+
],
|
|
26
48
|
"license": "MIT",
|
|
27
49
|
"peerDependencies": {
|
|
28
50
|
"express": ">=4.0.0"
|
|
@@ -33,6 +55,8 @@
|
|
|
33
55
|
"@types/node": "^20.0.0",
|
|
34
56
|
"jest": "^29.0.0",
|
|
35
57
|
"ts-jest": "^29.0.0",
|
|
58
|
+
"typedoc": "^0.28.18",
|
|
59
|
+
"typedoc-material-theme": "^1.4.1",
|
|
36
60
|
"typescript": "^5.0.0"
|
|
37
61
|
}
|
|
38
62
|
}
|