jao.life

kernel — Node's architecture, in vanilla JavaScript

path, events, buffer, streams, fs, a two-phase CommonJS loader, and an HTTP server with no sockets. Every file below is the same file in both worlds: a Node process and this tab. The only per-host code is a binding that moves bytes.

01 Run it here

These files are written to /app on a real filesystem (navigator.storage, not localStorage), then mounted and executed by the kernel in this tab. require('./lib/greet') resolves the way it does in Node.

host … entry /app/main.js
Press Run. Output appears here.

This editor executes code in your own tab. It is a demonstration, not a security boundary — do not paste programs you would not run yourself.

02 The layers

Node is a JavaScript API layer sitting on a native one. This keeps that split and replaces the native half with a binding object: read, write, list, stat, mkdir, remove, rename. Seven methods, and the rest is portable.

filewhat it isNode's equivalent
lib/path.jsposix resolution bound to the kernel's cwdpath
lib/events.jsEventEmitter, including the unhandled-error throwevents
lib/buffer.jsUint8Array with utf8, hex, base64 and latin1buffer
lib/timers.jsnextTick and the timer entry pointsprocess.nextTick, timers
lib/streams.jsReadable, Writable, Transform, for await, pipestream
lib/fs.jsencodings, errno codes, streams over the bindingfs/promises
lib/process.jsargv, env, cwd, exit codes, stdout and stderrprocess
lib/module.jsCommonJS resolution, cache and evaluationmodule, require
lib/http.jsservers and a client over a port registryhttp, minus the socket
hosts/node.jsthe binding for a real filesystemlibuv + fs bindings
hosts/browser.jsthe binding for OPFS, with an in-memory fallback—

03 Modules: resolve async, evaluate sync

Node resolves and evaluates a module in one blocking call. A browser cannot block, so the loader runs in two phases: mount walks the graph and reads every file asynchronously, then evaluate runs the factories in dependency order with a synchronous require that only ever reads what mount already found.

App code keeps Node's shape — require('./x') returns exports immediately — because by the time it runs, everything it can reach is already in memory. Cycles see partial exports instead of looping, because a module is marked loaded before its body runs.

// what a module body actually runs inside
new Function('exports', 'require', 'module', '__filename', '__dirname',
             'kernel', 'console', source)

// bindings probed in order, like Node
./lib/greet  →  ./lib/greet.js  →  ./lib/greet/index.js
// bare specifiers walk up for node_modules, honouring package.json main
'tiny'  →  /app/node_modules/tiny  →  main: "lib/entry.js"

04 What is deliberately missing

no sync fs

readFileSync does not exist

There is no blocking file syscall in a tab and no second thread to fake one on, so the sync API is absent rather than emulated badly.

no sockets

listen() takes a port, not a socket

The port is an entry in a registry and http.fetch dispatches into the handler. Same request and response objects; the transport is what is missing.

no native addons

and no child_process

Both need to start a process or load machine code. Neither has any meaning inside a page, so neither is pretended.

ordering

nextTick is a microtask

libuv drains nextTick before promise continuations. A page has no such hook, so ticks are queued ahead of the work that scheduled them — not ahead of every promise.

05 Two hosts, one program

Both bindings are in the repository. The page you are reading uses the browser one; the same kernel under the Node binding is what node test/kernel.test.mjs exercises — 44 checks covering path, buffer, events, timer ordering, fs and errno codes, streaming, node_modules resolution, cycles, the port registry and ECONNREFUSED.

hosts/node.js

real filesystem

Kernel paths are rooted inside a directory you choose, so a kernel cannot reach outside it. This is how the tests run.

hosts/browser.js

OPFS, or memory

Origin-private storage with real directories and files that survive a reload. Where OPFS is unavailable the same contract is served from a Map.

The next host is the one this architecture was built for: a service worker answering fetch events, so the same createServer handler serves real URLs from the visitor's own browser.