mirror of
https://github.com/getpaseo/paseo.git
synced 2026-11-02 22:52:58 +00:00
* feat(plugins): stream events and bytes between plugins and the app
Plugins can declare a subscription with defineSubscription, handle it with
server.handle(contract, (input, { push, signal })), and listen from a surface
with useSubscription or client.subscribe. A top-level Uint8Array in an event,
an RPC output, or an RPC input crosses the wire as a binary frame (opcodes
0x20-0x2f) without JSON or base64; a nested one fails validation on the sender.
Each subscriber runs its own handler, owned by the session that subscribed,
so release, disconnect, plugin stop and reload need no plugin code. The
runtime buffers pushes until the subscribe response has gone out, so a client
never sees an event for a subscription it does not know yet.
Gated on server_info.features.pluginSubscriptions; binary outputs reach only
clients that advertise plugin_binary_payloads.
* fix(plugins): close subscription gaps found in review
- Losing daemon.manage now releases the session's plugin subscriptions;
binary events bypass the outbound permission check, so streams would
otherwise outlive the revocation.
- A subscribe that times out sends unsubscribe, since the plugin may
still start the handler after the daemon gave up.
- The byte limit leaves room for the largest frame header and routing,
and lives in assertPluginWireValue so events, outputs and inputs share it.
- A release right after the host accepted keeps `ready` resolved, so a
handler that returns at once reports closed rather than an error.
- Session trace logging tolerates loggers without isLevelEnabled.
* fix(plugins): withhold binary plugin results after plugin access is revoked
A binary RPC output or event skipped the outbound permission check its
JSON form passes, so an RPC in flight when daemon.manage was revoked still
delivered its bytes. Each frame now checks the permission of the JSON
message it stands in for.
* test(plugins): cover subscriptions in a real browser instead of JSDOM
The app subscription tests mounted hooks in JSDOM, which docs/testing.md
rules out. The adapter and contract behavior is now a plain unit test,
and a Playwright spec drives the diff-watch and media-stream examples:
JSON events following file changes, streamed bytes, and an upload.
* test(plugins): add the plain subscription unit test and the browser spec
* fix(plugins): apply subscription schema transforms once
Both ends parsed each subscription value, and the sender sent its parse
output, so the receiver parsed a schema's output as fresh input. An event
schema like z.string().transform((s) => s.length) closed the stream as
invalid. The sender now validates and sends the value as given; the
receiver's parse produces what the handler or callback sees.
* fix(plugins): parse a subscription's input once in the plugin process
server.handle wrapped subscription handlers in a second input parse, so a
transforming input schema rejected its own parsed output before the
handler ran.
96 lines
3.6 KiB
TypeScript
96 lines
3.6 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import {
|
|
assertPluginWireValue,
|
|
decodeBinaryFrame,
|
|
decodePluginFrame,
|
|
encodePluginEventFrame,
|
|
encodePluginInputFrame,
|
|
encodePluginOutputFrame,
|
|
FileTransferOpcode,
|
|
MAX_PLUGIN_PAYLOAD_BYTES,
|
|
MAX_ROUTED_PAYLOAD_BYTES,
|
|
PluginFrameOpcode,
|
|
TerminalStreamOpcode,
|
|
} from "./index.js";
|
|
|
|
describe("plugin binary frames", () => {
|
|
it("uses the reserved plugin opcode range", () => {
|
|
for (const opcode of Object.values(PluginFrameOpcode)) {
|
|
expect(opcode).toBeGreaterThanOrEqual(0x20);
|
|
expect(opcode).toBeLessThanOrEqual(0x2f);
|
|
expect(Object.values(TerminalStreamOpcode)).not.toContain(opcode);
|
|
expect(Object.values(FileTransferOpcode)).not.toContain(opcode);
|
|
}
|
|
});
|
|
|
|
it("frames a subscription event as opcode, id length, id, and the untouched payload", () => {
|
|
const payload = new Uint8Array([9, 8, 7]);
|
|
const frame = encodePluginEventFrame("sub-1", payload);
|
|
expect([...frame]).toEqual([0x20, 5, ...new TextEncoder().encode("sub-1"), 9, 8, 7]);
|
|
expect(decodeBinaryFrame(frame)).toEqual({
|
|
kind: "plugin",
|
|
frame: { opcode: PluginFrameOpcode.Event, subscriptionId: "sub-1", payload },
|
|
});
|
|
});
|
|
|
|
it("decodes payloads as views into the received buffer", () => {
|
|
const frame = encodePluginOutputFrame("req-1", new Uint8Array([1, 2, 3, 4]));
|
|
const decoded = decodePluginFrame(frame);
|
|
if (decoded?.opcode !== PluginFrameOpcode.Output) throw new Error("Expected an output frame");
|
|
expect(decoded.requestId).toBe("req-1");
|
|
expect(decoded.payload.buffer).toBe(frame.buffer);
|
|
expect([...decoded.payload]).toEqual([1, 2, 3, 4]);
|
|
});
|
|
|
|
it("frames an RPC input with the plugin and method in its header", () => {
|
|
const frame = encodePluginInputFrame({
|
|
requestId: "req-2",
|
|
pluginId: "uploads",
|
|
method: "upload.store",
|
|
payload: new Uint8Array([5, 6]),
|
|
});
|
|
expect(decodePluginFrame(frame)).toEqual({
|
|
opcode: PluginFrameOpcode.Input,
|
|
requestId: "req-2",
|
|
pluginId: "uploads",
|
|
method: "upload.store",
|
|
payload: new Uint8Array([5, 6]),
|
|
});
|
|
});
|
|
|
|
it("rejects truncated frames", () => {
|
|
expect(decodePluginFrame(new Uint8Array([0x20]))).toBeNull();
|
|
expect(decodePluginFrame(new Uint8Array([0x20, 9, 1]))).toBeNull();
|
|
expect(decodePluginFrame(new Uint8Array([0x22, 1, 65, 0, 40]))).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("the plugin byte rule", () => {
|
|
it("accepts JSON values and a top-level Uint8Array", () => {
|
|
expect(() => assertPluginWireValue({ a: [1, "b", null] }, "event")).not.toThrow();
|
|
expect(() => assertPluginWireValue(new Uint8Array(4), "event")).not.toThrow();
|
|
});
|
|
|
|
it("rejects bytes that would not fit in one frame with the largest header", () => {
|
|
const bytes = new Uint8Array(MAX_PLUGIN_PAYLOAD_BYTES + 1);
|
|
expect(() => assertPluginWireValue(bytes.subarray(1), "event")).not.toThrow();
|
|
expect(() => assertPluginWireValue(bytes, "event")).toThrow(
|
|
`event is ${MAX_PLUGIN_PAYLOAD_BYTES + 1} bytes; one value carries at most ${MAX_PLUGIN_PAYLOAD_BYTES} bytes`,
|
|
);
|
|
const frame = encodePluginInputFrame({
|
|
requestId: "r".repeat(255),
|
|
pluginId: "p",
|
|
method: "m".repeat(0xffff - 30),
|
|
payload: bytes.subarray(1),
|
|
});
|
|
expect(frame.byteLength).toBeLessThanOrEqual(MAX_ROUTED_PAYLOAD_BYTES);
|
|
});
|
|
|
|
it("rejects bytes nested in a value and names the rule and the path", () => {
|
|
expect(() => assertPluginWireValue({ frames: [{ data: new Uint8Array(1) }] }, "event")).toThrow(
|
|
"event.frames[0].data is a Uint8Array. Bytes cross the wire only as the whole value: send the Uint8Array itself, or encode it into the value.",
|
|
);
|
|
});
|
|
});
|