Skip to content

Commit

Permalink
Eliminate the TypeScript's namespace feature
Browse files Browse the repository at this point in the history
  • Loading branch information
moznion committed Apr 29, 2019
1 parent 831a91e commit e2b9326
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 55 deletions.
2 changes: 1 addition & 1 deletion src/proxy-protocol.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export { INETProtocol } from './v1/enum/INETProtocol';
export { Peer } from './v1/Peer';
export { V1ProxyProtocol } from './v1/V1ProxyProtocol';
export { AddressFamily } from './v2/enum/AddressFamily';
export { AddressFamily, AddressFamilyType } from './v2/enum/AddressFamily';
export { TransportProtocol } from './v2/enum/TransportProtocol';
export { Command } from './v2/enum/Command';
export { UnixAddressTuple, UnixAddress, UnixProxyAddress } from './v2/proxy_address/UnixProxyAddress';
Expand Down
23 changes: 12 additions & 11 deletions src/v2/V2ProxyProtocol.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AddressFamily } from './enum/AddressFamily';
import { AddressFamily, AddressFamilyType } from './enum/AddressFamily';
import { Command } from './enum/Command';
import { TransportProtocol } from './enum/TransportProtocol';
import { IPv4ProxyAddress } from './proxy_address/IPv4ProxyAddress';
Expand Down Expand Up @@ -35,22 +35,22 @@ export class V2ProxyProtocol {
private static readonly initialHeaderOffset =
V2ProxyProtocol.protocolSignatureLength + V2ProxyProtocol.protocolMetaLength;

readonly addressFamily: AddressFamily;
readonly addressFamilyType: AddressFamilyType;

constructor(
readonly command: Command,
readonly transportProtocol: TransportProtocol,
readonly proxyAddress: IPv4ProxyAddress | IPv6ProxyAddress | UnixProxyAddress | UnspecProxyAddress,
readonly data?: Uint8Array,
) {
this.addressFamily = proxyAddress.getAddressFamily();
this.addressFamilyType = proxyAddress.getAddressFamilyType();
}

build(): Uint8Array {
const proto = this.initProto();
let cursor = V2ProxyProtocol.initialHeaderOffset;

if (this.addressFamily === AddressFamily.INET && this.proxyAddress instanceof IPv4ProxyAddress) {
if (this.addressFamilyType === AddressFamilyType.INET && this.proxyAddress instanceof IPv4ProxyAddress) {
for (let i = 0; i < 4; i++) {
proto[cursor++] = this.proxyAddress.sourceAddress.address[i];
}
Expand All @@ -66,7 +66,7 @@ export class V2ProxyProtocol {
const dstPort = V2ProxyProtocol.separate32bitTo16bitPair(this.proxyAddress.destinationPort);
proto[cursor++] = dstPort[0];
proto[cursor++] = dstPort[1];
} else if (this.addressFamily === AddressFamily.INET6 && this.proxyAddress instanceof IPv6ProxyAddress) {
} else if (this.addressFamilyType === AddressFamilyType.INET6 && this.proxyAddress instanceof IPv6ProxyAddress) {
for (let i = 0; i < 16; i++) {
proto[cursor++] = this.proxyAddress.sourceAddress.address[i];
}
Expand All @@ -82,7 +82,7 @@ export class V2ProxyProtocol {
const dstPort = V2ProxyProtocol.separate32bitTo16bitPair(this.proxyAddress.destinationPort);
proto[cursor++] = dstPort[0];
proto[cursor++] = dstPort[1];
} else if (this.addressFamily === AddressFamily.UNIX && this.proxyAddress instanceof UnixProxyAddress) {
} else if (this.addressFamilyType === AddressFamilyType.UNIX && this.proxyAddress instanceof UnixProxyAddress) {
for (let i = 0; i < 108; i++) {
proto[cursor++] = this.proxyAddress.sourceAddress.address[i];
}
Expand Down Expand Up @@ -131,8 +131,8 @@ export class V2ProxyProtocol {
);
}
const afAndTransportProtocol = this.separate8bit(afAndTransportProtocolByte);
const addressFamily = AddressFamily[AddressFamily[afAndTransportProtocol[0]]];
if (addressFamily === undefined) {
const addressFamilyType = AddressFamilyType[AddressFamilyType[afAndTransportProtocol[0]]];
if (addressFamilyType === undefined) {
throw new V2ProxyProtocolParseError('given address family is invalid');
}
const transportProtocol = TransportProtocol[TransportProtocol[afAndTransportProtocol[1]]];
Expand All @@ -147,15 +147,16 @@ export class V2ProxyProtocol {
throw new V2ProxyProtocolParseError("given binary doesn't have bytes for specifying length");
}
const length = (upperLengthByte << 8) + lowerLengthByte;
const addressFamilyLength = AddressFamily.getLength(addressFamily);
const addressFamily = new AddressFamily(addressFamilyType);
const addressFamilyLength = addressFamily.getLength();
if (length < addressFamilyLength) {
throw new V2ProxyProtocolParseError("given specified length is shorter than address family's length");
}

return new V2ProxyProtocol(
command,
transportProtocol,
AddressFamily.getFactoryMethod(addressFamily)(input.slice(16)),
addressFamily.getFactoryMethod()(input.slice(16)),
input.slice(addressFamilyLength + 16),
);
}
Expand Down Expand Up @@ -188,7 +189,7 @@ export class V2ProxyProtocol {
}

private unionAddressFamilyAndTransportProtocol(): number {
return this.addressFamily | this.transportProtocol;
return this.addressFamilyType | this.transportProtocol;
}

private static separate32bitTo16bitPair(num: number): [number, number] {
Expand Down
17 changes: 9 additions & 8 deletions src/v2/enum/AddressFamily.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ import {
UnixProxyAddress,
UnspecProxyAddress,
} from '../../proxy-protocol';
import { AddressFamilyType } from './AddressFamily';

test('should getLength() returns exactly value for each AddressFamily', async () => {
expect(AddressFamily.getLength(AddressFamily.INET)).toBe(12);
expect(AddressFamily.getLength(AddressFamily.INET6)).toBe(36);
expect(AddressFamily.getLength(AddressFamily.UNIX)).toBe(216);
expect(AddressFamily.getLength(AddressFamily.UNSPEC)).toBe(0);
expect(new AddressFamily(AddressFamilyType.INET).getLength()).toBe(12);
expect(new AddressFamily(AddressFamilyType.INET6).getLength()).toBe(36);
expect(new AddressFamily(AddressFamilyType.UNIX).getLength()).toBe(216);
expect(new AddressFamily(AddressFamilyType.UNSPEC).getLength()).toBe(0);
});

test('should getFactoryMethod() returns exactly value for each AddressFamily', async () => {
expect(AddressFamily.getFactoryMethod(AddressFamily.INET)).toBe(IPv4ProxyAddress.from);
expect(AddressFamily.getFactoryMethod(AddressFamily.INET6)).toBe(IPv6ProxyAddress.from);
expect(AddressFamily.getFactoryMethod(AddressFamily.UNIX)).toBe(UnixProxyAddress.from);
expect(AddressFamily.getFactoryMethod(AddressFamily.UNSPEC)).toBe(UnspecProxyAddress.from);
expect(new AddressFamily(AddressFamilyType.INET).getFactoryMethod()).toBe(IPv4ProxyAddress.from);
expect(new AddressFamily(AddressFamilyType.INET6).getFactoryMethod()).toBe(IPv6ProxyAddress.from);
expect(new AddressFamily(AddressFamilyType.UNIX).getFactoryMethod()).toBe(UnixProxyAddress.from);
expect(new AddressFamily(AddressFamilyType.UNSPEC).getFactoryMethod()).toBe(UnspecProxyAddress.from);
});
30 changes: 14 additions & 16 deletions src/v2/enum/AddressFamily.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,39 @@ import { IPv6ProxyAddress } from '../proxy_address/IPv6ProxyAddress';
import { UnixProxyAddress } from '../proxy_address/UnixProxyAddress';
import { UnspecProxyAddress } from '../proxy_address/UnspecProxyAddress';

enum AddressFamily {
export enum AddressFamilyType {
UNSPEC = 0x00,
INET = 0x10,
INET6 = 0x20,
UNIX = 0x30,
}

namespace AddressFamily {
export function getLength(addressFamily: AddressFamily) {
switch (addressFamily) {
case AddressFamily.INET:
export class AddressFamily {
constructor(private readonly addressFamilyType: AddressFamilyType) {}

getLength() {
switch (this.addressFamilyType) {
case AddressFamilyType.INET:
return 12;
case AddressFamily.INET6:
case AddressFamilyType.INET6:
return 36;
case AddressFamily.UNIX:
case AddressFamilyType.UNIX:
return 216;
default:
return 0;
}
}

export function getFactoryMethod(
addressFamily: AddressFamily,
): (d: Uint8Array) => IPv4ProxyAddress | IPv6ProxyAddress | UnixProxyAddress | UnspecProxyAddress {
switch (addressFamily) {
case AddressFamily.INET:
getFactoryMethod(): (d: Uint8Array) => IPv4ProxyAddress | IPv6ProxyAddress | UnixProxyAddress | UnspecProxyAddress {
switch (this.addressFamilyType) {
case AddressFamilyType.INET:
return IPv4ProxyAddress.from;
case AddressFamily.INET6:
case AddressFamilyType.INET6:
return IPv6ProxyAddress.from;
case AddressFamily.UNIX:
case AddressFamilyType.UNIX:
return UnixProxyAddress.from;
default:
return UnspecProxyAddress.from;
}
}
}

export { AddressFamily };
8 changes: 4 additions & 4 deletions src/v2/proxy_address/IPv4ProxyAddress.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AddressFamily } from '../enum/AddressFamily';
import { AddressFamily, AddressFamilyType } from '../enum/AddressFamily';
import { ProxyAddress } from './ProxyAddress';

export type IPv4AddressTuple = [number, number, number, number];
Expand Down Expand Up @@ -34,10 +34,10 @@ export class IPv4ProxyAddress implements ProxyAddress {

// for TCP/UDP over IPv4, len = 12
getLength(): number {
return AddressFamily.getLength(this.getAddressFamily());
return new AddressFamily(this.getAddressFamilyType()).getLength();
}

getAddressFamily(): AddressFamily {
return AddressFamily.INET;
getAddressFamilyType(): AddressFamilyType {
return AddressFamilyType.INET;
}
}
8 changes: 4 additions & 4 deletions src/v2/proxy_address/IPv6ProxyAddress.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AddressFamily } from '../enum/AddressFamily';
import { AddressFamily, AddressFamilyType } from '../enum/AddressFamily';
import { ProxyAddress } from './ProxyAddress';

export type IPv6AddressTuple = [
Expand Down Expand Up @@ -102,10 +102,10 @@ export class IPv6ProxyAddress implements ProxyAddress {

// for TCP/UDP over IPv6, len = 36
getLength(): number {
return AddressFamily.getLength(this.getAddressFamily());
return new AddressFamily(this.getAddressFamilyType()).getLength();
}

getAddressFamily(): AddressFamily {
return AddressFamily.INET6;
getAddressFamilyType(): AddressFamilyType {
return AddressFamilyType.INET6;
}
}
4 changes: 2 additions & 2 deletions src/v2/proxy_address/ProxyAddress.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AddressFamily } from '../enum/AddressFamily';
import { AddressFamilyType } from '../enum/AddressFamily';

export interface ProxyAddress {
getLength(): number;
getAddressFamily(): AddressFamily;
getAddressFamilyType(): AddressFamilyType;
}
8 changes: 4 additions & 4 deletions src/v2/proxy_address/UnixProxyAddress.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AddressFamily } from '../enum/AddressFamily';
import { AddressFamily, AddressFamilyType } from '../enum/AddressFamily';
import { ProxyAddress } from './ProxyAddress';

export type UnixAddressTuple = [
Expand Down Expand Up @@ -252,10 +252,10 @@ export class UnixProxyAddress implements ProxyAddress {

// for AF_UNIX sockets, len = 216
getLength(): number {
return AddressFamily.getLength(this.getAddressFamily());
return new AddressFamily(this.getAddressFamilyType()).getLength();
}

getAddressFamily(): AddressFamily {
return AddressFamily.UNIX;
getAddressFamilyType(): AddressFamilyType {
return AddressFamilyType.UNIX;
}
}
8 changes: 4 additions & 4 deletions src/v2/proxy_address/UnspecProxyAddress.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AddressFamily } from '../enum/AddressFamily';
import { AddressFamily, AddressFamilyType } from '../enum/AddressFamily';
import { ProxyAddress } from './ProxyAddress';

export class UnspecProxyAddress implements ProxyAddress {
Expand All @@ -9,10 +9,10 @@ export class UnspecProxyAddress implements ProxyAddress {
}

getLength(): number {
return AddressFamily.getLength(this.getAddressFamily());
return new AddressFamily(this.getAddressFamilyType()).getLength();
}

getAddressFamily(): AddressFamily {
return AddressFamily.UNSPEC;
getAddressFamilyType(): AddressFamilyType {
return AddressFamilyType.UNSPEC;
}
}
2 changes: 1 addition & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"no-default-export": true,
"no-duplicate-variable": true,
"no-inferrable-types": true,
"no-namespace": [false],
"no-namespace": [true],
"no-reference": true,
"no-string-throw": true,
"no-unused-expression": true,
Expand Down

0 comments on commit e2b9326

Please sign in to comment.