This repository has been archived by the owner on Oct 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: update LS to match the latest spec (#5)
Multistream spec: multiformats/multistream-select#19 fixes #4
- Loading branch information
Showing
7 changed files
with
52 additions
and
53 deletions.
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
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,38 +1,35 @@ | ||
'use strict' | ||
|
||
const log = require('debug')('it-multistream-select:handle') | ||
const { Buffer } = require('buffer') | ||
const BufferList = require('bl') | ||
const Varint = require('varint') | ||
const Multistream = require('./multistream') | ||
const multistream = require('./multistream') | ||
const toReaderWriter = require('./to-reader-writer') | ||
|
||
module.exports = async (stream, protocols) => { | ||
protocols = Array.isArray(protocols) ? protocols : [protocols] | ||
const { reader, writer, rest } = toReaderWriter(stream) | ||
|
||
while (true) { | ||
const protocol = (await Multistream.read(reader)).toString() | ||
const protocol = (await multistream.read(reader)).toString() | ||
log('read "%s"', protocol) | ||
|
||
if (protocols.includes(protocol)) { | ||
Multistream.write(writer, protocol) | ||
multistream.write(writer, protocol) | ||
log('write "%s" "%s"', protocol, protocol) | ||
writer.end() | ||
return { stream: rest, protocol } | ||
} | ||
|
||
if (protocol === 'ls') { | ||
// <varint-msg-len><varint-num-protos><varint-proto-name-len><proto-name>\n<varint-proto-name-len><proto-name>\n | ||
Multistream.write(writer, new BufferList([ | ||
Buffer.from(Varint.encode(protocols.length)), | ||
...protocols.map(p => Multistream.encode(p)) | ||
]).shallowSlice(0, -1)) | ||
// <varint-msg-len><varint-proto-name-len><proto-name>\n<varint-proto-name-len><proto-name>\n\n | ||
multistream.write(writer, new BufferList( | ||
protocols.map(p => multistream.encode(p)) | ||
)) | ||
log('write "%s" %s', protocol, protocols) | ||
continue | ||
} | ||
|
||
Multistream.write(writer, 'na') | ||
multistream.write(writer, 'na') | ||
log('write "%s" "na"', protocol) | ||
} | ||
} |
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
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
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
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
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