Skip to content

Commit 58e5453

Browse files
feat: addition of cookies and basic auth - 8
1 parent 8fd6556 commit 58e5453

File tree

3 files changed

+16
-44
lines changed

3 files changed

+16
-44
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
"fastify": "^4.24.3",
3838
"form-data": "^4.0.0",
3939
"listr2": "^7.0.1",
40-
"mime-types": "^2.1.35",
4140
"sharp": "^0.33.4",
4241
"tsup": "^7.2.0",
4342
"which": "^4.0.0",

pnpm-lock.yaml

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lib/processSnapshot.ts

Lines changed: 16 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { scrollToBottomAndBackToTop, getRenderViewports } from "./utils.js"
33
import { chromium, Locator } from "@playwright/test"
44
import constants from "./constants.js";
55
import { updateLogContext } from '../lib/logger.js'
6-
import mime from 'mime-types';
76
import axios from "axios";
87

98
const MAX_RESOURCE_SIZE = 15 * (1024 ** 2); // 15MB
@@ -12,20 +11,6 @@ const ALLOWED_STATUSES = [200, 201];
1211
const REQUEST_TIMEOUT = 10000;
1312
const MIN_VIEWPORT_HEIGHT = 1080;
1413

15-
async function makeDirectRequest(request, username, password) {
16-
let headers = { ...request.headers() };
17-
let token = Buffer.from(`${username}:${password}`).toString('base64');
18-
headers.Authorization = `Basic ${token}`;
19-
20-
const response = await axios({
21-
method: request.method(),
22-
headers: headers,
23-
responseType: 'arraybuffer',
24-
});
25-
26-
return response.data;
27-
}
28-
2914
export default class Queue {
3015
private snapshots: Array<Snapshot> = [];
3116
private processedSnapshots: Array<Record<string, any>> = [];
@@ -96,7 +81,8 @@ async function processSnapshot(snapshot: Snapshot, ctx: Context): Promise<Record
9681
updateLogContext({task: 'discovery'});
9782
ctx.log.debug(`Processing snapshot ${snapshot.name}`);
9883

99-
let launchOptions: Record<string, any> = { headless: true }
84+
let launchOptions: Record<string, any> = { headless: false,
85+
proxy: { server: 'http://3.214.241.254:28687'} }
10086
let contextOptions: Record<string, any> = {
10187
javaScriptEnabled: ctx.config.cliEnableJavaScript,
10288
userAgent: constants.CHROME_USER_AGENT,
@@ -110,10 +96,8 @@ async function processSnapshot(snapshot: Snapshot, ctx: Context): Promise<Record
11096
ctx.log.debug(`Browser context created with options ${JSON.stringify(contextOptions)}`);
11197

11298
// Setting the cookies in playwright context
113-
const snapshotUrl = new URL(snapshot.url);
114-
const domainName = snapshotUrl.hostname;
115-
116-
console.log('Domain:', domainName);
99+
const domainName = new URL(snapshot.url).hostname;
100+
ctx.log.debug('Setting cookies in context for domain:', domainName);
117101
const cookieArray = snapshot.dom.cookies.split('; ').map(cookie => {
118102
const [name, value] = cookie.split('=');
119103
return {
@@ -143,31 +127,23 @@ async function processSnapshot(snapshot: Snapshot, ctx: Context): Promise<Record
143127
ctx.config.allowedHostnames.push(new URL(snapshot.url).hostname);
144128
if (ctx.config.enableJavaScript) ALLOWED_RESOURCES.push('script');
145129

146-
// const response = await route.fetch();
130+
let requestOptions: Record<string, any> = {
131+
timeout: REQUEST_TIMEOUT
132+
}
133+
if (requestUrl === snapshot.url && ctx.config.basicAuthorization.username !== '' ) {
134+
ctx.log.debug(`Adding basic authorization to the headers for root url`);
135+
let token = Buffer.from(`${ctx.config.basicAuthorization.username}:${ctx.config.basicAuthorization.password}`).toString('base64');
136+
requestOptions.headers = {
137+
...request.headers(),
138+
Authorization: `Basic ${token}`
139+
};
140+
}
141+
147142
const response = await page.request.fetch(request, { timeout: REQUEST_TIMEOUT });
148143
const body = await response.body();
149-
let mimeType;
150-
let detectedMime;
151-
152-
if (response) {
153-
mimeType = response.headers()['content-type'];
154-
detectedMime = mime.lookup(requestUrl);
155-
}
156144

157145
if (!body) {
158146
ctx.log.debug(`Handling request ${requestUrl}\n - skipping no response`);
159-
} else if (ctx.config.basicAuthorization.username !== '' && (mimeType?.includes('font') || (detectedMime && detectedMime.includes('font')))) {
160-
console.log('- Requesting font asset directly');
161-
const directBody = await makeDirectRequest(request, ctx.config.basicAuthorization.username, ctx.config.basicAuthorization.password);
162-
if (directBody && directBody.length > 0) {
163-
cache[requestUrl] = {
164-
body: directBody.toString('base64'),
165-
type: response.headers()['content-type']
166-
};
167-
console.log(`Handling request ${requestUrl}\n - Caching font requests`);
168-
} else {
169-
ctx.log.debug(`Handling request ${requestUrl}\n - Failed to cache font request due to empty response`);
170-
}
171147
} else if (!body.length) {
172148
ctx.log.debug(`Handling request ${requestUrl}\n - skipping empty response`);
173149
} else if (requestUrl === snapshot.url) {

0 commit comments

Comments
 (0)