Skip to content

Commit

Permalink
lintfix
Browse files Browse the repository at this point in the history
  • Loading branch information
amydevs committed Sep 23, 2023
1 parent 27010f9 commit d107589
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 87 deletions.
5 changes: 1 addition & 4 deletions src/WebSocketClient.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import type {
Host,
Port,
TLSVerifyCallback,
WebSocketClientConfigInput,
WebSocketConfig,
} from './types';
import type { ContextTimed, ContextTimedInput } from '@matrixai/contexts';
import { AbstractEvent } from '@matrixai/events';
import { createDestroy } from '@matrixai/async-init';
import Logger from '@matrixai/logger';
import WebSocket from 'ws';
import { Validator } from 'ip-num';
import { EventAll, EventDefault } from '@matrixai/events';
import { EventAll } from '@matrixai/events';
import { context, timedCancellable } from '@matrixai/contexts/dist/decorators';
import * as errors from './errors';
import WebSocketConnection from './WebSocketConnection';
Expand Down
91 changes: 38 additions & 53 deletions src/WebSocketConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@ import type {
ConnectionMetadata,
Host,
Port,
RemoteInfo,
StreamCodeToReason,
StreamReasonToCode,
WebSocketConfig,
} from './types';
import type { DetailedPeerCertificate, TLSSocket } from 'tls';
import type { TLSSocket } from 'tls';
import type { StreamId } from './message';
import { startStop } from '@matrixai/async-init';
import { Lock } from '@matrixai/async-locks';
import { context, timedCancellable } from '@matrixai/contexts/dist/decorators';
import Logger from '@matrixai/logger';
import * as ws from 'ws';
import { Timer } from '@matrixai/timer';
import { AbstractEvent, EventAll, EventDefault } from '@matrixai/events';
import { AbstractEvent, EventAll } from '@matrixai/events';
import { concatUInt8Array } from './message';
import WebSocketStream from './WebSocketStream';
import * as errors from './errors';
Expand Down Expand Up @@ -223,16 +222,13 @@ class WebSocketConnection {
const reason = "WebSocket received data received that wasn't binary";
this.dispatchEvent(
new events.EventWebSocketConnectionError({
detail: new errors.ErrorWebSocketConnectionLocal(
reason,
{
cause: new errors.ErrorWebSocketUndefinedBehaviour(),
data: {
errorCode: utils.ConnectionErrorCode.InternalServerError,
reason
}
detail: new errors.ErrorWebSocketConnectionLocal(reason, {
cause: new errors.ErrorWebSocketUndefinedBehaviour(),
data: {
errorCode: utils.ConnectionErrorCode.InternalServerError,
reason,
},
),
}),
}),
);
return;
Expand All @@ -248,19 +244,16 @@ class WebSocketConnection {
remainder = postStreamIdRemainder;
} catch (e) {
// TODO: domain specific error
const reason = "Parsing streamId failed"
const reason = 'Parsing streamId failed';
this.dispatchEvent(
new events.EventWebSocketConnectionError({
detail: new errors.ErrorWebSocketConnectionLocal(
reason,
{
cause: e,
data: {
errorCode: utils.ConnectionErrorCode.InternalServerError,
reason
}
detail: new errors.ErrorWebSocketConnectionLocal(reason, {
cause: e,
data: {
errorCode: utils.ConnectionErrorCode.InternalServerError,
reason,
},
),
}),
}),
);
return;
Expand Down Expand Up @@ -518,53 +511,45 @@ class WebSocketConnection {
let reason: string;
let errorCode: number;
switch (e.code) {
case "UNABLE_TO_VERIFY_LEAF_SIGNATURE":
case 'UNABLE_TO_VERIFY_LEAF_SIGNATURE':
errorCode = utils.ConnectionErrorCode.TLSHandshake;
reason = 'WebSocket could not open due to failure to verify a peer\'s TLS certificate';
e_ = new errors.ErrorWebSocketConnectionLocalTLS(
reason,
{
cause: e,
data: {
errorCode,
reason,
}
}
);
reason =
"WebSocket could not open due to failure to verify a peer's TLS certificate";
e_ = new errors.ErrorWebSocketConnectionLocalTLS(reason, {
cause: e,
data: {
errorCode,
reason,
},
});
break;
case "ECONNRESET":
case 'ECONNRESET':
reason = 'WebSocket could not open due to socket closure by peer';
errorCode = utils.ConnectionErrorCode.AbnormalClosure,
e_ = new errors.ErrorWebSocketConnectionPeer(
reason,
{
(errorCode = utils.ConnectionErrorCode.AbnormalClosure),
(e_ = new errors.ErrorWebSocketConnectionPeer(reason, {
cause: e,
data: {
errorCode,
reason
}
}
);
reason,
},
}));
break;
default:
reason = 'WebSocket could not open due to internal error';
errorCode = utils.ConnectionErrorCode.InternalServerError,
e_ = new errors.ErrorWebSocketConnectionLocal(
reason,
{
(errorCode = utils.ConnectionErrorCode.InternalServerError),
(e_ = new errors.ErrorWebSocketConnectionLocal(reason, {
cause: e,
data: {
errorCode,
reason
}
}
);
reason,
},
}));
break;
}
this.closeSocket(errorCode, reason);
this.dispatchEvent(
new events.EventWebSocketConnectionError({
detail: e_
detail: e_,
}),
);
};
Expand Down Expand Up @@ -600,7 +585,7 @@ class WebSocketConnection {
const errorCode = utils.ConnectionErrorCode.TLSHandshake;
const reason =
'Failed connection due to custom verification callback';
// request.destroy() will make the socket dispatch a 'close' event,
// Request.destroy() will make the socket dispatch a 'close' event,
// so I'm setting socketLocallyClosed to true, as that is what is happening.
this.socketLocallyClosed = true;
request.destroy(e);
Expand Down
11 changes: 2 additions & 9 deletions src/WebSocketServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,18 @@ import type tls from 'tls';
import type {
Host,
Port,
PromiseDeconstructed,
StreamCodeToReason,
StreamReasonToCode,
WebSocketConfig,
WebSocketServerConfigInput,
} from './types';
import type { EventAll } from '@matrixai/events';
import type { DetailedPeerCertificate, TLSSocket } from 'tls';
import type { TLSSocket } from 'tls';
import https from 'https';
import { AbstractEvent } from '@matrixai/events';
import {
StartStop,
status,
running,
ready,
} from '@matrixai/async-init/dist/StartStop';
import { StartStop, running, ready } from '@matrixai/async-init/dist/StartStop';
import Logger from '@matrixai/logger';
import * as ws from 'ws';
import { EventDefault } from '@matrixai/events';
import * as errors from './errors';
import * as events from './events';
import * as utils from './utils';
Expand Down
1 change: 0 additions & 1 deletion src/WebSocketStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
ready,
running,
status,
initLock,
} from '@matrixai/async-init/dist/StartStop';
import Logger from '@matrixai/logger';
import { generateStreamId } from './message';
Expand Down
13 changes: 8 additions & 5 deletions src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AbstractError, POJO } from '@matrixai/errors';
import { ConnectionError } from './types';
import type { POJO } from '@matrixai/errors';
import type { ConnectionError } from './types';
import { AbstractError } from '@matrixai/errors';

class ErrorWebSocket<T> extends AbstractError<T> {
static description = 'WebSocket error';
Expand Down Expand Up @@ -84,13 +85,15 @@ class ErrorWebSocketConnectionLocal<T> extends ErrorWebSocketConnection<T> {
timestamp?: Date;
data: POJO & ConnectionError;
cause?: T;
}
},
) {
super(message, options);
}
}

