@libp2p/websockets 9.2.18 → 9.2.19-6059227cb
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 +15 -13
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +26 -41
- package/dist/src/index.js.map +1 -1
- package/dist/src/listener.d.ts +5 -1
- package/dist/src/listener.d.ts.map +1 -1
- package/dist/src/listener.js +32 -30
- package/dist/src/listener.js.map +1 -1
- package/dist/src/utils.d.ts +7 -0
- package/dist/src/utils.d.ts.map +1 -0
- package/dist/src/utils.js +30 -0
- package/dist/src/utils.js.map +1 -0
- package/dist/src/websocket-to-conn.d.ts +9 -0
- package/dist/src/websocket-to-conn.d.ts.map +1 -0
- package/dist/src/websocket-to-conn.js +83 -0
- package/dist/src/websocket-to-conn.js.map +1 -0
- package/package.json +14 -13
- package/src/index.ts +41 -61
- package/src/listener.ts +37 -32
- package/src/utils.ts +36 -0
- package/src/websocket-to-conn.ts +108 -0
- package/dist/src/filters.d.ts +0 -18
- package/dist/src/filters.d.ts.map +0 -1
- package/dist/src/filters.js +0 -34
- package/dist/src/filters.js.map +0 -1
- package/dist/src/socket-to-conn.d.ts +0 -11
- package/dist/src/socket-to-conn.d.ts.map +0 -1
- package/dist/src/socket-to-conn.js +0 -83
- package/dist/src/socket-to-conn.js.map +0 -1
- package/dist/typedoc-urls.json +0 -20
- package/src/filters.ts +0 -38
- package/src/socket-to-conn.ts +0 -109
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
|
-
}
|
package/src/socket-to-conn.ts
DELETED
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
import { AbortError } 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 log = options.logger.forComponent('libp2p:websockets:maconn')
|
|
18
|
-
const metrics = options.metrics
|
|
19
|
-
const metricPrefix = options.metricPrefix ?? ''
|
|
20
|
-
|
|
21
|
-
const maConn: MultiaddrConnection = {
|
|
22
|
-
log,
|
|
23
|
-
|
|
24
|
-
async sink (source) {
|
|
25
|
-
try {
|
|
26
|
-
await stream.sink((async function * () {
|
|
27
|
-
for await (const buf of source) {
|
|
28
|
-
if (buf instanceof Uint8Array) {
|
|
29
|
-
yield buf
|
|
30
|
-
} else {
|
|
31
|
-
yield buf.subarray()
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
})())
|
|
35
|
-
} catch (err: any) {
|
|
36
|
-
if (err.type !== 'aborted') {
|
|
37
|
-
log.error(err)
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
},
|
|
41
|
-
|
|
42
|
-
source: stream.source,
|
|
43
|
-
|
|
44
|
-
remoteAddr,
|
|
45
|
-
|
|
46
|
-
timeline: { open: Date.now() },
|
|
47
|
-
|
|
48
|
-
async close (options: AbortOptions = {}) {
|
|
49
|
-
const start = Date.now()
|
|
50
|
-
|
|
51
|
-
if (options.signal == null) {
|
|
52
|
-
const signal = AbortSignal.timeout(CLOSE_TIMEOUT)
|
|
53
|
-
|
|
54
|
-
options = {
|
|
55
|
-
...options,
|
|
56
|
-
signal
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
const listener = (): void => {
|
|
61
|
-
const { host, port } = maConn.remoteAddr.toOptions()
|
|
62
|
-
log('timeout closing stream to %s:%s after %dms, destroying it manually',
|
|
63
|
-
host, port, Date.now() - start)
|
|
64
|
-
|
|
65
|
-
this.abort(new AbortError('Socket close timeout'))
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
options.signal?.addEventListener('abort', listener)
|
|
69
|
-
|
|
70
|
-
try {
|
|
71
|
-
await stream.close()
|
|
72
|
-
} catch (err: any) {
|
|
73
|
-
log.error('error closing WebSocket gracefully', err)
|
|
74
|
-
this.abort(err)
|
|
75
|
-
} finally {
|
|
76
|
-
options.signal?.removeEventListener('abort', listener)
|
|
77
|
-
maConn.timeline.close = Date.now()
|
|
78
|
-
}
|
|
79
|
-
},
|
|
80
|
-
|
|
81
|
-
abort (err: Error): void {
|
|
82
|
-
const { host, port } = maConn.remoteAddr.toOptions()
|
|
83
|
-
log('timeout closing stream to %s:%s due to error',
|
|
84
|
-
host, port, err)
|
|
85
|
-
|
|
86
|
-
stream.destroy()
|
|
87
|
-
maConn.timeline.close = Date.now()
|
|
88
|
-
|
|
89
|
-
// ws WebSocket.terminate does not accept an Error arg to emit an 'error'
|
|
90
|
-
// event on destroy like other node streams so we can't update a metric
|
|
91
|
-
// with an event listener
|
|
92
|
-
// https://github.com/websockets/ws/issues/1752#issuecomment-622380981
|
|
93
|
-
metrics?.increment({ [`${metricPrefix}error`]: true })
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
stream.socket.addEventListener('close', () => {
|
|
98
|
-
metrics?.increment({ [`${metricPrefix}close`]: true })
|
|
99
|
-
|
|
100
|
-
// In instances where `close` was not explicitly called,
|
|
101
|
-
// such as an iterable stream ending, ensure we have set the close
|
|
102
|
-
// timeline
|
|
103
|
-
if (maConn.timeline.close == null) {
|
|
104
|
-
maConn.timeline.close = Date.now()
|
|
105
|
-
}
|
|
106
|
-
}, { once: true })
|
|
107
|
-
|
|
108
|
-
return maConn
|
|
109
|
-
}
|