library inited

This commit is contained in:
C
2023-01-10 18:17:19 +05:00
parent 9c0ddba58c
commit 92ebcbb613
2 changed files with 115 additions and 0 deletions
+81
View File
@@ -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)
})
}
+34
View File
@@ -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
}