Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export * from './errors.js';
export * from './types.js';
export { ProcessTransport } from './transport.js';

export { dispatchNotification, ProtocolEngine } from './protocol.js';
export { dispatchNotification, ProtocolEngine, setGlobalMetaProvider } from './protocol.js';
export type {
AskUserHandler,
NotificationCallback,
Expand Down
15 changes: 15 additions & 0 deletions src/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,19 @@ const DEFAULT_SERVER_REQUEST_METHOD_MAP: Record<
[DroidClientMethod.ASK_USER]: ServerRequestHandlerType.AskUser,
};

// ─── Module-level JSON-RPC metadata provider ──────────────────────────────

type MetaProvider = () => Record<string, string | undefined> | undefined;
let _metaProvider: MetaProvider | null = null;

/**
* Registers a callback that is invoked on every outgoing JSON-RPC request to
* inject metadata into the envelope _meta field. Pass null to clear.
*/
export function setGlobalMetaProvider(fn: MetaProvider | null): void {
_metaProvider = fn;
}

export class ProtocolEngine {
private readonly _transport: DroidClientTransport;
private readonly _defaultTimeout: number;
Expand Down Expand Up @@ -160,6 +173,7 @@ export class ProtocolEngine {
const effectiveTimeout = timeout ?? this._defaultTimeout;
const requestId = uuidv4();

const _meta = _metaProvider?.();
const envelope = {
jsonrpc: JSONRPC_VERSION,
factoryApiVersion: LEGACY_FACTORY_API_VERSION,
Expand All @@ -168,6 +182,7 @@ export class ProtocolEngine {
id: requestId,
method,
params,
...(_meta ? { _meta } : {}),
};

// Create a promise that will be resolved when we get a matching response
Expand Down