-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Counterpart to writeDelimitedTo/parseDelimitedFrom #115
Comments
FWIW I ended up just implementing two stream transforms. One accepts a delimited stream, reads the length and emits full message chunks, which can then be decoded very easily. The other one automatically prepends every write with a delimiter. |
Hm, I'm missing this too - I'd like to read from a stream ( @sorccu, do you have any code you can share? Or is there any cute idiom that makes this easy? |
@joliss See https://gist.github.com/sorccu/63cdb5cb55df46e26a17. It could be made a little cleaner and more efficient but it works like you want. |
Thanks @sorccu! |
I've adapted Message#decodeDelimited a bit to make it more useful when streaming data: https://github.com/dcodeIO/ProtoBuf.js/blob/master/src/ProtoBuf/Builder/Message.js#L473 It now reads the preceding varint length and simply returns Hope this helps. If you have any additional suggestions, let me know. |
@dcodeIO Can you show me an example of this? I'm looking for a way to buffer an stream of a socket. It would be nice to have an api like this:
|
This is what I ended up doing var server = net.createServer(function (socket) {
var buffer;
socket.pipe(through(function (chunk) {
chunk = ByteBuffer.wrap(chunk);
buffer = buffer ? ByteBuffer.concat([buffer, chunk]) : chunk;
var decoded;
while (buffer.remaining() > 0 && (decoded = Request.decodeDelimited(buffer))) {
buffer.compact();
this.queue(decoded);
}
})).on('data', function (m) {
console.log(m);
});
}); |
Spent few hours looking for solution, and here's my working version: function transformFromProtobuf<OUT>(type: protobuf.Type): TransformTyped<Buffer, OUT> {
let remainder = Buffer.alloc(0)
return new Transform({
objectMode: false,
readableObjectMode: true,
async transform(chunk: Buffer, _encoding, cb) {
// console.log(chunk)
const buf = Buffer.concat([remainder, chunk])
let lastPos = 0
const reader = protobuf.Reader.create(buf)
try {
while (reader.pos < reader.len) {
lastPos = reader.pos
const frame = type.decodeDelimited(reader)
this.push(frame)
}
remainder = Buffer.alloc(0)
} catch (err) {
// console.log({pos: reader.pos, lastPos, len: reader.len}, 'got err')
remainder = buf.slice(lastPos)
}
// console.log(`remainder size: ${remainder.length}`)
cb() // finished processing
},
})
} |
I often find myself missing the built-in parseDelimitedFrom and writeDelimitedTo from the official Java API.
You can kind of replicate the functionality with this:
... but it's obviously a bit of a pain. Now I know that for example the C++ API doesn't have these methods either, but then again it's C++, where few things are convenient.
I may have missed an easier method to achieve the same result (if so, please let me know), but it would be great if we could get some kind of an
encodeDelimited
anddecodeDelimited
methods for convenience. Or, it would at least be nice to have a one-liner to create the delimiter without having to require ByteBuffer.The text was updated successfully, but these errors were encountered: