Skip to main content

Web Platform APIs

txiki.js implements a number of Web Platform APIs to provide a familiar environment for JavaScript developers.

Supported APIs

APINotes
AbortController / AbortSignalIncludes static AbortSignal.abort(), timeout(), any()
atob / btoaBase64 encode/decode
Blob
BroadcastChannelCross-worker pub/sub messaging
Channel Messaging APIMessageChannel / MessagePort, transferable across workers
CompressionStream / DecompressionStreamFormats: gzip, deflate, deflate-raw
Console
CryptoIncludes SubtleCrypto
Direct SocketsTCP, TLS, UDP and Unix pipe sockets — see the Networking guide
DOMException
Encoding APITextEncoder / TextDecoder, plus the streaming TextEncoderStream / TextDecoderStream
EventSourceServer-Sent Events over fetch; no CORS (withCredentials is a no-op)
EventTarget
fetch
File
FileReader
FormData
Import attributesJSON, text, and bytes
Navigator.userAgentData
Performance
queueMicrotask
setTimeout, setInterval
Storage APIlocalStorage persists to SQLite at $TJS_HOME/localStorage.db; sessionStorage is in-memory
Streams API
structuredClone
URL
URLPattern
URLSearchParams
WebAssemblyInterpreter-based (WAMR); some limitations
WebSocketExtensions
WebSocketStreamExtensions
Web WorkersStructured-clone messaging with transferables (ArrayBuffer, MessagePort)
XMLHttpRequest

Web Crypto (SubtleCrypto)

The global crypto object implements both getRandomValues() / randomUUID() and the full crypto.subtle (SubtleCrypto) API.

All twelve SubtleCrypto methods are available: digest, encrypt, decrypt, sign, verify, generateKey, deriveBits, deriveKey, importKey, exportKey, wrapKey, and unwrapKey.

CategoryAlgorithms
DigestSHA-1, SHA-256, SHA-384, SHA-512
Symmetric encryptionAES-CBC, AES-CTR, AES-GCM
Key wrappingAES-KW, plus wrapKey/unwrapKey with the encryption algorithms
Asymmetric encryptionRSA-OAEP
SignaturesRSASSA-PKCS1-v1_5, RSA-PSS, ECDSA, Ed25519, HMAC
Key agreementECDH, X25519
Key derivationPBKDF2, HKDF
// Hash some bytes.
const data = new TextEncoder().encode('hello world');
const digest = await crypto.subtle.digest('SHA-256', data);
console.log(new Uint8Array(digest));

// Generate an AES-GCM key and encrypt.
const key = await crypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, true, ['encrypt', 'decrypt']);
const iv = crypto.getRandomValues(new Uint8Array(12));
const ciphertext = await crypto.subtle.encrypt({ name: 'AES-GCM', iv }, key, data);

For synchronous, streaming, or MD5/SHA-3 hashing, see the tjs:hashing standard-library module.

Extensions

WebSocket / WebSocketStream headers

Both WebSocket and WebSocketStream support setting custom HTTP headers on the client handshake request. This is a non-standard extension useful for authentication, API keys, and other scenarios where you need to send headers during the WebSocket upgrade.

Certain headers related to the WebSocket handshake itself (e.g. Connection, Upgrade, Sec-WebSocket-*) are forbidden and will throw a TypeError.

WebSocket

Instead of passing protocols as the second argument, pass an options object with headers (and optionally protocols):

const ws = new WebSocket('wss://example.com/ws', {
protocols: ['chat'],
headers: {
'Authorization': 'Bearer my-token',
'X-Custom-Header': 'value',
},
});

The headers option accepts a plain object, a Headers instance, or an array of [name, value] pairs.

WebSocketStream

Pass headers in the options object:

const wss = new WebSocketStream('wss://example.com/ws', {
protocols: ['chat'],
headers: {
'Authorization': 'Bearer my-token',
},
});

const { readable, writable } = await wss.opened;

WebAssembly

WebAssembly is powered by the WAMR interpreter. Most of the JavaScript API is implemented:

  • WebAssembly.validate(), compile(), instantiate(), and the compileStreaming() / instantiateStreaming() variants.
  • Module (including Module.exports() and Module.imports()), Instance, Memory, Table, Global, and the CompileError / LinkError / RuntimeError types.
  • Function, global, and memory imports.
  • The reference-types, SIMD, and bulk-memory proposals. externref / funcref work for exported functions, globals, and tables.

The following are not currently supported:

  • Table imports — a Table cannot be supplied through the import object. Table exports work.
  • Reference types in imported functionsexternref / funcref as parameters or results of JS-backed imported functions.
  • Multi-value returns from imported functions — multi-value returns from exported functions work.
  • Re-instantiating a Module with different imports — imports are resolved at the module level, so passing a new import object to a second new WebAssembly.Instance(module, ...) reuses the first set. Use WebAssembly.instantiate(bytes, importObject) to get independent instances.

To run WASI modules, see tjs:wasi.

WinterTC compliance

txiki.js aims to be WinterTC compliant. You can track the progress here.