-
Notifications
You must be signed in to change notification settings - Fork 4
/
endpoints-client.js
55 lines (43 loc) · 1.14 KB
/
endpoints-client.js
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
var es = require('event-stream')
module.exports = function (prefix, exports) {
exports = exports || {}
//put, get, del, has
exports.put = function (key, opts) {
var _key = prefix+':'+key
opts = opts || {flags: 'w'}
if(opts.flags !== 'a' || !localStorage[_key])
localStorage[_key] = ''
//assume write if not explicit append.
var ws = es.through(function (data) {
localStorage[_key] += data
})
//remove readable api.
ws.readable = false
delete ws.pause
delete ws.resume
return ws
}
exports.get = function (key, opts) {
var _key = prefix+':'+key
var array = [localStorage[_key]]
return es.readArray(array)
}
exports.del = function (key, cb) {
var _key = prefix+':'+key
process.nextTick(function () {
if(!localStorage[_key])
return cb(new Error ('no record: ' + key))
delete localStorage[prefix+':'+key]
cb()
})
}
exports.has = function (key, cb) {
var _key = prefix+':'+key
process.nextTick(function () {
if(!localStorage[_key])
return cb(new Error ('no record: ' + key))
cb()
})
}
return exports
}