forked from teralomaniac/miaomiaomiao
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.mjs
More file actions
123 lines (117 loc) · 2.78 KB
/
utils.mjs
File metadata and controls
123 lines (117 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import * as docx from "docx";
import cookie from "cookie";
import fs from "fs";
import { execSync } from "child_process";
function getGitRevision() {
// get git revision and branch
try {
const revision = execSync("git rev-parse --short HEAD", { stdio: "pipe" }).toString().trim();
const branch = execSync("git rev-parse --abbrev-ref HEAD", { stdio: "pipe" }).toString().trim();
return { revision, branch };
} catch (e) {
return { revision: "unknown", branch: "unknown" };
}
}
function createDirectoryIfNotExists(dirPath) {
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
}
}
function extractCookie(cookies) {
var jwtSession = null;
var jwtToken = null;
cookies = cookie.parse(cookies);
if (cookies["stytch_session"]) {
jwtSession = cookies["stytch_session"];
}
if (cookies["stytch_session_jwt"]) {
jwtToken = cookies["stytch_session_jwt"];
}
return { jwtSession, jwtToken };
}
function getSessionCookie(jwtSession, jwtToken) {
var sessionCookie = [
{
name: "stytch_session",
value: jwtSession,
domain: "you.com",
path: "/",
expires: 1800000000,
httpOnly: false,
secure: true,
sameSite: "Lax",
},
{
name: "ydc_stytch_session",
value: jwtSession,
domain: "you.com",
path: "/",
expires: 1800000000,
httpOnly: true,
secure: true,
sameSite: "Lax",
},
{
name: "stytch_session_jwt",
value: jwtToken,
domain: "you.com",
path: "/",
expires: 1800000000,
httpOnly: false,
secure: true,
sameSite: "Lax",
},
{
name: "ydc_stytch_session_jwt",
value: jwtToken,
domain: "you.com",
path: "/",
expires: 1800000000,
httpOnly: true,
secure: true,
sameSite: "Lax",
}
];
if(process.env.INCOGNITO_MODE === "true") {
sessionCookie.push({
name: "incognito",
value: "true",
domain: "you.com",
path: "/",
expires: 1800000000,
secure: true,
});
}
return sessionCookie;
}
function sleep(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 { createEvent, createDirectoryIfNotExists, sleep, extractCookie, getSessionCookie, createDocx, getGitRevision };