1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-23 07:44:48 -05:00

fix(ext/kv): throw on the Kv constructor (#18978)

Closes #18963

---------

Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
This commit is contained in:
Luca Casonato 2023-05-03 23:08:42 +02:00 committed by GitHub
parent 632395da89
commit d905f20cad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 2 deletions

View file

@ -1256,6 +1256,12 @@ dbTest("keys must be arrays", async (db) => {
);
});
Deno.test("Deno.Kv constructor throws", () => {
assertThrows(() => {
new Deno.Kv();
});
});
// This function is never called, it is just used to check that all the types
// are behaving as expected.
async function _typeCheckingTests() {

View file

@ -23,7 +23,7 @@ const encodeCursor: (
async function openKv(path: string) {
const rid = await core.opAsync("op_kv_database_open", path);
return new Kv(rid);
return new Kv(rid, kvSymbol);
}
interface RawKvEntry {
@ -43,10 +43,17 @@ type RawValue = {
value: bigint;
};
const kvSymbol = Symbol("KvRid");
class Kv {
#rid: number;
constructor(rid: number) {
constructor(rid: number = undefined, symbol: symbol = undefined) {
if (kvSymbol !== symbol) {
throw new TypeError(
"Deno.Kv can not be constructed, use Deno.openKv instead.",
);
}
this.#rid = rid;
}