diff --git a/.gitignore b/.gitignore index 3eb0123..f99ce71 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..6713544 --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,111 @@ + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..79ee123 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..fbc2c5d --- /dev/null +++ b/.travis.yml @@ -0,0 +1,10 @@ +language: node_js + +node_js: + - stable + +install: + - npm install + +script: + - npm test diff --git a/README.md b/README.md index 75bd1fc..919aee3 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/custom-typings/dom/index.d.ts b/custom-typings/dom/index.d.ts new file mode 100644 index 0000000..2cbda0b --- /dev/null +++ b/custom-typings/dom/index.d.ts @@ -0,0 +1,8 @@ +declare interface Element { + [key: string]: any; +} + +declare interface NodeListOf { + [key: string]: any; +} + diff --git a/typings/custom/static-server/index.d.ts b/custom-typings/static-server/index.d.ts similarity index 100% rename from typings/custom/static-server/index.d.ts rename to custom-typings/static-server/index.d.ts diff --git a/package.json b/package.json index 5ad650c..2c9cd02 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -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" }, @@ -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", diff --git a/src/RequestInterceptor.ts b/src/RequestInterceptor.ts index 7a1eda8..ae7b909 100644 --- a/src/RequestInterceptor.ts +++ b/src/RequestInterceptor.ts @@ -21,24 +21,32 @@ export class RequestInterceptor { public async intercept(interceptedUrl: Request): Promise { 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); } } @@ -62,25 +70,25 @@ export class RequestInterceptor { this.urlsToBlock = urlsToBlock; } - private async blockUrl(interceptedUrl: Request): Promise { + private async blockUrl(interceptedUrl: Request, url: string): Promise { try { await interceptedUrl.abort(); - this.logger.log(`aborted: ${interceptedUrl.url}`); + this.logger.log(`aborted: ${url}`); } catch (error) { this.logger.log(( error).toString()); } } - private async acceptUrl(interceptedUrl: Request): Promise { + private async acceptUrl(interceptedUrl: Request, url: string): Promise { 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).toString()); } } else { - this.logger.log(`loaded: ${interceptedUrl.url}`); + this.logger.log(`loaded: ${url}`); } } } diff --git a/test/common/Browser.ts b/test/common/Browser.ts index aa648f4..3eaf6c4 100644 --- a/test/common/Browser.ts +++ b/test/common/Browser.ts @@ -21,4 +21,4 @@ export class BrowserLauncher { } export const browserLauncher: BrowserLauncher = new BrowserLauncher(); - + \ No newline at end of file diff --git a/test/integration/puppeteer-request-spy.deploy.spec.ts b/test/integration/puppeteer-request-spy.deploy.spec.ts index 3c2b772..ef2b775 100644 --- a/test/integration/puppeteer-request-spy.deploy.spec.ts +++ b/test/integration/puppeteer-request-spy.deploy.spec.ts @@ -106,9 +106,9 @@ describe('puppeteer-request-spy: integration', function (): void { it('requestInterceptor blocks all matched requests', async function (): Promise { let logs: Array = []; let expectedLogs: Array = [ - '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)); diff --git a/tsconfig.json b/tsconfig.json index 03d3c0e..beed5d8 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,7 +6,9 @@ "removeComments": true, "preserveConstEnums": false, "target": "ES2015", - "lib": ["es6"], + "lib": [ + "es6" + ], "moduleResolution": "node", "experimentalDecorators": true, "noEmitOnError": true, @@ -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" ] } diff --git a/typings.json b/typings.json deleted file mode 100644 index ae45aeb..0000000 --- a/typings.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "globalDevDependencies": { - "mocha": "registry:dt/mocha#2.2.5+20170311011848", - "node": "registry:dt/node#7.0.0+20170322231424", - "sinon": "registry:dt/sinon#1.16.1+20161208163514" - }, - "devDependencies": { - "minimatch": "registry:npm/minimatch#3.0.0+20170119000329" - } -}