@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.
| Subpath | What you get |
|---|---|
. | MemoryPlatform, MemoryPlatformOptions, SpawnScript, ScriptAnswer, SpawnRun, … |
./platform | memoryPlatform({ env, dirs, terminal, fetch, workers, spawn, directory }) |
./process | memorySpawn(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:
envis the record you give, none by default.pathis POSIX, and resolves againstdirs.cwd.dirsis what you give, astestDirs({ cwd })of@wtf/platform/dirsmakes it, or a Linux’s folders of/home/user, working in/.consolekeeps each record, andterminalkeeps each text it displays and reads the lines ofterminal.input.openkeeps each URL.fetchanswers each request with your handler, a function of aRequestthat gives aResponse. With none, each request fails as one with no network, aTypeError.spawnanswers 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 itsinput. A script that givesundefinedsays the command is not there: the process’sstatusrejects with aSystemErrorofNotFound, as a real platform’s does. Every process is recorded inspawn.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, asSIGTERMwould.navigator.storage.getDirectory()gives the folder yourdirectoryfunction gives, called once. With none, it rejects with aNotSupportedError. This package depends on no file system: you bring your own folder handle, such asmemoryDirectory()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,performanceandcryptoare 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.
