Compare commits

...

13 Commits
v3.2.1 ... main

Author SHA1 Message Date
Fedor Indutny
f90dd065e9 3.3.5
Some checks failed
Test / test (clang, clang++, macos-latest) (push) Has been cancelled
Test / test (gcc, g++, ubuntu-latest) (push) Has been cancelled
Test / test (windows-latest) (push) Has been cancelled
2026-05-26 12:32:59 -07:00
Fedor Indutny
9e947e02ae fix: pnpm build with prebuilds 2026-05-26 12:32:50 -07:00
Fedor Indutny
ef6443f848 fix: linux ci 2026-05-26 12:24:23 -07:00
Fedor Indutny
84a54d03d5 3.3.4 2026-05-26 12:08:35 -07:00
Fedor Indutny
d27e1d9a27 fix: install pnpm in Dockerfile 2026-05-26 12:08:25 -07:00
Fedor Indutny
19eb15285d 3.3.3 2026-05-26 11:58:19 -07:00
Fedor Indutny
c5c58ee0c4 fix: specify pnpm version in package.json 2026-05-26 11:58:08 -07:00
Fedor Indutny
d755f4997e 3.3.2 2026-05-26 11:31:32 -07:00
Fedor Indutny
3df7bb3fda chore: update pnpm to 10.18.1 2026-05-26 11:31:18 -07:00
Fedor Indutny
14ea17da26 3.3.1 2026-05-26 11:02:26 -07:00
Fedor Indutny
c2ba9fcc16
chore: update docker image to fix linux builds 2026-05-26 11:02:06 -07:00
Fedor Indutny
788376d0cf 3.3.0 2026-05-26 09:49:16 -07:00
Jamie
f6c6098522
Add db.setWalHook()
Co-authored-by: Fedor Indutny <indutny@signal.org>
2026-05-26 09:49:06 -07:00
10 changed files with 194 additions and 13 deletions

View File

@ -40,7 +40,7 @@ jobs:
- name: Setup pnpm - name: Setup pnpm
uses: pnpm/action-setup@a7487c7e89a18df4991f7f222e4898a00d66ddda # v4.1.0 uses: pnpm/action-setup@a7487c7e89a18df4991f7f222e4898a00d66ddda # v4.1.0
with: with:
version: 10.3.0 version: 10.18.1
- name: Get Node version from .nvmrc - name: Get Node version from .nvmrc
id: get-nvm-version id: get-nvm-version
@ -90,7 +90,7 @@ jobs:
- name: Setup pnpm - name: Setup pnpm
uses: pnpm/action-setup@a7487c7e89a18df4991f7f222e4898a00d66ddda # v4.1.0 uses: pnpm/action-setup@a7487c7e89a18df4991f7f222e4898a00d66ddda # v4.1.0
with: with:
version: 10.3.0 version: 10.18.1
- name: Get Node version from .nvmrc - name: Get Node version from .nvmrc
id: get-nvm-version id: get-nvm-version
@ -137,7 +137,7 @@ jobs:
- name: Setup pnpm - name: Setup pnpm
uses: pnpm/action-setup@a7487c7e89a18df4991f7f222e4898a00d66ddda # v4.1.0 uses: pnpm/action-setup@a7487c7e89a18df4991f7f222e4898a00d66ddda # v4.1.0
with: with:
version: 10.3.0 version: 10.18.1
- name: Setup node.js - name: Setup node.js
uses: actions/setup-node@1d0ff469b7ec7b3cb9d8673fde0c81c44821de2a # v4.2.0 uses: actions/setup-node@1d0ff469b7ec7b3cb9d8673fde0c81c44821de2a # v4.2.0
with: with:

View File

@ -30,7 +30,7 @@ jobs:
- name: Setup pnpm - name: Setup pnpm
uses: pnpm/action-setup@a7487c7e89a18df4991f7f222e4898a00d66ddda # v4.1.0 uses: pnpm/action-setup@a7487c7e89a18df4991f7f222e4898a00d66ddda # v4.1.0
with: with:
version: 10.3.0 version: 10.18.1
- name: Setup node.js - name: Setup node.js
uses: actions/setup-node@1d0ff469b7ec7b3cb9d8673fde0c81c44821de2a # v4.2.0 uses: actions/setup-node@1d0ff469b7ec7b3cb9d8673fde0c81c44821de2a # v4.2.0
with: with:

