async function* readDelim(
  reader,
  delim,
) {
  // Avoid unicode problems
  const delimLen = delim.length;
  const delimLPS = createLPS(delim);
  let inputBuffer = new Buffer();
  const inspectArr = new Uint8Array(Math.max(1024, delimLen + 1));
  // Modified KMP
  let inspectIndex = 0;
  let matchIndex = 0;
  while (true) {
    const result = await reader.read(inspectArr);
    if (result === null) {
      // Yield last chunk.
      yield inputBuffer.bytes();
      return;
    }
    if (result < 0) {
      // Discard all remaining and silently fail.
      return;
    }
    const sliceRead = inspectArr.subarray(0, result);
    await writeAll(inputBuffer, sliceRead);
    let sliceToProcess = inputBuffer.bytes();
    while (inspectIndex < sliceToProcess.length) {
      if (sliceToProcess[inspectIndex] === delim[matchIndex]) {
        inspectIndex++;
        matchIndex++;
        if (matchIndex === delimLen) {
          // Full match
          const matchEnd = inspectIndex - delimLen;
          const readyBytes = sliceToProcess.subarray(0, matchEnd);
          // Copy
          const pendingBytes = sliceToProcess.slice(inspectIndex);
          yield readyBytes;
          // Reset match, different from KMP.
          sliceToProcess = pendingBytes;
          inspectIndex = 0;
          matchIndex = 0;
        }
      } else {
        if (matchIndex === 0) {
          inspectIndex++;
        } else {
          matchIndex = delimLPS[matchIndex - 1];
        }
      }
    }
    // Keep inspectIndex and matchIndex.
    inputBuffer = new Buffer(sliceToProcess);
  }
}
/** Read delimited bytes from a Reader. */
Read delimited bytes from a Reader.
Read delimited bytes from a Reader .
async function* readDelim(
  reader,
  delim,
) {
  const delimLen = delim.length;
  const delimLPS = createLPS(delim);
  let inputBuffer = new Buffer();
  const inspectArr = new Uint8Array(Math.max(1024, delimLen + 1));
  let inspectIndex = 0;
  let matchIndex = 0;
  while (true) {
    const result = await reader.read(inspectArr);
    if (result === null) {
      yield inputBuffer.bytes();
      return;
    }
    if (result < 0) {
      return;
    }
    const sliceRead = inspectArr.subarray(0, result);
    await writeAll(inputBuffer, sliceRead);
    let sliceToProcess = inputBuffer.bytes();
    while (inspectIndex < sliceToProcess.length) {
      if (sliceToProcess[inspectIndex] === delim[matchIndex]) {
        inspectIndex++;
        matchIndex++;
        if (matchIndex === delimLen) {
          const matchEnd = inspectIndex - delimLen;
          const readyBytes = sliceToProcess.subarray(0, matchEnd);
          const pendingBytes = sliceToProcess.slice(inspectIndex);
          yield readyBytes;
          sliceToProcess = pendingBytes;
          inspectIndex = 0;
          matchIndex = 0;
        }
      } else {
        if (matchIndex === 0) {
          inspectIndex++;
        } else {
          matchIndex = delimLPS[matchIndex - 1];
        }
      }
    }
    inputBuffer = new Buffer(sliceToProcess);
  }
}
async function * readDelim ( reader , delim , ) { const delimLen = delim . length ; const delimLPS = createLPS ( delim ) ; let inputBuffer = new Buffer ( ) ; const inspectArr = new Uint8Array ( Math . max ( 1024 , delimLen + 1 ) ) ; let inspectIndex = 0 ; let matchIndex = 0 ; while ( true ) { const result = await reader . read ( inspectArr ) ; if ( result === null ) { yield inputBuffer . bytes ( ) ; return ; } if ( result < 0 ) { return ; } const sliceRead = inspectArr . subarray ( 0 , result ) ; await writeAll ( inputBuffer , sliceRead ) ; let sliceToProcess = inputBuffer . bytes ( ) ; while ( inspectIndex < sliceToProcess . length ) { if ( sliceToProcess [ inspectIndex ] === delim [ matchIndex ] ) { inspectIndex ++ ; matchIndex ++ ; if ( matchIndex === delimLen ) { const matchEnd = inspectIndex - delimLen ; const readyBytes = sliceToProcess . subarray ( 0 , matchEnd ) ; const pendingBytes = sliceToProcess . slice ( inspectIndex ) ; yield readyBytes ; sliceToProcess = pendingBytes ; inspectIndex = 0 ; matchIndex = 0 ; } } else { if ( matchIndex === 0 ) { inspectIndex ++ ; } else { matchIndex = delimLPS [ matchIndex - 1 ] ; } } } inputBuffer = new Buffer ( sliceToProcess ) ; } }
Read delimited bytes from a Reader.
Read delimited bytes from a Reader .
// Avoid unicode problems // Modified KMP // Yield last chunk. // Discard all remaining and silently fail. // Full match // Copy // Reset match, different from KMP. // Keep inspectIndex and matchIndex.