Skip to content

Commit

Permalink
enable debug logs as soon as __RX_PLAYER_DEBUG_MODE__
Browse files Browse the repository at this point in the history
Today the `RX_PLAYER_DEBUG_MODE__` global boolean only enable debug logs
if it is set **BEFORE** the RxPlayer files are evaluated / initially
  executed.

This is difficult to explain to people not used to that kind of tricks.

In this commit, I propose to perform an ugly `Object.defineProperty`
trick on the global scope (`window` / `globalThis`) so we enable debug
mode even if the RxPlayer was already imported.

This way:

  1. any application running with the RxPlayer could just at any time
     set something like `window.RX_PLAYER_DEBUG_MODE__ = true` in the
     console to obtain debug logs

  2. [RxPaired](https://github.com/canalplus/RxPaired/)'s client script
     could be included in the console at any time without asking users
     to pass as argument the RxPlayer class if it's done at runtime.

This can be seen as hacky and unusual, we would also mess ourselves
with global scope which we usually don't like to do as a library.

However reserving the `__RX_PLAYER_DEBUG_MODE__` global property and
reacting on it still doesn't seems outlandish to me.

---

Note that executing two times the RxPlayer files will make only the
last one react to that property. I think this is not an issue because
applications should never execute two times the same RxPlayer file in
a given JavaScript realm (if it does, they rely on a bundler or
user-agent not respecting ECMAScript's specification regarding
module evaluation, so it's OK IMO to not support that case).
  • Loading branch information
peaBerberian committed Jan 9, 2025
1 parent a117437 commit 17fb6af
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 30 deletions.
12 changes: 0 additions & 12 deletions src/compat/is_debug_mode_enabled.ts

This file was deleted.

7 changes: 0 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
* This is the class used from a regular build.
*/

import isDebugModeEnabled from "./compat/is_debug_mode_enabled";
import patchWebkitSourceBuffer from "./compat/patch_webkit_source_buffer";
import {
DASH,
Expand All @@ -35,7 +34,6 @@ import {
NATIVE_VTT_PARSER,
SMOOTH,
} from "./features/list";
import logger from "./log";
import Player from "./main_thread/api";
import globalScope from "./utils/global_scope";

Expand All @@ -55,11 +53,6 @@ Player.addFeatures([
HTML_VTT_PARSER,
HTML_SRT_PARSER,
]);
if (isDebugModeEnabled()) {
logger.setLevel("DEBUG", "standard");
} else if ((__ENVIRONMENT__.CURRENT_ENV as number) === (__ENVIRONMENT__.DEV as number)) {
logger.setLevel(__LOGGER_LEVEL__.CURRENT_LEVEL, "standard");
}
export default Player;

if (typeof __GLOBAL_SCOPE__ === "boolean" && __GLOBAL_SCOPE__) {
Expand Down
35 changes: 32 additions & 3 deletions src/main_thread/api/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import {
import getStartDate from "../../compat/get_start_date";
import hasMseInWorker from "../../compat/has_mse_in_worker";
import hasWorkerApi from "../../compat/has_worker_api";
import isDebugModeEnabled from "../../compat/is_debug_mode_enabled";
import config from "../../config";
import type { ISegmentSinkMetrics } from "../../core/segment_sinks/segment_sinks_store";
import type {
Expand Down Expand Up @@ -107,6 +106,7 @@ import arrayIncludes from "../../utils/array_includes";
import assert, { assertUnreachable } from "../../utils/assert";
import type { IEventPayload, IListener } from "../../utils/event_emitter";
import EventEmitter from "../../utils/event_emitter";
import globalScope from "../../utils/global_scope";
import idGenerator from "../../utils/id_generator";
import isNullOrUndefined from "../../utils/is_null_or_undefined";
import type Logger from "../../utils/logger";
Expand Down Expand Up @@ -141,6 +141,35 @@ import {

/* eslint-disable @typescript-eslint/naming-convention */

// Enable debug mode as soon as `RX_PLAYER_DEBUG_MODE__` is set to `true`:

const globals: typeof globalScope & {
__RX_PLAYER_DEBUG_MODE__?: boolean;
} = globalScope;

let isDebugModeEnabled: boolean =
typeof globals.__RX_PLAYER_DEBUG_MODE__ === "boolean" &&
globals.__RX_PLAYER_DEBUG_MODE__;

Object.defineProperty(globals, "__RX_PLAYER_DEBUG_MODE__", {
get(): boolean {
return isDebugModeEnabled;
},
set(val: boolean) {
isDebugModeEnabled = val;
if (val) {
Player.LogLevel = "DEBUG";
Player.LogFormat = "full";
}
},
});

if (isDebugModeEnabled) {
log.setLevel("DEBUG", "full");
} else if ((__ENVIRONMENT__.CURRENT_ENV as number) === (__ENVIRONMENT__.DEV as number)) {
log.setLevel(__LOGGER_LEVEL__.CURRENT_LEVEL, "standard");
}

const generateContentId = idGenerator();

/**
Expand Down Expand Up @@ -550,7 +579,7 @@ class Player extends EventEmitter<IPublicAPIEvent> {
dashWasmUrl: workerSettings.dashWasmUrl,
logLevel: log.getLevel(),
logFormat: log.getFormat(),
sendBackLogs: isDebugModeEnabled(),
sendBackLogs: isDebugModeEnabled,
date: Date.now(),
timestamp: getMonotonicTimeStamp(),
hasVideo: this.videoElement?.nodeName.toLowerCase() === "video",
Expand All @@ -569,7 +598,7 @@ class Player extends EventEmitter<IPublicAPIEvent> {
value: {
logLevel: logInfo.level,
logFormat: logInfo.format,
sendBackLogs: isDebugModeEnabled(),
sendBackLogs: isDebugModeEnabled,
},
});
},
Expand Down
8 changes: 0 additions & 8 deletions src/minimal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,11 @@
* import only features that is needed.
*/

import isDebugModeEnabled from "./compat/is_debug_mode_enabled";
import patchWebkitSourceBuffer from "./compat/patch_webkit_source_buffer";
import logger from "./log";
import Player from "./main_thread/api";

patchWebkitSourceBuffer();

if (isDebugModeEnabled()) {
logger.setLevel("DEBUG", "standard");
} else if ((__ENVIRONMENT__.CURRENT_ENV as number) === (__ENVIRONMENT__.DEV as number)) {
logger.setLevel(__LOGGER_LEVEL__.CURRENT_LEVEL, "standard");
}

/**
* Minimal Player which starts with no feature.
*/
Expand Down

0 comments on commit 17fb6af

Please sign in to comment.