Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
shinyoshiaki committed Jan 1, 2024
1 parent 0660f08 commit d71258a
Show file tree
Hide file tree
Showing 73 changed files with 759 additions and 1,658 deletions.
6 changes: 4 additions & 2 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"noParameterAssign": "off",
"useSingleVarDeclarator": "off",
"useDefaultParameterLast": "off",
"noVar": "off"
"noVar": "off",
"noInferrableTypes": "off"
},
"complexity": {
"noStaticOnlyClass": "off",
Expand All @@ -33,7 +34,8 @@
"noDoubleEquals": "off",
"noFallthroughSwitchClause": "off",
"noAssignInExpressions": "off",
"noRedeclare": "off"
"noRedeclare": "off",
"noConfusingVoidType": "off"
},
"correctness": {
"noSwitchDeclarations": "error"
Expand Down
2 changes: 1 addition & 1 deletion packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"scripts": {
"build": "rm -rf lib && npm run format && tsc -p ./tsconfig.production.json",
"deploy": "npm run build && npm publish",
"format": "npx @biomejs/biome check --apply src/**/*ts",
"format": "npx @biomejs/biome check --apply src",
"type": "tsc --noEmit --project ./tsconfig.json",
"unused": "organize-imports-cli src/**/*.ts"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/network.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createSocket, SocketType } from "dgram";
import { SocketType, createSocket } from "dgram";

export type InterfaceAddresses = {
[K in SocketType]?: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/dtls/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"client": "DEBUG=werift* ts-node-dev --files --project tsconfig.json examples/client.ts",
"dep": "depcheck",
"deploy": "npm run build && npm publish",
"format": "npx @biomejs/biome check --apply src/**/*ts tests/**/*ts",
"format": "npx @biomejs/biome check --apply src tests",
"server": "DEBUG=werift* ts-node-dev --files --project tsconfig.json examples/server.ts",
"test": "npm run type && jest --forceExit",
"type": "tsc --noEmit -p .",
Expand Down
93 changes: 44 additions & 49 deletions packages/dtls/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,60 +35,55 @@ export class DtlsClient extends DtlsSocket {
for (const handshake of assembled) {
switch (handshake.msg_type) {
// flight2
case HandshakeType.hello_verify_request_3:
{
const verifyReq = ServerHelloVerifyRequest.deSerialize(
handshake.fragment,
);
await new Flight3(this.transport, this.dtls).exec(verifyReq);
}
break;
case HandshakeType.hello_verify_request_3: {
const verifyReq = ServerHelloVerifyRequest.deSerialize(
handshake.fragment,
);
await new Flight3(this.transport, this.dtls).exec(verifyReq);
}
break;
// flight 4
case HandshakeType.server_hello_2:
{
if (this.connected) return;
this.flight5 = new Flight5(
this.transport,
this.dtls,
this.cipher,
this.srtp,
);
this.flight5.handleHandshake(handshake);
}
break;
case HandshakeType.server_hello_2: {
if (this.connected) return;
this.flight5 = new Flight5(
this.transport,
this.dtls,
this.cipher,
this.srtp,
);
this.flight5.handleHandshake(handshake);
}
break;
case HandshakeType.certificate_11:
case HandshakeType.server_key_exchange_12:
case HandshakeType.certificate_request_13:
{
await this.waitForReady(() => !!this.flight5);
this.flight5?.handleHandshake(handshake);
}
break;
case HandshakeType.server_hello_done_14:
{
await this.waitForReady(() => !!this.flight5);
this.flight5?.handleHandshake(handshake);
case HandshakeType.certificate_request_13: {
await this.waitForReady(() => !!this.flight5);
this.flight5?.handleHandshake(handshake);
}
break;
case HandshakeType.server_hello_done_14: {
await this.waitForReady(() => !!this.flight5);
this.flight5?.handleHandshake(handshake);

const targets = [
11,
12,
this.options.certificateRequest && 13,
].filter((n): n is number => typeof n === "number");
await this.waitForReady(() =>
this.dtls.checkHandshakesExist(targets),
);
await this.flight5?.exec();
}
break;
const targets = [
11,
12,
this.options.certificateRequest && 13,
].filter((n): n is number => typeof n === "number");
await this.waitForReady(() =>
this.dtls.checkHandshakesExist(targets),
);
await this.flight5?.exec();
}
break;
// flight 6
case HandshakeType.finished_20:
{
this.dtls.flight = 7;
this.connected = true;
this.onConnect.execute();
log(this.dtls.sessionId, "dtls connected");
}
break;
case HandshakeType.finished_20: {
this.dtls.flight = 7;
this.connected = true;
this.onConnect.execute();
log(this.dtls.sessionId, "dtls connected");
}
break;
}
}
};
Expand Down
5 changes: 3 additions & 2 deletions packages/dtls/src/flight/client/flight5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ handlers[HandshakeType.server_hello_2] =
if (message.extensions) {
message.extensions.forEach((extension) => {
switch (extension.type) {
case UseSRTP.type:
case UseSRTP.type: {
const useSrtp = UseSRTP.fromData(extension.data);
const profile = SrtpContext.findMatchingSRTPProfile(
useSrtp.profiles as Profile[],
Expand All @@ -244,7 +244,8 @@ handlers[HandshakeType.server_hello_2] =
log(dtls.sessionId, "selected srtp profile", profile);
if (profile == undefined) return;
srtp.srtpProfile = profile;
break;
}
break;
case ExtendedMasterSecret.type:
dtls.remoteExtendedMasterSecret = true;
break;
Expand Down
101 changes: 48 additions & 53 deletions packages/dtls/src/flight/server/flight2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,63 +50,58 @@ export const flight2 =

clientHello.extensions.forEach((extension) => {
switch (extension.type) {
case EllipticCurves.type:
{
const curves = EllipticCurves.fromData(extension.data).data;
log(dtls.sessionId, "curves", curves);
const curve = curves.filter((curve) =>
NamedCurveAlgorithmList.includes(curve as any),
)[0] as NamedCurveAlgorithms;
cipher.namedCurve = curve;
log(dtls.sessionId, "curve selected", cipher.namedCurve);
}
break;
case Signature.type:
{
if (!cipher.signatureHashAlgorithm)
throw new Error("need to set certificate");
case EllipticCurves.type: {
const curves = EllipticCurves.fromData(extension.data).data;
log(dtls.sessionId, "curves", curves);
const curve = curves.filter((curve) =>
NamedCurveAlgorithmList.includes(curve as any),
)[0] as NamedCurveAlgorithms;
cipher.namedCurve = curve;
log(dtls.sessionId, "curve selected", cipher.namedCurve);
}
break;
case Signature.type: {
if (!cipher.signatureHashAlgorithm)
throw new Error("need to set certificate");

const signatureHash = Signature.fromData(extension.data).data;
log(dtls.sessionId, "hash,signature", signatureHash);
const signature = signatureHash.find(
(v) => v.signature === cipher.signatureHashAlgorithm?.signature,
)?.signature;
const hash = signatureHash.find(
(v) => v.hash === cipher.signatureHashAlgorithm?.hash,
)?.hash;
if (signature == undefined || hash == undefined) {
throw new Error("invalid signatureHash");
}
const signatureHash = Signature.fromData(extension.data).data;
log(dtls.sessionId, "hash,signature", signatureHash);
const signature = signatureHash.find(
(v) => v.signature === cipher.signatureHashAlgorithm?.signature,
)?.signature;
const hash = signatureHash.find(
(v) => v.hash === cipher.signatureHashAlgorithm?.hash,
)?.hash;
if (signature == undefined || hash == undefined) {
throw new Error("invalid signatureHash");
}
break;
case UseSRTP.type:
{
if (!dtls.options?.srtpProfiles) return;
if (dtls.options.srtpProfiles.length === 0) return;
}
break;
case UseSRTP.type: {
if (!dtls.options?.srtpProfiles) return;
if (dtls.options.srtpProfiles.length === 0) return;

const useSrtp = UseSRTP.fromData(extension.data);
log(dtls.sessionId, "srtp profiles", useSrtp.profiles);
const profile = SrtpContext.findMatchingSRTPProfile(
useSrtp.profiles as Profile[],
dtls.options?.srtpProfiles,
);
if (!profile) {
throw new Error();
}
srtp.srtpProfile = profile;
log(dtls.sessionId, "srtp profile selected", srtp.srtpProfile);
}
break;
case ExtendedMasterSecret.type:
{
dtls.remoteExtendedMasterSecret = true;
}
break;
case RenegotiationIndication.type:
{
log(dtls.sessionId, "RenegotiationIndication", extension.data);
const useSrtp = UseSRTP.fromData(extension.data);
log(dtls.sessionId, "srtp profiles", useSrtp.profiles);
const profile = SrtpContext.findMatchingSRTPProfile(
useSrtp.profiles as Profile[],
dtls.options?.srtpProfiles,
);
if (!profile) {
throw new Error();
}
break;
srtp.srtpProfile = profile;
log(dtls.sessionId, "srtp profile selected", srtp.srtpProfile);
}
break;
case ExtendedMasterSecret.type: {
dtls.remoteExtendedMasterSecret = true;
}
break;
case RenegotiationIndication.type: {
log(dtls.sessionId, "RenegotiationIndication", extension.data);
}
break;
}
});

Expand Down
2 changes: 1 addition & 1 deletion packages/dtls/src/flight/server/flight4.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import debug from "debug";

import { certificateTypes, CurveType, signatures } from "../../cipher/const";
import { CurveType, certificateTypes, signatures } from "../../cipher/const";
import { CipherContext } from "../../context/cipher";
import { DtlsContext } from "../../context/dtls";
import { SrtpContext } from "../../context/srtp";
Expand Down
5 changes: 1 addition & 4 deletions packages/dtls/src/handshake/message/alert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ export class Alert {
description: types.uint8,
};

constructor(
public level: number,
public description: number,
) {}
constructor(public level: number, public description: number) {}

static deSerialize(buf: Buffer) {
return new Alert(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ export class CertificateVerify implements Handshake {
signature: types.buffer(types.uint16be),
};

constructor(
public algorithm: SignatureSchemes,
public signature: Buffer,
) {}
constructor(public algorithm: SignatureSchemes, public signature: Buffer) {}

static createEmpty() {
return new CertificateVerify(undefined as any, undefined as any);
Expand Down
2 changes: 1 addition & 1 deletion packages/dtls/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const dumpBuffer = (data: Buffer) =>
.join(",0x");

export const getObjectSummary = (obj: any) =>
Object.entries({ ...obj }).reduce((acc: {}, [key, value]) => {
Object.entries({ ...obj }).reduce((acc: any, [key, value]) => {
if (typeof value === "number" || typeof value === "string") {
acc[key] = value;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/dtls/src/record/message/fragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export class FragmentedHandshake {

static assemble(messages: FragmentedHandshake[]): FragmentedHandshake {
// cannot reassemble empty arrays
if (!(messages && messages.length)) {
if (!messages?.length) {
throw new Error("cannot reassemble handshake from empty array");
}

Expand Down Expand Up @@ -135,7 +135,7 @@ export class FragmentedHandshake {
if (!reference) return [];

// ignore empty arrays
if (!(fragments && fragments.length)) return [];
if (!fragments?.length) return [];

// return all fragments with matching msg_type, message_seq and total length
return fragments.filter((f) => {
Expand Down
Loading

0 comments on commit d71258a

Please sign in to comment.