vargai 0.4.0-alpha46 → 0.4.0-alpha48

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/package.json CHANGED
@@ -69,7 +69,7 @@
69
69
  "sharp": "^0.34.5",
70
70
  "zod": "^4.2.1"
71
71
  },
72
- "version": "0.4.0-alpha46",
72
+ "version": "0.4.0-alpha48",
73
73
  "exports": {
74
74
  ".": "./src/index.ts",
75
75
  "./ai": "./src/ai-sdk/index.ts",
@@ -12,6 +12,7 @@ import {
12
12
  } from "@ai-sdk/provider";
13
13
  import { fal } from "@fal-ai/client";
14
14
  import pMap from "p-map";
15
+ import type { CacheStorage } from "../cache";
15
16
  import { fileCache } from "../file-cache";
16
17
  import type { VideoModelV3, VideoModelV3CallOptions } from "../video-model";
17
18
 
@@ -21,7 +22,72 @@ interface PendingRequest {
21
22
  submitted_at: number;
22
23
  }
23
24
 
24
- const pendingStorage = fileCache({ dir: ".cache/fal-pending" });
25
+ const memoryStorage = new Map<string, { value: unknown; expires: number }>();
26
+
27
+ function createMemoryCache(): CacheStorage {
28
+ return {
29
+ async get(key: string) {
30
+ const entry = memoryStorage.get(key);
31
+ if (!entry) return undefined;
32
+ if (entry.expires && Date.now() > entry.expires) {
33
+ memoryStorage.delete(key);
34
+ return undefined;
35
+ }
36
+ return entry.value;
37
+ },
38
+ async set(key: string, value: unknown, ttl?: number) {
39
+ memoryStorage.set(key, { value, expires: ttl ? Date.now() + ttl : 0 });
40
+ },
41
+ async delete(key: string) {
42
+ memoryStorage.delete(key);
43
+ },
44
+ };
45
+ }
46
+
47
+ // TODO: allow passing CacheStorage via providerOptions.fal.cacheStorage for proper serverless support
48
+ function createFalCache(name: string): CacheStorage {
49
+ let cache: CacheStorage | null = null;
50
+ let useFallback = false;
51
+ const fallback = createMemoryCache();
52
+
53
+ const getCache = () => {
54
+ if (useFallback) return fallback;
55
+ if (!cache) cache = fileCache({ dir: `.cache/${name}` });
56
+ return cache;
57
+ };
58
+
59
+ return {
60
+ async get(key) {
61
+ if (useFallback) return fallback.get(key);
62
+ try {
63
+ return await getCache().get(key);
64
+ } catch {
65
+ useFallback = true;
66
+ return fallback.get(key);
67
+ }
68
+ },
69
+ async set(key, value, ttl) {
70
+ if (useFallback) return fallback.set(key, value, ttl);
71
+ try {
72
+ await getCache().set(key, value, ttl);
73
+ } catch {
74
+ useFallback = true;
75
+ await fallback.set(key, value, ttl);
76
+ }
77
+ },
78
+ async delete(key) {
79
+ if (useFallback) return fallback.delete(key);
80
+ try {
81
+ await getCache().delete(key);
82
+ } catch {
83
+ useFallback = true;
84
+ await fallback.delete(key);
85
+ }
86
+ },
87
+ };
88
+ }
89
+
90
+ const pendingStorage = createFalCache("fal-pending");
25
91
 
26
92
  const DEFAULT_TIMEOUT_MS = 10 * 60 * 1000;
27
93
  const FAL_TIMEOUT_MS = (() => {
@@ -183,7 +249,7 @@ function detectImageType(bytes: Uint8Array): string | undefined {
183
249
  return undefined;
184
250
  }
185
251
 
186
- const uploadCache = fileCache({ dir: ".cache/fal-uploads" });
252
+ const uploadCache = createFalCache("fal-uploads");
187
253
 
188
254
  async function fileToUrl(file: ImageModelV3File): Promise<string> {
189
255
  if (file.type === "url") return file.url;