Files
turso/bindings/javascript/sync
Pekka Enberg 09ec06a3ab Merge 'sync/bindings: fix memory issues with JS/NAPI buffers' from Preston Thorpe
## 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
2026-09-04 13:45:14 +03:00
..
2025-09-10 22:35:57 +04:00
2026-07-03 10:56:57 -05:00

Turso Sync for JavaScript

npm

Chat with other users of Turso on Discord


About

This package is for syncing local Turso databases to the Turso Cloud and back.

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.

Installation

npm install @tursodatabase/sync

Getting Started

To sync a database hosted at Turso Cloud:

import { connect } from '@tursodatabase/sync';

const db = await connect({
    path: 'local.db',                // path used as a prefix for local files created by sync-engine
    url: 'https://<db>.turso.io',    // URL of the remote database: turso db show <db>
    authToken: '...',                // auth token issued from the Turso Cloud: turso db tokens create <db>
    clientName: 'turso-sync-example' // arbitrary client name
});

// db has same functions as Database class from @tursodatabase/database package but adds few more methods for sync:
await db.pull(); // pull changes from the remote
await db.push(); // push changes to the remote
await db.sync(); // pull & push changes

License

This project is licensed under the MIT license.

Support