Hello, I'm running protobufjs version 6.0.1 and I'm getting this error when try to encode a json:
/home/rodrigo/src/nodejs/socket/node_modules/protobufjs/src/writer.js:518
val.copy(buf, pos, 0, val.length);
^
TypeError: val.copy is not a function
at Op.writeBytesBuffer [as fn] (/home/rodrigo/src/nodejs/socket/node_modules/protobufjs/src/writer.js:518:13)
at BufferWriter.finish_buffer [as finish] (/home/rodrigo/src/nodejs/socket/node_modules/protobufjs/src/writer.js:559:14)
at Function.encode (/home/rodrigo/src/nodejs/socket/node_modules/protobufjs/src/inherits.js:68:63)
at Object.<anonymous> (/home/rodrigo/src/nodejs/socket/server.js:37:20)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
The following code generate the error above. I'm doing something wrong? The documentation looks outdated
const protobuf = require("protobufjs")
const event = protobuf.parse(fs.readFileSync('./event.proto').toString())
const Prototype = protobuf.Prototype
function Event(properties) {
Prototype.call(this, properties)
}
protobuf.inherits(Event, event.root.lookup('Event'))
var evt = new Event(
{
"_developerApplication": {},
"creator": {
"name": "Test"
},
"_responseConfigError": {},
"_userAgent": "Dalvik/2.1.0 (Linux; U; Android 6.0.1; SM-J500M Build/MMB29M)",
"version": "1.2",
"tags": ["_successful"],
"entries": {}
}
)
var buffer = Event.encode(evt)
Looks like one of your fields is of type bytes but isn't set to a buffer. What's in event.proto?
On docs: The wiki refers to protobuf.js 5 but you are currently using protobuf.js 6 (see the README or the API documentation).
Hi @dcodeIO, this is my event.proto file:
syntax = "proto3";
option go_package = "protobuf";
message Event {
Creator creator = 1;
Organization _organization = 2;
API _api = 3;
Resource _resource = 4;
}
message Creator {
string name = 1;
}
message Organization {
int64 id = 1;
string name = 2;
string slug = 3;
}
message API {
int64 id = 1;
string path = 2;
string name = 3;
Version version = 4;
}
message Version {
int64 id = 1;
string path = 2;
int64 package = 3;
bytes package_name = 4;
bool package_auto_deployable = 5;
}
message Resource {
int64 id = 1;
string path = 2;
bytes name = 3;
Endpoint endpoint = 4;
}
message Endpoint {
string url = 1;
string encoding = 2;
}
Thank you for the hint, I realized that i was using a wrong json to create the event, so this is the correct one. However, the problem still occurring.
var evt = new Event(
{
"creator": {
"name": "Me"
},
"_organization": {
"id": 1,
"name": "Test",
"slug": "test"
},
"_api": {
"id": 1,
"path": "/test",
"name": "Candies API",
"version": {
"id": 1,
"path": "/v1",
"package": 10,
"package_name": "dGVzdGluZyBwcm90b2J1Zg==",
"package_auto_deployable": true
}
},
"_resource": {
"id": 1,
"path": "/choco",
"name": "Q2hvY29sYXRlIHJlc291cmNl",
"endpoint": {
"url": "http://endpoint:8080/",
"encoding": "UTF-8"
}
}
}
)
If I remove this field:
"name": "Q2hvY29sYXRlIHJlc291cmNl"
The code works...
Looks like this field is base64 encoded. At the moment, there is no base64 decoder included, but if you change this field to a buffer (i.e. Buffer.from("Q2hvY29sYXRlIHJlc291cmNl", "base64")) it should work.
Here doesn't is buf.copy instead of val.copy ?
function writeBytesBuffer(buf, pos, val) {
if (val.length)
val.copy(buf, pos, 0, val.length);
}
This fix my issue.
This copies buf to buf but the actual value is in val. Might not throw, but doesn't do what you want.
True, I'll do as you suggested, thanks!