# gizlang:node

TypeScript's and Node's globals: the timers and `new Promise`, and the ones that reach the process: `fetch`, `process`, and the file functions of `node:fs` and `node:fs/promises`. A program's own code calls them with no System parameter: they use the process's System, the authority that a C declaration has too (checker ambientAuthority). A package whose manifest limits its imports gets none of them, and passes a System instead.

```gizlang
import * as Node from "gizlang:node";
```

## Types

### `Env`

```gizlang
type Env
```

The environment of the process: `process.env.HOME` reads one variable, undefined when it is not set.

Opaque: create and use it through this module's functions.

### `Process`

```gizlang
type Process
```

Node's `process`: its arguments, environment and working directory, and the exit.

Opaque: create and use it through this module's functions.

### `Response`

```gizlang
type Response
```

An HTTP response as `fetch` gives it: the status, its headers, and the body read whole.

- `status`: `f64`
- `ok`: `bool`
- `headers`: `Vec<gizlang_http.Header>`

### `RequestInit`

```gizlang
type RequestInit = { method?: string; body?: string; }
```

The options of `fetch`: the method (GET when none) and the body.

- `method`: `_gizlang_sdk_core.Option<string>`
- `body`: `_gizlang_sdk_core.Option<string>`

## Functions

### `fetch`

```gizlang
function fetch(url: string, init?: RequestInit): Response
```

An HTTP or HTTPS request to URL, read whole, as Node's `fetch(url)`. It throws an Error when the URL is not http or
https, or the request fails; a status of 400 or more is a Response with `ok` false.

Effects: fail, read, write, console, io.

### `readFileSync`

```gizlang
function readFileSync(path: string, encoding?: string): string
```

The text of the file PATH (`readFileSync(path, "utf8")`). The encoding is read as UTF-8 whatever it names.

Effects: fail, read, write, console, io.

### `writeFileSync`

```gizlang
function writeFileSync(path: string, text: string): void
```

Replace the file PATH with TEXT, made when it is missing.

Effects: fail, read, write, console, io.

### `appendFileSync`

```gizlang
function appendFileSync(path: string, text: string): void
```

Add TEXT at the end of the file PATH, made when it is missing.

Effects: fail, read, write, console, io.

### `existsSync`

```gizlang
function existsSync(path: string): bool
```

Whether PATH exists.

Effects: fail, read, write, console, io.

### `readFile`

```gizlang
async function readFile(path: string, encoding?: string): Promise<string>
```

`node:fs/promises` readFile: the text of the file PATH.

Effects: fail, read, write, console, io.

### `writeFile`

```gizlang
async function writeFile(path: string, text: string): Promise<void>
```

`node:fs/promises` writeFile: replace the file PATH with TEXT.

Effects: fail, read, write, console, io.

### `setTimeout`

```gizlang
function setTimeout(run: () => void, ms?: number): number
```

Run RUN once after MS milliseconds, when the code before it has run: the end of main, or a Promise that waits.

Effects: fail, read, write, console, io.

### `setInterval`

```gizlang
function setInterval(run: () => void, ms?: number): number
```

Run RUN every MS milliseconds until clearInterval.

Effects: fail, read, write, console, io.

### `clearTimeout`

```gizlang
function clearTimeout(id: number): void
```

Stop the timer ID; nothing for an ID that has run or never was.

Effects: fail, read, write, console, io.

### `clearInterval`

```gizlang
function clearInterval(id: number): void
```

Stop the interval ID.

Effects: fail, read, write, console, io.

### `runTimers`

```gizlang
function runTimers(): void
```

Run every timer left: the end of a main that set one (parser timers).

Effects: fail, read, write, console, io.

### `waitPromise`

```gizlang
function waitPromise(executor: (resolve: () => void) => void): void
```

`new Promise(resolve => ...)` with no value: EXECUTOR runs, then the timers until it calls resolve.

Effects: fail, read, write, console, io.

### `newPromise`

```gizlang
function newPromise<T>(executor: (resolve: (value: T) => void) => void): T
```

`new Promise<T>(resolve => ...)`: EXECUTOR runs, then the timers until it calls resolve with the value.

### `newPromiseOr`

```gizlang
function newPromiseOr<T>(executor: (resolve: (value: T) => void, reject: (reason: Error) => void) => void): T
```

`new Promise<T>((resolve, reject) => ...)`: as newPromise, and a call of reject throws its reason.

### `random`

```gizlang
function random(): number
```

A number in [0, 1), as TypeScript's `Math.random()`. Not for keys or tokens: gizlang:crypto's randomBytes is.

Effects: fail, read, write, console, io.

## Methods

### `Env.property`

```gizlang
function Env.property(self: Env, name: string): Option<string>
```

The value of the environment variable NAME (`process.env.NAME`), or undefined.

Effects: fail, read, write, console, io.

### `Process.argv`

```gizlang
function Process.argv(self: Process): Vec<string>
```

The arguments (`process.argv`). `argv[0]` and `argv[1]` stand for Node's runtime and script, so the arguments
start at index 2, where `process.argv.slice(2)` reads them.

Effects: fail, read, write, console, io.

### `Process.env`

```gizlang
function Process.env(self: Process): Env
```

The environment (`process.env`).

### `Process.platform`

```gizlang
function Process.platform(self: Process): string
```

The operating system as Node names it: "linux", "darwin" or "win32".

Effects: fail, read, write, console, io.

### `Process.exit`

```gizlang
function Process.exit(self: Process, code: number): void
```

End the process with CODE (0 to 255), as `process.exit(code)` does.

Effects: fail, read, write, console, io.

### `Process.cwd`

```gizlang
function Process.cwd(self: Process): string
```

The working directory.

Effects: fail, read, write, console, io.

### `Response.text`

```gizlang
function Response.text(self: Response): string
```

The body as UTF-8 text.

Effects: fail.

### `Response.json`

```gizlang
function Response.json(self: Response): Json
```

The body parsed as JSON.

Effects: fail.

## Constants

### `process`

```gizlang
const process: Process = { _unused: false }
```

Node's `process`.

