Merge 'Initial JavaScript bindings with napi-rs' from Pekka Enberg

Closes #1183
This commit is contained in:
Pekka Enberg
2025-03-26 13:36:19 +02:00
22 changed files with 4093 additions and 151 deletions

247
.github/workflows/napi.yml vendored Normal file
View File

@@ -0,0 +1,247 @@
name: JavaScript
on:
push:
branches:
- main
tags:
- v*
pull_request:
branches:
- main
env:
DEBUG: napi:*
APP_NAME: turso-limbo
MACOSX_DEPLOYMENT_TARGET: '10.13'
defaults:
run:
working-directory: bindings/javascript
jobs:
build:
timeout-minutes: 10
strategy:
fail-fast: false
matrix:
settings:
- host: macos-13
target: x86_64-apple-darwin
build: yarn build --target x86_64-apple-darwin
- host: windows-latest
build: |
yarn build --target x86_64-pc-windows-msvc
yarn test
target: x86_64-pc-windows-msvc
- host: ubuntu-latest
target: x86_64-unknown-linux-gnu
docker: ghcr.io/napi-rs/napi-rs/nodejs-rust:lts-debian
build: yarn build --target x86_64-unknown-linux-gnu
- host: macos-latest
target: aarch64-apple-darwin
build: yarn build --target aarch64-apple-darwin
name: stable - ${{ matrix.settings.target }} - node@20
runs-on: ${{ matrix.settings.host }}
steps:
- uses: actions/checkout@v4
- name: Setup node
uses: actions/setup-node@v4
if: ${{ !matrix.settings.docker }}
with:
node-version: 20
cache: yarn
cache-dependency-path: bindings/javascript
- name: Install
uses: dtolnay/rust-toolchain@stable
if: ${{ !matrix.settings.docker }}
with:
toolchain: stable
targets: ${{ matrix.settings.target }}
- name: Cache cargo
uses: actions/cache@v4
with:
path: |
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
.cargo-cache
target/
key: ${{ matrix.settings.target }}-cargo-${{ matrix.settings.host }}
- uses: goto-bus-stop/setup-zig@v2
if: ${{ matrix.settings.target == 'armv7-unknown-linux-gnueabihf' || matrix.settings.target == 'armv7-unknown-linux-musleabihf' }}
with:
version: 0.13.0
- name: Setup toolchain
run: ${{ matrix.settings.setup }}
if: ${{ matrix.settings.setup }}
shell: bash
- name: Install dependencies
run: yarn install
- name: Setup node x86
uses: actions/setup-node@v4
if: matrix.settings.target == 'x86_64-pc-windows-msvc'
with:
node-version: 20
# cache: yarn
architecture: x64
# cache-dependency-path: bindings/javascript
- name: Build in docker
uses: addnab/docker-run-action@v3
if: ${{ matrix.settings.docker }}
with:
image: ${{ matrix.settings.docker }}
options: '--user 0:0 -v ${{ github.workspace }}/.cargo-cache/git/db:/usr/local/cargo/git/db -v ${{ github.workspace }}/.cargo/registry/cache:/usr/local/cargo/registry/cache -v ${{ github.workspace }}/.cargo/registry/index:/usr/local/cargo/registry/index -v ${{ github.workspace }}:/build -w /build/bindings/javascript'
run: ${{ matrix.settings.build }}
- name: Build
run: ${{ matrix.settings.build }}
if: ${{ !matrix.settings.docker }}
shell: bash
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: bindings-${{ matrix.settings.target }}
path: bindings/javascript/${{ env.APP_NAME }}.*.node
if-no-files-found: error
test-macOS-windows-binding:
name: Test bindings on ${{ matrix.settings.target }} - node@${{ matrix.node }}
needs:
- build
strategy:
fail-fast: false
matrix:
settings:
- host: macos-13
target: x86_64-apple-darwin
node:
- '18'
- '20'
runs-on: ${{ matrix.settings.host }}
steps:
- uses: actions/checkout@v4
- name: Setup node
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
cache: yarn
cache-dependency-path: bindings/javascript
architecture: x64
- name: Install dependencies
run: yarn install
- name: Download artifacts
uses: actions/download-artifact@v4
with:
name: bindings-${{ matrix.settings.target }}
path: bindings/javascript
- name: List packages
run: ls -R .
shell: bash
- name: Test bindings
run: yarn test
test-linux-x64-gnu-binding:
name: Test bindings on Linux-x64-gnu - node@${{ matrix.node }}
needs:
- build
strategy:
fail-fast: false
matrix:
node:
- '18'
- '20'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup node
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
cache: yarn
cache-dependency-path: bindings/javascript
- name: Install dependencies
run: yarn install
- name: Download artifacts
uses: actions/download-artifact@v4
with:
name: bindings-x86_64-unknown-linux-gnu
path: bindings/javascript
- name: List packages
run: ls -R .
shell: bash
- name: Test bindings
run: docker run --rm -v $(pwd):/build -w /build node:${{ matrix.node }}-slim yarn test
universal-macOS:
name: Build universal macOS binary
needs:
- build
runs-on: macos-latest
steps:
- uses: actions/checkout@v4
- name: Setup node
uses: actions/setup-node@v4
with:
node-version: 20
cache: yarn
cache-dependency-path: bindings/javascript
- name: Install dependencies
run: yarn install
- name: Download macOS x64 artifact
uses: actions/download-artifact@v4
with:
name: bindings-x86_64-apple-darwin
path: bindings/javascript/artifacts
- name: Download macOS arm64 artifact
uses: actions/download-artifact@v4
with:
name: bindings-aarch64-apple-darwin
path: bindings/javascript/artifacts
- name: Combine binaries
run: yarn universal
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: bindings-universal-apple-darwin
path: bindings/javascript/${{ env.APP_NAME }}.*.node
if-no-files-found: error
publish:
name: Publish
runs-on: ubuntu-latest
needs:
- test-macOS-windows-binding
- test-linux-x64-gnu-binding
- universal-macOS
steps:
- uses: actions/checkout@v4
- name: Setup node
uses: actions/setup-node@v4
with:
node-version: 20
cache: yarn
cache-dependency-path: bindings/javascript
- name: Install dependencies
run: yarn install
- name: Download all artifacts
uses: actions/download-artifact@v4
with:
path: bindings/javascript/artifacts
- name: Move artifacts
run: yarn artifacts
- name: List packages
run: ls -R ./npm
shell: bash
- name: Publish
run: |
npm config set provenance true
if git log -1 --pretty=%B | grep "^[0-9]\+\.[0-9]\+\.[0-9]\+$";
then
echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc
npm publish --access public
elif git log -1 --pretty=%B | grep "^[0-9]\+\.[0-9]\+\.[0-9]\+";
then
echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc
npm publish --tag next --access public
else
echo "Not a release, skipping publish"
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

