From 4555940d0bc17b7fd9327dd0164c382a3dbf1858 Mon Sep 17 00:00:00 2001 From: Niklas von Hertzen Date: Mon, 12 Jul 2021 19:01:43 +0800 Subject: [PATCH] fix: handle unhandled promise rejections (#2568) --- src/core/cache-storage.ts | 4 +- tests/reftests/iframe.html | 11 ++ tests/test.js | 4 + tests/testrunner.ts | 5 + www/package-lock.json | 200 +++++++++++++++++++++++++++++-------- www/package.json | 2 +- 6 files changed, 181 insertions(+), 45 deletions(-) diff --git a/src/core/cache-storage.ts b/src/core/cache-storage.ts index 6231d4f..aad1fea 100644 --- a/src/core/cache-storage.ts +++ b/src/core/cache-storage.ts @@ -87,7 +87,9 @@ export class Cache { } if (isBlobImage(src) || isRenderable(src)) { - this._cache[src] = this.loadImage(src); + (this._cache[src] = this.loadImage(src)).catch(() => { + // prevent unhandled rejection + }); return result; } diff --git a/tests/reftests/iframe.html b/tests/reftests/iframe.html index 1aa7f03..652a670 100644 --- a/tests/reftests/iframe.html +++ b/tests/reftests/iframe.html @@ -2,10 +2,21 @@ iframe test +
Parent document content
+ diff --git a/tests/test.js b/tests/test.js index 946affe..6e58cb8 100644 --- a/tests/test.js +++ b/tests/test.js @@ -24,6 +24,10 @@ var REFTEST = window.location.search.indexOf('reftest') !== -1; ]) .forEach(appendScript); + window.addEventListener("unhandledrejection", function(event) { + console.info('UNHANDLED PROMISE REJECTION:', event); + }); + window.onload = function() { (function($) { $.fn.html2canvas = function(options) { diff --git a/tests/testrunner.ts b/tests/testrunner.ts index beefec2..15e86dd 100644 --- a/tests/testrunner.ts +++ b/tests/testrunner.ts @@ -86,6 +86,11 @@ testList throw new Error('Window not found for iframe'); } + contentWindow.addEventListener('unhandledrejection', (event) => { + console.error(event.reason); + throw new Error(`unhandledrejection: ${JSON.stringify(event.reason)}`); + }); + const canvas: HTMLCanvasElement = await contentWindow // @ts-ignore .html2canvas(contentWindow.forceElement || contentWindow.document.documentElement, { diff --git a/www/package-lock.json b/www/package-lock.json index 21eab0c..6494b2d 100644 --- a/www/package-lock.json +++ b/www/package-lock.json @@ -23,7 +23,7 @@ "gatsby-transformer-remark": "^2.3.8", "glamor": "^2.20.40", "gzip-size": "^5.0.0", - "html2canvas": "^1.0.0-alpha.12", + "html2canvas": "file:../", "mkdirp": "^0.5.1", "prismjs": "^1.16.0", "react": "^16.8.6", @@ -34,6 +34,88 @@ "typography-theme-github": "^0.16.19" } }, + "..": { + "version": "1.1.0", + "license": "MIT", + "dependencies": { + "css-line-break": "1.1.1" + }, + "devDependencies": { + "@babel/cli": "^7.4.3", + "@babel/core": "^7.4.3", + "@babel/preset-env": "^7.4.3", + "@babel/preset-flow": "^7.0.0", + "@rollup/plugin-commonjs": "^19.0.0", + "@rollup/plugin-json": "^4.1.0", + "@rollup/plugin-node-resolve": "^13.0.0", + "@rollup/plugin-typescript": "^8.2.1", + "@types/chai": "^4.1.7", + "@types/express": "^4.17.13", + "@types/glob": "^7.1.1", + "@types/jest": "^26.0.24", + "@types/karma": "^6.3.0", + "@types/mocha": "^8.2.3", + "@types/node": "^16.3.1", + "@types/platform": "^1.3.4", + "@types/promise-polyfill": "^6.0.3", + "@typescript-eslint/eslint-plugin": "^4.28.2", + "@typescript-eslint/parser": "^4.28.2", + "appium-ios-simulator": "^3.10.0", + "babel-eslint": "^10.0.1", + "babel-loader": "^8.0.5", + "babel-plugin-add-module-exports": "^1.0.2", + "babel-plugin-dev-expression": "^0.2.1", + "base64-arraybuffer": "0.2.0", + "body-parser": "^1.19.0", + "chai": "4.1.1", + "chromeless": "^1.5.2", + "cors": "^2.8.5", + "es6-promise": "^4.2.8", + "eslint": "^7.30.0", + "eslint-config-prettier": "^8.3.0", + "eslint-plugin-prettier": "3.4.0", + "express": "^4.17.1", + "filenamify-url": "1.0.0", + "glob": "7.1.3", + "html2canvas-proxy": "1.0.1", + "jest": "^27.0.6", + "jquery": "^3.5.1", + "js-polyfills": "^0.1.42", + "karma": "^6.3.2", + "karma-chrome-launcher": "^3.1.0", + "karma-edge-launcher": "^0.4.2", + "karma-firefox-launcher": "^2.1.0", + "karma-ie-launcher": "^1.0.0", + "karma-junit-reporter": "^2.0.1", + "karma-mocha": "^2.0.1", + "karma-safarinative-launcher": "^1.1.0", + "karma-sauce-launcher": "^2.0.2", + "mocha": "^9.0.2", + "node-simctl": "^5.3.0", + "platform": "^1.3.6", + "prettier": "^2.3.2", + "replace-in-file": "^3.0.0", + "rimraf": "^3.0.2", + "rollup": "^2.53.1", + "rollup-plugin-sourcemaps": "^0.6.3", + "serve-index": "^1.9.1", + "slash": "1.0.0", + "standard-version": "^8.0.2", + "ts-jest": "^27.0.3", + "ts-loader": "^8.3.0", + "ts-node": "^10.1.0", + "tslib": "^2.3.0", + "typescript": "^4.3.5", + "uglify-js": "^3.13.10", + "uglifyjs-webpack-plugin": "^2.2.0", + "webpack": "^4.46.0", + "webpack-cli": "^3.3.12", + "yargs": "^17.0.1" + }, + "engines": { + "node": ">=8.0.0" + } + }, "node_modules/@ardatan/aggregate-error": { "version": "0.0.6", "resolved": "https://registry.npmjs.org/@ardatan/aggregate-error/-/aggregate-error-0.0.6.tgz", @@ -3692,14 +3774,6 @@ "node": ">=0.10.0" } }, - "node_modules/base64-arraybuffer": { - "version": "0.1.5", - "resolved": "https://registry.npmjs.org/base64-arraybuffer/-/base64-arraybuffer-0.1.5.tgz", - "integrity": "sha1-c5JncZI7Whl0etZmqlzUv5xunOg=", - "engines": { - "node": ">= 0.6.0" - } - }, "node_modules/base64-js": { "version": "1.5.1", "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz", @@ -5512,14 +5586,6 @@ "isobject": "^3.0.1" } }, - "node_modules/css-line-break": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/css-line-break/-/css-line-break-1.0.1.tgz", - "integrity": "sha1-GfIGOjPpX7KDG4ZEbAuAwYivRQo=", - "dependencies": { - "base64-arraybuffer": "^0.1.5" - } - }, "node_modules/css-loader": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/css-loader/-/css-loader-1.0.1.tgz", @@ -12365,15 +12431,8 @@ "integrity": "sha512-SaGhCDPXJVNrQyKMtKy24q6IMdXg5FCPN3z+xizxw9l+oXQw5fOoaj/ERU5KqWhSYhXtW5bWthlDbTDLBhJQrA==" }, "node_modules/html2canvas": { - "version": "1.0.0-alpha.12", - "resolved": "https://registry.npmjs.org/html2canvas/-/html2canvas-1.0.0-alpha.12.tgz", - "integrity": "sha1-OxmS48mz9WBjw1/WIElPN+uohRM=", - "dependencies": { - "css-line-break": "1.0.1" - }, - "engines": { - "node": ">=4.0.0" - } + "resolved": "..", + "link": true }, "node_modules/htmlparser2": { "version": "3.10.1", @@ -26395,11 +26454,6 @@ } } }, - "base64-arraybuffer": { - "version": "0.1.5", - "resolved": "https://registry.npmjs.org/base64-arraybuffer/-/base64-arraybuffer-0.1.5.tgz", - "integrity": "sha1-c5JncZI7Whl0etZmqlzUv5xunOg=" - }, "base64-js": { "version": "1.5.1", "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz", @@ -27888,14 +27942,6 @@ "isobject": "^3.0.1" } }, - "css-line-break": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/css-line-break/-/css-line-break-1.0.1.tgz", - "integrity": "sha1-GfIGOjPpX7KDG4ZEbAuAwYivRQo=", - "requires": { - "base64-arraybuffer": "^0.1.5" - } - }, "css-loader": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/css-loader/-/css-loader-1.0.1.tgz", @@ -33223,11 +33269,79 @@ "integrity": "sha512-SaGhCDPXJVNrQyKMtKy24q6IMdXg5FCPN3z+xizxw9l+oXQw5fOoaj/ERU5KqWhSYhXtW5bWthlDbTDLBhJQrA==" }, "html2canvas": { - "version": "1.0.0-alpha.12", - "resolved": "https://registry.npmjs.org/html2canvas/-/html2canvas-1.0.0-alpha.12.tgz", - "integrity": "sha1-OxmS48mz9WBjw1/WIElPN+uohRM=", + "version": "file:..", "requires": { - "css-line-break": "1.0.1" + "@babel/cli": "^7.4.3", + "@babel/core": "^7.4.3", + "@babel/preset-env": "^7.4.3", + "@babel/preset-flow": "^7.0.0", + "@rollup/plugin-commonjs": "^19.0.0", + "@rollup/plugin-json": "^4.1.0", + "@rollup/plugin-node-resolve": "^13.0.0", + "@rollup/plugin-typescript": "^8.2.1", + "@types/chai": "^4.1.7", + "@types/express": "^4.17.13", + "@types/glob": "^7.1.1", + "@types/jest": "^26.0.24", + "@types/karma": "^6.3.0", + "@types/mocha": "^8.2.3", + "@types/node": "^16.3.1", + "@types/platform": "^1.3.4", + "@types/promise-polyfill": "^6.0.3", + "@typescript-eslint/eslint-plugin": "^4.28.2", + "@typescript-eslint/parser": "^4.28.2", + "appium-ios-simulator": "^3.10.0", + "babel-eslint": "^10.0.1", + "babel-loader": "^8.0.5", + "babel-plugin-add-module-exports": "^1.0.2", + "babel-plugin-dev-expression": "^0.2.1", + "base64-arraybuffer": "0.2.0", + "body-parser": "^1.19.0", + "chai": "4.1.1", + "chromeless": "^1.5.2", + "cors": "^2.8.5", + "css-line-break": "1.1.1", + "es6-promise": "^4.2.8", + "eslint": "^7.30.0", + "eslint-config-prettier": "^8.3.0", + "eslint-plugin-prettier": "3.4.0", + "express": "^4.17.1", + "filenamify-url": "1.0.0", + "glob": "7.1.3", + "html2canvas-proxy": "1.0.1", + "jest": "^27.0.6", + "jquery": "^3.5.1", + "js-polyfills": "^0.1.42", + "karma": "^6.3.2", + "karma-chrome-launcher": "^3.1.0", + "karma-edge-launcher": "^0.4.2", + "karma-firefox-launcher": "^2.1.0", + "karma-ie-launcher": "^1.0.0", + "karma-junit-reporter": "^2.0.1", + "karma-mocha": "^2.0.1", + "karma-safarinative-launcher": "^1.1.0", + "karma-sauce-launcher": "^2.0.2", + "mocha": "^9.0.2", + "node-simctl": "^5.3.0", + "platform": "^1.3.6", + "prettier": "^2.3.2", + "replace-in-file": "^3.0.0", + "rimraf": "^3.0.2", + "rollup": "^2.53.1", + "rollup-plugin-sourcemaps": "^0.6.3", + "serve-index": "^1.9.1", + "slash": "1.0.0", + "standard-version": "^8.0.2", + "ts-jest": "^27.0.3", + "ts-loader": "^8.3.0", + "ts-node": "^10.1.0", + "tslib": "^2.3.0", + "typescript": "^4.3.5", + "uglify-js": "^3.13.10", + "uglifyjs-webpack-plugin": "^2.2.0", + "webpack": "^4.46.0", + "webpack-cli": "^3.3.12", + "yargs": "^17.0.1" } }, "htmlparser2": { diff --git a/www/package.json b/www/package.json index 4989c37..45c4e4d 100644 --- a/www/package.json +++ b/www/package.json @@ -19,7 +19,7 @@ "gatsby-transformer-remark": "^2.3.8", "glamor": "^2.20.40", "gzip-size": "^5.0.0", - "html2canvas": "^1.0.0-alpha.12", + "html2canvas": "file:../", "mkdirp": "^0.5.1", "prismjs": "^1.16.0", "react": "^16.8.6",