@libsql/core
Advanced tools
Comparing version 0.7.0 to 0.8.0-pre.1
@@ -7,2 +7,3 @@ "use strict"; | ||
const util_js_1 = require("./util.js"); | ||
const inMemoryMode = ":memory:"; | ||
function expandConfig(config, preferHttp) { | ||
@@ -14,52 +15,57 @@ if (typeof config !== "object") { | ||
} | ||
const concurrency = Math.max(0, config.concurrency || 20); | ||
let tls = config.tls; | ||
let authToken = config.authToken; | ||
let encryptionKey = config.encryptionKey; | ||
let syncUrl = config.syncUrl; | ||
let syncInterval = config.syncInterval; | ||
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)}`); | ||
let { url, authToken, tls, intMode, concurrency } = config; | ||
// fill simple defaults right here | ||
concurrency = Math.max(0, concurrency || 20); | ||
intMode ??= "number"; | ||
let connectionQueryParams = []; // recognized query parameters which we sanitize through white list of valid key-value pairs | ||
// convert plain :memory: url to URI format to make logic more uniform | ||
if (url === inMemoryMode) { | ||
url = "file::memory:"; | ||
} | ||
if (config.url === ":memory:") { | ||
return { | ||
path: ":memory:", | ||
scheme: "file", | ||
syncUrl, | ||
syncInterval, | ||
intMode, | ||
fetch: config.fetch, | ||
tls: false, | ||
authToken: undefined, | ||
encryptionKey: undefined, | ||
authority: undefined, | ||
concurrency, | ||
// parse url parameters first and override config with update values | ||
const uri = (0, uri_js_1.parseUri)(url); | ||
const originalUriScheme = uri.scheme.toLowerCase(); | ||
const isInMemoryMode = originalUriScheme === "file" && | ||
uri.path === inMemoryMode && | ||
uri.authority === undefined; | ||
let queryParamsDef; | ||
if (isInMemoryMode) { | ||
queryParamsDef = { | ||
cache: { | ||
values: ["shared", "private"], | ||
update: (key, value) => connectionQueryParams.push(`${key}=${value}`), | ||
}, | ||
}; | ||
} | ||
const uri = (0, uri_js_1.parseUri)(config.url); | ||
else { | ||
queryParamsDef = { | ||
tls: { | ||
values: ["0", "1"], | ||
update: (_, value) => (tls = value === "1"), | ||
}, | ||
authToken: { | ||
update: (_, value) => (authToken = value), | ||
}, | ||
}; | ||
} | ||
for (const { key, value } of uri.query?.pairs ?? []) { | ||
if (key === "authToken") { | ||
authToken = value ? value : undefined; | ||
if (!Object.hasOwn(queryParamsDef, key)) { | ||
throw new api_js_1.LibsqlError(`Unsupported URL query parameter ${JSON.stringify(key)}`, "URL_PARAM_NOT_SUPPORTED"); | ||
} | ||
else if (key === "tls") { | ||
if (value === "0") { | ||
tls = false; | ||
} | ||
else if (value === "1") { | ||
tls = true; | ||
} | ||
else { | ||
throw new api_js_1.LibsqlError(`Unknown value for the "tls" query argument: ${JSON.stringify(value)}. ` + | ||
'Supported values are "0" and "1"', "URL_INVALID"); | ||
} | ||
const queryParamDef = queryParamsDef[key]; | ||
if (queryParamDef.values !== undefined && | ||
!queryParamDef.values.includes(value)) { | ||
throw new api_js_1.LibsqlError(`Unknown value for the "${key}" query argument: ${JSON.stringify(value)}. Supported values are: [${queryParamDef.values.map((x) => '"' + x + '"').join(", ")}]`, "URL_INVALID"); | ||
} | ||
else { | ||
throw new api_js_1.LibsqlError(`Unknown URL query parameter ${JSON.stringify(key)}`, "URL_PARAM_NOT_SUPPORTED"); | ||
if (queryParamDef.update !== undefined) { | ||
queryParamDef?.update(key, value); | ||
} | ||
} | ||
const uriScheme = uri.scheme.toLowerCase(); | ||
// fill complex defaults & validate config | ||
const connectionQueryParamsString = connectionQueryParams.length === 0 | ||
? "" | ||
: `?${connectionQueryParams.join("&")}`; | ||
const path = uri.path + connectionQueryParamsString; | ||
let scheme; | ||
if (uriScheme === "libsql") { | ||
if (originalUriScheme === "libsql") { | ||
if (tls === false) { | ||
@@ -75,12 +81,16 @@ if (uri.authority?.port === undefined) { | ||
} | ||
else if (uriScheme === "http" || uriScheme === "ws") { | ||
scheme = uriScheme; | ||
else { | ||
scheme = originalUriScheme; | ||
} | ||
if (scheme === "http" || scheme === "ws") { | ||
tls ??= false; | ||
} | ||
else if (uriScheme === "https" || | ||
uriScheme === "wss" || | ||
uriScheme === "file") { | ||
scheme = uriScheme; | ||
else { | ||
tls ??= true; | ||
} | ||
else { | ||
if (scheme !== "http" && | ||
scheme !== "ws" && | ||
scheme !== "https" && | ||
scheme !== "wss" && | ||
scheme !== "file") { | ||
throw new api_js_1.LibsqlError('The client supports only "libsql:", "wss:", "ws:", "https:", "http:" and "file:" URLs, ' + | ||
@@ -90,19 +100,37 @@ `got ${JSON.stringify(uri.scheme + ":")}. ` + | ||
} | ||
if (intMode !== "number" && intMode !== "bigint" && intMode !== "string") { | ||
throw new TypeError(`Invalid value for intMode, expected "number", "bigint" or "string", got ${JSON.stringify(intMode)}`); | ||
} | ||
if (uri.fragment !== undefined) { | ||
throw new api_js_1.LibsqlError(`URL fragments are not supported: ${JSON.stringify("#" + uri.fragment)}`, "URL_INVALID"); | ||
} | ||
if (isInMemoryMode) { | ||
return { | ||
scheme: "file", | ||
tls: false, | ||
path, | ||
intMode, | ||
concurrency, | ||
syncUrl: config.syncUrl, | ||
syncInterval: config.syncInterval, | ||
fetch: config.fetch, | ||
authToken: undefined, | ||
encryptionKey: undefined, | ||
authority: undefined, | ||
}; | ||
} | ||
return { | ||
scheme, | ||
tls: tls ?? true, | ||
tls, | ||
authority: uri.authority, | ||
path: uri.path, | ||
path, | ||
authToken, | ||
encryptionKey, | ||
syncUrl, | ||
syncInterval, | ||
intMode, | ||
concurrency, | ||
encryptionKey: config.encryptionKey, | ||
syncUrl: config.syncUrl, | ||
syncInterval: config.syncInterval, | ||
fetch: config.fetch, | ||
concurrency, | ||
}; | ||
} | ||
exports.expandConfig = expandConfig; |
@@ -45,4 +45,3 @@ /** Configuration object for {@link createClient}. */ | ||
* By default, the client performs up to 20 concurrent requests. You can set this option to a higher | ||
* number to increase the concurrency limit. You can also set this option to `undefined` to disable concurrency | ||
* completely. | ||
* number to increase the concurrency limit or set it to 0 to disable concurrency limits completely. | ||
*/ | ||
@@ -49,0 +48,0 @@ concurrency?: number | undefined; |
@@ -17,2 +17,2 @@ import type { Config, IntMode } from "./api.js"; | ||
export type ExpandedScheme = "wss" | "ws" | "https" | "http" | "file"; | ||
export declare function expandConfig(config: Config, preferHttp: boolean): ExpandedConfig; | ||
export declare function expandConfig(config: Readonly<Config>, preferHttp: boolean): ExpandedConfig; |
import { LibsqlError } from "./api.js"; | ||
import { parseUri } from "./uri.js"; | ||
import { supportedUrlLink } from "./util.js"; | ||
const inMemoryMode = ":memory:"; | ||
export function expandConfig(config, preferHttp) { | ||
@@ -10,52 +11,57 @@ if (typeof config !== "object") { | ||
} | ||
const concurrency = Math.max(0, config.concurrency || 20); | ||
let tls = config.tls; | ||
let authToken = config.authToken; | ||
let encryptionKey = config.encryptionKey; | ||
let syncUrl = config.syncUrl; | ||
let syncInterval = config.syncInterval; | ||
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)}`); | ||
let { url, authToken, tls, intMode, concurrency } = config; | ||
// fill simple defaults right here | ||
concurrency = Math.max(0, concurrency || 20); | ||
intMode ??= "number"; | ||
let connectionQueryParams = []; // recognized query parameters which we sanitize through white list of valid key-value pairs | ||
// convert plain :memory: url to URI format to make logic more uniform | ||
if (url === inMemoryMode) { | ||
url = "file::memory:"; | ||
} | ||
if (config.url === ":memory:") { | ||
return { | ||
path: ":memory:", | ||
scheme: "file", | ||
syncUrl, | ||
syncInterval, | ||
intMode, | ||
fetch: config.fetch, | ||
tls: false, | ||
authToken: undefined, | ||
encryptionKey: undefined, | ||
authority: undefined, | ||
concurrency, | ||
// parse url parameters first and override config with update values | ||
const uri = parseUri(url); | ||
const originalUriScheme = uri.scheme.toLowerCase(); | ||
const isInMemoryMode = originalUriScheme === "file" && | ||
uri.path === inMemoryMode && | ||
uri.authority === undefined; | ||
let queryParamsDef; | ||
if (isInMemoryMode) { | ||
queryParamsDef = { | ||
cache: { | ||
values: ["shared", "private"], | ||
update: (key, value) => connectionQueryParams.push(`${key}=${value}`), | ||
}, | ||
}; | ||
} | ||
const uri = parseUri(config.url); | ||
else { | ||
queryParamsDef = { | ||
tls: { | ||
values: ["0", "1"], | ||
update: (_, value) => (tls = value === "1"), | ||
}, | ||
authToken: { | ||
update: (_, value) => (authToken = value), | ||
}, | ||
}; | ||
} | ||
for (const { key, value } of uri.query?.pairs ?? []) { | ||
if (key === "authToken") { | ||
authToken = value ? value : undefined; | ||
if (!Object.hasOwn(queryParamsDef, key)) { | ||
throw new LibsqlError(`Unsupported URL query parameter ${JSON.stringify(key)}`, "URL_PARAM_NOT_SUPPORTED"); | ||
} | ||
else if (key === "tls") { | ||
if (value === "0") { | ||
tls = false; | ||
} | ||
else if (value === "1") { | ||
tls = true; | ||
} | ||
else { | ||
throw new LibsqlError(`Unknown value for the "tls" query argument: ${JSON.stringify(value)}. ` + | ||
'Supported values are "0" and "1"', "URL_INVALID"); | ||
} | ||
const queryParamDef = queryParamsDef[key]; | ||
if (queryParamDef.values !== undefined && | ||
!queryParamDef.values.includes(value)) { | ||
throw new LibsqlError(`Unknown value for the "${key}" query argument: ${JSON.stringify(value)}. Supported values are: [${queryParamDef.values.map((x) => '"' + x + '"').join(", ")}]`, "URL_INVALID"); | ||
} | ||
else { | ||
throw new LibsqlError(`Unknown URL query parameter ${JSON.stringify(key)}`, "URL_PARAM_NOT_SUPPORTED"); | ||
if (queryParamDef.update !== undefined) { | ||
queryParamDef?.update(key, value); | ||
} | ||
} | ||
const uriScheme = uri.scheme.toLowerCase(); | ||
// fill complex defaults & validate config | ||
const connectionQueryParamsString = connectionQueryParams.length === 0 | ||
? "" | ||
: `?${connectionQueryParams.join("&")}`; | ||
const path = uri.path + connectionQueryParamsString; | ||
let scheme; | ||
if (uriScheme === "libsql") { | ||
if (originalUriScheme === "libsql") { | ||
if (tls === false) { | ||
@@ -71,12 +77,16 @@ if (uri.authority?.port === undefined) { | ||
} | ||
else if (uriScheme === "http" || uriScheme === "ws") { | ||
scheme = uriScheme; | ||
else { | ||
scheme = originalUriScheme; | ||
} | ||
if (scheme === "http" || scheme === "ws") { | ||
tls ??= false; | ||
} | ||
else if (uriScheme === "https" || | ||
uriScheme === "wss" || | ||
uriScheme === "file") { | ||
scheme = uriScheme; | ||
else { | ||
tls ??= true; | ||
} | ||
else { | ||
if (scheme !== "http" && | ||
scheme !== "ws" && | ||
scheme !== "https" && | ||
scheme !== "wss" && | ||
scheme !== "file") { | ||
throw new LibsqlError('The client supports only "libsql:", "wss:", "ws:", "https:", "http:" and "file:" URLs, ' + | ||
@@ -86,18 +96,36 @@ `got ${JSON.stringify(uri.scheme + ":")}. ` + | ||
} | ||
if (intMode !== "number" && intMode !== "bigint" && intMode !== "string") { | ||
throw new TypeError(`Invalid value for intMode, expected "number", "bigint" or "string", got ${JSON.stringify(intMode)}`); | ||
} | ||
if (uri.fragment !== undefined) { | ||
throw new LibsqlError(`URL fragments are not supported: ${JSON.stringify("#" + uri.fragment)}`, "URL_INVALID"); | ||
} | ||
if (isInMemoryMode) { | ||
return { | ||
scheme: "file", | ||
tls: false, | ||
path, | ||
intMode, | ||
concurrency, | ||
syncUrl: config.syncUrl, | ||
syncInterval: config.syncInterval, | ||
fetch: config.fetch, | ||
authToken: undefined, | ||
encryptionKey: undefined, | ||
authority: undefined, | ||
}; | ||
} | ||
return { | ||
scheme, | ||
tls: tls ?? true, | ||
tls, | ||
authority: uri.authority, | ||
path: uri.path, | ||
path, | ||
authToken, | ||
encryptionKey, | ||
syncUrl, | ||
syncInterval, | ||
intMode, | ||
concurrency, | ||
encryptionKey: config.encryptionKey, | ||
syncUrl: config.syncUrl, | ||
syncInterval: config.syncInterval, | ||
fetch: config.fetch, | ||
concurrency, | ||
}; | ||
} |
{ | ||
"name": "@libsql/core", | ||
"version": "0.7.0", | ||
"version": "0.8.0-pre.1", | ||
"keywords": [ | ||
@@ -5,0 +5,0 @@ "libsql", |
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
45890
1122