1 line
40 KiB
JSON
1 line
40 KiB
JSON
|
{"ast":null,"code":"\"use strict\";\n\nObject.defineProperty(exports, \"__esModule\", {\n value: true\n});\nexports.WsClient = exports.subprotocolsV3 = exports.subprotocolsV2 = void 0;\nconst client_js_1 = require(\"../client.js\");\nconst index_js_1 = require(\"../encoding/index.js\");\nconst errors_js_1 = require(\"../errors.js\");\nconst id_alloc_js_1 = require(\"../id_alloc.js\");\nconst result_js_1 = require(\"../result.js\");\nconst sql_js_1 = require(\"../sql.js\");\nconst util_js_1 = require(\"../util.js\");\nconst stream_js_1 = require(\"./stream.js\");\nconst json_encode_js_1 = require(\"./json_encode.js\");\nconst protobuf_encode_js_1 = require(\"./protobuf_encode.js\");\nconst json_decode_js_1 = require(\"./json_decode.js\");\nconst protobuf_decode_js_1 = require(\"./protobuf_decode.js\");\nexports.subprotocolsV2 = new Map([[\"hrana2\", {\n version: 2,\n encoding: \"json\"\n}], [\"hrana1\", {\n version: 1,\n encoding: \"json\"\n}]]);\nexports.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. */\nclass WsClient extends client_js_1.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 id_alloc_js_1.IdAlloc();\n this._streamIdAlloc = new id_alloc_js_1.IdAlloc();\n this._cursorIdAlloc = new id_alloc_js_1.IdAlloc();\n this.#sqlIdAlloc = new id_alloc_js_1.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 errors_js_1.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 th
|