tricache 0.9.0 → 1.0.1

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.
Files changed (53) hide show
  1. package/README.md +7 -3
  2. package/dist/{cache-service-DVvroyqA.d.ts → cache-service-CREOp7MM.d.ts} +57 -12
  3. package/dist/{cache-service-KI5ACF3F.js → cache-service-MG7PN6MX.js} +3 -3
  4. package/dist/{chunk-XZL45ABR.js → chunk-2AJKEQMZ.js} +54 -1
  5. package/dist/{chunk-QNU7B43J.js → chunk-3I5L7K6I.js} +17 -0
  6. package/dist/{chunk-PL5J3ZNT.js → chunk-JYQFQJNJ.js} +682 -277
  7. package/dist/{chunk-LJ5QJB5O.js → chunk-UBWLPKTI.js} +127 -11
  8. package/dist/cli.cjs +857 -293
  9. package/dist/cli.js +4 -4
  10. package/dist/dashboard/index.d.ts +2 -2
  11. package/dist/drizzle/index.cjs +865 -294
  12. package/dist/drizzle/index.d.ts +2 -2
  13. package/dist/drizzle/index.js +1 -1
  14. package/dist/edge/index.cjs +115 -36
  15. package/dist/edge/index.d.ts +3 -3
  16. package/dist/edge/index.js +88 -37
  17. package/dist/hono/index.cjs +865 -294
  18. package/dist/hono/index.d.ts +2 -2
  19. package/dist/hono/index.js +1 -1
  20. package/dist/{hono-VA9A1m0I.d.ts → hono-DSV2ve66.d.ts} +11 -4
  21. package/dist/http/index.cjs +917 -315
  22. package/dist/http/index.d.ts +3 -3
  23. package/dist/http/index.js +60 -29
  24. package/dist/index.cjs +885 -294
  25. package/dist/index.d.ts +37 -7
  26. package/dist/index.js +16 -6
  27. package/dist/nestjs/index.cjs +856 -292
  28. package/dist/nestjs/index.d.ts +2 -2
  29. package/dist/nestjs/index.js +4 -4
  30. package/dist/next/index.cjs +858 -300
  31. package/dist/next/index.d.ts +2 -2
  32. package/dist/next/index.js +5 -11
  33. package/dist/prisma/index.d.ts +2 -2
  34. package/dist/serialize-worker.cjs +127 -9
  35. package/dist/serialize-worker.js +8 -4
  36. package/dist/{types-B_QkVwJ7.d.ts → types-Dyip5wAl.d.ts} +53 -7
  37. package/package.json +7 -4
  38. package/src/adapters/node-redis.ts +19 -0
  39. package/src/cache-service.ts +781 -297
  40. package/src/cli.ts +1 -1
  41. package/src/compression.ts +193 -15
  42. package/src/cross-region.ts +1 -1
  43. package/src/edge/cache.ts +94 -42
  44. package/src/env.d.ts +13 -0
  45. package/src/http/express.ts +55 -16
  46. package/src/http/fastify.ts +3 -7
  47. package/src/index.ts +9 -1
  48. package/src/next/cache-handler.ts +1 -4
  49. package/src/next/isr-handler.ts +1 -4
  50. package/src/serialize-worker.ts +6 -2
  51. package/src/smart-memory-cache.ts +3 -2
  52. package/src/types.ts +45 -5
  53. package/src/utils/tag-matcher.ts +90 -0
@@ -1,8 +1,8 @@
1
1
  import { K as KeyDerivationOptions } from '../utils-CEsU_Z0s.js';
2
2
  export { b as buildDeterministicKey, g as generateETag, s as shouldSkipCache } from '../utils-CEsU_Z0s.js';
3
- import { C as CacheService } from '../cache-service-DVvroyqA.js';
4
- import { W as WrapOptions } from '../types-B_QkVwJ7.js';
5
- export { C as CachedEdgeHttpResponse, H as HonoEdgeCacheOptions, b as buildEdgeDeterministicKey, c as computeEdgeETag, h as honoCache } from '../hono-VA9A1m0I.js';
3
+ import { C as CacheService } from '../cache-service-CREOp7MM.js';
4
+ import { W as WrapOptions } from '../types-Dyip5wAl.js';
5
+ export { C as CachedEdgeHttpResponse, H as HonoEdgeCacheOptions, b as buildEdgeDeterministicKey, c as computeEdgeETag, h as honoCache } from '../hono-DSV2ve66.js';
6
6
  import 'crypto';
