This repository was archived by the owner on Apr 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbrowser.ts
140 lines (136 loc) · 4.37 KB
/
browser.ts
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { Queue } from "./queue";
import puppeteer from 'puppeteer';
import { debugLog } from "./logging";
interface paramsType {
maxPages: number,
maxHits: number, // max number of pages over lifetime
maxAgeUnused: number, // seconds since last page opened
}
interface statsType {
pages: number, // curent number of pages
hits: number, // current count of pages opened over lifetime
lastUsedAt: Date,
}
export class Browser {
browser: puppeteer.Browser | null;
launched: Promise<void>;
queue: Queue; // queue of pages to create
params: paramsType;
stats: statsType;
constructor(
executablePath: string,
maxPages: number,
maxHits: number,
maxAgeUnused: number,
maxQueueSize: number,
) {
debugLog.browser("creating");
this.browser = null;
// need to await browser.launched
this.launched = this.launch(executablePath);
this.queue = new Queue(maxQueueSize);
this.params = {
maxPages,
maxHits,
maxAgeUnused,
}
this.stats = {
pages: 0,
hits: 0,
lastUsedAt: new Date(),
}
}
async launch(executablePathStr: string) {
let executablePath: string | undefined = undefined;
if (executablePathStr !== "Puppeteer-bundled") {
executablePath = executablePathStr;
}
this.browser = await puppeteer.launch({
headless: true,
executablePath,
args: [
"--disable-dev-shm-usage",
"--disable-background-networking",
"--disable-default-apps",
"--disable-extensions",
"--disable-gpu",
"--disable-sync",
"--disable-translate",
"--hide-scrollbars",
"--metrics-recording-only",
"--mute-audio",
"--no-first-run",
"--safebrowsing-disable-auto-update"
]
});
debugLog.browser("created");
}
isOverHitLimit() {
const result = this.stats.hits > this.params.maxHits;
if (result) {
debugLog.browser("over hit limit");
}
return result;
}
isUnused() {
const t = new Date().valueOf() / 1000;
const lastUsed = this.stats.lastUsedAt.valueOf() / 1000;
const result = t - lastUsed > this.params.maxAgeUnused;
if (result) {
debugLog.browser("unused");
}
return result;
}
// Set a periodic function to check on renewMe()
renewMe() {
const result = this.isOverHitLimit() && this.stats.pages === 0;
if (result) {
debugLog.browser("renew me");
}
return result;
}
pageAvailable() {
return this.stats.pages < this.params.maxPages;
}
async createPage() {
debugLog.browser("creating page");
if (this.browser === null) {
throw Error("cannot create page for null browser");
}
if (this.isOverHitLimit()) {
throw Error("browser has reached its hit limit of pages");
}
// Browser local queue to create new pages (FIFO)
const id = this.queue.push(); // raise an error if maximum is reached
while (!(this.pageAvailable() && this.queue.isFirst(id))) {
debugLog.browser("waiting to create page");
await sleepAsync(100);
}
this.queue.shift();
this.stats.lastUsedAt = new Date();
const page = await this.browser.newPage();
this.stats.pages++;
debugLog.browser("created page");
return page;
}
async closePage(page: puppeteer.Page) {
debugLog.browser("closing page");
await page.close();
debugLog.browser("closed page");
this.stats.pages--;
}
async close() {
debugLog.browser("closing browser");
if (this.browser === null) {
debugLog.browser("browser is already null");
return;
}
await this.browser.close();
debugLog.browser("closed browser");
}
}
const sleepAsync = async (ms: number) => {
return new Promise(resolve => {
setTimeout(resolve, ms)
});
}