Expand description
§51Degrees JavaScript builder
The JavaScript builder element renders a client-side JavaScript include from
the JSON payload produced by the fiftyone_json_builder crate. The
generated script creates a global manager object on the client device, runs
any JavaScript-typed properties the JSON carries, and (when a callback URL is
configured) posts the evidence it gathers back to the server for a refreshed
result. It implements the
javascript-builder specification.
§The template
The script is rendered from a single Mustache template,
JavaScriptResource.mustache, embedded at compile time with include_str!.
Rendering uses a small,
pure-safe-Rust Mustache renderer (in this crate’s mustache module) with
HTML escaping disabled, so the JSON payload, the callback URL and the object
name reach the client verbatim.
§Configuration and per-request overrides
JavaScriptBuilderElementBuilder sets the host, endpoint, protocol, object
name, cookie behavior and minification. Several of those can be overridden
per request through evidence (query.fod-js-object-name,
query.fod-js-enable-cookies, header.host and header.protocol). See
JavaScriptBuilderElement for the full derivation rules.
§Minification
With the default-on minify feature the rendered script is minified by the
oxc toolchain (see the minify module). A minification failure falls back to
the unminified script, so valid JavaScript is always served. The set_minify
builder flag turns minification off for an element, and building the crate
with default-features = false drops the oxc dependency entirely.
§A worked pipeline
use std::sync::Arc;
use fiftyone_pipeline_core::{Evidence, Pipeline};
use fiftyone_json_builder::JsonBuilderElement;
use fiftyone_javascript_builder::{JavaScriptBuilderElement, JAVASCRIPT_BUILDER_DATA_KEY};
let pipeline = Pipeline::builder()
.add_element(Arc::new(JsonBuilderElement::new()))
.add_element(Arc::new(JavaScriptBuilderElement::new()))
.build()
.unwrap();
let mut data = pipeline.create_flow_data_with(
Evidence::builder().add("header.host", "localhost").build(),
);
data.process().unwrap();
let js = data.get(JAVASCRIPT_BUILDER_DATA_KEY).unwrap().javascript().to_owned();
assert!(js.contains("fiftyoneDegreesManager"));Structs§
- Java
Script Builder Element - Generates a JavaScript include to be run on the client device.
- Java
Script Builder Element Builder - Configures and constructs a
JavaScriptBuilderElement. - Java
Script Builder Element Data - The element data the JavaScript builder writes into the flow data.
- Java
Script Resource - The parameters required by the
JavaScriptResource.mustachetemplate.
Constants§
- BUILDER_
DEFAULT_ ENABLE_ COOKIES - The default value of the flag that controls whether client-side processing stores results in cookies.
- BUILDER_
DEFAULT_ HOST - The default host for the builder. An empty string means the host from the evidence collection (the host the original request used) is applied.
- BUILDER_
DEFAULT_ MINIFY - The default value of the flag that controls minification.
- BUILDER_
DEFAULT_ OBJECT_ NAME - The default value for the JavaScript ‘object name’.
- BUILDER_
DEFAULT_ PROTOCOL - The default protocol for the builder. An empty string means the protocol from the evidence collection (the protocol the original request used) is applied.
- EVIDENCE_
ENABLE_ COOKIES - The complete
query.fod-js-enable-cookiesevidence key. - EVIDENCE_
ENABLE_ COOKIES_ SUFFIX - The suffix used when the JavaScript builder ‘enable cookies’ parameter is supplied as evidence.
- EVIDENCE_
HOST_ KEY - The complete key to use when the
HostHTTP header is passed as evidence (header.host). - EVIDENCE_
OBJECT_ NAME - The complete
query.fod-js-object-nameevidence key. - EVIDENCE_
OBJECT_ NAME_ SUFFIX - The suffix used when the JavaScript builder ‘object name’ parameter is supplied as evidence.
- FALLBACK_
PROTOCOL - The protocol used when creating a callback URL if no other protocol value was found or specified.
- JAVASCRIPT_
BUILDER_ DATA_ KEY - The typed key for retrieving
JavaScriptBuilderElementDatafrom a flow data. - JAVASCRIPT_
BUILDER_ ELEMENT_ DATA_ KEY - The element data key under which the JavaScript builder stores its element data.
- JAVASCRIPT_
PROPERTY_ KEY - The name of the single property the JavaScript builder populates: the generated JavaScript, as a string.
