chore: 纳入本地 Dify 1.19.0 源码及品牌与免登改动
演示目录只保留可重建的源码。运行密钥、数据卷、依赖和带口令的本地配置不进入仓库。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
60158935f4
commit
6519682885
15831 files changed
+3063428
No files matched your search
@@ -0,0 +1,132 @@
|
||||
import { execFile } from 'node:child_process'
|
||||
import { promisify } from 'node:util'
|
||||
import { describe, expect, it } from 'vite-plus/test'
|
||||
import { parseSSE } from './sse'
|
||||
|
||||
function streamOf(...chunks: string[]): ReadableStream<Uint8Array> {
|
||||
const enc = new TextEncoder()
|
||||
return new ReadableStream({
|
||||
start(c) {
|
||||
for (const ch of chunks) c.enqueue(enc.encode(ch))
|
||||
c.close()
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
async function collect(
|
||||
s: ReadableStream<Uint8Array>,
|
||||
): Promise<{ name: string; data: string; id?: string }[]> {
|
||||
const out: { name: string; data: string; id?: string }[] = []
|
||||
const dec = new TextDecoder()
|
||||
for await (const ev of parseSSE(s))
|
||||
out.push({ name: ev.name, data: dec.decode(ev.data), id: ev.id })
|
||||
return out
|
||||
}
|
||||
|
||||
describe('parseSSE', () => {
|
||||
it('emits one event per blank-line-terminated record', async () => {
|
||||
const s = streamOf('event: message\ndata: hello\n\nevent: ping\ndata: \n\n')
|
||||
const got = await collect(s)
|
||||
expect(got).toEqual([
|
||||
{ name: 'message', data: 'hello', id: undefined },
|
||||
{ name: 'ping', data: '', id: undefined },
|
||||
])
|
||||
})
|
||||
|
||||
it('joins multi-line data with newlines', async () => {
|
||||
const s = streamOf('event: message\ndata: line1\ndata: line2\n\n')
|
||||
const got = await collect(s)
|
||||
expect(got[0]?.data).toBe('line1\nline2')
|
||||
})
|
||||
|
||||
it('propagates id field', async () => {
|
||||
const s = streamOf('id: 42\nevent: m\ndata: x\n\n')
|
||||
expect((await collect(s))[0]?.id).toBe('42')
|
||||
})
|
||||
|
||||
it('skips comment lines', async () => {
|
||||
const s = streamOf(': comment\nevent: m\ndata: x\n\n')
|
||||
expect(await collect(s)).toEqual([{ name: 'm', data: 'x', id: undefined }])
|
||||
})
|
||||
|
||||
it('survives chunk boundaries inside a field', async () => {
|
||||
const s = streamOf('event: mes', 'sage\nda', 'ta: hel', 'lo\n\n')
|
||||
expect((await collect(s))[0]).toEqual({ name: 'message', data: 'hello', id: undefined })
|
||||
})
|
||||
|
||||
it('handles multi-byte utf-8 split across chunks', async () => {
|
||||
const enc = new TextEncoder().encode('event: m\ndata: 😀\n\n')
|
||||
const a = enc.slice(0, 14)
|
||||
const b = enc.slice(14)
|
||||
const s = new ReadableStream<Uint8Array>({
|
||||
start(c) {
|
||||
c.enqueue(a)
|
||||
c.enqueue(b)
|
||||
c.close()
|
||||
},
|
||||
})
|
||||
expect((await collect(s))[0]?.data).toBe('😀')
|
||||
})
|
||||
|
||||
it('propagates a response body error to the consumer', async () => {
|
||||
const error = new Error('network disconnected')
|
||||
const body = new ReadableStream<Uint8Array>({
|
||||
start(controller) {
|
||||
controller.error(error)
|
||||
},
|
||||
})
|
||||
await expect(collect(body)).rejects.toBe(error)
|
||||
})
|
||||
|
||||
it('observes read failures while the consumer is processing a yielded event', async () => {
|
||||
// Isolate Node's strict unhandled-rejection policy from the test runner.
|
||||
const source = new URL('./sse.ts', import.meta.url).href
|
||||
const script = `
|
||||
import assert from 'node:assert/strict'
|
||||
import { setImmediate } from 'node:timers/promises'
|
||||
import { parseSSE } from ${JSON.stringify(source)}
|
||||
let controller
|
||||
const body = new ReadableStream({ start(c) { controller = c } })
|
||||
const events = parseSSE(body)
|
||||
controller.enqueue(new TextEncoder().encode('data: first\\n\\ndata: second\\n\\n'))
|
||||
const decoder = new TextDecoder()
|
||||
assert.equal(decoder.decode((await events.next()).value.data), 'first')
|
||||
const error = new Error('network disconnected')
|
||||
controller.error(error)
|
||||
await setImmediate()
|
||||
assert.equal(decoder.decode((await events.next()).value.data), 'second')
|
||||
await assert.rejects(events.next(), (failure) => failure === error)
|
||||
console.log('stream error reached the consumer')
|
||||
`
|
||||
const { stdout, stderr } = await promisify(execFile)(process.execPath, [
|
||||
'--unhandled-rejections=strict',
|
||||
'--input-type=module',
|
||||
'-e',
|
||||
script,
|
||||
])
|
||||
expect(stdout.trim()).toBe('stream error reached the consumer')
|
||||
expect(stderr).toBe('')
|
||||
})
|
||||
|
||||
it('aborts when signal fires', async () => {
|
||||
const ctrl = new AbortController()
|
||||
const slow = new ReadableStream<Uint8Array>({
|
||||
pull(c) {
|
||||
c.enqueue(new TextEncoder().encode('event: m\ndata: x\n\n'))
|
||||
},
|
||||
})
|
||||
let seen = 0
|
||||
let caught: unknown
|
||||
try {
|
||||
for await (const _ of parseSSE(slow, ctrl.signal)) {
|
||||
seen++
|
||||
if (seen === 1) ctrl.abort()
|
||||
}
|
||||
} catch (e) {
|
||||
caught = e
|
||||
}
|
||||
expect(seen).toBeGreaterThanOrEqual(1)
|
||||
expect(seen).toBeLessThan(50)
|
||||
expect((caught as Error).name).toBe('AbortError')
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user