diff --git a/types/soupbintcp/index.d.ts b/types/soupbintcp/index.d.ts new file mode 100644 index 0000000000..d5ca4146c7 --- /dev/null +++ b/types/soupbintcp/index.d.ts @@ -0,0 +1,110 @@ +// Type definitions for soupbintcp 0.2 +// Project: https://github.com/jvirtanen/node-soupbintcp#readme +// Definitions by: Vilim Stubičan +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.4 + +/// + +export as namespace soupbintcp; + +import { EventEmitter } from 'events'; +import { Socket } from "net"; + +export class Client extends EventEmitter { + constructor(options: { port: number, host: string }, callback?: () => void); + + login(payload: LoginRequestPayload, callback?: (data?: any) => void): void; + + logout(callback?: (data?: any) => void): void; + + send(payload: any, callback?: (data?: any) => void): void; + + end(): void; +} + +export interface ConnectionOptions { + rxTimeoutMillis?: number; + txIntervalMillis?: number; + heartbeatPacketType: PacketType; + keepAliveMillis?: number; +} + +export class Connection extends EventEmitter { + constructor(socket: Socket, options: ConnectionOptions); + + send(packetType: PacketType, payload: any, callback?: (data?: any) => void): void; + + end(): void; +} + +export enum PacketType { + DEBUG = 0x2b, // + + + LOGIN_ACCEPTED = 0x41, // A + LOGIN_REJECTED = 0x4a, // J + SEQUENCED_DATA = 0x53, // S + SERVER_HEARTBEAT = 0x48, // H + END_OF_SESSION = 0x5a, // Z + + LOGIN_REQUEST = 0x4c, // L + UNSEQUENCED_DATA = 0x55, // U + CLIENT_HEARTBEAT = 0x52, // R + LOGOUT_REQUEST = 0x4f // O +} + +export interface LoginRequestPayload { + username: string; + password: string; + requestedSession: string; + requestedSequenceNumber: number; +} + +export function formatLoginRequest(payload: LoginRequestPayload): Buffer; + +export function parseLoginRequest(payload: Buffer): LoginRequestPayload; + +export interface LoginAcceptedPayload { + username: string; + sequenceNumber: number; +} + +export function formatLoginAccepted(payload: LoginAcceptedPayload): Buffer; + +export function parseLoginAccepted(payload: Buffer): LoginAcceptedPayload; + +export interface LoginRejectedPayload { + rejectReasonCode: string; +} + +export function formatLoginRejected(payload: LoginRejectedPayload): Buffer; + +export function parseLoginRejected(payload: Buffer): LoginRejectedPayload; + +export class Parser { + constructor(callback: (packetType: PacketType, payload: Buffer) => void); + + parse(data: Buffer): void; +} + +export class Server extends EventEmitter { + constructor(options: { port: number, host: string }, callback?: (data?: any) => void); + + address(): { port: number; family: string; address: string; }; + + close(callback: () => void): void; +} + +export class Session extends EventEmitter { + constructor(socket: Socket); + + accept(payload: LoginAcceptedPayload, callback?: (data?: any) => void): void; + + reject(payload: LoginRejectedPayload, callback?: (data?: any) => void): void; + + send(payload: any, callback?: (data?: any) => void): void; + + ending(callback?: (data?: any) => void): void; + + end(): void; +} diff --git a/types/soupbintcp/soupbintcp-tests.ts b/types/soupbintcp/soupbintcp-tests.ts new file mode 100644 index 0000000000..1ad62a8d11 --- /dev/null +++ b/types/soupbintcp/soupbintcp-tests.ts @@ -0,0 +1,77 @@ +import { Client, Server } from 'soupbintcp'; + +// Arrange +const client = new Client({port: 8000, host: ''}); +const server = new Server({port: 8000, host: ''}); + +// Act & Assert +// $ExpectType Client +new Client({port: 8000, host: ''}); + +// $ExpectType Server +new Server({port: 8000, host: ''}); + +// Invalid type +// $ExpectError +new Client({host: ''}); +// Invalid type + +// $ExpectError +new Client({host: 8000}); + +// $ExpectError +new Client({port: ''}); + +// $ExpectError +new Client({port: 8000}); + +// $ExpectError +new Client(); + +// Invalid type +// $ExpectError +new Server({host: ''}); +// Invalid type + +// $ExpectError +new Server({host: 8000}); + +// $ExpectError +new Server({port: ''}); + +// $ExpectError +new Server({port: 8000}); + +// $ExpectError +new Server(); + +// $ExpectError +client.login({ + password: '', + requestedSession: '', + requestedSequenceNumber: 8000 +}); + +// $ExpectError +client.login({ + username: '', + requestedSession: '', + requestedSequenceNumber: 8000 +}); + +// $ExpectError +client.login({ + username: '', + password: '', + requestedSequenceNumber: 8000 +}); + +// $ExpectError +client.login({ + username: '', + password: '', + requestedSession: '', +}); + +// $ExpectError +client.login(); diff --git a/types/soupbintcp/tsconfig.json b/types/soupbintcp/tsconfig.json new file mode 100644 index 0000000000..e934988e2b --- /dev/null +++ b/types/soupbintcp/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "soupbintcp-tests.ts" + ] +} \ No newline at end of file diff --git a/types/soupbintcp/tslint.json b/types/soupbintcp/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/soupbintcp/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }