@ezetgalaxy/titan 26.13.6 → 26.13.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/templates/common/app/t.native.d.ts +1834 -61
|
@@ -1,82 +1,631 @@
|
|
|
1
|
-
//
|
|
1
|
+
// =============================================================================
|
|
2
|
+
// Titan Planet — Type Definitions
|
|
3
|
+
// Framework: JavaScript-first backend compiled to native Rust + Axum binary
|
|
4
|
+
// Version: v26 (Stable)
|
|
5
|
+
// Docs: https://titan-docs-ez.vercel.app/docs
|
|
6
|
+
// GitHub: https://github.com/ezet-galaxy/titanpl
|
|
7
|
+
// =============================================================================
|
|
2
8
|
|
|
9
|
+
// ---------------------------------------------------------------------------
|
|
10
|
+
// Module Definitions — Imports from "titan"
|
|
11
|
+
// ---------------------------------------------------------------------------
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Represents a normalized HTTP request object passed to every Titan action.
|
|
15
|
+
*
|
|
16
|
+
* This object is **stable, predictable, and serializable** — it is identical
|
|
17
|
+
* across both development (`titan dev`) and production (native binary) modes.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```js
|
|
21
|
+
* // Accessing the request inside an action
|
|
22
|
+
* export function getUser(req) {
|
|
23
|
+
* const userId = req.params.id; // Route parameter
|
|
24
|
+
* const page = req.query.page; // Query string ?page=2
|
|
25
|
+
* const data = req.body; // Parsed JSON body (POST/PUT/PATCH)
|
|
26
|
+
* const auth = req.headers["authorization"];
|
|
27
|
+
* return { userId, page, data, auth };
|
|
28
|
+
* }
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* @see https://titan-docs-ez.vercel.app/docs/03-actions — Actions documentation
|
|
32
|
+
* @see https://titan-docs-ez.vercel.app/docs/02-routes — Routes & parameters
|
|
33
|
+
*/
|
|
3
34
|
export interface TitanRequest {
|
|
35
|
+
/**
|
|
36
|
+
* The parsed request body.
|
|
37
|
+
*
|
|
38
|
+
* - For `POST`, `PUT`, and `PATCH` requests, this contains the parsed JSON payload.
|
|
39
|
+
* - For `GET` and `DELETE` requests, this is typically `null`.
|
|
40
|
+
*
|
|
41
|
+
* Titan automatically parses `application/json` bodies — no middleware needed.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```js
|
|
45
|
+
* export function createUser(req) {
|
|
46
|
+
* const { name, email } = req.body;
|
|
47
|
+
* return { created: true, name, email };
|
|
48
|
+
* }
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
4
51
|
body: any;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The HTTP method of the incoming request.
|
|
55
|
+
*
|
|
56
|
+
* Titan performs automatic method matching at the route level, so each
|
|
57
|
+
* action typically handles a single method. However, you can inspect
|
|
58
|
+
* this property to branch logic if needed.
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```js
|
|
62
|
+
* export function handler(req) {
|
|
63
|
+
* if (req.method === "POST") return createItem(req.body);
|
|
64
|
+
* if (req.method === "GET") return listItems();
|
|
65
|
+
* }
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
5
68
|
method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The full URL path of the request (e.g., `"/user/42"`).
|
|
72
|
+
*
|
|
73
|
+
* This is the raw matched path including resolved dynamic segments.
|
|
74
|
+
*/
|
|
6
75
|
path: string;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* All HTTP request headers as a flat key-value map.
|
|
79
|
+
*
|
|
80
|
+
* Header names are **lowercased** (e.g., `"content-type"`, `"authorization"`).
|
|
81
|
+
* A header may be `undefined` if it was not sent by the client.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```js
|
|
85
|
+
* export function secureAction(req) {
|
|
86
|
+
* const token = req.headers["authorization"];
|
|
87
|
+
* if (!token) return { error: "Unauthorized" };
|
|
88
|
+
* // ...
|
|
89
|
+
* }
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
7
92
|
headers: Record<string, string | undefined>;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Dynamic route parameters extracted from the URL path.
|
|
96
|
+
*
|
|
97
|
+
* Defined by route segments like `:id` or typed segments like `:id<number>`.
|
|
98
|
+
* Values are always delivered as **strings** — cast them as needed.
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```js
|
|
102
|
+
* // Route: /user/:id<number>
|
|
103
|
+
* export function getUser(req) {
|
|
104
|
+
* const id = Number(req.params.id); // "42" → 42
|
|
105
|
+
* return { id };
|
|
106
|
+
* }
|
|
107
|
+
* ```
|
|
108
|
+
*
|
|
109
|
+
* @see https://titan-docs-ez.vercel.app/docs/02-routes — Dynamic routes
|
|
110
|
+
*/
|
|
8
111
|
params: Record<string, string>;
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Parsed query string parameters from the URL.
|
|
115
|
+
*
|
|
116
|
+
* For a request to `/search?q=titan&page=2`, this would be:
|
|
117
|
+
* `{ q: "titan", page: "2" }`.
|
|
118
|
+
*
|
|
119
|
+
* Values are always strings. Returns an empty object `{}` when no
|
|
120
|
+
* query parameters are present.
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* ```js
|
|
124
|
+
* // GET /products?category=electronics&limit=10
|
|
125
|
+
* export function listProducts(req) {
|
|
126
|
+
* const category = req.query.category; // "electronics"
|
|
127
|
+
* const limit = Number(req.query.limit) || 20;
|
|
128
|
+
* return { category, limit };
|
|
129
|
+
* }
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
9
132
|
query: Record<string, string>;
|
|
10
133
|
}
|
|
11
134
|
|
|
135
|
+
/**
|
|
136
|
+
* Wraps an action handler function with type-safe request typing.
|
|
137
|
+
*
|
|
138
|
+
* `defineAction` is an optional helper that provides IntelliSense and
|
|
139
|
+
* type-checking for your action's `req` parameter and return value.
|
|
140
|
+
* It is **purely a development-time utility** — at runtime it simply
|
|
141
|
+
* returns the same function unchanged (zero overhead).
|
|
142
|
+
*
|
|
143
|
+
* @typeParam T - The return type of the action handler.
|
|
144
|
+
* @param handler - The action function that receives a `TitanRequest` and returns `T`.
|
|
145
|
+
* @returns The same handler function with proper type annotations.
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```js
|
|
149
|
+
* import { defineAction } from "titan";
|
|
150
|
+
*
|
|
151
|
+
* export const getUser = defineAction((req) => {
|
|
152
|
+
* // req is fully typed as TitanRequest
|
|
153
|
+
* const id = Number(req.params.id);
|
|
154
|
+
* return { id, method: req.method };
|
|
155
|
+
* });
|
|
156
|
+
* ```
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* ```ts
|
|
160
|
+
* // TypeScript — with explicit return type
|
|
161
|
+
* import { defineAction } from "titan";
|
|
162
|
+
*
|
|
163
|
+
* interface UserResponse { id: number; name: string; }
|
|
164
|
+
*
|
|
165
|
+
* export const getUser = defineAction<UserResponse>((req) => {
|
|
166
|
+
* return { id: Number(req.params.id), name: "Titan" };
|
|
167
|
+
* });
|
|
168
|
+
* ```
|
|
169
|
+
*
|
|
170
|
+
* @see https://titan-docs-ez.vercel.app/docs/03-actions — Action definition patterns
|
|
171
|
+
*/
|
|
12
172
|
export function defineAction<T>(
|
|
13
173
|
handler: (req: TitanRequest) => T
|
|
14
174
|
): (req: TitanRequest) => T;
|
|
15
175
|
|
|
176
|
+
/**
|
|
177
|
+
* Built-in Rust-powered HTTP client.
|
|
178
|
+
*
|
|
179
|
+
* Re-exported from the `t` global for module-style imports.
|
|
180
|
+
* @see {@link TitanRuntimeUtils.fetch} for full documentation.
|
|
181
|
+
*/
|
|
16
182
|
export const fetch: typeof t.fetch;
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Action-scoped logging utility.
|
|
186
|
+
*
|
|
187
|
+
* Re-exported from the `t` global for module-style imports.
|
|
188
|
+
* @see {@link TitanRuntimeUtils.log} for full documentation.
|
|
189
|
+
*/
|
|
17
190
|
export const log: typeof t.log;
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Synchronous file reader for local files.
|
|
194
|
+
*
|
|
195
|
+
* Re-exported from the `t` global for module-style imports.
|
|
196
|
+
* @see {@link TitanRuntimeUtils.read} for full documentation.
|
|
197
|
+
*/
|
|
18
198
|
export const read: typeof t.read;
|
|
19
199
|
|
|
200
|
+
/**
|
|
201
|
+
* JWT (JSON Web Token) signing and verification utilities.
|
|
202
|
+
*
|
|
203
|
+
* Re-exported from the `t` global for module-style imports.
|
|
204
|
+
* @see {@link TitanRuntimeUtils.jwt} for full documentation.
|
|
205
|
+
*/
|
|
20
206
|
export const jwt: typeof t.jwt;
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Secure password hashing and verification (bcrypt-based).
|
|
210
|
+
*
|
|
211
|
+
* Re-exported from the `t` global for module-style imports.
|
|
212
|
+
* @see {@link TitanRuntimeUtils.password} for full documentation.
|
|
213
|
+
*/
|
|
21
214
|
export const password: typeof t.password;
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Database connection and query interface.
|
|
218
|
+
*
|
|
219
|
+
* Re-exported from the `t` global for module-style imports.
|
|
220
|
+
* @see {@link TitanRuntimeUtils.db} for full documentation.
|
|
221
|
+
*/
|
|
22
222
|
export const db: typeof t.db;
|
|
23
223
|
|
|
224
|
+
/**
|
|
225
|
+
* Async file system operations (read, write, mkdir, stat, etc.).
|
|
226
|
+
*
|
|
227
|
+
* Re-exported from the `t` global for module-style imports.
|
|
228
|
+
* @see {@link TitanCore.FileSystem} for full documentation.
|
|
229
|
+
*/
|
|
24
230
|
export const fs: typeof t.fs;
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Path manipulation utilities (join, resolve, extname, etc.).
|
|
234
|
+
*
|
|
235
|
+
* Re-exported from the `t` global for module-style imports.
|
|
236
|
+
* @see {@link TitanCore.Path} for full documentation.
|
|
237
|
+
*/
|
|
25
238
|
export const path: typeof t.path;
|
|
26
239
|
|
|
240
|
+
/**
|
|
241
|
+
* Cryptographic utilities (hashing, encryption, UUIDs, etc.).
|
|
242
|
+
*
|
|
243
|
+
* Re-exported from the `t` global for module-style imports.
|
|
244
|
+
* @see {@link TitanCore.Crypto} for full documentation.
|
|
245
|
+
*/
|
|
27
246
|
export const crypto: typeof t.crypto;
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Buffer encoding/decoding utilities (Base64, Hex, UTF-8).
|
|
250
|
+
*
|
|
251
|
+
* Re-exported from the `t` global for module-style imports.
|
|
252
|
+
* @see {@link TitanCore.BufferModule} for full documentation.
|
|
253
|
+
*/
|
|
28
254
|
export const buffer: typeof t.buffer;
|
|
29
255
|
|
|
256
|
+
/**
|
|
257
|
+
* Persistent key-value local storage (shorthand alias).
|
|
258
|
+
*
|
|
259
|
+
* Re-exported from the `t` global for module-style imports.
|
|
260
|
+
* @see {@link TitanCore.LocalStorage} for full documentation.
|
|
261
|
+
*/
|
|
30
262
|
export const ls: typeof t.ls;
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Persistent key-value local storage.
|
|
266
|
+
*
|
|
267
|
+
* Re-exported from the `t` global for module-style imports.
|
|
268
|
+
* @see {@link TitanCore.LocalStorage} for full documentation.
|
|
269
|
+
*/
|
|
31
270
|
export const localStorage: typeof t.localStorage;
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Server-side session management by session ID.
|
|
274
|
+
*
|
|
275
|
+
* Re-exported from the `t` global for module-style imports.
|
|
276
|
+
* @see {@link TitanCore.Session} for full documentation.
|
|
277
|
+
*/
|
|
32
278
|
export const session: typeof t.session;
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* HTTP cookie read/write/delete utilities.
|
|
282
|
+
*
|
|
283
|
+
* Re-exported from the `t` global for module-style imports.
|
|
284
|
+
* @see {@link TitanCore.Cookies} for full documentation.
|
|
285
|
+
*/
|
|
33
286
|
export const cookies: typeof t.cookies;
|
|
34
287
|
|
|
288
|
+
/**
|
|
289
|
+
* Operating system information (platform, CPU count, memory).
|
|
290
|
+
*
|
|
291
|
+
* Re-exported from the `t` global for module-style imports.
|
|
292
|
+
* @see {@link TitanCore.OS} for full documentation.
|
|
293
|
+
*/
|
|
35
294
|
export const os: typeof t.os;
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Network utilities (DNS resolution, IP lookup, ping).
|
|
298
|
+
*
|
|
299
|
+
* Re-exported from the `t` global for module-style imports.
|
|
300
|
+
* @see {@link TitanCore.Net} for full documentation.
|
|
301
|
+
*/
|
|
36
302
|
export const net: typeof t.net;
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Process-level information (PID, uptime, memory usage).
|
|
306
|
+
*
|
|
307
|
+
* Re-exported from the `t` global for module-style imports.
|
|
308
|
+
* @see {@link TitanCore.Process} for full documentation.
|
|
309
|
+
*/
|
|
37
310
|
export const proc: typeof t.proc;
|
|
38
311
|
|
|
312
|
+
/**
|
|
313
|
+
* Time utilities (sleep, timestamps, high-resolution clock).
|
|
314
|
+
*
|
|
315
|
+
* Re-exported from the `t` global for module-style imports.
|
|
316
|
+
* @see {@link TitanCore.Time} for full documentation.
|
|
317
|
+
*/
|
|
39
318
|
export const time: typeof t.time;
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* URL parsing and formatting utilities.
|
|
322
|
+
*
|
|
323
|
+
* Re-exported from the `t` global for module-style imports.
|
|
324
|
+
* @see {@link TitanCore.URLModule} for full documentation.
|
|
325
|
+
*/
|
|
40
326
|
export const url: typeof t.url;
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* HTTP response builder for controlling status codes, headers, and content types.
|
|
330
|
+
*
|
|
331
|
+
* Re-exported from the `t` global for module-style imports.
|
|
332
|
+
* @see {@link TitanCore.ResponseModule} for full documentation.
|
|
333
|
+
*/
|
|
41
334
|
export const response: typeof t.response;
|
|
42
|
-
export const valid: any;
|
|
43
335
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
* ## Mechanism
|
|
52
|
-
* Drift utilizes a suspension model similar to **Algebraic Effects**. When a `drift()` operation is encountered,
|
|
53
|
-
* the runtime suspends the isolate, offloads the task to the background Tokio executor, and frees the isolate
|
|
54
|
-
* to handle other requests. Upon completion, the code is efficiently **re-played** with the result injected.
|
|
55
|
-
*
|
|
56
|
-
* @param promise - The promise or expression to drift.
|
|
57
|
-
* @returns The resolved value of the input promise.
|
|
58
|
-
*
|
|
59
|
-
* @example
|
|
60
|
-
* ```javascript
|
|
61
|
-
* const resp = drift t.fetch("http://api.titan.com");
|
|
62
|
-
* ```
|
|
336
|
+
/**
|
|
337
|
+
* Runtime validation utilities.
|
|
338
|
+
*
|
|
339
|
+
* Provides schema-based validation for request data. Works with
|
|
340
|
+
* the `@titanpl/valid` package for advanced validation rules.
|
|
341
|
+
*
|
|
342
|
+
* @see https://titan-docs-ez.vercel.app/docs/12-sdk — TitanPl SDK
|
|
63
343
|
*/
|
|
344
|
+
export const valid: any;
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
// ---------------------------------------------------------------------------
|
|
348
|
+
// Global Definitions — Runtime Environment
|
|
349
|
+
// ---------------------------------------------------------------------------
|
|
64
350
|
|
|
65
351
|
declare global {
|
|
352
|
+
|
|
353
|
+
// -----------------------------------------------------------------------
|
|
354
|
+
// Drift — Asynchronous Orchestration Engine
|
|
355
|
+
// -----------------------------------------------------------------------
|
|
356
|
+
|
|
66
357
|
/**
|
|
67
|
-
*
|
|
358
|
+
* # Drift — Deterministic Replay-based Suspension Engine
|
|
359
|
+
*
|
|
360
|
+
* `drift` is Titan's revolutionary mechanism for handling asynchronous
|
|
361
|
+
* operations inside the **strictly synchronous Gravity V8 runtime**.
|
|
362
|
+
*
|
|
363
|
+
* ## How it works
|
|
364
|
+
*
|
|
365
|
+
* When the runtime encounters a `drift()` call:
|
|
366
|
+
*
|
|
367
|
+
* 1. **Suspend** — The current V8 isolate is suspended (not blocked).
|
|
368
|
+
* 2. **Offload** — The async task is dispatched to Rust's Tokio executor.
|
|
369
|
+
* 3. **Free** — The isolate is released to handle other incoming requests.
|
|
370
|
+
* 4. **Replay** — Once the task completes, the action code is **re-played**
|
|
371
|
+
* from the beginning with the resolved value injected deterministically.
|
|
372
|
+
*
|
|
373
|
+
* This model is conceptually similar to **Algebraic Effects** — your code
|
|
374
|
+
* reads as synchronous while the runtime handles concurrency under the hood.
|
|
375
|
+
*
|
|
376
|
+
* ## Important notes
|
|
377
|
+
*
|
|
378
|
+
* - `drift` is the **only** way to await promises in Titan actions.
|
|
379
|
+
* - The action function may be re-executed (replayed) — avoid side effects
|
|
380
|
+
* before the `drift` call that shouldn't be repeated.
|
|
381
|
+
* - Can be used with any Titan API that returns a `Promise` (e.g.,
|
|
382
|
+
* `t.fetch`, `t.db.connect`, `t.password.hash`, `t.fs.readFile`, etc.).
|
|
383
|
+
*
|
|
384
|
+
* @typeParam T - The resolved type of the promise.
|
|
385
|
+
* @param promise - The promise or expression to drift (suspend and resolve).
|
|
386
|
+
* @returns The synchronously resolved value of the input promise.
|
|
387
|
+
*
|
|
388
|
+
* @example
|
|
389
|
+
* ```js
|
|
390
|
+
* // Basic fetch with drift
|
|
391
|
+
* export function getExternalData(req) {
|
|
392
|
+
* const resp = drift(t.fetch("https://api.example.com/data"));
|
|
393
|
+
* return { ok: resp.ok, data: JSON.parse(resp.body) };
|
|
394
|
+
* }
|
|
395
|
+
* ```
|
|
396
|
+
*
|
|
397
|
+
* @example
|
|
398
|
+
* ```js
|
|
399
|
+
* // Multiple drift calls (sequential)
|
|
400
|
+
* export function processOrder(req) {
|
|
401
|
+
* const user = drift(t.fetch("https://api.example.com/user/1"));
|
|
402
|
+
* const order = drift(t.fetch("https://api.example.com/order/99"));
|
|
403
|
+
* return { user: JSON.parse(user.body), order: JSON.parse(order.body) };
|
|
404
|
+
* }
|
|
405
|
+
* ```
|
|
406
|
+
*
|
|
407
|
+
* @example
|
|
408
|
+
* ```js
|
|
409
|
+
* // Drift with database operations
|
|
410
|
+
* export function getUsers(req) {
|
|
411
|
+
* const conn = drift(t.db.connect(process.env.DATABASE_URL));
|
|
412
|
+
* const users = drift(conn.query("SELECT * FROM users LIMIT 10"));
|
|
413
|
+
* return { users };
|
|
414
|
+
* }
|
|
415
|
+
* ```
|
|
416
|
+
*
|
|
417
|
+
* @see https://titan-docs-ez.vercel.app/docs/14-drift — Drift documentation
|
|
418
|
+
* @see https://titan-docs-ez.vercel.app/docs/runtime-architecture — Gravity Runtime
|
|
68
419
|
*/
|
|
69
420
|
var drift: <T>(promise: Promise<T> | T) => T;
|
|
70
421
|
|
|
71
422
|
|
|
423
|
+
// -----------------------------------------------------------------------
|
|
424
|
+
// Database Connection Interface
|
|
425
|
+
// -----------------------------------------------------------------------
|
|
72
426
|
|
|
427
|
+
/**
|
|
428
|
+
* Represents an active database connection returned by `t.db.connect()`.
|
|
429
|
+
*
|
|
430
|
+
* Provides a single `query()` method for executing SQL statements with
|
|
431
|
+
* optional parameterized values to prevent SQL injection.
|
|
432
|
+
*
|
|
433
|
+
* @example
|
|
434
|
+
* ```js
|
|
435
|
+
* export function listUsers(req) {
|
|
436
|
+
* const conn = drift(t.db.connect(process.env.DATABASE_URL));
|
|
437
|
+
*
|
|
438
|
+
* // Simple query
|
|
439
|
+
* const all = drift(conn.query("SELECT * FROM users"));
|
|
440
|
+
*
|
|
441
|
+
* // Parameterized query (safe from SQL injection)
|
|
442
|
+
* const one = drift(conn.query(
|
|
443
|
+
* "SELECT * FROM users WHERE id = $1",
|
|
444
|
+
* [req.params.id]
|
|
445
|
+
* ));
|
|
446
|
+
*
|
|
447
|
+
* return { all, user: one[0] };
|
|
448
|
+
* }
|
|
449
|
+
* ```
|
|
450
|
+
*
|
|
451
|
+
* @see https://titan-docs-ez.vercel.app/docs/04-runtime-apis — Runtime APIs
|
|
452
|
+
*/
|
|
73
453
|
interface DbConnection {
|
|
74
|
-
|
|
454
|
+
/**
|
|
455
|
+
* Execute a SQL query against the connected database.
|
|
456
|
+
*
|
|
457
|
+
* Supports parameterized queries using positional placeholders (`$1`, `$2`, etc.)
|
|
458
|
+
* to prevent SQL injection attacks.
|
|
459
|
+
*
|
|
460
|
+
* @param sql - The SQL query string. Use `$1`, `$2`, ... for parameter placeholders.
|
|
461
|
+
* @param params - Optional array of values to bind to the query placeholders (in order).
|
|
462
|
+
* @returns A promise that resolves to an array of result rows (as plain objects).
|
|
463
|
+
*
|
|
464
|
+
* @example
|
|
465
|
+
* ```js
|
|
466
|
+
* // SELECT with parameters
|
|
467
|
+
* const users = drift(conn.query(
|
|
468
|
+
* "SELECT * FROM users WHERE role = $1 AND active = $2",
|
|
469
|
+
* ["admin", true]
|
|
470
|
+
* ));
|
|
471
|
+
*
|
|
472
|
+
* // INSERT
|
|
473
|
+
* drift(conn.query(
|
|
474
|
+
* "INSERT INTO users (name, email) VALUES ($1, $2)",
|
|
475
|
+
* ["Alice", "alice@example.com"]
|
|
476
|
+
* ));
|
|
477
|
+
*
|
|
478
|
+
* // UPDATE
|
|
479
|
+
* drift(conn.query(
|
|
480
|
+
* "UPDATE users SET name = $1 WHERE id = $2",
|
|
481
|
+
* ["Bob", 42]
|
|
482
|
+
* ));
|
|
483
|
+
* ```
|
|
484
|
+
*/
|
|
485
|
+
query(sql: string, params?: any[]): Promise<any[]>;
|
|
75
486
|
}
|
|
76
487
|
|
|
488
|
+
|
|
489
|
+
// -----------------------------------------------------------------------
|
|
490
|
+
// Titan Runtime Utils — The `t` / `Titan` global object
|
|
491
|
+
// -----------------------------------------------------------------------
|
|
492
|
+
|
|
493
|
+
/**
|
|
494
|
+
* The Titan Runtime Utilities interface — the core API surface available
|
|
495
|
+
* globally as `t` (or `Titan`) in every action.
|
|
496
|
+
*
|
|
497
|
+
* All methods on `t` are powered by native Rust implementations under
|
|
498
|
+
* the hood, providing near-zero overhead and memory safety. Async methods
|
|
499
|
+
* must be consumed via the `drift()` operator.
|
|
500
|
+
*
|
|
501
|
+
* @example
|
|
502
|
+
* ```js
|
|
503
|
+
* // The `t` object is always available — no imports needed
|
|
504
|
+
* export function myAction(req) {
|
|
505
|
+
* t.log("Request received:", req.method, req.path);
|
|
506
|
+
* const resp = drift(t.fetch("https://api.example.com/data"));
|
|
507
|
+
* return { status: resp.status, body: JSON.parse(resp.body) };
|
|
508
|
+
* }
|
|
509
|
+
* ```
|
|
510
|
+
*
|
|
511
|
+
* @see https://titan-docs-ez.vercel.app/docs/04-runtime-apis — Complete Runtime APIs reference
|
|
512
|
+
*/
|
|
77
513
|
interface TitanRuntimeUtils {
|
|
514
|
+
|
|
515
|
+
// -------------------------------------------------------------------
|
|
516
|
+
// Core I/O
|
|
517
|
+
// -------------------------------------------------------------------
|
|
518
|
+
|
|
519
|
+
/**
|
|
520
|
+
* Action-scoped, sandboxed logging utility.
|
|
521
|
+
*
|
|
522
|
+
* Writes messages to the Titan **Gravity Logs** system. Logs are
|
|
523
|
+
* prefixed with the action name for easy filtering and debugging.
|
|
524
|
+
* Accepts any number of arguments of any type (they are serialized
|
|
525
|
+
* automatically).
|
|
526
|
+
*
|
|
527
|
+
* Output in the terminal appears as:
|
|
528
|
+
* ```
|
|
529
|
+
* [Titan] log(myAction): your message here
|
|
530
|
+
* ```
|
|
531
|
+
*
|
|
532
|
+
* @param args - One or more values to log (strings, numbers, objects, etc.).
|
|
533
|
+
*
|
|
534
|
+
* @example
|
|
535
|
+
* ```js
|
|
536
|
+
* t.log("Processing user", req.params.id);
|
|
537
|
+
* t.log("Body received:", req.body);
|
|
538
|
+
* t.log("Multiple", "values", { are: "supported" }, 42);
|
|
539
|
+
* ```
|
|
540
|
+
*
|
|
541
|
+
* @see https://titan-docs-ez.vercel.app/docs/06-logs — Gravity Logs
|
|
542
|
+
*/
|
|
78
543
|
log(...args: any[]): void;
|
|
544
|
+
|
|
545
|
+
/**
|
|
546
|
+
* Synchronously reads the contents of a local file as a UTF-8 string.
|
|
547
|
+
*
|
|
548
|
+
* This is a **blocking, synchronous** operation — suitable for reading
|
|
549
|
+
* small configuration files, templates, or static assets at startup.
|
|
550
|
+
* For larger or async file operations, prefer `t.fs.readFile()` with `drift()`.
|
|
551
|
+
*
|
|
552
|
+
* @param path - Absolute or relative path to the file to read.
|
|
553
|
+
* @returns The file contents as a UTF-8 string.
|
|
554
|
+
* @throws If the file does not exist or cannot be read.
|
|
555
|
+
*
|
|
556
|
+
* @example
|
|
557
|
+
* ```js
|
|
558
|
+
* export function getConfig(req) {
|
|
559
|
+
* const raw = t.read("./config.json");
|
|
560
|
+
* return JSON.parse(raw);
|
|
561
|
+
* }
|
|
562
|
+
* ```
|
|
563
|
+
*
|
|
564
|
+
* @see https://titan-docs-ez.vercel.app/docs/04-runtime-apis — Runtime APIs
|
|
565
|
+
*/
|
|
79
566
|
read(path: string): string;
|
|
567
|
+
|
|
568
|
+
/**
|
|
569
|
+
* Built-in Rust-powered HTTP client for making outbound requests.
|
|
570
|
+
*
|
|
571
|
+
* Powered by Rust's native HTTP stack — **not** `node-fetch` or any
|
|
572
|
+
* JS-based client. Provides high-performance, non-blocking HTTP calls.
|
|
573
|
+
*
|
|
574
|
+
* Returns a `Promise` — use with `drift()` to resolve it inside an action.
|
|
575
|
+
*
|
|
576
|
+
* @param url - The target URL to request (must include protocol, e.g. `"https://..."`).
|
|
577
|
+
* @param options - Optional request configuration.
|
|
578
|
+
* @param options.method - HTTP method. Defaults to `"GET"`.
|
|
579
|
+
* @param options.headers - Key-value map of request headers.
|
|
580
|
+
* @param options.body - Request body. Strings are sent as-is; objects are
|
|
581
|
+
* automatically JSON-serialized with `Content-Type: application/json`.
|
|
582
|
+
*
|
|
583
|
+
* @returns A promise resolving to a response object with:
|
|
584
|
+
* - `ok` — `true` if the status code is 2xx.
|
|
585
|
+
* - `status` — The HTTP status code (e.g., `200`, `404`, `500`).
|
|
586
|
+
* - `body` — The response body as a string (parse with `JSON.parse()` if needed).
|
|
587
|
+
* - `error` — An error message string if the request failed at the network level.
|
|
588
|
+
*
|
|
589
|
+
* @example
|
|
590
|
+
* ```js
|
|
591
|
+
* // Simple GET
|
|
592
|
+
* export function getData(req) {
|
|
593
|
+
* const resp = drift(t.fetch("https://api.example.com/items"));
|
|
594
|
+
* return JSON.parse(resp.body);
|
|
595
|
+
* }
|
|
596
|
+
* ```
|
|
597
|
+
*
|
|
598
|
+
* @example
|
|
599
|
+
* ```js
|
|
600
|
+
* // POST with JSON body and headers
|
|
601
|
+
* export function createItem(req) {
|
|
602
|
+
* const resp = drift(t.fetch("https://api.example.com/items", {
|
|
603
|
+
* method: "POST",
|
|
604
|
+
* headers: {
|
|
605
|
+
* "Authorization": "Bearer " + process.env.API_KEY,
|
|
606
|
+
* "Content-Type": "application/json"
|
|
607
|
+
* },
|
|
608
|
+
* body: { name: req.body.name, price: req.body.price }
|
|
609
|
+
* }));
|
|
610
|
+
*
|
|
611
|
+
* if (!resp.ok) return { error: "Failed", status: resp.status };
|
|
612
|
+
* return JSON.parse(resp.body);
|
|
613
|
+
* }
|
|
614
|
+
* ```
|
|
615
|
+
*
|
|
616
|
+
* @example
|
|
617
|
+
* ```js
|
|
618
|
+
* // Error handling
|
|
619
|
+
* export function safeFetch(req) {
|
|
620
|
+
* const resp = drift(t.fetch("https://unreliable-api.com/data"));
|
|
621
|
+
* if (resp.error) return { error: resp.error };
|
|
622
|
+
* if (!resp.ok) return { error: `HTTP ${resp.status}` };
|
|
623
|
+
* return JSON.parse(resp.body);
|
|
624
|
+
* }
|
|
625
|
+
* ```
|
|
626
|
+
*
|
|
627
|
+
* @see https://titan-docs-ez.vercel.app/docs/04-runtime-apis — Runtime APIs (t.fetch)
|
|
628
|
+
*/
|
|
80
629
|
fetch(url: string, options?: {
|
|
81
630
|
method?: "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
|
|
82
631
|
headers?: Record<string, string>;
|
|
@@ -88,167 +637,1391 @@ declare global {
|
|
|
88
637
|
error?: string;
|
|
89
638
|
}>;
|
|
90
639
|
|
|
640
|
+
|
|
641
|
+
// -------------------------------------------------------------------
|
|
642
|
+
// Authentication & Security
|
|
643
|
+
// -------------------------------------------------------------------
|
|
644
|
+
|
|
645
|
+
/**
|
|
646
|
+
* JSON Web Token (JWT) utilities for stateless authentication.
|
|
647
|
+
*
|
|
648
|
+
* Provides `sign` and `verify` methods backed by Rust cryptographic
|
|
649
|
+
* implementations. Ideal for issuing access tokens and validating
|
|
650
|
+
* incoming Bearer tokens in API actions.
|
|
651
|
+
*
|
|
652
|
+
* > **Note:** Both methods are **synchronous** — no `drift()` needed.
|
|
653
|
+
*
|
|
654
|
+
* @example
|
|
655
|
+
* ```js
|
|
656
|
+
* // Sign a token
|
|
657
|
+
* export function login(req) {
|
|
658
|
+
* const { username, password } = req.body;
|
|
659
|
+
* // ... validate credentials ...
|
|
660
|
+
* const token = t.jwt.sign(
|
|
661
|
+
* { sub: userId, role: "admin" },
|
|
662
|
+
* process.env.JWT_SECRET,
|
|
663
|
+
* { expiresIn: "7d" }
|
|
664
|
+
* );
|
|
665
|
+
* return { token };
|
|
666
|
+
* }
|
|
667
|
+
*
|
|
668
|
+
* // Verify a token
|
|
669
|
+
* export function protectedAction(req) {
|
|
670
|
+
* const token = req.headers["authorization"]?.replace("Bearer ", "");
|
|
671
|
+
* try {
|
|
672
|
+
* const payload = t.jwt.verify(token, process.env.JWT_SECRET);
|
|
673
|
+
* return { userId: payload.sub, role: payload.role };
|
|
674
|
+
* } catch (e) {
|
|
675
|
+
* return t.response.json({ error: "Invalid token" }, 401);
|
|
676
|
+
* }
|
|
677
|
+
* }
|
|
678
|
+
* ```
|
|
679
|
+
*
|
|
680
|
+
* @see https://titan-docs-ez.vercel.app/docs/04-runtime-apis — Runtime APIs (t.jwt)
|
|
681
|
+
*/
|
|
91
682
|
jwt: {
|
|
683
|
+
/**
|
|
684
|
+
* Create a signed JWT from a payload object.
|
|
685
|
+
*
|
|
686
|
+
* @param payload - The data to encode in the token (e.g., `{ sub: "user123", role: "admin" }`).
|
|
687
|
+
* Avoid putting sensitive data here — JWTs are encoded, not encrypted.
|
|
688
|
+
* @param secret - The secret key used for HMAC signing. Store in environment variables.
|
|
689
|
+
* @param options - Optional signing options.
|
|
690
|
+
* @param options.expiresIn - Token expiration time.
|
|
691
|
+
* Accepts duration strings (`"1h"`, `"7d"`, `"30m"`) or
|
|
692
|
+
* a number representing seconds.
|
|
693
|
+
* @returns The signed JWT as a string (e.g., `"eyJhbGciOi..."`).
|
|
694
|
+
*
|
|
695
|
+
* @example
|
|
696
|
+
* ```js
|
|
697
|
+
* const token = t.jwt.sign({ userId: 1 }, "my-secret", { expiresIn: "24h" });
|
|
698
|
+
* ```
|
|
699
|
+
*/
|
|
92
700
|
sign(payload: object, secret: string, options?: { expiresIn?: string | number }): string;
|
|
701
|
+
|
|
702
|
+
/**
|
|
703
|
+
* Verify and decode a JWT, returning the original payload.
|
|
704
|
+
*
|
|
705
|
+
* @param token - The JWT string to verify.
|
|
706
|
+
* @param secret - The secret key used to verify the signature.
|
|
707
|
+
* @returns The decoded payload object if the token is valid and not expired.
|
|
708
|
+
* @throws If the token is invalid, expired, or the signature doesn't match.
|
|
709
|
+
*
|
|
710
|
+
* @example
|
|
711
|
+
* ```js
|
|
712
|
+
* try {
|
|
713
|
+
* const payload = t.jwt.verify(token, process.env.JWT_SECRET);
|
|
714
|
+
* t.log("User:", payload.sub);
|
|
715
|
+
* } catch (err) {
|
|
716
|
+
* t.log("Token verification failed");
|
|
717
|
+
* }
|
|
718
|
+
* ```
|
|
719
|
+
*/
|
|
93
720
|
verify(token: string, secret: string): any;
|
|
94
721
|
};
|
|
95
722
|
|
|
723
|
+
/**
|
|
724
|
+
* Secure password hashing and verification powered by bcrypt (Rust implementation).
|
|
725
|
+
*
|
|
726
|
+
* Both methods return `Promise` values — use `drift()` to resolve them.
|
|
727
|
+
*
|
|
728
|
+
* @example
|
|
729
|
+
* ```js
|
|
730
|
+
* // Registration: hash the password before storing
|
|
731
|
+
* export function register(req) {
|
|
732
|
+
* const { email, password } = req.body;
|
|
733
|
+
* const hashed = drift(t.password.hash(password));
|
|
734
|
+
* // Store `hashed` in your database
|
|
735
|
+
* return { success: true, email };
|
|
736
|
+
* }
|
|
737
|
+
*
|
|
738
|
+
* // Login: compare submitted password against stored hash
|
|
739
|
+
* export function login(req) {
|
|
740
|
+
* const { email, password } = req.body;
|
|
741
|
+
* // Retrieve `storedHash` from your database
|
|
742
|
+
* const valid = drift(t.password.verify(password, storedHash));
|
|
743
|
+
* if (!valid) return t.response.json({ error: "Invalid credentials" }, 401);
|
|
744
|
+
* return { token: t.jwt.sign({ email }, process.env.JWT_SECRET) };
|
|
745
|
+
* }
|
|
746
|
+
* ```
|
|
747
|
+
*
|
|
748
|
+
* @see https://titan-docs-ez.vercel.app/docs/04-runtime-apis — Runtime APIs (t.password)
|
|
749
|
+
*/
|
|
96
750
|
password: {
|
|
97
|
-
|
|
98
|
-
|
|
751
|
+
/**
|
|
752
|
+
* Hash a plain-text password using bcrypt.
|
|
753
|
+
*
|
|
754
|
+
* Automatically generates a secure salt. The resulting hash string
|
|
755
|
+
* includes the salt, making it safe to store directly in a database.
|
|
756
|
+
*
|
|
757
|
+
* @param password - The plain-text password to hash.
|
|
758
|
+
* @returns A promise resolving to the bcrypt hash string.
|
|
759
|
+
*
|
|
760
|
+
* @example
|
|
761
|
+
* ```js
|
|
762
|
+
* const hash = drift(t.password.hash("my-secure-password"));
|
|
763
|
+
* // hash → "$2b$12$LJ3m4ys..."
|
|
764
|
+
* ```
|
|
765
|
+
*/
|
|
766
|
+
hash(password: string): Promise<string>;
|
|
767
|
+
|
|
768
|
+
/**
|
|
769
|
+
* Verify a plain-text password against a previously hashed value.
|
|
770
|
+
*
|
|
771
|
+
* @param password - The plain-text password to check.
|
|
772
|
+
* @param hash - The bcrypt hash string to compare against.
|
|
773
|
+
* @returns A promise resolving to `true` if the password matches, `false` otherwise.
|
|
774
|
+
*
|
|
775
|
+
* @example
|
|
776
|
+
* ```js
|
|
777
|
+
* const isValid = drift(t.password.verify("my-password", storedHash));
|
|
778
|
+
* if (!isValid) return { error: "Wrong password" };
|
|
779
|
+
* ```
|
|
780
|
+
*/
|
|
781
|
+
verify(password: string, hash: string): Promise<boolean>;
|
|
99
782
|
};
|
|
100
783
|
|
|
101
|
-
|
|
784
|
+
|
|
785
|
+
// -------------------------------------------------------------------
|
|
786
|
+
// Database
|
|
787
|
+
// -------------------------------------------------------------------
|
|
788
|
+
|
|
789
|
+
/**
|
|
790
|
+
* Database connection interface.
|
|
791
|
+
*
|
|
792
|
+
* Establish connections to SQL databases (PostgreSQL, MySQL, SQLite, etc.)
|
|
793
|
+
* using a connection URL. Returns a `DbConnection` instance for executing queries.
|
|
794
|
+
*
|
|
795
|
+
* > **Important:** `connect()` returns a `Promise` — always wrap it with `drift()`.
|
|
796
|
+
*
|
|
797
|
+
* @example
|
|
798
|
+
* ```js
|
|
799
|
+
* export function getUsers(req) {
|
|
800
|
+
* const conn = drift(t.db.connect(process.env.DATABASE_URL));
|
|
801
|
+
* const users = drift(conn.query("SELECT id, name, email FROM users"));
|
|
802
|
+
* return { users };
|
|
803
|
+
* }
|
|
804
|
+
* ```
|
|
805
|
+
*
|
|
806
|
+
* @example
|
|
807
|
+
* ```js
|
|
808
|
+
* // Full CRUD example
|
|
809
|
+
* export function createUser(req) {
|
|
810
|
+
* const conn = drift(t.db.connect(process.env.DATABASE_URL));
|
|
811
|
+
* const { name, email } = req.body;
|
|
812
|
+
*
|
|
813
|
+
* drift(conn.query(
|
|
814
|
+
* "INSERT INTO users (name, email) VALUES ($1, $2)",
|
|
815
|
+
* [name, email]
|
|
816
|
+
* ));
|
|
817
|
+
*
|
|
818
|
+
* return { created: true, name, email };
|
|
819
|
+
* }
|
|
820
|
+
* ```
|
|
821
|
+
*
|
|
822
|
+
* @see https://titan-docs-ez.vercel.app/docs/04-runtime-apis — Runtime APIs (t.db)
|
|
823
|
+
*/
|
|
102
824
|
db: {
|
|
103
|
-
|
|
825
|
+
/**
|
|
826
|
+
* Establish a database connection.
|
|
827
|
+
*
|
|
828
|
+
* @param url - A database connection string.
|
|
829
|
+
*
|
|
830
|
+
* Supported formats:
|
|
831
|
+
* - PostgreSQL: `"postgres://user:pass@host:5432/dbname"`
|
|
832
|
+
* - MySQL: `"mysql://user:pass@host:3306/dbname"`
|
|
833
|
+
* - SQLite: `"sqlite://./data.db"`
|
|
834
|
+
*
|
|
835
|
+
* @returns A promise resolving to a {@link DbConnection} instance.
|
|
836
|
+
*
|
|
837
|
+
* @example
|
|
838
|
+
* ```js
|
|
839
|
+
* const conn = drift(
|
|
840
|
+
* t.db.connect("postgres://admin:secret@localhost:5432/mydb")
|
|
841
|
+
* );
|
|
842
|
+
* ```
|
|
843
|
+
*/
|
|
844
|
+
connect(url: string): Promise<DbConnection>;
|
|
845
|
+
|
|
846
|
+
/**
|
|
847
|
+
* Execute a SQL query using the default database connection.
|
|
848
|
+
*
|
|
849
|
+
* Uses the configured `DATABASE_URL` internally.
|
|
850
|
+
* Ideal for simple and one-off queries.
|
|
851
|
+
*
|
|
852
|
+
* @example
|
|
853
|
+
* ```js
|
|
854
|
+
* const users = drift(
|
|
855
|
+
* t.db.query("SELECT * FROM users")
|
|
856
|
+
* );
|
|
857
|
+
* ```
|
|
858
|
+
*
|
|
859
|
+
* @example
|
|
860
|
+
* ```js
|
|
861
|
+
* const user = drift(
|
|
862
|
+
* t.db.query(
|
|
863
|
+
* "SELECT * FROM users WHERE id = $1",
|
|
864
|
+
* [42]
|
|
865
|
+
* )
|
|
866
|
+
* );
|
|
867
|
+
* ```
|
|
868
|
+
*
|
|
869
|
+
* @example
|
|
870
|
+
* ```js
|
|
871
|
+
* const sql = drift(t.fs.readFile("./db/login.sql"));
|
|
872
|
+
* const rows = drift(t.db.query(sql, [email, hash]));
|
|
873
|
+
* ```
|
|
874
|
+
*
|
|
875
|
+
* @param sql - SQL query string.
|
|
876
|
+
* @param params - Optional positional parameters.
|
|
877
|
+
* @returns A promise resolving to query result rows.
|
|
878
|
+
*/
|
|
879
|
+
query(sql: string, params?: any[]): Promise<any[]>;
|
|
104
880
|
};
|
|
105
881
|
|
|
106
|
-
|
|
882
|
+
|
|
883
|
+
|
|
884
|
+
// -------------------------------------------------------------------
|
|
885
|
+
// File System & Paths
|
|
886
|
+
// -------------------------------------------------------------------
|
|
887
|
+
|
|
888
|
+
/**
|
|
889
|
+
* Asynchronous file system operations.
|
|
890
|
+
*
|
|
891
|
+
* Provides async methods for reading, writing, and managing files and
|
|
892
|
+
* directories. All methods return `Promise` — use `drift()` to resolve.
|
|
893
|
+
*
|
|
894
|
+
* @see {@link TitanCore.FileSystem} for method signatures.
|
|
895
|
+
* @see https://titan-docs-ez.vercel.app/docs/13-titan-core — TitanCore Runtime APIs (t.fs)
|
|
896
|
+
*/
|
|
107
897
|
fs: TitanCore.FileSystem;
|
|
108
898
|
|
|
109
|
-
/**
|
|
899
|
+
/**
|
|
900
|
+
* Path manipulation utilities.
|
|
901
|
+
*
|
|
902
|
+
* All methods are **synchronous** — no `drift()` needed. Provides
|
|
903
|
+
* cross-platform path joining, resolving, and component extraction.
|
|
904
|
+
*
|
|
905
|
+
* @see {@link TitanCore.Path} for method signatures.
|
|
906
|
+
* @see https://titan-docs-ez.vercel.app/docs/13-titan-core — TitanCore Runtime APIs (t.path)
|
|
907
|
+
*/
|
|
110
908
|
path: TitanCore.Path;
|
|
111
909
|
|
|
112
|
-
|
|
910
|
+
|
|
911
|
+
// -------------------------------------------------------------------
|
|
912
|
+
// Cryptography & Encoding
|
|
913
|
+
// -------------------------------------------------------------------
|
|
914
|
+
|
|
915
|
+
/**
|
|
916
|
+
* Cryptographic utilities powered by Rust's native crypto libraries.
|
|
917
|
+
*
|
|
918
|
+
* Includes hashing (SHA-256, SHA-512, MD5), HMAC, symmetric encryption/decryption,
|
|
919
|
+
* random bytes generation, and UUID creation. Async methods require `drift()`.
|
|
920
|
+
*
|
|
921
|
+
* @see {@link TitanCore.Crypto} for method signatures.
|
|
922
|
+
* @see https://titan-docs-ez.vercel.app/docs/13-titan-core — TitanCore Runtime APIs (t.crypto)
|
|
923
|
+
*/
|
|
113
924
|
crypto: TitanCore.Crypto;
|
|
114
925
|
|
|
115
|
-
/**
|
|
926
|
+
/**
|
|
927
|
+
* Buffer encoding and decoding utilities for Base64, Hex, and UTF-8 conversions.
|
|
928
|
+
*
|
|
929
|
+
* All methods are **synchronous** — no `drift()` needed.
|
|
930
|
+
*
|
|
931
|
+
* @see {@link TitanCore.BufferModule} for method signatures.
|
|
932
|
+
* @see https://titan-docs-ez.vercel.app/docs/13-titan-core — TitanCore Runtime APIs (t.buffer)
|
|
933
|
+
*/
|
|
116
934
|
buffer: TitanCore.BufferModule;
|
|
117
935
|
|
|
118
|
-
|
|
936
|
+
|
|
937
|
+
// -------------------------------------------------------------------
|
|
938
|
+
// Storage & State
|
|
939
|
+
// -------------------------------------------------------------------
|
|
940
|
+
|
|
941
|
+
/**
|
|
942
|
+
* Persistent key-value local storage (shorthand alias for `t.localStorage`).
|
|
943
|
+
*
|
|
944
|
+
* Data persists across requests and server restarts. All methods are
|
|
945
|
+
* **synchronous**. Useful for caching, feature flags, or small config values.
|
|
946
|
+
*
|
|
947
|
+
* @see {@link TitanCore.LocalStorage} for method signatures.
|
|
948
|
+
* @see https://titan-docs-ez.vercel.app/docs/13-titan-core — TitanCore Runtime APIs (t.ls)
|
|
949
|
+
*/
|
|
119
950
|
ls: TitanCore.LocalStorage;
|
|
951
|
+
|
|
952
|
+
/**
|
|
953
|
+
* Persistent key-value local storage.
|
|
954
|
+
*
|
|
955
|
+
* Data persists across requests and server restarts. All methods are
|
|
956
|
+
* **synchronous**. Identical to `t.ls` — use whichever alias you prefer.
|
|
957
|
+
*
|
|
958
|
+
* @see {@link TitanCore.LocalStorage} for method signatures.
|
|
959
|
+
* @see https://titan-docs-ez.vercel.app/docs/13-titan-core — TitanCore Runtime APIs (t.localStorage)
|
|
960
|
+
*/
|
|
120
961
|
localStorage: TitanCore.LocalStorage;
|
|
121
962
|
|
|
122
|
-
/**
|
|
963
|
+
/**
|
|
964
|
+
* Server-side session management.
|
|
965
|
+
*
|
|
966
|
+
* Store and retrieve data by session ID. Sessions are scoped per client
|
|
967
|
+
* and useful for tracking authentication state, user preferences, or
|
|
968
|
+
* multi-step form data.
|
|
969
|
+
*
|
|
970
|
+
* All methods are **synchronous**.
|
|
971
|
+
*
|
|
972
|
+
* @see {@link TitanCore.Session} for method signatures.
|
|
973
|
+
* @see https://titan-docs-ez.vercel.app/docs/13-titan-core — TitanCore Runtime APIs (t.session)
|
|
974
|
+
*/
|
|
123
975
|
session: TitanCore.Session;
|
|
124
976
|
|
|
125
|
-
/**
|
|
977
|
+
/**
|
|
978
|
+
* HTTP cookie utilities for reading, setting, and deleting cookies.
|
|
979
|
+
*
|
|
980
|
+
* @see {@link TitanCore.Cookies} for method signatures.
|
|
981
|
+
* @see https://titan-docs-ez.vercel.app/docs/13-titan-core — TitanCore Runtime APIs (t.cookies)
|
|
982
|
+
*/
|
|
126
983
|
cookies: TitanCore.Cookies;
|
|
127
984
|
|
|
128
|
-
|
|
985
|
+
|
|
986
|
+
// -------------------------------------------------------------------
|
|
987
|
+
// System & Network
|
|
988
|
+
// -------------------------------------------------------------------
|
|
989
|
+
|
|
990
|
+
/**
|
|
991
|
+
* Operating system information.
|
|
992
|
+
*
|
|
993
|
+
* Retrieve platform details, CPU count, and memory statistics of the
|
|
994
|
+
* host machine running the Titan server.
|
|
995
|
+
*
|
|
996
|
+
* @see {@link TitanCore.OS} for method signatures.
|
|
997
|
+
* @see https://titan-docs-ez.vercel.app/docs/13-titan-core — TitanCore Runtime APIs (t.os)
|
|
998
|
+
*/
|
|
129
999
|
os: TitanCore.OS;
|
|
130
1000
|
|
|
131
|
-
/**
|
|
1001
|
+
/**
|
|
1002
|
+
* Network utilities for DNS resolution, IP lookup, and host pinging.
|
|
1003
|
+
*
|
|
1004
|
+
* All methods return `Promise` — use `drift()` to resolve.
|
|
1005
|
+
*
|
|
1006
|
+
* @see {@link TitanCore.Net} for method signatures.
|
|
1007
|
+
* @see https://titan-docs-ez.vercel.app/docs/13-titan-core — TitanCore Runtime APIs (t.net)
|
|
1008
|
+
*/
|
|
132
1009
|
net: TitanCore.Net;
|
|
133
1010
|
|
|
134
|
-
/**
|
|
1011
|
+
/**
|
|
1012
|
+
* Process-level information for the running Titan server binary.
|
|
1013
|
+
*
|
|
1014
|
+
* All methods are **synchronous**.
|
|
1015
|
+
*
|
|
1016
|
+
* @see {@link TitanCore.Process} for method signatures.
|
|
1017
|
+
* @see https://titan-docs-ez.vercel.app/docs/13-titan-core — TitanCore Runtime APIs (t.proc)
|
|
1018
|
+
*/
|
|
135
1019
|
proc: TitanCore.Process;
|
|
136
1020
|
|
|
137
|
-
|
|
1021
|
+
|
|
1022
|
+
// -------------------------------------------------------------------
|
|
1023
|
+
// Utilities
|
|
1024
|
+
// -------------------------------------------------------------------
|
|
1025
|
+
|
|
1026
|
+
/**
|
|
1027
|
+
* Time-related utilities including sleep, timestamps, and high-resolution clock.
|
|
1028
|
+
*
|
|
1029
|
+
* `t.time.sleep()` is async (requires `drift()`); other methods are synchronous.
|
|
1030
|
+
*
|
|
1031
|
+
* @see {@link TitanCore.Time} for method signatures.
|
|
1032
|
+
* @see https://titan-docs-ez.vercel.app/docs/13-titan-core — TitanCore Runtime APIs (t.time)
|
|
1033
|
+
*/
|
|
138
1034
|
time: TitanCore.Time;
|
|
139
1035
|
|
|
140
|
-
/**
|
|
1036
|
+
/**
|
|
1037
|
+
* URL parsing and formatting utilities.
|
|
1038
|
+
*
|
|
1039
|
+
* @see {@link TitanCore.URLModule} for method signatures.
|
|
1040
|
+
* @see https://titan-docs-ez.vercel.app/docs/13-titan-core — TitanCore Runtime APIs (t.url)
|
|
1041
|
+
*/
|
|
141
1042
|
url: TitanCore.URLModule;
|
|
142
1043
|
|
|
143
|
-
/**
|
|
1044
|
+
/**
|
|
1045
|
+
|
|
1046
|
+
|
|
1047
|
+
/**
|
|
1048
|
+
* HTTP response builder utilities.
|
|
1049
|
+
* @see https://www.npmjs.com/package/@titanpl/13-titan-core — TitanCore Valid Extension
|
|
1050
|
+
*/
|
|
144
1051
|
response: TitanCore.ResponseModule;
|
|
145
1052
|
|
|
1053
|
+
|
|
1054
|
+
|
|
1055
|
+
/**
|
|
1056
|
+
* Runtime validation utilities.
|
|
1057
|
+
*
|
|
1058
|
+
* Provides schema-based validation for request payloads and other data.
|
|
1059
|
+
* Works with the `@titanpl/valid` package for advanced rules.
|
|
1060
|
+
*
|
|
1061
|
+
* @see https://www.npmjs.com/package/@titanpl/valid — TitanPl Valid Extension
|
|
1062
|
+
*/
|
|
146
1063
|
valid: any;
|
|
1064
|
+
|
|
1065
|
+
/**
|
|
1066
|
+
* Extension index signature — allows access to dynamically loaded
|
|
1067
|
+
* Titan Extensions registered via `titan create ext`.
|
|
1068
|
+
*
|
|
1069
|
+
* Custom extensions attach their methods to the `t` object at runtime,
|
|
1070
|
+
* making them available as `t.myExtension.someMethod()`.
|
|
1071
|
+
*
|
|
1072
|
+
* @see https://titan-docs-ez.vercel.app/docs/10-extensions — Extensions documentation
|
|
1073
|
+
*/
|
|
147
1074
|
[key: string]: any;
|
|
148
1075
|
}
|
|
149
1076
|
|
|
1077
|
+
/**
|
|
1078
|
+
* The primary global Titan runtime object.
|
|
1079
|
+
*
|
|
1080
|
+
* Available in every action without imports. Provides access to all
|
|
1081
|
+
* built-in Titan APIs: HTTP client, logging, JWT, database, file system,
|
|
1082
|
+
* crypto, storage, sessions, cookies, OS info, networking, and more.
|
|
1083
|
+
*
|
|
1084
|
+
* @example
|
|
1085
|
+
* ```js
|
|
1086
|
+
* export function myAction(req) {
|
|
1087
|
+
* t.log("Hello from Titan!");
|
|
1088
|
+
* return { message: "It works" };
|
|
1089
|
+
* }
|
|
1090
|
+
* ```
|
|
1091
|
+
*
|
|
1092
|
+
* @see https://titan-docs-ez.vercel.app/docs/04-runtime-apis — Full API reference
|
|
1093
|
+
* @see https://titan-docs-ez.vercel.app/docs/13-titan-core — TitanCore Runtime APIs
|
|
1094
|
+
*/
|
|
150
1095
|
const t: TitanRuntimeUtils;
|
|
1096
|
+
|
|
1097
|
+
/**
|
|
1098
|
+
* Alias for the `t` global runtime object.
|
|
1099
|
+
*
|
|
1100
|
+
* `Titan` and `t` are interchangeable — use whichever you prefer.
|
|
1101
|
+
* Both reference the exact same runtime utilities instance.
|
|
1102
|
+
*
|
|
1103
|
+
* @example
|
|
1104
|
+
* ```js
|
|
1105
|
+
* export function myAction(req) {
|
|
1106
|
+
* Titan.log("Using the Titan alias");
|
|
1107
|
+
* const resp = drift(Titan.fetch("https://api.example.com"));
|
|
1108
|
+
* return JSON.parse(resp.body);
|
|
1109
|
+
* }
|
|
1110
|
+
* ```
|
|
1111
|
+
*/
|
|
151
1112
|
const Titan: TitanRuntimeUtils;
|
|
152
1113
|
|
|
1114
|
+
|
|
1115
|
+
// -----------------------------------------------------------------------
|
|
1116
|
+
// TitanCore Namespace — Detailed Sub-module Interfaces
|
|
1117
|
+
// -----------------------------------------------------------------------
|
|
1118
|
+
|
|
153
1119
|
namespace TitanCore {
|
|
1120
|
+
interface TitanResponse {
|
|
1121
|
+
readonly __titan_response: true;
|
|
1122
|
+
}
|
|
1123
|
+
/**
|
|
1124
|
+
* Asynchronous file system operations.
|
|
1125
|
+
*
|
|
1126
|
+
* All methods return `Promise` and must be used with `drift()`.
|
|
1127
|
+
*
|
|
1128
|
+
* @example
|
|
1129
|
+
* ```js
|
|
1130
|
+
* export function fileOps(req) {
|
|
1131
|
+
* // Check if a file exists
|
|
1132
|
+
* const exists = drift(t.fs.exists("./data/config.json"));
|
|
1133
|
+
*
|
|
1134
|
+
* // Read a file
|
|
1135
|
+
* const content = drift(t.fs.readFile("./data/config.json"));
|
|
1136
|
+
*
|
|
1137
|
+
* // Write a file
|
|
1138
|
+
* drift(t.fs.writeFile("./data/output.json", JSON.stringify({ ok: true })));
|
|
1139
|
+
*
|
|
1140
|
+
* // Create a directory
|
|
1141
|
+
* drift(t.fs.mkdir("./data/backups"));
|
|
1142
|
+
*
|
|
1143
|
+
* // List directory contents
|
|
1144
|
+
* const files = drift(t.fs.readdir("./data"));
|
|
1145
|
+
*
|
|
1146
|
+
* // Get file metadata
|
|
1147
|
+
* const info = drift(t.fs.stat("./data/config.json"));
|
|
1148
|
+
* t.log("Size:", info.size, "Is file:", info.isFile);
|
|
1149
|
+
*
|
|
1150
|
+
* // Delete a file
|
|
1151
|
+
* drift(t.fs.remove("./data/temp.txt"));
|
|
1152
|
+
*
|
|
1153
|
+
* return { exists, files, info };
|
|
1154
|
+
* }
|
|
1155
|
+
* ```
|
|
1156
|
+
*
|
|
1157
|
+
* @see https://titan-docs-ez.vercel.app/docs/04-runtime-apis — Runtime APIs (t.fs)
|
|
1158
|
+
*/
|
|
154
1159
|
interface FileSystem {
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
1160
|
+
/**
|
|
1161
|
+
* Read the entire contents of a file as a UTF-8 string.
|
|
1162
|
+
*
|
|
1163
|
+
* @param path - Path to the file to read.
|
|
1164
|
+
* @returns A promise resolving to the file contents.
|
|
1165
|
+
* @throws If the file does not exist or cannot be read.
|
|
1166
|
+
*/
|
|
1167
|
+
readFile(path: string): Promise<string>;
|
|
1168
|
+
|
|
1169
|
+
/**
|
|
1170
|
+
* Write a string to a file, creating or overwriting it.
|
|
1171
|
+
*
|
|
1172
|
+
* @param path - Path to the file to write.
|
|
1173
|
+
* @param content - The string content to write.
|
|
1174
|
+
* @returns A promise that resolves when writing is complete.
|
|
1175
|
+
*/
|
|
1176
|
+
writeFile(path: string, content: string): Promise<void>;
|
|
1177
|
+
|
|
1178
|
+
/**
|
|
1179
|
+
* List the names of all entries in a directory.
|
|
1180
|
+
*
|
|
1181
|
+
* @param path - Path to the directory.
|
|
1182
|
+
* @returns A promise resolving to an array of file/directory names.
|
|
1183
|
+
*/
|
|
1184
|
+
readdir(path: string): Promise<string[]>;
|
|
1185
|
+
|
|
1186
|
+
/**
|
|
1187
|
+
* Create a directory (and parent directories if needed).
|
|
1188
|
+
*
|
|
1189
|
+
* @param path - Path of the directory to create.
|
|
1190
|
+
* @returns A promise that resolves when the directory is created.
|
|
1191
|
+
*/
|
|
1192
|
+
mkdir(path: string): Promise<void>;
|
|
1193
|
+
|
|
1194
|
+
/**
|
|
1195
|
+
* Check whether a file or directory exists at the given path.
|
|
1196
|
+
*
|
|
1197
|
+
* @param path - Path to check.
|
|
1198
|
+
* @returns A promise resolving to `true` if the path exists, `false` otherwise.
|
|
1199
|
+
*/
|
|
1200
|
+
exists(path: string): Promise<boolean>;
|
|
1201
|
+
|
|
1202
|
+
/**
|
|
1203
|
+
* Get metadata about a file or directory.
|
|
1204
|
+
*
|
|
1205
|
+
* @param path - Path to the file or directory.
|
|
1206
|
+
* @returns A promise resolving to a stat object with:
|
|
1207
|
+
* - `size` — File size in bytes.
|
|
1208
|
+
* - `isFile` — `true` if the path is a regular file.
|
|
1209
|
+
* - `isDir` — `true` if the path is a directory.
|
|
1210
|
+
* - `modified` — Last modification time as a Unix timestamp (ms).
|
|
1211
|
+
*/
|
|
1212
|
+
stat(path: string): Promise<{
|
|
1213
|
+
size: number;
|
|
1214
|
+
isFile: boolean;
|
|
1215
|
+
isDir: boolean;
|
|
1216
|
+
modified: number;
|
|
1217
|
+
}>;
|
|
1218
|
+
|
|
1219
|
+
/**
|
|
1220
|
+
* Delete a file or directory.
|
|
1221
|
+
*
|
|
1222
|
+
* @param path - Path to the file or directory to remove.
|
|
1223
|
+
* @returns A promise that resolves when the removal is complete.
|
|
1224
|
+
* @throws If the path does not exist.
|
|
1225
|
+
*/
|
|
1226
|
+
remove(path: string): Promise<void>;
|
|
162
1227
|
}
|
|
163
1228
|
|
|
1229
|
+
/**
|
|
1230
|
+
* Cross-platform path manipulation utilities.
|
|
1231
|
+
*
|
|
1232
|
+
* All methods are **synchronous** — no `drift()` needed.
|
|
1233
|
+
*
|
|
1234
|
+
* @example
|
|
1235
|
+
* ```js
|
|
1236
|
+
* const full = t.path.join("data", "users", "profile.json");
|
|
1237
|
+
* // → "data/users/profile.json"
|
|
1238
|
+
*
|
|
1239
|
+
* const abs = t.path.resolve("./config.json");
|
|
1240
|
+
* // → "/app/config.json"
|
|
1241
|
+
*
|
|
1242
|
+
* t.path.extname("photo.png"); // → ".png"
|
|
1243
|
+
* t.path.dirname("/a/b/c.txt"); // → "/a/b"
|
|
1244
|
+
* t.path.basename("/a/b/c.txt"); // → "c.txt"
|
|
1245
|
+
* ```
|
|
1246
|
+
*
|
|
1247
|
+
* @see https://titan-docs-ez.vercel.app/docs/04-runtime-apis — Runtime APIs (t.path)
|
|
1248
|
+
*/
|
|
164
1249
|
interface Path {
|
|
1250
|
+
/**
|
|
1251
|
+
* Join multiple path segments into a single normalized path.
|
|
1252
|
+
*
|
|
1253
|
+
* @param args - Two or more path segments to join.
|
|
1254
|
+
* @returns The joined and normalized path string.
|
|
1255
|
+
*
|
|
1256
|
+
* @example
|
|
1257
|
+
* ```js
|
|
1258
|
+
* t.path.join("data", "users", "profile.json");
|
|
1259
|
+
* // → "data/users/profile.json"
|
|
1260
|
+
* ```
|
|
1261
|
+
*/
|
|
165
1262
|
join(...args: string[]): string;
|
|
1263
|
+
|
|
1264
|
+
/**
|
|
1265
|
+
* Resolve a sequence of paths into an absolute path.
|
|
1266
|
+
*
|
|
1267
|
+
* @param args - Path segments. Relative segments are resolved against the working directory.
|
|
1268
|
+
* @returns The resolved absolute path string.
|
|
1269
|
+
*
|
|
1270
|
+
* @example
|
|
1271
|
+
* ```js
|
|
1272
|
+
* t.path.resolve("./config.json");
|
|
1273
|
+
* // → "/app/config.json"
|
|
1274
|
+
* ```
|
|
1275
|
+
*/
|
|
166
1276
|
resolve(...args: string[]): string;
|
|
1277
|
+
|
|
1278
|
+
/**
|
|
1279
|
+
* Get the file extension (including the leading dot).
|
|
1280
|
+
*
|
|
1281
|
+
* @param path - The file path.
|
|
1282
|
+
* @returns The extension string (e.g., `".json"`, `".png"`), or `""` if none.
|
|
1283
|
+
*/
|
|
167
1284
|
extname(path: string): string;
|
|
1285
|
+
|
|
1286
|
+
/**
|
|
1287
|
+
* Get the directory portion of a path.
|
|
1288
|
+
*
|
|
1289
|
+
* @param path - The file path.
|
|
1290
|
+
* @returns The directory path (e.g., `"/a/b"` from `"/a/b/c.txt"`).
|
|
1291
|
+
*/
|
|
168
1292
|
dirname(path: string): string;
|
|
1293
|
+
|
|
1294
|
+
/**
|
|
1295
|
+
* Get the last segment (filename) of a path.
|
|
1296
|
+
*
|
|
1297
|
+
* @param path - The file path.
|
|
1298
|
+
* @returns The filename (e.g., `"c.txt"` from `"/a/b/c.txt"`).
|
|
1299
|
+
*/
|
|
169
1300
|
basename(path: string): string;
|
|
170
1301
|
}
|
|
171
1302
|
|
|
1303
|
+
/**
|
|
1304
|
+
* Cryptographic operations powered by Rust's native crypto libraries.
|
|
1305
|
+
*
|
|
1306
|
+
* Includes hashing, HMAC, symmetric encryption/decryption, random bytes,
|
|
1307
|
+
* UUID generation, and constant-time comparison.
|
|
1308
|
+
*
|
|
1309
|
+
* Async methods (`hash`, `encrypt`, `decrypt`, `hashKeyed`) require `drift()`.
|
|
1310
|
+
* Sync methods (`randomBytes`, `uuid`, `compare`) do not.
|
|
1311
|
+
*
|
|
1312
|
+
* @example
|
|
1313
|
+
* ```js
|
|
1314
|
+
* export function cryptoDemo(req) {
|
|
1315
|
+
* // Hash a string
|
|
1316
|
+
* const sha = drift(t.crypto.hash("sha256", "hello world"));
|
|
1317
|
+
*
|
|
1318
|
+
* // Generate a UUID
|
|
1319
|
+
* const id = t.crypto.uuid();
|
|
1320
|
+
*
|
|
1321
|
+
* // Random bytes (hex-encoded)
|
|
1322
|
+
* const rand = t.crypto.randomBytes(32);
|
|
1323
|
+
*
|
|
1324
|
+
* // HMAC signing
|
|
1325
|
+
* const sig = drift(t.crypto.hashKeyed("hmac-sha256", "secret", "message"));
|
|
1326
|
+
*
|
|
1327
|
+
* // Encrypt / Decrypt
|
|
1328
|
+
* const encrypted = drift(t.crypto.encrypt("aes-256-gcm", "my-key", "secret data"));
|
|
1329
|
+
* const decrypted = drift(t.crypto.decrypt("aes-256-gcm", "my-key", encrypted));
|
|
1330
|
+
*
|
|
1331
|
+
* return { sha, id, rand, sig, decrypted };
|
|
1332
|
+
* }
|
|
1333
|
+
* ```
|
|
1334
|
+
*
|
|
1335
|
+
* @see https://titan-docs-ez.vercel.app/docs/04-runtime-apis — Runtime APIs (t.crypto)
|
|
1336
|
+
*/
|
|
172
1337
|
interface Crypto {
|
|
173
|
-
|
|
1338
|
+
/**
|
|
1339
|
+
* Compute a cryptographic hash of the input data.
|
|
1340
|
+
*
|
|
1341
|
+
* @param algorithm - The hash algorithm: `"sha256"`, `"sha512"`, or `"md5"`.
|
|
1342
|
+
* @param data - The string to hash.
|
|
1343
|
+
* @returns A promise resolving to the hex-encoded hash string.
|
|
1344
|
+
*
|
|
1345
|
+
* @example
|
|
1346
|
+
* ```js
|
|
1347
|
+
* const hash = drift(t.crypto.hash("sha256", "hello"));
|
|
1348
|
+
* // → "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
|
|
1349
|
+
* ```
|
|
1350
|
+
*/
|
|
1351
|
+
hash(algorithm: 'sha256' | 'sha512' | 'md5', data: string): Promise<string>;
|
|
1352
|
+
|
|
1353
|
+
/**
|
|
1354
|
+
* Generate cryptographically secure random bytes as a hex string.
|
|
1355
|
+
*
|
|
1356
|
+
* This is a **synchronous** operation — no `drift()` needed.
|
|
1357
|
+
*
|
|
1358
|
+
* @param size - Number of random bytes to generate.
|
|
1359
|
+
* @returns A hex-encoded string of `size` random bytes (length = `size * 2`).
|
|
1360
|
+
*
|
|
1361
|
+
* @example
|
|
1362
|
+
* ```js
|
|
1363
|
+
* const secret = t.crypto.randomBytes(32);
|
|
1364
|
+
* // → "a1b2c3d4e5f6..." (64 hex chars)
|
|
1365
|
+
* ```
|
|
1366
|
+
*/
|
|
174
1367
|
randomBytes(size: number): string;
|
|
1368
|
+
|
|
1369
|
+
/**
|
|
1370
|
+
* Generate a random UUID v4 string.
|
|
1371
|
+
*
|
|
1372
|
+
* This is a **synchronous** operation — no `drift()` needed.
|
|
1373
|
+
*
|
|
1374
|
+
* @returns A UUID v4 string (e.g., `"550e8400-e29b-41d4-a716-446655440000"`).
|
|
1375
|
+
*
|
|
1376
|
+
* @example
|
|
1377
|
+
* ```js
|
|
1378
|
+
* const id = t.crypto.uuid();
|
|
1379
|
+
* // → "f47ac10b-58cc-4372-a567-0e02b2c3d479"
|
|
1380
|
+
* ```
|
|
1381
|
+
*/
|
|
175
1382
|
uuid(): string;
|
|
1383
|
+
|
|
1384
|
+
/**
|
|
1385
|
+
* Perform a constant-time string comparison to prevent timing attacks.
|
|
1386
|
+
*
|
|
1387
|
+
* This is a **synchronous** operation — no `drift()` needed.
|
|
1388
|
+
*
|
|
1389
|
+
* @param hash - The first string (typically a hash or token).
|
|
1390
|
+
* @param target - The second string to compare against.
|
|
1391
|
+
* @returns `true` if the strings are identical, `false` otherwise.
|
|
1392
|
+
*
|
|
1393
|
+
* @example
|
|
1394
|
+
* ```js
|
|
1395
|
+
* const isMatch = t.crypto.compare(computedHash, expectedHash);
|
|
1396
|
+
* ```
|
|
1397
|
+
*/
|
|
176
1398
|
compare(hash: string, target: string): boolean;
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
1399
|
+
|
|
1400
|
+
/**
|
|
1401
|
+
* Encrypt data using a symmetric encryption algorithm.
|
|
1402
|
+
*
|
|
1403
|
+
* @param algorithm - The encryption algorithm (e.g., `"aes-256-gcm"`).
|
|
1404
|
+
* @param key - The encryption key string.
|
|
1405
|
+
* @param plaintext - The data to encrypt.
|
|
1406
|
+
* @returns A promise resolving to the encrypted ciphertext string.
|
|
1407
|
+
*
|
|
1408
|
+
* @example
|
|
1409
|
+
* ```js
|
|
1410
|
+
* const encrypted = drift(t.crypto.encrypt("aes-256-gcm", myKey, "secret data"));
|
|
1411
|
+
* ```
|
|
1412
|
+
*/
|
|
1413
|
+
encrypt(algorithm: string, key: string, plaintext: string): Promise<string>;
|
|
1414
|
+
|
|
1415
|
+
/**
|
|
1416
|
+
* Decrypt data previously encrypted with `t.crypto.encrypt()`.
|
|
1417
|
+
*
|
|
1418
|
+
* @param algorithm - The same algorithm used for encryption.
|
|
1419
|
+
* @param key - The same key used for encryption.
|
|
1420
|
+
* @param ciphertext - The encrypted string to decrypt.
|
|
1421
|
+
* @returns A promise resolving to the original plaintext string.
|
|
1422
|
+
*
|
|
1423
|
+
* @example
|
|
1424
|
+
* ```js
|
|
1425
|
+
* const plaintext = drift(t.crypto.decrypt("aes-256-gcm", myKey, encrypted));
|
|
1426
|
+
* ```
|
|
1427
|
+
*/
|
|
1428
|
+
decrypt(algorithm: string, key: string, ciphertext: string): Promise<string>;
|
|
1429
|
+
|
|
1430
|
+
/**
|
|
1431
|
+
* Compute an HMAC (Hash-based Message Authentication Code).
|
|
1432
|
+
*
|
|
1433
|
+
* Useful for verifying message integrity and authenticity
|
|
1434
|
+
* (e.g., webhook signature validation).
|
|
1435
|
+
*
|
|
1436
|
+
* @param algorithm - The HMAC algorithm: `"hmac-sha256"` or `"hmac-sha512"`.
|
|
1437
|
+
* @param key - The secret key for the HMAC.
|
|
1438
|
+
* @param message - The message to authenticate.
|
|
1439
|
+
* @returns A promise resolving to the hex-encoded HMAC string.
|
|
1440
|
+
*
|
|
1441
|
+
* @example
|
|
1442
|
+
* ```js
|
|
1443
|
+
* // Verify a webhook signature
|
|
1444
|
+
* export function webhook(req) {
|
|
1445
|
+
* const signature = req.headers["x-signature"];
|
|
1446
|
+
* const computed = drift(t.crypto.hashKeyed(
|
|
1447
|
+
* "hmac-sha256",
|
|
1448
|
+
* process.env.WEBHOOK_SECRET,
|
|
1449
|
+
* JSON.stringify(req.body)
|
|
1450
|
+
* ));
|
|
1451
|
+
* if (!t.crypto.compare(computed, signature)) {
|
|
1452
|
+
* return t.response.json({ error: "Invalid signature" }, 401);
|
|
1453
|
+
* }
|
|
1454
|
+
* return { verified: true };
|
|
1455
|
+
* }
|
|
1456
|
+
* ```
|
|
1457
|
+
*/
|
|
1458
|
+
hashKeyed(algorithm: 'hmac-sha256' | 'hmac-sha512', key: string, message: string): Promise<string>;
|
|
180
1459
|
}
|
|
181
1460
|
|
|
1461
|
+
/**
|
|
1462
|
+
* Buffer encoding and decoding utilities.
|
|
1463
|
+
*
|
|
1464
|
+
* Convert between `string`, `Uint8Array`, Base64, Hex, and UTF-8
|
|
1465
|
+
* representations. All methods are **synchronous** — no `drift()` needed.
|
|
1466
|
+
*
|
|
1467
|
+
* @example
|
|
1468
|
+
* ```js
|
|
1469
|
+
* // Base64 encode/decode
|
|
1470
|
+
* const encoded = t.buffer.toBase64("Hello, Titan!");
|
|
1471
|
+
* const decoded = t.buffer.toUtf8(t.buffer.fromBase64(encoded));
|
|
1472
|
+
*
|
|
1473
|
+
* // Hex encode/decode
|
|
1474
|
+
* const hex = t.buffer.toHex("Hello");
|
|
1475
|
+
* const bytes = t.buffer.fromHex(hex);
|
|
1476
|
+
* ```
|
|
1477
|
+
*
|
|
1478
|
+
* @see https://titan-docs-ez.vercel.app/docs/04-runtime-apis — Runtime APIs (t.buffer)
|
|
1479
|
+
*/
|
|
182
1480
|
interface BufferModule {
|
|
1481
|
+
/**
|
|
1482
|
+
* Decode a Base64-encoded string into a `Uint8Array`.
|
|
1483
|
+
*
|
|
1484
|
+
* @param str - The Base64-encoded string.
|
|
1485
|
+
* @returns The decoded byte array.
|
|
1486
|
+
*/
|
|
183
1487
|
fromBase64(str: string): Uint8Array;
|
|
1488
|
+
|
|
1489
|
+
/**
|
|
1490
|
+
* Encode bytes or a string to Base64.
|
|
1491
|
+
*
|
|
1492
|
+
* @param bytes - A `Uint8Array` or plain string to encode.
|
|
1493
|
+
* @returns The Base64-encoded string.
|
|
1494
|
+
*/
|
|
184
1495
|
toBase64(bytes: Uint8Array | string): string;
|
|
1496
|
+
|
|
1497
|
+
/**
|
|
1498
|
+
* Decode a hex-encoded string into a `Uint8Array`.
|
|
1499
|
+
*
|
|
1500
|
+
* @param str - The hex-encoded string (e.g., `"48656c6c6f"`).
|
|
1501
|
+
* @returns The decoded byte array.
|
|
1502
|
+
*/
|
|
185
1503
|
fromHex(str: string): Uint8Array;
|
|
1504
|
+
|
|
1505
|
+
/**
|
|
1506
|
+
* Encode bytes or a string to hexadecimal.
|
|
1507
|
+
*
|
|
1508
|
+
* @param bytes - A `Uint8Array` or plain string to encode.
|
|
1509
|
+
* @returns The hex-encoded string.
|
|
1510
|
+
*/
|
|
186
1511
|
toHex(bytes: Uint8Array | string): string;
|
|
1512
|
+
|
|
1513
|
+
/**
|
|
1514
|
+
* Encode a UTF-8 string into a `Uint8Array`.
|
|
1515
|
+
*
|
|
1516
|
+
* @param str - The string to encode.
|
|
1517
|
+
* @returns The UTF-8 encoded byte array.
|
|
1518
|
+
*/
|
|
187
1519
|
fromUtf8(str: string): Uint8Array;
|
|
1520
|
+
|
|
1521
|
+
/**
|
|
1522
|
+
* Decode a `Uint8Array` back into a UTF-8 string.
|
|
1523
|
+
*
|
|
1524
|
+
* @param bytes - The byte array to decode.
|
|
1525
|
+
* @returns The decoded UTF-8 string.
|
|
1526
|
+
*/
|
|
188
1527
|
toUtf8(bytes: Uint8Array): string;
|
|
189
1528
|
}
|
|
190
1529
|
|
|
1530
|
+
/**
|
|
1531
|
+
* Persistent server-side key-value storage.
|
|
1532
|
+
*
|
|
1533
|
+
* Data persists across requests and server restarts. Accessible via
|
|
1534
|
+
* `t.ls` (shorthand) or `t.localStorage` (full name).
|
|
1535
|
+
*
|
|
1536
|
+
* Values are stored as strings — serialize complex objects with
|
|
1537
|
+
* `JSON.stringify()` and parse with `JSON.parse()`.
|
|
1538
|
+
*
|
|
1539
|
+
* All methods are **synchronous** — no `drift()` needed.
|
|
1540
|
+
*
|
|
1541
|
+
* @example
|
|
1542
|
+
* ```js
|
|
1543
|
+
* // Set and retrieve values
|
|
1544
|
+
* t.ls.set("app:version", "2.0.0");
|
|
1545
|
+
* const version = t.ls.get("app:version"); // → "2.0.0"
|
|
1546
|
+
*
|
|
1547
|
+
* // Store objects (serialize manually)
|
|
1548
|
+
* t.ls.set("config", JSON.stringify({ theme: "dark", lang: "en" }));
|
|
1549
|
+
* const config = JSON.parse(t.ls.get("config"));
|
|
1550
|
+
*
|
|
1551
|
+
* // List all keys
|
|
1552
|
+
* const allKeys = t.ls.keys(); // → ["app:version", "config"]
|
|
1553
|
+
*
|
|
1554
|
+
* // Delete a key
|
|
1555
|
+
* t.ls.remove("app:version");
|
|
1556
|
+
*
|
|
1557
|
+
* // Clear all stored data
|
|
1558
|
+
* t.ls.clear();
|
|
1559
|
+
* ```
|
|
1560
|
+
*
|
|
1561
|
+
* @see https://titan-docs-ez.vercel.app/docs/04-runtime-apis — Runtime APIs (t.ls / t.localStorage)
|
|
1562
|
+
*/
|
|
191
1563
|
interface LocalStorage {
|
|
1564
|
+
/**
|
|
1565
|
+
* Retrieve the value associated with a key.
|
|
1566
|
+
*
|
|
1567
|
+
* @param key - The storage key.
|
|
1568
|
+
* @returns The stored string value, or `null` if the key does not exist.
|
|
1569
|
+
*/
|
|
192
1570
|
get(key: string): string | null;
|
|
1571
|
+
|
|
1572
|
+
/**
|
|
1573
|
+
* Store a value under the given key (creates or overwrites).
|
|
1574
|
+
*
|
|
1575
|
+
* @param key - The storage key.
|
|
1576
|
+
* @param value - The string value to store.
|
|
1577
|
+
*/
|
|
193
1578
|
set(key: string, value: string): void;
|
|
1579
|
+
|
|
1580
|
+
/**
|
|
1581
|
+
* Delete a single key from storage.
|
|
1582
|
+
*
|
|
1583
|
+
* @param key - The key to remove. No error if the key doesn't exist.
|
|
1584
|
+
*/
|
|
194
1585
|
remove(key: string): void;
|
|
1586
|
+
|
|
1587
|
+
/**
|
|
1588
|
+
* Clear all keys and values from local storage.
|
|
1589
|
+
*
|
|
1590
|
+
* ⚠️ **Destructive** — removes everything. Use with caution.
|
|
1591
|
+
*/
|
|
195
1592
|
clear(): void;
|
|
1593
|
+
|
|
1594
|
+
/**
|
|
1595
|
+
* Get a list of all stored keys.
|
|
1596
|
+
*
|
|
1597
|
+
* @returns An array of all key names currently in storage.
|
|
1598
|
+
*/
|
|
196
1599
|
keys(): string[];
|
|
197
1600
|
}
|
|
198
1601
|
|
|
1602
|
+
/**
|
|
1603
|
+
* Server-side session management scoped by session ID.
|
|
1604
|
+
*
|
|
1605
|
+
* Sessions store ephemeral per-client data on the server. Each session
|
|
1606
|
+
* is identified by a unique `sessionId` (typically from a cookie or token).
|
|
1607
|
+
*
|
|
1608
|
+
* All methods are **synchronous** — no `drift()` needed.
|
|
1609
|
+
*
|
|
1610
|
+
* @example
|
|
1611
|
+
* ```js
|
|
1612
|
+
* export function setPreference(req) {
|
|
1613
|
+
* const sid = req.headers["x-session-id"];
|
|
1614
|
+
* t.session.set(sid, "theme", "dark");
|
|
1615
|
+
* t.session.set(sid, "lang", "en");
|
|
1616
|
+
* return { saved: true };
|
|
1617
|
+
* }
|
|
1618
|
+
*
|
|
1619
|
+
* export function getPreference(req) {
|
|
1620
|
+
* const sid = req.headers["x-session-id"];
|
|
1621
|
+
* const theme = t.session.get(sid, "theme");
|
|
1622
|
+
* return { theme };
|
|
1623
|
+
* }
|
|
1624
|
+
*
|
|
1625
|
+
* export function logout(req) {
|
|
1626
|
+
* const sid = req.headers["x-session-id"];
|
|
1627
|
+
* t.session.clear(sid); // Destroy entire session
|
|
1628
|
+
* return { loggedOut: true };
|
|
1629
|
+
* }
|
|
1630
|
+
* ```
|
|
1631
|
+
*
|
|
1632
|
+
* @see https://titan-docs-ez.vercel.app/docs/04-runtime-apis — Runtime APIs (t.session)
|
|
1633
|
+
*/
|
|
199
1634
|
interface Session {
|
|
1635
|
+
/**
|
|
1636
|
+
* Retrieve a value from a session.
|
|
1637
|
+
*
|
|
1638
|
+
* @param sessionId - The unique session identifier.
|
|
1639
|
+
* @param key - The key to look up within the session.
|
|
1640
|
+
* @returns The stored value, or `null` if not found.
|
|
1641
|
+
*/
|
|
200
1642
|
get(sessionId: string, key: string): string | null;
|
|
1643
|
+
|
|
1644
|
+
/**
|
|
1645
|
+
* Store a value in a session (creates or overwrites).
|
|
1646
|
+
*
|
|
1647
|
+
* @param sessionId - The unique session identifier.
|
|
1648
|
+
* @param key - The key to store under.
|
|
1649
|
+
* @param value - The string value to store.
|
|
1650
|
+
*/
|
|
201
1651
|
set(sessionId: string, key: string, value: string): void;
|
|
1652
|
+
|
|
1653
|
+
/**
|
|
1654
|
+
* Delete a single key from a session.
|
|
1655
|
+
*
|
|
1656
|
+
* @param sessionId - The unique session identifier.
|
|
1657
|
+
* @param key - The key to delete.
|
|
1658
|
+
*/
|
|
202
1659
|
delete(sessionId: string, key: string): void;
|
|
1660
|
+
|
|
1661
|
+
/**
|
|
1662
|
+
* Destroy an entire session, removing all its data.
|
|
1663
|
+
*
|
|
1664
|
+
* @param sessionId - The unique session identifier to clear.
|
|
1665
|
+
*/
|
|
203
1666
|
clear(sessionId: string): void;
|
|
204
1667
|
}
|
|
205
1668
|
|
|
1669
|
+
/**
|
|
1670
|
+
* HTTP cookie management utilities.
|
|
1671
|
+
*
|
|
1672
|
+
* Read, set, and delete cookies from incoming requests and outgoing responses.
|
|
1673
|
+
*
|
|
1674
|
+
* @example
|
|
1675
|
+
* ```js
|
|
1676
|
+
* export function handleCookies(req) {
|
|
1677
|
+
* // Read a cookie from the request
|
|
1678
|
+
* const token = t.cookies.get(req, "auth_token");
|
|
1679
|
+
*
|
|
1680
|
+
* // Set a cookie (attach to response)
|
|
1681
|
+
* t.cookies.set(req, "visited", "true", {
|
|
1682
|
+
* httpOnly: true,
|
|
1683
|
+
* secure: true,
|
|
1684
|
+
* maxAge: 86400 // 1 day in seconds
|
|
1685
|
+
* });
|
|
1686
|
+
*
|
|
1687
|
+
* // Delete a cookie
|
|
1688
|
+
* t.cookies.delete(req, "old_cookie");
|
|
1689
|
+
*
|
|
1690
|
+
* return { hasToken: !!token };
|
|
1691
|
+
* }
|
|
1692
|
+
* ```
|
|
1693
|
+
*
|
|
1694
|
+
* @see https://titan-docs-ez.vercel.app/docs/04-runtime-apis — Runtime APIs (t.cookies)
|
|
1695
|
+
*/
|
|
206
1696
|
interface Cookies {
|
|
1697
|
+
/**
|
|
1698
|
+
* Read a cookie value from the request.
|
|
1699
|
+
*
|
|
1700
|
+
* @param req - The Titan request object (or response context).
|
|
1701
|
+
* @param name - The cookie name.
|
|
1702
|
+
* @returns The cookie value string, or `null` if the cookie is not present.
|
|
1703
|
+
*/
|
|
207
1704
|
get(req: any, name: string): string | null;
|
|
1705
|
+
|
|
1706
|
+
/**
|
|
1707
|
+
* Set a cookie on the response.
|
|
1708
|
+
*
|
|
1709
|
+
* @param res - The response context object.
|
|
1710
|
+
* @param name - The cookie name.
|
|
1711
|
+
* @param value - The cookie value.
|
|
1712
|
+
* @param options - Optional cookie attributes (e.g., `httpOnly`, `secure`, `maxAge`, `path`, `sameSite`).
|
|
1713
|
+
*/
|
|
208
1714
|
set(res: any, name: string, value: string, options?: any): void;
|
|
1715
|
+
|
|
1716
|
+
/**
|
|
1717
|
+
* Delete a cookie by setting its expiration in the past.
|
|
1718
|
+
*
|
|
1719
|
+
* @param res - The response context object.
|
|
1720
|
+
* @param name - The cookie name to delete.
|
|
1721
|
+
*/
|
|
209
1722
|
delete(res: any, name: string): void;
|
|
210
1723
|
}
|
|
211
1724
|
|
|
1725
|
+
/**
|
|
1726
|
+
* Operating system information about the host running the Titan server.
|
|
1727
|
+
*
|
|
1728
|
+
* All methods are **synchronous** — no `drift()` needed.
|
|
1729
|
+
*
|
|
1730
|
+
* @example
|
|
1731
|
+
* ```js
|
|
1732
|
+
* export function serverInfo(req) {
|
|
1733
|
+
* return {
|
|
1734
|
+
* platform: t.os.platform(), // e.g., "linux"
|
|
1735
|
+
* cpus: t.os.cpus(), // e.g., 8
|
|
1736
|
+
* totalMemory: t.os.totalMemory(), // bytes
|
|
1737
|
+
* freeMemory: t.os.freeMemory(), // bytes
|
|
1738
|
+
* tmpdir: t.os.tmpdir() // e.g., "/tmp"
|
|
1739
|
+
* };
|
|
1740
|
+
* }
|
|
1741
|
+
* ```
|
|
1742
|
+
*
|
|
1743
|
+
* @see https://titan-docs-ez.vercel.app/docs/04-runtime-apis — Runtime APIs (t.os)
|
|
1744
|
+
*/
|
|
212
1745
|
interface OS {
|
|
1746
|
+
/**
|
|
1747
|
+
* Get the operating system platform identifier.
|
|
1748
|
+
*
|
|
1749
|
+
* @returns A string like `"linux"`, `"darwin"` (macOS), or `"win32"` (Windows).
|
|
1750
|
+
*/
|
|
213
1751
|
platform(): string;
|
|
1752
|
+
|
|
1753
|
+
/**
|
|
1754
|
+
* Get the number of logical CPU cores available.
|
|
1755
|
+
*
|
|
1756
|
+
* @returns The number of CPU cores.
|
|
1757
|
+
*/
|
|
214
1758
|
cpus(): number;
|
|
1759
|
+
|
|
1760
|
+
/**
|
|
1761
|
+
* Get the total system memory in bytes.
|
|
1762
|
+
*
|
|
1763
|
+
* @returns Total memory in bytes.
|
|
1764
|
+
*/
|
|
215
1765
|
totalMemory(): number;
|
|
1766
|
+
|
|
1767
|
+
/**
|
|
1768
|
+
* Get the currently available (free) system memory in bytes.
|
|
1769
|
+
*
|
|
1770
|
+
* @returns Free memory in bytes.
|
|
1771
|
+
*/
|
|
216
1772
|
freeMemory(): number;
|
|
1773
|
+
|
|
1774
|
+
/**
|
|
1775
|
+
* Get the path to the system's temporary directory.
|
|
1776
|
+
*
|
|
1777
|
+
* @returns The temporary directory path (e.g., `"/tmp"`).
|
|
1778
|
+
*/
|
|
217
1779
|
tmpdir(): string;
|
|
218
1780
|
}
|
|
219
1781
|
|
|
1782
|
+
/**
|
|
1783
|
+
* Network utility functions.
|
|
1784
|
+
*
|
|
1785
|
+
* All methods return `Promise` — use `drift()` to resolve.
|
|
1786
|
+
*
|
|
1787
|
+
* @example
|
|
1788
|
+
* ```js
|
|
1789
|
+
* export function networkInfo(req) {
|
|
1790
|
+
* const ips = drift(t.net.resolveDNS("example.com"));
|
|
1791
|
+
* const myIp = drift(t.net.ip());
|
|
1792
|
+
* const reachable = drift(t.net.ping("8.8.8.8"));
|
|
1793
|
+
* return { ips, myIp, reachable };
|
|
1794
|
+
* }
|
|
1795
|
+
* ```
|
|
1796
|
+
*
|
|
1797
|
+
* @see https://titan-docs-ez.vercel.app/docs/04-runtime-apis — Runtime APIs (t.net)
|
|
1798
|
+
*/
|
|
220
1799
|
interface Net {
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
1800
|
+
/**
|
|
1801
|
+
* Resolve a hostname to its IP addresses via DNS lookup.
|
|
1802
|
+
*
|
|
1803
|
+
* @param hostname - The domain to resolve (e.g., `"example.com"`).
|
|
1804
|
+
* @returns A promise resolving to an array of IP address strings.
|
|
1805
|
+
*/
|
|
1806
|
+
resolveDNS(hostname: string): Promise<string[]>;
|
|
1807
|
+
|
|
1808
|
+
/**
|
|
1809
|
+
* Get the public IP address of the current server.
|
|
1810
|
+
*
|
|
1811
|
+
* @returns A promise resolving to the server's public IP string.
|
|
1812
|
+
*/
|
|
1813
|
+
ip(): Promise<string>;
|
|
1814
|
+
|
|
1815
|
+
/**
|
|
1816
|
+
* Ping a host to check if it is reachable.
|
|
1817
|
+
*
|
|
1818
|
+
* @param host - The hostname or IP address to ping.
|
|
1819
|
+
* @returns A promise resolving to `true` if the host responds, `false` otherwise.
|
|
1820
|
+
*/
|
|
1821
|
+
ping(host: string): Promise<boolean>;
|
|
224
1822
|
}
|
|
225
1823
|
|
|
1824
|
+
/**
|
|
1825
|
+
* Process-level information about the running Titan server binary.
|
|
1826
|
+
*
|
|
1827
|
+
* All methods are **synchronous** — no `drift()` needed.
|
|
1828
|
+
*
|
|
1829
|
+
* @example
|
|
1830
|
+
* ```js
|
|
1831
|
+
* export function processInfo(req) {
|
|
1832
|
+
* return {
|
|
1833
|
+
* pid: t.proc.pid(), // e.g., 12345
|
|
1834
|
+
* uptime: t.proc.uptime(), // seconds since start
|
|
1835
|
+
* memory: t.proc.memory() // { rss, heapTotal, heapUsed, ... }
|
|
1836
|
+
* };
|
|
1837
|
+
* }
|
|
1838
|
+
* ```
|
|
1839
|
+
*
|
|
1840
|
+
* @see https://titan-docs-ez.vercel.app/docs/04-runtime-apis — Runtime APIs (t.proc)
|
|
1841
|
+
*/
|
|
226
1842
|
interface Process {
|
|
1843
|
+
/**
|
|
1844
|
+
* Get the process ID (PID) of the Titan server.
|
|
1845
|
+
*
|
|
1846
|
+
* @returns The numeric process ID.
|
|
1847
|
+
*/
|
|
227
1848
|
pid(): number;
|
|
1849
|
+
|
|
1850
|
+
/**
|
|
1851
|
+
* Get the server uptime in seconds since the process started.
|
|
1852
|
+
*
|
|
1853
|
+
* @returns Uptime in seconds.
|
|
1854
|
+
*/
|
|
228
1855
|
uptime(): number;
|
|
1856
|
+
|
|
1857
|
+
/**
|
|
1858
|
+
* Get memory usage statistics for the server process.
|
|
1859
|
+
*
|
|
1860
|
+
* @returns An object with memory metrics (e.g., `rss`, `heapTotal`, `heapUsed`).
|
|
1861
|
+
*/
|
|
229
1862
|
memory(): Record<string, any>;
|
|
230
1863
|
}
|
|
231
1864
|
|
|
1865
|
+
/**
|
|
1866
|
+
* Time-related utilities.
|
|
1867
|
+
*
|
|
1868
|
+
* `sleep()` is async (requires `drift()`). `now()` and `timestamp()` are synchronous.
|
|
1869
|
+
*
|
|
1870
|
+
* @example
|
|
1871
|
+
* ```js
|
|
1872
|
+
* export function timeDemo(req) {
|
|
1873
|
+
* const start = t.time.now(); // High-resolution ms timestamp
|
|
1874
|
+
* drift(t.time.sleep(100)); // Wait 100ms
|
|
1875
|
+
* const elapsed = t.time.now() - start;
|
|
1876
|
+
* const iso = t.time.timestamp(); // "2026-01-15T12:30:45.123Z"
|
|
1877
|
+
* return { elapsed, iso };
|
|
1878
|
+
* }
|
|
1879
|
+
* ```
|
|
1880
|
+
*
|
|
1881
|
+
* @see https://titan-docs-ez.vercel.app/docs/04-runtime-apis — Runtime APIs (t.time)
|
|
1882
|
+
*/
|
|
232
1883
|
interface Time {
|
|
233
|
-
|
|
1884
|
+
/**
|
|
1885
|
+
* Pause execution for the given number of milliseconds.
|
|
1886
|
+
*
|
|
1887
|
+
* Returns a `Promise` — use `drift()` to suspend the action without blocking
|
|
1888
|
+
* the entire server. Useful for rate limiting, retry delays, or testing.
|
|
1889
|
+
*
|
|
1890
|
+
* @param ms - Duration to sleep in milliseconds.
|
|
1891
|
+
* @returns A promise that resolves after the specified duration.
|
|
1892
|
+
*
|
|
1893
|
+
* @example
|
|
1894
|
+
* ```js
|
|
1895
|
+
* drift(t.time.sleep(500)); // Wait 500ms
|
|
1896
|
+
* ```
|
|
1897
|
+
*/
|
|
1898
|
+
sleep(ms: number): Promise<void>;
|
|
1899
|
+
|
|
1900
|
+
/**
|
|
1901
|
+
* Get the current time as a high-resolution millisecond timestamp.
|
|
1902
|
+
*
|
|
1903
|
+
* This is a **synchronous** operation.
|
|
1904
|
+
*
|
|
1905
|
+
* @returns A numeric timestamp in milliseconds (similar to `Date.now()`).
|
|
1906
|
+
*/
|
|
234
1907
|
now(): number;
|
|
1908
|
+
|
|
1909
|
+
/**
|
|
1910
|
+
* Get the current time as an ISO 8601 formatted string.
|
|
1911
|
+
*
|
|
1912
|
+
* This is a **synchronous** operation.
|
|
1913
|
+
*
|
|
1914
|
+
* @returns An ISO timestamp string (e.g., `"2026-01-15T12:30:45.123Z"`).
|
|
1915
|
+
*/
|
|
235
1916
|
timestamp(): string;
|
|
236
1917
|
}
|
|
237
1918
|
|
|
1919
|
+
/**
|
|
1920
|
+
* URL parsing and formatting utilities.
|
|
1921
|
+
*
|
|
1922
|
+
* @example
|
|
1923
|
+
* ```js
|
|
1924
|
+
* const parsed = t.url.parse("https://example.com/path?q=titan&page=1");
|
|
1925
|
+
* // → { protocol: "https:", host: "example.com", pathname: "/path", ... }
|
|
1926
|
+
*
|
|
1927
|
+
* const url = t.url.format({
|
|
1928
|
+
* protocol: "https:",
|
|
1929
|
+
* host: "api.example.com",
|
|
1930
|
+
* pathname: "/v2/users"
|
|
1931
|
+
* });
|
|
1932
|
+
* // → "https://api.example.com/v2/users"
|
|
1933
|
+
* ```
|
|
1934
|
+
*
|
|
1935
|
+
* @see https://titan-docs-ez.vercel.app/docs/04-runtime-apis — Runtime APIs (t.url)
|
|
1936
|
+
*/
|
|
238
1937
|
interface URLModule {
|
|
1938
|
+
/**
|
|
1939
|
+
* Parse a URL string into its component parts.
|
|
1940
|
+
*
|
|
1941
|
+
* @param url - The URL string to parse.
|
|
1942
|
+
* @returns An object with URL components (protocol, host, pathname, search, hash, etc.).
|
|
1943
|
+
*/
|
|
239
1944
|
parse(url: string): any;
|
|
1945
|
+
|
|
1946
|
+
/**
|
|
1947
|
+
* Format a URL object back into a URL string.
|
|
1948
|
+
*
|
|
1949
|
+
* @param urlObj - An object with URL components.
|
|
1950
|
+
* @returns The formatted URL string.
|
|
1951
|
+
*/
|
|
240
1952
|
format(urlObj: any): string;
|
|
1953
|
+
|
|
1954
|
+
/**
|
|
1955
|
+
* URL search parameters utility (similar to the Web API `URLSearchParams`).
|
|
1956
|
+
*/
|
|
241
1957
|
SearchParams: any;
|
|
242
1958
|
}
|
|
243
1959
|
|
|
1960
|
+
|
|
1961
|
+
|
|
244
1962
|
interface ResponseModule {
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
1963
|
+
|
|
1964
|
+
/**
|
|
1965
|
+
* Return a JSON response.
|
|
1966
|
+
* Body is automatically JSON.stringify-ed.
|
|
1967
|
+
*
|
|
1968
|
+
* @example
|
|
1969
|
+
* ```js
|
|
1970
|
+
* return t.response.json({ ok: true });
|
|
1971
|
+
* ```
|
|
1972
|
+
*/
|
|
1973
|
+
json(
|
|
1974
|
+
data: any,
|
|
1975
|
+
status?: number,
|
|
1976
|
+
extraHeaders?: Record<string, string>
|
|
1977
|
+
): TitanResponse;
|
|
1978
|
+
|
|
1979
|
+
/**
|
|
1980
|
+
* Return a plain-text response.
|
|
1981
|
+
* Content-Type: text/plain
|
|
1982
|
+
*
|
|
1983
|
+
* @example
|
|
1984
|
+
* ```js
|
|
1985
|
+
* return t.response.text("Hello World");
|
|
1986
|
+
* ```
|
|
1987
|
+
*/
|
|
1988
|
+
text(
|
|
1989
|
+
content: string,
|
|
1990
|
+
status?: number,
|
|
1991
|
+
extraHeaders?: Record<string, string>
|
|
1992
|
+
): TitanResponse;
|
|
1993
|
+
|
|
1994
|
+
/**
|
|
1995
|
+
* Return an HTML response.
|
|
1996
|
+
* Content-Type: text/html
|
|
1997
|
+
*
|
|
1998
|
+
* @example
|
|
1999
|
+
* ```js
|
|
2000
|
+
* return t.response.html("<h1>Hello</h1>");
|
|
2001
|
+
* ```
|
|
2002
|
+
*/
|
|
2003
|
+
html(
|
|
2004
|
+
content: string,
|
|
2005
|
+
status?: number,
|
|
2006
|
+
extraHeaders?: Record<string, string>
|
|
2007
|
+
): TitanResponse;
|
|
2008
|
+
|
|
2009
|
+
/**
|
|
2010
|
+
* Return a redirect response.
|
|
2011
|
+
* Defaults to HTTP 302.
|
|
2012
|
+
*
|
|
2013
|
+
* @example
|
|
2014
|
+
* ```js
|
|
2015
|
+
* return t.response.redirect("/login");
|
|
2016
|
+
* ```
|
|
2017
|
+
*/
|
|
2018
|
+
redirect(
|
|
2019
|
+
url: string,
|
|
2020
|
+
status?: number,
|
|
2021
|
+
extraHeaders?: Record<string, string>
|
|
2022
|
+
): TitanResponse;
|
|
251
2023
|
}
|
|
2024
|
+
|
|
252
2025
|
}
|
|
253
2026
|
}
|
|
254
2027
|
|