Skip to content

Javascript utilities

This reference covers built-in Javascript utility functions. These functions are available anywhere that Javascript can be used in Sirveo.

Built-in functions are usually implemented because they are either convenient for common needs, or much more performant than pure-javascript alternatives, or because they provide capabilities which are impractical or impossible to implement in pure javascript.

Accessing utility functions

All built-in utility functions are namespaced on a global lib object, such that they can be accessed with lib.UUID(), for example.

Utility functions

The following code snippets are for use in JS Code nodes, although the utility functions are available everywhere that javascript is supported in Sirveo.

lib.UUID

Generate a new UUIDv4 value.

$out.uuid = lib.UUID()

Output:

{
"uuid": "37f96aa4-3968-45e7-be3e-1122d309067a"
}

lib.NowISO

Obtain the current timestamp, as an ISO string format, necessarily in UTC.

$out.ts = lib.NowISO()

Output:

{
"ts": "2024-06-15T21:44:34.689Z"
}

lib.NowSec

Obtain the current timestamp, as unix time / unix epoch.

$out.ts = lib.NowSec()

Output:

{
"ts": 1718487989
}

lib.NowMs

Obtain the current timestamp, as unix time / unix epoch, but with milliseconds.

$out.ts = lib.NowMs()

Output:

{
"ts": 1718487925668
}

lib.SHA256

Obtain the hex-encoded SHA256 digest for the given input. It is recommended to use string inputs.

$out = {
// strings are expected
string: lib.SHA256("input string"),
// booleans are treated as strings
bool: lib.SHA256(true), // "true"
// numbers are treated as strings
number: lib.SHA256(123.4), // "123.4"
// null is treated as empty string
null: lib.SHA256(null), // ""
// undefined is treated as empty string
undefined: lib.SHA256(undefined), // ""
}

Output:

{
"bool": "b5bea41b6c623f7c09f1bf24dcae58ebab3c0cdd90ad966bc43a45b44867e12b",
"null": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
"number": "5f466d7afa48b619c7045d54b15d8d48f47e401335078e9267f5e1d942e09ca5",
"string": "f23f4781d6814ebe349c6b230c1f700714f4f70f735022bd4b1fb69421859993",
"undefined": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
}

lib.Base64Encode

Perform base64 encoding on the input string, as per RFC4648.

The following example demonstrates all base64-related utilities.

$out.a_input = "http://127.0.0.1:1234/path?a=b"
// RFC4648 section 4 (standard)
$out.b_encoded = lib.Base64Encode($out.a_input)
$out.c_decoded = lib.Base64Decode($out.b_encoded)
// RFC4648 section 5 (filename & url safe)
$out.d_url_encoded = lib.Base64EncodeUrl($out.a_input)
$out.e_url_decoded = lib.Base64DecodeUrl($out.d_url_encoded)
// bad input
$out.f_bad_decode = lib.Base64Decode("badBase64")

Output:

{
"a_input": "http://127.0.0.1:1234/path?a=b",
"b_encoded": "aHR0cDovLzEyNy4wLjAuMToxMjM0L3BhdGg/YT1i",
"c_decoded": "http://127.0.0.1:1234/path?a=b",
"d_url_encoded": "aHR0cDovLzEyNy4wLjAuMToxMjM0L3BhdGg_YT1i",
"e_url_decoded": "http://127.0.0.1:1234/path?a=b",
"f_bad_decode": ""
}

lib.Base64Decode

Decodes a base64 string value. Invalid input results in an empty string.

See example usage.

lib.Base64EncodeUrl

Perform URL-safe base64 encoding on the input string, as per section 5.

See example usage.

lib.Base64DecodeUrl

Decodes a url-safe base64 string value. Invalid input results in an empty string.

See example usage.

Tips

The output of the timestamp-related utility functions will be as accurate as the system clock.