Alex Potsides f439d9b589
deps!: update all deps to support no-copy operations (#1335)
Updates all deps needed to support passing lists of byte arrays where they have been created from multiple input buffers.

When reading multiplexed data, all messages arrive in length-prefixed buffers, which means the first few bytes tell the consumer how many bytes long next chunk will be.

One length prefixed chunk can be delivered in several payloads from the underlying network transport. The first payload can also include the length prefix and some or all of the data, so we stitch these together in a `Uint8ArrayList` to avoid having to concatenate `Uint8Array`s together.

Previously once we'd received enough bytes to satisfy the length prefix we'd concatenate the bytes together, but this is a potentially expensive operation where transports have small message sizes so instead just pass the `Uint8ArrayList` to the consumer and let them decide wether to concatenate or not as some consumers will be smart enough to operate on lists of `Uint8Array`s instead of always requiring a contiguous block of memory.

BREAKING CHANGE: Streams are now `Duplex<Uint8ArrayList, Uint8ArrayList | Uint8Array>`
2022-08-11 13:21:04 +01:00

87 lines
2.1 KiB
JavaScript

/* eslint-disable no-console */
import { createLibp2p } from 'libp2p'
import { TCP } from '@libp2p/tcp'
import { WebSockets } from '@libp2p/websockets'
import { Noise } from '@chainsafe/libp2p-noise'
import { Mplex } from '@libp2p/mplex'
import fs from 'fs'
import https from 'https'
import { pipe } from 'it-pipe'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
const httpServer = https.createServer({
cert: fs.readFileSync('./test_certs/cert.pem'),
key: fs.readFileSync('./test_certs/key.pem'),
});
const createNode = async (addresses = []) => {
if (!Array.isArray(addresses)) {
addresses = [addresses]
}
const node = await createLibp2p({
addresses: {
listen: addresses
},
transports: [
new TCP(),
new WebSockets({
server: httpServer,
websocket: {
rejectUnauthorized: false
}
})
],
connectionEncryption: [new Noise()],
streamMuxers: [new Mplex()],
connectionManager: {
// Disable autoDial as it would fail because we are using a self-signed cert.
// `dialProtocol` does not fail because we pass `rejectUnauthorized: false`.
autoDial: false
}
})
await node.start()
return node
}
function printAddrs(node, number) {
console.log('node %s is listening on:', number)
node.getMultiaddrs().forEach((ma) => console.log(ma.toString()))
}
function print ({ stream }) {
pipe(
stream,
async function (source) {
for await (const msg of source) {
console.log(uint8ArrayToString(msg.subarray()))
}
}
)
}
;(async () => {
const [node1, node2] = await Promise.all([
createNode('/ip4/127.0.0.1/tcp/10000/wss'),
createNode([])
])
printAddrs(node1, '1')
printAddrs(node2, '2')
node1.handle('/print', print)
node2.handle('/print', print)
const targetAddr = node1.getMultiaddrs()[0];
// node 2 (Secure WebSockets) dials to node 1 (Secure Websockets)
const stream = await node2.dialProtocol(targetAddr, '/print')
await pipe(
[uint8ArrayFromString('node 2 dialed to node 1 successfully')],
stream
)
})();