How to use protobufs in NodeJS? - node.js

I'm a NodeJS beginner and I'm trying to create a protobuf object in NodeJS but I don't seem to understand the concept yet.
The .proto file I'm working with is here: https://github.com/meshtastic/Meshtastic-protobufs/blob/ab16c249dd5ed99a26ee3fe76ec84808d53d791a/mesh.proto#L889
My code looks like this:
[…]
const root = protobuf.loadSync('Meshtastic-protobufs/mesh.proto');
const toRadio = root.lookupType('ToRadio');
[…]
const message = toRadio.create({
want_config_id: 12345678
});
const buffer = toRadio.encode(message).finish();
console.log(util.inspect(toRadio.decode(buffer)));
But apparenty, no protobuf object is created. The debug output from the console.log format shows ToRadio {}.
I'm looking for somewhat of a "protobufs for Dummies" explanation on how to read and use the proto files so that I can properly create objects.

Have a look at the quickstart and tutorial on the gRPC web site:
https://grpc.io/docs/languages/node/

Oh well, turns out, the parameter names in Node actually differ from the parameter names in the .proto file.
e.g. "want_config_id" in the proto file changes to "wantConfigId" in Node.

Related

fuse_js_1.default is not a constructor | Fuse.js in Typescript

i have a little API in express js with Typescript - Node 14 and i'm using fuse js to search a list of packages in the database. (the file name is server.ts)
This is how the ts code looks:
const fuse = new Fuse (list, options)
and compiles fine but when i deploy the code in the server show an error
... fuse_js_1.default is not a constructor ...
so i go to the server.js result file and the code is like this
const fuse = new fuse_js_1.default(list, options)
if i remove .default word, the code looks like this new fuse_js_1(list, options) the code runs fine and works!!
Is there a way to force Typescript not to compile this line? or remove the .default automatically? I accept your comments and ideas thanks :)
Sorry, bag english :/
I found the problem, change
import Fuse from "fuse.js"
to
const Fuse = require("fuse.js")
:)

how to save an image from discord using discord bot node js

So far I've only been able to get text and links from what other people on my discord channel type, but I want to be able to save posted images/gifs. is there any way I can do this through the bot or is it impossible? I'm using discord.js.
Images in Discord.js come in the form of MessageAttachments via Message#attachments. By looping through the amount of attachments, we can retrieve the raw file via MessageAttachment#attachment and the file type using MessageAttachment#name. Then, we use node's FileSystem to write the file onto the system. Here's a quick example. This example assumes you already have the message event and the message variable.
const fs = require('fs');
msg.attachments.forEach(a => {
fs.writeFileSync(`./${a.name}`, a.file); // Write the file to the system synchronously.
});
Please note that in a real world scenario you should surround the synchronous function with a try/catch statement, for errors.
Also note that, according to the docs, the attachment can be a stream. I have yet to have this happen in the real world, but if it does it might be worth checking if a is typeof Stream, and then using fs.createWriteStream and piping the file into it.

How can I capture the output of a webpack build as a string before it is written to disk?

There seems to be no output option in webpack to write the output as a string, either to a nodejs Buffer or to stdout. Googling has yielded nothing promising. Is this possible, via either configuration or 3rd-party-module?
You can write to an in-memory filesystem and read from it after compilation:
const compiler = webpack({ / options / });
const outputFileSystem = new webpack.MemoryOutputFileSystem()
compiler.outputFileSystem = outputFileSystem;
compiler.run((err, stats) => {
// Read the output later:
const content = outputFileSystem.readFileSync('...');
});
This involves calling webpack via its Node API, but the same may also be accomplished entirely in webpack.config.json by creating a custom webpack plugin that changes the compiler’s outputFileSystem on apply.
After researching, the only way I see to accomplish this would be to monkey-patch webpack's NodeOutputFileSystem.writeFile to capture the string contents.
I ended up using raw babeljs instead of webpack to get the string.

nodejs + express file upload tmp name unique

if I use post to upload a file to my nodejs server I get a temp that looks something like this:
/tmp/22608-16eior5.jpg
by using this:
var tmp_path = req.files.postImage.path;
My question is, would the name "22608-16eior5.jpg" be unique enough to be used as a unique filename for one folder full of thousands of others generated the same way?
Or is it recommended that I generate something myself?
May very well depend on just how many you'll be generating. If you're unsure, you might wanna take a look at the node-uuid package.
From the docs:
// Generate a v4 (random) id
uuid.v4(); // -> '110ec58a-a0f2-4ac4-8393-c866d813b8d1'

Nodejs: parsing XML, editing values, saving the end result using sax-js module

What I'd like to achieve is:
parsing a chunk of XML
editing some values
saving the end result in a
new xml file
The module is sax-js: https://github.com/isaacs/sax-js#readme
The module has some built-in mechanism to read/write any.
I thought the task would be a piece of cake; on the contrary I have been struggling with it for the whole day.
Here is my code:
var fs = require('fs');
var saxStream = require("sax").createStream(true);
saxStream.on("text", function (node) {
if (node === 'foo') { //the content I want to update
node = 'blabla';
}
});
fs.createReadStream("mysongs.xml")
.pipe(saxStream)
.pipe(fs.createWriteStream("mysongs-copy.xml"));
I did think that updating some content (see the comment above) would suffice to write the updated stream into a new file.
What's wrong with this code?
Thanks for your help,
Roland
The sax module doesn't let you modify nodes like that. If you take a look at this bit of code, you'll see that the input is passed indiscriminately to the output.
All hope is not, however, lost! Check out the pretty-print example - it would be a good starting point for what you want to do. You'd have to do a bit of work to implement the readable part of the stream, though, if you still want to be able to .pipe() out of it.
If you know the general structure of the XML, you can try xml-flow. It converts an XML stream into objects, but has a utility to convert them back to xml strings:
https://github.com/matthewmatician/xml-flow
Based on deoxxa's answer I wrote an NPM module for this https://www.npmjs.com/package/sax-streamer

Resources