What are extensions?
Extensions are collections of JavaScript modules that:- Extend the runtime: Add new APIs available to all workers
- Internal or public: Can be internal (only for other extension modules) or public (importable by user code)
- Enable advanced patterns: Support wrapped bindings, custom event types, and middleware
- Late-linked: Prepared separately and linked at configuration time
Defining extensions
Extensions are defined in theextensions list of your top-level configuration:
Extension structure
An extension consists of a list of modules:Extension module fields
Full JavaScript module name. Convention is to use a namespace prefix like
"extension-name:module-name".Raw source code of the ES module. Use
embed to include external files.Whether this module is internal. Internal modules:
- Can be imported by other extension modules only
- Cannot be imported by user code
- Useful for implementation details and helpers
Using extensions in workers
Public modules
Public extension modules (withinternal = false) can be imported directly in worker code:
Wrapped bindings
Extensions are commonly used to implement wrapped bindings. The extension module exports a function that accepts inner bindings and returns an API object:Complete example
Here’s a complete example of an extension that provides a custom logging API:Use cases for extensions
Custom API wrappers
Wrap complex APIs or external services with a simpler interface:Middleware patterns
Implement reusable middleware through extensions:Configuration management
Centralize configuration logic in extensions:Observability
Add tracing, metrics, and monitoring through extensions:Best practices
Naming convention: Use a consistent namespace prefix for your extension modules (e.g.,
"my-extension:module-name") to avoid conflicts with other extensions.Internal vs public: Mark implementation details as
internal = true and only expose the public API. This gives you flexibility to change internals without breaking user code.Error handling: Extension modules should have robust error handling since they’re shared across all workers.
Documentation: Document your extension APIs clearly since they become part of the runtime API surface for your workers.
Limitations
Extensions have some limitations to be aware of:- Extension modules cannot perform async I/O during module evaluation
- Extensions are loaded once per isolate, not per worker
- Extension state is shared across all workers using the extension
- Internal modules cannot be imported by user code, only by other extension modules
Next steps
Bindings
Learn about wrapped bindings
Workers
Configure workers that use extensions