7
7
 
8
8
  interface ExpressCacheOptions extends Omit<WrapOptions, 'tags'>, KeyDerivationOptions {
@@ -1,13 +1,13 @@
1
- import {
2
- buildDeterministicKey,
3
- generateETag,
4
- shouldSkipCache
5
- } from "../chunk-BQRURGGD.js";
6
1
  import {
7
2
  buildEdgeDeterministicKey,
8
3
  computeEdgeETag,
9
4
  honoEdgeCache
10
5
  } from "../chunk-IITRFWHY.js";
6
+ import {
7
+ buildDeterministicKey,
8
+ generateETag,
9
+ shouldSkipCache
10
+ } from "../chunk-BQRURGGD.js";
11
11
 
12
12
  // src/http/express.ts
13
13
  function createExpressMiddleware(options = {}) {
@@ -30,33 +30,59 @@ function createExpressMiddleware(options = {}) {
30
30
  }
31
31
  let activeCache = cache;
32
32
  if (!activeCache) {
33
- const { CacheService } = await import("../cache-service-KI5ACF3F.js");
33
+ const { CacheService } = await import("../cache-service-MG7PN6MX.js");
34
34
  activeCache = CacheService.create();
35
35
  }
36
36
  const key = buildDeterministicKey(req, { keyGenerator, headerWhitelist });
37
37
  const ifNoneMatch = req.headers ? req.headers["if-none-match"] || req.headers["If-None-Match"] : void 0;
38
38
  const resolvedTags = typeof tags === "function" ? tags(req) : tags;
39
+ let respondedLocally = false;
39
40
  try {
40
41
  const cached = await activeCache.get(
41
42
  key,
42
43
  async () => {
43
- return new Promise((resolve) => {
44
- const origJson = res.json?.bind(res);
45
- const origSend = res.send?.bind(res);
46
- const origEnd = res.end?.bind(res);
44
+ return new Promise((resolve, reject) => {
45
+ const rawJson = res.json;
46
+ const rawSend = res.send;
47
+ const rawEnd = res.end;
48
+ const origJson = rawJson?.bind(res);
49
+ const origSend = rawSend?.bind(res);
50
+ const origEnd = rawEnd?.bind(res);
51
+ let settled = false;
52
+ const cleanup = () => {
53
+ if (rawJson !== void 0) res.json = rawJson;
54
+ if (rawSend !== void 0) res.send = rawSend;
55
+ if (rawEnd !== void 0) res.end = rawEnd;
56
+ res.removeListener?.("close", onClose);
57
+ };
58
+ const settle = (response) => {
59
+ if (settled) return;
60
+ settled = true;
61
+ respondedLocally = true;
62
+ cleanup();
63
+ resolve(response);
64
+ };
65
+ const onClose = () => {
66
+ if (settled) return;
67
+ settled = true;
68
+ respondedLocally = true;
69
+ cleanup();
70
+ reject(new Error("Express response closed before completion"));
71
+ };
72
+ res.once?.("close", onClose);
47
73
  const getStatusCode = () => res.statusCode ?? 200;
48
74
  if (origJson) {
49
75
  res.json = (body) => {
50
76
  const status = getStatusCode();
51
77
  const bodyEtag = etag ? generateETag(body) : void 0;
52
78
  const contentType = res.getHeader?.("content-type") || "application/json; charset=utf-8";
53
- resolve({ body, contentType, etag: bodyEtag, status });
54
- if (ifNoneMatch && bodyEtag && ifNoneMatch === bodyEtag) {
79
+ settle({ body, contentType, etag: bodyEtag, status });
80
+ if (status >= 200 && status < 300 && ifNoneMatch && bodyEtag && ifNoneMatch === bodyEtag) {
55
81
  res.status?.(304);
56
82
  origEnd?.();
57
83
  return;
58
84
  }
59
- if (bodyEtag && res.setHeader) {
85
+ if (status >= 200 && status < 300 && bodyEtag && res.setHeader) {
60
86
  res.setHeader("ETag", bodyEtag);
61
87
  }
62
88
  return origJson(body);
@@ -67,13 +93,13 @@ function createExpressMiddleware(options = {}) {
67
93
  const status = getStatusCode();
68
94
  const bodyEtag = etag ? generateETag(body) : void 0;
69
95
  const contentType = res.getHeader?.("content-type");
70
- resolve({ body, contentType, etag: bodyEtag, status });
71
- if (ifNoneMatch && bodyEtag && ifNoneMatch === bodyEtag) {
96
+ settle({ body, contentType, etag: bodyEtag, status });
97
+ if (status >= 200 && status < 300 && ifNoneMatch && bodyEtag && ifNoneMatch === bodyEtag) {
72
98
  res.status?.(304);
73
99
  origEnd?.();
74
100
  return;
75
101
  }
76
- if (bodyEtag && res.setHeader) {
102
+ if (status >= 200 && status < 300 && bodyEtag && res.setHeader) {
77
103
  res.setHeader("ETag", bodyEtag);
78
104
  }
79
105
  return origSend(body);
@@ -84,23 +110,31 @@ function createExpressMiddleware(options = {}) {
84
110
  const status = getStatusCode();
85
111
  const bodyEtag = etag && body != null ? generateETag(body) : void 0;
86
112
  const contentType = res.getHeader?.("content-type");
87
- resolve({ body, contentType, etag: bodyEtag, status });
113
+ settle({ body, contentType, etag: bodyEtag, status });
88
114
  return origEnd(body);
89
115
  };
90
116
  }
91
- next();
117
+ try {
118
+ next();
119
+ } catch (err) {
120
+ if (!settled) {
121
+ settled = true;
122
+ cleanup();
123
+ reject(err);
124
+ }
125
+ }
92
126
  });
93
127
  },
94
128
  ttl,
95
129
  { swr, tags: resolvedTags }
96
130
  );
97
- if (typeof cached.status === "number" && (cached.status < 200 || cached.status >= 300)) {
131
+ const isNon2xx = typeof cached.status === "number" && (cached.status < 200 || cached.status >= 300);
132
+ if (isNon2xx) {
98
133
  await activeCache.delete(key).catch(() => {
99
134
  });
100
- return;
101
135
  }
102
- if (!res.headersSent) {
103
- if (cached.etag) {
136
+ if (!respondedLocally && !res.headersSent) {
137
+ if (!isNon2xx && cached.etag) {
104
138
  if (res.setHeader) res.setHeader("ETag", cached.etag);
105
139
  if (ifNoneMatch === cached.etag) {
106
140
  return res.status(304).end();
@@ -135,7 +169,7 @@ function createFastifyPlugin(options = {}) {
135
169
  const ttl = mergedOpts.ttl ?? 300;
136
170
  let activeCache = mergedOpts.cache;
137
171
  if (!activeCache) {
138
- const { CacheService } = await import("../cache-service-KI5ACF3F.js");
172
+ const { CacheService } = await import("../cache-service-MG7PN6MX.js");
139
173
  activeCache = CacheService.create();
140
174
  }
141
175
  fastify.addHook("onRequest", async (req, reply) => {
@@ -150,11 +184,7 @@ function createFastifyPlugin(options = {}) {
150
184
  headerWhitelist: mergedOpts.headerWhitelist
151
185
  });
152
186
  const ifNoneMatch = req.headers ? req.headers["if-none-match"] || req.headers["If-None-Match"] : void 0;
153
- const cached = await activeCache.get(
154
- key,
155
- async () => null,
156
- 0
157
- );
187
+ const cached = await activeCache.peek(key);
158
188
  if (cached && typeof cached === "object" && cached.body !== null) {
159
189
  if (cached.etag) {
160
190
  reply.header("ETag", cached.etag);
@@ -198,6 +228,7 @@ function createFastifyPlugin(options = {}) {
198
228
  };
199
229
  void activeCache.set(key, entry, mergedOpts.ttl ?? ttl, void 0, {
200
230
  tags: req._tricacheTags
231
+ }).catch(() => {
201
232
  });
202
233
  }
203
234
  return payload;
@@ -219,7 +250,7 @@ function fastifyCache(options = {}) {
219
250
  if (shouldSkipCache(req, options.skipCache)) return;
220
251
  let activeCache = options.cache;
221
252
  if (!activeCache) {
222
- const { CacheService } = await import("../cache-service-KI5ACF3F.js");
253
+ const { CacheService } = await import("../cache-service-MG7PN6MX.js");
223
254
  activeCache = CacheService.create();
224
255
  }
225
256
  const key = buildDeterministicKey(req, {