Skip to content

Commit

Permalink
Externalize puppeteer launchArgs. Add --disable-dev-shm-usage. #164, #…
Browse files Browse the repository at this point in the history
  • Loading branch information
claabs committed Oct 15, 2021
1 parent 5a3b8b1 commit fe2d90c
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 51 deletions.
13 changes: 13 additions & 0 deletions src/common/puppeteer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,16 @@ export function getDevtoolsUrl(page: Page): string {
// devtools://devtools/bundled/inspector.html?ws=127.0.0.1:35871/devtools/page/2B4E5714B42640A1C61AB9EE7E432730
return `devtools://devtools/bundled/inspector.html?ws=${wsEndpoint.host}/devtools/page/${targetId}`;
}

export const launchArgs: Parameters<typeof puppeteer.launch>[0] = {
headless: true,
args: [
'--disable-web-security', // For accessing iframes
'--disable-features=IsolateOrigins,site-per-process', // For accessing iframes
'--no-sandbox', // For Docker root user
'--disable-dev-shm-usage', // https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md#tips
// For debugging in Docker
// '--remote-debugging-port=3001',
// '--remote-debugging-address=0.0.0.0', // Change devtools url to localhost
],
};
11 changes: 2 additions & 9 deletions src/notify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from './common/config';
import L from './common/logger';
import { NotificationReason } from './interfaces/notification-reason';
import puppeteer, { getDevtoolsUrl } from './common/puppeteer';
import puppeteer, { getDevtoolsUrl, launchArgs } from './common/puppeteer';

