From f6b5e9ff3b5a701b9e8ab31b36924dd5a7a518bf Mon Sep 17 00:00:00 2001 From: Luke Childs Date: Fri, 7 Oct 2016 18:00:49 +0100 Subject: [PATCH] Don't use jsdom private API features to get global properties Before we were manually grabbing `window._core` which was a bit of a hack. This sets the correct settings to allow us to get all globals with `Object.getOwnPropertyNames(window)`. We also need to clone the settings object before passing it to jsdom otherwise it tacks loads of stuff on to the referenced object. --- src/index.js | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/index.js b/src/index.js index bc56958..306715a 100644 --- a/src/index.js +++ b/src/index.js @@ -1,16 +1,19 @@ -import { jsdom } from 'jsdom'; +import jsdom from 'jsdom'; -function getWindowPropertyKeys(window) { - return Object - .keys(window) - .concat(Object.keys(window._core)) - .filter(prop => prop.substring(0, 1) !== '_'); -} +const defaultJsdomConfig = { + features: { + FetchExternalResources: false, + ProcessExternalResources: false + } +}; + +const cloneObject = obj => JSON.parse(JSON.stringify(obj)); const protectedproperties = (() => { - const window = jsdom('').defaultView; + const window = jsdom.jsdom('', cloneObject(defaultJsdomConfig)).defaultView; - return getWindowPropertyKeys(window) + return Object + .getOwnPropertyNames(window) .filter(prop => global[prop]); })(); @@ -18,11 +21,14 @@ const getType = val => Object.prototype.toString.call(val); module.exports = (...args) => { const properties = args.filter(arg => getType(arg) === '[object Array]')[0]; - const jsdomConfig = args.filter(arg => getType(arg) === '[object Object]')[0]; + const userJsdomConfig = args.filter(arg => getType(arg) === '[object Object]')[0]; + + const jsdomConfig = Object.assign({}, userJsdomConfig, defaultJsdomConfig); - const window = jsdom('', jsdomConfig).defaultView; + const window = jsdom.jsdom('', cloneObject(jsdomConfig)).defaultView; - getWindowPropertyKeys(window) + Object + .getOwnPropertyNames(window) .filter(prop => protectedproperties.indexOf(prop) === -1) .filter(prop => !(properties && properties.indexOf(prop) === -1)) .forEach(prop => global[prop] = window[prop]);