-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to intercept websocket upgrades and raise events.
Signed-off-by: [email protected]
- Loading branch information
1 parent
3415be0
commit 2eb4319
Showing
3 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2021 MayStreet Inc. and others | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the Eclipse | ||
* Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
* with the GNU Classpath Exception which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
|
||
import { inject, injectable, named } from 'inversify'; | ||
import { ContributionProvider, MaybePromise } from '../../common'; | ||
|
||
import * as http from 'http'; | ||
import * as ws from 'ws'; | ||
|
||
/** | ||
* Bind components to this symbol to subscribe to WebSocket events. | ||
*/ | ||
export const MessagingListenerContribution = Symbol('MessagingListenerContribution'); | ||
export interface MessagingListenerContribution { | ||
/** | ||
* Function invoked when a HTTP connection is upgraded to a websocket. | ||
* | ||
* @param request The HTTP connection upgrade request received by the server. | ||
* @param socket The WebSocket that the connection was upgraded to. | ||
*/ | ||
onDidWebSocketUpgrade(request: http.IncomingMessage, socket: ws): MaybePromise<void>; | ||
} | ||
|
||
/** | ||
* Handler of Theia messaging system events, dispatching to MessagingListenerContribution instances. | ||
*/ | ||
@injectable() | ||
export class MessagingListener { | ||
|
||
@inject(ContributionProvider) @named(MessagingListenerContribution) | ||
protected readonly messagingListenerContributions: ContributionProvider<MessagingListenerContribution>; | ||
|
||
/** | ||
* Notify all the subscribed `MessagingListenerContribution`s that the Websocket was upgraded. | ||
*/ | ||
async onDidWebSocketUpgrade(request: http.IncomingMessage, socket: ws): Promise<void> { | ||
await Promise.all(Array.from(this.messagingListenerContributions.getContributions(), async messagingListener => messagingListener.onDidWebSocketUpgrade(request, socket))); | ||
} | ||
} |