Commit Graph

54 Commits

Author SHA1 Message Date
Glauber Costa
4a0467d9dc serverless/javascript: await async prepare() in integration tests
The prepare()/Statement.run()/iterate() tests called `client.prepare(...)`
without awaiting it. Connection.prepare is async (returns Promise<Statement>),
so `stmt` was a Promise and `stmt.get/run/iterate` were "not a function". Add
the missing await.

This was surfaced running the JS suite against a live libsql-server.

All 46 JS integration tests pass against the live server.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-17 18:22:50 -05:00
Pekka Enberg
51f269e4fd serverless/javascript: 1.3.1 2026-07-17 19:48:43 +03:00
Nikita Sivukhin
344cea792d Merge branch 'main' into request-headers-serverless 2026-07-17 15:00:34 +02:00
Pekka Enberg
26695c91c0 serverless/javascript: 1.3.0 2026-07-17 15:56:22 +03:00
Nikita Sivukhin
8069982e0d fix example 2026-07-17 14:45:58 +02:00
Nikita Sivukhin
74311642b0 allow to pass arbitrary request headers in the queries through queryOptions 2026-07-17 13:37:15 +02:00
Pekka Enberg
cbfd05b1de serverless/javascript: fix stale inTransaction after execute and batch
The driver caches the server's get_autocommit answer to back the
synchronous Connection.inTransaction property, but only describe and
sequence refreshed it. execute() and batch() went through the cursor
endpoint, which cannot carry a get_autocommit probe, so the cached
flag went stale. After a raw BEGIN IMMEDIATE batch hit a constraint
error, the server-side write transaction stayed open (statement-level
ABORT) while inTransaction still read false, so cleanup code skipped
ROLLBACK and the leaked transaction starved every other writer until
timeout.

Learn the transaction state in-band instead: every cursor batch gets a
trailing SELECT 1 step gated on the is_autocommit condition. The
server executes the step only when the connection is in autocommit,
so its presence in the entry stream is the authoritative answer, with
no extra round trip and no change to the streaming transport. A shared
wrapper filters the probe's entries out of the stream and falls back
to a standalone get_autocommit pipeline request when the stream ends
abnormally (fatal error entry or the consumer stops iterating early),
where the probe answer is unreliable.

Non-atomic batch() now defers its first step error until the stream is
drained instead of throwing immediately, so the probe is still
observed; the server ran the remaining unconditioned steps either way,
so the observable semantics are unchanged.

With the flag accurate, an atomic batch inside a manually-opened
transaction now joins it (mode ignored) instead of failing on a nested
BEGIN, matching batch() inside transaction(); the conformance test
pinning the old failure mode now pins the join contract.

Tests: testing/conformance/javascript async.test.js and compat.test.js
(PROVIDER=turso, PROVIDER=serverless against sqld, including the
queryTimeout cases), serverless/javascript integration-tests, and a
direct check that a 20MB row-producing batch streams successfully.
2026-07-17 09:33:00 +03:00
Pekka Enberg
9393b89f0b serverless/javascript: 1.3.0-pre.1 2026-06-18 09:40:40 +03:00
Pekka Enberg
45e80cfe26 Return per-statement results from batch()
Make the JavaScript batch() return an array of ResultSet, one per input
statement, matching the libSQL client contract. Each ResultSet carries
that statement's columns, columnTypes, rows (with both positional and
named access), rowsAffected, and a BigInt lastInsertRowid.

Previously the native Database.batch() returned an aggregate
{ rowsAffected, lastInsertRowid } and discarded rows, while the
libSQL-compat client.batch() ignored statement args and collapsed all
statements into a single result. This reworks every layer:

- Native Database.batch() reads each statement's columns, collects rows
  in raw mode, and builds one ResultSet per statement.
- session.batch() requests rows (want_rows) and returns one raw result
  per statement, preserving the atomic Hrana condition chain.
- Connection.batch() shapes each result into a libSQL ResultSet.
- compat client.batch() binds args and runs the batch atomically.

Add conformance tests for both the native Database.batch() and the
libSQL-compat client.batch().
2026-06-18 09:09:38 +03:00
Pekka Enberg
971cac6dcf serverless/javascript: 1.2.3 2026-06-12 11:23:02 +03:00
Pekka Enberg
bf622cd968 serverless/javascript: Fix publish step to build bundle 2026-06-12 11:22:46 +03:00
Pekka Enberg
1e5d1eca21 serverless/javascript: 1.2.2 2026-06-12 08:53:18 +03:00
Nikita Sivukhin
083ccc811d Merge 'serverless/javascript: add option for custom request headers' from Nikita Sivukhin
Simple PR to allow user to set arbitrary request headers for serverless
driver. For example:
```js
const conn = connect({
  url: process.env.TURSO_DATABASE_URL,
  authToken: process.env.TURSO_AUTH_TOKEN,
  requestHeaders: { "x-custom-header": "value" },
});
```

Reviewed-by: Pekka Enberg <penberg@iki.fi>

Closes #7403
2026-06-11 21:36:39 +04:00
Nikita Sivukhin
90ae357645 add validation for requestHeaders parameter 2026-06-11 19:25:27 +02:00
Nikita Sivukhin
300e0ce13b add test 2026-06-11 19:16:51 +02:00
Pekka Enberg
fa16183110 serverless/javascript: 1.2.1 2026-06-11 20:06:23 +03:00
Pedro Muniz
7715979bd0 Merge 'Preserve JS row positional values' from Pedro Muniz
I think JS has problems overall. But my guess here is that there is some
positional property that we were not adding.
Closes https://github.com/tursodatabase/turso/issues/7278
Vibed as Im not quite a JS script kiddie

Reviewed-by: Mikaël Francoeur (@LeMikaelF)
Reviewed-by: Pekka Enberg <penberg@iki.fi>

Closes #7285
2026-06-08 16:22:28 -03:00
Nikita Sivukhin
2006463cff add requestHeaders option so that user can set arbitrary headers for serverless driver 2026-06-08 08:58:08 +02:00
pedrocarlo
4c0febfed2 Preserve JS row positional values 2026-06-02 18:58:41 -03:00
Pekka Enberg
07ba170691 serverless/javascript: 1.2.0 2026-06-02 18:57:48 +03:00
Pekka Enberg
34ae4061ca Merge 'serverless/javascript: Fix stack overflow error with large blobs' from Henry
Fixes https://github.com/tursodatabase/turso/issues/7330
No AI

Closes #7331
2026-06-02 18:54:17 +03:00
mcmah309
42a4ea085b Fix serverless stackoverflow issue for large blobs 2026-06-02 14:26:56 +03:00
Pekka Enberg
e5ddef05ed javascript: Fix Database.inTransaction property
Fix the inTransaction property to probe the database connection whether
it is in a transaction or not.
2026-06-01 18:36:54 +03:00
Pekka Enberg
cfd9fb9669 serverless/javascript: 1.2.0-pre.2 2026-05-19 18:00:58 +03:00
Pekka Enberg
f6a16e8299 Add Database.batch() to the JavaScript SDKs
The batch() method executes an array of SQL statements over a single
connection and returns aggregated `rowsAffected` and `lastInsertRowid`. Each
statement is either a SQL string or a `{ sql, args }` object with positional
or named bind parameters; named-args objects are forwarded to the underlying
engine as named parameters rather than collapsed via Object.values().

By default the batch is non-transactional: each statement runs in its own
autocommit step. Passing a `mode` -- `deferred`, `immediate`, `exclusive`, or
`concurrent` -- makes the batch atomic. You can also use `batch()` with the
transaction API to make it transactional.

