require

package
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 14, 2023 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidModule     = errors.New("invalid module")
	ErrInvalidModuleName = errors.New("invalid module name")
	ErrModuleNotExist    = errors.New("module does not exist")
)

Functions

func DefaultSourceLoader

func DefaultSourceLoader(filename string) ([]byte, error)

DefaultSourceLoader is used if none was set (see WithLoader()). It simply loads files from the host's filesystem.

func Require

func Require(runtime *goja.Runtime, name string) (goja.Value, error)

Types

type ModuleResolver

type ModuleResolver struct {
	// contains filtered or unexported fields
}

func (*ModuleResolver) Require

func (r *ModuleResolver) Require(p string) (ret goja.Value, err error)

Require can be used to import modules from Go source (similar to goja require() function).

type NativeModule

type NativeModule interface {
	// Enable registers global objects and methods of the module to the JavaScript runtime.
	// This allows using the module's features globally in the runtime environment.
	Enable(runtime *goja.Runtime)

	// Export registers native objects and methods to the module.exports object.
	// This allows importing the module's features into other modules or scripts as needed.
	Export(runtime *goja.Runtime, module *goja.Object)
}

NativeModule is an interface that represents a module with native methods and objects. It provides the methods to enable global access and to export its functionality.

type Option

type Option func(*Registry)

func WithGlobalFolders

func WithGlobalFolders(globalFolders ...string) Option

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()

type Registry

type Registry struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Registry contains a cache of compiled modules which can be used by multiple Runtimes

func NewRegistry

func NewRegistry(opts ...Option) *Registry

func NewRegistryWithLoader

func NewRegistryWithLoader(srcLoader SourceLoader) *Registry

func (*Registry) Enable

func (r *Registry) Enable(runtime *goja.Runtime) *ModuleResolver

Enable adds the require() function to the specified runtime.

func (*Registry) RegisterNativeModule

func (r *Registry) RegisterNativeModule(name string, module NativeModule)

type SourceLoader

type SourceLoader func(path string) ([]byte, error)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL