Skip to content

Commit 5686bdc

Browse files
committed
F
1 parent 3225905 commit 5686bdc

File tree

6 files changed

+49
-23
lines changed

6 files changed

+49
-23
lines changed

packages/nextjs-cache-handler/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/nextjs-cache-handler/package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"next",
1919
"redis"
2020
],
21-
"version": "2.1.0-canary6",
21+
"version": "2.1.0-canary7",
2222
"type": "module",
2323
"license": "MIT",
2424
"description": "Next.js cache handlers",
@@ -42,6 +42,10 @@
4242
"./helpers/withAbortSignal": {
4343
"require": "./dist/helpers/withAbortSignal.cjs",
4444
"import": "./dist/helpers/withAbortSignal.js"
45+
},
46+
"./helpers/withAbortSignalProxy": {
47+
"require": "./dist/helpers/withAbortSignalProxy.cjs",
48+
"import": "./dist/helpers/withAbortSignalProxy.js"
4549
}
4650
},
4751
"typesVersions": {
@@ -66,6 +70,9 @@
6670
],
6771
"helpers/withAbortSignal": [
6872
"dist/helpers/withAbortSignal.d.ts"
73+
],
74+
"helpers/withAbortSignalProxy": [
75+
"dist/helpers/withAbortSignalProxy.d.ts"
6976
]
7077
}
7178
},

packages/nextjs-cache-handler/src/handlers/redis-strings.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import {
66
convertStringsToBuffers,
77
parseBuffersToStrings,
88
} from "../helpers/buffer";
9-
import { RedisClientType } from "@redis/client";
9+
import type { RedisClientType } from "@redis/client";
1010
import { RedisClusterCacheProxy } from "../helpers/redisClusterProxy";
11+
import { withAbortSignalProxy } from "../helpers/withAbortSignalProxy";
1112

1213
/**
1314
* Creates a Handler for handling cache operations using Redis strings.
@@ -25,7 +26,7 @@ import { RedisClusterCacheProxy } from "../helpers/redisClusterProxy";
2526
* - The `revalidateTag` and `delete` methods handle cache revalidation and deletion.
2627
*/
2728
export default function createHandler({
28-
client,
29+
client: innerClient,
2930
keyPrefix = "",
3031
sharedTagsKey = "__sharedTags__",
3132
sharedTagsTtlKey = "__sharedTagsTtl__",
@@ -35,6 +36,8 @@ export default function createHandler({
3536
}: CreateRedisStringsHandlerOptions<
3637
RedisClientType | RedisClusterCacheProxy
3738
>): Handler {
39+
const client = withAbortSignalProxy(innerClient);
40+
3841
function assertClientIsReady(): void {
3942
if (!client.isReady) {
4043
throw new Error(

packages/nextjs-cache-handler/src/helpers/redisClusterProxy.ts

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,19 @@
11
import type { RedisClusterType } from "@redis/client";
2-
import { withAbortSignal } from "./withAbortSignal";
32

43
export type RedisClusterCacheProxy = RedisClusterType & {
5-
withAbortSignal(signal: AbortSignal): RedisClusterType;
64
isReady: boolean;
75
};
86

97
export function withProxy<T extends RedisClusterType>(
108
cluster: RedisClusterType,
11-
defaultSignal?: AbortSignal,
129
): RedisClusterCacheProxy {
13-
let signal: AbortSignal | undefined = defaultSignal;
14-
1510
const handler: ProxyHandler<T> = {
1611
get(target, prop, receiver) {
17-
if (prop === "withAbortSignal") {
18-
return (s: AbortSignal) => {
19-
signal = s;
20-
return new Proxy(cluster, handler);
21-
};
22-
}
23-
2412
if (prop === "isReady") {
2513
return cluster.replicas.every((s) => s.client?.isReady);
2614
}
2715

28-
const orig = Reflect.get(target, prop, receiver);
29-
30-
if (typeof orig !== "function") return orig;
31-
32-
return (...args: unknown[]) =>
33-
withAbortSignal(() => (orig as Function).apply(target, args), signal);
16+
return Reflect.get(target, prop, receiver);
3417
},
3518
};
3619

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { withAbortSignal } from "./withAbortSignal";
2+
3+
export type WithAbortSignalProxy<T> = T & {
4+
withAbortSignal(signal: AbortSignal): T;
5+
};
6+
7+
export function withAbortSignalProxy<T extends object>(
8+
obj: T,
9+
defaultSignal?: AbortSignal,
10+
): WithAbortSignalProxy<T> {
11+
let signal: AbortSignal | undefined = defaultSignal;
12+
13+
const handler: ProxyHandler<T> = {
14+
get(target, prop, receiver) {
15+
if (prop === "withAbortSignal") {
16+
return (s: AbortSignal) => {
17+
signal = s;
18+
return new Proxy(obj, handler);
19+
};
20+
}
21+
22+
const orig = Reflect.get(target, prop, receiver);
23+
24+
if (typeof orig !== "function") return orig;
25+
26+
return (...args: unknown[]) =>
27+
withAbortSignal(() => (orig as Function).apply(target, args), signal);
28+
},
29+
};
30+
31+
return new Proxy(obj, handler) as WithAbortSignalProxy<T>;
32+
}

packages/nextjs-cache-handler/tsup.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const tsup = defineConfig({
77
"src/instrumentation/*.ts",
88
"src/helpers/redisClusterProxy.ts",
99
"src/helpers/withAbortSignal.ts",
10+
"src/helpers/withAbortSignalProxy.ts",
1011
],
1112
splitting: false,
1213
outDir: "dist",

0 commit comments

Comments
 (0)