diff --git a/package-lock.json b/package-lock.json index 636a455..d2609f1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@accordproject/template-engine", - "version": "2.5.11", + "version": "2.5.12", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@accordproject/template-engine", - "version": "2.5.11", + "version": "2.5.12", "license": "Apache-2.0", "dependencies": { "@accordproject/concerto-codegen": "3.23.1-20240509154651", @@ -15,6 +15,7 @@ "@accordproject/markdown-common": "^0.16.22", "@accordproject/markdown-template": "^0.16.22", "@typescript/twoslash": "^3.2.4", + "browser-or-node": "^3.0.0", "dayjs": "1.11.10", "jsonpath": "^1.1.1", "tar": "^6.2.0", @@ -3035,6 +3036,11 @@ "node": ">=8" } }, + "node_modules/browser-or-node": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/browser-or-node/-/browser-or-node-3.0.0.tgz", + "integrity": "sha512-iczIdVJzGEYhP5DqQxYM9Hh7Ztpqqi+CXZpSmX8ALFs9ecXkQIeqRyM6TfxEfMVpwhl3dSuDvxdzzo9sUOIVBQ==" + }, "node_modules/browser-process-hrtime": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/browser-process-hrtime/-/browser-process-hrtime-1.0.0.tgz", diff --git a/package.json b/package.json index 13b6e0a..251ac04 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@accordproject/template-engine", - "version": "2.5.11", + "version": "2.5.12", "description": "Generation of AgreementMark from TemplateMark + JSON Data", "homepage": "https://github.com/accordproject", "engines": { @@ -46,6 +46,7 @@ "@accordproject/markdown-common": "^0.16.22", "@accordproject/markdown-template": "^0.16.22", "@typescript/twoslash": "^3.2.4", + "browser-or-node": "^3.0.0", "dayjs": "1.11.10", "jsonpath": "^1.1.1", "tar": "^6.2.0", diff --git a/src/JavaScriptEvaluator.ts b/src/JavaScriptEvaluator.ts index 4a2cf12..783d1db 100644 --- a/src/JavaScriptEvaluator.ts +++ b/src/JavaScriptEvaluator.ts @@ -74,7 +74,7 @@ export class JavaScriptEvaluator { workers: Array; // child processes queue: Array; // queue of work to do - constructor(options: JavaScriptEvaluatorOptions) { + constructor(options: JavaScriptEvaluatorOptions = {waitInterval: 50, maxWorkers: 8, maxQueueDepth: 1000}) { this.options = options; this.workers = []; this.queue = []; diff --git a/src/TemplateMarkInterpreter.ts b/src/TemplateMarkInterpreter.ts index 5f705fb..60fecc2 100644 --- a/src/TemplateMarkInterpreter.ts +++ b/src/TemplateMarkInterpreter.ts @@ -14,6 +14,7 @@ import jp from 'jsonpath'; import traverse from 'traverse'; +import { isBrowser } from 'browser-or-node'; import os from 'os'; import { ClassDeclaration, Factory, Introspector, ModelManager, Serializer } from '@accordproject/concerto-core'; @@ -51,9 +52,8 @@ function checkCode(code:ICode) { // this is a global because we don't want the user // to configure child processes at the TemplateMarkInterpreter instance level -const availableProcessors = process.env.MAX_WORKERS ? Number.parseInt(process.env.MAX_WORKERS) : os.availableParallelism(); -const javaScriptEvaluator = new JavaScriptEvaluator({ - maxWorkers: availableProcessors, // how many child processes +const javaScriptEvaluator = isBrowser ? new JavaScriptEvaluator() : new JavaScriptEvaluator({ + maxWorkers: process.env.MAX_WORKERS ? Number.parseInt(process.env.MAX_WORKERS) : os.availableParallelism(), // how many child processes waitInterval: process.env.WAIT_INTERVAL ? Number.parseInt(process.env.WAIT_INTERVAL) : 50, // how long to wait before rescheduling work maxQueueDepth: process.env.MAX_QUEUE_DEPTH ? Number.parseInt(process.env.MAX_QUEUE_DEPTH) : 1000 // max requests to queue }); @@ -91,6 +91,9 @@ async function evaluateJavaScript(clauseLibrary:object, data: TemplateData, fn: try { const request = {code: expression, argumentNames: functionArgNames, arguments: functionArgValues}; if(options?.childProcessJavaScriptEvaluation) { + if(isBrowser) { + throw new Error('Child process evaluation is not supported inside web browser'); + } const evalOptions = options?.timeout ? {timeout: options.timeout} : undefined; const r = await javaScriptEvaluator.evalChildProcess(request, evalOptions); return (typeof r.result === 'object') ? JSON.stringify(r.result) : r.result.toString();