converter-toless-plugin
Advanced tools
Comparing version 0.1.3 to 0.1.4
import type { Express, Response } from "express" | ||
import type { Express, Request, Response } from "express" | ||
import { NamePro } from "../src/components/utils"; | ||
@@ -24,3 +25,3 @@ | ||
buildOutputDirectory: boolean; | ||
whitelistInputs: string[]; | ||
accept: string[]; | ||
} | ||
@@ -109,1 +110,31 @@ | ||
export interface fileData { | ||
id: string; | ||
finished: boolean; | ||
logs: string; | ||
} | ||
export interface ReqestData { | ||
cmd: string; | ||
name: string; | ||
callback: string; | ||
} | ||
export namespace Props { | ||
interface compileWithLink { | ||
req: Request; | ||
token: string; | ||
nameprop: NamePro; | ||
compiler: Compiler; | ||
cmd: string; | ||
} | ||
interface compileFile { | ||
req: Request; | ||
token: string; | ||
nameprops: NamePro; | ||
compileIndex: number; | ||
} | ||
} |
@@ -31,3 +31,3 @@ "use strict"; | ||
buildOutputDirectory: true, | ||
whitelistInputs: ["Doc", "Docx"] | ||
accept: ["Doc", "Docx"] | ||
}, | ||
@@ -74,3 +74,2 @@ ], | ||
return __awaiter(this, void 0, void 0, function* () { | ||
this.log(`visited`, req); | ||
this.router.setPage(res, "main", { | ||
@@ -123,5 +122,5 @@ compilers: this.compilers | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const compileType = req.body.type; | ||
const compileIndex = req.body.type; | ||
const file = req.file; | ||
if (compileType == undefined) { | ||
if (compileIndex == undefined) { | ||
this.error("No compiler has been selected!", req, token); | ||
@@ -133,13 +132,12 @@ return; | ||
yield nameprops.filter(this.filter); | ||
const nameWT = nameprops.withType(); | ||
const compiler = this.compilers[compileType]; | ||
const compiler = this.compilers[compileIndex]; | ||
if (!compiler) { | ||
this.error(`the compiler ${compileType} doesn't exists`, req, token); | ||
this.error(`the compiler ${compileIndex} doesn't exists`, req, token); | ||
return; | ||
} | ||
if (compiler.whitelistInputs[0]) { | ||
if (compiler.whitelistInputs.length > 0 && compiler.whitelistInputs.map(a => a.toUpperCase()).indexOf(nameprops.type.toUpperCase()) == -1) { | ||
if (compiler.accept[0]) { | ||
if (compiler.accept.length > 0 && compiler.accept.map(a => a.toUpperCase()).indexOf(nameprops.type.toUpperCase()) == -1) { | ||
this.error(`Not an acceptable file type by the compiler | ||
${compiler.name}\n it only accepts | ||
[${compiler.whitelistInputs.join(`,`)}]`, req, token); | ||
[${compiler.accept.join(`,`)}]`, req, token); | ||
return; | ||
@@ -155,3 +153,3 @@ } | ||
this.router.newSocketMessage(token, "log", "Compiling"); | ||
yield this.compileFile(token, nameprops, compileType).catch(errlog); | ||
yield this.compileFile({ req, token, nameprops, compileIndex }).catch(errlog); | ||
yield utils_1.deleteFile(inputFilePath).catch(errlog); | ||
@@ -203,13 +201,12 @@ this.router.newSocketMessage(token, "log", "zipping the folder"); | ||
} | ||
compileFile(token, nameprop, compileIndex) { | ||
compileFile({ req, token, nameprops, compileIndex }) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const command = yield this.Command(nameprop, compileIndex); | ||
const command = yield this.Command(nameprops, compileIndex); | ||
const compiler = this.compilers[compileIndex]; | ||
let compilerPath = path_1.join(this.router.path("main"), compiler.CompilerPath); | ||
const cmd = `${compiler.commander} "${compilerPath}" ${command}`; | ||
if (compiler.CompilerLink) { | ||
return this.requestCompiler(compiler.CompilerLink, `${compiler.commander} "${compilerPath}" ${command}`, (stdout) => { | ||
this.router.newSocketMessage(token, "log", stdout); | ||
}); | ||
return this.compileWithLink({ req, token, nameprop: nameprops, compiler, cmd }); | ||
} | ||
return this.execShellCommand(`${compiler.commander} "${compilerPath}" ${command}`, (stdout) => { | ||
return this.execShellCommand(cmd, (stdout) => { | ||
this.router.newSocketMessage(token, "log", stdout); | ||
@@ -219,2 +216,17 @@ }); | ||
} | ||
compileWithLink(props) { | ||
const { req, token, nameprop, compiler, cmd } = props; | ||
return new Promise((solve, reject) => __awaiter(this, void 0, void 0, function* () { | ||
const href = `${req.protocol}://${req.get('host')}`; | ||
const callback = `/${this.alldir.maindir}/cb/${nameprop.name}`; | ||
const reqData = { cmd, name: nameprop.name, callback: href + callback }; | ||
let file = yield this.requestCompiler(compiler.CompilerLink, reqData); | ||
this.router.newSocketMessage(token, "log", file.logs); | ||
const inter = setTimeout(reject, 3e+7); | ||
this.app.post(callback, (req) => { | ||
clearTimeout(inter); | ||
solve(req.body.file); | ||
}); | ||
})); | ||
} | ||
zipTheOutputDirectory(path) { | ||
@@ -246,7 +258,9 @@ return __awaiter(this, void 0, void 0, function* () { | ||
} | ||
requestCompiler(cCompilerLink, cmd, stdcb) { | ||
requestCompiler(CompilerLink, data) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let data = yield node_fetch_1.default(cCompilerLink, { method: 'GET', body: JSON.stringify({ cmd }) }); | ||
stdcb(data); | ||
return data; | ||
return yield node_fetch_1.default(CompilerLink, { | ||
method: 'POST', | ||
body: JSON.stringify(data), | ||
headers: { 'Content-Type': 'application/json' } | ||
}).then(res => res.json()); | ||
}); | ||
@@ -262,3 +276,3 @@ } | ||
}); | ||
Promise.resolve(); | ||
return null; | ||
}); | ||
@@ -288,4 +302,4 @@ } | ||
} | ||
log(errorMes, req, resOrToken) { | ||
return this.logger("log", errorMes, req, resOrToken); | ||
log(logMes, req, resOrToken) { | ||
return this.logger("log", logMes, req, resOrToken); | ||
} | ||
@@ -292,0 +306,0 @@ error(errorMes, req, resOrToken) { |
{ | ||
"name": "converter-toless-plugin", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "this is a plugin", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
135
src/index.ts
@@ -14,3 +14,3 @@ // for running commands | ||
// for types | ||
import { Compiler, converterOptions, Dirs, Router } from "../d/types"; | ||
import { Compiler, converterOptions, Dirs, fileData, Props, ReqestData, Router } from "../d/types"; | ||
@@ -45,3 +45,3 @@ | ||
buildOutputDirectory: true, | ||
whitelistInputs: ["Doc", "Docx"] | ||
accept: ["Doc", "Docx"] | ||
}, | ||
@@ -133,3 +133,3 @@ ], | ||
// log a user visit | ||
this.log(`visited`, req) | ||
//this.log(`visited`, req) | ||
@@ -206,5 +206,8 @@ this.router.setPage(res, "main", | ||
/** This function handles converting steps | ||
* @param token the user socket token | ||
* @param req the user request | ||
*/ | ||
async convert(token: string, req: Request) { | ||
const compileType: number = req.body.type; | ||
const compileIndex: number = req.body.type; | ||
const file = req.file | ||
@@ -214,3 +217,3 @@ | ||
// check if a compile is been selected | ||
if (compileType == undefined) { | ||
if (compileIndex == undefined) { | ||
this.error("No compiler has been selected!", req, token); | ||
@@ -229,11 +232,8 @@ return; | ||
const nameWT = nameprops.withType(); | ||
// get the right compiler | ||
const compiler = this.compilers[compileType] | ||
const compiler = this.compilers[compileIndex] | ||
// Check if the compiler actually there | ||
if (!compiler) { | ||
this.error(`the compiler ${compileType} doesn't exists`, req, token); | ||
this.error(`the compiler ${compileIndex} doesn't exists`, req, token); | ||
return; | ||
@@ -243,8 +243,8 @@ } | ||
// check if the compiler can work with the file | ||
if (compiler.whitelistInputs[0]) { | ||
if (compiler.whitelistInputs.length > 0 && compiler.whitelistInputs.map(a => a.toUpperCase()).indexOf(nameprops.type.toUpperCase()) == -1) { | ||
if (compiler.accept[0]) { | ||
if (compiler.accept.length > 0 && compiler.accept.map(a => a.toUpperCase()).indexOf(nameprops.type.toUpperCase()) == -1) { | ||
this.error( | ||
`Not an acceptable file type by the compiler | ||
${compiler.name}\n it only accepts | ||
[${compiler.whitelistInputs.join(`,`)}]` | ||
[${compiler.accept.join(`,`)}]` | ||
@@ -277,6 +277,6 @@ , req, token) | ||
// compiling | ||
await this.compileFile(token, nameprops, compileType).catch(errlog); | ||
await this.compileFile({ req, token, nameprops, compileIndex }).catch(errlog); | ||
// delete the input file | ||
@@ -390,17 +390,20 @@ await deleteFile(inputFilePath).catch(errlog); | ||
/** this function compiles a file*/ | ||
async compileFile(token: string, nameprop: NamePro, compileIndex: number) { | ||
/** this function compiles a file | ||
* | ||
* @param token user token from the socket | ||
* @param nameprop name using {NamePro} class | ||
* @param compileIndex index of the compiler that's been used | ||
*/ | ||
async compileFile({ req, token, nameprops, compileIndex }: Props.compileFile) { | ||
const command = await this.Command(nameprop, compileIndex); | ||
const command = await this.Command(nameprops, compileIndex); | ||
const compiler = this.compilers[compileIndex] | ||
let compilerPath = join(this.router.path("main"), compiler.CompilerPath) | ||
const cmd = `${compiler.commander} "${compilerPath}" ${command}` | ||
if (compiler.CompilerLink) { | ||
return this.requestCompiler(compiler.CompilerLink, `${compiler.commander} "${compilerPath}" ${command}`, (stdout: string) => { | ||
// socket.io sending logs to the user on the proccess | ||
this.router.newSocketMessage(token, "log", stdout) | ||
}) | ||
return this.compileWithLink({ req, token, nameprop: nameprops, compiler, cmd }) | ||
} | ||
return this.execShellCommand(`${compiler.commander} "${compilerPath}" ${command}`, (stdout: string) => { | ||
return this.execShellCommand(cmd, (stdout: string) => { | ||
// socket.io sending logs to the user on the proccess | ||
@@ -411,3 +414,30 @@ this.router.newSocketMessage(token, "log", stdout) | ||
compileWithLink(props: Props.compileWithLink) { | ||
const { req, token, nameprop, compiler, cmd } = props; | ||
return new Promise(async (solve, reject) => { | ||
const href: string = `${req.protocol}://${req.get('host')}`; | ||
const callback: string = `/${this.alldir.maindir}/cb/${nameprop.name}`; | ||
const reqData: ReqestData = { cmd, name: nameprop.name, callback: href + callback }; | ||
let file: fileData = await this.requestCompiler(compiler.CompilerLink, reqData); | ||
// socket.io sending logs | ||
this.router.newSocketMessage(token, "log", file.logs) | ||
const inter: NodeJS.Timeout = setTimeout(reject, 3e+7) | ||
// set a listener for file finishing | ||
this.app.post(callback, (req: Request) => { | ||
clearTimeout(inter) | ||
solve(req.body.file) | ||
}) | ||
}) | ||
} | ||
/** This function compress a directory | ||
* @param path path for the directory | ||
*/ | ||
async zipTheOutputDirectory(path: string) { | ||
@@ -432,3 +462,8 @@ return new Promise((solve, reject) => { | ||
/** this function exicute a programmer with params */ | ||
/** this function execute a programme with params | ||
* | ||
* @param cmd the comamnd that runs the server | ||
* @param stdcb a callback function that handles stdouts | ||
*/ | ||
execShellCommand(cmd: string, stdcb: Function) { | ||
@@ -449,11 +484,20 @@ const execi = exec(cmd, (error, stdout, stderr) => { | ||
/** make a request to one of the compilers */ | ||
async requestCompiler(cCompilerLink: string, cmd: string, stdcb: Function) { | ||
let data = await fetch(cCompilerLink, { method: 'GET', body: JSON.stringify({ cmd }) }) | ||
stdcb(data) | ||
return data | ||
/** Request compiling a file from a compiler. | ||
* @param CompilerLink link for the compiler api | ||
* @param data requestData | ||
*/ | ||
async requestCompiler(CompilerLink: string, data: ReqestData) { | ||
return await fetch(CompilerLink, { | ||
method: 'POST', | ||
body: JSON.stringify(data), | ||
headers: { 'Content-Type': 'application/json' } | ||
}).then(res => res.json()) | ||
} | ||
// downloader | ||
async makeGetReqForTheFile(urlLink: string, filepath: string) { | ||
/** Make a download link for the file | ||
* | ||
* @param urlLink link for the file after sublink | ||
* @param filepath path for the file | ||
*/ | ||
async makeGetReqForTheFile(urlLink: string, filepath: string): Promise<any> { | ||
this.app.get(`/${this.alldir.maindir}/${urlLink}`, (req: Request, res: Response) => { | ||
@@ -465,3 +509,3 @@ if (!fs.existsSync(filepath)) { | ||
}) | ||
Promise.resolve() | ||
return null | ||
} | ||
@@ -471,3 +515,9 @@ | ||
// make logs of the errors and other stuff for debuging. | ||
/** make logs of the errors and other stuff for debuging. | ||
* @param type type of the message | ||
* @param errorMes the error message | ||
* @param req the request to get the ip adress | ||
* @param resOrToken respawns to send or token to send using socket. | ||
*/ | ||
private logger(type: string, errorMes: string, req?: Request, resOrToken?: any) { | ||
@@ -499,8 +549,17 @@ if (resOrToken) | ||
/** just a debugger and a messenger to the client if error*/ | ||
log(errorMes: string, req?: Request, resOrToken?: any) { | ||
return this.logger("log", errorMes, req, resOrToken) | ||
/** just a debugger and a messenger to the client if log | ||
* @param logMes the error message | ||
* @param req the request to get the ip adress | ||
* @param resOrToken respawns to send or token to send using socket. | ||
*/ | ||
log(logMes: string, req?: Request, resOrToken?: any) { | ||
return this.logger("log", logMes, req, resOrToken) | ||
} | ||
/** just a debugger and a messenger to the client if error*/ | ||
/** just a debugger and a messenger to the client if error | ||
* @param errorMes the error message | ||
* @param req the request to get the ip adress | ||
* @param resOrToken respawns to send or token to send using socket. | ||
*/ | ||
error(errorMes: string, req?: Request, resOrToken?: any) { | ||
@@ -507,0 +566,0 @@ return this.logger("err", errorMes, req, resOrToken) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
73410
1411