protobuf.js icon indicating copy to clipboard operation
protobuf.js copied to clipboard

Error: invalid wire type 6 at offset 297 is reported in the parsing field

Open a554878526 opened this issue 1 year ago • 1 comments

protobuf.js version: ^7.2.6

This data is serialized to binary data using c++ protobuf, and can be deserialized using c++ or net

message


message SpLocation {
  optional int32 layer = 2;           
}
message SpLocationMsg {
  optional SpLocation location = 2; 
}
import protobufjs from 'protobufjs';
import { FileDescriptorSet } from 'protobufjs/ext/descriptor';
import { useState } from 'react';


function parseProtobufSchema(schemaName, schemaData) {
  const descriptorSet = FileDescriptorSet.decode(schemaData);

  const root = protobufjs.Root.fromDescriptor(descriptorSet);
  root.resolveAll();
  const rootType = root.lookupType(schemaName);
 
  const deserialize = data => {
    return rootType.toObject(rootType.decode(new Uint8Array(data.buffer)), {
      defaults: true,
    });
  };

  return { deserialize };
}

const App = () => {
  const [json, setJson] = useState({});

  const handleFileChange = event => {
    const file = event.target.files[0];
    const reader = new FileReader();
    reader.onload = async () => {
      const resultArr = [
        10, 39, 10, 9, 83, 68, 47, 86, 48, 46, 50, 46, 56, 18, 10, 49, 57, 48,
        48, 47, 48, 49, 47, 48, 49, 24, 187, 140, 198, 237, 214, 49, 32, 0, 50,
        5, 108, 111, 99, 97, 108, 18, 205, 2, 10, 245, 1, 8, 228, 156, 198, 237,
        214, 49, 18, 16, 115, 117, 112, 101, 114, 112, 97, 114, 107, 105, 110,
        103, 95, 109, 97, 112, 26, 7, 99, 104, 97, 115, 115, 105, 115, 32, 0,
        42, 36, 9, 3, 12, 248, 228, 199, 36, 117, 63, 17, 31, 255, 68, 252, 113,
        19, 117, 191, 25, 205, 155, 21, 151, 146, 170, 230, 63, 33, 246, 182, 5,
        211, 165, 150, 230, 63, 50, 27, 9, 204, 53, 40, 232, 196, 88, 42, 192,
        17, 183, 192, 185, 131, 240, 1, 56, 64, 25, 0, 0, 0, 0, 0, 0, 48, 192,
        58, 72, 233, 205, 232, 169, 52, 131, 192, 63, 254, 80, 59, 239, 33, 152,
        88, 191, 43, 56, 95, 43, 94, 55, 103, 63, 255, 80, 59, 239, 33, 152, 88,
        191, 130, 132, 227, 221, 243, 13, 225, 63, 81, 83, 146, 26, 224, 232,
        227, 62, 46, 56, 95, 43, 94, 55, 103, 63, 82, 83, 146, 26, 224, 232,
        227, 62, 138, 28, 30, 120, 223, 241, 94, 63, 66, 27, 9, 0, 0, 0, 0, 0,
        0, 0, 0, 17, 0, 0, 0, 0, 0, 0, 0, 0, 25, 0, 0, 0, 0, 0, 0, 0, 0, 74, 27,
        9, 196, 96, 169, 80, 195, 139, 86, 64, 17, 20, 64, 120, 198, 13, 186,
        234, 191, 25, 0, 0, 0, 32, 213, 253, 53, 63, 82, 8, 0, 0, 0, 0, 0, 0, 0,
        0, 18, 27, 17, 91, 54, 73, 7, 167, 97, 44, 63, 25, 129, 116, 191, 193,
        164, 111, 103, 192, 41, 95, 174, 231, 210, 187, 99, 46, 63, 40, 2, 50,
        1, 0, 58, 1, 0, 64, 2, 74, 8, 217, 55, 146, 42, 9, 33, 50, 48, 88, 100,
        26, 15, 13, 0, 0, 0, 0, 21, 0, 0, 0, 0, 29, 0, 0, 0, 0, 34, 15, 13, 27,
        94, 180, 66, 21, 110, 208, 85, 191, 29, 169, 238, 175, 57, 26, 0,
      ];
      const result = new Uint8Array(resultArr);
      const schema = fetch('ndm_proto_v2.SpLocationMsg');
      const schemaBuf = await (await schema).arrayBuffer();
      const channel = parseProtobufSchema(
        'ndm_proto_v2.SpLocationMsg',
        new Uint8Array(schemaBuf)
      );
      const result2 = channel.deserialize(result);
      setJson(result2);
    };
    reader.readAsArrayBuffer(file);
  };

  return (
    <div>
      <input type='file' onChange={handleFileChange} />
      <pre>{JSON.stringify(json, null, 2)}</pre>
    </div>
  );
};

export default App;


Uncaught (in promise) Error: invalid wire type 6 at offset 297
    at Reader.skipType (reader.js:382:19)
    at Type.SpLocation$decode [as decode] (eval at Codegen (index.js:50:33), <anonymous>:15:5)
    at Type.decode_setup [as decode] (type.js:507:25)
    at Type.SpLocationMsg$decode [as decode] (eval at Codegen (index.js:50:33), <anonymous>:11:23)
    at Type.decode_setup [as decode] (type.js:507:25)
    at Object.deserialize (App.jsx:67:39)
    at reader.onload (App.jsx:19:31)

a554878526 avatar Mar 13 '24 07:03 a554878526

Have you solved it? If so, can you share the solution?

JungminByun avatar Feb 14 '25 00:02 JungminByun