Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/pull mplex #4

Merged
merged 50 commits into from
Feb 7, 2019
Merged
Show file tree
Hide file tree
Changes from 46 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
3c5caed
feat: initial commit
dryajov Apr 15, 2018
7c6bdf1
feat: correct channel closing
dryajov Mar 31, 2018
2798b03
feat: getting interface-stream-muxer tests to pass
dryajov Apr 1, 2018
0987b24
feat: improve performance
dryajov Apr 3, 2018
c8f2115
feat: correct encoding/decoding
dryajov Apr 3, 2018
117adbb
feat: correct encoding and channel closing
dryajov Apr 3, 2018
fb79753
feat: correct channel and plex closing
dryajov Apr 5, 2018
284fd44
feat: interop with old libp2p-mplex
dryajov Apr 6, 2018
aeee3d9
fix: correctly read chunked data
dryajov Apr 8, 2018
1a97616
feat: interep with old stream based mplex
dryajov Apr 8, 2018
e2f4579
feat: avoid negative sizes
dryajov Apr 8, 2018
56694e0
feat: dont close channel too early
dryajov Apr 9, 2018
e9cd98d
feat: validation and coverage
dryajov Apr 10, 2018
79f5a60
fix: correct channel id sequence
dryajov Apr 11, 2018
487ff5d
feat: use bl to avoid buffer concat/copy
dryajov Apr 14, 2018
2272897
chore: renaming back to pull-mplex
dryajov Apr 15, 2018
de7f5bb
chore: rename old to stream-mplex-interop
dryajov Apr 15, 2018
66473b5
chore: adding misc files
dryajov Apr 15, 2018
bfd691e
chore: license
dryajov Apr 15, 2018
3bc0908
feat: use two lists to avoid clashes
dryajov Apr 18, 2018
18d43eb
style: formatting
dryajov Apr 18, 2018
dc95ed9
feat: remove unused deps
dryajov Apr 18, 2018
ff8e56a
feat: update README.md
dryajov Apr 2, 2018
b340326
feat: performance
dryajov Apr 21, 2018
9a5a867
chore: update repo link
dryajov Apr 21, 2018
6659c45
fix: dont use bl
dryajov Apr 24, 2018
86eab54
perf: optimize
dryajov Apr 24, 2018
1e9f794
style: fix formating
dryajov Apr 24, 2018
64794fe
perf: improoving performance
dryajov Apr 27, 2018
8c26286
style: remove unused variable
dryajov Apr 28, 2018
fa387c5
fix: msg length encoding
dryajov Apr 30, 2018
adc1884
fix: dont blow up the stack
dryajov May 5, 2018
0fc4ca1
fix: use arrow function
dryajov May 8, 2018
5c2c914
fix: remove initiator id logic
dryajov May 9, 2018
3b29ca5
fix: start channel from 0
dryajov May 9, 2018
1dbe5dc
chore: adding commitizen
dryajov May 9, 2018
01fd7e3
feat: initial commit
dryajov Mar 26, 2018
54a249b
feat: getting interface-stream-muxer tests to pass
dryajov Apr 1, 2018
4c537d5
chore: remove unused file
dryajov May 10, 2018
5431c65
feat: send chan name
dryajov May 10, 2018
7f67df8
fix: send chan name when creating stream
dryajov May 10, 2018
c8a1c7e
fix: remove nyc_output file
dryajov May 11, 2018
a1d9788
chore: move benchmarks and inteop to libp2p-mplex
dryajov May 15, 2018
22ef8d0
chore: remove non jenkins ci
jacobheun Feb 5, 2019
b895055
chore: update dependencies
jacobheun Feb 6, 2019
b59b1fe
fix: listen to the channel error
jacobheun Feb 6, 2019
0eaf7d8
refactor: spread op instead of lodash defaults
jacobheun Feb 6, 2019
cb8b719
refactor: avoid toString when debug not enabled
jacobheun Feb 6, 2019
03520cc
docs: add more comments
jacobheun Feb 6, 2019
ef06716
docs: add more js docs
jacobheun Feb 6, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

**/node_modules/
**/*.log
test/repo-tests*
**/bundle.js
**/.nyc_output

# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

build

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules

lib
dist
test/test-data/go-ipfs-repo/LOCK
test/test-data/go-ipfs-repo/LOG
test/test-data/go-ipfs-repo/LOG.old

docs
# while testing npm5
package-lock.json
yarn.lock
35 changes: 35 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
test

# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
node_modules

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017 Protocol Labs Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# pull-mplex
multiplexer implementing the https://github.com/libp2p/mplex spec with pull-streams
# pull-plex
pull-streams based multiplexer implementing the https://github.com/libp2p/mplex spec
2 changes: 2 additions & 0 deletions ci/Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Warning: This file is automatically synced from https://github.com/ipfs/ci-sync so if you want to change it, please change it there and ask someone to sync all repositories.
javascript()
56 changes: 56 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"name": "pull-mplex",
"version": "0.0.1",
"description": "multiplex implementation of https://github.com/libp2p/mplex",
"main": "src/index.js",
"scripts": {
"lint": "aegir lint",
"build": "aegir build",
"test": "aegir test -t node -t browser",
"test:node": "aegir test -t node",
"test:browser": "aegir test -t browser",
"release": "aegir release -t node -t browser",
"release-minor": "aegir release --type minor -t node -t browser",
"release-major": "aegir release --type major -t node -t browser",
"coverage": "aegir coverage",
"coverage-publish": "aegir coverage --provider coveralls"
},
"author": "dryajov@gmail.com",
"license": "MIT",
"devDependencies": {
"aegir": "^18.0.3",
"async": "^2.6.0",
"chai": "^4.2.0",
"chai-checkmark": "^1.0.1",
"chunky": "0.0.0",
"dirty-chai": "^2.0.1",
"libp2p-mplex": "~0.8.4",
"pull-abortable": "^4.1.1",
"pull-generate": "^2.2.0",
"pull-length-prefixed": "^1.3.1",
"pull-stream-to-stream": "^1.3.4",
"pump": "^3.0.0"
},
"repository": {
"type": "git",
"url": "git+https://github.com/libp2p/pull-mplex.git"
},
"dependencies": {
"buffer-reuse-pool": "^1.0.0",
"debug": "^4.1.1",
"interface-connection": "~0.3.3",
"lodash.defaults": "^4.2.0",
"looper": "^4.0.0",
"pull-offset-limit": "^1.1.1",
"pull-pair": "^1.1.0",
"pull-pushable": "^2.2.0",
"pull-stream": "^3.6.9",
"pull-through": "^1.0.18",
"varint": "^5.0.0"
},
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
}
}
195 changes: 195 additions & 0 deletions src/channel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
'use strict'

const pushable = require('pull-pushable')
const defaults = require('lodash.defaults')
const looper = require('looper')

const consts = require('./consts')
const EE = require('events')

const debug = require('debug')

const log = debug('pull-plex:chan')
log.err = debug('pull-plex:chan:err')

class Channel extends EE {
constructor (opts) {
super()

opts = defaults({}, opts, { initiator: false })

this._id = opts.id
this._name = opts.name
this._plex = opts.plex
this._open = opts.open
this._initiator = opts.initiator
this._endedRemote = false // remote stream ended
this._endedLocal = false // local stream ended
this._reset = false

this.MSG = this._initiator
? consts.type.OUT_MESSAGE
: consts.type.IN_MESSAGE

this.END = this._initiator
? consts.type.OUT_CLOSE
: consts.type.IN_CLOSE

this.RESET = this._initiator
? consts.type.OUT_RESET
: consts.type.IN_RESET

this._log = (name, data) => {
log({
op: name,
name: this._name,
id: this._id,
endedLocal: this._endedLocal,
endedRemote: this._endedRemote,
initiator: this._initiator,
data: (data && data.toString()) || ''
})
}

this._log('new channel', this._name)

this._msgs = pushable((err) => {
this._log('source closed', err)
if (err && typeof err !== 'boolean') {
setImmediate(() => this.emit('error', err))
}
// this.endChan() // TODO: do not uncomment this, it will end the channel too early
})

this._source = this._msgs

this.sink = (read) => {
const next = looper(() => {
read(null, (end, data) => {
// stream already ended
if (this._endedLocal) { return }

this._endedLocal = end || false

// source ended, close the stream
if (end === true) {
return this.endChan()
}

// source errored, reset stream
if (end || this._reset) {
this.resetChan()
this.emit('error', end || this._reset)
this.reset()
return
}

// just send
this.sendMsg(data)
next()
})
})
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The above creates a lot of closures. I'd recommend to refactor this logic into a top level or instance function to ease V8 optimizer job.


next()
}
}

get source () {
return this._source
}

get id () {
return this._id
}

get open () {
return this._open
}

set open (open) {
this._open = open
}

get name () {
return this._name
}

get destroyed () {
return this._endedRemote && this._endedLocal
}

push (data) {
this._log('push')
this._msgs.push(data)
}

// close for reading
close (err) {
this._log('close', err)
if (!this._endedRemote) {
this._endedRemote = err || true
this._msgs.end(this._endedRemote)
this.emit('close', err)
this.plex = null
}
}

reset (err) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is often called destroy() in Node lingo. Should this have a callback?

this._log('reset', err)
this._reset = err || 'channel reset!'
this.close(this._reset)
}

openChan (name) {
if (this.open) { return } // chan already open

this.open = true
this._plex.push([
this._id,
consts.type.NEW,
name !== this._id.toString() ? name : this._id.toString()
])
}

sendMsg (data) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this have a callback?

this._log('sendMsg')

if (!this.open) {
this.openChan(this.name)
}

this._plex.push([
this._id,
this.MSG,
data
])
}

endChan () {
this._log('endChan')

if (!this.open) {
return
}

this._plex.push([
this._id,
this.END
])
}

resetChan () {
this._log('resetChan')

if (!this.open) {
return
}

this._plex.push([
this._id,
this.RESET
])
}
}

module.exports = Channel
Loading