mirror of
https://github.com/allankoechke/turso.git
synced 2026-11-30 11:12:58 +00:00
## Summary
`push()` in `@tursodatabase/sync` (Node) used roughly 14–20x the size of
the change set in RSS. A user pushing a ~50 MB change set saw a +900 MB
RSS peak and had to set `pushOperationsThreshold` to avoid OOM kills in
CI. The dominant cost was not the sync engine but the napi boundary: the
serialized HTTP request body was handed to JavaScript as a plain
`Array<number>`, one element per byte.
This PR passes the body (and `FullWrite` content) across the boundary as
a napi `Buffer` instead, and drops the extra `Uint8Array` copy on the JS
side.
## Root cause
`JsProtocolRequest::Http { body: Option<Vec<u8>>, .. }` and `FullWrite {
content: Vec<u8> }` are fields of a `#[napi]` enum. napi-rs marshals
`Vec<u8>` as a JS array of numbers (the generated typing was `body?:
Array<number>`). V8 stores such an array at 8 bytes per element, so a 21
MiB JSON body became a ~170 MiB array before `run.ts` copied it again
with `new Uint8Array(requestType.body)` and undici buffered it once
more.
Everything else in the push path (decoded CDC row images, the
`BatchStep` vector, the JSON body, undici's copy) adds up to a few
multiples of the body, which is what remains after this change.
## Changes
- `bindings/javascript/sync/src/js_protocol_io.rs`: `Http.body` is
`Option<Buffer>`, `FullWrite.content` is `Buffer`. Both are built with
`Buffer::from(Vec<u8>)`, which takes ownership of the allocation without
copying.
- `bindings/javascript/sync/packages/common/run.ts`: pass the buffer
straight to `fetch`; a `Buffer` is already a `Uint8Array`.
- `bindings/javascript/sync/packages/native/index.d.ts`: the two
generated types updated to `Buffer`.
- `bindings/javascript/sync/packages/native/promise.test.ts`: new
regression test.
No sync-engine (Rust core) changes. The wasm package shares this Rust
code and its `write()` implementations already accept `Buffer |
Uint8Array`, so `FullWrite` needs no JS follow-up there.
## Measurements
Same test (`push()` of one 16 MiB blob, 21.3 MiB JSON body), same
machine, debug builds:
| Build | RSS growth during `push()` | Multiple of body |
|---|---|---|
| Without this change | 296 MiB | 13.9x |
| With this change | 83 MiB | 3.9x |
Standalone, holding 50 MiB of bytes as a JS `Array<number>` cost 1080
MiB of RSS on Node 26 versus 50 MiB as a `Uint8Array`, which matches the
user report almost exactly.
Closes #8635
Turso Database for JavaScript
About
This package is the Turso in-memory database library for JavaScript.
Turso Database runs in production today at multiple organizations. It has not yet reached 1.0, so as with any database we recommend keeping backups — see the FAQ for where the project stands.
Features
- SQLite compatible: SQLite query language and file format support (status).
- In-process: No network overhead, runs directly in your Node.js process
- TypeScript support: Full TypeScript definitions included
- Cross-platform: Supports Linux (x86 and arm64), macOS, Windows and browsers (through WebAssembly)
Installation
npm install @tursodatabase/database
Getting Started
In-Memory Database
import { connect } from '@tursodatabase/database';
// Create an in-memory database
const db = await connect(':memory:');
// Create a table
await db.exec(
'CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT)'
);
// Insert data
const insert = db.prepare('INSERT INTO users (name, email) VALUES (?, ?)');
await insert.run('Alice', 'alice@example.com');
await insert.run('Bob', 'bob@example.com');
// Query data
const users = await db.prepare('SELECT * FROM users').all();
console.log(users);
// Output: [
// { id: 1, name: 'Alice', email: 'alice@example.com' },
// { id: 2, name: 'Bob', email: 'bob@example.com' }
// ]
File-Based Database
import { connect } from '@tursodatabase/database';
// Create or open a database file
const db = await connect('my-database.db');
// Create a table
await db.exec(`
CREATE TABLE IF NOT EXISTS posts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
content TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
)
`);
// Insert a post
const insertPost = db.prepare('INSERT INTO posts (title, content) VALUES (?, ?)');
const result = await insertPost.run('Hello World', 'This is my first blog post!');
console.log(`Inserted post with ID: ${result.lastInsertRowid}`);
Transactions
import { connect } from '@tursodatabase/database';
const db = await connect('transactions.db');
// Using transactions for atomic operations
const transaction = db.transactionAsync(async (txn, users) => {
const insert = await txn.prepare('INSERT INTO users (name, email) VALUES (?, ?)');
for (const user of users) {
await insert.run(user.name, user.email);
}
});
// Execute transaction
await transaction([
{ name: 'Alice', email: 'alice@example.com' },
{ name: 'Bob', email: 'bob@example.com' }
]);
WebAssembly Support
Turso Database can run in browsers using WebAssembly. Check the browser.js and WASM artifacts for browser usage.
API Reference
For complete API documentation, see JavaScript API Reference.
Related Packages
- The @tursodatabase/serverless package provides a serverless driver with the same API.
- The @tursodatabase/sync package provides bidirectional sync between a local Turso database and Turso Cloud.
License
This project is licensed under the MIT license.