Skip to content

Commit

Permalink
fix: fix encapsulation and jests
Browse files Browse the repository at this point in the history
lintfix
tested
  • Loading branch information
addievo committed Oct 17, 2023
1 parent 3b195c8 commit 4079fdf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 34 deletions.
22 changes: 12 additions & 10 deletions src/PolykeyClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { FileSystem } from './types';
import type { StreamFactory } from '@matrixai/rpc';
import type { WebSocketConfig } from '@matrixai/ws/dist/types';
import path from 'path';
import Logger from '@matrixai/logger';
import { CreateDestroyStartStop } from '@matrixai/async-init/dist/CreateDestroyStartStop';
Expand Down Expand Up @@ -36,15 +37,19 @@ interface PolykeyClient extends CreateDestroyStartStop {}
class PolykeyClient {
static async createPolykeyClient({
nodePath = config.defaultsUser.nodePath,
streamFactory,
wsPort,
wsHost,
wsConfig,
streamKeepAliveTimeoutTime,
parserBufferByteLimit,
fs = require('fs'),
logger = new Logger(this.name),
fresh = false,
}: {
nodePath?: string;
streamFactory: StreamFactory;
wsPort: number;
wsHost: string;
wsConfig?: Partial<WebSocketConfig>;
streamKeepAliveTimeoutTime?: number;
parserBufferByteLimit?: number;
fs?: FileSystem;
Expand All @@ -63,13 +68,14 @@ class PolykeyClient {
fresh,
});
const ws = await WebSocketClient.createWebSocketClient({
host: config.defaultsUser.clientServiceHost,
port: config.defaultsUser.clientServicePort,
host: wsHost,
port: wsPort,
config: wsConfig,
logger: logger.getChild('WebSocketClient'),
});
const rpcClientClient = new RPCClient({
manifest: clientClientManifest,
streamFactory,
streamFactory: () => ws.connection.newStream(),
middlewareFactory: rpcMiddleware.defaultClientMiddlewareWrapper(
clientMiddleware.middlewareClient(session),
parserBufferByteLimit,
Expand Down Expand Up @@ -99,10 +105,6 @@ class PolykeyClient {
protected fs: FileSystem;
protected logger: Logger;

public setWs(ws: WebSocketClient){
this.ws = ws;
}

constructor({
nodePath,
rpcClientClient,
Expand Down Expand Up @@ -136,7 +138,7 @@ class PolykeyClient {

public async destroy() {
this.logger.info(`Destroying ${this.constructor.name}`);
await this.ws.destroy({force: true});
await this.ws.destroy({ force: true });
await this.session.destroy();
this.logger.info(`Destroyed ${this.constructor.name}`);
}
Expand Down
41 changes: 17 additions & 24 deletions tests/PolykeyClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,14 @@ describe('PolykeyClient', () => {
});
await session.writeToken('dummy' as SessionToken);
// Using fresh: true means that any token would be destroyed
const webSocketClient = await WebSocketClient.createWebSocketClient({
config: {
verifyPeer: false,
},
host: pkAgent.clientServiceHost,
port: pkAgent.clientServicePort,
logger,
});
const wsconfig = {
verifyPeer: false,
};
const pkClient = await PolykeyClient.createPolykeyClient({
streamFactory: () => webSocketClient.connection.newStream(),
nodePath,
wsHost: pkAgent.clientServiceHost,
wsPort: pkAgent.clientServicePort,
wsConfig: wsconfig,
fs,
logger,
fresh: true,
Expand All @@ -77,26 +74,22 @@ describe('PolykeyClient', () => {
expect(await session.readToken()).toBeDefined();
await pkClient.destroy();
expect(await session.readToken()).toBeUndefined();
await webSocketClient.destroy({ force: true });
});
test('end to end with authentication logic', async () => {
const webSocketClient = await WebSocketClient.createWebSocketClient({
config: {
verifyPeer: true,
verifyCallback: async (certs) => {
await clientUtils.verifyServerCertificateChain(
[pkAgent.keyRing.getNodeId()],
certs,
);
},
const wsconfig = {
verifyPeer: true,
verifyCallback: async (certs) => {
await clientUtils.verifyServerCertificateChain(
[pkAgent.keyRing.getNodeId()],
certs,
);
},
host: pkAgent.clientServiceHost,
port: pkAgent.clientServicePort,
logger: logger.getChild(WebSocketClient.name),
});
};
const pkClient = await PolykeyClient.createPolykeyClient({
streamFactory: () => webSocketClient.connection.newStream(),
nodePath,
wsPort: pkAgent.clientServicePort,
wsHost: pkAgent.clientServiceHost,
wsConfig: wsconfig,
fs,
logger: logger.getChild(PolykeyClient.name),
fresh: true,
Expand Down

0 comments on commit 4079fdf

Please sign in to comment.