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/index.js CHANGED
@@ -608,12 +608,28 @@ var COMMON_PROXY_RANGES = {
608
608
 
609
609
  // src/shared/plugins/CORSPlugin.ts
610
610
  function cors(opts) {
611
- const { origin, methods, allowedHeaders } = opts;
612
- return (ctx, next) => {
613
- ctx.response.setHeader(
614
- "Access-Control-Allow-Origin",
615
- Array.isArray(origin) ? origin.join(",") : origin
616
- );
611
+ const allowedOrigin = opts.origin ?? "*";
612
+ const methods = opts.methods ?? "GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS";
613
+ const allowedHeaders = opts.allowedHeaders ?? "Content-Type,Authorization,Cookie,X-Requested-With,Accept";
614
+ const credentials = opts.credentials === true;
615
+ return async (ctx, next) => {
616
+ const requestOrigin = ctx.request.headers["origin"];
617
+ if (allowedOrigin === "*") {
618
+ if (credentials && requestOrigin) {
619
+ ctx.response.setHeader("Access-Control-Allow-Origin", requestOrigin);
620
+ } else {
621
+ ctx.response.setHeader("Access-Control-Allow-Origin", "*");
622
+ }
623
+ } else if (Array.isArray(allowedOrigin)) {
624
+ if (requestOrigin && allowedOrigin.includes(requestOrigin)) {
625
+ ctx.response.setHeader("Access-Control-Allow-Origin", requestOrigin);
626
+ }
627
+ } else if (allowedOrigin) {
628
+ ctx.response.setHeader("Access-Control-Allow-Origin", allowedOrigin);
629
+ }
630
+ if (credentials) {
631
+ ctx.response.setHeader("Access-Control-Allow-Credentials", "true");
632
+ }
617
633
  ctx.response.setHeader(
618
634
  "Access-Control-Allow-Methods",
619
635
  Array.isArray(methods) ? methods.join(",") : methods
@@ -622,9 +638,12 @@ function cors(opts) {
622
638
  "Access-Control-Allow-Headers",
623
639
  Array.isArray(allowedHeaders) ? allowedHeaders.join(",") : allowedHeaders
624
640
  );
641
+ ctx.response.setHeader("Vary", "Origin");
625
642
  if (ctx.request.method === "OPTIONS") {
626
- ctx.response.writeHead(204);
627
- return ctx.response.end();
643
+ ctx.response.statusCode = 204;
644
+ ctx.response.setHeader("Content-Length", "0");
645
+ ctx.response.end();
646
+ return;
628
647
  }
629
648
  return next();
630
649
  };
@@ -936,15 +955,23 @@ var AzuraClient = class {
936
955
  return;
937
956
  }
938
957
  if (this.opts.plugins?.cors?.enabled) {
939
- cors({
958
+ const corsPlugin = cors({
940
959
  origin: this.opts.plugins.cors.origins,
941
960
  methods: this.opts.plugins.cors.methods,
942
- allowedHeaders: this.opts.plugins.cors.allowedHeaders
961
+ allowedHeaders: this.opts.plugins.cors.allowedHeaders,
962
+ credentials: this.opts.plugins.cors.credentials
943
963
  });
964
+ this.middlewares.unshift((ctx) => corsPlugin(ctx, ctx.next));
944
965
  logger("info", "CORS plugin enabled");
945
966
  }
946
967
  if (this.opts.plugins?.rateLimit?.enabled) {
947
- rateLimit(this.opts.plugins.rateLimit.limit, this.opts.plugins.rateLimit.timeframe);
968
+ const rateLimitPlugin = rateLimit(
969
+ this.opts.plugins.rateLimit.limit,
970
+ this.opts.plugins.rateLimit.timeframe
971
+ );
972
+ if (typeof rateLimitPlugin === "function") {
973
+ this.middlewares.unshift((ctx) => rateLimitPlugin(ctx, ctx.next));
974
+ }
948
975
  logger("info", "Rate Limit plugin enabled");
949
976
  }
950
977
  this.server = http.createServer();