Titan Planet Logo

@titanpl/core

The official Core Standard Library for Titan Planet - a high-performance JavaScript extension for Gravity Runtime.

@titanpl/core

The official Core Standard Library for Titan Planet.

A high-performance JavaScript extension for Gravity Runtime, providing essential utilities including file system operations, cryptography, session management, URL parsing, and more — all built with native Rust for maximum speed.

Overview

@titanpl/core is a comprehensive extension for TitanPL that brings a suite of essential utilities directly to your V8 runtime. It provides file system access, cryptographic functions, session handling, and much more, all optimized at the native Rust level.

Unlike traditional Node.js modules, these APIs are compiled directly into Gravity, ensuring zero dependency overhead and instant availability without any require() or import statements.

Auto-Loading

The extension automatically attaches to Gravity. You can access it via t.core or the t global object alias.

// Access via t.core (Recommended)
const { fs, crypto, os } = t.core;

// Read a file
const content = fs.readFile("config.json");

// Generate UUID
const id = crypto.uuid();

API Reference

fs (File System)

Perform synchronous file system operations.

File System Root

The file system root directory is always the server folder. To access files in the app folder, you must use relative paths like ../app/filename.

fs.readFile

Read file content as UTF-8 string.

Prop

Type

Returns: string

fs.writeFile

Write string content to file.

Prop

Type

Returns: void

fs.exists

Check if path exists.

Prop

Type

Returns: boolean

fs.mkdir

Create a directory (recursive).

Prop

Type

Returns: void

fs.remove

Remove file or directory.

Prop

Type

Returns: void

fs.readdir

List directory contents.

Prop

Type

Returns: string[]

fs.stat

Get file statistics.

Prop

Type

Returns: Object

File Statistics Object Properties:

Prop

Type

path (Path Manipulation)

Utilities for handling file paths.

path.join

Join path segments.

Prop

Type

Returns: string

path.resolve

Resolve path to absolute.

Prop

Type

Returns: string

path.dirname

Get directory name.

Prop

Type

Returns: string

path.basename

Get base file name.

Prop

Type

Returns: string

path.extname

Get file extension.

Prop

Type

Returns: string

crypto (Cryptography)

Cryptographic utilities using native Rust implementations.

crypto.hash

Hash data. supported algos: sha256, sha512, md5.

Prop

Type

Returns: string

crypto.randomBytes

Generate random bytes as hex string.

Prop

Type

Returns: string

crypto.uuid

Generate a UUID v4. Returns: string

crypto.compare

Constant-time comparison.

Prop

Type

Returns: boolean

crypto.encrypt

AES-256-GCM Encrypt (Returns Base64).

Prop

Type

Returns: string

crypto.decrypt

AES-256-GCM Decrypt.

Prop

Type

Returns: string

crypto.hashKeyed

HMAC-SHA256/512.

Prop

Type

Returns: string

buffer (Buffer Utilities)

Utilities for binary data manipulation.

buffer.fromBase64

Decode Base64 string.

Prop

Type

Returns: Uint8Array

buffer.toBase64

Encode to Base64.

Prop

Type

Returns: string

buffer.fromHex

Decode Hex string.

Prop

Type

Returns: Uint8Array

buffer.toHex

Encode to Hex.

Prop

Type

Returns: string

buffer.fromUtf8

Encode UTF-8 string to bytes.

Prop

Type

Returns: Uint8Array

buffer.toUtf8

Decode bytes to UTF-8 string.

Prop

Type

Returns: string

ls / localStorage (Persistent Storage)

Key-value storage persisted to disk (via Sled).

ls.get

Get value.

Prop

Type

Returns: string | null

ls.set

Set value.

Prop

Type

Returns: void

ls.remove

Remove key.

Prop

Type

Returns: void

ls.clear

Clear all storage. Returns: void

ls.keys

List all keys. Returns: string[]

session (Server-side Sessions)

Session management backed by persistent storage.

session.get

Get session value.

Prop

Type

Returns: string | null

session.set

Set session value.

Prop

Type

Returns: void

session.delete

Delete session value.

Prop

Type

Returns: void

session.clear

Clear entire session.

Prop

Type

Returns: void

cookies (HTTP Cookies)

Cookie parsing and serialization.

cookies.get

Parse cookie from request headers.

Prop

Type

Returns: string | null

cookies.set

Set Set-Cookie header on response.

Prop

Type

Returns: void

cookies.delete

Delete cookie (expire).

Prop

Type

Returns: void

os (Operating System)

Get system information.

os.platform

OS platform (e.g., linux, windows). Returns: string

os.cpus

Number of CPU cores. Returns: number

os.totalMemory

Total system memory in bytes. Returns: number

os.freeMemory

Free system memory in bytes. Returns: number

os.tmpdir

Temporary directory path. Returns: string

net (Network)

Network utilities.

net.resolveDNS

Resolve hostname to IP addresses.

Prop

Type

Returns: string[]

net.ip

Get local IP address. Returns: string

proc (Process)

Current process information.

proc.pid

Process ID. Returns: number

proc.uptime

System uptime in seconds. Returns: number

time (Time)

Time utilities.

time.sleep

Sleep for specified milliseconds.

Prop

Type

Returns: void

time.now

Current timestamp (ms). Returns: number

time.timestamp

Current ISO timestamp. Returns: string

url (URL)

URL parsing and manipulation.

url.parse

Parse URL string.

Prop

Type

Returns: URLObject

url.format

Format URL object.

Prop

Type

Returns: string

new url.SearchParams

Handle query strings.

Prop

Type

Returns: URLSearchParams

URLObject Properties:

Prop

Type

Native Bindings & Node.js Compatibility

The @titanpl/core extension serves as the backbone of the Titan ecosystem, providing a robust set of standard library modules that mirror Node.js APIs. It leverages high-performance Rust native bindings to ensure optimal speed and efficiency for system-level operations.

Key Features

  • Pre-installed & Ready: @titanpl/core comes pre-installed with every new Titan project, giving you immediate access to essential tools like file system operations, cryptography, and networking without any setup.
  • Modular & Optional: While it is a core part of the experience, it is built as an extension. If your project has specific needs or you wish to maintain an ultra-lightweight runtime, you can uninstall it.
  • Foundation for Extensions: This library exemplifies the power of Titan's extension system. Developers can use it as a reference or a foundation to build their own high-performance, JS-only or hybrid extensions, expanding the capabilities of Gravity.

Node.js Compatibility Layer

If you are migrating an existing Node.js project or using libraries that depend on Node.js built-ins, we recommend using the @titanpl/node compatibility layer. It provides shims for fs, path, crypto, and more, mapping them directly to the native implementations found in @titanpl/core.

On this page