-
Notifications
You must be signed in to change notification settings - Fork 3
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
Changes from 46 commits
Commits
Show all changes
50 commits
Select commit
Hold shift + click to select a range
3c5caed
feat: initial commit
dryajov 7c6bdf1
feat: correct channel closing
dryajov 2798b03
feat: getting interface-stream-muxer tests to pass
dryajov 0987b24
feat: improve performance
dryajov c8f2115
feat: correct encoding/decoding
dryajov 117adbb
feat: correct encoding and channel closing
dryajov fb79753
feat: correct channel and plex closing
dryajov 284fd44
feat: interop with old libp2p-mplex
dryajov aeee3d9
fix: correctly read chunked data
dryajov 1a97616
feat: interep with old stream based mplex
dryajov e2f4579
feat: avoid negative sizes
dryajov 56694e0
feat: dont close channel too early
dryajov e9cd98d
feat: validation and coverage
dryajov 79f5a60
fix: correct channel id sequence
dryajov 487ff5d
feat: use bl to avoid buffer concat/copy
dryajov 2272897
chore: renaming back to pull-mplex
dryajov de7f5bb
chore: rename old to stream-mplex-interop
dryajov 66473b5
chore: adding misc files
dryajov bfd691e
chore: license
dryajov 3bc0908
feat: use two lists to avoid clashes
dryajov 18d43eb
style: formatting
dryajov dc95ed9
feat: remove unused deps
dryajov ff8e56a
feat: update README.md
dryajov b340326
feat: performance
dryajov 9a5a867
chore: update repo link
dryajov 6659c45
fix: dont use bl
dryajov 86eab54
perf: optimize
dryajov 1e9f794
style: fix formating
dryajov 64794fe
perf: improoving performance
dryajov 8c26286
style: remove unused variable
dryajov fa387c5
fix: msg length encoding
dryajov adc1884
fix: dont blow up the stack
dryajov 0fc4ca1
fix: use arrow function
dryajov 5c2c914
fix: remove initiator id logic
dryajov 3b29ca5
fix: start channel from 0
dryajov 1dbe5dc
chore: adding commitizen
dryajov 01fd7e3
feat: initial commit
dryajov 54a249b
feat: getting interface-stream-muxer tests to pass
dryajov 4c537d5
chore: remove unused file
dryajov 5431c65
feat: send chan name
dryajov 7f67df8
fix: send chan name when creating stream
dryajov c8a1c7e
fix: remove nyc_output file
dryajov a1d9788
chore: move benchmarks and inteop to libp2p-mplex
dryajov 22ef8d0
chore: remove non jenkins ci
jacobheun b895055
chore: update dependencies
jacobheun b59b1fe
fix: listen to the channel error
jacobheun 0eaf7d8
refactor: spread op instead of lodash defaults
jacobheun cb8b719
refactor: avoid toString when debug not enabled
jacobheun 03520cc
docs: add more comments
jacobheun ef06716
docs: add more js docs
jacobheun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) | ||
|
||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is often called |
||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.