-
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.
get rid of direct usages of web sockets on the backend
Signed-off-by: Anton Kosiakov <anton.kosyakov@typefox.io>
- Loading branch information
Showing
14 changed files
with
224 additions
and
236 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 was deleted.
Oops, something went wrong.
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
131 changes: 131 additions & 0 deletions
131
packages/core/src/node/messaging/messaging-contribution.ts
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,131 @@ | ||
/* | ||
* Copyright (C) 2018 TypeFox and others. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
|
||
import * as ws from 'ws'; | ||
import * as url from 'url'; | ||
import * as http from 'http'; | ||
import * as https from 'https'; | ||
import { injectable, inject, named, postConstruct } from "inversify"; | ||
import { MessageConnection } from 'vscode-jsonrpc'; | ||
import { createWebSocketConnection } from 'vscode-ws-jsonrpc/lib/socket/connection'; | ||
import { IConnection } from 'vscode-ws-jsonrpc/lib/server/connection'; | ||
import * as launch from 'vscode-ws-jsonrpc/lib/server/launch'; | ||
import { IWebSocket } from 'vscode-ws-jsonrpc/lib/socket/socket'; | ||
import { ContributionProvider, ConnectionHandler } from '../../common'; | ||
import { BackendApplicationContribution } from "../backend-application"; | ||
import { MessagingService } from './messaging-service'; | ||
import { ConsoleLogger } from "./logger"; | ||
|
||
import Route = require('route-parser'); | ||
|
||
@injectable() | ||
export class MessagingContribution implements BackendApplicationContribution, MessagingService { | ||
|
||
@inject(ContributionProvider) @named(ConnectionHandler) | ||
protected readonly handlers: ContributionProvider<ConnectionHandler>; | ||
|
||
@inject(ContributionProvider) @named(MessagingService.Contribution) | ||
protected readonly contributions: ContributionProvider<MessagingService.Contribution>; | ||
|
||
@postConstruct() | ||
protected init(): void { | ||
for (const contribution of this.contributions.getContributions()) { | ||
contribution.configure(this); | ||
} | ||
for (const handler of this.handlers.getContributions()) { | ||
this.listen(handler.path, (params, connection) => | ||
handler.onConnection(connection) | ||
); | ||
} | ||
} | ||
|
||
listen(spec: string, callback: (params: MessagingService.Params, connection: MessageConnection) => void): void { | ||
return this.pushAcceptor(spec, (params, socket) => { | ||
const connection = createWebSocketConnection(this.toIWebSocket(socket), new ConsoleLogger()); | ||
callback(params, connection); | ||
}); | ||
} | ||
|
||
forward(spec: string, callback: (params: MessagingService.Params, connection: IConnection) => void): void { | ||
return this.pushAcceptor(spec, (params, socket) => { | ||
const connection = launch.createWebSocketConnection(this.toIWebSocket(socket)); | ||
callback(params, connection); | ||
}); | ||
} | ||
|
||
protected readonly acceptors: ((path: string, socket: ws) => boolean)[] = []; | ||
protected pushAcceptor(spec: string, callback: (params: MessagingService.Params, socket: ws) => void): void { | ||
const route = new Route(spec); | ||
this.acceptors.push((path, socket) => { | ||
const params = route.match(path); | ||
if (!params) { | ||
return false; | ||
} | ||
callback(params, socket); | ||
return true; | ||
}); | ||
} | ||
protected dispatch(socket: ws, request: http.IncomingMessage): void { | ||
const pathname = request.url && url.parse(request.url).pathname; | ||
if (!pathname) { | ||
return; | ||
} | ||
for (const acceptor of this.acceptors) { | ||
try { | ||
if (acceptor(pathname, socket)) { | ||
return; | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
} | ||
} | ||
|
||
protected checkAliveTimeout = 30000; | ||
onStart(server: http.Server | https.Server): void { | ||
const wss = new ws.Server({ | ||
server, | ||
perMessageDeflate: false | ||
}); | ||
interface CheckAliveWS extends ws { | ||
alive: boolean; | ||
} | ||
wss.on('connection', (socket: CheckAliveWS, request) => { | ||
socket.alive = true; | ||
socket.on('pong', () => socket.alive = true); | ||
this.dispatch(socket, request); | ||
}); | ||
setInterval(() => { | ||
wss.clients.forEach((socket: CheckAliveWS) => { | ||
if (socket.alive === false) { | ||
return socket.terminate(); | ||
} | ||
socket.alive = false; | ||
socket.ping(); | ||
}); | ||
}, this.checkAliveTimeout); | ||
} | ||
|
||
protected toIWebSocket(webSocket: ws): IWebSocket { | ||
return <IWebSocket>{ | ||
send: content => webSocket.send(content, error => { | ||
if (error) { | ||
console.log(error); | ||
} | ||
}), | ||
onMessage: cb => webSocket.on('message', cb), | ||
onError: cb => webSocket.on('error', cb), | ||
onClose: cb => webSocket.on('close', cb), | ||
dispose: () => { | ||
if (webSocket.readyState < ws.CLOSING) { | ||
webSocket.close(); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
} |
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,24 @@ | ||
|
||
/* | ||
* Copyright (C) 2018 TypeFox and others. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
*/ | ||
|
||
import { MessageConnection } from "vscode-jsonrpc"; | ||
import { IConnection } from "vscode-ws-jsonrpc/lib/server/connection"; | ||
|
||
export interface MessagingService { | ||
listen(path: string, callback: (params: MessagingService.Params, connection: MessageConnection) => void): void; | ||
forward(path: string, callback: (params: MessagingService.Params, connection: IConnection) => void): void; | ||
} | ||
export namespace MessagingService { | ||
export interface Params { | ||
[name: string]: string | ||
} | ||
export const Contribution = Symbol('MessagingService.Contribution'); | ||
export interface Contribution { | ||
configure(service: MessagingService): void; | ||
} | ||
} |
Oops, something went wrong.