Documentation
¶
Index ¶
- Constants
- Variables
- func DefaultPathResolver(base, path string) string
- func DefaultSourceLoader(filename string) ([]byte, error)
- func RegisterCoreModule(name string, loader ModuleLoader)
- func RegisterNativeModule(name string, loader ModuleLoader)
- func Require(runtime *js.Runtime, name string) js.Value
- type ModuleLoader
- type Option
- type PathResolver
- type Registry
- type RequireModule
- type SourceLoader
Constants ¶
const NodePrefix = "node:"
Variables ¶
Functions ¶
func DefaultPathResolver ¶
DefaultPathResolver is used if none was set (see WithPathResolver). It converts the path using filepath.FromSlash(), then joins it with base and resolves symlinks on the resulting path. Note, it does not make the path absolute, so to match nodejs behaviour, the initial script name should be set to an absolute path. The implementation is somewhat suboptimal because it runs filepath.EvalSymlinks() on the joint path, not using the fact that the base path is already resolved. This is because there is no way to resolve symlinks only in a portion of a path without re-implementing a significant part of filepath.FromSlash().
func DefaultSourceLoader ¶
DefaultSourceLoader is used if none was set (see WithLoader()). It simply loads files from the host's filesystem.
func RegisterCoreModule ¶
func RegisterCoreModule(name string, loader ModuleLoader)
RegisterCoreModule registers a nodejs core module. If the name does not start with "node:", the module will also be loadable as "node:<name>". Hence, for "builtin" modules (such as buffer, console, etc.) the name should not include the "node:" prefix, but for prefix-only core modules (such as "node:test") it should include the prefix.
func RegisterNativeModule ¶
func RegisterNativeModule(name string, loader ModuleLoader)
RegisterNativeModule registers a module that isn't loaded through a SourceLoader, but rather through a provided ModuleLoader. Typically, this will be a module implemented in Go (although theoretically it can be anything, depending on the ModuleLoader implementation). Such modules take precedence over modules loaded through a SourceLoader, i.e. if a module name resolves as native, the native module is loaded, and the SourceLoader is not consulted. The binding is global and affects all instances of Registry. It should be called from a package init() function as it may not be used concurrently with require() calls. For registry-specific bindings see Registry.RegisterNativeModule.
Types ¶
type Option ¶
type Option func(*Registry)
func WithGlobalFolders ¶
WithGlobalFolders appends the given paths to the registry's list of global folders to search if the requested module is not found elsewhere. By default, a registry's global folders list is empty. In the reference Node.js implementation, the default global folders list is $NODE_PATH, $HOME/.node_modules, $HOME/.node_libraries and $PREFIX/lib/node, see https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders.
func WithLoader ¶
func WithLoader(srcLoader SourceLoader) Option
WithLoader sets a function which will be called by the require() function in order to get a source code for a module at the given path. The same function will be used to get external source maps. Note, this only affects the modules loaded by the require() function. If you need to use it as a source map loader for code parsed in a different way (such as runtime.RunString() or eval()), use (*Runtime).SetParserOptions()
func WithPathResolver ¶
func WithPathResolver(pathResolver PathResolver) Option
WithPathResolver sets a function which will be used to resolve paths (see PathResolver). If not specified, the DefaultPathResolver is used.
type PathResolver ¶
PathResolver is a function that should return a canonical path of the path parameter relative to the base. The base is expected to be already canonical as it would be a result of a previous call to the PathResolver for all cases except for the initial evaluation, but it's a responsibility of the caller to ensure that the name of the script is a canonical path. To match Node JS behaviour, it should resolve symlinks. The path parameter is the argument of the require() call. The returned value will be supplied to the SourceLoader.
type Registry ¶
Registry contains a cache of compiled modules which can be used by multiple Runtimes
func NewRegistry ¶
func NewRegistryWithLoader ¶
func NewRegistryWithLoader(srcLoader SourceLoader) *Registry
func (*Registry) Enable ¶
func (r *Registry) Enable(runtime *js.Runtime) *RequireModule
Enable adds the require() function to the specified runtime.
func (*Registry) RegisterNativeModule ¶
func (r *Registry) RegisterNativeModule(name string, loader ModuleLoader)
type RequireModule ¶
type RequireModule struct {
// contains filtered or unexported fields
}
type SourceLoader ¶
SourceLoader represents a function that returns a file data at a given path. The function should return ModuleFileDoesNotExistError if the file either doesn't exist or is a directory. This error will be ignored by the resolver and the search will continue. Any other errors will be propagated.