tricache 0.7.0 → 0.8.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.
Files changed (100) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +144 -1428
  3. package/bin/tricache.js +8 -8
  4. package/dashboards/tricache-alerts.yaml +54 -0
  5. package/dashboards/tricache-grafana.json +548 -0
  6. package/dist/cache-service-5MZXFIGK.js +12 -0
  7. package/dist/cache-service-CyLdpo77.d.ts +871 -0
  8. package/dist/{chunk-3XQUQBOZ.js → chunk-6PQ5MOWR.js} +2950 -523
  9. package/dist/chunk-7RV5ZVBB.js +1023 -0
  10. package/dist/{chunk-PFB5FMEY.js → chunk-LJ5QJB5O.js} +116 -20
  11. package/dist/chunk-QNU7B43J.js +687 -0
  12. package/dist/chunk-UGD4VEPD.js +138 -0
  13. package/dist/chunk-XZL45ABR.js +108 -0
  14. package/dist/chunk-ZTSWCFYO.js +135 -0
  15. package/dist/cli.cjs +2894 -518
  16. package/dist/cli.d.ts +1 -0
  17. package/dist/cli.js +103 -5
  18. package/dist/dashboard/index.cjs +1065 -0
  19. package/dist/dashboard/index.d.ts +183 -0
  20. package/dist/dashboard/index.js +22 -0
  21. package/dist/drizzle/index.cjs +2648 -508
  22. package/dist/drizzle/index.d.ts +3 -1
  23. package/dist/drizzle/index.js +1 -1
  24. package/dist/edge/index.cjs +1498 -0
  25. package/dist/edge/index.d.ts +127 -0
  26. package/dist/edge/index.js +1109 -0
  27. package/dist/hono-DQRS1Xd7.d.ts +370 -0
  28. package/dist/http/index.cjs +3038 -589
  29. package/dist/http/index.d.ts +109 -23
  30. package/dist/http/index.js +238 -62
  31. package/dist/index.cjs +6962 -2487
  32. package/dist/index.d.ts +547 -30
  33. package/dist/index.js +104 -5
  34. package/dist/murmur3-CMGw9PbV.d.ts +98 -0
  35. package/dist/nestjs/index.cjs +2586 -498
  36. package/dist/nestjs/index.d.ts +3 -1
  37. package/dist/nestjs/index.js +6 -3
  38. package/dist/next/index.cjs +2633 -519
  39. package/dist/next/index.d.ts +13 -7
  40. package/dist/next/index.js +45 -18
  41. package/dist/prisma/index.cjs +2 -1
  42. package/dist/prisma/index.d.ts +3 -1
  43. package/dist/prisma/index.js +2 -1
  44. package/dist/serialize-worker.cjs +31 -19
  45. package/dist/serialize-worker.js +1 -1
  46. package/dist/{cache-service-BblHuF3i.d.ts → types-B6krw64X.d.ts} +605 -646
  47. package/package.json +37 -7
  48. package/src/adapters/auto-pipeliner.ts +206 -0
  49. package/src/adapters/node-redis.ts +370 -0
  50. package/src/cache-service.ts +4231 -3226
  51. package/src/cli.ts +261 -143
  52. package/src/codec.ts +72 -0
  53. package/src/compression.ts +56 -63
  54. package/src/cross-region.ts +245 -0
  55. package/src/dashboard/handler.ts +392 -0
  56. package/src/dashboard/html.ts +594 -0
  57. package/src/dashboard/index.ts +195 -0
  58. package/src/dashboard/types.ts +96 -0
  59. package/src/disk-tier.ts +1223 -821
  60. package/src/drizzle/index.ts +86 -86
  61. package/src/edge/adapters/cloudflare-do.ts +130 -0
  62. package/src/edge/adapters/cloudflare-kv.ts +115 -0
  63. package/src/edge/adapters/upstash.ts +143 -0
  64. package/src/edge/cache.ts +797 -0
  65. package/src/edge/crypto.ts +182 -0
  66. package/src/edge/hono.ts +203 -0
  67. package/src/edge/index.ts +37 -0
  68. package/src/edge/types.ts +230 -0
  69. package/src/edge/utils/base64.ts +63 -0
  70. package/src/edge/utils/murmur3.ts +179 -0
  71. package/src/encryption.ts +635 -493
  72. package/src/http/express.ts +195 -0
  73. package/src/http/fastify.ts +260 -0
  74. package/src/http/index.ts +46 -223
  75. package/src/http/utils.ts +92 -0
  76. package/src/index.ts +149 -47
  77. package/src/ipc-telemetry.ts +481 -0
  78. package/src/latency-watchdog.ts +282 -0
  79. package/src/nestjs/decorators.ts +161 -161
  80. package/src/nestjs/index.ts +23 -23
  81. package/src/nestjs/tricache.module.ts +104 -104
  82. package/src/nestjs/tricache.store.ts +97 -97
  83. package/src/nestjs/types.ts +79 -79
  84. package/src/next/cache-handler.ts +271 -244
  85. package/src/next/index.ts +37 -37
  86. package/src/next/isr-handler.ts +105 -105
  87. package/src/next/types.ts +130 -130
  88. package/src/prisma/index.ts +154 -151
  89. package/src/remote-snapshot.ts +153 -0
  90. package/src/serialize-worker.ts +120 -120
  91. package/src/sigv4-snapshot-adapter.ts +260 -0
  92. package/src/smart-memory-cache.ts +1109 -1024
  93. package/src/types.ts +1199 -902
  94. package/src/utils/cgroup.ts +100 -0
  95. package/src/utils/tracing.ts +54 -0
  96. package/src/wasm/bloom-filter-wasm.ts +109 -102
  97. package/src/worker-pool.ts +299 -299
  98. package/src/wtiny-lfu.ts +624 -0
  99. package/dist/cache-service-AJASH5FX.js +0 -7
  100. package/dist/chunk-UBSR5UYA.js +0 -0
