Skip to content

@wtf/platform-memory

Documentation / @wtf/platform-memory

Run any program on @wtf/platform in memory, for a test or a dry run. memoryPlatform() gives you a whole platform that keeps what your program logs, displays and opens, answers what it fetches and the processes it starts with your own scripts, and records each process it runs. Its timers and crypto are the real ones, so your program runs at its own speed.

SubpathWhat you get
.MemoryPlatform, MemoryPlatformOptions, SpawnScript, ScriptAnswer, SpawnRun, …
./platformmemoryPlatform({ env, dirs, terminal, fetch, workers, spawn, directory })
./processmemorySpawn(script, { cwd, env }): a spawn that a script answers, with each run in its runs
import { exec } from '@wtf/platform/process'
import { memoryPlatform } from '@wtf/platform-memory/platform'
const platform = memoryPlatform({
env: { CI: 'true' },
spawn: (command, args) =>
command === 'git' && args[0] === 'rev-parse' ? { stdout: 'abc123\n' } : undefined,
fetch: (request) => Response.json({ url: request.url })
})
const { stdout } = await exec(platform.spawn, 'git', ['rev-parse', 'HEAD'])
platform.console.log('At %s', stdout.trim())
platform.spawn.runs // [{ command: 'git', args: ['rev-parse', 'HEAD'], cwd: '/', env: { CI: 'true' } }]
platform.console.records // [{ method: 'log', message: 'At abc123', … }]

What each member is:

  • env is the record you give, none by default.
  • path is POSIX, and resolves against dirs.cwd. dirs is what you give, as testDirs({ cwd }) of @wtf/platform/dirs makes it, or a Linux’s folders of /home/user, working in /.
  • console keeps each record, and terminal keeps each text it displays and reads the lines of terminal.input. open keeps each URL.
  • fetch answers each request with your handler, a function of a Request that gives a Response. With none, each request fails as one with no network, a TypeError.
  • spawn answers each process with your script, a function of its command, its arguments and { input, cwd, env } that gives { stdout, stderr, code }, each optional. The process first reads its stdin to the end, so the script gets all of its input. A script that gives undefined says the command is not there: the process’s status rejects with a SystemError of NotFound, as a real platform’s does. Every process is recorded in spawn.runs, with the folder and the environment it ran with, whether a script answered it or not. An aborted signal ends a process that has not been answered yet, as SIGTERM would.
  • navigator.storage.getDirectory() gives the folder your directory function gives, called once. With none, it rejects with a NotSupportedError. This package depends on no file system: you bring your own folder handle, such as memoryDirectory() from @wtf/platform-fs-memory/handles.
  • Locks, channels, ports and workers run in this realm, each worker the script of its URL in workers. setTimeout, clearTimeout, performance and crypto are the real globals, passed through.

For a test where time must stand still, use testPlatform() of @wtf/platform/test, whose timers move only when you tell them to. The memory platform passes the same suites as Node’s.

It uses only web standards, imports nothing of Node, and depends on nothing but @wtf/platform.

Modules