starlight-server 1.6.5 → 1.6.7

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/http/cors.js CHANGED
@@ -30,4 +30,6 @@ export function handleCORS(request, response, rule) {
30
30
  nodeResponse.setHeader('Access-Control-Allow-Headers', rule.allowHeaders);
31
31
  if (!requestIsPreflight && rule.exposeHeaders !== undefined)
32
32
  nodeResponse.setHeader('Access-Control-Expose-Headers', rule.exposeHeaders);
33
+ if (requestIsPreflight)
34
+ nodeResponse.setHeader('Access-Control-Allow-Methods', getPreflightRequestMethod(request) ?? request.method ?? '');
33
35
  }
@@ -15,6 +15,7 @@ export declare class ResponseUtils {
15
15
  jsonReplacer?: ((key: string, value: unknown) => unknown) | undefined);
16
16
  header(name: string, value: string): void;
17
17
  headers(...items: [string, string][]): void;
18
+ headers(items: Record<string, string>): void;
18
19
  text(content: string | Buffer): void;
19
20
  /**
20
21
  * Output JSON
@@ -19,7 +19,10 @@ export class ResponseUtils {
19
19
  this.nodeResponse.setHeader(name, value);
20
20
  }
21
21
  headers(...items) {
22
- items.forEach(([name, value]) => this.header(name, value));
22
+ if (items.length === 1 && typeof items[0] === 'object') {
23
+ items = Object.entries(items[0]);
24
+ }
25
+ void items.forEach(([name, value]) => this.header(name, value));
23
26
  }
24
27
  text(content) {
25
28
  this.nodeResponse.end(content);
@@ -95,6 +95,10 @@ export class Router {
95
95
  }
96
96
  throw new HTTPError(405); // 有路径匹配的路由,但是没有 method 匹配的
97
97
  }
98
+ else {
99
+ const corsRule = matched.route.cors ?? this.cors;
100
+ handleCORS(request, response, typeof corsRule === 'function' ? corsRule(request) : corsRule);
101
+ }
98
102
  const basicContext = {};
99
103
  const helpers = new Helpers(basicContext);
100
104
  Object.assign(basicContext, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "starlight-server",
3
- "version": "1.6.5",
3
+ "version": "1.6.7",
4
4
  "description": "Simple But Powerful Node.js HTTP Server",
5
5
  "type": "module",
6
6
  "keywords": [
package/src/http/cors.ts CHANGED
@@ -42,7 +42,7 @@ export type CORSRule =
42
42
  export function handleCORS(
43
43
  request: Request | NodeRequest,
44
44
  response: ResponseUtils | NodeResponse,
45
- rule: CORSRule
45
+ rule: CORSRule,
46
46
  ) {
47
47
  // 不输出 CORS 相关 headers,浏览器会默认服务端不允许跨域请求
48
48
  if (rule === false) return
@@ -56,4 +56,9 @@ export function handleCORS(
56
56
  nodeResponse.setHeader('Access-Control-Allow-Headers', rule.allowHeaders)
57
57
  if (!requestIsPreflight && rule.exposeHeaders !== undefined)
58
58
  nodeResponse.setHeader('Access-Control-Expose-Headers', rule.exposeHeaders)
59
+ if (requestIsPreflight)
60
+ nodeResponse.setHeader(
61
+ 'Access-Control-Allow-Methods',
62
+ getPreflightRequestMethod(request) ?? request.method ?? '',
63
+ )
59
64
  }
@@ -18,8 +18,13 @@ export class ResponseUtils {
18
18
  this.nodeResponse.setHeader(name, value)
19
19
  }
20
20
 
21
- headers(...items: [string, string][]) {
22
- items.forEach(([name, value]) => this.header(name, value))
21
+ headers(...items: [string, string][]): void
22
+ headers(items: Record<string, string>): void
23
+ headers(...items: [string, string][] | [Record<string, string>]) {
24
+ if (items.length === 1 && typeof items[0] === 'object') {
25
+ items = Object.entries(items[0] as Record<string, string>)
26
+ }
27
+ void (items as [string, string][]).forEach(([name, value]) => this.header(name, value))
23
28
  }
24
29
 
25
30
  text(content: string | Buffer) {
@@ -131,6 +131,9 @@ export class Router {
131
131
  return
132
132
  }
133
133
  throw new HTTPError(405) // 有路径匹配的路由,但是没有 method 匹配的
134
+ } else {
135
+ const corsRule = matched.route.cors ?? this.cors
136
+ handleCORS(request, response, typeof corsRule === 'function' ? corsRule(request) : corsRule)
134
137
  }
135
138
 
136
139
  const basicContext = {} as BasicContext