Node effects

The operations a program performs on Node.js, and the NodeProgram type a command-line program is. module.f.mjs declares the operations and their helpers, types.ts their types, and module.mjs is the runner that performs them against Node's own APIs.

import { exitStep, readUtf8File } from './module.f.mjs'
import { log } from '../common/module.f.mjs'
import { mapStep, step } from '../module.f.mjs'

/** Prints how many lines a file has, counting line breaks as `wc -l` does. @type {import('./types.ts').NodeProgram} */
export const main = ({ args }) => {
    const count = mapStep(readUtf8File(args[0]), text => text.split('\n').length - 1)
    const printed = step(count, n => log(`${n} lines`))
    return exitStep(printed)
}

fjs run lines.f.mjs README.md runs it. exitStep is the exit-code policy at the end of the chain: 0 on success, and on failure the error printed to stderr and 1.

The operations

GroupOperations
filesreadFile, readUtf8File, readWhole, writeFile, writeUtf8File, writeFiles, writeBytes, writeChunks, mkdir, readdir, rm, rmdir, rename, access, exists, stat, open, fstat, pread, close, createExclusive, writeExclusive
networkfetch, createServer, listen, forever
processesexec, spawn, childWait
otherinflate, now, resolveFileModule, test
from commonwrite, log, error, errorExit, read, readLine, sandbox, catch_, import_, all, allOk, both, re-exported here

Every operation answers an IoResult<T>: the value, an IoError (the host's failure, with its OS code such as 'ENOENT'), or NotImplemented. isNotFound and isDirectory test an error, so a program can forgive the one it expects: catchStep(readFile(path), orElse(isNotFound, null)).

Running

RunnerUse it for
run(main) in module.mjsa bin script: performs the program, then exits with its code
runEffect(main) in module.mjsthe same, resolving to the exit code without exiting
virtualproofs: a synchronous, in-memory file system and console
memorythe asynchronous interpreter for ../memory's operations

A program does not import the runner; the fjs CLI and the tests do.

Emergent Testing

Idle. Press Run to start the suite.

  • ./proof.f.mjs
  • ./virtual/proof.f.mjs
  • ./virtual/readdir/proof.f.mjs