WebContainers: Run Node.js natively in the browser
blog.stackblitz.com
blog.stackblitz.com
JS Developers come in: so we can compile node.js to WASM and run that in the browser?! Yay!! Now we can have backend JS running in the browser alongside browser JS!
Jokes aside: this is actually quite interesting, the demo is very impressive (except for the "only works in Chromium-based browser" message on the next.js demo, where the preview should be - despite Mozzila being one of the main WASM backers and Firefox having arguably the best WASM engine).
The similarly named "FileSystem API" gives you access to a sandboxed "virtual drive" in the browser. Firefox has supported that for years, and polyfills are possible, but it's irrelevant in this case.
Meanwhile, WebAssembly suffers from there not being a good programming language that compiles to it natively. I think the Go support is great, but there's no library support for building the webapps that people get paid to build (React, Apollo, etc. all exist for a reason, however much you hate that reason). AssemblyScript was supposed to be this language, but it's just a Typescript-inspired programming language, it's not Typescript and the npm ecosystem.
I don't know what the details of this implementation are, but the direction I'd like to see web development move is having a statically typed language that compiles to VM bytecode. This sounds like the first step for removing Javascript as a Typescript intermediary. Once that happens, then modern programming language features can be bolted on, people can add good compiler optimizations that result in minimal bytes output for users to download, the module system can be made stricter (like "go mod"), etc. Javascript is just a little bit more dynamic than anyone really wants, and it makes the build/deployment tooling complicated and, frankly, kind of bad.
Why? Because the initial parser is relatively cheap. This makes all of the code available, even if a stray eval() calls something unexpected.
The real resource expense comes when it's time to JIT a hotspot. But a hotspot is by definition code you use for sure. And code you use a lot.
JS engines use tracing JIT. Tracing allows JIT compilers to see how code runs in practice, and compile THAT to machine language. How it's organized on the file system etc is entirely irrelevant.
So basically in a trace JIT system, code that isn't hot is interpreted, and code that is hot is JIT-ted (and code that's very hot, gets JIT-ted with higher optimization).
Interpreting is the new "tree shaking". It saves the compiler a lot of work, but also it won't crash your app (unlike bad tree shaking).
Java… you want Java. And so the cycle continues!
We started with Java. It didn't gain any traction for browser scripting. They took "Java" out and called it JavaScript. Now we take the "script" out and call it WebAssembly. If someone in 1995 made Java a DOM-editing thing instead of an applet thing, we could have saved 25 years of running around in circles :)
(Going back even further, we all connected to a mainframe with a dumb terminal. The desktop revolution happened... and now we're back to dumb terminals attached to a mainframe. But we call it "The Cloud" instead, and the dumb terminal fits in your pocket.)
Maybe the past wasn't as dumb as we think it was. We just weren't smart enough to understand it at the time.
Overall it's hard to say how Sun could have won here, perhaps acquiring macromedia and using Flash technology to make a browser that ran those cool portals out of the box, and hey scripted first-class in Java too btw ;-)
> In 1995, Netscape hired Brendan Eich with the promise of letting him implement Scheme (a Lisp dialect) in the browser.
How JavaScript Was Created - http://speakingjs.com/es5/ch04.html
---
> Whether that language should be Scheme was an open question, but Scheme was the bait I went for in joining Netscape. Previously, at SGI, Nick Thompson had turned me on to SICP.
> ..The diktat from upper engineering management was that the language must “look like Java”. That ruled out Perl, Python, and Tcl, along with Scheme.
<script type=“text/scheme”>
I also wish browsers had built out the ability to plug-in interpreters for processing script tags.Is Grain insufficient for some reason?
I remember back when SOAP still had the upperhand against REST but just barely the W3C published a document where they showed how you could implement HTTP over SOAP to basically mass ridicule. Unfortunately can't find it anymore, but it was pretty funny.
Tim Bray, 2004, "HTTP over SOAP!?!?!?": https://www.tbray.org/ongoing/When/200x/2004/05/01/SRRH
Erik Wilde, 2008, "HTTP over SOAP over HTTP": https://dret.typepad.com/dretblog/2008/11/http-over-soap-ove...
William Vambenepe, 2008, "WS Resource Access working group starting at W3C": http://stage.vambenepe.com/archives/436
Here are some of the specs:
Web Services Transfer (WS-Transfer). This specification describes a general SOAP-based protocol for accessing XML representations of Web service-based resources. https://www.w3.org/Submission/WS-Transfer/
Web Services Resource Transfer (WS-RT). This specification defines extensions to WS-Transfer. While its initial design focuses on management resource access its use is not necessarily limited to those situations. https://www.w3.org/Submission/WSRT/
https://developer.mozilla.org/en-US/docs/WebAssembly/Concept...
> WebAssembly is a different language from JavaScript, but it is not intended as a replacement. Instead, it is designed to complement and work alongside JavaScript, allowing web developers to take advantage of both languages' strong points
"Not intended as a replacement"
WASM is a math coprocessor for JS. WASM can't even talk to the DOM API directly (which is the API you use to build web pages in JS); you have to write JavaScript glue code for any/all I/O in WASM.
For now. It's planned.
If WASM gains a foreign object model calling convention or whatever, we're in game.
- https://github.com/WebAssembly/reference-types
- https://github.com/webassembly/threads
- https://github.com/WebAssembly/exception-handling
- https://github.com/WebAssembly/function-references
- https://github.com/WebAssembly/gc
Though it is true that the original MVP was very barebone and could be compared to assembler. However, the reference types proposal has been shipped in all major browsers and is considered "finished".
Pretty useless, then. What features are missing? As far as I know Firefox was one of the major contributors to pushing WebASM to the web and has a superior WASM engine in many aspects.
I'd be more interested in a post about what WebASM features are missing than an announcement that someone hacked Javascript into a browser.
For example, Chrome released File System Access by defaul tin Chrome 86, on October 6, 2020.
And yet, as late as August 2020 they were saying that they'd just got the spec in shape: https://github.com/mozilla/standards-positions/issues/154#is.... And there are still unresolved issues around actual security of the thing (that Chrome happily ignores).
The "spec" itself lists 13 issues: https://wicg.github.io/file-system-access/
And Safari won't implement it: https://lists.webkit.org/pipermail/webkit-dev/2020-August/03...
It's not just about privacy of web APIs...it's about Apple's chokehold on iPhone users.
Funny how you fully ignore Firefox's position
You're right — frankly, I don't look to Mozilla as the leader or voice of web development anymore.
Funny how you went from "Apple's chokehold on iPhone users" to "unable to keep anything close to security parity".
I wonder in which respects Safari "can't keep up"?
> I don't look to Mozilla as the leader or voice of web development anymore.
- There are only two other independent browser engines of any importance, and their input is increasingly ignored by Chrome
- Who cares, go Chrome
This is not as good a take as you think it is.
These are mostly a to-do list...which makes sense at this stage?
I'm not sure what you're getting at.
Do you think that Apple or Mozilla can stop webapps from taking their rightful place?
Which stage is it? Chrome is already shipping this, enabled by default.
And the "todo list" covers important issues around permissions, restricting file system access we etc.
> Do you think that Apple or Mozilla can stop webapps from taking their rightful place?
I think that Chrome plays fast and loose with web standards, and has essentially replaced web standards with Chrome-designed and Chrome-specific APIs, while fully ignoring any concerns from other browsers.
The stage where a majority of professional web devs (y'know, the people who aren't here!) still haven't even heard, or fully assimilated, the term "PWA" yet.
> the "todo list" covers important issues around permissions, restricting file system access we etc.
They've proven very capable of managing this in AOSP.
> I think that Chrome plays fast and loose with web standards, and has essentially replaced web standards with Chrome-designed and Chrome-specific APIs, while fully ignoring any concerns from other browsers.
"Chrome-specific" implies DRM etc which I don't find fair here, as a very regular Google critic.
If the concerns are always "We can't keep up", I don't blame Chromium team for pushing on ahead, and letting other browsers catch up.
Frankly: Mozilla isn't leading the way on web standards anymore. (And they're not doing as good a job with security+privacy, really, either, compared to properly configured Chromium.)
Ah yes. Because that's exactly what you want from a browser: spend time properly configuring it to make it secure.
The security is inherent.
Chrome is more secure.
So, not so simple as a blanket statement you provided.
Which is why I recommend Ungoogled Chromium.
Or just Chromium.
Safari and Mozilla: here are the security concerns, here are privacy concerns, here are concerns that the "specs" reflect internal APIs and are not actual specs.
HN: nah, concerns are always "we can't keep up"
No, those are not "we can't keep up".
Anecdotally, I feel that the quickly growing number of websites that complain that Firefox is an "ad blocker" with an unconfigured, out of the box default install seems to imply Mozilla is doing a great job with respect to security+privacy.
> Mozilla isn't leading the way on web standards anymore.
All the way back to the IE3-6 era, Mozilla has never been about "leading" web standards. IE3-6 lead web standards. Mozilla's role in web standard has almost always largely been about curtailing the excesses of whoever is currently leading, and keeping standards fair, safe, "quality" over quantity.
It's not Mozilla's job to keep up with ~IE6~ Chromium, it's their job to keep Google from spinning web standards out of control. To keep Chromium from being IE6 2.0. To keep Google from just defining all the standards in ways that make Google happy but maybe aren't great long term for the health/safety of the web. It's Mozilla's job to slow Chromium down, and the fact that people are complaining "Firefox isn't keeping up" with Chrome's non-standards and in general that they are losing that battle in mainstream browser usage isn't at all great for the web. (The last time something like this happened was IE6. Whether or not Chromium is the "New IE6" will take a while to play out, but we're deep into the IE6 playbook at this point and anyone not seeing parallels either doesn't remember the early years of IE6 well or has too high of an opinion of Google.)
It works in Firefox today (minor issues keeping it feature flagged for now) and Safari is close to shipping WASM Threads, so this will likely work on all major browsers by EOY.
I'm fascinated in learning more about how you run a web server with this - the article says "WebContainers include a virtualized TCP network stack that's mapped to your browser's ServiceWorker API" but I'd love to understand a bit more about how that works.
Since this is all done with WebAssembly are you planning on targeting other stacks such as Python? The amount of time I lose helping other people getting their Python development environments to work remains horrifying.
We also did an hour long podcast a few months back that goes into pretty deep detail: https://www.youtube.com/watch?v=5F9qH-ea5Qk
Any plan to work towards supporting Firefox & other browsers?
Can you link to the blog post where you actually discuss literally any of the technical stack in any depth further than one sentence of wow more pizzaz much fast.
It sounds like each native binary gets compiled to a WebAssembly binary, but how do they communicate? How are the network and file system implemented? Are parts of the file system persistent? How does a system call work?
We also did an hour long podcast a few months back that goes into pretty deep detail: https://www.youtube.com/watch?v=5F9qH-ea5Qk
I found this on the GH repo: "Is this open source? Today no, but the API will be open source to developers at GA."
I'm confused by what "the API" is, exactly. Is WebContainers a technology you plan to make available for others to use node, in the browser, in their own apps?
If they make it Open Source, Microsoft will add it to VScode and eat their lunch.
If they don't, developers might be afraid of lock in.
Best exit for them is to just grow users and not make a decision either way until they get acquired by Github (aka Microsoft) and folded into VScode.
Put the code under the GPL so no one can use it to build proprietary software. Visual Studio Code contains proprietary components, so this would prevent that.
do you think that a docker.js running containers in the browser is something that could become a reality soon if we follow you line of work ?
It looks like the shell is JSH. Is this an in house shell? Is there any more information on it?
With all the security and design problems of Node.js, when there are actually secure and reliable ways (harder ways, yes. Security and reliability are hard) to do all of this. Why?
Above is a quote from the post. I feel like this is a stupid question, but how can running yarn/npm in a browser on my machine be faster than running yarn/npm on my machine? Particularly when each page load runs a fresh npm/yarn install?
Nonetheless, this is a incredible piece of software that i'll be following closely
If that is the reason, then if your machine becomes memory constrained, the performance will drop through the floor quicker than with full-local installs.
It could be a mix of this and your suggestion (lower network latency related bottlenecks than experienced with non-local deployment).
I dont see any way to set breakpoints. When i use a debugger statement and open inspector it breaks on transpiled/packaged code (its different to the actual file). This is a deal breaker for me at present. Am I doing something wrong?
(Using the http server template)
We have a CLI tool built with node.js, could we build one of these containers with with the CLI tool setup and expose just a terminal with access to the local file system?
There were a few native file system issues I encountered that this would be perfect for!
sudo apt install apache2 php libapache2-mod-php; vim /var/www/html/index.php
Most things can be simplified to the raw essentials, doesn't mean you're going to achieve a result that people want or need.
Worked like a charm.
for example, people sometimes did not know how to freeze their dependencies and use python virtual environments when working with python projects, that lead to problems in collaboration, handing students "magic" prefabricated environments could lead them to believe that that's all there is to it, and explaining that it is not to the student in an edge case might end up being less productive than understanding this process from the get go
you could say that the _effort_ to set up a development machine should be part of the learning experience, at least that's what I think.
I remember seeing my classmates having their first freelance jobs and editing minified css/javascript directly because they did not know anything about the transpiling that goes on or the toolset surrounding javascript and web development
At LeaningTech we have since several months been able to run nodejs, but also python and ruby using our Wasm based x86 VM (CheerpX), the demo is available here: https://repl.leaningtech.com/?nodejs
With our solution the full Linux x86 binary of nodejs is running including the whole of V8 and its JIT engine. Of course fully client side.
How close is CheerpX to getting (for example) an app that uses a PyTorch model running in the browser? :)
I'm working on a note taking project right now that I think would be great as a web app, but I'd like to store the result in a flat text file and the file system access API looks like it will let me do exactly that.
For now, File System Access API seems to be available only in desktop Chromium browsers: Edge, Chrome, Opera. I believe Firefox has not implemented it yet.
https://caniuse.com/native-filesystem-api
---
Mozilla's current position on the specs:
> The ability to read and write from the filesystem is potentially very dangerous. We will need to carefully consider any solution in light of the security and privacy implications. We recognize that the spec authors take this issue seriously, but we are concerned that any solution will increase the risk of security incidents more than we are willing to tolerate.
> Right now, there isn't enough detail in the specification to make an assessment of these risks, so we will defer our decision until we have more information.
https://mozilla.github.io/standards-positions/#native-file-s...
Since Electron is just chromium, I'm not sure why you would expect a difference in the first place? The differences that previously existed between Electron & Web hasn't changed - Electron's only real purpose was to basically turn off the browser permission model & let the "web app" do whatever it wants. That's still true with WebContainers, it still has its hands & features tied to whatever Chrome & browsers in general are comfortable letting websites ask permission to do.
EDIT: Upon closer inspection, it looks like this is an online IDE meant to mimic a desktop environment. I don't know if there's any demand for that, but sure, why not. I'm sure someone will come up w/ a reason to run WebAssembly in a jQuery plugin, too.
Or a desktop-only Electron App would be usable from a mobile phone browser etc.
How? Isn't this using npm?
We haven't released any more info on Turbo v2 yet but will soon.
Rather, I assume they compiled a library or two, but otherwise they use the JS VM in the browser, and they've ported the Node.js runtime scripts to that environment.
That doesn't take anything away from the achievement here - it's really impressive! It's better to use the VM in the browser, if you can get those runtime scripts portable enough, which from other comments it seems like the answer is (or will be) yes.
Dude you live on such a black and white world
Not automatically, but it can mean that, and in my opinion it is true in this particular case. Safety is not defined as the complete absence of risk.
It completely depends on the specifics of the app. Some apps benefit from tight device integration. For others it's a waste of resources that could be spent on features.
Second: looks like js is catching up with smalltalk. JJust a shame the debugger is still rather crappy, and the browser/inspector only applies to a subset of widgets/controls (the html/css stuff, not the tab bar, address line, bookmarks menu etc..).
If that works that means many intranet applications can be server-less. Both the JavaScript app and the "middle-tier" runs in the browser, and the middle-tier talks to MySQL server which is also on the intranet. All you need is a file server to serve the static JS files.
Wouldn't it make more sense for the app to send SQL prepared statements to the database (via a tiny standardised wrapper on the server to enforce per-user permissions based on the authenticated session)?
It probably depends on whether other apps are requesting data from the same database, in which case it might make sense to convert from the relational model to JSON objects in a tier which exposes the objects over REST.
That defeats the point of not having a server.
The middle-tier should be written in Nodejs because it has drivers for relational databases such as MySQL.
Man, the yet another layer they've made between code and the CPU makes me uncomfortable. Imagine finding a bug and going through 20 layers to troubleshoot it.
Also: https://www.theregister.com/2017/03/23/cursor_devours_cpu_cy...
We also did an hour long podcast a few months back that goes into pretty deep detail: https://www.youtube.com/watch?v=5F9qH-ea5Qk
emscripten[1] did this a long time ago for C, which is how a bunch of native applications have already been ported to run on the browser.
webcontainers[2] seem to do a similar thing but focused on exposing the browser API to the native apps in a way that integrates well with the JS environment.
I don't think this is universally true.
No bundlers and a full IDE? Maybe this could even mean that there's a chance for a some of the 'just fiddle with the page' experience to come back to the web! (/rainbows and unicorns)
I mean, a console is nice and all. But I'm highly sceptic as to how the filesystem and networking works, because fetch cannot be used to have dgram or tls/net sockets. And that is literally the primary reason nodejs exists.
> WebContainers include a virtualized TCP network stack that's mapped to your browser's ServiceWorker API, enabling you to instantly create live Node.js servers on-demand that continue to work even when you go offline.
This sounds very likely like http module injection, so that the nodejs module isn't really doing what it thinks it does, but is using an injected API behind the scenes.
Raw TCP is only available for Chrome Extensions, on Chrome OS, iirc.
(Setting aside the lack of TLS or a real crypto module that isn't using the Browser's Web Crypto API approach)
Is wasm in a browser somehow considered native now?
Reloading now spends 10+s "installing dependencies" - pretty quick, and the UI is still interactive.
Also, Chrome has two "Google Chrome helper" processes each sitting at 550MB+ RSS. I'm not sure if "no node_modules black hole on disk" is quite as compelling if the black hole has merely been relocated to RAM.
I noticed that outgoing http requests don't seem to work (there's no `curl` and after installing a CLI tool I work on via npm (which worked like a charm!), the outgoing API calls it makes were blocked. I assume that's intentional at this stage. Is that changing in the future?
https://stackoverflow.com/questions/14928222/tcp-socket-to-w...
Full WASI support in WebContainer is landing in the next 1-2 months.
So a full JS engine is loaded, completely separate from the built-in one.
That's why electron exists. But this can't do that, it's still limited by the APIs that are present in the browser. It's seemingly just full-overhead for... some... reason?
My assumption was that the node APIs are simply being exposed to v8.
Supporting chrome/chromium doesn't seem like a bad place to start with a prototype with that kind of potential adoption.
Maybe one day, smartphones and tablets are just browsers in hand.
It looks just like a canvas, there's "no" DOM.
At that point you could replace the browser with ZINE (Zine is not Electron) and run webapps natively just how WINE works.
Anyone having same issue?
More like if I called the OS "Windows.jpg".
If you're going to use something that looks like a filename as a product name, then make sure that file actually exists and is in the expected format.
D3.js --> good
Angular.js --> good
Node.js --> bad
python3.py --> bad
Windows.jpg --> bad
Windows.exe --> okay (I don't use windows but I assume there's something of the sort)
Building on the really good work of the WASM folks, running Node.js, with all its design problems, over the top of it.
I am forever astounded anew by the hubris and naivety of the Node.js crew. It is a example of "It is easier to write than read, easier to talk than listen, easier to build than design"
It isn't open source. It has a name, WebContainers, that implies it's based on the web, but it's designed around Node.js which isn't built on browser technologies as much as something like Skypack or Deno.
It also seems like it's going to be a memory, disk, and CPU hog, and that it's going to be pretty complex. I like where Skypack is headed and this seems like the opposite direction.
It's pretty cool but it seems like they're trying to create a lot of hype around it.
I did a quick google search because I had never heard of Skypack.
Skypack seems to be a far cry away from what WebContainers is claiming to do. Like, they're completely different products. I guess I don't understand why you're even comparing the two.