rust-node-cache 0.2.0 → 0.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/README.md +145 -48
- package/binding.d.ts +35 -27
- package/dist/express.d.mts +14 -10
- package/dist/express.d.ts +14 -10
- package/dist/express.js +13 -3
- package/dist/express.js.map +1 -1
- package/dist/express.mjs +13 -3
- package/dist/express.mjs.map +1 -1
- package/dist/fastify.d.mts +9 -9
- package/dist/fastify.d.ts +9 -9
- package/dist/fastify.js +5 -11
- package/dist/fastify.js.map +1 -1
- package/dist/fastify.mjs +5 -11
- package/dist/fastify.mjs.map +1 -1
- package/dist/index.d.mts +73 -44
- package/dist/index.d.ts +73 -44
- package/dist/index.js +128 -15
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +128 -15
- package/dist/index.mjs.map +1 -1
- package/dist/nestjs.d.mts +11 -11
- package/dist/nestjs.d.ts +11 -11
- package/dist/nestjs.js +11 -3
- package/dist/nestjs.js.map +1 -1
- package/dist/nestjs.mjs +11 -3
- package/dist/nestjs.mjs.map +1 -1
- package/package.json +7 -4
- package/rust-node-cache.darwin-arm64.node +0 -0
- package/rust-node-cache.darwin-x64.node +0 -0
- package/rust-node-cache.linux-arm64-gnu.node +0 -0
- package/rust-node-cache.linux-arm64-musl.node +0 -0
- package/rust-node-cache.linux-x64-gnu.node +0 -0
- package/rust-node-cache.linux-x64-musl.node +0 -0
- package/rust-node-cache.win32-x64-msvc.node +0 -0
package/dist/nestjs.mjs
CHANGED
|
@@ -19,10 +19,11 @@ var CacheInterceptor = class {
|
|
|
19
19
|
}
|
|
20
20
|
return next.handle().pipe(
|
|
21
21
|
tap((body) => {
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
const json = toJson(body);
|
|
23
|
+
if (json !== void 0 && json !== "null") {
|
|
24
|
+
this.cache.setJson(
|
|
24
25
|
key,
|
|
25
|
-
|
|
26
|
+
json,
|
|
26
27
|
this.ttlSeconds ? { ttlSeconds: this.ttlSeconds } : void 0
|
|
27
28
|
);
|
|
28
29
|
}
|
|
@@ -30,6 +31,13 @@ var CacheInterceptor = class {
|
|
|
30
31
|
);
|
|
31
32
|
}
|
|
32
33
|
};
|
|
34
|
+
function toJson(body) {
|
|
35
|
+
try {
|
|
36
|
+
return JSON.stringify(body);
|
|
37
|
+
} catch {
|
|
38
|
+
return void 0;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
33
41
|
export {
|
|
34
42
|
CacheInterceptor
|
|
35
43
|
};
|
package/dist/nestjs.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../js/nestjs.ts"],"sourcesContent":["/**\n *
|
|
1
|
+
{"version":3,"sources":["../js/nestjs.ts"],"sourcesContent":["/**\n * NestJS integration: a response caching interceptor.\n *\n * Usage:\n * import { Cache } from \"rust-node-cache\";\n * import { CacheInterceptor } from \"rust-node-cache/nestjs\";\n *\n * const cache = new Cache();\n * app.useGlobalInterceptors(new CacheInterceptor({ cache, ttlSeconds: 60 }));\n *\n * `@nestjs/common` is imported with `import type`, so there is NO runtime import\n * of Nest here — the package does not force Nest as a dependency. The only runtime\n * dependencies are RxJS's `of`/`tap` operators (present in any Nest app).\n */\n\nimport type {\n CallHandler,\n ExecutionContext,\n NestInterceptor,\n} from \"@nestjs/common\";\nimport type { Observable } from \"rxjs\";\nimport { of, tap } from \"rxjs\";\nimport type { Cache } from \"./index\";\n\nexport interface CacheInterceptorOptions {\n /** Cache instance to use. */\n cache: Cache;\n /** TTL applied to stored responses (in seconds). */\n ttlSeconds?: number;\n /** HTTP methods eligible for caching. Default: `[\"GET\"]`. */\n methods?: string[];\n /** Builds the cache key from the request. Default: `METHOD:url`. */\n keyGenerator?: (req: { method: string; url: string }) => string;\n}\n\n/**\n * NestJS interceptor that caches the value returned by handlers. On a HIT, it\n * returns the cached value without running the handler.\n */\nexport class CacheInterceptor implements NestInterceptor {\n private readonly cache: Cache;\n private readonly ttlSeconds?: number;\n private readonly methods: string[];\n private readonly keyGenerator: (req: {\n method: string;\n url: string;\n }) => string;\n\n constructor(options: CacheInterceptorOptions) {\n this.cache = options.cache;\n this.ttlSeconds = options.ttlSeconds;\n this.methods = options.methods ?? [\"GET\"];\n this.keyGenerator =\n options.keyGenerator ?? ((req) => `${req.method}:${req.url}`);\n }\n\n intercept(\n context: ExecutionContext,\n next: CallHandler,\n ): Observable<unknown> {\n const req = context.switchToHttp().getRequest<{\n method: string;\n url: string;\n }>();\n\n if (!this.methods.includes(req.method)) {\n return next.handle();\n }\n\n const key = this.keyGenerator(req);\n const cached = this.cache.get(key);\n if (cached !== null) {\n return of(cached);\n }\n\n return next.handle().pipe(\n tap((body) => {\n // Store the body as Nest will send it (JSON), so a HIT returns the\n // same data as the MISS did (a `Date` becomes its ISO string).\n const json = toJson(body);\n if (json !== undefined && json !== \"null\") {\n this.cache.setJson(\n key,\n json,\n this.ttlSeconds ? { ttlSeconds: this.ttlSeconds } : undefined,\n );\n }\n }),\n );\n }\n}\n\n/** The body as JSON text, or `undefined` when it cannot be cached. */\nfunction toJson(body: unknown): string | undefined {\n try {\n return JSON.stringify(body);\n } catch {\n return undefined; // circular or BigInt: left for Nest to report.\n }\n}\n"],"mappings":";AAqBA,SAAS,IAAI,WAAW;AAkBjB,IAAM,mBAAN,MAAkD;AAAA,EASvD,YAAY,SAAkC;AAC5C,SAAK,QAAQ,QAAQ;AACrB,SAAK,aAAa,QAAQ;AAC1B,SAAK,UAAU,QAAQ,WAAW,CAAC,KAAK;AACxC,SAAK,eACH,QAAQ,iBAAiB,CAAC,QAAQ,GAAG,IAAI,MAAM,IAAI,IAAI,GAAG;AAAA,EAC9D;AAAA,EAEA,UACE,SACA,MACqB;AACrB,UAAM,MAAM,QAAQ,aAAa,EAAE,WAGhC;AAEH,QAAI,CAAC,KAAK,QAAQ,SAAS,IAAI,MAAM,GAAG;AACtC,aAAO,KAAK,OAAO;AAAA,IACrB;AAEA,UAAM,MAAM,KAAK,aAAa,GAAG;AACjC,UAAM,SAAS,KAAK,MAAM,IAAI,GAAG;AACjC,QAAI,WAAW,MAAM;AACnB,aAAO,GAAG,MAAM;AAAA,IAClB;AAEA,WAAO,KAAK,OAAO,EAAE;AAAA,MACnB,IAAI,CAAC,SAAS;AAGZ,cAAM,OAAO,OAAO,IAAI;AACxB,YAAI,SAAS,UAAa,SAAS,QAAQ;AACzC,eAAK,MAAM;AAAA,YACT;AAAA,YACA;AAAA,YACA,KAAK,aAAa,EAAE,YAAY,KAAK,WAAW,IAAI;AAAA,UACtD;AAAA,QACF;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AACF;AAGA,SAAS,OAAO,MAAmC;AACjD,MAAI;AACF,WAAO,KAAK,UAAU,IAAI;AAAA,EAC5B,QAAQ;AACN,WAAO;AAAA,EACT;AACF;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rust-node-cache",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "In-memory cache for Node.js that keeps your data off the V8 heap. Rust core, O(1) LRU, TTL.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cache",
|
|
7
7
|
"in-memory",
|
|
@@ -9,7 +9,8 @@
|
|
|
9
9
|
"napi",
|
|
10
10
|
"performance",
|
|
11
11
|
"ttl",
|
|
12
|
-
"
|
|
12
|
+
"lru",
|
|
13
|
+
"off-heap",
|
|
13
14
|
"express",
|
|
14
15
|
"fastify",
|
|
15
16
|
"nestjs"
|
|
@@ -66,7 +67,9 @@
|
|
|
66
67
|
"aarch64-apple-darwin",
|
|
67
68
|
"aarch64-unknown-linux-gnu",
|
|
68
69
|
"x86_64-unknown-linux-gnu",
|
|
69
|
-
"x86_64-pc-windows-msvc"
|
|
70
|
+
"x86_64-pc-windows-msvc",
|
|
71
|
+
"x86_64-unknown-linux-musl",
|
|
72
|
+
"aarch64-unknown-linux-musl"
|
|
70
73
|
]
|
|
71
74
|
}
|
|
72
75
|
},
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|