Skip to content

Commit 4045fc6

Browse files
Fix tools/generated.js script
1 parent ecfafae commit 4045fc6

File tree

4 files changed

+77
-69
lines changed

4 files changed

+77
-69
lines changed

src/commands/network.js

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,38 @@ function parseBlockNetworkRequest(parser) {
1717

1818
return {
1919
'instructions': [
20-
`await pages[0].setRequestInterception(true);
21-
pages[0].on('request', interceptedRequest => {
22-
if (interceptedRequest.isInterceptResolutionHandled()) return;
23-
function matchesGlob(glob, text) {
24-
const wildcard = glob.indexOf("*");
25-
if (wildcard === -1) {
26-
return glob === text;
27-
}
28-
const prefixGlob = glob.substring(0, wildcard);
29-
const prefixText = text.substring(0, wildcard);
30-
if (prefixGlob !== prefixText) {
31-
return false;
32-
}
33-
const suffixGlob = glob.substring(wildcard + 1);
34-
let suffixText = text.substring(wildcard);
35-
if (suffixGlob.indexOf("*") === -1) {
36-
return suffixText.endsWith(suffixGlob);
37-
}
38-
let matched = matchesGlob(suffixGlob, suffixText);
39-
while (suffixText !== "" && !matched) {
40-
suffixText = suffixText.substring(1);
41-
matched = matchesGlob(suffixGlob, suffixText);
42-
}
43-
return matched;
44-
}
45-
if (matchesGlob("${glob}", interceptedRequest.url())) {
46-
interceptedRequest.abort();
47-
} else {
48-
interceptedRequest.continue({}, 0);
49-
}
50-
});`,
20+
`\
21+
await pages[0].setRequestInterception(true);
22+
pages[0].on('request', interceptedRequest => {
23+
if (interceptedRequest.isInterceptResolutionHandled()) return;
24+
function matchesGlob(glob, text) {
25+
const wildcard = glob.indexOf("*");
26+
if (wildcard === -1) {
27+
return glob === text;
28+
}
29+
const prefixGlob = glob.substring(0, wildcard);
30+
const prefixText = text.substring(0, wildcard);
31+
if (prefixGlob !== prefixText) {
32+
return false;
33+
}
34+
const suffixGlob = glob.substring(wildcard + 1);
35+
let suffixText = text.substring(wildcard);
36+
if (suffixGlob.indexOf("*") === -1) {
37+
return suffixText.endsWith(suffixGlob);
38+
}
39+
let matched = matchesGlob(suffixGlob, suffixText);
40+
while (suffixText !== "" && !matched) {
41+
suffixText = suffixText.substring(1);
42+
matched = matchesGlob(suffixGlob, suffixText);
43+
}
44+
return matched;
45+
}
46+
if (matchesGlob("${glob}", interceptedRequest.url())) {
47+
interceptedRequest.abort();
48+
} else {
49+
interceptedRequest.continue({}, 0);
50+
}
51+
});`,
5152
],
5253
};
5354
}

src/index.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ const consts = require('./consts.js');
2121
const Module = require('module');
2222
const readline = require('readline-sync');
2323

