rouzer 1.1.1 → 1.2.1

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.
@@ -46,7 +46,7 @@ export interface Router<T extends MiddlewareTypes = any> extends HattipHandler<T
46
46
  *
47
47
  * @returns a new `Router` instance.
48
48
  */
49
- use<const TMiddleware extends ExtractMiddleware<this>>(middleware: TMiddleware): Router<ApplyMiddleware<this, TMiddleware>>;
49
+ use<const TMiddleware extends ExtractMiddleware<this>>(middleware: TMiddleware | null): Router<ApplyMiddleware<this, TMiddleware>>;
50
50
  /**
51
51
  * Clone this router and add the given routes and handlers to the chain.
52
52
  *
@@ -7,12 +7,21 @@ export { chain };
7
7
  class RouterObject extends MiddlewareChain {
8
8
  config;
9
9
  basePath;
10
- allowOrigins;
11
10
  constructor(config) {
12
11
  super();
13
12
  this.config = config;
14
13
  this.basePath = config.basePath?.replace(/\/?$/, '/');
15
- this.allowOrigins = config.cors?.allowOrigins?.map(createOriginPattern);
14
+ const allowOrigins = config.cors?.allowOrigins?.map(createOriginPattern);
15
+ if (allowOrigins) {
16
+ super.use((ctx) => {
17
+ const origin = ctx.request.headers.get('Origin');
18
+ if (origin &&
19
+ allowOrigins &&
20
+ !allowOrigins.some(pattern => pattern.test(origin))) {
21
+ return new Response(null, { status: 403 });
22
+ }
23
+ });
24
+ }
16
25
  }
17
26
  use(...args) {
18
27
  const handler = args.length === 1 ? super.use(args[0]) : this.useRoutes(...args);
@@ -21,23 +30,20 @@ class RouterObject extends MiddlewareChain {
21
30
  }
22
31
  /** @internal */
23
32
  useRoutes(routes, handlers) {
24
- const { config, basePath, allowOrigins } = this;
33
+ const { config, basePath } = this;
25
34
  const keys = Object.keys(routes);
26
35
  const patterns = mapValues(routes, ({ path }) => basePath ? new RoutePattern(path.source.replace(/^\/?/, basePath)) : path);
27
36
  return super.use(async function (context) {
28
37
  const request = context.request;
29
38
  const origin = request.headers.get('Origin');
30
- if (origin &&
31
- allowOrigins &&
32
- !allowOrigins.some(pattern => pattern.test(origin))) {
33
- return new Response(null, { status: 403 });
34
- }
35
39
  const url = (context.url ??= new URL(request.url));
36
- let method = request.method.toUpperCase();
37
40
  let isPreflight = false;
41
+ let method = request.method;
38
42
  if (method === 'OPTIONS') {
39
- method = request.headers.get('Access-Control-Request-Method') ?? 'GET';
40
43
  isPreflight = true;
44
+ method =
45
+ request.headers.get('Access-Control-Request-Method')?.toUpperCase() ??
46
+ 'GET';
41
47
  }
42
48
  for (let i = 0; i < keys.length; i++) {
43
49
  const { methods } = routes[keys[i]];
@@ -59,7 +65,7 @@ class RouterObject extends MiddlewareChain {
59
65
  if (isPreflight) {
60
66
  return new Response(null, {
61
67
  headers: {
62
- 'Access-Control-Allow-Origin': origin ?? '*',
68
+ 'Access-Control-Allow-Origin': origin ?? '',
63
69
  'Access-Control-Allow-Methods': method,
64
70
  'Access-Control-Allow-Headers': request.headers.get('Access-Control-Request-Headers') ?? '',
65
71
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rouzer",
3
- "version": "1.1.1",
3
+ "version": "1.2.1",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -27,7 +27,7 @@
27
27
  "dependencies": {
28
28
  "@hattip/core": "^0.0.49",
29
29
  "@remix-run/route-pattern": "^0.15.3",
30
- "alien-middleware": "^0.11.3"
30
+ "alien-middleware": "^0.11.4"
31
31
  },
32
32
  "prettier": "@alloc/prettier-config",
33
33
  "license": "MIT",