the-forest/client/node_modules/.cache/babel-loader/19b2fc21916102406f1544c0998953a0f5e55a2d40d367c0cf5d4fa1c844cf3f.json

1 line
13 KiB
JSON
Raw Normal View History

2024-09-17 20:35:18 -04:00
{"ast":null,"code":"\"use strict\";\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\nexports.HttpClient = exports.checkEndpoints = void 0;\nconst isomorphic_fetch_1 = require(\"@libsql/isomorphic-fetch\");\nconst client_js_1 = require(\"../client.js\");\nconst errors_js_1 = require(\"../errors.js\");\nconst stream_js_1 = require(\"./stream.js\");\nexports.checkEndpoints = [{\n versionPath: \"v3-protobuf\",\n pipelinePath: \"v3-protobuf/pipeline\",\n cursorPath: \"v3-protobuf/cursor\",\n version: 3,\n encoding: \"protobuf\"\n}\n/*\n{\n versionPath: \"v3\",\n pipelinePath: \"v3/pipeline\",\n cursorPath: \"v3/cursor\",\n version: 3,\n encoding: \"json\",\n},\n*/];\nconst fallbackEndpoint = {\n versionPath: \"v2\",\n pipelinePath: \"v2/pipeline\",\n cursorPath: undefined,\n version: 2,\n encoding: \"json\"\n};\n/** A client for the Hrana protocol over HTTP. */\nclass HttpClient extends client_js_1.Client {\n #url;\n #jwt;\n #fetch;\n #closed;\n #streams;\n /** @private */\n _endpointPromise;\n /** @private */\n _endpoint;\n /** @private */\n constructor(url, jwt, customFetch, protocolVersion = 2) {\n super();\n this.#url = url;\n this.#jwt = jwt;\n this.#fetch = customFetch ?? isomorphic_fetch_1.fetch;\n this.#closed = undefined;\n this.#streams = new Set();\n if (protocolVersion == 3) {\n this._endpointPromise = findEndpoint(this.#fetch, this.#url);\n this._endpointPromise.then(endpoint => this._endpoint = endpoint, error => this.#setClosed(error));\n } else {\n this._endpointPromise = Promise.resolve(fallbackEndpoint);\n this._endpointPromise.then(endpoint => this._endpoint = endpoint, error => this.#setClosed(error));\n }\n }\n /** Get the protocol version supported by the server. */\n async getVersion() {\n if (this._endpoint !== undefined) {\n return this._endpoint.version;\n }\n return (await this._endpointPromise).version;\n }\n // Make sure that the negotiated version is at least `minVersion`.\n /** @private */\n _ensureVersion(minVersion, feature) {\n if (minVersion <= fallbackEndpoint.version) {\n return;\n } else if (this._endpoint === undefined) {\n throw new errors_js_1.ProtocolVersionError(`${feature} is supported only on protocol version ${minVersion} and higher, ` + \"but the version supported by the HTTP server is not yet known. \" + \"Use Client.getVersion() to wait until the version is available.\");\n } else if (this._endpoint.version < minVersion) {\n throw new errors_js_1.ProtocolVersionError(`${feature} is supported only on protocol version ${minVersion} and higher, ` + `but the HTTP server only supports version ${this._endpoint.version}.`);\n }\n }\n /** Open a {@link HttpStream}, a stream for executing SQL statements. */\n openStream() {\n if (this.#closed !== undefined) {\n throw new errors_js_1.ClosedError(\"Client is closed\", this.#closed);\n }\n const stream = new stream_js_1.HttpStream(this, this.#url, this.#jwt, this.#fetch);\n this.#streams.add(stream);\n return stream;\n }\n /** @private */\n _streamClosed(stream) {\n this.#streams.delete(stream);\n }\n /** Close the client and all its streams. */\n close() {\n this.#setClosed(new errors_js_1.ClientError(\"Client was manually closed\"));\n }\n /** True if the client is closed. */\n get closed() {\n return this.#closed !== undefined;\n }\n #setClosed(error) {\n if (this.#closed !== undefined) {\n return;\n }\n this.#closed = error;\n for (const stream of Array.from(this.#streams)) {\n stream._setClosed(new errors_js_1.ClosedError(\"Client was closed\", error));\n }\n }\n}\nexports.HttpClient = HttpClient;\nasync function findEndpoint(customFetch, clientUrl) {\n const fetch = customFetch;\n for (const endpoint of exports.checkEndpoints) {\n const url = new URL(endpoint.versionPath, clientUrl);\n const request = new isomorphic_fetch_1.Request(url.toString(), {\n method: \"GET\"\n });\n const response = aw