This repository was archived by the owner on Dec 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathprotocol.ts
183 lines (165 loc) · 4.23 KB
/
protocol.ts
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
177
178
179
180
181
182
183
import { EventEmitter } from 'events'
import { Config } from '../../config'
import { Peer } from '../peer/peer'
import { BoundProtocol } from './boundprotocol'
import { Sender } from './sender'
export interface ProtocolOptions {
config: Config
/* Handshake timeout in ms (default: 8000) */
timeout?: number
}
export type Message = {
name: string
code: number
// TODO: check semantics of this field
response?: number
// TODO: check if this should be optional
encode?: Function
// TODO: check if this should be optional
decode?: Function
}
/**
* Protocol message
* @typedef {Object} Protocol~Message
* @property {string} name message name
* @property {number} code message code
* @property {response} response code of response message
* @property {boolean} flow true if message includes flow control
* @property {function(...*): *} encode encode message arguments
* @property {function(*): *} decode decodes message payload
*/
/**
* Base class for all wire protocols
* @memberof module:net/protocol
*/
export class Protocol extends EventEmitter {
public config: Config
public timeout: number
public opened: boolean
/**
* Create new protocol
* @param {ProtocolOptions}
*/
constructor(options: ProtocolOptions) {
super()
this.config = options.config
this.timeout = options.timeout ?? 8000
this.opened = false
}
/**
* Opens protocol and any associated dependencies
* @return {Promise}
*/
async open(): Promise<boolean | void> {
this.opened = true
}
/**
* Perform handshake given a sender from subclass.
* @private
* @return {Promise}
*/
async handshake(sender: Sender) {
const status = this.encodeStatus()
sender.sendStatus(status)
return new Promise((resolve, reject) => {
let timeout: any = setTimeout(() => {
timeout = null
reject(new Error(`Handshake timed out after ${this.timeout}ms`))
}, this.timeout)
const handleStatus = (status: any) => {
if (timeout) {
clearTimeout(timeout)
resolve(this.decodeStatus(status))
}
}
if (sender.status) {
handleStatus(sender.status)
} else {
sender.once('status', handleStatus)
}
})
}
/**
* Abstract getter for name of protocol
* @type {string}
*/
get name(): string {
return 'protocol'
}
/**
* Protocol versions supported
* @type {number[]}
*/
get versions(): number[] {
throw new Error('Unimplemented')
}
/**
* Messages defined by this protocol
* @type {Protocol~Message[]}
*/
get messages(): Message[] {
throw new Error('Unimplemented')
}
/**
* Encodes status into status message payload. Must be implemented by subclass.
* @return {Object}
*/
encodeStatus(): any {
throw new Error('Unimplemented')
}
/**
* Decodes status message payload into a status object. Must be implemented
* by subclass.
* @param {Object} status status message payload
* @return {Object}
*/
decodeStatus(_status: any): any {
throw new Error('Unimplemented')
}
/**
* Encodes message into proper format before sending
* @protected
* @param message message definition
* @param {*} args message arguments
* @return {*}
*/
encode(message: Message, args: any): any {
if (message.encode) {
return message.encode(args)
}
return args
}
/**
* Decodes message payload
* @protected
* @param message message definition
* @param {*} payload message payload
* @param {BoundProtocol} bound reference to bound protocol
* @return {*}
*/
decode(message: Message, payload: any): any {
if (message.decode) {
return message.decode(payload)
}
return payload
}
/**
* Binds this protocol to a given peer using the specified sender to handle
* message communication.
* @param {Peer} peer peer
* @param {Sender} sender sender
* @return {Promise}
*/
async bind(peer: Peer, sender: Sender): Promise<BoundProtocol> {
const bound = new BoundProtocol({
config: this.config,
protocol: this,
peer: peer,
sender: sender,
})
await bound.handshake(sender)
//@ts-ignore TODO: evaluate this line
peer[this.name] = bound
return bound
}
}