What is the correct way to insert Buffer as BLOB just 1-to-1 without serializing to string/hex? Is it possible at all from Node.js?
At my backend I already have Buffers with binary data and want to store them with minimum overheads using prepared statements. I'm getting ER_TRUNCATED_WRONG_VALUE_FOR_FIELD errno:1366 Incorrect string value error. It looks like the library just assumes there is a valid utf8 string in a buffer.
Related
I have a field in mongo which is of number type and I want to encrypt it into a unique number to save it and then decrypt it back to the original number without converting it to a string value.
You can create a Buffer object, write a Javascript number to the Buffer (creating a binary set of bytes) and then encrypt the contents of the buffer. Then, save the encrypted binary data from the buffer in your database.
To decrypt, you can reverse the process by reading the binary from the database into a Buffer, decrypt the buffer and then read the number from it.
You can write a Javascript number to a Buffer with something like:
buf.writeDoubleBE(value[, offset])
and there's a complementary method for reading a number from the buffer.
So, this doesn't have to be converted to a string anywhere, but it is converted to a binary Buffer.
FYI, here's an article on encrypting strings, buffers and streams in nodejs: https://attacomsian.com/blog/nodejs-encrypt-decrypt-data
FYI, MongoDB has some built-in encryption options (depending upon what storage engine you're using). This is more like database-level encryption that protects the whole database on disk rather than encrypting individual fields. Here's some info on that: https://docs.mongodb.com/manual/core/security-encryption-at-rest/#std-label-encrypted-storage-engine.
The MongoDB doc also has some info on client-side encrypting of individual fields here: https://docs.mongodb.com/manual/core/security-client-side-encryption/
and here:
https://docs.mongodb.com/manual/core/security-explicit-client-side-encryption/
And, apparently the enterprise version of mongodb 4.2 or later has some automatic field level encryption.
I have a nodejs application and need to convert a Buffer object to array of byte as c/c++ API's payload. I tried "Uint8Array" and "Array.prototype.slice.call", but it seems they don't work as expect. The API could not decode them correctly.
Is there any way I can do this?
Thanks.
Problem solved. I made a mistake before. After retest, to convert Buffer to Uint8Array which could be passed to c/c++ API as array of byte.
I'm writing a browser game and am looking for a way to send raw array buffers to and from the node.js server.
I don't like the idea of sending JSON strings over WebSockets because:
I have to specify keys when the receiver already knows the format
There is no validation or checking if you send malformed structure (not required though)
Parsing JSON strings in to objects is inherently slower than reading binary
Wasted bandwidth from the entire payload being a string (instead of packed ints, for example)
Ideally, I would be able to have a schema for every message type, construct that message, and send its raw array buffer to the server. Something like this:
schema PlayerAttack {
required int32 damage;
required int16[] coords;
required string name;
}
var message = new PlayerAttack(5, [24, 32], "John");
websockets.send(message.arrayBuffer());
And it would then arrive at the Node.js server as a Buffer with the option to decode in to an object.
Google's Protocol Buffers almost fits this use-case, but are too slow and have too much overhead (7x slower than JSON.parse from my benchmark and includes features like tagging which I have no use for).
I used protostuff to transform to byte array a json input i have. The code in java is:
LinkedBuffer buffer = LinkedBuffer.allocate(1024);
Schema<String> orderSchema = RuntimeSchema.getSchema(String.class);
int i = 1 ;
for(String p:poligonsStr) {
buffer.clear();
byteslist.add(ProtostuffIOUtil.toByteArray(p, orderSchema, buffer));
}
The problem is I don't know the algorithm that is used and how I can decode with the JavaScript client (Node.js). Also I saw there is a very good algorithm called Smile implemented for protostuff in project com.dyuproject.protostuff but I would like to know how to get schema with that library- I didn't manage that yet.
I would like to know what's the best to use: ProtostuffIOUtil or SmileIOUtil?
And how to use? And how to decode with JavaScript?
protostuff binary encoding is different from protobuf, and as far as I know there is no JavaScript library to decode protostuff-encoded data at the moment.
smile is not supported by web browsers out of the box, but there are libraries that can decode it.
As for me, there are two optiomal ways how you can encode data on server using Protostuff library, and decode it using JavaScript on client side:
Use protobuf encoding, it is good if size of encoded data is important. On server side, you should use ProtobufIOUtil to serialize your data to protobuf binary format. On client side, you can use https://github.com/dcodeIO/ProtoBuf.js/ to decode binary data from server.
Use JSON encoding, it is native format for JavaScript and usually it will be parsed faster than binary protobuf-encoded data. On server side, you should use JsonIOUtil (from protostuff-json module) to serialize your data to JSON text format. On client side, it is supported out of the box.
Here is an example how to serialize your POJO into protobuf binary using Protostuff: HelloService.java
I have a very long list of points (lat/long) in my C# program and I need to pass it to my webgl code. I have done it before using json but I thought I could reduce the bandwidth if I sent the data in binary format. Based on my research, on the client side I should use XMLHttpRequest with arraybuffer as response type. I just do not know what to do on the server side. I mean how to prepare the binary data in C# that could be interpreted as arraybuffer in javascript.
I am new to web programming, so if I am not clear on any part, please let me know.
I am not an expert, but I found this solution to work:
On the server side, convert the array of numbers to a byte array and then send the byte array to javascript as a binary file (Set the Mime type to "application/octet-stream")