Standard Library Overview
Chuks ships with a built-in standard library that covers common tasks — HTTP, file I/O, math, JSON, string manipulation, time utilities, logging, cryptography, authentication, and more. Every module lives under the std/ namespace and is imported with a single line.
Importing Modules
Section titled “Importing Modules”import { http } from "std/http";import { math } from "std/math";import { json } from "std/json";import { fs } from "std/fs";import { time } from "std/time";import { date } from "std/date";import { strings } from "std/strings";import { log } from "std/log";import { crypto } from "std/crypto";import { jwt } from "std/jwt";import { uuid } from "std/uuid";import { dotenv } from "std/dotenv";import { validate } from "std/validate";import { rateLimit } from "std/rate_limit";import { test } from "std/test";import { channel } from "std/channel";import { db } from "std/db";import { Schema, SchemaBuilder, ColumnBuilder } from "std/db/schema";import { Repository } from "std/db/repository";import { QueryBuilder } from "std/db/query";import { Buffer } from "std/buffer";import { TcpConnection, TlsConnection, TcpServer, resolve } from "std/net";// Error is globally available — no import needed// new Error("message", code?, data?)Available Modules
Section titled “Available Modules”| Module | Import | Description |
|---|---|---|
| HTTP | std/http | HTTP client requests and server creation |
| Math | std/math | Mathematical functions and constants |
| JSON | std/json | Parse and stringify JSON data |
| File System | std/fs | Read, write, and manage files and directories |
| Time | std/time | Timestamps, formatting, sleep, and date utilities |
| Date | std/date | Date creation, arithmetic, comparisons, and boundaries |
| Strings | std/strings | String splitting, joining, replacing, and more |
| Log | std/log | Structured, leveled logging with timestamps |
| Crypto | std/crypto | SHA, HMAC, MD5, PBKDF2, bcrypt, and secure random generation |
| JWT | std/jwt | JSON Web Token signing, verification, decoding |
| UUID | std/uuid | UUID v4/v7 generation and validation |
| Dotenv | std/dotenv | Load and manage environment variables |
| Validate | std/validate | Input validation for strings, types, and ranges |
| Rate Limit | std/rate_limit | In-memory rate limiting with time windows |
| Test | std/test | Assertions and formatted test reporting |
| Base64 | std/base64 | Base64 encoding, decoding, and URL-safe variants |
| URL | std/url | URL parsing, formatting, query strings, encoding |
| Regex | std/regex | Regular expression matching, replacing, splitting |
| Path | std/path | File path joining, resolving, and manipulation |
| OS | std/os | Platform info, env vars, shell commands, process info |
| Database | std/db | SQL database access (SQLite, PostgreSQL, MySQL, MSSQL built-in) |
| Schema | std/db/schema | Fluent schema definitions, DDL generation, relationships |
| Repository | std/db/repository | Type-safe CRUD, query chaining, eager loading |
| Query Builder | std/db/query | Fluent, chainable SQL query builder |
| Channel | std/channel | Inter-task communication with buffered/unbuffered channels |
| Buffer | std/buffer | Binary data handling, byte-level I/O, protocol buffers |
| Net | std/net | TCP/TLS networking, servers, and DNS resolution |
| Error | (global) | Structured error class with message, code, and data |
Quick Examples
Section titled “Quick Examples”HTTP Request
Section titled “HTTP Request”import { http } from "std/http";
const resp = await http.get("https://api.example.com/data");println(resp.status); // 200println(resp.body); // response bodyRead a File
Section titled “Read a File”import { fs } from "std/fs";
const content = await fs.readFile("./config.json");println(content);Parse JSON
Section titled “Parse JSON”import { json } from "std/json";
const data = json.parse('{"name": "Chuks", "version": 1}');println(data["name"]); // "Chuks"Math Operations
Section titled “Math Operations”import { math } from "std/math";
const val = math.floor(math.random() * 100);println(val);Time & Sleep
Section titled “Time & Sleep”import { time } from "std/time";
println(time.now());await time.sleep(1000);println("1 second later");String Utilities
Section titled “String Utilities”import { strings } from "std/strings";
const parts = strings.split("a,b,c", ",");const joined = strings.join(parts, "-"); // "a-b-c"Structured Logging
Section titled “Structured Logging”import { log } from "std/log";
log.info("Server started", { "port": "3000" });log.error("Request failed", { "status": "500" });Cryptography
Section titled “Cryptography”import { crypto } from "std/crypto";
const hash = crypto.sha256("hello world");const token = crypto.randomBytes(32);JSON Web Tokens
Section titled “JSON Web Tokens”import { jwt } from "std/jwt";
const token = jwt.sign({ "sub": "user123" }, "secret", 3600000);const claims = jwt.verify(token, "secret");UUID Generation
Section titled “UUID Generation”import { uuid } from "std/uuid";
const id = uuid.v7(); // Time-ordered, sortableprintln(uuid.isValid(id)); // trueEnvironment Variables
Section titled “Environment Variables”import { dotenv } from "std/dotenv";
dotenv.load();const port = dotenv.get("PORT", "3000");Input Validation
Section titled “Input Validation”import { validate } from "std/validate";
validate.isEmail("user@example.com"); // truevalidate.minLength("hi", 3); // falseRate Limiting
Section titled “Rate Limiting”import { rateLimit } from "std/rate_limit";
const limiter = rateLimit.create(100, 60000); // 100 req/minrateLimit.check(limiter, "192.168.1.1"); // true or falseChannels
Section titled “Channels”import { channel } from "std/channel";
const ch = channel.new(0);spawn { channel.send(ch, "hello from task");}const msg = channel.receive(ch);println(msg); // "hello from task"Error Handling
Section titled “Error Handling”// Error is globally available — no import neededtry { throw new Error("not found", 404);} catch (e) { println(e.message); // "not found" println(e.code); // 404}Testing
Section titled “Testing”import { test } from "std/test";
test.assertEqual(1 + 1, 2);test.assert(len("hello") == 5);Built-in String & Collection Methods
Section titled “Built-in String & Collection Methods”In addition to the std/strings module, strings, arrays, and maps in Chuks have built-in methods that can be called directly on values without any import:
// String methods — no import neededvar s = "Hello World";println(s.toUpperCase()); // "HELLO WORLD"println(s.includes("World")); // true
// Array methods — no import neededvar nums = [3, 1, 2];nums.sort();println(nums); // [1, 2, 3]
// Map methods — no import neededvar m: map[string]int = {"a": 1, "b": 2};println(m.keys()); // ["a", "b"]See the dedicated Strings, Arrays, and Maps pages for complete API references.