Skip to content

Commit 5a43493

Browse files
committed
fix: serialize map into json
1 parent aa5ad06 commit 5a43493

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

src/RedisStringsHandler.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { commandOptions, createClient, RedisClientOptions } from 'redis';
22
import { SyncedMap } from './SyncedMap';
33
import { DeduplicatedRequestHandler } from './DeduplicatedRequestHandler';
44
import { debug } from './utils/debug';
5-
import { bufferReviver, bufferReplacer } from './utils/json';
5+
import { bufferAndMapReviver, bufferAndMapReplacer } from './utils/json';
66

77
export type CommandOptions = ReturnType<typeof commandOptions>;
88
export type Client = ReturnType<typeof createClient>;
@@ -405,7 +405,7 @@ export default class RedisStringsHandler {
405405

406406
const cacheEntry: CacheEntry | null = JSON.parse(
407407
serializedCacheEntry,
408-
bufferReviver,
408+
bufferAndMapReviver,
409409
);
410410

411411
debug(
@@ -606,7 +606,10 @@ export default class RedisStringsHandler {
606606
tags: ctx?.tags || [],
607607
value: data,
608608
};
609-
const serializedCacheEntry = JSON.stringify(cacheEntry, bufferReplacer);
609+
const serializedCacheEntry = JSON.stringify(
610+
cacheEntry,
611+
bufferAndMapReplacer,
612+
);
610613

611614
// pre seed data into deduplicated get client. This will reduce redis load by not requesting
612615
// the same value from redis which was just set.

src/utils/json.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2-
export function bufferReviver(_: string, value: any): any {
2+
export function bufferAndMapReviver(_: string, value: any): any {
33
if (value && typeof value === 'object' && typeof value.$binary === 'string') {
44
return Buffer.from(value.$binary, 'base64');
55
}
6+
if (
7+
value &&
8+
typeof value === 'object' &&
9+
typeof value.$map === 'object' &&
10+
!!value.$map
11+
) {
12+
return new Map(
13+
Object.entries(value.$map).map(([key, value]) => {
14+
const revivedValue = bufferAndMapReviver('', value);
15+
return [key, revivedValue];
16+
}),
17+
);
18+
}
619
return value;
720
}
821
// eslint-disable-next-line @typescript-eslint/no-explicit-any
9-
export function bufferReplacer(_: string, value: any): any {
22+
export function bufferAndMapReplacer(_: string, value: any): any {
1023
if (Buffer.isBuffer(value)) {
1124
return {
1225
$binary: value.toString('base64'),
@@ -22,5 +35,15 @@ export function bufferReplacer(_: string, value: any): any {
2235
$binary: Buffer.from(value.data).toString('base64'),
2336
};
2437
}
38+
if (value && typeof value === 'object' && value instanceof Map) {
39+
return {
40+
$map: Object.fromEntries(
41+
Array.from(value.entries()).map(([key, value]) => {
42+
const replacedValue = bufferAndMapReplacer('', value);
43+
return [key, replacedValue];
44+
}),
45+
),
46+
};
47+
}
2548
return value;
2649
}

0 commit comments

Comments
 (0)