From f07acc361a0627ae848804ed6eb422efad70878f Mon Sep 17 00:00:00 2001 From: tuyennhv Date: Tue, 6 Sep 2022 22:48:18 +0700 Subject: [PATCH] docs: make the example work (#206) - Use esm instead of commonjs - Follow the latest api --- examples/dialer.js | 17 ++++++++++------- examples/listener.js | 22 ++++++++++++---------- examples/util.js | 2 +- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/examples/dialer.js b/examples/dialer.js index c4170c8..9c33ba3 100644 --- a/examples/dialer.js +++ b/examples/dialer.js @@ -1,17 +1,20 @@ /* eslint-disable no-console */ 'use strict' -const tcp = require('net') +import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' +import { toString as uint8ArrayToString } from 'uint8arrays/to-string' +import tcp from 'net' import { pipe } from 'it-pipe' -const { toIterable } = require('./util') -const Mplex = require('../src') +import { toIterable } from './util.js' +import { Mplex } from '../dist/src/index.js' const socket = toIterable(tcp.connect(9999)) console.log('[dialer] socket stream opened') const controller = new AbortController() -const muxer = new Mplex({ signal: controller.signal }) +const factory = new Mplex({ signal: controller.signal }) +const muxer = factory.createStreamMuxer() const pipeMuxerToSocket = async () => { await pipe(muxer, socket, muxer) @@ -19,16 +22,16 @@ const pipeMuxerToSocket = async () => { } const sendAndReceive = async () => { - const muxedStream = muxer.newStream() + const muxedStream = muxer.newStream('hello') console.log('[dialer] muxed stream opened') await pipe( - ['hey, how is it going. I am the dialer'], + [uint8ArrayFromString('hey, how is it going. I am the dialer')], muxedStream, async source => { for await (const chunk of source) { console.log('[dialer] received:') - console.log(chunk.toString()) + console.log(uint8ArrayToString(chunk.slice())) } } ) diff --git a/examples/listener.js b/examples/listener.js index 3e18330..189c945 100644 --- a/examples/listener.js +++ b/examples/listener.js @@ -1,24 +1,28 @@ /* eslint-disable no-console */ 'use strict' -const tcp = require('net') +import tcp from 'net' import { pipe } from 'it-pipe' -const { toIterable } = require('./util') -const Mplex = require('../src') +import { toIterable } from './util.js' +import { Mplex } from '../dist/src/index.js' +import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' +import { toString as uint8ArrayToString } from 'uint8arrays/to-string' const listener = tcp.createServer(async socket => { console.log('[listener] Got connection!') - const muxer = new Mplex({ - async onStream (stream) { - console.log('[listener] muxed stream opened') + const factory = new Mplex() + socket = toIterable(socket) + const muxer = factory.createStreamMuxer({ + onIncomingStream: async (stream) => { + console.log('[listener] muxed stream opened, id:', stream.id) await pipe( stream, source => (async function * () { for await (const chunk of source) { console.log('[listener] received:') - console.log(chunk.toString()) - yield 'thanks for the message, I am the listener' + console.log(uint8ArrayToString(chunk.slice())) + yield uint8ArrayFromString('thanks for the message, I am the listener') } })(), stream @@ -26,8 +30,6 @@ const listener = tcp.createServer(async socket => { console.log('[listener] muxed stream closed') } }) - - socket = toIterable(socket) await pipe(socket, muxer, socket) console.log('[listener] socket stream closed') }) diff --git a/examples/util.js b/examples/util.js index ad9e35d..057c0c6 100644 --- a/examples/util.js +++ b/examples/util.js @@ -1,5 +1,5 @@ // Simple convertion of Node.js duplex to iterable duplex (no backpressure) -exports.toIterable = socket => { +export const toIterable = socket => { return { sink: async source => { try {