@libsql/client
Advanced tools
Comparing version 0.1.0 to 0.1.1
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.mapLibsqlUrl = exports.expandConfig = void 0; | ||
exports.expandConfig = void 0; | ||
const api_js_1 = require("./api.js"); | ||
const uri_js_1 = require("./uri.js"); | ||
function expandConfig(config) { | ||
const url = new URL(config.url); | ||
const uri = (0, uri_js_1.parseUri)(config.url); | ||
let authToken = config.authToken; | ||
for (const [key, value] of url.searchParams.entries()) { | ||
for (const { key, value } of uri.query?.pairs ?? []) { | ||
if (key === "authToken") { | ||
@@ -16,18 +17,12 @@ authToken = value ? value : undefined; | ||
} | ||
for (const key of Array.from(url.searchParams.keys())) { | ||
url.searchParams.delete(key); | ||
if (uri.fragment !== undefined) { | ||
throw new api_js_1.LibsqlError(`Unsupported URL fragment: ${JSON.stringify("#" + uri.fragment)}`, "URL_INVALID"); | ||
} | ||
return { url, authToken }; | ||
return { | ||
scheme: uri.scheme, | ||
authority: uri.authority, | ||
path: uri.path, | ||
authToken, | ||
}; | ||
} | ||
exports.expandConfig = expandConfig; | ||
function mapLibsqlUrl(url, protocol) { | ||
if (url.protocol === "libsql:") { | ||
// we can't use the `URL.protocol` setter, because the specification forbids changing a non-special | ||
// scheme ("libsql") to special scheme ("https"). | ||
return new URL(protocol + url.toString().substring(url.protocol.length)); | ||
} | ||
else { | ||
return url; | ||
} | ||
} | ||
exports.mapLibsqlUrl = mapLibsqlUrl; |
@@ -33,2 +33,3 @@ "use strict"; | ||
const config_js_1 = require("./config.js"); | ||
const uri_js_1 = require("./uri.js"); | ||
__exportStar(require("./api.js"), exports); | ||
@@ -41,3 +42,11 @@ function createClient(config) { | ||
function _createClient(config) { | ||
const url = (0, config_js_1.mapLibsqlUrl)(config.url, "wss:"); | ||
let scheme = config.scheme.toLowerCase(); | ||
if (scheme === "libsql") { | ||
scheme = "wss"; | ||
} | ||
if (scheme !== "wss" && scheme !== "ws") { | ||
throw new api_js_1.LibsqlError('The WebSocket (Hrana) client supports only "libsql", "wss" and "ws" URLs, ' + | ||
`got ${JSON.stringify(config.scheme)}`, "URL_SCHEME_NOT_SUPPORTED"); | ||
} | ||
const url = (0, uri_js_1.encodeBaseUrl)(scheme, config.authority, config.path); | ||
const client = hrana.open(url, config.authToken); | ||
@@ -44,0 +53,0 @@ return new HranaClient(client); |
@@ -35,2 +35,3 @@ "use strict"; | ||
const hrana_js_1 = require("./hrana.js"); | ||
const uri_js_1 = require("./uri.js"); | ||
__exportStar(require("./api.js"), exports); | ||
@@ -43,3 +44,11 @@ function createClient(config) { | ||
function _createClient(config) { | ||
const url = (0, config_js_1.mapLibsqlUrl)(config.url, "https:"); | ||
let scheme = config.scheme.toLowerCase(); | ||
if (scheme === "libsql") { | ||
scheme = "https"; | ||
} | ||
if (scheme !== "https" && scheme !== "http") { | ||
throw new api_js_1.LibsqlError('The HTTP client supports only "libsql", "https" and "http" URLs, ' + | ||
`got ${JSON.stringify(config.scheme)}`, "URL_SCHEME_NOT_SUPPORTED"); | ||
} | ||
const url = (0, uri_js_1.encodeBaseUrl)(scheme, config.authority, config.path); | ||
return new HttpClient(url, config.authToken); | ||
@@ -46,0 +55,0 @@ } |
@@ -18,5 +18,7 @@ "use strict"; | ||
exports.createClient = void 0; | ||
const api_js_1 = require("./api.js"); | ||
const config_js_1 = require("./config.js"); | ||
const sqlite3_js_1 = require("./sqlite3.js"); | ||
const web_js_1 = require("./web.js"); | ||
const hrana_js_1 = require("./hrana.js"); | ||
const http_js_1 = require("./http.js"); | ||
__exportStar(require("./api.js"), exports); | ||
@@ -28,9 +30,16 @@ function createClient(config) { | ||
function _createClient(config) { | ||
const url = config.url; | ||
if (url.protocol === "file:") { | ||
const scheme = config.scheme.toLowerCase(); | ||
if (scheme === "file") { | ||
return (0, sqlite3_js_1._createClient)(config); | ||
} | ||
else if (scheme === "libsql" || scheme === "wss" || scheme === "ws") { | ||
return (0, hrana_js_1._createClient)(config); | ||
} | ||
else if (scheme === "https" || scheme === "http") { | ||
return (0, http_js_1._createClient)(config); | ||
} | ||
else { | ||
return (0, web_js_1._createClient)(config); | ||
throw new api_js_1.LibsqlError('The client supports only "file", "libsql", "wss", "ws", "https" and "http" URLs, ' + | ||
`got ${JSON.stringify(config.scheme)}`, "URL_SCHEME_NOT_SUPPORTED"); | ||
} | ||
} |
@@ -32,7 +32,19 @@ "use strict"; | ||
function _createClient(config) { | ||
const url = config.url; | ||
if (url.protocol !== "file:") { | ||
throw new api_js_1.LibsqlError(`URL scheme ${JSON.stringify(url.protocol)} is not supported by the sqlite3 client`, "URL_SCHEME_NOT_SUPPORTED"); | ||
if (config.scheme !== "file") { | ||
throw new api_js_1.LibsqlError(`URL scheme ${JSON.stringify(config.scheme)} is not supported by the local sqlite3 client`, "URL_SCHEME_NOT_SUPPORTED"); | ||
} | ||
const path = url.pathname; | ||
const authority = config.authority; | ||
if (authority !== undefined) { | ||
const host = authority.host.toLowerCase(); | ||
if (host !== "" && host !== "localhost") { | ||
throw new api_js_1.LibsqlError(`Invalid host in file URL: ${JSON.stringify(authority.host)}`, "URL_INVALID"); | ||
} | ||
if (authority.port !== undefined) { | ||
throw new api_js_1.LibsqlError("File URL cannot have port", "URL_INVALID"); | ||
} | ||
if (authority.userinfo !== undefined) { | ||
throw new api_js_1.LibsqlError("File URL cannot have username and password", "URL_INVALID"); | ||
} | ||
} | ||
const path = config.path; | ||
const options = {}; | ||
@@ -39,0 +51,0 @@ const db = new better_sqlite3_1.default(path, options); |
@@ -29,13 +29,14 @@ "use strict"; | ||
function _createClient(config) { | ||
const url = config.url; | ||
if (url.protocol === "http:" || url.protocol === "https:") { | ||
const scheme = config.scheme.toLowerCase(); | ||
if (scheme === "libsql" || scheme === "wss" || scheme === "ws") { | ||
return (0, hrana_js_1._createClient)(config); | ||
} | ||
else if (scheme === "https" || scheme === "http") { | ||
return (0, http_js_1._createClient)(config); | ||
} | ||
else if (url.protocol === "ws:" || url.protocol === "wss:" || url.protocol === "libsql:") { | ||
return (0, hrana_js_1._createClient)(config); | ||
} | ||
else { | ||
throw new api_js_1.LibsqlError(`URL scheme ${JSON.stringify(url.protocol)} is not supported`, "URL_SCHEME_NOT_SUPPORTED"); | ||
throw new api_js_1.LibsqlError('The client that uses Web standard APIs supports only "libsql", "wss", "ws", "https" and "http" URLs, ' + | ||
`got ${JSON.stringify(config.scheme)}`, "URL_SCHEME_NOT_SUPPORTED"); | ||
} | ||
} | ||
exports._createClient = _createClient; |
@@ -1,8 +0,9 @@ | ||
/// <reference types="node" /> | ||
import type { Config } from "./api.js"; | ||
import type { Authority } from "./uri.js"; | ||
export interface ExpandedConfig { | ||
url: URL; | ||
scheme: string; | ||
authority: Authority | undefined; | ||
path: string; | ||
authToken: string | undefined; | ||
} | ||
export declare function expandConfig(config: Config): ExpandedConfig; | ||
export declare function mapLibsqlUrl(url: URL, protocol: string): URL; |
import { LibsqlError } from "./api.js"; | ||
import { parseUri } from "./uri.js"; | ||
export function expandConfig(config) { | ||
const url = new URL(config.url); | ||
const uri = parseUri(config.url); | ||
let authToken = config.authToken; | ||
for (const [key, value] of url.searchParams.entries()) { | ||
for (const { key, value } of uri.query?.pairs ?? []) { | ||
if (key === "authToken") { | ||
@@ -13,16 +14,11 @@ authToken = value ? value : undefined; | ||
} | ||
for (const key of Array.from(url.searchParams.keys())) { | ||
url.searchParams.delete(key); | ||
if (uri.fragment !== undefined) { | ||
throw new LibsqlError(`Unsupported URL fragment: ${JSON.stringify("#" + uri.fragment)}`, "URL_INVALID"); | ||
} | ||
return { url, authToken }; | ||
return { | ||
scheme: uri.scheme, | ||
authority: uri.authority, | ||
path: uri.path, | ||
authToken, | ||
}; | ||
} | ||
export function mapLibsqlUrl(url, protocol) { | ||
if (url.protocol === "libsql:") { | ||
// we can't use the `URL.protocol` setter, because the specification forbids changing a non-special | ||
// scheme ("libsql") to special scheme ("https"). | ||
return new URL(protocol + url.toString().substring(url.protocol.length)); | ||
} | ||
else { | ||
return url; | ||
} | ||
} |
import * as hrana from "@libsql/hrana-client"; | ||
import { LibsqlError } from "./api.js"; | ||
import { expandConfig, mapLibsqlUrl } from "./config.js"; | ||
import { expandConfig } from "./config.js"; | ||
import { encodeBaseUrl } from "./uri.js"; | ||
export * from "./api.js"; | ||
@@ -10,3 +11,11 @@ export function createClient(config) { | ||
export function _createClient(config) { | ||
const url = mapLibsqlUrl(config.url, "wss:"); | ||
let scheme = config.scheme.toLowerCase(); | ||
if (scheme === "libsql") { | ||
scheme = "wss"; | ||
} | ||
if (scheme !== "wss" && scheme !== "ws") { | ||
throw new LibsqlError('The WebSocket (Hrana) client supports only "libsql", "wss" and "ws" URLs, ' + | ||
`got ${JSON.stringify(config.scheme)}`, "URL_SCHEME_NOT_SUPPORTED"); | ||
} | ||
const url = encodeBaseUrl(scheme, config.authority, config.path); | ||
const client = hrana.open(url, config.authToken); | ||
@@ -13,0 +22,0 @@ return new HranaClient(client); |
import * as hrana from "@libsql/hrana-client"; | ||
import { fetch } from "@libsql/isomorphic-fetch"; | ||
import { LibsqlError } from "./api.js"; | ||
import { expandConfig, mapLibsqlUrl } from "./config.js"; | ||
import { expandConfig } from "./config.js"; | ||
import { stmtToHrana, resultSetFromHrana, mapHranaError } from "./hrana.js"; | ||
import { encodeBaseUrl } from "./uri.js"; | ||
export * from "./api.js"; | ||
@@ -12,3 +13,11 @@ export function createClient(config) { | ||
export function _createClient(config) { | ||
const url = mapLibsqlUrl(config.url, "https:"); | ||
let scheme = config.scheme.toLowerCase(); | ||
if (scheme === "libsql") { | ||
scheme = "https"; | ||
} | ||
if (scheme !== "https" && scheme !== "http") { | ||
throw new LibsqlError('The HTTP client supports only "libsql", "https" and "http" URLs, ' + | ||
`got ${JSON.stringify(config.scheme)}`, "URL_SCHEME_NOT_SUPPORTED"); | ||
} | ||
const url = encodeBaseUrl(scheme, config.authority, config.path); | ||
return new HttpClient(url, config.authToken); | ||
@@ -15,0 +24,0 @@ } |
@@ -0,4 +1,6 @@ | ||
import { LibsqlError } from "./api.js"; | ||
import { expandConfig } from "./config.js"; | ||
import { _createClient as _createSqlite3Client } from "./sqlite3.js"; | ||
import { _createClient as _createWebClient } from "./web.js"; | ||
import { _createClient as _createHranaClient } from "./hrana.js"; | ||
import { _createClient as _createHttpClient } from "./http.js"; | ||
export * from "./api.js"; | ||
@@ -9,9 +11,16 @@ export function createClient(config) { | ||
function _createClient(config) { | ||
const url = config.url; | ||
if (url.protocol === "file:") { | ||
const scheme = config.scheme.toLowerCase(); | ||
if (scheme === "file") { | ||
return _createSqlite3Client(config); | ||
} | ||
else if (scheme === "libsql" || scheme === "wss" || scheme === "ws") { | ||
return _createHranaClient(config); | ||
} | ||
else if (scheme === "https" || scheme === "http") { | ||
return _createHttpClient(config); | ||
} | ||
else { | ||
return _createWebClient(config); | ||
throw new LibsqlError('The client supports only "file", "libsql", "wss", "ws", "https" and "http" URLs, ' + | ||
`got ${JSON.stringify(config.scheme)}`, "URL_SCHEME_NOT_SUPPORTED"); | ||
} | ||
} |
@@ -11,7 +11,19 @@ import Database from "better-sqlite3"; | ||
export function _createClient(config) { | ||
const url = config.url; | ||
if (url.protocol !== "file:") { | ||
throw new LibsqlError(`URL scheme ${JSON.stringify(url.protocol)} is not supported by the sqlite3 client`, "URL_SCHEME_NOT_SUPPORTED"); | ||
if (config.scheme !== "file") { | ||
throw new LibsqlError(`URL scheme ${JSON.stringify(config.scheme)} is not supported by the local sqlite3 client`, "URL_SCHEME_NOT_SUPPORTED"); | ||
} | ||
const path = url.pathname; | ||
const authority = config.authority; | ||
if (authority !== undefined) { | ||
const host = authority.host.toLowerCase(); | ||
if (host !== "" && host !== "localhost") { | ||
throw new LibsqlError(`Invalid host in file URL: ${JSON.stringify(authority.host)}`, "URL_INVALID"); | ||
} | ||
if (authority.port !== undefined) { | ||
throw new LibsqlError("File URL cannot have port", "URL_INVALID"); | ||
} | ||
if (authority.userinfo !== undefined) { | ||
throw new LibsqlError("File URL cannot have username and password", "URL_INVALID"); | ||
} | ||
} | ||
const path = config.path; | ||
const options = {}; | ||
@@ -18,0 +30,0 @@ const db = new Database(path, options); |
@@ -11,12 +11,13 @@ import { LibsqlError } from "./api.js"; | ||
export function _createClient(config) { | ||
const url = config.url; | ||
if (url.protocol === "http:" || url.protocol === "https:") { | ||
const scheme = config.scheme.toLowerCase(); | ||
if (scheme === "libsql" || scheme === "wss" || scheme === "ws") { | ||
return _createHranaClient(config); | ||
} | ||
else if (scheme === "https" || scheme === "http") { | ||
return _createHttpClient(config); | ||
} | ||
else if (url.protocol === "ws:" || url.protocol === "wss:" || url.protocol === "libsql:") { | ||
return _createHranaClient(config); | ||
} | ||
else { | ||
throw new LibsqlError(`URL scheme ${JSON.stringify(url.protocol)} is not supported`, "URL_SCHEME_NOT_SUPPORTED"); | ||
throw new LibsqlError('The client that uses Web standard APIs supports only "libsql", "wss", "ws", "https" and "http" URLs, ' + | ||
`got ${JSON.stringify(config.scheme)}`, "URL_SCHEME_NOT_SUPPORTED"); | ||
} | ||
} |
{ | ||
"name": "@libsql/client", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"keywords": [ | ||
@@ -69,2 +69,3 @@ "libsql", | ||
"scripts": { | ||
"prepublishOnly": "npm run build", | ||
"prebuild": "rm -rf ./lib-cjs ./lib-esm", | ||
@@ -71,0 +72,0 @@ "build": "npm run build:cjs && npm run build:esm", |
@@ -27,3 +27,3 @@ # libSQL client API for TypeScript and JavaScript | ||
const config = { | ||
url: "file:/tmp/example.db" | ||
url: "file:local.db" | ||
}; | ||
@@ -35,5 +35,12 @@ const db = createClient(config); | ||
## Features | ||
## Supported URLs | ||
* Connect to `sqld` (with HTTP or WebSockets) | ||
* Connect to a local SQLite database | ||
The client can connect to the database using different methods depending on the scheme (protocol) of the passed URL: | ||
* `file:` connects to a local SQLite database (using `better-sqlite3`) | ||
* `file:/absolute/path` or `file:///absolute/path` is an absolute path on local filesystem | ||
* `file:relative/path` is a relative path on local filesystem | ||
* (`file://path` is not a valid URL) | ||
* `ws:` or `wss:` connect to `sqld` using WebSockets (the Hrana protocol). | ||
* `http:` or `https:` connect to `sqld` using HTTP. The `transaction()` API is not available in this case. | ||
* `libsql:` is equivalent to `wss:`. |
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
68331
28
1780
45