Skip to content

Commit

Permalink
browser detection
Browse files Browse the repository at this point in the history
Signed-off-by: Dan Selman <[email protected]>
  • Loading branch information
dselman committed May 9, 2024
1 parent 50b862d commit cfa340e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
10 changes: 8 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/JavaScriptEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class JavaScriptEvaluator {
workers: Array<ChildProcess>; // child processes
queue: Array<WorkItem>; // queue of work to do

constructor(options: JavaScriptEvaluatorOptions) {
constructor(options: JavaScriptEvaluatorOptions = {waitInterval: 50, maxWorkers: 8, maxQueueDepth: 1000}) {
this.options = options;
this.workers = [];
this.queue = [];
Expand Down
9 changes: 6 additions & 3 deletions src/TemplateMarkInterpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
});
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit cfa340e

Please sign in to comment.