@sourceregistry/sveltekit-enhance 1.3.1 → 1.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -80,6 +80,30 @@ export const handle = enhance.handle(
80
80
  );
81
81
  ```
82
82
 
83
+ #### SSE / streaming responses
84
+
85
+ `enhance.handle` automatically detects Server-Sent Events and other streaming requests (via `Accept: text/event-stream`) and bypasses the main handler entirely — going straight to SvelteKit's `resolve`. This prevents the handler from blocking indefinitely on `await resolve(event)`, which never settles for a streaming response.
86
+
87
+ Consequences:
88
+ - The **main handler** does not run for SSE requests — it cannot inspect or modify the response headers.
89
+ - **`responseHandlers`** (added by enhancers via `contextInput.responseHandlers.push(...)`) are also skipped for responses with `Content-Type: text/event-stream`.
90
+ - **Enhancers** (the variadic context arguments) still run normally — they can set locals, authenticate the request, or short-circuit with an error response.
91
+
92
+ ```ts
93
+ // This handler is skipped for SSE requests — no change needed on your end.
94
+ export const handle = enhance.handle(
95
+ async ({ event, resolve }) => {
96
+ const response = await resolve(event); // not called for SSE
97
+ response.headers.set('x-custom', 'value');
98
+ return response;
99
+ },
100
+ Auth.Bearer, // still runs — can reject unauthorized SSE requests
101
+ SessionGuard.hook, // still runs
102
+ );
103
+ ```
104
+
105
+ If you need to set response headers on SSE routes, do it in the route handler itself (e.g. `+server.ts`) rather than in the `enhance.handle` main handler.
106
+
83
107
  ### `enhance.load`
84
108
 
85
109
  Wraps server `load` functions.
@@ -1,3 +1,4 @@
1
+ import { randomUUID } from "node:crypto";
1
2
  const durationMs = (startedAt) => Number(process.hrtime.bigint() - startedAt) / 1_000_000;
2
3
  const resolveRoute = (event) => event.route.id ?? event.url.pathname;
3
4
  export const RequestMonitor = {
@@ -5,7 +6,11 @@ export const RequestMonitor = {
5
6
  const { logger = console, record, } = options;
6
7
  return async (event) => {
7
8
  const locals = event.locals;
8
- const requestId = locals.requestId ?? "unknown";
9
+ const requestId = locals.correlation_id ??
10
+ locals.request_id ??
11
+ event.request.headers.get('x-correlation-id') ??
12
+ event.request.headers.get('x-request-id') ??
13
+ randomUUID();
9
14
  const route = resolveRoute(event);
10
15
  locals.trace = {
11
16
  id: requestId,
package/dist/index.js CHANGED
@@ -181,6 +181,11 @@ export const handle = (handle, ...contexts) => async (input) => {
181
181
  contextInput.resolve = previous_resolve;
182
182
  }
183
183
  }
184
+ // SSE responses cannot be awaited — bypass the main handler to avoid blocking
185
+ // indefinitely on a streaming response that never ends.
186
+ if (event.request.headers.get('accept')?.includes('text/event-stream')) {
187
+ return input.resolve(event, parent_options);
188
+ }
184
189
  try {
185
190
  return await handle({
186
191
  ...input,
@@ -195,7 +200,8 @@ export const handle = (handle, ...contexts) => async (input) => {
195
200
  };
196
201
  try {
197
202
  const response = await apply_handle(0, input.event);
198
- if (contextInput.responseHandlers.length > 0) {
203
+ const isSSE = response.headers.get('content-type')?.includes('text/event-stream');
204
+ if (!isSSE && contextInput.responseHandlers.length > 0) {
199
205
  for (const responseHandler of contextInput.responseHandlers) {
200
206
  await responseHandler({ event: contextInput, response });
201
207
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sourceregistry/sveltekit-enhance",
3
- "version": "1.3.1",
3
+ "version": "1.3.3",
4
4
  "description": "Composable enhance and form utilities for SvelteKit actions, loads, methods, and hooks.",
5
5
  "author": "A.P.A. Slaa (a.p.a.slaa@projectsource.nl)",
6
6
  "scripts": {