feat(jsplugin): run task plugins on moejs

Task plugins now run on github.com/Calcium-Ion/moejs v0.1.0-alpha.3
instead of Sobek, and the host utils are ported to it. The plugin API is
unchanged. Code compiled from strings is no longer allowed: eval and the
Function constructors throw an EvalError.

Hooks receive host values without defensive copies. The engine converts
an object or array when the hook first reads it and never writes
JavaScript changes back to the host value. Stored protocol state and task
views are passed as raw JSON and parsed inside the runtime instead of
being decoded into Go maps first. An idle runtime drops its references to
the last call's arguments.

Whether an optional hook is exported is decided once when the plugin
loads, so checking for it no longer takes an execution slot.

Usage validation returns the normalized facts in a new map instead of
writing into the hook result, so a hook that returns the request body no
longer changes it. The request descriptor is copied instead of being
round-tripped through JSON.
This commit is contained in:
CaIon committed 2026-09-30 17:24:19 +08:00
1 parent 335ebcff71
commit 2ffc595901
24 files changed
+1206 -611

No files matched your search

+5 -3
View File
@@ -126,9 +126,11 @@ the plugin's declared native routes. The generic management surface remains
## Security boundary
Plugins have no `fetch`, filesystem, `require`, imports, async functions, or
environment access. The host limits execution time, concurrency, input size,
allowed request hosts, and resolves OAuth credentials outside JavaScript.
Plugins have no `fetch`, filesystem, `require`, imports, async functions, code
compiled from strings (`eval` and the `Function` constructors throw an
`EvalError`), or environment access. The host limits execution time,
concurrency, input size, allowed request hosts, and resolves OAuth credentials
outside JavaScript.
Multipart files enter JavaScript only as opaque references.
This is not a hard memory-isolation boundary. A plugin sees data needed for the
+51 -6
View File
@@ -243,6 +243,42 @@ Host functions are synchronous and must bound their own work: the JavaScript
execution interrupt cannot preempt a running Go function. Request-scoped data
must remain owned by the host request, not by pooled runtime globals.
## Hook arguments and results
The host passes plain JSON values to hooks without copying them: objects,
arrays, strings, finite numbers, booleans and null. The engine converts each
object or array when the hook first reads it and never writes JavaScript
changes back into the host value. Other values (structs, byte slices,
`json.Number`, raw JSON, invalid UTF-8, non-finite numbers, nil slices, or
nesting deeper than 64 levels) are first normalized through the host JSON codec.
Plugin rules:
- Treat every argument as read-only input. Assigning to an argument changes
only the current call's view; the host, other hooks and later calls never see
it. Return the value the host should use, and use `utils.json.clone`
(`json-clone@1`) when a mutable copy is needed.
- Do not keep arguments, or objects and arrays read from them, in module
variables after the hook returns. Runtimes are pooled across requests, so a
kept value can surface in a later request, keeps the original request's data
alive, and is still read from the host value it came from.
Host rules:
- Once a value has been passed to a hook, host code must not change it. A
plugin may still hold it, and reading a Go map while another goroutine writes
it is a fatal runtime error that `recover` cannot catch and that stops the
whole process. Build a new value, or copy it first (`jsonValue` in the task
adaptor copies plain JSON and normalizes everything else).
- Hook results may be host values. A hook that returns an argument, or part of
one, unchanged hands back the original host value rather than a copy. Copy a
result before editing it in place; the request descriptor's file placeholders
are replaced on a copy for this reason.
- Integral numbers created in JavaScript come back as `int64` and other numbers
as `float64`; host values returned unchanged keep their original Go type.
Code reading numbers from hook results must accept every numeric type rather
than asserting `float64`. The JSON encoding is the same either way.
## Request body
Every decoder receives one host-parsed body:
@@ -404,14 +440,23 @@ when accumulating, rather than mutating host-backed arrays in place.
Idle runtimes survive garbage collection in a pool bounded by the plugin's
configured concurrency. The host creates additional instances on demand and
discards interrupted instances. Cancellation callbacks finish before an instance
is returned to the pool. Hook bindings remain live; the host does not freeze
exported functions or object members. No JavaScript execution slot is held while
is returned to the pool, and the engine then drops its references to the
finished call's arguments, so an idle instance does not keep the last request's
data alive; values a plugin kept in module variables still keep theirs. Hook
bindings remain live; the host does not freeze
exported functions or object members. Whether an optional hook (the usage
hooks, `listArtifacts`, `buildContentRequest`) is exported is decided once,
when the plugin loads: an export that is `undefined` then counts as absent,
and assigning it later does not make the host call it. No JavaScript execution slot is held while
waiting for the next upstream event.
The host may copy plain JSON containers without re-encoding their string values;
hook inputs and successive states remain separate snapshots. State size is still
checked against its actual encoded JSON bytes after every event, including
intermediate state that a later event might replace with a smaller value.
The host passes the returned state to the next event as is (see
[Hook arguments and results](#hook-arguments-and-results)); changes a hook makes
to `previousState` without returning them are not kept. State that is not plain
JSON, such as a typed array, is first normalized through its JSON encoding.
State size is still checked against its actual encoded JSON bytes after every
event, including intermediate state that a later event might replace with a
smaller value.
A complete, blank-line-delimited event whose hook returns `done: true` terminates
the reader and closes the upstream response. The last state is passed as