lib/streams.js
function slice(input, begin=0, end=Infinity) {
  if (isStream(input)) {
    if (begin >= 0 && end >= 0) {
      let bytesRead = 0;
      return transformRaw(input, {
        transform(value, controller) {
          if (bytesRead < end) {
            if (bytesRead + value.length >= begin) {
              controller.enqueue(slice(value, Math.max(begin - bytesRead, 0), end - bytesRead));
            }
            bytesRead += value.length;
          } else {
            controller.terminate();
          }
        }
      });
    }
    if (begin < 0 && (end < 0 || end === Infinity)) {
      let lastBytes = [];
      return transform(input, value => {
        if (value.length >= -begin) lastBytes = [value];
        else lastBytes.push(value);
      }, () => slice(concat(lastBytes), begin, end));
    }
    if (begin === 0 && end < 0) {
      let lastBytes;
      return transform(input, value => {
        const returnValue = lastBytes ? concat([lastBytes, value]) : value;
        if (returnValue.length >= -end) {
          lastBytes = slice(returnValue, end);
          return slice(returnValue, begin, end);
        } else {
          lastBytes = returnValue;
        }
      });
    }
    console.warn(`stream.slice(input, ${begin}, ${end}) not implemented efficiently.`);
    return fromAsync(async () => slice(await readToEnd(input), begin, end));
  }
  if (input[externalBuffer]) {
    input = concat(input[externalBuffer].concat([input]));
  }
  if (isUint8Array(input) && !(NodeBuffer && NodeBuffer.isBuffer(input))) {
    if (end === Infinity) end = input.length;
    return input.subarray(begin, end);
  }
  return input.slice(begin, end);
}
function slice(input, begin=0, end=Infinity) {
  if (isStream(input)) {
    if (begin >= 0 && end >= 0) {
      let bytesRead = 0;
      return transformRaw(input, {
        transform(value, controller) {
          if (bytesRead < end) {
            if (bytesRead + value.length >= begin) {
              controller.enqueue(slice(value, Math.max(begin - bytesRead, 0), end - bytesRead));
            }
            bytesRead += value.length;
          } else {
            controller.terminate();
          }
        }
      });
    }
    if (begin < 0 && (end < 0 || end === Infinity)) {
      let lastBytes = [];
      return transform(input, value => {
        if (value.length >= -begin) lastBytes = [value];
        else lastBytes.push(value);
      }, () => slice(concat(lastBytes), begin, end));
    }
    if (begin === 0 && end < 0) {
      let lastBytes;
      return transform(input, value => {
        const returnValue = lastBytes ? concat([lastBytes, value]) : value;
        if (returnValue.length >= -end) {
          lastBytes = slice(returnValue, end);
          return slice(returnValue, begin, end);
        } else {
          lastBytes = returnValue;
        }
      });
    }
    console.warn(`stream.slice(input, ${begin}, ${end}) not implemented efficiently.`);
    return fromAsync(async () => slice(await readToEnd(input), begin, end));
  }
  if (input[externalBuffer]) {
    input = concat(input[externalBuffer].concat([input]));
  }
  if (isUint8Array(input) && !(NodeBuffer && NodeBuffer.isBuffer(input))) {
    if (end === Infinity) end = input.length;
    return input.subarray(begin, end);
  }
  return input.slice(begin, end);
}
function slice ( input , begin = 0 , end = Infinity ) { if ( isStream ( input ) ) { if ( begin >= 0 && end >= 0 ) { let bytesRead = 0 ; return transformRaw ( input , { transform ( value , controller ) { if ( bytesRead < end ) { if ( bytesRead + value . length >= begin ) { controller . enqueue ( slice ( value , Math . max ( begin - bytesRead , 0 ) , end - bytesRead ) ) ; } bytesRead += value . length ; } else { controller . terminate ( ) ; } } } ) ; } if ( begin < 0 && ( end < 0 || end === Infinity ) ) { let lastBytes = [ ] ; return transform ( input , value => { if ( value . length >= - begin ) lastBytes = [ value ] ; else lastBytes . push ( value ) ; } , ( ) => slice ( concat ( lastBytes ) , begin , end ) ) ; } if ( begin === 0 && end < 0 ) { let lastBytes ; return transform ( input , value => { const returnValue = lastBytes ? concat ( [ lastBytes , value ] ) : value ; if ( returnValue . length >= - end ) { lastBytes = slice ( returnValue , end ) ; return slice ( returnValue , begin , end ) ; } else { lastBytes = returnValue ; } } ) ; } console . warn ( ` ${ begin } ${ end } ` ) ; return fromAsync ( async ( ) => slice ( await readToEnd ( input ) , begin , end ) ) ; } if ( input [ externalBuffer ] ) { input = concat ( input [ externalBuffer ] . concat ( [ input ] ) ) ; } if ( isUint8Array ( input ) && ! ( NodeBuffer && NodeBuffer . isBuffer ( input ) ) ) { if ( end === Infinity ) end = input . length ; return input . subarray ( begin , end ) ; } return input . slice ( begin , end ) ; }
Return a stream pointing to a part of the input stream.
@param {ReadableStream|Uint8array|String} input
@returns {ReadableStream|Uint8array|String} clone
Return a stream pointing to a part of the input stream .