Package Exports
- @rangermauve/webrun
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@rangermauve/webrun) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
WebRun
A custom module loader and global shim for Node to make it compatible with the browser.
The goal is to make code that works in browsers first, but can also run anywhere that Node runs.
You can use import.meta.require to load node modules, but please consider opening an issue for adding a Web API that performs the same function if one exists.
Warning: This is still in development. Use at your own risk!
Bug reports welcome!
Usage:
# Install the CLI
npm install -g @rangermauve/webrun
# Load a module from the web and log to the console
webrun "https://rangermauve.hashbase.io/example.js"
# Run a local file
webrun ./example.jsThen in your JS:
import example from "https://rangermauve.hashbase.io/esm.js";
import p2pexample from "dat://rangermauve.hashbase.io/esm.js";
example();
p2pexample();Help it's not working!
- Delete the
.webrunfolder in the current directory. This will clear the cache - If that doesn't work, raise an issue.
How it works:
The new experimental-modules feature in Node.js is great, but it currently only works with file: URLs.
This means that modules made for the web are totally incompatible with Node. As a result, there's now four environments to code against: The legacy web with script tags, the web with ESM, Legacy CommonJS modules, and ESM in Node.js.
Luckily, node's vm builtin module now has support for custom ESM loaders. This means that we can now create a context that's separated from Node's globals and use anything we want for loading the contents of modules.
That's exactly how this works. It intercepts calls to https:// imports, downloads the content to the ./.webrun/web-cache folder, and loads it with the VM module.
Some browser APIs have been added to the global scope so hopefully a lot of modules made for browsers should work here, too. Feel free to open an issue to add your favorite missing browser API.
In addition to loading content from https:// URLs, this loader also supports dat:// URLs. This way you can download code right from the peer to peer web!
You can still load Node modules by using import.meta.require, but this should only be done for APIs that you absolutely can't get on the web because otherwise your code won't be portable.
PRs for additional protocols are welcome! All you need is an async function that takes a URL, and returns the file content string.
Progress:
- Able to load from the filesystem
- Able to load HTTPS URLs
- Able to load Dat URLs
- Browser APIs
- console.*
- timers
- atob and btoa
- fetch
- websocket
- localStorage (Persists to
./.webrun/localstorage) - indexDB (Doesn't persist, removed)
- crypto.randomBytes() using node's crypto module.
- SubtleCrypto (or with this)
- TextEncoder / TextDecoder
- WebRTC
- Cache Storage
- self.navigator
- self.location
- WindowEventHandlers
- self.close()
- self.postMessage / self.onmessage
- EventTarget
- libdweb APIs
- Dat protocol support
- Load from Dat URLs
- DatArchive global
- Experimental Beaker APIs (does it make sense?)
- DatPeers Issue
- Library
- IPFS
- Load from IPFS / IPNS URLs
- ipfs global