Skip to content

Commit

Permalink
Handle server errors better (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante authored Mar 26, 2023
1 parent 0536a69 commit e8fe783
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 15 deletions.
40 changes: 27 additions & 13 deletions source/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {type WebSocket} from 'ws';
import filenamify from 'filenamify';
import * as codelens from './codelens.js';
import {documents} from './state.js';
import {startServer, stopServer} from './server.js';
import {registerCommand} from './vscode.js';
import {Eaddrinuse, startServer, stopServer} from './server.js';
import {registerCommand, type Subscriptions} from './vscode.js';

/** When the browser sends new content, the editor should not detect this "change" event and echo it */
let updateFromBrowserInProgress = false;
Expand Down Expand Up @@ -152,9 +152,9 @@ async function onLocalSelection(event: vscode.TextEditorSelectionChangeEvent) {
field.socket.send(JSON.stringify({text: content, selections}));
}

function onConfigurationChange(event: vscode.ConfigurationChangeEvent) {
async function onConfigurationChange(event: vscode.ConfigurationChangeEvent) {
if (event.affectsConfiguration('ghostText.serverPort')) {
startServer(context.subscriptions, openConnection);
await startServer(context.subscriptions, openConnection);
}
}

Expand All @@ -174,15 +174,10 @@ async function onLocalEdit(event: vscode.TextDocumentChangeEvent) {
field.socket.send(JSON.stringify({text: content, selections}));
}

export function activate(_context: vscode.ExtensionContext) {
// Set global
context = _context;
const {subscriptions} = context;

function registerListeners(subscriptions: Subscriptions) {
const setup = [null, subscriptions] as const;
startServer(subscriptions, openConnection);
codelens.activate(subscriptions);

codelens.activate(subscriptions);
// Watch for changes to the HTTP port option
// This event is already debounced
vscode.workspace.onDidChangeConfiguration(onConfigurationChange, ...setup);
Expand All @@ -194,12 +189,31 @@ export function activate(_context: vscode.ExtensionContext) {
registerCommand(
'ghostText.startServer',
async () => {
startServer(subscriptions, openConnection);
await startServer(subscriptions, openConnection);
},
subscriptions,
);
}

export async function activate(_context: vscode.ExtensionContext) {
// Set global
context = _context;
const {subscriptions} = context;

// Listen to commands before starting the server
registerListeners(subscriptions);

try {
await startServer(subscriptions, openConnection);
} catch (error: unknown) {
if (error instanceof Eaddrinuse) {
return;
}

throw error;
}

context.subscriptions.push({
subscriptions.push({
dispose() {
documents.clear();
},
Expand Down
24 changes: 22 additions & 2 deletions source/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,33 @@ async function pingResponder(_: unknown, response: http.ServerResponse) {
);
}

export function startServer(
export class Eaddrinuse extends Error {}

async function listen(server: http.Server) {
const port = getPort();
return new Promise((resolve, reject) => {
server
.listen(getPort())
.once('listening', resolve)
.once('error', (error) => {
if ((error as any).code === 'EADDRINUSE') {
reject(new Eaddrinuse(`The port ${port} is already in use`));
} else {
reject(error);
}
});
});
}

export async function startServer(
subscriptions: Subscriptions,
onConnection: (socket: WebSocket, request: http.IncomingMessage) => void,
) {
console.log('GhostText: Server starting');
server?.close();
server = http.createServer(pingResponder).listen(getPort());
server = http.createServer(pingResponder);

await listen(server);
const ws = new Server({server});
ws.on('connection', onConnection);
console.log('GhostText: Server started');
Expand Down

0 comments on commit e8fe783

Please sign in to comment.