@qping/plugin-bus 0.1.0 → 0.2.0
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/dist/bootstrap.d.ts +34 -0
- package/dist/bootstrap.mjs +170 -16
- package/dist/framing.d.ts +36 -0
- package/dist/i18n.d.ts +19 -0
- package/dist/i18n.mjs +2343 -0
- package/dist/node.d.ts +65 -0
- package/dist/{server.mjs → node.mjs} +244 -60
- package/dist/protocol.d.ts +101 -0
- package/dist/protocol.mjs +73 -1
- package/dist/router.d.ts +41 -0
- package/dist/transport.d.ts +24 -0
- package/dist/webClient.d.ts +34 -0
- package/dist/webClient.mjs +2599 -0
- package/dist/webTypes.d.ts +50 -0
- package/package.json +22 -8
- package/src/bootstrap.ts +98 -9
- package/src/i18n.ts +120 -0
- package/src/node.ts +213 -0
- package/src/protocol.ts +82 -17
- package/src/router.ts +65 -18
- package/src/transport.ts +2 -1
- package/src/webClient.ts +285 -0
- package/src/webTypes.ts +52 -0
- package/dist/bootstrap.d.mts +0 -1
- package/dist/protocol.d.mts +0 -1
- package/dist/server.d.mts +0 -1
- package/src/server.ts +0 -156
package/dist/protocol.mjs
CHANGED
|
@@ -1,4 +1,68 @@
|
|
|
1
1
|
// src/protocol.ts
|
|
2
|
+
var MessageKind = {
|
|
3
|
+
Request: "request",
|
|
4
|
+
Response: "response",
|
|
5
|
+
Event: "event"
|
|
6
|
+
};
|
|
7
|
+
var ErrorCode = {
|
|
8
|
+
ProtocolMismatch: "ProtocolMismatch",
|
|
9
|
+
HandshakeFailed: "HandshakeFailed",
|
|
10
|
+
CapabilityNotDeclared: "CapabilityNotDeclared",
|
|
11
|
+
CapabilityDenied: "CapabilityDenied",
|
|
12
|
+
InvalidPayload: "InvalidPayload",
|
|
13
|
+
MessageTooLarge: "MessageTooLarge",
|
|
14
|
+
RouteNotFound: "RouteNotFound",
|
|
15
|
+
RequestTimeout: "RequestTimeout",
|
|
16
|
+
TooManyRequests: "TooManyRequests",
|
|
17
|
+
TransportDisconnected: "TransportDisconnected",
|
|
18
|
+
PluginUnavailable: "PluginUnavailable",
|
|
19
|
+
InternalError: "InternalError",
|
|
20
|
+
Cancelled: "Cancelled",
|
|
21
|
+
RateLimited: "RateLimited"
|
|
22
|
+
};
|
|
23
|
+
var ProtocolVersion = "3.0";
|
|
24
|
+
var EndpointIds = {
|
|
25
|
+
NodeMain: "node-main",
|
|
26
|
+
Host: "host"
|
|
27
|
+
};
|
|
28
|
+
var Routes = {
|
|
29
|
+
Bus: {
|
|
30
|
+
Handshake: "bus.handshake",
|
|
31
|
+
Ping: "bus.ping",
|
|
32
|
+
Cancel: "bus.cancel",
|
|
33
|
+
Subscribe: "bus.subscribe",
|
|
34
|
+
Unsubscribe: "bus.unsubscribe"
|
|
35
|
+
},
|
|
36
|
+
Prefix: {
|
|
37
|
+
PluginCall: "plugin.call.",
|
|
38
|
+
HostCall: "host.call.",
|
|
39
|
+
PluginEvent: "plugin.event.",
|
|
40
|
+
HostEvent: "host.event.",
|
|
41
|
+
Diagnostics: "diagnostics."
|
|
42
|
+
},
|
|
43
|
+
PluginCall: {
|
|
44
|
+
Initialize: "plugin.call.initialize",
|
|
45
|
+
Search: "plugin.call.search",
|
|
46
|
+
InvokeAction: "plugin.call.invokeAction"
|
|
47
|
+
},
|
|
48
|
+
HostEvent: {
|
|
49
|
+
Initialize: "host.event.initialize",
|
|
50
|
+
Search: "host.event.search",
|
|
51
|
+
Key: "host.event.key",
|
|
52
|
+
LanguageChanged: "host.event.languageChanged",
|
|
53
|
+
ThemeChanged: "host.event.themeChanged",
|
|
54
|
+
InputActionCaptured: "host.event.inputActionCaptured"
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
function pluginCallRoute(method) {
|
|
58
|
+
return method.startsWith(Routes.Prefix.PluginCall) ? method : `${Routes.Prefix.PluginCall}${method}`;
|
|
59
|
+
}
|
|
60
|
+
function hostCallRoute(method) {
|
|
61
|
+
return method.startsWith(Routes.Prefix.HostCall) ? method : `${Routes.Prefix.HostCall}${method}`;
|
|
62
|
+
}
|
|
63
|
+
function pluginEventRoute(subjectId) {
|
|
64
|
+
return subjectId.startsWith(Routes.Prefix.PluginEvent) ? subjectId : `${Routes.Prefix.PluginEvent}${subjectId}`;
|
|
65
|
+
}
|
|
2
66
|
function canonicalStringify(value) {
|
|
3
67
|
return JSON.stringify(stripNulls(value));
|
|
4
68
|
}
|
|
@@ -19,6 +83,14 @@ function canonicalize(json) {
|
|
|
19
83
|
return canonicalStringify(JSON.parse(json));
|
|
20
84
|
}
|
|
21
85
|
export {
|
|
86
|
+
EndpointIds,
|
|
87
|
+
ErrorCode,
|
|
88
|
+
MessageKind,
|
|
89
|
+
ProtocolVersion,
|
|
90
|
+
Routes,
|
|
22
91
|
canonicalStringify,
|
|
23
|
-
canonicalize
|
|
92
|
+
canonicalize,
|
|
93
|
+
hostCallRoute,
|
|
94
|
+
pluginCallRoute,
|
|
95
|
+
pluginEventRoute
|
|
24
96
|
};
|
package/dist/router.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Handler router for the Node SDK v3. Dispatches inbound plugin.call.* requests to registered
|
|
3
|
+
* handlers, auto-replies to bus.ping, and provides callHost() to invoke host.call.* capabilities
|
|
4
|
+
* and correlate their responses. Mirrors the C# MessageBus routing rules on the Node side.
|
|
5
|
+
*/
|
|
6
|
+
import { type Envelope } from "./protocol.ts";
|
|
7
|
+
type Handler = (payload: unknown) => Promise<unknown> | unknown;
|
|
8
|
+
type Sender = (env: Envelope) => void;
|
|
9
|
+
export declare const DefaultHostCallTimeoutMs = 30000;
|
|
10
|
+
/** Remaining ms of the inbound plugin.call timeout, if currently inside a request. */
|
|
11
|
+
export declare function remainingTimeoutMs(): number | undefined;
|
|
12
|
+
export declare function resolveHostCallTimeoutMs(explicit?: number): number;
|
|
13
|
+
export declare class HandlerRouter {
|
|
14
|
+
private handlers;
|
|
15
|
+
private pendingHostCalls;
|
|
16
|
+
private pluginId;
|
|
17
|
+
private entryId;
|
|
18
|
+
private sessionId;
|
|
19
|
+
private endpointId;
|
|
20
|
+
/** Injected transport send fn; tests can override `router.send` directly. */
|
|
21
|
+
send: Sender;
|
|
22
|
+
constructor(deps: {
|
|
23
|
+
send: Sender;
|
|
24
|
+
});
|
|
25
|
+
/** Sets the bound identity stamped on outbound messages (after handshake). */
|
|
26
|
+
setIdentity(ids: {
|
|
27
|
+
pluginId: string;
|
|
28
|
+
entryId: string;
|
|
29
|
+
sessionId: string;
|
|
30
|
+
endpointId: string;
|
|
31
|
+
}): void;
|
|
32
|
+
handle(route: string, handler: Handler): void;
|
|
33
|
+
/** Dispatches an inbound request/response. Returns once handled. */
|
|
34
|
+
dispatch(env: Envelope): Promise<void>;
|
|
35
|
+
/** Calls a host.call.* capability and resolves with the response payload. */
|
|
36
|
+
callHost(route: string, payload: unknown, timeoutMs?: number): Promise<unknown>;
|
|
37
|
+
private handleHostResponse;
|
|
38
|
+
private responseFor;
|
|
39
|
+
private errorResponseFor;
|
|
40
|
+
}
|
|
41
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node-side named-pipe transport for the v3 message bus. Connects to the host's pipe server
|
|
3
|
+
* (created by NamedPipeTransport on the C# side), runs an incremental FrameDecoder read loop,
|
|
4
|
+
* and sends length-prefixed frames. Mirrors MyTools.Host.Transports.NamedPipeTransport.
|
|
5
|
+
*/
|
|
6
|
+
import { type Envelope } from "./protocol.ts";
|
|
7
|
+
type MessageHandler = (env: Envelope) => void;
|
|
8
|
+
type DisconnectHandler = () => void;
|
|
9
|
+
export declare class NodeTransport {
|
|
10
|
+
private socket;
|
|
11
|
+
private messageHandlers;
|
|
12
|
+
private disconnectHandlers;
|
|
13
|
+
private closed;
|
|
14
|
+
onMessage(handler: MessageHandler): () => void;
|
|
15
|
+
onDisconnect(handler: DisconnectHandler): void;
|
|
16
|
+
get isConnected(): boolean;
|
|
17
|
+
/** Connects to a Windows named pipe (\\.\pipe\<name>). */
|
|
18
|
+
connect(pipePath: string): Promise<void>;
|
|
19
|
+
/** Serializes an envelope to a length-prefixed frame and writes it. */
|
|
20
|
+
send(env: Envelope): void;
|
|
21
|
+
close(): Promise<void>;
|
|
22
|
+
private handleDisconnect;
|
|
23
|
+
}
|
|
24
|
+
export {};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* v3 Web SDK: speaks protocol envelopes over chrome.webview.postMessage.
|
|
3
|
+
* Host stamps identity; the page does not supply plugin/entry/session ids.
|
|
4
|
+
*
|
|
5
|
+
* Connections start with bus.handshake. Subsequent plugin.call.* envelopes use the
|
|
6
|
+
* negotiated version. call("refresh") is sent as plugin.call.refresh.
|
|
7
|
+
*/
|
|
8
|
+
import { mytoolsI18n } from "./i18n.ts";
|
|
9
|
+
import type { MyToolsThemePayload } from "./webTypes.ts";
|
|
10
|
+
export { mytoolsI18n } from "./i18n.ts";
|
|
11
|
+
export { HostEvents } from "./webTypes.ts";
|
|
12
|
+
export type { MyToolsHostInitializePayload, MyToolsHostKeyPayload, MyToolsHostSearchPayload, MyToolsInputActionCapturedPayload, MyToolsLanguageChangedPayload, MyToolsThemeChangedPayload, MyToolsThemePayload, } from "./webTypes.ts";
|
|
13
|
+
export interface WebBusClient {
|
|
14
|
+
/** Sends plugin.call.<method>. Bare names are prefixed; full routes are left as-is. */
|
|
15
|
+
call<T = unknown>(method: string, payload?: unknown, timeoutMs?: number): Promise<T>;
|
|
16
|
+
on<T = unknown>(route: string, handler: (payload: T) => void): () => void;
|
|
17
|
+
i18n: typeof mytoolsI18n;
|
|
18
|
+
theme: typeof mytoolsTheme;
|
|
19
|
+
close(): void;
|
|
20
|
+
}
|
|
21
|
+
declare const mytoolsTheme: {
|
|
22
|
+
current: string;
|
|
23
|
+
apply(payload: MyToolsThemePayload): void;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Creates a Web bus client. Registers the message listener immediately so host
|
|
27
|
+
* events that arrive before `on()` are buffered and replayed.
|
|
28
|
+
*
|
|
29
|
+
* Handshake is required before call(). Page scripts are bundled as IIFE, so this
|
|
30
|
+
* function is synchronous; handshake runs in the background and gates call().
|
|
31
|
+
*/
|
|
32
|
+
export declare function createWebBusClient(options?: {
|
|
33
|
+
timeoutMs?: number;
|
|
34
|
+
}): WebBusClient;
|