Skip to content

Commit

Permalink
Merge pull request #38 from comake/feat/websockets
Browse files Browse the repository at this point in the history
feat/websockets
  • Loading branch information
adlerfaulkner authored Jul 18, 2023
2 parents ed2f172 + 350062c commit 8f0843d
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/server/configurator/WebSocketServerConfigurator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export class WebSocketServerConfigurator extends ServerConfigurator {
}

public async handle(server: Server): Promise<void> {
// Create WebSocket server
const webSocketServer = new WebSocketServer({ noServer: true });
server.on('upgrade', (upgradeRequest: IncomingMessage, socket: Socket, head: Buffer): void => {
webSocketServer.handleUpgrade(upgradeRequest, socket, head, async(webSocket: WebSocket): Promise<void> => {
Expand Down
63 changes: 63 additions & 0 deletions test/integration/WebSockets.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { WebSocket } from 'ws';
import { WebSocketHandler } from '../../src/http/handler/WebSocketHandler';
import type { WebSocketHandlerInput } from '../../src/http/handler/WebSocketHandler';
import type { App } from '../../src/init/App';
import { getPort } from '../util/Util';
import {
getDefaultVariables,
getTestConfigPath,
instantiateFromConfig,
} from './Config';

const port = getPort('WebSockets');
const baseUrl = `http://localhost:${port}`;
const message = 'Hello there!';

class SimpleWebSocketHandler extends WebSocketHandler {
public async handle(input: WebSocketHandlerInput): Promise<void> {
input.webSocket.send(message);
}
}

describe('A http server with websocket endpoint handlers', (): void => {
let app: App;

beforeAll(async(): Promise<void> => {
const instances = await instantiateFromConfig(
'urn:solid-on-rails:test:Instances',
getTestConfigPath('websocket-server.json'),
{
...getDefaultVariables(port, baseUrl),
'urn:solid-on-rails:default:ExampleWebSocketHandler': new SimpleWebSocketHandler(),
},
) as Record<string, any>;
({ app } = instances);
await app.start();
});

afterAll(async(): Promise<void> => {
await app.stop();
});

it('handles websocket connections.', async(): Promise<void> => {
let parsedMessage: string | undefined;
await new Promise<void>((resolve, reject): void => {
const webSocket = new WebSocket(`ws://localhost:${port}/websocket-path`);
webSocket.on('message', (data): void => {
if (typeof data === 'string') {
parsedMessage = data;
} else if (data instanceof Buffer) {
parsedMessage = data.toString();
}
webSocket.close();
resolve();
});
webSocket.on('error', (error): void => {
reject(error);
});
});

expect(parsedMessage).toEqual(message);
});
});
46 changes: 46 additions & 0 deletions test/integration/config/websocket-server.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"@context": "https://linkedsoftwaredependencies.org/bundles/npm/@comake/solid-on-rails/^0.0.0/components/context.jsonld",
"import": [
"files-sor:config/app/main/default.json",
"files-sor:config/app/initialize/default.json",
"files-sor:config/app/finalize/default.json",
"files-sor:config/app/variables/default.json",
"files-sor:config/app/path/default.json",
"files-sor:config/http/static/default.json",
"files-sor:config/http/error-handler.json",
"files-sor:config/server/server-factory/default.json",
"files-sor:config/storage/key-value/memory.json",
"files-sor:config/util/variables/default.json",
"files-sor:config/util/logging/winston.json"
],
"@graph": [
{
"comment": "Attempt to resolve requests first as static assets, then as normal routes.",
"@id": "urn:solid-on-rails:default:HttpHandler",
"@type": "SequenceHandler",
"handlers": [
{ "@id": "urn:solid-on-rails:default:StaticAssetHandler" }
]
},
{
"@id": "urn:solid-on-rails:default:WebSocketHandler",
"WaterfallHandler:_handlers": [
{ "@id": "urn:solid-on-rails:default:ExampleWebSocketHandler" }
]
},
{
"@id": "urn:solid-on-rails:default:ExampleWebSocketHandler",
"@type": "Variable"
},
{
"@id": "urn:solid-on-rails:test:Instances",
"@type": "RecordObject",
"RecordObject:_record": [
{
"RecordObject:_record_key": "app",
"RecordObject:_record_value": { "@id": "urn:solid-on-rails:default:App" }
}
]
}
]
}
1 change: 1 addition & 0 deletions test/util/Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const portNames = [
'Job',
'Middleware',
'Migration',
'WebSockets',
// Unit
'BaseServerFactory',
] as const;
Expand Down

0 comments on commit 8f0843d

Please sign in to comment.