From 92ebcbb6130851fd67b3448b90646a93009639f1 Mon Sep 17 00:00:00 2001 From: C Date: Tue, 10 Jan 2023 18:17:19 +0500 Subject: [PATCH] library inited --- src/http_client.js | 81 ++++++++++++++++++++++++++++++++++++++++++++++ src/logger.js | 34 +++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 src/http_client.js create mode 100644 src/logger.js diff --git a/src/http_client.js b/src/http_client.js new file mode 100644 index 0000000..570a1a5 --- /dev/null +++ b/src/http_client.js @@ -0,0 +1,81 @@ +const http = require('http') +const https = require('https') + +module.exports = class http_client { + constructor(ip, port) { + this.#ip = ip + this.#port = port + } + async post(data, callback_answer, callback_error) { + // Build the post string from an object + + // An object of options to indicate where to post to + var post_options = { + host: this.#ip, + port: this.#port, + method: 'POST', + headers: { + 'Content-Type': 'applicaiton/json', + 'Content-Length': Buffer.byteLength(data) + } + } + // Set up the request + var request = http.request(post_options, function(res) { + res.setEncoding('utf8') + res.on('data', async function (answer) { + callback_answer(answer) + }) + }) + request.on('error', function(e) { + callback_error(e) + }) + // post the data + request.write(data) + request.end() + + } + #ip + #port +} + +module.exports.post = async (options, callback_res, callback_err) => { + http.post(options, function(res) { + var body_chunks = [] + res.on('data', chunk => { + body_chunks.push(chunk) + }).on('end', () => { + var body = Buffer.concat(body_chunks) + callback_res(body) + }) + }).on('error', err => { + callback_err(err) + }) +} + +module.exports.get = async (options, callback_res, callback_err) => { + http.get(options, function(res) { + var body_chunks = [] + res.on('data', chunk => { + body_chunks.push(chunk) + }).on('end', () => { + var body = Buffer.concat(body_chunks) + callback_res(body) + }) + }).on('error', err => { + callback_err(err) + }) +} + +module.exports.gets = async (options, callback_res, callback_err) => { + https.get(options, function(res) { + var body_chunks = [] + res.on('data', chunk => { + body_chunks.push(chunk) + }).on('end', () => { + var body = Buffer.concat(body_chunks) + callback_res(body) + }) + }).on('error', err => { + callback_err(err) + }) +} \ No newline at end of file diff --git a/src/logger.js b/src/logger.js new file mode 100644 index 0000000..35a02f0 --- /dev/null +++ b/src/logger.js @@ -0,0 +1,34 @@ +const fs = require('fs') +const path = require('path') +const node_time = require('node-datetime') +const util = require('util') + +module.exports = class logger { + constructor(filepath) { + this.#filepath = filepath + this.#filename = path.basename(filepath) + var dirname = path.dirname(filepath) + + if (!fs.existsSync(dirname)) + fs.mkdirSync(dirname, {recursive:true}) + } + error(...arg) { + this.#log('error', arg) + } + log(...arg) { + this.#log('', arg) + } + + #log(message = '', ...arg) { + var args = '' + arg.forEach(value => { + args += util.format(value) + ' ' + }) + args = (node_time.create().format('d-m-Y H:M:S')).toString() + ((message != '') ? ': ' + message : '') + ': ' + args + console.log(this.#filename + ': ' + args) + fs.appendFileSync(this.#filepath, args + '\n') + } + + #filepath + #filename +} \ No newline at end of file