@lanaqi/rsr 0.0.1-rc.0 → 0.0.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.
Files changed (3) hide show
  1. package/README.md +1 -1
  2. package/dist/index.js +55 -58
  3. package/package.json +8 -8
package/README.md CHANGED
@@ -15,7 +15,7 @@ rsr 是 react security router 的简写,一个基于 react router 实现路由
15
15
 
16
16
  # 版本兼容
17
17
 
18
- 注意:不兼容 react router 7 版本,在 v7 😒 版本上的 `useBlocker` 逻辑变更了,暂时不考虑兼容。
18
+ 注意:目前只兼容 react router v6 & v7 版本,其它的版本,暂时不考虑兼容。
19
19
 
20
20
  # 简单例子
21
21
 
package/dist/index.js CHANGED
@@ -97,7 +97,7 @@ class SimpleResource {
97
97
  this.patterns = new Set(patterns);
98
98
  this.permissions = new Set(permissions);
99
99
  this.labels = new Set(labels);
100
- if (0 === this.patterns.size) throw new Error('访问资源模式集合不能为空,请检查!');
100
+ if (0 === this.patterns.size) throw new Error("\u8BBF\u95EE\u8D44\u6E90\u6A21\u5F0F\u96C6\u5408\u4E0D\u80FD\u4E3A\u7A7A\uFF0C\u8BF7\u68C0\u67E5\uFF01");
101
101
  if (0 === this.permissions.size) {
102
102
  this.anonymous = true;
103
103
  this.authenticated = false;
@@ -431,10 +431,8 @@ class SimpleStorer {
431
431
  const authenticationStr = this.aaaStorage.getItem(this.authenticationKey);
432
432
  if (authenticationStr) {
433
433
  const authenticationObj = JSON.parse(authenticationStr);
434
- if ('boolean' == typeof authenticationObj.authenticated) {
435
- if (void 0 !== authenticationObj.permissions && Array.isArray(authenticationObj.permissions)) return new SimpleUser(authenticationObj);
436
- return new SimpleAuthentication(authenticationObj);
437
- }
434
+ if ('boolean' == typeof authenticationObj.authenticated) if (void 0 !== authenticationObj.permissions && Array.isArray(authenticationObj.permissions)) return new SimpleUser(authenticationObj);
435
+ else return new SimpleAuthentication(authenticationObj);
438
436
  }
439
437
  }
440
438
  verifyAuthentication(recorder, authentication) {
@@ -639,7 +637,7 @@ class BehaveHandler {
639
637
  return common_AccessBehave.goNavigate;
640
638
  }
641
639
  if (this._config.notAuthenticationFunc) return this._config.notAuthenticationFunc(context);
642
- throw new Error('没有认证的行为是必须配置的,请检查!');
640
+ throw new Error("\u6CA1\u6709\u8BA4\u8BC1\u7684\u884C\u4E3A\u662F\u5FC5\u987B\u914D\u7F6E\u7684\uFF0C\u8BF7\u68C0\u67E5\uFF01");
643
641
  }
644
642
  invalidAuthentication(context) {
645
643
  if (this._config.invalidAuthenticationPath) {
@@ -671,24 +669,21 @@ class BehaveHandler {
671
669
  return common_AccessBehave.goNavigate;
672
670
  }
673
671
  if (this._config.accessDeniedFunc) return this._config.accessDeniedFunc(context);
674
- throw new Error('拒绝访问的行为是必须配置的,请检查!');
672
+ throw new Error("\u62D2\u7EDD\u8BBF\u95EE\u7684\u884C\u4E3A\u662F\u5FC5\u987B\u914D\u7F6E\u7684\uFF0C\u8BF7\u68C0\u67E5\uFF01");
675
673
  }
676
674
  allowAccess(context) {
677
675
  if (this._config.allowAccessFunc) this._config.allowAccessFunc(context);
678
676
  return common_AccessBehave.doNothing;
679
677
  }
680
678
  errorDecision(context, decision) {
681
- if (this._config.errorDecisionFunc) {
682
- this._config.errorDecisionFunc(context, decision);
683
- return;
684
- }
679
+ if (this._config.errorDecisionFunc) return void this._config.errorDecisionFunc(context, decision);
685
680
  if (decision === common_AccessDecision.notSignature) return;
686
681
  switch(decision){
687
682
  case common_AccessDecision.notResource:
688
683
  this.accessDenied(context);
689
684
  break;
690
685
  case common_AccessDecision.notAuthentication:
691
- throw new Error('没有认证的错误决策,请检查!');
686
+ throw new Error("\u6CA1\u6709\u8BA4\u8BC1\u7684\u9519\u8BEF\u51B3\u7B56\uFF0C\u8BF7\u68C0\u67E5\uFF01");
692
687
  case common_AccessDecision.invalidAuthentication:
693
688
  this.notAuthentication(context);
694
689
  break;
@@ -697,7 +692,7 @@ class BehaveHandler {
697
692
  break;
698
693
  case common_AccessDecision.accessDenied:
699
694
  default:
700
- throw new Error('拒绝访问的错误决策,请检查!');
695
+ throw new Error("\u62D2\u7EDD\u8BBF\u95EE\u7684\u9519\u8BEF\u51B3\u7B56\uFF0C\u8BF7\u68C0\u67E5\uFF01");
701
696
  }
702
697
  }
703
698
  }
@@ -1121,7 +1116,7 @@ class AccessNavigatorBuilder {
1121
1116
  return this;
1122
1117
  }
1123
1118
  build() {
1124
- if (!this._navigate) throw new Error('导航函数是必须设置的,请检查!');
1119
+ if (!this._navigate) throw new Error("\u5BFC\u822A\u51FD\u6570\u662F\u5FC5\u987B\u8BBE\u7F6E\u7684\uFF0C\u8BF7\u68C0\u67E5\uFF01");
1125
1120
  return new SimpleNavigator(this._navigate);
1126
1121
  }
1127
1122
  }
@@ -1210,7 +1205,7 @@ class AccessHandlerBuilder {
1210
1205
  return this;
1211
1206
  }
1212
1207
  build() {
1213
- if (!this._config) throw new Error('行为配置是必须设置,请检查!');
1208
+ if (!this._config) throw new Error("\u884C\u4E3A\u914D\u7F6E\u662F\u5FC5\u987B\u8BBE\u7F6E\uFF0C\u8BF7\u68C0\u67E5\uFF01");
1214
1209
  return new BehaveHandler(this._config);
1215
1210
  }
1216
1211
  }
@@ -1307,7 +1302,7 @@ class AccessGuarderBuilder {
1307
1302
  const SecurityContext = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.createContext)(null);
1308
1303
  const useSecurityContext = ()=>{
1309
1304
  const sc = (0, __WEBPACK_EXTERNAL_MODULE_react__.useContext)(SecurityContext);
1310
- if (!sc) throw new Error('安全上下文为空,必须使用安全提供者包裹组件来设置安全上下文');
1305
+ if (!sc) throw new Error("\u5B89\u5168\u4E0A\u4E0B\u6587\u4E3A\u7A7A\uFF0C\u5FC5\u987B\u4F7F\u7528\u5B89\u5168\u63D0\u4F9B\u8005\u5305\u88F9\u7EC4\u4EF6\u6765\u8BBE\u7F6E\u5B89\u5168\u4E0A\u4E0B\u6587");
1311
1306
  return sc;
1312
1307
  };
1313
1308
  function SecurityProvider({ children, bundler }) {
@@ -1331,74 +1326,76 @@ function SecurityBlocker({ children }) {
1331
1326
  const [nextPath, setNextPath] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)((0, __WEBPACK_EXTERNAL_MODULE_react_router_dom_5358f3fe__.useLocation)());
1332
1327
  const [firstAccess, setFirstAccess] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(true);
1333
1328
  const [firstHandle, setFirstHandle] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(true);
1334
- const [firstSignature, setFirstSignature] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(1);
1335
- const [handleDecision, setHandleDecision] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(false);
1329
+ const [countSignature, setCountSignature] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(1);
1330
+ const [handledDecision, setHandledDecision] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(false);
1336
1331
  const [beforeDecision, setBeforeDecision] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(void 0);
1337
1332
  const [currentDecision, setCurrentDecision] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(void 0);
1338
- const [executeBlock, setExecuteBlock] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(false);
1333
+ const [securityBlock, setSecurityBlock] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(false);
1334
+ const [executableBlocked, setExecutableBlocked] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(true);
1339
1335
  (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
1340
- if ('blocked' === blocker.state) {
1336
+ if ('blocked' === blocker.state && executableBlocked) {
1337
+ let isProceed;
1341
1338
  const recorder = context.getRecorder();
1342
1339
  const stayPath = recorder.getAllowPath();
1343
1340
  const blockPath = blocker.location;
1344
1341
  let guardBlocked = false;
1345
- if (executeBlock) guardBlocked = guarder.guardBlock(blockPath);
1342
+ if (securityBlock) guardBlocked = guarder.guardBlock(blockPath);
1346
1343
  if (guardBlocked) {
1347
- blocker.reset();
1348
- setNextPath(blockPath);
1349
- setHandleDecision(false);
1344
+ setHandledDecision(false);
1350
1345
  setCurrentDecision(void 0);
1346
+ isProceed = false;
1351
1347
  } else {
1352
1348
  if (!beforeDecision) guarder.guardBefore(blockPath);
1353
1349
  const blockedDecision = guarder.guardDecision(blockPath);
1354
- if (blockedDecision === common_AccessDecision.allowAccess) {
1355
- const isDiff = !!stayPath && stayPath.pathname !== blockPath.pathname;
1356
- if (isDiff) guarder.permitBefore(stayPath, blockPath);
1357
- blocker.proceed();
1358
- if (isDiff) guarder.permitAfter(stayPath, blockPath);
1359
- } else blocker.reset();
1360
- setNextPath(blockPath);
1361
- setHandleDecision(true);
1350
+ setHandledDecision(true);
1362
1351
  setCurrentDecision(blockedDecision);
1352
+ isProceed = blockedDecision === common_AccessDecision.allowAccess;
1363
1353
  }
1354
+ setNextPath(blockPath);
1355
+ setExecutableBlocked(false);
1356
+ if (isProceed) {
1357
+ const isDiff = !!stayPath && stayPath.pathname !== blockPath.pathname;
1358
+ if (isDiff) guarder.permitBefore(stayPath, blockPath);
1359
+ blocker.proceed();
1360
+ if (isDiff) guarder.permitAfter(stayPath, blockPath);
1361
+ } else blocker.reset();
1364
1362
  } else if ('unblocked' === blocker.state) {
1365
- if (handleDecision && currentDecision) {
1363
+ if (handledDecision && currentDecision) {
1366
1364
  let navNext;
1367
1365
  const behave = guarder.guardHandle(currentDecision, beforeDecision);
1368
1366
  switch(behave){
1369
1367
  case common_AccessBehave.reDecision:
1370
1368
  setBeforeDecision(currentDecision);
1371
- setExecuteBlock(false);
1369
+ setSecurityBlock(false);
1372
1370
  navNext = true;
1373
1371
  break;
1374
1372
  case common_AccessBehave.goNavigate:
1375
1373
  setBeforeDecision(void 0);
1376
1374
  navNext = false;
1377
- setExecuteBlock(false);
1375
+ setSecurityBlock(false);
1378
1376
  break;
1379
1377
  case common_AccessBehave.doNothing:
1380
1378
  default:
1381
1379
  setBeforeDecision(void 0);
1382
1380
  navNext = false;
1383
- setExecuteBlock(true);
1381
+ setSecurityBlock(true);
1384
1382
  break;
1385
1383
  }
1386
- if (firstHandle && currentDecision === common_AccessDecision.notSignature && behave === common_AccessBehave.reDecision) {
1387
- if (firstSignature >= 3) {
1388
- setFirstHandle(false);
1389
- setBeforeDecision(common_AccessDecision.notSignature);
1390
- setHandleDecision(true);
1391
- setCurrentDecision(common_AccessDecision.accessDenied);
1392
- } else {
1393
- const signDecision = guarder.guardDecision(nextPath);
1394
- setHandleDecision(true);
1395
- setBeforeDecision(void 0);
1396
- setCurrentDecision(signDecision);
1397
- setFirstSignature(firstSignature + 1);
1398
- }
1384
+ if (firstHandle && currentDecision === common_AccessDecision.notSignature && behave === common_AccessBehave.reDecision) if (countSignature >= 3) {
1385
+ setFirstHandle(false);
1386
+ setBeforeDecision(common_AccessDecision.notSignature);
1387
+ setHandledDecision(true);
1388
+ setCurrentDecision(common_AccessDecision.accessDenied);
1399
1389
  } else {
1390
+ const signDecision = guarder.guardDecision(nextPath);
1391
+ setHandledDecision(true);
1392
+ setBeforeDecision(void 0);
1393
+ setCurrentDecision(signDecision);
1394
+ setCountSignature(countSignature + 1);
1395
+ }
1396
+ else {
1400
1397
  setFirstHandle(false);
1401
- setHandleDecision(false);
1398
+ setHandledDecision(false);
1402
1399
  setCurrentDecision(void 0);
1403
1400
  if (navNext) context.getNavigator().navigate(nextPath);
1404
1401
  else {
@@ -1410,9 +1407,10 @@ function SecurityBlocker({ children }) {
1410
1407
  guarder.guardBefore(nextPath);
1411
1408
  const firstDecision = guarder.guardDecision(nextPath);
1412
1409
  setFirstAccess(false);
1413
- setHandleDecision(true);
1410
+ setHandledDecision(true);
1414
1411
  setCurrentDecision(firstDecision);
1415
1412
  }
1413
+ if (!executableBlocked) setExecutableBlocked(true);
1416
1414
  }
1417
1415
  }, [
1418
1416
  blocker,
@@ -1421,11 +1419,12 @@ function SecurityBlocker({ children }) {
1421
1419
  nextPath,
1422
1420
  firstAccess,
1423
1421
  firstHandle,
1424
- firstSignature,
1425
- handleDecision,
1422
+ countSignature,
1423
+ handledDecision,
1426
1424
  beforeDecision,
1427
1425
  currentDecision,
1428
- executeBlock
1426
+ securityBlock,
1427
+ executableBlocked
1429
1428
  ]);
1430
1429
  if (!guarded) return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime_225474f2__.jsx)(__WEBPACK_EXTERNAL_MODULE_react_jsx_runtime_225474f2__.Fragment, {});
1431
1430
  return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime_225474f2__.jsx)(__WEBPACK_EXTERNAL_MODULE_react_jsx_runtime_225474f2__.Fragment, {
@@ -1559,10 +1558,8 @@ const useSaveSignature = (navigate = true, redirect = '/')=>{
1559
1558
  let target;
1560
1559
  target = path ? path : recorder.getOriginPath();
1561
1560
  if (target) storer.saveSignature(recorder, target);
1562
- if (navigate) {
1563
- if (target) navigator.navigate(target);
1564
- else navigator.navigate(redirect);
1565
- }
1561
+ if (navigate) if (target) navigator.navigate(target);
1562
+ else navigator.navigate(redirect);
1566
1563
  };
1567
1564
  };
1568
1565
  const useDeleteSignature = ()=>{
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lanaqi/rsr",
3
- "version": "0.0.1-rc.0",
3
+ "version": "0.0.1",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -49,18 +49,18 @@
49
49
  "devDependencies": {
50
50
  "@biomejs/biome": "^1.9.4",
51
51
  "@rsbuild/plugin-react": "^1.1.1",
52
- "@rslib/core": "^0.5.5",
53
- "@types/react": "^18.3.19",
54
- "@types/react-dom": "^18.3.5",
55
- "react": "^18.3.1",
56
- "react-dom": "^18.3.1",
57
- "react-router-dom": "^6.30.0",
52
+ "@rslib/core": "^0.6.8",
53
+ "@types/react": "^19.1.3",
54
+ "@types/react-dom": "^19.1.3",
55
+ "react": "^19.1.0",
56
+ "react-dom": "^19.1.0",
57
+ "react-router-dom": "^7.5.3",
58
58
  "typescript": "^5.8.2"
59
59
  },
60
60
  "peerDependencies": {
61
61
  "react": ">=17.0.0",
62
62
  "react-dom": ">=17.0.0",
63
- "react-router-dom": "^6.0.0"
63
+ "react-router-dom": "^6.0.0 || ^7.0.0"
64
64
  },
65
65
  "private": false,
66
66
  "publishConfig": {