qarpeo 0.1.34 → 0.1.35
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/dist/chunks/wrangler.mjs
CHANGED
|
@@ -9,7 +9,7 @@ import { fileURLToPath } from 'node:url';
|
|
|
9
9
|
import { mkdir, writeFile, readFile } from 'node:fs/promises';
|
|
10
10
|
import { defu } from 'defu';
|
|
11
11
|
|
|
12
|
-
const version = "0.1.
|
|
12
|
+
const version = "0.1.35";
|
|
13
13
|
|
|
14
14
|
function scanSchemaExports(schemaFiles) {
|
|
15
15
|
const types = [];
|
package/dist/module.mjs
CHANGED
|
@@ -2914,6 +2914,10 @@ export default async function (batch: any, env?: any, ctx?: any) {
|
|
|
2914
2914
|
name: "purgeCacheTag",
|
|
2915
2915
|
from: resolver.resolve("./runtime/server/utils/cache")
|
|
2916
2916
|
},
|
|
2917
|
+
{
|
|
2918
|
+
name: "defineCachedFunction",
|
|
2919
|
+
from: resolver.resolve("./runtime/server/utils/cache")
|
|
2920
|
+
},
|
|
2917
2921
|
{
|
|
2918
2922
|
name: "rememberCache",
|
|
2919
2923
|
from: resolver.resolve("./runtime/server/utils/cache")
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type { RememberCacheOptions } from "../../../types";
|
|
1
|
+
import type { CachedFunctionOptions, RememberCacheOptions } from "../../../types";
|
|
2
|
+
export type { CachedFunctionOptions, RememberCacheOptions };
|
|
2
3
|
/**
|
|
3
4
|
* Access Nitro's persistent Edge Cache storage. Connected to Cloudflare KV in production and local
|
|
4
5
|
* filesystem (.data/cache) in dev.
|
|
@@ -30,14 +31,34 @@ export declare function purgeCache(pattern?: string): Promise<string[]>;
|
|
|
30
31
|
*/
|
|
31
32
|
export declare function purgeCacheTag(...tags: string[]): Promise<string[]>;
|
|
32
33
|
/**
|
|
33
|
-
*
|
|
34
|
-
*
|
|
34
|
+
* Factory pattern: define a reusable cached function backed by Cloudflare KV / edge cache.
|
|
35
|
+
* Matches Nitro's `defineCachedFunction` convention with enhanced support for human duration
|
|
36
|
+
* strings ('5m', '1h', '7d'), SWR windows, and semantic tags.
|
|
35
37
|
*
|
|
36
|
-
* @
|
|
37
|
-
*
|
|
38
|
-
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```ts
|
|
40
|
+
* export const getStats = defineCachedFunction(fetchStats, {
|
|
41
|
+
* key: 'dashboard:stats',
|
|
42
|
+
* maxAge: 60 * 5,
|
|
43
|
+
* })
|
|
44
|
+
* const stats = await getStats()
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export declare function defineCachedFunction<T, Args extends any[] = any[]>(fn: (...args: Args) => Promise<T> | T, options?: CachedFunctionOptions<Args>): (...args: Args) => Promise<T>;
|
|
48
|
+
/**
|
|
49
|
+
* Inline execution: fetch and cache data immediately, or retrieve fresh from cache.
|
|
50
|
+
* Supports inline key/options/fetcher calls or fetcher-first options.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```ts
|
|
54
|
+
* // Direct inline execution:
|
|
55
|
+
* const stats = await rememberCache('stats', { maxAge: '5m' }, fetchStats)
|
|
56
|
+
* const user = await rememberCache('user:123', '1h', () => fetchUser(123))
|
|
57
|
+
* ```
|
|
39
58
|
*/
|
|
40
|
-
export declare function rememberCache<T>(key: string, ttlOrOptions: number | string | RememberCacheOptions, fetcher: () => Promise<T>): Promise<T>;
|
|
59
|
+
export declare function rememberCache<T>(key: string, ttlOrOptions: number | string | RememberCacheOptions, fetcher: () => Promise<T> | T): Promise<T>;
|
|
60
|
+
export declare function rememberCache<T>(key: string, fetcher: () => Promise<T> | T, ttlOrOptions?: number | string | RememberCacheOptions): Promise<T>;
|
|
61
|
+
export declare function rememberCache<T, Args extends any[] = any[]>(fn: (...args: Args) => Promise<T> | T, options?: RememberCacheOptions<Args>): ((...args: Args) => Promise<T>) & Promise<T>;
|
|
41
62
|
/** Retrieves a typed value from the edge cache if not expired. */
|
|
42
63
|
export declare function getCacheItem<T = unknown>(key: string): Promise<T | null>;
|
|
43
64
|
/** Stores a typed value in the edge cache with an expiration TTL, optional SWR window, and tags. */
|
|
@@ -71,55 +71,136 @@ export async function purgeCacheTag(...tags) {
|
|
|
71
71
|
}
|
|
72
72
|
return Array.from(purgedKeys);
|
|
73
73
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
if (
|
|
77
|
-
|
|
74
|
+
function serializeCacheArg(arg) {
|
|
75
|
+
if (arg === null || arg === void 0) return "";
|
|
76
|
+
if (typeof arg === "object") {
|
|
77
|
+
try {
|
|
78
|
+
return JSON.stringify(arg);
|
|
79
|
+
} catch {
|
|
80
|
+
return String(arg);
|
|
81
|
+
}
|
|
78
82
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
if (opts
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
83
|
+
return String(arg);
|
|
84
|
+
}
|
|
85
|
+
function resolveFunctionCacheKey(fn, opts, args = []) {
|
|
86
|
+
if (opts?.getKey) {
|
|
87
|
+
const custom = opts.getKey(...args);
|
|
88
|
+
const prefix = opts.name || (typeof opts.key === "string" ? opts.key : "") || fn.name || "cache";
|
|
89
|
+
return `${prefix}:${custom}`.replace(/^:+|:+$/g, "");
|
|
86
90
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
if (
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
91
|
+
if (typeof opts?.key === "function") {
|
|
92
|
+
return opts.key(...args);
|
|
93
|
+
}
|
|
94
|
+
if (typeof opts?.key === "string") {
|
|
95
|
+
if (args.length === 0) return opts.key;
|
|
96
|
+
return `${opts.key}:${args.map(serializeCacheArg).join(":")}`;
|
|
97
|
+
}
|
|
98
|
+
if (opts?.name) {
|
|
99
|
+
if (args.length === 0) return opts.name;
|
|
100
|
+
return `${opts.name}:${args.map(serializeCacheArg).join(":")}`;
|
|
101
|
+
}
|
|
102
|
+
const base = fn.name || "cache";
|
|
103
|
+
if (args.length === 0) return base;
|
|
104
|
+
return `${base}:${args.map(serializeCacheArg).join(":")}`;
|
|
105
|
+
}
|
|
106
|
+
function resolveDurationSec(opts) {
|
|
107
|
+
const duration = opts?.maxAge ?? opts?.ttl;
|
|
108
|
+
return duration ? parseDurationSeconds(duration) : 3600;
|
|
109
|
+
}
|
|
110
|
+
function resolveSwrSec(opts, ttlSec = 3600) {
|
|
111
|
+
if (opts?.swr === true) {
|
|
112
|
+
return ttlSec * 2;
|
|
113
|
+
}
|
|
114
|
+
if (opts?.swr) {
|
|
115
|
+
return parseDurationSeconds(opts.swr);
|
|
116
|
+
}
|
|
117
|
+
return 0;
|
|
118
|
+
}
|
|
119
|
+
function resolveCacheTags(opts, args = []) {
|
|
120
|
+
if (typeof opts?.tags === "function") {
|
|
121
|
+
return opts.tags(...args);
|
|
122
|
+
}
|
|
123
|
+
if (Array.isArray(opts?.tags) && opts.tags.length > 0) {
|
|
124
|
+
return opts.tags;
|
|
125
|
+
}
|
|
126
|
+
return void 0;
|
|
127
|
+
}
|
|
128
|
+
function createCachedFunction(fn, opts) {
|
|
129
|
+
const ttlSec = resolveDurationSec(opts);
|
|
130
|
+
const swrSec = resolveSwrSec(opts, ttlSec);
|
|
131
|
+
return async (...args) => {
|
|
132
|
+
const storage = useCacheStorage();
|
|
133
|
+
if (!storage) {
|
|
134
|
+
return await fn(...args);
|
|
135
|
+
}
|
|
136
|
+
const key = resolveFunctionCacheKey(fn, opts, args);
|
|
137
|
+
const cleanKey = key.replace(/^\/+/, "");
|
|
138
|
+
const tags = resolveCacheTags(opts, args);
|
|
139
|
+
try {
|
|
140
|
+
const raw = await storage.getItem(cleanKey);
|
|
141
|
+
if (raw !== null && typeof raw === "object" && "value" in raw && "expiresAt" in raw) {
|
|
142
|
+
const entry = raw;
|
|
143
|
+
const now = Date.now();
|
|
144
|
+
if (now < entry.expiresAt) {
|
|
145
|
+
return entry.value;
|
|
146
|
+
}
|
|
147
|
+
if (entry.swrUntil && now < entry.swrUntil) {
|
|
148
|
+
Promise.resolve(fn(...args)).then(async (freshValue) => {
|
|
149
|
+
await setCacheItem(cleanKey, freshValue, {
|
|
150
|
+
ttl: ttlSec,
|
|
151
|
+
swr: swrSec,
|
|
152
|
+
tags
|
|
153
|
+
});
|
|
154
|
+
}).catch(() => {
|
|
103
155
|
});
|
|
104
|
-
|
|
105
|
-
}
|
|
106
|
-
|
|
156
|
+
return entry.value;
|
|
157
|
+
}
|
|
158
|
+
} else if (raw !== null && raw !== void 0) {
|
|
159
|
+
return raw;
|
|
107
160
|
}
|
|
108
|
-
}
|
|
109
|
-
return raw;
|
|
161
|
+
} catch {
|
|
110
162
|
}
|
|
111
|
-
|
|
163
|
+
const value = await fn(...args);
|
|
164
|
+
try {
|
|
165
|
+
await setCacheItem(cleanKey, value, {
|
|
166
|
+
ttl: ttlSec,
|
|
167
|
+
swr: swrSec,
|
|
168
|
+
tags
|
|
169
|
+
});
|
|
170
|
+
} catch {
|
|
171
|
+
}
|
|
172
|
+
return value;
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
export function defineCachedFunction(fn, options) {
|
|
176
|
+
return createCachedFunction(fn, options);
|
|
177
|
+
}
|
|
178
|
+
export function rememberCache(arg1, arg2, arg3) {
|
|
179
|
+
if (typeof arg1 === "string") {
|
|
180
|
+
let fetcher;
|
|
181
|
+
let opts = {};
|
|
182
|
+
if (typeof arg2 === "function") {
|
|
183
|
+
fetcher = arg2;
|
|
184
|
+
opts = typeof arg3 === "object" ? arg3 : arg3 !== void 0 ? { maxAge: arg3 } : {};
|
|
185
|
+
} else if (typeof arg3 === "function") {
|
|
186
|
+
fetcher = arg3;
|
|
187
|
+
opts = typeof arg2 === "object" ? arg2 : arg2 !== void 0 ? { maxAge: arg2 } : {};
|
|
188
|
+
}
|
|
189
|
+
if (!fetcher) {
|
|
190
|
+
throw new TypeError("rememberCache requires a fetcher function");
|
|
191
|
+
}
|
|
192
|
+
const cachedFn = createCachedFunction(fetcher, { key: arg1, ...opts });
|
|
193
|
+
return cachedFn();
|
|
112
194
|
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
} catch {
|
|
195
|
+
if (typeof arg1 === "function") {
|
|
196
|
+
const opts = typeof arg2 === "object" ? arg2 : arg2 !== void 0 ? { maxAge: arg2 } : {};
|
|
197
|
+
const cachedFn = createCachedFunction(arg1, opts);
|
|
198
|
+
const callable = (...args) => cachedFn(...args);
|
|
199
|
+
callable.then = (onResolve, onReject) => cachedFn().then(onResolve, onReject);
|
|
200
|
+
callable.catch = (onReject) => cachedFn().catch(onReject);
|
|
201
|
+
return callable;
|
|
121
202
|
}
|
|
122
|
-
|
|
203
|
+
throw new TypeError("Invalid arguments passed to rememberCache");
|
|
123
204
|
}
|
|
124
205
|
export async function getCacheItem(key) {
|
|
125
206
|
const storage = useCacheStorage();
|
|
@@ -145,26 +226,22 @@ export async function setCacheItem(key, value, ttlOrOptions) {
|
|
|
145
226
|
const storage = useCacheStorage();
|
|
146
227
|
if (!storage) return;
|
|
147
228
|
const cleanKey = key.replace(/^\/+/, "");
|
|
148
|
-
const opts = typeof ttlOrOptions === "object" ? ttlOrOptions : {
|
|
149
|
-
const durationSec =
|
|
150
|
-
|
|
151
|
-
if (opts.swr === true) {
|
|
152
|
-
swrSec = durationSec * 2;
|
|
153
|
-
} else if (opts.swr) {
|
|
154
|
-
swrSec = parseDurationSeconds(opts.swr);
|
|
155
|
-
}
|
|
229
|
+
const opts = typeof ttlOrOptions === "object" ? ttlOrOptions : { maxAge: ttlOrOptions };
|
|
230
|
+
const durationSec = resolveDurationSec(opts);
|
|
231
|
+
const swrSec = resolveSwrSec(opts, durationSec);
|
|
156
232
|
const now = Date.now();
|
|
157
233
|
const expiresAt = now + durationSec * 1e3;
|
|
158
234
|
const swrUntil = swrSec > 0 ? expiresAt + swrSec * 1e3 : void 0;
|
|
235
|
+
const tags = resolveCacheTags(opts);
|
|
159
236
|
const entry = {
|
|
160
237
|
value,
|
|
161
238
|
expiresAt,
|
|
162
239
|
...swrUntil ? { swrUntil } : {},
|
|
163
|
-
...
|
|
240
|
+
...tags ? { tags } : {}
|
|
164
241
|
};
|
|
165
242
|
await storage.setItem(cleanKey, entry);
|
|
166
|
-
if (
|
|
167
|
-
for (const tag of
|
|
243
|
+
if (tags && tags.length > 0) {
|
|
244
|
+
for (const tag of tags) {
|
|
168
245
|
const tagIndexKey = `${TAG_INDEX_PREFIX}${tag}`;
|
|
169
246
|
try {
|
|
170
247
|
const existing = await storage.getItem(tagIndexKey);
|
package/dist/types.d.ts
CHANGED
|
@@ -1288,14 +1288,34 @@ export interface MailOptions {
|
|
|
1288
1288
|
*/
|
|
1289
1289
|
unsubscribe?: boolean | MailUnsubscribeOptions;
|
|
1290
1290
|
}
|
|
1291
|
-
export interface
|
|
1292
|
-
/**
|
|
1291
|
+
export interface CachedFunctionOptions<Args extends any[] = any[]> {
|
|
1292
|
+
/**
|
|
1293
|
+
* Unique cache key string or key generator function.
|
|
1294
|
+
* If omitted, defaults to `name` or the function name.
|
|
1295
|
+
*/
|
|
1296
|
+
key?: string | ((...args: Args) => string);
|
|
1297
|
+
/**
|
|
1298
|
+
* Key prefix or function name. Matches Nitro defineCachedFunction convention.
|
|
1299
|
+
*/
|
|
1300
|
+
name?: string;
|
|
1301
|
+
/**
|
|
1302
|
+
* Custom key generator function based on arguments. Matches Nitro defineCachedFunction convention.
|
|
1303
|
+
*/
|
|
1304
|
+
getKey?: (...args: Args) => string;
|
|
1305
|
+
/**
|
|
1306
|
+
* Maximum age / TTL duration (e.g. 300, '5m', '1h', '7d'). Matches Nitro defineCachedFunction convention.
|
|
1307
|
+
*
|
|
1308
|
+
* @default 3600
|
|
1309
|
+
*/
|
|
1310
|
+
maxAge?: string | number;
|
|
1311
|
+
/** Time-To-Live duration (alias for `maxAge`) */
|
|
1293
1312
|
ttl?: string | number;
|
|
1294
1313
|
/** Stale-While-Revalidate duration allowing serving stale data during background fetch */
|
|
1295
1314
|
swr?: string | number | boolean;
|
|
1296
1315
|
/** Semantic cache tags for group invalidation via purgeCacheTag() */
|
|
1297
|
-
tags?: string[];
|
|
1316
|
+
tags?: string[] | ((...args: Args) => string[]);
|
|
1298
1317
|
}
|
|
1318
|
+
export type RememberCacheOptions<Args extends any[] = any[]> = CachedFunctionOptions<Args>;
|
|
1299
1319
|
export interface CacheOptions {
|
|
1300
1320
|
/**
|
|
1301
1321
|
* Cloudflare KV binding name override for cache storage.
|