Files
magpie/internal/jsmod/jsmod.go
T
yetone 60520ddb3b moejs: v0.1.0-alpha.11, at its new module path github.com/MoeForge/moejs
Gateway middleware and agent plugins run on moejs. alpha.11 moves the
module to github.com/MoeForge/moejs; every other API change since
alpha.4 is an addition. What magpie gets: faster calls, loops, property
lookups and JSON.parse, allocation-free await, a bytecode verifier, a
correct default.pgo, and Intl / toLocaleString in plugin code.

Checked against both tags' source: every name magpie uses (NewRuntime,
Options, Compile, Link, Hook, Value, Arg, Undefined, NativeFunc,
Referrer, the Promise results, Exception) has the same signature, and
Options' defaults (shared frozen builtins, no timers) are unchanged.
fetch, Streams, URL, node:fs and node:buffer live in the separate
webapi and node packages, installed only on request; magpie installs
neither, so a plugin still reaches no network or file.

Intl's CLDR, Unicode, ICU and V8 material is compiled into every build,
and moejs asks a program that includes it to carry sections 1 to 4 of
its notices. THIRD_PARTY_NOTICES holds them verbatim. Release archives
ship no LICENSE today either; that is not changed here.

Doc and site links (docs/, site/public/docs/{,zh/,ja/}plugins.html)
point at the new repo.

Run on mote at this tree: gofmt; go build -trimpath -s -w (60,670,658
bytes on origin/main 58fb3a778, 62,329,250 now, +1.66MB); go vet of
middleware, jsmod, agentplug for darwin, linux and windows; go test
./internal/middleware/... ./internal/jsmod/... ./internal/agentplug/...
ok; go test -run 'Middleware|Strateg|OnUsage|JSPlugin|Agentplug|AgentPlug'
./internal/gateway ./internal/provider ./internal/plugin ./internal/gui
ok.
Not run: the full suite, the GUI suite, a real request through a
middleware in a running gateway.
2026-10-28 14:36:17 +08:00

57 lines
1.6 KiB
Go

// Package jsmod compiles a plugin's JavaScript module for moejs, the
// JavaScript engine written in Go that magpie runs plugin code in its own
// process with: gateway middleware (internal/middleware) and agents
// (internal/agentplug).
package jsmod
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/MoeForge/moejs"
)
// Compile compiles the module in file and the modules it imports, which
// are files under root: there is no node_modules here, so a package is
// bundled into the module. what names the kind of module in an error
// ("a middleware").
func Compile(root, file, what string) (*moejs.Module, error) {
src, err := os.ReadFile(file)
if err != nil {
return nil, err
}
entry, err := moejs.Compile(file, string(src))
if err != nil {
return nil, err
}
mods := map[string]*moejs.Module{file: entry}
return moejs.Link(entry, func(ref moejs.Referrer, spec string) (*moejs.Module, error) {
if !strings.HasPrefix(spec, "./") && !strings.HasPrefix(spec, "../") {
return nil, fmt.Errorf("%q: %s imports only files beside it; bundle packages into it", spec, what)
}
from := file
if ref != nil {
from = ref.Name()
}
p := filepath.Clean(filepath.Join(filepath.Dir(from), filepath.FromSlash(spec)))
if rel, err := filepath.Rel(root, p); err != nil || strings.HasPrefix(rel, "..") {
return nil, fmt.Errorf("%q is outside the plugin", spec)
}
if m, ok := mods[p]; ok {
return m, nil
}
src, err := os.ReadFile(p)
if err != nil {
return nil, err
}
m, err := moejs.Compile(p, string(src))
if err != nil {
return nil, err
}
mods[p] = m
return m, nil
})
}