From e208b386aa008d58486b6b7874008403e2e18380 Mon Sep 17 00:00:00 2001 From: Jason Mitchell Date: Sun, 19 Aug 2018 03:05:00 -0700 Subject: [PATCH] fix: Puppeteer 1.7.0 support (#100) * fix: Puppeteer 1.7.0 support * chore: Refactor to remove mutating puppeteer request object --- .../@pollyjs/adapter-puppeteer/package.json | 2 +- .../@pollyjs/adapter-puppeteer/src/index.js | 51 ++++++++++++------- yarn.lock | 6 +-- 3 files changed, 37 insertions(+), 22 deletions(-) diff --git a/packages/@pollyjs/adapter-puppeteer/package.json b/packages/@pollyjs/adapter-puppeteer/package.json index d82a89b9..7a0a95ee 100644 --- a/packages/@pollyjs/adapter-puppeteer/package.json +++ b/packages/@pollyjs/adapter-puppeteer/package.json @@ -55,7 +55,7 @@ "node-fetch": "^2.2.0", "npm-run-all": "^4.1.3", "prettier": "^1.14.2", - "puppeteer": "1.6.2", + "puppeteer": "1.7.0", "rimraf": "^2.6.2", "rollup": "^0.64.1" } diff --git a/packages/@pollyjs/adapter-puppeteer/src/index.js b/packages/@pollyjs/adapter-puppeteer/src/index.js index 0f8dd55e..9a1bb43b 100644 --- a/packages/@pollyjs/adapter-puppeteer/src/index.js +++ b/packages/@pollyjs/adapter-puppeteer/src/index.js @@ -2,8 +2,6 @@ import Adapter from '@pollyjs/adapter'; import { URL } from '@pollyjs/utils'; const LISTENERS = Symbol(); -const POLLY_REQUEST = Symbol(); -const PASSTHROUGH_PROMISE = Symbol(); const PASSTHROUGH_PROMISES = Symbol(); const PASSTHROUGH_REQ_ID_QP = 'pollyjs_passthrough_req_id'; @@ -15,7 +13,18 @@ export default class PuppeteerAdapter extends Adapter { get defaultOptions() { return { page: null, - requestResourceTypes: ['xhr', 'fetch'] + + /* NOTE: `"other" is needed as of puppeteer 1.7.0 to capture the passthrough request */ + requestResourceTypes: ['xhr', 'fetch', 'other'] + }; + } + + constructor() { + super(...arguments); + + this._requestsMapping = { + passthroughs: new WeakMap(), + pollyRequests: new WeakMap() }; } @@ -28,6 +37,7 @@ export default class PuppeteerAdapter extends Adapter { 'A puppeteer page instance is required.', !!(page && typeof page === 'object') ); + this.attachToPageEvents(page); } @@ -61,8 +71,11 @@ export default class PuppeteerAdapter extends Adapter { // If this is a polly passthrough request // Get the associated promise object for the request id and set it // on the request - request[PASSTHROUGH_PROMISE] = this[PASSTHROUGH_PROMISES].get( - parsedUrl.query[PASSTHROUGH_REQ_ID_QP] + this._requestsMapping.passthroughs.set( + request, + this[PASSTHROUGH_PROMISES].get( + parsedUrl.query[PASSTHROUGH_REQ_ID_QP] + ) ); // Delete the query param to remove any pollyjs footprint @@ -88,32 +101,34 @@ export default class PuppeteerAdapter extends Adapter { }, requestfinished: request => { const response = request.response(); + const { passthroughs, pollyRequests } = this._requestsMapping; // Resolve the passthrough promise with the response if it exists - if (request[PASSTHROUGH_PROMISE]) { - request[PASSTHROUGH_PROMISE].resolve(response); - delete request[PASSTHROUGH_PROMISE]; + if (passthroughs.has(request)) { + passthroughs.get(request).resolve(response); + passthroughs.delete(request); } // Resolve the deferred pollyRequest promise if it exists - if (request[POLLY_REQUEST]) { - request[POLLY_REQUEST].promise.resolve(response); - delete request[POLLY_REQUEST]; + if (pollyRequests.has(request)) { + pollyRequests.get(request).promise.resolve(response); + pollyRequests.delete(request); } }, requestfailed: request => { const error = request.failure(); + const { passthroughs, pollyRequests } = this._requestsMapping; // Reject the passthrough promise with the error object if it exists - if (request[PASSTHROUGH_PROMISE]) { - request[PASSTHROUGH_PROMISE].reject(error); - delete request[PASSTHROUGH_PROMISE]; + if (passthroughs.has(request)) { + passthroughs.get(request).reject(error); + passthroughs.delete(request); } // Reject the deferred pollyRequest promise with the error object if it exists - if (request[POLLY_REQUEST]) { - request[POLLY_REQUEST].promise.reject(error); - delete request[POLLY_REQUEST]; + if (pollyRequests.has(request)) { + pollyRequests.get(request).promise.reject(error); + pollyRequests.delete(request); } }, close: () => this[LISTENERS].delete(page) @@ -129,7 +144,7 @@ export default class PuppeteerAdapter extends Adapter { Create an access point to the `pollyRequest` so it can be accessed from the emitted page events */ - request[POLLY_REQUEST] = pollyRequest; + this._requestsMapping.pollyRequests.set(request, pollyRequest); } /** diff --git a/yarn.lock b/yarn.lock index 5daa319d..9099e304 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8316,9 +8316,9 @@ punycode@^2.1.0: version "2.1.1" resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec" -puppeteer@1.6.2: - version "1.6.2" - resolved "https://registry.yarnpkg.com/puppeteer/-/puppeteer-1.6.2.tgz#e3c31f8184e8fb8b36e4263921bcaaab7a99b3bf" +puppeteer@1.7.0: + version "1.7.0" + resolved "https://registry.yarnpkg.com/puppeteer/-/puppeteer-1.7.0.tgz#edcba2300a50847202c0f19fd15e7a96171ff3bd" dependencies: debug "^3.1.0" extract-zip "^1.6.6"