{"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 = await fetch(request);\n await response.arrayBuffer();\n if (response.ok) {\n return endpoint;\n }\n }\n return fallbackEndpoint;\n}","map":{"version":3,"names":["Object","defineProperty","exports","value","HttpClient","checkEndpoints","isomorphic_fetch_1","require","client_js_1","errors_js_1","stream_js_1","versionPath","pipelinePath","cursorPath","version","encoding","fallbackEndpoint","undefined","Client","url","jwt","fetch","closed","streams","_endpointPromise","_endpoint","constructor","customFetch","protocolVersion","Set","findEndpoint","then","endpoint","error","setClosed","Promise","resolve","getVersion","_ensureVersion","minVersion","feature","ProtocolVersionError","openStream","ClosedError","stream","HttpStream","add","_streamClosed","delete","close","ClientError","#setClosed","Array","from","_setClosed","clientUrl","URL","request","Request","toString","method","response","arrayBuffer","ok"],"sources":["/Users/shoofle/Projects/the-forest/node_modules/@libsql/hrana-client/lib-cjs/http/client.js"],"sourcesContent":["\"use strict\";\nObject.defineProperty(exports, \"__esModule\", { value: true });\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 {\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 */\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 }\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 }\n else if (this._endpoint === undefined) {\n throw new errors_js_1.ProtocolVersionError(`${feature} is supported only on protocol version ${minVersion} and higher, ` +\n \"but the version supported by the HTTP server is not yet known. \" +\n \"Use Client.getVersion() to wait until the version is available.\");\n }\n else if (this._endpoint.version < minVersion) {\n throw new errors_js_1.ProtocolVersionError(`${feature} is supported only on protocol version ${minVersion} and higher, ` +\n `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(), { method: \"GET\" });\n const response = await fetch(request);\n await response.arrayBuffer();\n if (response.ok) {\n return endpoint;\n }\n }\n return fallbackEndpoint;\n}\n"],"mappings":"AAAA,YAAY;;AACZA,MAAM,CAACC,cAAc,CAACC,OAAO,EAAE,YAAY,EAAE;EAAEC,KAAK,EAAE;AAAK,CAAC,CAA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