From b970fe547fa1bedafe20886c53b9b185b637e07d Mon Sep 17 00:00:00 2001 From: zorkow Date: Sat, 16 Nov 2024 21:10:08 +0100 Subject: [PATCH] make SRE webworker ready --- ts/common/cli.ts | 2 +- ts/common/dom_util.ts | 2 +- ts/common/engine.ts | 3 ++- ts/common/system.ts | 2 +- ts/common/system_external.ts | 20 +++++++++++++++----- ts/common/xpath_util.ts | 3 ++- 6 files changed, 22 insertions(+), 10 deletions(-) diff --git a/ts/common/cli.ts b/ts/common/cli.ts index 1d390b8ad..af27e3786 100644 --- a/ts/common/cli.ts +++ b/ts/common/cli.ts @@ -37,7 +37,7 @@ export class Cli { /** * Commander library. */ - public static commander = SystemExternal.documentSupported + public static commander = (SystemExternal.documentSupported || SystemExternal.webworker) ? null : SystemExternal.extRequire('commander').program; diff --git a/ts/common/dom_util.ts b/ts/common/dom_util.ts index b673312dd..8ce1229db 100644 --- a/ts/common/dom_util.ts +++ b/ts/common/dom_util.ts @@ -73,7 +73,7 @@ export function parseInput(input: string): Element { ); if (Engine.getInstance().mode === EngineConst.Mode.HTTP) { XpathUtil.xpath.currentDocument = doc; - return html ? doc.body.childNodes[0] : doc.documentElement; + return (html ? doc.body.childNodes[0] : doc.documentElement) as Element; } return doc.documentElement; } catch (err) { diff --git a/ts/common/engine.ts b/ts/common/engine.ts index 92a073b7c..96f2b74e8 100644 --- a/ts/common/engine.ts +++ b/ts/common/engine.ts @@ -22,6 +22,7 @@ import { AuditoryDescription } from '../audio/auditory_description.js'; import * as Dcstr from '../rule_engine/dynamic_cstr.js'; import * as EngineConst from './engine_const.js'; +import { SystemExternal } from './system_external.js'; import { Debugger } from './debugger.js'; import { Variables } from './variables.js'; @@ -349,7 +350,7 @@ export class Engine { * @param feature An object describing some setup features. */ public configurate(feature: { [key: string]: boolean | string }) { - if (this.mode === EngineConst.Mode.HTTP && !this.config) { + if (this.mode === EngineConst.Mode.HTTP && !SystemExternal.webworker && !this.config) { configBlocks(feature); this.config = true; } diff --git a/ts/common/system.ts b/ts/common/system.ts index fb15f7398..5a1c44f6d 100644 --- a/ts/common/system.ts +++ b/ts/common/system.ts @@ -405,7 +405,7 @@ export function exit(opt_value?: number) { */ export const localePath = FileUtil.localePath; -if (SystemExternal.documentSupported) { +if (SystemExternal.documentSupported || SystemExternal.webworker) { setupEngine({ mode: EngineConst.Mode.HTTP }).then(() => setupEngine({})); } else { setupEngine({ mode: EngineConst.Mode.SYNC }).then(() => diff --git a/ts/common/system_external.ts b/ts/common/system_external.ts index 1d95b75cc..0cc27387f 100644 --- a/ts/common/system_external.ts +++ b/ts/common/system_external.ts @@ -22,10 +22,13 @@ */ import { Variables } from './variables.js'; +import * as Xmldom from '@xmldom/xmldom'; +import * as wgx from 'wicked-good-xpath'; declare let global: any; declare let require: (name: string) => any; declare let process: any; +declare const DedicatedWorkerGlobalScope: any; export class SystemExternal { @@ -59,19 +62,27 @@ export class SystemExternal { SystemExternal.windowSupported && !(typeof window.document === 'undefined'))(); + /** + * Check if webworker environment. + * + * @returns True if the DedicatedWorkerGlobalScope is available. + */ + public static webworker: boolean = (() => + !(typeof DedicatedWorkerGlobalScope === 'undefined'))(); + /** * Xmldom library. */ public static xmldom = SystemExternal.documentSupported ? window - : SystemExternal.extRequire('@xmldom/xmldom'); + : Xmldom; /** * DOM document implementation. */ public static document: Document = SystemExternal.documentSupported ? window.document - : new SystemExternal.xmldom.DOMImplementation().createDocument('', '', 0); + : new SystemExternal.xmldom.DOMImplementation().createDocument('', ''); /** * Xpath library. @@ -80,7 +91,6 @@ export class SystemExternal { ? document : (function () { const window = { document: {}, XPathResult: {} }; - const wgx = SystemExternal.extRequire('wicked-good-xpath'); wgx.install(window); (window.document as any).XPathResult = window.XPathResult; return window.document; @@ -97,7 +107,7 @@ export class SystemExternal { /** * Filesystem library. */ - public static fs = SystemExternal.documentSupported + public static fs = SystemExternal.documentSupported || SystemExternal.webworker ? null : SystemExternal.extRequire('fs'); @@ -110,7 +120,7 @@ export class SystemExternal { * Path to JSON files. */ public static jsonPath = (function () { - if (SystemExternal.documentSupported) { + if (SystemExternal.documentSupported || SystemExternal.webworker) { return SystemExternal.url; } if (process.env.SRE_JSON_PATH || global.SRE_JSON_PATH) { diff --git a/ts/common/xpath_util.ts b/ts/common/xpath_util.ts index 48e7f1358..ef0ee7f99 100644 --- a/ts/common/xpath_util.ts +++ b/ts/common/xpath_util.ts @@ -114,7 +114,8 @@ function evaluateXpath( rootNode: Node, type: number ): XPathResult { - return Engine.getInstance().mode === EngineConst.Mode.HTTP && + return !SystemExternal.webworker && + Engine.getInstance().mode === EngineConst.Mode.HTTP && !Engine.getInstance().isIE && !Engine.getInstance().isEdge ? xpath.currentDocument.evaluate(