Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@libsql/client

Package Overview
Dependencies
Maintainers
3
Versions
93
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@libsql/client - npm Package Compare versions

Comparing version 0.2.1 to 0.2.2

6

lib-cjs/config.js

@@ -64,2 +64,7 @@ "use strict";

}
const intMode = "" + (config.intMode ?? "number");
if (intMode !== "number" && intMode !== "bigint" && intMode !== "string") {
throw new TypeError(`Invalid value for intMode, expected "number", "bigint" or "string", \
got ${JSON.stringify(intMode)}`);
}
return {

@@ -71,4 +76,5 @@ scheme,

authToken,
intMode,
};
}
exports.expandConfig = expandConfig;

7

lib-cjs/http.js

@@ -46,3 +46,3 @@ "use strict";

throw new api_js_1.LibsqlError('The HTTP client supports only "libsql:", "https:" and "http:" URLs, ' +
`got ${JSON.stringify(config.scheme)}. For more information, please read ${util_js_1.supportedUrlLink}`, "URL_SCHEME_NOT_SUPPORTED");
`got ${JSON.stringify(config.scheme + ":")}. For more information, please read ${util_js_1.supportedUrlLink}`, "URL_SCHEME_NOT_SUPPORTED");
}

@@ -56,3 +56,3 @@ if (config.scheme === "http" && config.tls) {

const url = (0, uri_js_1.encodeBaseUrl)(config.scheme, config.authority, config.path);
return new HttpClient(url, config.authToken);
return new HttpClient(url, config.authToken, config.intMode);
}

@@ -64,4 +64,5 @@ exports._createClient = _createClient;

