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.
- package/LICENSE +21 -21
- package/README.md +144 -1428
- package/bin/tricache.js +8 -8
- package/dashboards/tricache-alerts.yaml +54 -0
- package/dashboards/tricache-grafana.json +548 -0
- package/dist/cache-service-5MZXFIGK.js +12 -0
- package/dist/cache-service-CyLdpo77.d.ts +871 -0
- package/dist/{chunk-3XQUQBOZ.js → chunk-6PQ5MOWR.js} +2950 -523
- package/dist/chunk-7RV5ZVBB.js +1023 -0
- package/dist/{chunk-PFB5FMEY.js → chunk-LJ5QJB5O.js} +116 -20
- package/dist/chunk-QNU7B43J.js +687 -0
- package/dist/chunk-UGD4VEPD.js +138 -0
- package/dist/chunk-XZL45ABR.js +108 -0
- package/dist/chunk-ZTSWCFYO.js +135 -0
- package/dist/cli.cjs +2894 -518
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +103 -5
- package/dist/dashboard/index.cjs +1065 -0
- package/dist/dashboard/index.d.ts +183 -0
- package/dist/dashboard/index.js +22 -0
- package/dist/drizzle/index.cjs +2648 -508
- package/dist/drizzle/index.d.ts +3 -1
- package/dist/drizzle/index.js +1 -1
- package/dist/edge/index.cjs +1498 -0
- package/dist/edge/index.d.ts +127 -0
- package/dist/edge/index.js +1109 -0
- package/dist/hono-DQRS1Xd7.d.ts +370 -0
- package/dist/http/index.cjs +3038 -589
- package/dist/http/index.d.ts +109 -23
- package/dist/http/index.js +238 -62
- package/dist/index.cjs +6962 -2487
- package/dist/index.d.ts +547 -30
- package/dist/index.js +104 -5
- package/dist/murmur3-CMGw9PbV.d.ts +98 -0
- package/dist/nestjs/index.cjs +2586 -498
- package/dist/nestjs/index.d.ts +3 -1
- package/dist/nestjs/index.js +6 -3
- package/dist/next/index.cjs +2633 -519
- package/dist/next/index.d.ts +13 -7
- package/dist/next/index.js +45 -18
- package/dist/prisma/index.cjs +2 -1
- package/dist/prisma/index.d.ts +3 -1
- package/dist/prisma/index.js +2 -1
- package/dist/serialize-worker.cjs +31 -19
- package/dist/serialize-worker.js +1 -1
- package/dist/{cache-service-BblHuF3i.d.ts → types-B6krw64X.d.ts} +605 -646
- package/package.json +37 -7
- package/src/adapters/auto-pipeliner.ts +206 -0
- package/src/adapters/node-redis.ts +370 -0
- package/src/cache-service.ts +4231 -3226
- package/src/cli.ts +261 -143
- package/src/codec.ts +72 -0
- package/src/compression.ts +56 -63
- package/src/cross-region.ts +245 -0
- package/src/dashboard/handler.ts +392 -0
- package/src/dashboard/html.ts +594 -0
- package/src/dashboard/index.ts +195 -0
- package/src/dashboard/types.ts +96 -0
- package/src/disk-tier.ts +1223 -821
- package/src/drizzle/index.ts +86 -86
- package/src/edge/adapters/cloudflare-do.ts +130 -0
- package/src/edge/adapters/cloudflare-kv.ts +115 -0
- package/src/edge/adapters/upstash.ts +143 -0
- package/src/edge/cache.ts +797 -0
- package/src/edge/crypto.ts +182 -0
- package/src/edge/hono.ts +203 -0
- package/src/edge/index.ts +37 -0
- package/src/edge/types.ts +230 -0
- package/src/edge/utils/base64.ts +63 -0
- package/src/edge/utils/murmur3.ts +179 -0
- package/src/encryption.ts +635 -493
- package/src/http/express.ts +195 -0
- package/src/http/fastify.ts +260 -0
- package/src/http/index.ts +46 -223
- package/src/http/utils.ts +92 -0
- package/src/index.ts +149 -47
- package/src/ipc-telemetry.ts +481 -0
- package/src/latency-watchdog.ts +282 -0
- package/src/nestjs/decorators.ts +161 -161
- package/src/nestjs/index.ts +23 -23
- package/src/nestjs/tricache.module.ts +104 -104
- package/src/nestjs/tricache.store.ts +97 -97
- package/src/nestjs/types.ts +79 -79
- package/src/next/cache-handler.ts +271 -244
- package/src/next/index.ts +37 -37
- package/src/next/isr-handler.ts +105 -105
- package/src/next/types.ts +130 -130
- package/src/prisma/index.ts +154 -151
- package/src/remote-snapshot.ts +153 -0
- package/src/serialize-worker.ts +120 -120
- package/src/sigv4-snapshot-adapter.ts +260 -0
- package/src/smart-memory-cache.ts +1109 -1024
- package/src/types.ts +1199 -902
- package/src/utils/cgroup.ts +100 -0
- package/src/utils/tracing.ts +54 -0
- package/src/wasm/bloom-filter-wasm.ts +109 -102
- package/src/worker-pool.ts +299 -299
- package/src/wtiny-lfu.ts +624 -0
- package/dist/cache-service-AJASH5FX.js +0 -7
- package/dist/chunk-UBSR5UYA.js +0 -0
package/src/prisma/index.ts
CHANGED
|
@@ -1,151 +1,154 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* tricache/prisma — First-class Prisma Client extension for TriCache.
|
|
3
|
-
*
|
|
4
|
-
* Usage:
|
|
5
|
-
* import { PrismaClient } from '@prisma/client';
|
|
6
|
-
* import { withTriCache } from 'tricache/prisma';
|
|
7
|
-
*
|
|
8
|
-
* const prisma = new PrismaClient().$extends(
|
|
9
|
-
* withTriCache({
|
|
10
|
-
* cache,
|
|
11
|
-
* defaultTtl: 300,
|
|
12
|
-
* autoInvalidate: true, // Invalidate model tags on create/update/delete
|
|
13
|
-
* })
|
|
14
|
-
* );
|
|
15
|
-
*
|
|
16
|
-
* // Query with cache options:
|
|
17
|
-
* const users = await prisma.user.findMany({
|
|
18
|
-
* where: { active: true },
|
|
19
|
-
* cache: { ttl: 60, tags: ['users'], swr: 30 },
|
|
20
|
-
* });
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
import type { CacheService } from '../cache-service.js';
|
|
24
|
-
import type { WrapOptions } from '../types.js';
|
|
25
|
-
import crypto from 'crypto';
|
|
26
|
-
|
|
27
|
-
export interface PrismaCacheQueryOptions extends WrapOptions {
|
|
28
|
-
/** Explicit cache key. Defaults to SHA-256 hash of (model + operation + args). */
|
|
29
|
-
key?: string;
|
|
30
|
-
/** Explicit tags for invalidation. Automatically includes `[model.toLowerCase()]`. */
|
|
31
|
-
tags?: string[];
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export interface PrismaTriCacheOptions {
|
|
35
|
-
/** TriCache instance to use for caching. */
|
|
36
|
-
cache: CacheService;
|
|
37
|
-
/** Default TTL in seconds for cached queries (default: 300 s). */
|
|
38
|
-
defaultTtl?: number;
|
|
39
|
-
/** Whether to automatically invalidate model tags on write operations (default: true). */
|
|
40
|
-
autoInvalidate?: boolean;
|
|
41
|
-
/** Automatically cache all read queries even if `cache` options are not provided (default: false). */
|
|
42
|
-
autoCache?: boolean;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
const READ_OPERATIONS = new Set([
|
|
46
|
-
'findUnique',
|
|
47
|
-
'findUniqueOrThrow',
|
|
48
|
-
'findFirst',
|
|
49
|
-
'findFirstOrThrow',
|
|
50
|
-
'findMany',
|
|
51
|
-
'count',
|
|
52
|
-
'aggregate',
|
|
53
|
-
'groupBy',
|
|
54
|
-
]);
|
|
55
|
-
|
|
56
|
-
const MUTATION_OPERATIONS = new Set([
|
|
57
|
-
'create',
|
|
58
|
-
'createMany',
|
|
59
|
-
'createManyAndReturn',
|
|
60
|
-
'update',
|
|
61
|
-
'updateMany',
|
|
62
|
-
'updateManyAndReturn',
|
|
63
|
-
'upsert',
|
|
64
|
-
'delete',
|
|
65
|
-
'deleteMany',
|
|
66
|
-
]);
|
|
67
|
-
|
|
68
|
-
/** Recursively sorts object keys for deterministic serialization of nested query filters */
|
|
69
|
-
function deterministicStringify(val: unknown): string {
|
|
70
|
-
if (val === null || typeof val !== 'object') {
|
|
71
|
-
return JSON.stringify(val);
|
|
72
|
-
}
|
|
73
|
-
if (Array.isArray(val)) {
|
|
74
|
-
return `[${val.map(deterministicStringify).join(',')}]`;
|
|
75
|
-
}
|
|
76
|
-
const obj = val as Record<string, unknown>;
|
|
77
|
-
const keys = Object.keys(obj).sort();
|
|
78
|
-
return `{${keys.map(k => `${JSON.stringify(k)}:${deterministicStringify(obj[k])}`).join(',')}}`;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/** Generate a deterministic SHA-256 hash key for a Prisma query. */
|
|
82
|
-
export function generatePrismaCacheKey(model: string, operation: string, args: unknown): string {
|
|
83
|
-
const normalized = deterministicStringify({ model, operation, args });
|
|
84
|
-
const hash = crypto.createHash('sha256').update(normalized, 'utf8').digest('hex').slice(0, 32);
|
|
85
|
-
return `prisma:${model.toLowerCase()}:${operation}:${hash}`;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Creates a Prisma Client Extension for TriCache.
|
|
90
|
-
*/
|
|
91
|
-
export function withTriCache(options: PrismaTriCacheOptions) {
|
|
92
|
-
const { cache, defaultTtl = 300, autoInvalidate = true, autoCache = false } = options;
|
|
93
|
-
|
|
94
|
-
return {
|
|
95
|
-
name: 'tricache-prisma-extension',
|
|
96
|
-
query: {
|
|
97
|
-
$allModels: {
|
|
98
|
-
async $allOperations({ model, operation, args, query }: {
|
|
99
|
-
model: string;
|
|
100
|
-
operation: string;
|
|
101
|
-
args: Record<string, unknown> | undefined;
|
|
102
|
-
query: (args: unknown) => Promise<unknown>;
|
|
103
|
-
}) {
|
|
104
|
-
const modelTag = model.toLowerCase();
|
|
105
|
-
|
|
106
|
-
// Auto-invalidation on write operations
|
|
107
|
-
if (autoInvalidate && MUTATION_OPERATIONS.has(operation)) {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
const
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* tricache/prisma — First-class Prisma Client extension for TriCache.
|
|
3
|
+
*
|
|
4
|
+
* Usage:
|
|
5
|
+
* import { PrismaClient } from '@prisma/client';
|
|
6
|
+
* import { withTriCache } from 'tricache/prisma';
|
|
7
|
+
*
|
|
8
|
+
* const prisma = new PrismaClient().$extends(
|
|
9
|
+
* withTriCache({
|
|
10
|
+
* cache,
|
|
11
|
+
* defaultTtl: 300,
|
|
12
|
+
* autoInvalidate: true, // Invalidate model tags on create/update/delete
|
|
13
|
+
* })
|
|
14
|
+
* );
|
|
15
|
+
*
|
|
16
|
+
* // Query with cache options:
|
|
17
|
+
* const users = await prisma.user.findMany({
|
|
18
|
+
* where: { active: true },
|
|
19
|
+
* cache: { ttl: 60, tags: ['users'], swr: 30 },
|
|
20
|
+
* });
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import type { CacheService } from '../cache-service.js';
|
|
24
|
+
import type { WrapOptions } from '../types.js';
|
|
25
|
+
import crypto from 'crypto';
|
|
26
|
+
|
|
27
|
+
export interface PrismaCacheQueryOptions extends WrapOptions {
|
|
28
|
+
/** Explicit cache key. Defaults to SHA-256 hash of (model + operation + args). */
|
|
29
|
+
key?: string;
|
|
30
|
+
/** Explicit tags for invalidation. Automatically includes `[model.toLowerCase()]`. */
|
|
31
|
+
tags?: string[];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface PrismaTriCacheOptions {
|
|
35
|
+
/** TriCache instance to use for caching. */
|
|
36
|
+
cache: CacheService;
|
|
37
|
+
/** Default TTL in seconds for cached queries (default: 300 s). */
|
|
38
|
+
defaultTtl?: number;
|
|
39
|
+
/** Whether to automatically invalidate model tags on write operations (default: true). */
|
|
40
|
+
autoInvalidate?: boolean;
|
|
41
|
+
/** Automatically cache all read queries even if `cache` options are not provided (default: false). */
|
|
42
|
+
autoCache?: boolean;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const READ_OPERATIONS = new Set([
|
|
46
|
+
'findUnique',
|
|
47
|
+
'findUniqueOrThrow',
|
|
48
|
+
'findFirst',
|
|
49
|
+
'findFirstOrThrow',
|
|
50
|
+
'findMany',
|
|
51
|
+
'count',
|
|
52
|
+
'aggregate',
|
|
53
|
+
'groupBy',
|
|
54
|
+
]);
|
|
55
|
+
|
|
56
|
+
const MUTATION_OPERATIONS = new Set([
|
|
57
|
+
'create',
|
|
58
|
+
'createMany',
|
|
59
|
+
'createManyAndReturn',
|
|
60
|
+
'update',
|
|
61
|
+
'updateMany',
|
|
62
|
+
'updateManyAndReturn',
|
|
63
|
+
'upsert',
|
|
64
|
+
'delete',
|
|
65
|
+
'deleteMany',
|
|
66
|
+
]);
|
|
67
|
+
|
|
68
|
+
/** Recursively sorts object keys for deterministic serialization of nested query filters */
|
|
69
|
+
function deterministicStringify(val: unknown): string {
|
|
70
|
+
if (val === null || typeof val !== 'object') {
|
|
71
|
+
return JSON.stringify(val);
|
|
72
|
+
}
|
|
73
|
+
if (Array.isArray(val)) {
|
|
74
|
+
return `[${val.map(deterministicStringify).join(',')}]`;
|
|
75
|
+
}
|
|
76
|
+
const obj = val as Record<string, unknown>;
|
|
77
|
+
const keys = Object.keys(obj).sort();
|
|
78
|
+
return `{${keys.map(k => `${JSON.stringify(k)}:${deterministicStringify(obj[k])}`).join(',')}}`;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Generate a deterministic SHA-256 hash key for a Prisma query. */
|
|
82
|
+
export function generatePrismaCacheKey(model: string, operation: string, args: unknown): string {
|
|
83
|
+
const normalized = deterministicStringify({ model, operation, args });
|
|
84
|
+
const hash = crypto.createHash('sha256').update(normalized, 'utf8').digest('hex').slice(0, 32);
|
|
85
|
+
return `prisma:${model.toLowerCase()}:${operation}:${hash}`;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Creates a Prisma Client Extension for TriCache.
|
|
90
|
+
*/
|
|
91
|
+
export function withTriCache(options: PrismaTriCacheOptions) {
|
|
92
|
+
const { cache, defaultTtl = 300, autoInvalidate = true, autoCache = false } = options;
|
|
93
|
+
|
|
94
|
+
return {
|
|
95
|
+
name: 'tricache-prisma-extension',
|
|
96
|
+
query: {
|
|
97
|
+
$allModels: {
|
|
98
|
+
async $allOperations({ model, operation, args, query }: {
|
|
99
|
+
model: string;
|
|
100
|
+
operation: string;
|
|
101
|
+
args: Record<string, unknown> | undefined;
|
|
102
|
+
query: (args: unknown) => Promise<unknown>;
|
|
103
|
+
}) {
|
|
104
|
+
const modelTag = model.toLowerCase();
|
|
105
|
+
|
|
106
|
+
// Auto-invalidation on write operations
|
|
107
|
+
if (autoInvalidate && MUTATION_OPERATIONS.has(operation)) {
|
|
108
|
+
// Strip the cache pseudo-option here too — the read path removes it,
|
|
109
|
+
// but the mutation path forwarded it verbatim to the Prisma engine.
|
|
110
|
+
const { cache: _cacheOpt, ...cleanMutationArgs } = (args ?? {}) as Record<string, unknown>;
|
|
111
|
+
const result = await query(cleanMutationArgs);
|
|
112
|
+
await cache.invalidateTag(modelTag);
|
|
113
|
+
return result;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// Read operations
|
|
117
|
+
if (READ_OPERATIONS.has(operation)) {
|
|
118
|
+
const queryArgs = args ?? {};
|
|
119
|
+
const cacheConfig = (queryArgs as { cache?: PrismaCacheQueryOptions | boolean }).cache;
|
|
120
|
+
|
|
121
|
+
if (cacheConfig !== undefined || autoCache) {
|
|
122
|
+
const opts: PrismaCacheQueryOptions = typeof cacheConfig === 'object' && cacheConfig !== null
|
|
123
|
+
? cacheConfig
|
|
124
|
+
: {};
|
|
125
|
+
|
|
126
|
+
// Clone args without the `cache` property to pass clean args to Prisma query engine
|
|
127
|
+
const { cache: _c, ...cleanArgs } = queryArgs as Record<string, unknown>;
|
|
128
|
+
|
|
129
|
+
const key = opts.key ?? generatePrismaCacheKey(model, operation, cleanArgs);
|
|
130
|
+
const tags = [...new Set([modelTag, ...(opts.tags ?? [])])];
|
|
131
|
+
|
|
132
|
+
return cache.wrap(
|
|
133
|
+
key,
|
|
134
|
+
() => query(cleanArgs),
|
|
135
|
+
{
|
|
136
|
+
ttl: opts.ttl ?? defaultTtl,
|
|
137
|
+
swr: opts.swr,
|
|
138
|
+
priority: opts.priority,
|
|
139
|
+
tags,
|
|
140
|
+
dependsOn: opts.dependsOn,
|
|
141
|
+
refreshAhead: opts.refreshAhead,
|
|
142
|
+
xfetchBeta: opts.xfetchBeta,
|
|
143
|
+
notFoundTtl: opts.notFoundTtl,
|
|
144
|
+
},
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return query(args);
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
},
|
|
153
|
+
};
|
|
154
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* tricache — Remote Blob Storage Cold-Start Hydration
|
|
3
|
+
*
|
|
4
|
+
* Enables stateless containers (Kubernetes, AWS ECS/Fargate, Google Cloud Run, Fly.io)
|
|
5
|
+
* to persist and hydrate L1 cache snapshots from remote object storage (S3, GCS, R2, Azure, MinIO, or HTTP).
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export interface IRemoteSnapshotAdapter {
|
|
9
|
+
/**
|
|
10
|
+
* Read the latest snapshot blob from remote storage.
|
|
11
|
+
* Returns null if no snapshot exists yet (e.g. 404 Not Found / first deploy).
|
|
12
|
+
*/
|
|
13
|
+
get(): Promise<Buffer | Uint8Array | null>;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Write the snapshot blob to remote storage.
|
|
17
|
+
*/
|
|
18
|
+
put(data: Buffer): Promise<void>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
import type { EnvelopeEncryptionOptions } from './encryption';
|
|
22
|
+
|
|
23
|
+
export interface RemoteSnapshotOptions {
|
|
24
|
+
/**
|
|
25
|
+
* Remote storage adapter implementation.
|
|
26
|
+
*/
|
|
27
|
+
adapter: IRemoteSnapshotAdapter;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Maximum age in milliseconds of a remote snapshot before it is rejected as stale.
|
|
31
|
+
* Default: 2 hours (7,200,000 ms).
|
|
32
|
+
*/
|
|
33
|
+
maxAgeMs?: number;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Whether to trigger an asynchronous remote snapshot upload during graceful shutdown (SIGTERM/SIGINT).
|
|
37
|
+
* Ensure your container orchestration allows sufficient termination grace period (e.g. >= 10s).
|
|
38
|
+
* Default: true.
|
|
39
|
+
*/
|
|
40
|
+
saveOnShutdown?: boolean;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Optional periodic background upload interval in milliseconds.
|
|
44
|
+
* Ensures remote snapshots stay fresh even in the event of ungraceful container termination (SIGKILL / OOM / spot eviction).
|
|
45
|
+
* Default: undefined (disabled).
|
|
46
|
+
*/
|
|
47
|
+
intervalMs?: number;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Optional asymmetric envelope encryption configuration.
|
|
51
|
+
* When configured, snapshot payloads are encrypted with an ephemeral AES-256 DEK wrapped by RSA-OAEP or KMS.
|
|
52
|
+
*/
|
|
53
|
+
envelope?: EnvelopeEncryptionOptions;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface HttpSnapshotAdapterOptions {
|
|
57
|
+
/**
|
|
58
|
+
* URL to fetch the snapshot from (e.g. S3 presigned GET URL or internal blob service).
|
|
59
|
+
*/
|
|
60
|
+
getUrl: string;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* URL to upload the snapshot to via HTTP PUT. If omitted, defaults to `getUrl`.
|
|
64
|
+
*/
|
|
65
|
+
putUrl?: string;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Optional HTTP headers (e.g. Authorization or custom metadata).
|
|
69
|
+
*/
|
|
70
|
+
headers?: Record<string, string>;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Creates an HTTP-based remote snapshot adapter using standard global `fetch()`.
|
|
75
|
+
* Ideal for S3/R2 presigned URLs, internal storage gateways, or serverless webhooks.
|
|
76
|
+
*/
|
|
77
|
+
export function createHttpSnapshotAdapter(options: HttpSnapshotAdapterOptions): IRemoteSnapshotAdapter {
|
|
78
|
+
const getUrl = options.getUrl;
|
|
79
|
+
const putUrl = options.putUrl ?? getUrl;
|
|
80
|
+
const headers = options.headers ?? {};
|
|
81
|
+
|
|
82
|
+
return {
|
|
83
|
+
async get(): Promise<Buffer | null> {
|
|
84
|
+
const res = await fetch(getUrl, {
|
|
85
|
+
method: 'GET',
|
|
86
|
+
headers,
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
if (res.status === 404) return null;
|
|
90
|
+
if (!res.ok) {
|
|
91
|
+
throw new Error(`HttpSnapshotAdapter: GET failed with status ${res.status} ${res.statusText}`);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const ab = await res.arrayBuffer();
|
|
95
|
+
return Buffer.from(ab);
|
|
96
|
+
},
|
|
97
|
+
|
|
98
|
+
async put(data: Buffer): Promise<void> {
|
|
99
|
+
const res = await fetch(putUrl, {
|
|
100
|
+
method: 'PUT',
|
|
101
|
+
headers: {
|
|
102
|
+
'Content-Type': 'application/octet-stream',
|
|
103
|
+
...headers,
|
|
104
|
+
},
|
|
105
|
+
body: data as unknown as BodyInit,
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
if (!res.ok) {
|
|
109
|
+
throw new Error(`HttpSnapshotAdapter: PUT failed with status ${res.status} ${res.statusText}`);
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Helper to define and type-check a custom remote snapshot adapter (e.g. AWS SDK v3, GCS, Azure Blob).
|
|
117
|
+
*/
|
|
118
|
+
export function createCustomSnapshotAdapter(adapter: IRemoteSnapshotAdapter): IRemoteSnapshotAdapter {
|
|
119
|
+
return adapter;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* In-memory snapshot adapter designed for unit testing, CI pipelines, and ephemeral mock environments.
|
|
124
|
+
*/
|
|
125
|
+
export function createMemorySnapshotAdapter(): IRemoteSnapshotAdapter & {
|
|
126
|
+
getBuffer(): Buffer | null;
|
|
127
|
+
clear(): void;
|
|
128
|
+
} {
|
|
129
|
+
let stored: Buffer | null = null;
|
|
130
|
+
|
|
131
|
+
return {
|
|
132
|
+
async get(): Promise<Buffer | null> {
|
|
133
|
+
return stored ? Buffer.from(stored) : null;
|
|
134
|
+
},
|
|
135
|
+
async put(data: Buffer): Promise<void> {
|
|
136
|
+
stored = Buffer.from(data);
|
|
137
|
+
},
|
|
138
|
+
getBuffer(): Buffer | null {
|
|
139
|
+
return stored;
|
|
140
|
+
},
|
|
141
|
+
clear(): void {
|
|
142
|
+
stored = null;
|
|
143
|
+
},
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export {
|
|
148
|
+
SigV4SnapshotAdapter,
|
|
149
|
+
createSigV4SnapshotAdapter,
|
|
150
|
+
createS3SnapshotAdapter,
|
|
151
|
+
createR2SnapshotAdapter,
|
|
152
|
+
type SigV4SnapshotAdapterOptions,
|
|
153
|
+
} from './sigv4-snapshot-adapter.js';
|