@libp2p/websockets 9.2.19 → 10.0.0-55b7e5fea

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/src/filters.ts DELETED
@@ -1,38 +0,0 @@
1
- import { WebSocketsSecure, WebSockets, DNS } from '@multiformats/multiaddr-matcher'
2
- import type { Multiaddr } from '@multiformats/multiaddr'
3
-
4
- /**
5
- * @deprecated Configure this globally by passing a `connectionGater` to `createLibp2p` with a `denyDialMultiaddr` method that returns `false`
6
- */
7
- export function all (multiaddrs: Multiaddr[]): Multiaddr[] {
8
- return multiaddrs.filter((ma) => {
9
- return WebSocketsSecure.exactMatch(ma) || WebSockets.exactMatch(ma)
10
- })
11
- }
12
-
13
- /**
14
- * @deprecated Configure this globally by passing a `connectionGater` to `createLibp2p`
15
- */
16
- export function wss (multiaddrs: Multiaddr[]): Multiaddr[] {
17
- return multiaddrs.filter((ma) => {
18
- return WebSocketsSecure.exactMatch(ma)
19
- })
20
- }
21
-
22
- /**
23
- * @deprecated Configure this globally by passing a `connectionGater` to `createLibp2p`
24
- */
25
- export function dnsWss (multiaddrs: Multiaddr[]): Multiaddr[] {
26
- return multiaddrs.filter((ma) => {
27
- return DNS.matches(ma) && WebSocketsSecure.exactMatch(ma)
28
- })
29
- }
30
-
31
- /**
32
- * @deprecated Configure this globally by passing a `connectionGater` to `createLibp2p`
33
- */
34
- export function dnsWsOrWss (multiaddrs: Multiaddr[]): Multiaddr[] {
35
- return multiaddrs.filter((ma) => {
36
- return DNS.matches(ma) && (WebSocketsSecure.exactMatch(ma) || WebSockets.exactMatch(ma))
37
- })
38
- }
@@ -1,114 +0,0 @@
1
- import { AbortError, ConnectionFailedError } from '@libp2p/interface'
2
- import { CLOSE_TIMEOUT } from './constants.js'
3
- import type { AbortOptions, ComponentLogger, CounterGroup, MultiaddrConnection } from '@libp2p/interface'
4
- import type { Multiaddr } from '@multiformats/multiaddr'
5
- import type { DuplexWebSocket } from 'it-ws/duplex'
6
-
7
- export interface SocketToConnOptions {
8
- localAddr?: Multiaddr
9
- logger: ComponentLogger
10
- metrics?: CounterGroup
11
- metricPrefix?: string
12
- }
13
-
14
- // Convert a stream into a MultiaddrConnection
15
- // https://github.com/libp2p/interface-transport#multiaddrconnection
16
- export function socketToMaConn (stream: DuplexWebSocket, remoteAddr: Multiaddr, options: SocketToConnOptions): MultiaddrConnection {
17
- const metrics = options.metrics
18
- const metricPrefix = options.metricPrefix ?? ''
19
-
20
- const maConn: MultiaddrConnection = {
21
- log: options.logger.forComponent('libp2p:websockets:connection'),
22
-
23
- async sink (source) {
24
- try {
25
- await stream.sink((async function * () {
26
- for await (const buf of source) {
27
- if (buf instanceof Uint8Array) {
28
- yield buf
29
- } else {
30
- yield buf.subarray()
31
- }
32
- }
33
- })())
34
- } catch (err: any) {
35
- if (err.type !== 'aborted') {
36
- maConn.log.error(err)
37
- }
38
- }
39
- },
40
-
41
- source: stream.source,
42
-
43
- remoteAddr,
44
-
45
- timeline: { open: Date.now() },
46
-
47
- async close (options: AbortOptions = {}) {
48
- const start = Date.now()
49
-
50
- if (options.signal == null) {
51
- const signal = AbortSignal.timeout(CLOSE_TIMEOUT)
52
-
53
- options = {
54
- ...options,
55
- signal
56
- }
57
- }
58
-
59
- const listener = (): void => {
60
- const { host, port } = maConn.remoteAddr.toOptions()
61
- maConn.log('timeout closing stream to %s:%s after %dms, destroying it manually',
62
- host, port, Date.now() - start)
63
-
64
- this.abort(new AbortError('Socket close timeout'))
65
- }
66
-
67
- options.signal?.addEventListener('abort', listener)
68
-
69
- try {
70
- await stream.close()
71
- } catch (err: any) {
72
- maConn.log.error('error closing WebSocket gracefully - %e', err)
73
- this.abort(err)
74
- } finally {
75
- options.signal?.removeEventListener('abort', listener)
76
- maConn.timeline.close = Date.now()
77
- }
78
- },
79
-
80
- abort (err: Error): void {
81
- maConn.log.error('destroying WebSocket after error - %e', err)
82
- stream.destroy()
83
- maConn.timeline.close = Date.now()
84
-
85
- // ws WebSocket.terminate does not accept an Error arg to emit an 'error'
86
- // event on destroy like other node streams so we can't update a metric
87
- // with an event listener
88
- // https://github.com/websockets/ws/issues/1752#issuecomment-622380981
89
- metrics?.increment({ [`${metricPrefix}error`]: true })
90
- }
91
- }
92
-
93
- // track local vs remote closing
94
- let closedLocally = false
95
- const close = stream.socket.close.bind(stream.socket)
96
- stream.socket.close = (...args) => {
97
- closedLocally = true
98
- return close(...args)
99
- }
100
-
101
- stream.socket.addEventListener('close', (evt) => {
102
- maConn.log('closed %s, code %d, reason "%s", wasClean %s', closedLocally ? 'locally' : 'by remote', evt.code, evt.reason, evt.wasClean)
103
-
104
- if (!evt.wasClean) {
105
- maConn.abort(new ConnectionFailedError(`${closedLocally ? 'Local' : 'Remote'} did not close WebSocket cleanly`))
106
- return
107
- }
108
-
109
- metrics?.increment({ [`${metricPrefix}close`]: true })
110
- maConn.timeline.close = Date.now()
111
- }, { once: true })
112
-
113
- return maConn
114
- }