-
Notifications
You must be signed in to change notification settings - Fork 380
/
Copy pathsocket-wrapper-factory.ts
44 lines (37 loc) · 1.63 KB
/
socket-wrapper-factory.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
import * as binaryMessageBuilder from '@deepstream/protobuf/dist/src/message-builder'
import * as binaryMessageParser from '@deepstream/protobuf/dist/src/message-parser'
import { ParseResult, Message } from '../../../constants'
import { WebSocketServerConfig } from '../../base/connection-endpoint'
import { SocketConnectionEndpoint, DeepstreamServices } from '@deepstream/types'
import { WSSocketWrapper } from '../../base/socket-wrapper'
export class WSBinarySocketWrapper extends WSSocketWrapper<Uint8Array> {
public socketType = 'wsBinary'
public getAckMessage (message: Message): Uint8Array {
return binaryMessageBuilder.getMessage(message, true)
}
public getMessage (message: Message): Uint8Array {
return binaryMessageBuilder.getMessage(message, false)
}
public parseMessage (message: ArrayBuffer): ParseResult[] {
if (typeof message === 'string') {
this.invalidTypeReceived()
return []
}
/* we copy the underlying buffer (since a shallow reference won't be safe
* outside of the callback)
* the copy could be avoided if we make sure not to store references to the
* raw buffer within the message
*/
return binaryMessageParser.parse(Buffer.from(Buffer.from(message)))
}
public parseData (message: Message): true | Error {
return binaryMessageParser.parseData(message)
}
}
export const createWSSocketWrapper = function (
socket: any,
handshakeData: any,
services: DeepstreamServices,
config: WebSocketServerConfig,
connectionEndpoint: SocketConnectionEndpoint
) { return new WSBinarySocketWrapper(socket, handshakeData, services, config, connectionEndpoint, true) }