deepbase-json 3.9.0 → 3.10.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.
- package/README.md +45 -40
- package/package.json +7 -7
package/README.md
CHANGED
|
@@ -484,58 +484,63 @@ await db.set("a", "b", "circular", "self", await db.get("a", "b"));
|
|
|
484
484
|
|
|
485
485
|
## 🔒 Secure Storage with Encryption
|
|
486
486
|
|
|
487
|
-
|
|
487
|
+
Use the built-in plugin to encrypt every value with AES-256-GCM before it
|
|
488
|
+
reaches the driver. With `JsonDriver`, values stay encrypted both on disk and
|
|
489
|
+
in its internal memory cache; no custom serialization or memory hooks are needed.
|
|
488
490
|
|
|
489
491
|
```javascript
|
|
490
|
-
import CryptoJS from 'crypto-js';
|
|
491
492
|
import DeepBase from 'deepbase';
|
|
493
|
+
import { encryptedValues } from 'deepbase/plugins/encryption';
|
|
492
494
|
import { JsonDriver } from 'deepbase-json';
|
|
493
495
|
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
delete opts.encryptionKey;
|
|
498
|
-
|
|
499
|
-
// Create JSON driver with encryption
|
|
500
|
-
const driver = new JsonDriver({
|
|
501
|
-
...opts,
|
|
502
|
-
stringify: (obj) => {
|
|
503
|
-
const iv = CryptoJS.lib.WordArray.random(128 / 8);
|
|
504
|
-
const encrypted = CryptoJS.AES.encrypt(
|
|
505
|
-
JSON.stringify(obj),
|
|
506
|
-
encryptionKey,
|
|
507
|
-
{ iv }
|
|
508
|
-
);
|
|
509
|
-
return iv.toString(CryptoJS.enc.Hex) + ':' + encrypted.toString();
|
|
510
|
-
},
|
|
511
|
-
parse: (encryptedData) => {
|
|
512
|
-
const [ivHex, encrypted] = encryptedData.split(':');
|
|
513
|
-
const iv = CryptoJS.enc.Hex.parse(ivHex);
|
|
514
|
-
const bytes = CryptoJS.AES.decrypt(encrypted, encryptionKey, { iv });
|
|
515
|
-
return JSON.parse(bytes.toString(CryptoJS.enc.Utf8));
|
|
516
|
-
}
|
|
517
|
-
});
|
|
518
|
-
|
|
519
|
-
super(driver);
|
|
520
|
-
}
|
|
496
|
+
const encodedKey = process.env.DEEPBASE_ENCRYPTION_KEY;
|
|
497
|
+
if (!encodedKey) {
|
|
498
|
+
throw new Error('DEEPBASE_ENCRYPTION_KEY is required');
|
|
521
499
|
}
|
|
522
500
|
|
|
523
|
-
|
|
524
|
-
const
|
|
501
|
+
const encryptionKey = Buffer.from(encodedKey, 'base64');
|
|
502
|
+
const encryption = encryptedValues({
|
|
503
|
+
activeKeyId: 'primary',
|
|
504
|
+
keys: { primary: encryptionKey }
|
|
505
|
+
});
|
|
506
|
+
|
|
507
|
+
const driver = new JsonDriver({
|
|
525
508
|
path: '/var/lib/myapp/data',
|
|
526
|
-
name: 'secure_db'
|
|
527
|
-
encryptionKey: 'your-secret-key-here'
|
|
509
|
+
name: 'secure_db'
|
|
528
510
|
});
|
|
511
|
+
const secureDB = new DeepBase(driver).use(encryption);
|
|
529
512
|
|
|
530
|
-
|
|
513
|
+
try {
|
|
514
|
+
await secureDB.set('config', {
|
|
515
|
+
service: 'my-app',
|
|
516
|
+
accessToken: 'example-token',
|
|
517
|
+
retries: 0
|
|
518
|
+
});
|
|
519
|
+
|
|
520
|
+
await secureDB.inc('config', 'retries', 1);
|
|
521
|
+
const config = await secureDB.get('config'); // Decrypted for the application
|
|
522
|
+
} finally {
|
|
523
|
+
await secureDB.dispose({ clearMemory: true, releaseInstance: true });
|
|
524
|
+
encryptionKey.fill(0);
|
|
525
|
+
}
|
|
526
|
+
```
|
|
531
527
|
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
528
|
+
Provide a base64-encoded, randomly generated 32-byte key through your
|
|
529
|
+
application's secret manager or environment. The application reads it explicitly;
|
|
530
|
+
the plugin requires a 32-byte `Buffer` or `Uint8Array` and never supplies a
|
|
531
|
+
default key. Keep the key available under the same `keyId` to reopen the database.
|
|
536
532
|
|
|
537
|
-
|
|
538
|
-
|
|
533
|
+
Object keys, array lengths, and empty containers remain visible. Plaintext
|
|
534
|
+
exists while your application supplies or reads values, including inside
|
|
535
|
+
`upd()` callbacks; the plugin does not cache decrypted values. Disposal clears
|
|
536
|
+
the JSON driver's cache and the plugin's internal key copies.
|
|
537
|
+
|
|
538
|
+
Authentication failures, malformed envelopes, and unknown key IDs throw.
|
|
539
|
+
Existing plaintext data is encrypted when rewritten through the plugin;
|
|
540
|
+
registering it does not migrate existing data automatically.
|
|
541
|
+
|
|
542
|
+
See the [encryption plugin documentation](../core/docs/plugins/encryption.md)
|
|
543
|
+
for additional keys and key rotation.
|
|
539
544
|
|
|
540
545
|
## 🛠️ Creating Custom Drivers
|
|
541
546
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "deepbase-json",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.10.1",
|
|
4
4
|
"description": "⚡ DeepBase JSON - filesystem driver",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.cjs",
|
|
@@ -18,7 +18,10 @@
|
|
|
18
18
|
"steno": "^0.4.4"
|
|
19
19
|
},
|
|
20
20
|
"peerDependencies": {
|
|
21
|
-
"deepbase": "^3.
|
|
21
|
+
"deepbase": "^3.10.1"
|
|
22
|
+
},
|
|
23
|
+
"scripts": {
|
|
24
|
+
"test": "mocha test/test.js"
|
|
22
25
|
},
|
|
23
26
|
"devDependencies": {
|
|
24
27
|
"mocha": "^10.8.2"
|
|
@@ -42,8 +45,5 @@
|
|
|
42
45
|
"bugs": {
|
|
43
46
|
"url": "https://github.com/clasen/DeepBase/issues"
|
|
44
47
|
},
|
|
45
|
-
"homepage": "https://github.com/clasen/DeepBase/tree/main/packages/driver-json"
|
|
46
|
-
|
|
47
|
-
"test": "mocha test/test.js"
|
|
48
|
-
}
|
|
49
|
-
}
|
|
48
|
+
"homepage": "https://github.com/clasen/DeepBase/tree/main/packages/driver-json"
|
|
49
|
+
}
|