1 line
12 KiB
JSON
1 line
12 KiB
JSON
|
{"ast":null,"code":"import { fetch, Request } from \"@libsql/isomorphic-fetch\";\nimport { Client } from \"../client.js\";\nimport { ClientError, ClosedError, ProtocolVersionError } from \"../errors.js\";\nimport { HttpStream } from \"./stream.js\";\nexport const 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. */\nexport class HttpClient extends 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 ?? 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 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 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 ClosedError(\"Client is closed\", this.#closed);\n }\n const stream = new 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 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 ClosedError(\"Client was closed\", error));\n }\n }\n}\nasync function findEndpoint(customFetch, clientUrl) {\n const fetch = customFetch;\n for (const endpoint of checkEndpoints) {\n const url = new URL(endpoint.versionPath, clientUrl);\n const request = new 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":["fetch","Request","Client","ClientError","ClosedError","ProtocolVersionError","HttpStream","checkEnd
|