@@ -0,0 +1,195 @@
1
+ import http from 'node:http';
2
+ import type { IncomingMessage, ServerResponse } from 'node:http';
3
+ import { handleDashboardRequest } from './handler.js';
4
+ import type { DashboardOptions, StandaloneDashboardOptions } from './types.js';
5
+
6
+ export * from './types.js';
7
+ export { handleDashboardRequest, safeCompare, buildMetricsPayload, resolveInstanceId } from './handler.js';
8
+ export { DASHBOARD_HTML, DASHBOARD_GZIP_BUFFER } from './html.js';
9
+
10
+ /**
11
+ * Convert a Node.js IncomingMessage into a Web Standards Request.
12
+ */
13
+ async function nodeToWebRequest(req: IncomingMessage): Promise<Request> {
14
+ const protocol = (req.socket as { encrypted?: boolean }).encrypted ? 'https' : 'http';
15
+ const host = req.headers.host || 'localhost';
16
+ const url = new URL(req.url || '/', `${protocol}://${host}`);
17
+
18
+ const headers = new Headers();
19
+ for (const [key, val] of Object.entries(req.headers)) {
20
+ if (val === undefined) continue;
21
+ if (Array.isArray(val)) {
22
+ for (const item of val) headers.append(key, item);
23
+ } else {
24
+ headers.set(key, val);
25
+ }
26
+ }
27
+
28
+ let body: Uint8Array | null = null;
29
+ if (req.method !== 'GET' && req.method !== 'HEAD') {
30
+ const chunks: Buffer[] = [];
31
+ for await (const chunk of req) {
32
+ chunks.push(typeof chunk === 'string' ? Buffer.from(chunk) : chunk);
33
+ }
34
+ if (chunks.length > 0) {
35
+ body = Buffer.concat(chunks);
36
+ }
37
+ }
38
+
39
+ const controller = new AbortController();
40
+ req.on('close', () => {
41
+ if (req.destroyed || !req.complete) {
42
+ controller.abort();
43
+ }
44
+ });
45
+
46
+ const init: RequestInit & { duplex?: string } = {
47
+ method: req.method,
48
+ headers,
49
+ body: body ? (body as unknown as BodyInit) : null,
50
+ signal: controller.signal,
51
+ };
52
+ if (body) {
53
+ init.duplex = 'half';
54
+ }
55
+
56
+ return new Request(url.toString(), init);
57
+ }
58
+
59
+ /**
60
+ * Pipe a Web Standards Response back into a Node.js ServerResponse.
61
+ */
62
+ async function pipeWebResponseToNode(webRes: Response, nodeRes: ServerResponse): Promise<void> {
63
+ nodeRes.statusCode = webRes.status;
64
+ webRes.headers.forEach((val, key) => {
65
+ nodeRes.setHeader(key, val);
66
+ });
67
+
68
+ if (!webRes.body) {
69
+ nodeRes.end();
70
+ return;
71
+ }
72
+
73
+ const reader = webRes.body.getReader();
74
+ try {
75
+ while (true) {
76
+ const { done, value } = await reader.read();
77
+ if (done) break;
78
+ nodeRes.write(value);
79
+ }
80
+ } finally {
81
+ nodeRes.end();
82
+ }
83
+ }
84
+
85
+ /**
86
+ * Connect/Express/Fastify middleware adapter for the TriCache observability dashboard.
87
+ *
88
+ * @example
89
+ * ```typescript
90
+ * import { tricacheDashboard } from 'tricache/dashboard';
91
+ *
92
+ * app.use('/admin/cache', tricacheDashboard({
93
+ * cache,
94
+ * auth: { username: 'admin', password: process.env.DASHBOARD_PASSWORD },
95
+ * readOnly: process.env.NODE_ENV === 'production',
96
+ * }));
97
+ * ```
98
+ */
99
+ export function tricacheDashboard(options: DashboardOptions) {
100
+ return async (req: IncomingMessage, res: ServerResponse, next?: (err?: unknown) => void): Promise<void> => {
101
+ try {
102
+ const webReq = await nodeToWebRequest(req);
103
+ const webRes = await handleDashboardRequest(webReq, options);
104
+ await pipeWebResponseToNode(webRes, res);
105
+ } catch (err) {
106
+ if (typeof next === 'function') {
107
+ next(err);
108
+ } else {
109
+ res.statusCode = 500;
110
+ res.end(JSON.stringify({ error: 'Internal Server Error' }));
111
+ }
112
+ }
113
+ };
114
+ }
115
+
116
+ /**
117
+ * Next.js App Router route handlers for the TriCache dashboard.
118
+ *
119
+ * @example
120
+ * ```typescript
121
+ * // app/admin/cache/[...slug]/route.ts
122
+ * import { createNextDashboardHandlers } from 'tricache/dashboard';
123
+ * import { cache } from '@/lib/cache';
124
+ *
125
+ * export const { GET, POST } = createNextDashboardHandlers({
126
+ * cache,
127
+ * basePath: '/admin/cache',
128
+ * readOnly: true,
129
+ * });
130
+ * ```
131
+ */
132
+ export function createNextDashboardHandlers(options: DashboardOptions) {
133
+ const handler = async (req: Request): Promise<Response> => {
134
+ return handleDashboardRequest(req, options);
135
+ };
136
+ return {
137
+ GET: handler,
138
+ POST: handler,
139
+ };
140
+ }
141
+
142
+ /**
143
+ * Starts a standalone, dedicated HTTP management server for the TriCache dashboard.
144
+ * Ideal for microservices, background workers, and Kubernetes pods via `kubectl port-forward`.
145
+ *
146
+ * @example
147
+ * ```typescript
148
+ * import { startDashboardServer } from 'tricache/dashboard';
149
+ *
150
+ * const mgmt = await startDashboardServer({
151
+ * cache,
152
+ * port: 9090,
153
+ * host: '127.0.0.1',
154
+ * authSecret: process.env.MANAGEMENT_SECRET,
155
+ * });
156
+ * console.log(`Observability dashboard running at http://localhost:${mgmt.port}`);
157
+ * ```
158
+ */
159
+ export async function startDashboardServer(options: StandaloneDashboardOptions): Promise<{
160
+ server: http.Server;
161
+ port: number;
162
+ host: string;
163
+ close: () => Promise<void>;
164
+ }> {
165
+ const port = options.port || 9090;
166
+ const host = options.host || '127.0.0.1';
167
+
168
+ const middleware = tricacheDashboard(options);
169
+ const server = http.createServer((req, res) => {
170
+ middleware(req, res).catch(err => {
171
+ res.statusCode = 500;
172
+ res.end(String(err));
173
+ });
174
+ });
175
+
176
+ await new Promise<void>((resolve, reject) => {
177
+ server.once('error', reject);
178
+ server.listen(port, host, () => {
179
+ server.off('error', reject);
180
+ resolve();
181
+ });
182
+ });
183
+
184
+ const actualAddress = server.address();
185
+ const actualPort = typeof actualAddress === 'object' && actualAddress ? actualAddress.port : port;
186
+
187
+ return {
188
+ server,
189
+ port: actualPort,
190
+ host,
191
+ close: () => new Promise<void>((resolve, reject) => {
192
+ server.close(err => (err ? reject(err) : resolve()));
193
+ }),
194
+ };
195
+ }
@@ -0,0 +1,96 @@
1
+ import type { CacheService } from '../cache-service.js';
2
+
3
+ export interface DashboardPeerInstance {
4
+ name: string;
5
+ url: string;
6
+ }
7
+
8
+ export interface DashboardActionEvent {
9
+ action: 'clear' | 'invalidate-tag';
10
+ target?: string;
11
+ user?: string;
12
+ ip?: string;
13
+ timestamp: number;
14
+ success: boolean;
15
+ error?: string;
16
+ }
17
+
18
+ export interface DashboardOptions {
19
+ /**
20
+ * TriCache instance to monitor and manage.
21
+ */
22
+ cache: CacheService;
23
+
24
+ /**
25
+ * Base mounting path for the dashboard (e.g. '/cache/dashboard' or '/admin/cache').
26
+ * Used for generating relative links and API request paths.
27
+ * @default ''
28
+ */
29
+ basePath?: string;
30
+
31
+ /**
32
+ * Custom title displayed in the browser tab and dashboard header.
33
+ * @default 'TriCache Observability'
34
+ */
35
+ title?: string;
36
+
37
+ /**
38
+ * Identifier for the local container/pod/process.
39
+ * Defaults to process.env.POD_NAME, process.env.HOSTNAME, or os.hostname().
40
+ */
41
+ instanceId?: string;
42
+
43
+ /**
44
+ * Optional list of sibling peer instance URLs for pod switching in multi-pod clusters.
45
+ */
46
+ peerInstances?: DashboardPeerInstance[];
47
+
48
+ /**
49
+ * HTTP Basic Authentication credentials.
50
+ * Evaluated using timing-safe comparison (crypto.timingSafeEqual with SHA-256).
51
+ */
52
+ auth?: {
53
+ username: string;
54
+ password: string;
55
+ };
56
+
57
+ /**
58
+ * Bearer token or URL token query parameter ('?token=...') for API authentication.
59
+ * Evaluated using timing-safe comparison.
60
+ */
61
+ authSecret?: string;
62
+
63
+ /**
64
+ * Enforces read-only mode: disables and blocks all mutating operations
65
+ * (e.g. invalidate tag, clear cache tier). Mutating endpoints return 403 Forbidden.
66
+ * @default false
67
+ */
68
+ readOnly?: boolean;
69
+
70
+ /**
71
+ * Server-Sent Events (SSE) metric stream push interval in milliseconds.
72
+ * @default 2000
73
+ */
74
+ streamIntervalMs?: number;
75
+
76
+ /**
77
+ * Enterprise audit logging hook invoked when an administrative action is attempted.
78
+ * Allows piping security/audit events to Datadog, CloudWatch, Splunk, etc.
79
+ */
80
+ onAction?: (event: DashboardActionEvent) => void;
81
+ }
82
+
83
+ export interface StandaloneDashboardOptions extends DashboardOptions {
84
+ /**
85
+ * Port to listen on.
86
+ * @default 9090
87
+ */
88
+ port?: number;
89
+
90
+ /**
91
+ * Host / IP interface to bind. Defaults to '127.0.0.1' for security.
92
+ * Use '0.0.0.0' only inside private VPCs or behind authenticated reverse proxies.
93
+ * @default '127.0.0.1'
94
+ */
95
+ host?: string;
96
+ }