Skip to content

Commit

Permalink
Add breadcrumbs and accept a log callback to retrieve breadcrumbs
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielParpal committed Jul 26, 2024
1 parent 8785e87 commit 95915cf
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions packages/rpc/src/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,28 @@ export const RELEASE = 3;
export const FUNCTION_APPLY = 5;
export const FUNCTION_RESULT = 6;

const BREADCRUMB_PREFIX = 'remote-ui';

type AnyFunction = (...args: any[]) => any;

interface ErrorWithBreadcrumbs extends Error {
breadcrumbs?: string;
}

interface MessageMap {
[CALL]: [string, string | number, any];
[RESULT]: [string, Error?, any?];
[RESULT]: [string, ErrorWithBreadcrumbs?, any?];
[TERMINATE]: void;
[RELEASE]: [string];
[FUNCTION_APPLY]: [string, string, any];
[FUNCTION_RESULT]: [string, Error?, any?];
[FUNCTION_RESULT]: [string, ErrorWithBreadcrumbs?, any?];
}

export interface CreateEndpointOptions<T = unknown> {
uuid?(): string;
createEncoder?(api: EncodingStrategyApi): EncodingStrategy;
callable?: (keyof T)[];
logCallback?(message?: string): void;
}

export interface Endpoint<T> {
Expand Down Expand Up @@ -69,10 +76,12 @@ export function createEndpoint<T>(
uuid = defaultUuid,
createEncoder = createBasicEncoder,
callable,
logCallback,
}: CreateEndpointOptions<T> = {},
): Endpoint<T> {
let terminated = false;
let messenger = initialMessenger;
const breadcrumbs: string[] = [];

const activeApi = new Map<string | number, AnyFunction>();
const callIdsToResolver = new Map<
Expand Down Expand Up @@ -101,6 +110,7 @@ export function createEndpoint<T>(
});

messenger.addEventListener('message', listener);
breadcrumbs.push(`${BREADCRUMB_PREFIX}::endpoint listener added`);

return {
call,
Expand All @@ -112,6 +122,7 @@ export function createEndpoint<T>(
newMessenger.addEventListener('message', listener);
},
expose(api) {
breadcrumbs.push(`${BREADCRUMB_PREFIX}::expose-started`);
for (const key of Object.keys(api)) {
const value = api[key];

Expand All @@ -121,6 +132,10 @@ export function createEndpoint<T>(
activeApi.delete(key);
}
}
const activeApiKeys = Array.from(activeApi.keys());
breadcrumbs.push(
`${BREADCRUMB_PREFIX}::expose-completed ${activeApiKeys.join(', ')}`,
);
},
callable(...newCallable) {
// If no callable methods are supplied initially, we use a Proxy instead,
Expand Down Expand Up @@ -190,7 +205,10 @@ export function createEndpoint<T>(
send(RESULT, [id, undefined, encoded], transferables);
} catch (error) {
const {name, message, stack} = error as Error;
send(RESULT, [id, {name, message, stack}]);
send(RESULT, [
id,
{name, message, stack, breadcrumbs: breadcrumbs.join(' ')},
]);
throw error;
} finally {
stackFrame.release();
Expand Down Expand Up @@ -231,7 +249,10 @@ export function createEndpoint<T>(
send(FUNCTION_RESULT, [callId, undefined, encoded], transferables);
} catch (error) {
const {name, message, stack} = error as Error;
send(FUNCTION_RESULT, [callId, {name, message, stack}]);
send(FUNCTION_RESULT, [
callId,
{name, message, stack, breadcrumbs: breadcrumbs.join(' ')},
]);
throw error;
}

Expand Down Expand Up @@ -274,6 +295,7 @@ export function createEndpoint<T>(
if (errorResult == null) {
resolve(value && encoder.decode(value, retainedBy));
} else {
logCallback?.(errorResult?.breadcrumbs);
const error = new Error();
Object.assign(error, errorResult);
reject(error);
Expand Down

0 comments on commit 95915cf

Please sign in to comment.