1
.gitignore vendored
View File

@ -7,3 +7,4 @@ docs/
.tmp/ .tmp/
.eslintcache .eslintcache
todo.md todo.md
.vscode

View File

@ -2,7 +2,7 @@
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
# #
FROM ubuntu:focal-20240530@sha256:fa17826afb526a9fc7250e0fbcbfd18d03fe7a54849472f86879d8bf562c629e FROM ubuntu:jammy-20250714@sha256:1ec65b2719518e27d4d25f104d93f9fac60dc437f81452302406825c46fcc9cb
# Avoid getting prompted to configure things during installation. # Avoid getting prompted to configure things during installation.
ENV DEBIAN_FRONTEND=noninteractive ENV DEBIAN_FRONTEND=noninteractive
@ -15,13 +15,15 @@ COPY docker/apt.conf docker/sources.list /etc/apt/
# But we can't install it because it doesn't trust our mirror! # But we can't install it because it doesn't trust our mirror!
# Temporarily disables APT's certificate signature checking # Temporarily disables APT's certificate signature checking
# to download the certificates. # to download the certificates.
RUN apt-get update -oAcquire::https::Verify-Peer=false \ RUN apt update -oAcquire::https::Verify-Peer=false
&& apt-get install -oAcquire::https::Verify-Peer=false -y ca-certificates RUN apt install -oAcquire::https::Verify-Peer=false -y ca-certificates
# Back to normal, verification back on # Back to normal, verification back on
# Install only what's needed to set up Rust and Node. # Install only what's needed to set up Rust and Node.
# We'll install additional tools at the end to take advantage of Docker's caching of earlier steps. # We'll install additional tools at the end to take advantage of Docker's caching of earlier steps.
RUN apt-get update && apt-get install -y apt-transport-https xz-utils unzip RUN apt update
RUN apt install -y apt-transport-https xz-utils unzip
# User-specific setup! # User-specific setup!
@ -65,6 +67,9 @@ RUN tar -xf node.tar.xz \
ENV PATH="/home/sqlcipher/node/bin:${PATH}" ENV PATH="/home/sqlcipher/node/bin:${PATH}"
# Install pnpm
RUN npm install -g pnpm@10.18.1
# And finally any bonus packages we're going to need # And finally any bonus packages we're going to need
# Note that we jump back to root for this. # Note that we jump back to root for this.
USER root USER root

View File

@ -1,3 +1,3 @@
deb [snapshot=20240829T060900Z] http://archive.ubuntu.com/ubuntu/ focal main universe deb [snapshot=20250811T060900Z] http://archive.ubuntu.com/ubuntu/ jammy main universe
deb [snapshot=20240829T060900Z] http://archive.ubuntu.com/ubuntu/ focal-updates main universe deb [snapshot=20250811T060900Z] http://archive.ubuntu.com/ubuntu/ jammy-updates main universe
deb [snapshot=20240829T060900Z] http://security.ubuntu.com/ubuntu focal-security main universe deb [snapshot=20250811T060900Z] http://security.ubuntu.com/ubuntu jammy-security main universe

View File

