azurajs 2.7.0 → 2.7.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.
package/dist/plugins.cjs CHANGED
@@ -15,12 +15,28 @@ var https__default = /*#__PURE__*/_interopDefault(https);
15
15
 
16
16
  // src/shared/plugins/CORSPlugin.ts
17
17
  function cors(opts) {
18
- const { origin, methods, allowedHeaders } = opts;
19
- return (ctx, next) => {
20
- ctx.response.setHeader(
21
- "Access-Control-Allow-Origin",
22
- Array.isArray(origin) ? origin.join(",") : origin
23
- );
18
+ const allowedOrigin = opts.origin ?? "*";
19
+ const methods = opts.methods ?? "GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS";
20
+ const allowedHeaders = opts.allowedHeaders ?? "Content-Type,Authorization,Cookie,X-Requested-With,Accept";
21
+ const credentials = opts.credentials === true;
22
+ return async (ctx, next) => {
23
+ const requestOrigin = ctx.request.headers["origin"];
24
+ if (allowedOrigin === "*") {
25
+ if (credentials && requestOrigin) {
26
+ ctx.response.setHeader("Access-Control-Allow-Origin", requestOrigin);
27
+ } else {
28
+ ctx.response.setHeader("Access-Control-Allow-Origin", "*");
29
+ }
30
+ } else if (Array.isArray(allowedOrigin)) {
31
+ if (requestOrigin && allowedOrigin.includes(requestOrigin)) {
32
+ ctx.response.setHeader("Access-Control-Allow-Origin", requestOrigin);
33
+ }
34
+ } else if (allowedOrigin) {
35
+ ctx.response.setHeader("Access-Control-Allow-Origin", allowedOrigin);
36
+ }
37
+ if (credentials) {
38
+ ctx.response.setHeader("Access-Control-Allow-Credentials", "true");
39
+ }
24
40
  ctx.response.setHeader(
25
41
  "Access-Control-Allow-Methods",
26
42
  Array.isArray(methods) ? methods.join(",") : methods
@@ -29,9 +45,12 @@ function cors(opts) {
29
45
  "Access-Control-Allow-Headers",
30
46
  Array.isArray(allowedHeaders) ? allowedHeaders.join(",") : allowedHeaders
31
47
  );
48
+ ctx.response.setHeader("Vary", "Origin");
32
49
  if (ctx.request.method === "OPTIONS") {
33
- ctx.response.writeHead(204);
34
- return ctx.response.end();
50
+ ctx.response.statusCode = 204;
51
+ ctx.response.setHeader("Content-Length", "0");
52
+ ctx.response.end();
53
+ return;
35
54
  }
36
55
  return next();
37
56
  };