tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

factory.js (1415B)


      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 const CONDITIONS_MAP = {
      6  and: globalThis.ConditionAnd,
      7  test: globalThis.ConditionTest,
      8  or: globalThis.ConditionOr,
      9  cookie: globalThis.ConditionCookie,
     10  not: globalThis.ConditionNot,
     11  url: globalThis.ConditionUrl,
     12 };
     13 
     14 /**
     15 * The condition factory creates a set of conditions based on the breakages
     16 */
     17 class ConditionFactory {
     18  #storage = {};
     19  #context = {};
     20 
     21  constructor(context = {}) {
     22    this.#context = context || {};
     23  }
     24 
     25  static async run(conditionDesc, context = {}) {
     26    if (conditionDesc === undefined) {
     27      return true;
     28    }
     29 
     30    const factory = new ConditionFactory(context);
     31    const condition = await factory.create(conditionDesc);
     32    await condition.init();
     33    return condition.check();
     34  }
     35 
     36  create(conditionDesc) {
     37    const conditionClass = CONDITIONS_MAP[conditionDesc.type];
     38    if (!conditionClass) {
     39      throw new Error("Unknown condition type: " + String(conditionDesc?.type));
     40    }
     41    return new conditionClass(this, conditionDesc);
     42  }
     43 
     44  storeData(key, value) {
     45    this.#storage[key] = value;
     46  }
     47 
     48  retrieveData(key) {
     49    return this.#storage[key];
     50  }
     51 
     52  get context() {
     53    return this.#context;
     54  }
     55 }
     56 
     57 globalThis.ConditionFactory = ConditionFactory;