Skip to content

Commit 7b6a003

Browse files
committed
v1.2.3
1 parent 7650c67 commit 7b6a003

File tree

10 files changed

+69
-89
lines changed

10 files changed

+69
-89
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ const useProxy = require('puppeteer-page-proxy');
5656
await page2.goto(site);
5757
})();
5858
```
59+
To remove a proxy set this way, simply pass a falsy value (e.g `null`) instead of the proxy;
60+
```js
61+
await useProxy(page, null);
62+
```
5963

6064
#### Proxy per request:
6165
```js

changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
# Change log
2+
### [1.2.3] - 2020-02-14
3+
#### Changes
4+
- Added ability to remove page-wide proxy
5+
- Changed static classes to object literals for compability with Node.js **10.16.x** ([#6](https://github.com/Cuadrix/puppeteer-page-proxy/issues/6))
6+
- Removed `src\util\` folder along with proxy-validator
27
### [1.2.2] - 2020-02-09
38
#### Patches
49
- Fixed code indentation.

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "puppeteer-page-proxy",
33
"description": "Additional Node.js module to use with 'puppeteer' for setting proxies per page basis.",
4-
"version": "1.2.2",
4+
"version": "1.2.3",
55
"author": "Cuadrix <[email protected]> (https://github.com/Cuadrix)",
66
"homepage": "https://github.com/Cuadrix/puppeteer-page-proxy",
77
"main": "./src/index.js",
@@ -22,10 +22,11 @@
2222
],
2323
"license": "MIT",
2424
"dependencies": {
25-
"got": "^10.5.2",
25+
"got": "^10.5.5",
2626
"http-proxy-agent": "^4.0.1",
2727
"https-proxy-agent": "^5.0.0",
2828
"socks-proxy-agent": "^5.0.0",
29-
"tough-cookie": "^3.0.1"
29+
"tough-cookie": "^3.0.1",
30+
"type-dragoon": "^1.0.0"
3031
}
3132
}

src/core/lookup.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
const enforceTypes = require("../util/type-enforcer");
1+
const enforceTypes = require("type-dragoon");
22

3-
module.exports = async (page, lookupService = "https://api.ipify.org?format=json", isJSON = true, timeout = 30000) => {
3+
const lookup = async (page, lookupService = "https://api.ipify.org?format=json", isJSON = true, timeout = 30000) => {
44
/**/
5-
enforceTypes(
6-
[page, "object"], [lookupService, "string"], [isJSON, "boolean"], [timeout, "number"]
7-
);
5+
enforceTypes({object: page}, {string: lookupService}, {boolean: isJSON}, {number: timeout});
86
/**/
97
const XMLHttpRequest = async () => {
108
return await page.evaluate((lookupService, timeout, isJSON) => {
@@ -41,4 +39,5 @@ module.exports = async (page, lookupService = "https://api.ipify.org?format=json
4139
return await XMLHttpRequest();
4240
}
4341
}
44-
}
42+
};
43+
module.exports = lookup;

src/core/page-proxy.js

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,21 @@
11
const {setHeaders, setAgent, request} = require("../lib/request");
22
const cookies = require("../lib/cookies");
3-
const enforceTypes = require("../util/type-enforcer");
4-
const validateProxy = require("../util/proxy-validator");
3+
const enforceTypes = require("type-dragoon");
54

6-
module.exports = async (param, proxy) => {
5+
const pageProxy = async (param, proxy) => {
76
/**/
8-
enforceTypes(
9-
[param, "object"], [proxy, "string"]
10-
); validateProxy(proxy);
7+
enforceTypes({object: param});
118
/**/
129
let page, req;
1310
if (param.constructor.name === "Request") {
1411
req = param;
15-
}
16-
else if (param.constructor.name === "Page") {
12+
} else if (param.constructor.name === "Page") {
1713
page = param;
1814
await page.setRequestInterception(true);
1915
} else {
20-
throw new Error("@arg1: Not valid 'Page' or 'Request' object");
16+
throw new Error("Not valid `Page` or `Request` object");
2117
}
2218
const $puppeteerPageProxyHandler = async req => {
23-
if (req._interceptionHandled || !req._allowInterception) {
24-
return;
25-
}
2619
const cookieJar = cookies.store(await cookies.get(
2720
req._client._connection._url, req._frame._id
2821
));
@@ -43,13 +36,23 @@ module.exports = async (param, proxy) => {
4336
await req.abort();
4437
}
4538
};
39+
const removeRequestListener = () => {
40+
const listeners = page.listeners("request");
41+
for (let i = 0; i < listeners.length; i++) {
42+
if (listeners[i].name === "$puppeteerPageProxyHandler") {
43+
page.removeListener("request", listeners[i]);
44+
}
45+
}
46+
};
4647
if (req) {
4748
$puppeteerPageProxyHandler(req);
4849
} else {
49-
for (const listener of page.listeners("request")) {
50-
if (listener.name === "$puppeteerPageProxyHandler") {
51-
page.removeListener("request", listener);
52-
}
53-
}; page.on("request", $puppeteerPageProxyHandler);
50+
removeRequestListener();
51+
if (proxy) {
52+
page.on("request", $puppeteerPageProxyHandler);
53+
} else {
54+
await page.setRequestInterception(false);
55+
}
5456
}
55-
}
57+
};
58+
module.exports = pageProxy;

src/lib/cdp.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
module.exports = class CDP {
2-
static async Send(ws, command) {
1+
const cdp = {
2+
async _send(ws, command) {
33
ws.send(JSON.stringify(command));
44
return new Promise(resolve => {
55
ws.on("message", function handler(msg) {
@@ -10,10 +10,10 @@ module.exports = class CDP {
1010
}
1111
});
1212
});
13-
}
14-
static Target = {
15-
attachToTarget: async (ws, targetId) => {
16-
const result = (await this.Send(ws, {
13+
},
14+
Target: {
15+
async attachToTarget(ws, targetId) {
16+
const result = (await cdp._send(ws, {
1717
id: 1,
1818
method: "Target.attachToTarget",
1919
params: {
@@ -25,10 +25,10 @@ module.exports = class CDP {
2525
return result.sessionId;
2626
}
2727
}
28-
};
29-
static Network = {
30-
getCookies: async (ws, sessionId) => {
31-
const result = (await this.Send(ws, {
28+
},
29+
Network: {
30+
async getCookies(ws, sessionId) {
31+
const result = (await cdp._send(ws, {
3232
sessionId,
3333
id: 2,
3434
method: "Network.getCookies"
@@ -37,5 +37,6 @@ module.exports = class CDP {
3737
return result.cookies;
3838
}
3939
}
40-
};
41-
}
40+
}
41+
};
42+
module.exports = cdp;

src/lib/cookies.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ const WebSocket = require("ws");
22
const {CookieJar} = require("tough-cookie");
33
const {Target, Network} = require("./cdp");
44

5-
module.exports = class Cookies {
6-
static async get(endpoint, targetId) {
5+
const cookies = {
6+
async get(endpoint, targetId) {
77
const ws = new WebSocket(endpoint, {
88
perMessageDeflate: false,
99
maxPayload: 180 * 4096 // 0.73728Mb
@@ -12,8 +12,8 @@ module.exports = class Cookies {
1212
/* Attach to target then get cookies */
1313
const sessionId = await Target.attachToTarget(ws, targetId);
1414
return await Network.getCookies(ws, sessionId);
15-
};
16-
static store(cookies) {
15+
},
16+
store(cookies) {
1717
if (!cookies) {
1818
return;
1919
}
@@ -32,9 +32,10 @@ module.exports = class Cookies {
3232
httpOnly: cookie.httpOnly,
3333
sameSite: cookie.sameSite,
3434
creation: new Date().toISOString(),
35-
hostOnly: !cookie.domain.match(/^\./)
35+
hostOnly: !(/^\./).test(cookie.domain)
3636
};
3737
})
3838
});
39-
};
40-
};
39+
}
40+
};
41+
module.exports = cookies;

src/lib/request.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ const HttpProxyAgent = require("http-proxy-agent");
33
const HttpsProxyAgent = require("https-proxy-agent");
44
const SocksProxyAgent = require("socks-proxy-agent");
55

6-
module.exports = class RequestCore {
7-
static setHeaders(req) {
6+
const request = {
7+
setHeaders(req) {
88
const headers = {
99
...req.headers(),
1010
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
@@ -20,8 +20,8 @@ module.exports = class RequestCore {
2020
headers["sec-fetch-site"] = "same-origin";
2121
}
2222
return headers;
23-
}
24-
static setAgent(url, proxy) {
23+
},
24+
setAgent(url, proxy) {
2525
if (proxy.startsWith("socks")) {
2626
return new SocksProxyAgent(proxy);
2727
}
@@ -30,17 +30,18 @@ module.exports = class RequestCore {
3030
} else {
3131
return new HttpProxyAgent(proxy);
3232
}
33-
}
34-
static async request(url, options) {
33+
},
34+
async request(url, options) {
3535
try {
3636
const res = await got(url, options);
3737
return {
3838
status: res.statusCode,
3939
headers: res.headers,
4040
body: res.body,
4141
};
42-
} catch (error) {
42+
} catch(error) {
4343
throw new Error(error);
4444
}
4545
}
46-
}
46+
};
47+
module.exports = request;

src/util/proxy-validator.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/util/type-enforcer.js

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)