From 20d7db879ae7cbc466b615a7574acf516c45e74c Mon Sep 17 00:00:00 2001 From: James Rosewell Date: Sun, 15 Nov 2026 22:57:12 +0000 Subject: [PATCH 1/5] Test: the JSON endpoint returns JSON in the same form as the JSON in the JavaScript The JavaScript Builder specification's SetEndpoint option says "This endpoint MUST return JSON data in the same form as the JSON embedded within the JavaScript" (pipeline-specification/pipeline-elements/javascript-builder.md line 348 at 51Degrees/specifications 706de1f0). The test serves the JSON endpoint and the JavaScript endpoint for one request through a real Pipeline, and checks that the JSON the script assigns to its json variable is the JSON the endpoint returns, with a value, a Property assigned no value and its nullreason. --- .../web/ClientsideJsonEndpointTests.java | 208 ++++++++++++++++++ 1 file changed, 208 insertions(+) create mode 100644 pipeline/pipeline.web.packages/pipeline.web/src/test/java/fiftyone/pipeline/web/ClientsideJsonEndpointTests.java diff --git a/pipeline/pipeline.web.packages/pipeline.web/src/test/java/fiftyone/pipeline/web/ClientsideJsonEndpointTests.java b/pipeline/pipeline.web.packages/pipeline.web/src/test/java/fiftyone/pipeline/web/ClientsideJsonEndpointTests.java new file mode 100644 index 000000000..583ac565e --- /dev/null +++ b/pipeline/pipeline.web.packages/pipeline.web/src/test/java/fiftyone/pipeline/web/ClientsideJsonEndpointTests.java @@ -0,0 +1,208 @@ +/* ********************************************************************* + * This Original Work is copyright of 51 Degrees Mobile Experts Limited. + * Copyright 2026 51 Degrees Mobile Experts Limited, Davidson House, + * Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU. + * + * This Original Work is licensed under the European Union Public Licence + * (EUPL) v.1.2 and is subject to its terms as set out below. + * + * If a copy of the EUPL was not distributed with this file, You can obtain + * one at https://opensource.org/licenses/EUPL-1.2. + * + * The 'Compatible Licences' set out in the Appendix to the EUPL (as may be + * amended by the European Commission) shall be deemed incompatible for + * the purposes of the Work and the provisions of the compatibility + * clause in Article 5 of the EUPL shall not apply. + * + * If using the Work as, or as part of, a network application, by + * including the attribution notice(s) required under Article 5 of the EUPL + * in the end user terms of the application under an appropriate heading, + * such notice(s) shall fulfill the requirements of that article. + * ********************************************************************* */ + +package fiftyone.pipeline.web; + +import fiftyone.pipeline.core.data.ElementDataBase; +import fiftyone.pipeline.core.data.ElementPropertyMetaData; +import fiftyone.pipeline.core.data.ElementPropertyMetaDataDefault; +import fiftyone.pipeline.core.data.EvidenceKeyFilter; +import fiftyone.pipeline.core.data.EvidenceKeyFilterWhitelist; +import fiftyone.pipeline.core.data.FlowData; +import fiftyone.pipeline.core.flowelements.FlowElementBase; +import fiftyone.pipeline.core.flowelements.Pipeline; +import fiftyone.pipeline.core.flowelements.PipelineBuilder; +import fiftyone.pipeline.engines.data.AspectPropertyValueDefault; +import fiftyone.pipeline.engines.fiftyone.flowelements.SequenceElementBuilder; +import fiftyone.pipeline.javascriptbuilder.flowelements.JavaScriptBuilderElementBuilder; +import fiftyone.pipeline.jsonbuilder.flowelements.JsonBuilderElementBuilder; +import fiftyone.pipeline.web.services.ClientsidePropertyServiceCore; +import fiftyone.pipeline.web.services.FlowDataProviderCore; +import fiftyone.pipeline.web.services.WebRequestEvidenceServiceCore; +import org.json.JSONObject; +import org.json.JSONTokener; +import org.junit.Test; +import org.slf4j.ILoggerFactory; +import org.slf4j.LoggerFactory; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * The JSON endpoint answers with JSON in the same form as the JSON the + * JavaScript holds, as the SetEndpoint row of the JavaScript Builder's + * configuration options asks. + * @see Specification + */ +public class ClientsideJsonEndpointTests { + + private static final ILoggerFactory LOGGERS = + LoggerFactory.getILoggerFactory(); + + /** + * The text that the template puts before the JSON in the script. + */ + private static final String JSON_IN_SCRIPT = "var json = "; + + /** + * The JSON served by the JSON endpoint and the JSON the script served by + * the JavaScript endpoint holds, for one request, are the same, with a + * value, a Property assigned no value and its nullreason, and the + * JavaScript Property and session values the builders add. + */ + @Test + public void JsonEndpoint_SameFormAsJsonInJavaScript() throws Exception { + try (Pipeline pipeline = new PipelineBuilder(LOGGERS) + .addFlowElement(new SequenceElementBuilder(LOGGERS).build()) + .addFlowElement(new ValuesElement()) + .addFlowElement(new JsonBuilderElementBuilder(LOGGERS).build()) + .addFlowElement(new JavaScriptBuilderElementBuilder(LOGGERS) + .setEndpoint("/51dpipeline/json") + .build()) + .setAutoCloseElements(true) + .build(); + FlowData data = pipeline.createFlowData()) { + HttpServletRequest request = request(); + new WebRequestEvidenceServiceCore.Default() + .addEvidenceFromRequest(data, request); + data.process(); + assertTrue("Processing recorded errors: " + data.getErrors(), + data.getErrors().isEmpty()); + + FlowDataProviderCore provider = mock(FlowDataProviderCore.class); + when(provider.getFlowData(request)).thenReturn(data); + ClientsidePropertyServiceCore service = + new ClientsidePropertyServiceCore.Default(provider, pipeline); + + StringWriter json = new StringWriter(); + service.serveJson(request, response(json)); + StringWriter javaScript = new StringWriter(); + service.serveJavascript(request, response(javaScript)); + + JSONObject served = new JSONObject(json.toString()); + JSONObject embedded = jsonInScript(javaScript.toString()); + assertTrue("The JSON endpoint answered\n" + served.toString(2) + + "\nbut the script holds\n" + embedded.toString(2), + served.similar(embedded)); + JSONObject values = served.getJSONObject("values"); + assertEquals("a value", values.getString("value")); + assertTrue(values.isNull("novalue")); + assertEquals("No value for the test.", + values.getString("novaluenullreason")); + } + } + + /** + * The JSON object the script assigns to its json variable. + */ + private static JSONObject jsonInScript(String javaScript) { + int start = javaScript.indexOf(JSON_IN_SCRIPT); + assertTrue("The script holds no JSON: " + javaScript, start >= 0); + return (JSONObject) new JSONTokener( + javaScript.substring(start + JSON_IN_SCRIPT.length())) + .nextValue(); + } + + private static HttpServletRequest request() { + HttpServletRequest request = mock(HttpServletRequest.class); + when(request.getMethod()).thenReturn("GET"); + when(request.getRequestURI()).thenReturn("/51dpipeline/json"); + when(request.getHeaderNames()).thenReturn( + Collections.enumeration(Arrays.asList("Host"))); + when(request.getHeader("Host")).thenReturn("example.com"); + when(request.getRemoteAddr()).thenReturn("1.2.3.4"); + when(request.getScheme()).thenReturn("https"); + return request; + } + + private static HttpServletResponse response(StringWriter body) + throws Exception { + HttpServletResponse response = mock(HttpServletResponse.class); + when(response.getWriter()).thenReturn(new PrintWriter(body, true)); + return response; + } + + /** + * An element that adds a value, a Property assigned no value and a + * JavaScript Property, in place of an engine. + */ + private static class ValuesElement + extends FlowElementBase { + + ValuesElement() { + super(LoggerFactory.getLogger(ValuesElement.class), null); + } + + @Override + protected void processInternal(FlowData data) { + ElementDataBase values = + new ElementDataBase(logger, data) { }; + values.put("value", "a value"); + AspectPropertyValueDefault noValue = + new AspectPropertyValueDefault<>(); + noValue.setNoValueMessage("No value for the test."); + values.put("novalue", noValue); + values.put("javascript", "var a = 1;"); + data.getOrAdd(getElementDataKey(), d -> values); + } + + @Override + public String getElementDataKey() { + return "values"; + } + + @Override + public EvidenceKeyFilter getEvidenceKeyFilter() { + return new EvidenceKeyFilterWhitelist( + Collections.emptyList()); + } + + @Override + public List getProperties() { + return Arrays.asList( + new ElementPropertyMetaDataDefault( + "value", this, "test", String.class, true), + new ElementPropertyMetaDataDefault( + "novalue", this, "test", String.class, true), + new ElementPropertyMetaDataDefault( + "javascript", this, "test", String.class, true)); + } + + @Override + protected void managedResourcesCleanup() { + } + + @Override + protected void unmanagedResourcesCleanup() { + } + } +} From 191ae04ac7f6be9c4d9a205fe3c3bd3b93effd44 Mon Sep 17 00:00:00 2001 From: James Rosewell Date: Sun, 15 Nov 2026 22:58:35 +0000 Subject: [PATCH 2/5] Test: the script is minified, or not, as configured, and is minified by default The JavaScript Builder specification says "Finally, the resulting JavaScript will be minified (or not, based on the configuration provided). Minification SHOULD be performed using an existing third-party library." (pipeline-specification/pipeline-elements/javascript-builder.md lines 105 to 107 at 51Degrees/specifications 706de1f0), and its configuration options give SetMinify with the default True (line 350). The tests check that the script is minified with no setting and with minification on, is the template as written with it off, carries the same JSON, callback URL, parameters, session id and object name either way, and parses and runs when minified. --- .../JavaScriptMinifyTests.java | 313 ++++++++++++++++++ 1 file changed, 313 insertions(+) create mode 100644 pipeline/pipeline.javascriptbuilder/src/test/java/fiftyone/pipeline/javascriptbuilder/JavaScriptMinifyTests.java diff --git a/pipeline/pipeline.javascriptbuilder/src/test/java/fiftyone/pipeline/javascriptbuilder/JavaScriptMinifyTests.java b/pipeline/pipeline.javascriptbuilder/src/test/java/fiftyone/pipeline/javascriptbuilder/JavaScriptMinifyTests.java new file mode 100644 index 000000000..6cc83a269 --- /dev/null +++ b/pipeline/pipeline.javascriptbuilder/src/test/java/fiftyone/pipeline/javascriptbuilder/JavaScriptMinifyTests.java @@ -0,0 +1,313 @@ +/* ********************************************************************* + * This Original Work is copyright of 51 Degrees Mobile Experts Limited. + * Copyright 2026 51 Degrees Mobile Experts Limited, Davidson House, + * Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU. + * + * This Original Work is licensed under the European Union Public Licence + * (EUPL) v.1.2 and is subject to its terms as set out below. + * + * If a copy of the EUPL was not distributed with this file, You can obtain + * one at https://opensource.org/licenses/EUPL-1.2. + * + * The 'Compatible Licences' set out in the Appendix to the EUPL (as may be + * amended by the European Commission) shall be deemed incompatible for + * the purposes of the Work and the provisions of the compatibility + * clause in Article 5 of the EUPL shall not apply. + * + * If using the Work as, or as part of, a network application, by + * including the attribution notice(s) required under Article 5 of the EUPL + * in the end user terms of the application under an appropriate heading, + * such notice(s) shall fulfill the requirements of that article. + * ********************************************************************* */ + +package fiftyone.pipeline.javascriptbuilder; + +import fiftyone.common.testhelpers.TestLoggerFactory; +import fiftyone.pipeline.core.data.ElementDataBase; +import fiftyone.pipeline.core.data.ElementPropertyMetaData; +import fiftyone.pipeline.core.data.ElementPropertyMetaDataDefault; +import fiftyone.pipeline.core.data.EvidenceKeyFilter; +import fiftyone.pipeline.core.data.EvidenceKeyFilterWhitelist; +import fiftyone.pipeline.core.data.FlowData; +import fiftyone.pipeline.core.flowelements.FlowElementBase; +import fiftyone.pipeline.core.flowelements.Pipeline; +import fiftyone.pipeline.core.flowelements.PipelineBuilder; +import fiftyone.pipeline.engines.fiftyone.flowelements.SequenceElementBuilder; +import fiftyone.pipeline.javascriptbuilder.flowelements.JavaScriptBuilderElement; +import fiftyone.pipeline.javascriptbuilder.flowelements.JavaScriptBuilderElementBuilder; +import fiftyone.pipeline.jsonbuilder.flowelements.JsonBuilderElementBuilder; +import org.graalvm.polyglot.Context; +import org.graalvm.polyglot.PolyglotException; +import org.graalvm.polyglot.Source; +import org.graalvm.polyglot.Value; +import org.json.JSONObject; +import org.json.JSONTokener; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + +/** + * The script is minified, or not, as configured, and minification is on + * unless it is turned off. See the Process section and the SetMinify + * configuration option of the JavaScript Builder specification. + * @see Specification + */ +public class JavaScriptMinifyTests { + + /** + * A comment in the template, which a minified script does not carry. + */ + private static final String TEMPLATE_COMMENT = + "// Log any errors returned in the JSON object."; + + /** + * A value that looks like code a minifier would change, and holds + * quotes and a line break, so it shows whether minification has changed + * the data the script carries. + */ + private static final String VALUE = + "a /* not a comment */ b // nor this\n\t'c' \"d\" {{_objName}}"; + + private static final String BROWSER = + "var window = globalThis;\n" + + "var console = { log: function() {}, warn: function() {},\n" + + " error: function() {} };\n" + + "var document = { cookie: '' };\n" + + "var sessionStorage = (function() {\n" + + " var data = {};\n" + + " return {\n" + + " get length() { return Object.keys(data).length; },\n" + + " key: function(i) {\n" + + " var k = Object.keys(data)[i];\n" + + " return k === undefined ? null : k; },\n" + + " getItem: function(k) {\n" + + " return k in data ? data[k] : null; },\n" + + " setItem: function(k, v) { data[k] = String(v); },\n" + + " removeItem: function(k) { delete data[k]; }\n" + + " };\n" + + "})();\n"; + + private final TestLoggerFactory loggerFactory = new TestLoggerFactory(null); + + /** + * Renders the script for one request. + * @param minify null to leave the builder's default, otherwise the + * value given to setMinify + * @param endpoint the endpoint, or an empty string for no callback + * @param cookies the value given to setEnableCookies + */ + private String render(Boolean minify, String endpoint, boolean cookies) + throws Exception { + JavaScriptBuilderElementBuilder builder = + new JavaScriptBuilderElementBuilder(loggerFactory) + .setEndpoint(endpoint) + .setHost("example.com") + .setProtocol("https") + .setEnableCookies(cookies); + if (minify != null) { + builder.setMinify(minify); + } + JavaScriptBuilderElement jsElement = builder.build(); + try (Pipeline pipeline = new PipelineBuilder(loggerFactory) + .addFlowElement(new SequenceElementBuilder(loggerFactory).build()) + .addFlowElement(new ValuesElement()) + .addFlowElement(new JsonBuilderElementBuilder(loggerFactory).build()) + .addFlowElement(jsElement) + .build(); + FlowData flowData = pipeline.createFlowData()) { + flowData.addEvidence("query.some-value", "x y"); + flowData.process(); + return flowData.getFromElement(jsElement).getJavaScript(); + } + } + + /** + * With no setting the script is minified, so it carries none of the + * template's comments and is much shorter than the script made with + * minification turned off. + */ + @Test + public void Minify_Default_ScriptIsMinified() throws Exception { + String minified = render(null, "/json", true); + String asWritten = render(false, "/json", true); + + assertFalse(minified.contains(TEMPLATE_COMMENT), + "The default script still carries the template's comments."); + assertTrue(minified.length() < asWritten.length() * 3 / 4, + "The default script is " + minified.length() + + " characters and the script with minification off is " + + asWritten.length() + "."); + } + + /** + * With minification turned on the script is minified as it is with no + * setting. + */ + @Test + public void Minify_On_ScriptIsMinified() throws Exception { + assertEquals(render(null, "/json", true), render(true, "/json", true)); + } + + /** + * With minification turned off the script is the template as written. + */ + @Test + public void Minify_Off_ScriptIsNotMinified() throws Exception { + assertTrue(render(false, "/json", true).contains(TEMPLATE_COMMENT)); + } + + /** + * Minification changes the code and not what the script carries: the + * JSON, the callback URL, the parameters, the session id, the sequence + * and the object name are the same as in the script made with + * minification turned off. + */ + @Test + public void Minify_On_ValuesUnchanged() throws Exception { + String minified = render(true, "/json", true); + String asWritten = render(false, "/json", true); + + assertTrue(json(asWritten).similar(json(minified)), + "The JSON differs.\n" + json(asWritten).toString(2) + "\n" + + json(minified).toString(2)); + assertEquals(VALUE, json(minified).getJSONObject("values") + .getString("text")); + assertTrue(minified.contains("'https://example.com/json'"), + "The callback URL is missing."); + assertEquals( + parameters(asWritten).toString(), + parameters(minified).toString()); + assertTrue(minified.contains("\"" + sessionId(asWritten) + "\""), + "The session id is missing."); + assertTrue(Pattern.compile("var fod\\s*=\\s*new fiftyoneDegreesManager") + .matcher(minified).find(), "The object is not created."); + } + + /** + * Every form of the minified script parses, whether it has a callback + * and whether cookies are enabled. + */ + @ParameterizedTest + @CsvSource({"/json, true", "/json, false", "'', true", "'', false"}) + public void Minify_On_EveryFormParses(String endpoint, boolean cookies) + throws Exception { + String script = render(true, endpoint, cookies); + try (Context context = context()) { + try { + context.parse(Source.create("js", script)); + } catch (PolyglotException e) { + fail("The minified script does not parse: " + e.getMessage()); + } + } + } + + /** + * The minified script runs and creates the object with the values from + * the JSON. + */ + @Test + public void Minify_On_ScriptRuns() throws Exception { + String script = render(true, "", true); + try (Context context = context()) { + context.eval("js", BROWSER); + context.eval(Source.create("js", script)); + Value fod = context.eval("js", "window").getMember("fod"); + assertFalse(fod == null || fod.isNull(), "fod was not created."); + assertTrue(fod.getMember("complete").canExecute()); + assertEquals(VALUE, + fod.getMember("values").getMember("text").asString()); + } + } + + private static Context context() { + return Context.newBuilder("js") + .option("engine.WarnInterpreterOnly", "false") + .build(); + } + + /** + * The JSON the script assigns to its json variable. + */ + private static JSONObject json(String script) { + Matcher matcher = Pattern.compile("var json\\s*=\\s*").matcher(script); + assertTrue(matcher.find(), "The script holds no JSON."); + return (JSONObject) new JSONTokener(script.substring(matcher.end())) + .nextValue(); + } + + /** + * The parameters object the script's request parameters function + * returns. + */ + private static JSONObject parameters(String script) { + Matcher matcher = Pattern.compile( + "renderedParameters\\s*=\\s*function\\(\\)\\s*\\{\\s*return\\s*") + .matcher(script); + assertTrue(matcher.find(), "The script holds no parameters."); + return (JSONObject) new JSONTokener(script.substring(matcher.end())) + .nextValue(); + } + + private static String sessionId(String script) { + Matcher matcher = Pattern.compile("var sessionId\\s*=\\s*\"([^\"]*)\"") + .matcher(script); + assertTrue(matcher.find(), "The script holds no session id."); + return matcher.group(1); + } + + /** + * An element that adds a value which looks like code, in place of an + * engine. + */ + private static class ValuesElement + extends FlowElementBase { + + ValuesElement() { + super(new TestLoggerFactory(null).getLogger("values"), null); + } + + @Override + protected void processInternal(FlowData data) { + ElementDataBase values = new ElementDataBase(logger, data) { }; + values.put("text", VALUE); + data.getOrAdd(getElementDataKey(), d -> values); + } + + @Override + public String getElementDataKey() { + return "values"; + } + + @Override + public EvidenceKeyFilter getEvidenceKeyFilter() { + return new EvidenceKeyFilterWhitelist( + Collections.emptyList()); + } + + @Override + public List getProperties() { + return Arrays.asList( + new ElementPropertyMetaDataDefault( + "text", this, "test", String.class, true)); + } + + @Override + protected void managedResourcesCleanup() { + } + + @Override + protected void unmanagedResourcesCleanup() { + } + } +} From 6c9161b3bad00875f41222f27b9905e26a7b2647 Mon Sep 17 00:00:00 2001 From: James Rosewell Date: Sun, 15 Nov 2026 23:11:49 +0000 Subject: [PATCH 3/5] Fix: minify the JavaScript with a third-party library, on unless configured off The JavaScript Builder specification says "Finally, the resulting JavaScript will be minified (or not, based on the configuration provided). Minification SHOULD be performed using an existing third-party library." (pipeline-specification/pipeline-elements/javascript-builder.md lines 105 to 107 at 51Degrees/specifications 706de1f0), with SetMinify defaulting to True (line 350). The builder has setMinify, true by default, and the element a constructor that takes it. The other constructors minify. The script is minified with net.logicsquad:minifier, a Java 8 port of JSMin, without the log4j binding its pom names, which would take over an application's logging. Minifying is done once for each combination of the template's sections, with a placeholder for each value, and kept. Each request's values are put in place of the placeholders, so minifying costs almost nothing per request and the values the script carries never pass through the minifier. If minifying fails it is logged once and the script is sent as written. The minify tests now give each render the same session, as two requests otherwise have different session ids. Tests that read the script line by line build it unminified, and the others match with or without the white space minifying takes out. --- pipeline/pipeline.javascriptbuilder/pom.xml | 15 ++ .../JavaScriptBuilderElement.java | 171 +++++++++++++++++- .../JavaScriptBuilderElementBuilder.java | 26 ++- .../JavaScriptBuilderTests.java | 6 + .../JavaScriptMinifyTests.java | 4 + .../JavaScriptObjectNameTests.java | 48 +++-- .../javascriptbuilder/JavaScriptUrlTests.java | 9 +- .../SessionIdAndSequenceTests.java | 7 +- .../web/ClientsideJsonEndpointTests.java | 14 +- pipeline/pom.xml | 1 + 10 files changed, 266 insertions(+), 35 deletions(-) diff --git a/pipeline/pipeline.javascriptbuilder/pom.xml b/pipeline/pipeline.javascriptbuilder/pom.xml index 7a7a03bf8..73a9e1f19 100644 --- a/pipeline/pipeline.javascriptbuilder/pom.xml +++ b/pipeline/pipeline.javascriptbuilder/pom.xml @@ -53,6 +53,21 @@ compiler ${mustache-compiler.version} + + + net.logicsquad + minifier + ${minifier.version} + + + org.apache.logging.log4j + log4j-slf4j-impl + + + org.graalvm.js js-scriptengine diff --git a/pipeline/pipeline.javascriptbuilder/src/main/java/fiftyone/pipeline/javascriptbuilder/flowelements/JavaScriptBuilderElement.java b/pipeline/pipeline.javascriptbuilder/src/main/java/fiftyone/pipeline/javascriptbuilder/flowelements/JavaScriptBuilderElement.java index 30e7ffbf2..da5e9c6a2 100644 --- a/pipeline/pipeline.javascriptbuilder/src/main/java/fiftyone/pipeline/javascriptbuilder/flowelements/JavaScriptBuilderElement.java +++ b/pipeline/pipeline.javascriptbuilder/src/main/java/fiftyone/pipeline/javascriptbuilder/flowelements/JavaScriptBuilderElement.java @@ -38,13 +38,16 @@ import fiftyone.pipeline.javascriptbuilder.data.JavaScriptBuilderData; import fiftyone.pipeline.javascriptbuilder.templates.JavaScriptResource; import fiftyone.pipeline.jsonbuilder.data.JsonBuilderData; +import net.logicsquad.minifier.js.JSMinifier; import org.json.JSONObject; import org.slf4j.Logger; import java.io.*; import java.net.URLEncoder; import java.util.*; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.regex.Matcher; import java.util.regex.Pattern; import static fiftyone.pipeline.core.Constants.*; @@ -69,8 +72,50 @@ public class JavaScriptBuilderElement protected String contextRoot; protected final String objName; protected final boolean enableCookies; + protected final boolean minify; private final Mustache mustache; + /** + * The minified forms of the template, one for each combination of its + * sections, with a placeholder where each value goes. Minifying is done + * once for each form and not for each request, so minification costs + * almost nothing per request, and the values the script carries are + * never passed through the minifier, so it cannot change them. + */ + private final ConcurrentHashMap minifiedForms = + new ConcurrentHashMap<>(); + + /** + * Set once minification has failed and been logged, after which the + * script is not minified. + */ + private final AtomicBoolean minifyFailed = new AtomicBoolean(false); + + /** + * The text each value is given when the template is minified. Each is + * an identifier, so the minifier leaves it as it is, whether it is in + * code or in a string. + */ + private static final String PLACEHOLDER_PREFIX = + "fiftyoneDegreesMinifyPlaceholder"; + + /** + * The values of the template that differ for each request, in the + * order of {@link #PLACEHOLDER_NAMES}. + */ + private static final String[] VALUE_KEYS = { + "_objName", "_jsonObject", "_sessionId", "_sequence", "_url", + "_parameters"}; + + /** + * The placeholder for each of {@link #VALUE_KEYS}. + */ + private static final String[] PLACEHOLDER_NAMES = { + "ObjName", "Json", "SessionId", "Sequence", "Url", "Parameters"}; + + private static final Pattern PLACEHOLDER_PATTERN = Pattern.compile( + PLACEHOLDER_PREFIX + "(" + String.join("|", PLACEHOLDER_NAMES) + ")"); + /** * Set once a callback URL that is not valid has been logged, because a * misconfigured element would otherwise log on every request. @@ -205,6 +250,48 @@ public JavaScriptBuilderElement( String host, String protocol, String contextRoot) { + this(logger, elementDataFactory, endpoint, objName, enableCookies, + host, protocol, contextRoot, true); + } + //! [constructor] + + //! [constructor] + /** + * Constructor. + * @param logger The logger. + * @param elementDataFactory The element data factory. + * @param endpoint Set the endpoint which will be queried on the host. + * e.g /api/v4/json + * @param objName The default name of the object instantiated by the client + * JavaScript. + * @param enableCookies Set whether the client JavaScript stored results of + * client side processing in cookies. + * @param host The host that the client JavaScript should query for updates. + * If null or blank then the host from the request will be used + * @param protocol The protocol (HTTP or HTTPS) that the client JavaScript + * will use when querying for updates. If null or empty + * then the protocol from the request will be used + * @param contextRoot The <context-root> setting from the web.xml. + * This is needed when creating the callback URL. + * @param minify True to minify the JavaScript that is produced. The other + * constructors minify it. + * @throws PipelineConfigurationException if protocol is not null or empty + * and is neither http nor https in any case of its ASCII letters, white + * space included, or if objName is not null and is + * not a valid JavaScript identifier, see {@link #isValidObjectName}. + * A null name means the default name is used, as no name was + * configured, and an empty name is refused + */ + public JavaScriptBuilderElement( + Logger logger, + ElementDataFactory elementDataFactory, + String endpoint, + String objName, + boolean enableCookies, + String host, + String protocol, + String contextRoot, + boolean minify) { super(logger, elementDataFactory); MustacheFactory mf = new DefaultMustacheFactory(); @@ -232,6 +319,7 @@ public JavaScriptBuilderElement( } this.enableCookies = enableCookies; this.contextRoot = contextRoot; + this.minify = minify; } //! [constructor] @@ -619,13 +707,88 @@ private void buildJavaScript( cookies, updateEnabled, hasDelayedProperties); - - StringWriter stringWriter = new StringWriter(); - mustache.execute(stringWriter, javaScriptObj.asMap()); - String content = stringWriter.toString(); + Map values = javaScriptObj.asMap(); + String content = null; + if (minify) { + content = renderMinified(values); + } + if (content == null) { + content = render(values); + } elementData.setJavaScript(content); } + + /** + * Renders the template as written. + */ + private String render(Map values) { + StringWriter stringWriter = new StringWriter(); + mustache.execute(stringWriter, values); + return stringWriter.toString(); + } + + /** + * Renders the minified template. The minified form of the template for + * the request's sections is made once and kept, and the request's values + * are put in place of its placeholders. + * @return the minified script, or null if minification failed + */ + private String renderMinified(Map values) { + if (minifyFailed.get()) { + return null; + } + // The form depends only on the template's sections, being every + // value that is not one of the placeholders. The URL is a value, and + // whether it is present is the section _updateEnabled. + int key = 0; + for (String section : new String[] { + "_supportsPromises", "_enableCookies", "_updateEnabled", + "_hasDelayedProperties"}) { + key = (key << 1) | (Boolean.TRUE.equals(values.get(section)) ? 1 : 0); + } + String form = minifiedForms.computeIfAbsent(key, k -> minifyForm(values)); + if (form.isEmpty()) { + return null; + } + + Matcher matcher = PLACEHOLDER_PATTERN.matcher(form); + StringBuffer script = new StringBuffer(form.length() + + String.valueOf(values.get("_jsonObject")).length()); + while (matcher.find()) { + String valueKey = VALUE_KEYS[ + Arrays.asList(PLACEHOLDER_NAMES).indexOf(matcher.group(1))]; + matcher.appendReplacement(script, + Matcher.quoteReplacement(String.valueOf(values.get(valueKey)))); + } + matcher.appendTail(script); + return script.toString(); + } + + /** + * Renders the template with the given sections and a placeholder for + * each value, and minifies it. + * @return the minified form, or an empty string if minification failed + */ + private String minifyForm(Map values) { + Map placeholders = new HashMap<>(values); + for (int i = 0; i < VALUE_KEYS.length; i++) { + placeholders.put(VALUE_KEYS[i], + PLACEHOLDER_PREFIX + PLACEHOLDER_NAMES[i]); + } + try { + StringWriter minified = new StringWriter(); + new JSMinifier(new StringReader(render(placeholders))) + .minify(minified); + return minified.toString(); + } catch (Exception e) { + if (minifyFailed.compareAndSet(false, true)) { + logger.error("The JavaScript could not be minified, so it " + + "is sent as it is written.", e); + } + return ""; + } + } } //! [class] diff --git a/pipeline/pipeline.javascriptbuilder/src/main/java/fiftyone/pipeline/javascriptbuilder/flowelements/JavaScriptBuilderElementBuilder.java b/pipeline/pipeline.javascriptbuilder/src/main/java/fiftyone/pipeline/javascriptbuilder/flowelements/JavaScriptBuilderElementBuilder.java index 47a449373..89d3bcb8a 100644 --- a/pipeline/pipeline.javascriptbuilder/src/main/java/fiftyone/pipeline/javascriptbuilder/flowelements/JavaScriptBuilderElementBuilder.java +++ b/pipeline/pipeline.javascriptbuilder/src/main/java/fiftyone/pipeline/javascriptbuilder/flowelements/JavaScriptBuilderElementBuilder.java @@ -40,6 +40,10 @@ @ElementBuilder public class JavaScriptBuilderElementBuilder { public static final boolean ENABLE_COOKIES = true; + /** + * Whether the script is minified when nothing else is configured. + */ + public static final boolean MINIFY = true; private final ILoggerFactory loggerFactory; private final Logger logger; @@ -49,6 +53,7 @@ public class JavaScriptBuilderElementBuilder { protected String contextRoot = ""; protected String objName = Constants.DEFAULT_OBJECT_NAME; private boolean enableCookies = ENABLE_COOKIES; + private boolean minify = MINIFY; /** * Construct a new instance. @@ -171,6 +176,22 @@ public JavaScriptBuilderElementBuilder setEnableCookies( return this; } + /** + * Set whether the JavaScript that is produced is minified. Minifying + * takes out the template's comments and white space, so the script is + * smaller to send. The values the script carries, such as the JSON, are + * not changed. + *

