From 66b9e4a1867bcc421cd3369c114426edbb7c4e97 Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Mon, 19 Nov 2018 22:06:28 +0100 Subject: [PATCH] [fix] fix JSONP transport in Node.js environment The commit 99bcc62 resulted in `window is not defined` errors. --- lib/transports/polling-jsonp.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/transports/polling-jsonp.js b/lib/transports/polling-jsonp.js index 7b208ebd3..8e693375f 100644 --- a/lib/transports/polling-jsonp.js +++ b/lib/transports/polling-jsonp.js @@ -30,6 +30,15 @@ var callbacks; function empty () { } +/** + * Until https://github.com/tc39/proposal-global is shipped. + */ +function glob () { + return typeof self !== 'undefined' ? self + : typeof window !== 'undefined' ? window + : typeof global !== 'undefined' ? global : {}; +} + /** * JSONP Polling constructor. * @@ -46,8 +55,8 @@ function JSONPPolling (opts) { // we do this here (lazily) to avoid unneeded global pollution if (!callbacks) { // we need to consider multiple engines in the same page - if (!window.___eio) window.___eio = []; - callbacks = window.___eio; + var global = glob(); + callbacks = global.___eio = (global.___eio || []); } // callback identifier