the-forest/client/node_modules/.cache/babel-loader/af3db1292392d2301b25271fd668e909b9bbec3e9410e7e6feb185bf96488e19.json

1 line
20 KiB
JSON
Raw Normal View History

2024-09-17 20:35:18 -04:00
{"ast":null,"code":"import * as hrana from \"@libsql/hrana-client\";\nimport { LibsqlError } from \"@libsql/core/api\";\nimport { expandConfig } from \"@libsql/core/config\";\nimport { HranaTransaction, executeHranaBatch, stmtToHrana, resultSetFromHrana, mapHranaError } from \"./hrana.js\";\nimport { SqlCache } from \"./sql_cache.js\";\nimport { encodeBaseUrl } from \"@libsql/core/uri\";\nimport { supportedUrlLink } from \"@libsql/core/util\";\nimport promiseLimit from \"promise-limit\";\nexport * from \"@libsql/core/api\";\nexport function createClient(config) {\n return _createClient(expandConfig(config, true));\n}\n/** @private */\nexport function _createClient(config) {\n if (config.scheme !== \"https\" && config.scheme !== \"http\") {\n 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\");\n }\n if (config.encryptionKey !== undefined) {\n throw new LibsqlError(\"Encryption key is not supported by the remote client.\", \"ENCRYPTION_KEY_NOT_SUPPORTED\");\n }\n if (config.scheme === \"http\" && config.tls) {\n throw new LibsqlError(`A \"http:\" URL cannot opt into TLS by using ?tls=1`, \"URL_INVALID\");\n } else if (config.scheme === \"https\" && !config.tls) {\n throw new LibsqlError(`A \"https:\" URL cannot opt out of TLS by using ?tls=0`, \"URL_INVALID\");\n }\n const url = encodeBaseUrl(config.scheme, config.authority, config.path);\n return new HttpClient(url, config.authToken, config.intMode, config.fetch, config.concurrency);\n}\nconst sqlCacheCapacity = 30;\nexport class HttpClient {\n #client;\n protocol;\n #authToken;\n #promiseLimitFunction;\n /** @private */\n constructor(url, authToken, intMode, customFetch, concurrency) {\n this.#client = hrana.openHttp(url, authToken, customFetch);\n this.#client.intMode = intMode;\n this.protocol = \"http\";\n this.#authToken = authToken;\n this.#promiseLimitFunction = promiseLimit(concurrency);\n }\n async limit(fn) {\n return this.#promiseLimitFunction(fn);\n }\n async execute(stmtOrSql, args) {\n let stmt;\n if (typeof stmtOrSql === \"string\") {\n stmt = {\n sql: stmtOrSql,\n args: args || []\n };\n } else {\n stmt = stmtOrSql;\n }\n return this.limit(async () => {\n try {\n const hranaStmt = stmtToHrana(stmt);\n // Pipeline all operations, so `hrana.HttpClient` can open the stream, execute the statement and\n // close the stream in a single HTTP request.\n let rowsPromise;\n const stream = this.#client.openStream();\n try {\n rowsPromise = stream.query(hranaStmt);\n } finally {\n stream.closeGracefully();\n }\n const rowsResult = await rowsPromise;\n return resultSetFromHrana(rowsResult);\n } catch (e) {\n throw mapHranaError(e);\n }\n });\n }\n async batch(stmts, mode = \"deferred\") {\n return this.limit(async () => {\n try {\n const hranaStmts = stmts.map(stmtToHrana);\n const version = await this.#client.getVersion();\n // Pipeline all operations, so `hrana.HttpClient` can open the stream, execute the batch and\n // close the stream in a single HTTP request.\n let resultsPromise;\n const stream = this.#client.openStream();\n try {\n // It makes sense to use a SQL cache even for a single batch, because it may contain the same\n // statement repeated multiple times.\n const sqlCache = new SqlCache(stream, sqlCacheCapacity);\n sqlCache.apply(hranaStmts);\n // TODO: we do not use a cursor here, because it would cause three roundtrips:\n // 1. pipeline request to store SQL texts\n // 2. cursor request\n // 3. pipeline request to close the stream\n const batch = stream.batch(false);\n resultsPromise = executeHranaBatch(mode, version, batch