-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.js
176 lines (151 loc) · 4.97 KB
/
module.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
const MuxDemux = require('mux-demux')
const env = require('./environment')
const Base = require('./base')
module.exports = class Module extends Base {
constructor(options = {}) {
options.bindReady = false
super(options)
if (options.secure === undefined) {
options.secure = false
}
this.authentication = options.authentication
this.muxes = {}
this.environment = env(options.environment)
this.network = this.environment.network
this.bindReady()
}
async bindReady(dependency = () => Promise.resolve()) {
super.bindReady(() => {
return new Promise(async resolve => {
await this.environment.ready()
await dependency()
resolve()
})
})
}
handleReady() {
super.handleReady()
this.environment.handleModule(this)
this.bindConnectionListeners()
this.bindStreamHandlers()
}
bindEventListeners() {
super.bindEventListeners()
this.on('mux', mux => this.handleMux(mux))
}
bindConnectionListeners() {
this.network.on('connection', c => this.handleConnection(c))
this.network.connections.forEach(c => this.handleConnection(c))
}
bindStreamHandlers() {}
handleConnection(connection) {
if (this.options.secure && !connection.secure) return
if (connection.incoming) {
this.debug('incoming connection %s %s', connection.id, connection.peer)
this.handleIncomingConnection(connection)
} else if (connection.outgoing) {
this.debug('outgoing connection %s %s', connection.id, connection.peer)
this.handleOutgoingConnection(connection)
}
connection
.once('close', () => this.handleConnectionClose(connection))
.once('error', () => this.handleConnectionError(connection))
}
async handleIncomingConnection(connection) {
if (this.authentication) {
const session = await new Promise(resolve => {
const session = this.authentication.sessions[connection.peer]
if (session) resolve(session)
else this.authentication.once(`session.${connection.peer}`, resolve)
})
const mux = this.createMux(connection)
const stream = connection.createStream(session.options.id)
mux.pipe(stream).pipe(mux)
mux.once('error', () => stream.destroy())
stream.once('error', () => mux.destroy())
this.createStreams(mux)
this.emit('mux', mux)
} else {
const mux = this.createMux(connection)
const stream = connection.createStream(this.streamName)
mux.pipe(stream).pipe(mux)
mux.once('error', () => stream.destroy())
stream.once('error', () => mux.destroy())
this.createStreams(mux)
this.emit('mux', mux)
}
}
async handleOutgoingConnection(connection) {
if (this.authentication) {
const session = await new Promise(resolve => {
const session = this.authentication.sessions[connection.peer]
if (session) resolve(session)
else this.authentication.once(`session.${connection.peer}`, resolve)
})
const stream = connection.streams[session.options.id]
if (stream) {
const mux = this.createMux(connection)
mux.once('error', () => stream.destroy())
stream.once('error', () => mux.destroy())
stream.pipe(mux).pipe(stream)
this.emit('mux', mux)
} else {
connection.once(`stream.${session.options.id}`, stream => {
const mux = this.createMux(connection)
mux.once('error', () => stream.destroy())
stream.once('error', () => mux.destroy())
stream.pipe(mux).pipe(stream)
this.emit('mux', mux)
})
}
} else {
connection.once(`stream.${this.streamName}`, stream => {
const mux = this.createMux(connection)
mux.once('error', () => stream.destroy())
stream.once('error', () => mux.destroy())
stream.pipe(mux).pipe(stream)
this.emit('mux', mux)
})
}
}
handleConnectionClose(connection) {
const mux = this.muxes[connection.id]
if (!mux) return
mux.destroy()
this.muxes[connection.id] = null
}
handleConnectionError(connection) {
this.handleConnectionClose(connection)
}
handleMux(mux) {
this.muxes[mux.connection.id] = mux
}
handleStream(stream) {
this.debug('handling a stream %s %s', stream.meta, stream.id)
this.emit('stream', stream)
this.emit(`stream.${stream.meta}`, stream)
}
createMux(connection) {
const mux = new MuxDemux(stream => {
stream.mux = mux
this.handleStream(stream)
})
mux.connection = connection
const { createStream } = mux
mux.createStream = (...args) => {
const stream = createStream.apply(mux, args)
this.debug('created a stream %s %s', stream.meta, stream.id)
stream.mux = mux
return stream
}
return mux
}
createStreams() {}
get discoverable() {
const { discoverable } = this.options
return discoverable || discoverable === undefined
}
get streamName() {
return this.options.streamName || this.label
}
}