/** @private */
constructor(url, authToken) {
constructor(url, authToken, intMode) {
this.#client = hrana.openHttp(url, authToken);
this.#client.intMode = intMode;
}

@@ -68,0 +69,0 @@ async execute(stmt) {

@@ -57,3 +57,3 @@ "use strict";

try {
executeStmt(db, "SELECT 1 AS checkThatTheDatabaseCanBeOpened");
executeStmt(db, "SELECT 1 AS checkThatTheDatabaseCanBeOpened", config.intMode);
}

@@ -63,13 +63,15 @@ finally {

}
return new Sqlite3Client(path, options);
return new Sqlite3Client(path, options, config.intMode);
}
exports._createClient = _createClient;
class Sqlite3Client {
path;
options;
#path;
#options;
#intMode;
closed;
/** @private */
constructor(path, options) {
this.path = path;
this.options = options;
constructor(path, options, intMode) {
this.#path = path;
this.#options = options;
this.#intMode = intMode;
this.closed = false;

@@ -79,5 +81,5 @@ }

this.#checkNotClosed();
const db = new better_sqlite3_1.default(this.path, this.options);
const db = new better_sqlite3_1.default(this.#path, this.#options);
try {
return executeStmt(db, stmt);
return executeStmt(db, stmt, this.#intMode);
}

@@ -91,7 +93,7 @@ finally {

this.#checkNotClosed();
const db = new better_sqlite3_1.default(this.path, this.options);
const db = new better_sqlite3_1.default(this.#path, this.#options);
try {
executeStmt(db, (0, util_js_1.transactionModeToBegin)(mode));
const resultSets = stmts.map(stmt => executeStmt(db, stmt));
executeStmt(db, "COMMIT");
executeStmt(db, (0, util_js_1.transactionModeToBegin)(mode), this.#intMode);
const resultSets = stmts.map(stmt => executeStmt(db, stmt, this.#intMode));
executeStmt(db, "COMMIT", this.#intMode);
return resultSets;

@@ -105,6 +107,6 @@ }

this.#checkNotClosed();
const db = new better_sqlite3_1.default(this.path, this.options);
const db = new better_sqlite3_1.default(this.#path, this.#options);
try {
executeStmt(db, (0, util_js_1.transactionModeToBegin)(mode));
return new Sqlite3Transaction(db);
executeStmt(db, (0, util_js_1.transactionModeToBegin)(mode), this.#intMode);
return new Sqlite3Transaction(db, this.#intMode);
}

@@ -118,3 +120,3 @@ catch (e) {

this.#checkNotClosed();
const db = new better_sqlite3_1.default(this.path, this.options);
const db = new better_sqlite3_1.default(this.#path, this.#options);
try {

@@ -138,39 +140,41 @@ return executeMultiple(db, sql);

class Sqlite3Transaction {
database;
#database;
#intMode;
/** @private */
constructor(database) {
this.database = database;
constructor(database, intMode) {
this.#database = database;
this.#intMode = intMode;
}
async execute(stmt) {
this.#checkNotClosed();
return executeStmt(this.database, stmt);
return executeStmt(this.#database, stmt, this.#intMode);
}
async batch(stmts) {
this.#checkNotClosed();
return stmts.map(stmt => executeStmt(this.database, stmt));
return stmts.map(stmt => executeStmt(this.#database, stmt, this.#intMode));
}
async executeMultiple(sql) {
this.#checkNotClosed();
return executeMultiple(this.database, sql);
return executeMultiple(this.#database, sql);
}
async rollback() {
if (!this.database.open) {
if (!this.#database.open) {
return;
}
executeStmt(this.database, "ROLLBACK");
this.database.close();
executeStmt(this.#database, "ROLLBACK", this.#intMode);
this.#database.close();
}
async commit() {
this.#checkNotClosed();
executeStmt(this.database, "COMMIT");
this.database.close();
executeStmt(this.#database, "COMMIT", this.#intMode);
this.#database.close();
}
close() {
this.database.close();
this.#database.close();
}
get closed() {
return !this.database.open;
return !this.#database.open;
}
#checkNotClosed() {
if (!this.database.open) {
if (!this.#database.open) {
throw new api_js_1.LibsqlError("The transaction is closed", "TRANSACTION_CLOSED");

@@ -181,3 +185,3 @@ }

exports.Sqlite3Transaction = Sqlite3Transaction;
function executeStmt(db, stmt) {
function executeStmt(db, stmt, intMode) {
let sql;

@@ -205,2 +209,3 @@ let args;

const sqlStmt = db.prepare(sql);
sqlStmt.safeIntegers(true);
let returnsData = true;

@@ -216,3 +221,5 @@ try {

const columns = Array.from(sqlStmt.columns().map(col => col.name));
const rows = sqlStmt.all(args).map(sqlRow => rowFromSql(sqlRow, columns));
const rows = sqlStmt.all(args).map((sqlRow) => {
return rowFromSql(sqlRow, columns, intMode);
});
// TODO: can we get this info from better-sqlite3?

@@ -234,3 +241,3 @@ const rowsAffected = 0;

}
function rowFromSql(sqlRow, columns) {
function rowFromSql(sqlRow, columns, intMode) {
const row = {};

@@ -240,3 +247,3 @@ // make sure that the "length" property is not enumerable

for (let i = 0; i < sqlRow.length; ++i) {
const value = valueFromSql(sqlRow[i]);
const value = valueFromSql(sqlRow[i], intMode);
Object.defineProperty(row, i, { value });

@@ -250,4 +257,21 @@ const column = columns[i];

}
function valueFromSql(sqlValue) {
if (sqlValue instanceof node_buffer_1.Buffer) {
function valueFromSql(sqlValue, intMode) {
if (typeof sqlValue === "bigint") {
if (intMode === "number") {
if (sqlValue < minSafeBigint || sqlValue > maxSafeBigint) {
throw new RangeError("Received integer which cannot be safely represented as a JavaScript number");
}
return Number(sqlValue);
}
else if (intMode === "bigint") {
return sqlValue;
}
else if (intMode === "string") {
return "" + sqlValue;
}
else {
throw new Error("Invalid value for IntMode");
}
}
else if (sqlValue instanceof node_buffer_1.Buffer) {
return sqlValue.buffer;

@@ -257,2 +281,4 @@ }

}
const minSafeBigint = -9007199254740991n;
const maxSafeBigint = 9007199254740991n;
function valueToSql(value) {

@@ -259,0 +285,0 @@ if (typeof value === "number") {

@@ -69,3 +69,3 @@ "use strict";

}
return new WsClient(client, url, config.authToken);
return new WsClient(client, url, config.authToken, config.intMode);
}

@@ -78,2 +78,3 @@ exports._createClient = _createClient;

#authToken;
#intMode;
// State of the current connection. The `hrana.WsClient` inside may be closed at any moment due to an

@@ -86,5 +87,6 @@ // asynchronous error.

/** @private */
constructor(client, url, authToken) {
constructor(client, url, authToken, intMode) {
this.#url = url;
this.#authToken = authToken;
this.#intMode = intMode;
this.#connState = this.#openConn(client);

@@ -226,2 +228,3 @@ this.#futureConnState = undefined;

const stream = connState.client.openStream();
stream.intMode = this.#intMode;
const streamState = { conn: connState, stream };

@@ -228,0 +231,0 @@ connState.streamStates.add(streamState);

@@ -18,3 +18,15 @@ /** Configuration object for {@link createClient}. */

tls?: boolean;
/** How to convert SQLite integers to JavaScript values:
*
* - `"number"` (default): returns SQLite integers as JavaScript `number`-s (double precision floats).
* `number` cannot precisely represent integers larger than 2^53-1 in absolute value, so attempting to read
* larger integers will throw a `RangeError`.
* - `"bigint"`: returns SQLite integers as JavaScript `bigint`-s (arbitrary precision integers). Bigints can
* precisely represent all SQLite integers.
* - `"string"`: returns SQLite integers as strings.
*/
intMode?: IntMode;
}
/** Representation of integers from database as JavaScript values. See {@link Config.intMode}. */
export type IntMode = "number" | "bigint" | "string";
/** Client object for a remote or local database.

@@ -21,0 +33,0 @@ *

@@ -1,2 +0,2 @@

import type { Config } from "./api.js";
import type { Config, IntMode } from "./api.js";
import type { Authority } from "./uri.js";

@@ -9,4 +9,5 @@ export interface ExpandedConfig {

authToken: string | undefined;
intMode: IntMode;
}
export type ExpandedScheme = "wss" | "ws" | "https" | "http" | "file";
export declare function expandConfig(config: Config, preferHttp: boolean): ExpandedConfig;

@@ -61,2 +61,7 @@ import { LibsqlError } from "./api.js";

}
const intMode = "" + (config.intMode ?? "number");
if (intMode !== "number" && intMode !== "bigint" && intMode !== "string") {
throw new TypeError(`Invalid value for intMode, expected "number", "bigint" or "string", \
got ${JSON.stringify(intMode)}`);
}
return {

@@ -68,3 +73,4 @@ scheme,

authToken,
intMode,
};
}
/// <reference types="node" />
import * as hrana from "@libsql/hrana-client";
import type { Config, Client } from "./api.js";
import type { InStatement, ResultSet, Transaction } from "./api.js";
import type { InStatement, ResultSet, Transaction, IntMode } from "./api.js";
import { TransactionMode } from "./api.js";

@@ -16,3 +16,3 @@ import type { ExpandedConfig } from "./config.js";

/** @private */
constructor(url: URL, authToken: string | undefined);
constructor(url: URL, authToken: string | undefined, intMode: IntMode);
execute(stmt: InStatement): Promise<ResultSet>;

@@ -19,0 +19,0 @@ batch(mode: TransactionMode, stmts: Array<InStatement>): Promise<Array<ResultSet>>;

@@ -16,3 +16,3 @@ import * as hrana from "@libsql/hrana-client";

throw new LibsqlError('The HTTP client supports only "libsql:", "https:" and "http:" URLs, ' +
`got ${JSON.stringify(config.scheme)}. For more information, please read ${supportedUrlLink}`, "URL_SCHEME_NOT_SUPPORTED");
`got ${JSON.stringify(config.scheme + ":")}. For more information, please read ${supportedUrlLink}`, "URL_SCHEME_NOT_SUPPORTED");
}

@@ -26,3 +26,3 @@ if (config.scheme === "http" && config.tls) {

const url = encodeBaseUrl(config.scheme, config.authority, config.path);
return new HttpClient(url, config.authToken);
return new HttpClient(url, config.authToken, config.intMode);
}

@@ -33,4 +33,5 @@ const sqlCacheCapacity = 30;

/** @private */
constructor(url, authToken) {
constructor(url, authToken, intMode) {
this.#client = hrana.openHttp(url, authToken);
this.#client.intMode = intMode;
}

@@ -37,0 +38,0 @@ async execute(stmt) {

import Database from "better-sqlite3";
import type { Config, Client, Transaction, TransactionMode, ResultSet, InStatement } from "./api.js";
import type { Config, IntMode, Client, Transaction, TransactionMode, ResultSet, InStatement } from "./api.js";
import type { ExpandedConfig } from "./config.js";

@@ -10,7 +10,5 @@ export * from "./api.js";

#private;
path: string;
options: Database.Options;
closed: boolean;
/** @private */
constructor(path: string, options: Database.Options);
constructor(path: string, options: Database.Options, intMode: IntMode);
execute(stmt: InStatement): Promise<ResultSet>;

@@ -25,5 +23,4 @@ batch(mode: TransactionMode, stmts: Array<InStatement>): Promise<Array<ResultSet>>;

#private;
database: Database.Database;
/** @private */
constructor(database: Database.Database);
constructor(database: Database.Database, intMode: IntMode);
execute(stmt: InStatement): Promise<ResultSet>;

@@ -30,0 +27,0 @@ batch(stmts: Array<InStatement>): Promise<Array<ResultSet>>;

@@ -36,3 +36,3 @@ import Database from "better-sqlite3";

try {
executeStmt(db, "SELECT 1 AS checkThatTheDatabaseCanBeOpened");
executeStmt(db, "SELECT 1 AS checkThatTheDatabaseCanBeOpened", config.intMode);
}

@@ -42,12 +42,14 @@ finally {

}
return new Sqlite3Client(path, options);
return new Sqlite3Client(path, options, config.intMode);
}
export class Sqlite3Client {
path;
options;
#path;
#options;
#intMode;
closed;
/** @private */
constructor(path, options) {
this.path = path;
this.options = options;
constructor(path, options, intMode) {
this.#path = path;
this.#options = options;
this.#intMode = intMode;
this.closed = false;

@@ -57,5 +59,5 @@ }

this.#checkNotClosed();
const db = new Database(this.path, this.options);
const db = new Database(this.#path, this.#options);
try {
return executeStmt(db, stmt);
return executeStmt(db, stmt, this.#intMode);
}

@@ -69,7 +71,7 @@ finally {

this.#checkNotClosed();
const db = new Database(this.path, this.options);
const db = new Database(this.#path, this.#options);
try {
executeStmt(db, transactionModeToBegin(mode));
const resultSets = stmts.map(stmt => executeStmt(db, stmt));
executeStmt(db, "COMMIT");
executeStmt(db, transactionModeToBegin(mode), this.#intMode);
const resultSets = stmts.map(stmt => executeStmt(db, stmt, this.#intMode));
executeStmt(db, "COMMIT", this.#intMode);
return resultSets;

@@ -83,6 +85,6 @@ }

this.#checkNotClosed();
const db = new Database(this.path, this.options);
const db = new Database(this.#path, this.#options);
try {
executeStmt(db, transactionModeToBegin(mode));
return new Sqlite3Transaction(db);
executeStmt(db, transactionModeToBegin(mode), this.#intMode);
return new Sqlite3Transaction(db, this.#intMode);
}

@@ -96,3 +98,3 @@ catch (e) {

this.#checkNotClosed();
const db = new Database(this.path, this.options);
const db = new Database(this.#path, this.#options);
try {

@@ -115,39 +117,41 @@ return executeMultiple(db, sql);

export class Sqlite3Transaction {
database;
#database;
#intMode;
/** @private */
constructor(database) {
this.database = database;
constructor(database, intMode) {
this.#database = database;
this.#intMode = intMode;
}
async execute(stmt) {
this.#checkNotClosed();
return executeStmt(this.database, stmt);
return executeStmt(this.#database, stmt, this.#intMode);
}
async batch(stmts) {
this.#checkNotClosed();
return stmts.map(stmt => executeStmt(this.database, stmt));
return stmts.map(stmt => executeStmt(this.#database, stmt, this.#intMode));
}
async executeMultiple(sql) {
this.#checkNotClosed();
return executeMultiple(this.database, sql);
return executeMultiple(this.#database, sql);
}
async rollback() {
if (!this.database.open) {
if (!this.#database.open) {
return;
}
executeStmt(this.database, "ROLLBACK");
this.database.close();
executeStmt(this.#database, "ROLLBACK", this.#intMode);
this.#database.close();
}
async commit() {
this.#checkNotClosed();
executeStmt(this.database, "COMMIT");
this.database.close();
executeStmt(this.#database, "COMMIT", this.#intMode);
this.#database.close();
}
close() {
this.database.close();
this.#database.close();
}
get closed() {
return !this.database.open;
return !this.#database.open;
}
#checkNotClosed() {
if (!this.database.open) {
if (!this.#database.open) {
throw new LibsqlError("The transaction is closed", "TRANSACTION_CLOSED");

@@ -157,3 +161,3 @@ }

}
function executeStmt(db, stmt) {
function executeStmt(db, stmt, intMode) {
let sql;

@@ -181,2 +185,3 @@ let args;

const sqlStmt = db.prepare(sql);
sqlStmt.safeIntegers(true);
let returnsData = true;

@@ -192,3 +197,5 @@ try {

const columns = Array.from(sqlStmt.columns().map(col => col.name));
const rows = sqlStmt.all(args).map(sqlRow => rowFromSql(sqlRow, columns));
const rows = sqlStmt.all(args).map((sqlRow) => {
return rowFromSql(sqlRow, columns, intMode);
});
// TODO: can we get this info from better-sqlite3?

@@ -210,3 +217,3 @@ const rowsAffected = 0;

}
function rowFromSql(sqlRow, columns) {
function rowFromSql(sqlRow, columns, intMode) {
const row = {};

@@ -216,3 +223,3 @@ // make sure that the "length" property is not enumerable

for (let i = 0; i < sqlRow.length; ++i) {
const value = valueFromSql(sqlRow[i]);
const value = valueFromSql(sqlRow[i], intMode);
Object.defineProperty(row, i, { value });

@@ -226,4 +233,21 @@ const column = columns[i];

}
function valueFromSql(sqlValue) {
if (sqlValue instanceof Buffer) {
function valueFromSql(sqlValue, intMode) {
if (typeof sqlValue === "bigint") {
if (intMode === "number") {
if (sqlValue < minSafeBigint || sqlValue > maxSafeBigint) {
throw new RangeError("Received integer which cannot be safely represented as a JavaScript number");
}
return Number(sqlValue);
}
else if (intMode === "bigint") {
return sqlValue;
}
else if (intMode === "string") {
return "" + sqlValue;
}
else {
throw new Error("Invalid value for IntMode");
}
}
else if (sqlValue instanceof Buffer) {
return sqlValue.buffer;

@@ -233,2 +257,4 @@ }

}
const minSafeBigint = -9007199254740991n;
const maxSafeBigint = 9007199254740991n;
function valueToSql(value) {

@@ -235,0 +261,0 @@ if (typeof value === "number") {

/// <reference types="node" />
import * as hrana from "@libsql/hrana-client";
import type { Config, Client, Transaction, ResultSet, InStatement } from "./api.js";
import type { Config, IntMode, Client, Transaction, ResultSet, InStatement } from "./api.js";
import { TransactionMode } from "./api.js";

@@ -27,3 +27,3 @@ import type { ExpandedConfig } from "./config.js";

/** @private */
constructor(client: hrana.WsClient, url: URL, authToken: string | undefined);
constructor(client: hrana.WsClient, url: URL, authToken: string | undefined, intMode: IntMode);
execute(stmt: InStatement): Promise<ResultSet>;

@@ -30,0 +30,0 @@ batch(mode: TransactionMode, stmts: Array<InStatement>): Promise<Array<ResultSet>>;

@@ -39,3 +39,3 @@ import * as hrana from "@libsql/hrana-client";

}
return new WsClient(client, url, config.authToken);
return new WsClient(client, url, config.authToken, config.intMode);
}

@@ -47,2 +47,3 @@ const maxConnAgeMillis = 60 * 1000;

#authToken;
#intMode;
// State of the current connection. The `hrana.WsClient` inside may be closed at any moment due to an

@@ -55,5 +56,6 @@ // asynchronous error.

/** @private */
constructor(client, url, authToken) {
constructor(client, url, authToken, intMode) {
this.#url = url;
this.#authToken = authToken;
this.#intMode = intMode;
this.#connState = this.#openConn(client);

@@ -195,2 +197,3 @@ this.#futureConnState = undefined;

const stream = connState.client.openStream();
stream.intMode = this.#intMode;
const streamState = { conn: connState, stream };

@@ -197,0 +200,0 @@ connState.streamStates.add(streamState);

{
"name": "@libsql/client",
"version": "0.2.1",
"version": "0.2.2",
"keywords": [

@@ -75,3 +75,3 @@ "libsql",

"postbuild": "cp package-cjs.json ./lib-cjs/package.json",
"test": "jest",
"test": "jest --runInBand",
"typecheck": "tsc --noEmit",

@@ -91,3 +91,3 @@ "typedoc": "rm -rf ./docs && typedoc"

"dependencies": {
"@libsql/hrana-client": "^0.4.1",
"@libsql/hrana-client": "^0.4.2",
"better-sqlite3": "^8.0.1",

@@ -94,0 +94,0 @@ "js-base64": "^3.7.5"

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc