@obarlik/streaming-pipeline-core
Version:
🔄 Memory-efficient circular buffer streaming pipeline with universal processing - by Codechu
78 lines (77 loc) • 2.62 kB
JavaScript
/**
* Streaming interfaces - single buffer system
* Universal approach with circular buffer and bounded lookahead/lookbehind
*/
import { TextCircularBuffer } from './CircularStreamBuffer';
// Helper functions for processors
export class StreamingUtils {
/**
* Check if current position matches a pattern
*/
static matchesPattern(context, pattern) {
if (typeof pattern === 'string') {
return context.buffer instanceof TextCircularBuffer &&
context.buffer.lookAheadString(pattern.length) === pattern;
}
else {
// For regex, we need to get a reasonable chunk to test
const testString = context.buffer instanceof TextCircularBuffer ?
context.buffer.lookAheadString(100) : '';
return pattern.test(testString);
}
}
/**
* Get text content around current position
*/
static getContext(context, behind = 10, ahead = 10) {
if (!(context.buffer instanceof TextCircularBuffer))
return '';
const beforeText = context.buffer.lookBehindString(behind);
const currentChar = context.buffer.peekChar() || '';
const afterText = context.buffer.lookAheadString(ahead);
return beforeText + currentChar + afterText;
}
/**
* Find next occurrence of pattern
*/
static findNext(context, pattern, maxDistance = 100) {
if (!(context.buffer instanceof TextCircularBuffer))
return -1;
const searchText = context.buffer.lookAheadString(maxDistance);
return searchText.indexOf(pattern);
}
/**
* Check if current position is at word boundary
*/
static isWordBoundary(context) {
if (!(context.buffer instanceof TextCircularBuffer))
return false;
const current = context.buffer.peekChar();
const before = context.buffer.lookBehindString(1);
if (!current)
return true;
const isCurrentWord = /\w/.test(current);
const isBeforeWord = before && /\w/.test(before);
return isCurrentWord !== isBeforeWord;
}
}
/**
* Base processor class with common functionality
*/
export class BaseStreamProcessor {
constructor() {
this.preferredLookBehind = 64;
this.preferredLookAhead = 256;
this.encoding = 'utf-8';
}
// Helper method to create chunks
createChunk(type, content, data, position) {
return {
type,
content,
data,
position,
metadata: { processor: this.name }
};
}
}