index.js
function readToMatch(stream, regexp, options) {
  const endOK = Boolean(options && (options.endOK || options.endOk));
  const maxMatchLen = Number(options && options.maxMatchLen);
  // Convert to RegExp where necessary, like String.prototype.match
  // Make sure RegExp has global flag so lastIndex will be set
  if (!(regexp instanceof RegExp)) {
    try {
      regexp = new RegExp(regexp, 'g');
    } catch (errRegExp) {
      // Note:  Synchronous Yaku emits unhandledRejection before returning.
      // Best current option is to use an async promise, even when flowing
      const ReadPromise = (options && options.Promise) || Promise;
      return ReadPromise.reject(errRegExp);
    }
  } else if (!regexp.global) {
    regexp = new RegExp(regexp.source, `${regexp.flags || ''}g`);
  }
  function until(result, chunk, ended) {
    if (ended) {
      return endOK ? (result ? result.length : 0) : -1;
    }

    if (typeof result !== 'string') {
      throw new TypeError('readToMatch requires a string stream'
          + ' (use constructor options.encoding or .setEncoding method)');
    }

    regexp.lastIndex = maxMatchLen
      ? Math.max((result.length - chunk.length - maxMatchLen) + 1, 0)
      : 0;
    if (regexp.test(result)) {
      return regexp.lastIndex;
    }

    return -1;
  }
  return readInternal(stream, undefined, until, options);
}
function readToMatch(stream, regexp, options) {
  const endOK = Boolean(options && (options.endOK || options.endOk));
  const maxMatchLen = Number(options && options.maxMatchLen);
  // Convert to RegExp where necessary, like String.prototype.match
  // Make sure RegExp has global flag so lastIndex will be set
  if (!(regexp instanceof RegExp)) {
    try {
      regexp = new RegExp(regexp, 'g');
    } catch (errRegExp) {
      // Note:  Synchronous Yaku emits unhandledRejection before returning.
      // Best current option is to use an async promise, even when flowing
      const ReadPromise = (options && options.Promise) || Promise;
      return ReadPromise.reject(errRegExp);
    }
  } else if (!regexp.global) {
    regexp = new RegExp(regexp.source, `${regexp.flags || ''}g`);
  }
  function until(result, chunk, ended) {
    if (ended) {
      return endOK ? (result ? result.length : 0) : -1;
    }

    if (typeof result !== 'string') {
      throw new TypeError('readToMatch requires a string stream'
          + ' (use constructor options.encoding or .setEncoding method)');
    }

    regexp.lastIndex = maxMatchLen
      ? Math.max((result.length - chunk.length - maxMatchLen) + 1, 0)
      : 0;
    if (regexp.test(result)) {
      return regexp.lastIndex;
    }

    return -1;
  }
  return readInternal(stream, undefined, until, options);
}
function readToMatch ( stream , regexp , options ) { const endOK = Boolean ( options && ( options . endOK || options . endOk ) ) ; const maxMatchLen = Number ( options && options . maxMatchLen ) ; // Convert to RegExp where necessary, like String.prototype.match // Make sure RegExp has global flag so lastIndex will be set if ( ! ( regexp instanceof RegExp ) ) { try { regexp = new RegExp ( regexp , 'g' ) ; } catch ( errRegExp ) { // Note:  Synchronous Yaku emits unhandledRejection before returning. // Best current option is to use an async promise, even when flowing const ReadPromise = ( options && options . Promise ) || Promise ; return ReadPromise . reject ( errRegExp ) ; } } else if ( ! regexp . global ) { regexp = new RegExp ( regexp . source , ` ${ regexp . flags || '' } ` ) ; } function until ( result , chunk , ended ) { if ( ended ) { return endOK ? ( result ? result . length : 0 ) : - 1 ; } if ( typeof result !== 'string' ) { throw new TypeError ( 'readToMatch requires a string stream' + ' (use constructor options.encoding or .setEncoding method)' ) ; } regexp . lastIndex = maxMatchLen ? Math . max ( ( result . length - chunk . length - maxMatchLen ) + 1 , 0 ) : 0 ; if ( regexp . test ( result ) ) { return regexp . lastIndex ; } return - 1 ; } return readInternal ( stream , undefined , until , options ) ; }
Reads from a stream.Readable until a given expression is matched.

<p>This function calls {@link readUntil} with an <code>until</code> function
which applies <code>regexp</code> to the data read.</p>

<p>Doc note:  options should be a ReadToMatchOptions type which extends
ReadToOptions, but record types can't currently be extended.
See {@link https://github.com/google/closure-compiler/issues/604}.</p>

@param {stream.Readable<string>} stream Stream from which to read.  This
stream must produce strings (so call <code>.setEncoding</code> if necessary).
@param {!RegExp|string} regexp Expression to find in the read result.
The stream will be read until this value is matched or <code>'end'</code> or
<code>'error'</code> is emitted.
@param {ReadOptions=} options Options.  This function additionally supports
an <code>endOK</code> option which prevents {@link EOFError} on
<code>'end'</code> and a <code>maxMatchLen</code> option which specifies
the maximum length of a match, which allow additional search optimizations.
@return {Promise<string>|CancellableReadPromise<string>} Promise with the
data read, up to and including the data matched by <code>regexp</code>, or
an Error if one occurs.  If <code>stream</code> does not support
<code>unshift</code>, the result may include additional data.  If
<code>'end'</code> is emitted before <code>regexp</code> is matched, an
{@link EOFError} is returned, unless <code>options.endOK</code> is truthy in
which case any remaining data is returned or <code>null</code> if none was
read.  If an error occurs after reading some data, the <code>.read</code>
property of the error object will contain the partial read result.  The
promise is resolved synchronously for streams in flowing mode (see README.md
for details).
Reads from a stream . Readable until a given expression is matched .