Repository navigation
feat(p2-shim): add host-side write/read helpers for browser preopens - #2237
Conversation
vados-cosmonic
left a comment
There was a problem hiding this comment.
So I really like the idea of having these helpers, but I think we need a different namespace for them. Up until now the helpers that have existed have been primarily to configure the p2-shim in some way that was necessary -- it's nice to have helper functions that make using WASI p2 (and p3) easier, but they may need to be in another package to be well contained.
I get that this should all be hopefully tree shaken away anyway, but I'd like to keep the intent of the underscore prefix helpers distinct.
Can we can add new export (...helpers/filesystem? ...helpers?) to preview2-shim and move the helper functions to a different file?
188de2c to
0d55149
Compare
|
I completely agree with the approach. I pushed the helpers to a folder within the target platform. Those should be easy to migrate to a separate package whenever convenient. |
5b6d9b2 to
d773152
Compare
d773152 to
b27f16d
Compare
6ef83ab to
e22ab80
Compare
f15b0b7 to
924750c
Compare
924750c to
b568751
Compare
|
🎉 The code in this PR was made available in the following release: |
Summary
Hosts implementing shell-style redirection need to write bytes into a preopen's tree from JS without a guest, and had nothing adapter-agnostic to reach for — only the in-memory adapter's whole-tree
_setFileData/_getFileData.Changes
Adds two host-side filesystem helpers, built only on the public
Descriptorinterface so they work with any adapter:writeFileAt(virtualPath, path, data, { append? })— writes bytes into a preopen's tree, creating missing intermediate directories, truncating by defaultreadFileAt(virtualPath, path)— reads a file's full contents back out, in chunksThese are exposed from a new
helpers/filesystemsubpath export, keeping the convenience helpers separate from the shim's implementation and the underscore-prefixed configuration surface:The shared implementation lives in
src/common/helpers/filesystem.ts(adapter-agnostic, preopens injected); thin per-build entries undersrc/browser/helpers/andsrc/nodejs/helpers/wire in each build's preopens so the import resolves under both the browser-default andnodeconditions.The shared helper opens the file with
write/readdescriptor flags (the Node build maps empty flags to a read-only fd, so writes failed there), loops until every byte is written, and disposes the descriptors it opens. A Node test covers thenodeentry against a real host directory, alongside the browser in-memory tests. Both suites run concurrently: each test mounts its own preopen (a fresh in-memory tree in the browser, a fresh temp directory viafs/promiseson Node) through a small helper and cleans up in afinally, so there are nobeforeEach/afterEachhooks and no shared state between tests.Documentation
The helpers are documented in the Jco book under the WASI support section, at Preview 2 shim → Filesystem helpers (
docs/src/wasi/preview2-shim/helpers/filesystem.md). It covers which preopens the helpers see (the global set, not an isolatedWASIShim's), each function, error behaviour, and a shell-style redirection example. The Preview 2 shim overview page gains ahelpers/filesystemrow in its subpath table and a Helpers bullet, and the package README's helper paragraph now mentions both helpers and links to both book pages.Node consumers now get the Node typings
The
.and./*exports had a top-leveltypespointing at the browser.d.tsahead of thenodeblock. TypeScript undernodenextincludesnodein its condition set but stops at the first matching condition, so Node consumers were typed against the browser build (which is why_setPreopensappeared to reject host paths). Thetypesentries are now nested undernodeanddefaultwith no top-leveltypeskey, which oxfmt's package.json sorting leaves alone. Verified with a consumer project:moduleResolution: nodenextresolvesdist/nodejs/*.d.ts,moduleResolution: bundlerresolvesdist/browser/*.d.ts, and Node runtime resolution is unchanged. In-repo consumers pin the published 0.28.0 shim, so nothing else in the workspace changes until the next release.