Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(nodejs-client): handle SSE line endings and leading BOMs (#44263)

Winorenf70735bdc3dd
diff --git a/sdks/nodejs-client/src/http/sse.test.ts b/sdks/nodejs-client/src/http/sse.test.ts
index e3bbdd3f405..8328434f1c6 100644
--- a/sdks/nodejs-client/src/http/sse.test.ts
+++ b/sdks/nodejs-client/src/http/sse.test.ts
@@ -1,9 +1,83 @@
-import { Readable } from 'node:stream'
+import { PassThrough, Readable } from 'node:stream'
import { describe, expect, it } from 'vite-plus/test'
import { APIError } from '../errors/dify-error'
import { createBinaryStream, createSseStream, parseSseStream } from './sse'
describe('sse parsing', () => {
+ it.each(['\n', '\r\n', '\r'])(
+ 'ignores one leading UTF-8 BOM with %j line endings across byte boundaries',
+ async (ending) => {
+ const body = Buffer.from(`\uFEFFdata: {"answer":"你好"}${ending}${ending}`)
+ const chunks = [Buffer.alloc(0), ...Array.from(body, (byte) => Buffer.from([byte]))]
+ const events = []
+ for await (const event of parseSseStream(Readable.from(chunks))) {
+ events.push(event)
+ }
+ expect(events).toEqual([
+ { event: undefined, data: { answer: '你好' }, raw: '{"answer":"你好"}' },
+ ])
+ },
+ )
+
+ it('preserves BOM characters in payloads and does not strip later field prefixes', async () => {
+ const body = 'data: {"answer":"\uFEFFkept"}\n\n\uFEFFdata: {"answer":"ignored"}\n\n'
+ const stream = createSseStream(Readable.from([body]), { status: 200, headers: {} })
+ expect(await stream.toText()).toBe('\uFEFFkept')
+ })
+
+ it.each(['\n', '\r\n', '\r'])(
+ 'parses events with %j line endings across byte boundaries',
+ async (ending) => {
+ const body = Buffer.from(
+ [
+ ': keep-alive',
+ 'event: message',
+ 'data: {"answer":"你好"}',
+ '',
+ 'data: {"delta":" world"}',
+ '',
+ '',
+ ].join(ending),
+ )
+ const chunks = Array.from(body, (byte) => Buffer.from([byte]))
+ const events = []
+ for await (const event of parseSseStream(Readable.from(chunks))) {
+ events.push(event)
+ }
+ expect(events).toEqual([
+ { event: 'message', data: { answer: '你好' }, raw: '{"answer":"你好"}' },
+ { event: undefined, data: { delta: ' world' }, raw: '{"delta":" world"}' },
+ ])
+ },
+ )
+
+ it('emits a CR-delimited event before the stream closes', async () => {
+ const stream = new PassThrough()
+ const iterator = parseSseStream(stream)[Symbol.asyncIterator]()
+ try {
+ const next = iterator.next()
+ stream.write('data: first\r\r')
+ expect(await next).toEqual({
+ done: false,
+ value: { event: undefined, data: 'first', raw: 'first' },
+ })
+ } finally {
+ stream.end()
+ await iterator.return?.()
+ }
+ })
+
+ it('toText accepts mixed SSE line endings', async () => {
+ const stream = Readable.from([
+ 'event: message\rdata: {"answer":"Hello"}\r\r',
+ 'data: {"delta":" world"}\r\n\r\n',
+ 'data: {"delta":"!"}\n\n',
+ ])
+ expect(await createSseStream(stream, { status: 200, headers: {} }).toText()).toBe(
+ 'Hello world!',
+ )
+ })
+
it.each([
['data: indented', ' indented'],
['data:\tindented', '\tindented'],
diff --git a/sdks/nodejs-client/src/http/sse.ts b/sdks/nodejs-client/src/http/sse.ts
index 04584b9b80f..5ebe3a9e294 100644
--- a/sdks/nodejs-client/src/http/sse.ts
+++ b/sdks/nodejs-client/src/http/sse.ts
@@ -17,17 +17,31 @@ const toBufferChunk = (chunk: unknown): Buffer => {
const readLines = async function* (stream: Readable): AsyncIterable<string> {
const decoder = new StringDecoder('utf8')
let buffered = ''
+ let skipLeadingLf = false
for await (const chunk of stream) {
- buffered += decoder.write(toBufferChunk(chunk))
- let index = buffered.indexOf('\n')
+ let decoded = decoder.write(toBufferChunk(chunk))
+ if (skipLeadingLf && decoded.length > 0) {
+ if (decoded.startsWith('\n')) {
+ decoded = decoded.slice(1)
+ }
+ skipLeadingLf = false
+ }
+ buffered += decoded
+ let index = buffered.search(/[\r\n]/)
while (index >= 0) {
- let line = buffered.slice(0, index)
+ const line = buffered.slice(0, index)
+ const carriageReturn = buffered[index] === '\r'
buffered = buffered.slice(index + 1)
- if (line.endsWith('\r')) {
- line = line.slice(0, -1)
+ if (carriageReturn) {
+ if (buffered.startsWith('\n')) {
+ buffered = buffered.slice(1)
+ } else if (buffered.length === 0) {
+ // A CRLF pair may be split between transport chunks.
+ skipLeadingLf = true
+ }
}
yield line
- index = buffered.indexOf('\n')
+ index = buffered.search(/[\r\n]/)
}
}
buffered += decoder.end()
@@ -66,7 +80,14 @@ export const parseSseStream = async function* <T>(stream: Readable): AsyncIterab
dataLines.length = 0
}
- for await (const line of readLines(stream)) {
+ let firstLine = true
+ for await (let line of readLines(stream)) {
+ if (firstLine) {
+ firstLine = false
+ if (line.startsWith('\uFEFF')) {
+ line = line.slice(1)
+ }
+ }
if (!line) {
yield* emitEvent()
continue