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

Stream for encoding #18

Open
arestov opened this issue May 18, 2014 · 7 comments
Open

Stream for encoding #18

arestov opened this issue May 18, 2014 · 7 comments

Comments

@arestov
Copy link

arestov commented May 18, 2014

Currently we can stream file and decoding it.
Is is possible to implement encoding stream?

I want to do this

var cryptoStream = crypto.createHash('sha1');
cryptoStream.on('end', function(){
    console.log(cryptoStream.digest('hex'));
});

var bencStream = new benc.EncodeStream({prop: []});
bencStream.on('data', function(chunk){
    cryptoStream.update(chunk);
});
bencStream.on('end', function(){
    cryptoStream.update(null);
});
@a2800276
Copy link
Owner

Not sure if I understand your question, but yes the library can encode bencoding.
Could you describe what you want to do instead of just posting code?

@feross
Copy link
Contributor

feross commented May 18, 2014

I think he's asking for the ability to do streaming decoding as the bits arrive on the wire.

@a2800276
Copy link
Owner

The text & sample are pretty clear about encoding not decoding...

Not sure why you'd need a stream interface to encode a Javascript Object to beencoding...? Not sure to what extent this would even be possible.

I have the feeling he wants to stream a datafile somewhere, and as it's being streamed, magically create the beencoded meta-data?

Streaming decoding of deencoded dictonionaries works with the Stream object somebody implemented (I think you, even :).

Or maybe I'm just being obtuse and am missing something entirely obvious...

@arestov
Copy link
Author

arestov commented May 18, 2014

For example I want to create torrent file, from list of files. The list is big: 2000 files.

To create torrent I must create infoDictionary. (using benc.encode)
Then calculate infoHash (sha1).

Both of this operations are heavy, so them can freeze application. So I want to be able to make this operations asynchronously.

The real reason is that https://github.com/mafintosh/torrent-stream completely revalidates torrent infoDictionary before use the torrent.
torrent-stream revalidates it by calculating benc.encode and sha1 from scratch. https://github.com/mafintosh/torrent-stream/blob/master/encode-metadata.js

My application automatically search torrents which it uses. And I found torrent which is 2.5 MB, so application freezed for few minutes.
I think I will filter out such torrents and I doubt about reasonableness of the revalidation, but anyway asynchronous encoding may be usefull.

@a2800276
Copy link
Owner

I think I'm still not entirely clear what the problem is. Currently, the encode function takes the infoDictionary to be encoded. I can't really think a a scenario that the infoDict to encode is so huge that it would take more than a negligible amount of time.

As I see it, the bottleneck is probably calculating the sha hash. You could try to pass the Buffer containing the encoded infodict to sha in pieces. I guess you separate the infoDict into pieces and call the various encode methods inside of BEncode directly, but that doesn't really seem viable, at the least, it will require reworking the internals substantially.

If you can post the dict you're trying to encode I can try to reproduce any performance issues you're having.

Finally, from my point of view, it's unecessary to decode and reencode the dict to get the sha1 hash, you could just run the stream through the hash while you're receiving it.

@albertalef
Copy link

This can be closed. right?

@a2800276
Copy link
Owner

a2800276 commented Nov 9, 2024

I guess. At least if you understood the original posters problem and it confuses you as it did me 14 years ago and you feel that this resolves it? May I ask why you're interested in a 14 year old issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants