-
Notifications
You must be signed in to change notification settings - Fork 0
Add @gtbuchanan/libsql-termux-shim package #338
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| --- | ||
| '@gtbuchanan/libsql-termux-shim': minor | ||
| --- | ||
|
|
||
| Add `@gtbuchanan/libsql-termux-shim`, a stand-in for libsql's native binding | ||
| implemented on `node:sqlite`. libsql publishes no `@libsql/android-arm64`, so | ||
| dependents such as promptfoo fail at startup on Termux; aliasing the missing | ||
| target to this package lets them run. Local databases only — embedded replicas | ||
| throw. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| # @gtbuchanan/libsql-termux-shim | ||
|
|
||
| Stand-in for [libsql](https://github.com/tursodatabase/libsql-js)'s native | ||
| binding on Termux/Android, implemented on Node's built-in `node:sqlite`. | ||
|
|
||
| ## Why | ||
|
|
||
| libsql publishes prebuilt bindings for darwin, linux, and win32 only — there is | ||
| no `@libsql/android-arm64`. Anything that reaches libsql therefore fails at | ||
| startup on Termux. [promptfoo](https://promptfoo.dev) is the motivating case: its | ||
| results database is mandatory and reached through drizzle, so it dies before | ||
| running a single eval. | ||
|
|
||
| ```text | ||
| Database migration failed: Cannot find module '@libsql/android-arm64' | ||
| ``` | ||
|
|
||
| Node 24 ships SQLite in core, so the binding can be reimplemented in JavaScript | ||
| rather than cross-compiled. | ||
|
|
||
| ## Install | ||
|
|
||
| Alias the missing target, gated to Android so every other platform keeps the real | ||
| binding: | ||
|
|
||
| ```json | ||
| { | ||
| "optionalDependencies": { | ||
| "@libsql/android-arm64": "npm:@gtbuchanan/libsql-termux-shim@^0.1.0" | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| Node resolves `require("@libsql/android-arm64")` from inside libsql by walking up | ||
| to the project's `node_modules`, so a root-level install is found even under | ||
| pnpm's isolated layout. | ||
|
|
||
| `libsql` itself declares an `os` allowlist that omits android, which makes pnpm | ||
| refuse to resolve the workspace before it ever reaches this shim. Ungate it in | ||
| `.pnpmfile.cjs` — in both hooks, since `readPackage` ungates resolution while | ||
| `afterAllResolved` drops the field pnpm records in the lockfile and rechecks on | ||
| every later install: | ||
|
|
||
| ```js | ||
| const readPackage = (pkg) => { | ||
| if (pkg.name !== 'libsql') return pkg; | ||
|
|
||
| const { os, ...ungated } = pkg; | ||
| return ungated; | ||
| }; | ||
|
|
||
| /* Lockfile keys are `<name>@<version>`, and the name may itself be scoped. */ | ||
| const nameOf = (id) => id.slice(0, id.lastIndexOf('@')); | ||
|
|
||
| const afterAllResolved = (lockFile) => { | ||
| for (const [id, pkg] of Object.entries(lockFile.packages ?? {})) { | ||
| if (nameOf(id) === 'libsql') delete pkg.os; | ||
| } | ||
|
|
||
| return lockFile; | ||
| }; | ||
|
|
||
| module.exports = { hooks: { afterAllResolved, readPackage } }; | ||
| ``` | ||
|
|
||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| ## Scope | ||
|
|
||
| Local databases only. Embedded replicas (`syncUrl`, `sync()`, `syncUntil()`) need | ||
| libsql's replication protocol, which has no `node:sqlite` equivalent, so those | ||
| entry points throw rather than silently misbehave. | ||
|
|
||
| Extension loading throws for a different reason: libsql permits it per call, but | ||
| `node:sqlite` decides it when the connection is constructed. Opting every database | ||
| in to `allowExtension` so an unused call could work would trade real capability | ||
| for hypothetical fidelity, so the shim reports the gap instead. | ||
|
|
||
| ## How it works | ||
|
|
||
| libsql's JS wrapper calls each native function as `fn.call(handle, ...)`, where | ||
| the handle is whatever `databaseOpen` or `databasePrepareSync` returned. Both | ||
| sides belong to the binding, so plain objects serve as handles. | ||
| `node:sqlite`'s `DatabaseSync`/`StatementSync` are synchronous like libsql's | ||
| `*Sync` natives, and `run()` already returns libsql's | ||
| `{ changes, lastInsertRowid }` shape, so most of the mapping is direct. | ||
|
|
||
| Three places where it isn't, all load-bearing: | ||
|
|
||
| - **`raw(true)` must throw for a statement returning no columns.** | ||
| `@libsql/client` calls it inside a `try`/`catch` purely to detect whether a | ||
| statement yields rows, routing `BEGIN`/`COMMIT`/`INSERT` to `run()` when it | ||
| throws. Succeeding sends `BEGIN` down the query path, and the client then fails | ||
| the batch with `TRANSACTION_CLOSED`. | ||
| - **Transaction state is tracked from the SQL.** `node:sqlite` exposes no | ||
| autocommit flag. Both entry points matter: libsql's own `transaction()` issues | ||
| `BEGIN` through `exec()`, while `@libsql/client` issues it through | ||
| `prepare().run()`. | ||
| - **Rows are rebuilt as plain objects.** `node:sqlite` returns null-prototype | ||
| rows where the real binding returns ordinary ones, a difference that otherwise | ||
| leaks to anything inspecting a row's prototype. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import { configure } from '@gtbuchanan/eslint-config'; | ||
|
|
||
| export default configure({ | ||
| tsconfigRootDir: import.meta.dirname, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| { | ||
| "name": "@gtbuchanan/libsql-termux-shim", | ||
| "version": "0.0.0", | ||
| "description": "libsql native-binding stand-in for Termux/Android, implemented on node:sqlite, so libsql-dependent tools (e.g. promptfoo) run where libsql ships no prebuilt binding", | ||
| "homepage": "https://github.com/gtbuchanan/tooling/tree/main/packages/libsql-termux-shim", | ||
| "bugs": "https://github.com/gtbuchanan/tooling/issues", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/gtbuchanan/tooling.git", | ||
| "directory": "packages/libsql-termux-shim" | ||
| }, | ||
| "license": "MIT", | ||
| "type": "module", | ||
| "imports": { | ||
| "#src/*": "./src/*" | ||
| }, | ||
| "main": "./src/index.cjs", | ||
| "files": [ | ||
| "src" | ||
| ], | ||
| "scripts": { | ||
| "compile:ts": "node --experimental-strip-types scripts/build.ts", | ||
| "coverage:codecov:upload": "pnpm run gtb task coverage:codecov:upload", | ||
| "coverage:vitest:merge": "pnpm run gtb task coverage:vitest:merge", | ||
| "gtb": "node --experimental-strip-types ../../packages/cli/bin/gtb.ts", | ||
| "lint:eslint": "pnpm run gtb task lint:eslint", | ||
| "pack:npm": "pnpm run gtb task pack:npm", | ||
| "test:vitest:fast": "pnpm run gtb task test:vitest:fast", | ||
| "test:vitest:slow": "pnpm run gtb task test:vitest:slow", | ||
| "typecheck:ts": "pnpm run gtb task typecheck:ts" | ||
| }, | ||
| "devDependencies": { | ||
| "@gtbuchanan/eslint-config": "workspace:*", | ||
| "@gtbuchanan/tsconfig": "workspace:*", | ||
| "@gtbuchanan/vitest-config": "workspace:*" | ||
| }, | ||
| "engines": { | ||
| "node": ">=24.0.0" | ||
| }, | ||
| "publishConfig": { | ||
| "directory": "dist/source", | ||
| "linkDirectory": false, | ||
| "os": [ | ||
| "android" | ||
| ] | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| /** | ||
| * Builds the publishable layout under `dist/source/`. The shim is hand-written | ||
| * CommonJS rather than compiled TypeScript — it has to be `require()`-able by | ||
| * libsql's own CJS wrapper — so this copies it verbatim instead of running tsc. | ||
| * `publishConfig.directory` is `dist/source`, so `package.json`'s | ||
| * `./src/index.cjs` main resolves identically in the workspace and the tarball. | ||
| * Mirrors the convention used by other non-TypeScript packages in this repo | ||
| * (e.g. `@gtbuchanan/pnpm-termux-shim`). | ||
| */ | ||
|
|
||
| import { copyFileSync, mkdirSync } from 'node:fs'; | ||
| import path from 'node:path'; | ||
|
|
||
| const pkgDir = path.join(import.meta.dirname, '..'); | ||
| const outSrcDir = path.join(pkgDir, 'dist', 'source', 'src'); | ||
|
|
||
| mkdirSync(outSrcDir, { recursive: true }); | ||
| copyFileSync( | ||
| path.join(pkgDir, 'src', 'index.cjs'), | ||
| path.join(outSrcDir, 'index.cjs'), | ||
| ); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.