Skip to content

Commit

Permalink
make SRE webworker ready
Browse files Browse the repository at this point in the history
  • Loading branch information
zorkow committed Nov 16, 2024
1 parent 9f9ec8d commit b970fe5
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion ts/common/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion ts/common/dom_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion ts/common/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion ts/common/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() =>
Expand Down
20 changes: 15 additions & 5 deletions ts/common/system_external.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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.
Expand All @@ -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;
Expand All @@ -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');

Expand All @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion ts/common/xpath_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit b970fe5

Please sign in to comment.