websocket-ts 1.1.0 → 2.1.1
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 +21 -21
- package/README.md +211 -138
- package/package.json +46 -33
- package/src/backoff/backoff.ts +26 -18
- package/src/backoff/constantbackoff.ts +38 -20
- package/src/backoff/exponentialbackoff.ts +78 -38
- package/src/backoff/linearbackoff.ts +88 -37
- 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 +487 -162
- 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} +71 -71
- package/tsconfig.esm.json +71 -0
- package/jest.config.js +0 -8
- package/lib/backoff/backoff.d.ts +0 -18
- package/lib/backoff/backoff.d.ts.map +0 -1
- package/lib/backoff/backoff.js +0 -3
- package/lib/backoff/backoff.js.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 +0 -3
- 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/lib/wsbuilder.d.ts +0 -29
- package/lib/wsbuilder.d.ts.map +0 -1
- package/lib/wsbuilder.js +0 -88
- package/lib/wsbuilder.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/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c)
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Joscha Behrmann
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,138 +1,211 @@
|
|
|
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
|
-
|
|
1
|
+
<div>
|
|
2
|
+
<div align="center">
|
|
3
|
+
<img src="https://raw.githubusercontent.com/jjxxs/websocket-ts/gh-pages/websocket-ts-logo.svg" alt="websocket-ts" width="300" height="65" />
|
|
4
|
+
</div>
|
|
5
|
+
<p align="center">
|
|
6
|
+
<img src="https://github.com/jjxxs/websocket-ts/actions/workflows/build.yml/badge.svg" alt="Build Status" />
|
|
7
|
+
<img src="https://github.com/jjxxs/websocket-ts/actions/workflows/test.yml/badge.svg" alt="Build Status" />
|
|
8
|
+
<a href="https://coveralls.io/github/jjxxs/websocket-ts?branch=master">
|
|
9
|
+
<img src="https://coveralls.io/repos/github/jjxxs/websocket-ts/badge.svg?branch=master&service=github" alt="Coverage Status" />
|
|
10
|
+
</a>
|
|
11
|
+
<a href="https://github.com/jjxxs/websocket-ts/releases/latest">
|
|
12
|
+
<img src="https://img.shields.io/github/v/release/jjxxs/websocket-ts" alt="Release" />
|
|
13
|
+
</a>
|
|
14
|
+
<a href="/LICENSE">
|
|
15
|
+
<img src="https://img.shields.io/github/license/jjxxs/websocket-ts" alt="License" />
|
|
16
|
+
</a>
|
|
17
|
+
</p>
|
|
18
|
+
</div>
|
|
19
|
+
|
|
20
|
+
<div align="center">
|
|
21
|
+
A <b>WebSocket</b> for browsers with <b>auto-reconnect</b> and <b>message buffering</b> written in <b>TypeScript</b>.
|
|
22
|
+
</div>
|
|
23
|
+
|
|
24
|
+
## Features
|
|
25
|
+
|
|
26
|
+
- **Lightweight & Standalone**: No dependencies, 2.1 kB minified & gzipped.
|
|
27
|
+
- **Browser-native**: Utilizes WebSocket API, offers direct access.
|
|
28
|
+
- **Smart Reconnect**: Optional auto-reconnect and message buffering.
|
|
29
|
+
- **Easy Setup**: Optional builder class for quick initialization.
|
|
30
|
+
- **Well-Tested**: High test coverage, well-documented for extensibility.
|
|
31
|
+
- **Module Support**: Supports CommonJS, and ES6 modules.
|
|
32
|
+
|
|
33
|
+
## Installation
|
|
34
|
+
|
|
35
|
+
Install `websocket-ts` with npm:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
$ npm install websocket-ts
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Quickstart
|
|
42
|
+
This example shows how to use the package, complete with message buffering and automatic reconnection.
|
|
43
|
+
The created websocket will echo back any received messages. It will buffer messages when disconnected
|
|
44
|
+
and attempt to reconnect every 1 second.
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
import {
|
|
48
|
+
ArrayQueue,
|
|
49
|
+
ConstantBackoff,
|
|
50
|
+
Websocket,
|
|
51
|
+
WebsocketBuilder,
|
|
52
|
+
WebsocketEvent,
|
|
53
|
+
} from "websocket-ts";
|
|
54
|
+
|
|
55
|
+
// Initialize WebSocket with buffering and 1s reconnection delay
|
|
56
|
+
const ws = new WebsocketBuilder("ws://localhost:8080")
|
|
57
|
+
.withBuffer(new ArrayQueue()) // buffer messages when disconnected
|
|
58
|
+
.withBackoff(new ConstantBackoff(1000)) // retry every 1s
|
|
59
|
+
.build();
|
|
60
|
+
|
|
61
|
+
// Function to output & echo received messages
|
|
62
|
+
const echoOnMessage = (i: Websocket, ev: MessageEvent) => {
|
|
63
|
+
console.log(`received message: ${ev.data}`);
|
|
64
|
+
i.send(`echo: ${ev.data}`);
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
// Add event listeners
|
|
68
|
+
ws.addEventListener(WebsocketEvent.open, () => console.log("opened!"));
|
|
69
|
+
ws.addEventListener(WebsocketEvent.close, () => console.log("closed!"));
|
|
70
|
+
ws.addEventListener(WebsocketEvent.message, echoOnMessage);
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Usage
|
|
74
|
+
This will demonstrate how to use `websocket-ts` in your project using the provided `WebsocketBuild`-class.
|
|
75
|
+
|
|
76
|
+
For a more detailed description of the API, please refer to the [API Documentation](https://jjxxs.github.io/websocket-ts/).
|
|
77
|
+
|
|
78
|
+
#### Initialization
|
|
79
|
+
|
|
80
|
+
Create a new instance with the `WebsocketBuilder`:
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
const ws = new WebsocketBuilder("ws://localhost:42421").build();
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
#### Events
|
|
87
|
+
|
|
88
|
+
There are six events which can be subscribed to through with event listeners:
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
export enum WebsocketEvent {
|
|
92
|
+
open = "open", // Connection opened
|
|
93
|
+
close = "close", // Connection closed
|
|
94
|
+
error = "error", // Error-induced closure
|
|
95
|
+
message = "message", // Message received
|
|
96
|
+
retry = "retry", // Reconnect attempt
|
|
97
|
+
reconnect = "reconnect" // Successful reconnect
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
#### Add Event Listeners
|
|
102
|
+
Event listeners receive the websocket instance (`i`) and the triggering event (`ev`) as arguments.
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
const ws = new WebsocketBuilder("ws://localhost:42421")
|
|
106
|
+
.onOpen((i, ev) => console.log("opened"))
|
|
107
|
+
.onClose((i, ev) => console.log("closed"))
|
|
108
|
+
.onError((i, ev) => console.log("error"))
|
|
109
|
+
.onMessage((i, ev) => console.log("message"))
|
|
110
|
+
.onRetry((i, ev) => console.log("retry"))
|
|
111
|
+
.onReconnect((i, ev) => console.log("reconnect"))
|
|
112
|
+
.build();
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
#### Remove Event Listeners
|
|
116
|
+
|
|
117
|
+
To unregister a specific event listener, use `removeEventListener`:
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
let ws: Websocket
|
|
121
|
+
/* ... */
|
|
122
|
+
ws.removeEventListener(WebsocketEvent.open, openEventListener);
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
#### Send Message
|
|
126
|
+
|
|
127
|
+
Use the `send` method to send a message to the server:
|
|
128
|
+
|
|
129
|
+
```typescript
|
|
130
|
+
let ws: Websocket;
|
|
131
|
+
/* ... */
|
|
132
|
+
ws.send("Hello World!");
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
#### Reconnect & Backoff (Optional)
|
|
136
|
+
|
|
137
|
+
If you'd like the websocket to automatically reconnect upon disconnection, you can optionally provide a `Backoff` strategy.
|
|
138
|
+
This sets the delay between reconnection attempts. There are three built-in `Backoff` implementations, or you can create
|
|
139
|
+
your own by implementing the `Backoff` interface. If no Backoff is provided, the websocket will not attempt to reconnect.
|
|
140
|
+
|
|
141
|
+
##### ConstantBackoff
|
|
142
|
+
|
|
143
|
+
The `ConstantBackoff` strategy enforces a fixed delay between each reconnection attempt.
|
|
144
|
+
To set a constant 1-second wait time, use:
|
|
145
|
+
|
|
146
|
+
```typescript
|
|
147
|
+
const ws = new WebsocketBuilder("ws://localhost:42421")
|
|
148
|
+
.withBackoff(new ConstantBackoff(1000)) // 1000ms = 1s
|
|
149
|
+
.build();
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
##### LinearBackoff
|
|
153
|
+
|
|
154
|
+
The `LinearBackoff` strategy increases the delay between reconnection attempts linearly,
|
|
155
|
+
up to an optional maximum. For example, to start with a 0-second delay and increase by
|
|
156
|
+
10 second for each retry, capping at 60 seconds, use:
|
|
157
|
+
|
|
158
|
+
```typescript
|
|
159
|
+
const ws = new WebsocketBuilder("ws://localhost:42421")
|
|
160
|
+
.withBackoff(new LinearBackoff(0, 10000, 60000)) // 0ms, 10s, 20s, 30s, 40s, 50s, 60s
|
|
161
|
+
.build();
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
##### ExponentialBackoff
|
|
165
|
+
|
|
166
|
+
The `ExponentialBackoff` strategy doubles the delay between each reconnection attempt, up
|
|
167
|
+
to a specified maximum. This approach is inspired by the binary exponential backoff algorithm
|
|
168
|
+
commonly used in networking. For example, to generate a backoff series like `[1s, 2s, 4s, 8s]`, use:
|
|
169
|
+
|
|
170
|
+
```typescript
|
|
171
|
+
const ws = new WebsocketBuilder("ws://localhost:42421")
|
|
172
|
+
.withBackoff(new ExponentialBackoff(1000, 6)) // 1s, 2s, 4s, 8s, 16s, 32s, 64s
|
|
173
|
+
.build();
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
#### Buffer (Optional)
|
|
177
|
+
|
|
178
|
+
To buffer outgoing messages when the websocket is disconnected, you can optionally specify
|
|
179
|
+
a `Queue`. This queue will temporarily store your messages and send them in sequence when
|
|
180
|
+
the websocket (re)connects. Two built-in `Queue` implementations are available, or you can
|
|
181
|
+
create your own by implementing the `Queue` interface. If no queue is provided, messages
|
|
182
|
+
won't be buffered.
|
|
183
|
+
|
|
184
|
+
##### RingQueue
|
|
185
|
+
|
|
186
|
+
The `RingQueue` is a fixed-capacity, first-in-first-out (FIFO) queue. When it reaches capacity,
|
|
187
|
+
the oldest element is removed to accommodate new ones. Reading from the queue returns and
|
|
188
|
+
removes the oldest element. For instance, to set up a `RingQueue` with a 100-element capacity,
|
|
189
|
+
use:
|
|
190
|
+
|
|
191
|
+
```typescript
|
|
192
|
+
const ws = new WebsocketBuilder("ws://localhost:42421")
|
|
193
|
+
.withBuffer(new RingQueue(100))
|
|
194
|
+
.build();
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
##### ArrayQueue
|
|
198
|
+
|
|
199
|
+
The ArrayQueue offers an unbounded capacity, functioning as a first-in-first-out (FIFO) queue.
|
|
200
|
+
Reading from this queue returns and removes the oldest element. To use an `ArrayQueue`, use:
|
|
201
|
+
|
|
202
|
+
```typescript
|
|
203
|
+
const ws = new WebsocketBuilder("ws://localhost:42421")
|
|
204
|
+
.withBuffer(new ArrayQueue())
|
|
205
|
+
.build();
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
## Build & Tests
|
|
209
|
+
|
|
210
|
+
To compile the project, execute `npm run build`. The codebase includes unit tests for all
|
|
211
|
+
components. To run these tests, use `npm run test`.
|
package/package.json
CHANGED
|
@@ -1,33 +1,46 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "websocket-ts",
|
|
3
|
-
"version": "1.1
|
|
4
|
-
"main": "
|
|
5
|
-
"types": "
|
|
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
|
-
"typescript": "^
|
|
31
|
-
"
|
|
32
|
-
|
|
33
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "websocket-ts",
|
|
3
|
+
"version": "2.1.1",
|
|
4
|
+
"main": "dist/cjs/src/index.js",
|
|
5
|
+
"types": "dist/cjs/src/index.d.ts",
|
|
6
|
+
"module": "dist/esm/src/index.js",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"keywords": [
|
|
9
|
+
"websocket",
|
|
10
|
+
"browser",
|
|
11
|
+
"client",
|
|
12
|
+
"typescript",
|
|
13
|
+
"reconnecting",
|
|
14
|
+
"buffered"
|
|
15
|
+
],
|
|
16
|
+
"sideEffects": false,
|
|
17
|
+
"repository": {
|
|
18
|
+
"url": "github:jjxxs/websocket-ts"
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
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",
|
|
24
|
+
"test": "jest",
|
|
25
|
+
"test:coverage": "jest --coverage",
|
|
26
|
+
"coveralls": "cat ./coverage/lcov.info | coveralls",
|
|
27
|
+
"lint": "cross-env-shell eslint './src/{**/*,*}.{js,ts}' './tests/{**/*,*}.{js,ts}'"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
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"
|
|
45
|
+
}
|
|
46
|
+
}
|
package/src/backoff/backoff.ts
CHANGED
|
@@ -1,18 +1,26 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Backoff
|
|
3
|
-
*
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
1
|
+
/**
|
|
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.
|
|
4
|
+
*/
|
|
5
|
+
export interface Backoff {
|
|
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;
|
|
10
|
+
|
|
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;
|
|
26
|
+
}
|
|
@@ -1,20 +1,38 @@
|
|
|
1
|
-
import {Backoff} from "./backoff";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* ConstantBackoff always returns the same
|
|
5
|
-
*/
|
|
6
|
-
export class ConstantBackoff implements Backoff {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
1
|
+
import { Backoff } from "./backoff";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* ConstantBackoff always returns the same backoff-time.
|
|
5
|
+
*/
|
|
6
|
+
export class ConstantBackoff implements Backoff {
|
|
7
|
+
private readonly backoff: number;
|
|
8
|
+
private _retries: number = 0;
|
|
9
|
+
|
|
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");
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
this.backoff = backoff;
|
|
20
|
+
}
|
|
21
|
+
|
|
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
|
+
}
|
|
38
|
+
}
|