|
| 1 | +import { Transport } from "../transport.js"; |
| 2 | +import { nextTick } from "./websocket-constructor.js"; |
| 3 | +import { |
| 4 | + encodePacketToBinary, |
| 5 | + decodePacketFromBinary, |
| 6 | + Packet, |
| 7 | +} from "engine.io-parser"; |
| 8 | +import debugModule from "debug"; // debug() |
| 9 | + |
| 10 | +const debug = debugModule("engine.io-client:webtransport"); // debug() |
| 11 | + |
| 12 | +function shouldIncludeBinaryHeader(packet, encoded) { |
| 13 | + // 48 === "0".charCodeAt(0) (OPEN packet type) |
| 14 | + // 54 === "6".charCodeAt(0) (NOOP packet type) |
| 15 | + return ( |
| 16 | + packet.type === "message" && |
| 17 | + typeof packet.data !== "string" && |
| 18 | + encoded[0] >= 48 && |
| 19 | + encoded[0] <= 54 |
| 20 | + ); |
| 21 | +} |
| 22 | + |
| 23 | +export class WT extends Transport { |
| 24 | + private transport: any; |
| 25 | + private writer: any; |
| 26 | + |
| 27 | + get name() { |
| 28 | + return "webtransport"; |
| 29 | + } |
| 30 | + |
| 31 | + protected doOpen() { |
| 32 | + // @ts-ignore |
| 33 | + if (typeof WebTransport !== "function") { |
| 34 | + return; |
| 35 | + } |
| 36 | + // @ts-ignore |
| 37 | + this.transport = new WebTransport( |
| 38 | + this.uri("https"), |
| 39 | + this.opts.transportOptions[this.name] |
| 40 | + ); |
| 41 | + |
| 42 | + this.transport.closed.then(() => this.onClose()); |
| 43 | + |
| 44 | + // note: we could have used async/await, but that would require some additional polyfills |
| 45 | + this.transport.ready.then(() => { |
| 46 | + this.transport.createBidirectionalStream().then((stream) => { |
| 47 | + const reader = stream.readable.getReader(); |
| 48 | + this.writer = stream.writable.getWriter(); |
| 49 | + |
| 50 | + let binaryFlag; |
| 51 | + |
| 52 | + const read = () => { |
| 53 | + reader.read().then(({ done, value }) => { |
| 54 | + if (done) { |
| 55 | + debug("session is closed"); |
| 56 | + return; |
| 57 | + } |
| 58 | + debug("received chunk: %o", value); |
| 59 | + if (!binaryFlag && value.byteLength === 1 && value[0] === 54) { |
| 60 | + binaryFlag = true; |
| 61 | + } else { |
| 62 | + // TODO expose binarytype |
| 63 | + this.onPacket( |
| 64 | + decodePacketFromBinary(value, binaryFlag, "arraybuffer") |
| 65 | + ); |
| 66 | + binaryFlag = false; |
| 67 | + } |
| 68 | + read(); |
| 69 | + }); |
| 70 | + }; |
| 71 | + |
| 72 | + read(); |
| 73 | + |
| 74 | + const handshake = this.query.sid ? `0{"sid":"${this.query.sid}"}` : "0"; |
| 75 | + this.writer |
| 76 | + .write(new TextEncoder().encode(handshake)) |
| 77 | + .then(() => this.onOpen()); |
| 78 | + }); |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + protected write(packets: Packet[]) { |
| 83 | + this.writable = false; |
| 84 | + |
| 85 | + for (let i = 0; i < packets.length; i++) { |
| 86 | + const packet = packets[i]; |
| 87 | + const lastPacket = i === packets.length - 1; |
| 88 | + |
| 89 | + encodePacketToBinary(packet, (data) => { |
| 90 | + if (shouldIncludeBinaryHeader(packet, data)) { |
| 91 | + debug("writing binary header"); |
| 92 | + this.writer.write(Uint8Array.of(54)); |
| 93 | + } |
| 94 | + debug("writing chunk: %o", data); |
| 95 | + this.writer.write(data).then(() => { |
| 96 | + if (lastPacket) { |
| 97 | + nextTick(() => { |
| 98 | + this.writable = true; |
| 99 | + this.emitReserved("drain"); |
| 100 | + }, this.setTimeoutFn); |
| 101 | + } |
| 102 | + }); |
| 103 | + }); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + protected doClose() { |
| 108 | + this.transport?.close(); |
| 109 | + } |
| 110 | +} |
0 commit comments