export async function sendNotification(
url: string,
Expand Down Expand Up @@ -51,14 +51,7 @@ export async function sendNotification(

export async function testNotifiers(): Promise<void> {
L.info('Testing all configured notifiers');
const browser = await puppeteer.launch({
headless: true,
args: [
'--disable-web-security',
'--disable-features=IsolateOrigins,site-per-process',
'--no-sandbox',
],
});
const browser = await puppeteer.launch(launchArgs);
const page = await browser.newPage();
L.trace(getDevtoolsUrl(page));
await page.goto('https://claabs.github.io/epicgames-freegames-node/test.html');
Expand Down
4 changes: 2 additions & 2 deletions src/puppet/hcaptcha.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'fs-extra';
import { ElementHandle, Protocol } from 'puppeteer';
import path from 'path';
import puppeteer, { getDevtoolsUrl } from '../common/puppeteer';
import puppeteer, { getDevtoolsUrl, launchArgs } from '../common/puppeteer';
import { config, CONFIG_DIR } from '../common/config';
import L from '../common/logger';

Expand Down Expand Up @@ -43,7 +43,7 @@ export const getHcaptchaCookies = async (): Promise<Protocol.Network.Cookie[]> =
if (!cookieData) {
try {
L.debug('Setting hCaptcha accessibility cookies');
const browser = await puppeteer.launch({ headless: true, args: ['--no-sandbox'] });
const browser = await puppeteer.launch(launchArgs);
const page = await browser.newPage();

L.trace(getDevtoolsUrl(page));
Expand Down
13 changes: 2 additions & 11 deletions src/puppet/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Protocol, ElementHandle, Page } from 'puppeteer';
import logger from '../common/logger';
import puppeteer, {
getDevtoolsUrl,
launchArgs,
toughCookieFileStoreToPuppeteerCookie,
} from '../common/puppeteer';
import { getCookiesRaw, setPuppeteerCookies } from '../common/request';
Expand Down Expand Up @@ -67,17 +68,7 @@ export default class PuppetLogin {
const userCookies = await getCookiesRaw(this.email);
const puppeteerCookies = toughCookieFileStoreToPuppeteerCookie(userCookies);
this.L.debug('Logging in with puppeteer');
const browser = await puppeteer.launch({
headless: true,
args: [
'--disable-web-security',
'--disable-features=IsolateOrigins,site-per-process',
'--no-sandbox',
// For debugging in Docker
// '--remote-debugging-port=3001',
// '--remote-debugging-address=0.0.0.0', // Change devtools url to localhost
],
});
const browser = await puppeteer.launch(launchArgs);
const page = await browser.newPage();
this.L.trace(getDevtoolsUrl(page));
const cdpClient = await page.target().createCDPSession();
Expand Down
19 changes: 3 additions & 16 deletions src/puppet/purchase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Protocol, ElementHandle } from 'puppeteer';
import logger from '../common/logger';
import puppeteer, {
getDevtoolsUrl,
launchArgs,
toughCookieFileStoreToPuppeteerCookie,
} from '../common/puppeteer';
import { getCookiesRaw, setPuppeteerCookies } from '../common/request';
Expand All @@ -30,14 +31,7 @@ export default class PuppetPurchase {
const userCookies = await getCookiesRaw(this.email);
const puppeteerCookies = toughCookieFileStoreToPuppeteerCookie(userCookies);
this.L.debug('Purchasing with puppeteer');
const browser = await puppeteer.launch({
headless: true,
args: [
'--disable-web-security',
'--disable-features=IsolateOrigins,site-per-process',
'--no-sandbox',
],
});
const browser = await puppeteer.launch(launchArgs);
const page = await browser.newPage();
this.L.trace(getDevtoolsUrl(page));
const cdpClient = await page.target().createCDPSession();
Expand Down Expand Up @@ -113,14 +107,7 @@ export default class PuppetPurchase {
const userCookies = await getCookiesRaw(this.email);
const puppeteerCookies = toughCookieFileStoreToPuppeteerCookie(userCookies);
this.L.debug('Purchasing with puppeteer (short)');
const browser = await puppeteer.launch({
headless: true,
args: [
'--disable-web-security',
'--disable-features=IsolateOrigins,site-per-process',
'--no-sandbox',
],
});
const browser = await puppeteer.launch(launchArgs);
const page = await browser.newPage();
this.L.trace(getDevtoolsUrl(page));
const cdpClient = await page.target().createCDPSession();
Expand Down
7 changes: 2 additions & 5 deletions test/notify.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import puppeteer from '../src/common/puppeteer';
import puppeteer, { launchArgs } from '../src/common/puppeteer';
import { NotificationReason } from '../src/interfaces/notification-reason';
import { sendNotification } from '../src/notify';

jest.setTimeout(1000000);
describe('Create account and redeem free games', () => {
it('should open a portal and notify', async () => {
const browser = await puppeteer.launch({
headless: true,
args: ['--disable-web-security', '--disable-features=IsolateOrigins,site-per-process'],
});
const browser = await puppeteer.launch(launchArgs);
const page = await browser.newPage();
await page.goto('https://google.com');
const url = await page.openPortal();
Expand Down
11 changes: 3 additions & 8 deletions test/util/puppet-account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getHcaptchaCookies } from '../../src/puppet/hcaptcha';
import puppeteer, {
toughCookieFileStoreToPuppeteerCookie,
getDevtoolsUrl,
launchArgs,
} from '../../src/common/puppeteer';
import logger from '../../src/common/logger';
import Smtp4Dev from './smtp4dev';
Expand Down Expand Up @@ -83,10 +84,7 @@ export default class AccountManager {
const userCookies = await getCookiesRaw(this.email);
const puppeteerCookies = toughCookieFileStoreToPuppeteerCookie(userCookies);
this.L.debug('Logging in with puppeteer');
const browser = await puppeteer.launch({
headless: true,
args: ['--disable-web-security', '--disable-features=IsolateOrigins,site-per-process'],
});
const browser = await puppeteer.launch(launchArgs);
const page = await browser.newPage();
this.L.trace(getDevtoolsUrl(page));
const cdpClient = await page.target().createCDPSession();
Expand Down Expand Up @@ -145,10 +143,7 @@ export default class AccountManager {
const userCookies = await getCookiesRaw(this.email);
const puppeteerCookies = toughCookieFileStoreToPuppeteerCookie(userCookies);
this.L.debug('Logging in with puppeteer');
const browser = await puppeteer.launch({
headless: true,
args: ['--disable-web-security', '--disable-features=IsolateOrigins,site-per-process'],
});
const browser = await puppeteer.launch(launchArgs);
const page = await browser.newPage();
this.L.trace(getDevtoolsUrl(page));
const cdpClient = await page.target().createCDPSession();
Expand Down

0 comments on commit fe2d90c

Please sign in to comment.