class ErrorWebSocketConnectionLocalTLS<T> extends ErrorWebSocketConnectionLocal<T> {
class ErrorWebSocketConnectionLocalTLS<
T,
> extends ErrorWebSocketConnectionLocal<T> {
static description = 'WebSocket Connection local TLS error';
}

Expand All @@ -103,7 +106,7 @@ class ErrorWebSocketConnectionPeer<T> extends ErrorWebSocketConnection<T> {
timestamp?: Date;
data: POJO & ConnectionError;
cause?: T;
}
},
) {
super(message, options);
}
Expand Down
4 changes: 1 addition & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type { DetailedPeerCertificate } from 'tls';

// Async

/**
Expand Down Expand Up @@ -185,5 +183,5 @@ export type {
WebSocketClientConfigInput,
WebSocketServerConfigInput,
Parsed,
ConnectionError
ConnectionError,
};
2 changes: 0 additions & 2 deletions tests/WebSocket.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { X509Certificate } from '@peculiar/x509';
import Logger, { formatting, LogLevel, StreamHandler } from '@matrixai/logger';
import { status } from '@matrixai/async-init';
import * as events from '@/events';
import WebSocketClient from '@/WebSocketClient';
import WebSocketServer from '@/WebSocketServer';
import * as utils from '@/utils';
import * as errors from '@/errors';
import * as testsUtils from './utils';

// Process.on('unhandledRejection', (reason) => {
Expand Down
21 changes: 13 additions & 8 deletions tests/WebSocketClient.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import type { KeyTypes } from './utils';
import type WebSocketConnection from '@/WebSocketConnection';
import { DetailedPeerCertificate } from 'tls';
import Logger, { formatting, LogLevel, StreamHandler } from '@matrixai/logger';
import { X509Certificate } from '@peculiar/x509';
import { fc, testProp } from '@fast-check/jest';
import { promise, pemToDER, ConnectionErrorCode } from '@/utils';
import { promise, pemToDER } from '@/utils';
import * as events from '@/events';
import * as errors from '@/errors';
import WebSocketClient from '@/WebSocketClient';
Expand Down Expand Up @@ -393,7 +390,10 @@ describe(WebSocketClient.name, () => {
verifyPeer: true,
},
}),
).rejects.toHaveProperty('name', errors.ErrorWebSocketConnectionLocalTLS.name);
).rejects.toHaveProperty(
'name',
errors.ErrorWebSocketConnectionLocalTLS.name,
);
await server.stop();
});
test('graceful failure verifying client', async () => {
Expand Down Expand Up @@ -422,7 +422,10 @@ describe(WebSocketClient.name, () => {
verifyPeer: false,
},
}),
).rejects.toHaveProperty('name', errors.ErrorWebSocketConnectionPeer.name);
).rejects.toHaveProperty(
'name',
errors.ErrorWebSocketConnectionPeer.name,
);

await server.stop();
});
Expand All @@ -440,7 +443,6 @@ describe(WebSocketClient.name, () => {
await server.start({
host: localhost,
});
server.addEventListener(errors.ErrorWebSocketConnectionLocalTLS.name, (e) => console.log(e))
// Connection should fail
await expect(
WebSocketClient.createWebSocketClient({
Expand All @@ -453,7 +455,10 @@ describe(WebSocketClient.name, () => {
verifyPeer: true,
},
}),
).rejects.toHaveProperty('name', errors.ErrorWebSocketConnectionLocalTLS.name);
).rejects.toHaveProperty(
'name',
errors.ErrorWebSocketConnectionLocalTLS.name,
);

await server.stop();
});
Expand Down
2 changes: 0 additions & 2 deletions tests/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import type { X509Certificate } from '@peculiar/x509';
import type WebSocketClient from '@/WebSocketClient';
import type WebSocketServer from '@/WebSocketServer';
import type WebSocketStream from '@/WebSocketStream';
import type { StreamCodeToReason, StreamReasonToCode } from '@/types';
import * as peculiarWebcrypto from '@peculiar/webcrypto';
Expand Down

0 comments on commit d107589

Please sign in to comment.