Skip to content

Commit

Permalink
Release/3.2.0 (#266)
Browse files Browse the repository at this point in the history
* Async brotli (#265)

* refactor: use async brotli compress

* chore: add map kv test

* refactor: privatize internals

* chore: minor fixes and testable localstorage

* chore: update minimum deno version

* refactor: prefer assertEquals

* chore: bumped version

* chore: remove workflows from release branch push (#267)
  • Loading branch information
oliver-oloughlin authored Feb 22, 2025
1 parent 2ddf597 commit e3ac1b4
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches: ["main"]
pull_request:
branches: ["main"]
branches: ["main", "release/**/*"]

permissions:
contents: read
Expand Down
25 changes: 25 additions & 0 deletions .github/workflows/test_map_kv.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Test KV map

on:
push:
branches: ["main"]
pull_request:
branches: ["main", "release/**/*"]

permissions:
contents: read
id-token: write # The OIDC ID token is used for authentication with JSR.

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Setup repo
uses: actions/checkout@v3

- name: Setup Deno
uses: denoland/setup-deno@v2

- name: Run tests
run: deno task test -- map
4 changes: 2 additions & 2 deletions .github/workflows/test_minimum_deno_version.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches: ["main"]
pull_request:
branches: ["main"]
branches: ["main", "release/**/*"]

permissions:
contents: read
Expand All @@ -21,7 +21,7 @@ jobs:
- name: Setup Deno
uses: denoland/setup-deno@v2
with:
deno-version: v2.0.0
deno-version: v2.2.0

- name: Check Types
run: deno task check
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ native functionality as possible, like atomic operations, real-time data updates
and queue listeners. Also works with other runtimes such as Node.js and Bun, and
has compatibility options for the browser.

_Supported Deno verisons:_ **^2.0.0**
_Supported Deno verisons:_ **^2.2.0**

## Highlights

Expand Down
10 changes: 10 additions & 0 deletions benchmarks/utils/encoder.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { brotliCompressor } from "../../src/ext/encoding/brotli/brotli_compressor.ts";
import { v8Serialize } from "../../src/ext/encoding/v8/utils.ts";
import { obj } from "./_object.ts";

const data = v8Serialize(obj);
const compressor = brotliCompressor();

Deno.bench("encoder - brotli_compress", async () => {
await compressor.compress(data);
});
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@olli/kvdex",
"version": "3.1.4",
"version": "3.2.0",
"exports": {
".": "./mod.ts",
"./zod": "./src/ext/zod/mod.ts",
Expand Down
30 changes: 23 additions & 7 deletions src/ext/encoding/brotli/brotli_compressor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Compressor } from "../../../types.ts";
import { brotliCompressSync, brotliDecompressSync, constants } from "node:zlib";
import { brotliCompress, brotliDecompress, constants } from "node:zlib";
import type { BrotliCompressorOptions } from "./types.ts";

/**
Expand All @@ -23,19 +23,35 @@ class BrotliCompressor implements Compressor {
this.quality = quality;
}

compress(data: Uint8Array): Uint8Array {
const buffer = brotliCompressSync(data, {
compress(data: Uint8Array): Promise<Uint8Array> {
const { promise, resolve, reject } = Promise.withResolvers<Uint8Array>();

brotliCompress(data, {
params: { [constants.BROTLI_PARAM_QUALITY]: this.quality },
}, (err, result) => {
if (err) {
reject(err);
}

resolve(new Uint8Array(result));
});

return new Uint8Array(buffer);
return promise;
}

decompress(data: Uint8Array): Uint8Array {
const buffer = brotliDecompressSync(data, {
decompress(data: Uint8Array): Promise<Uint8Array> {
const { promise, resolve, reject } = Promise.withResolvers<Uint8Array>();

brotliDecompress(data, {
params: { [constants.BROTLI_PARAM_QUALITY]: this.quality },
}, (err, result) => {
if (err) {
reject(err);
}

resolve(new Uint8Array(result));
});

return new Uint8Array(buffer);
return promise;
}
}
16 changes: 8 additions & 8 deletions src/ext/kv/atomic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class MapKvAtomicOperation implements DenoAtomicOperation {
options?: DenoKvSetOptions,
): DenoAtomicOperation {
this.ops.push(async (versionstamp) => {
await this.kv._set(key, value, versionstamp, options);
await this.kv["setDocument"](key, value, versionstamp, options);
});
return this;
}
Expand All @@ -45,7 +45,7 @@ export class MapKvAtomicOperation implements DenoAtomicOperation {
this.ops.push(async (versionstamp) => {
const { value } = await this.kv.get(key);
if (!value) {
await this.kv._set(key, { value: n }, versionstamp);
await this.kv["setDocument"](key, { value: n }, versionstamp);
return;
}

Expand All @@ -54,7 +54,7 @@ export class MapKvAtomicOperation implements DenoAtomicOperation {
throw new Error("Min operation can only be performed on KvU64 value");
}

await this.kv._set(key, {
await this.kv["setDocument"](key, {
value: n < val ? n : val,
}, versionstamp);
});
Expand All @@ -66,7 +66,7 @@ export class MapKvAtomicOperation implements DenoAtomicOperation {
this.ops.push(async (versionstamp) => {
const { value } = await this.kv.get(key);
if (!value) {
await this.kv._set(key, { value: n }, versionstamp);
await this.kv["setDocument"](key, { value: n }, versionstamp);
return;
}

Expand All @@ -75,7 +75,7 @@ export class MapKvAtomicOperation implements DenoAtomicOperation {
throw new Error("Max operation can only be performed on KvU64 value");
}

await this.kv._set(key, {
await this.kv["setDocument"](key, {
value: n > val ? n : val,
}, versionstamp);
});
Expand All @@ -87,7 +87,7 @@ export class MapKvAtomicOperation implements DenoAtomicOperation {
this.ops.push(async (versionstamp) => {
const { value } = await this.kv.get(key);
if (!value) {
await this.kv._set(key, { value: n }, versionstamp);
await this.kv["setDocument"](key, { value: n }, versionstamp);
return;
}

Expand All @@ -96,7 +96,7 @@ export class MapKvAtomicOperation implements DenoAtomicOperation {
throw new Error("Sum operation can only be performed on KvU64 value");
}

await this.kv._set(key, {
await this.kv["setDocument"](key, {
value: n + val,
}, versionstamp);
});
Expand All @@ -117,7 +117,7 @@ export class MapKvAtomicOperation implements DenoAtomicOperation {

enqueue(value: unknown, options?: DenoKvEnqueueOptions): DenoAtomicOperation {
this.ops.push(async (versionstamp) => {
await this.kv._enqueue(value, versionstamp, options);
await this.kv["enqueueValue"](value, versionstamp, options);
});

return this;
Expand Down
8 changes: 4 additions & 4 deletions src/ext/kv/map_kv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export class MapKv implements DenoKv {
value: unknown,
options?: DenoKvSetOptions,
): Promise<DenoKvCommitResult> {
return await this._set(key, value, createVersionstamp(), options);
return await this.setDocument(key, value, createVersionstamp(), options);
}

async list(
Expand Down Expand Up @@ -208,7 +208,7 @@ export class MapKv implements DenoKv {
value: unknown,
options?: DenoKvEnqueueOptions,
): Promise<DenoKvCommitResult> | DenoKvCommitResult {
return this._enqueue(value, createVersionstamp(), options);
return this.enqueueValue(value, createVersionstamp(), options);
}

watch(
Expand All @@ -224,7 +224,7 @@ export class MapKv implements DenoKv {
return new MapKvAtomicOperation(this, this.atomicLock);
}

async _set(
private async setDocument(
key: DenoKvStrictKey,
value: unknown,
versionstamp: string,
Expand Down Expand Up @@ -252,7 +252,7 @@ export class MapKv implements DenoKv {
};
}

_enqueue(
private enqueueValue(
value: unknown,
versionstamp: string,
options?: DenoKvEnqueueOptions,
Expand Down
4 changes: 2 additions & 2 deletions tests/db/deleteAll.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { collection, kvdex, model } from "../../mod.ts";
import { assert } from "@std/assert";
import { assert, assertEquals } from "@std/assert";
import type { User } from "../models.ts";
import { generateLargeUsers, generateUsers, useKv } from "../utils.ts";
import { jsonEncoder } from "../../src/ext/encoding/mod.ts";
Expand Down Expand Up @@ -49,7 +49,7 @@ Deno.test("db - deleteAll", async (t) => {
const { result: docs3 } = await db.u64s.getMany({ limit: 1 });

const count1 = await db.countAll();
assert(count1 === users.length + largeUsers.length + u64s.length);
assertEquals(count1, users.length + largeUsers.length + u64s.length);

await db.deleteAll();

Expand Down
4 changes: 2 additions & 2 deletions tests/db/wipe.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { collection, kvdex, model } from "../../mod.ts";
import { assert } from "@std/assert";
import { assert, assertEquals } from "@std/assert";
import type { User } from "../models.ts";
import { generateLargeUsers, generateUsers, useKv } from "../utils.ts";
import { jsonEncoder } from "../../src/ext/encoding/mod.ts";
Expand Down Expand Up @@ -49,7 +49,7 @@ Deno.test("db - wipe", async (t) => {
const { result: docs3 } = await db.u64s.getMany({ limit: 1 });

const count1 = await db.countAll();
assert(count1 === users.length + largeUsers.length + u64s.length);
assertEquals(count1, users.length + largeUsers.length + u64s.length);

await db.wipe();

Expand Down
2 changes: 1 addition & 1 deletion tests/ext/kv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ Deno.test("ext - kv", async (t) => {
});
});

await t.step("Should delete entry by key", async () => {
await t.step("Should delete all entries", async () => {
await useStore((store) => {
const entries = [
["1", 10],
Expand Down
9 changes: 7 additions & 2 deletions tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { collection, type DenoKv, type DenoKvU64, kvdex } from "../mod.ts";
import { brotliCompressor } from "../src/ext/encoding/brotli/brotli_compressor.ts";
import { jsonEncoder } from "../src/ext/encoding/mod.ts";
import { MapKv } from "../src/ext/kv/map_kv.ts";
import { StorageAdapter } from "../src/ext/kv/mod.ts";
import { model } from "../src/model.ts";
import { TransformUserModel, type User, UserSchema } from "./models.ts";

Expand Down Expand Up @@ -77,8 +78,12 @@ export function createDb(kv: DenoKv) {
export async function useKv(
fn: (kv: DenoKv) => unknown,
) {
const kv = Deno.args[0] === "map"
? new MapKv()
const kvArg = Deno.args[0];

const kv = kvArg === "map"
? new MapKv({ clearOnClose: true })
: kvArg === "map-local"
? new MapKv({ map: new StorageAdapter(localStorage), clearOnClose: true })
: await Deno.openKv(":memory:");

const result = await fn(kv);
Expand Down

0 comments on commit e3ac1b4

Please sign in to comment.