From 85fb91285e2ddcfdca6618fdcdf0dac39d846abf Mon Sep 17 00:00:00 2001 From: kmsheng Date: Thu, 28 May 2020 19:47:32 +0800 Subject: [PATCH 1/3] add promise.js --- src/promise.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/promise.js diff --git a/src/promise.js b/src/promise.js new file mode 100644 index 0000000..272616c --- /dev/null +++ b/src/promise.js @@ -0,0 +1,25 @@ +/* eslint func-names:0 no-extra-parens:0 */ +import Promise from 'bluebird'; + +const es6methods = ['then', 'catch', 'constructor']; +const es6StaticMethods = ['all', 'race', 'resolve', 'reject', 'cast']; + +function isNotMethod(name) { + return !(es6methods.includes(name) || es6StaticMethods.includes(name) || name.charAt(0) === '_'); +} + +function del(obj) { + /* eslint no-param-reassign: 0 */ + return (key) => { delete obj[key]; }; +} + +function toFastProperties(obj) { + (function () {}).prototype = obj; +} + +Object.keys(Promise.prototype).filter(isNotMethod).forEach(del(Promise.prototype)); +Object.keys(Promise).filter(isNotMethod).forEach(del(Promise)); +toFastProperties(Promise); +toFastProperties(Promise.prototype); + +export default Promise; From 6b1efcb8673147474ce43a4255c7f00ad29eb627 Mon Sep 17 00:00:00 2001 From: kmsheng Date: Thu, 28 May 2020 19:50:40 +0800 Subject: [PATCH 2/3] import Promise directly from promise.js --- src/coordinator.js | 1 + src/utils/BatchManager.js | 2 ++ src/utils/lifecycle.js | 1 + src/worker.js | 1 + 4 files changed, 5 insertions(+) diff --git a/src/coordinator.js b/src/coordinator.js index 13c2fa8..a2370c4 100644 --- a/src/coordinator.js +++ b/src/coordinator.js @@ -2,6 +2,7 @@ import cluster from 'cluster'; import os from 'os'; import './environment'; +import Promise from './promise'; import logger from './utils/logger'; import { raceTo } from './utils/lifecycle'; diff --git a/src/utils/BatchManager.js b/src/utils/BatchManager.js index 304e542..561efa7 100644 --- a/src/utils/BatchManager.js +++ b/src/utils/BatchManager.js @@ -1,3 +1,5 @@ +import Promise from '../promise'; + const noHTMLError = new TypeError( 'HTML was not returned to Hypernova, this is most likely an error within your application. Check your logs for any uncaught errors and/or rejections.', ); diff --git a/src/utils/lifecycle.js b/src/utils/lifecycle.js index 8fb5029..6a947c9 100644 --- a/src/utils/lifecycle.js +++ b/src/utils/lifecycle.js @@ -1,3 +1,4 @@ +import Promise from '../promise'; import logger from './logger'; const MAX_LIFECYCLE_EXECUTION_TIME_IN_MS = 300; diff --git a/src/worker.js b/src/worker.js index 59db4fd..4f9230e 100644 --- a/src/worker.js +++ b/src/worker.js @@ -1,6 +1,7 @@ import bodyParser from 'body-parser'; import './environment'; +import Promise from './promise'; import logger from './utils/logger'; import renderBatch from './utils/renderBatch'; import { runAppLifecycle, errorSync, raceTo } from './utils/lifecycle'; From 61ebdf047d6e5d195ff88455ab7d27b9e8c566e9 Mon Sep 17 00:00:00 2001 From: kmsheng Date: Thu, 28 May 2020 19:51:56 +0800 Subject: [PATCH 3/3] avoid global.Promise pollution --- src/environment.js | 24 ------------------------ 1 file changed, 24 deletions(-) diff --git a/src/environment.js b/src/environment.js index 80ad23d..6d2605e 100644 --- a/src/environment.js +++ b/src/environment.js @@ -1,26 +1,2 @@ /* eslint func-names:0 no-extra-parens:0 */ import 'airbnb-js-shims'; -import Promise from 'bluebird'; - -const es6methods = ['then', 'catch', 'constructor']; -const es6StaticMethods = ['all', 'race', 'resolve', 'reject', 'cast']; - -function isNotMethod(name) { - return !(es6methods.includes(name) || es6StaticMethods.includes(name) || name.charAt(0) === '_'); -} - -function del(obj) { - /* eslint no-param-reassign: 0 */ - return (key) => { delete obj[key]; }; -} - -function toFastProperties(obj) { - (function () {}).prototype = obj; -} - -Object.keys(Promise.prototype).filter(isNotMethod).forEach(del(Promise.prototype)); -Object.keys(Promise).filter(isNotMethod).forEach(del(Promise)); -toFastProperties(Promise); -toFastProperties(Promise.prototype); - -global.Promise = Promise;