Language: TypeScript
Label: safe
CWE: CWE-331 \u2014 Insufficient Entropy
The product uses an algorithm or scheme that produces insufficient entropy, leaving patterns or clusters of values that are more likely to occur than others.

Code:
  static tryGetMetadataSizeFromStream(stream: NodeJS.ReadableStream) {
    const sizeBuf = stream.read(1);
    if (sizeBuf && (sizeBuf[0] as number) < 255) {
      return sizeBuf[0] as number;
    }
    if ((sizeBuf[0] as number) === 255) {
      const nextBuf = stream.read(2);
      if (nextBuf.length >= 2) {
        return new Uint16Array([nextBuf[0] as number, nextBuf[1] as number]).reduce((acc, val) => (acc << 8) + val, 0);
      }
    }
    throw new Error('decryption error. Invalid metadata size');
  }