Skip to content
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

feat/websockets #38

Merged
merged 1 commit into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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