websocket-ts 1.1.1 → 2.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +157 -93
- package/dist/cjs/src/backoff/backoff.d.ts +24 -0
- package/dist/cjs/src/backoff/backoff.d.ts.map +1 -0
- package/{lib → dist/cjs/src}/backoff/backoff.js.map +1 -1
- package/dist/cjs/src/backoff/constantbackoff.d.ts +18 -0
- package/dist/cjs/src/backoff/constantbackoff.d.ts.map +1 -0
- package/dist/cjs/src/backoff/constantbackoff.js +43 -0
- package/dist/cjs/src/backoff/constantbackoff.js.map +1 -0
- package/dist/cjs/src/backoff/exponentialbackoff.d.ts +48 -0
- package/dist/cjs/src/backoff/exponentialbackoff.d.ts.map +1 -0
- package/dist/cjs/src/backoff/exponentialbackoff.js +81 -0
- package/dist/cjs/src/backoff/exponentialbackoff.js.map +1 -0
- package/dist/cjs/src/backoff/linearbackoff.d.ts +51 -0
- package/dist/cjs/src/backoff/linearbackoff.d.ts.map +1 -0
- package/dist/cjs/src/backoff/linearbackoff.js +89 -0
- package/dist/cjs/src/backoff/linearbackoff.js.map +1 -0
- package/dist/cjs/src/index.d.ts +14 -0
- package/dist/cjs/src/index.d.ts.map +1 -0
- package/dist/cjs/src/index.js +20 -0
- package/dist/cjs/src/index.js.map +1 -0
- package/dist/cjs/src/queue/array_queue.d.ts +17 -0
- package/dist/cjs/src/queue/array_queue.d.ts.map +1 -0
- package/dist/cjs/src/queue/array_queue.js +36 -0
- package/dist/cjs/src/queue/array_queue.js.map +1 -0
- package/dist/cjs/src/queue/queue.d.ts +41 -0
- package/dist/cjs/src/queue/queue.d.ts.map +1 -0
- package/{lib/buffer/buffer.js → dist/cjs/src/queue/queue.js} +1 -1
- package/dist/cjs/src/queue/queue.js.map +1 -0
- package/dist/cjs/src/queue/ring_queue.d.ts +20 -0
- package/dist/cjs/src/queue/ring_queue.d.ts.map +1 -0
- package/dist/cjs/src/queue/ring_queue.js +57 -0
- package/dist/cjs/src/queue/ring_queue.js.map +1 -0
- package/dist/cjs/src/websocket.d.ts +206 -0
- package/dist/cjs/src/websocket.d.ts.map +1 -0
- package/dist/cjs/src/websocket.js +434 -0
- package/dist/cjs/src/websocket.js.map +1 -0
- package/dist/cjs/src/websocket_buffer.d.ts +16 -0
- package/dist/cjs/src/websocket_buffer.d.ts.map +1 -0
- package/dist/cjs/src/websocket_buffer.js +3 -0
- package/dist/cjs/src/websocket_buffer.js.map +1 -0
- package/dist/cjs/src/websocket_builder.d.ts +147 -0
- package/dist/cjs/src/websocket_builder.d.ts.map +1 -0
- package/dist/cjs/src/websocket_builder.js +263 -0
- package/dist/cjs/src/websocket_builder.js.map +1 -0
- package/dist/cjs/src/websocket_event.d.ts +67 -0
- package/dist/cjs/src/websocket_event.d.ts.map +1 -0
- package/dist/cjs/src/websocket_event.js +22 -0
- package/dist/cjs/src/websocket_event.js.map +1 -0
- package/dist/cjs/src/websocket_options.d.ts +21 -0
- package/dist/cjs/src/websocket_options.d.ts.map +1 -0
- package/dist/cjs/src/websocket_options.js +3 -0
- package/dist/cjs/src/websocket_options.js.map +1 -0
- package/dist/cjs/src/websocket_retry_options.d.ts +20 -0
- package/dist/cjs/src/websocket_retry_options.d.ts.map +1 -0
- package/dist/cjs/src/websocket_retry_options.js +3 -0
- package/dist/cjs/src/websocket_retry_options.js.map +1 -0
- package/dist/esm/src/backoff/backoff.d.ts +24 -0
- package/dist/esm/src/backoff/backoff.d.ts.map +1 -0
- package/dist/esm/src/backoff/backoff.js +2 -0
- package/dist/esm/src/backoff/backoff.js.map +1 -0
- package/dist/esm/src/backoff/constantbackoff.d.ts +18 -0
- package/dist/esm/src/backoff/constantbackoff.d.ts.map +1 -0
- package/dist/esm/src/backoff/constantbackoff.js +30 -0
- package/dist/esm/src/backoff/constantbackoff.js.map +1 -0
- package/dist/esm/src/backoff/exponentialbackoff.d.ts +48 -0
- package/dist/esm/src/backoff/exponentialbackoff.d.ts.map +1 -0
- package/dist/esm/src/backoff/exponentialbackoff.js +68 -0
- package/dist/esm/src/backoff/exponentialbackoff.js.map +1 -0
- package/dist/esm/src/backoff/linearbackoff.d.ts +51 -0
- package/dist/esm/src/backoff/linearbackoff.d.ts.map +1 -0
- package/dist/esm/src/backoff/linearbackoff.js +76 -0
- package/dist/esm/src/backoff/linearbackoff.js.map +1 -0
- package/dist/esm/src/index.d.ts +14 -0
- package/dist/esm/src/index.d.ts.map +1 -0
- package/dist/esm/src/index.js +9 -0
- package/dist/esm/src/index.js.map +1 -0
- package/dist/esm/src/queue/array_queue.d.ts +17 -0
- package/dist/esm/src/queue/array_queue.d.ts.map +1 -0
- package/dist/esm/src/queue/array_queue.js +31 -0
- package/dist/esm/src/queue/array_queue.js.map +1 -0
- package/dist/esm/src/queue/queue.d.ts +41 -0
- package/dist/esm/src/queue/queue.d.ts.map +1 -0
- package/dist/esm/src/queue/queue.js +2 -0
- package/dist/esm/src/queue/queue.js.map +1 -0
- package/dist/esm/src/queue/ring_queue.d.ts +20 -0
- package/dist/esm/src/queue/ring_queue.d.ts.map +1 -0
- package/dist/esm/src/queue/ring_queue.js +52 -0
- package/dist/esm/src/queue/ring_queue.js.map +1 -0
- package/dist/esm/src/websocket.d.ts +206 -0
- package/dist/esm/src/websocket.d.ts.map +1 -0
- package/dist/esm/src/websocket.js +356 -0
- package/dist/esm/src/websocket.js.map +1 -0
- package/dist/esm/src/websocket_buffer.d.ts +16 -0
- package/dist/esm/src/websocket_buffer.d.ts.map +1 -0
- package/dist/esm/src/websocket_buffer.js +2 -0
- package/dist/esm/src/websocket_buffer.js.map +1 -0
- package/dist/esm/src/websocket_builder.d.ts +147 -0
- package/dist/esm/src/websocket_builder.d.ts.map +1 -0
- package/dist/esm/src/websocket_builder.js +213 -0
- package/dist/esm/src/websocket_builder.js.map +1 -0
- package/dist/esm/src/websocket_event.d.ts +67 -0
- package/dist/esm/src/websocket_event.d.ts.map +1 -0
- package/dist/esm/src/websocket_event.js +19 -0
- package/dist/esm/src/websocket_event.js.map +1 -0
- package/dist/esm/src/websocket_options.d.ts +21 -0
- package/dist/esm/src/websocket_options.d.ts.map +1 -0
- package/dist/esm/src/websocket_options.js +2 -0
- package/dist/esm/src/websocket_options.js.map +1 -0
- package/dist/esm/src/websocket_retry_options.d.ts +20 -0
- package/dist/esm/src/websocket_retry_options.d.ts.map +1 -0
- package/dist/esm/src/websocket_retry_options.js +2 -0
- package/dist/esm/src/websocket_retry_options.js.map +1 -0
- package/package.json +27 -14
- package/src/backoff/backoff.ts +21 -13
- package/src/backoff/constantbackoff.ts +29 -11
- package/src/backoff/exponentialbackoff.ts +68 -28
- package/src/backoff/linearbackoff.ts +77 -26
- package/src/index.ts +23 -9
- package/src/queue/array_queue.ts +41 -0
- package/src/queue/queue.ts +46 -0
- package/src/queue/ring_queue.ts +69 -0
- package/src/websocket.ts +463 -138
- package/src/websocket_buffer.ts +18 -0
- package/src/websocket_builder.ts +274 -0
- package/src/websocket_event.ts +88 -0
- package/src/websocket_options.ts +23 -0
- package/src/websocket_retry_options.ts +22 -0
- package/{tsconfig.json → tsconfig.cjs.json} +8 -8
- package/tsconfig.esm.json +71 -0
- package/jest.config.js +0 -9
- package/lib/backoff/backoff.d.ts +0 -18
- package/lib/backoff/backoff.d.ts.map +0 -1
- package/lib/backoff/constantbackoff.d.ts +0 -11
- package/lib/backoff/constantbackoff.d.ts.map +0 -1
- package/lib/backoff/constantbackoff.js +0 -20
- package/lib/backoff/constantbackoff.js.map +0 -1
- package/lib/backoff/exponentialbackoff.d.ts +0 -22
- package/lib/backoff/exponentialbackoff.d.ts.map +0 -1
- package/lib/backoff/exponentialbackoff.js +0 -35
- package/lib/backoff/exponentialbackoff.js.map +0 -1
- package/lib/backoff/linearbackoff.d.ts +0 -19
- package/lib/backoff/linearbackoff.d.ts.map +0 -1
- package/lib/backoff/linearbackoff.js +0 -34
- package/lib/backoff/linearbackoff.js.map +0 -1
- package/lib/buffer/buffer.d.ts +0 -41
- package/lib/buffer/buffer.d.ts.map +0 -1
- package/lib/buffer/buffer.js.map +0 -1
- package/lib/buffer/lrubuffer.d.ts +0 -24
- package/lib/buffer/lrubuffer.d.ts.map +0 -1
- package/lib/buffer/lrubuffer.js +0 -76
- package/lib/buffer/lrubuffer.js.map +0 -1
- package/lib/buffer/timebuffer.d.ts +0 -25
- package/lib/buffer/timebuffer.d.ts.map +0 -1
- package/lib/buffer/timebuffer.js +0 -92
- package/lib/buffer/timebuffer.js.map +0 -1
- package/lib/index.d.ts +0 -10
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js +0 -22
- package/lib/index.js.map +0 -1
- package/lib/websocket.d.ts +0 -47
- package/lib/websocket.d.ts.map +0 -1
- package/lib/websocket.js +0 -120
- package/lib/websocket.js.map +0 -1
- package/lib/websocketBuilder.d.ts +0 -32
- package/lib/websocketBuilder.d.ts.map +0 -1
- package/lib/websocketBuilder.js +0 -68
- package/lib/websocketBuilder.js.map +0 -1
- package/src/buffer/buffer.ts +0 -45
- package/src/buffer/lrubuffer.ts +0 -80
- package/src/buffer/timebuffer.ts +0 -105
- package/src/websocketBuilder.ts +0 -98
- package/test/backoff/constantbackoff.test.ts +0 -21
- package/test/backoff/exponentialbackoff.test.ts +0 -13
- package/test/backoff/linearbackoff.test.ts +0 -32
- package/test/buffer/common.ts +0 -10
- package/test/buffer/lrubuffer.test.ts +0 -128
- package/test/buffer/timebuffer.test.ts +0 -115
- package/test/websocket.test.ts +0 -341
- package/test/websocketBuilder.test.ts +0 -36
- /package/{lib → dist/cjs/src}/backoff/backoff.js +0 -0
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import { WebsocketEvent, } from "./websocket_event";
|
|
2
|
+
import { Websocket } from "./websocket";
|
|
3
|
+
/**
|
|
4
|
+
* Builder for websockets.
|
|
5
|
+
*/
|
|
6
|
+
export class WebsocketBuilder {
|
|
7
|
+
/**
|
|
8
|
+
* Creates a new WebsocketBuilder.
|
|
9
|
+
*
|
|
10
|
+
* @param url the url to connect to
|
|
11
|
+
*/
|
|
12
|
+
constructor(url) {
|
|
13
|
+
this._url = url;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Getter for the url.
|
|
17
|
+
*
|
|
18
|
+
* @returns the url
|
|
19
|
+
*/
|
|
20
|
+
get url() {
|
|
21
|
+
return this._url;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Adds protocols to the websocket. Subsequent calls to this method will override the previously set protocols.
|
|
25
|
+
*
|
|
26
|
+
* @param protocols the protocols to add
|
|
27
|
+
*/
|
|
28
|
+
withProtocols(protocols) {
|
|
29
|
+
this._protocols = protocols;
|
|
30
|
+
return this;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Getter for the protocols.
|
|
34
|
+
*
|
|
35
|
+
* @returns the protocols, undefined if no protocols have been set
|
|
36
|
+
*/
|
|
37
|
+
get protocols() {
|
|
38
|
+
return this._protocols;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Sets the maximum number of retries before giving up. No limit if undefined.
|
|
42
|
+
*
|
|
43
|
+
* @param maxRetries the maximum number of retries before giving up
|
|
44
|
+
*/
|
|
45
|
+
withMaxRetries(maxRetries) {
|
|
46
|
+
var _a;
|
|
47
|
+
this._options = Object.assign(Object.assign({}, this._options), { retry: Object.assign(Object.assign({}, (_a = this._options) === null || _a === void 0 ? void 0 : _a.retry), { maxRetries }) });
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Getter for the maximum number of retries before giving up.
|
|
52
|
+
*
|
|
53
|
+
* @returns the maximum number of retries before giving up, undefined if no maximum has been set
|
|
54
|
+
*/
|
|
55
|
+
get maxRetries() {
|
|
56
|
+
var _a, _b;
|
|
57
|
+
return (_b = (_a = this._options) === null || _a === void 0 ? void 0 : _a.retry) === null || _b === void 0 ? void 0 : _b.maxRetries;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Sets wether to reconnect immediately after a connection has been lost, ignoring the backoff strategy for the first retry.
|
|
61
|
+
*
|
|
62
|
+
* @param instantReconnect wether to reconnect immediately after a connection has been lost
|
|
63
|
+
*/
|
|
64
|
+
withInstantReconnect(instantReconnect) {
|
|
65
|
+
var _a;
|
|
66
|
+
this._options = Object.assign(Object.assign({}, this._options), { retry: Object.assign(Object.assign({}, (_a = this._options) === null || _a === void 0 ? void 0 : _a.retry), { instantReconnect }) });
|
|
67
|
+
return this;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Getter for wether to reconnect immediately after a connection has been lost, ignoring the backoff strategy for the first retry.
|
|
71
|
+
*
|
|
72
|
+
* @returns wether to reconnect immediately after a connection has been lost, undefined if no value has been set
|
|
73
|
+
*/
|
|
74
|
+
get instantReconnect() {
|
|
75
|
+
var _a, _b;
|
|
76
|
+
return (_b = (_a = this._options) === null || _a === void 0 ? void 0 : _a.retry) === null || _b === void 0 ? void 0 : _b.instantReconnect;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Adds a backoff to the websocket. Subsequent calls to this method will override the previously set backoff.
|
|
80
|
+
*
|
|
81
|
+
* @param backoff the backoff to add
|
|
82
|
+
*/
|
|
83
|
+
withBackoff(backoff) {
|
|
84
|
+
var _a;
|
|
85
|
+
this._options = Object.assign(Object.assign({}, this._options), { retry: Object.assign(Object.assign({}, (_a = this._options) === null || _a === void 0 ? void 0 : _a.retry), { backoff }) });
|
|
86
|
+
return this;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Getter for the backoff.
|
|
90
|
+
*
|
|
91
|
+
* @returns the backoff, undefined if no backoff has been set
|
|
92
|
+
*/
|
|
93
|
+
get backoff() {
|
|
94
|
+
var _a, _b;
|
|
95
|
+
return (_b = (_a = this._options) === null || _a === void 0 ? void 0 : _a.retry) === null || _b === void 0 ? void 0 : _b.backoff;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Adds a buffer to the websocket. Subsequent calls to this method will override the previously set buffer.
|
|
99
|
+
*
|
|
100
|
+
* @param buffer the buffer to add
|
|
101
|
+
*/
|
|
102
|
+
withBuffer(buffer) {
|
|
103
|
+
this._options = Object.assign(Object.assign({}, this._options), { buffer });
|
|
104
|
+
return this;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Getter for the buffer.
|
|
108
|
+
*
|
|
109
|
+
* @returns the buffer, undefined if no buffer has been set
|
|
110
|
+
*/
|
|
111
|
+
get buffer() {
|
|
112
|
+
var _a;
|
|
113
|
+
return (_a = this._options) === null || _a === void 0 ? void 0 : _a.buffer;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Adds an 'open' event listener to the websocket. Subsequent calls to this method will add additional listeners that will be
|
|
117
|
+
* called in the order they were added.
|
|
118
|
+
*
|
|
119
|
+
* @param listener the listener to add
|
|
120
|
+
* @param options the listener options
|
|
121
|
+
*/
|
|
122
|
+
onOpen(listener, options) {
|
|
123
|
+
this.addListener(WebsocketEvent.open, listener, options);
|
|
124
|
+
return this;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Adds an 'close' event listener to the websocket. Subsequent calls to this method will add additional listeners that will be
|
|
128
|
+
* called in the order they were added.
|
|
129
|
+
*
|
|
130
|
+
* @param listener the listener to add
|
|
131
|
+
* @param options the listener options
|
|
132
|
+
*/
|
|
133
|
+
onClose(listener, options) {
|
|
134
|
+
this.addListener(WebsocketEvent.close, listener, options);
|
|
135
|
+
return this;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Adds an 'error' event listener to the websocket. Subsequent calls to this method will add additional listeners that will be
|
|
139
|
+
* called in the order they were added.
|
|
140
|
+
*
|
|
141
|
+
* @param listener the listener to add
|
|
142
|
+
* @param options the listener options
|
|
143
|
+
*/
|
|
144
|
+
onError(listener, options) {
|
|
145
|
+
this.addListener(WebsocketEvent.error, listener, options);
|
|
146
|
+
return this;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Adds an 'message' event listener to the websocket. Subsequent calls to this method will add additional listeners that will be
|
|
150
|
+
* called in the order they were added.
|
|
151
|
+
*
|
|
152
|
+
* @param listener the listener to add
|
|
153
|
+
* @param options the listener options
|
|
154
|
+
*/
|
|
155
|
+
onMessage(listener, options) {
|
|
156
|
+
this.addListener(WebsocketEvent.message, listener, options);
|
|
157
|
+
return this;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Adds an 'retry' event listener to the websocket. Subsequent calls to this method will add additional listeners that will be
|
|
161
|
+
* called in the order they were added.
|
|
162
|
+
*
|
|
163
|
+
* @param listener the listener to add
|
|
164
|
+
* @param options the listener options
|
|
165
|
+
*/
|
|
166
|
+
onRetry(listener, options) {
|
|
167
|
+
this.addListener(WebsocketEvent.retry, listener, options);
|
|
168
|
+
return this;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Adds an 'reconnect' event listener to the websocket. Subsequent calls to this method will add additional listeners that will be
|
|
172
|
+
* called in the order they were added.
|
|
173
|
+
*
|
|
174
|
+
* @param listener the listener to add
|
|
175
|
+
* @param options the listener options
|
|
176
|
+
*/
|
|
177
|
+
onReconnect(listener, options) {
|
|
178
|
+
this.addListener(WebsocketEvent.reconnect, listener, options);
|
|
179
|
+
return this;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Builds the websocket.
|
|
183
|
+
*
|
|
184
|
+
* @return a new websocket, with the set options
|
|
185
|
+
*/
|
|
186
|
+
build() {
|
|
187
|
+
return new Websocket(this._url, this._protocols, this._options); // instantiate the websocket with the set options
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Adds an event listener to the options.
|
|
191
|
+
*
|
|
192
|
+
* @param event the event to add the listener to
|
|
193
|
+
* @param listener the listener to add
|
|
194
|
+
* @param options the listener options
|
|
195
|
+
*/
|
|
196
|
+
addListener(event, listener, options) {
|
|
197
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w;
|
|
198
|
+
this._options = Object.assign(Object.assign({}, this._options), { listeners: {
|
|
199
|
+
open: (_c = (_b = (_a = this._options) === null || _a === void 0 ? void 0 : _a.listeners) === null || _b === void 0 ? void 0 : _b.open) !== null && _c !== void 0 ? _c : [],
|
|
200
|
+
close: (_f = (_e = (_d = this._options) === null || _d === void 0 ? void 0 : _d.listeners) === null || _e === void 0 ? void 0 : _e.close) !== null && _f !== void 0 ? _f : [],
|
|
201
|
+
error: (_j = (_h = (_g = this._options) === null || _g === void 0 ? void 0 : _g.listeners) === null || _h === void 0 ? void 0 : _h.error) !== null && _j !== void 0 ? _j : [],
|
|
202
|
+
message: (_m = (_l = (_k = this._options) === null || _k === void 0 ? void 0 : _k.listeners) === null || _l === void 0 ? void 0 : _l.message) !== null && _m !== void 0 ? _m : [],
|
|
203
|
+
retry: (_q = (_p = (_o = this._options) === null || _o === void 0 ? void 0 : _o.listeners) === null || _p === void 0 ? void 0 : _p.retry) !== null && _q !== void 0 ? _q : [],
|
|
204
|
+
reconnect: (_t = (_s = (_r = this._options) === null || _r === void 0 ? void 0 : _r.listeners) === null || _s === void 0 ? void 0 : _s.reconnect) !== null && _t !== void 0 ? _t : [],
|
|
205
|
+
[event]: [
|
|
206
|
+
...((_w = (_v = (_u = this._options) === null || _u === void 0 ? void 0 : _u.listeners) === null || _v === void 0 ? void 0 : _v[event]) !== null && _w !== void 0 ? _w : []),
|
|
207
|
+
{ listener, options },
|
|
208
|
+
],
|
|
209
|
+
} });
|
|
210
|
+
return this;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
//# sourceMappingURL=websocket_builder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"websocket_builder.js","sourceRoot":"","sources":["../../../src/websocket_builder.ts"],"names":[],"mappings":"AACA,OAAO,EACL,cAAc,GAGf,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAIx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C,mCAAI,EAAE,CAAC;oBAC5C,EAAE,QAAQ,EAAE,OAAO,EAAE;iBACtB;aACF,GACF,CAAC;QACF,OAAO,IAAI,CAAC;IACd,CAAC;CACF"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { Websocket } from "./websocket";
|
|
2
|
+
/**
|
|
3
|
+
* Events that can be fired by the websocket.
|
|
4
|
+
*/
|
|
5
|
+
export declare enum WebsocketEvent {
|
|
6
|
+
/** Fired when the connection is opened. */
|
|
7
|
+
open = "open",
|
|
8
|
+
/** Fired when the connection is closed. */
|
|
9
|
+
close = "close",
|
|
10
|
+
/** Fired when the connection has been closed because of an error, such as when some data couldn't be sent. */
|
|
11
|
+
error = "error",
|
|
12
|
+
/** Fired when a message is received. */
|
|
13
|
+
message = "message",
|
|
14
|
+
/** Fired when the websocket tries to reconnect after a connection loss. */
|
|
15
|
+
retry = "retry",
|
|
16
|
+
/** Fired when the websocket successfully reconnects after a connection loss. */
|
|
17
|
+
reconnect = "reconnect"
|
|
18
|
+
}
|
|
19
|
+
/***
|
|
20
|
+
* Details/properties of a retry-event.
|
|
21
|
+
*/
|
|
22
|
+
export type RetryEventDetail = {
|
|
23
|
+
/** Number of retries that have been made since the connection was lost. */
|
|
24
|
+
readonly retries: number;
|
|
25
|
+
/** Time (ms) waited since the last connection-retry. */
|
|
26
|
+
readonly backoff: number;
|
|
27
|
+
/** Timestamp of when the connection was lost or undefined if the connection has never been established. */
|
|
28
|
+
readonly lastConnection: Date | undefined;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Properties of a reconnect-event.
|
|
32
|
+
*/
|
|
33
|
+
export type ReconnectEventDetail = Omit<RetryEventDetail, "backoff">;
|
|
34
|
+
/**
|
|
35
|
+
* Maps websocket events to their corresponding event.
|
|
36
|
+
*/
|
|
37
|
+
export type WebsocketEventMap = {
|
|
38
|
+
[WebsocketEvent.open]: Event;
|
|
39
|
+
[WebsocketEvent.close]: CloseEvent;
|
|
40
|
+
[WebsocketEvent.error]: Event;
|
|
41
|
+
[WebsocketEvent.message]: MessageEvent;
|
|
42
|
+
[WebsocketEvent.retry]: CustomEvent<RetryEventDetail>;
|
|
43
|
+
[WebsocketEvent.reconnect]: CustomEvent<ReconnectEventDetail>;
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Listener for websocket events.
|
|
47
|
+
* */
|
|
48
|
+
export type WebsocketEventListener<K extends WebsocketEvent> = (instance: Websocket, ev: WebsocketEventMap[K]) => unknown;
|
|
49
|
+
export type WebsocketEventListenerParams<K extends WebsocketEvent> = Parameters<WebsocketEventListener<K>>;
|
|
50
|
+
/**
|
|
51
|
+
* Options for websocket events.
|
|
52
|
+
*/
|
|
53
|
+
export type WebsocketEventListenerOptions = EventListenerOptions & AddEventListenerOptions;
|
|
54
|
+
/**
|
|
55
|
+
* Listener for websocket events with options.
|
|
56
|
+
*/
|
|
57
|
+
export type WebsocketEventListenerWithOptions<K extends WebsocketEvent> = {
|
|
58
|
+
readonly listener: WebsocketEventListener<K>;
|
|
59
|
+
readonly options?: WebsocketEventListenerOptions;
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* Maps websocket events to their corresponding event-listeners.
|
|
63
|
+
*/
|
|
64
|
+
export type WebsocketEventListeners = {
|
|
65
|
+
[K in WebsocketEvent]: WebsocketEventListenerWithOptions<K>[];
|
|
66
|
+
};
|
|
67
|
+
//# sourceMappingURL=websocket_event.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"websocket_event.d.ts","sourceRoot":"","sources":["../../../src/websocket_event.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC;;GAEG;AACH,oBAAY,cAAc;IACxB,2CAA2C;IAC3C,IAAI,SAAS;IAEb,2CAA2C;IAC3C,KAAK,UAAU;IAEf,8GAA8G;IAC9G,KAAK,UAAU;IAEf,wCAAwC;IACxC,OAAO,YAAY;IAEnB,2EAA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AAI;IACxE,QAAQ,CAAC,QAAQ,EAAE,sBAAsB,CAAC,CAAC,CAAC,CAAC;IAC7C,QAAQ,CAAC,OAAO,CAAC,EAAE,6BAA6B,CAAC;CAClD,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,uBAAuB,GAAG;KACnC,CAAC,IAAI,cAAc,GAAG,iCAAiC,CAAC,CAAC,CAAC,EAAE;CAC9D,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Events that can be fired by the websocket.
|
|
3
|
+
*/
|
|
4
|
+
export var WebsocketEvent;
|
|
5
|
+
(function (WebsocketEvent) {
|
|
6
|
+
/** Fired when the connection is opened. */
|
|
7
|
+
WebsocketEvent["open"] = "open";
|
|
8
|
+
/** Fired when the connection is closed. */
|
|
9
|
+
WebsocketEvent["close"] = "close";
|
|
10
|
+
/** Fired when the connection has been closed because of an error, such as when some data couldn't be sent. */
|
|
11
|
+
WebsocketEvent["error"] = "error";
|
|
12
|
+
/** Fired when a message is received. */
|
|
13
|
+
WebsocketEvent["message"] = "message";
|
|
14
|
+
/** Fired when the websocket tries to reconnect after a connection loss. */
|
|
15
|
+
WebsocketEvent["retry"] = "retry";
|
|
16
|
+
/** Fired when the websocket successfully reconnects after a connection loss. */
|
|
17
|
+
WebsocketEvent["reconnect"] = "reconnect";
|
|
18
|
+
})(WebsocketEvent || (WebsocketEvent = {}));
|
|
19
|
+
//# sourceMappingURL=websocket_event.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"websocket_event.js","sourceRoot":"","sources":["../../../src/websocket_event.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,MAAM,CAAN,IAAY,cAkBX;AAlBD,WAAY,cAAc;IACxB,2CAA2C;IAC3C,+BAAa,CAAA;IAEb,2CAA2C;IAC3C,iCAAe,CAAA;IAEf,8GAA8G;IAC9G,iCAAe,CAAA;IAEf,wCAAwC;IACxC,qCAAmB,CAAA;IAEnB,2EAA2E;IAC3E,iCAAe,CAAA;IAEf,gFAAgF;IAChF,yCAAuB,CAAA;AACzB,CAAC,EAlBW,cAAc,KAAd,cAAc,QAkBzB"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { WebsocketBuffer } from "./websocket_buffer";
|
|
2
|
+
import { WebsocketConnectionRetryOptions } from "./websocket_retry_options";
|
|
3
|
+
import { WebsocketEventListeners } from "./websocket_event";
|
|
4
|
+
/**
|
|
5
|
+
* Options that can be passed to the Websocket constructor.
|
|
6
|
+
*/
|
|
7
|
+
export interface WebsocketOptions {
|
|
8
|
+
/**
|
|
9
|
+
* The Buffer to use.
|
|
10
|
+
*/
|
|
11
|
+
readonly buffer?: WebsocketBuffer;
|
|
12
|
+
/**
|
|
13
|
+
* The options for the connection-retry-strategy.
|
|
14
|
+
*/
|
|
15
|
+
readonly retry?: WebsocketConnectionRetryOptions;
|
|
16
|
+
/**
|
|
17
|
+
* The initial listeners to add to the websocket.
|
|
18
|
+
*/
|
|
19
|
+
readonly listeners?: WebsocketEventListeners;
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=websocket_options.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"websocket_options.d.ts","sourceRoot":"","sources":["../../../src/websocket_options.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,EAAE,+BAA+B,EAAE,MAAM,2BAA2B,CAAC;AAC5E,OAAO,EAAE,uBAAuB,EAAE,MAAM,mBAAmB,CAAC;AAE5D;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B;;OAEG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,eAAe,CAAC;IAElC;;OAEG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAE,+BAA+B,CAAC;IAEjD;;OAEG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,uBAAuB,CAAC;CAC9C"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"websocket_options.js","sourceRoot":"","sources":["../../../src/websocket_options.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Backoff } from "./backoff/backoff";
|
|
2
|
+
/**
|
|
3
|
+
* Options for the websockets retry-strategy.
|
|
4
|
+
*/
|
|
5
|
+
export interface WebsocketConnectionRetryOptions {
|
|
6
|
+
/**
|
|
7
|
+
* The maximum number of retries before giving up. No limit if undefined.
|
|
8
|
+
*/
|
|
9
|
+
readonly maxRetries?: number;
|
|
10
|
+
/**
|
|
11
|
+
* Wether to reconnect immediately after a connection has been lost,
|
|
12
|
+
* ignoring the backoff strategy for the first retry.
|
|
13
|
+
*/
|
|
14
|
+
readonly instantReconnect?: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* The backoff strategy to use. This is used to determine the delay between connection-retries.
|
|
17
|
+
*/
|
|
18
|
+
readonly backoff?: Backoff;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=websocket_retry_options.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"websocket_retry_options.d.ts","sourceRoot":"","sources":["../../../src/websocket_retry_options.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAE5C;;GAEG;AACH,MAAM,WAAW,+BAA+B;IAC9C;;OAEG;IACH,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;IAE7B;;;OAGG;IACH,QAAQ,CAAC,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAEpC;;OAEG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC;CAC5B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"websocket_retry_options.js","sourceRoot":"","sources":["../../../src/websocket_retry_options.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "websocket-ts",
|
|
3
|
-
"version": "
|
|
4
|
-
"main": "
|
|
5
|
-
"types": "
|
|
3
|
+
"version": "2.1.2",
|
|
4
|
+
"main": "dist/cjs/src/index.js",
|
|
5
|
+
"types": "dist/cjs/src/index.d.ts",
|
|
6
|
+
"module": "dist/esm/src/index.js",
|
|
6
7
|
"license": "MIT",
|
|
7
8
|
"keywords": [
|
|
8
9
|
"websocket",
|
|
@@ -12,22 +13,34 @@
|
|
|
12
13
|
"reconnecting",
|
|
13
14
|
"buffered"
|
|
14
15
|
],
|
|
16
|
+
"sideEffects": false,
|
|
15
17
|
"repository": {
|
|
16
18
|
"url": "github:jjxxs/websocket-ts"
|
|
17
19
|
},
|
|
18
20
|
"scripts": {
|
|
19
|
-
"build": "tsc -p .",
|
|
20
|
-
"
|
|
21
|
+
"build:cjs": "tsc -p tsconfig.cjs.json",
|
|
22
|
+
"build:esm": "tsc -p tsconfig.esm.json",
|
|
23
|
+
"build": "npm run build:cjs && npm run build:esm",
|
|
21
24
|
"test": "jest",
|
|
22
|
-
"
|
|
25
|
+
"test:coverage": "jest --coverage",
|
|
26
|
+
"coveralls": "cat ./coverage/lcov.info | coveralls",
|
|
27
|
+
"lint": "cross-env-shell eslint './src/{**/*,*}.{js,ts}' './tests/{**/*,*}.{js,ts}'"
|
|
23
28
|
},
|
|
24
29
|
"devDependencies": {
|
|
25
|
-
"@
|
|
26
|
-
"@
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"
|
|
30
|
+
"@typescript-eslint/eslint-plugin": "^6.6.0",
|
|
31
|
+
"@typescript-eslint/parser": "^6.6.0",
|
|
32
|
+
"eslint-config-prettier": "^9.0.0",
|
|
33
|
+
"eslint-plugin-prettier": "^5.0.0",
|
|
34
|
+
"coveralls": "^3.1.1",
|
|
35
|
+
"eslint": "^8.49.0",
|
|
36
|
+
"@types/jest": "^29.5.4",
|
|
37
|
+
"jest": "^29.7.0",
|
|
38
|
+
"jest-environment-jsdom": "^29.7.0",
|
|
39
|
+
"ts-jest": "^29.1.1",
|
|
40
|
+
"ts-node": "^10.9.1",
|
|
41
|
+
"typescript": "^5.2.2",
|
|
42
|
+
"@types/ws": "^8.5.5",
|
|
43
|
+
"ws": "^7.0.0",
|
|
44
|
+
"cross-env": "^7.0.3"
|
|
32
45
|
}
|
|
33
|
-
}
|
|
46
|
+
}
|
package/src/backoff/backoff.ts
CHANGED
|
@@ -1,18 +1,26 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Backoff
|
|
3
|
-
*
|
|
4
|
-
* exponentially increasing etc.
|
|
2
|
+
* A Backoff produces a series of numbers that are used to determine
|
|
3
|
+
* the delay between connection-retries. Values are expected to be in milliseconds.
|
|
5
4
|
*/
|
|
6
5
|
export interface Backoff {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
*/
|
|
12
|
-
next(): number
|
|
6
|
+
/**
|
|
7
|
+
* The number of retries. Starts at 0, increases by 1 for each call to next(). Resets to 0 when reset() is called.
|
|
8
|
+
*/
|
|
9
|
+
readonly retries: number;
|
|
13
10
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
11
|
+
/**
|
|
12
|
+
* Current number in the series.
|
|
13
|
+
*/
|
|
14
|
+
readonly current: number;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Advances the series to the next number and returns it.
|
|
18
|
+
* @return the next number in the series
|
|
19
|
+
*/
|
|
20
|
+
next(): number;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Resets the series to its initial state.
|
|
24
|
+
*/
|
|
25
|
+
reset(): void;
|
|
18
26
|
}
|
|
@@ -1,20 +1,38 @@
|
|
|
1
|
-
import {Backoff} from "./backoff";
|
|
1
|
+
import { Backoff } from "./backoff";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* ConstantBackoff always returns the same
|
|
4
|
+
* ConstantBackoff always returns the same backoff-time.
|
|
5
5
|
*/
|
|
6
6
|
export class ConstantBackoff implements Backoff {
|
|
7
|
-
|
|
7
|
+
private readonly backoff: number;
|
|
8
|
+
private _retries: number = 0;
|
|
8
9
|
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
/**
|
|
11
|
+
* Creates a new ConstantBackoff.
|
|
12
|
+
* @param backoff the backoff-time to return
|
|
13
|
+
*/
|
|
14
|
+
constructor(backoff: number) {
|
|
15
|
+
if (!Number.isInteger(backoff) || backoff < 0) {
|
|
16
|
+
throw new Error("Backoff must be a positive integer");
|
|
11
17
|
}
|
|
12
18
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
19
|
+
this.backoff = backoff;
|
|
20
|
+
}
|
|
16
21
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
22
|
+
get retries(): number {
|
|
23
|
+
return this._retries;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
get current(): number {
|
|
27
|
+
return this.backoff;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
next(): number {
|
|
31
|
+
this._retries++;
|
|
32
|
+
return this.backoff;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
reset(): void {
|
|
36
|
+
this._retries = 0;
|
|
37
|
+
}
|
|
20
38
|
}
|
|
@@ -1,38 +1,78 @@
|
|
|
1
|
-
import {Backoff} from "./backoff";
|
|
1
|
+
import { Backoff } from "./backoff";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* ExponentialBackoff
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* ExponentialBackoff increases the backoff-time exponentially.
|
|
5
|
+
* An optional maximum can be provided as an upper bound to the
|
|
6
|
+
* exponent and thus to the returned backoff.
|
|
7
7
|
*
|
|
8
|
-
* The
|
|
9
|
-
*
|
|
8
|
+
* The series can be described as ('i' is the current step/retry):
|
|
9
|
+
* backoff = base * 2^i | without bound
|
|
10
|
+
* backoff = base * 2^min(i, expMax) | with bound
|
|
10
11
|
*
|
|
11
|
-
* Example:
|
|
12
|
-
*
|
|
12
|
+
* Example:
|
|
13
|
+
*
|
|
14
|
+
* 1) Without bound:
|
|
15
|
+
* base = 1000, expMax = undefined
|
|
16
|
+
* backoff = 1000 * 2^0 = 1000 // first retry
|
|
17
|
+
* backoff = 1000 * 2^1 = 2000 // second retry
|
|
18
|
+
* backoff = 1000 * 2^2 = 4000 // ...doubles with every retry
|
|
19
|
+
* backoff = 1000 * 2^3 = 8000
|
|
20
|
+
* backoff = 1000 * 2^4 = 16000
|
|
21
|
+
* ... // and so on
|
|
22
|
+
*
|
|
23
|
+
* 2) With bound:
|
|
24
|
+
* base = 1000, expMax = 3
|
|
25
|
+
* backoff = 1000 * 2^0 = 1000 // first retry
|
|
26
|
+
* backoff = 1000 * 2^1 = 2000 // second retry
|
|
27
|
+
* backoff = 1000 * 2^2 = 4000 // third retry
|
|
28
|
+
* backoff = 1000 * 2^3 = 8000 // maximum reached, don't increase further
|
|
29
|
+
* backoff = 1000 * 2^3 = 8000
|
|
30
|
+
* backoff = 1000 * 2^3 = 8000
|
|
31
|
+
* ... // and so on
|
|
13
32
|
*/
|
|
14
33
|
export class ExponentialBackoff implements Backoff {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
34
|
+
private readonly base: number;
|
|
35
|
+
private readonly expMax?: number;
|
|
36
|
+
private i: number;
|
|
37
|
+
private _retries: number = 0;
|
|
19
38
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
39
|
+
/**
|
|
40
|
+
* Creates a new ExponentialBackoff.
|
|
41
|
+
* @param base the base of the exponentiation
|
|
42
|
+
* @param expMax the maximum exponent, no bound if undefined
|
|
43
|
+
*/
|
|
44
|
+
constructor(base: number, expMax?: number) {
|
|
45
|
+
if (!Number.isInteger(base) || base < 0) {
|
|
46
|
+
throw new Error("Base must be a positive integer or zero");
|
|
25
47
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const backoff = this.current;
|
|
29
|
-
if (this.expMax > this.expCurrent++)
|
|
30
|
-
this.current = this.current * 2;
|
|
31
|
-
return backoff;
|
|
48
|
+
if (expMax !== undefined && (!Number.isInteger(expMax) || expMax < 0)) {
|
|
49
|
+
throw new Error("ExpMax must be a undefined, a positive integer or zero");
|
|
32
50
|
}
|
|
33
51
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
52
|
+
this.base = base;
|
|
53
|
+
this.expMax = expMax;
|
|
54
|
+
this.i = 0;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
get retries() {
|
|
58
|
+
return this._retries;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
get current(): number {
|
|
62
|
+
return this.base * Math.pow(2, this.i);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
next(): number {
|
|
66
|
+
this._retries++;
|
|
67
|
+
this.i =
|
|
68
|
+
this.expMax === undefined
|
|
69
|
+
? this.i + 1
|
|
70
|
+
: Math.min(this.i + 1, this.expMax);
|
|
71
|
+
return this.current;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
reset(): void {
|
|
75
|
+
this._retries = 0;
|
|
76
|
+
this.i = 0;
|
|
77
|
+
}
|
|
78
|
+
}
|