🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
Sign In

fs-key-value

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fs-key-value - npm Package Compare versions

Comparing version
0.1.2
to
1.0.0
+215
-35
fs-key-value.js

@@ -1,46 +0,226 @@

var fs = require('fs-ext')
var path = require('path')
var fs = require('fs-ext'),
path = require('path'),
util = require('util'),
Step = require('step')
function FsKeyValue (directory) {
this.directory = directory
if (!fs.existsSync(this.directory)) {
fs.mkdirSync(this.directory)
function FsKeyValue (directory, callback) {
this.open(directory, callback)
}
FsKeyValue.prototype.open = function (directory, callback) {
var self = this
if (typeof callback != 'function') {
var callback = function (err) {
if (err) {
throw err
}
}
}
var filename = path.join(this.directory, '.lock')
this.lock = fs.openSync(filename, 'a')
Step(
function initialize () {
fs.exists(directory, this)
},
function makeDirectoryIfNotExists (exists) {
if (exists) {
return this()
} else {
fs.mkdir(directory, 0777, this)
}
},
function openDirectoryLockFile (err) {
if (err) {
return callback(err)
}
var filename = path.join(directory, '.lock')
fs.open(filename, 'a', 0666, this)
},
function assignDirectoryLockFile (err, fd) {
if (err) {
return callback(err)
}
self.lock = fd
this()
},
function finishInitialization() {
self.directory = directory
callback(null, self)
}
)
}
FsKeyValue.prototype.get = function (key) {
fs.flockSync(this.lock, 'sh')
var filename = path.join(this.directory, key)
if (!fs.existsSync(filename)) {
return
FsKeyValue.prototype.get = function (key, callback) {
var self = this
if (typeof callback != 'function') {
var callback = function (err) {
if (err) {
throw err
}
}
}
var fd = fs.openSync(filename, 'a+')
fs.flockSync(fd, 'sh')
var data = fs.readFileSync(filename, {'encoding': 'utf8'})
fs.flockSync(fd, 'un')
fs.flockSync(this.lock, 'un')
return JSON.parse(data)
var filename
var keyfile
var value
Step(
function getDirectorySharedLock () {
fs.flock(self.lock, 'sh', this)
},
function doesKeyFileExist (err) {
if (err) {
return callback(err)
}
filename = path.join(self.directory, key)
fs.exists(filename, this)
},
function openKeyFile (exists) {
if (exists) {
fs.open(filename, 'a+', 0666, this)
} else {
callback()
}
},
function getKeyFileSharedLock (err, fd) {
if (err) {
return callback(err)
}
keyfile = fd
fs.flock(keyfile, 'sh', this)
},
function readKeyFile (err) {
if (err) {
return callback(err)
}
fs.readFile(filename, {'encoding': 'utf8'}, this)
},
function recordKeyValue (err, data) {
if (err) {
return callback(err)
}
value = data
this()
},
function releaseKeyFileSharedLock () {
fs.flock(keyfile, 'un', this)
},
function releaseDirectorySharedLock (err) {
if (err) {
return callback(err)
}
fs.flock(self.lock, 'un', this)
},
function finishGettingKey (err) {
if (err) {
return callback(err)
}
callback(err, JSON.parse(value))
}
)
}
FsKeyValue.prototype.put = function (key, value) {
fs.flockSync(this.lock, 'sh')
var filename = path.join(this.directory, key)
var fd = fs.openSync(filename, 'a')
fs.flockSync(fd, 'ex')
fs.writeFileSync(filename, JSON.stringify(value), {'encoding': 'utf8'})
fs.flockSync(fd, 'un')
fs.flockSync(this.lock, 'un')
};
FsKeyValue.prototype.put = function (key, value, callback) {
var self = this
FsKeyValue.prototype.del = function (key) {
fs.flockSync(this.lock, 'ex')
var filename = path.join(this.directory, key)
if (fs.existsSync(filename)) {
fs.unlinkSync(filename)
if (typeof callback != 'function') {
var callback = function (err) {
if (err) {
throw err
}
}
}
fs.flockSync(this.lock, 'un')
};
var filename;
var keyfile;
var value;
Step(
function getDirectorySharedLock () {
fs.flock(self.lock, 'sh', this)
},
function openKeyFile (err) {
if (err) {
return callback(err)
}
filename = path.join(self.directory, key)
fs.open(filename, 'a', 0666, this)
},
function getKeyFileExclusiveLock (err, fd) {
if (err) {
return callback(err)
}
keyfile = fd
fs.flock(keyfile, 'ex', this)
},
function writeKeyFile (err) {
if (err) {
return callback(err)
}
fs.writeFile(filename, JSON.stringify(value), {'encoding': 'utf8'}, this)
},
function releaseKeyFileSharedLock (err) {
if (err) {
return callback(err)
}
fs.flock(keyfile, 'un', this)
},
function releaseDirectorySharedLock (err) {
if (err) {
return callback(err)
}
fs.flock(self.lock, 'un', this)
},
function finishPuttingKey (err) {
return callback(err)
}
)
}
FsKeyValue.prototype.delete = function (key, callback) {
var self = this
if (typeof callback != 'function') {
var callback = function (err) {
if (err) {
throw err
}
}
}
var filename;
var keyfile;
var value;
Step(
function getDirectoryExclusiveLock () {
fs.flock(self.lock, 'ex', this)
},
function doesKeyFileExist (err) {
if (err) {
return callback(err)
}
filename = path.join(self.directory, key)
fs.exists(filename, this)
},
function deleteKeyFile (exists) {
if (exists) {
fs.unlink(filename, this)
} else {
return callback()
}
},
function releaseDirectorySharedLock (err) {
if (err) {
return callback(err)
}
fs.flock(self.lock, 'un', this)
},
function finishDeletingKey (err) {
return callback(err)
}
)
}
module.exports = FsKeyValue
+3
-2

@@ -5,3 +5,3 @@ {

"keywords": ["key", "value", "fs", "fs-ext", "flock", "db"],
"version": "0.1.2",
"version": "1.0.0",
"homepage": "http://andrewshell.github.com/node-fs-key-value",

@@ -13,3 +13,4 @@ "repository": "git://github.com/andrewshell/node-fs-key-value.git",

"fs-ext": ">= 0.3.x",
"path": ">= 0.4.x"
"path": ">= 0.4.x",
"step": "*"
},

@@ -16,0 +17,0 @@ "engines": {

+24
-12

@@ -16,8 +16,5 @@ # fs-key-value

```js
var FsKeyValue = new require('fs-key-value')
var cluster = require('cluster'),
FsKeyValue = require('fs-key-value')
var db = new FsKeyValue('./mydb')
var cluster = require('cluster')
if (cluster.isMaster) {

@@ -30,13 +27,28 @@ for (var i = 0; i < 8; i++) {

db.put('hoopla' + id, {'msg': 'ballyhoo ' + cluster.worker.id})
var mydb = new FsKeyValue('./mydb', function (err, db) {
if (err) {
return console.log(err)
}
var data = db.get('hoopla' + id)
if (data != undefined) {
console.log(data.msg)
}
db.put('hoopla' + id, {'msg': 'ballyhoo ' + cluster.worker.id}, function (err) {
if (err) {
return console.log(cluster.worker.id + ' err ' + err)
}
db.del('hoopla' + id)
db.get('hoopla' + id, function (err, data) {
if (err) {
return console.log(cluster.worker.id + ' err ' + err)
}
cluster.worker.kill()
if (data != undefined) {
console.log(data.msg)
}
db.delete('hoopla' + id)
cluster.worker.kill()
})
})
})
}
```

Sorry, the diff of this file is not supported yet