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.js CHANGED
@@ -8,12 +8,28 @@ import { createHash, randomBytes, createHmac, timingSafeEqual } from 'crypto';
8
8
 
9
9
  // src/shared/plugins/CORSPlugin.ts
10
10
  function cors(opts) {
11
- const { origin, methods, allowedHeaders } = opts;
12
- return (ctx, next) => {
13
- ctx.response.setHeader(
14
- "Access-Control-Allow-Origin",
15
- Array.isArray(origin) ? origin.join(",") : origin
16
- );
11
+ const allowedOrigin = opts.origin ?? "*";
12
+ const methods = opts.methods ?? "GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS";
13
+ const allowedHeaders = opts.allowedHeaders ?? "Content-Type,Authorization,Cookie,X-Requested-With,Accept";
14
+ const credentials = opts.credentials === true;
15
+ return async (ctx, next) => {
16
+ const requestOrigin = ctx.request.headers["origin"];
17
+ if (allowedOrigin === "*") {
18
+ if (credentials && requestOrigin) {
19
+ ctx.response.setHeader("Access-Control-Allow-Origin", requestOrigin);
20
+ } else {
21
+ ctx.response.setHeader("Access-Control-Allow-Origin", "*");
22
+ }
23
+ } else if (Array.isArray(allowedOrigin)) {
24
+ if (requestOrigin && allowedOrigin.includes(requestOrigin)) {
25
+ ctx.response.setHeader("Access-Control-Allow-Origin", requestOrigin);
26
+ }
27
+ } else if (allowedOrigin) {
28
+ ctx.response.setHeader("Access-Control-Allow-Origin", allowedOrigin);
29
+ }
30
+ if (credentials) {
31
+ ctx.response.setHeader("Access-Control-Allow-Credentials", "true");
32
+ }
17
33
  ctx.response.setHeader(
18
34
  "Access-Control-Allow-Methods",
19
35
  Array.isArray(methods) ? methods.join(",") : methods
@@ -22,9 +38,12 @@ function cors(opts) {
22
38
  "Access-Control-Allow-Headers",
23
39
  Array.isArray(allowedHeaders) ? allowedHeaders.join(",") : allowedHeaders
24
40
  );
41
+ ctx.response.setHeader("Vary", "Origin");
25
42
  if (ctx.request.method === "OPTIONS") {
26
- ctx.response.writeHead(204);
27
- return ctx.response.end();
43
+ ctx.response.statusCode = 204;
44
+ ctx.response.setHeader("Content-Length", "0");
45
+ ctx.response.end();
46
+ return;
28
47
  }
29
48
  return next();
30
49
  };