webtransport.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.WT = void 0;
  7. const transport_js_1 = require("../transport.js");
  8. const websocket_constructor_js_1 = require("./websocket-constructor.js");
  9. const engine_io_parser_1 = require("engine.io-parser");
  10. const debug_1 = __importDefault(require("debug")); // debug()
  11. const debug = (0, debug_1.default)("engine.io-client:webtransport"); // debug()
  12. class WT extends transport_js_1.Transport {
  13. get name() {
  14. return "webtransport";
  15. }
  16. doOpen() {
  17. // @ts-ignore
  18. if (typeof WebTransport !== "function") {
  19. return;
  20. }
  21. // @ts-ignore
  22. this.transport = new WebTransport(this.createUri("https"), this.opts.transportOptions[this.name]);
  23. this.transport.closed
  24. .then(() => {
  25. debug("transport closed gracefully");
  26. this.onClose();
  27. })
  28. .catch((err) => {
  29. debug("transport closed due to %s", err);
  30. this.onError("webtransport error", err);
  31. });
  32. // note: we could have used async/await, but that would require some additional polyfills
  33. this.transport.ready.then(() => {
  34. this.transport.createBidirectionalStream().then((stream) => {
  35. const decoderStream = (0, engine_io_parser_1.createPacketDecoderStream)(Number.MAX_SAFE_INTEGER, this.socket.binaryType);
  36. const reader = stream.readable.pipeThrough(decoderStream).getReader();
  37. const encoderStream = (0, engine_io_parser_1.createPacketEncoderStream)();
  38. encoderStream.readable.pipeTo(stream.writable);
  39. this.writer = encoderStream.writable.getWriter();
  40. const read = () => {
  41. reader
  42. .read()
  43. .then(({ done, value }) => {
  44. if (done) {
  45. debug("session is closed");
  46. return;
  47. }
  48. debug("received chunk: %o", value);
  49. this.onPacket(value);
  50. read();
  51. })
  52. .catch((err) => {
  53. debug("an error occurred while reading: %s", err);
  54. });
  55. };
  56. read();
  57. const packet = { type: "open" };
  58. if (this.query.sid) {
  59. packet.data = `{"sid":"${this.query.sid}"}`;
  60. }
  61. this.writer.write(packet).then(() => this.onOpen());
  62. });
  63. });
  64. }
  65. write(packets) {
  66. this.writable = false;
  67. for (let i = 0; i < packets.length; i++) {
  68. const packet = packets[i];
  69. const lastPacket = i === packets.length - 1;
  70. this.writer.write(packet).then(() => {
  71. if (lastPacket) {
  72. (0, websocket_constructor_js_1.nextTick)(() => {
  73. this.writable = true;
  74. this.emitReserved("drain");
  75. }, this.setTimeoutFn);
  76. }
  77. });
  78. }
  79. }
  80. doClose() {
  81. var _a;
  82. (_a = this.transport) === null || _a === void 0 ? void 0 : _a.close();
  83. }
  84. }
  85. exports.WT = WT;