Path: blob/main/extensions/copilot/src/extension/onboardDebug/node/copilotDebugWorker/streamSplitter.ts
13405 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45// Do not edit, from https://github.com/microsoft/vscode-js-debug/blob/272c88ddf0b806325a1c723b6369d524d6e9409b/src/common/streamSplitter.ts#L167import { Transform } from 'stream';89/**10* A Transform stream that splits the input on the "splitter" substring.11* The resulting chunks will contain (and trail with) the splitter match.12* The last chunk when the stream ends will be emitted even if a splitter13* is not encountered.14*/15export class StreamSplitter extends Transform {16private prefix: Buffer[] = [];17private readonly splitter: number;1819/** Suffix added after each split chunk. */20protected splitSuffix = Buffer.alloc(0);2122constructor(splitter: string | number | Buffer) {23super();24if (typeof splitter === 'string' && splitter.length === 1) {25this.splitter = splitter.charCodeAt(0);26} else if (typeof splitter === 'number') {27this.splitter = splitter;28} else {29throw new Error('not implemented here');30}31}3233override _transform(34chunk: Buffer,35_encoding: string,36callback: (error?: Error | null, data?: unknown) => void,37): void {38let offset = 0;39while (offset < chunk.length) {40const index = chunk.indexOf(this.splitter, offset);41if (index === -1) {42break;43}4445const thisChunk = chunk.subarray(offset, index);46const toEmit =47this.prefix.length || this.splitSuffix.length48? Buffer.concat([...this.prefix, thisChunk, this.splitSuffix])49: thisChunk;5051this.push(toEmit);52this.prefix.length = 0;53offset = index + 1;54}5556if (offset < chunk.length) {57this.prefix.push(chunk.subarray(offset));58}5960callback();61}6263override _flush(callback: (error?: Error | null, data?: unknown) => void): void {64if (this.prefix.length) {65this.push(Buffer.concat([...this.prefix, this.splitSuffix]));66}6768callback();69}70}717273