File size: 891 Bytes
ddce7e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import { getLineBuffer, releaseLineBuffer, parseAndEmitStreamChunk } from './stream_parser.js';

export function createStreamLineProcessor({ state, onEvent, onRawChunk } = {}) {
  if (!state || typeof state !== 'object') throw new Error('createStreamLineProcessor: state is required');
  if (typeof onEvent !== 'function') throw new Error('createStreamLineProcessor: onEvent callback is required');

  const lineBuffer = getLineBuffer();

  const processChunk = (chunk) => {
    const text = typeof chunk === 'string' ? chunk : chunk?.toString?.('utf8') ?? String(chunk);
    if (typeof onRawChunk === 'function') onRawChunk(text);

    const lines = lineBuffer.append(text);
    for (let i = 0; i < lines.length; i++) {
      parseAndEmitStreamChunk(lines[i], state, onEvent);
    }
  };

  const close = () => {
    releaseLineBuffer(lineBuffer);
  };

  return { processChunk, close };
}