Skip to content

Commit d4f8dfa

Browse files
fix: avoid async boundary when no middleware is configured
Only await middleware application when there's actually middleware configured. This preserves synchronous behavior for existing tests that depend on immediate message queuing.
1 parent 74fa356 commit d4f8dfa

1 file changed

Lines changed: 54 additions & 27 deletions

File tree

packages/core/src/shared/protocol.ts

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -663,20 +663,26 @@ export abstract class Protocol<SendRequestT extends Request, SendNotificationT e
663663
this._transport.onmessage = (message, extra) => {
664664
_onmessage?.(message, extra);
665665

666-
// Apply receive middleware and then route the message
667-
this._applyMiddleware(message, this._options?.receiveMiddleware)
668-
.then(transformedMessage => {
669-
if (isJSONRPCResultResponse(transformedMessage) || isJSONRPCErrorResponse(transformedMessage)) {
670-
this._onresponse(transformedMessage);
671-
} else if (isJSONRPCRequest(transformedMessage)) {
672-
this._onrequest(transformedMessage, extra);
673-
} else if (isJSONRPCNotification(transformedMessage)) {
674-
this._onnotification(transformedMessage);
675-
} else {
676-
this._onerror(new Error(`Unknown message type: ${JSON.stringify(transformedMessage)}`));
677-
}
678-
})
679-
.catch(error => this._onerror(new Error(`Receive middleware error: ${error}`)));
666+
// Route the message, applying receive middleware if configured
667+
const routeMessage = (msg: JSONRPCMessageLike) => {
668+
if (isJSONRPCResultResponse(msg) || isJSONRPCErrorResponse(msg)) {
669+
this._onresponse(msg);
670+
} else if (isJSONRPCRequest(msg)) {
671+
this._onrequest(msg, extra);
672+
} else if (isJSONRPCNotification(msg)) {
673+
this._onnotification(msg);
674+
} else {
675+
this._onerror(new Error(`Unknown message type: ${JSON.stringify(msg)}`));
676+
}
677+
};
678+
679+
if (this._options?.receiveMiddleware && this._options.receiveMiddleware.length > 0) {
680+
this._applyMiddleware(message, this._options.receiveMiddleware)
681+
.then(routeMessage)
682+
.catch(error => this._onerror(new Error(`Receive middleware error: ${error}`)));
683+
} else {
684+
routeMessage(message);
685+
}
680686
};
681687

682688
await this._transport.start();
@@ -1158,8 +1164,10 @@ export abstract class Protocol<SendRequestT extends Request, SendNotificationT e
11581164
};
11591165
}
11601166

1161-
// Apply send middleware before sending
1162-
jsonrpcRequest = (await this._applyMiddleware(jsonrpcRequest, this._options?.sendMiddleware)) as JSONRPCRequest;
1167+
// Apply send middleware before sending (only await if there's middleware)
1168+
if (this._options?.sendMiddleware && this._options.sendMiddleware.length > 0) {
1169+
jsonrpcRequest = (await this._applyMiddleware(jsonrpcRequest, this._options.sendMiddleware)) as JSONRPCRequest;
1170+
}
11631171

11641172
// Send the request
11651173
return new Promise<SchemaOutput<T>>((resolve, reject) => {
@@ -1349,12 +1357,18 @@ export abstract class Protocol<SendRequestT extends Request, SendNotificationT e
13491357
}
13501358
};
13511359

1352-
// Apply send middleware before queuing
1353-
const transformedNotification = await this._applyMiddleware(jsonrpcNotification, this._options?.sendMiddleware);
1360+
// Apply send middleware before queuing (only if there's middleware)
1361+
let notificationToQueue: JSONRPCNotification = jsonrpcNotification;
1362+
if (this._options?.sendMiddleware && this._options.sendMiddleware.length > 0) {
1363+
notificationToQueue = (await this._applyMiddleware(
1364+
jsonrpcNotification,
1365+
this._options.sendMiddleware
1366+
)) as JSONRPCNotification;
1367+
}
13541368

13551369
await this._enqueueTaskMessage(relatedTaskId, {
13561370
type: 'notification',
1357-
message: transformedNotification as JSONRPCNotification,
1371+
message: notificationToQueue,
13581372
timestamp: Date.now()
13591373
});
13601374

@@ -1410,11 +1424,17 @@ export abstract class Protocol<SendRequestT extends Request, SendNotificationT e
14101424

14111425
// Apply send middleware and send the notification.
14121426
// Handle potential errors with a .catch().
1413-
this._applyMiddleware(jsonrpcNotification, this._options?.sendMiddleware)
1414-
.then(transformedNotification => {
1415-
this._transport?.send(transformedNotification as JSONRPCNotification, options).catch(error => this._onerror(error));
1416-
})
1417-
.catch(error => this._onerror(error));
1427+
if (this._options?.sendMiddleware && this._options.sendMiddleware.length > 0) {
1428+
this._applyMiddleware(jsonrpcNotification, this._options.sendMiddleware)
1429+
.then(transformedNotification => {
1430+
this._transport
1431+
?.send(transformedNotification as JSONRPCNotification, options)
1432+
.catch(error => this._onerror(error));
1433+
})
1434+
.catch(error => this._onerror(error));
1435+
} else {
1436+
this._transport?.send(jsonrpcNotification, options).catch(error => this._onerror(error));
1437+
}
14181438
});
14191439

14201440
// Return immediately.
@@ -1440,9 +1460,16 @@ export abstract class Protocol<SendRequestT extends Request, SendNotificationT e
14401460
};
14411461
}
14421462

1443-
// Apply send middleware before sending
1444-
const transformedNotification = await this._applyMiddleware(jsonrpcNotification, this._options?.sendMiddleware);
1445-
await this._transport.send(transformedNotification as JSONRPCNotification, options);
1463+
// Apply send middleware before sending (only if there's middleware)
1464+
if (this._options?.sendMiddleware && this._options.sendMiddleware.length > 0) {
1465+
const transformedNotification = (await this._applyMiddleware(
1466+
jsonrpcNotification,
1467+
this._options.sendMiddleware
1468+
)) as JSONRPCNotification;
1469+
await this._transport.send(transformedNotification, options);
1470+
} else {
1471+
await this._transport.send(jsonrpcNotification, options);
1472+
}
14461473
}
14471474

14481475
/**

0 commit comments

Comments
 (0)