mirror of
https://github.com/denoland/deno.git
synced 2024-11-21 15:04:11 -05:00
fix(ext/kv): keys must be arrays (#18655)
There was some leftover code from previous iterations, where keys could be single parts instead of arrays also. This didn't match the types.
This commit is contained in:
parent
015eb4ef9c
commit
e0bf8e6faf
2 changed files with 49 additions and 17 deletions
|
@ -1138,6 +1138,51 @@ dbTest("operation size limit", async (db) => {
|
|||
);
|
||||
});
|
||||
|
||||
dbTest("keys must be arrays", async (db) => {
|
||||
await assertRejects(
|
||||
// @ts-expect-error invalid type
|
||||
async () => await db.get("a"),
|
||||
TypeError,
|
||||
);
|
||||
|
||||
await assertRejects(
|
||||
// @ts-expect-error invalid type
|
||||
async () => await db.getMany(["a"]),
|
||||
TypeError,
|
||||
);
|
||||
|
||||
await assertRejects(
|
||||
// @ts-expect-error invalid type
|
||||
async () => await db.set("a", 1),
|
||||
TypeError,
|
||||
);
|
||||
|
||||
await assertRejects(
|
||||
// @ts-expect-error invalid type
|
||||
async () => await db.delete("a"),
|
||||
TypeError,
|
||||
);
|
||||
|
||||
await assertRejects(
|
||||
async () =>
|
||||
await db.atomic()
|
||||
// @ts-expect-error invalid type
|
||||
.mutate({ key: "a", type: "set", value: 1 } satisfies Deno.KvMutation)
|
||||
.commit(),
|
||||
TypeError,
|
||||
);
|
||||
|
||||
await assertRejects(
|
||||
async () =>
|
||||
await db.atomic()
|
||||
// @ts-expect-error invalid type
|
||||
.check({ key: "a", versionstamp: null })
|
||||
.set(["a"], 1)
|
||||
.commit(),
|
||||
TypeError,
|
||||
);
|
||||
});
|
||||
|
||||
// This function is never called, it is just used to check that all the types
|
||||
// are behaving as expected.
|
||||
async function _typeCheckingTests() {
|
||||
|
|
|
@ -48,7 +48,6 @@ class Kv {
|
|||
}
|
||||
|
||||
async get(key: Deno.KvKey, opts?: { consistency?: Deno.KvConsistencyLevel }) {
|
||||
key = convertKey(key);
|
||||
const [entries]: [RawKvEntry[]] = await core.opAsync(
|
||||
"op_kv_snapshot_read",
|
||||
this.#rid,
|
||||
|
@ -76,7 +75,6 @@ class Kv {
|
|||
keys: Deno.KvKey[],
|
||||
opts?: { consistency?: Deno.KvConsistencyLevel },
|
||||
): Promise<Deno.KvEntry<unknown>[]> {
|
||||
keys = keys.map(convertKey);
|
||||
const ranges: RawKvEntry[][] = await core.opAsync(
|
||||
"op_kv_snapshot_read",
|
||||
this.#rid,
|
||||
|
@ -103,7 +101,6 @@ class Kv {
|
|||
}
|
||||
|
||||
async set(key: Deno.KvKey, value: unknown) {
|
||||
key = convertKey(key);
|
||||
value = serializeValue(value);
|
||||
|
||||
const checks: Deno.AtomicCheck[] = [];
|
||||
|
@ -123,8 +120,6 @@ class Kv {
|
|||
}
|
||||
|
||||
async delete(key: Deno.KvKey) {
|
||||
key = convertKey(key);
|
||||
|
||||
const checks: Deno.AtomicCheck[] = [];
|
||||
const mutations = [
|
||||
[key, "delete", null],
|
||||
|
@ -211,14 +206,14 @@ class AtomicOperation {
|
|||
|
||||
check(...checks: Deno.AtomicCheck[]): this {
|
||||
for (const check of checks) {
|
||||
this.#checks.push([convertKey(check.key), check.versionstamp]);
|
||||
this.#checks.push([check.key, check.versionstamp]);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
mutate(...mutations: Deno.KvMutation[]): this {
|
||||
for (const mutation of mutations) {
|
||||
const key = convertKey(mutation.key);
|
||||
const key = mutation.key;
|
||||
let type: string;
|
||||
let value: RawValue | null;
|
||||
switch (mutation.type) {
|
||||
|
@ -247,12 +242,12 @@ class AtomicOperation {
|
|||
}
|
||||
|
||||
set(key: Deno.KvKey, value: unknown): this {
|
||||
this.#mutations.push([convertKey(key), "set", serializeValue(value)]);
|
||||
this.#mutations.push([key, "set", serializeValue(value)]);
|
||||
return this;
|
||||
}
|
||||
|
||||
delete(key: Deno.KvKey): this {
|
||||
this.#mutations.push([convertKey(key), "delete", null]);
|
||||
this.#mutations.push([key, "delete", null]);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -296,14 +291,6 @@ class KvU64 {
|
|||
}
|
||||
}
|
||||
|
||||
function convertKey(key: Deno.KvKey | Deno.KvKeyPart): Deno.KvKey {
|
||||
if (Array.isArray(key)) {
|
||||
return key;
|
||||
} else {
|
||||
return [key as Deno.KvKeyPart];
|
||||
}
|
||||
}
|
||||
|
||||
function deserializeValue(entry: RawKvEntry): Deno.KvEntry<unknown> {
|
||||
const { kind, value } = entry.value;
|
||||
switch (kind) {
|
||||
|
|
Loading…
Reference in a new issue