summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cli/tests/unit/kv_test.ts6
-rw-r--r--ext/kv/01_db.ts11
2 files changed, 15 insertions, 2 deletions
diff --git a/cli/tests/unit/kv_test.ts b/cli/tests/unit/kv_test.ts
index 3a3476857..3c5efa588 100644
--- a/cli/tests/unit/kv_test.ts
+++ b/cli/tests/unit/kv_test.ts
@@ -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() {
diff --git a/ext/kv/01_db.ts b/ext/kv/01_db.ts
index ca37aa840..f8181cc2e 100644
--- a/ext/kv/01_db.ts
+++ b/ext/kv/01_db.ts
@@ -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;
}