Skip to content

Commit

Permalink
fix: Puppeteer 1.7.0 support (#100)
Browse files Browse the repository at this point in the history
* fix: Puppeteer 1.7.0 support

* chore: Refactor to remove mutating puppeteer request object
  • Loading branch information
jasonmit authored Aug 19, 2018
1 parent 24c15bd commit e208b38
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 22 deletions.
2 changes: 1 addition & 1 deletion packages/@pollyjs/adapter-puppeteer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
51 changes: 33 additions & 18 deletions packages/@pollyjs/adapter-puppeteer/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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()
};
}

Expand All @@ -28,6 +37,7 @@ export default class PuppeteerAdapter extends Adapter {
'A puppeteer page instance is required.',
!!(page && typeof page === 'object')
);

this.attachToPageEvents(page);
}

Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit e208b38

Please sign in to comment.