fs-key-value 1.0.0 → 1.2.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/LICENSE +21 -0
- package/README.md +181 -0
- package/fs-key-value.js +292 -182
- package/package.json +30 -15
- package/.npmignore +0 -2
- package/Readme.md +0 -52
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2013-present Andrew Shell
|
|
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,181 @@
|
|
|
1
|
+
# fs-key-value
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/fs-key-value)
|
|
4
|
+
[](https://github.com/andrewshell/node-fs-key-value/actions/workflows/ci.yml)
|
|
5
|
+
[](https://opensource.org/licenses/MIT)
|
|
6
|
+
|
|
7
|
+
A simple key-value data store using only the filesystem. Uses POSIX file locking for safe operation in multi-process environments without the overhead of a separate database server.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- Simple key-value API (`get`, `put`, `delete`)
|
|
12
|
+
- Both callback and async/await interfaces
|
|
13
|
+
- File-based storage (values stored as JSON)
|
|
14
|
+
- POSIX file locking for safe concurrent access
|
|
15
|
+
- Works across multiple processes (via cluster, child_process, etc.)
|
|
16
|
+
- No external database required
|
|
17
|
+
|
|
18
|
+
## Requirements
|
|
19
|
+
|
|
20
|
+
- Node.js >= 18.0.0
|
|
21
|
+
- POSIX-compliant filesystem (Linux, macOS, etc.)
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install fs-key-value
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Quick Start
|
|
30
|
+
|
|
31
|
+
### Using async/await
|
|
32
|
+
|
|
33
|
+
```js
|
|
34
|
+
const FsKeyValue = require('fs-key-value');
|
|
35
|
+
|
|
36
|
+
const db = new FsKeyValue();
|
|
37
|
+
await db.openAsync('./mydb');
|
|
38
|
+
|
|
39
|
+
// Store a value
|
|
40
|
+
await db.putAsync('user:1', { name: 'Alice', age: 30 });
|
|
41
|
+
|
|
42
|
+
// Retrieve the value
|
|
43
|
+
const data = await db.getAsync('user:1');
|
|
44
|
+
console.log(data); // { name: 'Alice', age: 30 }
|
|
45
|
+
|
|
46
|
+
// Delete the value
|
|
47
|
+
await db.deleteAsync('user:1');
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Using callbacks
|
|
51
|
+
|
|
52
|
+
```js
|
|
53
|
+
var FsKeyValue = require('fs-key-value');
|
|
54
|
+
|
|
55
|
+
var db = new FsKeyValue('./mydb', function (err, db) {
|
|
56
|
+
if (err) throw err;
|
|
57
|
+
|
|
58
|
+
// Store a value
|
|
59
|
+
db.put('user:1', { name: 'Alice', age: 30 }, function (err) {
|
|
60
|
+
if (err) throw err;
|
|
61
|
+
|
|
62
|
+
// Retrieve the value
|
|
63
|
+
db.get('user:1', function (err, data) {
|
|
64
|
+
if (err) throw err;
|
|
65
|
+
console.log(data); // { name: 'Alice', age: 30 }
|
|
66
|
+
|
|
67
|
+
// Delete the value
|
|
68
|
+
db.delete('user:1', function (err) {
|
|
69
|
+
if (err) throw err;
|
|
70
|
+
console.log('Deleted!');
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## API
|
|
78
|
+
|
|
79
|
+
### `new FsKeyValue([directory], [callback])`
|
|
80
|
+
|
|
81
|
+
Creates a new key-value store instance.
|
|
82
|
+
|
|
83
|
+
- `directory` (string, optional): Path to the storage directory
|
|
84
|
+
- `callback` (function, optional): Called with `(err, db)` when initialization completes
|
|
85
|
+
|
|
86
|
+
### `db.open(directory, callback)`
|
|
87
|
+
|
|
88
|
+
Initialize or reinitialize the store with a directory. Creates the directory if it doesn't exist.
|
|
89
|
+
|
|
90
|
+
- `directory` (string): Path to the storage directory
|
|
91
|
+
- `callback` (function): Called with `(err, db)` when complete
|
|
92
|
+
|
|
93
|
+
### `db.get(key, callback)`
|
|
94
|
+
|
|
95
|
+
Retrieve a value by key.
|
|
96
|
+
|
|
97
|
+
- `key` (string): The key to retrieve
|
|
98
|
+
- `callback` (function): Called with `(err, value)`. Value is `undefined` if key doesn't exist.
|
|
99
|
+
|
|
100
|
+
### `db.put(key, value, callback)`
|
|
101
|
+
|
|
102
|
+
Store a value. The value is serialized to JSON.
|
|
103
|
+
|
|
104
|
+
- `key` (string): The key to store
|
|
105
|
+
- `value` (any): Any JSON-serializable value
|
|
106
|
+
- `callback` (function): Called with `(err)` when complete
|
|
107
|
+
|
|
108
|
+
### `db.delete(key, callback)`
|
|
109
|
+
|
|
110
|
+
Delete a key from the store.
|
|
111
|
+
|
|
112
|
+
- `key` (string): The key to delete
|
|
113
|
+
- `callback` (function): Called with `(err)` when complete
|
|
114
|
+
|
|
115
|
+
### Async Methods
|
|
116
|
+
|
|
117
|
+
All methods are also available as async/Promise versions:
|
|
118
|
+
|
|
119
|
+
- `db.openAsync(directory)` → `Promise<void>`
|
|
120
|
+
- `db.getAsync(key)` → `Promise<value | undefined>`
|
|
121
|
+
- `db.putAsync(key, value)` → `Promise<void>`
|
|
122
|
+
- `db.deleteAsync(key)` → `Promise<void>`
|
|
123
|
+
|
|
124
|
+
## Multi-Process Example
|
|
125
|
+
|
|
126
|
+
This example demonstrates safe concurrent access from multiple worker processes:
|
|
127
|
+
|
|
128
|
+
```js
|
|
129
|
+
var cluster = require('cluster');
|
|
130
|
+
var FsKeyValue = require('fs-key-value');
|
|
131
|
+
|
|
132
|
+
if (cluster.isMaster) {
|
|
133
|
+
// Fork 8 worker processes
|
|
134
|
+
for (var i = 0; i < 8; i++) {
|
|
135
|
+
cluster.fork();
|
|
136
|
+
}
|
|
137
|
+
} else {
|
|
138
|
+
var id = cluster.worker.id % 2;
|
|
139
|
+
|
|
140
|
+
var mydb = new FsKeyValue('./mydb', function (err, db) {
|
|
141
|
+
if (err) {
|
|
142
|
+
return console.log(err);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
db.put(
|
|
146
|
+
'hoopla' + id,
|
|
147
|
+
{ msg: 'ballyhoo ' + cluster.worker.id },
|
|
148
|
+
function (err) {
|
|
149
|
+
if (err) {
|
|
150
|
+
return console.log(cluster.worker.id + ' err ' + err);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
db.get('hoopla' + id, function (err, data) {
|
|
154
|
+
if (err) {
|
|
155
|
+
return console.log(cluster.worker.id + ' err ' + err);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (data != undefined) {
|
|
159
|
+
console.log(data.msg);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
db.delete('hoopla' + id);
|
|
163
|
+
cluster.worker.kill();
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
);
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
## How It Works
|
|
172
|
+
|
|
173
|
+
- Each key is stored as a separate file in the specified directory
|
|
174
|
+
- Values are serialized as JSON
|
|
175
|
+
- Uses `fs-ext` for POSIX file locking (`flock`)
|
|
176
|
+
- Shared locks for reads, exclusive locks for writes
|
|
177
|
+
- Directory-level lock file (`.lock`) coordinates operations
|
|
178
|
+
|
|
179
|
+
## License
|
|
180
|
+
|
|
181
|
+
MIT
|
package/fs-key-value.js
CHANGED
|
@@ -1,226 +1,336 @@
|
|
|
1
|
-
|
|
2
|
-
path = require('path'),
|
|
3
|
-
util = require('util'),
|
|
4
|
-
Step = require('step')
|
|
1
|
+
'use strict';
|
|
5
2
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const fsp = require('fs/promises');
|
|
5
|
+
const fsExt = require('fs-ext');
|
|
6
|
+
const path = require('path');
|
|
7
|
+
const { promisify } = require('util');
|
|
9
8
|
|
|
10
|
-
|
|
11
|
-
var self = this
|
|
9
|
+
const flock = promisify(fsExt.flock);
|
|
12
10
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
}
|
|
11
|
+
/**
|
|
12
|
+
* @callback ErrorCallback
|
|
13
|
+
* @param {Error|null} err - Error if one occurred
|
|
14
|
+
*/
|
|
20
15
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
16
|
+
/**
|
|
17
|
+
* @callback GetCallback
|
|
18
|
+
* @param {Error|null} err - Error if one occurred
|
|
19
|
+
* @param {*} [value] - The retrieved value, or undefined if key doesn't exist
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @callback OpenCallback
|
|
24
|
+
* @param {Error|null} err - Error if one occurred
|
|
25
|
+
* @param {FsKeyValue} [instance] - The initialized FsKeyValue instance
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Filesystem-based key-value store with file locking.
|
|
30
|
+
*
|
|
31
|
+
* Stores values as JSON files in a directory, using POSIX file locks
|
|
32
|
+
* for safe concurrent access.
|
|
33
|
+
*
|
|
34
|
+
* @constructor
|
|
35
|
+
* @param {string} [directory] - Path to storage directory
|
|
36
|
+
* @param {OpenCallback} [callback] - Called when initialization completes
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* var FsKeyValue = require('fs-key-value');
|
|
40
|
+
* var store = new FsKeyValue('/tmp/mydb', function(err, db) {
|
|
41
|
+
* if (err) throw err;
|
|
42
|
+
* db.put('mykey', { foo: 'bar' }, function(err) {
|
|
43
|
+
* if (err) throw err;
|
|
44
|
+
* console.log('Value stored');
|
|
45
|
+
* });
|
|
46
|
+
* });
|
|
47
|
+
*/
|
|
48
|
+
function FsKeyValue(directory, callback) {
|
|
49
|
+
if (directory) {
|
|
50
|
+
this.open(directory, callback);
|
|
51
|
+
}
|
|
51
52
|
}
|
|
52
53
|
|
|
53
|
-
|
|
54
|
-
|
|
54
|
+
/**
|
|
55
|
+
* The storage directory path.
|
|
56
|
+
* @type {string}
|
|
57
|
+
*/
|
|
58
|
+
FsKeyValue.prototype.directory = undefined;
|
|
55
59
|
|
|
56
|
-
|
|
57
|
-
|
|
60
|
+
/**
|
|
61
|
+
* Path to the directory lock file.
|
|
62
|
+
* @type {string}
|
|
63
|
+
*/
|
|
64
|
+
FsKeyValue.prototype.directoryLock = undefined;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Initialize the store with a directory.
|
|
68
|
+
*
|
|
69
|
+
* Creates the directory if it doesn't exist.
|
|
70
|
+
*
|
|
71
|
+
* @param {string} directory - Path to storage directory
|
|
72
|
+
* @param {OpenCallback} [callback] - Called when initialization completes
|
|
73
|
+
*/
|
|
74
|
+
FsKeyValue.prototype.open = function (directory, callback) {
|
|
75
|
+
if (typeof callback !== 'function') {
|
|
76
|
+
callback = function (err) {
|
|
58
77
|
if (err) {
|
|
59
|
-
throw err
|
|
78
|
+
throw err;
|
|
60
79
|
}
|
|
61
|
-
}
|
|
80
|
+
};
|
|
62
81
|
}
|
|
63
82
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
83
|
+
this.openAsync(directory)
|
|
84
|
+
.then(() => callback(null, this))
|
|
85
|
+
.catch((err) => callback(err));
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Initialize the store with a directory (async version).
|
|
90
|
+
*
|
|
91
|
+
* Creates the directory if it doesn't exist.
|
|
92
|
+
*
|
|
93
|
+
* @param {string} directory - Path to storage directory
|
|
94
|
+
* @returns {Promise<void>}
|
|
95
|
+
*/
|
|
96
|
+
FsKeyValue.prototype.openAsync = async function (directory) {
|
|
97
|
+
const exists = fs.existsSync(directory);
|
|
98
|
+
if (!exists) {
|
|
99
|
+
await fsp.mkdir(directory, { mode: 0o777 });
|
|
100
|
+
}
|
|
101
|
+
this.directory = directory;
|
|
102
|
+
this.directoryLock = path.join(directory, '.lock');
|
|
103
|
+
};
|
|
67
104
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
105
|
+
/**
|
|
106
|
+
* Retrieve a value by key.
|
|
107
|
+
*
|
|
108
|
+
* Uses a shared lock on the key file for safe concurrent reads.
|
|
109
|
+
*
|
|
110
|
+
* @param {string} key - The key to retrieve
|
|
111
|
+
* @param {GetCallback} [callback] - Called with the value or undefined if not found
|
|
112
|
+
*/
|
|
113
|
+
FsKeyValue.prototype.get = function (key, callback) {
|
|
114
|
+
if (typeof callback !== 'function') {
|
|
115
|
+
callback = function (err) {
|
|
73
116
|
if (err) {
|
|
74
|
-
|
|
117
|
+
throw err;
|
|
75
118
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
this.getAsync(key)
|
|
123
|
+
.then((value) => {
|
|
124
|
+
if (value === undefined) {
|
|
125
|
+
// Preserve original behavior: callback() with no args for missing keys
|
|
126
|
+
callback();
|
|
82
127
|
} else {
|
|
83
|
-
callback()
|
|
128
|
+
callback(null, value);
|
|
84
129
|
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
130
|
+
})
|
|
131
|
+
.catch((err) => callback(err));
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Retrieve a value by key (async version).
|
|
136
|
+
*
|
|
137
|
+
* Uses a shared lock on the key file for safe concurrent reads.
|
|
138
|
+
*
|
|
139
|
+
* @param {string} key - The key to retrieve
|
|
140
|
+
* @returns {Promise<*>} The value, or undefined if key doesn't exist
|
|
141
|
+
*/
|
|
142
|
+
FsKeyValue.prototype.getAsync = async function (key) {
|
|
143
|
+
const filename = path.join(this.directory, key);
|
|
144
|
+
let dirlock = null;
|
|
145
|
+
let keyfile = null;
|
|
146
|
+
|
|
147
|
+
try {
|
|
148
|
+
// Open and lock directory
|
|
149
|
+
dirlock = fs.openSync(this.directoryLock, 'a', 0o666);
|
|
150
|
+
await flock(dirlock, 'sh');
|
|
151
|
+
|
|
152
|
+
// Check if key exists
|
|
153
|
+
const exists = fs.existsSync(filename);
|
|
154
|
+
if (!exists) {
|
|
155
|
+
return undefined;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Open and lock key file
|
|
159
|
+
keyfile = fs.openSync(filename, 'a+', 0o666);
|
|
160
|
+
await flock(keyfile, 'sh');
|
|
161
|
+
|
|
162
|
+
// Read value
|
|
163
|
+
const data = await fsp.readFile(filename, { encoding: 'utf8' });
|
|
164
|
+
return JSON.parse(data);
|
|
165
|
+
} finally {
|
|
166
|
+
// Release locks and close files
|
|
167
|
+
if (keyfile !== null) {
|
|
168
|
+
try {
|
|
169
|
+
await flock(keyfile, 'un');
|
|
170
|
+
} catch {
|
|
171
|
+
/* ignore */
|
|
96
172
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
return callback(err)
|
|
173
|
+
try {
|
|
174
|
+
fs.closeSync(keyfile);
|
|
175
|
+
} catch {
|
|
176
|
+
/* ignore */
|
|
102
177
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
function releaseDirectorySharedLock (err) {
|
|
110
|
-
if (err) {
|
|
111
|
-
return callback(err)
|
|
178
|
+
}
|
|
179
|
+
if (dirlock !== null) {
|
|
180
|
+
try {
|
|
181
|
+
await flock(dirlock, 'un');
|
|
182
|
+
} catch {
|
|
183
|
+
/* ignore */
|
|
112
184
|
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
return callback(err)
|
|
185
|
+
try {
|
|
186
|
+
fs.closeSync(dirlock);
|
|
187
|
+
} catch {
|
|
188
|
+
/* ignore */
|
|
118
189
|
}
|
|
119
|
-
callback(err, JSON.parse(value))
|
|
120
190
|
}
|
|
121
|
-
|
|
122
|
-
}
|
|
191
|
+
}
|
|
192
|
+
};
|
|
123
193
|
|
|
194
|
+
/**
|
|
195
|
+
* Store a value by key.
|
|
196
|
+
*
|
|
197
|
+
* Uses an exclusive lock on the key file for safe writes.
|
|
198
|
+
* The value is serialized to JSON.
|
|
199
|
+
*
|
|
200
|
+
* @param {string} key - The key to store
|
|
201
|
+
* @param {*} value - The value to store (must be JSON-serializable)
|
|
202
|
+
* @param {ErrorCallback} [callback] - Called when write completes
|
|
203
|
+
*/
|
|
124
204
|
FsKeyValue.prototype.put = function (key, value, callback) {
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
if (typeof callback != 'function') {
|
|
128
|
-
var callback = function (err) {
|
|
205
|
+
if (typeof callback !== 'function') {
|
|
206
|
+
callback = function (err) {
|
|
129
207
|
if (err) {
|
|
130
|
-
throw err
|
|
208
|
+
throw err;
|
|
131
209
|
}
|
|
132
|
-
}
|
|
210
|
+
};
|
|
133
211
|
}
|
|
134
212
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
213
|
+
this.putAsync(key, value)
|
|
214
|
+
.then(() => callback(null))
|
|
215
|
+
.catch((err) => callback(err));
|
|
216
|
+
};
|
|
138
217
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
218
|
+
/**
|
|
219
|
+
* Store a value by key (async version).
|
|
220
|
+
*
|
|
221
|
+
* Uses an exclusive lock on the key file for safe writes.
|
|
222
|
+
* The value is serialized to JSON.
|
|
223
|
+
*
|
|
224
|
+
* @param {string} key - The key to store
|
|
225
|
+
* @param {*} value - The value to store (must be JSON-serializable)
|
|
226
|
+
* @returns {Promise<void>}
|
|
227
|
+
*/
|
|
228
|
+
FsKeyValue.prototype.putAsync = async function (key, value) {
|
|
229
|
+
const filename = path.join(this.directory, key);
|
|
230
|
+
let dirlock = null;
|
|
231
|
+
let keyfile = null;
|
|
232
|
+
|
|
233
|
+
try {
|
|
234
|
+
// Open and lock directory (shared lock for puts)
|
|
235
|
+
dirlock = fs.openSync(this.directoryLock, 'a', 0o666);
|
|
236
|
+
await flock(dirlock, 'sh');
|
|
237
|
+
|
|
238
|
+
// Open and lock key file (exclusive lock for write)
|
|
239
|
+
keyfile = fs.openSync(filename, 'a', 0o666);
|
|
240
|
+
await flock(keyfile, 'ex');
|
|
241
|
+
|
|
242
|
+
// Write value
|
|
243
|
+
await fsp.writeFile(filename, JSON.stringify(value), { encoding: 'utf8' });
|
|
244
|
+
} finally {
|
|
245
|
+
// Release locks and close files
|
|
246
|
+
if (keyfile !== null) {
|
|
247
|
+
try {
|
|
248
|
+
await flock(keyfile, 'un');
|
|
249
|
+
} catch {
|
|
250
|
+
/* ignore */
|
|
153
251
|
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
if (err) {
|
|
159
|
-
return callback(err)
|
|
252
|
+
try {
|
|
253
|
+
fs.closeSync(keyfile);
|
|
254
|
+
} catch {
|
|
255
|
+
/* ignore */
|
|
160
256
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
257
|
+
}
|
|
258
|
+
if (dirlock !== null) {
|
|
259
|
+
try {
|
|
260
|
+
await flock(dirlock, 'un');
|
|
261
|
+
} catch {
|
|
262
|
+
/* ignore */
|
|
166
263
|
}
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
return callback(err)
|
|
264
|
+
try {
|
|
265
|
+
fs.closeSync(dirlock);
|
|
266
|
+
} catch {
|
|
267
|
+
/* ignore */
|
|
172
268
|
}
|
|
173
|
-
fs.flock(self.lock, 'un', this)
|
|
174
|
-
},
|
|
175
|
-
function finishPuttingKey (err) {
|
|
176
|
-
return callback(err)
|
|
177
269
|
}
|
|
178
|
-
|
|
179
|
-
}
|
|
270
|
+
}
|
|
271
|
+
};
|
|
180
272
|
|
|
273
|
+
/**
|
|
274
|
+
* Delete a key from the store.
|
|
275
|
+
*
|
|
276
|
+
* Uses an exclusive lock on the directory for safe deletion.
|
|
277
|
+
* Does nothing if the key doesn't exist.
|
|
278
|
+
*
|
|
279
|
+
* @param {string} key - The key to delete
|
|
280
|
+
* @param {ErrorCallback} [callback] - Called when deletion completes
|
|
281
|
+
*/
|
|
181
282
|
FsKeyValue.prototype.delete = function (key, callback) {
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
if (typeof callback != 'function') {
|
|
185
|
-
var callback = function (err) {
|
|
283
|
+
if (typeof callback !== 'function') {
|
|
284
|
+
callback = function (err) {
|
|
186
285
|
if (err) {
|
|
187
|
-
throw err
|
|
286
|
+
throw err;
|
|
188
287
|
}
|
|
189
|
-
}
|
|
288
|
+
};
|
|
190
289
|
}
|
|
191
290
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
291
|
+
this.deleteAsync(key)
|
|
292
|
+
.then(() => callback(null))
|
|
293
|
+
.catch((err) => callback(err));
|
|
294
|
+
};
|
|
195
295
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
296
|
+
/**
|
|
297
|
+
* Delete a key from the store (async version).
|
|
298
|
+
*
|
|
299
|
+
* Uses an exclusive lock on the directory for safe deletion.
|
|
300
|
+
* Does nothing if the key doesn't exist.
|
|
301
|
+
*
|
|
302
|
+
* @param {string} key - The key to delete
|
|
303
|
+
* @returns {Promise<void>}
|
|
304
|
+
*/
|
|
305
|
+
FsKeyValue.prototype.deleteAsync = async function (key) {
|
|
306
|
+
const filename = path.join(this.directory, key);
|
|
307
|
+
let dirlock = null;
|
|
308
|
+
|
|
309
|
+
try {
|
|
310
|
+
// Open and lock directory (exclusive lock for delete)
|
|
311
|
+
dirlock = fs.openSync(this.directoryLock, 'a', 0o666);
|
|
312
|
+
await flock(dirlock, 'ex');
|
|
313
|
+
|
|
314
|
+
// Delete if exists
|
|
315
|
+
const exists = fs.existsSync(filename);
|
|
316
|
+
if (exists) {
|
|
317
|
+
await fsp.unlink(filename);
|
|
318
|
+
}
|
|
319
|
+
} finally {
|
|
320
|
+
// Release lock and close file
|
|
321
|
+
if (dirlock !== null) {
|
|
322
|
+
try {
|
|
323
|
+
await flock(dirlock, 'un');
|
|
324
|
+
} catch {
|
|
325
|
+
/* ignore */
|
|
212
326
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
327
|
+
try {
|
|
328
|
+
fs.closeSync(dirlock);
|
|
329
|
+
} catch {
|
|
330
|
+
/* ignore */
|
|
217
331
|
}
|
|
218
|
-
fs.flock(self.lock, 'un', this)
|
|
219
|
-
},
|
|
220
|
-
function finishDeletingKey (err) {
|
|
221
|
-
return callback(err)
|
|
222
332
|
}
|
|
223
|
-
|
|
224
|
-
}
|
|
333
|
+
}
|
|
334
|
+
};
|
|
225
335
|
|
|
226
|
-
module.exports = FsKeyValue
|
|
336
|
+
module.exports = FsKeyValue;
|
package/package.json
CHANGED
|
@@ -1,22 +1,37 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fs-key-value",
|
|
3
|
+
"version": "1.2.0",
|
|
3
4
|
"description": "Key value data store using the filesystem",
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
"author": "Andrew Shell <andrew@andrewshell.org> (http://blog.andrewshell.org/)",
|
|
9
|
-
"main": "./fs-key-value.js",
|
|
10
|
-
"dependencies": {
|
|
11
|
-
"fs-ext": ">= 0.3.x",
|
|
12
|
-
"path": ">= 0.4.x",
|
|
13
|
-
"step": "*"
|
|
5
|
+
"homepage": "https://github.com/andrewshell/node-fs-key-value",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/andrewshell/node-fs-key-value.git"
|
|
14
9
|
},
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://github.com/andrewshell/node-fs-key-value/issues"
|
|
12
|
+
},
|
|
13
|
+
"author": "Andrew Shell <andrew@andrewshell.org> (https://blog.andrewshell.org/)",
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"main": "./fs-key-value.js",
|
|
16
|
+
"files": [
|
|
17
|
+
"fs-key-value.js",
|
|
18
|
+
"README.md",
|
|
19
|
+
"LICENSE"
|
|
20
|
+
],
|
|
15
21
|
"engines": {
|
|
16
|
-
"node": ">=
|
|
22
|
+
"node": ">=18.0.0"
|
|
17
23
|
},
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
"
|
|
24
|
+
"scripts": {
|
|
25
|
+
"test": "node --test 'test/**/*.test.js'",
|
|
26
|
+
"lint": "eslint .",
|
|
27
|
+
"format": "prettier --write .",
|
|
28
|
+
"format:check": "prettier --check ."
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"fs-ext": "^2.0.0"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"eslint": "^9.17.0",
|
|
35
|
+
"prettier": "^3.4.2"
|
|
21
36
|
}
|
|
22
|
-
}
|
|
37
|
+
}
|
package/.npmignore
DELETED
package/Readme.md
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
# fs-key-value
|
|
2
|
-
|
|
3
|
-
This module provides a simple key value data store using only the file system. It makes use of file locking to allow safe operation in a multiple process environment without the overhead of a separate database server.
|
|
4
|
-
|
|
5
|
-
[](https://nodei.co/npm/fs-key-value/)
|
|
6
|
-
|
|
7
|
-
## Installation
|
|
8
|
-
|
|
9
|
-
```bash
|
|
10
|
-
$ npm install fs-key-value
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
## Example
|
|
14
|
-
|
|
15
|
-
```js
|
|
16
|
-
var cluster = require('cluster'),
|
|
17
|
-
FsKeyValue = require('fs-key-value')
|
|
18
|
-
|
|
19
|
-
if (cluster.isMaster) {
|
|
20
|
-
for (var i = 0; i < 8; i++) {
|
|
21
|
-
cluster.fork()
|
|
22
|
-
}
|
|
23
|
-
} else {
|
|
24
|
-
var id = cluster.worker.id % 2
|
|
25
|
-
|
|
26
|
-
var mydb = new FsKeyValue('./mydb', function (err, db) {
|
|
27
|
-
if (err) {
|
|
28
|
-
return console.log(err)
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
db.put('hoopla' + id, {'msg': 'ballyhoo ' + cluster.worker.id}, function (err) {
|
|
32
|
-
if (err) {
|
|
33
|
-
return console.log(cluster.worker.id + ' err ' + err)
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
db.get('hoopla' + id, function (err, data) {
|
|
37
|
-
if (err) {
|
|
38
|
-
return console.log(cluster.worker.id + ' err ' + err)
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
if (data != undefined) {
|
|
42
|
-
console.log(data.msg)
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
db.delete('hoopla' + id)
|
|
46
|
-
|
|
47
|
-
cluster.worker.kill()
|
|
48
|
-
})
|
|
49
|
-
})
|
|
50
|
-
})
|
|
51
|
-
}
|
|
52
|
-
```
|