txiki.js Documentation

website·Indexed 32 days ago

https://txikijs.org

A small and powerful JavaScript runtime targeting state-of-the-art ECMAScript. It features a high-performance HTTP/HTTPS server with WebSocket support, a standard library of modules (tjs:), and support for Web Platform APIs including Web Crypto, WebAssembly, and WinterCG compliance. The runtime provides low-level networking via the WHATWG Direct Sockets API and a WASI implementation for running WebAssembly modules.

Tokens
67.3K
Snippets
533
Records
757
Agent score
62%

What's inside txiki.js

  1. Overview of the Worker interface

    The Worker interface represents a background task created via script. It allows the main thread to offload work to a separate thread and communicate with it using a messaging system.
  2. Overview of the txiki.js global tjs namespace

    The tjs global object is the primary entry point for core txiki.js functionality. It provides APIs for the following domains:

    • Filesystem: File I/O, directory operations, path manipulation, and file watching.
    • Networking: TCP, UDP, and Unix pipe sockets.
    • HTTP Server: High-performance HTTP server and WebSocket support via tjs.serve().
    • Process: Child process spawning, signal handling, and process management.
    • System: Environment variables, OS information, and runtime metadata.
    • Engine: Low-level engine access including bytecode compilation, serialization, garbage collection, and build-time feature flags via tjs.engine.features.
    • Utilities: Console helpers and interactive prompts.
  3. Overview of TPK packaging for txiki.js apps

    TPK is an experimental packaging format for txiki.js applications that allows bundling a multi-file app (including source files and assets) into a single .tpk archive or a standalone executable. Unlike tjs compile which uses bytecode for a single file, tjs app compile packages the entire source tree as a ZIP archive appended to the runtime binary. The app is extracted to a temporary directory on the first run and cached for subsequent launches.
  4. Overview of ReadlineInterface

    The ReadlineInterface class provides a readline interface for interactive line editing. It supports full line editing with Emacs-style key bindings, history navigation, tab completion, and syntax colorization when connected to a terminal. For non-TTY streams, it falls back to simple line-buffered input. It extends the Disposable class.
  5. Use StructDef to pack and unpack C structs

    A StructDef (created via defineStruct) allows a developer to convert plain JavaScript objects into binary struct bytes and vice versa. Because it is an AdvancedType, it can be used directly as an argument or return type in dlopen calls, where it will be passed and returned by value using its pack and unpack logic.
  6. Use the tjs:path module for path manipulation

    The tjs:path module provides a set of utilities for working with file and directory paths. It is designed as an adaptation of the NodeJS path module, allowing developers to join paths, extract directory names, basenames, and file extensions.
    import path from 'tjs:path';
    
    const full = path.join('/home', 'user', 'docs', 'file.txt');
    console.log(path.dirname(full));  // '/home/user/docs'
    console.log(path.basename(full)); // 'file.txt'
    console.log(path.extname(full));  // '.txt'
  7. Access properties of PipeServerSocketOpenInfo

    The PipeServerSocketOpenInfo interface provides information about a successfully opened pipe server socket. It contains the local address of the socket and a readable stream for accepting incoming connections.
    interface PipeServerSocketOpenInfo {
      localAddress: string;
      readable: ReadableStream<PipeSocket>;
    }
  8. Use StructFieldInfo to inspect struct field layout

    The StructFieldInfo interface provides metadata about a specific field's placement and characteristics within a struct layout, typically returned by StructDef.describe. It includes the field's name, its byte offset from the start of the struct, the amount of space it occupies in bytes, and its type definition.
    interface StructFieldInfo {
      readonly name: string;
      readonly offset: number; // Byte offset from the start of the struct
      readonly size: number;   // Bytes the field occupies
      readonly type: string;   // Field type name (e.g., 'type_sint32', 'u8[]', 'enum(RED, GREEN)')
    }
  9. Use the `c` ANSI color helper for styled output

    The c object (of type StyleFunction & object) provides a chainable API for applying ANSI styles and colors to strings. It allows for simple color calls, chained modifiers (like bold), and custom RGB colors.
    import { c } from 'tjs:readline';
    
    c.red('error!');              // red text
    c.bold.green('success');      // bold green text
    c.rgb(255, 128, 0)('orange'); // RGB color
  10. Use the IAssertionResult interface to handle assertion outcomes

    The IAssertionResult<T> interface represents the result of an assertion. It uses a type parameter T to define the type of the expected value. The interface provides details about whether the assertion passed, the operator used, and the values involved.
    interface IAssertionResult<T> {
      actual: unknown;
      at?: string;
      description: string;
      expected: T;
      operator: string;
      pass: boolean;
    }
  11. Core runtime features of txiki.js

    txiki.js provides a comprehensive set of runtime capabilities for system-level development, including:

    • Networking: TCP, TLS, UDP, and Unix sockets
    • Process Management: Signal handling and child processes
    • Filesystem: File operations
    • DNS: getaddrinfo via lookup
    • Web Services: Built-in HTTP server
    • Compatibility: WASI support
    • Distribution: Ability to create standalone executables
    • Testing: Built-in test runner via the CLI