abler-api 0.1.76 → 0.1.77

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.
@@ -1324,6 +1324,37 @@ class apiUtil$2 {
1324
1324
  value
1325
1325
  };
1326
1326
  }
1327
+ static corsCheckOrigin(origin, callback) {
1328
+ if (!origin || appSetting.corsOrigin === "*") {
1329
+ callback(null, true);
1330
+ return;
1331
+ }
1332
+ origin = origin.toLowerCase();
1333
+ let isAllowed = false;
1334
+ let allowedOrigins = appSetting.corsOrigin;
1335
+ if (Array.isArray(allowedOrigins)) {
1336
+ allowedOrigins = appSetting.corsOrigin.map(o => o.toLowerCase());
1337
+ // 检查请求的origin是否在允许的列表中,支持简单的通配符匹配
1338
+ isAllowed = allowedOrigins.some(allowedOrigin => {
1339
+ return allowed(allowedOrigin);
1340
+ });
1341
+ } else {
1342
+ return allowed(allowedOrigins.toLowerCase());
1343
+ }
1344
+ if (isAllowed) {
1345
+ callback(null, true);
1346
+ } else {
1347
+ // callback(new Error('Not allowed by CORS')); // 拒绝CORS请求
1348
+ callback(null, false);
1349
+ }
1350
+ function allowed(allowedOrigin) {
1351
+ if (allowedOrigin.includes("*")) {
1352
+ const regex = new RegExp("^" + allowedOrigin.replace(/\*/g, ".*") + "$");
1353
+ return regex.test(origin);
1354
+ }
1355
+ return allowedOrigin === origin;
1356
+ }
1357
+ }
1327
1358
 
1328
1359
  //#endregion
1329
1360
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "abler-api",
3
- "version": "0.1.76",
3
+ "version": "0.1.77",
4
4
  "description": "API服务相关工具",
5
5
  "main": "./dist/cjs/pp-util.js",
6
6
  "-module": "./dist/es/pp-util.js",