openvidu-browser: Node.js platform: Use `globalThis` instead of `window`

While `window` is specific to the web browser, and `global` is specific
to Node.js, the standard `globalThis` global variable is common to both
platforms.

Thus, to make openvidu-browser more portable to Node.js, it's better to
use the common name.

More info:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis
pull/672/merge
Juan Navarro 2022-07-27 16:36:08 +02:00
parent 71b0e6ca43
commit 2b70c12be6
5 changed files with 33 additions and 33 deletions

View File

@ -1,8 +1,8 @@
import { OpenVidu } from './OpenVidu/OpenVidu'; import { OpenVidu } from './OpenVidu/OpenVidu';
import { JL } from 'jsnlog'; import { JL } from 'jsnlog';
if (window) { if (typeof globalThis !== 'undefined') {
window['OpenVidu'] = OpenVidu; globalThis['OpenVidu'] = OpenVidu;
} }
// Disable jsnlog when library is loaded // Disable jsnlog when library is loaded

View File

@ -263,11 +263,11 @@ export class LocalRecorder {
a.style.display = 'none'; a.style.display = 'none';
document.body.appendChild(a); document.body.appendChild(a);
const url = window.URL.createObjectURL(<any>this.blob); const url = globalThis.URL.createObjectURL(<any>this.blob);
a.href = url; a.href = url;
a.download = this.id + '.' + Mime.getExtension(this.blob!.type); a.download = this.id + '.' + Mime.getExtension(this.blob!.type);
a.click(); a.click();
window.URL.revokeObjectURL(url); globalThis.URL.revokeObjectURL(url);
document.body.removeChild(a); document.body.removeChild(a);
} }
@ -377,7 +377,7 @@ export class LocalRecorder {
this.blob = new Blob(this.chunks, { type: this.mediaRecorder.mimeType }); this.blob = new Blob(this.chunks, { type: this.mediaRecorder.mimeType });
this.chunks = []; this.chunks = [];
this.videoPreviewSrc = window.URL.createObjectURL(this.blob); this.videoPreviewSrc = globalThis.URL.createObjectURL(this.blob);
this.state = LocalRecorderState.FINISHED; this.state = LocalRecorderState.FINISHED;
} }

View File

