-
-
Notifications
You must be signed in to change notification settings - Fork 36
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
decode without using the entire input #61
Comments
Sorry I don't have much experience with parsers and how they usually deal with this. I ended up using another lib that behaves pretty much like json parser - option a). Since I'm dealing with chunks of data coming from a network connection, I had to expect chunks with incomplete bencode parts as well as chunks with multiple bencode parts and I had to try to decode them as I received them because the message saying that I could stop listening to the connection is actually in those bencoded parts. So I did a method that received a
Could you elaborate on how your potential Anyway, if it's not a common case, I think it's safe to just go with option a). |
in case we do (b), the parser would work as it currently does. There would be a bencode.decode.resume() function that checks if the internal cursor is at the end of the internal buffer (decode uses global state all over the place anyway) and if there are bytes left to be read it would just start another decode() on the remaining buffer. const input = buffer_from_source()
let results = [ bencode.decode(input) ]
while(let more = bencode.decode.resume()) {
results.push(more)
} Maybe a "canResume" method could be of good use so you can actually check if theres more data before calling resume. |
Oh I understand better your idea now.
|
while strict parser should choose 3., you should currently be able to call decode.next() and get the next result. Thats pretty close to what a Throwing an exception would also require a new major release since it would change the current behavior in these cases. @jhermsmeier what do you think about a 2.0 branch that throws an exception and offers a decodeGreedy (name to be fixed) method that returns something clever that contains (or returns) all results. Like an iterator? |
Like iterators! |
Not sure if this is a bug or not but I'd like to discuss how decode should behave when we feed multiple parts.
Example:
The input has more data than that. When we use the decode function, it silently returns just the first decoded part. Is this intended?
I saw some cases where a single buffer they wanted to decode had multiple bencoded parts (it was some data received from the network they had no control over). And when they used this lib, it was silently missing parts. So they had to implement some logic to check if the decoded result was everything the input had or not. They managed to get everything working, but I'm wondering if the lib should give a heads up (exception or whatever) when this happens.
The text was updated successfully, but these errors were encountered: