@chdlabs/my-shield 0.1.3 → 0.3.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.
@@ -0,0 +1,56 @@
1
+ /**
2
+ * In-Memory Storage Adapter
3
+ *
4
+ * Default ephemeral adapter for non-clustered, single-process deployments
5
+ * with zero filesystem or network overhead.
6
+ *
7
+ * @author ChdLabs
8
+ * @license MIT
9
+ */
10
+ import { BaseStorageAdapter } from './BaseAdapter.js';
11
+
12
+ export class MemoryAdapter extends BaseStorageAdapter {
13
+ constructor() {
14
+ super();
15
+ this.records = new Map();
16
+ }
17
+
18
+ async init(shield) {
19
+ await super.init(shield);
20
+ }
21
+
22
+ async loadBlockedIPs() {
23
+ const now = Date.now();
24
+ const active = [];
25
+ for (const [ip, item] of this.records.entries()) {
26
+ if (item.expiresAt && item.expiresAt <= now) {
27
+ this.records.delete(ip);
28
+ } else {
29
+ active.push(item);
30
+ }
31
+ }
32
+ return active;
33
+ }
34
+
35
+ async saveBlockedIP(ip, reason, metadata = {}, expiresAt = null) {
36
+ if (!ip) return;
37
+ this.records.set(ip, {
38
+ ip,
39
+ reason,
40
+ metadata,
41
+ expiresAt,
42
+ savedAt: Date.now()
43
+ });
44
+ }
45
+
46
+ async removeBlockedIP(ip) {
47
+ if (!ip) return;
48
+ this.records.delete(ip);
49
+ }
50
+
51
+ async close() {
52
+ this.records.clear();
53
+ }
54
+ }
55
+
56
+ export default MemoryAdapter;
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Storage Subsystem Exports & Factory
3
+ *
4
+ * @author ChdLabs
5
+ * @license MIT
6
+ */
7
+ import { BaseStorageAdapter } from './BaseAdapter.js';
8
+ import { MemoryAdapter } from './MemoryAdapter.js';
9
+ import { FileStorageAdapter } from './FileStorageAdapter.js';
10
+ import { CallbackStorageAdapter } from './CallbackStorageAdapter.js';
11
+
12
+ export {
13
+ BaseStorageAdapter,
14
+ MemoryAdapter,
15
+ FileStorageAdapter,
16
+ CallbackStorageAdapter
17
+ };
18
+
19
+ /**
20
+ * Instantiate appropriate storage adapter based on options
21
+ * @param {string|Object|BaseStorageAdapter} [storageOption]
22
+ * @param {Object} [logger]
23
+ * @returns {BaseStorageAdapter}
24
+ */
25
+ export function createStorageAdapter(storageOption, logger = console) {
26
+ if (!storageOption || storageOption === 'memory') {
27
+ return new MemoryAdapter();
28
+ }
29
+
30
+ if (storageOption instanceof BaseStorageAdapter) {
31
+ return storageOption;
32
+ }
33
+
34
+ if (typeof storageOption === 'object') {
35
+ // Callback adapter: has onLoad, onBlock, or onUnblock
36
+ if (storageOption.onLoad || storageOption.onBlock || storageOption.onUnblock) {
37
+ return new CallbackStorageAdapter({ ...storageOption, logger });
38
+ }
39
+
40
+ // File adapter: has type === 'file' or directory or filename
41
+ if (storageOption.type === 'file' || storageOption.directory || storageOption.filename) {
42
+ return new FileStorageAdapter({ ...storageOption, logger });
43
+ }
44
+ }
45
+
46
+ if (storageOption === 'file') {
47
+ return new FileStorageAdapter({ logger });
48
+ }
49
+
50
+ return new MemoryAdapter();
51
+ }
52
+
53
+ export default createStorageAdapter;