This commit is contained in:
Archeb
2024-06-25 00:25:10 +08:00
parent 3a11caf76e
commit 79078fccc1
3 changed files with 478 additions and 387 deletions
+35 -8
View File
@@ -1,11 +1,11 @@
import * as docx from "docx";
import cookie from "cookie";
import fs from "fs";
function createDirectoryIfNotExists(dirPath) {
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
}
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
}
}
function extractCookie(cookies) {
@@ -62,12 +62,39 @@ function getSessionCookie(jwtSession, jwtToken) {
httpOnly: true,
secure: true,
sameSite: "Lax",
}
];
},
];
}
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
return new Promise((resolve) => setTimeout(resolve, ms));
}
function createDocx(content) {
var paragraphs = [];
content.split("\n").forEach((line) => {
paragraphs.push(
new docx.Paragraph({
children: [new docx.TextRun(line)],
})
);
});
var doc = new docx.Document({
sections: [
{
properties: {},
children: paragraphs,
},
],
});
return docx.Packer.toBuffer(doc).then((buffer) => buffer);
}
// eventStream util
function createEvent(event, data) {
// if data is object, stringify it
if (typeof data === "object") {
data = JSON.stringify(data);
}
return `event: ${event}\ndata: ${data}\n\n`;
}
export { createDirectoryIfNotExists, sleep, extractCookie, getSessionCookie };
export { createEvent, createDirectoryIfNotExists, sleep, extractCookie, getSessionCookie, createDocx };