-
Notifications
You must be signed in to change notification settings - Fork 5
/
inmem.js
38 lines (33 loc) · 1023 Bytes
/
inmem.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
var Docuset = require('./docuset')
var es = require('event-stream')
//this implements a server interface, but entirely in memory.
module.exports = function (opts) {
var d = new Docuset(opts)
d.createServer = function (onConnect) {
this.listening = false
this.listen = function () {
var cb = arguments[arguments.length - 1]
this.listening = true
if('function' == typeof cb)
process.nextTick(cb)
return this
}
this._isInMemoryServer = true
this._connect = function () {
var ins = es.asyncThrough()
var out = es.asyncThrough()
this.createHandler(onConnect)(ins, out)
return es.duplex(ins, out)
}
return this
}
d.connect = d.createConnection =
function (other, cb) {
if(!other._isInMemoryServer)
throw new Error('other must be an inMemory Server')
if(!other.listening)
throw new Error('other is not listening')
return this.createHandler()(other._connect())
}
return d
}