refactor: apply eslint

This commit is contained in:
sbplat
2024-01-04 20:17:54 -05:00
parent 5fd505d4f4
commit 9c1588d150
53 changed files with 647 additions and 636 deletions

View File

@@ -1,14 +1,18 @@
import "@stirling-pdf/shared-operations/src/locales/i18next.config";
import express from 'express';
import express from "express";
const app = express();
const PORT = 8000;
// server-node: backend api
import api from './routes/api/api-controller';
import api from "./routes/api/api-controller";
app.use("/api", api);
// serve
app.listen(PORT, () => {
console.log(`http://localhost:${PORT}`);
});
});
process.on('unhandledRejection', (reason, promise) => {
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
});

View File

@@ -1,7 +1,7 @@
import express, { Request, Response } from 'express';
import express, { Request, Response } from "express";
import workflow from './workflow-controller';
import dynamicOperations from './dynamic-operations-controller';
import workflow from "./workflow-controller";
import dynamicOperations from "./dynamic-operations-controller";
const router = express.Router();

View File

@@ -1,26 +1,26 @@
import express, { Request, Response } from 'express';
import express, { Request, Response } from "express";
const router = express.Router();
import multer from 'multer';
import multer from "multer";
const upload = multer();
import { getOperatorByName } from '@stirling-pdf/shared-operations/src/workflow/getOperatorByName';
import { Operator } from '@stirling-pdf/shared-operations/src/functions';
import { getOperatorByName } from "@stirling-pdf/shared-operations/src/workflow/getOperatorByName";
import { Operator } from "@stirling-pdf/shared-operations/src/functions";
import { PdfFile } from '@stirling-pdf/shared-operations/src/wrappers/PdfFile';
import { respondWithPdfFiles } from '../../utils/endpoint-utils';
import { Action } from '@stirling-pdf/shared-operations/declarations/Action';
import { JoiPDFFileSchema } from '@stirling-pdf/shared-operations/src/wrappers/PdfFileJoi';
import { PdfFile } from "@stirling-pdf/shared-operations/src/wrappers/PdfFile";
import { respondWithPdfFiles } from "../../utils/endpoint-utils";
import { Action } from "@stirling-pdf/shared-operations/declarations/Action";
import { JoiPDFFileSchema } from "@stirling-pdf/shared-operations/src/wrappers/PdfFileJoi";
router.post('/:func', upload.array("file"), async function(req: Request, res: Response) {
router.post("/:func", upload.array("file"), async function(req: Request, res: Response) {
handleEndpoint(req, res);
});
router.post('/:dir/:func', upload.array("file"), async function(req: Request, res: Response) {
router.post("/:dir/:func", upload.array("file"), async function(req: Request, res: Response) {
handleEndpoint(req, res);
});
function handleEndpoint(req: Request, res: Response) {
if(!req.files || req.files.length == 0) {
res.status(400).json({error: "no input file(s) were provided"})
res.status(400).json({error: "no input file(s) were provided"});
return;
}
@@ -46,11 +46,11 @@ function handleEndpoint(req: Request, res: Response) {
operation.run(validationResults.value.input, (progress) => {}).then(pdfFiles => {
respondWithPdfFiles(res, pdfFiles, req.params.func + "_result");
})
});
}
}
else {
res.status(400).json({error: `the operator of type ${req.params.func} does not exist`})
res.status(400).json({error: `the operator of type ${req.params.func} does not exist`});
}
}

View File

@@ -1,15 +1,15 @@
import express, { Request, Response } from 'express';
import crypto from 'crypto';
import multer from 'multer'
import express, { Request, Response } from "express";
import crypto from "crypto";
import multer from "multer";
const upload = multer();
import { traverseOperations } from "@stirling-pdf/shared-operations/src/workflow/traverseOperations";
import { PdfFile, RepresentationType } from '@stirling-pdf/shared-operations/src/wrappers/PdfFile';
import { respondWithPdfFiles } from '../../utils/endpoint-utils';
import { JoiPDFFileSchema } from '@stirling-pdf/shared-operations/src/wrappers/PdfFileJoi';
import { PdfFile, RepresentationType } from "@stirling-pdf/shared-operations/src/wrappers/PdfFile";
import { respondWithPdfFiles } from "../../utils/endpoint-utils";
import { JoiPDFFileSchema } from "@stirling-pdf/shared-operations/src/wrappers/PdfFileJoi";
interface Workflow {
eventStream?: express.Response<any, Record<string, any>>,
eventStream?: express.Response,
result?: PdfFile[],
finished: boolean,
createdAt: EpochTimeStamp,
@@ -73,19 +73,19 @@ router.post("/:workflowUuid?", [
} else {
throw err;
}
})
});
}
else {
console.log("Start Aync Workflow");
// TODO: UUID collision checks
let workflowID = req.params.workflowUuid
let workflowID = req.params.workflowUuid;
if(!workflowID)
workflowID = generateWorkflowID();
activeWorkflows[workflowID] = {
createdAt: Date.now(),
finished: false
}
};
const activeWorkflow = activeWorkflows[workflowID];
res.status(200).json({
@@ -104,7 +104,7 @@ router.post("/:workflowUuid?", [
activeWorkflow.eventStream.write(`data: ${state}\n\n`);
}).then(async (pdfResults) => {
if(activeWorkflow.eventStream) {
activeWorkflow.eventStream.write(`data: processing done\n\n`);
activeWorkflow.eventStream.write("data: processing done\n\n");
activeWorkflow.eventStream.end();
}
@@ -170,16 +170,16 @@ router.get("/progress-stream/:workflowUuid", (req: Request, res: Response) => {
// TODO: Check if already done
// Send realtime updates
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Connection', 'keep-alive');
res.setHeader("Cache-Control", "no-cache");
res.setHeader("Content-Type", "text/event-stream");
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Connection", "keep-alive");
res.flushHeaders(); // flush the headers to establish SSE with client
const workflow = activeWorkflows[req.params.workflowUuid];
workflow.eventStream = res;
res.on('close', () => {
res.on("close", () => {
res.end();
// TODO: Abort if not already done?
});
@@ -203,7 +203,7 @@ router.get("/result/:workflowUuid", async (req: Request, res: Response) => {
const workflow = activeWorkflows[req.params.workflowUuid];
if(!workflow.finished) {
res.status(202).json({ message: "Workflow hasn't finished yet. Check progress or connect to progress-steam to get notified when its done." });
return
return;
}
await respondWithPdfFiles(res, workflow.result, "workflow-results");

View File

@@ -1,13 +1,13 @@
import { Response } from 'express';
import { PdfFile } from '@stirling-pdf/shared-operations/src/wrappers/PdfFile'
import Archiver from 'archiver';
import { Response } from "express";
import { PdfFile } from "@stirling-pdf/shared-operations/src/wrappers/PdfFile";
import Archiver from "archiver";
export async function respondWithFile(res: Response, uint8Array: Uint8Array, filename: string, mimeType: string): Promise<void> {
res.writeHead(200, {
'Content-Type': mimeType,
'Content-disposition': `attachment; filename="${filename}"`,
'Content-Length': uint8Array.length
"Content-Type": mimeType,
"Content-disposition": `attachment; filename="${filename}"`,
"Content-Length": uint8Array.length
});
res.end(uint8Array);
}
@@ -23,14 +23,14 @@ export async function respondWithZip(res: Response, filename: string, files: {ui
return;
}
console.log(filename)
console.log(filename);
res.writeHead(200, {
'Content-Type': 'application/zip',
'Content-disposition': `attachment; filename="${filename}.zip"`,
"Content-Type": "application/zip",
"Content-disposition": `attachment; filename="${filename}.zip"`,
});
// TODO: Also allow changing the compression level
var zip = Archiver('zip');
const zip = Archiver("zip");
// Stream the file to the user.
zip.pipe(res);
@@ -50,10 +50,10 @@ export async function respondWithPdfFiles(res: Response, pdfFiles: PdfFile[] | u
res.status(500).json({"warning": "The workflow had no outputs."});
}
else if (pdfFiles.length == 1) {
respondWithPdfFile(res, pdfFiles[0])
respondWithPdfFile(res, pdfFiles[0]);
}
else {
const promises = pdfFiles.map(async (pdf) => {return{uint8Array: await pdf.uint8Array, filename: pdf.filename + ".pdf"}})
const promises = pdfFiles.map(async (pdf) => {return{uint8Array: await pdf.uint8Array, filename: pdf.filename + ".pdf"}});
const files = await Promise.all(promises);
respondWithZip(res, filename, files);
}

View File

@@ -1,9 +1,9 @@
import fs from 'fs';
import os from 'os';
import path from 'path';
import { exec, spawn } from 'child_process'
import { PdfFile, RepresentationType } from '@stirling-pdf/shared-operations/src/wrappers/PdfFile'
import fs from "fs";
import os from "os";
import path from "path";
import { exec, spawn } from "child_process";
import { PdfFile, RepresentationType } from "@stirling-pdf/shared-operations/src/wrappers/PdfFile";
export async function fileToPdf(byteArray: Uint8Array, filename: string): Promise<PdfFile> {
const parentDir = path.join(os.tmpdir(), "StirlingPDF");
@@ -46,14 +46,14 @@ export function isLibreOfficeInstalled() {
const result = stdout.match("LibreOffice ([0-9]+\.){4}.*");
resolve(result ? true : false);
});
})
});
}
function writeBytesToFile(filePath: string, bytes: Uint8Array): Promise<void> {
return new Promise((resolve, reject) => {
fs.writeFile(filePath, bytes, function(err) {
if(err) {
reject(err)
reject(err);
return;
}
resolve();
@@ -80,17 +80,17 @@ function runLibreOfficeCommand(idKey: string, args: string[]): Promise<string[]>
const process = spawn("libreoffice", args);
process.stdout.on('data', (data) => {
process.stdout.on("data", (data) => {
const dataStr = data.toString();
console.log(`Progress ${idKey}:`, dataStr);
messageList.push(dataStr);
});
process.stderr.on('data', (data) => {
process.stderr.on("data", (data) => {
console.error(`stderr ${idKey}:`, data.toString());
});
process.on('exit', (code) => {
process.on("exit", (code) => {
if (code === 0) {
resolve(messageList);
} else {
@@ -98,7 +98,7 @@ function runLibreOfficeCommand(idKey: string, args: string[]): Promise<string[]>
}
});
process.on('error', (err) => {
process.on("error", (err) => {
reject(err);
});