@riebeckite/runtime 0.0.0-stage → 0.0.37
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +97 -2
- package/README_ja.md +100 -0
- package/dist/client.d.ts +8 -0
- package/dist/client.js +1343 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +617 -0
- package/dist/src/client/errors.d.ts +2 -0
- package/dist/src/client/fallback.d.ts +7 -0
- package/dist/src/client/host.d.ts +10 -0
- package/dist/src/client/iframe.d.ts +16 -0
- package/dist/src/client/main.d.ts +19 -0
- package/dist/src/client/wasm_worker.d.ts +12 -0
- package/dist/src/client/worker.d.ts +31 -0
- package/dist/src/core/abort.d.ts +1 -0
- package/dist/src/core/errors.d.ts +44 -0
- package/dist/src/core/instance.d.ts +32 -0
- package/dist/src/core/outcome.d.ts +7 -0
- package/dist/src/core/payload.d.ts +6 -0
- package/dist/src/core/protocol.d.ts +26 -0
- package/dist/src/core/registry.d.ts +17 -0
- package/dist/src/core/types.d.ts +77 -0
- package/dist/worker.d.ts +37 -0
- package/dist/worker.js +438 -0
- package/package.json +66 -3
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { RuntimeCapability, RuntimeDefinition, RuntimeKind, RuntimeLimits } from "../core/types.js";
|
|
2
|
+
export declare const MAX_ACTIVE_WORKERS = 8;
|
|
3
|
+
export type WorkerMessageEvent = {
|
|
4
|
+
readonly data: unknown;
|
|
5
|
+
};
|
|
6
|
+
export type WorkerErrorEvent = {
|
|
7
|
+
readonly error?: unknown;
|
|
8
|
+
readonly message?: string;
|
|
9
|
+
};
|
|
10
|
+
export type WorkerLike = {
|
|
11
|
+
postMessage(message: unknown, transfer?: Transferable[]): void;
|
|
12
|
+
addEventListener(type: "message", listener: (event: WorkerMessageEvent) => void): void;
|
|
13
|
+
addEventListener(type: "error", listener: (event: WorkerErrorEvent) => void): void;
|
|
14
|
+
addEventListener(type: "messageerror", listener: (event: WorkerMessageEvent) => void): void;
|
|
15
|
+
removeEventListener(type: "message", listener: (event: WorkerMessageEvent) => void): void;
|
|
16
|
+
removeEventListener(type: "error", listener: (event: WorkerErrorEvent) => void): void;
|
|
17
|
+
removeEventListener(type: "messageerror", listener: (event: WorkerMessageEvent) => void): void;
|
|
18
|
+
terminate(): void;
|
|
19
|
+
};
|
|
20
|
+
export type WorkerRuntimeOptions<TInput, TOutput> = {
|
|
21
|
+
readonly id: string;
|
|
22
|
+
readonly kind?: RuntimeKind;
|
|
23
|
+
readonly capabilities?: readonly RuntimeCapability[];
|
|
24
|
+
readonly limits?: RuntimeLimits;
|
|
25
|
+
readonly createWorker: () => WorkerLike;
|
|
26
|
+
readonly init?: unknown;
|
|
27
|
+
readonly initTransfer?: readonly Transferable[];
|
|
28
|
+
readonly invokePayload?: (input: TInput) => unknown;
|
|
29
|
+
readonly mapResult?: (payload: unknown) => TOutput;
|
|
30
|
+
};
|
|
31
|
+
export declare function createWorkerRuntime<TInput, TOutput>(options: WorkerRuntimeOptions<TInput, TOutput>): RuntimeDefinition<TInput, TOutput>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function abortReasonToError(signal: AbortSignal): Error;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export declare class RuntimeError extends Error {
|
|
2
|
+
constructor(message: string, options?: ErrorOptions);
|
|
3
|
+
}
|
|
4
|
+
export declare class RuntimeStateError extends RuntimeError {
|
|
5
|
+
constructor(message: string);
|
|
6
|
+
}
|
|
7
|
+
export declare class RuntimeFailureError extends RuntimeStateError {
|
|
8
|
+
constructor(message: string, options?: ErrorOptions);
|
|
9
|
+
}
|
|
10
|
+
export declare class RuntimeAbortError extends RuntimeError {
|
|
11
|
+
constructor(message?: string);
|
|
12
|
+
}
|
|
13
|
+
export declare class RuntimeTimeoutError extends RuntimeError {
|
|
14
|
+
constructor(message?: string);
|
|
15
|
+
}
|
|
16
|
+
export declare class RuntimePayloadError extends RuntimeError {
|
|
17
|
+
constructor(message: string);
|
|
18
|
+
}
|
|
19
|
+
export declare class RuntimeConcurrencyError extends RuntimeError {
|
|
20
|
+
constructor(message: string);
|
|
21
|
+
}
|
|
22
|
+
export declare class RuntimeWorkerError extends RuntimeError {
|
|
23
|
+
readonly remoteName: string | undefined;
|
|
24
|
+
readonly remoteKind: string | undefined;
|
|
25
|
+
constructor(message: string, options?: ErrorOptions & {
|
|
26
|
+
remoteName?: string;
|
|
27
|
+
remoteKind?: string;
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
export declare class RuntimeWasmError extends RuntimeError {
|
|
31
|
+
readonly trap: boolean;
|
|
32
|
+
constructor(message: string, options?: ErrorOptions & {
|
|
33
|
+
trap?: boolean;
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
export declare class RuntimeIframeError extends RuntimeError {
|
|
37
|
+
readonly remoteKind: string | undefined;
|
|
38
|
+
constructor(message: string, options?: ErrorOptions & {
|
|
39
|
+
remoteKind?: string;
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
export declare class RuntimeUnsupportedError extends RuntimeError {
|
|
43
|
+
constructor(message: string);
|
|
44
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { InteractiveRuntime, InvokeOptions, RuntimeCapability, RuntimeDefinition, RuntimeInstanceOptions, RuntimeKind, RuntimeState, RuntimeStateChange } from "./types.js";
|
|
2
|
+
export declare class RuntimeInstance<TInput, TOutput> implements InteractiveRuntime<TInput, TOutput> {
|
|
3
|
+
readonly id: string;
|
|
4
|
+
readonly instanceId: string;
|
|
5
|
+
readonly kind: RuntimeKind;
|
|
6
|
+
readonly capabilities: readonly RuntimeCapability[];
|
|
7
|
+
private readonly driver;
|
|
8
|
+
private readonly controller;
|
|
9
|
+
private readonly listeners;
|
|
10
|
+
private readonly active;
|
|
11
|
+
private readonly limits;
|
|
12
|
+
private readonly onError;
|
|
13
|
+
private currentState;
|
|
14
|
+
private requestCount;
|
|
15
|
+
private mountPromise;
|
|
16
|
+
private disposePromise;
|
|
17
|
+
private stopPromise;
|
|
18
|
+
private failure;
|
|
19
|
+
constructor(definition: RuntimeDefinition<TInput, TOutput>, options: RuntimeInstanceOptions);
|
|
20
|
+
get state(): RuntimeState;
|
|
21
|
+
get signal(): AbortSignal;
|
|
22
|
+
onStateChange(listener: (change: RuntimeStateChange) => void): () => void;
|
|
23
|
+
mount(): Promise<void>;
|
|
24
|
+
invoke(input: TInput, options?: InvokeOptions): Promise<TOutput>;
|
|
25
|
+
dispose(): Promise<void>;
|
|
26
|
+
private runStart;
|
|
27
|
+
private runStop;
|
|
28
|
+
private stopDriver;
|
|
29
|
+
private disposedError;
|
|
30
|
+
private transition;
|
|
31
|
+
private report;
|
|
32
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare class RuntimeTransfer<TOutput> {
|
|
2
|
+
readonly value: TOutput;
|
|
3
|
+
readonly transfer: readonly unknown[];
|
|
4
|
+
constructor(value: TOutput, transfer?: readonly unknown[]);
|
|
5
|
+
}
|
|
6
|
+
export declare function transferOutput<TOutput>(value: TOutput, transfer?: readonly unknown[]): RuntimeTransfer<TOutput>;
|
|
7
|
+
export declare function isRuntimeTransfer<TOutput>(value: unknown): value is RuntimeTransfer<TOutput>;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Estimates the structured-clone payload size in bytes. It intentionally
|
|
3
|
+
* rejects graphs that are too deep or too large instead of walking forever.
|
|
4
|
+
*/
|
|
5
|
+
export declare function estimatePayloadSize(value: unknown): number;
|
|
6
|
+
export declare function assertPayloadSize(value: unknown, limit: number, label: string): void;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { RuntimeCapability, RuntimeLimits } from "./types.js";
|
|
2
|
+
export declare const RUNTIME_PROTOCOL = "riebeckite:runtime";
|
|
3
|
+
export declare const RUNTIME_PROTOCOL_VERSION = 1;
|
|
4
|
+
export declare const RUNTIME_MESSAGE_KINDS: readonly ["init", "invoke", "cancel", "ready", "result", "error", "fatal", "dispose"];
|
|
5
|
+
export type RuntimeMessageKind = (typeof RUNTIME_MESSAGE_KINDS)[number];
|
|
6
|
+
export type RuntimeErrorWire = {
|
|
7
|
+
readonly name: string;
|
|
8
|
+
readonly message: string;
|
|
9
|
+
readonly kind?: string;
|
|
10
|
+
readonly trap?: boolean;
|
|
11
|
+
readonly stack?: string;
|
|
12
|
+
};
|
|
13
|
+
export type RuntimeMessage = {
|
|
14
|
+
readonly protocol: typeof RUNTIME_PROTOCOL;
|
|
15
|
+
readonly version: number;
|
|
16
|
+
readonly instanceId: string;
|
|
17
|
+
readonly kind: RuntimeMessageKind;
|
|
18
|
+
readonly requestId?: number;
|
|
19
|
+
readonly capability?: RuntimeCapability;
|
|
20
|
+
readonly payload?: unknown;
|
|
21
|
+
readonly transfer?: readonly unknown[];
|
|
22
|
+
readonly limits?: RuntimeLimits;
|
|
23
|
+
readonly error?: RuntimeErrorWire;
|
|
24
|
+
};
|
|
25
|
+
export declare function isRuntimeMessage(value: unknown): value is RuntimeMessage;
|
|
26
|
+
export declare function serializeRuntimeError(error: unknown): RuntimeErrorWire;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { InteractiveRuntime, RuntimeCapability, RuntimeDefinition, RuntimeInstanceOptions, RuntimeKind, RuntimeLimits } from "./types.js";
|
|
2
|
+
export declare class InteractiveRuntimeRegistry {
|
|
3
|
+
private readonly definitions;
|
|
4
|
+
register<TInput, TOutput>(definition: RuntimeDefinition<TInput, TOutput>): void;
|
|
5
|
+
has(id: string): boolean;
|
|
6
|
+
get<TInput = unknown, TOutput = unknown>(id: string): RuntimeDefinition<TInput, TOutput>;
|
|
7
|
+
get ids(): readonly string[];
|
|
8
|
+
create<TInput = unknown, TOutput = unknown>(id: string, options?: RuntimeInstanceOptions): InteractiveRuntime<TInput, TOutput>;
|
|
9
|
+
}
|
|
10
|
+
export declare function defineInteractiveRuntime<TInput, TOutput>(definition: {
|
|
11
|
+
readonly id: string;
|
|
12
|
+
readonly kind: RuntimeKind;
|
|
13
|
+
readonly capabilities?: readonly RuntimeCapability[];
|
|
14
|
+
readonly limits?: RuntimeLimits;
|
|
15
|
+
readonly create: RuntimeDefinition<TInput, TOutput>["create"];
|
|
16
|
+
}): RuntimeDefinition<TInput, TOutput>;
|
|
17
|
+
export declare function createInteractiveRuntime<TInput, TOutput>(definition: RuntimeDefinition<TInput, TOutput>, options?: RuntimeInstanceOptions): InteractiveRuntime<TInput, TOutput>;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
export declare const RUNTIME_KINDS: readonly ["main", "worker", "wasm-worker", "iframe"];
|
|
2
|
+
export type RuntimeKind = (typeof RUNTIME_KINDS)[number];
|
|
3
|
+
export declare const RUNTIME_STATES: readonly ["created", "starting", "ready", "running", "stopping", "disposed", "failed"];
|
|
4
|
+
export type RuntimeState = (typeof RUNTIME_STATES)[number];
|
|
5
|
+
export declare const RUNTIME_CAPABILITIES: readonly ["dom", "worker", "wasm", "network", "message"];
|
|
6
|
+
export type RuntimeCapability = (typeof RUNTIME_CAPABILITIES)[number];
|
|
7
|
+
export type RuntimeLimits = {
|
|
8
|
+
readonly maxInputBytes?: number;
|
|
9
|
+
readonly maxOutputBytes?: number;
|
|
10
|
+
readonly maxConcurrent?: number;
|
|
11
|
+
readonly invokeTimeoutMs?: number;
|
|
12
|
+
readonly startTimeoutMs?: number;
|
|
13
|
+
};
|
|
14
|
+
export type ResolvedRuntimeLimits = {
|
|
15
|
+
readonly maxInputBytes: number;
|
|
16
|
+
readonly maxOutputBytes: number;
|
|
17
|
+
readonly maxConcurrent: number;
|
|
18
|
+
readonly invokeTimeoutMs: number;
|
|
19
|
+
readonly startTimeoutMs: number;
|
|
20
|
+
};
|
|
21
|
+
export type RuntimeInvokeContext = {
|
|
22
|
+
readonly signal: AbortSignal;
|
|
23
|
+
readonly requestId: number;
|
|
24
|
+
readonly capability?: RuntimeCapability;
|
|
25
|
+
readonly transfer?: readonly unknown[];
|
|
26
|
+
};
|
|
27
|
+
export type RuntimeDriverResult<TOutput> = {
|
|
28
|
+
readonly value: TOutput;
|
|
29
|
+
readonly transfer: readonly unknown[];
|
|
30
|
+
};
|
|
31
|
+
export type RuntimeDriver<TInput, TOutput> = {
|
|
32
|
+
start(): void | Promise<void>;
|
|
33
|
+
invoke(input: TInput, context: RuntimeInvokeContext): TOutput | RuntimeDriverResult<TOutput> | Promise<TOutput | RuntimeDriverResult<TOutput>>;
|
|
34
|
+
stop(): void | Promise<void>;
|
|
35
|
+
};
|
|
36
|
+
export type RuntimeCreateContext = {
|
|
37
|
+
readonly instanceId: string;
|
|
38
|
+
readonly signal: AbortSignal;
|
|
39
|
+
readonly limits: ResolvedRuntimeLimits;
|
|
40
|
+
/** DOM mount target. Core never reads it; adapters interpret it. */
|
|
41
|
+
readonly root?: unknown;
|
|
42
|
+
};
|
|
43
|
+
export type RuntimeDefinition<TInput, TOutput> = {
|
|
44
|
+
readonly id: string;
|
|
45
|
+
readonly kind: RuntimeKind;
|
|
46
|
+
readonly capabilities: readonly RuntimeCapability[];
|
|
47
|
+
readonly limits: RuntimeLimits;
|
|
48
|
+
create(context: RuntimeCreateContext): RuntimeDriver<TInput, TOutput>;
|
|
49
|
+
};
|
|
50
|
+
export type InvokeOptions = {
|
|
51
|
+
readonly signal?: AbortSignal;
|
|
52
|
+
readonly timeoutMs?: number;
|
|
53
|
+
readonly capability?: RuntimeCapability;
|
|
54
|
+
readonly transfer?: readonly unknown[];
|
|
55
|
+
};
|
|
56
|
+
export type RuntimeInstanceOptions = {
|
|
57
|
+
readonly signal?: AbortSignal;
|
|
58
|
+
readonly root?: unknown;
|
|
59
|
+
readonly onError?: (error: unknown, phase: RuntimeErrorPhase) => void;
|
|
60
|
+
};
|
|
61
|
+
export type RuntimeErrorPhase = "start" | "invoke" | "stop" | "state-change";
|
|
62
|
+
export type RuntimeStateChange = {
|
|
63
|
+
readonly previous: RuntimeState;
|
|
64
|
+
readonly state: RuntimeState;
|
|
65
|
+
};
|
|
66
|
+
export type InteractiveRuntime<TInput, TOutput> = {
|
|
67
|
+
readonly id: string;
|
|
68
|
+
readonly instanceId: string;
|
|
69
|
+
readonly kind: RuntimeKind;
|
|
70
|
+
readonly capabilities: readonly RuntimeCapability[];
|
|
71
|
+
readonly state: RuntimeState;
|
|
72
|
+
readonly signal: AbortSignal;
|
|
73
|
+
onStateChange(listener: (change: RuntimeStateChange) => void): () => void;
|
|
74
|
+
mount(): Promise<void>;
|
|
75
|
+
invoke(input: TInput, options?: InvokeOptions): Promise<TOutput>;
|
|
76
|
+
dispose(): Promise<void>;
|
|
77
|
+
};
|
package/dist/worker.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { type RuntimeTransfer } from "./src/core/outcome.js";
|
|
2
|
+
import type { RuntimeCapability } from "./src/core/types.js";
|
|
3
|
+
export type RuntimeMessageScope = {
|
|
4
|
+
postMessage(message: unknown, transfer?: Transferable[]): void;
|
|
5
|
+
addEventListener(type: "message", listener: (event: {
|
|
6
|
+
readonly data: unknown;
|
|
7
|
+
}) => void): void;
|
|
8
|
+
removeEventListener(type: "message", listener: (event: {
|
|
9
|
+
readonly data: unknown;
|
|
10
|
+
}) => void): void;
|
|
11
|
+
close?(): void;
|
|
12
|
+
};
|
|
13
|
+
export type WorkerRuntimeHandler<TInput, TOutput> = {
|
|
14
|
+
initialize?(init: unknown, context: {
|
|
15
|
+
readonly instanceId: string;
|
|
16
|
+
readonly signal: AbortSignal;
|
|
17
|
+
}): void | Promise<void>;
|
|
18
|
+
invoke(input: TInput, context: {
|
|
19
|
+
readonly instanceId: string;
|
|
20
|
+
readonly requestId: number;
|
|
21
|
+
readonly signal: AbortSignal;
|
|
22
|
+
readonly capability?: RuntimeCapability;
|
|
23
|
+
}): TOutput | RuntimeTransfer<TOutput> | Promise<TOutput | RuntimeTransfer<TOutput>>;
|
|
24
|
+
dispose?(): void | Promise<void>;
|
|
25
|
+
};
|
|
26
|
+
export declare function serveWorkerRuntime<TInput, TOutput>(scope: RuntimeMessageScope, handler: WorkerRuntimeHandler<TInput, TOutput>): () => void;
|
|
27
|
+
export type WasmWorkerInit = {
|
|
28
|
+
readonly wasmUrl: string;
|
|
29
|
+
readonly exportName: string;
|
|
30
|
+
};
|
|
31
|
+
export type WasmWorkerHostOptions = {
|
|
32
|
+
readonly imports?: WebAssembly.Imports;
|
|
33
|
+
readonly fetch?: (input: RequestInfo | URL) => Promise<Response>;
|
|
34
|
+
readonly allowCrossOrigin?: boolean;
|
|
35
|
+
readonly streaming?: boolean;
|
|
36
|
+
};
|
|
37
|
+
export declare function serveWasmWorkerRuntime(scope: RuntimeMessageScope, options?: WasmWorkerHostOptions): () => void;
|