From 369673999b0fc4873c466f693b0935e7f4efdf10 Mon Sep 17 00:00:00 2001 From: theMackabu Date: Fri, 9 Jan 2026 17:19:17 -0800 Subject: [PATCH] add types for all builtins --- meson.build | 2 +- src/types/ant.d.ts | 22 ++++++ src/types/buffer.d.ts | 13 ++++ src/types/fetch.d.ts | 14 ++++ src/types/index.d.ts | 16 ++++ src/types/modules/child_process.d.ts | 50 +++++++++++++ src/types/modules/crypto.d.ts | 15 ++++ src/types/modules/events.d.ts | 13 ++++ src/types/modules/ffi.d.ts | 55 ++++++++++++++ src/types/modules/fs.d.ts | 30 ++++++++ src/types/modules/os.d.ts | 108 +++++++++++++++++++++++++++ src/types/modules/path.d.ts | 23 ++++++ src/types/modules/shell.d.ts | 12 +++ src/types/process.d.ts | 15 ++++ src/types/timers.d.ts | 7 ++ src/types/url.d.ts | 6 ++ tsconfig.json | 3 +- 17 files changed, 402 insertions(+), 2 deletions(-) create mode 100644 src/types/buffer.d.ts create mode 100644 src/types/fetch.d.ts create mode 100644 src/types/index.d.ts create mode 100644 src/types/modules/child_process.d.ts create mode 100644 src/types/modules/crypto.d.ts create mode 100644 src/types/modules/events.d.ts create mode 100644 src/types/modules/ffi.d.ts create mode 100644 src/types/modules/fs.d.ts create mode 100644 src/types/modules/os.d.ts create mode 100644 src/types/modules/path.d.ts create mode 100644 src/types/modules/shell.d.ts create mode 100644 src/types/process.d.ts create mode 100644 src/types/timers.d.ts create mode 100644 src/types/url.d.ts diff --git a/meson.build b/meson.build index 841d84c..958cda4 100644 --- a/meson.build +++ b/meson.build @@ -96,7 +96,7 @@ endif build_date = run_command('date', '+%Y-%m-%d', check: true).stdout().strip() version_conf = configuration_data() -version_conf.set('ANT_VERSION', '0.3.3.7') +version_conf.set('ANT_VERSION', '0.3.3.8') version_conf.set('ANT_GIT_HASH', git_hash) version_conf.set('ANT_BUILD_DATE', build_date) diff --git a/src/types/ant.d.ts b/src/types/ant.d.ts index 54f863b..0b420ea 100644 --- a/src/types/ant.d.ts +++ b/src/types/ant.d.ts @@ -50,6 +50,25 @@ interface AntRaw { typeof(t: unknown): number; } +interface HttpContext { + request: { + method: string; + url: string; + path: string; + query: string; + body: string; + headers: Record; + }; + response: { + status(code: number): HttpContext['response']; + header(name: string, value: string): HttpContext['response']; + send(body: string): void; + json(data: unknown): void; + redirect(url: string, status?: number): void; + }; + store: Record; +} + interface AntStatic { version: string; revision: string; @@ -61,10 +80,13 @@ interface AntStatic { gc(): AntGcResult; alloc(): AntAllocResult; stats(): AntStatsResult; + signal(signum: number, handler: (signum: number) => void): void; sleep(seconds: number): void; msleep(milliseconds: number): void; usleep(microseconds: number): void; + + serve(port: number, handler?: (ctx: HttpContext) => void | Promise): number; } declare const Ant: AntStatic; diff --git a/src/types/buffer.d.ts b/src/types/buffer.d.ts new file mode 100644 index 0000000..1212e8d --- /dev/null +++ b/src/types/buffer.d.ts @@ -0,0 +1,13 @@ +interface BufferStatic { + from(data: string | ArrayLike): Uint8Array & BufferMethods; + alloc(size: number): Uint8Array & BufferMethods; + allocUnsafe(size: number): Uint8Array & BufferMethods; +} + +interface BufferMethods { + toString(encoding?: 'utf8' | 'base64' | 'hex'): string; + toBase64(): string; + write(string: string, offset?: number, length?: number): number; +} + +declare const Buffer: BufferStatic; diff --git a/src/types/fetch.d.ts b/src/types/fetch.d.ts new file mode 100644 index 0000000..4f4c12d --- /dev/null +++ b/src/types/fetch.d.ts @@ -0,0 +1,14 @@ +interface FetchResponse { + status: number; + ok: boolean; + body: string; + json(): unknown; +} + +interface FetchOptions { + method?: string; + headers?: Record; + body?: string; +} + +declare function fetch(url: string, options?: FetchOptions): Promise; diff --git a/src/types/index.d.ts b/src/types/index.d.ts new file mode 100644 index 0000000..3b1778a --- /dev/null +++ b/src/types/index.d.ts @@ -0,0 +1,16 @@ +/// +/// +/// +/// +/// +/// +/// + +/// +/// +/// +/// +/// +/// +/// +/// diff --git a/src/types/modules/child_process.d.ts b/src/types/modules/child_process.d.ts new file mode 100644 index 0000000..1f75ced --- /dev/null +++ b/src/types/modules/child_process.d.ts @@ -0,0 +1,50 @@ +declare module 'child_process' { + interface SpawnResult { + stdout: string; + stderr: string; + exitCode: number; + signalCode: number | null; + pid: number; + } + + interface ChildProcess { + stdout: string; + stderr: string; + exitCode: number | null; + signalCode: number | null; + pid: number; + killed: boolean; + on(event: 'exit', listener: (code: number, signal: number | null) => void): ChildProcess; + on(event: 'close', listener: (code: number, signal: number | null) => void): ChildProcess; + on(event: 'error', listener: (err: Error) => void): ChildProcess; + on(event: 'data', listener: (data: string) => void): ChildProcess; + once(event: string, listener: (...args: unknown[]) => void): ChildProcess; + kill(signal?: number): boolean; + write(data: string): void; + end(): void; + } + + interface SpawnOptions { + cwd?: string; + shell?: boolean; + detached?: boolean; + } + + interface ExecOptions { + cwd?: string; + } + + interface SpawnSyncOptions { + input?: string; + } + + interface ForkOptions { + execArgv?: string[]; + } + + function spawn(command: string, args?: string[], options?: SpawnOptions): ChildProcess & Promise; + function exec(command: string, options?: ExecOptions): Promise; + function execSync(command: string): string; + function spawnSync(command: string, args?: string[], options?: SpawnSyncOptions): SpawnResult; + function fork(modulePath: string, options?: ForkOptions): ChildProcess & Promise; +} diff --git a/src/types/modules/crypto.d.ts b/src/types/modules/crypto.d.ts new file mode 100644 index 0000000..d5c1b57 --- /dev/null +++ b/src/types/modules/crypto.d.ts @@ -0,0 +1,15 @@ +declare module 'crypto' { + interface WebCrypto { + random(): number; + randomBytes(length: number): number[]; + randomUUID(): string; + randomUUIDv7(): string; + getRandomValues(array: T): T; + } + + const webcrypto: WebCrypto; + + function randomBytes(length: number): number[]; + function randomUUID(): string; + function getRandomValues(array: T): T; +} diff --git a/src/types/modules/events.d.ts b/src/types/modules/events.d.ts new file mode 100644 index 0000000..73e028b --- /dev/null +++ b/src/types/modules/events.d.ts @@ -0,0 +1,13 @@ +declare module 'events' { + class EventEmitter { + on(event: string, listener: (...args: unknown[]) => void): this; + addListener(event: string, listener: (...args: unknown[]) => void): this; + once(event: string, listener: (...args: unknown[]) => void): this; + off(event: string, listener: (...args: unknown[]) => void): this; + removeListener(event: string, listener: (...args: unknown[]) => void): this; + emit(event: string, ...args: unknown[]): boolean; + removeAllListeners(event?: string): this; + listenerCount(event: string): number; + eventNames(): string[]; + } +} diff --git a/src/types/modules/ffi.d.ts b/src/types/modules/ffi.d.ts new file mode 100644 index 0000000..cf49828 --- /dev/null +++ b/src/types/modules/ffi.d.ts @@ -0,0 +1,55 @@ +declare module 'ffi' { + type FFIType = + | 'void' + | 'int8' + | 'int16' + | 'int' + | 'int64' + | 'uint8' + | 'uint16' + | 'uint64' + | 'float' + | 'double' + | 'pointer' + | 'string' + | '...'; + + interface FFITypeConstants { + void: 'void'; + int8: 'int8'; + int16: 'int16'; + int: 'int'; + int64: 'int64'; + uint8: 'uint8'; + uint16: 'uint16'; + uint64: 'uint64'; + float: 'float'; + double: 'double'; + pointer: 'pointer'; + string: 'string'; + spread: '...'; + } + + interface FunctionSignature { + returns: FFIType; + args: FFIType[]; + } + + interface FFILibrary { + define(name: string, signature: FunctionSignature | [FFIType, FFIType[]]): (...args: unknown[]) => unknown; + call(name: string, ...args: unknown[]): unknown; + } + + const suffix: 'dylib' | 'so' | 'dll'; + const FFIType: FFITypeConstants; + + function dlopen(libraryPath: string): FFILibrary; + function alloc(size: number): number; + function free(ptr: number): void; + function read(ptr: number, size: number, type?: FFIType): unknown; + function write(ptr: number, value: unknown, type?: FFIType): void; + function pointer(value: unknown): number; + function callback(signature: FunctionSignature | [FFIType, FFIType[]], fn: (...args: unknown[]) => unknown): number; + function freeCallback(ptr: number): void; + function readPtr(ptr: number): number; +} diff --git a/src/types/modules/fs.d.ts b/src/types/modules/fs.d.ts new file mode 100644 index 0000000..c24572b --- /dev/null +++ b/src/types/modules/fs.d.ts @@ -0,0 +1,30 @@ +declare module 'fs' { + interface Stats { + size: number; + mode: number; + isFile(): boolean; + isDirectory(): boolean; + } + + function readFile(path: string): Promise; + function readFileSync(path: string): string; + function stream(path: string): Promise; + function open(path: string): string; + function writeFile(path: string, data: string): Promise; + function writeFileSync(path: string, data: string): void; + function appendFileSync(path: string, data: string): void; + function copyFileSync(src: string, dest: string): void; + function renameSync(oldPath: string, newPath: string): void; + function unlink(path: string): Promise; + function unlinkSync(path: string): void; + function mkdir(path: string, options?: { recursive?: boolean }): Promise; + function mkdirSync(path: string, options?: { recursive?: boolean }): void; + function rmdir(path: string): Promise; + function rmdirSync(path: string): void; + function stat(path: string): Promise; + function statSync(path: string): Stats; + function exists(path: string): Promise; + function existsSync(path: string): boolean; + function readdir(path: string): Promise; + function readdirSync(path: string): string[]; +} diff --git a/src/types/modules/os.d.ts b/src/types/modules/os.d.ts new file mode 100644 index 0000000..68aa43c --- /dev/null +++ b/src/types/modules/os.d.ts @@ -0,0 +1,108 @@ +declare module 'os' { + interface CpuInfo { + model: string; + speed: number; + times: { + user: number; + nice: number; + sys: number; + idle: number; + irq: number; + }; + } + + interface NetworkInterfaceInfo { + address: string; + netmask: string; + family: 'IPv4' | 'IPv6'; + mac: string; + internal: boolean; + cidr: string | null; + } + + interface UserInfo { + username: string; + uid: number; + gid: number; + shell: string; + homedir: string; + } + + interface OsConstants { + signals: { + SIGHUP?: number; + SIGINT?: number; + SIGQUIT?: number; + SIGILL?: number; + SIGTRAP?: number; + SIGABRT?: number; + SIGIOT?: number; + SIGBUS?: number; + SIGFPE?: number; + SIGKILL?: number; + SIGUSR1?: number; + SIGUSR2?: number; + SIGSEGV?: number; + SIGPIPE?: number; + SIGALRM?: number; + SIGTERM?: number; + SIGCHLD?: number; + SIGCONT?: number; + SIGSTOP?: number; + SIGTSTP?: number; + SIGTTIN?: number; + SIGTTOU?: number; + SIGURG?: number; + SIGXCPU?: number; + SIGXFSZ?: number; + SIGVTALRM?: number; + SIGPROF?: number; + SIGWINCH?: number; + SIGIO?: number; + SIGSYS?: number; + }; + errno: Record; + priority: { + PRIORITY_LOW: number; + PRIORITY_BELOW_NORMAL: number; + PRIORITY_NORMAL: number; + PRIORITY_ABOVE_NORMAL: number; + PRIORITY_HIGH: number; + PRIORITY_HIGHEST: number; + }; + dlopen: { + RTLD_LAZY?: number; + RTLD_NOW?: number; + RTLD_GLOBAL?: number; + RTLD_LOCAL?: number; + RTLD_DEEPBIND?: number; + }; + UV_UDP_REUSEADDR: number; + } + + const EOL: string; + const devNull: string; + const constants: OsConstants; + + function arch(): string; + function platform(): string; + function type(): string; + function release(): string; + function version(): string; + function machine(): string; + function hostname(): string; + function homedir(): string; + function tmpdir(): string; + function endianness(): 'BE' | 'LE'; + function uptime(): number; + function totalmem(): number; + function freemem(): number; + function availableParallelism(): number; + function cpus(): CpuInfo[]; + function loadavg(): [number, number, number]; + function networkInterfaces(): Record; + function userInfo(): UserInfo; + function getPriority(pid?: number): number; + function setPriority(priority: number): void; + function setPriority(pid: number, priority: number): void; +} diff --git a/src/types/modules/path.d.ts b/src/types/modules/path.d.ts new file mode 100644 index 0000000..1d58cac --- /dev/null +++ b/src/types/modules/path.d.ts @@ -0,0 +1,23 @@ +declare module 'path' { + interface ParsedPath { + root: string; + dir: string; + base: string; + ext: string; + name: string; + } + + const sep: string; + const delimiter: string; + + function basename(path: string, ext?: string): string; + function dirname(path: string): string; + function extname(path: string): string; + function join(...paths: string[]): string; + function normalize(path: string): string; + function resolve(...paths: string[]): string; + function relative(from: string, to: string): string; + function isAbsolute(path: string): boolean; + function parse(path: string): ParsedPath; + function format(pathObject: Partial): string; +} diff --git a/src/types/modules/shell.d.ts b/src/types/modules/shell.d.ts new file mode 100644 index 0000000..3294837 --- /dev/null +++ b/src/types/modules/shell.d.ts @@ -0,0 +1,12 @@ +declare module 'shell' { + interface ShellResult { + stdout: string; + stderr: string; + exitCode: number; + text(): string; + lines(): string[]; + } + + function $(strings: TemplateStringsArray, ...values: unknown[]): ShellResult; + function $(command: string): ShellResult; +} diff --git a/src/types/process.d.ts b/src/types/process.d.ts new file mode 100644 index 0000000..79b44b9 --- /dev/null +++ b/src/types/process.d.ts @@ -0,0 +1,15 @@ +type ProcessEnv = { + [key: string]: string | undefined; +} & { toObject(): Record }; + +interface Process { + env: ProcessEnv; + argv: string[]; + pid: number; + platform: string; + arch: string; + exit(code?: number): never; + cwd(): string; +} + +declare const process: Process; diff --git a/src/types/timers.d.ts b/src/types/timers.d.ts new file mode 100644 index 0000000..b236626 --- /dev/null +++ b/src/types/timers.d.ts @@ -0,0 +1,7 @@ +declare function setTimeout(callback: () => void, delay: number): number; +declare function clearTimeout(timerId: number): void; +declare function setInterval(callback: () => void, delay: number): number; +declare function clearInterval(timerId: number): void; +declare function setImmediate(callback: () => void): number; +declare function clearImmediate(immediateId: number): void; +declare function queueMicrotask(callback: () => void): void; diff --git a/src/types/url.d.ts b/src/types/url.d.ts new file mode 100644 index 0000000..7b12c00 --- /dev/null +++ b/src/types/url.d.ts @@ -0,0 +1,6 @@ +declare function encodeURI(uri: string): string; +declare function encodeURIComponent(component: string): string; +declare function decodeURI(uri: string): string; +declare function decodeURIComponent(component: string): string; +declare function escape(str: string): string; +declare function unescape(str: string): string; diff --git a/tsconfig.json b/tsconfig.json index feb4e11..082eb3b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,6 +2,7 @@ "compilerOptions": { "noEmit": true, "strict": true, + "noUnusedLocals": false, "target": "ES2024", "module": "ESNext", @@ -9,5 +10,5 @@ "lib": ["ES2024", "DOM", "DOM.Iterable"], "typeRoots": ["./node_modules/@types", "./src/types"] }, - "include": ["src/types/**/*.d.ts", "src/core/**/*"] + "include": ["src/types/**/*.d.ts", "src/core/**/*", "examples/**/*"] } -- 2.51.2