This repository was archived by the owner on Jan 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
62 lines (50 loc) · 1.39 KB
/
index.js
File metadata and controls
62 lines (50 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
51
52
53
54
55
56
57
58
59
60
61
62
var path = require('path')
var homedir = require('os-homedir')
var raf = require('random-access-file')
module.exports = function (dir) {
if (!dir) dir = path.join(homedir(), '.dat', 'secret_keys')
return function (name, opts) {
var discKey = opts.discoveryKey
if (!discKey) throw new Error('Discovery key required')
if (typeof discKey !== 'string') discKey = discKey.toString('hex')
return new Storage(
raf(name),
raf(path.join(dir, discKey.slice(0, 2), discKey.slice(2)))
)
}
}
function Storage (ownerFile, secretFile) {
this.ownerFile = ownerFile
this.secretFile = secretFile
}
Storage.prototype.open = function (cb) {
if (!cb) cb = noop
var self = this
this.ownerFile.open(function (err) {
if (err) return cb(err)
self.secretFile.open(cb)
})
}
Storage.prototype.read = function (offset, length, cb) {
var self = this
this.ownerFile.read(0, 1, function (err) {
if (err) return cb(err)
self.secretFile.read(offset, length, cb)
})
}
Storage.prototype.write = function (offset, data, cb) {
if (!cb) cb = noop
var self = this
this.ownerFile.write(0, new Buffer([0]), function (err) {
if (err) return cb(err)
self.secretFile.write(offset, data, cb)
})
}
Storage.prototype.close = function (cb) {
if (!cb) cb = noop
var self = this
this.ownerFile.close(function () {
self.secretFile.close(cb)
})
}
function noop () {}