Docker and others

This commit is contained in:
2021-06-13 20:02:21 -03:00
parent d4651be3c8
commit 0bc516c676
11 changed files with 296 additions and 100 deletions

@ -1,6 +1,8 @@
const { execSync } = require("child_process");
const { readFileSync, writeFileSync } = require("fs");
const { tmpdir } = require("os");
const ce = require("./commandExist");
const { join } = require("path");
const CommandExists = require("./commandExist");
/**
* make a request and receive its value, this locks up all the java work
*
@ -8,35 +10,113 @@ const ce = require("./commandExist");
* @param {JSON} options
* @return {*}
*/
const FetchSync = function (url, options){
const CommandsAvaible = {
"curl": CommandExists("curl"),
"wget": CommandExists("wget"),
}
const CommandOptions = {
wget: {
method: {
GET: {
arg: ""
},
POST: {
arg: ""
},
PUT: {
arg: ""
},
DELETE: {
arg: ""
}
},
headers: "",
body: "",
output: "-O"
},
curl: {
method: {
GET: {
arg: "-X GET"
},
POST: {
arg: "-X POST"
},
PUT: {
arg: "-X PUT"
},
DELETE: {
arg: "-X DELETE"
}
},
headers: "-H",
body: "-F",
output: "--output"
}
}
function FetchSync(
url = "https://google.com",
binary = false,
options = {
body: {},
headers: {},
method: ""
}
){
// Decode URL
url = decodeURI(url);
var command = {
command: null,
shell: null
};
if (typeof options !== "object") options = {}
if (process.platform === "linux" || process.platform === "android" || process.platform === "darwin"){
if (ce("curl")) {
command.command = `curl -sS "${url}"`
command.shell = "/bin/sh"
} else if (ce("wget")) {
command.command = `wget -qO- "${url}"`
command.shell = "/bin/sh"
}
} else if (process.platform === "win32") {
if (ce("curl")) {
command.command = `curl -sS "${url}"`
command.shell = "C:\\Windows\\System32\\cmd.exe"
} else {
command.command = `(Invoke-WebRequest -URI "${url}").Content`;
if (process.arch === "x64") command.shell = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe";
else command.shell = "C:\\Windows\\SysWOW64\\WindowsPowerShell\\v1.0\\powershell.exe";
const CommandExec = {
command: "",
body: [],
headers: [],
output: undefined
}
// Body
if (options.body){
if (CommandsAvaible.curl) {
for (let _B of Object.getOwnPropertyNames(options.body)) {
CommandExec.body.push(`${CommandOptions.curl.body} ${_B}="${options.body[_B]}"`)
}
}
}
const ValueReturn = execSync(command.command.split(/\s+/).join(" "), {cwd: tmpdir(), shell: command.shell})
return {
text: function (){return ValueReturn.toString()},
json: function(){return JSON.parse(ValueReturn.toString())}
// Header
if (options.headers){
if (CommandsAvaible.curl) {
CommandExec.command = "curl"
for (let _B of Object.getOwnPropertyNames(options.headers)) {
CommandExec.body.push(`${CommandOptions.curl.headers} ${_B}="${options.headers[_B]}"`)
}
}
}
// Binary
const tmpFile = join(tmpdir(), `${(Math.random() * 1000 * Math.random())}_FetchSyncTemp.tmp`)
if (CommandsAvaible.wget) {
CommandExec.command = "wget"
if (binary) CommandExec.output = `${CommandOptions.wget.output} ${tmpFile}`; else CommandExec.output = " -qO-"
} else if (CommandsAvaible.curl) {
CommandExec.command = "curl -sS"
if (binary) CommandExec.output = `${CommandOptions.curl.output} "${tmpFile}"`;
}
// Exec
const command = `${CommandExec.command} '${url}' ${CommandExec.body.join(" ")} ${CommandExec.headers.join(" ")} ${CommandExec.output}`
console.log(command);
var Exec = execSync(command);
if (binary) {
const SavedBuffer = Buffer.from(readFileSync(tmpFile))
return {
Buffer: SavedBuffer,
save: function(path = tmpFile){writeFileSync(path, SavedBuffer, "binary");return path}
}
} else {
return {
Buffer: Exec,
text: function (){return Exec.toString()},
json: function(){return JSON.parse(Exec.toString())}
}
}
}