Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(types): fix accidental exposure of Buffer type to cloudflare #709

Merged
merged 1 commit into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/streaming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ class SSEDecoder {
*
* https://github.com/encode/httpx/blob/920333ea98118e9cf617f246905d7b202510941c/httpx/_decoders.py#L258
*/
export class LineDecoder {
class LineDecoder {
// prettier-ignore
static NEWLINE_CHARS = new Set(['\n', '\r', '\x0b', '\x0c', '\x1c', '\x1d', '\x1e', '\x85', '\u2028', '\u2029']);
static NEWLINE_REGEXP = /\r\n|[\n\r\x0b\x0c\x1c\x1d\x1e\x85\u2028\u2029]/g;
Expand Down Expand Up @@ -372,6 +372,17 @@ export class LineDecoder {
}
}

/** This is an internal helper function that's just used for testing */
export function _decodeChunks(chunks: string[]): string[] {
const decoder = new LineDecoder();
const lines = [];
for (const chunk of chunks) {
lines.push(...decoder.decode(chunk));
}

return lines;
}

function partition(str: string, delimiter: string): [string, string, string] {
const index = str.indexOf(delimiter);
if (index !== -1) {
Expand Down
15 changes: 1 addition & 14 deletions tests/streaming.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,4 @@
import { LineDecoder } from 'openai/streaming';

function decodeChunks(chunks: string[], decoder?: LineDecoder): string[] {
if (!decoder) {
decoder = new LineDecoder();
}

const lines = [];
for (const chunk of chunks) {
lines.push(...decoder.decode(chunk));
}

return lines;
}
import { _decodeChunks as decodeChunks } from 'openai/streaming';

describe('line decoder', () => {
test('basic', () => {
Expand Down