strata-storage 2.5.0 → 2.6.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.
@@ -13,11 +13,42 @@ import Capacitor
13
13
  public class StrataStoragePlugin: CAPPlugin {
14
14
  private let userDefaultsStorage = UserDefaultsStorage()
15
15
  private let keychainStorage = KeychainStorage()
16
- private let sqliteStorage = SQLiteStorage()
16
+ private let filesystemStorage = FilesystemStorage()
17
17
 
18
18
  /// Storage types that have a real native backend on iOS.
19
- /// `filesystem` is intentionally absent see isAvailable / resolveUnsupported.
20
- private let supportedStorageTypes: Set<String> = ["preferences", "secure", "sqlite"]
19
+ private let supportedStorageTypes: Set<String> = ["preferences", "secure", "sqlite", "filesystem"]
20
+
21
+ // MARK: - SQLite multi-store helpers
22
+
23
+ /// Default database name (→ file `strata_storage.db`) and table.
24
+ private static let defaultDatabase = "strata_storage"
25
+ private static let defaultTable = "storage"
26
+
27
+ /// Sanitizes a database name into a safe `.db` filename. Strips everything
28
+ /// outside `[A-Za-z0-9_]` (which also blocks path separators / traversal),
29
+ /// then appends `.db`. Empty input falls back to the default. The filename
30
+ /// CANNOT be a bound parameter, so it must be sanitized before use.
31
+ private func sqliteFileName(from database: String?) -> String {
32
+ let raw = database ?? StrataStoragePlugin.defaultDatabase
33
+ let cleaned = String(raw.unicodeScalars.filter { CharacterSet(charactersIn: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_").contains($0) })
34
+ let safe = cleaned.isEmpty ? StrataStoragePlugin.defaultDatabase : cleaned
35
+ return "\(safe).db"
36
+ }
37
+
38
+ /// Sanitizes a table name to a safe SQL identifier matching `^[A-Za-z0-9_]+$`.
39
+ /// Disallowed characters are removed; empty input falls back to the default.
40
+ /// The table name is interpolated into SQL (it cannot be bound), so this is
41
+ /// the single point that guarantees injection-safety for the identifier.
42
+ private func sqliteTableName(from table: String?) -> String {
43
+ let raw = table ?? StrataStoragePlugin.defaultTable
44
+ let cleaned = String(raw.unicodeScalars.filter { CharacterSet(charactersIn: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_").contains($0) })
45
+ return cleaned.isEmpty ? StrataStoragePlugin.defaultTable : cleaned
46
+ }
47
+
48
+ /// Returns the cached SQLite store for the call's `database` option.
49
+ private func sqliteStore(for call: CAPPluginCall) -> SQLiteStorage {
50
+ return SQLiteStorageManager.shared.store(forFile: sqliteFileName(from: call.getString("database")))
51
+ }
21
52
 
22
53
  /**
23
54
  * Check if a specific storage type is available.
@@ -30,16 +61,10 @@ public class StrataStoragePlugin: CAPPlugin {
30
61
  ])
31
62
  }
32
63
 
33
- private func rejectUnsupportedFilesystem(_ call: CAPPluginCall) {
34
- call.reject(
35
- "Filesystem storage is not implemented in the native iOS plugin. " +
36
- "Use the 'preferences', 'secure', or 'sqlite' storage types, or a " +
37
- "web/Capacitor Filesystem-backed adapter."
38
- )
39
- }
40
-
41
64
  /**
42
- * Get value from storage
65
+ * Get value from storage.
66
+ * For sqlite/filesystem the resolved `value` is the full StorageValue
67
+ * wrapper object (or NSNull on a miss).
43
68
  */
44
69
  @objc func get(_ call: CAPPluginCall) {
45
70
  guard let key = call.getString("key") else {
@@ -56,10 +81,9 @@ public class StrataStoragePlugin: CAPPlugin {
56
81
  case "secure":
57
82
  value = try keychainStorage.get(key: key)
58
83
  case "sqlite":
59
- value = sqliteStorage.get(key: key)
84
+ value = sqliteStore(for: call).get(table: sqliteTableName(from: call.getString("table")), key: key)
60
85
  case "filesystem":
61
- rejectUnsupportedFilesystem(call)
62
- return
86
+ value = filesystemStorage.get(key: key)
63
87
  case "preferences":
64
88
  fallthrough
65
89
  default:
@@ -75,7 +99,9 @@ public class StrataStoragePlugin: CAPPlugin {
75
99
  }
76
100
 
77
101
  /**
78
- * Set value in storage
102
+ * Set value in storage.
103
+ * For sqlite/filesystem the `value` option is the full StorageValue
104
+ * wrapper object and is stored verbatim (JSON) for a perfect round-trip.
79
105
  */
80
106
  @objc func set(_ call: CAPPluginCall) {
81
107
  guard let key = call.getString("key") else {
@@ -83,25 +109,44 @@ public class StrataStoragePlugin: CAPPlugin {
83
109
  return
84
110
  }
85
111
 
86
- let value = call.getValue("value") ?? NSNull()
87
112
  let storage = call.getString("storage") ?? "preferences"
88
113
 
89
114
  do {
90
115
  switch storage {
91
116
  case "secure":
117
+ let value = call.getValue("value") ?? NSNull()
92
118
  _ = try keychainStorage.set(key: key, value: value)
93
119
  case "sqlite":
94
- let ok = try sqliteStorage.set(key: key, value: value)
120
+ // getObject returns JSObject ([String: JSValue]); upcast each
121
+ // value to Any (the canonical Capacitor pattern) so the bridged
122
+ // Foundation values are JSONSerialization-compatible.
123
+ guard let jsObject = call.getObject("value") else {
124
+ call.reject("SQLite value must be a StorageValue object")
125
+ return
126
+ }
127
+ let ok = try sqliteStore(for: call).set(
128
+ table: sqliteTableName(from: call.getString("table")),
129
+ key: key,
130
+ wrapper: jsObject as [String: Any]
131
+ )
95
132
  if !ok {
96
133
  call.reject("Failed to write value to SQLite")
97
134
  return
98
135
  }
99
136
  case "filesystem":
100
- rejectUnsupportedFilesystem(call)
101
- return
137
+ guard let jsObject = call.getObject("value") else {
138
+ call.reject("Filesystem value must be a StorageValue object")
139
+ return
140
+ }
141
+ let ok = try filesystemStorage.set(key: key, wrapper: jsObject as [String: Any])
142
+ if !ok {
143
+ call.reject("Failed to write value to filesystem")
144
+ return
145
+ }
102
146
  case "preferences":
103
147
  fallthrough
104
148
  default:
149
+ let value = call.getValue("value") ?? NSNull()
105
150
  userDefaultsStorage.set(key: key, value: value)
106
151
  }
107
152
 
@@ -127,10 +172,9 @@ public class StrataStoragePlugin: CAPPlugin {
127
172
  case "secure":
128
173
  _ = try keychainStorage.remove(key: key)
129
174
  case "sqlite":
130
- _ = sqliteStorage.remove(key: key)
175
+ _ = sqliteStore(for: call).remove(table: sqliteTableName(from: call.getString("table")), key: key)
131
176
  case "filesystem":
132
- rejectUnsupportedFilesystem(call)
133
- return
177
+ _ = filesystemStorage.remove(key: key)
134
178
  case "preferences":
135
179
  fallthrough
136
180
  default:
@@ -158,10 +202,9 @@ public class StrataStoragePlugin: CAPPlugin {
158
202
  case "secure":
159
203
  _ = try keychainStorage.clear(prefix: prefix)
160
204
  case "sqlite":
161
- _ = sqliteStorage.clear(prefix: prefix)
205
+ _ = sqliteStore(for: call).clear(table: sqliteTableName(from: call.getString("table")), prefix: prefix)
162
206
  case "filesystem":
163
- rejectUnsupportedFilesystem(call)
164
- return
207
+ _ = filesystemStorage.clear(prefix: prefix)
165
208
  case "preferences":
166
209
  fallthrough
167
210
  default:
@@ -188,10 +231,9 @@ public class StrataStoragePlugin: CAPPlugin {
188
231
  case "secure":
189
232
  keys = try keychainStorage.keys(pattern: pattern)
190
233
  case "sqlite":
191
- keys = sqliteStorage.keys(pattern: pattern)
234
+ keys = sqliteStore(for: call).keys(table: sqliteTableName(from: call.getString("table")), pattern: pattern)
192
235
  case "filesystem":
193
- rejectUnsupportedFilesystem(call)
194
- return
236
+ keys = filesystemStorage.keys(pattern: pattern)
195
237
  case "preferences":
196
238
  fallthrough
197
239
  default:
@@ -207,32 +249,45 @@ public class StrataStoragePlugin: CAPPlugin {
207
249
  }
208
250
 
209
251
  /**
210
- * Get storage size information
252
+ * Get storage size information.
253
+ * When `detailed` is true, resolves `{ total, count, detailed: { keys,
254
+ * values, metadata } }`; otherwise `{ total, count }`.
211
255
  */
212
256
  @objc func size(_ call: CAPPluginCall) {
213
257
  let storage = call.getString("storage") ?? "preferences"
258
+ let detailed = call.getBool("detailed") ?? false
214
259
 
215
260
  do {
216
- let sizeInfo: (total: Int, count: Int)
261
+ // segments carries total/count/keys/values/metadata in bytes.
262
+ let segments: [String: Int]
217
263
 
218
264
  switch storage {
219
265
  case "secure":
220
- sizeInfo = try keychainStorage.size()
266
+ let info = try keychainStorage.size()
267
+ segments = ["total": info.total, "count": info.count, "keys": 0, "values": info.total, "metadata": 0]
221
268
  case "sqlite":
222
- sizeInfo = try sqliteStorage.size()
269
+ segments = try sqliteStore(for: call).size(table: sqliteTableName(from: call.getString("table")))
223
270
  case "filesystem":
224
- rejectUnsupportedFilesystem(call)
225
- return
271
+ segments = filesystemStorage.size()
226
272
  case "preferences":
227
273
  fallthrough
228
274
  default:
229
- sizeInfo = userDefaultsStorage.size()
275
+ let info = userDefaultsStorage.size()
276
+ segments = ["total": info.total, "count": info.count, "keys": 0, "values": info.total, "metadata": 0]
230
277
  }
231
278
 
232
- call.resolve([
233
- "total": sizeInfo.total,
234
- "count": sizeInfo.count
235
- ])
279
+ var result: [String: Any] = [
280
+ "total": segments["total"] ?? 0,
281
+ "count": segments["count"] ?? 0
282
+ ]
283
+ if detailed {
284
+ result["detailed"] = [
285
+ "keys": segments["keys"] ?? 0,
286
+ "values": segments["values"] ?? 0,
287
+ "metadata": segments["metadata"] ?? 0
288
+ ]
289
+ }
290
+ call.resolve(result)
236
291
  } catch {
237
292
  call.reject("Failed to get size", nil, error)
238
293
  }
@@ -241,7 +296,7 @@ public class StrataStoragePlugin: CAPPlugin {
241
296
  /**
242
297
  * Query SQLite-backed storage.
243
298
  * Matches the optional `query` method in the JS contract:
244
- * resolves `{ results: [{ key, value }] }`.
299
+ * resolves `{ results: [{ key }] }` (JS re-fetches each value via get).
245
300
  */
246
301
  @objc func query(_ call: CAPPluginCall) {
247
302
  let storage = call.getString("storage") ?? "sqlite"
@@ -252,7 +307,10 @@ public class StrataStoragePlugin: CAPPlugin {
252
307
  }
253
308
 
254
309
  let condition = call.getObject("condition") ?? [:]
255
- let results = sqliteStorage.query(condition: condition)
310
+ let results = sqliteStore(for: call).query(
311
+ table: sqliteTableName(from: call.getString("table")),
312
+ condition: condition
313
+ )
256
314
  call.resolve([
257
315
  "results": results
258
316
  ])
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "strata-storage",
3
- "version": "2.5.0",
3
+ "version": "2.6.1",
4
4
  "description": "Zero-dependency universal storage plugin providing a unified API for all storage operations across web, Android, and iOS platforms",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -103,9 +103,9 @@
103
103
  }
104
104
  },
105
105
  "devDependencies": {
106
- "@angular/common": "^21.2.14",
107
- "@angular/core": "^21.2.14",
108
- "@angular/forms": "^21.2.14",
106
+ "@angular/common": "^21.2.15",
107
+ "@angular/core": "^21.2.15",
108
+ "@angular/forms": "^21.2.15",
109
109
  "@capacitor/core": "^8.3.4",
110
110
  "@types/node": "^25.9.1",
111
111
  "@types/react": "^19.2.15",
@@ -114,7 +114,7 @@
114
114
  "@vitest/coverage-v8": "^4.1.7",
115
115
  "eslint": "^10.4.0",
116
116
  "eslint-config-prettier": "^10.1.8",
117
- "eslint-plugin-prettier": "^5.5.5",
117
+ "eslint-plugin-prettier": "^5.5.6",
118
118
  "jsdom": "^29.1.1",
119
119
  "prettier": "^3.8.3",
120
120
  "react": "^19.2.6",
@@ -122,7 +122,7 @@
122
122
  "typescript": "^6.0.3",
123
123
  "typescript-eslint": "^8.60.0",
124
124
  "vitest": "^4.1.7",
125
- "vue": "^3.5.34",
125
+ "vue": "^3.5.35",
126
126
  "zone.js": "~0.16.0"
127
127
  },
128
128
  "engines": {