-
Notifications
You must be signed in to change notification settings - Fork 1
/
fast-ws.js
167 lines (146 loc) · 4.53 KB
/
fast-ws.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
const Replicator = require('replicator')
const BasicProtocol = require('./basic')
const ServerError = require('../errors')
const PING = '\x0F'
const PONG = '\x0E'
const DATA_START = '\x01'
const DATA_END = '\x02'
const EVENT = '\x05'
const RESPONSE = '\x06'
const IDLE = '\x16'
const eventId = (str) => str.split('').reduce((sum, char, index) => sum + char.charCodeAt(0) * (index + 1), 0).toString(16)
class Parser {
constructor (options) {
this.replicator = new Replicator(options)
}
parse (payload) {
if (payload[0] === DATA_START && payload[payload.length - 1] === DATA_END) {
return { type: 'message', data: this.replicator.decode(payload.slice(1, -1)) }
} else if (payload[0] === PING) {
return { type: 'ping', data: Number(payload.slice(1)) }
} else if (payload[0] === PONG) {
return { type: 'pong', data: new Date() - Number(payload.slice(1)) }
} else if (payload[0] === EVENT) {
const eventSplitIndex = payload.indexOf(IDLE)
const replySplitIndex = payload.indexOf(DATA_START)
if (eventSplitIndex === -1 || replySplitIndex === -1) {
throw new ServerError({ code: 'WS_INVALID_PAYLOAD' })
}
const event = payload.slice(1, eventSplitIndex)
const replyId = payload.slice(eventSplitIndex + 1, replySplitIndex)
const dataPayload = payload.slice(replySplitIndex)
return {
type: 'event',
name: event,
reply_id: replyId,
data: this.parse(dataPayload).data
}
} else {
throw new Error('Invalid payload')
}
}
stringify (data, type = 'message') {
if (type === 'reply') {
return RESPONSE + data.replyId + this.stringify(data.data)
} else if (type === 'event') {
return EVENT + eventId(data.event) + this.stringify(data.data)
} else if (type === 'ping') {
return PING + new Date().valueOf().toString()
} else if (type === 'pong') {
return PONG + data.toString()
} else if (type === 'message') {
return DATA_START + this.replicator.encode(data) + DATA_END
} else {
throw new Error('Invalid type')
}
}
}
class WSClient extends BasicProtocol.WSClient {
constructor (connection, parser) {
super(connection, { parser })
}
onOpen (socket) {
super.onOpen(socket)
this.doSend('\x00\x02', 0, 0)
}
reply (replyId, data) {
this.doSend(this.parser.stringify({ replyId, data }, 'reply'))
}
incomingPacket (payload, isBinary) {
if (isBinary) {
super.emit('binary', payload)
} else {
const data = this.parser.parse(payload.toString(), isBinary)
if (data.type === 'event') {
data.reply = this.reply.bind(this, data.reply_id)
super.emit(data.name, data)
} else {
if (data.type === 'ping') {
this.doSend(this.parser.stringify(data.data, 'pong'))
}
super.emit(data.type, data.data)
}
}
}
ping () {
this.doSend(this.parser.stringify(null, 'ping'))
}
on (event, listener) {
if (this.internalEvents.includes(event)) {
super.on(event, listener)
} else {
super.on(eventId(event), listener)
}
}
once (event, listener) {
if (this.internalEvents.includes(event)) {
super.once(event, listener)
} else {
super.once(eventId(event), listener)
}
}
addListener (event, listener) {
if (this.internalEvents.includes(event)) {
super.addListener(event, listener)
} else {
super.addListener(eventId(event), listener)
}
}
off (event, listener) {
if (this.internalEvents.includes(event)) {
super.off(event, listener)
} else {
super.off(eventId(event), listener)
}
}
removeListener (event, listener) {
if (this.internalEvents.includes(event)) {
super.removeListener(event, listener)
} else {
super.removeListener(eventId(event), listener)
}
}
removeAllListener (event) {
if (this.internalEvents.includes(event)) {
super.removeAllListener(event)
} else {
super.removeAllListener(eventId(event))
}
}
emit (event, data, compress = true) {
return this.doSend(this.parser.stringify({ event, data }, 'event'), false, compress)
}
emitToChannel (channel, event, data, compress = true) {
this.doPublish(channel, this.parser.stringify({ event, data }, 'event'), false, compress)
}
}
class WSProtocol extends BasicProtocol {
constructor (options = {}) {
super()
this.parser = new Parser(options.parserOptions)
}
newClient (connection) {
return new WSClient(connection, this.parser)
}
}
module.exports = WSProtocol