451
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -5,6 +5,7 @@ resolver = "2"
members = [
"bindings/go",
"bindings/java",
"bindings/javascript",
"bindings/python",
"bindings/rust",
"bindings/wasm",

197
bindings/javascript/.gitignore vendored Normal file
View File

@@ -0,0 +1,197 @@
# Created by https://www.toptal.com/developers/gitignore/api/node
# Edit at https://www.toptal.com/developers/gitignore?templates=node
### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# TypeScript v1 declaration files
typings/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
.env.test
# parcel-bundler cache (https://parceljs.org/)
.cache
# Next.js build output
.next
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
# Stores VSCode versions used for testing VSCode extensions
.vscode-test
# End of https://www.toptal.com/developers/gitignore/api/node
# Created by https://www.toptal.com/developers/gitignore/api/macos
# Edit at https://www.toptal.com/developers/gitignore?templates=macos
### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
### macOS Patch ###
# iCloud generated files
*.icloud
# End of https://www.toptal.com/developers/gitignore/api/macos
# Created by https://www.toptal.com/developers/gitignore/api/windows
# Edit at https://www.toptal.com/developers/gitignore?templates=windows
### Windows ###
# Windows thumbnail cache files
Thumbs.db
Thumbs.db:encryptable
ehthumbs.db
ehthumbs_vista.db
# Dump file
*.stackdump
# Folder config file
[Dd]esktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/
# Windows Installer files
*.cab
*.msi
*.msix
*.msm
*.msp
# Windows shortcuts
*.lnk
# End of https://www.toptal.com/developers/gitignore/api/windows
#Added by cargo
/target
Cargo.lock
.pnp.*
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions
*.node

View File

@@ -0,0 +1,13 @@
target
Cargo.lock
.cargo
.github
npm
.eslintrc
.prettierignore
rustfmt.toml
yarn.lock
*.node
.yarn
__test__
renovate.json

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,3 @@
nodeLinker: node-modules
yarnPath: .yarn/releases/yarn-4.6.0.cjs

View File

@@ -0,0 +1,19 @@
[package]
name = "limbo_node"
version.workspace = true
authors.workspace = true
edition.workspace = true
license.workspace = true
repository.workspace = true
description = "Limbo Rust API"
[lib]
crate-type = ["cdylib"]
[dependencies]
limbo_core = { workspace = true }
napi = { version = "2.12.2", default-features = false, features = ["napi4"] }
napi-derive = "2.12.2"
[build-dependencies]
napi-build = "2.0.1"

View File

@@ -0,0 +1,20 @@
import test from "ava";
import { Database } from "../index.js";
test.serial("Open in-memory database", async (t) => {
const [db] = await connect(":memory:");
t.is(db.memory, true);
});
test.serial("Statement.get()", async (t) => {
const [db] = await connect(":memory:");
const stmt = db.prepare("SELECT 1");
const result = stmt.get();
t.is(result["1"], 1);
});
const connect = async (path) => {
const db = new Database(path);
return [db];
};

View File

@@ -0,0 +1,5 @@
extern crate napi_build;
fn main() {
napi_build::setup();
}

View File

@@ -0,0 +1,171 @@
# class Database
The `Database` class represents a connection that can prepare and execute SQL statements.
## Methods
### new Database(path, [options]) ⇒ Database
Creates a new database connection.
| Param | Type | Description |
| ------- | ------------------- | ------------------------- |
| path | <code>string</code> | Path to the database file |
| options | <code>object</code> | Options. |
The `path` parameter points to the SQLite database file to open. If the file pointed to by `path` does not exists, it will be created.
To open an in-memory database, please pass `:memory:` as the `path` parameter.
The function returns a `Database` object.
### prepare(sql) ⇒ Statement
Prepares a SQL statement for execution.
| Param | Type | Description |
| ------ | ------------------- | ------------------------------------ |
| sql | <code>string</code> | The SQL statement string to prepare. |
The function returns a `Statement` object.
This function is currently not supported.
### transaction(function) ⇒ function
Returns a function that runs the given function in a transaction.
| Param | Type | Description |
| -------- | --------------------- | ------------------------------------- |
| function | <code>function</code> | The function to run in a transaction. |
This function is currently not supported.
### pragma(string, [options]) ⇒ results
This function is currently not supported.
### backup(destination, [options]) ⇒ promise
This function is currently not supported.
### serialize([options]) ⇒ Buffer
This function is currently not supported.
### function(name, [options], function) ⇒ this
This function is currently not supported.
### aggregate(name, options) ⇒ this
This function is currently not supported.
### table(name, definition) ⇒ this
This function is currently not supported.
### loadExtension(path, [entryPoint]) ⇒ this
Loads a SQLite3 extension
This function is currently not supported.
### exec(sql) ⇒ this
Executes a SQL statement.
| Param | Type | Description |
| ------ | ------------------- | ------------------------------------ |
| sql | <code>string</code> | The SQL statement string to execute. |
This function is currently not supported.
### interrupt() ⇒ this
Cancel ongoing operations and make them return at earliest opportunity.
**Note:** This is an extension in libSQL and not available in `better-sqlite3`.
This function is currently not supported.
### close() ⇒ this
Closes the database connection.
This function is currently not supported.
# class Statement
## Methods
### run([...bindParameters]) ⇒ object
Executes the SQL statement and returns an info object.
| Param | Type | Description |
| -------------- | ----------------------------- | ------------------------------------------------ |
| bindParameters | <code>array of objects</code> | The bind parameters for executing the statement. |
The returned info object contains two properties: `changes` that describes the number of modified rows and `info.lastInsertRowid` that represents the `rowid` of the last inserted row.
This function is currently not supported.
### get([...bindParameters]) ⇒ row
Executes the SQL statement and returns the first row.
| Param | Type | Description |
| -------------- | ----------------------------- | ------------------------------------------------ |
| bindParameters | <code>array of objects</code> | The bind parameters for executing the statement. |
This function is currently not supported.
### all([...bindParameters]) ⇒ array of rows
Executes the SQL statement and returns an array of the resulting rows.
| Param | Type | Description |
| -------------- | ----------------------------- | ------------------------------------------------ |
| bindParameters | <code>array of objects</code> | The bind parameters for executing the statement. |
This function is currently not supported.
### iterate([...bindParameters]) ⇒ iterator
Executes the SQL statement and returns an iterator to the resulting rows.
| Param | Type | Description |
| -------------- | ----------------------------- | ------------------------------------------------ |
| bindParameters | <code>array of objects</code> | The bind parameters for executing the statement. |
This function is currently not supported.
### pluck([toggleState]) ⇒ this
This function is currently not supported.
### expand([toggleState]) ⇒ this
This function is currently not supported.
### raw([rawMode]) ⇒ this
Toggle raw mode.
| Param | Type | Description |
| ------- | -------------------- | --------------------------------------------------------------------------------- |
| rawMode | <code>boolean</code> | Enable or disable raw mode. If you don't pass the parameter, raw mode is enabled. |
This function enables or disables raw mode. Prepared statements return objects by default, but if raw mode is enabled, the functions return arrays instead.
This function is currently not supported.
### columns() ⇒ array of objects
Returns the columns in the result set returned by this prepared statement.
This function is currently not supported.
### bind([...bindParameters]) ⇒ this
This function is currently not supported.

13
bindings/javascript/index.d.ts vendored Normal file
View File

@@ -0,0 +1,13 @@
/* tslint:disable */
/* eslint-disable */
/* auto-generated by NAPI-RS */
export declare class Database {
memory: boolean
constructor(path: string)
prepare(sql: string): Statement
}
export declare class Statement {
get(): NapiResult
}

View File

@@ -0,0 +1,316 @@
/* tslint:disable */
/* eslint-disable */
/* prettier-ignore */
/* auto-generated by NAPI-RS */
const { existsSync, readFileSync } = require('fs')
const { join } = require('path')
const { platform, arch } = process
let nativeBinding = null
let localFileExisted = false
let loadError = null
function isMusl() {
// For Node 10
if (!process.report || typeof process.report.getReport !== 'function') {
try {
const lddPath = require('child_process').execSync('which ldd').toString().trim()
return readFileSync(lddPath, 'utf8').includes('musl')
} catch (e) {
return true
}
} else {
const { glibcVersionRuntime } = process.report.getReport().header
return !glibcVersionRuntime
}
}
switch (platform) {
case 'android':
switch (arch) {
case 'arm64':
localFileExisted = existsSync(join(__dirname, 'turso-limbo.android-arm64.node'))
try {
if (localFileExisted) {
nativeBinding = require('./turso-limbo.android-arm64.node')
} else {
nativeBinding = require('@tursodatabase/limbo-android-arm64')
}
} catch (e) {
loadError = e
}
break
case 'arm':
localFileExisted = existsSync(join(__dirname, 'turso-limbo.android-arm-eabi.node'))
try {
if (localFileExisted) {
nativeBinding = require('./turso-limbo.android-arm-eabi.node')
} else {
nativeBinding = require('@tursodatabase/limbo-android-arm-eabi')
}
} catch (e) {
loadError = e
}
break
default:
throw new Error(`Unsupported architecture on Android ${arch}`)
}
break
case 'win32':
switch (arch) {
case 'x64':
localFileExisted = existsSync(
join(__dirname, 'turso-limbo.win32-x64-msvc.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./turso-limbo.win32-x64-msvc.node')
} else {
nativeBinding = require('@tursodatabase/limbo-win32-x64-msvc')
}
} catch (e) {
loadError = e
}
break
case 'ia32':
localFileExisted = existsSync(
join(__dirname, 'turso-limbo.win32-ia32-msvc.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./turso-limbo.win32-ia32-msvc.node')
} else {
nativeBinding = require('@tursodatabase/limbo-win32-ia32-msvc')
}
} catch (e) {
loadError = e
}
break
case 'arm64':
localFileExisted = existsSync(
join(__dirname, 'turso-limbo.win32-arm64-msvc.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./turso-limbo.win32-arm64-msvc.node')
} else {
nativeBinding = require('@tursodatabase/limbo-win32-arm64-msvc')
}
} catch (e) {
loadError = e
}
break
default:
throw new Error(`Unsupported architecture on Windows: ${arch}`)
}
break
case 'darwin':
localFileExisted = existsSync(join(__dirname, 'turso-limbo.darwin-universal.node'))
try {
if (localFileExisted) {
nativeBinding = require('./turso-limbo.darwin-universal.node')
} else {
nativeBinding = require('@tursodatabase/limbo-darwin-universal')
}
break
} catch {}
switch (arch) {
case 'x64':
localFileExisted = existsSync(join(__dirname, 'turso-limbo.darwin-x64.node'))
try {
if (localFileExisted) {
nativeBinding = require('./turso-limbo.darwin-x64.node')
} else {
nativeBinding = require('@tursodatabase/limbo-darwin-x64')
}
} catch (e) {
loadError = e
}
break
case 'arm64':
localFileExisted = existsSync(
join(__dirname, 'turso-limbo.darwin-arm64.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./turso-limbo.darwin-arm64.node')
} else {
nativeBinding = require('@tursodatabase/limbo-darwin-arm64')
}
} catch (e) {
loadError = e
}
break
default:
throw new Error(`Unsupported architecture on macOS: ${arch}`)
}
break
case 'freebsd':
if (arch !== 'x64') {
throw new Error(`Unsupported architecture on FreeBSD: ${arch}`)
}
localFileExisted = existsSync(join(__dirname, 'turso-limbo.freebsd-x64.node'))
try {
if (localFileExisted) {
nativeBinding = require('./turso-limbo.freebsd-x64.node')
} else {
nativeBinding = require('@tursodatabase/limbo-freebsd-x64')
}
} catch (e) {
loadError = e
}
break
case 'linux':
switch (arch) {
case 'x64':
if (isMusl()) {
localFileExisted = existsSync(
join(__dirname, 'turso-limbo.linux-x64-musl.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./turso-limbo.linux-x64-musl.node')
} else {
nativeBinding = require('@tursodatabase/limbo-linux-x64-musl')
}
} catch (e) {
loadError = e
}
} else {
localFileExisted = existsSync(
join(__dirname, 'turso-limbo.linux-x64-gnu.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./turso-limbo.linux-x64-gnu.node')
} else {
nativeBinding = require('@tursodatabase/limbo-linux-x64-gnu')
}
} catch (e) {
loadError = e
}
}
break
case 'arm64':
if (isMusl()) {
localFileExisted = existsSync(
join(__dirname, 'turso-limbo.linux-arm64-musl.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./turso-limbo.linux-arm64-musl.node')
} else {
nativeBinding = require('@tursodatabase/limbo-linux-arm64-musl')
}
} catch (e) {
loadError = e
}
} else {
localFileExisted = existsSync(
join(__dirname, 'turso-limbo.linux-arm64-gnu.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./turso-limbo.linux-arm64-gnu.node')
} else {
nativeBinding = require('@tursodatabase/limbo-linux-arm64-gnu')
}
} catch (e) {
loadError = e
}
}
break
case 'arm':
if (isMusl()) {
localFileExisted = existsSync(
join(__dirname, 'turso-limbo.linux-arm-musleabihf.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./turso-limbo.linux-arm-musleabihf.node')
} else {
nativeBinding = require('@tursodatabase/limbo-linux-arm-musleabihf')
}
} catch (e) {
loadError = e
}
} else {
localFileExisted = existsSync(
join(__dirname, 'turso-limbo.linux-arm-gnueabihf.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./turso-limbo.linux-arm-gnueabihf.node')
} else {
nativeBinding = require('@tursodatabase/limbo-linux-arm-gnueabihf')
}
} catch (e) {
loadError = e
}
}
break
case 'riscv64':
if (isMusl()) {
localFileExisted = existsSync(
join(__dirname, 'turso-limbo.linux-riscv64-musl.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./turso-limbo.linux-riscv64-musl.node')
} else {
nativeBinding = require('@tursodatabase/limbo-linux-riscv64-musl')
}
} catch (e) {
loadError = e
}
} else {
localFileExisted = existsSync(
join(__dirname, 'turso-limbo.linux-riscv64-gnu.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./turso-limbo.linux-riscv64-gnu.node')
} else {
nativeBinding = require('@tursodatabase/limbo-linux-riscv64-gnu')
}
} catch (e) {
loadError = e
}
}
break
case 's390x':
localFileExisted = existsSync(
join(__dirname, 'turso-limbo.linux-s390x-gnu.node')
)
try {
if (localFileExisted) {
nativeBinding = require('./turso-limbo.linux-s390x-gnu.node')
} else {
nativeBinding = require('@tursodatabase/limbo-linux-s390x-gnu')
}
} catch (e) {
loadError = e
}
break
default:
throw new Error(`Unsupported architecture on Linux: ${arch}`)
}
break
default:
throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`)
}
if (!nativeBinding) {
if (loadError) {
throw loadError
}
throw new Error(`Failed to load native binding`)
}
const { Database, Statement } = nativeBinding
module.exports.Database = Database
module.exports.Statement = Statement

View File

@@ -0,0 +1,3 @@
# `limbo-darwin-universal`
This is the **universal-apple-darwin** binary for `limbo`

View File

@@ -0,0 +1,15 @@
{
"name": "@tursodatabase/limbo-darwin-universal",
"version": "0.0.0",
"os": [
"darwin"
],
"main": "turso-limbo.darwin-universal.node",
"files": [
"turso-limbo.darwin-universal.node"
],
"license": "MIT",
"engines": {
"node": ">= 10"
}
}

View File

@@ -0,0 +1,3 @@
# `limbo-linux-x64-gnu`
This is the **x86_64-unknown-linux-gnu** binary for `limbo`

View File

@@ -0,0 +1,21 @@
{
"name": "@tursodatabase/limbo-linux-x64-gnu",
"version": "0.0.0",
"os": [
"linux"
],
"cpu": [
"x64"
],
"main": "turso-limbo.linux-x64-gnu.node",
"files": [
"turso-limbo.linux-x64-gnu.node"
],
"license": "MIT",
"engines": {
"node": ">= 10"
},
"libc": [
"glibc"
]
}

View File

@@ -0,0 +1,3 @@
# `turso-limbo-win32-x64-msvc`
This is the **x86_64-pc-windows-msvc** binary for `@tursodatabase/limbo`

View File

@@ -0,0 +1,18 @@
{
"name": "@tursodatabase/limbo-win32-x64-msvc",
"version": "0.0.0",
"os": [
"win32"
],
"cpu": [
"x64"
],
"main": "turso-limbo.win32-x64-msvc.node",
"files": [
"turso-limbo.win32-x64-msvc.node"
],
"license": "MIT",
"engines": {
"node": ">= 10"
}
}

View File

@@ -0,0 +1,39 @@
{
"name": "@tursodatabase/limbo",
"version": "0.0.17",
"description": "The Limbo database library",
"main": "index.js",
"types": "index.d.ts",
"napi": {
"name": "turso-limbo",
"triples": {
"defaults": false,
"additional": [
"x86_64-unknown-linux-gnu",
"x86_64-pc-windows-msvc",
"universal-apple-darwin"
]
}
},
"license": "MIT",
"devDependencies": {
"@napi-rs/cli": "^2.18.4",
"ava": "^6.0.1"
},
"ava": {
"timeout": "3m"
},
"engines": {
"node": ">= 10"
},
"scripts": {
"artifacts": "napi artifacts",
"build": "napi build --platform --release",
"build:debug": "napi build --platform",
"prepublishOnly": "napi prepublish -t npm",
"test": "ava",
"universal": "napi universal",
"version": "napi version"
},
"packageManager": "yarn@4.6.0"
}

View File

@@ -0,0 +1,177 @@
#![deny(clippy::all)]
use std::cell::RefCell;
use std::rc::Rc;
use std::sync::Arc;
use napi::{Env, JsObject, JsUnknown, Result as NapiResult};
use napi_derive::napi;
#[napi(js_name = "Database")]
pub struct Database {
#[napi(writable = false)]
pub memory: bool,
_db: Arc<limbo_core::Database>,
conn: Rc<limbo_core::Connection>,
}
#[napi]
impl Database {
#[napi(constructor)]
pub fn new(path: String) -> Self {
let memory = path == ":memory:";
let io: Arc<dyn limbo_core::IO> = if memory {
Arc::new(limbo_core::MemoryIO::new())
} else {
Arc::new(IO {})
};
let file = io
.open_file(&path, limbo_core::OpenFlags::Create, false)
.unwrap();
limbo_core::maybe_init_database_file(&file, &io).unwrap();
let db_file = Arc::new(DatabaseFile::new(file));
let db_header = limbo_core::Pager::begin_open(db_file.clone()).unwrap();
// ensure db header is there
io.run_once().unwrap();
let page_size = db_header.lock().page_size;
let wal_path = format!("{}-wal", path);
let wal_shared =
limbo_core::WalFileShared::open_shared(&io, wal_path.as_str(), page_size).unwrap();
let db = limbo_core::Database::open(io, db_file, wal_shared, false).unwrap();
let conn = db.connect().unwrap();
Self {
memory,
_db: db,
conn,
}
}
#[napi]
pub fn prepare(&self, sql: String) -> Statement {
let stmt = self.conn.prepare(&sql).unwrap();
Statement::new(RefCell::new(stmt))
}
}
#[napi(js_name = "Statement")]
pub struct Statement {
inner: RefCell<limbo_core::Statement>,
}
#[napi]
impl Statement {
pub fn new(inner: RefCell<limbo_core::Statement>) -> Self {
Self { inner }
}
#[napi]
pub fn get(&self, env: Env) -> NapiResult<JsObject> {
let mut stmt = self.inner.borrow_mut();
match stmt.step() {
Ok(limbo_core::StepResult::Row) => {
let row = stmt.row().unwrap();
let mut obj = env.create_object()?;
for (idx, value) in row.get_values().iter().enumerate() {
let key = (idx + 1).to_string();
let js_value = to_js_value(&env, value);
obj.set_named_property(&key, js_value)?;
}
Ok(obj)
}
Ok(limbo_core::StepResult::IO)
| Ok(limbo_core::StepResult::Done)
| Ok(limbo_core::StepResult::Interrupt)
| Ok(limbo_core::StepResult::Busy) => Ok(env.create_object()?),
Err(e) => Err(napi::Error::from_reason(format!("Database error: {:?}", e))),
}
}
}
fn to_js_value(env: &napi::Env, value: &limbo_core::OwnedValue) -> JsUnknown {
match value {
limbo_core::OwnedValue::Null => env.get_null().unwrap().into_unknown(),
limbo_core::OwnedValue::Integer(i) => env.create_int64(*i).unwrap().into_unknown(),
limbo_core::OwnedValue::Float(f) => env.create_double(*f).unwrap().into_unknown(),
limbo_core::OwnedValue::Text(s) => env.create_string(s.as_str()).unwrap().into_unknown(),
limbo_core::OwnedValue::Blob(b) => {
env.create_buffer_copy(b.as_ref()).unwrap().into_unknown()
}
_ => env.get_null().unwrap().into_unknown(),
}
}
struct DatabaseFile {
file: Arc<dyn limbo_core::File>,
}
unsafe impl Send for DatabaseFile {}
unsafe impl Sync for DatabaseFile {}
impl DatabaseFile {
pub fn new(file: Arc<dyn limbo_core::File>) -> Self {
Self { file }
}
}
impl limbo_core::DatabaseStorage for DatabaseFile {
fn read_page(&self, page_idx: usize, c: limbo_core::Completion) -> limbo_core::Result<()> {
let r = match c {
limbo_core::Completion::Read(ref r) => r,
_ => unreachable!(),
};
let size = r.buf().len();
assert!(page_idx > 0);
if !(512..=65536).contains(&size) || size & (size - 1) != 0 {
return Err(limbo_core::LimboError::NotADB);
}
let pos = (page_idx - 1) * size;
self.file.pread(pos, c)?;
Ok(())
}
fn write_page(
&self,
page_idx: usize,
buffer: Arc<std::cell::RefCell<limbo_core::Buffer>>,
c: limbo_core::Completion,
) -> limbo_core::Result<()> {
let size = buffer.borrow().len();
let pos = (page_idx - 1) * size;
self.file.pwrite(pos, buffer, c)?;
Ok(())
}
fn sync(&self, _c: limbo_core::Completion) -> limbo_core::Result<()> {
todo!()
}
}
struct IO {}
impl limbo_core::IO for IO {
fn open_file(
&self,
_path: &str,
_flags: limbo_core::OpenFlags,
_direct: bool,
) -> limbo_core::Result<Arc<dyn limbo_core::File>> {
todo!();
}
fn run_once(&self) -> limbo_core::Result<()> {
todo!();
}
fn generate_random_number(&self) -> i64 {
todo!();
}
fn get_current_time(&self) -> String {
todo!();
}
}

File diff suppressed because it is too large Load Diff