@tekir/cors 0.1.1 → 0.1.2

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,7 +84,18 @@ export function cors(userConfig = {}) {
84
84
  },
85
85
  });
86
86
  }
87
- await next();
87
+ // Capture any throw from `next()` so the CORS merge below still runs
88
+ // even when a downstream middleware errors and an outer error handler
89
+ // turned it into a Response on `ctx.$result`. Without this guard the
90
+ // browser sees a "CORS error" on every error response, regardless of
91
+ // middleware order.
92
+ let nextError;
93
+ try {
94
+ await next();
95
+ }
96
+ catch (err) {
97
+ nextError = err;
98
+ }
88
99
  // Inject CORS headers into the actual response. Routes return a mix of
89
100
  // Response objects (SSE streams, file downloads, custom payloads) and
90
101
  // plain values that tekir wraps automatically. Coerce both shapes into
@@ -122,5 +133,12 @@ export function cors(userConfig = {}) {
122
133
  statusText: response.statusText,
123
134
  headers: merged,
124
135
  });
136
+ // Re-throw so the outer error handler can still see the original error
137
+ // for logging or transformation. By now `ctx.$result` carries the CORS
138
+ // headers, so even if the outer handler builds its own error response
139
+ // it can copy them off the existing one. If the outer handler swallows
140
+ // the throw, we already wrote a coerced response above.
141
+ if (nextError !== undefined)
142
+ throw nextError;
125
143
  };
126
144
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tekir/cors",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
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,7 +86,17 @@ 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 a downstream middleware errors and an outer error handler
91
+ // turned it into a Response on `ctx.$result`. Without this guard the
92
+ // browser sees a "CORS error" on every error response, regardless of
93
+ // middleware order.
94
+ let nextError: unknown
95
+ try {
96
+ await next()
97
+ } catch (err) {
98
+ nextError = err
99
+ }
90
100
 
91
101
  // Inject CORS headers into the actual response. Routes return a mix of
92
102
  // Response objects (SSE streams, file downloads, custom payloads) and
@@ -121,5 +131,12 @@ export function cors(userConfig: CorsConfig = {}) {
121
131
  statusText: response.statusText,
122
132
  headers: merged,
123
133
  })
134
+
135
+ // Re-throw so the outer error handler can still see the original error
136
+ // for logging or transformation. By now `ctx.$result` carries the CORS
137
+ // headers, so even if the outer handler builds its own error response
138
+ // it can copy them off the existing one. If the outer handler swallows
139
+ // the throw, we already wrote a coerced response above.
140
+ if (nextError !== undefined) throw nextError
124
141
  }
125
142
  }