All files / buffers/src StreamingReader.ts

25.84% Statements 23/89
20% Branches 2/10
41.37% Functions 12/29
25.58% Lines 22/86

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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 2252x 2x   2x   2x           8x     8x             42x                 33x                     11x                                     30x       83x       20x       10x 10x               5x                                                                                                         18x 17x                 4x 3x 3x 3x                     1x                                                                                                                                    
import {Writer} from './Writer';
import {decodeUtf8} from './utf8/decodeUtf8';
import type {IReader, IReaderResettable} from './types';
import {Reader} from './Reader';
 
export class StreamingReader implements IReader, IReaderResettable {
  protected readonly writer: Writer;
 
  /**
   * Offset from the start of the buffer (x0 in Writer).
   */
  protected dx = 0;
 
  constructor(allocSize: number = 16 * 1024) {
    this.writer = new Writer(allocSize);
  }
 
  /**
   * Returns the number of bytes remaining in the buffer.
   */
  public size(): number {
    return this.writer.x - this.x;
  }
 
  /**
   * Assert that there is enough data in the buffer to read `size` bytes.
   *
   * @param size Number of bytes to read.
   */
  protected assertSize(size: number): void {
    if (size > this.size()) throw new RangeError('OUT_OF_BOUNDS');
  }
 
  /**
   * Add a chunk of data to be decoded. The chunk is copied into the
   * internal buffer, so you can reuse the chunk after calling this method; or
   * this chunk can be neutered by the caller.
   *
   * @param uint8 `Uint8Array` chunk of data to be decoded.
   */
  public push(uint8: Uint8Array): void {
    this.writer.buf(uint8, uint8.length);
  }
 
  /**
   * Mark the current position as consumed. This will free up memory
   * for reuse.
   */
  public consume(): void {
    this.writer.x0 += this.dx;
    this.dx = 0;
  }
 
  // ------------------------------------------------------------------ IReader
 
  public get uint8(): Uint8Array {
    return this.writer.uint8;
  }
 
  public get view(): DataView {
    return this.writer.view;
  }
 
  public get x(): number {
    return this.writer.x0 + this.dx;
  }
 
  public set x(x: number) {
    this.dx = x - this.writer.x0;
  }
 
  public peek(): number {
    this.assertSize(1);
    return this.view.getUint8(this.x);
  }
 
  /**
   * Get current byte value without advancing the cursor.
   * @deprecated Use peek() instead.
   */
  public peak(): number {
    return this.peek();
  }
 
  public skip(length: number): void {
    this.assertSize(length);
    this.x += length;
  }
 
  public buf(size: number = this.size()): Uint8Array {
    this.assertSize(size);
    const end = this.x + size;
    const bin = this.uint8.subarray(this.x, end);
    this.x = end;
    return bin;
  }
 
  public subarray(start: number = 0, end?: number): Uint8Array {
    const x = this.x;
    const actualStart = x + start;
    const actualEnd = typeof end === 'number' ? x + end : this.size() + x - start;
    return this.uint8.subarray(actualStart, actualEnd);
  }
 
  /**
   * Creates a new {@link Reader} that references the same underlying memory
   * buffer. But with independent cursor and end.
   *
   * @param start Start offset relative to the current cursor position.
   * @param end End offset relative to the current cursor position.
   * @returns A new {@link Reader} instance.
   */
  public slice(start: number = 0, end?: number): Reader {
    const x = this.x;
    const actualStart = x + start;
    const actualEnd = typeof end === 'number' ? x + end : this.size() + x - start;
    return new Reader(this.uint8, this.view, actualStart, actualEnd);
  }
 
  /**
   * Similar to {@link slice} but also advances the cursor. Returns a new
   * {@link Reader} that references the same underlying memory buffer, starting
   * from the current cursor position.
   *
   * @param size Number of bytes to cut from the current position.
   * @returns A new {@link Reader} instance.
   */
  public cut(size: number = this.size()): Reader {
    const slice = this.slice(0, size);
    this.skip(size);
    return slice;
  }
 
  public u8(): number {
    this.assertSize(1);
    return this.view.getUint8(this.x++);
  }
 
  public i8(): number {
    this.assertSize(1);
    return this.view.getInt8(this.x++);
  }
 
  public u16(): number {
    this.assertSize(2);
    const num = this.view.getUint16(this.x);
    this.x += 2;
    return num;
  }
 
  public i16(): number {
    this.assertSize(2);
    const num = this.view.getInt16(this.x);
    this.x += 2;
    return num;
  }
 
  public u32(): number {
    this.assertSize(4);
    const num = this.view.getUint32(this.x);
    this.x += 4;
    return num;
  }
 
  public i32(): number {
    this.assertSize(4);
    const num = this.view.getInt32(this.x);
    this.x += 4;
    return num;
  }
 
  public u64(): bigint {
    this.assertSize(8);
    const num = this.view.getBigUint64(this.x);
    this.x += 8;
    return num;
  }
 
  public i64(): bigint {
    this.assertSize(8);
    const num = this.view.getBigInt64(this.x);
    this.x += 8;
    return num;
  }
 
  public f32(): number {
    this.assertSize(4);
    const pos = this.x;
    this.x += 4;
    return this.view.getFloat32(pos);
  }
 
  public f64(): number {
    this.assertSize(8);
    const pos = this.x;
    this.x += 8;
    return this.view.getFloat64(pos);
  }
 
  public utf8(size: number): string {
    this.assertSize(size);
    const start = this.x;
    this.x += size;
    return decodeUtf8(this.uint8, start, size);
  }
 
  public ascii(length: number): string {
    this.assertSize(length);
    const uint8 = this.uint8;
    let str = '';
    const end = this.x + length;
    for (let i = this.x; i < end; i++) str += String.fromCharCode(uint8[i]);
    this.x = end;
    return str;
  }
 
  // -------------------------------------------------------- IReaderResettable
 
  public reset(uint8: Uint8Array): void {
    this.dx = 0;
    this.writer.reset();
    this.push(uint8);
  }
}