remix-utils
Advanced tools
Comparing version 6.5.0 to 6.6.0
@@ -14,4 +14,7 @@ interface SendFunctionArgs { | ||
} | ||
interface AbortFunction { | ||
(): void; | ||
} | ||
interface InitFunction { | ||
(send: SendFunction): CleanupFunction; | ||
(send: SendFunction, abort: AbortFunction): CleanupFunction; | ||
} | ||
@@ -24,3 +27,3 @@ /** | ||
*/ | ||
export declare function eventStream(signal: AbortSignal, init: InitFunction): Response; | ||
export declare function eventStream(signal: AbortSignal, init: InitFunction, options?: ResponseInit): Response; | ||
export {}; |
@@ -7,3 +7,3 @@ /** | ||
*/ | ||
export function eventStream(signal, init) { | ||
export function eventStream(signal, init, options = {}) { | ||
let stream = new ReadableStream({ | ||
@@ -16,3 +16,3 @@ start(controller) { | ||
} | ||
let cleanup = init(send); | ||
let cleanup = init(send, close); | ||
let closed = false; | ||
@@ -32,9 +32,16 @@ function close() { | ||
}); | ||
return new Response(stream, { | ||
headers: { | ||
"Content-Type": "text/event-stream", | ||
"Cache-Control": "no-cache", | ||
Connection: "keep-alive", | ||
}, | ||
}); | ||
let headers = new Headers(options.headers); | ||
if (headers.has("Content-Type")) { | ||
console.warn("Overriding Content-Type header to `text/event-stream`"); | ||
} | ||
if (headers.has("Cache-Control")) { | ||
console.warn("Overriding Cache-Control header to `no-cache`"); | ||
} | ||
if (headers.has("Connection")) { | ||
console.warn("Overriding Connection header to `keep-alive`"); | ||
} | ||
headers.set("Content-Type", "text/event-stream"); | ||
headers.set("Cache-Control", "no-cache"); | ||
headers.set("Connection", "keep-alive"); | ||
return new Response(stream, { headers }); | ||
} |
@@ -14,4 +14,7 @@ interface SendFunctionArgs { | ||
} | ||
interface AbortFunction { | ||
(): void; | ||
} | ||
interface InitFunction { | ||
(send: SendFunction): CleanupFunction; | ||
(send: SendFunction, abort: AbortFunction): CleanupFunction; | ||
} | ||
@@ -24,3 +27,3 @@ /** | ||
*/ | ||
export declare function eventStream(signal: AbortSignal, init: InitFunction): Response; | ||
export declare function eventStream(signal: AbortSignal, init: InitFunction, options?: ResponseInit): Response; | ||
export {}; |
@@ -10,3 +10,3 @@ "use strict"; | ||
*/ | ||
function eventStream(signal, init) { | ||
function eventStream(signal, init, options = {}) { | ||
let stream = new ReadableStream({ | ||
@@ -19,3 +19,3 @@ start(controller) { | ||
} | ||
let cleanup = init(send); | ||
let cleanup = init(send, close); | ||
let closed = false; | ||
@@ -35,10 +35,17 @@ function close() { | ||
}); | ||
return new Response(stream, { | ||
headers: { | ||
"Content-Type": "text/event-stream", | ||
"Cache-Control": "no-cache", | ||
Connection: "keep-alive", | ||
}, | ||
}); | ||
let headers = new Headers(options.headers); | ||
if (headers.has("Content-Type")) { | ||
console.warn("Overriding Content-Type header to `text/event-stream`"); | ||
} | ||
if (headers.has("Cache-Control")) { | ||
console.warn("Overriding Cache-Control header to `no-cache`"); | ||
} | ||
if (headers.has("Connection")) { | ||
console.warn("Overriding Connection header to `keep-alive`"); | ||
} | ||
headers.set("Content-Type", "text/event-stream"); | ||
headers.set("Cache-Control", "no-cache"); | ||
headers.set("Connection", "keep-alive"); | ||
return new Response(stream, { headers }); | ||
} | ||
exports.eventStream = eventStream; |
{ | ||
"name": "remix-utils", | ||
"version": "6.5.0", | ||
"version": "6.6.0", | ||
"license": "MIT", | ||
@@ -12,2 +12,3 @@ "engines": { | ||
"scripts": { | ||
"prepare": "npm run build", | ||
"build": "npm run build:browser && npm run build:main", | ||
@@ -14,0 +15,0 @@ "build:browser": "tsc --project tsconfig.json --module ESNext --outDir ./browser", |
@@ -1278,3 +1278,3 @@ # Remix Utils | ||
The `eventStream` function is used to create a new event stream response needed to send events to the client. | ||
The `eventStream` function is used to create a new event stream response needed to send events to the client. This must live in a [Resource Route](https://remix.run/docs/en/1.18.1/guides/resource-routes). | ||
@@ -1305,2 +1305,3 @@ ```ts | ||
function Counter() { | ||
// Here `/sse/time` is the resource route returning an eventStream response | ||
let time = useEventSource("/sse/time", { event: "time" }); | ||
@@ -1307,0 +1308,0 @@ |
266832
5827
1647