@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.
- package/dist/index.min.js +1 -1
- package/dist/index.min.js.map +4 -4
- package/dist/src/index.d.ts +0 -19
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +27 -24
- package/dist/src/index.js.map +1 -1
- package/dist/src/listener.d.ts +1 -5
- package/dist/src/listener.d.ts.map +1 -1
- package/dist/src/listener.js +28 -23
- package/dist/src/listener.js.map +1 -1
- package/dist/src/socket-to-conn.d.ts +11 -0
- package/dist/src/socket-to-conn.d.ts.map +1 -0
- package/dist/src/socket-to-conn.js +88 -0
- package/dist/src/socket-to-conn.js.map +1 -0
- package/package.json +13 -14
- package/src/index.ts +30 -44
- package/src/listener.ts +30 -25
- package/src/socket-to-conn.ts +114 -0
- package/dist/src/utils.d.ts +0 -7
- package/dist/src/utils.d.ts.map +0 -1
- package/dist/src/utils.js +0 -30
- package/dist/src/utils.js.map +0 -1
- package/dist/src/websocket-to-conn.d.ts +0 -9
- package/dist/src/websocket-to-conn.d.ts.map +0 -1
- package/dist/src/websocket-to-conn.js +0 -83
- package/dist/src/websocket-to-conn.js.map +0 -1
- package/src/utils.ts +0 -36
- package/src/websocket-to-conn.ts +0 -108
package/src/websocket-to-conn.ts
DELETED
|
@@ -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
|
-
}
|