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): preserve SSE field value whitespace (#44083)

D41910c75558e986e5
diff --git a/sdks/nodejs-client/src/http/sse.test.ts b/sdks/nodejs-client/src/http/sse.test.ts
index bce014d30b9..843dde6bea0 100644
--- a/sdks/nodejs-client/src/http/sse.test.ts
+++ b/sdks/nodejs-client/src/http/sse.test.ts
@@ -3,6 +3,36 @@ import { describe, expect, it } from 'vite-plus/test'
import { createBinaryStream, createSseStream, parseSseStream } from './sse'
describe('sse parsing', () => {
+ it.each([
+ ['data: indented', ' indented'],
+ ['data:\tindented', '\tindented'],
+ ['data: \tindented', '\tindented'],
+ ['data:\u00A0indented', '\u00A0indented'],
+ ['data:plain', 'plain'],
+ ['data: plain', 'plain'],
+ ])('preserves payload whitespace in %s', async (line, expected) => {
+ const events = []
+ for await (const event of parseSseStream(Readable.from([`${line}\n\n`]))) {
+ events.push(event)
+ }
+ expect(events).toEqual([{ event: undefined, data: expected, raw: expected }])
+ })
+
+ it('keeps meaningful whitespace in event names', async () => {
+ const events = []
+ for await (const event of parseSseStream(Readable.from(['event: custom \ndata: text\n\n']))) {
+ events.push(event)
+ }
+ expect(events[0]?.event).toBe(' custom ')
+ })
+
+ it('toText preserves multi-line indentation', async () => {
+ const stream = Readable.from(['data: first\ndata: \tsecond\n\n'])
+ expect(await createSseStream(stream, { status: 200, headers: {} }).toText()).toBe(
+ ' first\n\tsecond',
+ )
+ })
+
it('parses event and data lines', async () => {
const stream = Readable.from(['event: message\n', 'data: {"answer":"hi"}\n', '\n'])
const events: Array<{ event?: string; data: unknown; raw: string }> = []
diff --git a/sdks/nodejs-client/src/http/sse.ts b/sdks/nodejs-client/src/http/sse.ts
index d7716652061..76a66655b99 100644
--- a/sdks/nodejs-client/src/http/sse.ts
+++ b/sdks/nodejs-client/src/http/sse.ts
@@ -74,11 +74,11 @@ export const parseSseStream = async function* <T>(stream: Readable): AsyncIterab
continue
}
if (line.startsWith('event:')) {
- eventName = line.slice('event:'.length).trim()
+ eventName = line.slice('event:'.length).replace(/^ /, '')
continue
}
if (line.startsWith('data:')) {
- dataLines.push(line.slice('data:'.length).trimStart())
+ dataLines.push(line.slice('data:'.length).replace(/^ /, ''))
continue
}
}