puppeteer-page-proxy
Advanced tools
Comparing version 1.2.2 to 1.2.3
# Change log | ||
### [1.2.3] - 2020-02-14 | ||
#### Changes | ||
- Added ability to remove page-wide proxy | ||
- Changed static classes to object literals for compability with Node.js **10.16.x** ([#6](https://github.com/Cuadrix/puppeteer-page-proxy/issues/6)) | ||
- Removed `src\util\` folder along with proxy-validator | ||
### [1.2.2] - 2020-02-09 | ||
@@ -3,0 +8,0 @@ #### Patches |
{ | ||
"name": "puppeteer-page-proxy", | ||
"description": "Additional Node.js module to use with 'puppeteer' for setting proxies per page basis.", | ||
"version": "1.2.2", | ||
"version": "1.2.3", | ||
"author": "Cuadrix <cuadrix12000@gmail.com> (https://github.com/Cuadrix)", | ||
@@ -25,8 +25,9 @@ "homepage": "https://github.com/Cuadrix/puppeteer-page-proxy", | ||
"dependencies": { | ||
"got": "^10.5.2", | ||
"got": "^10.5.5", | ||
"http-proxy-agent": "^4.0.1", | ||
"https-proxy-agent": "^5.0.0", | ||
"socks-proxy-agent": "^5.0.0", | ||
"tough-cookie": "^3.0.1" | ||
"tough-cookie": "^3.0.1", | ||
"type-dragoon": "^1.0.0" | ||
} | ||
} |
@@ -59,2 +59,6 @@ # puppeteer-page-proxy <img src="https://i.ibb.co/kQrN9QJ/puppeteer-page-proxy-logo.png" align="right" width=150 height=150/> | ||
``` | ||
To remove a proxy set this way, simply pass a falsy value (e.g `null`) instead of the proxy; | ||
```js | ||
await useProxy(page, null); | ||
``` | ||
@@ -61,0 +65,0 @@ #### Proxy per request: |
@@ -1,8 +0,6 @@ | ||
const enforceTypes = require("../util/type-enforcer"); | ||
const enforceTypes = require("type-dragoon"); | ||
module.exports = async (page, lookupService = "https://api.ipify.org?format=json", isJSON = true, timeout = 30000) => { | ||
const lookup = async (page, lookupService = "https://api.ipify.org?format=json", isJSON = true, timeout = 30000) => { | ||
/**/ | ||
enforceTypes( | ||
[page, "object"], [lookupService, "string"], [isJSON, "boolean"], [timeout, "number"] | ||
); | ||
enforceTypes({object: page}, {string: lookupService}, {boolean: isJSON}, {number: timeout}); | ||
/**/ | ||
@@ -44,2 +42,3 @@ const XMLHttpRequest = async () => { | ||
} | ||
} | ||
}; | ||
module.exports = lookup; |
const {setHeaders, setAgent, request} = require("../lib/request"); | ||
const cookies = require("../lib/cookies"); | ||
const enforceTypes = require("../util/type-enforcer"); | ||
const validateProxy = require("../util/proxy-validator"); | ||
const enforceTypes = require("type-dragoon"); | ||
module.exports = async (param, proxy) => { | ||
const pageProxy = async (param, proxy) => { | ||
/**/ | ||
enforceTypes( | ||
[param, "object"], [proxy, "string"] | ||
); validateProxy(proxy); | ||
enforceTypes({object: param}); | ||
/**/ | ||
@@ -15,13 +12,9 @@ let page, req; | ||
req = param; | ||
} | ||
else if (param.constructor.name === "Page") { | ||
} else if (param.constructor.name === "Page") { | ||
page = param; | ||
await page.setRequestInterception(true); | ||
} else { | ||
throw new Error("@arg1: Not valid 'Page' or 'Request' object"); | ||
throw new Error("Not valid `Page` or `Request` object"); | ||
} | ||
const $puppeteerPageProxyHandler = async req => { | ||
if (req._interceptionHandled || !req._allowInterception) { | ||
return; | ||
} | ||
const cookieJar = cookies.store(await cookies.get( | ||
@@ -47,11 +40,21 @@ req._client._connection._url, req._frame._id | ||
}; | ||
const removeRequestListener = () => { | ||
const listeners = page.listeners("request"); | ||
for (let i = 0; i < listeners.length; i++) { | ||
if (listeners[i].name === "$puppeteerPageProxyHandler") { | ||
page.removeListener("request", listeners[i]); | ||
} | ||
} | ||
}; | ||
if (req) { | ||
$puppeteerPageProxyHandler(req); | ||
} else { | ||
for (const listener of page.listeners("request")) { | ||
if (listener.name === "$puppeteerPageProxyHandler") { | ||
page.removeListener("request", listener); | ||
} | ||
}; page.on("request", $puppeteerPageProxyHandler); | ||
removeRequestListener(); | ||
if (proxy) { | ||
page.on("request", $puppeteerPageProxyHandler); | ||
} else { | ||
await page.setRequestInterception(false); | ||
} | ||
} | ||
} | ||
}; | ||
module.exports = pageProxy; |
@@ -1,3 +0,3 @@ | ||
module.exports = class CDP { | ||
static async Send(ws, command) { | ||
const cdp = { | ||
async _send(ws, command) { | ||
ws.send(JSON.stringify(command)); | ||
@@ -13,6 +13,6 @@ return new Promise(resolve => { | ||
}); | ||
} | ||
static Target = { | ||
attachToTarget: async (ws, targetId) => { | ||
const result = (await this.Send(ws, { | ||
}, | ||
Target: { | ||
async attachToTarget(ws, targetId) { | ||
const result = (await cdp._send(ws, { | ||
id: 1, | ||
@@ -29,6 +29,6 @@ method: "Target.attachToTarget", | ||
} | ||
}; | ||
static Network = { | ||
getCookies: async (ws, sessionId) => { | ||
const result = (await this.Send(ws, { | ||
}, | ||
Network: { | ||
async getCookies(ws, sessionId) { | ||
const result = (await cdp._send(ws, { | ||
sessionId, | ||
@@ -42,3 +42,4 @@ id: 2, | ||
} | ||
}; | ||
} | ||
} | ||
}; | ||
module.exports = cdp; |
@@ -5,4 +5,4 @@ const WebSocket = require("ws"); | ||
module.exports = class Cookies { | ||
static async get(endpoint, targetId) { | ||
const cookies = { | ||
async get(endpoint, targetId) { | ||
const ws = new WebSocket(endpoint, { | ||
@@ -16,4 +16,4 @@ perMessageDeflate: false, | ||
return await Network.getCookies(ws, sessionId); | ||
}; | ||
static store(cookies) { | ||
}, | ||
store(cookies) { | ||
if (!cookies) { | ||
@@ -37,7 +37,8 @@ return; | ||
creation: new Date().toISOString(), | ||
hostOnly: !cookie.domain.match(/^\./) | ||
hostOnly: !(/^\./).test(cookie.domain) | ||
}; | ||
}) | ||
}); | ||
}; | ||
}; | ||
} | ||
}; | ||
module.exports = cookies; |
@@ -6,4 +6,4 @@ const got = require("got"); | ||
module.exports = class RequestCore { | ||
static setHeaders(req) { | ||
const request = { | ||
setHeaders(req) { | ||
const headers = { | ||
@@ -24,4 +24,4 @@ ...req.headers(), | ||
return headers; | ||
} | ||
static setAgent(url, proxy) { | ||
}, | ||
setAgent(url, proxy) { | ||
if (proxy.startsWith("socks")) { | ||
@@ -35,4 +35,4 @@ return new SocksProxyAgent(proxy); | ||
} | ||
} | ||
static async request(url, options) { | ||
}, | ||
async request(url, options) { | ||
try { | ||
@@ -45,6 +45,7 @@ const res = await got(url, options); | ||
}; | ||
} catch (error) { | ||
} catch(error) { | ||
throw new Error(error); | ||
} | ||
} | ||
} | ||
}; | ||
module.exports = request; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
142
17709
6
10
253
+ Addedtype-dragoon@^1.0.0
+ Addedtype-dragoon@1.0.5(transitive)
Updatedgot@^10.5.5