@libp2p/websockets 9.2.19-8484de8a2 → 9.2.19-87bc8d4fb

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.
@@ -1,108 +0,0 @@
1
- import { AbstractMultiaddrConnection, repeatingTask } from '@libp2p/utils'
2
- import { Uint8ArrayList } from 'uint8arraylist'
3
- import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
4
- import type { AbortOptions, MultiaddrConnection } from '@libp2p/interface'
5
- import type { AbstractMultiaddrConnectionInit, RepeatingTask, SendResult } from '@libp2p/utils'
6
-
7
- const DEFAULT_MAX_BUFFERED_AMOUNT = 1024 * 1024 * 4
8
- const DEFAULT_BUFFERED_AMOUNT_POLL_INTERVAL = 10
9
-
10
- export interface WebSocketMultiaddrConnectionInit extends Omit<AbstractMultiaddrConnectionInit, 'name'> {
11
- websocket: WebSocket
12
- maxBufferedAmount?: number
13
- bufferedAmountPollInterval?: number
14
- }
15
-
16
- class WebSocketMultiaddrConnection extends AbstractMultiaddrConnection {
17
- private websocket: WebSocket
18
- private maxBufferedAmount: number
19
- private checkBufferedAmountTask: RepeatingTask
20
-
21
- constructor (init: WebSocketMultiaddrConnectionInit) {
22
- super(init)
23
-
24
- this.websocket = init.websocket
25
- this.maxBufferedAmount = init.maxBufferedAmount ?? DEFAULT_MAX_BUFFERED_AMOUNT
26
- this.checkBufferedAmountTask = repeatingTask(this.checkBufferedAmount.bind(this), init.bufferedAmountPollInterval ?? DEFAULT_BUFFERED_AMOUNT_POLL_INTERVAL)
27
-
28
- this.websocket.addEventListener('close', (evt) => {
29
- this.log('closed - code %d, reason "%s", wasClean %s', evt.code, evt.reason, evt.wasClean)
30
- this.checkBufferedAmountTask.stop()
31
-
32
- if (!evt.wasClean) {
33
- this.onRemoteReset()
34
- return
35
- }
36
-
37
- this.onTransportClosed()
38
- }, { once: true })
39
-
40
- this.websocket.addEventListener('message', (evt) => {
41
- try {
42
- let buf: Uint8Array
43
-
44
- if (typeof evt.data === 'string') {
45
- buf = uint8ArrayFromString(evt.data)
46
- } else if (evt.data instanceof ArrayBuffer) {
47
- buf = new Uint8Array(evt.data, 0, evt.data.byteLength)
48
- } else {
49
- this.abort(new Error('Incorrect binary type'))
50
- return
51
- }
52
-
53
- this.onData(buf)
54
- } catch (err: any) {
55
- this.log.error('error receiving data - %e', err)
56
- }
57
- })
58
- }
59
-
60
- sendData (data: Uint8ArrayList): SendResult {
61
- for (const buf of data) {
62
- this.websocket.send(buf)
63
- }
64
-
65
- const canSendMore = this.websocket.bufferedAmount < this.maxBufferedAmount
66
-
67
- if (!canSendMore) {
68
- this.checkBufferedAmountTask.start()
69
- }
70
-
71
- return {
72
- sentBytes: data.byteLength,
73
- canSendMore
74
- }
75
- }
76
-
77
- sendReset (): void {
78
- this.websocket.close(1006) // abnormal closure
79
- }
80
-
81
- async sendClose (options?: AbortOptions): Promise<void> {
82
- this.websocket.close()
83
- options?.signal?.throwIfAborted()
84
- }
85
-
86
- sendPause (): void {
87
- // read backpressure is not supported
88
- }
89
-
90
- sendResume (): void {
91
- // read backpressure is not supported
92
- }
93
-
94
- private checkBufferedAmount (): void {
95
- this.log('buffered amount now %d', this.websocket.bufferedAmount)
96
-
97
- if (this.websocket.bufferedAmount === 0) {
98
- this.checkBufferedAmountTask.stop()
99
- this.safeDispatchEvent('drain')
100
- }
101
- }
102
- }
103
-
104
- // Convert a stream into a MultiaddrConnection
105
- // https://github.com/libp2p/interface-transport#multiaddrconnection
106
- export function webSocketToMaConn (init: WebSocketMultiaddrConnectionInit): MultiaddrConnection {
107
- return new WebSocketMultiaddrConnection(init)
108
- }