All files / json-pack/src/ws WsFrameEncoder.ts

87.01% Statements 67/77
85.18% Branches 23/27
91.66% Functions 11/12
87.67% Lines 64/73

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 1271x 1x 1x     1x 1x   1x 12x     2x 2x       2x 2x       2x 2x       16x 16x                 2x 2x 1x 1x   1x         2x 2x 1x 1x   1x         2x 1x 1x 1x 1x         1x 1x 1x 1x 1x 1x           1x         26x 26x 26x 26x 15x 15x 11x 7x 7x   4x 4x 4x 4x   26x       1x 1x       1x 1x 1x               3x 3x 3x 3x 3x 3x 10x 3x      
import {Writer} from '@jsonjoy.com/buffers/lib/Writer';
import {WsFrameOpcode} from './constants';
import {WsFrameEncodingError} from './errors';
import type {IWriter, IWriterGrowable} from '@jsonjoy.com/buffers';
 
const maskBuf = new Uint8Array(4);
const maskBufView = new DataView(maskBuf.buffer, maskBuf.byteOffset, maskBuf.byteLength);
 
export class WsFrameEncoder<W extends IWriter & IWriterGrowable = IWriter & IWriterGrowable> {
  constructor(public readonly writer: W = new Writer() as any) {}
 
  public encodePing(data: Uint8Array | null): Uint8Array {
    this.writePing(data);
    return this.writer.flush();
  }
 
  public encodePong(data: Uint8Array | null): Uint8Array {
    this.writePong(data);
    return this.writer.flush();
  }
 
  public encodeClose(reason: string, code = 0): Uint8Array {
    this.writeClose(reason, code);
    return this.writer.flush();
  }
 
  public encodeHdr(fin: 0 | 1, opcode: WsFrameOpcode, length: number, mask: number): Uint8Array {
    this.writeHdr(fin, opcode, length, mask);
    return this.writer.flush();
  }
 
  public encodeDataMsgHdrFast(length: number): Uint8Array {
    this.writeDataMsgHdrFast(length);
    return this.writer.flush();
  }
 
  public writePing(data: Uint8Array | null): void {
    let length = 0;
    if (data && (length = data.length)) {
      this.writeHdr(1, WsFrameOpcode.PING, length, 0);
      this.writer.buf(data, length);
    } else {
      this.writeHdr(1, WsFrameOpcode.PING, 0, 0);
    }
  }
 
  public writePong(data: Uint8Array | null): void {
    let length = 0;
    if (data && (length = data.length)) {
      this.writeHdr(1, WsFrameOpcode.PONG, length, 0);
      this.writer.buf(data, length);
    } else {
      this.writeHdr(1, WsFrameOpcode.PONG, 0, 0);
    }
  }
 
  public writeClose(reason: string, code = 0): void {
    if (reason || code) {
      const reasonLength = reason.length;
      const length = 2 + reasonLength;
      const writer = this.writer;
      writer.ensureCapacity(
        2 + // Frame header
          2 + // Close code 2 bytes
          reasonLength * 4, // Close reason, max 4 bytes per UTF-8 char
      );
      const lengthX = writer.x + 1;
      this.writeHdr(1, WsFrameOpcode.CLOSE, length, 0);
      writer.u16(code);
      if (reasonLength) {
        const utf8Length = writer.utf8(reason);
        Iif (utf8Length !== reasonLength) {
          Iif (utf8Length > 126 - 2) throw new WsFrameEncodingError();
          writer.uint8[lengthX] = (writer.uint8[lengthX] & 0b10000000) | (utf8Length + 2);
        }
      }
    } else {
      this.writeHdr(1, WsFrameOpcode.CLOSE, 0, 0);
    }
  }
 
  public writeHdr(fin: 0 | 1, opcode: WsFrameOpcode, length: number, mask: number): void {
    const octet1 = (fin << 7) | opcode;
    const maskBit = mask ? 0b10000000 : 0b00000000;
    const writer = this.writer;
    if (length < 126) {
      const octet2 = maskBit | length;
      writer.u16((octet1 << 8) | octet2);
    } else if (length < 0x10000) {
      const octet2 = maskBit | 126;
      writer.u32(((octet1 << 8) | octet2) * 0x10000 + length);
    } else {
      const octet2 = maskBit | 127;
      writer.u16((octet1 << 8) | octet2);
      writer.u32(0);
      writer.u32(length);
    }
    if (mask) writer.u32(mask);
  }
 
  public writeDataMsgHdrFast(length: number): void {
    const writer = this.writer;
    Iif (length < 126) {
      writer.u16(0b10000010_00000000 + length);
      return;
    }
    if (length < 0x10000) {
      writer.u32(0b10000010_01111110_00000000_00000000 + length);
      return;
    }
    writer.u16(0b10000010_01111111);
    writer.u32(0);
    writer.u32(length);
  }
 
  public writeBufXor(buf: Uint8Array, mask: number): void {
    maskBufView.setUint32(0, mask, false);
    const writer = this.writer;
    const length = buf.length;
    writer.ensureCapacity(length);
    let x = writer.x;
    const uint8 = writer.uint8;
    for (let i = 0; i < length; i++) uint8[x++] = buf[i] ^ maskBuf[i & 3];
    writer.x = x;
  }
}