Add new definition for "engine.io" (#18323)

* Add new definition for "engine.io"

* pass lints.
This commit is contained in:
KentarouTakeda 2017-07-25 03:24:45 +09:00 committed by Andy
parent 33eaf4b8a6
commit 267e3c0322
4 changed files with 421 additions and 0 deletions

View File

@ -0,0 +1,168 @@
import engine = require('engine.io');
import http = require('http');
let serverOptions: engine.ServerOptions;
let server: engine.Server;
let httpServer: http.Server;
let attachOptions: engine.AttachOptions;
let serverAttachOptions: engine.ServerAttachOptions;
serverOptions = {};
serverOptions = {
pingTimeout: 60000,
pingInterval: 25000,
upgradeTimeout: 10000,
maxHttpBufferSize: 10E7,
transports: ['polling', 'websocket'],
allowUpgrades: true,
perMessageDeflate: true,
httpCompression: true,
cookie: 'io',
cookiePath: '/',
wsEngine: 'ws',
initialPacket: new Buffer([0, 1, 2, 3, 4, 5]),
allowRequest: (req, cb) => {
console.log(req.url);
cb(null, true);
}
};
attachOptions = {
path: '/engine.io' ,
destroyUpgrade: true,
destroyUpgradeTimeout: 1000,
};
attachOptions.handlePreflightRequest = true;
attachOptions.handlePreflightRequest = false;
attachOptions.handlePreflightRequest = (server, req, res) => {
console.log(server.clientsCount);
console.log(req.httpVersion);
console.log(res.finished);
};
serverAttachOptions = Object.assign({}, serverOptions, attachOptions);
console.log(engine.protocol);
httpServer = http.createServer();
httpServer.listen(8000);
server = engine(httpServer);
server.close();
server = engine(httpServer, serverOptions);
server.close();
httpServer.close();
server = engine.listen(8000);
server.httpServer!.close();
server.close();
server = engine.listen(8000, serverOptions);
server.httpServer!.close();
server.close();
server = engine.listen(8000, serverOptions, () => {});
server.httpServer!.close();
server.close();
httpServer = http.createServer();
httpServer.listen(8000);
server = engine.attach(httpServer);
server.close();
httpServer.close();
httpServer = http.createServer();
httpServer.listen(8000);
server = engine.attach(httpServer, serverOptions);
server.close();
httpServer.close();
httpServer = http.createServer();
httpServer.listen(8000);
server = engine.attach(httpServer, attachOptions);
server.close();
httpServer.close();
httpServer = http.createServer();
httpServer.listen(8000);
server = engine.attach(httpServer, serverAttachOptions);
server.close();
httpServer.close();
server = new engine.Server();
server.close();
server = new engine.Server();
server.close();
httpServer = http.createServer();
httpServer.listen(8000);
server = new engine.Server(serverOptions);
server.attach(httpServer);
server.attach(httpServer, attachOptions);
server.close();
httpServer.close();
server.generateId = (req) => Math.floor(Math.random() * 100000).toString();
httpServer = http.createServer();
httpServer.listen(8000);
server = new engine.Server(serverOptions);
httpServer.on('upgrade', (req, socket, head) => {
server.handleUpgrade(req, socket, head);
});
httpServer.on('request', (req, res) => {
server.handleRequest(req, res);
});
console.log(server.clients);
console.log(server.clientsCount);
server.on('connection', (socket) => {
console.log(socket.id);
console.log(socket.server.getMaxListeners());
console.log(socket.request.headers);
console.log(socket.upgraded);
console.log(socket.readyState);
console.log(server.clients[socket.id].id);
socket.on('close', (reason, description) => {
console.log('CLOSE', reason, description && description.message);
});
socket.on('message', (message) => {
console.log('MESSAGE', message);
});
socket.on('error', err => {
console.log('ERROR', err);
});
socket.on('flush', buffer => {
console.log('FLUSH', buffer);
});
socket.on('drain', () => {
console.log('DRAIN');
});
socket.on('packet', packet => {
console.log('PACKET', packet.type, packet.data);
});
socket.on('packetCreate', packet => {
console.log('PACKETCREATE', packet.type, packet.data);
});
socket.send('utf 8 string', {compress: false}, () => {
console.log("SENDCALLBACK");
});
socket.send(new Buffer([0, 1, 2, 3, 4, 5])); // binary data
});
server.once('flush', (socket, buffer) => {
console.log(socket.id);
console.log(buffer[0].type);
console.log(buffer[0].options);
console.log(buffer[0].data);
});
server.once('drain', (socket) => {
console.log(socket.id);
});
server.close();
httpServer.close();

229
types/engine.io/index.d.ts vendored Normal file
View File

@ -0,0 +1,229 @@
// Type definitions for engine.io 3.1
// Project: https://github.com/socketio/engine.io
// Definitions by: KentarouTakeda <https://github.com/KentarouTakeda/>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
import http = require('http');
import net = require('net');
import { EventEmitter } from "events";
declare namespace engine {
type Message = string|Buffer|ArrayBuffer|ArrayBufferView;
type Transport = "polling"|"websocket";
interface Packet {
type: string;
options?: MessageOptions;
data?: Message;
}
type AllowRequestFunction = (req: http.IncomingMessage, fn: (err: string|null|undefined, success: boolean) => void) => void;
interface ServerOptions {
/**
* how many ms without a pong packet to consider the connection closed (60000)
*/
pingTimeout?: number;
/**
* how many ms before sending a new ping packet (25000)
*/
pingInterval?: number;
/**
* how many ms before an uncompleted transport upgrade is cancelled (10000)
*/
upgradeTimeout?: number;
/**
* how many bytes or characters a message can be, before closing the session (to avoid DoS). Default value is 10E7.
*/
maxHttpBufferSize?: number;
/**
* A function that receives a given handshake or upgrade request as its first parameter,
* and can decide whether to continue or not. The second argument is a function that needs
* to be called with the decided information: fn(err, success), where success is a boolean
* value where false means that the request is rejected, and err is an error code.
*/
allowRequest?: AllowRequestFunction;
/**
* to allow connections to (['polling', 'websocket'])
*/
transports?: engine.Transport[];
/**
* whether to allow transport upgrades (true)
*/
allowUpgrades?: boolean;
/**
* parameters of the WebSocket permessage-deflate extension (see ws module api docs). Set to false to disable. (true)
*/
perMessageDeflate?: any;
/**
* parameters of the http compression for the polling transports (see zlib api docs). Set to false to disable. (true)
*/
httpCompression?: any;
/**
* name of the HTTP cookie that contains the client sid to send as part of handshake response headers. Set to false to not send one. (io)
*/
cookie?: string|boolean;
/**
* path of the above cookie option. If false, no path will be sent,
* which means browsers will only send the cookie on the engine.io
* attached path (/engine.io). Set false to not save io cookie
* on all requests. (/)
*/
cookiePath?: string|boolean;
/**
* If true HttpOnly io cookie cannot be accessed by client-side APIs,
* such as JavaScript. (true) This option has no effect
* if cookie or cookiePath is set to false.
*/
cookieHttpOnly?: boolean;
/**
* what WebSocket server implementation to use. Specified module must
* conform to the ws interface (see ws module api docs). Default value is ws.
* An alternative c++ addon is also available by installing uws module.
*/
wsEngine?: "ws"|"uws";
/**
* an optional packet which will be concatenated to the handshake packet emitted by Engine.IO.
*/
initialPacket?: Message;
}
interface AttachOptions {
/**
* name of the path to capture (/engine.io).
*/
path?: string;
/**
* destroy unhandled upgrade requests (true)
*/
destroyUpgrade?: boolean;
/**
* milliseconds after which unhandled requests are ended (1000)
*/
destroyUpgradeTimeout?: number;
/**
* whether to let engine.io handle the OPTIONS requests. You can also pass a custom function to handle the requests (true)
*/
handlePreflightRequest?: boolean|((server: Server, req: http.IncomingMessage, res: http.ServerResponse) => void);
}
interface ServerAttachOptions extends ServerOptions, AttachOptions {}
interface MessageOptions {
compress?: boolean;
}
/**
* The main server/manager. Inherits from EventEmitter.
*/
class Server extends EventEmitter {
/**
* hash of connected clients by id.
*/
clients: {[sid: string]: Socket};
/**
* number of connected clients.
*/
clientsCount: number;
/**
* Initializes the server
*/
constructor(opts?: engine.ServerOptions);
/**
* Fired when a new connection is established.
*/
on(ev: 'connection'|'drain', fn: (socket: Socket) => void): this;
on(ev: 'flush', fn: (socket: Socket, buffer: Packet[]) => void): this;
/**
* Closes all clients
*/
close(): this;
readonly httpServer?: http.Server;
/**
* Called internally when a Engine request is intercepted.
*/
handleRequest(req: http.IncomingMessage, res: http.ServerResponse): this;
/**
* Called internally when a Engine ws upgrade is intercepted.
*/
handleUpgrade(req: http.IncomingMessage, socket: net.Socket, head: Buffer): this;
/**
* Attach this Server instance to an http.Server
* Captures upgrade requests for a http.Server. In other words, makes a regular http.Server WebSocket-compatible.
*/
attach(http: http.Server, opts?: AttachOptions): this;
/**
* Generate a socket id.
* Overwrite this method to generate your custom socket id.
*/
generateId(req: http.IncomingMessage): string;
}
/**
* A representation of a client. Inherits from EventEmitter.
*/
class Socket extends EventEmitter {
/**
* unique identifier
*/
id: string;
/**
* engine parent reference
*/
server: Server;
/**
* request that originated the Socket
*/
request: http.IncomingMessage;
/**
* whether the transport has been upgraded
*/
upgraded: boolean;
/**
* readyState
*/
readyState: 'opening'|'open'|'closing'|'closed';
/**
* Sends a message, performing message = toString(arguments[0]) unless sending binary data, which is sent as is.
*/
send(message: Message, opts?: MessageOptions, fn?: ((transport: any) => void)): void;
/**
* Disconnects the client
*/
close(): this;
/**
* Fired when the client is disconnected.
*/
on(ev: "close", fn: (reason: string, description?: Error) => void): this;
/**
* Fired when the client sends a message.
*/
on(ev: "message", fn: (data: string|Buffer) => void): this;
/**
* Fired when an error occurs.
*/
on(ev: "error", fn: (err: Error) => void): this;
/**
* Called when the write buffer is being flushed.
*/
on(ev: "flush", fn: (buffer: Packet[]) => void): this;
/**
* Called when the write buffer is drained
*/
on(ev: "drain", fn: () => void): this;
/**
* packet: Called when a socket received a packet (message, ping)
* packetCreate: Called before a socket sends a packet (message, pong)
*/
on(ev: "packet" | "packetCreate", fn: (packet: Packet) => void): this;
}
function attach(http: net.Server, opyts?: engine.ServerAttachOptions): engine.Server;
function listen(port: number, opts?: engine.ServerOptions, fn?: () => void): engine.Server;
const protocol: number;
}
declare function engine(httpServer?: net.Server, opts?: engine.ServerOptions): engine.Server;
export = engine;

View File

@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strict": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"engine.io-tests.ts"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }