serializeBinary() not found using google-protobuf on nodejs - node.js

I could really use some help. I am trying to follow along on the google-protobuf example:
https://www.npmjs.com/package/google-protobuf
However, when I try this line of code:
// Serializes to a UInt8Array.
var bytes = message.serializeBinary();
I get a Type Error saying that serializeBinary is not a function.
Is there a different function, or something I am missing? I could really use the help.
Best Regards,
Mike

Is the problem solved now?
I had the same problem, until I realized maybe I wasn't creating a real proto object and that is why it cannot recognize serializeBinary() as a function.
True enough, my message was actually a JavaScript object.
So I had to build a proto object by following this article.
https://ednsquare.com/story/working-with-protocol-buffers-in-javascript------MaDIJH
Basically, you have to initialize your proto object and then add properties to it through the set methods. In this way, the proto object is properly built. And therefore, should have the serializeBinary() function.

Related

Azure Functions .Net 5: Is it possible to implement POCO binding somehow?

When moving my functions to .net5 I faced the fact that POCO binding that worked fine with 3.1 is not applicable with .net 5 anymore for some reason. They say it will be implemented at some point maybe, but for the certain reasons I need it now. Tried to find some hacky way to implement this, but failed. The best thought I had was to implement explicit operator in my DTO object which will cast HttpRequestData to it's type, but the problem is that HttpRequestData is an abstract type, and it's concrete implementation type is internal. After that I tried to cast the input parameter to HttpRequestData in middleware with reflection, but parameters are stored in IReadOnlyDictionary which is immutable. So I ran out of ideas now. Maybe someone found workaround to this and can kindly share, would be much appreciated.
I suppose you're using the "dotnet-isolated" mode (only way to run on .NET 5).
I'm trying to find a more elegant solution to this as well.
Meanwhile, what I did was to deserialize the data myself, inside the function.
var body = await new StreamReader(request.Body).ReadToEndAsync();
var myobject = JsonSerializer.Deserialize<MyPocoClass>(json);
I would really prefer if the runtime did it by itself, but I couln't find a way yet. I read somewhere that it is possible to create our own binding code, but I haven't tried it.
I noticed that I could bind to individual properties of the json payload, but not to an object...
I hope this arrives in Azure Functions v4 + .NET6, since it is right around the corner.

In Javascript (Node.js), Is It Possible To Pass an Already Instantiated Class Object into Another Code Module

I'm probably missing a larger point of Javascript here, but I wanted to ask the community if the answer is 'NO!'.
Let's say you have an index.js that requires a udp port module:
index.js:
let port1 = require(udp_port.js);
port1.start( { port: 1234, classObj: new myClassObj() } );
udp_port.js:
let dgram = require('dgram');
let msgProcessor; // This is the class obj I'm trying to pass in from index.js
let server = dgram.createSocket('udp4');
exports.start = function(configObj) {
msgProcessor = configObj.classObj; // Can I do this???
}
Any advice would be great at this point, thanks.
In Javascript (Node.js), Is It Possible To Pass an Already Instantiated Class Object into Another Code Module
Yes, it is perfectly possible to pass an instantiated object created in one module to another module. That is done all the time.
Integers and Strings absolutely, but can you pass in a class object? Something you instantiated with the keyword 'new'?
Yes, something instantiated with new can be passed. There are no limitations on what you can pass. Any Javascript type of data. Separate modules loaded into the same nodejs program all run in the same Javascript interpreter. There are no limitations at all on how they can share data with each other.
#James Yep, I had code that I ran before posting but saw mixed results. The code I put in this post was code I cut out of my program. I ended up solving the problem though. What I thought was a limitation of passing variables, was actually a problem related to globals inside a container module (that the instantiated class object was getting passed into).
I was declaring multiple container variables and the global inside was getting reassigned/overwrote with each instantiated class object I passed in.
port1.start(...);
port2.start(...);
had 'msgProcessor' getting assigned twice. Yikes!

How do i convert a custom object to the bytes data type and back again?

I am trying to follow the websocket tutorial that can be found here in the readme of this python github repo:
https://github.com/aaugustin/websockets
For my use case, I want the client to not pass a string to the websocket server but rather an object. When I tried replacing the generic "Hello World!" parameter that the client sends to the server though I get the following error:
TypeError: data must be bytes or str
Ok, makes sense. Obviously websocket requires a string or a bytes object to be passed from client to server. My question is how do i easily convert a generic object of some custom class I've created to the bytes/string type using the best practice problem. Obviously, I would also like to be able to convert the object back from the bytes class to the original class type I have declared.
When searching I couldn't find anything talking about how to do this (only how to do this for strings) and tried hard casting by passing my object into the bytes() method but this threw an error.
Thoughts?
I am an idiot. Converting the object to JSON works.

NonProxyHosts usage with Groovy HttpBuilder

If I create my httpBuilder as shown below (assume that a proxyUsername IS set, so setCredentials is called), then calls to httpAddress-es that are passed in properly are routed through the proxy. However, the Application has some http calls that are within the local network. Can http.nonProxyHosts be used to work around this and bypass the Proxy? If so, how? Use System.setProperty? Or something on HttpBuilder?
HTTPBuilder httpBuilder = new HTTPBuilder(httpAddress)
httpBuilder.setProxy(webProxyHost, webProxyPort, webProxyProtocol)
if (proxyUsername) {
httpBuilder.client.getCredentialsProvider().setCredentials(
new AuthScope(webProxyHost, webProxyPort),
new UsernamePasswordCredentials(proxyUsername, proxyPassword))
}
}
In the code above, all of the various named elements (webProxyHost, etc) are declared as String and set accordingly.
In answer to the question in the above comment, our primary 'nonProxyHost' need was for 'localhost' which is there by default. Thus this ceased to be an issue. Did not ever really find out how to accomplish this as it is somewhat version-specific on HttpClient.
You can set the System property:
System.setProperty('http.nonProxyHosts', myNonProxyHosts)
However, if you call 'setProxy' on HttpBuilder, even if you call 'useSystemProperties' it will not. This is in their documentation, just not obvious!
Finally, you might be able to call:
httpBuilder.client.params.setParameter('http.nonProxyHosts', myNonProxyHosts)
But I do not know for sure if that is the property name and documentation of those properties is hard to find. Worse - those 'params' are deprecated - you are supposed to use the better 'config' classes, though once again finding comprehensive documentation on all the parameters for that is not the easiest! Wish I could have been of more help!

The specified object is not recognized as a fake object. Issue

I am having an issue where a FakeItEasy call in an extremely simple test is failing with the error "The specified object is not recognized as a fake object." The call is simple:
A.CallTo(myService.MyMethod(listOfStringsFilter)).MustHaveHappened();
The fake is similarly simple (A.Fake()), and fakes out an interfance with one method, that takes in a list and returns a list. In debug mode, I see the instance of myService is of type {Fake IMyInterface}. Anyway, this issue is really holding me up, thanks in advance for your help.
Update:
This was my own darn mistake, I needed to make the call say:
A.CallTo(() => myService.MyMethod(listOfStringsFilter)).MustHaveHappened();
This was my own darn mistake, I needed to make the call say:
A.CallTo(() => myService.MyMethod(listOfStringsFilter)).MustHaveHappened();

Resources