Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Commit

Permalink
docs: make the example work (#206)
Browse files Browse the repository at this point in the history
- Use esm instead of commonjs
- Follow the latest api
  • Loading branch information
twoeths authored Sep 6, 2022
1 parent 91d70d4 commit f07acc3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
17 changes: 10 additions & 7 deletions examples/dialer.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
/* 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)
console.log('[dialer] socket stream closed')
}

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()))
}
}
)
Expand Down
22 changes: 12 additions & 10 deletions examples/listener.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
/* 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
)
console.log('[listener] muxed stream closed')
}
})

socket = toIterable(socket)
await pipe(socket, muxer, socket)
console.log('[listener] socket stream closed')
})
Expand Down
2 changes: 1 addition & 1 deletion examples/util.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down

0 comments on commit f07acc3

Please sign in to comment.