badmfck-api-server 3.9.94 → 3.9.95

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.
@@ -29,6 +29,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
29
29
  exports.APIService = exports.Initializer = exports.REQ_DOC_USERS = exports.REQ_MONITOR_USERS = exports.REQ_INTERNAL_CALL = exports.REQ_HTTP_SERVER = exports.REQ_HTTP_REQUESTS_COUNT = exports.REQ_HTTP_LOG = exports.getDefaultOptions = void 0;
30
30
  const express_1 = __importDefault(require("express"));
31
31
  const BaseService_1 = require("./BaseService");
32
+ const cors_1 = __importDefault(require("cors"));
32
33
  const BaseEndpoint_1 = require("./BaseEndpoint");
33
34
  const DefaultErrors_1 = __importStar(require("./structures/DefaultErrors"));
34
35
  const badmfck_signal_1 = require("badmfck-signal");
@@ -207,13 +208,31 @@ class APIService extends BaseService_1.BaseService {
207
208
  return;
208
209
  });
209
210
  const corsSet = new Set(this.options.corsHostWhiteList.map(x => String(x).replace(/\/$/, "")));
211
+ const corsOptions = {
212
+ origin: (origin, callback) => {
213
+ if (!origin)
214
+ return callback(null, true);
215
+ try {
216
+ const o = new URL(String(origin));
217
+ const originNorm = o.origin.replace(/\/$/, "");
218
+ const hostNorm = o.host.replace(/\/$/, "");
219
+ const ok = corsSet.has(originNorm);
220
+ return callback(null, ok);
221
+ }
222
+ catch {
223
+ return callback(null, false);
224
+ }
225
+ },
226
+ credentials: true
227
+ };
228
+ app.use((0, cors_1.default)(corsOptions));
210
229
  app.use((req, res, next) => {
211
230
  const originHeader = req.headers.origin;
212
231
  if (!originHeader)
213
232
  return next();
214
233
  let originNorm;
215
234
  try {
216
- originNorm = new URL(String(originHeader)).origin;
235
+ originNorm = new URL(String(originHeader)).origin.replace(/\/$/, "");
217
236
  }
218
237
  catch {
219
238
  res.status(403).send({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "badmfck-api-server",
3
- "version": "3.9.94",
3
+ "version": "3.9.95",
4
4
  "description": "Simple API http server based on express",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",