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

Commit

Permalink
fix: update deps (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Aug 10, 2022
1 parent b1a68a2 commit 134c633
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -172,21 +172,24 @@
},
"dependencies": {
"@achingbrain/ip-address": "^8.1.0",
"@libp2p/interface-connection": "^2.1.0",
"@libp2p/interface-connection": "^3.0.1",
"@libp2p/interface-peer-store": "^1.0.0",
"@libp2p/logger": "^2.0.0",
"@multiformats/multiaddr": "^10.1.1",
"abortable-iterator": "^4.0.2",
"err-code": "^3.0.1",
"is-loopback-addr": "^2.0.1",
"it-stream-types": "^1.0.4",
"private-ip": "^2.1.1"
"private-ip": "^2.1.1",
"uint8arraylist": "^2.3.2"
},
"devDependencies": {
"aegir": "^37.2.0",
"it-all": "^1.0.6",
"it-map": "^1.0.6",
"it-pair": "^2.0.2",
"it-pipe": "^2.0.2",
"p-defer": "^4.0.0",
"uint8arrays": "^3.0.0"
}
}
12 changes: 10 additions & 2 deletions src/stream-to-ma-conn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { logger } from '@libp2p/logger'
import type { Multiaddr } from '@multiformats/multiaddr'
import type { MultiaddrConnection } from '@libp2p/interface-connection'
import type { Duplex } from 'it-stream-types'
import type { Uint8ArrayList } from 'uint8arraylist'

const log = logger('libp2p:stream:converter')

Expand All @@ -29,7 +30,7 @@ interface StreamOptions {
}

interface StreamProperties {
stream: Duplex<Uint8Array>
stream: Duplex<Uint8ArrayList, Uint8ArrayList | Uint8Array>
remoteAddr: Multiaddr
localAddr: Multiaddr
}
Expand All @@ -41,6 +42,13 @@ interface StreamProperties {
export function streamToMaConnection (props: StreamProperties, options: StreamOptions = {}) {
const { stream, remoteAddr } = props
const { sink, source } = stream

const mapSource = (async function * () {
for await (const list of source) {
yield * list
}
}())

const maConn: MultiaddrConnection = {
async sink (source) {
if (options.signal != null) {
Expand All @@ -60,7 +68,7 @@ export function streamToMaConnection (props: StreamProperties, options: StreamOp
}
}
},
source: (options.signal != null) ? abortableSource(source, options.signal) : source,
source: (options.signal != null) ? abortableSource(mapSource, options.signal) : mapSource,
remoteAddr,
/** @type {Timeline} */
timeline: { open: Date.now(), close: undefined },
Expand Down
28 changes: 28 additions & 0 deletions test/fixtures/pair.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import defer from 'p-defer'
import map from 'it-map'
import type { Source, Duplex } from 'it-stream-types'
import { Uint8ArrayList } from 'uint8arraylist'

/**
* A pair of streams where one drains from the other
*/
export function pair (): Duplex<Uint8ArrayList, Uint8ArrayList | Uint8Array> {
const deferred = defer<Source<Uint8ArrayList | Uint8Array>>()
let piped = false

return {
sink: async source => {
if (piped) {
throw new Error('already piped')
}

piped = true
deferred.resolve(source)
},
source: (async function * () {
const source = await deferred.promise

yield * map(source, (buf) => buf instanceof Uint8Array ? new Uint8ArrayList(buf) : buf)
}())
}
}
9 changes: 5 additions & 4 deletions test/stream-to-ma-conn.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
/* eslint-env mocha */

import { expect } from 'aegir/chai'
import { pair } from 'it-pair'
import { pair } from './fixtures/pair.js'
import { pipe } from 'it-pipe'
import { multiaddr } from '@multiformats/multiaddr'
import { streamToMaConnection } from '../src/stream-to-ma-conn.js'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import all from 'it-all'
import type { Stream } from '@libp2p/interface-connection'
import type { Duplex } from 'it-stream-types'
import type { Uint8ArrayList } from 'uint8arraylist'

function toMuxedStream (stream: Duplex<Uint8Array>) {
function toMuxedStream (stream: Duplex<Uint8ArrayList, Uint8ArrayList | Uint8Array>) {
const muxedStream: Stream = {
...stream,
close: () => {},
Expand All @@ -36,7 +37,7 @@ describe('Convert stream into a multiaddr connection', () => {
const remoteAddr = multiaddr('/ip4/100.46.74.201/tcp/6002')

it('converts a stream and adds the provided metadata', async () => {
const stream = pair<Uint8Array>()
const stream = pair()

const maConn = streamToMaConnection({
stream: toMuxedStream(stream),
Expand All @@ -57,7 +58,7 @@ describe('Convert stream into a multiaddr connection', () => {
})

it('can stream data over the multiaddr connection', async () => {
const stream = pair<Uint8Array>()
const stream = pair()
const maConn = streamToMaConnection({
stream: toMuxedStream(stream),
localAddr,
Expand Down

0 comments on commit 134c633

Please sign in to comment.