@libsql/client
Advanced tools
Comparing version 0.0.9 to 0.1.0
@@ -6,3 +6,3 @@ "use strict"; | ||
function expandConfig(config) { | ||
const url = config.url instanceof URL ? config.url : new URL(config.url); | ||
const url = new URL(config.url); | ||
let authToken = config.authToken; | ||
@@ -9,0 +9,0 @@ for (const [key, value] of url.searchParams.entries()) { |
@@ -29,3 +29,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.mapHranaError = exports.resultSetFromHrana = exports.stmtToHrana = exports.HranaTransaction = exports.HranaClient = exports.createClient = void 0; | ||
exports.mapHranaError = exports.resultSetFromHrana = exports.stmtToHrana = exports.HranaTransaction = exports.HranaClient = exports._createClient = exports.createClient = void 0; | ||
const hrana = __importStar(require("@libsql/hrana-client")); | ||
@@ -36,8 +36,12 @@ const api_js_1 = require("./api.js"); | ||
function createClient(config) { | ||
const expandedConfig = (0, config_js_1.expandConfig)(config); | ||
const url = (0, config_js_1.mapLibsqlUrl)(expandedConfig.url, "wss:"); | ||
const client = hrana.open(url, expandedConfig.authToken); | ||
return _createClient((0, config_js_1.expandConfig)(config)); | ||
} | ||
exports.createClient = createClient; | ||
/** @private */ | ||
function _createClient(config) { | ||
const url = (0, config_js_1.mapLibsqlUrl)(config.url, "wss:"); | ||
const client = hrana.open(url, config.authToken); | ||
return new HranaClient(client); | ||
} | ||
exports.createClient = createClient; | ||
exports._createClient = _createClient; | ||
class HranaClient { | ||
@@ -189,4 +193,13 @@ client; | ||
if (e instanceof hrana.ClientError) { | ||
// TODO: Hrana needs to support error codes | ||
return new api_js_1.LibsqlError(e.message, "UNKNOWN", e); | ||
let code = "UNKNOWN"; | ||
if (e instanceof hrana.ResponseError && e.code !== undefined) { | ||
code = e.code; | ||
} | ||
else if (e instanceof hrana.ProtoError) { | ||
code = "HRANA_PROTO_ERROR"; | ||
} | ||
else if (e instanceof hrana.ClosedError) { | ||
code = "HRANA_CLOSED_ERROR"; | ||
} | ||
return new api_js_1.LibsqlError(e.message, code, e); | ||
} | ||
@@ -193,0 +206,0 @@ return e; |
@@ -29,5 +29,5 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.HttpClient = exports.createClient = void 0; | ||
exports.HttpClient = exports._createClient = exports.createClient = void 0; | ||
const hrana = __importStar(require("@libsql/hrana-client")); | ||
const cross_fetch_1 = require("cross-fetch"); | ||
const isomorphic_fetch_1 = require("@libsql/isomorphic-fetch"); | ||
const api_js_1 = require("./api.js"); | ||
@@ -38,7 +38,11 @@ const config_js_1 = require("./config.js"); | ||
function createClient(config) { | ||
const expandedConfig = (0, config_js_1.expandConfig)(config); | ||
const url = (0, config_js_1.mapLibsqlUrl)(expandedConfig.url, "https:"); | ||
return new HttpClient(url, expandedConfig.authToken); | ||
return _createClient((0, config_js_1.expandConfig)(config)); | ||
} | ||
exports.createClient = createClient; | ||
/** @private */ | ||
function _createClient(config) { | ||
const url = (0, config_js_1.mapLibsqlUrl)(config.url, "https:"); | ||
return new HttpClient(url, config.authToken); | ||
} | ||
exports._createClient = _createClient; | ||
class HttpClient { | ||
@@ -135,3 +139,3 @@ #url; | ||
} | ||
const resp = await (0, cross_fetch_1.fetch)(url, { | ||
const resp = await (0, isomorphic_fetch_1.fetch)(url, { | ||
method, | ||
@@ -146,4 +150,4 @@ headers, | ||
if ("message" in respBody) { | ||
// TODO: use the error code from the server, once implemented | ||
throw new api_js_1.LibsqlError(respBody["message"], "UNKNOWN"); | ||
const code = respBody["code"] ?? "UNKNOWN"; | ||
throw new api_js_1.LibsqlError(respBody["message"], code); | ||
} | ||
@@ -150,0 +154,0 @@ } |
@@ -18,24 +18,18 @@ "use strict"; | ||
exports.createClient = void 0; | ||
const api_js_1 = require("./api.js"); | ||
const config_js_1 = require("./config.js"); | ||
const hrana_js_1 = require("./hrana.js"); | ||
const http_js_1 = require("./http.js"); | ||
const sqlite3_js_1 = require("./sqlite3.js"); | ||
const web_js_1 = require("./web.js"); | ||
__exportStar(require("./api.js"), exports); | ||
function createClient(config) { | ||
const expandedConfig = (0, config_js_1.expandConfig)(config); | ||
const url = expandedConfig.url; | ||
if (url.protocol === "http:" || url.protocol === "https:") { | ||
return (0, http_js_1.createClient)(expandedConfig); | ||
return _createClient((0, config_js_1.expandConfig)(config)); | ||
} | ||
exports.createClient = createClient; | ||
function _createClient(config) { | ||
const url = config.url; | ||
if (url.protocol === "file:") { | ||
return (0, sqlite3_js_1._createClient)(config); | ||
} | ||
else if (url.protocol === "ws:" || url.protocol === "wss:" || url.protocol === "libsql:") { | ||
return (0, hrana_js_1.createClient)(expandedConfig); | ||
} | ||
else if (url.protocol === "file:") { | ||
return (0, sqlite3_js_1.createClient)(expandedConfig); | ||
} | ||
else { | ||
throw new api_js_1.LibsqlError(`URL scheme ${JSON.stringify(url.protocol)} is not supported`, "URL_SCHEME_NOT_SUPPORTED"); | ||
return (0, web_js_1._createClient)(config); | ||
} | ||
} | ||
exports.createClient = createClient; |
@@ -20,3 +20,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Sqlite3Transaction = exports.Sqlite3Client = exports.createClient = void 0; | ||
exports.Sqlite3Transaction = exports.Sqlite3Client = exports._createClient = exports.createClient = void 0; | ||
const better_sqlite3_1 = __importDefault(require("better-sqlite3")); | ||
@@ -28,4 +28,8 @@ const node_buffer_1 = require("node:buffer"); | ||
function createClient(config) { | ||
const expandedConfig = (0, config_js_1.expandConfig)(config); | ||
const url = expandedConfig.url; | ||
return _createClient((0, config_js_1.expandConfig)(config)); | ||
} | ||
exports.createClient = createClient; | ||
/** @private */ | ||
function _createClient(config) { | ||
const url = config.url; | ||
if (url.protocol !== "file:") { | ||
@@ -45,3 +49,3 @@ throw new api_js_1.LibsqlError(`URL scheme ${JSON.stringify(url.protocol)} is not supported by the sqlite3 client`, "URL_SCHEME_NOT_SUPPORTED"); | ||
} | ||
exports.createClient = createClient; | ||
exports._createClient = _createClient; | ||
class Sqlite3Client { | ||
@@ -48,0 +52,0 @@ path; |
export interface Config { | ||
url: string | URL; | ||
url: string; | ||
authToken?: string; | ||
transactions?: boolean; | ||
} | ||
@@ -41,2 +40,2 @@ export interface Client { | ||
} | ||
export type ErrorCode = "UNKNOWN" | "NOT_IMPLEMENTED" | "URL_SCHEME_NOT_SUPPORTED" | "URL_PARAM_NOT_SUPPORTED" | "URL_PARAM_INVALID_VALUE" | "SERVER_ERROR" | "TRANSACTION_CLOSED" | "CLIENT_CLOSED" | "TRANSACTIONS_NOT_SUPPORTED" | string; | ||
export type ErrorCode = string; |
@@ -0,3 +1,4 @@ | ||
/// <reference types="node" /> | ||
import type { Config } from "./api.js"; | ||
export interface ExpandedConfig extends Config { | ||
export interface ExpandedConfig { | ||
url: URL; | ||
@@ -4,0 +5,0 @@ authToken: string | undefined; |
import { LibsqlError } from "./api.js"; | ||
export function expandConfig(config) { | ||
const url = config.url instanceof URL ? config.url : new URL(config.url); | ||
const url = new URL(config.url); | ||
let authToken = config.authToken; | ||
@@ -5,0 +5,0 @@ for (const [key, value] of url.searchParams.entries()) { |
import * as hrana from "@libsql/hrana-client"; | ||
import type { Config, Client, Transaction, ResultSet, InStatement } from "./api.js"; | ||
import type { ExpandedConfig } from "./config.js"; | ||
export * from "./api.js"; | ||
export declare function createClient(config: Config): HranaClient; | ||
/** @private */ | ||
export declare function _createClient(config: ExpandedConfig): HranaClient; | ||
export declare class HranaClient implements Client { | ||
@@ -6,0 +9,0 @@ client: hrana.Client; |
@@ -6,5 +6,8 @@ import * as hrana from "@libsql/hrana-client"; | ||
export function createClient(config) { | ||
const expandedConfig = expandConfig(config); | ||
const url = mapLibsqlUrl(expandedConfig.url, "wss:"); | ||
const client = hrana.open(url, expandedConfig.authToken); | ||
return _createClient(expandConfig(config)); | ||
} | ||
/** @private */ | ||
export function _createClient(config) { | ||
const url = mapLibsqlUrl(config.url, "wss:"); | ||
const client = hrana.open(url, config.authToken); | ||
return new HranaClient(client); | ||
@@ -154,6 +157,15 @@ } | ||
if (e instanceof hrana.ClientError) { | ||
// TODO: Hrana needs to support error codes | ||
return new LibsqlError(e.message, "UNKNOWN", e); | ||
let code = "UNKNOWN"; | ||
if (e instanceof hrana.ResponseError && e.code !== undefined) { | ||
code = e.code; | ||
} | ||
else if (e instanceof hrana.ProtoError) { | ||
code = "HRANA_PROTO_ERROR"; | ||
} | ||
else if (e instanceof hrana.ClosedError) { | ||
code = "HRANA_CLOSED_ERROR"; | ||
} | ||
return new LibsqlError(e.message, code, e); | ||
} | ||
return e; | ||
} |
@@ -0,5 +1,9 @@ | ||
/// <reference types="node" /> | ||
import type { Config, Client } from "./api.js"; | ||
import { InStatement, ResultSet } from "./api.js"; | ||
import type { ExpandedConfig } from "./config.js"; | ||
export * from "./api.js"; | ||
export declare function createClient(config: Config): Client; | ||
/** @private */ | ||
export declare function _createClient(config: ExpandedConfig): Client; | ||
export declare class HttpClient implements Client { | ||
@@ -6,0 +10,0 @@ #private; |
import * as hrana from "@libsql/hrana-client"; | ||
import { fetch } from "cross-fetch"; | ||
import { fetch } from "@libsql/isomorphic-fetch"; | ||
import { LibsqlError } from "./api.js"; | ||
@@ -8,6 +8,9 @@ import { expandConfig, mapLibsqlUrl } from "./config.js"; | ||
export function createClient(config) { | ||
const expandedConfig = expandConfig(config); | ||
const url = mapLibsqlUrl(expandedConfig.url, "https:"); | ||
return new HttpClient(url, expandedConfig.authToken); | ||
return _createClient(expandConfig(config)); | ||
} | ||
/** @private */ | ||
export function _createClient(config) { | ||
const url = mapLibsqlUrl(config.url, "https:"); | ||
return new HttpClient(url, config.authToken); | ||
} | ||
export class HttpClient { | ||
@@ -114,4 +117,4 @@ #url; | ||
if ("message" in respBody) { | ||
// TODO: use the error code from the server, once implemented | ||
throw new LibsqlError(respBody["message"], "UNKNOWN"); | ||
const code = respBody["code"] ?? "UNKNOWN"; | ||
throw new LibsqlError(respBody["message"], code); | ||
} | ||
@@ -118,0 +121,0 @@ } |
@@ -1,22 +0,16 @@ | ||
import { LibsqlError } from "./api.js"; | ||
import { expandConfig } from "./config.js"; | ||
import { createClient as createHranaClient } from "./hrana.js"; | ||
import { createClient as createHttpClient } from "./http.js"; | ||
import { createClient as createSqlite3Client } from "./sqlite3.js"; | ||
import { _createClient as _createSqlite3Client } from "./sqlite3.js"; | ||
import { _createClient as _createWebClient } from "./web.js"; | ||
export * from "./api.js"; | ||
export function createClient(config) { | ||
const expandedConfig = expandConfig(config); | ||
const url = expandedConfig.url; | ||
if (url.protocol === "http:" || url.protocol === "https:") { | ||
return createHttpClient(expandedConfig); | ||
return _createClient(expandConfig(config)); | ||
} | ||
function _createClient(config) { | ||
const url = config.url; | ||
if (url.protocol === "file:") { | ||
return _createSqlite3Client(config); | ||
} | ||
else if (url.protocol === "ws:" || url.protocol === "wss:" || url.protocol === "libsql:") { | ||
return createHranaClient(expandedConfig); | ||
} | ||
else if (url.protocol === "file:") { | ||
return createSqlite3Client(expandedConfig); | ||
} | ||
else { | ||
throw new LibsqlError(`URL scheme ${JSON.stringify(url.protocol)} is not supported`, "URL_SCHEME_NOT_SUPPORTED"); | ||
return _createWebClient(config); | ||
} | ||
} |
import Database from "better-sqlite3"; | ||
import type { Config, Client, Transaction, ResultSet, InStatement } from "./api.js"; | ||
import type { ExpandedConfig } from "./config.js"; | ||
export * from "./api.js"; | ||
export declare function createClient(config: Config): Client; | ||
/** @private */ | ||
export declare function _createClient(config: ExpandedConfig): Client; | ||
export declare class Sqlite3Client implements Client { | ||
@@ -6,0 +9,0 @@ #private; |
@@ -7,4 +7,7 @@ import Database from "better-sqlite3"; | ||
export function createClient(config) { | ||
const expandedConfig = expandConfig(config); | ||
const url = expandedConfig.url; | ||
return _createClient(expandConfig(config)); | ||
} | ||
/** @private */ | ||
export function _createClient(config) { | ||
const url = config.url; | ||
if (url.protocol !== "file:") { | ||
@@ -11,0 +14,0 @@ throw new LibsqlError(`URL scheme ${JSON.stringify(url.protocol)} is not supported by the sqlite3 client`, "URL_SCHEME_NOT_SUPPORTED"); |
{ | ||
"name": "@libsql/client", | ||
"version": "0.0.9", | ||
"version": "0.1.0", | ||
"keywords": [ | ||
@@ -48,2 +48,7 @@ "libsql", | ||
"require": "./lib-cjs/sqlite3.js" | ||
}, | ||
"./web": { | ||
"types": "./lib-esm/web.d.ts", | ||
"import": "./lib-esm/web.js", | ||
"require": "./lib-cjs/web.js" | ||
} | ||
@@ -55,3 +60,4 @@ }, | ||
"hrana": ["./lib-esm/hrana.d.ts"], | ||
"sqlite3": ["./lib-esm/sqlite3.d.ts"] | ||
"sqlite3": ["./lib-esm/sqlite3.d.ts"], | ||
"web": ["./lib-esm/web.d.ts"] | ||
} | ||
@@ -83,7 +89,7 @@ }, | ||
"dependencies": { | ||
"@libsql/hrana-client": "^0.3.0", | ||
"@libsql/hrana-client": "^0.3.3", | ||
"@libsql/isomorphic-fetch": "^0.1.1", | ||
"better-sqlite3": "^8.0.1", | ||
"cross-fetch": "^3.1.5", | ||
"js-base64": "^3.7.5" | ||
} | ||
} |
# libSQL client API for TypeScript and JavaScript | ||
[![Node.js CI](https://github.com/libsql/libsql-client-ts/actions/workflows/node-ci.yaml/badge.svg)](https://github.com/libsql/libsql-client-ts/actions/workflows/node-ci.yaml) | ||
[![Node.js CI](https://github.com/libsql/libsql-client-ts/actions/workflows/ci.yaml/badge.svg)](https://github.com/libsql/libsql-client-ts/actions/workflows/ci.yaml) | ||
[![License](https://img.shields.io/badge/license-MIT-blue)](https://github.com/libsql/libsql-client-ts/blob/main/LICENSE) | ||
@@ -14,3 +14,3 @@ | ||
const config = { | ||
url: "http://localhost:8080" | ||
url: "ws://localhost:8080" | ||
}; | ||
@@ -22,3 +22,3 @@ const db = createClient(config); | ||
You can also just run against local SQLite with: | ||
You can also connect to a local SQLite database with: | ||
@@ -29,3 +29,3 @@ ```typescript | ||
const config = { | ||
url: "file:example.db" // Use "file::memory:" for in-memory mode. | ||
url: "file:/tmp/example.db" | ||
}; | ||
@@ -39,4 +39,3 @@ const db = createClient(config); | ||
* SQLite JavaScript API | ||
* SQLite-backed local-only backend | ||
* SQL over HTTP with `fetch()` | ||
* Connect to `sqld` (with HTTP or WebSockets) | ||
* Connect to a local SQLite database |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
53988
25
1429
1
38
+ Added@libsql/isomorphic-fetch@0.1.12(transitive)
+ Added@types/node-fetch@2.6.12(transitive)
+ Addedasynckit@0.4.0(transitive)
+ Addedcombined-stream@1.0.8(transitive)
+ Addeddelayed-stream@1.0.0(transitive)
+ Addedform-data@4.0.1(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
- Removedcross-fetch@^3.1.5
- Removedcross-fetch@3.1.8(transitive)
Updated@libsql/hrana-client@^0.3.3