Filesystem
Read and write files in the in-browser filesystem.
Strapkit gives you an async filesystem inside the browser. The methods on
this page wrap that filesystem in promises — every call waits for the
runtime to load the first time, so you don't have to call init()
yourself.
All paths are absolute. The default working directory is /.
write(path, contents)
write(path: string, contents: string | Uint8Array): Promise<void>Write contents to path, creating any missing parent directories along
the way. Strings are written as UTF-8; pass a Uint8Array for binary
data.
await sk.write('/app/package.json', JSON.stringify({ name: 'demo' }));
await sk.write('/app/bin/data.bin', new Uint8Array([0xde, 0xad, 0xbe, 0xef]));writeMultiple(files)
writeMultiple(files: Record<string, { file: { contents: string | Uint8Array } }>): Promise<void>Write a batch of files in one call. The shape mirrors a common file-tree format used by other in-browser runtimes, so you can hand snapshots between tools without translating them.
await sk.writeMultiple({
'/app/package.json': { file: { contents: '{}' } },
'/app/index.js': { file: { contents: 'console.log(1)' } },
});Each entry creates parent directories as needed. There's no rollback — if the tenth entry fails, the first nine are already written.
read(path)
read(path: string): Promise<string>Read the file at path as UTF-8 text.
readdir(path)
readdir(path?: string): Promise<string[]>List the entries directly inside path (default /). The result
includes . and .. — filter them out yourself if you don't want them.
isDir(path)
isDir(path: string): Promise<boolean>Returns true if path exists and is a directory. Returns false for
files, missing paths, and anything else — it never throws, so it's safe
to call on unknown input.
readTree(dir)
readTree(dir?: string): Promise<TreeEntry[]>
type TreeEntry =
| { name: string; path: string; type: 'file' }
| { name: string; path: string; type: 'directory'; children: TreeEntry[] };Walk dir recursively and return a sorted tree. Directories come before
files, then everything is alphabetical by name. . and .. are
skipped, and entries that can't be read are silently dropped.
const tree = await sk.readTree('/app');
console.log(tree[0].name, tree[0].type, tree[0].children?.length);mkdir(path)
mkdir(path: string): Promise<void>Create path and any missing parent directories. Equivalent to
mkdir -p on the command line.
rm(path)
rm(path: string): Promise<void>Remove a file or a whole directory tree. Files are removed directly; directories are emptied recursively and then removed.
There's no force flag — removing a path that doesn't exist throws.
Wrap the call in a try/catch if you want idempotent behaviour.
snapshot(filter?)
snapshot(filter?: (path: string) => boolean): Promise<Record<string, string>>Walk the whole filesystem from / and return an object mapping each
file's path to its text contents. If you pass filter, only files where
it returns true are included. Directories aren't in the result, and
files that can't be read are skipped.
const snap = await sk.snapshot((p) => p.startsWith('/app') && !p.includes('/node_modules/'));For larger trees or binary-safe snapshots, use
cacheSave instead.