puppeteer-interceptor
Advanced tools
Comparing version 2.1.0 to 2.2.0
@@ -17,4 +17,12 @@ import Protocol from 'devtools-protocol'; | ||
} | ||
interface ResponseOptions { | ||
responseHeaders?: Protocol.Fetch.HeaderEntry[]; | ||
binaryResponseHeaders?: string; | ||
body?: string; | ||
responsePhrase?: string; | ||
encodedBody?: string; | ||
} | ||
interface ControlCallbacks { | ||
abort: (msg: Protocol.Network.ErrorReason) => void; | ||
fulfill: (responseCode: number, responseOptions?: ResponseOptions) => void; | ||
} | ||
@@ -21,0 +29,0 @@ interface InterceptedResponse { |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
}; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
} | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -16,9 +9,9 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.intercept = exports.InterceptionHandler = void 0; | ||
const atob_1 = __importDefault(require("atob")); | ||
const btoa_1 = __importDefault(require("btoa")); | ||
const debug_1 = __importDefault(require("debug")); | ||
const lodash_pick_1 = __importDefault(require("lodash.pick")); | ||
const debug = debug_1.default('puppeteer-interceptor'); | ||
__exportStar(require("./types"), exports); | ||
__exportStar(require("./request-patterns"), exports); | ||
__export(require("./types")); | ||
__export(require("./request-patterns")); | ||
class InterceptionHandler { | ||
@@ -53,2 +46,3 @@ constructor(page, patterns = [], eventHandlers = {}) { | ||
let shouldContinue = true; | ||
let fulfill = undefined; | ||
const control = { | ||
@@ -59,2 +53,20 @@ abort: (msg) => { | ||
}, | ||
fulfill: (responseCode, responseOptions) => { | ||
const fulfillOptions = { | ||
requestId, | ||
responseCode, | ||
}; | ||
if (responseOptions) { | ||
const keys = ['body', 'binaryResponseHeaders', 'responseHeaders', 'responsePhrase']; | ||
Object.assign(fulfillOptions, lodash_pick_1.default(responseOptions, keys)); | ||
if (fulfillOptions.body) | ||
fulfillOptions.body = btoa_1.default(fulfillOptions.body); | ||
if (responseOptions.encodedBody) | ||
fulfillOptions.body = responseOptions.encodedBody; | ||
} | ||
fulfill = async () => { | ||
debug(`Fulfilling request ${requestId} with responseCode "${responseCode}"`); | ||
await client.send('Fetch.fulfillRequest', fulfillOptions); | ||
}; | ||
}, | ||
}; | ||
@@ -67,2 +79,6 @@ await this.eventHandlers.onInterception(event, control); | ||
} | ||
else if (fulfill) { | ||
await fulfill(); | ||
return; | ||
} | ||
} | ||
@@ -69,0 +85,0 @@ let newResponse = null; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.patterns = void 0; | ||
exports.patterns = { | ||
@@ -5,0 +4,0 @@ Document: patternGenerator('Document'), |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ERROR_REASON = void 0; | ||
// https://chromedevtools.github.io/devtools-protocol/tot/Network#type-ErrorReason | ||
@@ -5,0 +4,0 @@ var ERROR_REASON; |
@@ -134,3 +134,14 @@ "use strict"; | ||
}); | ||
it('should allow fulfilling requests', async function () { | ||
const randomString = Math.random().toString(16); | ||
src_1.intercept(page, src_1.patterns.Document('*'), { | ||
onInterception: (event, { fulfill }) => { | ||
fulfill(200, { body: randomString }); | ||
}, | ||
}); | ||
await page.goto(server.url('index.html'), {}); | ||
const content = await page.content(); | ||
assert_1.default(content.match(randomString), `content (${content}) should match ${randomString}`); | ||
}); | ||
}); | ||
//# sourceMappingURL=index.test.js.map |
@@ -6,3 +6,2 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.stop = exports.start = void 0; | ||
const serve_handler_1 = __importDefault(require("serve-handler")); | ||
@@ -9,0 +8,0 @@ const http_1 = require("http"); |
{ | ||
"name": "puppeteer-interceptor", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "Makes intercepting and modifying traffic from Puppeteer easier", | ||
@@ -31,23 +31,23 @@ "main": "dist/src/index.js", | ||
"dependencies": { | ||
"atob": "^2.1.2", | ||
"btoa": "^1.2.1", | ||
"debug": "^4.1.1" | ||
"atob": "2.1.2", | ||
"btoa": "1.2.1", | ||
"debug": "4.1.1", | ||
"lodash.pick": "^4.4.0" | ||
}, | ||
"devDependencies": { | ||
"@jsoverson/test-server": "^1.1.1", | ||
"@types/atob": "^2.1.2", | ||
"@types/btoa": "^1.2.3", | ||
"@types/debug": "^4.1.5", | ||
"@types/mocha": "^7.0.2", | ||
"@types/node": "^13.13.2", | ||
"@types/serve-handler": "^6.1.0", | ||
"@jsoverson/test-server": "1.1.1", | ||
"@types/atob": "2.1.2", | ||
"@types/btoa": "1.2.3", | ||
"@types/debug": "4.1.5", | ||
"@types/lodash.pick": "^4.4.6", | ||
"@types/mocha": "7.0.2", | ||
"@types/node": "13.13.2", | ||
"@types/serve-handler": "6.1.0", | ||
"devtools-protocol": "0.0.761827", | ||
"mocha": "^7.1.1", | ||
"prettier": "^2.0.5", | ||
"mocha": "7.1.1", | ||
"prettier": "2.0.5", | ||
"puppeteer": "5.2.1", | ||
"serve": "^11.3.0", | ||
"serve-handler": "^6.1.2", | ||
"ts-node": "^8.9.0", | ||
"typescript": "^3.8.3" | ||
"ts-node": "8.9.0", | ||
"typescript": "3.8.3" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
35831
14
417
4
+ Addedlodash.pick@^4.4.0
+ Addeddebug@4.1.1(transitive)
+ Addedlodash.pick@4.4.0(transitive)
+ Addedms@2.1.3(transitive)
- Removeddebug@4.3.4(transitive)
- Removedms@2.1.2(transitive)
Updatedatob@2.1.2
Updatedbtoa@1.2.1
Updateddebug@4.1.1