-
-
Notifications
You must be signed in to change notification settings - Fork 546
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
102 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,21 @@ | ||
import type { Context } from '../../context.ts' | ||
import { Context, HeaderRecord, TEXT_PLAIN } from '../../context.ts' | ||
import { StatusCode } from '../../utils/http-status.ts' | ||
import { StreamingApi } from '../../utils/stream.ts' | ||
|
||
interface SSEMessage { | ||
data: string | ||
event?: string | ||
id?: string | ||
export const stream = ( | ||
c: Context, | ||
cb: (stream: StreamingApi) => Promise<void>, | ||
arg?: StatusCode | ResponseInit, | ||
headers?: HeaderRecord | ||
): Response => { | ||
const { readable, writable } = new TransformStream() | ||
const stream = new StreamingApi(writable) | ||
cb(stream).finally(() => stream.close()) | ||
|
||
return typeof arg === 'number' | ||
? c.newResponse(readable, arg, headers) | ||
: c.newResponse(readable, arg) | ||
} | ||
|
||
class SSEStreamingApi extends StreamingApi { | ||
constructor(writable: WritableStream) { | ||
super(writable) | ||
} | ||
|
||
async writeSSE(message: SSEMessage) { | ||
const data = message.data | ||
.split('\n') | ||
.map((line) => { | ||
return `data: ${line}` | ||
}) | ||
.join('\n') | ||
|
||
const sseData = | ||
[message.event && `event: ${message.event}`, data, message.id && `id: ${message.id}`] | ||
.filter(Boolean) | ||
.join('\n') + '\n\n' | ||
|
||
await this.write(sseData) | ||
} | ||
} | ||
|
||
const setSSEHeaders = (context: Context) => { | ||
context.header('Transfer-Encoding', 'chunked') | ||
context.header('Content-Type', 'text/event-stream') | ||
context.header('Cache-Control', 'no-cache') | ||
context.header('Connection', 'keep-alive') | ||
} | ||
|
||
export const streamSSE = (c: Context, cb: (stream: SSEStreamingApi) => Promise<void>) => { | ||
return c.stream(async (originalStream: StreamingApi) => { | ||
const { readable, writable } = new TransformStream() | ||
const stream = new SSEStreamingApi(writable) | ||
|
||
originalStream.pipe(readable).catch((err) => { | ||
console.error('Error in stream piping: ', err) | ||
stream.close() | ||
}) | ||
|
||
setSSEHeaders(c) | ||
|
||
try { | ||
await cb(stream) | ||
} catch (err) { | ||
console.error('Error during streaming: ', err) | ||
} finally { | ||
await stream.close() | ||
} | ||
}) | ||
} | ||
export { streamSSE } from './sse.ts' | ||
export { streamText } from './text.ts' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { stream } from './index.ts' | ||
import type { Context } from '../../context.ts' | ||
import { StreamingApi } from '../../utils/stream.ts' | ||
|
||
interface SSEMessage { | ||
data: string | ||
event?: string | ||
id?: string | ||
} | ||
|
||
class SSEStreamingApi extends StreamingApi { | ||
constructor(writable: WritableStream) { | ||
super(writable) | ||
} | ||
|
||
async writeSSE(message: SSEMessage) { | ||
const data = message.data | ||
.split('\n') | ||
.map((line) => { | ||
return `data: ${line}` | ||
}) | ||
.join('\n') | ||
|
||
const sseData = | ||
[message.event && `event: ${message.event}`, data, message.id && `id: ${message.id}`] | ||
.filter(Boolean) | ||
.join('\n') + '\n\n' | ||
|
||
await this.write(sseData) | ||
} | ||
} | ||
|
||
const setSSEHeaders = (context: Context) => { | ||
context.header('Transfer-Encoding', 'chunked') | ||
context.header('Content-Type', 'text/event-stream') | ||
context.header('Cache-Control', 'no-cache') | ||
context.header('Connection', 'keep-alive') | ||
} | ||
|
||
export const streamSSE = (c: Context, cb: (stream: SSEStreamingApi) => Promise<void>) => { | ||
return stream(c, async (originalStream: StreamingApi) => { | ||
const { readable, writable } = new TransformStream() | ||
const stream = new SSEStreamingApi(writable) | ||
|
||
originalStream.pipe(readable).catch((err) => { | ||
console.error('Error in stream piping: ', err) | ||
stream.close() | ||
}) | ||
|
||
setSSEHeaders(c) | ||
|
||
try { | ||
await cb(stream) | ||
} catch (err) { | ||
console.error('Error during streaming: ', err) | ||
} finally { | ||
await stream.close() | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { stream } from './index.ts' | ||
import { Context, HeaderRecord, TEXT_PLAIN } from '../../context.ts' | ||
import { StatusCode } from '../../utils/http-status.ts' | ||
import { StreamingApi } from '../../utils/stream.ts' | ||
|
||
export const streamText = ( | ||
c: Context, | ||
cb: (stream: StreamingApi) => Promise<void>, | ||
arg?: StatusCode | ResponseInit, | ||
headers?: HeaderRecord | ||
): Response => { | ||
headers ??= {} | ||
c.header('content-type', TEXT_PLAIN) | ||
c.header('x-content-type-options', 'nosniff') | ||
c.header('transfer-encoding', 'chunked') | ||
return stream(c, cb, arg, headers) | ||
} |