@boredland/node-ts-cache 1.0.0 → 1.0.1
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/README.md +2 -2
- package/dist/index.d.mts +109 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +5 -2
- package/.github/workflows/ci.yml +0 -27
- package/biome.json +0 -50
- package/renovate.json +0 -37
- package/src/cacheContainer.ts +0 -90
- package/src/debug.ts +0 -3
- package/src/hash.ts +0 -15
- package/src/index.ts +0 -4
- package/src/lruStorage.test.ts +0 -613
- package/src/lruStorage.ts +0 -32
- package/src/storage.ts +0 -27
- package/src/withCache.ts +0 -110
- package/tsconfig.json +0 -48
- package/tsdown.config.ts +0 -9
package/src/withCache.ts
DELETED
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
import PQueue from "p-queue";
|
|
2
|
-
import type { CacheContainer, CachingOptions } from "./cacheContainer.ts";
|
|
3
|
-
import hash from "./hash.ts";
|
|
4
|
-
|
|
5
|
-
const revalidationQueues: Record<string, PQueue> = {};
|
|
6
|
-
|
|
7
|
-
type WithCacheOptions<Parameters, Result> = Partial<
|
|
8
|
-
Omit<CachingOptions, "calculateKey" | "isLazy">
|
|
9
|
-
> & {
|
|
10
|
-
/** an optional prefix to prepend to the key */
|
|
11
|
-
prefix?: string;
|
|
12
|
-
/** an optional function to calculate a key based on the parameters of the wrapped function */
|
|
13
|
-
calculateKey?: (input: Parameters) => string;
|
|
14
|
-
/** an optional function that is called just before the result is stored to the storage */
|
|
15
|
-
shouldStore?: (result: Awaited<Result>) => boolean;
|
|
16
|
-
/**
|
|
17
|
-
* caching strategy to use
|
|
18
|
-
* - "lazy": cache is populated in the background after returning the result
|
|
19
|
-
* - "swr": stale-while-revalidate, cache is returned if present and updated in the background
|
|
20
|
-
* - "eager": cache is populated before returning the result
|
|
21
|
-
* @default "eager"
|
|
22
|
-
*/
|
|
23
|
-
strategy?: "lazy" | "swr" | "eager";
|
|
24
|
-
/**
|
|
25
|
-
* Concurrency for revalidation queue
|
|
26
|
-
* @default 1
|
|
27
|
-
*/
|
|
28
|
-
revalidationConcurrency?: number;
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* wrapped function factory
|
|
33
|
-
* @param container - cache container to create the fn for
|
|
34
|
-
* @returns wrapping function
|
|
35
|
-
*/
|
|
36
|
-
export const withCacheFactory = (container: CacheContainer) => {
|
|
37
|
-
/**
|
|
38
|
-
* function wrapper
|
|
39
|
-
* @param operation - the function to be wrapped
|
|
40
|
-
* @param options - caching options
|
|
41
|
-
* @returns wrapped operation
|
|
42
|
-
*/
|
|
43
|
-
const withCache = <
|
|
44
|
-
Parameters extends Array<unknown>,
|
|
45
|
-
Result extends Promise<unknown>,
|
|
46
|
-
>(
|
|
47
|
-
operation: (...parameters: Parameters) => Result,
|
|
48
|
-
options: WithCacheOptions<Parameters, Result> = {},
|
|
49
|
-
) => {
|
|
50
|
-
return async (...parameters: Parameters): Promise<Result> => {
|
|
51
|
-
const {
|
|
52
|
-
calculateKey,
|
|
53
|
-
strategy = "eager",
|
|
54
|
-
revalidationConcurrency: concurrency = 1,
|
|
55
|
-
...rest
|
|
56
|
-
} = options;
|
|
57
|
-
const prefix = options.prefix ?? "default";
|
|
58
|
-
const key = `${operation.name}:${prefix}:${
|
|
59
|
-
calculateKey ? calculateKey(parameters) : hash(parameters)
|
|
60
|
-
}` as const;
|
|
61
|
-
|
|
62
|
-
const queueName = `${operation.name}:${prefix}` as const;
|
|
63
|
-
|
|
64
|
-
revalidationQueues[queueName] =
|
|
65
|
-
revalidationQueues[queueName] ??
|
|
66
|
-
new PQueue({
|
|
67
|
-
concurrency,
|
|
68
|
-
});
|
|
69
|
-
revalidationQueues[queueName].concurrency = concurrency;
|
|
70
|
-
|
|
71
|
-
const cachedResponse = await container.getItem<Awaited<Result>>(key);
|
|
72
|
-
|
|
73
|
-
const refreshedItem = async () => {
|
|
74
|
-
const result = await operation(...parameters);
|
|
75
|
-
if (!options.shouldStore || options.shouldStore(result)) {
|
|
76
|
-
await container.setItem(key, result, {
|
|
77
|
-
...rest,
|
|
78
|
-
isLazy: strategy === "lazy" || strategy === "swr",
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
return result;
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Stale-While-Revalidate strategy
|
|
86
|
-
* If the cached response is expired, we return it immediately and
|
|
87
|
-
* revalidate in the background
|
|
88
|
-
*/
|
|
89
|
-
if (strategy === "swr" && cachedResponse?.meta.expired) {
|
|
90
|
-
if (
|
|
91
|
-
!revalidationQueues[queueName].runningTasks.some(
|
|
92
|
-
(t) => t.id === key && t.startTime,
|
|
93
|
-
)
|
|
94
|
-
) {
|
|
95
|
-
revalidationQueues[queueName].add(refreshedItem, {
|
|
96
|
-
id: key,
|
|
97
|
-
});
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
if (cachedResponse) {
|
|
102
|
-
return cachedResponse.content;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
const result = await refreshedItem();
|
|
106
|
-
return result;
|
|
107
|
-
};
|
|
108
|
-
};
|
|
109
|
-
return withCache;
|
|
110
|
-
};
|
package/tsconfig.json
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
// Visit https://aka.ms/tsconfig to read more about this file
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"noEmit": true,
|
|
5
|
-
// File Layout
|
|
6
|
-
// "rootDir": "./src",
|
|
7
|
-
// "outDir": "./dist",
|
|
8
|
-
|
|
9
|
-
// Environment Settings
|
|
10
|
-
// See also https://aka.ms/tsconfig/module
|
|
11
|
-
"module": "ES2022",
|
|
12
|
-
"moduleResolution": "bundler",
|
|
13
|
-
"target": "ES2022",
|
|
14
|
-
"types": [],
|
|
15
|
-
// For nodejs:
|
|
16
|
-
// "lib": ["esnext"],
|
|
17
|
-
// "types": ["node"],
|
|
18
|
-
// and npm install -D @types/node
|
|
19
|
-
|
|
20
|
-
// Other Outputs
|
|
21
|
-
"sourceMap": true,
|
|
22
|
-
"declaration": true,
|
|
23
|
-
"declarationMap": true,
|
|
24
|
-
|
|
25
|
-
// Stricter Typechecking Options
|
|
26
|
-
"noUncheckedIndexedAccess": true,
|
|
27
|
-
"exactOptionalPropertyTypes": true,
|
|
28
|
-
|
|
29
|
-
// Style Options
|
|
30
|
-
"noImplicitReturns": true,
|
|
31
|
-
"noImplicitOverride": true,
|
|
32
|
-
"noUnusedLocals": true,
|
|
33
|
-
"noUnusedParameters": true,
|
|
34
|
-
"noFallthroughCasesInSwitch": true,
|
|
35
|
-
"noPropertyAccessFromIndexSignature": true,
|
|
36
|
-
|
|
37
|
-
// Recommended Options
|
|
38
|
-
"strict": true,
|
|
39
|
-
"jsx": "react-jsx",
|
|
40
|
-
"verbatimModuleSyntax": true,
|
|
41
|
-
"isolatedModules": true,
|
|
42
|
-
"noUncheckedSideEffectImports": true,
|
|
43
|
-
"moduleDetection": "force",
|
|
44
|
-
"skipLibCheck": true,
|
|
45
|
-
|
|
46
|
-
"allowImportingTsExtensions": true
|
|
47
|
-
}
|
|
48
|
-
}
|