@@ -7,13 +7,14 @@ import * as filter from '@libp2p/websockets/filters'
7
7
import { WebRTC } from '@multiformats/mafmt'
8
8
import { multiaddr } from '@multiformats/multiaddr'
9
9
import { expect } from 'aegir/chai'
10
- import all from 'it-all'
11
10
import map from 'it-map'
12
11
import { pipe } from 'it-pipe'
12
+ import toBuffer from 'it-to-buffer'
13
13
import { createLibp2p } from 'libp2p'
14
14
import { circuitRelayTransport } from 'libp2p/circuit-relay'
15
15
import { identifyService } from 'libp2p/identify'
16
16
import { webRTC } from '../src/index.js'
17
+ import type { Connection } from '@libp2p/interface-connection'
17
18
import type { Libp2p } from '@libp2p/interface-libp2p'
18
19
19
20
async function createNode ( ) : Promise < Libp2p > {
@@ -50,34 +51,19 @@ async function createNode (): Promise<Libp2p> {
50
51
}
51
52
52
53
describe ( 'basics' , ( ) => {
54
+ const echo = '/echo/1.0.0'
55
+
53
56
let localNode : Libp2p
54
57
let remoteNode : Libp2p
55
58
56
- beforeEach ( async ( ) => {
57
- localNode = await createNode ( )
58
- remoteNode = await createNode ( )
59
- } )
60
-
61
- afterEach ( async ( ) => {
62
- if ( localNode != null ) {
63
- await localNode . stop ( )
64
- }
65
-
66
- if ( remoteNode != null ) {
67
- await remoteNode . stop ( )
68
- }
69
- } )
70
-
71
- it ( 'can dial through a relay' , async ( ) => {
59
+ async function connectNodes ( ) : Promise < Connection > {
72
60
const remoteAddr = remoteNode . getMultiaddrs ( )
73
61
. filter ( ma => WebRTC . matches ( ma ) ) . pop ( )
74
62
75
63
if ( remoteAddr == null ) {
76
64
throw new Error ( 'Remote peer could not listen on relay' )
77
65
}
78
66
79
- const echo = '/echo/1.0.0'
80
-
81
67
await remoteNode . handle ( echo , ( { stream } ) => {
82
68
void pipe (
83
69
stream ,
@@ -91,6 +77,27 @@ describe('basics', () => {
91
77
await localNode . hangUp ( multiaddr ( process . env . RELAY_MULTIADDR ) )
92
78
await remoteNode . hangUp ( multiaddr ( process . env . RELAY_MULTIADDR ) )
93
79
80
+ return connection
81
+ }
82
+
83
+ beforeEach ( async ( ) => {
84
+ localNode = await createNode ( )
85
+ remoteNode = await createNode ( )
86
+ } )
87
+
88
+ afterEach ( async ( ) => {
89
+ if ( localNode != null ) {
90
+ await localNode . stop ( )
91
+ }
92
+
93
+ if ( remoteNode != null ) {
94
+ await remoteNode . stop ( )
95
+ }
96
+ } )
97
+
98
+ it ( 'can dial through a relay' , async ( ) => {
99
+ const connection = await connectNodes ( )
100
+
94
101
// open a stream on the echo protocol
95
102
const stream = await connection . newStream ( echo )
96
103
@@ -100,10 +107,29 @@ describe('basics', () => {
100
107
input ,
101
108
stream ,
102
109
( source ) => map ( source , list => list . subarray ( ) ) ,
103
- async ( source ) => all ( source )
110
+ async ( source ) => toBuffer ( source )
111
+ )
112
+
113
+ // asset that we got the right data
114
+ expect ( output ) . to . equalBytes ( toBuffer ( input ) )
115
+ } )
116
+
117
+ it ( 'can send a large file' , async ( ) => {
118
+ const connection = await connectNodes ( )
119
+
120
+ // open a stream on the echo protocol
121
+ const stream = await connection . newStream ( echo )
122
+
123
+ // send and receive some data
124
+ const input = new Array ( 5 ) . fill ( 0 ) . map ( ( ) => new Uint8Array ( 1024 * 1024 ) )
125
+ const output = await pipe (
126
+ input ,
127
+ stream ,
128
+ ( source ) => map ( source , list => list . subarray ( ) ) ,
129
+ async ( source ) => toBuffer ( source )
104
130
)
105
131
106
132
// asset that we got the right data
107
- expect ( output ) . to . deep . equal ( input )
133
+ expect ( output ) . to . equalBytes ( toBuffer ( input ) )
108
134
} )
109
135
} )
0 commit comments