@libp2p/utils 4.0.7-9c67c5b3d → 4.0.7-adea7bbbf

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,4 +1,4 @@
1
- import type { AbortOptions, ComponentLogger } from '@libp2p/interface'
1
+ import type { ComponentLogger } from '@libp2p/interface'
2
2
  import type { MultiaddrConnection, Stream } from '@libp2p/interface/connection'
3
3
  import type { Multiaddr } from '@multiformats/multiaddr'
4
4
 
@@ -15,55 +15,65 @@ export interface StreamProperties {
15
15
  */
16
16
  export function streamToMaConnection (props: StreamProperties): MultiaddrConnection {
17
17
  const { stream, remoteAddr, logger } = props
18
- const { sink, source } = stream
19
18
  const log = logger.forComponent('libp2p:stream:converter')
20
19
 
21
20
  let closedRead = false
22
21
  let closedWrite = false
23
22
 
24
- const mapSource = (async function * () {
23
+ // piggyback on `stream.close` invocations to close maconn
24
+ const streamClose = stream.close.bind(stream)
25
+ stream.close = async (options) => {
26
+ await streamClose(options)
27
+ close(true)
28
+ }
29
+
30
+ // piggyback on `stream.abort` invocations to close maconn
31
+ const streamAbort = stream.abort.bind(stream)
32
+ stream.abort = (err) => {
33
+ streamAbort(err)
34
+ close(true)
35
+ }
36
+
37
+ // piggyback on `stream.sink` invocations to close maconn
38
+ const streamSink = stream.sink.bind(stream)
39
+ stream.sink = async (source) => {
25
40
  try {
26
- for await (const list of source) {
27
- if (list instanceof Uint8Array) {
28
- yield list
29
- } else {
30
- yield * list
31
- }
41
+ await streamSink(source)
42
+ } catch (err: any) {
43
+ // If aborted we can safely ignore
44
+ if (err.type !== 'aborted') {
45
+ // If the source errored the socket will already have been destroyed by
46
+ // toIterable.duplex(). If the socket errored it will already be
47
+ // destroyed. There's nothing to do here except log the error & return.
48
+ log(err)
32
49
  }
33
50
  } finally {
34
- closedRead = true
51
+ closedWrite = true
35
52
  close()
36
53
  }
37
- }())
54
+ }
38
55
 
39
56
  const maConn: MultiaddrConnection = {
40
- async sink (source) {
57
+ log,
58
+ sink: stream.sink,
59
+ source: (async function * () {
41
60
  try {
42
- await sink(source)
43
- } catch (err: any) {
44
- // If aborted we can safely ignore
45
- if (err.type !== 'aborted') {
46
- // If the source errored the socket will already have been destroyed by
47
- // toIterable.duplex(). If the socket errored it will already be
48
- // destroyed. There's nothing to do here except log the error & return.
49
- log(err)
61
+ for await (const list of stream.source) {
62
+ if (list instanceof Uint8Array) {
63
+ yield list
64
+ } else {
65
+ yield * list
66
+ }
50
67
  }
51
68
  } finally {
52
- closedWrite = true
69
+ closedRead = true
53
70
  close()
54
71
  }
55
- },
56
- source: mapSource,
72
+ }()),
57
73
  remoteAddr,
58
74
  timeline: { open: Date.now(), close: undefined },
59
- async close (options?: AbortOptions) {
60
- close(true)
61
- await stream.close(options)
62
- },
63
- abort (err: Error): void {
64
- close(true)
65
- stream.abort(err)
66
- }
75
+ close: stream.close,
76
+ abort: stream.abort
67
77
  }
68
78
 
69
79
  function close (force?: boolean): void {