@effing/ffs 0.4.0 → 0.5.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.
@@ -0,0 +1,341 @@
1
+ // src/fetch.ts
2
+ import { fetch, Agent } from "undici";
3
+ async function ffsFetch(url, options) {
4
+ const {
5
+ method,
6
+ body,
7
+ headers,
8
+ headersTimeout = 3e5,
9
+ // 5 minutes
10
+ bodyTimeout = 3e5
11
+ // 5 minutes
12
+ } = options ?? {};
13
+ const agent = new Agent({ headersTimeout, bodyTimeout });
14
+ return fetch(url, {
15
+ method,
16
+ body,
17
+ headers: { "User-Agent": "FFS (+https://effing.dev/ffs)", ...headers },
18
+ dispatcher: agent
19
+ });
20
+ }
21
+
22
+ // src/storage.ts
23
+ import {
24
+ S3Client,
25
+ PutObjectCommand,
26
+ GetObjectCommand,
27
+ HeadObjectCommand,
28
+ DeleteObjectCommand
29
+ } from "@aws-sdk/client-s3";
30
+ import { Upload } from "@aws-sdk/lib-storage";
31
+ import fs from "fs/promises";
32
+ import { createReadStream, createWriteStream, existsSync } from "fs";
33
+ import { pipeline } from "stream/promises";
34
+ import path from "path";
35
+ import os from "os";
36
+ import crypto from "crypto";
37
+ var DEFAULT_SOURCE_TTL_MS = 60 * 60 * 1e3;
38
+ var DEFAULT_JOB_METADATA_TTL_MS = 8 * 60 * 60 * 1e3;
39
+ var S3TransientStore = class {
40
+ client;
41
+ bucket;
42
+ prefix;
43
+ sourceTtlMs;
44
+ jobMetadataTtlMs;
45
+ constructor(options) {
46
+ this.client = new S3Client({
47
+ endpoint: options.endpoint,
48
+ region: options.region ?? "auto",
49
+ credentials: options.accessKeyId ? {
50
+ accessKeyId: options.accessKeyId,
51
+ secretAccessKey: options.secretAccessKey
52
+ } : void 0,
53
+ forcePathStyle: !!options.endpoint
54
+ });
55
+ this.bucket = options.bucket;
56
+ this.prefix = options.prefix ?? "";
57
+ this.sourceTtlMs = options.sourceTtlMs ?? DEFAULT_SOURCE_TTL_MS;
58
+ this.jobMetadataTtlMs = options.jobMetadataTtlMs ?? DEFAULT_JOB_METADATA_TTL_MS;
59
+ }
60
+ getExpires(ttlMs) {
61
+ return new Date(Date.now() + ttlMs);
62
+ }
63
+ getFullKey(key) {
64
+ return `${this.prefix}${key}`;
65
+ }
66
+ async put(key, stream, ttlMs) {
67
+ const upload = new Upload({
68
+ client: this.client,
69
+ params: {
70
+ Bucket: this.bucket,
71
+ Key: this.getFullKey(key),
72
+ Body: stream,
73
+ Expires: this.getExpires(ttlMs ?? this.sourceTtlMs)
74
+ }
75
+ });
76
+ await upload.done();
77
+ }
78
+ async getStream(key) {
79
+ try {
80
+ const response = await this.client.send(
81
+ new GetObjectCommand({
82
+ Bucket: this.bucket,
83
+ Key: this.getFullKey(key)
84
+ })
85
+ );
86
+ return response.Body;
87
+ } catch (err) {
88
+ const error = err;
89
+ if (error.name === "NoSuchKey" || error.$metadata?.httpStatusCode === 404) {
90
+ return null;
91
+ }
92
+ throw err;
93
+ }
94
+ }
95
+ async exists(key) {
96
+ try {
97
+ await this.client.send(
98
+ new HeadObjectCommand({
99
+ Bucket: this.bucket,
100
+ Key: this.getFullKey(key)
101
+ })
102
+ );
103
+ return true;
104
+ } catch (err) {
105
+ const error = err;
106
+ if (error.name === "NotFound" || error.$metadata?.httpStatusCode === 404) {
107
+ return false;
108
+ }
109
+ throw err;
110
+ }
111
+ }
112
+ async existsMany(keys) {
113
+ const results = await Promise.all(
114
+ keys.map(async (key) => [key, await this.exists(key)])
115
+ );
116
+ return new Map(results);
117
+ }
118
+ async delete(key) {
119
+ try {
120
+ await this.client.send(
121
+ new DeleteObjectCommand({
122
+ Bucket: this.bucket,
123
+ Key: this.getFullKey(key)
124
+ })
125
+ );
126
+ } catch (err) {
127
+ const error = err;
128
+ if (error.name === "NoSuchKey" || error.$metadata?.httpStatusCode === 404) {
129
+ return;
130
+ }
131
+ throw err;
132
+ }
133
+ }
134
+ async putJson(key, data, ttlMs) {
135
+ await this.client.send(
136
+ new PutObjectCommand({
137
+ Bucket: this.bucket,
138
+ Key: this.getFullKey(key),
139
+ Body: JSON.stringify(data),
140
+ ContentType: "application/json",
141
+ Expires: this.getExpires(ttlMs ?? this.jobMetadataTtlMs)
142
+ })
143
+ );
144
+ }
145
+ async getJson(key) {
146
+ try {
147
+ const response = await this.client.send(
148
+ new GetObjectCommand({
149
+ Bucket: this.bucket,
150
+ Key: this.getFullKey(key)
151
+ })
152
+ );
153
+ const body = await response.Body?.transformToString();
154
+ if (!body) return null;
155
+ return JSON.parse(body);
156
+ } catch (err) {
157
+ const error = err;
158
+ if (error.name === "NoSuchKey" || error.$metadata?.httpStatusCode === 404) {
159
+ return null;
160
+ }
161
+ throw err;
162
+ }
163
+ }
164
+ close() {
165
+ }
166
+ };
167
+ var LocalTransientStore = class {
168
+ baseDir;
169
+ initialized = false;
170
+ cleanupInterval;
171
+ sourceTtlMs;
172
+ jobMetadataTtlMs;
173
+ /** For cleanup, use the longer of the two TTLs */
174
+ maxTtlMs;
175
+ constructor(options) {
176
+ this.baseDir = options?.baseDir ?? path.join(os.tmpdir(), "ffs-transient");
177
+ this.sourceTtlMs = options?.sourceTtlMs ?? DEFAULT_SOURCE_TTL_MS;
178
+ this.jobMetadataTtlMs = options?.jobMetadataTtlMs ?? DEFAULT_JOB_METADATA_TTL_MS;
179
+ this.maxTtlMs = Math.max(this.sourceTtlMs, this.jobMetadataTtlMs);
180
+ this.cleanupInterval = setInterval(() => {
181
+ this.cleanupExpired().catch(console.error);
182
+ }, 3e5);
183
+ }
184
+ /**
185
+ * Remove files older than max TTL
186
+ */
187
+ async cleanupExpired() {
188
+ if (!this.initialized) return;
189
+ const now = Date.now();
190
+ await this.cleanupDir(this.baseDir, now);
191
+ }
192
+ async cleanupDir(dir, now) {
193
+ let entries;
194
+ try {
195
+ entries = await fs.readdir(dir, { withFileTypes: true });
196
+ } catch {
197
+ return;
198
+ }
199
+ for (const entry of entries) {
200
+ const fullPath = path.join(dir, entry.name);
201
+ if (entry.isDirectory()) {
202
+ await this.cleanupDir(fullPath, now);
203
+ try {
204
+ await fs.rmdir(fullPath);
205
+ } catch {
206
+ }
207
+ } else if (entry.isFile()) {
208
+ try {
209
+ const stat = await fs.stat(fullPath);
210
+ if (now - stat.mtimeMs > this.maxTtlMs) {
211
+ await fs.rm(fullPath, { force: true });
212
+ }
213
+ } catch {
214
+ }
215
+ }
216
+ }
217
+ }
218
+ async ensureDir(filePath) {
219
+ await fs.mkdir(path.dirname(filePath), { recursive: true });
220
+ this.initialized = true;
221
+ }
222
+ filePath(key) {
223
+ return path.join(this.baseDir, key);
224
+ }
225
+ tmpPathFor(finalPath) {
226
+ const rand = crypto.randomBytes(8).toString("hex");
227
+ return `${finalPath}.tmp-${process.pid}-${rand}`;
228
+ }
229
+ async put(key, stream, _ttlMs) {
230
+ const fp = this.filePath(key);
231
+ await this.ensureDir(fp);
232
+ const tmpPath = this.tmpPathFor(fp);
233
+ try {
234
+ const writeStream = createWriteStream(tmpPath);
235
+ await pipeline(stream, writeStream);
236
+ await fs.rename(tmpPath, fp);
237
+ } catch (err) {
238
+ await fs.rm(tmpPath, { force: true }).catch(() => {
239
+ });
240
+ throw err;
241
+ }
242
+ }
243
+ async getStream(key) {
244
+ const fp = this.filePath(key);
245
+ if (!existsSync(fp)) return null;
246
+ return createReadStream(fp);
247
+ }
248
+ async exists(key) {
249
+ try {
250
+ await fs.access(this.filePath(key));
251
+ return true;
252
+ } catch {
253
+ return false;
254
+ }
255
+ }
256
+ async existsMany(keys) {
257
+ const results = await Promise.all(
258
+ keys.map(async (key) => [key, await this.exists(key)])
259
+ );
260
+ return new Map(results);
261
+ }
262
+ async delete(key) {
263
+ await fs.rm(this.filePath(key), { force: true });
264
+ }
265
+ async putJson(key, data, _ttlMs) {
266
+ const fp = this.filePath(key);
267
+ await this.ensureDir(fp);
268
+ const tmpPath = this.tmpPathFor(fp);
269
+ try {
270
+ await fs.writeFile(tmpPath, JSON.stringify(data));
271
+ await fs.rename(tmpPath, fp);
272
+ } catch (err) {
273
+ await fs.rm(tmpPath, { force: true }).catch(() => {
274
+ });
275
+ throw err;
276
+ }
277
+ }
278
+ async getJson(key) {
279
+ try {
280
+ const content = await fs.readFile(this.filePath(key), "utf-8");
281
+ return JSON.parse(content);
282
+ } catch {
283
+ return null;
284
+ }
285
+ }
286
+ close() {
287
+ if (this.cleanupInterval) {
288
+ clearInterval(this.cleanupInterval);
289
+ this.cleanupInterval = void 0;
290
+ }
291
+ }
292
+ };
293
+ function createTransientStore() {
294
+ const sourceTtlMs = process.env.FFS_SOURCE_CACHE_TTL_MS ? parseInt(process.env.FFS_SOURCE_CACHE_TTL_MS, 10) : DEFAULT_SOURCE_TTL_MS;
295
+ const jobMetadataTtlMs = process.env.FFS_JOB_METADATA_TTL_MS ? parseInt(process.env.FFS_JOB_METADATA_TTL_MS, 10) : DEFAULT_JOB_METADATA_TTL_MS;
296
+ if (process.env.FFS_TRANSIENT_STORE_BUCKET) {
297
+ return new S3TransientStore({
298
+ endpoint: process.env.FFS_TRANSIENT_STORE_ENDPOINT,
299
+ region: process.env.FFS_TRANSIENT_STORE_REGION ?? "auto",
300
+ bucket: process.env.FFS_TRANSIENT_STORE_BUCKET,
301
+ prefix: process.env.FFS_TRANSIENT_STORE_PREFIX,
302
+ accessKeyId: process.env.FFS_TRANSIENT_STORE_ACCESS_KEY,
303
+ secretAccessKey: process.env.FFS_TRANSIENT_STORE_SECRET_KEY,
304
+ sourceTtlMs,
305
+ jobMetadataTtlMs
306
+ });
307
+ }
308
+ return new LocalTransientStore({
309
+ baseDir: process.env.FFS_TRANSIENT_STORE_LOCAL_DIR,
310
+ sourceTtlMs,
311
+ jobMetadataTtlMs
312
+ });
313
+ }
314
+ function hashUrl(url) {
315
+ return crypto.createHash("sha256").update(url).digest("hex").slice(0, 16);
316
+ }
317
+ function sourceStoreKey(url) {
318
+ return `sources/${hashUrl(url)}`;
319
+ }
320
+ function warmupJobStoreKey(jobId) {
321
+ return `jobs/warmup/${jobId}.json`;
322
+ }
323
+ function renderJobStoreKey(jobId) {
324
+ return `jobs/render/${jobId}.json`;
325
+ }
326
+ function warmupAndRenderJobStoreKey(jobId) {
327
+ return `jobs/warmup-and-render/${jobId}.json`;
328
+ }
329
+ var storeKeys = {
330
+ source: sourceStoreKey,
331
+ warmupJob: warmupJobStoreKey,
332
+ renderJob: renderJobStoreKey,
333
+ warmupAndRenderJob: warmupAndRenderJobStoreKey
334
+ };
335
+
336
+ export {
337
+ ffsFetch,
338
+ createTransientStore,
339
+ storeKeys
340
+ };
341
+ //# sourceMappingURL=chunk-5SGOYTM2.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/fetch.ts","../src/storage.ts"],"sourcesContent":["import { fetch, Agent, type Response, type BodyInit } from \"undici\";\n\n/**\n * Options for ffsFetch function\n */\nexport type FfsFetchOptions = {\n /** HTTP method */\n method?: \"GET\" | \"POST\" | \"PUT\" | \"DELETE\" | \"PATCH\" | \"HEAD\" | \"OPTIONS\";\n /** Request body */\n body?: BodyInit;\n /** Headers to send (merged with default User-Agent) */\n headers?: Record<string, string>;\n /** Timeout for receiving response headers in ms. @default 300000 (5 min) */\n headersTimeout?: number;\n /** Timeout between body data chunks in ms. 0 = no timeout. @default 300000 (5 min) */\n bodyTimeout?: number;\n};\n\n/**\n * Fetch with default User-Agent and configurable timeouts.\n *\n * @example\n * // Simple GET\n * const response = await ffsFetch(\"https://example.com/data.json\");\n *\n * @example\n * // Large file with infinite body timeout\n * const response = await ffsFetch(\"https://example.com/video.mp4\", {\n * bodyTimeout: 0,\n * });\n *\n * @example\n * // PUT upload\n * const response = await ffsFetch(\"https://s3.example.com/video.mp4\", {\n * method: \"PUT\",\n * body: videoBuffer,\n * bodyTimeout: 0,\n * headers: { \"Content-Type\": \"video/mp4\" },\n * });\n */\nexport async function ffsFetch(\n url: string,\n options?: FfsFetchOptions,\n): Promise<Response> {\n const {\n method,\n body,\n headers,\n headersTimeout = 300000, // 5 minutes\n bodyTimeout = 300000, // 5 minutes\n } = options ?? {};\n\n const agent = new Agent({ headersTimeout, bodyTimeout });\n\n return fetch(url, {\n method,\n body,\n headers: { \"User-Agent\": \"FFS (+https://effing.dev/ffs)\", ...headers },\n dispatcher: agent,\n });\n}\n","import {\n S3Client,\n PutObjectCommand,\n GetObjectCommand,\n HeadObjectCommand,\n DeleteObjectCommand,\n} from \"@aws-sdk/client-s3\";\nimport { Upload } from \"@aws-sdk/lib-storage\";\nimport fs from \"fs/promises\";\nimport { createReadStream, createWriteStream, existsSync } from \"fs\";\nimport { pipeline } from \"stream/promises\";\nimport path from \"path\";\nimport os from \"os\";\nimport crypto from \"crypto\";\nimport type { Readable } from \"stream\";\n\n/** Default TTL for sources: 60 minutes */\nconst DEFAULT_SOURCE_TTL_MS = 60 * 60 * 1000;\n/** Default TTL for job metadata: 8 hours */\nconst DEFAULT_JOB_METADATA_TTL_MS = 8 * 60 * 60 * 1000;\n\n/**\n * Transient store interface for caching sources and storing ephemeral job metadata.\n */\nexport interface TransientStore {\n /** TTL for cached sources in milliseconds */\n readonly sourceTtlMs: number;\n /** TTL for job metadata in milliseconds */\n readonly jobMetadataTtlMs: number;\n /** Store a stream with the given key and optional TTL override */\n put(key: string, stream: Readable, ttlMs?: number): Promise<void>;\n /** Get a stream for the given key, or null if not found */\n getStream(key: string): Promise<Readable | null>;\n /** Check if a key exists */\n exists(key: string): Promise<boolean>;\n /** Check if multiple keys exist (batch operation) */\n existsMany(keys: string[]): Promise<Map<string, boolean>>;\n /** Delete a key */\n delete(key: string): Promise<void>;\n /** Store JSON data with optional TTL override */\n putJson(key: string, data: object, ttlMs?: number): Promise<void>;\n /** Get JSON data, or null if not found */\n getJson<T>(key: string): Promise<T | null>;\n /** Close and cleanup resources */\n close(): void;\n}\n\n/**\n * S3-compatible transient store implementation\n */\nexport class S3TransientStore implements TransientStore {\n private client: S3Client;\n private bucket: string;\n private prefix: string;\n public readonly sourceTtlMs: number;\n public readonly jobMetadataTtlMs: number;\n\n constructor(options: {\n endpoint?: string;\n region?: string;\n bucket: string;\n prefix?: string;\n accessKeyId?: string;\n secretAccessKey?: string;\n sourceTtlMs?: number;\n jobMetadataTtlMs?: number;\n }) {\n this.client = new S3Client({\n endpoint: options.endpoint,\n region: options.region ?? \"auto\",\n credentials: options.accessKeyId\n ? {\n accessKeyId: options.accessKeyId,\n secretAccessKey: options.secretAccessKey!,\n }\n : undefined,\n forcePathStyle: !!options.endpoint,\n });\n this.bucket = options.bucket;\n this.prefix = options.prefix ?? \"\";\n this.sourceTtlMs = options.sourceTtlMs ?? DEFAULT_SOURCE_TTL_MS;\n this.jobMetadataTtlMs =\n options.jobMetadataTtlMs ?? DEFAULT_JOB_METADATA_TTL_MS;\n }\n\n private getExpires(ttlMs: number): Date {\n return new Date(Date.now() + ttlMs);\n }\n\n private getFullKey(key: string): string {\n return `${this.prefix}${key}`;\n }\n\n async put(key: string, stream: Readable, ttlMs?: number): Promise<void> {\n const upload = new Upload({\n client: this.client,\n params: {\n Bucket: this.bucket,\n Key: this.getFullKey(key),\n Body: stream,\n Expires: this.getExpires(ttlMs ?? this.sourceTtlMs),\n },\n });\n await upload.done();\n }\n\n async getStream(key: string): Promise<Readable | null> {\n try {\n const response = await this.client.send(\n new GetObjectCommand({\n Bucket: this.bucket,\n Key: this.getFullKey(key),\n }),\n );\n return response.Body as Readable;\n } catch (err: unknown) {\n const error = err as {\n name?: string;\n $metadata?: { httpStatusCode?: number };\n };\n if (\n error.name === \"NoSuchKey\" ||\n error.$metadata?.httpStatusCode === 404\n ) {\n return null;\n }\n throw err;\n }\n }\n\n async exists(key: string): Promise<boolean> {\n try {\n await this.client.send(\n new HeadObjectCommand({\n Bucket: this.bucket,\n Key: this.getFullKey(key),\n }),\n );\n return true;\n } catch (err: unknown) {\n const error = err as {\n name?: string;\n $metadata?: { httpStatusCode?: number };\n };\n if (\n error.name === \"NotFound\" ||\n error.$metadata?.httpStatusCode === 404\n ) {\n return false;\n }\n throw err;\n }\n }\n\n async existsMany(keys: string[]): Promise<Map<string, boolean>> {\n const results = await Promise.all(\n keys.map(async (key) => [key, await this.exists(key)] as const),\n );\n return new Map(results);\n }\n\n async delete(key: string): Promise<void> {\n try {\n await this.client.send(\n new DeleteObjectCommand({\n Bucket: this.bucket,\n Key: this.getFullKey(key),\n }),\n );\n } catch (err: unknown) {\n const error = err as {\n name?: string;\n $metadata?: { httpStatusCode?: number };\n };\n if (\n error.name === \"NoSuchKey\" ||\n error.$metadata?.httpStatusCode === 404\n ) {\n return;\n }\n throw err;\n }\n }\n\n async putJson(key: string, data: object, ttlMs?: number): Promise<void> {\n await this.client.send(\n new PutObjectCommand({\n Bucket: this.bucket,\n Key: this.getFullKey(key),\n Body: JSON.stringify(data),\n ContentType: \"application/json\",\n Expires: this.getExpires(ttlMs ?? this.jobMetadataTtlMs),\n }),\n );\n }\n\n async getJson<T>(key: string): Promise<T | null> {\n try {\n const response = await this.client.send(\n new GetObjectCommand({\n Bucket: this.bucket,\n Key: this.getFullKey(key),\n }),\n );\n const body = await response.Body?.transformToString();\n if (!body) return null;\n return JSON.parse(body) as T;\n } catch (err: unknown) {\n const error = err as {\n name?: string;\n $metadata?: { httpStatusCode?: number };\n };\n if (\n error.name === \"NoSuchKey\" ||\n error.$metadata?.httpStatusCode === 404\n ) {\n return null;\n }\n throw err;\n }\n }\n\n close(): void {\n // nothing to do here\n }\n}\n\n/**\n * Local filesystem transient store implementation\n */\nexport class LocalTransientStore implements TransientStore {\n private baseDir: string;\n private initialized = false;\n private cleanupInterval?: ReturnType<typeof setInterval>;\n public readonly sourceTtlMs: number;\n public readonly jobMetadataTtlMs: number;\n /** For cleanup, use the longer of the two TTLs */\n private maxTtlMs: number;\n\n constructor(options?: {\n baseDir?: string;\n sourceTtlMs?: number;\n jobMetadataTtlMs?: number;\n }) {\n this.baseDir = options?.baseDir ?? path.join(os.tmpdir(), \"ffs-transient\");\n this.sourceTtlMs = options?.sourceTtlMs ?? DEFAULT_SOURCE_TTL_MS;\n this.jobMetadataTtlMs =\n options?.jobMetadataTtlMs ?? DEFAULT_JOB_METADATA_TTL_MS;\n this.maxTtlMs = Math.max(this.sourceTtlMs, this.jobMetadataTtlMs);\n\n // Cleanup expired files every 5 minutes\n this.cleanupInterval = setInterval(() => {\n this.cleanupExpired().catch(console.error);\n }, 300_000);\n }\n\n /**\n * Remove files older than max TTL\n */\n public async cleanupExpired(): Promise<void> {\n if (!this.initialized) return;\n\n const now = Date.now();\n await this.cleanupDir(this.baseDir, now);\n }\n\n private async cleanupDir(dir: string, now: number): Promise<void> {\n let entries;\n try {\n entries = await fs.readdir(dir, { withFileTypes: true });\n } catch {\n return; // Directory doesn't exist or can't be read\n }\n\n for (const entry of entries) {\n const fullPath = path.join(dir, entry.name);\n\n if (entry.isDirectory()) {\n await this.cleanupDir(fullPath, now);\n // Remove empty directories\n try {\n await fs.rmdir(fullPath);\n } catch {\n // Directory not empty or other error, ignore\n }\n } else if (entry.isFile()) {\n try {\n const stat = await fs.stat(fullPath);\n if (now - stat.mtimeMs > this.maxTtlMs) {\n await fs.rm(fullPath, { force: true });\n }\n } catch {\n // File may have been deleted, ignore\n }\n }\n }\n }\n\n private async ensureDir(filePath: string): Promise<void> {\n await fs.mkdir(path.dirname(filePath), { recursive: true });\n this.initialized = true;\n }\n\n private filePath(key: string): string {\n return path.join(this.baseDir, key);\n }\n\n private tmpPathFor(finalPath: string): string {\n const rand = crypto.randomBytes(8).toString(\"hex\");\n // Keep tmp file in the same directory so rename stays atomic on POSIX filesystems.\n return `${finalPath}.tmp-${process.pid}-${rand}`;\n }\n\n async put(key: string, stream: Readable, _ttlMs?: number): Promise<void> {\n // Note: TTL is not used for local storage; cleanup uses file mtime\n const fp = this.filePath(key);\n await this.ensureDir(fp);\n\n // Write to temp file, then rename for atomicity (no partial reads).\n const tmpPath = this.tmpPathFor(fp);\n try {\n const writeStream = createWriteStream(tmpPath);\n await pipeline(stream, writeStream);\n await fs.rename(tmpPath, fp);\n } catch (err) {\n await fs.rm(tmpPath, { force: true }).catch(() => {});\n throw err;\n }\n }\n\n async getStream(key: string): Promise<Readable | null> {\n const fp = this.filePath(key);\n if (!existsSync(fp)) return null;\n return createReadStream(fp);\n }\n\n async exists(key: string): Promise<boolean> {\n try {\n await fs.access(this.filePath(key));\n return true;\n } catch {\n return false;\n }\n }\n\n async existsMany(keys: string[]): Promise<Map<string, boolean>> {\n const results = await Promise.all(\n keys.map(async (key) => [key, await this.exists(key)] as const),\n );\n return new Map(results);\n }\n\n async delete(key: string): Promise<void> {\n await fs.rm(this.filePath(key), { force: true });\n }\n\n async putJson(key: string, data: object, _ttlMs?: number): Promise<void> {\n // Note: TTL is not used for local storage; cleanup uses file mtime\n const fp = this.filePath(key);\n await this.ensureDir(fp);\n\n // Write to temp file, then rename for atomicity (no partial reads).\n const tmpPath = this.tmpPathFor(fp);\n try {\n await fs.writeFile(tmpPath, JSON.stringify(data));\n await fs.rename(tmpPath, fp);\n } catch (err) {\n await fs.rm(tmpPath, { force: true }).catch(() => {});\n throw err;\n }\n }\n\n async getJson<T>(key: string): Promise<T | null> {\n try {\n const content = await fs.readFile(this.filePath(key), \"utf-8\");\n return JSON.parse(content) as T;\n } catch {\n return null;\n }\n }\n\n close(): void {\n // Stop the cleanup interval\n if (this.cleanupInterval) {\n clearInterval(this.cleanupInterval);\n this.cleanupInterval = undefined;\n }\n }\n}\n\n/**\n * Create a transient store instance based on environment variables.\n * Uses S3 if FFS_TRANSIENT_STORE_BUCKET is set, otherwise uses local filesystem.\n */\nexport function createTransientStore(): TransientStore {\n // Parse TTLs from env\n const sourceTtlMs = process.env.FFS_SOURCE_CACHE_TTL_MS\n ? parseInt(process.env.FFS_SOURCE_CACHE_TTL_MS, 10)\n : DEFAULT_SOURCE_TTL_MS;\n const jobMetadataTtlMs = process.env.FFS_JOB_METADATA_TTL_MS\n ? parseInt(process.env.FFS_JOB_METADATA_TTL_MS, 10)\n : DEFAULT_JOB_METADATA_TTL_MS;\n\n if (process.env.FFS_TRANSIENT_STORE_BUCKET) {\n return new S3TransientStore({\n endpoint: process.env.FFS_TRANSIENT_STORE_ENDPOINT,\n region: process.env.FFS_TRANSIENT_STORE_REGION ?? \"auto\",\n bucket: process.env.FFS_TRANSIENT_STORE_BUCKET,\n prefix: process.env.FFS_TRANSIENT_STORE_PREFIX,\n accessKeyId: process.env.FFS_TRANSIENT_STORE_ACCESS_KEY,\n secretAccessKey: process.env.FFS_TRANSIENT_STORE_SECRET_KEY,\n sourceTtlMs,\n jobMetadataTtlMs,\n });\n }\n\n return new LocalTransientStore({\n baseDir: process.env.FFS_TRANSIENT_STORE_LOCAL_DIR,\n sourceTtlMs,\n jobMetadataTtlMs,\n });\n}\n\nexport function hashUrl(url: string): string {\n return crypto.createHash(\"sha256\").update(url).digest(\"hex\").slice(0, 16);\n}\n\nexport type SourceStoreKey = `sources/${string}`;\nexport type WarmupJobStoreKey = `jobs/warmup/${string}.json`;\nexport type RenderJobStoreKey = `jobs/render/${string}.json`;\nexport type WarmupAndRenderJobStoreKey =\n `jobs/warmup-and-render/${string}.json`;\n\n/**\n * Build the store key for a source URL (hashing is handled internally).\n */\nexport function sourceStoreKey(url: string): SourceStoreKey {\n return `sources/${hashUrl(url)}`;\n}\n\nexport function warmupJobStoreKey(jobId: string): WarmupJobStoreKey {\n return `jobs/warmup/${jobId}.json`;\n}\n\nexport function renderJobStoreKey(jobId: string): RenderJobStoreKey {\n return `jobs/render/${jobId}.json`;\n}\n\nexport function warmupAndRenderJobStoreKey(\n jobId: string,\n): WarmupAndRenderJobStoreKey {\n return `jobs/warmup-and-render/${jobId}.json`;\n}\n\n/**\n * Centralized store key builders for known namespaces.\n * Prefer using these helpers over manual string interpolation.\n */\nexport const storeKeys = {\n source: sourceStoreKey,\n warmupJob: warmupJobStoreKey,\n renderJob: renderJobStoreKey,\n warmupAndRenderJob: warmupAndRenderJobStoreKey,\n} as const;\n"],"mappings":";AAAA,SAAS,OAAO,aAA2C;AAwC3D,eAAsB,SACpB,KACA,SACmB;AACnB,QAAM;AAAA,IAC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