Node.js platform adapter for Shovel with hot reloading and ESBuild integration
npm install @b9g/platform-nodeNode.js platform adapter for Shovel. Runs ServiceWorker applications on Node.js with HTTP server integration, hot reloading, and worker thread concurrency.
- Node.js HTTP server with Web API Request/Response conversion
- Hot module reloading for development via VM module system
- Worker thread pool for concurrent request handling
- Memory and filesystem cache backends
- File System Access API implementation via NodeFSDirectory
- ServiceWorker lifecycle support (install, activate, fetch events)
``bash`
npm install @b9g/platform-node @b9g/platform @b9g/cache @b9g/filesystem
`javascript
import NodePlatform from '@b9g/platform-node';
const platform = new NodePlatform({
caches: { type: 'memory' },
filesystem: { type: 'local', directory: './dist' }
});
const server = platform.createServer(async (request) => {
return new Response('Hello from Node.js');
}, { port: 7777, host: 'localhost' });
await server.listen();
console.log('Server running at http://localhost:7777');
`
`javascript
import NodePlatform from '@b9g/platform-node';
const platform = new NodePlatform({
cwd: process.cwd()
});
// Load ServiceWorker entrypoint
const instance = await platform.loadServiceWorker('./src/server.js', {
workerCount: 4 // Number of worker threads
});
// ServiceWorker is now handling requests
`
- NodePlatform - Node.js platform implementation (extends BasePlatform)
- Platform, CacheConfig, StaticConfig, Handler, Server, ServerOptions
- NodePlatform - The platform class
Creates a new Node.js platform instance.
Options:
- caches: Cache configuration object (see @b9g/platform)filesystem
- : Filesystem configuration objectport
- : Default port for servers (default: 7777)host
- : Default host for servers (default: localhost)cwd
- : Working directory for file resolution (default: process.cwd())
Creates an HTTP server with automatic Request/Response conversion.
Parameters:
- handler: (request: Request) => Promise - Request handler functionoptions
- : Server options (port, host)
Returns: Server instance with:
- listen(): Start the serverclose()
- : Stop the serverurl
- : Server URL (after listen)ready
- : Promise that resolves when server is ready
Loads and runs a ServiceWorker entrypoint.
Parameters:
- entrypoint: Path to ServiceWorker entry fileoptions
- : ServiceWorker options (workerCount, caches, etc.)
Returns: ServiceWorkerInstance with:
- handleRequest(request): Handle a request through the ServiceWorkerready
- : Promise that resolves when ServiceWorker is ready
Configured via caches option:
- memory: In-memory caching (MemoryCache)filesystem`: File-based caching (NodeFSDirectory)
-
The Node.js platform uses worker threads for true concurrency:
- Each worker runs the ServiceWorker code in isolation
- Round-robin load balancing across workers
- Shared cache storage coordinated via PostMessage
- Automatic request timeout (30s default)
MIT