why do mysql query show ColumnDefinition - node.js

I am using mysql2 package. I am doing simple sql query in node. so why does it tur
public async getTests(req: Request, res: Response): Promise<void> {
const pool = await connect();
const stmt = 'SELECT test, test2 FROM testTable';
const tests = await pool.query(stmt);
res.json({ tests});
}
Query Result:
{ foo, foo1 }, ColumnDefinition {
_buf: <Buffer 01 00 00 01 0a 51 00 00 02 03 64 65 66 13 61 75 72 6f 72 61 5f 61 75 64 69 74 5f 73 69 67 6e 61 6c 0a 65 6e 67 61 67 65
6d 65 6e 74 0a 65 6e 67 61 67 ... 1200 more bytes>,

Query function returns rows & fields. Please try to get rows:
const [rows] = await pool.query(stmt);
You can check below link for details.
https://github.com/sidorares/node-mysql2#using-promise-wrapper

Related

Receiving <Buffer> from Node JS Websocket

I'm simply trying to log messages from a websocket server but I seem to only receive Buffer data. The below example is trying to connect to https://www.piesocket.com/websocket-tester
What am I doing wrong?
import WebSocket from 'ws'
const demoStreamer = new WebSocket('wss://demo.piesocket.com/v3/channel_1?
api_key=oCdCMcMPQpbvNjUIzqtvF1d2X2okWpDQj4AwARJuAgtjhzKxVEjQU6IdCjwm&notify_self')
demoStreamer.on('message', (data) => {
console.log(data);
});
// OUTPUT:
// <Buffer 7b 22 69 6e 66 6f 22 3a 22 59 6f 75 20 61 72 65 20 75 73 69 6e 67 20 61 20 74 65 73
// 74 20 61 70 69 20 6b 65 79 22 7d>
// <Buffer 54 65 73 74 20 6d 65 73 73 61 67 65>
// <Buffer 54 65 73 74 20 6d 65 73 73 61 67 65>
7b 22 69 6e 66 6f 22 3a 22 59 6f 75 20 61 72 65 20 75 73 69 6e 67 20 61 20 74 65 73 74 20 61 70 69 20 6b 65 79 22 7d is a hexadecimal representation of {"info":"You are using a test api key"}
Just convert the buffer to string:
demoStreamer.on('message', (data) => {
console.log(data.toString()); // "{\"info\":\"You are using a test api key\"}"
// Retrieve the info message:
console.log(JSON.parse(data).info); // "You are using a test api key"
});

Parsing node-rest-client response when the response is in HTML format

I have an endpoint I'm testing using Node.js node-rest-client
return new Promise(function(resolve, reject) {
client.post(`${url}/auth/auth`, authArgs, function (authData) {
if (debug) {
console.log("Received authData:");
console.log(authData);
console.log();
if authData fails the response is in HTML, and when trying to print it I receive the following
Received authData:
<Buffer 49 6e 74 65 67 72 69 74 79 45 72 72 6f 72 20 61 74 20 2f 61 75 74 68 65 6e 74 69 63 61 74 69 6f 6e 2f 61 75 74 68 65 6e 74 69 63 61 74 65 0a 6e 75 6c ... >
How can I have this HTML to be parsed properly so it can show the error message properly instead of a buffer
The output error is Buffer, its a stream of binary, and to convert it to string you can just do:
...
console.log(authData.toString('utf8'));
...

Nodejs sync-request not returning HTML Response

const cheerio = require('cheerio');
const request = require('sync-request');
const fs = require('fs');
var res = request('GET', 'https://edition.cnn.com/');
console.log(res.getBody())
This is my code. I want to get HTML code of https://edition.cnn.com/. But it returning following:
<Buffer 3c 21 44 4f 43 54 59 50 45 20 68 74 6d 6c 3e 3c 68 74 6d 6c 20 63 6c 61 73 73 3d 22 6e 6f 2d 6a 73 22 3e 3c 68 65 61 64 3e 3c 6d 65 74 61 20 63 6f 6e ... >
You are receiving a buffer currently, you need to convert it to string. Use this -
console.log(res.getBody().toString())

Node crypto sha256 not working with whitespace

Am I doing something wrong or is this a bug?
When using
var checkSum = crypto.createHash("sha256").update(scriptInnerHTML, "utf-8").digest("base64")
To generate the sha256 for this script tag:
<script>
console.warn("works");
var some code ...
</script>
And using it inside a Content-Security-Policy like this:
<meta http-equiv="Content-Security-Policy" content="script-src 'sha256-8O+YTKIDgMhMvSanTZx1Om5XY2ERB+kIxN8AcO2r6Ok='">
Things works as it should, but however this one, identical except without the warning-log, doesn't work. Notice the couple of new-lines and a tab before the code.
<script>
var some code ...
</script>
It seems as Node interprets tabs (or rather newlines+tab) differently if there is text afterwards. Weird!
Tested both in Safari and Chrome so it should not be a browser issue.
Updates:
I of-course re-generate the hash for each input.
Examples:
(The script tag is at the bottom)
Working hash ('sha256-5++3ItSu+9maCZiuuXH60RG7EugmibMmhxhwpsynAn0='): http://aggressive.se/test/works.html
Not working hash ('sha256-hIRDHGUSaEmjNiVhNabY+8l4GNQdj/PXD4XHA21gdRM='): http://aggressive.se/test/fail.html
SOLVED:
The problem was due to the serialize() function in JSDom which I use to generate the HTML in node. Calling dom.serialize() after calculating the hash, it removed one unnecessary tab which changed the source (in a hard to notice way).
But just so you know, the problem was not due to node nor the crypto module. (Hope someone has use for this)
I am not sure how you obtained the scriptInnerHTML variable, but if I access the innerHTML property of the script of interest in the fail.html example, I get this hexdump:
00000000 0a 0a 0a 09 76 61 72 20 6c 61 79 6f 75 74 20 3d |....var layout =|
00000010 20 6e 65 77 20 41 67 67 72 65 73 73 69 76 65 4c | new AggressiveL|
00000020 61 79 6f 75 74 28 29 3b 0a 09 41 67 67 72 65 73 |ayout();..Aggres|
00000030 73 69 76 65 4c 61 79 6f 75 74 2e 70 72 6f 74 6f |siveLayout.proto|
00000040 74 79 70 65 2e 73 65 74 75 70 46 75 6e 63 74 69 |type.setupFuncti|
00000050 6f 6e 20 3d 20 66 75 6e 63 74 69 6f 6e 28 29 0a |on = function().|
00000060 09 7b 0a 09 09 76 61 72 20 65 6c 65 6d 65 6e 74 |.{...var element|
00000070 20 3d 20 64 6f 63 75 6d 65 6e 74 2e 63 72 65 61 | = document.crea|
00000080 74 65 45 6c 65 6d 65 6e 74 28 22 64 69 76 22 29 |teElement("div")|
00000090 3b 0a 09 09 65 6c 65 6d 65 6e 74 2e 69 6e 6e 65 |;...element.inne|
000000a0 72 48 54 4d 4c 20 3d 20 22 49 74 20 77 6f 72 6b |rHTML = "It work|
000000b0 73 22 3b 0a 09 09 74 68 69 73 2e 65 6c 65 6d 65 |s";...this.eleme|
000000c0 6e 74 73 2e 72 6f 6f 74 45 6c 65 6d 65 6e 74 2e |nts.rootElement.|
000000d0 61 70 70 65 6e 64 43 68 69 6c 64 28 65 6c 65 6d |appendChild(elem|
000000e0 65 6e 74 29 0a 09 7d 0a |ent)..}.|
the corresponding hash of which is bR9Os+NBLWNZ3/wFVRhBilP05u9OeSj0ABRo+T8QF+g=. If I use this hash in the html file, it seems to work...
An example with JSDOM:
const fs = require('fs');
const crypto = require('crypto');
const { JSDOM } = require('jsdom');
const dom = new JSDOM(fs.readFileSync('./fail.html'));
const data = dom.window.document.querySelector('head > script:last-child').innerHTML;
console.log(crypto.createHash('sha256').update(data, 'utf-8').digest('base64'));
//bR9Os+NBLWNZ3/wFVRhBilP05u9OeSj0ABRo+T8QF+g=

EPP server does not respond to EPP HELLO

I need to work with .NL registrar - sidn.nl - through their EPP API. I use standard EPP HELLO, add 4 bytes message size (big endian), call CURL - no header, no data back. Code:
var epp_hello = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>'+
'<epp xmlns="urn:ietf:params:xml:ns:epp-1.0">'+
' <hello/>'+
'</epp>'; // EPP HELLO
var xml = bigEndian(epp_hello)+""; // big endian
curl.setOpt(Curl.option.URL, "drs.domain-registry.nl");
curl.setOpt(Curl.option.PORT, 700);
curl.setOpt(Curl.option.POST, 1);
curl.setOpt(Curl.option.HEADER, true);
curl.setOpt(Curl.option.POSTFIELDS, xml);
curl.setOpt(Curl.option.HTTPHEADER , ['Content-type: text/xml']);
curl.setOpt(Curl.option.TIMEOUT , 180);
curl.setOpt(Curl.option.SSL_VERIFYPEER, true);
curl.setOpt(Curl.option.SSL_VERIFYHOST, false);
curl.perform();
My IP is whitelisted in control panel.
Why it responds nothing? It should be EPP GREETING.
Their tech support is useless, sent me link to standard manual :-)
Thanks in advance for any help / advice!
UPDATE: When calling through TCP (instead of CURL) the result is pretty much the same:
var epp_hello = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>'+
'<epp xmlns="urn:ietf:params:xml:ns:epp-1.0"><hello/></epp>';
var xml = bigEndian(epp_hello);
var ddd = new Date();
var client = new net.Socket();
client.connect(700, "drs.domain-registry.nl", function(xxml) {
console.log('Connected ' + ddd.toUTCString(),xxml);
client.write(xxml);
}.bind(null,xml));
client.on('data', function(data) {
console.log('Received: ' + data);
client.destroy();
});
client.on('close', function() {
console.log('Connection closed');
});
Outgoing requests:
Connected Thu, 23 Feb 2017 01:55:48 GMT <Buffer 00 00 00 74 3c 3f 78 6d 6c 20 76 65 72 73 69 6f 6e 3d 22 31 2e 30 22 20 65 6e 63 6f 64 69 6e 67 3d 22 55 54 46 2d 38 22 20 73 74 61 6e 64 61 6c 6f 6e ... >
Connection closed
Connected Thu, 23 Feb 2017 01:55:52 GMT <Buffer 00 00 00 74 3c 3f 78 6d 6c 20 76 65 72 73 69 6f 6e 3d 22 31 2e 30 22 20 65 6e 63 6f 64 69 6e 67 3d 22 55 54 46 2d 38 22 20 73 74 61 6e 64 61 6c 6f 6e ... >
Connection closed
Connected Thu, 23 Feb 2017 01:55:56 GMT <Buffer 00 00 00 74 3c 3f 78 6d 6c 20 76 65 72 73 69 6f 6e 3d 22 31 2e 30 22 20 65 6e 63 6f 64 69 6e 67 3d 22 55 54 46 2d 38 22 20 73 74 61 6e 64 61 6c 6f 6e ... >
Connection closed
Server returns no data, connection is being closed in a second
Resolved by using tls instead of net:
const epp_hello = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>'+
'<epp xmlns="urn:ietf:params:xml:ns:epp-1.0"><hello/></epp>';
const opts = {
};
var xml = bigEndian(epp_hello);
var client = tls.connect(700, "drs.domain-registry.nl", opts, function(xxml) {
client.write(xxml);
}.bind(null,xml));
client.on('data', function(data) {
console.log('Received: ' + data);
});
Received EPP GREETING

Resources