strata-storage 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 (102) hide show
  1. package/Readme.md +113 -0
  2. package/android/src/main/java/com/strata/storage/EncryptedStorage.java +65 -0
  3. package/android/src/main/java/com/strata/storage/SQLiteStorage.java +147 -0
  4. package/android/src/main/java/com/strata/storage/SharedPreferencesStorage.java +74 -0
  5. package/android/src/main/java/com/stratastorage/StrataStoragePlugin.java +256 -0
  6. package/dist/adapters/capacitor/FilesystemAdapter.d.ts +46 -0
  7. package/dist/adapters/capacitor/FilesystemAdapter.d.ts.map +1 -0
  8. package/dist/adapters/capacitor/FilesystemAdapter.js +162 -0
  9. package/dist/adapters/capacitor/PreferencesAdapter.d.ts +46 -0
  10. package/dist/adapters/capacitor/PreferencesAdapter.d.ts.map +1 -0
  11. package/dist/adapters/capacitor/PreferencesAdapter.js +162 -0
  12. package/dist/adapters/capacitor/SecureAdapter.d.ts +69 -0
  13. package/dist/adapters/capacitor/SecureAdapter.d.ts.map +1 -0
  14. package/dist/adapters/capacitor/SecureAdapter.js +214 -0
  15. package/dist/adapters/capacitor/SqliteAdapter.d.ts +68 -0
  16. package/dist/adapters/capacitor/SqliteAdapter.d.ts.map +1 -0
  17. package/dist/adapters/capacitor/SqliteAdapter.js +277 -0
  18. package/dist/adapters/capacitor/index.d.ts +9 -0
  19. package/dist/adapters/capacitor/index.d.ts.map +1 -0
  20. package/dist/adapters/capacitor/index.js +8 -0
  21. package/dist/adapters/web/CacheAdapter.d.ts +91 -0
  22. package/dist/adapters/web/CacheAdapter.d.ts.map +1 -0
  23. package/dist/adapters/web/CacheAdapter.js +291 -0
  24. package/dist/adapters/web/CookieAdapter.d.ts +77 -0
  25. package/dist/adapters/web/CookieAdapter.d.ts.map +1 -0
  26. package/dist/adapters/web/CookieAdapter.js +260 -0
  27. package/dist/adapters/web/IndexedDBAdapter.d.ts +78 -0
  28. package/dist/adapters/web/IndexedDBAdapter.d.ts.map +1 -0
  29. package/dist/adapters/web/IndexedDBAdapter.js +371 -0
  30. package/dist/adapters/web/LocalStorageAdapter.d.ts +63 -0
  31. package/dist/adapters/web/LocalStorageAdapter.d.ts.map +1 -0
  32. package/dist/adapters/web/LocalStorageAdapter.js +238 -0
  33. package/dist/adapters/web/MemoryAdapter.d.ts +69 -0
  34. package/dist/adapters/web/MemoryAdapter.d.ts.map +1 -0
  35. package/dist/adapters/web/MemoryAdapter.js +165 -0
  36. package/dist/adapters/web/SessionStorageAdapter.d.ts +53 -0
  37. package/dist/adapters/web/SessionStorageAdapter.d.ts.map +1 -0
  38. package/dist/adapters/web/SessionStorageAdapter.js +180 -0
  39. package/dist/adapters/web/index.d.ts +10 -0
  40. package/dist/adapters/web/index.d.ts.map +1 -0
  41. package/dist/adapters/web/index.js +9 -0
  42. package/dist/core/AdapterRegistry.d.ts +52 -0
  43. package/dist/core/AdapterRegistry.d.ts.map +1 -0
  44. package/dist/core/AdapterRegistry.js +102 -0
  45. package/dist/core/BaseAdapter.d.ts +79 -0
  46. package/dist/core/BaseAdapter.d.ts.map +1 -0
  47. package/dist/core/BaseAdapter.js +197 -0
  48. package/dist/core/StorageStrategy.d.ts +55 -0
  49. package/dist/core/StorageStrategy.d.ts.map +1 -0
  50. package/dist/core/StorageStrategy.js +199 -0
  51. package/dist/core/Strata.d.ts +122 -0
  52. package/dist/core/Strata.d.ts.map +1 -0
  53. package/dist/core/Strata.js +568 -0
  54. package/dist/features/compression.d.ts +65 -0
  55. package/dist/features/compression.d.ts.map +1 -0
  56. package/dist/features/compression.js +205 -0
  57. package/dist/features/encryption.d.ts +68 -0
  58. package/dist/features/encryption.d.ts.map +1 -0
  59. package/dist/features/encryption.js +172 -0
  60. package/dist/features/migration.d.ts +17 -0
  61. package/dist/features/migration.d.ts.map +1 -0
  62. package/dist/features/migration.js +43 -0
  63. package/dist/features/query.d.ts +75 -0
  64. package/dist/features/query.d.ts.map +1 -0
  65. package/dist/features/query.js +305 -0
  66. package/dist/features/sync.d.ts +87 -0
  67. package/dist/features/sync.d.ts.map +1 -0
  68. package/dist/features/sync.js +233 -0
  69. package/dist/features/ttl.d.ts +124 -0
  70. package/dist/features/ttl.d.ts.map +1 -0
  71. package/dist/features/ttl.js +236 -0
  72. package/dist/index.d.ts +44 -0
  73. package/dist/index.d.ts.map +1 -0
  74. package/dist/index.js +46 -0
  75. package/dist/package.json +60 -0
  76. package/dist/plugin/definitions.d.ts +219 -0
  77. package/dist/plugin/definitions.d.ts.map +1 -0
  78. package/dist/plugin/definitions.js +5 -0
  79. package/dist/plugin/index.d.ts +8 -0
  80. package/dist/plugin/index.d.ts.map +1 -0
  81. package/dist/plugin/index.js +27 -0
  82. package/dist/plugin/web.d.ts +24 -0
  83. package/dist/plugin/web.d.ts.map +1 -0
  84. package/dist/plugin/web.js +35 -0
  85. package/dist/types/index.d.ts +558 -0
  86. package/dist/types/index.d.ts.map +1 -0
  87. package/dist/types/index.js +14 -0
  88. package/dist/utils/errors.d.ts +92 -0
  89. package/dist/utils/errors.d.ts.map +1 -0
  90. package/dist/utils/errors.js +153 -0
  91. package/dist/utils/index.d.ts +105 -0
  92. package/dist/utils/index.d.ts.map +1 -0
  93. package/dist/utils/index.js +329 -0
  94. package/ios/Plugin/KeychainStorage.swift +87 -0
  95. package/ios/Plugin/SQLiteStorage.swift +167 -0
  96. package/ios/Plugin/StrataStoragePlugin.swift +204 -0
  97. package/ios/Plugin/UserDefaultsStorage.swift +44 -0
  98. package/package.json +126 -0
  99. package/scripts/build.js +52 -0
  100. package/scripts/cli.js +60 -0
  101. package/scripts/configure.js +444 -0
  102. package/scripts/postinstall.js +33 -0