The serverless driver implements atomic batch as a a single SQL over HTTP
request. The wire payload chains BEGIN, the user statements, COMMIT, and
ROLLBACK as a single batch using the server-side condition operators.
2026-05-19 16:25:07 +03:00
Pekka Enberg
27ba5c5a29 bindings/javascript: Add concurrent transaction support
Expose a `concurrent` mode on the transaction wrapper alongside the
existing `deferred`, `immediate`, and `exclusive` modes, so users can
write `db.transaction(fn).concurrent()` to run the body under
`BEGIN CONCURRENT`.
2026-05-18 15:44:19 +03:00
Pekka Enberg
43f3f7bc86 serverless/javascript: 1.2.0-pre.1 2026-05-04 14:41:26 +03:00
Pekka Enberg
b8763de6df serverless/javascript: Add bind parameter support to Database API 2026-05-04 13:46:48 +03:00
Pekka Enberg
308392d9d5 serverless/javascript: 1.1.3 2026-05-02 08:46:10 +03:00
Akira Noda
faed3df7ee serverless/javascript: preserve zero lastInsertRowid 2026-04-30 12:35:57 +09:00
Pekka Enberg
fbf3a7939c serverless/javascript: Encode out-of-range integers as float
Number.isInteger(Number.MAX_VALUE) returns true, so encodeValue() sent
it as { type: 'integer', value: '1.7976931348623157e+308' }, which the
server rejected with HTTP 400 when parsing as i64. Switch to
Number.isSafeInteger so values outside ±2^53 fall through to the float
branch.
2026-04-28 11:33:39 +03:00
Pekka Enberg
03df12290e serverless/javascript: 1.1.2 2026-04-25 07:36:10 +03:00
Pekka Enberg
c7287f31f9 Merge 'serverless/javascript: Fix prepare() losing transaction baton' from Glauber Costa
Connection.prepare() created a throwaway Session for the describe call,
which had no baton and therefore ran on a separate server-side
connection. This meant it could not see uncommitted DDL (e.g. CREATE
TABLE) from the current transaction.
Use this.session instead and serialize through execLock, matching how
exec() and execute() already work.
Fixes #6562

Closes #6563
2026-04-25 07:35:33 +03:00
Glauber Costa
8430a26743 fix prepare() losing transaction baton in serverless driver
Connection.prepare() created a throwaway Session for the describe call,
which had no baton and therefore ran on a separate server-side connection.
This meant it could not see uncommitted DDL (e.g. CREATE TABLE) from the
current transaction.

Use this.session instead and serialize through execLock, matching how
exec() and execute() already work.

Fixes #6562

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-24 17:33:59 -04:00
Pekka Enberg
5eaef90bab serverless/javascript: 1.1.2-pre.1 2026-04-24 20:11:11 +03:00
Pekka Enberg
b2cff7b467 serverless/javascript: Add 'clean' target to package.json 2026-04-24 20:10:56 +03:00
Pekka Enberg
d1f8d0b7b2 serverless/javascript: Add packaging for ESM and CommonJS
We only packaged for ESM so far, but let's add CommonJS to the mix too.

Fixes #6537
2026-04-24 20:10:07 +03:00
Pekka Enberg
b8e9ac8903 Merge 'serverless/javascript: Fix empty blob decoding returning null' from Glauber Costa
decodeValue() returned null for blobs without a base64 field (e.g.
{"type":"blob"} with no base64 key). Now returns Buffer.alloc(0),
matching the turso NAPI driver behavior.

Closes #6555
2026-04-24 17:54:46 +03:00
Glauber Costa
2a4bae2286 fix empty blob decoding returning null in serverless driver
decodeValue() returned null for blobs without a base64 field (e.g.
{"type":"blob"} with no base64 key). Now returns Buffer.alloc(0),
matching the turso NAPI driver behavior.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-24 08:55:31 -04:00
Pekka Enberg
c7e6936adf serverless/javascript: 1.1.1 2026-04-24 13:52:13 +03:00
Pekka Enberg
a034730e0e Merge 'serverless/javascript: Fix empty blobs and falsy rowids' from Glauber Costa
The serverless driver diverged from the database driver in three cases:
- Empty blobs (base64 '') decoded to null instead of empty Buffer
- Unpadded base64 strings could fail in strict environments
- last_insert_rowid of 0 was dropped (falsy check), and numeric type
from the database driver was not handled
Add conformance tests to testing/javascript async test suite.
Used a new tool, Hegel, from Antithesis, here.