+ * Default is true + * @param minify true to minify the JavaScript + * @return this builder + */ + @DefaultValue(booleanValue = MINIFY) + public JavaScriptBuilderElementBuilder setMinify(boolean minify) { + this.minify = minify; + return this; + } + /** * Build the {@link JavaScriptBuilderElement} * @return new {@link JavaScriptBuilderElement} instance @@ -194,7 +215,8 @@ public JavaScriptBuilderData create( enableCookies, host, protocol, - contextRoot); - } + contextRoot, + minify); + } } //! [class] diff --git a/pipeline/pipeline.javascriptbuilder/src/test/java/fiftyone/pipeline/javascriptbuilder/JavaScriptBuilderTests.java b/pipeline/pipeline.javascriptbuilder/src/test/java/fiftyone/pipeline/javascriptbuilder/JavaScriptBuilderTests.java index 7ec0c95e1..05fc697cc 100644 --- a/pipeline/pipeline.javascriptbuilder/src/test/java/fiftyone/pipeline/javascriptbuilder/JavaScriptBuilderTests.java +++ b/pipeline/pipeline.javascriptbuilder/src/test/java/fiftyone/pipeline/javascriptbuilder/JavaScriptBuilderTests.java @@ -256,8 +256,10 @@ public void JavaScriptBuilderElement_DelayExecution() throws Exception { public void JavaScriptBuilderElement_VerifySession() throws Exception { configureMocks(); + // Not minified, so the declarations read as the template writes them. javaScriptBuilderElement = new JavaScriptBuilderElementBuilder(loggerFactory) + .setMinify(false) .build(); javaScriptBuilderElement.process(flowData); @@ -326,8 +328,10 @@ public void JavaScriptBuilderElement_ParametersExcludeSessionAndSequence() throws Exception { configureMocks(); + // Not minified, so the script is read line by line as written. javaScriptBuilderElement = new JavaScriptBuilderElementBuilder(loggerFactory) + .setMinify(false) .build(); javaScriptBuilderElement.process(flowData); String javaScript = result.getJavaScript(); @@ -378,9 +382,11 @@ public void JavaScriptBuilderElement_NoParameters() throws Exception { evidence.remove("query.latitude"); evidence.remove("query.longitude"); + // Not minified, so the script is read line by line as written. javaScriptBuilderElement = new JavaScriptBuilderElementBuilder(loggerFactory) .setEndpoint("/json") + .setMinify(false) .build(); javaScriptBuilderElement.process(flowData); diff --git a/pipeline/pipeline.javascriptbuilder/src/test/java/fiftyone/pipeline/javascriptbuilder/JavaScriptMinifyTests.java b/pipeline/pipeline.javascriptbuilder/src/test/java/fiftyone/pipeline/javascriptbuilder/JavaScriptMinifyTests.java index 6cc83a269..606d91dfb 100644 --- a/pipeline/pipeline.javascriptbuilder/src/test/java/fiftyone/pipeline/javascriptbuilder/JavaScriptMinifyTests.java +++ b/pipeline/pipeline.javascriptbuilder/src/test/java/fiftyone/pipeline/javascriptbuilder/JavaScriptMinifyTests.java @@ -127,6 +127,10 @@ private String render(Boolean minify, String endpoint, boolean cookies) .build(); FlowData flowData = pipeline.createFlowData()) { flowData.addEvidence("query.some-value", "x y"); + // The same session for every render, so two renders differ only + // in whether they are minified. + flowData.addEvidence("query.session-id", "abc-123"); + flowData.addEvidence("query.sequence", "1"); flowData.process(); return flowData.getFromElement(jsElement).getJavaScript(); } diff --git a/pipeline/pipeline.javascriptbuilder/src/test/java/fiftyone/pipeline/javascriptbuilder/JavaScriptObjectNameTests.java b/pipeline/pipeline.javascriptbuilder/src/test/java/fiftyone/pipeline/javascriptbuilder/JavaScriptObjectNameTests.java index 712bc6873..119ab8149 100644 --- a/pipeline/pipeline.javascriptbuilder/src/test/java/fiftyone/pipeline/javascriptbuilder/JavaScriptObjectNameTests.java +++ b/pipeline/pipeline.javascriptbuilder/src/test/java/fiftyone/pipeline/javascriptbuilder/JavaScriptObjectNameTests.java @@ -316,7 +316,9 @@ public void ObjectName_WindowPropertyVarCanReplace_Allowed(String name) { String script = render(element); - assertTrue(script.contains("var " + name + " = new"), + assertTrue(Pattern.compile( + "var " + Pattern.quote(name) + "\\s*=\\s*new") + .matcher(script).find(), "the script should declare '" + name + "'"); assertEquals(0, warnings().size()); } @@ -356,16 +358,20 @@ public void ObjectName_NotConfigured_DefaultUsed() { } private void assertWorkingObject(String script, String name) { + // The script is minified by default, so the patterns allow for the + // white space around '=' being taken out. assertTrue(Pattern.compile( "var " + Pattern.quote(name) + - " = new fiftyoneDegreesManager\\(\\);") + "\\s*=\\s*new fiftyoneDegreesManager\\(\\);") .matcher(script).find(), "the script should declare var " + name); if (name.equals("fod") == false) { assertFalse(Pattern.compile("var fod\\b").matcher(script).find(), "the script should not declare var fod"); } - assertTrue(script.contains("var sessionKey = \"" + name + "\";"), + assertTrue(Pattern.compile( + "var sessionKey\\s*=\\s*\"" + Pattern.quote(name) + "\";") + .matcher(script).find(), "the storage key should be " + name); assertTrue(script.contains("window[\"" + name + "Evidence\"]"), "the evidence lookup should use " + name); @@ -406,25 +412,27 @@ private List warnings() { } /** - * The script with the line that assigns its request parameters taken - * out. The script sends the page's query evidence, the requested object - * name included, back with its own request. The builder url encodes - * each value and the line holds them as JSON strings, so the requested - * name there is data and not code, and it is left out of the checks - * that the name was not written into the code. The line is found as the - * existing tests find it, by the word and a brace, because the template - * has named the declaration differently over time. + * The script with the object holding its request parameters taken out. + * The script sends the page's query evidence, the requested object name + * included, back with its own request. The builder url encodes each + * value, so no value holds a brace, and the object holds them as JSON + * strings, so the requested name there is data and not code, and it is + * left out of the checks that the name was not written into the code. + * The object is found by the word parameters and a brace, because the + * template has named the declaration differently over time, and not by + * line, because the script is minified by default. */ + private static final Pattern PARAMETERS = Pattern.compile( + "(?i)\\w*parameters\\s*=\\s*" + + "(?:function\\s*\\(\\)\\s*\\{\\s*return\\s*)?\\{[^}]*\\}"); + private static String withoutParameters(String script) { - StringBuilder rest = new StringBuilder(); - for (String line : script.split("\\r?\\n")) { - if (line.toLowerCase().contains("parameters =") && - line.contains("{")) { - continue; - } - rest.append(line).append('\n'); - } - return rest.toString(); + Matcher matcher = PARAMETERS.matcher(script); + assertTrue(matcher.find(), "the script should assign its parameters"); + String rest = matcher.replaceFirst(""); + assertFalse(PARAMETERS.matcher(rest).find(), + "the script should assign its parameters once"); + return rest; } private static int count(String text, String find) { diff --git a/pipeline/pipeline.javascriptbuilder/src/test/java/fiftyone/pipeline/javascriptbuilder/JavaScriptUrlTests.java b/pipeline/pipeline.javascriptbuilder/src/test/java/fiftyone/pipeline/javascriptbuilder/JavaScriptUrlTests.java index fc8cb2f61..ca316fb58 100644 --- a/pipeline/pipeline.javascriptbuilder/src/test/java/fiftyone/pipeline/javascriptbuilder/JavaScriptUrlTests.java +++ b/pipeline/pipeline.javascriptbuilder/src/test/java/fiftyone/pipeline/javascriptbuilder/JavaScriptUrlTests.java @@ -55,7 +55,9 @@ /** * Checks the callback URL the rendered script is given. These tests render - * the script only and need no browser. + * the script only and need no browser. The script is not minified, so it is + * read as the template writes it. {@link JavaScriptMinifyTests} checks that + * the minified script carries the same values. */ public class JavaScriptUrlTests { private final TestLoggerFactory loggerFactory; @@ -72,7 +74,8 @@ private String render(String contextRoot) throws Exception { new JavaScriptBuilderElementBuilder(loggerFactory) .setEndpoint("/json") .setHost("example.com") - .setProtocol("https"); + .setProtocol("https") + .setMinify(false); if (contextRoot != null) { builder.setContextRoot(contextRoot); } @@ -168,6 +171,7 @@ private List renderWithHosts(String... hosts) throws Exception { JavaScriptBuilderElement jsElement = new JavaScriptBuilderElementBuilder(factory) .setEndpoint("/json") + .setMinify(false) .build(); List scripts = new ArrayList<>(); try (Pipeline pipeline = new PipelineBuilder(factory) @@ -270,6 +274,7 @@ public void Url_RefusedProtocol_NoCallbackAndLoggedOnce() throws Exception { JavaScriptBuilderElement jsElement = new JavaScriptBuilderElementBuilder(factory) .setEndpoint("/json") + .setMinify(false) .build(); try (Pipeline pipeline = new PipelineBuilder(factory) .addFlowElement(new SequenceElementBuilder(factory).build()) diff --git a/pipeline/pipeline.javascriptbuilder/src/test/java/fiftyone/pipeline/javascriptbuilder/SessionIdAndSequenceTests.java b/pipeline/pipeline.javascriptbuilder/src/test/java/fiftyone/pipeline/javascriptbuilder/SessionIdAndSequenceTests.java index 06bc9b6b6..e5628a3d6 100644 --- a/pipeline/pipeline.javascriptbuilder/src/test/java/fiftyone/pipeline/javascriptbuilder/SessionIdAndSequenceTests.java +++ b/pipeline/pipeline.javascriptbuilder/src/test/java/fiftyone/pipeline/javascriptbuilder/SessionIdAndSequenceTests.java @@ -76,11 +76,14 @@ */ public class SessionIdAndSequenceTests { + // The script is minified by default, so the patterns allow for the + // white space around '=' being taken out and for the rest of the + // script following on the same line. private static final Pattern SESSION_ID_LINE = - Pattern.compile("var sessionId = \"(.*)\";"); + Pattern.compile("var sessionId\\s*=\\s*\"([^\"]*)\";"); private static final Pattern SEQUENCE_LINE = - Pattern.compile("var sequence = (.*);"); + Pattern.compile("var sequence\\s*=\\s*([^;]*);"); private final TestLoggerFactory loggerFactory = new TestLoggerFactory(null); diff --git a/pipeline/pipeline.web.packages/pipeline.web/src/test/java/fiftyone/pipeline/web/ClientsideJsonEndpointTests.java b/pipeline/pipeline.web.packages/pipeline.web/src/test/java/fiftyone/pipeline/web/ClientsideJsonEndpointTests.java index 583ac565e..082ddf9ac 100644 --- a/pipeline/pipeline.web.packages/pipeline.web/src/test/java/fiftyone/pipeline/web/ClientsideJsonEndpointTests.java +++ b/pipeline/pipeline.web.packages/pipeline.web/src/test/java/fiftyone/pipeline/web/ClientsideJsonEndpointTests.java @@ -51,6 +51,8 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -69,9 +71,11 @@ public class ClientsideJsonEndpointTests { LoggerFactory.getILoggerFactory(); /** - * The text that the template puts before the JSON in the script. + * The text that the template puts before the JSON in the script, with + * or without the white space that minifying takes out. */ - private static final String JSON_IN_SCRIPT = "var json = "; + private static final Pattern JSON_IN_SCRIPT = + Pattern.compile("var json\\s*=\\s*"); /** * The JSON served by the JSON endpoint and the JSON the script served by @@ -125,10 +129,10 @@ public void JsonEndpoint_SameFormAsJsonInJavaScript() throws Exception { * The JSON object the script assigns to its json variable. */ private static JSONObject jsonInScript(String javaScript) { - int start = javaScript.indexOf(JSON_IN_SCRIPT); - assertTrue("The script holds no JSON: " + javaScript, start >= 0); + Matcher matcher = JSON_IN_SCRIPT.matcher(javaScript); + assertTrue("The script holds no JSON: " + javaScript, matcher.find()); return (JSONObject) new JSONTokener( - javaScript.substring(start + JSON_IN_SCRIPT.length())) + javaScript.substring(matcher.end())) .nextValue(); } diff --git a/pipeline/pom.xml b/pipeline/pom.xml index 533f13e51..bd4a7d7e5 100644 --- a/pipeline/pom.xml +++ b/pipeline/pom.xml @@ -80,6 +80,7 @@ 3.0.2 0.9.14 + 1.0 22.0.0.2 22.0.0.2 From 31d4db3d35afd87b326155997b849df1df459364 Mon Sep 17 00:00:00 2001 From: James Rosewell Date: Sun, 15 Nov 2026 23:16:57 +0000 Subject: [PATCH 4/5] Test: a performance test measures what minifying the JavaScript costs The JavaScript Builder specification says "Performance tests MUST be in place to verify the performance impact of this feature." (pipeline-specification/pipeline-elements/javascript-builder.md line 109 at 51Degrees/specifications 706de1f0), the feature being minification. The test renders with minification on and off in turn, 2000 times each after a warm up, prints the times, and fails if a minified render takes more than twice as long as one not minified. Minifying the template on every request, in place of once for each form, makes a minified render several times slower, and this test then fails. --- .../JavaScriptMinifyPerformanceTests.java | 140 ++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 pipeline/pipeline.javascriptbuilder/src/test/java/fiftyone/pipeline/javascriptbuilder/JavaScriptMinifyPerformanceTests.java diff --git a/pipeline/pipeline.javascriptbuilder/src/test/java/fiftyone/pipeline/javascriptbuilder/JavaScriptMinifyPerformanceTests.java b/pipeline/pipeline.javascriptbuilder/src/test/java/fiftyone/pipeline/javascriptbuilder/JavaScriptMinifyPerformanceTests.java new file mode 100644 index 000000000..cdece3b1c --- /dev/null +++ b/pipeline/pipeline.javascriptbuilder/src/test/java/fiftyone/pipeline/javascriptbuilder/JavaScriptMinifyPerformanceTests.java @@ -0,0 +1,140 @@ +/* ********************************************************************* + * This Original Work is copyright of 51 Degrees Mobile Experts Limited. + * Copyright 2026 51 Degrees Mobile Experts Limited, Davidson House, + * Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU. + * + * This Original Work is licensed under the European Union Public Licence + * (EUPL) v.1.2 and is subject to its terms as set out below. + * + * If a copy of the EUPL was not distributed with this file, You can obtain + * one at https://opensource.org/licenses/EUPL-1.2. + * + * The 'Compatible Licences' set out in the Appendix to the EUPL (as may be + * amended by the European Commission) shall be deemed incompatible for + * the purposes of the Work and the provisions of the compatibility + * clause in Article 5 of the EUPL shall not apply. + * + * If using the Work as, or as part of, a network application, by + * including the attribution notice(s) required under Article 5 of the EUPL + * in the end user terms of the application under an appropriate heading, + * such notice(s) shall fulfill the requirements of that article. + * ********************************************************************* */ + +package fiftyone.pipeline.javascriptbuilder; + +import fiftyone.common.testhelpers.TestLoggerFactory; +import fiftyone.pipeline.core.data.FlowData; +import fiftyone.pipeline.core.flowelements.Pipeline; +import fiftyone.pipeline.core.flowelements.PipelineBuilder; +import fiftyone.pipeline.engines.fiftyone.flowelements.SequenceElementBuilder; +import fiftyone.pipeline.javascriptbuilder.flowelements.JavaScriptBuilderElement; +import fiftyone.pipeline.javascriptbuilder.flowelements.JavaScriptBuilderElementBuilder; +import fiftyone.pipeline.jsonbuilder.flowelements.JsonBuilderElementBuilder; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Measures what minification costs, as the JavaScript Builder specification + * asks of the feature, because a slow minification library once caused a + * customer problems. + *

+ * The element with minification on and the element with it off render in + * turn, many times, so a busy machine slows both alike, and the test fails + * only when minifying makes rendering much slower. The times are printed so + * a change in the cost can be seen. + * @see Specification + */ +public class JavaScriptMinifyPerformanceTests { + + /** + * Renders of each element before measuring, so the first render, which + * minifies the template's form, and the JIT compiler's work are not + * measured. + */ + private static final int WARM_UP = 200; + + /** + * Renders of each element that are measured. + */ + private static final int MEASURED = 2000; + + /** + * How many times slower a minified render may be than one not + * minified. Minifying is done once for each form of the template and + * not for each request, so a minified render costs about the same as + * one that is not minified, and a minifier run on every request is many + * times slower. The margin is wide so that timing on a busy machine + * does not decide the result. + */ + private static final double MAX_RATIO = 2.0; + + private final TestLoggerFactory loggerFactory = new TestLoggerFactory(null); + + @Test + public void Minify_PerformanceImpact_Small() throws Exception { + JavaScriptBuilderElement minified = element(true); + JavaScriptBuilderElement asWritten = element(false); + try (Pipeline pipeline = new PipelineBuilder(loggerFactory) + .addFlowElement(new SequenceElementBuilder(loggerFactory).build()) + .addFlowElement(new JsonBuilderElementBuilder(loggerFactory).build()) + .build(); + FlowData data = pipeline.createFlowData()) { + data.addEvidence("query.user-agent", "iPhone"); + data.process(); + + long first = System.nanoTime(); + minified.process(data); + long firstMinified = System.nanoTime() - first; + + for (int i = 0; i < WARM_UP; i++) { + minified.process(data); + asWritten.process(data); + } + + long minifiedTime = 0; + long asWrittenTime = 0; + for (int i = 0; i < MEASURED; i++) { + // Each goes first in turn, so neither always follows the + // other. + if (i % 2 == 0) { + minifiedTime += time(minified, data); + asWrittenTime += time(asWritten, data); + } else { + asWrittenTime += time(asWritten, data); + minifiedTime += time(minified, data); + } + } + + double ratio = (double) minifiedTime / asWrittenTime; + System.out.printf( + "JavaScript render: first minified %.3f ms, then %.4f ms " + + "minified and %.4f ms not minified on average over %d, " + + "ratio %.2f%n", + firstMinified / 1e6, + minifiedTime / 1e6 / MEASURED, + asWrittenTime / 1e6 / MEASURED, + MEASURED, + ratio); + assertTrue(ratio <= MAX_RATIO, String.format( + "A minified render took %.2f times as long as one not " + + "minified, more than the %.1f allowed.", ratio, MAX_RATIO)); + } + } + + private JavaScriptBuilderElement element(boolean minify) { + return new JavaScriptBuilderElementBuilder(loggerFactory) + .setEndpoint("/json") + .setHost("example.com") + .setProtocol("https") + .setMinify(minify) + .build(); + } + + private static long time(JavaScriptBuilderElement element, FlowData data) + throws Exception { + long start = System.nanoTime(); + element.process(data); + return System.nanoTime() - start; + } +} From dcacaf8ef7d6f35c493fb5b45aaa6333109b19af Mon Sep 17 00:00:00 2001 From: James Rosewell Date: Mon, 16 Nov 2026 15:37:21 +0000 Subject: [PATCH 5/5] Parse the minified script in all sixteen of its forms --- .../JavaScriptMinifyTests.java | 121 ++++++++++++++++-- 1 file changed, 108 insertions(+), 13 deletions(-) diff --git a/pipeline/pipeline.javascriptbuilder/src/test/java/fiftyone/pipeline/javascriptbuilder/JavaScriptMinifyTests.java b/pipeline/pipeline.javascriptbuilder/src/test/java/fiftyone/pipeline/javascriptbuilder/JavaScriptMinifyTests.java index 606d91dfb..cc21c6455 100644 --- a/pipeline/pipeline.javascriptbuilder/src/test/java/fiftyone/pipeline/javascriptbuilder/JavaScriptMinifyTests.java +++ b/pipeline/pipeline.javascriptbuilder/src/test/java/fiftyone/pipeline/javascriptbuilder/JavaScriptMinifyTests.java @@ -29,9 +29,11 @@ import fiftyone.pipeline.core.data.EvidenceKeyFilter; import fiftyone.pipeline.core.data.EvidenceKeyFilterWhitelist; import fiftyone.pipeline.core.data.FlowData; +import fiftyone.pipeline.core.data.types.JavaScript; import fiftyone.pipeline.core.flowelements.FlowElementBase; import fiftyone.pipeline.core.flowelements.Pipeline; import fiftyone.pipeline.core.flowelements.PipelineBuilder; +import fiftyone.pipeline.engines.data.AspectPropertyValueDefault; import fiftyone.pipeline.engines.fiftyone.flowelements.SequenceElementBuilder; import fiftyone.pipeline.javascriptbuilder.flowelements.JavaScriptBuilderElement; import fiftyone.pipeline.javascriptbuilder.flowelements.JavaScriptBuilderElementBuilder; @@ -44,13 +46,17 @@ import org.json.JSONTokener; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; -import java.util.Arrays; +import java.util.ArrayList; import java.util.Collections; +import java.util.HashSet; import java.util.List; +import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -109,6 +115,25 @@ public class JavaScriptMinifyTests { */ private String render(Boolean minify, String endpoint, boolean cookies) throws Exception { + return render(minify, endpoint, cookies, false, false); + } + + /** + * Renders the script for one request, choosing every setting the + * minified forms are held by. + * @param minify null to leave the builder's default, otherwise the + * value given to setMinify + * @param endpoint the endpoint, or an empty string for no callback + * @param cookies the value given to setEnableCookies + * @param promises true if the request's browser supports promises + * @param delayed true if the JSON holds a delayed execution property + */ + private String render( + Boolean minify, + String endpoint, + boolean cookies, + boolean promises, + boolean delayed) throws Exception { JavaScriptBuilderElementBuilder builder = new JavaScriptBuilderElementBuilder(loggerFactory) .setEndpoint(endpoint) @@ -121,7 +146,7 @@ private String render(Boolean minify, String endpoint, boolean cookies) JavaScriptBuilderElement jsElement = builder.build(); try (Pipeline pipeline = new PipelineBuilder(loggerFactory) .addFlowElement(new SequenceElementBuilder(loggerFactory).build()) - .addFlowElement(new ValuesElement()) + .addFlowElement(new ValuesElement(promises, delayed)) .addFlowElement(new JsonBuilderElementBuilder(loggerFactory).build()) .addFlowElement(jsElement) .build(); @@ -199,14 +224,55 @@ public void Minify_On_ValuesUnchanged() throws Exception { } /** - * Every form of the minified script parses, whether it has a callback - * and whether cookies are enabled. + * Every choice of the four settings the minified forms are held by, + * being whether there is a callback, whether cookies are enabled, + * whether the browser supports promises and whether the JSON holds a + * delayed execution property. + */ + static Stream everyForm() { + List forms = new ArrayList<>(); + for (String endpoint : new String[] {"/json", ""}) { + for (boolean cookies : new boolean[] {true, false}) { + for (boolean promises : new boolean[] {true, false}) { + for (boolean delayed : new boolean[] {true, false}) { + forms.add(Arguments.of( + endpoint, cookies, promises, delayed)); + } + } + } + } + return forms.stream(); + } + + /** + * Each of the four settings changes the script as written, so the + * sixteen choices are sixteen different scripts and the forms tested + * below are every form the element holds. + */ + @Test + public void Minify_EveryForm_IsADifferentScript() throws Exception { + Set scripts = new HashSet<>(); + Iterable forms = everyForm()::iterator; + for (Arguments form : forms) { + Object[] a = form.get(); + scripts.add(render(false, (String) a[0], (Boolean) a[1], + (Boolean) a[2], (Boolean) a[3])); + } + assertEquals(16, scripts.size(), + "The four settings do not give sixteen different scripts."); + } + + /** + * Every form of the minified script parses. */ @ParameterizedTest - @CsvSource({"/json, true", "/json, false", "'', true", "'', false"}) - public void Minify_On_EveryFormParses(String endpoint, boolean cookies) - throws Exception { - String script = render(true, endpoint, cookies); + @MethodSource("everyForm") + public void Minify_On_EveryFormParses( + String endpoint, + boolean cookies, + boolean promises, + boolean delayed) throws Exception { + String script = render(true, endpoint, cookies, promises, delayed); try (Context context = context()) { try { context.parse(Source.create("js", script)); @@ -277,14 +343,33 @@ private static String sessionId(String script) { private static class ValuesElement extends FlowElementBase { - ValuesElement() { + private final boolean promises; + + private final boolean delayed; + + /** + * @param promises true to give Promise as "Full", which the + * JavaScript Builder reads as the browser + * supporting promises + * @param delayed true to add a delayed execution property + */ + ValuesElement(boolean promises, boolean delayed) { super(new TestLoggerFactory(null).getLogger("values"), null); + this.promises = promises; + this.delayed = delayed; } @Override protected void processInternal(FlowData data) { ElementDataBase values = new ElementDataBase(logger, data) { }; values.put("text", VALUE); + if (promises) { + values.put("Promise", + new AspectPropertyValueDefault<>("Full")); + } + if (delayed) { + values.put("delayed", new JavaScript("var delayed = 1;")); + } data.getOrAdd(getElementDataKey(), d -> values); } @@ -301,9 +386,19 @@ public EvidenceKeyFilter getEvidenceKeyFilter() { @Override public List getProperties() { - return Arrays.asList( - new ElementPropertyMetaDataDefault( - "text", this, "test", String.class, true)); + List properties = new ArrayList<>(); + properties.add(new ElementPropertyMetaDataDefault( + "text", this, "test", String.class, true)); + if (promises) { + properties.add(new ElementPropertyMetaDataDefault( + "Promise", this, "test", String.class, true)); + } + if (delayed) { + properties.add(new ElementPropertyMetaDataDefault( + "delayed", this, "test", JavaScript.class, true, null, + true, Collections.emptyList())); + } + return properties; } @Override