Skip to content

Commit

Permalink
improve normalize support
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jan 3, 2025
1 parent 74712ec commit fb2977a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 34 deletions.
63 changes: 41 additions & 22 deletions src/drivers/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,31 +91,13 @@ export default defineDriver((opts: RedisOptions) => {
},
setItemRaw:
opts.raw === true
? async (
key: string,
value: Uint8Array,
tOptions: TransactionOptions
) => {
let valueToSave: Buffer;
if (value instanceof Uint8Array) {
if (value instanceof Buffer) {
valueToSave = value;
} else {
valueToSave = Buffer.copyBytesFrom(
value,
value.byteOffset,
value.byteLength
);
}
} else {
throw createError(DRIVER_NAME, "Expected Buffer or Uint8Array");
}

? async (key: string, value: unknown, tOptions: TransactionOptions) => {
const _value = normalizeValue(value);
const ttl = tOptions?.ttl ?? opts.ttl;
if (ttl) {
await getRedisClient().set(p(key), valueToSave, "EX", ttl);
await getRedisClient().set(p(key), _value, "EX", ttl);
} else {
await getRedisClient().set(p(key), valueToSave);
await getRedisClient().set(p(key), _value);
}
}
: undefined,
Expand All @@ -140,3 +122,40 @@ export default defineDriver((opts: RedisOptions) => {
},
};
});

function normalizeValue(value: unknown): Buffer | string | number {
const type = typeof value;
if (type === "string" || type === "number") {
return value as string | number;
}
if (Buffer.isBuffer(value)) {
return value;
}
if (isTypedArray(value)) {
if (Buffer.copyBytesFrom) {
return Buffer.copyBytesFrom(value, value.byteOffset, value.byteLength);
} else {
return Buffer.from(value.buffer, value.byteOffset, value.byteLength);
}
}
if (value instanceof ArrayBuffer) {
return Buffer.from(value);
}
return JSON.stringify(value);
}

function isTypedArray(value: unknown): value is TypedArray {
return (
value instanceof Int8Array ||
value instanceof Uint8Array ||
value instanceof Uint8ClampedArray ||
value instanceof Int16Array ||
value instanceof Uint16Array ||
value instanceof Int32Array ||
value instanceof Uint32Array ||
value instanceof Float32Array ||
value instanceof Float64Array ||
value instanceof BigInt64Array ||
value instanceof BigUint64Array
);
}
12 changes: 0 additions & 12 deletions test/drivers/redis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,6 @@ describe("drivers: redis (raw: true)", () => {

await client.disconnect();
});

it("expects binary data to be sent to setItemRaw", async () => {
expect(() =>
ctx.storage.setItemRaw("s4:a", "Hello, world!")
).rejects.toThrow("Expected Buffer or Uint8Array");
expect(() => ctx.storage.setItemRaw("s5:a", 100)).rejects.toThrow(
"Expected Buffer or Uint8Array"
);
expect(() =>
ctx.storage.setItemRaw("s5:a", { foo: "bar" })
).rejects.toThrow("Expected Buffer or Uint8Array");
});
},
});
});

0 comments on commit fb2977a

Please sign in to comment.