taoyuan/memstreams

View on GitHub
src/buffer-reader.ts

Summary

Maintainability
A
3 hrs
Test Coverage
A
100%
import {Readable} from 'readable-stream';
import {any2buf} from './utils';
import {MemReadable, MemReaderOptions} from './types';

export class BufferReader extends Readable implements MemReadable {
  it?: Iterator<any>;

  constructor(
    source?: Iterable<any> | Array<any>,
    options: MemReaderOptions = {},
  ) {
    options.objectMode = false;
    super(options);
    this.init(source, options);
  }

  init(source?: Iterable<any> | Array<any>, options: MemReaderOptions = {}) {
    this.it = source?.[Symbol.iterator]();
  }

  _read() {
    if (this.it) {
      const next = this.it.next();
      if (next.done) {
        this.push(null);
      } else {
        return this.push(
          any2buf(
            next.value,
            <BufferEncoding>this._readableState.encoding || 'utf8',
          ),
        );
      }
    }
  }
}