Repository navigation
Add outgoing and incoming message hooks to MessageStrategy #1925
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
b11b9cf
ce58efa
efce8f7
4acc143
8575cc1
fbf0378
8a7946e
25e1a1a
46c7ab1
67fea13
64a8d96
887ffde
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -474,13 +474,56 @@ export namespace CancellationStrategy { | |
| } | ||
|
|
||
| export interface MessageStrategy { | ||
| handleMessage(message: Message, next: (message: Message) => NotificationResult): NotificationResult; | ||
| /** | ||
| * Handles a received message before it is dispatched. Not called if | ||
| * `handleIncomingMessage` is provided. | ||
| * | ||
| * @deprecated Use `handleIncomingMessage` instead. | ||
| * | ||
| * @param message The received message. | ||
| * @param next Dispatches the message. | ||
| */ | ||
| handleMessage?(message: Message, next: (message: Message) => NotificationResult): NotificationResult; | ||
|
|
||
| /** | ||
| * Handles a received message before it is dispatched. If provided, it is | ||
| * used instead of `handleMessage`. Unlike `handleMessage`, it also gets the | ||
| * messages that skip the message queue: responses when `maxParallelism` is | ||
| * set, and `$/cancelRequest` notifications as soon as they are read. | ||
| * | ||
| * You must call `next` for `$/cancelRequest`, or the request is not | ||
| * canceled. | ||
| * | ||
| * @param message The received message. | ||
| * @param next Dispatches the message. | ||
| */ | ||
| handleIncomingMessage?(message: Message, next: (message: Message) => NotificationResult): NotificationResult; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since cancellations now depend on the hook, a hook that only forwards some messages and forgets
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 8575cc1. The JSDoc now says "You must call |
||
|
|
||
| /** | ||
| * Handles a message before it is written, for example to add a property | ||
| * like a trace context to it. The hook is called synchronously when the | ||
| * message is sent, so messages keep their send order. | ||
| * | ||
| * Properties added at the top level of a message reach the writer | ||
| * unchanged. `Message` does not declare them, so the object passed to | ||
| * `next` has to be cast to `Message`. | ||
| * | ||
| * You must call `next` exactly once before the hook returns. If it is not | ||
| * called, the message is not written and sending it fails: a request is | ||
| * rejected with `ErrorCodes.MessageWriteError` and the promise of a | ||
| * notification is rejected. Any other call to `next`, including one made | ||
| * after the hook returned, logs a warning and writes nothing. | ||
| * | ||
| * @param message The message to send. | ||
| * @param next Writes the message. Pass it the message to write. | ||
| */ | ||
| handleOutgoingMessage?(message: Message, next: (message: Message) => void): void; | ||
| } | ||
|
|
||
| export namespace MessageStrategy { | ||
| export function is(value: any): value is MessageStrategy { | ||
| const candidate: MessageStrategy = value; | ||
| return candidate && Is.func(candidate.handleMessage); | ||
| return candidate && (Is.func(candidate.handleMessage) || Is.func(candidate.handleIncomingMessage) || Is.func(candidate.handleOutgoingMessage)); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -708,7 +751,7 @@ export function createMessageConnection(messageReader: MessageReader, messageWri | |
| } else { | ||
| // If we have limited parallelism we resolve responses to avoid | ||
| // dead locks. | ||
| handleResponse(message); | ||
| handleUnqueuedMessage(message, handleMessage); | ||
| } | ||
| } else { | ||
| queue.set(createNotificationQueueKey(), message); | ||
|
|
@@ -735,7 +778,11 @@ export function createMessageConnection(messageReader: MessageReader, messageWri | |
| try { | ||
| inFlight++; | ||
| const messageStrategy = options?.messageStrategy; | ||
| if (MessageStrategy.is(messageStrategy)) { | ||
| const handleIncoming = incomingHook(); | ||
| if (handleIncoming !== undefined) { | ||
| // A queued cancellation already went through the hook when it was read. | ||
| result = isCancelNotification(message) ? handleMessage(message) : handleIncoming(message, handleMessage); | ||
| } else if (messageStrategy !== undefined && Is.func(messageStrategy.handleMessage)) { | ||
| result = messageStrategy.handleMessage(message, handleMessage); | ||
| } else { | ||
| result = handleMessage(message); | ||
|
|
@@ -775,11 +822,78 @@ export function createMessageConnection(messageReader: MessageReader, messageWri | |
| } | ||
| } | ||
|
|
||
| function incomingHook(): MessageStrategy['handleIncomingMessage'] { | ||
| const messageStrategy = options?.messageStrategy; | ||
| if (messageStrategy === undefined || !Is.func(messageStrategy.handleIncomingMessage)) { | ||
| return undefined; | ||
| } | ||
| return (message, next) => messageStrategy.handleIncomingMessage!(message, next); | ||
| } | ||
|
|
||
| // Messages that skip the queue go through `handleIncomingMessage`. | ||
| // `handleMessage` keeps getting queued messages only. Errors are logged, | ||
| // since the reader can call us from an event handler. | ||
| function handleUnqueuedMessage(message: Message, handle: (message: Message) => NotificationResult): void { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: the check "does the strategy have
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 8575cc1. |
||
| const handleIncoming = incomingHook(); | ||
| const logError = (error: any) => logger.error(`Processing message failed: ${error}`); | ||
| try { | ||
| const result = handleIncoming !== undefined ? handleIncoming(message, handle) : handle(message); | ||
| if (result instanceof Promise) { | ||
| result.catch(logError); | ||
| } | ||
| } catch (error) { | ||
| logError(error); | ||
| } | ||
| } | ||
|
|
||
| function writeMessage(message: Message): Promise<void> { | ||
| const messageStrategy = options?.messageStrategy; | ||
| if (messageStrategy === undefined || !Is.func(messageStrategy.handleOutgoingMessage)) { | ||
| return messageWriter.write(message); | ||
| } | ||
| let written: Promise<void> | undefined; | ||
| let returned = false; | ||
| const next = (outgoing: Message): void => { | ||
| if (written !== undefined || returned) { | ||
| logger.warn(`handleOutgoingMessage must call next exactly once before it returns.`); | ||
| return; | ||
| } | ||
| written = messageWriter.write(outgoing); | ||
| }; | ||
| try { | ||
| messageStrategy.handleOutgoingMessage(message, next); | ||
| } catch (error) { | ||
| if (written === undefined) { | ||
| return Promise.reject(error); | ||
| } | ||
| // The message was written. Failing here would make handleRequest send | ||
| // a second response for the same request, so the error is logged. | ||
| logger.error(`handleOutgoingMessage failed after the message was written: ${error}`); | ||
| } finally { | ||
| returned = true; | ||
| } | ||
| return written ?? Promise.reject(new Error(`handleOutgoingMessage did not call next, so the message was not written.`)); | ||
| } | ||
|
|
||
| const callback: DataCallback = (message) => { | ||
| // The hook gets cancellations as soon as they are read and can drop them, | ||
| // so nothing is canceled or remembered before it calls next. | ||
| if (isCancelNotification(message) && incomingHook() !== undefined) { | ||
| handleUnqueuedMessage(message, receiveMessage); | ||
| } else { | ||
| receiveMessage(message); | ||
| } | ||
| }; | ||
|
|
||
| function isCancelNotification(message: Message): message is NotificationMessage { | ||
| return Message.isNotification(message) && message.method === CancelNotification.type.method; | ||
| } | ||
|
|
||
| function receiveMessage(message: Message): void { | ||
| try { | ||
| // We have received a cancellation message. Check if the message is still in the queue | ||
| // and cancel it if allowed to do so. | ||
| if (Message.isNotification(message) && message.method === CancelNotification.type.method) { | ||
| if (isCancelNotification(message)) { | ||
| const cancelId = (message.params as CancelParams).id; | ||
| const key = createRequestQueueKey(cancelId); | ||
| const toCancel = messageQueue.get(key); | ||
|
|
@@ -791,7 +905,7 @@ export function createMessageConnection(messageReader: MessageReader, messageWri | |
| requestTokens.delete(cancelId); | ||
| response.id = toCancel.id; | ||
| traceSendingResponse(response, message.method, Date.now()); | ||
| messageWriter.write(response).catch(() => logger.error(`Sending response for canceled message failed.`)); | ||
| writeMessage(response).catch(() => logger.error(`Sending response for canceled message failed.`)); | ||
| return; | ||
| } | ||
| } | ||
|
|
@@ -811,7 +925,7 @@ export function createMessageConnection(messageReader: MessageReader, messageWri | |
| } finally { | ||
| triggerMessageQueue(); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| async function handleRequest(requestMessage: RequestMessage): Promise<void> { | ||
| if (isDisposed()) { | ||
|
|
@@ -831,7 +945,7 @@ export function createMessageConnection(messageReader: MessageReader, messageWri | |
| message.result = resultOrError === undefined ? null : resultOrError; | ||
| } | ||
| traceSendingResponse(message, method, startTime); | ||
| return messageWriter.write(message); | ||
| return writeMessage(message); | ||
| } | ||
| function replyError(error: ResponseError<any>, method: string, startTime: number): Promise<void> { | ||
| const message: ResponseMessage = { | ||
|
|
@@ -840,7 +954,7 @@ export function createMessageConnection(messageReader: MessageReader, messageWri | |
| error: error.toJson() | ||
| }; | ||
| traceSendingResponse(message, method, startTime); | ||
| return messageWriter.write(message); | ||
| return writeMessage(message); | ||
| } | ||
| traceReceivedRequest(requestMessage); | ||
|
|
||
|
|
@@ -1313,7 +1427,7 @@ export function createMessageConnection(messageReader: MessageReader, messageWri | |
| params: messageParams | ||
| }; | ||
| traceSendingNotification(notificationMessage); | ||
| return messageWriter.write(notificationMessage).catch((error) => { | ||
| return writeMessage(notificationMessage).catch((error) => { | ||
| logger.error(`Sending notification failed.`); | ||
| throw error; | ||
| }); | ||
|
|
@@ -1460,7 +1574,7 @@ export function createMessageConnection(messageReader: MessageReader, messageWri | |
| const responsePromise: ResponsePromise | null = { method: method, timerStart: Date.now(), resolve: resolveWithCleanup, reject: rejectWithCleanup }; | ||
| try { | ||
| responsePromises.set(id, responsePromise); | ||
| await messageWriter.write(requestMessage); | ||
| await writeMessage(requestMessage); | ||
| if (tokenWasCancelled) { | ||
| sendCancellation(connection, id); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two things about
handleMessage:strategy.handleMessage(...)directly whenstrictNullChecksis on. If that is acceptable for this release, please add a note to the changelog. Otherwise we could leaveMessageStrategyas it is and add the new methods in a new interface that extends it.handleMessageandhandleIncomingMessagenow mean almost the same thing; the only difference is whether a message that skips the queue is passed in. Could we markhandleMessageas@deprecatedin favor ofhandleIncomingMessage? Then the API would have one incoming hook and one outgoing hook.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 8575cc1.
handleMessageis now marked@deprecatedin favor ofhandleIncomingMessage. I kept it optional and added a note under Compatibility in the history of the root README, in a newNextsection. If you would rather avoid the typing change in this release, I can move the new methods to a separate interface.