Skip to content

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 pathPackage versionExportsLicense
@noble/hashes/sha2.js2.4.0sha256, sha512MIT
@noble/hashes/hmac.js2.4.0hmacMIT
@noble/hashes/utils.js2.4.0bytesToHex, hexToBytesMIT
@stablelib/utf82.1.0encode, decode, encodedLengthMIT

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:

MemberResult and effect
update(bytes: Uint8Array)Adds bytes; returns the same state for chaining
digest(): Uint8ArrayFinalizes and returns digest bytes; state cannot be reused
digestInto(output: Uint8Array): voidFinalizes into a caller-owned buffer of exactly the digest length
clone()Independent copy of the current state
destroy(): voidClears 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): string returns lowercase hexadecimal text.
  • hexToBytes(hex: string): Uint8Array accepts upper- or lowercase hexadecimal without a 0x prefix. Invalid characters or odd lengths throw. An empty string returns an empty byte array.

UTF-8 conversion ​

  • encode(text: string): Uint8Array encodes text, including Unicode characters. Invalid UTF-16 such as an unpaired surrogate throws.
  • decode(bytes: Uint8Array): string decodes UTF-8. Invalid byte sequences throw rather than being silently replaced.
  • encodedLength(text: string): number returns 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

A little chemistry for your development workflow.