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

Typescript Encode/Decode delimited error with pbjs/pbts gencode #1063

Open
hellraisercenobit opened this issue Jun 4, 2018 · 10 comments
Open

Comments

@hellraisercenobit
Copy link

hellraisercenobit commented Jun 4, 2018

protobuf.js version: 6.8.6

Use pbjs to generate code from proto file (v2/v3)
pbjs -t static-module -w commonjs -o ./bundle.js ./bundle.proto && pbts -o ./bundle.d.ts ./bundle.js

Use encodeDelimited/decodeDelimited method
Try to decode message result to an error

Working with simple encode/decode
Working with single one encodeDelimited/decodeDelimited
Working with typescript decorators
Working with protobuf.load

import { Writer, Reader, Properties, Message } from 'protobufjs/minimal';
import { ActionMessage, HeaderMessage } from '../../../../protos/bundle.js';

let writer = new Writer();
let header = new HeaderMessage({type: 2, version: 0});
let action = new ActionMessage({width: 1024, height: 1024, index: 0});
HeaderMessage.encodeDelimited(header, writer);
ActionMessage.encodeDelimited(action, writer);
let encoded = writer.finish();

let reader = new Reader(encoded);
console.log('encode test end', HeaderMessage.decodeDelimited(reader), ActionMessage.decodeDelimited(reader));
Error: invalid wire type 4 at offset 2
    at Reader.push../node_modules/protobufjs/src/reader.js.Reader.skipType (reader.js:375)
@hellraisercenobit hellraisercenobit changed the title Encode/DecodeDelimited not working with pbjs cli generated file Typescript Encode/Decode delimited error with pbjs/pbts gencode Jun 5, 2018
@hellraisercenobit
Copy link
Author

It is possible to gen/export proto3 file from typescript messages class?
It could be cool for a workaround to share proto3 between client/server :)

@hellraisercenobit
Copy link
Author

hellraisercenobit commented Jul 24, 2018

Not resolved with v6.8.8

@dcodeIO
Copy link
Member

dcodeIO commented Jul 24, 2018

Does this also happen when using Writer.create, Reader.create, MyMessage.create instead of new? If so, would you share the .proto definition?

@hellraisercenobit
Copy link
Author

hellraisercenobit commented Jul 24, 2018

create instead of new, result to the same issue. Its not the same behavior like some others issues.

Its seems its coming from generator process.

I could copy proto file tomorrow.

Thanks for the help

@hellraisercenobit
Copy link
Author

hellraisercenobit commented Jul 25, 2018

I have tried just in case:

import { Writer, Reader, Properties, Message } from 'protobufjs/minimal';
import { ActionMessage, HeaderMessage } from '../../../../protos/bundle.js';

let writer = Writer.create();
let header = HeaderMessage.create({type: 2, version: 0});
let action = ActionMessage.create({width: 1024, height: 1024, index: 0});
HeaderMessage.encodeDelimited(header, writer);
ActionMessage.encodeDelimited(action, writer);
let encoded = writer.finish();

let reader = Reader.create(encoded);
console.log('encode test end', HeaderMessage.decodeDelimited(reader), ActionMessage.decodeDelimited(reader));

Nothing good

@hellraisercenobit
Copy link
Author

hellraisercenobit commented Jul 25, 2018

Proto file sample:

syntax = "proto2";

message HeaderMessage {
  required uint32 version = 1;
  required uint32 type = 2;
}

message ActionMessage {
  required uint32 width = 1;
  required uint32 height = 2;
  required uint32 index= 3;
}

@hellraisercenobit
Copy link
Author

It happens on second (or more) "decodeDelimited" call only.
One call is ok, so maybe the reader position could be incorrect with the gencode or it could come from the writer too.

@hellraisercenobit
Copy link
Author

hellraisercenobit commented Jul 25, 2018

FOUND!

It's coming from encodeDelimited gencode.
Method is not the same as the default one used by decorator/loader.

Wrong encodeDelimited gencode:
return this.encode(message, writer).ldelim();

Should be:
return this.encode(message, writer && writer.len ? writer.fork() : writer).ldelim();

@hellraisercenobit
Copy link
Author

Pull request
#1093

@feywind
Copy link

feywind commented Feb 3, 2023

I'm having the same trouble here while playing around with a test thing, and I'm pretty sure it's a manifestation of the same bug, but in pbjs form:

#531

The dynamically loaded proto version works fine, but the pbjs one does not.

Anything I can do to help along that pull request?

(edit: apologies, letting my enthusiasm run ahead of me... not remotely urgent nor a work thing)

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

No branches or pull requests

3 participants