@@ -0,0 +1,79 @@
1
+ /**
2
+ * Base adapter implementation with common functionality
3
+ */
4
+ import type { StorageAdapter, StorageValue, StorageType, StorageCapabilities, ClearOptions, SizeInfo, SubscriptionCallback, UnsubscribeFunction, QueryCondition } from '@/types';
5
+ import { EventEmitter } from '@/utils';
6
+ import { QueryEngine } from '@/features/query';
7
+ /**
8
+ * Abstract base adapter that implements common functionality
9
+ */
10
+ export declare abstract class BaseAdapter implements StorageAdapter {
11
+ abstract readonly name: StorageType;
12
+ abstract readonly capabilities: StorageCapabilities;
13
+ protected eventEmitter: EventEmitter;
14
+ protected queryEngine: QueryEngine;
15
+ protected ttlCleanupInterval?: ReturnType<typeof setInterval>;
16
+ protected ttlCheckInterval: number;
17
+ /**
18
+ * Initialize TTL cleanup if needed
19
+ */
20
+ protected startTTLCleanup(): void;
21
+ /**
22
+ * Stop TTL cleanup
23
+ */
24
+ protected stopTTLCleanup(): void;
25
+ /**
26
+ * Clean up expired items
27
+ */
28
+ protected cleanupExpired(): Promise<void>;
29
+ /**
30
+ * Check if value is expired
31
+ */
32
+ protected isExpired(value: StorageValue): boolean;
33
+ /**
34
+ * Filter keys by pattern
35
+ */
36
+ protected filterKeys(keys: string[], pattern?: string | RegExp): string[];
37
+ /**
38
+ * Calculate size of storage value
39
+ */
40
+ protected calculateSize(value: StorageValue): number;
41
+ /**
42
+ * Default has implementation using get
43
+ */
44
+ has(key: string): Promise<boolean>;
45
+ /**
46
+ * Default clear implementation
47
+ */
48
+ clear(options?: ClearOptions): Promise<void>;
49
+ /**
50
+ * Default size implementation
51
+ */
52
+ size(detailed?: boolean): Promise<SizeInfo>;
53
+ /**
54
+ * Subscribe to changes (if supported)
55
+ */
56
+ subscribe?(callback: SubscriptionCallback): UnsubscribeFunction;
57
+ /**
58
+ * Emit change event
59
+ */
60
+ protected emitChange(key: string, oldValue: unknown | undefined, newValue: unknown | undefined, source?: 'local' | 'remote'): void;
61
+ /**
62
+ * Query implementation (override in adapters that support it)
63
+ */
64
+ query?<T = unknown>(condition: QueryCondition): Promise<Array<{
65
+ key: string;
66
+ value: T;
67
+ }>>;
68
+ /**
69
+ * Close adapter (cleanup)
70
+ */
71
+ close?(): Promise<void>;
72
+ abstract isAvailable(): Promise<boolean>;
73
+ abstract initialize(config?: unknown): Promise<void>;
74
+ abstract get<T = unknown>(key: string): Promise<StorageValue<T> | null>;
75
+ abstract set<T = unknown>(key: string, value: StorageValue<T>): Promise<void>;
76
+ abstract remove(key: string): Promise<void>;
77
+ abstract keys(pattern?: string | RegExp): Promise<string[]>;
78
+ }
79
+ //# sourceMappingURL=BaseAdapter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"BaseAdapter.d.ts","sourceRoot":"","sources":["../../src/core/BaseAdapter.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EACV,cAAc,EACd,YAAY,EACZ,WAAW,EACX,mBAAmB,EAEnB,YAAY,EACZ,QAAQ,EACR,oBAAoB,EACpB,mBAAmB,EACnB,cAAc,EACf,MAAM,SAAS,CAAC;AAEjB,OAAO,EAAE,YAAY,EAA4B,MAAM,SAAS,CAAC;AACjE,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAE/C;;GAEG;AACH,8BAAsB,WAAY,YAAW,cAAc;IACzD,QAAQ,CAAC,QAAQ,CAAC,IAAI,EAAE,WAAW,CAAC;IACpC,QAAQ,CAAC,QAAQ,CAAC,YAAY,EAAE,mBAAmB,CAAC;IAEpD,SAAS,CAAC,YAAY,eAAsB;IAC5C,SAAS,CAAC,WAAW,cAAqB;IAC1C,SAAS,CAAC,kBAAkB,CAAC,EAAE,UAAU,CAAC,OAAO,WAAW,CAAC,CAAC;IAC9D,SAAS,CAAC,gBAAgB,SAAS;IAEnC;;OAEG;IACH,SAAS,CAAC,eAAe,IAAI,IAAI;IAYjC;;OAEG;IACH,SAAS,CAAC,cAAc,IAAI,IAAI;IAOhC;;OAEG;cACa,cAAc,IAAI,OAAO,CAAC,IAAI,CAAC;IAY/C;;OAEG;IACH,SAAS,CAAC,SAAS,CAAC,KAAK,EAAE,YAAY,GAAG,OAAO;IAKjD;;OAEG;IACH,SAAS,CAAC,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,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AC,GAAG,OAAO,CAAC,IAAI,CAAC;IAC7E,QAAQ,CAAC,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAC3C,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;CAC5D"}
@@ -0,0 +1,197 @@
1
+ /**
2
+ * Base adapter implementation with common functionality
3
+ */
4
+ import { NotSupportedError } from '@/utils/errors';
5
+ import { EventEmitter, matchGlob, getObjectSize } from '@/utils';
6
+ import { QueryEngine } from '@/features/query';
7
+ /**
8
+ * Abstract base adapter that implements common functionality
9
+ */
10
+ export class BaseAdapter {
11
+ eventEmitter = new EventEmitter();
12
+ queryEngine = new QueryEngine();
13
+ ttlCleanupInterval;
14
+ ttlCheckInterval = 60000; // Check every minute
15
+ /**
16
+ * Initialize TTL cleanup if needed
17
+ */
18
+ startTTLCleanup() {
19
+ if (this.ttlCleanupInterval)
20
+ return;
21
+ this.ttlCleanupInterval = setInterval(async () => {
22
+ try {
23
+ await this.cleanupExpired();
24
+ }
25
+ catch (error) {
26
+ console.error(`TTL cleanup error in ${this.name}:`, error);
27
+ }
28
+ }, this.ttlCheckInterval);
29
+ }
30
+ /**
31
+ * Stop TTL cleanup
32
+ */
33
+ stopTTLCleanup() {
34
+ if (this.ttlCleanupInterval) {
35
+ clearInterval(this.ttlCleanupInterval);
36
+ this.ttlCleanupInterval = undefined;
37
+ }
38
+ }
39
+ /**
40
+ * Clean up expired items
41
+ */
42
+ async cleanupExpired() {
43
+ const now = Date.now();
44
+ const keys = await this.keys();
45
+ for (const key of keys) {
46
+ const item = await this.get(key);
47
+ if (item?.expires && item.expires <= now) {
48
+ await this.remove(key);
49
+ }
50
+ }
51
+ }
52
+ /**
53
+ * Check if value is expired
54
+ */
55
+ isExpired(value) {
56
+ if (!value.expires)
57
+ return false;
58
+ return Date.now() > value.expires;
59
+ }
60
+ /**
61
+ * Filter keys by pattern
62
+ */
63
+ filterKeys(keys, pattern) {
64
+ if (!pattern)
65
+ return keys;
66
+ if (pattern instanceof RegExp) {
67
+ return keys.filter((key) => pattern.test(key));
68
+ }
69
+ return keys.filter((key) => matchGlob(pattern, key));
70
+ }
71
+ /**
72
+ * Calculate size of storage value
73
+ */
74
+ calculateSize(value) {
75
+ return getObjectSize(value);
76
+ }
77
+ /**
78
+ * Default has implementation using get
79
+ */
80
+ async has(key) {
81
+ const value = await this.get(key);
82
+ return value !== null && !this.isExpired(value);
83
+ }
84
+ /**
85
+ * Default clear implementation
86
+ */
87
+ async clear(options) {
88
+ const keys = await this.keys();
89
+ for (const key of keys) {
90
+ let shouldDelete = true;
91
+ if (options?.pattern) {
92
+ shouldDelete = this.filterKeys([key], options.pattern).length > 0;
93
+ }
94
+ if (shouldDelete && options?.tags) {
95
+ const value = await this.get(key);
96
+ if (!value?.tags || !options.tags.some((tag) => value.tags?.includes(tag))) {
97
+ shouldDelete = false;
98
+ }
99
+ }
100
+ if (shouldDelete && options?.expiredOnly) {
101
+ const value = await this.get(key);
102
+ if (!value || !this.isExpired(value)) {
103
+ shouldDelete = false;
104
+ }
105
+ }
106
+ if (shouldDelete) {
107
+ await this.remove(key);
108
+ }
109
+ }
110
+ }
111
+ /**
112
+ * Default size implementation
113
+ */
114
+ async size(detailed) {
115
+ const keys = await this.keys();
116
+ let total = 0;
117
+ let keySize = 0;
118
+ let valueSize = 0;
119
+ let metadataSize = 0;
120
+ for (const key of keys) {
121
+ keySize += key.length * 2; // UTF-16
122
+ const item = await this.get(key);
123
+ if (item) {
124
+ const size = this.calculateSize(item);
125
+ valueSize += getObjectSize(item.value);
126
+ metadataSize += size - getObjectSize(item.value);
127
+ total += size;
128
+ }
129
+ }
130
+ const result = {
131
+ total: total + keySize,
132
+ count: keys.length,
133
+ };
134
+ if (detailed) {
135
+ result.detailed = {
136
+ keys: keySize,
137
+ values: valueSize,
138
+ metadata: metadataSize,
139
+ };
140
+ }
141
+ return result;
142
+ }
143
+ /**
144
+ * Subscribe to changes (if supported)
145
+ */
146
+ subscribe(callback) {
147
+ if (!this.capabilities.observable) {
148
+ throw new NotSupportedError('subscribe', this.name);
149
+ }
150
+ const handler = (...args) => {
151
+ const change = args[0];
152
+ callback(change);
153
+ };
154
+ this.eventEmitter.on('change', handler);
155
+ return () => {
156
+ this.eventEmitter.off('change', handler);
157
+ };
158
+ }
159
+ /**
160
+ * Emit change event
161
+ */
162
+ emitChange(key, oldValue, newValue, source = 'local') {
163
+ this.eventEmitter.emit('change', {
164
+ key,
165
+ oldValue,
166
+ newValue,
167
+ source,
168
+ storage: this.name,
169
+ timestamp: Date.now(),
170
+ });
171
+ }
172
+ /**
173
+ * Query implementation (override in adapters that support it)
174
+ */
175
+ async query(condition) {
176
+ if (!this.capabilities.queryable) {
177
+ throw new NotSupportedError('query', this.name);
178
+ }
179
+ // Basic implementation for adapters that don't have native query support
180
+ const results = [];
181
+ const keys = await this.keys();
182
+ for (const key of keys) {
183
+ const item = await this.get(key);
184
+ if (item && !this.isExpired(item) && this.queryEngine.matches(item.value, condition)) {
185
+ results.push({ key, value: item.value });
186
+ }
187
+ }
188
+ return results;
189
+ }
190
+ /**
191
+ * Close adapter (cleanup)
192
+ */
193
+ async close() {
194
+ this.stopTTLCleanup();
195
+ this.eventEmitter.removeAllListeners();
196
+ }
197
+ }
@@ -0,0 +1,55 @@
1
+ /**
2
+ * Storage strategy implementation for automatic adapter selection
3
+ */
4
+ import type { StorageAdapter, StorageType, StorageCapabilities } from '@/types';
5
+ import { StorageStrategy } from '@/types';
6
+ import { AdapterRegistry } from './AdapterRegistry';
7
+ /**
8
+ * Strategy manager for selecting appropriate storage adapters
9
+ */
10
+ export declare class StrategyManager {
11
+ private registry;
12
+ private strategy;
13
+ constructor(registry: AdapterRegistry, strategy?: StorageStrategy);
14
+ /**
15
+ * Get the best adapter based on current strategy
16
+ */
17
+ getBestAdapter(preferredTypes?: StorageType[], requirements?: Partial<StorageCapabilities>): Promise<StorageAdapter | null>;
18
+ /**
19
+ * Get multiple adapters sorted by preference
20
+ */
21
+ getAdapterChain(count: number, preferredTypes?: StorageType[], requirements?: Partial<StorageCapabilities>): Promise<StorageAdapter[]>;
22
+ /**
23
+ * Check if capabilities meet requirements
24
+ */
25
+ private meetsRequirements;
26
+ /**
27
+ * Compare two adapters based on current strategy
28
+ */
29
+ private compareAdapters;
30
+ /**
31
+ * Compare adapters by performance
32
+ */
33
+ private comparePerformance;
34
+ /**
35
+ * Compare adapters by persistence
36
+ */
37
+ private comparePersistence;
38
+ /**
39
+ * Compare adapters by security
40
+ */
41
+ private compareSecurity;
42
+ /**
43
+ * Compare adapters by capacity
44
+ */
45
+ private compareCapacity;
46
+ /**
47
+ * Update strategy
48
+ */
49
+ setStrategy(strategy: StorageStrategy): void;
50
+ /**
51
+ * Get current strategy
52
+ */
53
+ getStrategy(): StorageStrategy;
54
+ }
55
+ //# sourceMappingURL=StorageStrategy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"StorageStrategy.d.ts","sourceRoot":"","sources":["../../src/core/StorageStrategy.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,WAAW,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAChF,OAAO,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAC1C,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEpD;;GAEG;AACH,qBAAa,eAAe;IAExB,OAAO,CAAC,QAAQ;IAChB,OAAO,CAAC,QAAQ;gBADR,QAAQ,EAAE,eAAe,EACzB,QAAQ,GAAE,eAAmD;IAGvE;;OAEG;IACG,cAAc,CAClB,cAAc,CAAC,EAAE,WAAW,EAAE,EAC9B,YAAY,CAAC,EAAE,OAAO,CAAC,mBAAmB,CAAC,GAC1C,OAAO,CAAC,cAAc,GAAG,IAAI,CAAC;IAqBjC;;OAEG;IACG,eAAe,CACnB,KAAK,EAAE,MAAM,EACb,cAAc,CAAC,EAAE,WAAW,EAAE,EAC9B,YAAY,CAAC,EAAE,OAAO,CAAC,mBAAmB,CAAC,GAC1C,OAAO,CAAC,cAAc,EAAE,CAAC;IAkB5B;;OAEG;IACH,OAAO,CAAC,iBAAiB;IAsBzB;;OAEG;IACH,OAAO,CAAC,eAAe;IAmBvB;;OAEG;IACH,OAAO,CAAC,kBAAkB;IA0B1B;;OAEG;IACH,OAAO,CAAC,kBAAkB;IA0B1B;;OAEG;IACH,OAAO,CAAC,eAAe;IA0BvB;;OAEG;IACH,OAAO,CAAC,eAAe;IA+BvB;;OAEG;IACH,WAAW,CAAC,QAAQ,EAAE,eAAe,GAAG,IAAI;IAI5C;;OAEG;IACH,WAAW,IAAI,eAAe;CAG/B"}
@@ -0,0 +1,199 @@
1
+ /**
2
+ * Storage strategy implementation for automatic adapter selection
3
+ */
4
+ import { StorageStrategy } from '@/types';
5
+ /**
6
+ * Strategy manager for selecting appropriate storage adapters
7
+ */
8
+ export class StrategyManager {
9
+ registry;
10
+ strategy;
11
+ constructor(registry, strategy = StorageStrategy.PERFORMANCE_FIRST) {
12
+ this.registry = registry;
13
+ this.strategy = strategy;
14
+ }
15
+ /**
16
+ * Get the best adapter based on current strategy
17
+ */
18
+ async getBestAdapter(preferredTypes, requirements) {
19
+ const available = await this.registry.getAvailable();
20
+ // Filter by preferred types if specified
21
+ let candidates = preferredTypes
22
+ ? available.filter((adapter) => preferredTypes.includes(adapter.name))
23
+ : available;
24
+ // Filter by requirements
25
+ if (requirements) {
26
+ candidates = candidates.filter((adapter) => this.meetsRequirements(adapter.capabilities, requirements));
27
+ }
28
+ // Sort by strategy
29
+ candidates.sort((a, b) => this.compareAdapters(a, b));
30
+ return candidates[0] || null;
31
+ }
32
+ /**
33
+ * Get multiple adapters sorted by preference
34
+ */
35
+ async getAdapterChain(count, preferredTypes, requirements) {
36
+ const available = await this.registry.getAvailable();
37
+ let candidates = preferredTypes
38
+ ? available.filter((adapter) => preferredTypes.includes(adapter.name))
39
+ : available;
40
+ if (requirements) {
41
+ candidates = candidates.filter((adapter) => this.meetsRequirements(adapter.capabilities, requirements));
42
+ }
43
+ candidates.sort((a, b) => this.compareAdapters(a, b));
44
+ return candidates.slice(0, count);
45
+ }
46
+ /**
47
+ * Check if capabilities meet requirements
48
+ */
49
+ meetsRequirements(capabilities, requirements) {
50
+ for (const [key, value] of Object.entries(requirements)) {
51
+ const capKey = key;
52
+ if (typeof value === 'boolean' && capabilities[capKey] !== value) {
53
+ return false;
54
+ }
55
+ if (capKey === 'maxSize' && typeof value === 'number') {
56
+ const capSize = capabilities.maxSize;
57
+ if (capSize !== -1 && capSize < value) {
58
+ return false;
59
+ }
60
+ }
61
+ }
62
+ return true;
63
+ }
64
+ /**
65
+ * Compare two adapters based on current strategy
66
+ */
67
+ compareAdapters(a, b) {
68
+ switch (this.strategy) {
69
+ case StorageStrategy.PERFORMANCE_FIRST:
70
+ return this.comparePerformance(a, b);
71
+ case StorageStrategy.PERSISTENCE_FIRST:
72
+ return this.comparePersistence(a, b);
73
+ case StorageStrategy.SECURITY_FIRST:
74
+ return this.compareSecurity(a, b);
75
+ case StorageStrategy.CAPACITY_FIRST:
76
+ return this.compareCapacity(a, b);
77
+ default:
78
+ return 0;
79
+ }
80
+ }
81
+ /**
82
+ * Compare adapters by performance
83
+ */
84
+ comparePerformance(a, b) {
85
+ // Priority order for performance
86
+ const performanceOrder = [
87
+ 'memory',
88
+ 'sessionStorage',
89
+ 'localStorage',
90
+ 'cache',
91
+ 'indexedDB',
92
+ 'preferences',
93
+ 'sqlite',
94
+ 'filesystem',
95
+ 'cookies',
96
+ 'secure',
97
+ ];
98
+ const aIndex = performanceOrder.indexOf(a.name);
99
+ const bIndex = performanceOrder.indexOf(b.name);
100
+ // Prefer synchronous adapters for performance
101
+ if (a.capabilities.synchronous !== b.capabilities.synchronous) {
102
+ return a.capabilities.synchronous ? -1 : 1;
103
+ }
104
+ return aIndex - bIndex;
105
+ }
106
+ /**
107
+ * Compare adapters by persistence
108
+ */
109
+ comparePersistence(a, b) {
110
+ // Prefer persistent storage
111
+ if (a.capabilities.persistent !== b.capabilities.persistent) {
112
+ return a.capabilities.persistent ? -1 : 1;
113
+ }
114
+ // Priority order for persistence
115
+ const persistenceOrder = [
116
+ 'sqlite',
117
+ 'filesystem',
118
+ 'secure',
119
+ 'indexedDB',
120
+ 'preferences',
121
+ 'localStorage',
122
+ 'cache',
123
+ 'cookies',
124
+ 'sessionStorage',
125
+ 'memory',
126
+ ];
127
+ const aIndex = persistenceOrder.indexOf(a.name);
128
+ const bIndex = persistenceOrder.indexOf(b.name);
129
+ return aIndex - bIndex;
130
+ }
131
+ /**
132
+ * Compare adapters by security
133
+ */
134
+ compareSecurity(a, b) {
135
+ // Prefer encrypted storage
136
+ if (a.capabilities.encrypted !== b.capabilities.encrypted) {
137
+ return a.capabilities.encrypted ? -1 : 1;
138
+ }
139
+ // Priority order for security
140
+ const securityOrder = [
141
+ 'secure',
142
+ 'preferences',
143
+ 'sqlite',
144
+ 'indexedDB',
145
+ 'filesystem',
146
+ 'localStorage',
147
+ 'sessionStorage',
148
+ 'cache',
149
+ 'memory',
150
+ 'cookies',
151
+ ];
152
+ const aIndex = securityOrder.indexOf(a.name);
153
+ const bIndex = securityOrder.indexOf(b.name);
154
+ return aIndex - bIndex;
155
+ }
156
+ /**
157
+ * Compare adapters by capacity
158
+ */
159
+ compareCapacity(a, b) {
160
+ // Compare max sizes (-1 means unlimited)
161
+ const aSize = a.capabilities.maxSize;
162
+ const bSize = b.capabilities.maxSize;
163
+ if (aSize === -1 && bSize !== -1)
164
+ return -1;
165
+ if (bSize === -1 && aSize !== -1)
166
+ return 1;
167
+ if (aSize !== -1 && bSize !== -1) {
168
+ return bSize - aSize; // Higher capacity first
169
+ }
170
+ // Priority order for capacity
171
+ const capacityOrder = [
172
+ 'filesystem',
173
+ 'sqlite',
174
+ 'indexedDB',
175
+ 'cache',
176
+ 'preferences',
177
+ 'secure',
178
+ 'localStorage',
179
+ 'memory',
180
+ 'sessionStorage',
181
+ 'cookies',
182
+ ];
183
+ const aIndex = capacityOrder.indexOf(a.name);
184
+ const bIndex = capacityOrder.indexOf(b.name);
185
+ return aIndex - bIndex;
186
+ }
187
+ /**
188
+ * Update strategy
189
+ */
190
+ setStrategy(strategy) {
191
+ this.strategy = strategy;
192
+ }
193
+ /**
194
+ * Get current strategy
195
+ */
196
+ getStrategy() {
197
+ return this.strategy;
198
+ }
199
+ }
@@ -0,0 +1,122 @@
1
+ /**
2
+ * Strata Storage - Main entry point
3
+ * Zero-dependency universal storage solution
4
+ */
5
+ import type { StrataConfig, StorageOptions, StorageType, SizeInfo, ClearOptions, ExportOptions, ImportOptions, SubscriptionCallback, UnsubscribeFunction, QueryCondition, StorageCapabilities } from '@/types';
6
+ /**
7
+ * Main Strata class - unified storage interface
8
+ */
9
+ export declare class Strata {
10
+ private config;
11
+ private registry;
12
+ private defaultAdapter?;
13
+ private adapters;
14
+ private platform;
15
+ private encryptionManager?;
16
+ private compressionManager?;
17
+ private syncManager?;
18
+ private ttlManager?;
19
+ constructor(config?: StrataConfig);
20
+ /**
21
+ * Initialize Strata with available adapters
22
+ */
23
+ initialize(): Promise<void>;
24
+ /**
25
+ * Get a value from storage
26
+ */
27
+ get<T = unknown>(key: string, options?: StorageOptions): Promise<T | null>;
28
+ /**
29
+ * Set a value in storage
30
+ */
31
+ set<T = unknown>(key: string, value: T, options?: StorageOptions): Promise<void>;
32
+ /**
33
+ * Remove a value from storage
34
+ */
35
+ remove(key: string, options?: StorageOptions): Promise<void>;
36
+ /**
37
+ * Check if a key exists
38
+ */
39
+ has(key: string, options?: StorageOptions): Promise<boolean>;
40
+ /**
41
+ * Clear storage
42
+ */
43
+ clear(options?: ClearOptions & StorageOptions): Promise<void>;
44
+ /**
45
+ * Get all keys
46
+ */
47
+ keys(pattern?: string | RegExp, options?: StorageOptions): Promise<string[]>;
48
+ /**
49
+ * Get storage size information
50
+ */
51
+ size(detailed?: boolean): Promise<SizeInfo>;
52
+ /**
53
+ * Subscribe to storage changes
54
+ */
55
+ subscribe(callback: SubscriptionCallback, options?: StorageOptions): UnsubscribeFunction;
56
+ /**
57
+ * Query storage (if supported)
58
+ */
59
+ query<T = unknown>(condition: QueryCondition, options?: StorageOptions): Promise<Array<{
60
+ key: string;
61
+ value: T;
62
+ }>>;
63
+ /**
64
+ * Export storage data
65
+ */
66
+ export(options?: ExportOptions): Promise<string>;
67
+ /**
68
+ * Import storage data
69
+ */
70
+ import(data: string, options?: ImportOptions): Promise<void>;
71
+ /**
72
+ * Get available storage types
73
+ */
74
+ getAvailableStorageTypes(): StorageType[];
75
+ /**
76
+ * Get adapter capabilities
77
+ */
78
+ getCapabilities(storage?: StorageType): StorageCapabilities | Record<string, StorageCapabilities>;
79
+ /**
80
+ * Generate a secure password for encryption
81
+ */
82
+ generatePassword(length?: number): string;
83
+ /**
84
+ * Hash data using SHA-256
85
+ */
86
+ hash(data: string): Promise<string>;
87
+ /**
88
+ * Get TTL (time to live) for a key
89
+ */
90
+ getTTL(key: string, options?: StorageOptions): Promise<number | null>;
91
+ /**
92
+ * Extend TTL for a key
93
+ */
94
+ extendTTL(key: string, extension: number, options?: StorageOptions): Promise<void>;
95
+ /**
96
+ * Make a key persistent (remove TTL)
97
+ */
98
+ persist(key: string, options?: StorageOptions): Promise<void>;
99
+ /**
100
+ * Get items expiring within a time window
101
+ */
102
+ getExpiring(timeWindow: number, options?: StorageOptions): Promise<Array<{
103
+ key: string;
104
+ expiresIn: number;
105
+ }>>;
106
+ /**
107
+ * Manually trigger TTL cleanup
108
+ */
109
+ cleanupExpired(options?: StorageOptions): Promise<number>;
110
+ /**
111
+ * Close all adapters
112
+ */
113
+ close(): Promise<void>;
114
+ private normalizeConfig;
115
+ private detectPlatform;
116
+ private getDefaultStorages;
117
+ private registerAdapters;
118
+ private selectDefaultAdapter;
119
+ private initializeAdapters;
120
+ private selectAdapter;
121
+ }
122
+ //# sourceMappingURL=Strata.d.ts.map
@@ -0,0 +1 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