@aomex/cors 0.0.20 → 0.0.21

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/CHANGELOG.md CHANGED
@@ -1,121 +1,9 @@
1
1
  # @aomex/cors
2
2
 
3
- ## 0.0.20
3
+ ## 0.0.21
4
4
 
5
5
  ### Patch Changes
6
6
 
7
- - Updated dependencies [[`e9996cb`](https://github.com/aomex/aomex/commit/e9996cb33cc0ef70b42147be02fbbe2b4954e022)]:
8
- - @aomex/web@0.0.22
9
- - @aomex/core@0.0.21
10
-
11
- ## 0.0.19
12
-
13
- ### Patch Changes
14
-
15
- - Updated dependencies [[`a8dbaf3`](https://github.com/aomex/aomex/commit/a8dbaf38524f5a5d58337bc617175115742f9e1a), [`ddcec91`](https://github.com/aomex/aomex/commit/ddcec91803b00f35f7c55964d1a2d97c40a22c6e)]:
16
- - @aomex/web@0.0.21
17
- - @aomex/core@0.0.20
18
-
19
- ## 0.0.18
20
-
21
- ### Patch Changes
22
-
23
- - Updated dependencies []:
24
- - @aomex/core@0.0.19
25
- - @aomex/web@0.0.20
26
-
27
- ## 0.0.17
28
-
29
- ### Patch Changes
30
-
31
- - Updated dependencies []:
32
- - @aomex/core@0.0.18
33
- - @aomex/web@0.0.19
34
-
35
- ## 0.0.16
36
-
37
- ### Patch Changes
38
-
39
- - Updated dependencies []:
40
- - @aomex/core@0.0.17
41
- - @aomex/web@0.0.18
42
-
43
- ## 0.0.15
44
-
45
- ### Patch Changes
46
-
47
- - Updated dependencies [[`216aa79`](https://github.com/aomex/aomex/commit/216aa798bcd2d0856f5fe4028235aa54d4b10922)]:
48
- - @aomex/web@0.0.17
49
- - @aomex/core@0.0.16
50
-
51
- ## 0.0.14
52
-
53
- ### Patch Changes
54
-
55
- - Updated dependencies [[`280359e`](https://github.com/aomex/aomex/commit/280359eb90b71720304cfc212374b6dc4c90236d), [`9881653`](https://github.com/aomex/aomex/commit/988165361439738d27bc29bd0ed5a85c608db5ef), [`15ecd1c`](https://github.com/aomex/aomex/commit/15ecd1c75bc21929f79980b32e63ea46e081cc6a)]:
56
- - @aomex/web@0.0.16
57
- - @aomex/core@0.0.15
58
-
59
- ## 0.0.13
60
-
61
- ### Patch Changes
62
-
63
- - Updated dependencies []:
64
- - @aomex/core@0.0.14
65
- - @aomex/web@0.0.15
66
-
67
- ## 0.0.12
68
-
69
- ### Patch Changes
70
-
71
- - Updated dependencies [[`715cf66`](https://github.com/aomex/aomex/commit/715cf660e3b275b338dcf15c25ba9523cf6fb204)]:
72
- - @aomex/web@0.0.14
73
- - @aomex/core@0.0.13
74
-
75
- ## 0.0.11
76
-
77
- ### Patch Changes
78
-
79
- - Updated dependencies [[`6e63df8`](https://github.com/aomex/aomex/commit/6e63df84d1b32e713aba8c52451e289a72b57bae), [`67acf4f`](https://github.com/aomex/aomex/commit/67acf4f42cd86bf1f0a7c647a5ff779c7cd0fa10)]:
80
- - @aomex/web@0.0.13
81
- - @aomex/core@0.0.12
82
-
83
- ## 0.0.10
84
-
85
- ### Patch Changes
86
-
87
- - Updated dependencies [[`f14eb5f`](https://github.com/aomex/aomex/commit/f14eb5fe7bf06e1e2f64d45e3546c96d6079a2f1), [`7d923cf`](https://github.com/aomex/aomex/commit/7d923cf971b6a23d50113ab104c67041c6489053), [`3c5ad4e`](https://github.com/aomex/aomex/commit/3c5ad4e2f34be7da54c8b63f6fd4b88fd1e593c4), [`6154ee5`](https://github.com/aomex/aomex/commit/6154ee5ed500984f969d4c335ac3ad800c739937), [`92273f9`](https://github.com/aomex/aomex/commit/92273f913e38543f34f41ec5941ff43b6e1caaa9), [`a0fd49e`](https://github.com/aomex/aomex/commit/a0fd49ef276c05f312257521c75887458857bbb7)]:
88
- - @aomex/web@0.0.12
89
- - @aomex/core@0.0.11
90
-
91
- ## 0.0.9
92
-
93
- ### Patch Changes
94
-
95
- - Updated dependencies [[`d9dc050`](https://github.com/aomex/aomex/commit/d9dc050897367353588c4bb778cbafb7607406ab), [`41ae605`](https://github.com/aomex/aomex/commit/41ae6054b2e87c4120f3fcee12302bb3d96f1df3)]:
96
- - @aomex/web@0.0.11
97
- - @aomex/core@0.0.10
98
-
99
- ## 0.0.8
100
-
101
- ### Patch Changes
102
-
103
- - Updated dependencies [[`2439286`](https://github.com/aomex/aomex/commit/2439286e69dcc7211ac280b91dbca5de95cc488b)]:
104
- - @aomex/web@0.0.10
105
- - @aomex/core@0.0.9
106
-
107
- ## 0.0.7
108
-
109
- ### Patch Changes
110
-
111
- - Updated dependencies [[`dcf2f4b`](https://github.com/aomex/aomex/commit/dcf2f4b6a13228d4731e4969e6abff9bef8ef0e2), [`d788ce7`](https://github.com/aomex/aomex/commit/d788ce731af885eae3819683813b2a9502742cea), [`c6fe6cb`](https://github.com/aomex/aomex/commit/c6fe6cb2f21acf74bc06db1509deff4aa35c48b3), [`a2727fb`](https://github.com/aomex/aomex/commit/a2727fb7619341dc3b23736e5dc11cee7d65e0bc), [`4253004`](https://github.com/aomex/aomex/commit/42530043854956a42d16be090b89fbac70e2fa17), [`fc7008e`](https://github.com/aomex/aomex/commit/fc7008e889a55e6a0a268bb77cab5be0f0bd9d30), [`d80df1a`](https://github.com/aomex/aomex/commit/d80df1a07eb92214a9c3c7fa15c629ea1ee88c43)]:
112
- - @aomex/web@0.0.9
113
- - @aomex/core@0.0.8
114
-
115
- ## 0.0.5
116
-
117
- ### Patch Changes
118
-
119
- - Updated dependencies [[`bbc8dda`](https://github.com/aomex/aomex/commit/bbc8dda9f0876c3402608133085350cc693def6f), [`ceb2d82`](https://github.com/aomex/aomex/commit/ceb2d8269755dca3f79b755224259b8a99bc65d0)]:
120
- - @aomex/core@0.0.6
121
- - @aomex/web@0.0.7
7
+ - Updated dependencies [[`6f7d706`](https://github.com/aomex/aomex/commit/6f7d7066c23711abdd149eb1c9a293ab8c4284a4), [`e21007a`](https://github.com/aomex/aomex/commit/e21007a82cb8eac73e1f696340bbe986d57dc159), [`e7bf93c`](https://github.com/aomex/aomex/commit/e7bf93cee6896c61d0bf3eb0921151dc6c1bc107), [`e21007a`](https://github.com/aomex/aomex/commit/e21007a82cb8eac73e1f696340bbe986d57dc159), [`818e840`](https://github.com/aomex/aomex/commit/818e840d36c7456a863fc071968b246c123c17f5), [`fb6c72d`](https://github.com/aomex/aomex/commit/fb6c72dbb266be4db92a542afe93dfa5d8c7cd41)]:
8
+ - @aomex/core@0.0.22
9
+ - @aomex/web@0.0.23
package/dist/index.d.ts CHANGED
@@ -2,17 +2,11 @@ import { WebContext, WebMiddleware } from '@aomex/web';
2
2
 
3
3
  interface CorsOptions {
4
4
  /**
5
- * `Access-Control-Allow-Origin`, default is request Origin header.
6
- *
7
- * @remarks
8
- * If a function is provided, it will be called for each request with
9
- * the web context object. It may return a string or a promise that
10
- * will resolve with a string.
5
+ * `Access-Control-Allow-Origin`
11
6
  */
12
7
  origin?: ((ctx: WebContext) => string) | ((ctx: WebContext) => PromiseLike<string>) | string;
13
8
  /**
14
- * `Access-Control-Allow-Methods`, default is
15
- * 'GET,HEAD,PUT,POST,DELETE,PATCH'
9
+ * `Access-Control-Allow-Methods`, 默认值:`GET,HEAD,PUT,POST,DELETE,PATCH`
16
10
  */
17
11
  allowMethods?: string[] | string;
18
12
  /**
@@ -24,30 +18,25 @@ interface CorsOptions {
24
18
  */
25
19
  allowHeaders?: string[] | string;
26
20
  /**
27
- * `Access-Control-Max-Age` in seconds
21
+ * `Access-Control-Max-Age`。时间单位:`秒`
28
22
  */
29
23
  maxAge?: number | string;
30
24
  /**
31
25
  * `Access-Control-Allow-Credentials`
32
- *
33
- * @remarks
34
- * If a function is provided, it will be called for each request with
35
- * the web context object. It may return a boolean or a promise that
36
- * will resolve with a boolean.
37
26
  */
38
27
  credentials?: ((ctx: WebContext) => boolean) | ((ctx: WebContext) => PromiseLike<boolean>) | boolean;
39
28
  /**
40
- * Add set headers to `err.header` if an error is thrown
29
+ * 抛出异常时把headers信息附加在`err.header`上
41
30
  */
42
31
  keepHeadersOnError?: boolean;
43
32
  /**
44
- * Add `Cross-Origin-Opener-Policy` & `Cross-Origin-Embedder-Policy` to response headers
33
+ * 响应头部增加 `Cross-Origin-Opener-Policy` 和 `Cross-Origin-Embedder-Policy` 这两个条信息。默认值:`false`
45
34
  *
46
35
  * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer/Planned_changes
47
36
  */
48
37
  secureContext?: boolean;
49
38
  /**
50
- * Handle `Access-Control-Request-Private-Network` request by return `Access-Control-Allow-Private-Network`
39
+ * 处理请求头部 `Access-Control-Request-Private-Network` 并返回 `Access-Control-Allow-Private-Network`
51
40
  *
52
41
  * @see https://wicg.github.io/private-network-access/
53
42
  */
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { HttpError, WebContext, WebMiddleware } from '@aomex/web';\nimport { middleware } from '@aomex/core';\n\nexport interface CorsOptions {\n /**\n * `Access-Control-Allow-Origin`, default is request Origin header.\n *\n * @remarks\n * If a function is provided, it will be called for each request with\n * the web context object. It may return a string or a promise that\n * will resolve with a string.\n */\n origin?:\n | ((ctx: WebContext) => string)\n | ((ctx: WebContext) => PromiseLike<string>)\n | string;\n\n /**\n * `Access-Control-Allow-Methods`, default is\n * 'GET,HEAD,PUT,POST,DELETE,PATCH'\n */\n allowMethods?: string[] | string;\n\n /**\n * `Access-Control-Expose-Headers`\n */\n exposeHeaders?: string[] | string;\n\n /**\n * `Access-Control-Allow-Headers`\n */\n allowHeaders?: string[] | string;\n\n /**\n * `Access-Control-Max-Age` in seconds\n */\n maxAge?: number | string;\n\n /**\n * `Access-Control-Allow-Credentials`\n *\n * @remarks\n * If a function is provided, it will be called for each request with\n * the web context object. It may return a boolean or a promise that\n * will resolve with a boolean.\n */\n credentials?:\n | ((ctx: WebContext) => boolean)\n | ((ctx: WebContext) => PromiseLike<boolean>)\n | boolean;\n\n /**\n * Add set headers to `err.header` if an error is thrown\n */\n keepHeadersOnError?: boolean;\n\n /**\n * Add `Cross-Origin-Opener-Policy` & `Cross-Origin-Embedder-Policy` to response headers\n *\n * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer/Planned_changes\n */\n secureContext?: boolean;\n\n /**\n * Handle `Access-Control-Request-Private-Network` request by return `Access-Control-Allow-Private-Network`\n *\n * @see https://wicg.github.io/private-network-access/\n */\n privateNetworkAccess?: boolean;\n}\n\nconst defaults: CorsOptions = {\n allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH',\n secureContext: false,\n};\n\nexport const cors = (options: CorsOptions = {}): WebMiddleware => {\n options = {\n ...defaults,\n ...options,\n };\n\n (<const>['exposeHeaders', 'allowMethods', 'allowHeaders']).forEach((key) => {\n const value = options[key];\n if (Array.isArray(value)) {\n options[key] = value.join(',');\n }\n });\n\n if (options.maxAge) {\n options.maxAge = String(options.maxAge);\n }\n\n options.keepHeadersOnError = options.keepHeadersOnError !== false;\n\n return middleware.web(async (ctx, next) => {\n const { request, response } = ctx;\n const requestOrigin = request.headers['origin'];\n\n // Always set Vary header\n // https://github.com/rs/cors/issues/10\n response.vary('Origin');\n\n if (!requestOrigin) return next();\n\n let origin: string;\n if (typeof options.origin === 'function') {\n origin = await options.origin(ctx);\n if (!origin) return next();\n } else {\n origin = options.origin || requestOrigin;\n }\n\n let credentials;\n if (typeof options.credentials === 'function') {\n credentials = await options.credentials(ctx);\n } else {\n credentials = !!options.credentials;\n }\n\n const headersSet: Record<string, any> = {};\n\n function setAndRecordHeader(key: string, value: any) {\n response.setHeader(key, value);\n headersSet[key] = value;\n }\n\n if (request.method !== 'OPTIONS') {\n // Simple Cross-Origin Request, Actual Request, and Redirects\n setAndRecordHeader('Access-Control-Allow-Origin', origin);\n credentials &&\n setAndRecordHeader('Access-Control-Allow-Credentials', 'true');\n\n if (options.exposeHeaders) {\n setAndRecordHeader(\n 'Access-Control-Expose-Headers',\n options.exposeHeaders,\n );\n }\n\n if (options.secureContext) {\n setAndRecordHeader('Cross-Origin-Opener-Policy', 'same-origin');\n setAndRecordHeader('Cross-Origin-Embedder-Policy', 'require-corp');\n }\n\n if (!options.keepHeadersOnError) return next();\n\n return next().catch((err: HttpError) => {\n const errHeadersSet = err.headers || {};\n const varyWithOrigin = response.varyAppend(\n errHeadersSet['vary'] || errHeadersSet['Vary'] || '',\n 'Origin',\n );\n delete errHeadersSet['Vary'];\n\n err.headers = {\n ...errHeadersSet,\n ...headersSet,\n ...{ vary: varyWithOrigin },\n };\n\n return Promise.reject(err);\n });\n } else {\n if (!request.headers['access-control-request-method']) return next();\n\n response.setHeader('Access-Control-Allow-Origin', origin);\n\n if (credentials === true) {\n response.setHeader('Access-Control-Allow-Credentials', 'true');\n }\n\n if (options.maxAge) {\n response.setHeader('Access-Control-Max-Age', options.maxAge);\n }\n\n if (\n options.privateNetworkAccess &&\n request.headers['access-control-request-private-network']\n ) {\n response.setHeader('Access-Control-Allow-Private-Network', 'true');\n }\n\n if (options.allowMethods) {\n response.setHeader(\n 'Access-Control-Allow-Methods',\n options.allowMethods,\n );\n }\n\n if (options.secureContext) {\n setAndRecordHeader('Cross-Origin-Opener-Policy', 'same-origin');\n setAndRecordHeader('Cross-Origin-Embedder-Policy', 'require-corp');\n }\n\n const allowHeaders =\n options.allowHeaders ||\n request.headers['access-control-request-headers'];\n if (allowHeaders) {\n response.setHeader('Access-Control-Allow-Headers', allowHeaders);\n }\n\n ctx.send(204);\n }\n });\n};\n"],"mappings":";AACA,SAAS,kBAAkB;AAsE3B,IAAM,WAAwB;AAAA,EAC5B,cAAc;AAAA,EACd,eAAe;AACjB;AAEO,IAAM,OAAO,CAAC,UAAuB,CAAC,MAAqB;AAChE,YAAU;AAAA,IACR,GAAG;AAAA,IACH,GAAG;AAAA,EACL;AAEA,EAAQ,CAAC,iBAAiB,gBAAgB,cAAc,EAAG,QAAQ,CAAC,QAAQ;AAC1E,UAAM,QAAQ,QAAQ,GAAG;AACzB,QAAI,MAAM,QAAQ,KAAK,GAAG;AAC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CAAC;AACtC,cAAM,iBAAiB,SAAS;AAAA,UAC9B,cAAc,MAAM,KAAK,cAAc,MAAM,KAAK;AAAA,UAClD;AAAA,QACF;AACA,eAAO,cAAc,MAAM;AAE3B,YAAI,UAAU;AAAA,UACZ,GAAG;AAAA,UACH,GAAG;AAAA,UACH,GAAG,EAAE,MAAM,eAAe;AAAA,QAC5B;AAEA,eAAO,QAAQ,OAAO,GAAG;AAAA,MAC3B,CAAC;AAAA,IACH,OAAO;AACL,UAAI,CAAC,QAAQ,QAAQ,+BAA+B;AAAG,eAAO,KAAK;AAEnE,eAAS,UAAU,+BAA+B,MAAM;AAExD,UAAI,gBAAgB,MAAM;AACxB,iBAAS,UAAU,oCAAoC,MAAM;AAAA,MAC/D;AAEA,UAAI,QAAQ,QAAQ;AAClB,iBAAS,UAAU,0BAA0B,QAAQ,MAAM;AAAA,MAC7D;AAEA,UACE,QAAQ,wBACR,QAAQ,QAAQ,wCAAwC,GACxD;AACA,iBAAS,UAAU,wCAAwC,MAAM;AAAA,MACnE;AAEA,UAAI,QAAQ,cAAc;AACxB,iBAAS;AAAA,UACP;AAAA,UACA,QAAQ;AAAA,QACV;AAAA,MACF;AAEA,UAAI,QAAQ,eAAe;AACzB,2BAAmB,8BAA8B,aAAa;AAC9D,2BAAmB,gCAAgC,cAAc;AAAA,MACnE;AAEA,YAAM,eACJ,QAAQ,gBACR,QAAQ,QAAQ,gCAAgC;AAClD,UAAI,cAAc;AAChB,iBAAS,UAAU,gCAAgC,YAAY;AAAA,MACjE;AAEA,UAAI,KAAK,GAAG;AAAA,IACd;AAAA,EACF,CAAC;AACH;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { HttpError, WebContext, WebMiddleware } from '@aomex/web';\nimport { middleware } from '@aomex/core';\n\nexport interface CorsOptions {\n /**\n * `Access-Control-Allow-Origin`\n */\n origin?:\n | ((ctx: WebContext) => string)\n | ((ctx: WebContext) => PromiseLike<string>)\n | string;\n\n /**\n * `Access-Control-Allow-Methods`, 默认值:`GET,HEAD,PUT,POST,DELETE,PATCH`\n */\n allowMethods?: string[] | string;\n\n /**\n * `Access-Control-Expose-Headers`\n */\n exposeHeaders?: string[] | string;\n\n /**\n * `Access-Control-Allow-Headers`\n */\n allowHeaders?: string[] | string;\n\n /**\n * `Access-Control-Max-Age`。时间单位:`秒`\n */\n maxAge?: number | string;\n\n /**\n * `Access-Control-Allow-Credentials`\n */\n credentials?:\n | ((ctx: WebContext) => boolean)\n | ((ctx: WebContext) => PromiseLike<boolean>)\n | boolean;\n\n /**\n * 抛出异常时把headers信息附加在`err.header`上\n */\n keepHeadersOnError?: boolean;\n\n /**\n * 响应头部增加 `Cross-Origin-Opener-Policy` 和 `Cross-Origin-Embedder-Policy` 这两个条信息。默认值:`false`\n *\n * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer/Planned_changes\n */\n secureContext?: boolean;\n\n /**\n * 处理请求头部 `Access-Control-Request-Private-Network` 并返回 `Access-Control-Allow-Private-Network`\n *\n * @see https://wicg.github.io/private-network-access/\n */\n privateNetworkAccess?: boolean;\n}\n\nconst defaults: CorsOptions = {\n allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH',\n secureContext: false,\n};\n\nexport const cors = (options: CorsOptions = {}): WebMiddleware => {\n options = {\n ...defaults,\n ...options,\n };\n\n (<const>['exposeHeaders', 'allowMethods', 'allowHeaders']).forEach((key) => {\n const value = options[key];\n if (Array.isArray(value)) {\n options[key] = value.join(',');\n }\n });\n\n if (options.maxAge) {\n options.maxAge = String(options.maxAge);\n }\n\n options.keepHeadersOnError = options.keepHeadersOnError !== false;\n\n return middleware.web(async (ctx, next) => {\n const { request, response } = ctx;\n const requestOrigin = request.headers['origin'];\n\n // Always set Vary header\n // https://github.com/rs/cors/issues/10\n response.vary('Origin');\n\n if (!requestOrigin) return next();\n\n let origin: string;\n if (typeof options.origin === 'function') {\n origin = await options.origin(ctx);\n if (!origin) return next();\n } else {\n origin = options.origin || requestOrigin;\n }\n\n let credentials;\n if (typeof options.credentials === 'function') {\n credentials = await options.credentials(ctx);\n } else {\n credentials = !!options.credentials;\n }\n\n const headersSet: Record<string, any> = {};\n\n function setAndRecordHeader(key: string, value: any) {\n response.setHeader(key, value);\n headersSet[key] = value;\n }\n\n if (request.method !== 'OPTIONS') {\n // Simple Cross-Origin Request, Actual Request, and Redirects\n setAndRecordHeader('Access-Control-Allow-Origin', origin);\n credentials &&\n setAndRecordHeader('Access-Control-Allow-Credentials', 'true');\n\n if (options.exposeHeaders) {\n setAndRecordHeader(\n 'Access-Control-Expose-Headers',\n options.exposeHeaders,\n );\n }\n\n if (options.secureContext) {\n setAndRecordHeader('Cross-Origin-Opener-Policy', 'same-origin');\n setAndRecordHeader('Cross-Origin-Embedder-Policy', 'require-corp');\n }\n\n if (!options.keepHeadersOnError) return next();\n\n return next().catch((err: HttpError) => {\n const errHeadersSet = err.headers || {};\n const varyWithOrigin = response.varyAppend(\n errHeadersSet['vary'] || errHeadersSet['Vary'] || '',\n 'Origin',\n );\n delete errHeadersSet['Vary'];\n\n err.headers = {\n ...errHeadersSet,\n ...headersSet,\n ...{ vary: varyWithOrigin },\n };\n\n return Promise.reject(err);\n });\n } else {\n if (!request.headers['access-control-request-method']) return next();\n\n response.setHeader('Access-Control-Allow-Origin', origin);\n\n if (credentials === true) {\n response.setHeader('Access-Control-Allow-Credentials', 'true');\n }\n\n if (options.maxAge) {\n response.setHeader('Access-Control-Max-Age', options.maxAge);\n }\n\n if (\n options.privateNetworkAccess &&\n request.headers['access-control-request-private-network']\n ) {\n response.setHeader('Access-Control-Allow-Private-Network', 'true');\n }\n\n if (options.allowMethods) {\n response.setHeader(\n 'Access-Control-Allow-Methods',\n options.allowMethods,\n );\n }\n\n if (options.secureContext) {\n setAndRecordHeader('Cross-Origin-Opener-Policy', 'same-origin');\n setAndRecordHeader('Cross-Origin-Embedder-Policy', 'require-corp');\n }\n\n const allowHeaders =\n options.allowHeaders ||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AmB,gCAAgC,cAAc;AAAA,MACnE;AAEA,YAAM,eACJ,QAAQ,gBACR,QAAQ,QAAQ,gCAAgC;AAClD,UAAI,cAAc;AAChB,iBAAS,UAAU,gCAAgC,YAAY;AAAA,MACjE;AAEA,UAAI,KAAK,GAAG;AAAA,IACd;AAAA,EACF,CAAC;AACH;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aomex/cors",
3
- "version": "0.0.20",
3
+ "version": "0.0.21",
4
4
  "description": "跨站资源共享",
5
5
  "keywords": [
6
6
  "cors"
@@ -29,12 +29,12 @@
29
29
  "access": "public"
30
30
  },
31
31
  "peerDependencies": {
32
- "@aomex/core": "^0.0.21",
33
- "@aomex/web": "^0.0.22"
32
+ "@aomex/core": "^0.0.22",
33
+ "@aomex/web": "^0.0.23"
34
34
  },
35
35
  "devDependencies": {
36
- "@aomex/core": "^0.0.21",
37
- "@aomex/web": "^0.0.22"
36
+ "@aomex/core": "^0.0.22",
37
+ "@aomex/web": "^0.0.23"
38
38
  },
39
39
  "scripts": {
40
40
  "test": "vitest"