24+
const CODE_WRAPPER = `module.exports.f = async function(pages, arg){
25+
let page = pages[pages.length - 1];
26+
if (page.contentFrame) {
27+
page = await page.contentFrame();
28+
} else {
29+
page = page.mainFrame();
30+
}`;
31+
2432
// TODO: Make it into a class to provide some utility methods like 'isFailure'.
2533
const Status = {
2634
'Ok': 0,
@@ -34,13 +42,7 @@ const Status = {
3442
function loadContent(content) {
3543
const m = new Module();
3644
m.paths = [__dirname];
37-
m._compile(`module.exports.f = async function(pages, arg){
38-
let page = pages[pages.length - 1];
39-
if (page.contentFrame) {
40-
page = await page.contentFrame();
41-
} else {
42-
page = page.mainFrame();
43-
}
45+
m._compile(`${CODE_WRAPPER}
4446
${content}
4547
};`, __dirname);
4648
return m.exports.f;
@@ -830,4 +832,7 @@ if (require.main === module) {
830832
'Options': Options,
831833
'loadBrowser': loadPuppeteer,
832834
};
835+
if (process.env.debug_tests === '1') {
836+
module.exports['CODE_WRAPPER'] = CODE_WRAPPER;
837+
}
833838
}
Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
instructions = [
22
"""await pages[0].setRequestInterception(true);
3-
pages[0].on('request', interceptedRequest => {
4-
if (interceptedRequest.isInterceptResolutionHandled()) return;
5-
function matchesGlob(glob, text) {
6-
const wildcard = glob.indexOf(\"*\");
7-
if (wildcard === -1) {
8-
return glob === text;
9-
}
10-
const prefixGlob = glob.substring(0, wildcard);
11-
const prefixText = text.substring(0, wildcard);
12-
if (prefixGlob !== prefixText) {
13-
return false;
14-
}
15-
const suffixGlob = glob.substring(wildcard + 1);
16-
let suffixText = text.substring(wildcard);
17-
if (suffixGlob.indexOf(\"*\") === -1) {
18-
return suffixText.endsWith(suffixGlob);
19-
}
20-
let matched = matchesGlob(suffixGlob, suffixText);
21-
while (suffixText !== \"\" && !matched) {
22-
suffixText = suffixText.substring(1);
23-
matched = matchesGlob(suffixGlob, suffixText);
24-
}
25-
return matched;
26-
}
27-
if (matchesGlob(\"x\", interceptedRequest.url())) {
28-
interceptedRequest.abort();
29-
} else {
30-
interceptedRequest.continue({}, 0);
31-
}
32-
});""",
3+
pages[0].on('request', interceptedRequest => {
4+
if (interceptedRequest.isInterceptResolutionHandled()) return;
5+
function matchesGlob(glob, text) {
6+
const wildcard = glob.indexOf(\"*\");
7+
if (wildcard === -1) {
8+
return glob === text;
9+
}
10+
const prefixGlob = glob.substring(0, wildcard);
11+
const prefixText = text.substring(0, wildcard);
12+
if (prefixGlob !== prefixText) {
13+
return false;
14+
}
15+
const suffixGlob = glob.substring(wildcard + 1);
16+
let suffixText = text.substring(wildcard);
17+
if (suffixGlob.indexOf(\"*\") === -1) {
18+
return suffixText.endsWith(suffixGlob);
19+
}
20+
let matched = matchesGlob(suffixGlob, suffixText);
21+
while (suffixText !== \"\" && !matched) {
22+
suffixText = suffixText.substring(1);
23+
matched = matchesGlob(suffixGlob, suffixText);
24+
}
25+
return matched;
26+
}
27+
if (matchesGlob(\"x\", interceptedRequest.url())) {
28+
interceptedRequest.abort();
29+
} else {
30+
interceptedRequest.continue({}, 0);
31+
}
32+
});""",
3333
]

tools/generated.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const path = require('path');
99
const fs = require('fs');
1010
const toml = require('@iarna/toml');
1111
const {getCurrentDir, stripCommonPathsPrefix} = require('../src/utils.js');
12+
process.env.debug_tests = '1'; // We enable this to get CODE_WRAPPER from `src/index.js`.
13+
const { CODE_WRAPPER } = require('../src/index.js');
1214

1315
async function checkGeneratedJs(x) {
1416
const { ESLint } = require('eslint');
@@ -38,7 +40,7 @@ async function checkGeneratedJs(x) {
3840
const full = `\
3941
${script}
4042
41-
module.exports.f = async function(page, arg){
43+
${CODE_WRAPPER}
4244
4345
// Start of code.
4446
${content['instructions'].join('\n// ----------\n')}

0 commit comments

Comments
 (0)