From 906b1e4fcc2b4af1a17f0460d4c053b5dc7abee3 Mon Sep 17 00:00:00 2001 From: Dario Vladovic Date: Wed, 29 Jan 2020 22:21:27 +0100 Subject: [PATCH 1/2] Ensure that URL constructor is globally available WHATWG URL API has been implemented since v7 but global URL constructor was added later as part of v10 release. --- src/index.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/index.js b/src/index.js index 41ef4ae..b1642f5 100644 --- a/src/index.js +++ b/src/index.js @@ -14,6 +14,12 @@ * limitations under the License. */ +// Ensure that URL constructor is globally available +// https://nodejs.org/api/globals.html#globals_url +if (typeof global === 'object') { + global.URL = global.URL || require('url').URL; +} + export { default as parse } from './parse'; export { default as format } from './format'; export { resolve, resolveObject } from './resolve'; From 7a105e8e412bf74d52faf749f861dddc6706c11d Mon Sep 17 00:00:00 2001 From: Dario Vladovic Date: Thu, 30 Jan 2020 00:01:16 +0100 Subject: [PATCH 2/2] Export native `URL` & `URLSearchParams` constructors --- src/index.js | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index b1642f5..75f1eb0 100644 --- a/src/index.js +++ b/src/index.js @@ -14,12 +14,27 @@ * limitations under the License. */ -// Ensure that URL constructor is globally available -// https://nodejs.org/api/globals.html#globals_url -if (typeof global === 'object') { - global.URL = global.URL || require('url').URL; +let root; +if (typeof window !== 'undefined') { + root = window; +} else if (typeof global !== 'undefined') { + /** + * Ensure that `URL` & `URLSearchParams` constructors are globally available + * @see https://nodejs.org/api/globals.html#globals_url + * @see https://nodejs.org/api/globals.html#globals_urlsearchparams + */ + const { URL, URLSearchParams } = require('url'); + global.URL = global.URL || URL; + global.URLSearchParams = global.URLSearchParams || URLSearchParams; + root = global; +} else if (typeof self !== 'undefined') { + root = self; +} else { + root = this; } export { default as parse } from './parse'; export { default as format } from './format'; export { resolve, resolveObject } from './resolve'; +export const { URL } = root; +export const { URLSearchParams } = root;