Skip to content

Commit

Permalink
Run Prettier on the Node client
Browse files Browse the repository at this point in the history
  • Loading branch information
mildbyte committed Oct 25, 2022
1 parent 771bd56 commit a445226
Showing 1 changed file with 64 additions and 59 deletions.
123 changes: 64 additions & 59 deletions examples/clients/node/seafowl-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* ```
* ENDPOINT="https://demo.seafowl.io/q" node seafowl-client.js 'SELECT 1;'
* ```
*
*
* By default, HTTP POST requests are made which can be used to execute
* both read-only and write queries. To test the GET endpoint, use the
* `-r` command line argument.
Expand All @@ -35,104 +35,109 @@
* ```
*/

const crypto = require("crypto");

const crypto = require('crypto');

const trimQuery = sql => sql.trim().replace(/(?:\r\n|\r|\n)/g, " ");
const trimQuery = (sql) => sql.trim().replace(/(?:\r\n|\r|\n)/g, " ");

const hash = sql => crypto.createHash('sha256').update(sql).digest('hex');
const hash = (sql) => crypto.createHash("sha256").update(sql).digest("hex");

const inspect = require('util').inspect;
const inspect = require("util").inspect;

const request = (endpoint, options={}, cb) => {
let {protocol, hostname, port, pathname} = new URL(endpoint);
const mod = protocol === 'https:' ? 'https' : 'http';
return require(mod).request({
port: parseInt(port, 10),
path: pathname,
hostname,
...options
}, cb);
}
const request = (endpoint, options = {}, cb) => {
let { protocol, hostname, port, pathname } = new URL(endpoint);
const mod = protocol === "https:" ? "https" : "http";
return require(mod).request(
{
port: parseInt(port, 10),
path: pathname,
hostname,
...options,
},
cb
);
};

const readQuery = (endpoint, query) => new Promise((resolve, reject) => {
const {pathname: pathPrefix} = new URL(endpoint);
const readQuery = (endpoint, query) =>
new Promise((resolve, reject) => {
const { pathname: pathPrefix } = new URL(endpoint);
let response = "";
const options = {
path: `${pathPrefix}/${hash(query)}.csv`,
method: 'GET',
method: "GET",
headers: {
'Content-Type': 'application/json',
'X-Seafowl-Query': encodeURIComponent(query)
}
"Content-Type": "application/json",
"X-Seafowl-Query": encodeURIComponent(query),
},
};

const req = request(endpoint, options, res => {
const req = request(endpoint, options, (res) => {
const statusCode = res.statusCode;
res.on('data', d => {
response += d.toString('utf8');
res.on("data", (d) => {
response += d.toString("utf8");
});
res.on('close', d => {
resolve({response, statusCode});
res.on("close", (d) => {
resolve({ response, statusCode });
});
});

req.on('error', error => {
req.on("error", (error) => {
reject(error);
});

req.end();
});
});

const writeQuery = (endpoint, query, password) => new Promise((resolve, reject) => {
const data = JSON.stringify({query});
const writeQuery = (endpoint, query, password) =>
new Promise((resolve, reject) => {
const data = JSON.stringify({ query });
const headers = {
'Content-Type': 'application/json',
'Content-Length': data.length,
"Content-Type": "application/json",
"Content-Length": data.length,
};
if (password) {
headers['Authorization'] = `Bearer ${password}`
headers["Authorization"] = `Bearer ${password}`;
}
let response = "";
const options = {
method: 'POST',
headers
method: "POST",
headers,
};

const req = request(endpoint, options, res => {
const req = request(endpoint, options, (res) => {
const statusCode = res.statusCode;
res.on('data', d => {
response += d.toString('utf8');
res.on("data", (d) => {
response += d.toString("utf8");
});
res.on('close', d => {
resolve({response, statusCode});
res.on("close", (d) => {
resolve({ response, statusCode });
});
});

req.write(data)
req.write(data);

req.on('error', error => {
req.on("error", (error) => {
reject(error);
});

req.end();
});
});

if (require.main === module) {
const endpoint = process.env['ENDPOINT'] ?? 'http://localhost:8080/q';
const args = process.argv.slice(2);
let result;
if (args[0]?.trim() === '-r') {
result = readQuery(endpoint, args.slice(1).join(" "))
} else {
result = writeQuery(endpoint, args.join(" "), process.env['PASSWORD'])
}
(async () => {
await result.then(
({response, statusCode})=> console.log(`code: ${statusCode}\n${inspect(JSON.parse(response))}`),
error => console.error(error)
);
})();
const endpoint = process.env["ENDPOINT"] ?? "http://localhost:8080/q";
const args = process.argv.slice(2);
let result;
if (args[0]?.trim() === "-r") {
result = readQuery(endpoint, args.slice(1).join(" "));
} else {
result = writeQuery(endpoint, args.join(" "), process.env["PASSWORD"]);
}
(async () => {
await result.then(
({ response, statusCode }) =>
console.log(`code: ${statusCode}\n${inspect(JSON.parse(response))}`),
(error) => console.error(error)
);
})();
}

module.exports = {readQuery, writeQuery}
module.exports = { readQuery, writeQuery };

0 comments on commit a445226

Please sign in to comment.