apiro-db 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.
- package/.gitattributes +2 -0
- package/LICENSE +21 -0
- package/README.md +107 -0
- package/index.js +1 -0
- package/lib/crypto.js +48 -0
- package/lib/file.js +17 -0
- package/lib/store.js +79 -0
- package/package.json +12 -0
package/.gitattributes
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kris Powers
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# Secure Store
|
|
2
|
+
|
|
3
|
+
A lightweight, zero-dependency, encrypted data store for Node.js.
|
|
4
|
+
|
|
5
|
+
Secure Store provides a simple key-value database similar to Quick.db, while removing all third-party dependencies and keeping your data encrypted at rest using Node’s built-in cryptography.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
* Zero dependencies
|
|
12
|
+
* Fully asynchronous API
|
|
13
|
+
* AES-256-GCM encrypted storage
|
|
14
|
+
* User-supplied encryption key
|
|
15
|
+
* Human-unreadable data at rest
|
|
16
|
+
* Simple, familiar database methods
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm install secure-store
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
```js
|
|
31
|
+
const { SecureStore } = require("secure-store");
|
|
32
|
+
|
|
33
|
+
const db = new SecureStore({
|
|
34
|
+
file: "./data.db",
|
|
35
|
+
secret: process.env.DB_SECRET
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
await db.set("coins", 100);
|
|
39
|
+
await db.add("coins", 25);
|
|
40
|
+
await db.push("items", "shield");
|
|
41
|
+
|
|
42
|
+
console.log(await db.get("coins")); // 125
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
## Configuration
|
|
48
|
+
|
|
49
|
+
| Option | Type | Required | Description |
|
|
50
|
+
| -------- | ------ | -------- | ----------------------------------- |
|
|
51
|
+
| `file` | string | No | Path to the encrypted data file |
|
|
52
|
+
| `secret` | string | Yes | Encryption key provided by the user |
|
|
53
|
+
|
|
54
|
+
> **Important:**
|
|
55
|
+
> Losing the secret key will permanently lock access to the data.
|
|
56
|
+
|
|
57
|
+
---
|
|
58
|
+
|
|
59
|
+
## API Reference
|
|
60
|
+
|
|
61
|
+
### `get(key)`
|
|
62
|
+
|
|
63
|
+
Returns the value stored under the key.
|
|
64
|
+
|
|
65
|
+
### `set(key, value)`
|
|
66
|
+
|
|
67
|
+
Sets a value for the key.
|
|
68
|
+
|
|
69
|
+
### `delete(key)`
|
|
70
|
+
|
|
71
|
+
Deletes a key and returns `true` if it existed.
|
|
72
|
+
|
|
73
|
+
### `add(key, number)`
|
|
74
|
+
|
|
75
|
+
Adds a number to an existing numeric value.
|
|
76
|
+
|
|
77
|
+
### `subtract(key, number)`
|
|
78
|
+
|
|
79
|
+
Subtracts a number from a value.
|
|
80
|
+
|
|
81
|
+
### `push(key, value)`
|
|
82
|
+
|
|
83
|
+
Pushes a value onto an array.
|
|
84
|
+
|
|
85
|
+
---
|
|
86
|
+
|
|
87
|
+
## Security
|
|
88
|
+
|
|
89
|
+
* Uses AES-256-GCM authenticated encryption
|
|
90
|
+
* Key derivation via `scrypt`
|
|
91
|
+
* Tamper detection built in
|
|
92
|
+
* No plaintext data stored on disk
|
|
93
|
+
* No external crypto dependencies
|
|
94
|
+
|
|
95
|
+
---
|
|
96
|
+
|
|
97
|
+
## Limitations
|
|
98
|
+
|
|
99
|
+
* Single-file storage
|
|
100
|
+
* Not designed for concurrent multi-process writes
|
|
101
|
+
* Entire store is encrypted as a single unit
|
|
102
|
+
|
|
103
|
+
---
|
|
104
|
+
|
|
105
|
+
## License
|
|
106
|
+
|
|
107
|
+
MIT
|
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require("./lib/store");
|
package/lib/crypto.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
const crypto = require("crypto");
|
|
2
|
+
|
|
3
|
+
const ALGO = "aes-256-gcm";
|
|
4
|
+
const IV_LENGTH = 12;
|
|
5
|
+
const SALT_LENGTH = 16;
|
|
6
|
+
const TAG_LENGTH = 16;
|
|
7
|
+
|
|
8
|
+
function deriveKey(secret, salt) {
|
|
9
|
+
return crypto.scryptSync(secret, salt, 32);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function encrypt(text, secret) {
|
|
13
|
+
const iv = crypto.randomBytes(IV_LENGTH);
|
|
14
|
+
const salt = crypto.randomBytes(SALT_LENGTH);
|
|
15
|
+
const key = deriveKey(secret, salt);
|
|
16
|
+
|
|
17
|
+
const cipher = crypto.createCipheriv(ALGO, key, iv);
|
|
18
|
+
const encrypted = Buffer.concat([
|
|
19
|
+
cipher.update(text, "utf8"),
|
|
20
|
+
cipher.final()
|
|
21
|
+
]);
|
|
22
|
+
|
|
23
|
+
const tag = cipher.getAuthTag();
|
|
24
|
+
return Buffer.concat([salt, iv, tag, encrypted]).toString("base64");
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function decrypt(payload, secret) {
|
|
28
|
+
const buffer = Buffer.from(payload, "base64");
|
|
29
|
+
|
|
30
|
+
const salt = buffer.subarray(0, SALT_LENGTH);
|
|
31
|
+
const iv = buffer.subarray(SALT_LENGTH, SALT_LENGTH + IV_LENGTH);
|
|
32
|
+
const tag = buffer.subarray(
|
|
33
|
+
SALT_LENGTH + IV_LENGTH,
|
|
34
|
+
SALT_LENGTH + IV_LENGTH + TAG_LENGTH
|
|
35
|
+
);
|
|
36
|
+
const encrypted = buffer.subarray(SALT_LENGTH + IV_LENGTH + TAG_LENGTH);
|
|
37
|
+
|
|
38
|
+
const key = deriveKey(secret, salt);
|
|
39
|
+
const decipher = crypto.createDecipheriv(ALGO, key, iv);
|
|
40
|
+
decipher.setAuthTag(tag);
|
|
41
|
+
|
|
42
|
+
return Buffer.concat([
|
|
43
|
+
decipher.update(encrypted),
|
|
44
|
+
decipher.final()
|
|
45
|
+
]).toString("utf8");
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
module.exports = { encrypt, decrypt };
|
package/lib/file.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
const fs = require("fs/promises");
|
|
2
|
+
const path = require("path");
|
|
3
|
+
|
|
4
|
+
async function readFile(file) {
|
|
5
|
+
try {
|
|
6
|
+
return await fs.readFile(file, "utf8");
|
|
7
|
+
} catch {
|
|
8
|
+
return null;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
async function writeFile(file, data) {
|
|
13
|
+
await fs.mkdir(path.dirname(file), { recursive: true });
|
|
14
|
+
await fs.writeFile(file, data);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
module.exports = { readFile, writeFile };
|
package/lib/store.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
const { readFile, writeFile } = require("./file");
|
|
2
|
+
const { encrypt, decrypt } = require("./crypto");
|
|
3
|
+
|
|
4
|
+
class SecureStore {
|
|
5
|
+
constructor(options = {}) {
|
|
6
|
+
if (!options.secret) {
|
|
7
|
+
throw new Error("SecureStore requires a secret key");
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
this.file = options.file || "./secure.db";
|
|
11
|
+
this.secret = options.secret;
|
|
12
|
+
this.data = {};
|
|
13
|
+
this.ready = this._load();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async _load() {
|
|
17
|
+
const encrypted = await readFile(this.file);
|
|
18
|
+
if (!encrypted) return;
|
|
19
|
+
|
|
20
|
+
try {
|
|
21
|
+
const decrypted = decrypt(encrypted, this.secret);
|
|
22
|
+
this.data = JSON.parse(decrypted);
|
|
23
|
+
} catch {
|
|
24
|
+
throw new Error("Failed to decrypt data store. Invalid secret?");
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async _save() {
|
|
29
|
+
const json = JSON.stringify(this.data);
|
|
30
|
+
const encrypted = encrypt(json, this.secret);
|
|
31
|
+
await writeFile(this.file, encrypted);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async get(key) {
|
|
35
|
+
await this.ready;
|
|
36
|
+
return this.data[key];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async set(key, value) {
|
|
40
|
+
await this.ready;
|
|
41
|
+
this.data[key] = value;
|
|
42
|
+
await this._save();
|
|
43
|
+
return value;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async delete(key) {
|
|
47
|
+
await this.ready;
|
|
48
|
+
const existed = key in this.data;
|
|
49
|
+
delete this.data[key];
|
|
50
|
+
await this._save();
|
|
51
|
+
return existed;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async add(key, amount) {
|
|
55
|
+
await this.ready;
|
|
56
|
+
if (typeof this.data[key] !== "number") {
|
|
57
|
+
this.data[key] = 0;
|
|
58
|
+
}
|
|
59
|
+
this.data[key] += amount;
|
|
60
|
+
await this._save();
|
|
61
|
+
return this.data[key];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async subtract(key, amount) {
|
|
65
|
+
return this.add(key, -amount);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async push(key, value) {
|
|
69
|
+
await this.ready;
|
|
70
|
+
if (!Array.isArray(this.data[key])) {
|
|
71
|
+
this.data[key] = [];
|
|
72
|
+
}
|
|
73
|
+
this.data[key].push(value);
|
|
74
|
+
await this._save();
|
|
75
|
+
return this.data[key];
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
module.exports = { SecureStore };
|
package/package.json
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "apiro-db",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A lightweight, zero-dependency, encrypted data store for Node.js.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"keywords": [],
|
|
10
|
+
"author": "krispowers",
|
|
11
|
+
"license": "ISC"
|
|
12
|
+
}
|