Concatenating Buffer chunks received from Gridfs - node.js

Only 255kb of data is being sent back, which is the default chunk size in gridfs. In mongolab, however I see that multiple chunks are being stored. So the problem is only with the read code. Here it is-
Retrive from gridfs and send response code-
app.get('/download/:fileid', function(req, res) {
var buffer = [];
var id = req.params.fileid;
var base64string;
var readStream = gfs.createReadStream({_id: id});
readStream.on('data', function(chunk) {
buffer.push(chunk);
});
readStream.on('end', function() {
base64string = Buffer(buffer.concat()[0]).toString('base64');
fs.writeFile("base64.txt", base64string);
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.write(JSON.stringify({body : base64string}));
res.end();
});
});
[Edit]- Further analysis suggests that I am getting all the chunks. So the problem is in concatenation-
base64string = Buffer(buffer.concat()[0]).toString('base64');
[Edit]- Buffer.concat() does not work. How do I create a single buffer out of these three buffers?
[ <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 02 01 00 60 00 60 00 00 ff e1 0f 07 45 78 69 66 00 00 4d 4d 00 2a 00 00 00
08 00 06 01 32 00 02 00 00 00 14 00 00 ... >,
<Buffer a1 b1 42 92 56 81 43 43 5e b8 46 61 0f 96 09 c1 c4 e8 96 2b 75 52 54 21 2a 7d 46 ba c6 66 80 01 95 3c 3d a7 16
31 ed d9 56 b5 2b 91 5e 07 14 cb 2f ee ... >,
<Buffer a6 b6 92 5a 4f 1a da c9 e8 55 97 8e d1 da 4a b3 88 26 2f e5 2c 23 2a 54 10 59 1b 9e eb 35 e8 92 cc 2e da 8b 16
91 d3 e2 91 93 99 62 e0 8e a3 80 56 aa ... >,
<Buffer 71 e6 83 65 5e b0 f1 5f 61 a6 21 a3 68 83 be 72 0b 7a 7d 47 d6 d3 69 ee 59 37 92 1a de 06 8a 22 2a 3a a0 a3 1f
1a 0e 7e ec 67 3b 8f e9 c5 87 6b 41 5b ... > ]
[EDIT] Piping the readstream into response as per neils recommendation. However I get a syntax error on client side. [Unexpected Token]
RESPONSE CODE-
app.get('/download/:fileid', function(req, res) {
var id = req.params.fileid;
res.set('Content-Type', 'image/jpeg');
gfs.createReadStream({_id: id}).pipe(res); });
CLIENT CODE-
download() {
this._http.get('http://localhost:9000/download/' + this._fileid)
.map(response => response.json())
.subscribe(
data => {
console.log(data);
//this.image = data.body;
},
err => console.log('Error is..:' + err)
);;
}

Related

Manually Parse Form Data Node.js Express

