nestjs-shield 1.0.0

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 (77) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +361 -0
  3. package/dist/algorithms/fixed-window.d.ts +4 -0
  4. package/dist/algorithms/fixed-window.js +9 -0
  5. package/dist/algorithms/leaky-bucket.d.ts +4 -0
  6. package/dist/algorithms/leaky-bucket.js +10 -0
  7. package/dist/algorithms/sliding-window-counter.d.ts +4 -0
  8. package/dist/algorithms/sliding-window-counter.js +9 -0
  9. package/dist/algorithms/sliding-window-log.d.ts +4 -0
  10. package/dist/algorithms/sliding-window-log.js +9 -0
  11. package/dist/algorithms/token-bucket.d.ts +4 -0
  12. package/dist/algorithms/token-bucket.js +10 -0
  13. package/dist/apply-to.d.ts +8 -0
  14. package/dist/apply-to.js +42 -0
  15. package/dist/checks/auto-ban.check.d.ts +6 -0
  16. package/dist/checks/auto-ban.check.js +43 -0
  17. package/dist/checks/blacklist.check.d.ts +4 -0
  18. package/dist/checks/blacklist.check.js +20 -0
  19. package/dist/checks/burst.check.d.ts +5 -0
  20. package/dist/checks/burst.check.js +28 -0
  21. package/dist/checks/payload.check.d.ts +5 -0
  22. package/dist/checks/payload.check.js +46 -0
  23. package/dist/checks/rate-limit.check.d.ts +9 -0
  24. package/dist/checks/rate-limit.check.js +59 -0
  25. package/dist/checks/slow-down.check.d.ts +5 -0
  26. package/dist/checks/slow-down.check.js +19 -0
  27. package/dist/checks/user-agent.check.d.ts +4 -0
  28. package/dist/checks/user-agent.check.js +31 -0
  29. package/dist/checks/whitelist.check.d.ts +4 -0
  30. package/dist/checks/whitelist.check.js +15 -0
  31. package/dist/decorators/blacklist.decorator.d.ts +2 -0
  32. package/dist/decorators/blacklist.decorator.js +7 -0
  33. package/dist/decorators/burst-limit.decorator.d.ts +2 -0
  34. package/dist/decorators/burst-limit.decorator.js +7 -0
  35. package/dist/decorators/max-payload.decorator.d.ts +2 -0
  36. package/dist/decorators/max-payload.decorator.js +7 -0
  37. package/dist/decorators/rate-limit.decorator.d.ts +2 -0
  38. package/dist/decorators/rate-limit.decorator.js +7 -0
  39. package/dist/decorators/skip-shield.decorator.d.ts +2 -0
  40. package/dist/decorators/skip-shield.decorator.js +7 -0
  41. package/dist/decorators/slow-down.decorator.d.ts +2 -0
  42. package/dist/decorators/slow-down.decorator.js +7 -0
  43. package/dist/decorators/user-agent-policy.decorator.d.ts +2 -0
  44. package/dist/decorators/user-agent-policy.decorator.js +7 -0
  45. package/dist/decorators/whitelist.decorator.d.ts +2 -0
  46. package/dist/decorators/whitelist.decorator.js +7 -0
  47. package/dist/exceptions/shield.exceptions.d.ts +23 -0
  48. package/dist/exceptions/shield.exceptions.js +40 -0
  49. package/dist/index.d.ts +32 -0
  50. package/dist/index.js +58 -0
  51. package/dist/shield.constants.d.ts +22 -0
  52. package/dist/shield.constants.js +33 -0
  53. package/dist/shield.engine.d.ts +21 -0
  54. package/dist/shield.engine.js +215 -0
  55. package/dist/shield.guard.d.ts +10 -0
  56. package/dist/shield.guard.js +89 -0
  57. package/dist/shield.middleware.d.ts +5 -0
  58. package/dist/shield.middleware.js +47 -0
  59. package/dist/shield.module.d.ts +14 -0
  60. package/dist/shield.module.js +105 -0
  61. package/dist/shield.types.d.ts +141 -0
  62. package/dist/shield.types.js +2 -0
  63. package/dist/storage/memory.storage.d.ts +30 -0
  64. package/dist/storage/memory.storage.js +201 -0
  65. package/dist/storage/redis.storage.d.ts +44 -0
  66. package/dist/storage/redis.storage.js +258 -0
  67. package/dist/storage/shield-storage.interface.d.ts +30 -0
  68. package/dist/storage/shield-storage.interface.js +2 -0
  69. package/dist/utils/headers.util.d.ts +5 -0
  70. package/dist/utils/headers.util.js +23 -0
  71. package/dist/utils/ip.util.d.ts +6 -0
  72. package/dist/utils/ip.util.js +100 -0
  73. package/dist/utils/key.util.d.ts +5 -0
  74. package/dist/utils/key.util.js +24 -0
  75. package/dist/utils/ua.util.d.ts +4 -0
  76. package/dist/utils/ua.util.js +26 -0
  77. package/package.json +77 -0
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.KeyUtil = void 0;
4
+ class KeyUtil {
5
+ static fromRequest(req, ip, keyBy) {
6
+ if (!keyBy || keyBy === 'ip')
7
+ return ip;
8
+ if (typeof keyBy === 'function')
9
+ return keyBy(req);
10
+ if (typeof keyBy === 'object' && 'header' in keyBy) {
11
+ const v = req.headers[keyBy.header.toLowerCase()];
12
+ if (!v)
13
+ return ip;
14
+ return Array.isArray(v) ? v.join(',') : String(v);
15
+ }
16
+ return ip;
17
+ }
18
+ static route(req) {
19
+ const method = (req.method ?? 'GET').toUpperCase();
20
+ const url = String(req.url ?? '/').split('?')[0];
21
+ return `${method} ${url}`;
22
+ }
23
+ }
24
+ exports.KeyUtil = KeyUtil;
@@ -0,0 +1,4 @@
1
+ export declare class UaUtil {
2
+ static match(ua: string, patterns: (string | RegExp)[]): boolean;
3
+ static extract(headers: Record<string, string | string[] | undefined>): string;
4
+ }
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UaUtil = void 0;
4
+ class UaUtil {
5
+ static match(ua, patterns) {
6
+ if (!ua)
7
+ return false;
8
+ for (const p of patterns) {
9
+ if (p instanceof RegExp) {
10
+ if (p.test(ua))
11
+ return true;
12
+ continue;
13
+ }
14
+ if (ua.toLowerCase().includes(String(p).toLowerCase()))
15
+ return true;
16
+ }
17
+ return false;
18
+ }
19
+ static extract(headers) {
20
+ const v = headers['user-agent'];
21
+ if (!v)
22
+ return '';
23
+ return Array.isArray(v) ? v.join(' ') : v;
24
+ }
25
+ }
26
+ exports.UaUtil = UaUtil;
package/package.json ADDED
@@ -0,0 +1,77 @@
1
+ {
2
+ "name": "nestjs-shield",
3
+ "version": "1.0.0",
4
+ "description": "Layered API protection for NestJS: rate limiting, IP allow/block lists, auto-ban, slow-down, UA filtering, burst caps, payload limits. Pluggable storage (memory/Redis).",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist",
9
+ "README.md",
10
+ "LICENSE"
11
+ ],
12
+ "scripts": {
13
+ "build": "tsc -p tsconfig.build.json",
14
+ "clean": "rimraf dist",
15
+ "prebuild": "npm run clean",
16
+ "prepublishOnly": "npm run build"
17
+ },
18
+ "keywords": [
19
+ "nestjs",
20
+ "rate-limit",
21
+ "rate-limiting",
22
+ "throttle",
23
+ "throttler",
24
+ "shield",
25
+ "security",
26
+ "api-protection",
27
+ "ddos",
28
+ "token-bucket",
29
+ "sliding-window",
30
+ "leaky-bucket",
31
+ "ip-blacklist",
32
+ "ip-whitelist",
33
+ "auto-ban",
34
+ "slow-down",
35
+ "redis"
36
+ ],
37
+ "author": "yassinoscoder",
38
+ "license": "MIT",
39
+ "homepage": "https://github.com/Yassinos-coder/nestjs-shield#readme",
40
+ "repository": {
41
+ "type": "git",
42
+ "url": "git+https://github.com/Yassinos-coder/nestjs-shield.git"
43
+ },
44
+ "bugs": {
45
+ "url": "https://github.com/Yassinos-coder/nestjs-shield/issues"
46
+ },
47
+ "publishConfig": {
48
+ "access": "public"
49
+ },
50
+ "engines": {
51
+ "node": ">=18"
52
+ },
53
+ "peerDependencies": {
54
+ "@nestjs/common": "^9.0.0 || ^10.0.0 || ^11.0.0",
55
+ "@nestjs/core": "^9.0.0 || ^10.0.0 || ^11.0.0",
56
+ "reflect-metadata": "^0.1.13 || ^0.2.0",
57
+ "rxjs": "^7.0.0"
58
+ },
59
+ "peerDependenciesMeta": {
60
+ "ioredis": {
61
+ "optional": true
62
+ }
63
+ },
64
+ "dependencies": {
65
+ "ipaddr.js": "^2.2.0"
66
+ },
67
+ "devDependencies": {
68
+ "@nestjs/common": "^10.4.0",
69
+ "@nestjs/core": "^10.4.0",
70
+ "@types/node": "^20.12.0",
71
+ "ioredis": "^5.4.1",
72
+ "reflect-metadata": "^0.2.2",
73
+ "rimraf": "^5.0.7",
74
+ "rxjs": "^7.8.1",
75
+ "typescript": "^5.4.5"
76
+ }
77
+ }