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

Support size-delimited messages #387

Merged
merged 4 commits into from
Mar 1, 2023
Merged

Support size-delimited messages #387

merged 4 commits into from
Mar 1, 2023

Conversation

timostamm
Copy link
Member

@timostamm timostamm commented Feb 6, 2023

This implements support for size-delimited messages described in protocolbuffers/protobuf#10229

A size-delimited message is a varint size in bytes, followed by exactly that many bytes of a message serialized with the binary format. This implementation is compatible with the counterparts in C++, Java, Go, and others.

You create such a message with protoDelimited.enc:

import { protoDelimited } from "@bufbuild/protobuf";

const bytes = protoDelimited.enc(new User({ firstName: "John" }));
const user = protoDelimited.dec(User, bytes);

With protoDelimited.decStream, you can parse messages from a stream. The method expects an AsyncIterable<Uint8Array> as a stream input, so it works with Node.js streams out of the box, and can be easily adapted to other stream APIs:

import { protoDelimited } from "@bufbuild/protobuf";
import { createReadStream, createWriteStream } from "fs";
import { tmpdir } from "os";
import { join } from "path";

// Let's write a couple of messages to a file
const ws = createWriteStream("protoDelimited.bin", {encoding: "binary"});
ws.write(protoDelimited.enc(new User({ firstName: "John" })));
ws.write(protoDelimited.enc(new User({ firstName: "Max" })));
ws.write(protoDelimited.enc(new User({ firstName: "Max" })));
ws.end();
ws.close();

// Now we can parse them from the stream
const readStream = createReadStream("protoDelimited.bin");
for await (const user of protoDelimited.decStream(User, readStream)) {
  console.log(user);
}

Copy link
Member

@akshayjshah akshayjshah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm almost completely unable to parse the actual code, but the unit tests look nice! As long as we're following the proposal in the discussion you linked, this lgtm. (I assume that short names like enc and dec match the rest of the code.)

@timostamm timostamm changed the title Basic support for size-delimited messages Support size-delimited messages Feb 28, 2023
@timostamm
Copy link
Member Author

Have added the decStream method to the object after running some tests. This PR also adds documentation now.

@timostamm timostamm requested a review from smaye81 February 28, 2023 19:01
@timostamm timostamm merged commit 882461e into main Mar 1, 2023
@timostamm timostamm deleted the tstamm/proto-delimited branch March 1, 2023 08:35
This was referenced Mar 1, 2023
smaye81 added a commit that referenced this pull request Mar 2, 2023
This release includes the following:

## Enhancements
* Support size-delimited messages by @timostamm in #387.

## Bug Fixes
*  Upgrade to Protobuf v22.0 by @smaye81 in #394.
* Fix type declaration emitting when using NodeNext module resolution by
@fubhy in #398.
*  Strip rewrite_imports parameter by @smaye81 in #386.

## New Contributors
* @s-hakase made their first contribution in #367.
* @balzdur made their first contribution in #368.
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

Successfully merging this pull request may close these issues.

3 participants