2021-07-03 13:56:53 +00:00
|
|
|
import http from 'http'
|
2021-10-11 00:21:57 +00:00
|
|
|
import fs from 'fs/promises'
|
|
|
|
import path from 'path'
|
2021-07-03 13:56:53 +00:00
|
|
|
import { URL } from 'url'
|
|
|
|
import defaults from '../api/defaults.mjs'
|
|
|
|
import config from '../api/config.mjs'
|
|
|
|
|
|
|
|
export default function Client(port = config.get('server:port'), opts) {
|
|
|
|
this.options = defaults(opts, {})
|
|
|
|
this.prefix = `http://localhost:${port}`
|
|
|
|
}
|
|
|
|
|
|
|
|
Client.prototype.customRequest = function(method = 'GET', path, body, options) {
|
|
|
|
if (path.slice(0, 4) !== 'http') {
|
|
|
|
path = this.prefix + path
|
|
|
|
}
|
|
|
|
let urlObj = new URL(path)
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const opts = defaults(defaults(options, {
|
|
|
|
method: method,
|
|
|
|
timeout: 500,
|
|
|
|
protocol: urlObj.protocol,
|
|
|
|
username: urlObj.username,
|
|
|
|
password: urlObj.password,
|
|
|
|
host: urlObj.hostname,
|
|
|
|
port: Number(urlObj.port),
|
|
|
|
path: urlObj.pathname + urlObj.search,
|
|
|
|
}))
|
|
|
|
|
|
|
|
const req = http.request(opts)
|
|
|
|
if (body) {
|
|
|
|
req.write(body)
|
|
|
|
}
|
|
|
|
|
|
|
|
req.on('error', reject)
|
|
|
|
req.on('timeout', function() { reject(new Error(`Request ${method} ${path} timed out`)) })
|
|
|
|
req.on('response', res => {
|
|
|
|
res.setEncoding('utf8')
|
|
|
|
let output = ''
|
|
|
|
|
|
|
|
res.on('data', function (chunk) {
|
|
|
|
output += chunk.toString()
|
|
|
|
})
|
|
|
|
|
|
|
|
res.on('end', function () {
|
2022-01-06 09:01:10 +00:00
|
|
|
if (!output) return resolve(null)
|
2021-07-03 13:56:53 +00:00
|
|
|
try {
|
|
|
|
output = JSON.parse(output)
|
|
|
|
} catch (e) {
|
|
|
|
return reject(new Error(`${e.message} while decoding: ${output}`))
|
|
|
|
}
|
|
|
|
if (output.status) {
|
|
|
|
let err = new Error(`Request failed [${output.status}]: ${output.message}`)
|
|
|
|
err.body = output
|
2021-10-11 00:21:57 +00:00
|
|
|
err.status = output.status
|
2021-07-03 13:56:53 +00:00
|
|
|
return reject(err)
|
|
|
|
}
|
|
|
|
resolve(output)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
req.end()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-11 00:21:57 +00:00
|
|
|
Client.prototype.get = function(url = '/') {
|
|
|
|
return this.customRequest('GET', url, null)
|
|
|
|
}
|
|
|
|
|
2022-01-06 09:01:10 +00:00
|
|
|
Client.prototype.del = function(url = '/', body = {}) {
|
|
|
|
return this.customRequest('DELETE', url, JSON.stringify(body))
|
|
|
|
}
|
|
|
|
|
2022-01-05 14:47:51 +00:00
|
|
|
const random = (length = 8) => {
|
|
|
|
// Declare all characters
|
|
|
|
let chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
|
|
// Pick characers randomly
|
|
|
|
let str = '';
|
|
|
|
for (let i = 0; i < length; i++) {
|
|
|
|
str += chars.charAt(Math.floor(Math.random() * chars.length));
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
Client.prototype.upload = function(url, file, method = 'POST', body = {}) {
|
2021-10-11 00:21:57 +00:00
|
|
|
return fs.readFile(file).then(data => {
|
|
|
|
const crlf = '\r\n'
|
|
|
|
const filename = path.basename(file)
|
2022-01-05 14:47:51 +00:00
|
|
|
const boundary = `---------${random(32)}`
|
|
|
|
|
2021-10-11 00:21:57 +00:00
|
|
|
const multipartBody = Buffer.concat([
|
|
|
|
Buffer.from(
|
|
|
|
`${crlf}--${boundary}${crlf}` +
|
2022-01-05 14:47:51 +00:00
|
|
|
`Content-Disposition: form-data; name="file"; filename="${filename}"` + crlf + crlf
|
2021-10-11 00:21:57 +00:00
|
|
|
),
|
|
|
|
data,
|
2022-01-05 14:47:51 +00:00
|
|
|
Buffer.concat(Object.keys(body).map(function(key) {
|
|
|
|
return Buffer.from(''
|
|
|
|
+ `${crlf}--${boundary}${crlf}`
|
|
|
|
+ `Content-Disposition: form-data; name="${key}"` + crlf + crlf
|
|
|
|
+ JSON.stringify(body[key])
|
|
|
|
)
|
|
|
|
})),
|
|
|
|
Buffer.from(`${crlf}--${boundary}--`),
|
2021-10-11 00:21:57 +00:00
|
|
|
])
|
|
|
|
|
|
|
|
return this.customRequest(method, url, multipartBody, {
|
|
|
|
timeout: 5000,
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'multipart/form-data; boundary=' + boundary,
|
|
|
|
'Content-Length': multipartBody.length,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
2021-07-03 13:56:53 +00:00
|
|
|
}
|