1 line
14 KiB
JSON
1 line
14 KiB
JSON
|
{"ast":null,"code":"import { LibsqlError } from \"./api.js\";\nimport { parseUri } from \"./uri.js\";\nimport { supportedUrlLink } from \"./util.js\";\nconst inMemoryMode = \":memory:\";\nexport function isInMemoryConfig(config) {\n return config.scheme === \"file\" && (config.path === \":memory:\" || config.path.startsWith(\":memory:?\"));\n}\nexport function expandConfig(config, preferHttp) {\n if (typeof config !== \"object\") {\n // produce a reasonable error message in the common case where users type\n // `createClient(\"libsql://...\")` instead of `createClient({url: \"libsql://...\"})`\n throw new TypeError(`Expected client configuration as object, got ${typeof config}`);\n }\n let {\n url,\n authToken,\n tls,\n intMode,\n concurrency\n } = config;\n // fill simple defaults right here\n concurrency = Math.max(0, concurrency || 20);\n intMode ??= \"number\";\n let connectionQueryParams = []; // recognized query parameters which we sanitize through white list of valid key-value pairs\n // convert plain :memory: url to URI format to make logic more uniform\n if (url === inMemoryMode) {\n url = \"file::memory:\";\n }\n // parse url parameters first and override config with update values\n const uri = parseUri(url);\n const originalUriScheme = uri.scheme.toLowerCase();\n const isInMemoryMode = originalUriScheme === \"file\" && uri.path === inMemoryMode && uri.authority === undefined;\n let queryParamsDef;\n if (isInMemoryMode) {\n queryParamsDef = {\n cache: {\n values: [\"shared\", \"private\"],\n update: (key, value) => connectionQueryParams.push(`${key}=${value}`)\n }\n };\n } else {\n queryParamsDef = {\n tls: {\n values: [\"0\", \"1\"],\n update: (_, value) => tls = value === \"1\"\n },\n authToken: {\n update: (_, value) => authToken = value\n }\n };\n }\n for (const {\n key,\n value\n } of uri.query?.pairs ?? []) {\n if (!Object.hasOwn(queryParamsDef, key)) {\n throw new LibsqlError(`Unsupported URL query parameter ${JSON.stringify(key)}`, \"URL_PARAM_NOT_SUPPORTED\");\n }\n const queryParamDef = queryParamsDef[key];\n if (queryParamDef.values !== undefined && !queryParamDef.values.includes(value)) {\n throw new LibsqlError(`Unknown value for the \"${key}\" query argument: ${JSON.stringify(value)}. Supported values are: [${queryParamDef.values.map(x => '\"' + x + '\"').join(\", \")}]`, \"URL_INVALID\");\n }\n if (queryParamDef.update !== undefined) {\n queryParamDef?.update(key, value);\n }\n }\n // fill complex defaults & validate config\n const connectionQueryParamsString = connectionQueryParams.length === 0 ? \"\" : `?${connectionQueryParams.join(\"&\")}`;\n const path = uri.path + connectionQueryParamsString;\n let scheme;\n if (originalUriScheme === \"libsql\") {\n if (tls === false) {\n if (uri.authority?.port === undefined) {\n throw new LibsqlError('A \"libsql:\" URL with ?tls=0 must specify an explicit port', \"URL_INVALID\");\n }\n scheme = preferHttp ? \"http\" : \"ws\";\n } else {\n scheme = preferHttp ? \"https\" : \"wss\";\n }\n } else {\n scheme = originalUriScheme;\n }\n if (scheme === \"http\" || scheme === \"ws\") {\n tls ??= false;\n } else {\n tls ??= true;\n }\n if (scheme !== \"http\" && scheme !== \"ws\" && scheme !== \"https\" && scheme !== \"wss\" && scheme !== \"file\") {\n throw new LibsqlError('The client supports only \"libsql:\", \"wss:\", \"ws:\", \"https:\", \"http:\" and \"file:\" URLs, ' + `got ${JSON.stringify(uri.scheme + \":\")}. ` + `For more information, please read ${supportedUrlLink}`, \"URL_SCHEME_NOT_SUPPORTED\");\n }\n if (intMode !== \"number\" && intMode !== \"bigint\" && intMode !== \"string\") {\n throw new TypeError(`Invalid value for intMode, expected \"number\", \"bigint\" or \"string\", got ${JSON.stringify(intMode)}`);\n }\n if (uri.fragment !== undefined) {\n throw new LibsqlError(`URL fragments are not supported: ${JSON.st
|