-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.ts
More file actions
54 lines (43 loc) · 1.46 KB
/
request.ts
File metadata and controls
54 lines (43 loc) · 1.46 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
import {request, RequestOptions} from "node:https";
import {Buffer} from "node:buffer";
import {env} from "process";
const lockDelay = parseInt(env.SESC_REQUEST_LOCK_DELAY ?? "10000");
let lastLockTime = 0;
type HTTPSResponse = {
status: number,
body: string
}
const HTTPSRequest = (options: string | RequestOptions | URL) => new Promise<HTTPSResponse>((resolve, reject) => {
let body = Buffer.alloc(0);
let clientRequest = request(options, (res) => {
res.on("data", (chunk: Buffer) => {
body = Buffer.concat([body, chunk]);
});
res.on("end", () => {
resolve({
status: res.statusCode ?? 400,
body: body.toString()
});
});
res.on("error", reject);
});
clientRequest.on("timeout", () => reject("timeout"));
clientRequest.on("error", reject);
clientRequest.end();
});
export default async function SESCRequest(options: string | RequestOptions | URL): Promise<string> {
while (true) {
if (Date.now() - lastLockTime < lockDelay) continue;
try {
let response = await HTTPSRequest(options);
if (response.status == 502) {
lastLockTime = Date.now();
continue;
}
if (!response.body.includes("Page is being generated.") && response.status == 200) return response.body;
}
catch (error) {
lastLockTime = Date.now();
}
}
}