@ -624,7 +624,7 @@ export class OpenVidu {
* @hidden * @hidden
*/ */
onOrientationChanged(handler): void { onOrientationChanged(handler): void {
(<any>window).addEventListener('orientationchange', handler); (globalThis as any).addEventListener('orientationchange', handler);
} }
/** /**

View File

@ -12,7 +12,7 @@ export class OpenViduLogger {
private MAX_MSECONDS_BATCH_MESSAGES: number = 5000; private MAX_MSECONDS_BATCH_MESSAGES: number = 5000;
private MAX_LENGTH_STRING_JSON: number = 1000; private MAX_LENGTH_STRING_JSON: number = 1000;
private defaultConsoleLogger: ConsoleLogger = new ConsoleLogger(window.console); private defaultConsoleLogger: ConsoleLogger = new ConsoleLogger(globalThis.console);
private currentAppender: any; private currentAppender: any;
@ -29,7 +29,7 @@ export class OpenViduLogger {
static configureJSNLog(openVidu: OpenVidu, token: string) { static configureJSNLog(openVidu: OpenVidu, token: string) {
try { try {
// If dev mode or... // If dev mode or...
if ((window['LOG_JSNLOG_RESULTS']) || if ((globalThis['LOG_JSNLOG_RESULTS']) ||
// If instance is created and it is OpenVidu Pro // If instance is created and it is OpenVidu Pro
(this.instance && openVidu.isAtLeastPro (this.instance && openVidu.isAtLeastPro
// If logs are enabled // If logs are enabled
@ -89,7 +89,7 @@ export class OpenViduLogger {
const seen = new WeakSet(); const seen = new WeakSet();
return (key, value) => { return (key, value) => {
if (typeof value === "object" && value != null) { if (typeof value === "object" && value != null) {
if (seen.has(value) || (HTMLElement && value instanceof HTMLElement)) { if (seen.has(value) || (globalThis.HTMLElement && value instanceof HTMLElement)) {
return; return;
} }
seen.add(value); seen.add(value);
@ -104,7 +104,7 @@ export class OpenViduLogger {
stringifyJson = `${stringifyJson.substring(0, this.instance.MAX_LENGTH_STRING_JSON)}...`; stringifyJson = `${stringifyJson.substring(0, this.instance.MAX_LENGTH_STRING_JSON)}...`;
} }
if (window['LOG_JSNLOG_RESULTS']) { if (globalThis['LOG_JSNLOG_RESULTS']) {
console.log(stringifyJson); console.log(stringifyJson);
} }
@ -193,12 +193,12 @@ export class OpenViduLogger {
} }
private replaceWindowConsole() { private replaceWindowConsole() {
window.console = this.defaultConsoleLogger.logger; globalThis.console = this.defaultConsoleLogger.logger;
window.console.log = this.getConsoleWithJSNLog().log; globalThis.console.log = this.getConsoleWithJSNLog().log;
window.console.info = this.getConsoleWithJSNLog().info; globalThis.console.info = this.getConsoleWithJSNLog().info;
window.console.debug = this.getConsoleWithJSNLog().debug; globalThis.console.debug = this.getConsoleWithJSNLog().debug;
window.console.warn = this.getConsoleWithJSNLog().warn; globalThis.console.warn = this.getConsoleWithJSNLog().warn;
window.console.error = this.getConsoleWithJSNLog().error; globalThis.console.error = this.getConsoleWithJSNLog().error;
} }
private disableLogger() { private disableLogger() {
@ -206,12 +206,12 @@ export class OpenViduLogger {
this.isJSNLogSetup = false; this.isJSNLogSetup = false;
this.loggingSessionId = undefined; this.loggingSessionId = undefined;
this.currentAppender = undefined; this.currentAppender = undefined;
window.console = this.defaultConsoleLogger.logger; globalThis.console = this.defaultConsoleLogger.logger;
window.console.log = this.defaultConsoleLogger.log; globalThis.console.log = this.defaultConsoleLogger.log;
window.console.info = this.defaultConsoleLogger.info; globalThis.console.info = this.defaultConsoleLogger.info;
window.console.debug = this.defaultConsoleLogger.debug; globalThis.console.debug = this.defaultConsoleLogger.debug;
window.console.warn = this.defaultConsoleLogger.warn; globalThis.console.warn = this.defaultConsoleLogger.warn;
window.console.error = this.defaultConsoleLogger.error; globalThis.console.error = this.defaultConsoleLogger.error;
} }
/** /**

View File

@ -23,7 +23,7 @@ getScreenId(function (error, sourceId, screen_constraints) {
}, 'pass second parameter only if you want system audio'); }, 'pass second parameter only if you want system audio');
*/ */
window.getScreenId = function (firefoxString, callback, custom_parameter) { globalThis.getScreenId = function (firefoxString, callback, custom_parameter) {
if (navigator.userAgent.indexOf('Edge') !== -1 && (!!navigator.msSaveOrOpenBlob || !!navigator.msSaveBlob)) { if (navigator.userAgent.indexOf('Edge') !== -1 && (!!navigator.msSaveOrOpenBlob || !!navigator.msSaveBlob)) {
// microsoft edge => navigator.getDisplayMedia(screen_constraints).then(onSuccess, onFailure); // microsoft edge => navigator.getDisplayMedia(screen_constraints).then(onSuccess, onFailure);
callback({ callback({
@ -45,7 +45,7 @@ window.getScreenId = function (firefoxString, callback, custom_parameter) {
return; return;
} }
window.addEventListener('message', onIFrameCallback); globalThis.addEventListener('message', onIFrameCallback);
function onIFrameCallback(event) { function onIFrameCallback(event) {
if (!event.data) return; if (!event.data) return;
@ -58,14 +58,14 @@ window.getScreenId = function (firefoxString, callback, custom_parameter) {
} }
// this event listener is no more needed // this event listener is no more needed
window.removeEventListener('message', onIFrameCallback); globalThis.removeEventListener('message', onIFrameCallback);
} }
if (event.data.chromeExtensionStatus) { if (event.data.chromeExtensionStatus) {
callback(event.data.chromeExtensionStatus, null, getScreenConstraints(event.data.chromeExtensionStatus)); callback(event.data.chromeExtensionStatus, null, getScreenConstraints(event.data.chromeExtensionStatus));
// this event listener is no more needed // this event listener is no more needed
window.removeEventListener('message', onIFrameCallback); globalThis.removeEventListener('message', onIFrameCallback);
} }
} }
@ -85,8 +85,8 @@ function getScreenConstraints(error, sourceId, canRequestAudioTrack) {
video: { video: {
mandatory: { mandatory: {
chromeMediaSource: error ? 'screen' : 'desktop', chromeMediaSource: error ? 'screen' : 'desktop',
maxWidth: window.screen.width > 1920 ? window.screen.width : 1920, maxWidth: globalThis.screen.width > 1920 ? globalThis.screen.width : 1920,
maxHeight: window.screen.height > 1080 ? window.screen.height : 1080 maxHeight: globalThis.screen.height > 1080 ? globalThis.screen.height : 1080
}, },
optional: [] optional: []
} }
@ -148,7 +148,7 @@ function postGetSourceIdMessage(custom_parameter) {
var iframe; var iframe;
// this function is used in RTCMultiConnection v3 // this function is used in RTCMultiConnection v3
window.getScreenConstraints = function (callback) { globalThis.getScreenConstraints = function (callback) {
loadIFrame(function () { loadIFrame(function () {
getScreenId(function (error, sourceId, screen_constraints) { getScreenId(function (error, sourceId, screen_constraints) {
if (!screen_constraints) { if (!screen_constraints) {
@ -178,14 +178,14 @@ function loadIFrame(loadCallback) {
(document.body || document.documentElement).appendChild(iframe); (document.body || document.documentElement).appendChild(iframe);
} }
window.getChromeExtensionStatus = function (callback) { globalThis.getChromeExtensionStatus = function (callback) {
// for Firefox: // for Firefox:
if (!!navigator.mozGetUserMedia) { if (!!navigator.mozGetUserMedia) {
callback('installed-enabled'); callback('installed-enabled');
return; return;
} }
window.addEventListener('message', onIFrameCallback); globalThis.addEventListener('message', onIFrameCallback);
function onIFrameCallback(event) { function onIFrameCallback(event) {
if (!event.data) return; if (!event.data) return;
@ -194,7 +194,7 @@ window.getChromeExtensionStatus = function (callback) {
callback(event.data.chromeExtensionStatus); callback(event.data.chromeExtensionStatus);
// this event listener is no more needed // this event listener is no more needed
window.removeEventListener('message', onIFrameCallback); globalThis.removeEventListener('message', onIFrameCallback);
} }
} }
@ -217,4 +217,4 @@ function postGetChromeExtensionStatusMessage() {
}, '*'); }, '*');
} }
exports.getScreenId = window.getScreenId; exports.getScreenId = globalThis.getScreenId;