Skip to content

Commit

Permalink
feat: IPv6 support
Browse files Browse the repository at this point in the history
[ci skip]
  • Loading branch information
tegefaulkes committed Feb 23, 2023
1 parent ec6bc83 commit e16a3b7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/clientRPC/ClientClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Logger from '@matrixai/logger';
import WebSocket from 'ws';
import { PromiseCancellable } from '@matrixai/async-cancellable';
import { Timer } from '@matrixai/timer';
import { Validator } from 'ip-num';
import * as clientRpcUtils from './utils';
import { promise } from '../utils';

Expand Down Expand Up @@ -49,18 +50,28 @@ class ClientClient {
return clientClient;
}

protected host: string;

protected activeConnections: Set<PromiseCancellable<void>> = new Set();

constructor(
protected logger: Logger,
protected host: string,
host: string,
protected port: number,
protected maxReadableStreamBytes: number,
protected expectedNodeIds: Array<NodeId>,
protected connectionTimeout: number | undefined,
protected pingInterval: number,
protected pingTimeout: number,
) {}
) {
if (Validator.isValidIPv4String(host)[0]) {
this.host = host;
} else if (Validator.isValidIPv6String(host)[0]) {
this.host = `[${host}]`;
} else {
throw Error('TMP Invalid host');
}
}

public async destroy(force: boolean = false) {
this.logger.info(`Destroying ${this.constructor.name}`);
Expand Down
35 changes: 35 additions & 0 deletions tests/clientRPC/clientRPC.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,41 @@ describe('ClientRPC', () => {
expect((await reader.read()).done).toBeTrue();
logger.info('ending');
});
test('makes a connection over IPv6', async () => {
clientServer = await ClientServer.createClientServer({
connectionCallback: (streamPair) => {
logger.info('inside callback');
void streamPair.readable
.pipeTo(streamPair.writable)
.catch(() => {})
.finally(() => loudLogger.info('STREAM HANDLING ENDED'));
},
basePath: dataDir,
tlsConfig,
host: '::1',
logger: loudLogger.getChild('server'),
});
logger.info(`Server started on port ${clientServer.port}`);
clientClient = await ClientClient.createClientClient({
host: '::1',
port: clientServer.port,
expectedNodeIds: [keyRing.getNodeId()],
logger: logger.getChild('clientClient'),
});
const websocket = await clientClient.startConnection();

const writer = websocket.writable.getWriter();
const reader = websocket.readable.getReader();
const message1 = Buffer.from('1request1');
await writer.write(message1);
expect((await reader.read()).value).toStrictEqual(message1);
const message2 = Buffer.from('1request2');
await writer.write(message2);
expect((await reader.read()).value).toStrictEqual(message2);
await writer.close();
expect((await reader.read()).done).toBeTrue();
logger.info('ending');
});
test('Handles a connection and closes before message', async () => {
clientServer = await ClientServer.createClientServer({
connectionCallback: (streamPair) => {
Expand Down

0 comments on commit e16a3b7

Please sign in to comment.