Skip to content
This repository has been archived by the owner on Nov 9, 2023. It is now read-only.

Commit

Permalink
Rename _middleware to _requestMiddleware
Browse files Browse the repository at this point in the history
  • Loading branch information
rekmarks committed May 16, 2022
1 parent 7d66fb9 commit 99bc8d7
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions src/JsonRpcEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ export type JsonRpcNotificationMiddleware<T> = (
* Give it a stack of middleware, pass it requests, and get back responses.
*/
export class JsonRpcEngine extends SafeEventEmitter {
private _middleware: JsonRpcMiddleware<unknown, unknown>[];
private _requestMiddleware: JsonRpcMiddleware<unknown, unknown>[];

private _notificationMiddleware: JsonRpcNotificationMiddleware<unknown>[];

constructor() {
super();
this._middleware = [];
this._requestMiddleware = [];
this._notificationMiddleware = [];
}

Expand All @@ -111,7 +111,9 @@ export class JsonRpcEngine extends SafeEventEmitter {
* @param middleware - The middleware function to add.
*/
pushRequestMiddleware<T, U>(middleware: JsonRpcMiddleware<T, U>): void {
this._middleware.push(middleware as JsonRpcMiddleware<unknown, unknown>);
this._requestMiddleware.push(
middleware as JsonRpcMiddleware<unknown, unknown>,
);
}

/**
Expand All @@ -133,7 +135,9 @@ export class JsonRpcEngine extends SafeEventEmitter {
pushNotificationMiddleware<T>(
middleware: JsonRpcNotificationMiddleware<T>,
): void {
this._middleware.push(middleware as JsonRpcNotificationMiddleware<unknown>);
this._requestMiddleware.push(
middleware as JsonRpcNotificationMiddleware<unknown>,
);
}

/**
Expand Down Expand Up @@ -225,7 +229,11 @@ export class JsonRpcEngine extends SafeEventEmitter {
return async (req, res, next, end) => {
try {
const [middlewareError, isComplete, returnHandlers] =
await JsonRpcEngine._runAllMiddleware(req, res, this._middleware);
await JsonRpcEngine._runAllMiddleware(
req,
res,
this._requestMiddleware,
);

if (isComplete) {
await JsonRpcEngine._runReturnHandlers(returnHandlers);
Expand Down Expand Up @@ -358,7 +366,7 @@ export class JsonRpcEngine extends SafeEventEmitter {

// Handle notifications.
// We can't use isJsonRpcNotification here because that narrows callerReq to
// "never" after the if clause.
// "never" after the if clause for unknown reasons.
if (!isJsonRpcRequest(callerReq)) {
try {
await JsonRpcEngine._processNotification(
Expand All @@ -379,7 +387,7 @@ export class JsonRpcEngine extends SafeEventEmitter {
};

try {
await JsonRpcEngine._processRequest(req, res, this._middleware);
await JsonRpcEngine._processRequest(req, res, this._requestMiddleware);
} catch (_error) {
// A request handler error, a re-thrown middleware error, or something
// unexpected.
Expand All @@ -401,13 +409,13 @@ export class JsonRpcEngine extends SafeEventEmitter {
* Runs all notification middleware on the specified notification.
*
* @param notification - The notification object to process.
* @param middlewareStack - The stack of notification middleware functions.
* @param notificationMiddlewares - The stack of notification middleware functions.
*/
private static async _processNotification(
notification: JsonRpcNotification<unknown>,
middlewareStack: JsonRpcNotificationMiddleware<unknown>[],
notificationMiddlewares: JsonRpcNotificationMiddleware<unknown>[],
): Promise<void> {
for (const middleware of middlewareStack) {
for (const middleware of notificationMiddlewares) {
await middleware(notification);
}
}
Expand All @@ -419,15 +427,15 @@ export class JsonRpcEngine extends SafeEventEmitter {
*
* @param req - The request object.
* @param res - The response object.
* @param middlewareStack - The stack of request middleware functions.
* @param requestMiddlewares - The stack of request middleware functions.
*/
private static async _processRequest(
req: JsonRpcRequest<unknown>,
res: PendingJsonRpcResponse<unknown>,
middlewareStack: JsonRpcMiddleware<unknown, unknown>[],
requestMiddlewares: JsonRpcMiddleware<unknown, unknown>[],
): Promise<void> {
const [error, isComplete, returnHandlers] =
await JsonRpcEngine._runAllMiddleware(req, res, middlewareStack);
await JsonRpcEngine._runAllMiddleware(req, res, requestMiddlewares);

// Throw if "end" was not called, or if the response has neither a result
// nor an error.
Expand All @@ -449,15 +457,15 @@ export class JsonRpcEngine extends SafeEventEmitter {
*
* @param req - The request object.
* @param res - The response object.
* @param middlewareStack - The stack of middleware functions to execute.
* @param requestMiddlewares - The stack of middleware functions to execute.
* @returns An array of any error encountered during middleware execution,
* a boolean indicating whether the request was completed, and an array of
* middleware-defined return handlers.
*/
private static async _runAllMiddleware(
req: JsonRpcRequest<unknown>,
res: PendingJsonRpcResponse<unknown>,
middlewareStack: JsonRpcMiddleware<unknown, unknown>[],
requestMiddlewares: JsonRpcMiddleware<unknown, unknown>[],
): Promise<
[
unknown, // error
Expand All @@ -470,7 +478,7 @@ export class JsonRpcEngine extends SafeEventEmitter {
let isComplete = false;

// Go down stack of middleware, call and collect optional returnHandlers
for (const middleware of middlewareStack) {
for (const middleware of requestMiddlewares) {
[error, isComplete] = await JsonRpcEngine._runMiddleware(
req,
res,
Expand Down

0 comments on commit 99bc8d7

Please sign in to comment.