All files / lib/Util DomEnvironment.js

100% Statements 6/6
100% Branches 4/4
100% Functions 2/2
100% Lines 6/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26  5x 5x 5x                 10x               8x 7x        
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isDocumentAvailable = isDocumentAvailable;
exports.assertDocumentAvailable = assertDocumentAvailable;
/**
 * Helpers for detecting a real browser document.
 *
 * MV3 service workers (and Node) have no `document`. Core crypto/fetch paths are SW-safe;
 * UI-only helpers ({@link DownloadService}, OTP QR canvas rendering) must call these guards
 * so they fail with a clear error instead of a cryptic `document is not defined`.
 */
function isDocumentAvailable() {
    return typeof document !== "undefined"
        && typeof document.createElement === "function";
}
/**
 * Throws if `document` is not available (e.g. MV3 service worker / Node).
 * @param feature Human-readable name of the DOM-only feature for the error message
 */
function assertDocumentAvailable(feature) {
    if (!isDocumentAvailable()) {
        throw new Error(`${feature} requires a DOM (document) and cannot run in an MV3 service worker. ` +
            `Call it from a page, popup, or offscreen document, or use the non-DOM API instead.`);
    }
}