Closes #6549
2026-04-24 13:51:39 +03:00
Glauber Costa
2f9d46d76b move serverless protocol tests to unit tests that need no server
The previous tests were in async.test.js which runs against the
database binding (PROVIDER=turso), not the serverless driver. They
tested the wrong code path. Replace with direct unit tests of
encodeValue, decodeValue, processCursorEntries, and baton reset
in the serverless package.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-24 02:02:22 -04:00
Glauber Costa
5f89ba0bf5 fix serverless driver stale baton after HTTP error
When executeCursor() or executePipeline() throws due to an HTTP error,
the baton was never cleared. Subsequent requests sent the stale baton,
causing the server to reject them with HTTP 400.

Reset baton to null on error in executeRaw(), batch(), describe(), and
sequence().

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-24 02:00:03 -04:00
Glauber Costa
66e24ac59b fix serverless driver conformance for empty blobs and falsy rowids
The serverless driver diverged from the database driver in three cases:
- Empty blobs (base64 '') decoded to null instead of empty Buffer
- Unpadded base64 strings could fail in strict environments
- last_insert_rowid of 0 was dropped (falsy check), and numeric type
  from the database driver was not handled

Add conformance tests to testing/javascript async test suite.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-24 01:04:52 -04:00
dependabot[bot]
e8bb78c6b4 Bump glob from 10.4.5 to 10.5.0 in /serverless/javascript
Bumps [glob](https://github.com/isaacs/node-glob) from 10.4.5 to 10.5.0.
- [Changelog](https://github.com/isaacs/node-glob/blob/main/changelog.md)
- [Commits](https://github.com/isaacs/node-glob/compare/v10.4.5...v10.5.0)

---
updated-dependencies:
- dependency-name: glob
  dependency-version: 10.5.0
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
2026-04-22 14:20:05 +00:00
Pekka Enberg
cd1188384b serverless/javascript: 1.1.0 2026-04-18 15:25:50 +03:00
Pekka Enberg
c6031f58e1 serverless/javascript: 1.1.0-pre.2 2026-04-18 15:23:26 +03:00
Pekka Enberg
c4f85a8a54 serverless/javascript: Interactive transaction support
Prepared statements used to allocate their own session, so a BEGIN
issued on the Connection didn't actually bracket anything run through
them — each stmt.run()/get()/all() went out on an independent stream
under auto-commit. Code that looked transactional wasn't.

Connection.prepare() now hands statements the connection's session
via Statement.fromSession() and its exec lock, which they acquire
around each call. A BEGIN on the connection bounds every prepared-
statement call until COMMIT or ROLLBACK. iterate() would deadlock
nested queries on the same connection if it held the lock across
yields, so on shared-session statements it buffers via all().

Tests cover commit visibility across connections, rollback discarding
writes, and error-then-rollback-then-reuse.

The compat layer's client.transaction() — previously throwing
NOT_IMPLEMENTED — now builds on the same primitives and is cross-
validated against @libsql/client.
2026-04-18 14:49:12 +03:00
Pekka Enberg
cb42ca32a9 Update tar to 7.5.13 2026-04-07 15:44:09 +03:00
Pekka Enberg
10d9db4980 Update lodash to 4.18.1
Addresses GHSA-f23m-r3pf-42rh / CVE-2026-2950: lodash versions <= 4.17.23
are vulnerable to prototype pollution in `_.unset` and `_.omit`. The
prior fix for CVE-2025-13465 only guarded against string key members,
so an attacker can bypass it by passing array-wrapped path segments,
allowing deletion of properties from built-in prototypes such as
`Object.prototype`, `Number.prototype`, and `String.prototype`.

lodash is only used transitively by dev dependencies in our JS example
and test projects (ava, react-native toolchain), but bump it anyway to
clear the dependabot alert.
2026-04-06 13:19:07 +03:00