shared-node-helpers.js (3451B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 /* global global */ 8 9 /** 10 * Adds mocks for browser-environment global variables/methods to Node global. 11 */ 12 function setMocksInGlobal() { 13 global.Cc = new Proxy( 14 {}, 15 { 16 get(target, prop) { 17 if (prop.startsWith("@mozilla.org")) { 18 return { getService: () => ({}) }; 19 } 20 return null; 21 }, 22 } 23 ); 24 global.Ci = { 25 // sw states from 26 // mozilla-central/source/dom/interfaces/base/nsIServiceWorkerManager.idl 27 nsIServiceWorkerInfo: { 28 STATE_PARSED: 0, 29 STATE_INSTALLING: 1, 30 STATE_INSTALLED: 2, 31 STATE_ACTIVATING: 3, 32 STATE_ACTIVATED: 4, 33 STATE_REDUNDANT: 5, 34 STATE_UNKNOWN: 6, 35 }, 36 }; 37 global.Cu = { 38 isInAutomation: true, 39 now: () => {}, 40 }; 41 42 global.Services = require("Services-mock"); 43 global.ChromeUtils = require("ChromeUtils-mock"); 44 global.Glean = require("Glean"); 45 46 global.isWorker = false; 47 48 global.loader = { 49 lazyGetter: (context, name, fn) => { 50 Object.defineProperty(global, name, { 51 get() { 52 delete global[name]; 53 global[name] = fn.apply(global); 54 return global[name]; 55 }, 56 configurable: true, 57 enumerable: true, 58 }); 59 }, 60 lazyRequireGetter: (context, names, module, destructure) => { 61 if (!Array.isArray(names)) { 62 names = [names]; 63 } 64 65 for (const name of names) { 66 global.loader.lazyGetter(context, name, () => { 67 return destructure ? require(module)[name] : require(module || name); 68 }); 69 } 70 }, 71 lazyServiceGetter: () => {}, 72 }; 73 74 global.define = function () {}; 75 76 // Used for the HTMLTooltip component. 77 // And set "isSystemPrincipal: false" because can't support XUL element in node. 78 global.document.nodePrincipal = { 79 isSystemPrincipal: false, 80 }; 81 82 global.requestIdleCallback = function () {}; 83 84 global.requestAnimationFrame = function (cb) { 85 cb(); 86 return null; 87 }; 88 89 // Mock getSelection 90 let selection; 91 global.getSelection = function () { 92 return { 93 toString: () => selection, 94 get type() { 95 if (selection === undefined) { 96 return "None"; 97 } 98 if (selection === "") { 99 return "Caret"; 100 } 101 return "Range"; 102 }, 103 setMockSelection: str => { 104 selection = str; 105 }, 106 }; 107 }; 108 109 // Array#flatMap is only supported in Node 11+ 110 if (!Array.prototype.flatMap) { 111 // eslint-disable-next-line no-extend-native 112 Array.prototype.flatMap = function (cb) { 113 return this.reduce((acc, x, i, arr) => { 114 return acc.concat(cb(x, i, arr)); 115 }, []); 116 }; 117 } 118 119 if (typeof global.TextEncoder === "undefined") { 120 const { TextEncoder } = require("util"); 121 global.TextEncoder = TextEncoder; 122 } 123 124 if (typeof global.TextDecoder === "undefined") { 125 const { TextDecoder } = require("util"); 126 global.TextDecoder = TextDecoder; 127 } 128 129 if (!Promise.withResolvers) { 130 Promise.withResolvers = function () { 131 let resolve, reject; 132 const promise = new Promise(function (res, rej) { 133 resolve = res; 134 reject = rej; 135 }); 136 return { resolve, reject, promise }; 137 }; 138 } 139 } 140 141 module.exports = { 142 setMocksInGlobal, 143 };