Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 25 additions & 2 deletions pkg/commands/hgetall.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { keygen, newHttpClient, randomID } from "../test-utils.ts";
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
import { afterAll } from "https://deno.land/[email protected]/testing/bdd.ts";
import { HSetCommand } from "./hset.ts";
import {
keygen,
newHttpClient,
randomID,
randomUnsafeIntegerString,
} from "../test-utils.ts";
import { HGetAllCommand } from "./hgetall.ts";
import { HSetCommand } from "./hset.ts";

const client = newHttpClient();

Expand All @@ -29,3 +34,21 @@ Deno.test("when hash does not exist", async (t) => {
assertEquals(res, null);
});
});
Deno.test("properly return bigint precisely", async () => {
const key = newKey();
const field3 = randomID();
const field2 = randomID();
const field1 = randomID();
const value1 = false;
const value2 = randomID();
const value3 = randomUnsafeIntegerString();
await new HSetCommand([
key,
{ [field1]: value1, [field2]: value2, [field3]: value3 },
]).exec(client);

const res = await new HGetAllCommand([key]).exec(client);

const obj = { [field1]: value1, [field2]: value2, [field3]: value3 };
assertEquals(res, obj);
});
9 changes: 8 additions & 1 deletion pkg/commands/hgetall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ function deserialize<TData extends Record<string, unknown>>(
const key = result.shift()!;
const value = result.shift()!;
try {
obj[key] = JSON.parse(value);
// handle unsafe integer
const valueIsNumberAndNotSafeInteger = !isNaN(Number(value)) &&
!Number.isSafeInteger(value);
if (valueIsNumberAndNotSafeInteger) {
obj[key] = value;
} else {
obj[key] = JSON.parse(value);
}
} catch {
obj[key] = value;
}
Expand Down
22 changes: 22 additions & 0 deletions pkg/test-utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {
assertEquals,
assertFalse,
} from "https://deno.land/[email protected]/testing/asserts.ts";
import { randomUnsafeIntegerString } from "./test-utils.ts";

Deno.test("randomUnsafeIntegerString() should return a string", () => {
const result = randomUnsafeIntegerString();
assertEquals(typeof result, "string");
});
Deno.test("randomUnsafeIntegerString() should return different values", () => {
const result1 = randomUnsafeIntegerString();
const result2 = randomUnsafeIntegerString();
assertEquals(result1 !== result2, true);
});
Deno.test(
"randomUnsafeIntegerString() should return a string with unsafe integer",
() => {
const result = randomUnsafeIntegerString();
assertFalse(Number.isSafeInteger(Number(result)));
},
);
7 changes: 7 additions & 0 deletions pkg/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ export function randomID(): string {
}
return btoa(s.join(""));
}
export const randomUnsafeIntegerString = (): string => {
const buffer = new Uint8Array(8);
crypto.getRandomValues(buffer);
const dataView = new DataView(buffer.buffer);
const unsafeInteger = dataView.getBigInt64(0, true); // true for little-endian
return unsafeInteger.toString();
};
export const newHttpClient = () => {
const url = Deno.env.get("UPSTASH_REDIS_REST_URL");
if (!url) {
Expand Down