websocket-ts 2.2.0 → 2.3.0
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 +225 -212
- package/dist/cjs/src/backoff/exponentialbackoff.js +1 -1
- package/dist/cjs/src/backoff/exponentialbackoff.js.map +1 -1
- package/dist/cjs/src/index.d.ts +1 -1
- package/dist/cjs/src/index.d.ts.map +1 -1
- package/dist/cjs/src/index.js.map +1 -1
- package/dist/cjs/src/websocket.d.ts +9 -3
- package/dist/cjs/src/websocket.d.ts.map +1 -1
- package/dist/cjs/src/websocket.js +7 -3
- package/dist/cjs/src/websocket.js.map +1 -1
- package/dist/cjs/src/websocket_builder.d.ts +9 -9
- package/dist/cjs/src/websocket_builder.d.ts.map +1 -1
- package/dist/cjs/src/websocket_builder.js +6 -6
- package/dist/cjs/src/websocket_builder.js.map +1 -1
- package/dist/cjs/src/websocket_event.d.ts +18 -7
- package/dist/cjs/src/websocket_event.d.ts.map +1 -1
- package/dist/cjs/src/websocket_event.js +8 -9
- package/dist/cjs/src/websocket_event.js.map +1 -1
- package/dist/cjs/src/websocket_retry_options.d.ts +1 -1
- package/dist/esm/src/backoff/exponentialbackoff.js +1 -1
- package/dist/esm/src/backoff/exponentialbackoff.js.map +1 -1
- package/dist/esm/src/index.d.ts +1 -1
- package/dist/esm/src/index.d.ts.map +1 -1
- package/dist/esm/src/index.js.map +1 -1
- package/dist/esm/src/websocket.d.ts +9 -3
- package/dist/esm/src/websocket.d.ts.map +1 -1
- package/dist/esm/src/websocket.js +7 -3
- package/dist/esm/src/websocket.js.map +1 -1
- package/dist/esm/src/websocket_builder.d.ts +9 -9
- package/dist/esm/src/websocket_builder.d.ts.map +1 -1
- package/dist/esm/src/websocket_builder.js +6 -6
- package/dist/esm/src/websocket_builder.js.map +1 -1
- package/dist/esm/src/websocket_event.d.ts +18 -7
- package/dist/esm/src/websocket_event.d.ts.map +1 -1
- package/dist/esm/src/websocket_event.js +8 -9
- package/dist/esm/src/websocket_event.js.map +1 -1
- package/dist/esm/src/websocket_retry_options.d.ts +1 -1
- package/package.json +19 -24
- package/src/backoff/exponentialbackoff.ts +1 -1
- package/src/index.ts +1 -1
- package/src/websocket.ts +16 -5
- package/src/websocket_builder.ts +10 -10
- package/src/websocket_event.ts +22 -7
- package/src/websocket_retry_options.ts +1 -1
- package/tsconfig.cjs.json +74 -74
- package/tsconfig.esm.json +74 -74
- package/websocket-ts-2.2.0.tgz +0 -0
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) 2020 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,212 +1,225 @@
|
|
|
1
|
-
<div>
|
|
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
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
.
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
.
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
1
|
+
<div align="center">
|
|
2
|
+
<img src="https://raw.githubusercontent.com/jjxxs/websocket-ts/gh-pages/websocket-ts-logo.svg" alt="websocket-ts" width="300" height="65" />
|
|
3
|
+
</div>
|
|
4
|
+
|
|
5
|
+
<div align="center">
|
|
6
|
+
A <b>WebSocket</b> for browsers with <b>auto-reconnect</b> and <b>message buffering</b> written in <b>TypeScript</b>.
|
|
7
|
+
</div>
|
|
8
|
+
|
|
9
|
+
<div align="center">
|
|
10
|
+
|
|
11
|
+
[](https://github.com/jjxxs/websocket-ts/actions/workflows/build.yml)
|
|
12
|
+
[](https://github.com/jjxxs/websocket-ts/actions/workflows/test.yml)
|
|
13
|
+
[](https://coveralls.io/github/jjxxs/websocket-ts?branch=master)
|
|
14
|
+
[](https://npm-stat.com/charts.html?package=websocket-ts)
|
|
15
|
+
|
|
16
|
+
</div>
|
|
17
|
+
|
|
18
|
+
<div align="center">
|
|
19
|
+
|
|
20
|
+
[](https://www.npmjs.org/package/websocket-ts)
|
|
21
|
+
[](https://bundlejs.com/?q=websocket-ts)
|
|
22
|
+
[](https://snyk.io/test/npm/websocket-ts)
|
|
23
|
+
[](/LICENSE)
|
|
24
|
+
|
|
25
|
+
</div>
|
|
26
|
+
|
|
27
|
+
## Features
|
|
28
|
+
|
|
29
|
+
- **Lightweight & Standalone**: No dependencies, 2.5 kB minified & gzipped.
|
|
30
|
+
- **Browser-native**: Utilizes native WebSocket API, offers direct access to the underlying connection.
|
|
31
|
+
- **Smart Reconnect**: Optional auto-reconnect and message buffering.
|
|
32
|
+
- **Easy Setup**: Optional builder class for quick initialization.
|
|
33
|
+
- **Well-Tested**: High test coverage, well-documented for extensibility.
|
|
34
|
+
- **Module Support**: Supports CommonJS and ES6 modules.
|
|
35
|
+
|
|
36
|
+
## Installation
|
|
37
|
+
|
|
38
|
+
Install `websocket-ts` with npm:
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
npm install websocket-ts
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Quickstart
|
|
45
|
+
This example creates a WebSocket with message buffering and automatic reconnection.
|
|
46
|
+
The created websocket will echo back any received messages. It will buffer messages when disconnected
|
|
47
|
+
and attempt to reconnect every 1 second.
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
import {
|
|
51
|
+
ArrayQueue,
|
|
52
|
+
ConstantBackoff,
|
|
53
|
+
Websocket,
|
|
54
|
+
WebsocketBuilder,
|
|
55
|
+
WebsocketEvent,
|
|
56
|
+
} from "websocket-ts";
|
|
57
|
+
|
|
58
|
+
// Initialize WebSocket with buffering and 1s reconnection delay
|
|
59
|
+
const ws = new WebsocketBuilder("ws://localhost:8080")
|
|
60
|
+
.withBuffer(new ArrayQueue()) // buffer messages when disconnected
|
|
61
|
+
.withBackoff(new ConstantBackoff(1000)) // retry every 1s
|
|
62
|
+
.build();
|
|
63
|
+
|
|
64
|
+
// Function to output & echo received messages
|
|
65
|
+
const echoOnMessage = (i: Websocket, ev: MessageEvent) => {
|
|
66
|
+
console.log(`received message: ${ev.data}`);
|
|
67
|
+
i.send(`echo: ${ev.data}`);
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
// Add event listeners
|
|
71
|
+
ws.addEventListener(WebsocketEvent.open, () => console.log("opened!"));
|
|
72
|
+
ws.addEventListener(WebsocketEvent.close, () => console.log("closed!"));
|
|
73
|
+
ws.addEventListener(WebsocketEvent.message, echoOnMessage);
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Usage
|
|
77
|
+
This section demonstrates how to use `websocket-ts` in your project using the provided `WebsocketBuilder` class.
|
|
78
|
+
|
|
79
|
+
For a more detailed description of the API, please refer to the [API Documentation](https://jjxxs.github.io/websocket-ts/).
|
|
80
|
+
|
|
81
|
+
#### Initialization
|
|
82
|
+
|
|
83
|
+
Create a new instance with the `WebsocketBuilder`:
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
const ws = new WebsocketBuilder("ws://localhost:42421").build();
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
#### Events
|
|
90
|
+
|
|
91
|
+
There are six events you can listen for:
|
|
92
|
+
|
|
93
|
+
| Event | Description |
|
|
94
|
+
|---|---|
|
|
95
|
+
| `open` | Connection opened |
|
|
96
|
+
| `close` | Connection closed |
|
|
97
|
+
| `error` | An error occurred |
|
|
98
|
+
| `message` | Message received |
|
|
99
|
+
| `retry` | Reconnect attempt |
|
|
100
|
+
| `reconnect` | Successful reconnect |
|
|
101
|
+
|
|
102
|
+
You can use either `WebsocketEvent.open` or the string `"open"` when registering listeners.
|
|
103
|
+
|
|
104
|
+
#### Add Event Listeners
|
|
105
|
+
Event listeners receive the websocket instance (`i`) and the triggering event (`ev`) as arguments.
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
const ws = new WebsocketBuilder("ws://localhost:42421")
|
|
109
|
+
.onOpen((i, ev) => console.log("opened"))
|
|
110
|
+
.onClose((i, ev) => console.log("closed"))
|
|
111
|
+
.onError((i, ev) => console.log("error"))
|
|
112
|
+
.onMessage((i, ev) => console.log("message"))
|
|
113
|
+
.onRetry((i, ev) => console.log("retry"))
|
|
114
|
+
.onReconnect((i, ev) => console.log("reconnect"))
|
|
115
|
+
.build();
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
#### Remove Event Listeners
|
|
119
|
+
|
|
120
|
+
To unregister a specific event listener, use `removeEventListener`:
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
let ws: Websocket
|
|
124
|
+
/* ... */
|
|
125
|
+
ws.removeEventListener(WebsocketEvent.open, openEventListener);
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
#### Send Message
|
|
129
|
+
|
|
130
|
+
Use the `send` method to send a message to the server:
|
|
131
|
+
|
|
132
|
+
```typescript
|
|
133
|
+
let ws: Websocket;
|
|
134
|
+
/* ... */
|
|
135
|
+
ws.send("Hello World!");
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
#### Reconnect & Backoff (Optional)
|
|
139
|
+
|
|
140
|
+
To automatically reconnect after a disconnection, provide a `Backoff` strategy.
|
|
141
|
+
This controls the delay between reconnection attempts. There are three built-in `Backoff` implementations, or you can create
|
|
142
|
+
your own by implementing the `Backoff` interface. If no backoff is provided, the websocket will not attempt to reconnect.
|
|
143
|
+
|
|
144
|
+
##### ConstantBackoff
|
|
145
|
+
|
|
146
|
+
`ConstantBackoff` uses a fixed delay between each reconnection attempt.
|
|
147
|
+
To set a constant 1-second wait time, use:
|
|
148
|
+
|
|
149
|
+
```typescript
|
|
150
|
+
const ws = new WebsocketBuilder("ws://localhost:42421")
|
|
151
|
+
.withBackoff(new ConstantBackoff(1000)) // 1000ms = 1s
|
|
152
|
+
.build();
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
#### LinearBackoff
|
|
156
|
+
|
|
157
|
+
`LinearBackoff` increases the delay between reconnection attempts linearly,
|
|
158
|
+
up to an optional maximum. For example, to start with a 0-second delay and increase by
|
|
159
|
+
10 seconds for each retry, capping at 60 seconds, use:
|
|
160
|
+
|
|
161
|
+
```typescript
|
|
162
|
+
const ws = new WebsocketBuilder("ws://localhost:42421")
|
|
163
|
+
.withBackoff(new LinearBackoff(0, 10000, 60000)) // 0ms, 10s, 20s, 30s, 40s, 50s, 60s
|
|
164
|
+
.build();
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
##### ExponentialBackoff
|
|
168
|
+
|
|
169
|
+
`ExponentialBackoff` doubles the delay between each reconnection attempt, up
|
|
170
|
+
to a specified maximum. This approach is inspired by the binary exponential backoff algorithm
|
|
171
|
+
commonly used in networking. For example, to use a base of 1000ms with a maximum exponent of 6:
|
|
172
|
+
|
|
173
|
+
```typescript
|
|
174
|
+
const ws = new WebsocketBuilder("ws://localhost:42421")
|
|
175
|
+
.withBackoff(new ExponentialBackoff(1000, 6)) // 1s, 2s, 4s, 8s, 16s, 32s, 64s
|
|
176
|
+
.build();
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
#### Buffer (Optional)
|
|
180
|
+
|
|
181
|
+
To buffer outgoing messages when the websocket is disconnected, provide a `Queue`.
|
|
182
|
+
The queue temporarily stores messages and sends them in order when
|
|
183
|
+
the websocket (re)connects. Two built-in `Queue` implementations are available, or you can
|
|
184
|
+
create your own by implementing the `Queue` interface. If no queue is provided, messages
|
|
185
|
+
won't be buffered.
|
|
186
|
+
|
|
187
|
+
##### RingQueue
|
|
188
|
+
|
|
189
|
+
`RingQueue` is a fixed-capacity, first-in-first-out (FIFO) queue. When it reaches capacity,
|
|
190
|
+
the oldest element is removed to make room for new ones. For instance, to set up a `RingQueue` with a 100-element capacity:
|
|
191
|
+
|
|
192
|
+
```typescript
|
|
193
|
+
const ws = new WebsocketBuilder("ws://localhost:42421")
|
|
194
|
+
.withBuffer(new RingQueue(100))
|
|
195
|
+
.build();
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
##### ArrayQueue
|
|
199
|
+
|
|
200
|
+
`ArrayQueue` is an unbounded first-in-first-out (FIFO) queue.
|
|
201
|
+
To use an `ArrayQueue`:
|
|
202
|
+
|
|
203
|
+
```typescript
|
|
204
|
+
const ws = new WebsocketBuilder("ws://localhost:42421")
|
|
205
|
+
.withBuffer(new ArrayQueue())
|
|
206
|
+
.build();
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
#### URL Provider (Optional)
|
|
210
|
+
|
|
211
|
+
By default, the URL is a static string. To use a different URL between connection attempts, provide
|
|
212
|
+
a function instead. The function is called on each connection attempt, including the initial one and
|
|
213
|
+
any retries. This enables use cases like load balancing, auth token rotation, and failover.
|
|
214
|
+
|
|
215
|
+
```typescript
|
|
216
|
+
const ws = new WebsocketBuilder(() => `ws://localhost:42421?token=${getToken()}`)
|
|
217
|
+
.withBackoff(new ConstantBackoff(1000))
|
|
218
|
+
.build();
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
## Build & Tests
|
|
222
|
+
|
|
223
|
+
To compile the project, run `npm run build`.
|
|
224
|
+
|
|
225
|
+
To run tests, use `npm run test`.
|
|
@@ -43,7 +43,7 @@ var ExponentialBackoff = /** @class */ (function () {
|
|
|
43
43
|
throw new Error("Base must be a positive integer or zero");
|
|
44
44
|
}
|
|
45
45
|
if (expMax !== undefined && (!Number.isInteger(expMax) || expMax < 0)) {
|
|
46
|
-
throw new Error("ExpMax must be
|
|
46
|
+
throw new Error("ExpMax must be undefined, a positive integer, or zero");
|
|
47
47
|
}
|
|
48
48
|
this.base = base;
|
|
49
49
|
this.expMax = expMax;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"exponentialbackoff.js","sourceRoot":"","sources":["../../../../src/backoff/exponentialbackoff.ts"],"names":[],"mappings":";;;AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH;IAME;;;;OAIG;IACH,4BAAY,IAAY,EAAE,MAAe;QAPjC,aAAQ,GAAW,CAAC,CAAC;QAQ3B,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,IAAI,GAAG,CAAC,EAAE,CAAC;YACxC,MAAM,IAAI,KAAK,CAAC,yCAAyC,CAAC,CAAC;QAC7D,CAAC;QACD,IAAI,MAAM,KAAK,SAAS,IAAI,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC,IAAI,MAAM,GAAG,CAAC,CAAC,EAAE,CAAC;YACtE,MAAM,IAAI,KAAK,CAAC,
|
|
1
|
+
{"version":3,"file":"exponentialbackoff.js","sourceRoot":"","sources":["../../../../src/backoff/exponentialbackoff.ts"],"names":[],"mappings":";;;AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH;IAME;;;;OAIG;IACH,4BAAY,IAAY,EAAE,MAAe;QAPjC,aAAQ,GAAW,CAAC,CAAC;QAQ3B,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,IAAI,GAAG,CAAC,EAAE,CAAC;YACxC,MAAM,IAAI,KAAK,CAAC,yCAAyC,CAAC,CAAC;QAC7D,CAAC;QACD,IAAI,MAAM,KAAK,SAAS,IAAI,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC,IAAI,MAAM,GAAG,CAAC,CAAC,EAAE,CAAC;YACtE,MAAM,IAAI,KAAK,CAAC,uDAAuD,CAAC,CAAC;QAC3E,CAAC;QAED,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC;IACb,CAAC;IAED,sBAAI,uCAAO;aAAX;YACE,OAAO,IAAI,CAAC,QAAQ,CAAC;QACvB,CAAC;;;OAAA;IAED,sBAAI,uCAAO;aAAX;YACE,OAAO,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC;QACzC,CAAC;;;OAAA;IAED,iCAAI,GAAJ;QACE,IAAI,CAAC,QAAQ,EAAE,CAAC;QAChB,IAAI,CAAC,CAAC;YACJ,IAAI,CAAC,MAAM,KAAK,SAAS;gBACvB,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC;gBACZ,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;QACxC,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;IAED,kCAAK,GAAL;QACE,IAAI,CAAC,QAAQ,GAAG,CAAC,CAAC;QAClB,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC;IACb,CAAC;IACH,yBAAC;AAAD,CAAC,AA7CD,IA6CC;AA7CY,gDAAkB"}
|
package/dist/cjs/src/index.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ export { LinearBackoff } from "./backoff/linearbackoff";
|
|
|
5
5
|
export { Queue } from "./queue/queue";
|
|
6
6
|
export { ArrayQueue } from "./queue/array_queue";
|
|
7
7
|
export { RingQueue } from "./queue/ring_queue";
|
|
8
|
-
export { Websocket } from "./websocket";
|
|
8
|
+
export { UrlProvider, Websocket } from "./websocket";
|
|
9
9
|
export { WebsocketBuffer } from "./websocket_buffer";
|
|
10
10
|
export { WebsocketBuilder } from "./websocket_builder";
|
|
11
11
|
export { WebsocketEvent, RetryEventDetail, ReconnectEventDetail, WebsocketEventMap, WebsocketEventListener, WebsocketEventListenerParams, WebsocketEventListenerOptions, WebsocketEventListenerWithOptions, WebsocketEventListeners, } from "./websocket_event";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC5C,OAAO,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC;AAC5D,OAAO,EAAE,kBAAkB,EAAE,MAAM,8BAA8B,CAAC;AAClE,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AACxD,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AACtC,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACjD,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAC/C,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC5C,OAAO,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAC;AAC5D,OAAO,EAAE,kBAAkB,EAAE,MAAM,8BAA8B,CAAC;AAClE,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AACxD,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AACtC,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACjD,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAC/C,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACrD,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AACvD,OAAO,EACL,cAAc,EACd,gBAAgB,EAChB,oBAAoB,EACpB,iBAAiB,EACjB,sBAAsB,EACtB,4BAA4B,EAC5B,6BAA6B,EAC7B,iCAAiC,EACjC,uBAAuB,GACxB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AACvD,OAAO,EAAE,+BAA+B,EAAE,MAAM,2BAA2B,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":";;;AACA,6DAA4D;AAAnD,kHAAA,eAAe,OAAA;AACxB,mEAAkE;AAAzD,wHAAA,kBAAkB,OAAA;AAC3B,yDAAwD;AAA/C,8GAAA,aAAa,OAAA;AAEtB,mDAAiD;AAAxC,yGAAA,UAAU,OAAA;AACnB,iDAA+C;AAAtC,uGAAA,SAAS,OAAA;AAClB,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":";;;AACA,6DAA4D;AAAnD,kHAAA,eAAe,OAAA;AACxB,mEAAkE;AAAzD,wHAAA,kBAAkB,OAAA;AAC3B,yDAAwD;AAA/C,8GAAA,aAAa,OAAA;AAEtB,mDAAiD;AAAxC,yGAAA,UAAU,OAAA;AACnB,iDAA+C;AAAtC,uGAAA,SAAS,OAAA;AAClB,yCAAqD;AAA/B,sGAAA,SAAS,OAAA;AAE/B,yDAAuD;AAA9C,qHAAA,gBAAgB,OAAA;AACzB,qDAU2B;AATzB,iHAAA,cAAc,OAAA"}
|
|
@@ -2,11 +2,17 @@ import { Backoff } from "./backoff/backoff";
|
|
|
2
2
|
import { WebsocketBuffer } from "./websocket_buffer";
|
|
3
3
|
import { WebsocketEvent, WebsocketEventListener, WebsocketEventListenerOptions } from "./websocket_event";
|
|
4
4
|
import { WebsocketOptions } from "./websocket_options";
|
|
5
|
+
/**
|
|
6
|
+
* A URL or a function that returns a URL. When a function is provided, it is called on each connection attempt,
|
|
7
|
+
* enabling use cases like load balancing, auth token rotation, and failover.
|
|
8
|
+
*/
|
|
9
|
+
export type UrlProvider = string | (() => string);
|
|
5
10
|
/**
|
|
6
11
|
* A websocket wrapper that can be configured to reconnect automatically and buffer messages when the websocket is not connected.
|
|
7
12
|
*/
|
|
8
13
|
export declare class Websocket {
|
|
9
|
-
private readonly
|
|
14
|
+
private readonly _urlProvider;
|
|
15
|
+
private _url;
|
|
10
16
|
private readonly _protocols?;
|
|
11
17
|
private _closedByUser;
|
|
12
18
|
private _lastConnection?;
|
|
@@ -16,11 +22,11 @@ export declare class Websocket {
|
|
|
16
22
|
/**
|
|
17
23
|
* Creates a new websocket.
|
|
18
24
|
*
|
|
19
|
-
* @param url to connect to.
|
|
25
|
+
* @param url to connect to, or a function that returns a URL.
|
|
20
26
|
* @param protocols optional protocols to use.
|
|
21
27
|
* @param options optional options to use.
|
|
22
28
|
*/
|
|
23
|
-
constructor(url:
|
|
29
|
+
constructor(url: UrlProvider, protocols?: string | string[], options?: WebsocketOptions);
|
|
24
30
|
/**
|
|
25
31
|
* Getter for the url.
|
|
26
32
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"websocket.d.ts","sourceRoot":"","sources":["../../../src/websocket.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC5C,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,EAGL,cAAc,EACd,sBAAsB,EACtB,6BAA6B,EAI9B,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAEvD;;GAEG;AACH,qBAAa,SAAS;IACpB,OAAO,CAAC,QAAQ,CAAC,IAAI,
|
|
1
|
+
{"version":3,"file":"websocket.d.ts","sourceRoot":"","sources":["../../../src/websocket.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC5C,OAAO,EAAE,eAAe,EAAE,M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H,OAAO,CAAC,+BAA+B;IAwCvC;;OAEG;IACH,OAAO,CAAC,8BAA8B;CAGvC"}
|
|
@@ -18,7 +18,7 @@ var Websocket = /** @class */ (function () {
|
|
|
18
18
|
/**
|
|
19
19
|
* Creates a new websocket.
|
|
20
20
|
*
|
|
21
|
-
* @param url to connect to.
|
|
21
|
+
* @param url to connect to, or a function that returns a URL.
|
|
22
22
|
* @param protocols optional protocols to use.
|
|
23
23
|
* @param options optional options to use.
|
|
24
24
|
*/
|
|
@@ -54,7 +54,7 @@ var Websocket = /** @class */ (function () {
|
|
|
54
54
|
this.handleMessageEvent = function (event) {
|
|
55
55
|
return _this.handleEvent(websocket_event_1.WebsocketEvent.message, event);
|
|
56
56
|
};
|
|
57
|
-
this.
|
|
57
|
+
this._urlProvider = url;
|
|
58
58
|
this._protocols = protocols;
|
|
59
59
|
// make a copy of the options to prevent the user from changing them
|
|
60
60
|
this._options = {
|
|
@@ -305,7 +305,11 @@ var Websocket = /** @class */ (function () {
|
|
|
305
305
|
* @return the created browser-native websocket which is also stored in the '_underlyingWebsocket' property.
|
|
306
306
|
*/
|
|
307
307
|
Websocket.prototype.tryConnect = function () {
|
|
308
|
-
this.
|
|
308
|
+
this._url =
|
|
309
|
+
typeof this._urlProvider === "function"
|
|
310
|
+
? this._urlProvider()
|
|
311
|
+
: this._urlProvider;
|
|
312
|
+
this._underlyingWebsocket = new WebSocket(this._url, this.protocols); // create new browser-native websocket and add all event listeners
|
|
309
313
|
this._underlyingWebsocket.addEventListener(websocket_event_1.WebsocketEvent.open, this.handleOpenEvent);
|
|
310
314
|
this._underlyingWebsocket.addEventListener(websocket_event_1.WebsocketEvent.close, this.handleCloseEvent);
|
|
311
315
|
this._underlyingWebsocket.addEventListener(websocket_event_1.WebsocketEvent.error, this.handleErrorEvent);
|