Skip to content

Commit

Permalink
fix: make batched_atomic loadable in web
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsonk committed Apr 2, 2024
1 parent 9d68751 commit 798d991
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
10 changes: 7 additions & 3 deletions batched_atomic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
* @module
*/

import { serialize } from "node:v8";

import { BLOB_KEY, BLOB_META_KEY, setBlob } from "./blob_util.ts";
import { keys } from "./keys.ts";

Expand All @@ -54,14 +52,17 @@ const MAX_MUTATIONS = 999;
const MAX_TOTAL_MUTATION_SIZE_BYTES = 800_000;
const MAX_TOTAL_KEY_SIZE_BYTES = 80_000;

let serialize: ((value: unknown) => { byteLength: number }) | undefined =
undefined;

function getByteLength(value: unknown): number {
if (value instanceof ArrayBuffer || ArrayBuffer.isView(value)) {
return value.byteLength;
}
if (value instanceof Deno.KvU64) {
return 8;
}
return serialize(value).byteLength;
return serialize!(value).byteLength;
}

/**
Expand Down Expand Up @@ -258,6 +259,9 @@ export class BatchedAtomicOperation {
if (!this.#queue.length) {
return Promise.resolve([]);
}
if (!serialize) {
serialize = (await import("node:v8")).serialize;
}
const results: Promise<Deno.KvCommitResult | Deno.KvCommitError>[] = [];
let checks = 0;
let mutations = 0;
Expand Down
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kitsonk/kv-toolbox",
"version": "0.16.0",
"version": "0.16.1-beta.1",
"exports": {
"./batched_atomic": "./batched_atomic.ts",
"./blob": "./blob.ts",
Expand Down

0 comments on commit 798d991

Please sign in to comment.