index.js
function tryUnshift(stream, result, desiredLength, emptySlice) {
  if (typeof stream.unshift !== 'function') {
    debug('Unable to unshift, stream does not have an unshift method.');
    return result;
  }

  const errorListeners = stream.listeners('error');
  stream.removeAllListeners('error');

  // Note:  Don't rely on the EventEmitter throwing on 'error' without
  // listeners, since it may be thrown in the stream's attached domain.
  let unshiftErr;
  function onUnshiftError(err) { unshiftErr = err; }
  stream.on('error', onUnshiftError);

  let resultLength = result.length;
  try {
    if (Array.isArray(result)) {
      while (resultLength > desiredLength && !unshiftErr) {
        stream.unshift(result[resultLength - 1]);
        if (!unshiftErr) {
          resultLength -= 1;
        }
      }
    } else {
      stream.unshift(result.slice(desiredLength));
      if (!unshiftErr) {
        resultLength = desiredLength;
      }
    }
  } catch (err) {
    unshiftErr = err;
  }

  if (unshiftErr) {
    debug('Unable to unshift data: ', unshiftErr);
  }

  stream.removeListener('error', onUnshiftError);
  errorListeners.forEach((errorListener) => {
    stream.on('error', errorListener);
  });

  return resultLength === 0 && !emptySlice ? null
    : resultLength < result.length ? result.slice(0, resultLength)
      : result;
}
function tryUnshift(stream, result, desiredLength, emptySlice) {
  if (typeof stream.unshift !== 'function') {
    debug('Unable to unshift, stream does not have an unshift method.');
    return result;
  }

  const errorListeners = stream.listeners('error');
  stream.removeAllListeners('error');

  // Note:  Don't rely on the EventEmitter throwing on 'error' without
  // listeners, since it may be thrown in the stream's attached domain.
  let unshiftErr;
  function onUnshiftError(err) { unshiftErr = err; }
  stream.on('error', onUnshiftError);

  let resultLength = result.length;
  try {
    if (Array.isArray(result)) {
      while (resultLength > desiredLength && !unshiftErr) {
        stream.unshift(result[resultLength - 1]);
        if (!unshiftErr) {
          resultLength -= 1;
        }
      }
    } else {
      stream.unshift(result.slice(desiredLength));
      if (!unshiftErr) {
        resultLength = desiredLength;
      }
    }
  } catch (err) {
    unshiftErr = err;
  }

  if (unshiftErr) {
    debug('Unable to unshift data: ', unshiftErr);
  }

  stream.removeListener('error', onUnshiftError);
  errorListeners.forEach((errorListener) => {
    stream.on('error', errorListener);
  });

  return resultLength === 0 && !emptySlice ? null
    : resultLength < result.length ? result.slice(0, resultLength)
      : result;
}
function tryUnshift ( stream , result , desiredLength , emptySlice ) { if ( typeof stream . unshift !== 'function' ) { debug ( 'Unable to unshift, stream does not have an unshift method.' ) ; return result ; } const errorListeners = stream . listeners ( 'error' ) ; stream . removeAllListeners ( 'error' ) ; // Note:  Don't rely on the EventEmitter throwing on 'error' without // listeners, since it may be thrown in the stream's attached domain. let unshiftErr ; function onUnshiftError ( err ) { unshiftErr = err ; } stream . on ( 'error' , onUnshiftError ) ; let resultLength = result . length ; try { if ( Array . isArray ( result ) ) { while ( resultLength > desiredLength && ! unshiftErr ) { stream . unshift ( result [ resultLength - 1 ] ) ; if ( ! unshiftErr ) { resultLength -= 1 ; } } } else { stream . unshift ( result . slice ( desiredLength ) ) ; if ( ! unshiftErr ) { resultLength = desiredLength ; } } } catch ( err ) { unshiftErr = err ; } if ( unshiftErr ) { debug ( 'Unable to unshift data: ' , unshiftErr ) ; } stream . removeListener ( 'error' , onUnshiftError ) ; errorListeners . forEach ( ( errorListener ) => { stream . on ( 'error' , errorListener ) ; } ) ; return resultLength === 0 && ! emptySlice ? null : resultLength < result . length ? result . slice ( 0 , resultLength ) : result ; }
Attempts to unshift result data down to a desired length.
@param {stream.Readable} stream Stream into which to unshift data.
@param {!Buffer|string|!Array} result Read result data.
@param {number} desiredLength Desired length of result after unshifting.
@param {boolean=} emptySlice Return an empty slice when all data is
unshifted, rather than <code>null</code>.
@return {Buffer|string|Array} Result data after unshifting, or
<code>null</code> if all data was unshifted and <code>emptySlice</code> is
falsey.
@private
Attempts to unshift result data down to a desired length .