This repository has been archived by the owner on Jul 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
/
browser.js
70 lines (56 loc) · 1.71 KB
/
browser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* eslint-env mocha */
'use strict'
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const multiaddr = require('multiaddr')
const pipe = require('it-pipe')
const goodbye = require('it-goodbye')
const { collect, take } = require('streaming-iterables')
const WS = require('../src')
const mockUpgrader = {
upgradeInbound: maConn => maConn,
upgradeOutbound: maConn => maConn
}
describe('libp2p-websockets', () => {
const ma = multiaddr('/ip4/127.0.0.1/tcp/9095/ws')
let ws
let conn
beforeEach(async () => {
ws = new WS({ upgrader: mockUpgrader })
conn = await ws.dial(ma)
})
it('echo', async () => {
const message = Buffer.from('Hello World!')
const s = goodbye({ source: [message], sink: collect })
const results = await pipe(s, conn, s)
expect(results).to.eql([message])
})
describe('stress', () => {
it('one big write', async () => {
const rawMessage = Buffer.allocUnsafe(1000000).fill('a')
const s = goodbye({ source: [rawMessage], sink: collect })
const results = await pipe(s, conn, s)
expect(results).to.eql([rawMessage])
})
it('many writes', async function () {
this.timeout(10000)
const s = goodbye({
source: pipe(
{
[Symbol.iterator] () { return this },
next: () => ({ done: false, value: Buffer.from(Math.random().toString()) })
},
take(20000)
),
sink: collect
})
const result = await pipe(s, conn, s)
expect(result).to.have.length(20000)
})
})
it('.createServer throws in browser', () => {
expect(new WS({ upgrader: mockUpgrader }).createListener).to.throw()
})
})