Support plugin-tee in the web host #15
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Support plugin-tee in the web host
This PR adds full support for the
plugin-tee
plugin in the web host, fixing the issues described in #12.Example of use:
tee
command writes the content of$0
(last command output) to the file specified in the argument.The Challenge
The
plugin-tee
plugin allows users to write content to files, making it the first plugin that performs WRITE operations in the REPL. While it worked correctly in the CLI version, the web host (relying on a virtual filesystem for the browser) encountered several critical errors:tee
over existing files:RangeError: offset is out of bounds
followed byRuntimeError: unreachable
tee -a
(append mode):TypeError: Resource error: Not a valid "OutputStream" resource
The Solution: Customizing the Filesystem Shim
1. Setting up a Local Fork
To fix these filesystem issues, a local fork of
@bytecodealliance/preview2-shim
was set up inpackages/web-host/overrides
. This allows customization of the filesystem implementation without waiting for upstream changes.Files copied from the original: 0906dd3
Configuration Updates: ce56bee
2. Fix #1: Correcting File Overwriting Behavior 01a56a9
The first issue was in the
Descriptor#writeViaStream()
method, which incorrectly tried to preserve existing file content when writing. This broke thetee
command's ability to overwrite files.Changes to
packages/web-host/overrides/@bytecodealliance/preview2-shim/lib/browser/filesystem.js
:What This Fix Does:
3. Fix #2: Implementing
appendViaStream
58bf163The second issue was in the
Descriptor#appendViaStream()
method, which was just a stub that logged to console.Changes to
packages/web-host/overrides/@bytecodealliance/preview2-shim/lib/browser/filesystem.js
:Before (Broken Implementation):
After (Working Implementation):
What This Fix Does:
4. Why This Approach?
The
@bytecodealliance/preview2-shim
is the bridge between WASI system calls and JavaScript implementations. By customizing it locally:Forking the shim also means that it can't be updated automatically anymore. This is a necessary trade-off. Maybe the modifications could be upstreamed to the original repo.
Results
After implementing both fixes:
✅
tee
creates new files - Works perfectly✅
tee
overwrites existing files - No more range errors or incorrect concatenation✅
tee -a
appends to files - No more resource errors✅ File truncation works correctly - Proper file management
✅ All e2e tests pass - Including comprehensive tee scenarios
Technical Impact
This implementation demonstrates how to:
Additional Changes
Closes #12
Now the web-host provides to the browser a virtual filesystem that supports WRITE operations.