@ -53,6 +53,10 @@ const addon = bindings<{
fn: (...args: ReadonlyArray<unknown>) => void, fn: (...args: ReadonlyArray<unknown>) => void,
bigint: boolean, bigint: boolean,
): void; ): void;
databaseSetWalHook(
db: NativeDatabase,
fn: (dbName: string, pageCount: number) => void,
): void;
signalTokenize(value: string): Array<string>; signalTokenize(value: string): Array<string>;
@ -409,6 +413,13 @@ export type DatabaseOptions = Readonly<{
cacheStatements?: boolean; cacheStatements?: boolean;
}>; }>;
/**
* @param dbName - The name of the database that was written to.
* @param pageCount - The number of pages currently in the write-ahead log file,
* including those that were just committed.
*/
export type WalHook = (dbName: string, pageCount: number) => void;
/** /**
* A sqlite database class. * A sqlite database class.
*/ */
@ -495,6 +506,22 @@ export default class Database {
); );
} }
/**
* Register a callback to be invoked each time data is commited to a database
* in WAL mode.
*
* @param fn - function implementation
*/
public setWalHook(fn: WalHook): void {
if (this.#native === undefined) {
throw new Error('Database closed');
}
if (typeof fn !== 'function') {
throw new TypeError('Invalid fn argument');
}
addon.databaseSetWalHook(this.#native, fn);
}
/** /**
* Compile a single SQL statement. * Compile a single SQL statement.
* *

View File

@ -1,6 +1,7 @@
{ {
"packageManager": "pnpm@10.18.1",
"name": "@signalapp/sqlcipher", "name": "@signalapp/sqlcipher",
"version": "3.2.1", "version": "3.3.5",
"description": "A fast N-API-based Node.js addon wrapping sqlcipher and FTS5 segmenting APIs", "description": "A fast N-API-based Node.js addon wrapping sqlcipher and FTS5 segmenting APIs",
"homepage": "http://github.com/signalapp/node-sqlcipher.git", "homepage": "http://github.com/signalapp/node-sqlcipher.git",
"license": "AGPL-3.0-only", "license": "AGPL-3.0-only",

View File

@ -158,6 +158,38 @@ class FunctionWrap {
bool is_bigint_; bool is_bigint_;
}; };
// WAL Hook
class WalHookWrap {
public:
explicit WalHookWrap(Napi::Function fn) { fn_.Reset(fn, 1); }
static int Run(void* p_app, sqlite3* _db, const char* db_name, int n_pages) {
auto wrap = static_cast<WalHookWrap*>(p_app);
wrap->Call(db_name, n_pages);
return SQLITE_OK;
}
protected:
void Call(const char* db_name, int n_pages) {
auto env = fn_.Env();
Napi::HandleScope scope(env);
auto result = fn_.Value().Call({
Napi::String::New(env, db_name),
Napi::Number::New(env, n_pages),
});
// Ignore exceptions
if (result.IsEmpty()) {
env.GetAndClearPendingException();
}
}
private:
Napi::Reference<Napi::Function> fn_;
};
// Global Settings // Global Settings
thread_local Napi::Reference<Napi::Function> logger_fn_; thread_local Napi::Reference<Napi::Function> logger_fn_;
@ -236,6 +268,8 @@ Napi::Object Database::Init(Napi::Env env, Napi::Object exports) {
exports["databaseExec"] = Napi::Function::New(env, &Database::Exec); exports["databaseExec"] = Napi::Function::New(env, &Database::Exec);
exports["databaseCreateFunction"] = exports["databaseCreateFunction"] =
Napi::Function::New(env, &Database::CreateFunction); Napi::Function::New(env, &Database::CreateFunction);
exports["databaseSetWalHook"] =
Napi::Function::New(env, &Database::SetWalHook);
return exports; return exports;
} }
@ -251,6 +285,9 @@ Database::~Database() {
return; return;
} }
delete wal_hook_wrap_;
wal_hook_wrap_ = nullptr;
int r = sqlite3_close(handle_); int r = sqlite3_close(handle_);
if (r != SQLITE_OK) { if (r != SQLITE_OK) {
fprintf(stderr, "Cleanup: sqlite3_close failure\n"); fprintf(stderr, "Cleanup: sqlite3_close failure\n");
@ -342,6 +379,9 @@ Napi::Value Database::Close(const Napi::CallbackInfo& info) {
} }
db->statements_.clear(); db->statements_.clear();
delete db->wal_hook_wrap_;
db->wal_hook_wrap_ = nullptr;
int r = sqlite3_close(db->handle_); int r = sqlite3_close(db->handle_);
if (r != SQLITE_OK) { if (r != SQLITE_OK) {
return db->ThrowSqliteError(env, r); return db->ThrowSqliteError(env, r);
@ -411,6 +451,32 @@ Napi::Value Database::CreateFunction(const Napi::CallbackInfo& info) {
return Napi::Value(); return Napi::Value();
} }
Napi::Value Database::SetWalHook(const Napi::CallbackInfo& info) {
auto env = info.Env();
auto db = FromExternal(info[0]);
auto fn = info[1].As<Napi::Function>();
assert(fn.IsFunction());
if (db == nullptr) {
return Napi::Value();
}
if (db->handle_ == nullptr) {
NAPI_THROW(Napi::Error::New(env, "Database closed"), Napi::Value());
}
auto wal_wrap = new WalHookWrap(fn);
delete db->wal_hook_wrap_;
db->wal_hook_wrap_ = wal_wrap;
sqlite3_wal_hook(db->handle_, WalHookWrap::Run, wal_wrap);
return Napi::Value();
}
Napi::Value Database::ThrowSqliteError(Napi::Env env, int error) { Napi::Value Database::ThrowSqliteError(Napi::Env env, int error) {
assert(handle_ != nullptr); assert(handle_ != nullptr);
const char* msg = sqlite3_errmsg(handle_); const char* msg = sqlite3_errmsg(handle_);

View File

@ -9,6 +9,7 @@
#include "sqlite3.h" #include "sqlite3.h"
class Statement; class Statement;
class WalHookWrap;
class Database { class Database {
public: public:
@ -31,6 +32,7 @@ class Database {
static Napi::Value Close(const Napi::CallbackInfo& info); static Napi::Value Close(const Napi::CallbackInfo& info);
static Napi::Value Exec(const Napi::CallbackInfo& info); static Napi::Value Exec(const Napi::CallbackInfo& info);
static Napi::Value CreateFunction(const Napi::CallbackInfo& info); static Napi::Value CreateFunction(const Napi::CallbackInfo& info);
static Napi::Value SetWalHook(const Napi::CallbackInfo& info);
fts5_api* GetFTS5API(Napi::Env env); fts5_api* GetFTS5API(Napi::Env env);
@ -45,6 +47,8 @@ class Database {
// All currently open statements for this database. Used to close all open // All currently open statements for this database. Used to close all open
// statements when closing the database. // statements when closing the database.
std::list<Statement*> statements_; std::list<Statement*> statements_;
WalHookWrap* wal_hook_wrap_ = nullptr;
}; };
class AutoResetStatement { class AutoResetStatement {

View File

@ -1,7 +1,7 @@
import { mkdtemp, rm } from 'node:fs/promises'; import { mkdtemp, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os'; import { tmpdir } from 'node:os';
import { join } from 'node:path'; import { join } from 'node:path';
import { expect, test, beforeEach, afterEach } from 'vitest'; import { expect, test, describe, beforeEach, afterEach, vi } from 'vitest';
import Database from '../lib/index.js'; import Database from '../lib/index.js';
@ -65,3 +65,80 @@ test.each([[false], [true]])('ciphertext=%j', (ciphertext) => {
expect(row).toEqual({ name: 'Adam', value: 'Sandler' }); expect(row).toEqual({ name: 'Adam', value: 'Sandler' });
}); });
describe('setWalHook', () => {
beforeEach(() => {
db.pragma('journal_mode = WAL');
db.exec('CREATE TABLE t (a INTEGER)');
});
test('calls hook after WAL commit', () => {
const hook = vi.fn();
db.setWalHook(hook);
db.prepare('INSERT INTO t (a) VALUES (1)').run();
expect(hook).toHaveBeenCalledOnce();
expect(hook).toHaveBeenCalledWith('main', expect.any(Number));
});
test('hook receives page count > 0', () => {
let pageCount: number | null = null;
db.setWalHook((_dbName, n) => {
pageCount = n;
});
db.prepare('INSERT INTO t (a) VALUES (1)').run();
expect(pageCount).toBeGreaterThan(0);
});
test('hook fires once per commit', () => {
const hook = vi.fn();
db.setWalHook(hook);
db.transaction(() => {
db.prepare('INSERT INTO t (a) VALUES (1)').run();
db.prepare('INSERT INTO t (a) VALUES (2)').run();
})();
expect(hook).toHaveBeenCalledOnce();
});
test('replaces previous hook', () => {
const first = vi.fn();
const second = vi.fn();
db.setWalHook(first);
db.setWalHook(second);
db.prepare('INSERT INTO t (a) VALUES (1)').run();
expect(first).not.toHaveBeenCalled();
expect(second).toHaveBeenCalledOnce();
});
test('silently ignores exceptions thrown by hook', () => {
let called = false;
db.setWalHook(() => {
called = true;
throw new Error('hook error');
});
expect(() =>
db.prepare('INSERT INTO t (a) VALUES (1)').run(),
).not.toThrow();
expect(called).toBe(true);
});
test('throws when database is closed', () => {
db.close();
expect(() => db.setWalHook(vi.fn())).toThrowError('Database closed');
db = new Database(join(dir, 'db2.sqlite'));
});
test('throws for invalid argument', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect(() => db.setWalHook(123 as any)).toThrowError('Invalid fn argument');
});
});