-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
options.js
88 lines (79 loc) · 2.14 KB
/
options.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
/**
* Default options.
*/
/**
* @typedef {Object} Options
* @property {Function} [createWebSocket=url => new WebSocket(url)] - custom function for WebSocket construction.
*
* @property {Function} [packMessage=noop] - packs message for sending. For example, `data => JSON.stringify(data)`.
*
* @property {Function} [unpackMessage=noop] - unpacks received message. For example, `data => JSON.parse(data)`.
*
* @property {Function} [attachRequestId=noop] - injects request id into data.
* For example, `(data, requestId) => Object.assign({requestId}, data)`.
*
* @property {Function} [extractRequestId=noop] - extracts request id from received data.
* For example, `data => data.requestId`.
*
* @property {Function} [extractMessageData=event => event.data] - extracts data from event object.
*
* @property {Number} timeout=0 - timeout for opening connection and sending messages.
*
* @property {Number} connectionTimeout=0 - special timeout for opening connection, by default equals to `timeout`.
*
* @defaults
* please see [options.js](https://github.com/vitalets/websocket-as-promised/blob/master/src/options.js)
*/
module.exports = {
/**
* See {@link Options.createWebSocket}
*
* @param {String} url
* @returns {WebSocket}
*/
createWebSocket: url => new WebSocket(url),
/**
* See {@link Options.packMessage}
*
* @param {*} data
* @returns {String|ArrayBuffer|Blob}
*/
packMessage: null,
/**
* See {@link Options.unpackMessage}
*
* @param {String|ArrayBuffer|Blob} data
* @returns {*}
*/
unpackMessage: null,
/**
* See {@link Options.attachRequestId}
*
* @param {*} data
* @param {String|Number} requestId
* @returns {*}
*/
attachRequestId: null,
/**
* See {@link Options.extractRequestId}
*
* @param {*} data
* @returns {String|Number|undefined}
*/
extractRequestId: null,
/**
* See {@link Options.extractMessageData}
*
* @param {*} event
* @returns {*}
*/
extractMessageData: event => event.data,
/**
* See {@link Options.timeout}
*/
timeout: 0,
/**
* See {@link Options.connectionTimeout}
*/
connectionTimeout: 0,
};