strata-storage 1.6.0 → 2.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.
- package/Readme.md +66 -20
- package/dist/README.md +66 -20
- package/dist/adapters/web/MemoryAdapter.js +1 -1
- package/dist/capacitor.d.ts +20 -0
- package/dist/capacitor.d.ts.map +1 -0
- package/dist/capacitor.js +48 -0
- package/dist/core/Strata.d.ts +28 -3
- package/dist/core/Strata.d.ts.map +1 -1
- package/dist/core/Strata.js +51 -52
- package/dist/features/compression/index.d.ts +2 -0
- package/dist/features/compression/index.d.ts.map +1 -0
- package/dist/features/compression/index.js +1 -0
- package/dist/features/encryption/index.d.ts +2 -0
- package/dist/features/encryption/index.d.ts.map +1 -0
- package/dist/features/encryption/index.js +1 -0
- package/dist/features/observer/index.d.ts +2 -0
- package/dist/features/observer/index.d.ts.map +1 -0
- package/dist/features/observer/index.js +1 -0
- package/dist/features/observer.d.ts +20 -0
- package/dist/features/observer.d.ts.map +1 -0
- package/dist/features/observer.js +32 -0
- package/dist/features/query/index.d.ts +2 -0
- package/dist/features/query/index.d.ts.map +1 -0
- package/dist/features/query/index.js +1 -0
- package/dist/features/query.d.ts.map +1 -1
- package/dist/features/query.js +8 -1
- package/dist/features/sync/index.d.ts +2 -0
- package/dist/features/sync/index.d.ts.map +1 -0
- package/dist/features/sync/index.js +1 -0
- package/dist/features/ttl/index.d.ts +2 -0
- package/dist/features/ttl/index.d.ts.map +1 -0
- package/dist/features/ttl/index.js +1 -0
- package/dist/firebase.d.ts +26 -0
- package/dist/firebase.d.ts.map +1 -0
- package/dist/firebase.js +147 -0
- package/dist/index.d.ts +13 -47
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +46 -50
- package/dist/package.json +9 -1
- package/dist/plugin/definitions.d.ts +2 -2
- package/dist/plugin/definitions.d.ts.map +1 -1
- package/dist/plugin/index.d.ts +2 -1
- package/dist/plugin/index.d.ts.map +1 -1
- package/dist/plugin/index.js +76 -10
- package/dist/types/index.d.ts +190 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +13 -0
- package/dist/utils/index.d.ts +20 -0
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +34 -0
- package/package.json +10 -1
- package/scripts/build.js +8 -0
- package/scripts/postinstall.js +15 -8
package/Readme.md
CHANGED
|
@@ -23,21 +23,56 @@ Zero-dependency universal storage plugin providing a unified API for all storage
|
|
|
23
23
|
npm install strata-storage
|
|
24
24
|
```
|
|
25
25
|
|
|
26
|
+
### Provider-less Usage (Zero Setup)
|
|
26
27
|
```typescript
|
|
27
|
-
import {
|
|
28
|
+
import { storage } from 'strata-storage';
|
|
28
29
|
|
|
29
|
-
|
|
30
|
-
await storage.initialize();
|
|
31
|
-
|
|
32
|
-
// Works everywhere - web, iOS, Android
|
|
30
|
+
// Works immediately - no setup, no providers, no initialization!
|
|
33
31
|
await storage.set('user', { name: 'John', age: 30 });
|
|
34
32
|
const user = await storage.get('user');
|
|
35
33
|
```
|
|
36
34
|
|
|
35
|
+
### With Capacitor (Optional)
|
|
36
|
+
```typescript
|
|
37
|
+
import { storage } from 'strata-storage';
|
|
38
|
+
import { registerCapacitorAdapters } from 'strata-storage/capacitor';
|
|
39
|
+
|
|
40
|
+
// Only if you need native features
|
|
41
|
+
if (window.Capacitor) {
|
|
42
|
+
await registerCapacitorAdapters(storage);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Use native storage when available
|
|
46
|
+
await storage.set('secure-data', 'secret', { storage: 'secure' });
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### With Firebase (Optional)
|
|
50
|
+
```typescript
|
|
51
|
+
import { storage } from 'strata-storage';
|
|
52
|
+
import { enableFirebaseSync } from 'strata-storage/firebase';
|
|
53
|
+
|
|
54
|
+
// Only if you need cloud sync
|
|
55
|
+
if (needCloudSync) {
|
|
56
|
+
await enableFirebaseSync(storage, {
|
|
57
|
+
apiKey: 'your-api-key',
|
|
58
|
+
projectId: 'your-project-id',
|
|
59
|
+
firestore: true
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Works offline-first, syncs when online
|
|
64
|
+
await storage.set('data', value, { storage: 'firestore' });
|
|
65
|
+
```
|
|
66
|
+
|
|
37
67
|
## ✨ Features
|
|
38
68
|
|
|
39
69
|
### Core Features
|
|
40
|
-
- ✅ **Zero Dependencies** - No
|
|
70
|
+
- ✅ **Zero Dependencies** - No runtime dependencies, pure implementation
|
|
71
|
+
- ✅ **Provider-less Architecture** - No providers, contexts, or wrappers needed (like zustand)
|
|
72
|
+
- ✅ **Works Everywhere** - React, Vue, Angular, Vanilla JS, Node.js - same API
|
|
73
|
+
- ✅ **Zero Configuration** - Import and use immediately, no setup required
|
|
74
|
+
- ✅ **Opt-in Complexity** - Start simple, add features only when needed
|
|
75
|
+
- ✅ **Dynamic Provider Loading** - Providers load only when used, keeping bundle small
|
|
41
76
|
- ✅ **Universal API** - Single interface for all storage types
|
|
42
77
|
- ✅ **Cross-Platform** - Web, iOS, Android support
|
|
43
78
|
- ✅ **TypeScript** - Full type safety and IntelliSense
|
|
@@ -67,22 +102,23 @@ const user = await storage.get('user');
|
|
|
67
102
|
## 📖 Basic Usage
|
|
68
103
|
|
|
69
104
|
```typescript
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
encryption: { enabled: true },
|
|
74
|
-
compression: { enabled: true },
|
|
75
|
-
ttl: { defaultTTL: 3600000 } // 1 hour
|
|
76
|
-
});
|
|
105
|
+
import { storage } from 'strata-storage';
|
|
106
|
+
|
|
107
|
+
// No initialization needed - works immediately!
|
|
77
108
|
|
|
78
|
-
|
|
109
|
+
// Simple usage
|
|
110
|
+
await storage.set('key', 'value');
|
|
111
|
+
const value = await storage.get('key');
|
|
112
|
+
await storage.remove('key');
|
|
113
|
+
await storage.clear();
|
|
79
114
|
|
|
80
|
-
//
|
|
115
|
+
// Advanced options
|
|
81
116
|
await storage.set('key', value, {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
117
|
+
storage: 'indexedDB', // Choose specific storage
|
|
118
|
+
ttl: 3600000, // Expire in 1 hour
|
|
119
|
+
encrypt: true, // Encrypt this value
|
|
120
|
+
compress: true, // Compress if beneficial
|
|
121
|
+
tags: ['user-data'] // Tag for grouping
|
|
86
122
|
});
|
|
87
123
|
|
|
88
124
|
// Query data
|
|
@@ -97,13 +133,23 @@ storage.subscribe((change) => {
|
|
|
97
133
|
});
|
|
98
134
|
```
|
|
99
135
|
|
|
136
|
+
## 🎯 Provider-less Architecture
|
|
137
|
+
|
|
138
|
+
Strata Storage follows a provider-less architecture similar to Zustand. The core library works everywhere with zero dependencies, and platform-specific features (like Capacitor) are completely optional.
|
|
139
|
+
|
|
140
|
+
- **Minimal by default** - Only includes web storage adapters
|
|
141
|
+
- **Opt-in native features** - Explicitly add Capacitor support when needed
|
|
142
|
+
- **Better tree-shaking** - Unused adapters are eliminated by bundlers
|
|
143
|
+
- **Smaller bundle size** - Web-only projects don't include native code
|
|
144
|
+
|
|
100
145
|
## 🏗 Project Status
|
|
101
146
|
|
|
102
147
|
Currently in active development. Phase 1-5 completed:
|
|
103
148
|
- ✅ Project setup and core architecture
|
|
104
149
|
- ✅ Memory and web storage adapters
|
|
105
|
-
- ✅ Capacitor plugin structure
|
|
150
|
+
- ✅ Capacitor plugin structure (now optional)
|
|
106
151
|
- ✅ Advanced features (encryption, compression, sync, query, TTL)
|
|
152
|
+
- ✅ Provider-less architecture
|
|
107
153
|
- 🚧 Native implementations (iOS/Android)
|
|
108
154
|
- 🚧 Testing and documentation
|
|
109
155
|
|
package/dist/README.md
CHANGED
|
@@ -23,21 +23,56 @@ Zero-dependency universal storage plugin providing a unified API for all storage
|
|
|
23
23
|
npm install strata-storage
|
|
24
24
|
```
|
|
25
25
|
|
|
26
|
+
### Provider-less Usage (Zero Setup)
|
|
26
27
|
```typescript
|
|
27
|
-
import {
|
|
28
|
+
import { storage } from 'strata-storage';
|
|
28
29
|
|
|
29
|
-
|
|
30
|
-
await storage.initialize();
|
|
31
|
-
|
|
32
|
-
// Works everywhere - web, iOS, Android
|
|
30
|
+
// Works immediately - no setup, no providers, no initialization!
|
|
33
31
|
await storage.set('user', { name: 'John', age: 30 });
|
|
34
32
|
const user = await storage.get('user');
|
|
35
33
|
```
|
|
36
34
|
|
|
35
|
+
### With Capacitor (Optional)
|
|
36
|
+
```typescript
|
|
37
|
+
import { storage } from 'strata-storage';
|
|
38
|
+
import { registerCapacitorAdapters } from 'strata-storage/capacitor';
|
|
39
|
+
|
|
40
|
+
// Only if you need native features
|
|
41
|
+
if (window.Capacitor) {
|
|
42
|
+
await registerCapacitorAdapters(storage);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Use native storage when available
|
|
46
|
+
await storage.set('secure-data', 'secret', { storage: 'secure' });
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### With Firebase (Optional)
|
|
50
|
+
```typescript
|
|
51
|
+
import { storage } from 'strata-storage';
|
|
52
|
+
import { enableFirebaseSync } from 'strata-storage/firebase';
|
|
53
|
+
|
|
54
|
+
// Only if you need cloud sync
|
|
55
|
+
if (needCloudSync) {
|
|
56
|
+
await enableFirebaseSync(storage, {
|
|
57
|
+
apiKey: 'your-api-key',
|
|
58
|
+
projectId: 'your-project-id',
|
|
59
|
+
firestore: true
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Works offline-first, syncs when online
|
|
64
|
+
await storage.set('data', value, { storage: 'firestore' });
|
|
65
|
+
```
|
|
66
|
+
|
|
37
67
|
## ✨ Features
|
|
38
68
|
|
|
39
69
|
### Core Features
|
|
40
|
-
- ✅ **Zero Dependencies** - No
|
|
70
|
+
- ✅ **Zero Dependencies** - No runtime dependencies, pure implementation
|
|
71
|
+
- ✅ **Provider-less Architecture** - No providers, contexts, or wrappers needed (like zustand)
|
|
72
|
+
- ✅ **Works Everywhere** - React, Vue, Angular, Vanilla JS, Node.js - same API
|
|
73
|
+
- ✅ **Zero Configuration** - Import and use immediately, no setup required
|
|
74
|
+
- ✅ **Opt-in Complexity** - Start simple, add features only when needed
|
|
75
|
+
- ✅ **Dynamic Provider Loading** - Providers load only when used, keeping bundle small
|
|
41
76
|
- ✅ **Universal API** - Single interface for all storage types
|
|
42
77
|
- ✅ **Cross-Platform** - Web, iOS, Android support
|
|
43
78
|
- ✅ **TypeScript** - Full type safety and IntelliSense
|
|
@@ -67,22 +102,23 @@ const user = await storage.get('user');
|
|
|
67
102
|
## 📖 Basic Usage
|
|
68
103
|
|
|
69
104
|
```typescript
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
encryption: { enabled: true },
|
|
74
|
-
compression: { enabled: true },
|
|
75
|
-
ttl: { defaultTTL: 3600000 } // 1 hour
|
|
76
|
-
});
|
|
105
|
+
import { storage } from 'strata-storage';
|
|
106
|
+
|
|
107
|
+
// No initialization needed - works immediately!
|
|
77
108
|
|
|
78
|
-
|
|
109
|
+
// Simple usage
|
|
110
|
+
await storage.set('key', 'value');
|
|
111
|
+
const value = await storage.get('key');
|
|
112
|
+
await storage.remove('key');
|
|
113
|
+
await storage.clear();
|
|
79
114
|
|
|
80
|
-
//
|
|
115
|
+
// Advanced options
|
|
81
116
|
await storage.set('key', value, {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
117
|
+
storage: 'indexedDB', // Choose specific storage
|
|
118
|
+
ttl: 3600000, // Expire in 1 hour
|
|
119
|
+
encrypt: true, // Encrypt this value
|
|
120
|
+
compress: true, // Compress if beneficial
|
|
121
|
+
tags: ['user-data'] // Tag for grouping
|
|
86
122
|
});
|
|
87
123
|
|
|
88
124
|
// Query data
|
|
@@ -97,13 +133,23 @@ storage.subscribe((change) => {
|
|
|
97
133
|
});
|
|
98
134
|
```
|
|
99
135
|
|
|
136
|
+
## 🎯 Provider-less Architecture
|
|
137
|
+
|
|
138
|
+
Strata Storage follows a provider-less architecture similar to Zustand. The core library works everywhere with zero dependencies, and platform-specific features (like Capacitor) are completely optional.
|
|
139
|
+
|
|
140
|
+
- **Minimal by default** - Only includes web storage adapters
|
|
141
|
+
- **Opt-in native features** - Explicitly add Capacitor support when needed
|
|
142
|
+
- **Better tree-shaking** - Unused adapters are eliminated by bundlers
|
|
143
|
+
- **Smaller bundle size** - Web-only projects don't include native code
|
|
144
|
+
|
|
100
145
|
## 🏗 Project Status
|
|
101
146
|
|
|
102
147
|
Currently in active development. Phase 1-5 completed:
|
|
103
148
|
- ✅ Project setup and core architecture
|
|
104
149
|
- ✅ Memory and web storage adapters
|
|
105
|
-
- ✅ Capacitor plugin structure
|
|
150
|
+
- ✅ Capacitor plugin structure (now optional)
|
|
106
151
|
- ✅ Advanced features (encryption, compression, sync, query, TTL)
|
|
152
|
+
- ✅ Provider-less architecture
|
|
107
153
|
- 🚧 Native implementations (iOS/Android)
|
|
108
154
|
- 🚧 Testing and documentation
|
|
109
155
|
|
|
@@ -134,7 +134,7 @@ export class MemoryAdapter extends BaseAdapter {
|
|
|
134
134
|
async query(condition) {
|
|
135
135
|
const results = [];
|
|
136
136
|
for (const [key, item] of this.storage.entries()) {
|
|
137
|
-
if (!this.isExpired(item) && this.queryEngine.matches(item
|
|
137
|
+
if (!this.isExpired(item) && this.queryEngine.matches(item, condition)) {
|
|
138
138
|
results.push({
|
|
139
139
|
key,
|
|
140
140
|
value: deepClone(item.value),
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Strata } from './core/Strata';
|
|
2
|
+
export { PreferencesAdapter } from './adapters/capacitor/PreferencesAdapter';
|
|
3
|
+
export { SqliteAdapter } from './adapters/capacitor/SqliteAdapter';
|
|
4
|
+
export { SecureAdapter } from './adapters/capacitor/SecureAdapter';
|
|
5
|
+
export { FilesystemAdapter } from './adapters/capacitor/FilesystemAdapter';
|
|
6
|
+
export { StrataStorage } from './plugin';
|
|
7
|
+
/**
|
|
8
|
+
* Register Capacitor adapters with a Strata instance
|
|
9
|
+
* This is completely optional and only needed for Capacitor apps
|
|
10
|
+
*/
|
|
11
|
+
export declare function registerCapacitorAdapters(storage: Strata): Promise<void>;
|
|
12
|
+
/**
|
|
13
|
+
* Helper to check if running in Capacitor environment
|
|
14
|
+
*/
|
|
15
|
+
export declare function isCapacitorEnvironment(): boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Get Capacitor-specific storage types
|
|
18
|
+
*/
|
|
19
|
+
export declare function getCapacitorStorageTypes(): string[];
|
|
20
|
+
//# sourceMappingURL=capacitor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"capacitor.d.ts","sourceRoot":"","sources":["../src/capacitor.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAO5C,OAAO,EAAE,kBAAkB,EAAE,MAAM,yCAAyC,CAAC;AAC7E,OAAO,EAAE,aAAa,EAAE,MAAM,oCAAoC,CAAC;AACnE,OAAO,EAAE,aAAa,EAAE,MAAM,oCAAoC,CAAC;AACnE,OAAO,EAAE,iBAAiB,EAAE,MAAM,wCAAwC,CAAC;AAG3E,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAEzC;;;GAGG;AACH,wBAAsB,yBAAyB,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAsB9E;AAED;;GAEG;AACH,wBAAgB,sBAAsB,IAAI,OAAO,CAMhD;AAED;;GAEG;AACH,wBAAgB,wBAAwB,IAAI,MAAM,EAAE,CAEnD"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { PreferencesAdapter } from "./adapters/capacitor/PreferencesAdapter.js";
|
|
2
|
+
import { SqliteAdapter } from "./adapters/capacitor/SqliteAdapter.js";
|
|
3
|
+
import { SecureAdapter } from "./adapters/capacitor/SecureAdapter.js";
|
|
4
|
+
import { FilesystemAdapter } from "./adapters/capacitor/FilesystemAdapter.js";
|
|
5
|
+
// Export Capacitor adapters
|
|
6
|
+
export { PreferencesAdapter } from "./adapters/capacitor/PreferencesAdapter.js";
|
|
7
|
+
export { SqliteAdapter } from "./adapters/capacitor/SqliteAdapter.js";
|
|
8
|
+
export { SecureAdapter } from "./adapters/capacitor/SecureAdapter.js";
|
|
9
|
+
export { FilesystemAdapter } from "./adapters/capacitor/FilesystemAdapter.js";
|
|
10
|
+
// Export the plugin for direct access if needed
|
|
11
|
+
export { StrataStorage } from "./plugin/index.js";
|
|
12
|
+
/**
|
|
13
|
+
* Register Capacitor adapters with a Strata instance
|
|
14
|
+
* This is completely optional and only needed for Capacitor apps
|
|
15
|
+
*/
|
|
16
|
+
export async function registerCapacitorAdapters(storage) {
|
|
17
|
+
// Check if Capacitor is available
|
|
18
|
+
const hasCapacitor = typeof window !== 'undefined' &&
|
|
19
|
+
window.Capacitor &&
|
|
20
|
+
window.Capacitor.isNativePlatform();
|
|
21
|
+
if (!hasCapacitor) {
|
|
22
|
+
console.warn('Capacitor not detected. Capacitor adapters will not be registered.');
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
// Register Capacitor adapters
|
|
26
|
+
storage.registerAdapter(new PreferencesAdapter());
|
|
27
|
+
storage.registerAdapter(new SqliteAdapter());
|
|
28
|
+
storage.registerAdapter(new SecureAdapter());
|
|
29
|
+
storage.registerAdapter(new FilesystemAdapter());
|
|
30
|
+
// Re-initialize to make new adapters available
|
|
31
|
+
if (storage.isInitialized) {
|
|
32
|
+
await storage.initialize();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Helper to check if running in Capacitor environment
|
|
37
|
+
*/
|
|
38
|
+
export function isCapacitorEnvironment() {
|
|
39
|
+
return (typeof window !== 'undefined' &&
|
|
40
|
+
window.Capacitor &&
|
|
41
|
+
window.Capacitor.isNativePlatform());
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Get Capacitor-specific storage types
|
|
45
|
+
*/
|
|
46
|
+
export function getCapacitorStorageTypes() {
|
|
47
|
+
return ['preferences', 'sqlite', 'secure', 'filesystem'];
|
|
48
|
+
}
|
package/dist/core/Strata.d.ts
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
* Strata Storage - Main entry point
|
|
3
3
|
* Zero-dependency universal storage solution
|
|
4
4
|
*/
|
|
5
|
-
import type { StrataConfig, StorageOptions, StorageType, SizeInfo, ClearOptions, ExportOptions, ImportOptions, SubscriptionCallback, UnsubscribeFunction, QueryCondition, StorageCapabilities } from '@/types';
|
|
5
|
+
import type { StrataConfig, StorageAdapter, StorageOptions, StorageType, Platform, SizeInfo, ClearOptions, ExportOptions, ImportOptions, SubscriptionCallback, UnsubscribeFunction, QueryCondition, StorageCapabilities } from '@/types';
|
|
6
|
+
import { AdapterRegistry } from './AdapterRegistry';
|
|
6
7
|
/**
|
|
7
8
|
* Main Strata class - unified storage interface
|
|
8
9
|
*/
|
|
@@ -11,12 +12,21 @@ export declare class Strata {
|
|
|
11
12
|
private registry;
|
|
12
13
|
private defaultAdapter?;
|
|
13
14
|
private adapters;
|
|
14
|
-
private
|
|
15
|
+
private readonly _platform;
|
|
15
16
|
private encryptionManager?;
|
|
16
17
|
private compressionManager?;
|
|
17
18
|
private syncManager?;
|
|
18
19
|
private ttlManager?;
|
|
20
|
+
private _initialized;
|
|
19
21
|
constructor(config?: StrataConfig);
|
|
22
|
+
/**
|
|
23
|
+
* Check if Strata has been initialized
|
|
24
|
+
*/
|
|
25
|
+
get isInitialized(): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Get the detected platform
|
|
28
|
+
*/
|
|
29
|
+
get platform(): Platform;
|
|
20
30
|
/**
|
|
21
31
|
* Initialize Strata with available adapters
|
|
22
32
|
*/
|
|
@@ -107,6 +117,22 @@ export declare class Strata {
|
|
|
107
117
|
* Manually trigger TTL cleanup
|
|
108
118
|
*/
|
|
109
119
|
cleanupExpired(options?: StorageOptions): Promise<number>;
|
|
120
|
+
/**
|
|
121
|
+
* Register a custom storage adapter
|
|
122
|
+
* This allows external adapters to be registered after initialization
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* ```typescript
|
|
126
|
+
* import { MyCustomAdapter } from './my-adapter';
|
|
127
|
+
* storage.registerAdapter(new MyCustomAdapter());
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
registerAdapter(adapter: StorageAdapter): void;
|
|
131
|
+
/**
|
|
132
|
+
* Get the adapter registry (for advanced use cases)
|
|
133
|
+
* @internal
|
|
134
|
+
*/
|
|
135
|
+
getRegistry(): AdapterRegistry;
|
|
110
136
|
/**
|
|
111
137
|
* Close all adapters
|
|
112
138
|
*/
|
|
@@ -114,7 +140,6 @@ export declare class Strata {
|
|
|
114
140
|
private normalizeConfig;
|
|
115
141
|
private detectPlatform;
|
|
116
142
|
private getDefaultStorages;
|
|
117
|
-
private registerAdapters;
|
|
118
143
|
private selectDefaultAdapter;
|
|
119
144
|
private initializeAdapters;
|
|
120
145
|
private selectAdapter;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Strata.d.ts","sourceRoot":"","sources":["../../src/core/Strata.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EACV,YAAY,
|
|
1
|
+
{"version":3,"file":"Strata.d.ts","sourceRoot":"","sources":["../../src/core/Strata.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EACV,YAAY,EACZ,cAAc,EACd,cAAc,EACd,WAAW,EACX,QAAQ,EACR,QAAQ,EACR,YAAY,EACZ,aAAa,EACb,aAAa,EACb,oBAAoB,EACpB,mBAAmB,EACnB,cAAc,EACd,mBAAmB,EACpB,MAAM,SAAS,CAAC;AAEjB,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAQpD;;GAEG;AACH,qBAAa,MAAM;IACjB,OAAO,CAAC,MAAM,CAAe;IAC7B,OAAO,CAAC,QAAQ,CAAkB;IAClC,OAAO,CAAC,cAAc,CA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AAC,IAAI,CAAC;IAgBnE;;OAEG;IACG,WAAW,CACf,UAAU,EAAE,MAAM,EAClB,OAAO,CAAC,EAAE,cAAc,GACvB,OAAO,CAAC,KAAK,CAAC;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IAWrD;;OAEG;IACG,cAAc,CAAC,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,MAAM,CAAC;IAa/D;;;;;;;;;OASG;IACH,eAAe,CAAC,OAAO,EAAE,cAAc,GAAG,IAAI;IAI9C;;;OAGG;IACH,WAAW,IAAI,eAAe;IAI9B;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IA0B5B,OAAO,CAAC,eAAe;IAQvB,OAAO,CAAC,cAAc;IAMtB,OAAO,CAAC,kBAAkB;YAgBZ,oBAAoB;YAoBpB,kBAAkB;YAWlB,aAAa;CA2B5B"}
|
package/dist/core/Strata.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* Zero-dependency universal storage solution
|
|
4
4
|
*/
|
|
5
5
|
import { AdapterRegistry } from "./AdapterRegistry.js";
|
|
6
|
-
import { isBrowser, isNode
|
|
6
|
+
import { isBrowser, isNode } from "../utils/index.js";
|
|
7
7
|
import { StorageError, EncryptionError } from "../utils/errors.js";
|
|
8
8
|
import { EncryptionManager } from "../features/encryption.js";
|
|
9
9
|
import { CompressionManager } from "../features/compression.js";
|
|
@@ -17,22 +17,35 @@ export class Strata {
|
|
|
17
17
|
registry;
|
|
18
18
|
defaultAdapter;
|
|
19
19
|
adapters = new Map();
|
|
20
|
-
|
|
20
|
+
_platform;
|
|
21
21
|
encryptionManager;
|
|
22
22
|
compressionManager;
|
|
23
23
|
syncManager;
|
|
24
24
|
ttlManager;
|
|
25
|
+
_initialized = false;
|
|
25
26
|
constructor(config = {}) {
|
|
26
27
|
this.config = this.normalizeConfig(config);
|
|
27
|
-
this.
|
|
28
|
+
this._platform = this.detectPlatform();
|
|
28
29
|
this.registry = new AdapterRegistry();
|
|
29
30
|
}
|
|
31
|
+
/**
|
|
32
|
+
* Check if Strata has been initialized
|
|
33
|
+
*/
|
|
34
|
+
get isInitialized() {
|
|
35
|
+
return this._initialized;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Get the detected platform
|
|
39
|
+
*/
|
|
40
|
+
get platform() {
|
|
41
|
+
return this._platform;
|
|
42
|
+
}
|
|
30
43
|
/**
|
|
31
44
|
* Initialize Strata with available adapters
|
|
32
45
|
*/
|
|
33
46
|
async initialize() {
|
|
34
|
-
//
|
|
35
|
-
|
|
47
|
+
// No automatic adapter registration - adapters should be registered before initialize()
|
|
48
|
+
// This allows for zero-dependency operation and explicit opt-in for features
|
|
36
49
|
// Find and set default adapter
|
|
37
50
|
await this.selectDefaultAdapter();
|
|
38
51
|
// Initialize configured adapters
|
|
@@ -64,6 +77,8 @@ export class Strata {
|
|
|
64
77
|
if (this.defaultAdapter && this.config.ttl?.autoCleanup !== false) {
|
|
65
78
|
this.ttlManager.startAutoCleanup(() => this.defaultAdapter.keys(), (key) => this.defaultAdapter.get(key), (key) => this.defaultAdapter.remove(key));
|
|
66
79
|
}
|
|
80
|
+
// Mark as initialized
|
|
81
|
+
this._initialized = true;
|
|
67
82
|
}
|
|
68
83
|
/**
|
|
69
84
|
* Get a value from storage
|
|
@@ -429,6 +444,26 @@ export class Strata {
|
|
|
429
444
|
const expired = await this.ttlManager.cleanup(() => adapter.keys(), (key) => adapter.get(key), (key) => adapter.remove(key));
|
|
430
445
|
return expired.length;
|
|
431
446
|
}
|
|
447
|
+
/**
|
|
448
|
+
* Register a custom storage adapter
|
|
449
|
+
* This allows external adapters to be registered after initialization
|
|
450
|
+
*
|
|
451
|
+
* @example
|
|
452
|
+
* ```typescript
|
|
453
|
+
* import { MyCustomAdapter } from "./my-adapter.js";
|
|
454
|
+
* storage.registerAdapter(new MyCustomAdapter());
|
|
455
|
+
* ```
|
|
456
|
+
*/
|
|
457
|
+
registerAdapter(adapter) {
|
|
458
|
+
this.registry.register(adapter);
|
|
459
|
+
}
|
|
460
|
+
/**
|
|
461
|
+
* Get the adapter registry (for advanced use cases)
|
|
462
|
+
* @internal
|
|
463
|
+
*/
|
|
464
|
+
getRegistry() {
|
|
465
|
+
return this.registry;
|
|
466
|
+
}
|
|
432
467
|
/**
|
|
433
468
|
* Close all adapters
|
|
434
469
|
*/
|
|
@@ -456,13 +491,11 @@ export class Strata {
|
|
|
456
491
|
normalizeConfig(config) {
|
|
457
492
|
return {
|
|
458
493
|
platform: config.platform || this.detectPlatform(),
|
|
459
|
-
defaultStorages: config.defaultStorages ||
|
|
494
|
+
defaultStorages: config.defaultStorages || ['memory'], // Default to memory adapter
|
|
460
495
|
...config,
|
|
461
496
|
};
|
|
462
497
|
}
|
|
463
498
|
detectPlatform() {
|
|
464
|
-
if (isCapacitor())
|
|
465
|
-
return 'web'; // Capacitor runs in web context
|
|
466
499
|
if (isBrowser())
|
|
467
500
|
return 'web';
|
|
468
501
|
if (isNode())
|
|
@@ -470,50 +503,16 @@ export class Strata {
|
|
|
470
503
|
return 'web'; // Default to web
|
|
471
504
|
}
|
|
472
505
|
getDefaultStorages() {
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
return ['preferences', 'sqlite', 'secure', 'memory'];
|
|
484
|
-
default:
|
|
485
|
-
return ['memory'];
|
|
486
|
-
}
|
|
487
|
-
}
|
|
488
|
-
async registerAdapters() {
|
|
489
|
-
// Register adapters based on platform
|
|
490
|
-
if (this.platform === 'web') {
|
|
491
|
-
// Dynamically import and register web adapters
|
|
492
|
-
const { MemoryAdapter } = await import("../adapters/web/MemoryAdapter.js");
|
|
493
|
-
const { LocalStorageAdapter } = await import("../adapters/web/LocalStorageAdapter.js");
|
|
494
|
-
const { SessionStorageAdapter } = await import("../adapters/web/SessionStorageAdapter.js");
|
|
495
|
-
const { IndexedDBAdapter } = await import("../adapters/web/IndexedDBAdapter.js");
|
|
496
|
-
const { CookieAdapter } = await import("../adapters/web/CookieAdapter.js");
|
|
497
|
-
const { CacheAdapter } = await import("../adapters/web/CacheAdapter.js");
|
|
498
|
-
this.registry.register(new MemoryAdapter());
|
|
499
|
-
this.registry.register(new LocalStorageAdapter());
|
|
500
|
-
this.registry.register(new SessionStorageAdapter());
|
|
501
|
-
this.registry.register(new IndexedDBAdapter());
|
|
502
|
-
this.registry.register(new CookieAdapter());
|
|
503
|
-
this.registry.register(new CacheAdapter());
|
|
504
|
-
// If running in Capacitor, also register native adapters
|
|
505
|
-
if (isCapacitor()) {
|
|
506
|
-
const { PreferencesAdapter } = await import("../adapters/capacitor/PreferencesAdapter.js");
|
|
507
|
-
const { SqliteAdapter } = await import("../adapters/capacitor/SqliteAdapter.js");
|
|
508
|
-
const { SecureAdapter } = await import("../adapters/capacitor/SecureAdapter.js");
|
|
509
|
-
const { FilesystemAdapter } = await import("../adapters/capacitor/FilesystemAdapter.js");
|
|
510
|
-
this.registry.register(new PreferencesAdapter());
|
|
511
|
-
this.registry.register(new SqliteAdapter());
|
|
512
|
-
this.registry.register(new SecureAdapter());
|
|
513
|
-
this.registry.register(new FilesystemAdapter());
|
|
514
|
-
}
|
|
515
|
-
}
|
|
516
|
-
// Additional adapters will be registered as they are implemented
|
|
506
|
+
// Only return adapters that are actually registered
|
|
507
|
+
const registered = Array.from(this.registry.getAll().keys()).map((key) => String(key));
|
|
508
|
+
// Prefer these storages in order if available
|
|
509
|
+
const preferredOrder = ['indexedDB', 'localStorage', 'sessionStorage', 'memory'];
|
|
510
|
+
const available = preferredOrder.filter((storage) => registered.includes(storage));
|
|
511
|
+
// Always include memory as fallback if registered
|
|
512
|
+
if (available.length === 0 && registered.includes('memory')) {
|
|
513
|
+
return ['memory'];
|
|
514
|
+
}
|
|
515
|
+
return (available.length > 0 ? available : registered);
|
|
517
516
|
}
|
|
518
517
|
async selectDefaultAdapter() {
|
|
519
518
|
const storages = this.config.defaultStorages || this.getDefaultStorages();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/features/compression/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "../compression.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/features/encryption/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "../encryption.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/features/observer/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "../observer.js";
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Storage Observer - Watch for storage changes
|
|
3
|
+
*/
|
|
4
|
+
import type { ObserverCallback, StorageEvent } from '@/types';
|
|
5
|
+
export declare class StorageObserver {
|
|
6
|
+
private observers;
|
|
7
|
+
/**
|
|
8
|
+
* Subscribe to storage events
|
|
9
|
+
*/
|
|
10
|
+
subscribe(callback: ObserverCallback): () => void;
|
|
11
|
+
/**
|
|
12
|
+
* Emit a storage event
|
|
13
|
+
*/
|
|
14
|
+
emit(event: StorageEvent): void;
|
|
15
|
+
/**
|
|
16
|
+
* Clear all observers
|
|
17
|
+
*/
|
|
18
|
+
clear(): void;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=observer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"observer.d.ts","sourceRoot":"","sources":["../../src/features/observer.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAE9D,qBAAa,eAAe;IAC1B,OAAO,CAAC,SAAS,CAAoC;IAErD;;OAEG;IACH,SAAS,CAAC,QAAQ,EAAE,gBAAgB,GAAG,MAAM,IAAI;IAKjD;;OAEG;IACH,IAAI,CAAC,KAAK,EAAE,YAAY,GAAG,IAAI;IAU/B;;OAEG;IACH,KAAK,IAAI,IAAI;CAGd"}
|