-
Notifications
You must be signed in to change notification settings - Fork 35
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
How to parse multiple objects #38
Comments
I found a solution eventually. Not sure it's the ideal one, but it seems to be working. The idea is that I keep iterating over the data buffer until:
For every iteration, the buffer is slice with the This is a streaming implementation of this approach. const { Transform } = require('stream')
const capnp = require('capnp')
class CapnpReaderStream extends Transform {
constructor (schema, options = {}) {
options.objectMode = true
super(options)
this.schema = schema
this.buffer = null
}
_transform (chunk, encoding, callback) {
if (!this.buffer) {
this.buffer = chunk
} else {
this.buffer = Buffer.concat([this.buffer, chunk])
}
let expectedSize = capnp.expectedSizeFromPrefix(this.buffer)
let data = null
while (this.buffer.length >= expectedSize) {
data = capnp.parse(this.schema, this.buffer)
this.buffer = this.buffer.slice(expectedSize)
expectedSize = capnp.expectedSizeFromPrefix(this.buffer)
this.push(data)
}
callback()
}
}
module.exports = CapnpReaderStream Please @kentonv, let me know if this approach makes sense of if there are better approaches. |
Turned this into an npm module https://github.com/lmammino/capnp-stream/ I will probably add a writable equivalent Looking forward for @kentonv feedback |
Hmm, I guess that's probably the best way to do it given the current interface. In C++ you would read a stream by constructing a new I'd be happy to accept a PR implementing a new variant of parse() that also returns the endpoint of the message, or returns the slice of the buffer after the message end. |
Thanks, @kentonv. This approach doesn't seem to work when using the Regarding the PR, unfortunately, I don't have experience in porting c/c++ modules to Node.js |
|
Is it possible to parse a buffer that contains multiple objects?
After a 30-minutes investigation, I couldn't figure out a way to do it.
The text was updated successfully, but these errors were encountered: