Skip to content

Commit

Permalink
Merge pull request #1 from Tabueeee/v-1.0.7
Browse files Browse the repository at this point in the history
V 1.0.7 - fixed error caused by puppeteer api change; added travis build
  • Loading branch information
Tabueeee authored Apr 28, 2018
2 parents 7206cdd + f88ac5f commit 5b29437
Show file tree
Hide file tree
Showing 13 changed files with 173 additions and 40 deletions.
5 changes: 0 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,6 @@ build/Release
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/globals/
typings/modules/
typings/index.d.ts

# Optional npm cache directory
.npm

Expand Down
111 changes: 111 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
language: node_js

node_js:
- stable

install:
- npm install

script:
- npm test
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# puppeteer-request-spy
[![Build Status](https://travis-ci.org/Tabueeee/puppeteer-request-spy.svg?branch=master)](https://travis-ci.org/Tabueeee/puppeteer-request-spy)

> With puppeteer-request-spy you can easily watch or block requests from puppeteer matching patterns.
Expand Down
8 changes: 8 additions & 0 deletions custom-typings/dom/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
declare interface Element {
[key: string]: any;
}

declare interface NodeListOf<T> {
[key: string]: any;
}

File renamed without changes.
14 changes: 9 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "puppeteer-request-spy",
"version": "1.0.6",
"version": "1.0.7",
"description": "watch or block requests from puppeteer matching patterns",
"main": "build/src/index.js",
"scripts": {
Expand All @@ -9,8 +9,8 @@
"prebuild-dev": "tslint --project tsconfig-lint.json",
"build-dev": "node_modules/.bin/tsc -p tsconfig.json --sourcemap",
"pretest": "npm run build",
"test": "node_modules/.bin/mocha --require source-map-support/register build/test/**/*.spec.js",
"test-silent": "node_modules/.bin/mocha --require source-map-support/register build/test/**/*.spec.js > test-ts.log",
"test": "node_modules/.bin/mocha --timeout 10000 --require source-map-support/register build/test/**/*.dev.spec.js",
"test-silent": "node_modules/.bin/mocha --timeout 10000 --require source-map-support/register build/test/**/*.spec.js > test-ts.log",
"pretest-coverage": "npm run build-dev",
"test-coverage": "node_modules/.bin/nyc --all --reporter=html npm run test-silent"
},
Expand Down Expand Up @@ -44,11 +44,15 @@
"author": "Tobias Nießen",
"license": "MIT",
"devDependencies": {
"@types/puppeteer": "^0.13.7",
"@types/minimatch": "^3.0.3",
"@types/mocha": "^5.2.0",
"@types/node": "^10.0.0",
"@types/puppeteer": "^1.2.3",
"@types/sinon": "^4.3.1",
"minimatch": "^3.0.4",
"mocha": "^4.0.1",
"nyc": "^11.3.0",
"puppeteer": "^0.13.0",
"puppeteer": "^1.3.0",
"sinon": "^4.1.3",
"source-map-support": "^0.5.0",
"static-server": "^3.0.0",
Expand Down
28 changes: 18 additions & 10 deletions src/RequestInterceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,32 @@ export class RequestInterceptor {

public async intercept(interceptedUrl: Request): Promise<void> {
let aborted: boolean = false;
let url: string;

if (typeof interceptedUrl.url === 'string') {
// @ts-ignore: support old puppeteer version
url = interceptedUrl.url;
} else {
url = interceptedUrl.url();
}

for (let spy of this.spies) {
for (let pattern of spy.getPatterns()) {
if (this.matcher(interceptedUrl.url, pattern)) {
spy.addMatchedUrl(interceptedUrl.url);
if (this.matcher(url, pattern)) {
spy.addMatchedUrl(url);
}
}
}

for (let urlToBlock of this.urlsToBlock) {
if (this.matcher(interceptedUrl.url, urlToBlock)) {
if (this.matcher(url, urlToBlock)) {
aborted = true;
await this.blockUrl(interceptedUrl);
await this.blockUrl(interceptedUrl, url);
}
}

if (aborted === false) {
await this.acceptUrl(interceptedUrl);
await this.acceptUrl(interceptedUrl, url);
}
}

Expand All @@ -62,25 +70,25 @@ export class RequestInterceptor {
this.urlsToBlock = urlsToBlock;
}

private async blockUrl(interceptedUrl: Request): Promise<void> {
private async blockUrl(interceptedUrl: Request, url: string): Promise<void> {
try {
await interceptedUrl.abort();
this.logger.log(`aborted: ${interceptedUrl.url}`);
this.logger.log(`aborted: ${url}`);
} catch (error) {
this.logger.log((<Error> error).toString());
}
}

private async acceptUrl(interceptedUrl: Request): Promise<void> {
private async acceptUrl(interceptedUrl: Request, url: string): Promise<void> {
if (this.urlsToBlock.length > 0) {
try {
await interceptedUrl.continue();
this.logger.log(`loaded: ${interceptedUrl.url}`);
this.logger.log(`loaded: ${url}`);
} catch (error) {
this.logger.log((<Error> error).toString());
}
} else {
this.logger.log(`loaded: ${interceptedUrl.url}`);
this.logger.log(`loaded: ${url}`);
}
}
}
2 changes: 1 addition & 1 deletion test/common/Browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ export class BrowserLauncher {
}

export const browserLauncher: BrowserLauncher = new BrowserLauncher();

6 changes: 3 additions & 3 deletions test/integration/puppeteer-request-spy.deploy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ describe('puppeteer-request-spy: integration', function (): void {
it('requestInterceptor blocks all matched requests', async function (): Promise<void> {
let logs: Array<string> = [];
let expectedLogs: Array<string> = [
'AssertionError: Request Interception is not enabled!',
'AssertionError: Request Interception is not enabled!',
'AssertionError: Request Interception is not enabled!'
'AssertionError [ERR_ASSERTION]: Request Interception is not enabled!',
'AssertionError [ERR_ASSERTION]: Request Interception is not enabled!',
'AssertionError [ERR_ASSERTION]: Request Interception is not enabled!'
];

let requestInterceptorWithLoggerFake: RequestInterceptor = new RequestInterceptor(minimatch, getLoggerFake(logs));
Expand Down
13 changes: 7 additions & 6 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"removeComments": true,
"preserveConstEnums": false,
"target": "ES2015",
"lib": ["es6"],
"lib": [
"es6"
],
"moduleResolution": "node",
"experimentalDecorators": true,
"noEmitOnError": true,
Expand All @@ -19,18 +21,17 @@
"noUnusedLocals": true,
"noUnusedParameters": true,
"strict": true,
"strictNullChecks": true,
"strictNullChecks": false,
"stripInternal": true,
"suppressImplicitAnyIndexErrors": true,
"outDir": "./build",
"typeRoots": [
"typings/globals",
"typings/modules",
"typings/custom"
"custom-typings",
"./node_modules/@types"
]
},
"include": [
"./**/*.ts",
"./typings/index"
"./custom-typings/index"
]
}
10 changes: 0 additions & 10 deletions typings.json

This file was deleted.

0 comments on commit 5b29437

Please sign in to comment.