@tekir/cors 0.1.1 → 0.1.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/dist/cors.js CHANGED
@@ -84,12 +84,33 @@ export function cors(userConfig = {}) {
84
84
  },
85
85
  });
86
86
  }
87
- await next();
88
- // Inject CORS headers into the actual response. Routes return a mix of
89
- // Response objects (SSE streams, file downloads, custom payloads) and
90
- // plain values that tekir wraps automatically. Coerce both shapes into
91
- // a Response here so the headers always land on the wire.
87
+ // Capture any throw from `next()` so the CORS merge below still runs
88
+ // even when an inner middleware caught the error and set
89
+ // `ctx.$result` before re-throwing. Without this guard the browser
90
+ // sees a "CORS error" on every error response.
91
+ let nextError;
92
+ try {
93
+ await next();
94
+ }
95
+ catch (err) {
96
+ nextError = err;
97
+ }
98
+ // When `next()` threw and nothing along the way set `$result`, do not
99
+ // synthesize one here. An outer error-handling middleware is the
100
+ // natural place to build the error response, and tekir's chain only
101
+ // adopts a middleware's return value when `$result` is still
102
+ // undefined; coercing to a 204 here would silently win over the real
103
+ // error response and the client would see "CORS-OK 204" instead of a
104
+ // 401/500. The throw still propagates, so the outer handler runs.
92
105
  const result = ctx.$result;
106
+ if (nextError !== undefined && result == null) {
107
+ throw nextError;
108
+ }
109
+ // Inject CORS headers into whatever the chain produced. Routes return
110
+ // a mix of Response objects (SSE streams, file downloads, custom
111
+ // payloads) and plain values that tekir wraps automatically. Coerce
112
+ // both shapes into a Response here so the headers always land on the
113
+ // wire.
93
114
  let response;
94
115
  if (result instanceof Response) {
95
116
  response = result;
@@ -122,5 +143,11 @@ export function cors(userConfig = {}) {
122
143
  statusText: response.statusText,
123
144
  headers: merged,
124
145
  });
146
+ // Re-throw so outer error handlers and loggers still see the original
147
+ // error. `$result` already carries the merged CORS headers; an outer
148
+ // handler that builds its own response can copy them off the existing
149
+ // one if it cares to.
150
+ if (nextError !== undefined)
151
+ throw nextError;
125
152
  };
126
153
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tekir/cors",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "CORS middleware for cross-origin requests",
5
5
  "author": "dev@tekir.io",
6
6
  "license": "MIT",
package/src/cors.ts CHANGED
@@ -86,13 +86,34 @@ export function cors(userConfig: CorsConfig = {}) {
86
86
  })
87
87
  }
88
88
 
89
- await next()
89
+ // Capture any throw from `next()` so the CORS merge below still runs
90
+ // even when an inner middleware caught the error and set
91
+ // `ctx.$result` before re-throwing. Without this guard the browser
92
+ // sees a "CORS error" on every error response.
93
+ let nextError: unknown
94
+ try {
95
+ await next()
96
+ } catch (err) {
97
+ nextError = err
98
+ }
90
99
 
91
- // Inject CORS headers into the actual response. Routes return a mix of
92
- // Response objects (SSE streams, file downloads, custom payloads) and
93
- // plain values that tekir wraps automatically. Coerce both shapes into
94
- // a Response here so the headers always land on the wire.
100
+ // When `next()` threw and nothing along the way set `$result`, do not
101
+ // synthesize one here. An outer error-handling middleware is the
102
+ // natural place to build the error response, and tekir's chain only
103
+ // adopts a middleware's return value when `$result` is still
104
+ // undefined; coercing to a 204 here would silently win over the real
105
+ // error response and the client would see "CORS-OK 204" instead of a
106
+ // 401/500. The throw still propagates, so the outer handler runs.
95
107
  const result = ctx.$result
108
+ if (nextError !== undefined && result == null) {
109
+ throw nextError
110
+ }
111
+
112
+ // Inject CORS headers into whatever the chain produced. Routes return
113
+ // a mix of Response objects (SSE streams, file downloads, custom
114
+ // payloads) and plain values that tekir wraps automatically. Coerce
115
+ // both shapes into a Response here so the headers always land on the
116
+ // wire.
96
117
  let response: Response
97
118
  if (result instanceof Response) {
98
119
  response = result
@@ -121,5 +142,11 @@ export function cors(userConfig: CorsConfig = {}) {
121
142
  statusText: response.statusText,
122
143
  headers: merged,
123
144
  })
145
+
146
+ // Re-throw so outer error handlers and loggers still see the original
147
+ // error. `$result` already carries the merged CORS headers; an outer
148
+ // handler that builds its own response can copy them off the existing
149
+ // one if it cares to.
150
+ if (nextError !== undefined) throw nextError
124
151
  }
125
152
  }