How can I parse form-data manually at node js?
I get chunks of the large file at the server side. And can't merge it, because it will borrow RAM equals to the (file size + file info size).
As far as I understand, I can't use the libraries, such as multer/connect-busboy/connect-multiparty etc.
In simple terms, now I get following data at server side and can't figure out how to receive, example, a file name, and how to determine, which byte is responsible for the beginning of the file.
[1] Get request...
[1] data: <Buffer 2d 2d 2d 2d 2d 2d 57 65 62 4b 69 74 46 6f 72 6d 42 6f 75 6e 64 61 72 79 62 66 30 31 63 62 36 38 71 4d 41 4d 36 6d 51 31 0d 0a 43 6f 6e 74 65 6e 74 2d ... 65486 more bytes>
[1] data: <Buffer 02 10 00 00 01 ce 00 00 01 ef 00 00 14 e4 00 00 01 de 00 00 01 b2 00 00 01 d8 00 05 f0 1a 00 00 06 70 00 00 00 fa 00 00 00 cf 00 00 0a 3b 00 00 01 4a ... 65486 more bytes>
[1] data: <Buffer 31 52 2e 9a 5b df 11 19 5f c2 5d 38 ed db 23 00 06 05 bd 8f 71 e7 6b 03 9d 3a 88 e3 24 10 00 0f a1 10 43 82 ff 67 bd 5d eb 8e 59 fe a9 d3 e6 45 f2 9e ... 65486 more bytes>
[1] data: <Buffer 2b 6c cf 18 40 13 d8 fd 09 fa 56 25 e6 6d 6a 57 94 8c a4 9c d1 7b 99 bb 20 c0 21 e7 4a 1f 72 5a a5 3d 8e 84 5d 97 eb 1b 0f db b1 a9 81 f9 db ef 06 36 ... 65486 more bytes>
[1] data: <Buffer bb 29 4b 0d 06 1d 69 e1 c7 47 a7 99 c6 e4 c3 48 2e 85 6a b3 57 01 68 09 00 aa 6d b4 7b c7 07 2f 73 c0 c4 6b c4 48 9b 8f 81 64 8e 25 c7 a3 de c3 4f 2a ... 65486 more bytes>
[1] data: <Buffer 21 2b 1c dd 57 02 23 f5 43 a1 70 72 b4 8d 8d a8 4a 4b c1 e1 21 75 84 ed 07 00 34 de 5c 8d b0 0a 6f 99 60 28 34 b1 c5 bd a2 3c 3e d5 01 0f 62 57 a8 e8 ... 65486 more bytes>
[1] data: <Buffer 2d 96 e3 47 75 ce 25 63 77 26 15 96 b6 43 b9 d2 59 7d 12 c9 64 b2 11 f2 39 29 3e bf 83 1a f3 15 7b 14 ee f3 33 52 5c 39 34 75 06 41 2f e7 11 e8 26 4e ... 65486 more bytes>
[1] data: <Buffer 4a 9d 20 c7 a8 55 35 22 26 ff c7 b4 11 d9 6c 4d ce e1 a0 b2 b3 01 37 da 6a ad ae 98 fd 9b 8e 9c 8b 4a 27 8e e3 10 4a dc 80 f2 50 df 88 d9 c8 f9 ef 48 ... 65486 more bytes>
[1] data: <Buffer 52 ee b3 96 1f 7c af df fa 3f 9c 9a f7 01 20 7d 3f ea 4e 7e aa 4e d9 57 31 cb b5 f3 09 49 c7 ee 92 83 2e cb 58 d2 ea 1b b0 35 2a 3c 6c 27 75 0c d0 39 ... 65486 more bytes>
...more
...more
...and more
After receiving all the above data, I need to get file name and send each chunk of the file(not other info, such as name/mime type) to another client. (It's file sharing web resource)
Another solutions?
At the same time, I could to send the file separated manually by the chunks of type ArrayBuffer. Each chunk would be encoded additional information, like a "file name", "mime type". But in this case, I should to separate file at client side and get again the above problem the lack of memory.
A working solution, but a bit insecure. Send the file name and mime type separately from the main file data.
The part of the code responsible for sending data:
app.get(`/${senderId}/get`, (request, response) => {
let countChunks = 0;
// waiting for the sender's request now...
app.post(`/${senderId}/send`, (requestSender, responseSender) => {
// start sharing...
let countChunks = 0;
requestSender.on("data", (chunk) => {
if (countChunks === 0) {
// need get the name from the chunks
const fileName = "test_name";
response.setHeader("Content-Disposition", `attachment; filename="${fileName}"`);
}
countChunks += 1;
// need to get the first byte of the file data and start sending starting with it
response.write(chunk);
})
requestSender.on("end", () => {
response.end();
})
})
})

Buffer library in browser does not return the same value as in nodejs

When running the Buffer.from static method in node js on a public key I get different console.log when running it from the browser (in an angular project). Should'nt they be the same? Is there something I'm doing wrong?
const pubKey='30819F300D06092A864886F70D010101050003818D0030818902818100B2E9AFAFEED76A5C31D069F84328D785DFE6C40A69F51B29C7D7C91EF171A5EF6AD9FC30AF31F4B59C0FE317E47B5DBAA04E3753AC7F8B0E54D8EB4372894900DE247FD11B8C2208FE1C837ADEC409B0F2EE89A5C54B8AB80D5934FC65100406077D129DC5EB961E883B937C4251FDA4BD77224D1CDEF09151894F902758AA3B0203010001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000';
const buff = Buffer.from(pubKey, 'hex');
console.log(buff)
<Buffer 30 81 9f 30 0d 06 09 2a 86 48 86 f7 0d 01 01 01 05 00 03 81 8d 00 30 81 89 02 81 81 00 b2 e9 af af ee d7 6a 5c 31 d0 69 f8 43 28 d7 85 df e6 c4 0a 69 ... 244 more bytes>
In angular I have installed npm i buffer and provided it in the pollyfills.ts like this
(window as any).global = window;
(window as any).global.Buffer = require('buffer').Buffer;
(window as any).process = {};
Running the same code on the browser Stackblitz yields
So my questions is
Why does the Buffer in the browser return something different? [0] = 48 vs [0]= 30
In the Stackblitz example you can see that I'm using the buffer with node-rsa for encyrption. The encrypted value from the nodesjs script works while the one from the browser doesnt.
The data are the same, just two different representation:
console.log(buff)
<Buffer 30 81 9f 30 0d 06 09 2a 86 48 86 f7 0d 01 01 01 05 00 03 81 8d 00 30 81 89 02 81 81 00 b2 e9 af af ee d7 6a 5c 31 d0 69 f8 43 28 d7 85 df e6 c4 0a 69 ... >
undefined
console.log(buff.toJSON())
{ type: 'Buffer',
data:
[ 48,
129,

Node.js - node-rsa - Error during encryption. Original error: Error: error:0306E06C:bignum routines:BN_mod_inverse:no inverse

I`m trying to encrypt data via RSA using public key (128-bytes == 1024-bits) received from auth server.
Here is a code in Node.js:
const NodeRSA = require('node-rsa');
const openData = Buffer.from('example');
const rsaPublicKey = Buffer.from('04 D4 8B 30 F6 1C 89 8B 36 0B 32 BB 64 25 ED C0 76 1D 23 76 A9 49 D4 E7 24 99 24 C4 2E D7 D8 90 96 AF EE 53 3F 65 CE 3F 42 34 AB 56 47 7B 9A DD D5 7C 97 21 6F 37 2D 5A 7A E0 72 08 38 7A 18 85 AA FF C8 14 96 84 BB 65 33 68 11 E5 C4 9F CE 9F 19 1A C7 29 A5 13 80 4B D4 7E 8C 63 81 A1 FE 99 7D 11 35 46 08 93 BF D2 23 28 47 04 B4 B6 2B EF 5D 30 CF 33 CB D5 0E 28 A6 87 63 62 22 1E 46 74'.split(' ').join(''), 'hex');
const key = new NodeRSA();
key.importKey({
n: rsaPublicKey,
e: 65537
}, 'components-public');
const encryptedData = key.encrypt(openData);
But I`ve got error:
/Users/xok/node_modules/node-rsa/src/NodeRSA.js:283
throw Error('Error during encryption. Original error: ' + e);
^
Error: Error during encryption. Original error: Error: error:0306E06C:bignum routines:BN_mod_inverse:no inverse
at NodeRSA.module.exports.NodeRSA.$$encryptKey (/Users/xok/node_modules/node-rsa/src/NodeRSA.js:283:19)
at NodeRSA.module.exports.NodeRSA.encrypt (/Users/xok/node_modules/node-rsa/src/NodeRSA.js:238:21)
If I use another key received from the same auth server, then all is OK:
const rsaPublicKey = Buffer.from('41 F5 0E FC 66 16 4D 28 89 E8 50 C9 8A CD C7 64 A7 5B D8 D0 98 4E 29 9F 52 FC 24 6C EA A5 5B 23 CD 37 B5 1E 9F F9 61 C5 FD C7 95 35 51 13 A0 4A 94 7E FA 23 92 0E DA 4E AD B8 98 86 6F EC 7D D4 C3 DA BF 98 01 A0 3F 8C 7A EC EE CB 53 2F 26 4C 66 2D D6 48 48 25 02 09 85 35 9F 6F F8 5F F7 1B BD 0A E0 02 61 B8 81 6A EE B2 F3 B0 BA EF 18 25 48 B6 1B 73 CB 32 33 E7 13 A7 3B D1 D7 D8 95 A9'.split(' ').join(''), 'hex');
Question: Could you say what I`m doing wrong, please?

Nodejs WebSocket response is encrypted or not readable (connecting to a exchange socket)

I am trying to make a request to a WebSocket of a crypto-exchange to get live market data for analysis.
In the Browser the data is shown in readable Text as it is used to populate the orderbook.
When trying to connect to it via nodejs I am getting a hex code that when converted still doesn't make sense.
const WebSocket = require('ws');
const sendData = '{"method":"deals.subscribe","params":["ETHBTC"],"id":18}'
// Establish a WebSocket connection to the echo server
const ws = new WebSocket('wss://ws.exchnageAddres.com');
ws.addEventListener('open', () => {
console.log('Sending:', sendData);
// Send message to the WebSocket server
ws.send(sendData);
});
ws.addEventListener('message', event => {
console.log('Received:', event.data);
});
Response:
Received: <Buffer 1f 8b 08 00 00 00 00 00 00 03 ab 56 4a 2d 2a ca 2f 52 b2 52 c8 2b cd c9 d1 51 50 2a 4a 2d 2e cd 29 01 f2 ab 95 8a 4b 12 4b 4a 8b 81 4c a5 e2 d2 e4 e4 ... 23 more bytes>
Received: <Buffer 1f 8b 08 00 00 00 00 00 00 03 9d 9a df 8a 5c 47 0e c6 5f 25 cc 75 28 24 95 a4 52 ed 65 96 85 7d 80 dc 85 5c 78 d7 86 35 d8 89 89 ed 8b 10 f2 ee fb 95 ... 2012 more bytes>
Received: <Buffer 1f 8b 08 00 00 00 00 00 00 03 55 8c b1 0a 83 30 14 45 7f 45 de 5c 42 e2 6b 92 da d1 52 e8 07 b8 89 43 da 04 2a 18 0d 26 19 44 fc f7 e6 15 3a 74 3d f7 ... 106 more bytes>
How can I connect to the ws in node?
Do I have to use a certificate or anything? Would be awesome if someone could help me out here.
Testing the connection on a chrome extension is giving me a Blob object but I can't really convert that to an array.
Using Burp Decoder works but only there.
Thanks and best regards.

Buffer conversion decodes correctly the <Topic> in a PUB/SUB, but not the messages. Why?

This is the code for a SUB-side handler of a ZeroMQ PUB / SUB socket:
sock.on('message', function(topic, message) {
console.log("--Topic--");
console.log(topic);
console.log(topic.toString('utf8'));
console.log("--Message--");
console.log(message);
console.log(message.toString('utf8'));
console.log("");
});
Probably missing something here, but I am having a hard time working out why my buffer conversion is returning junk:
--Topic--
<Buffer 70 72 65 73 65 6e 63 65>
presence
--Message--
<Buffer 08 a0 43 10 f9 dd d3 cb 05 18 00 20 18 2a 06 00 0c 29 f2 d5 b4 aa 1f 30 10 00 1a 14 13 00 b3 75 48 ec 55 a7 92 a2 bd ee 9d 4b b6 25 a0 77 a5 d3 22 0c ... >
�C���� *
)�մ�0�uH�U������K�%�w��"
Aruba AP 215*
��۟
The first item (topic) decodes without issue, but the message doesn't seem to.

Resources