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

Allow GrpcWebStreamParser to accept Uint8Array #734

Merged
merged 5 commits into from
Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion javascript/net/grpc/web/grpcwebclientreadablestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ const GrpcWebClientReadableStream = function(genericTransportInterface) {
} else {
return;
}
var messages = self.parser_.parse([].slice.call(byteSource));
var messages = self.parser_.parse(byteSource);
if (messages) {
var FrameType = GrpcWebStreamParser.FrameType;
for (var i = 0; i < messages.length; i++) {
Expand Down
18 changes: 15 additions & 3 deletions javascript/net/grpc/web/grpcwebstreamparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,28 @@ Parser.prototype.error_ = function(inputBytes, pos, errorMsg) {


/**
* Parse the new input.
*
* Note that there is no Parser state to indicate the end of a stream.
*
* @param {string|!ArrayBuffer|!Uint8Array|!Array<number>} input The input data
* @throws {!Error} Throws an error message if the input is invalid.
* @override
Copy link
Collaborator

Choose a reason for hiding this comment

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

Sorry another issue: apparently you need @override here because now the spec differs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

* @return {?Array<string|!Object>} any parsed objects (atomic messages)
* in an array, or null if more data needs be read to parse any new object.
*/
GrpcWebStreamParser.prototype.parse = function(input) {
asserts.assert(input instanceof Array || input instanceof ArrayBuffer);
asserts.assert(input instanceof Array || input instanceof ArrayBuffer || input instanceof Uint8Array);

var parser = this;
var inputBytes = (input instanceof Array) ? input : new Uint8Array(input);
var inputBytes;
var pos = 0;

if (input instanceof Uint8Array || input instanceof Array) {
inputBytes = input;
} else {
inputBytes = new Uint8Array(input);
}

while (pos < inputBytes.length) {
switch (parser.state_) {
case Parser.State_.INVALID: {
Expand Down