script

package
v0.2.5 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2023 License: GPL-3.0 Imports: 20 Imported by: 0

README

Script

TODO

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrFileNameInvalid = errors.New("The plugin's filename is invalid")
	ErrPluginLoaded    = errors.New("The plugin is already loaded")
	ErrScriptInvalid   = errors.New("Invalid script")
)

Functions

func ProxyStorage

func ProxyStorage(s Storage, vm *goja.Runtime) goja.Proxy

func WrapPacketBuilder

func WrapPacketBuilder(p *liter.PacketBuilder, conn *liter.Conn, vm *goja.Runtime) (o *goja.Object)

Types

type Event

type Event struct {
	Name       string
	Data       map[string]any
	Cancelable bool
	// contains filtered or unexported fields
}

func NewEvent

func NewEvent(name string, data map[string]any) *Event

NewEvent will create a cancelable event

func (*Event) Cancel

func (e *Event) Cancel()

func (*Event) Cancelled

func (e *Event) Cancelled() bool

type EventEmitter

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

func NewEventEmitter

func NewEventEmitter(vm *goja.Runtime, loop *eventloop.EventLoop) (e *EventEmitter)

func (*EventEmitter) Emit

func (e *EventEmitter) Emit(event *Event) (cancelled bool)

func (*EventEmitter) EmitAsync

func (e *EventEmitter) EmitAsync(event *Event) (done <-chan bool)

func (*EventEmitter) EventNames

func (e *EventEmitter) EventNames() (names []string)

func (*EventEmitter) ExportTo

func (e *EventEmitter) ExportTo(o *goja.Object)

func (*EventEmitter) Listeners

func (e *EventEmitter) Listeners(name string) (handlers []goja.Callable)

func (*EventEmitter) Off

func (e *EventEmitter) Off(name string, listener goja.Callable)

func (*EventEmitter) OffAsync

func (e *EventEmitter) OffAsync(name string, listener goja.Callable)

func (*EventEmitter) On

func (e *EventEmitter) On(name string, listener goja.Callable) *EventEmitter

func (*EventEmitter) OnAsync

func (e *EventEmitter) OnAsync(name string, listener goja.Callable)

func (*EventEmitter) RemoveAllListeners

func (e *EventEmitter) RemoveAllListeners(name string)

type Exportsable

type Exportsable interface {
	Exports() *goja.Object
}

type JsonStorage

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

TODO JsonStorage is a local storage, it's thready-safe

type Manager

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

func NewManager

func NewManager() (m *Manager)

func (*Manager) Emit

func (m *Manager) Emit(event *Event) (done <-chan bool)

func (*Manager) List

func (m *Manager) List() (scripts []*Script)

List will return all active plugins

func (*Manager) Load

func (m *Manager) Load(path string) (script *Script, err error)

Load will call LoadWithContext with context.Background()

func (*Manager) LoadFromDir

func (m *Manager) LoadFromDir(path string) (scripts []*Script, err error)

LoadFromDir will load all plugins under the path which have the ext `.js`, and return all successfully loaded plugins with a possible error If the target path is not exists, LoadFromDir will do nothing and return no error If there are errors during load any plugin, the errors will be wrapped use `errors.Join`, and other plugins will continue to be load.

func (*Manager) LoadWithContext

func (m *Manager) LoadWithContext(ctx context.Context, path string) (script *Script, err error)

LoadWithContext will load a script plugin use the given filepath. Script's filename must match `^([a-z_][0-9a-z_]{0,31})(?:@(\d+(?:\.\d+)*)(?:-.+)?)?\..+$` The first capture group will be the script's ID. The second capture group is the script's version If the script's ID is already loaded, then an error will be returned

func (*Manager) SetLogger

func (m *Manager) SetLogger(loger logger.Logger)

func (*Manager) Unload

func (m *Manager) Unload(id string) (script *Script)

Unload will unload plugin by ID and return the unloaded plugin instance During unloading, the unload event will be emitted

func (*Manager) UnloadAll

func (m *Manager) UnloadAll() (scripts []*Script)

UnloadAll will unload all plugins and return them During unloading, the unload event will be emitted

func (*Manager) WrapConn

func (m *Manager) WrapConn(conn *liter.Conn) (wrapped *WrappedConn)

type MemoryStorage

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

MemoryStorage is designed for single thread use, so it's not thread-safe. It should only be called inside the js loop with same Runtime instance.

func NewMemoryStorage

func NewMemoryStorage(vm *goja.Runtime) (s *MemoryStorage)

func (*MemoryStorage) Clear

func (s *MemoryStorage) Clear()

func (*MemoryStorage) Exports

func (s *MemoryStorage) Exports() *goja.Object

func (*MemoryStorage) GetItem

func (s *MemoryStorage) GetItem(key string) (value goja.Value)

func (*MemoryStorage) Keys

func (s *MemoryStorage) Keys() (keys []string)

func (*MemoryStorage) Len

func (s *MemoryStorage) Len() int

func (*MemoryStorage) RemoveItem

func (s *MemoryStorage) RemoveItem(key string)

func (*MemoryStorage) SetItem

func (s *MemoryStorage) SetItem(key string, value goja.Value)

type Script

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

func (*Script) Emit

func (s *Script) Emit(event *Event) (done <-chan bool)

func (*Script) Exports

func (s *Script) Exports() *goja.Object

TODO: Exports return a possible exports object from the script it's always nil for now, maybe it will be used later

func (*Script) Id

func (s *Script) Id() string

func (*Script) Logger

func (s *Script) Logger() logger.Logger

func (*Script) Off

func (s *Script) Off(name string, listener goja.Callable)

func (*Script) On

func (s *Script) On(name string, listener goja.Callable)

type Storage

type Storage interface {
	Len() int
	Keys() (keys []string)
	GetItem(key string) (value goja.Value)
	SetItem(key string, value goja.Value)
	RemoveItem(key string)
	Clear()
}

type WrappedConn

type WrappedConn struct {
	*liter.Conn
	// contains filtered or unexported fields
}

func WrapConn

func WrapConn(conn *liter.Conn, vm *goja.Runtime, loop *eventloop.EventLoop) (c *WrappedConn)

func (*WrappedConn) Closed

func (c *WrappedConn) Closed() bool

Closed return wheather the connection is closed by the script

func (*WrappedConn) Emit

func (c *WrappedConn) Emit(event *Event) (done <-chan bool)

func (*WrappedConn) Exports

func (c *WrappedConn) Exports() *goja.Object

func (*WrappedConn) Recv

func (c *WrappedConn) Recv() (w *WrappedPacketReader, err error)

type WrappedPacketReader

type WrappedPacketReader struct {
	*liter.PacketReader
	// contains filtered or unexported fields
}

func WrapPacketReader

func WrapPacketReader(p *liter.PacketReader, vm *goja.Runtime) *WrappedPacketReader

func (*WrappedPacketReader) Exports

func (p *WrappedPacketReader) Exports() *goja.Object

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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