Skip to content

Commit

Permalink
chore: migrate to TypeScript
Browse files Browse the repository at this point in the history
Related: #510
  • Loading branch information
darrachequesne committed Oct 8, 2021
1 parent 18a6eb8 commit c0d6eaa
Show file tree
Hide file tree
Showing 19 changed files with 482 additions and 1,130 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,2 +1,3 @@
node_modules
npm-debug.log
build/
127 changes: 0 additions & 127 deletions lib/engine.io.js

This file was deleted.

55 changes: 55 additions & 0 deletions lib/engine.io.ts
@@ -0,0 +1,55 @@
import { createServer } from "http";
import { Server, AttachOptions, ServerOptions } from "./server";
import transports from "./transports/index";
import * as parser from "engine.io-parser";

export { Server, transports, listen, attach, parser };
export { AttachOptions, ServerOptions } from "./server";
export { Socket } from "./socket";
export { Transport } from "./transport";
export const protocol = parser.protocol;

/**
* Creates an http.Server exclusively used for WS upgrades.
*
* @param {Number} port
* @param {Function} callback
* @param {Object} options
* @return {Server} websocket.io server
* @api public
*/

function listen(port, options: AttachOptions & ServerOptions, fn) {
if ("function" === typeof options) {
fn = options;
options = {};
}

const server = createServer(function(req, res) {
res.writeHead(501);
res.end("Not Implemented");
});

// create engine server
const engine = attach(server, options);
engine.httpServer = server;

server.listen(port, fn);

return engine;
}

/**
* Captures upgrade requests for a http.Server.
*
* @param {http.Server} server
* @param {Object} options
* @return {Server} engine server
* @api public
*/

function attach(server, options: AttachOptions & ServerOptions) {
const engine = new Server(options);
engine.attach(server, options);
return engine;
}
38 changes: 20 additions & 18 deletions lib/parser-v3/index.js → lib/parser-v3/index.ts
Expand Up @@ -9,7 +9,7 @@ var utf8 = require('./utf8');
/**
* Current protocol version.
*/
exports.protocol = 3;
export const protocol = 3;

const hasBinary = (packets) => {
for (const packet of packets) {
Expand All @@ -24,7 +24,7 @@ const hasBinary = (packets) => {
* Packet types.
*/

var packets = exports.packets = {
export const packets = {
open: 0 // non-ws
, close: 1 // non-ws
, ping: 2
Expand Down Expand Up @@ -60,7 +60,7 @@ const EMPTY_BUFFER = Buffer.concat([]);
* @api private
*/

exports.encodePacket = function (packet, supportsBinary, utf8encode, callback) {
export function encodePacket (packet, supportsBinary, utf8encode, callback) {
if (typeof supportsBinary === 'function') {
callback = supportsBinary;
supportsBinary = null;
Expand Down Expand Up @@ -94,7 +94,7 @@ exports.encodePacket = function (packet, supportsBinary, utf8encode, callback) {

function encodeBuffer(packet, supportsBinary, callback) {
if (!supportsBinary) {
return exports.encodeBase64Packet(packet, callback);
return encodeBase64Packet(packet, callback);
}

var data = packet.data;
Expand All @@ -110,7 +110,7 @@ function encodeBuffer(packet, supportsBinary, callback) {
* @return {String} base64 encoded message
*/

exports.encodeBase64Packet = function(packet, callback){
export function encodeBase64Packet (packet, callback){
var data = Buffer.isBuffer(packet.data) ? packet.data : arrayBufferToBuffer(packet.data);
var message = 'b' + packets[packet.type];
message += data.toString('base64');
Expand All @@ -124,7 +124,7 @@ exports.encodeBase64Packet = function(packet, callback){
* @api private
*/

exports.decodePacket = function (data, binaryType, utf8decode) {
export function decodePacket (data, binaryType, utf8decode) {
if (data === undefined) {
return err;
}
Expand All @@ -137,7 +137,7 @@ exports.decodePacket = function (data, binaryType, utf8decode) {
type = data.charAt(0);

if (type === 'b') {
return exports.decodeBase64Packet(data.substr(1), binaryType);
return decodeBase64Packet(data.substr(1), binaryType);
}

if (utf8decode) {
Expand Down Expand Up @@ -189,14 +189,15 @@ function tryDecode(data) {
* @return {Object} with `type` and `data` (if any)
*/

exports.decodeBase64Packet = function(msg, binaryType) {
export function decodeBase64Packet (msg, binaryType) {
var type = packetslist[msg.charAt(0)];
var data = Buffer.from(msg.substr(1), 'base64');
if (binaryType === 'arraybuffer') {
var abv = new Uint8Array(data.length);
for (var i = 0; i < abv.length; i++){
abv[i] = data[i];
}
// @ts-ignore
data = abv.buffer;
}
return { type: type, data: data };
Expand All @@ -218,22 +219,22 @@ exports.decodeBase64Packet = function(msg, binaryType) {
* @api private
*/

exports.encodePayload = function (packets, supportsBinary, callback) {
export function encodePayload (packets, supportsBinary, callback) {
if (typeof supportsBinary === 'function') {
callback = supportsBinary;
supportsBinary = null;
}

if (supportsBinary && hasBinary(packets)) {
return exports.encodePayloadAsBinary(packets, callback);
return encodePayloadAsBinary(packets, callback);
}

if (!packets.length) {
return callback('0:');
}

function encodeOne(packet, doneCallback) {
exports.encodePacket(packet, supportsBinary, false, function(message) {
encodePacket(packet, supportsBinary, false, function(message) {
doneCallback(null, setLengthHeader(message));
});
}
Expand Down Expand Up @@ -273,9 +274,9 @@ function map(ary, each, done) {
* @api public
*/

exports.decodePayload = function (data, binaryType, callback) {
export function decodePayload (data, binaryType, callback) {
if (typeof data !== 'string') {
return exports.decodePayloadAsBinary(data, binaryType, callback);
return decodePayloadAsBinary(data, binaryType, callback);
}

if (typeof binaryType === 'function') {
Expand All @@ -298,6 +299,7 @@ exports.decodePayload = function (data, binaryType, callback) {
continue;
}

// @ts-ignore
if (length === '' || (length != (n = Number(length)))) {
// parser error - ignoring payload
return callback(err, 0, 1);
Expand All @@ -311,7 +313,7 @@ exports.decodePayload = function (data, binaryType, callback) {
}

if (msg.length) {
packet = exports.decodePacket(msg, binaryType, false);
packet = decodePacket(msg, binaryType, false);

if (err.type === packet.type && err.data === packet.data) {
// parser error in individual packet - ignoring payload
Expand Down Expand Up @@ -393,7 +395,7 @@ function arrayBufferToBuffer(data) {
* @api private
*/

exports.encodePayloadAsBinary = function (packets, callback) {
export function encodePayloadAsBinary (packets, callback) {
if (!packets.length) {
return callback(EMPTY_BUFFER);
}
Expand Down Expand Up @@ -430,7 +432,7 @@ function encodeOneBinaryPacket(p, doneCallback) {
doneCallback(null, Buffer.concat([sizeBuffer, packet]));
}

exports.encodePacket(p, true, true, onBinaryPacketEncode);
encodePacket(p, true, true, onBinaryPacketEncode);

}

Expand All @@ -444,7 +446,7 @@ function encodeOneBinaryPacket(p, doneCallback) {
* @api public
*/

exports.decodePayloadAsBinary = function (data, binaryType, callback) {
export function decodePayloadAsBinary (data, binaryType, callback) {
if (typeof binaryType === 'function') {
callback = binaryType;
binaryType = null;
Expand Down Expand Up @@ -478,6 +480,6 @@ exports.decodePayloadAsBinary = function (data, binaryType, callback) {
var total = buffers.length;
for (i = 0; i < total; i++) {
var buffer = buffers[i];
callback(exports.decodePacket(buffer, binaryType, true), i, total);
callback(decodePacket(buffer, binaryType, true), i, total);
}
};
2 changes: 1 addition & 1 deletion lib/parser-v3/utf8.js → lib/parser-v3/utf8.ts
Expand Up @@ -203,7 +203,7 @@ function utf8decode(byteString, opts) {
return ucs2encode(codePoints);
}

module.exports = {
export default {
version: '2.1.2',
encode: utf8encode,
decode: utf8decode
Expand Down

0 comments on commit c0d6eaa

Please sign in to comment.