mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-05-25 16:11:24 +08:00
Merge pull request #23956 from jewbre/feature/soupbintcp-type-definitions
Feature/soupbintcp type definitions
This commit is contained in:
110
types/soupbintcp/index.d.ts
vendored
Normal file
110
types/soupbintcp/index.d.ts
vendored
Normal file
@@ -0,0 +1,110 @@
|
||||
// Type definitions for soupbintcp 0.2
|
||||
// Project: https://github.com/jvirtanen/node-soupbintcp#readme
|
||||
// Definitions by: Vilim Stubičan <https://github.com/jewbre>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 2.4
|
||||
|
||||
/// <reference types="node" />
|
||||
|
||||
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;
|
||||
}
|
||||
77
types/soupbintcp/soupbintcp-tests.ts
Normal file
77
types/soupbintcp/soupbintcp-tests.ts
Normal file
@@ -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();
|
||||
23
types/soupbintcp/tsconfig.json
Normal file
23
types/soupbintcp/tsconfig.json
Normal file
@@ -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"
|
||||
]
|
||||
}
|
||||
1
types/soupbintcp/tslint.json
Normal file
1
types/soupbintcp/tslint.json
Normal file
@@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
Reference in New Issue
Block a user