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

1 line
38 KiB
JSON
Raw Normal View History

2024-09-17 20:35:18 -04:00
{"ast":null,"code":"import { Client } from \"../client.js\";\nimport { readJsonObject, writeJsonObject, readProtobufMessage, writeProtobufMessage } from \"../encoding/index.js\";\nimport { ClientError, ProtoError, ClosedError, WebSocketError, ProtocolVersionError, InternalError } from \"../errors.js\";\nimport { IdAlloc } from \"../id_alloc.js\";\nimport { errorFromProto } from \"../result.js\";\nimport { Sql } from \"../sql.js\";\nimport { impossible } from \"../util.js\";\nimport { WsStream } from \"./stream.js\";\nimport { ClientMsg as json_ClientMsg } from \"./json_encode.js\";\nimport { ClientMsg as protobuf_ClientMsg } from \"./protobuf_encode.js\";\nimport { ServerMsg as json_ServerMsg } from \"./json_decode.js\";\nimport { ServerMsg as protobuf_ServerMsg } from \"./protobuf_decode.js\";\nexport const subprotocolsV2 = new Map([[\"hrana2\", {\n version: 2,\n encoding: \"json\"\n}], [\"hrana1\", {\n version: 1,\n encoding: \"json\"\n}]]);\nexport const subprotocolsV3 = new Map([[\"hrana3-protobuf\", {\n version: 3,\n encoding: \"protobuf\"\n}], [\"hrana3\", {\n version: 3,\n encoding: \"json\"\n}], [\"hrana2\", {\n version: 2,\n encoding: \"json\"\n}], [\"hrana1\", {\n version: 1,\n encoding: \"json\"\n}]]);\n/** A client for the Hrana protocol over a WebSocket. */\nexport class WsClient extends Client {\n #socket;\n // List of callbacks that we queue until the socket transitions from the CONNECTING to the OPEN state.\n #openCallbacks;\n // Have we already transitioned from CONNECTING to OPEN and fired the callbacks in #openCallbacks?\n #opened;\n // Stores the error that caused us to close the client (and the socket). If we are not closed, this is\n // `undefined`.\n #closed;\n // Have we received a response to our \"hello\" from the server?\n #recvdHello;\n // Subprotocol negotiated with the server. It is only available after the socket transitions to the OPEN\n // state.\n #subprotocol;\n // Has the `getVersion()` function been called? This is only used to validate that the API is used\n // correctly.\n #getVersionCalled;\n // A map from request id to the responses that we expect to receive from the server.\n #responseMap;\n // An allocator of request ids.\n #requestIdAlloc;\n // An allocator of stream ids.\n /** @private */\n _streamIdAlloc;\n // An allocator of cursor ids.\n /** @private */\n _cursorIdAlloc;\n // An allocator of SQL text ids.\n #sqlIdAlloc;\n /** @private */\n constructor(socket, jwt) {\n super();\n this.#socket = socket;\n this.#openCallbacks = [];\n this.#opened = false;\n this.#closed = undefined;\n this.#recvdHello = false;\n this.#subprotocol = undefined;\n this.#getVersionCalled = false;\n this.#responseMap = new Map();\n this.#requestIdAlloc = new IdAlloc();\n this._streamIdAlloc = new IdAlloc();\n this._cursorIdAlloc = new IdAlloc();\n this.#sqlIdAlloc = new IdAlloc();\n this.#socket.binaryType = \"arraybuffer\";\n this.#socket.addEventListener(\"open\", () => this.#onSocketOpen());\n this.#socket.addEventListener(\"close\", event => this.#onSocketClose(event));\n this.#socket.addEventListener(\"error\", event => this.#onSocketError(event));\n this.#socket.addEventListener(\"message\", event => this.#onSocketMessage(event));\n this.#send({\n type: \"hello\",\n jwt\n });\n }\n // Send (or enqueue to send) a message to the server.\n #send(msg) {\n if (this.#closed !== undefined) {\n throw new InternalError(\"Trying to send a message on a closed client\");\n }\n if (this.#opened) {\n this.#sendToSocket(msg);\n } else {\n const openCallback = () => this.#sendToSocket(msg);\n const errorCallback = () => undefined;\n this.#openCallbacks.push({\n openCallback,\n errorCallback\n });\n }\n }\n // The socket transitioned from CONNECTING to OPEN\n #onSocketOpen() {\n const protocol = this.#socket.protocol;\n if (protocol === undefined) {\n this.#setClosed(new ClientError(\"The `WebSocket.protocol` property is un