Appearance
Script package imports
Pre-Request and Post-Request scripts can import the modules below. They are bundled with elchimist at fixed versions, work in browser and desktop, and require no CDN or npm access during execution. Start typing a module name after from " or import(" to see the supported paths, versions and exports in the editor.
Package catalog
Only the exact paths and exports in this table are available. Use named or namespace imports; these modules do not have a default export.
| Import path | Package version | Exports | License |
|---|---|---|---|
@noble/hashes/sha2.js | 2.4.0 | sha256, sha512 | MIT |
@noble/hashes/hmac.js | 2.4.0 | hmac | MIT |
@noble/hashes/utils.js | 2.4.0 | bytesToHex, hexToBytes | MIT |
@stablelib/utf8 | 2.1.0 | encode, decode, encodedLength | MIT |
The catalog exposes a subset of these packages. Other algorithms, random number helpers and package paths are unavailable. node:crypto, crypto-js, require(), URL imports, relative/file imports and version-suffixed paths are not supported. For example, use @noble/hashes/sha2.js, not @noble/hashes@2.4.0/sha2.js. Unsupported imports fail with the exact module name in Script error details. Unsupported named exports also fail before the script body runs.
js
import { sha256 } from '@noble/hashes/sha2.js';
import { bytesToHex } from '@noble/hashes/utils.js';
import { encode } from '@stablelib/utf8';
const digest = bytesToHex(sha256(encode('abc')));
elc.request.headers.set('X-Content-Hash', digest);SHA-256 and SHA-512
sha256(message: Uint8Array): Uint8Array returns 32 digest bytes. sha512(message: Uint8Array): Uint8Array returns 64 digest bytes. Convert strings with encode first and use bytesToHex for header values. Non-byte inputs throw. Neither function persists values or performs HTTP calls.
Both functions expose outputLen (32/64), blockLen (64/128) and create() for incremental processing. A created state supports:
| Member | Result and effect |
|---|---|
update(bytes: Uint8Array) | Adds bytes; returns the same state for chaining |
digest(): Uint8Array | Finalizes and returns digest bytes; state cannot be reused |
digestInto(output: Uint8Array): void | Finalizes into a caller-owned buffer of exactly the digest length |
clone() | Independent copy of the current state |
destroy(): void | Clears internal state; subsequent updates fail |
js
const state = sha256.create();
state.update(encode('first '));
const digest = state.update(encode('second')).digest();HMAC
hmac(hash, key: Uint8Array, message: Uint8Array): Uint8Array computes a message authentication tag using sha256 or sha512. The result has the hash's output length. Invalid hash functions or non-byte inputs throw. Use actual key bytes; encode a textual key with encode, or decode a hexadecimal key with hexToBytes.
hmac.create(hash, key) returns an incremental state with the same update, digest, digestInto, clone and destroy methods described above.
Define signingKey in your environment before using this Pre-Request example:
js
import { sha256 } from '@noble/hashes/sha2.js';
import { hmac } from '@noble/hashes/hmac.js';
import { bytesToHex } from '@noble/hashes/utils.js';
import { encode } from '@stablelib/utf8';
const key = elc.environment.get('signingKey');
if (!key) throw new Error('Set signingKey before sending');
const message = elc.variables.resolve('{{tenantId}}');
const signature = bytesToHex(hmac(sha256, encode(key), encode(message)));
elc.request.headers.set('X-Signature', signature);Hex conversion
bytesToHex(bytes: Uint8Array): stringreturns lowercase hexadecimal text.hexToBytes(hex: string): Uint8Arrayaccepts upper- or lowercase hexadecimal without a0xprefix. Invalid characters or odd lengths throw. An empty string returns an empty byte array.
UTF-8 conversion
encode(text: string): Uint8Arrayencodes text, including Unicode characters. Invalid UTF-16 such as an unpaired surrogate throws.decode(bytes: Uint8Array): stringdecodes UTF-8. Invalid byte sequences throw rather than being silently replaced.encodedLength(text: string): numberreturns the encoded byte length without creating an output array; invalid UTF-16 throws.
These functions run inside the VM. Native-style TextEncoder and TextDecoder are also available through the Web API bindings.
js
import { encode, decode, encodedLength } from '@stablelib/utf8';
const bytes = encode('Hello 🦌');
const text = decode(bytes);
const length = encodedLength(text);Dynamic imports and lifetime
Dynamic imports use exactly the same catalog and do not download code:
js
const { sha256 } = await import('@noble/hashes/sha2.js');Modules are cached within one script invocation. Each collection, folder or request script runs in a fresh runtime, so module state is not shared across sources, phases or sends. Use elc.variables to carry temporary string values between scripts. Package calls execute inside the same memory and time limits as your script; imports do not grant filesystem, process or DOM access.
Package versions change only with an elchimist update. Scripts cannot install or upgrade packages. The supported subset is tested in the isolated runtime; this does not imply compatibility with every API in the upstream packages.
Related: Scripts and the elc API · HTTP client