Skip to content

Commit c0d6eaa

Browse files
chore: migrate to TypeScript
Related: #510
1 parent 18a6eb8 commit c0d6eaa

19 files changed

+482
-1130
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
22
npm-debug.log
3+
build/

lib/engine.io.js

-127
This file was deleted.

lib/engine.io.ts

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { createServer } from "http";
2+
import { Server, AttachOptions, ServerOptions } from "./server";
3+
import transports from "./transports/index";
4+
import * as parser from "engine.io-parser";
5+
6+
export { Server, transports, listen, attach, parser };
7+
export { AttachOptions, ServerOptions } from "./server";
8+
export { Socket } from "./socket";
9+
export { Transport } from "./transport";
10+
export const protocol = parser.protocol;
11+
12+
/**
13+
* Creates an http.Server exclusively used for WS upgrades.
14+
*
15+
* @param {Number} port
16+
* @param {Function} callback
17+
* @param {Object} options
18+
* @return {Server} websocket.io server
19+
* @api public
20+
*/
21+
22+
function listen(port, options: AttachOptions & ServerOptions, fn) {
23+
if ("function" === typeof options) {
24+
fn = options;
25+
options = {};
26+
}
27+
28+
const server = createServer(function(req, res) {
29+
res.writeHead(501);
30+
res.end("Not Implemented");
31+
});
32+
33+
// create engine server
34+
const engine = attach(server, options);
35+
engine.httpServer = server;
36+
37+
server.listen(port, fn);
38+
39+
return engine;
40+
}
41+
42+
/**
43+
* Captures upgrade requests for a http.Server.
44+
*
45+
* @param {http.Server} server
46+
* @param {Object} options
47+
* @return {Server} engine server
48+
* @api public
49+
*/
50+
51+
function attach(server, options: AttachOptions & ServerOptions) {
52+
const engine = new Server(options);
53+
engine.attach(server, options);
54+
return engine;
55+
}

lib/parser-v3/index.js renamed to lib/parser-v3/index.ts

+20-18
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var utf8 = require('./utf8');
99
/**
1010
* Current protocol version.
1111
*/
12-
exports.protocol = 3;
12+
export const protocol = 3;
1313

