modules

package
v0.0.0-...-659b40f Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2025 License: MIT Imports: 16 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidModule module is invalid
	ErrInvalidModule = errors.New("invalid module")
	// ErrIllegalModuleName module name is illegal
	ErrIllegalModuleName = errors.New("illegal module name")
	// ErrNotFoundModule not found module
	ErrNotFoundModule = errors.New("cannot found module")
)

Functions

func All

func All() map[string]Module

All get all module

func Register

func Register(name string, mod Module)

Register registers a Module with the given name and implementation. If the module is not a Global module, the name will be prefixed with "ski/". The registered modules can later be imported in JavaScript code by name.

Example:

// Register a regular module that must be imported as "ski/mymodule"
modules.Register("mymodule", new(MyModule))

// Register a global module that can be lazily instantiated
modules.Register("sleep", modules.Global{
	"sleep": modules.ModuleFunc(func(call sobek.FunctionCall, rt *sobek.Runtime) sobek.Value {
		time.Sleep(time.Duration(call.Argument(0).ToInteger()))
		return sobek.Undefined()
	}),
})

func Remove

func Remove(names ...string)

Remove the modules

Types

type FileLoader

type FileLoader func(specifier *url.URL, name string) ([]byte, error)

FileLoader is a type alias for a function that returns the contents of the referenced file.

func DefaultFileLoader

func DefaultFileLoader(fetch func(*http.Request) (*http.Response, error)) FileLoader

DefaultFileLoader the default file loader. Supports file and HTTP scheme loading.

type Global

type Global map[string]Module

Global represents a collection of related modules grouped under a namespace. It maps module names to their Module implementations. Global modules are instantiated lazily when first accessed through the global object.

func (Global) Instantiate

func (g Global) Instantiate(rt *sobek.Runtime) (sobek.Value, error)

type Loader

type Loader interface {
	// CompileModule compile module from source string (cjs/esm).
	CompileModule(name, source string) (sobek.CyclicModuleRecord, error)
	// ResolveModule resolve the module returns the sobek.ModuleRecord.
	ResolveModule(any, string) (sobek.ModuleRecord, error)
	// EnableRequire enable the global function require to the sobek.Runtime.
	EnableRequire(*sobek.Runtime) Loader
	// EnableImportModuleDynamically sobek runtime SetImportModuleDynamically
	EnableImportModuleDynamically(*sobek.Runtime) Loader
	// EnableImportMeta sobek runtime SetFinalImportMeta
	EnableImportMeta(*sobek.Runtime) Loader
	// InitGlobal instantiates global objects for the runtime. It creates a proxy around the global object
	// to lazily load global modules when they are first accessed.
	//
	// This allows for automatic loading of global modules like fetch, TextEncoder, etc.
	// when they are referenced in code.
	InitGlobal(*sobek.Runtime) Loader
	// SetFileLoader set the FileLoader.
	SetFileLoader(fl FileLoader)
}

Loader the js module loader.

func NewLoader

func NewLoader(opts ...Option) Loader

NewLoader returns a new module resolver if the fileLoader option not provided, uses the default DefaultFileLoader.

type Module

type Module interface {
	Instantiate(*sobek.Runtime) (sobek.Value, error)
}

Module is the interface that must be implemented by JavaScript modules. It defines how a module is instantiated and made available to the JavaScript runtime.

Example implementation:

func init() {
	// register a new module named "process"
	modules.Register("process", new(Process))
}

type Process struct{}

func (Process) Instantiate(rt *sobek.Runtime) (sobek.Value, error) {
	environ := os.Environ()
	env := make(map[string]string, len(environ))
	for _, kv := range environ {
		k, v, _ := strings.Cut(kv, "=")
		env[k] = v
	}
	ret := rt.NewObject()
	_ = ret.Set("env", env)
	return ret, nil
}

func Get

func Get(name string) (Module, bool)

Get the module

type ModuleFunc

type ModuleFunc func(sobek.FunctionCall, *sobek.Runtime) sobek.Value

ModuleFunc type is an adapter to allow the use of ordinary functions as Module.

func (ModuleFunc) Instantiate

func (m ModuleFunc) Instantiate(rt *sobek.Runtime) (sobek.Value, error)

type Option

type Option func(*loader)

Option the new Loader options.

func WithBase

func WithBase(base *url.URL) Option

WithBase the base directory of module loader.

func WithFileLoader

func WithFileLoader(fl FileLoader) Option

WithFileLoader the file loader of module loader.

func WithSourceMapLoader

func WithSourceMapLoader(fn func(path string) ([]byte, error)) Option

WithSourceMapLoader the source map loader of module loader.

Directories

Path Synopsis
Package cache the cache JS implementation
Package cache the cache JS implementation
Package crypto the crypto JS implementation
Package crypto the crypto JS implementation

Jump to

Keyboard shortcuts

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