1 line
22 KiB
JSON
1 line
22 KiB
JSON
|
{"ast":null,"code":"import { ClientError, ClosedError, InternalError } from \"../errors.js\";\nimport { Queue } from \"../queue.js\";\nimport { Stream } from \"../stream.js\";\nimport { WsCursor } from \"./cursor.js\";\nexport class WsStream extends Stream {\n #client;\n #streamId;\n #queue;\n #cursor;\n #closing;\n #closed;\n /** @private */\n static open(client) {\n const streamId = client._streamIdAlloc.alloc();\n const stream = new WsStream(client, streamId);\n const responseCallback = () => undefined;\n const errorCallback = e => stream.#setClosed(e);\n const request = {\n type: \"open_stream\",\n streamId\n };\n client._sendRequest(request, {\n responseCallback,\n errorCallback\n });\n return stream;\n }\n /** @private */\n constructor(client, streamId) {\n super(client.intMode);\n this.#client = client;\n this.#streamId = streamId;\n this.#queue = new Queue();\n this.#cursor = undefined;\n this.#closing = false;\n this.#closed = undefined;\n }\n /** Get the {@link WsClient} object that this stream belongs to. */\n client() {\n return this.#client;\n }\n /** @private */\n _sqlOwner() {\n return this.#client;\n }\n /** @private */\n _execute(stmt) {\n return this.#sendStreamRequest({\n type: \"execute\",\n streamId: this.#streamId,\n stmt\n }).then(response => {\n return response.result;\n });\n }\n /** @private */\n _batch(batch) {\n return this.#sendStreamRequest({\n type: \"batch\",\n streamId: this.#streamId,\n batch\n }).then(response => {\n return response.result;\n });\n }\n /** @private */\n _describe(protoSql) {\n this.#client._ensureVersion(2, \"describe()\");\n return this.#sendStreamRequest({\n type: \"describe\",\n streamId: this.#streamId,\n sql: protoSql.sql,\n sqlId: protoSql.sqlId\n }).then(response => {\n return response.result;\n });\n }\n /** @private */\n _sequence(protoSql) {\n this.#client._ensureVersion(2, \"sequence()\");\n return this.#sendStreamRequest({\n type: \"sequence\",\n streamId: this.#streamId,\n sql: protoSql.sql,\n sqlId: protoSql.sqlId\n }).then(_response => {\n return undefined;\n });\n }\n /** Check whether the SQL connection underlying this stream is in autocommit state (i.e., outside of an\n * explicit transaction). This requires protocol version 3 or higher.\n */\n getAutocommit() {\n this.#client._ensureVersion(3, \"getAutocommit()\");\n return this.#sendStreamRequest({\n type: \"get_autocommit\",\n streamId: this.#streamId\n }).then(response => {\n return response.isAutocommit;\n });\n }\n #sendStreamRequest(request) {\n return new Promise((responseCallback, errorCallback) => {\n this.#pushToQueue({\n type: \"request\",\n request,\n responseCallback,\n errorCallback\n });\n });\n }\n /** @private */\n _openCursor(batch) {\n this.#client._ensureVersion(3, \"cursor\");\n return new Promise((cursorCallback, errorCallback) => {\n this.#pushToQueue({\n type: \"cursor\",\n batch,\n cursorCallback,\n errorCallback\n });\n });\n }\n /** @private */\n _sendCursorRequest(cursor, request) {\n if (cursor !== this.#cursor) {\n throw new InternalError(\"Cursor not associated with the stream attempted to execute a request\");\n }\n return new Promise((responseCallback, errorCallback) => {\n if (this.#closed !== undefined) {\n errorCallback(new ClosedError(\"Stream is closed\", this.#closed));\n } else {\n this.#client._sendRequest(request, {\n responseCallback,\n errorCallback\n });\n }\n });\n }\n /** @private */\n _cursorClosed(cursor) {\n if (cursor !== this.#cursor) {\n throw new InternalError(\"Cursor was closed, but it was not associated with the stream\");\n }\n this.#cursor = undefined;\n this.#flushQueue();\n }\n #pushToQue
|