1414
const hasBinary = (packets) => {
1515
for (const packet of packets) {
@@ -24,7 +24,7 @@ const hasBinary = (packets) => {
2424
* Packet types.
2525
*/
2626

27-
var packets = exports.packets = {
27+
export const packets = {
2828
open: 0 // non-ws
2929
, close: 1 // non-ws
3030
, ping: 2
@@ -60,7 +60,7 @@ const EMPTY_BUFFER = Buffer.concat([]);
6060
* @api private
6161
*/
6262

63-
exports.encodePacket = function (packet, supportsBinary, utf8encode, callback) {
63+
export function encodePacket (packet, supportsBinary, utf8encode, callback) {
6464
if (typeof supportsBinary === 'function') {
6565
callback = supportsBinary;
6666
supportsBinary = null;
@@ -94,7 +94,7 @@ exports.encodePacket = function (packet, supportsBinary, utf8encode, callback) {
9494

9595
function encodeBuffer(packet, supportsBinary, callback) {
9696
if (!supportsBinary) {
97-
return exports.encodeBase64Packet(packet, callback);
97+
return encodeBase64Packet(packet, callback);
9898
}
9999

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

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

127-
exports.decodePacket = function (data, binaryType, utf8decode) {
127+
export function decodePacket (data, binaryType, utf8decode) {
128128
if (data === undefined) {
129129
return err;
130130
}
@@ -137,7 +137,7 @@ exports.decodePacket = function (data, binaryType, utf8decode) {
137137
type = data.charAt(0);
138138

139139
if (type === 'b') {
140-
return exports.decodeBase64Packet(data.substr(1), binaryType);
140+
return decodeBase64Packet(data.substr(1), binaryType);
141141
}
142142

143143
if (utf8decode) {
@@ -189,14 +189,15 @@ function tryDecode(data) {
189189
* @return {Object} with `type` and `data` (if any)
190190
*/
191191

192-
exports.decodeBase64Packet = function(msg, binaryType) {
192+
export function decodeBase64Packet (msg, binaryType) {
193193
var type = packetslist[msg.charAt(0)];
194194
var data = Buffer.from(msg.substr(1), 'base64');
195195
if (binaryType === 'arraybuffer') {
196196
var abv = new Uint8Array(data.length);
197197
for (var i = 0; i < abv.length; i++){
198198
abv[i] = data[i];
199199
}
200+
// @ts-ignore
200201
data = abv.buffer;
201202
}
202203
return { type: type, data: data };
@@ -218,22 +219,22 @@ exports.decodeBase64Packet = function(msg, binaryType) {
218219
* @api private
219220
*/
220221

221-
exports.encodePayload = function (packets, supportsBinary, callback) {
222+
export function encodePayload (packets, supportsBinary, callback) {
222223
if (typeof supportsBinary === 'function') {
223224
callback = supportsBinary;
224225
supportsBinary = null;
225226
}
226227

227228
if (supportsBinary && hasBinary(packets)) {
228-
return exports.encodePayloadAsBinary(packets, callback);
229+
return encodePayloadAsBinary(packets, callback);
229230
}
230231

231232
if (!packets.length) {
232233
return callback('0:');
233234
}
234235

235236
function encodeOne(packet, doneCallback) {
236-
exports.encodePacket(packet, supportsBinary, false, function(message) {
237+
encodePacket(packet, supportsBinary, false, function(message) {
237238
doneCallback(null, setLengthHeader(message));
238239
});
239240
}
@@ -273,9 +274,9 @@ function map(ary, each, done) {
273274
* @api public
274275
*/
275276

276-
exports.decodePayload = function (data, binaryType, callback) {
277+
export function decodePayload (data, binaryType, callback) {
277278
if (typeof data !== 'string') {
278-
return exports.decodePayloadAsBinary(data, binaryType, callback);
279+
return decodePayloadAsBinary(data, binaryType, callback);
279280
}
280281

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

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

313315
if (msg.length) {
314-
packet = exports.decodePacket(msg, binaryType, false);
316+
packet = decodePacket(msg, binaryType, false);
315317

316318
if (err.type === packet.type && err.data === packet.data) {
317319
// parser error in individual packet - ignoring payload
@@ -393,7 +395,7 @@ function arrayBufferToBuffer(data) {
393395
* @api private
394396
*/
395397

396-
exports.encodePayloadAsBinary = function (packets, callback) {
398+
export function encodePayloadAsBinary (packets, callback) {
397399
if (!packets.length) {
398400
return callback(EMPTY_BUFFER);
399401
}
@@ -430,7 +432,7 @@ function encodeOneBinaryPacket(p, doneCallback) {
430432
doneCallback(null, Buffer.concat([sizeBuffer, packet]));
431433
}
432434

433-
exports.encodePacket(p, true, true, onBinaryPacketEncode);
435+
encodePacket(p, true, true, onBinaryPacketEncode);
434436

435437
}
436438

@@ -444,7 +446,7 @@ function encodeOneBinaryPacket(p, doneCallback) {
444446
* @api public
445447
*/
446448

447-
exports.decodePayloadAsBinary = function (data, binaryType, callback) {
449+
export function decodePayloadAsBinary (data, binaryType, callback) {
448450
if (typeof binaryType === 'function') {
449451
callback = binaryType;
450452
binaryType = null;
@@ -478,6 +480,6 @@ exports.decodePayloadAsBinary = function (data, binaryType, callback) {
478480
var total = buffers.length;
479481
for (i = 0; i < total; i++) {
480482
var buffer = buffers[i];
481-
callback(exports.decodePacket(buffer, binaryType, true), i, total);
483+
callback(decodePacket(buffer, binaryType, true), i, total);
482484
}
483485
};

lib/parser-v3/utf8.js renamed to lib/parser-v3/utf8.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ function utf8decode(byteString, opts) {
203203
return ucs2encode(codePoints);
204204
}
205205

206-
module.exports = {
206+
export default {
207207
version: '2.1.2',
208208
encode: utf8encode,
209209
decode: utf8decode

0 commit comments

Comments
 (0)