Skip to content

Commit

Permalink
Merge PR #9 from 'nodech/ts-lint'
Browse files Browse the repository at this point in the history
  • Loading branch information
nodech committed Aug 31, 2023
2 parents 6c6b7fa + cad966e commit 2cc9ad3
Show file tree
Hide file tree
Showing 15 changed files with 110 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
run: npm run lint

- name: Lint types
run: npm run lint
run: npm run lint-types

test:
name: Test
Expand Down
6 changes: 2 additions & 4 deletions lib/backend-browser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict';

module.exports = {
Client: global.WebSocket || global.MozWebSocket,
EventSource: global.EventSource
};
exports.Client = global.WebSocket || global.MozWebSocket;
exports.EventSource = global.EventSource;
7 changes: 6 additions & 1 deletion lib/backend.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
'use strict';

module.exports = require('../vendor/faye-websocket');
/** @type {any} */
const WebSocket = require('../vendor/faye-websocket');

exports.WebSocket = WebSocket;
exports.Client = WebSocket.Client;
exports.EventSource = WebSocket.EventSource;
7 changes: 4 additions & 3 deletions lib/bsock.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
'use strict';

const WebSocket = require('./backend');
const Server = require('./server');
const Socket = require('./socket');
const {WebSocket, Client} = require('./backend');
const {Server} = require('./server');
const {Socket} = require('./socket');

exports.WebSocket = WebSocket;
exports.Client = Client;
exports.Server = Server;
exports.server = () => new Server();
exports.createServer = Server.createServer.bind(Server);
Expand Down
2 changes: 1 addition & 1 deletion lib/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,4 @@ class Frame {
Frame.types = types;
Frame.table = table;

module.exports = Frame;
exports.Frame = Frame;
17 changes: 14 additions & 3 deletions lib/packet.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ function isPlaceholder(obj) {
&& (obj.num >>> 0) === obj.num;
}

/**
* @param {Object} obj
* @returns {[string, Buffer[]]}
*/

function deconstruct(obj) {
const buffers = [];
const out = replace('', obj, buffers, new Map());
Expand Down Expand Up @@ -199,11 +204,17 @@ function reconstruct(str, buffers) {
});
}

function readChar(str, i) {
/**
* @param {String} str
* @param {Number} i
* @returns {[Number, Number]}
*/

function readChar(str, i) {
const ch = str.charCodeAt(i) - 0x30;

if (ch < 0 || ch > 9)
return -1;
return [0, -1];

return [i + 1, ch];
}
Expand Down Expand Up @@ -250,4 +261,4 @@ function readTo(str, i, ch) {
* Expose
*/

module.exports = Packet;
exports.Packet = Packet;
4 changes: 2 additions & 2 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

const assert = require('bsert');
const EventEmitter = require('events');
const Frame = require('./frame');
const {Frame} = require('./frame');

const MAX_MESSAGE = 100000000;

Expand Down Expand Up @@ -64,4 +64,4 @@ class Parser extends EventEmitter {
* Expose
*/

module.exports = Parser;
exports.Parser = Parser;
4 changes: 2 additions & 2 deletions lib/server-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Server extends EventEmitter {
this.mounts = [];
}

attach() {
attach(server) {
return this;
}

Expand Down Expand Up @@ -47,4 +47,4 @@ class Server extends EventEmitter {
}
}

module.exports = Server;
exports.Server = Server;
8 changes: 4 additions & 4 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

const assert = require('bsert');
const EventEmitter = require('events');
const Packet = require('./packet');
const WebSocket = require('./backend');
const Socket = require('./socket');
const {Packet} = require('./packet');
const {WebSocket} = require('./backend');
const {Socket} = require('./socket');

class Server extends EventEmitter {
constructor(options = {}) {
Expand Down Expand Up @@ -177,4 +177,4 @@ class Server extends EventEmitter {
}
}

module.exports = Server;
exports.Server = Server;
42 changes: 30 additions & 12 deletions lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
const assert = require('bsert');
const EventEmitter = require('events');
const WebSocket = require('./backend').Client;
const Packet = require('./packet');
const Frame = require('./frame');
const {Packet} = require('./packet');
const {Frame} = require('./frame');
const util = require('./util');
const Parser = require('./parser');
const {Parser} = require('./parser');
const codes = require('./codes');
const blacklist = require('./blacklist');

Expand Down Expand Up @@ -274,9 +274,7 @@ class Socket extends EventEmitter {
const reason = event.reason || 'Unknown reason';
const msg = `Websocket Closed: ${reason} (code=${code}).`;

const err = new Error(msg);
err.reason = event.reason || '';
err.code = event.code || 0;
const err = new SocketError(msg, reason, code);

this.emit('error', err);

Expand Down Expand Up @@ -492,14 +490,14 @@ class Socket extends EventEmitter {
this.close();
}

async handlePing() {
async handlePing(frame) {
if (!this.inbound)
throw new Error('Outbound socket sent a ping frame.');

this.sendPong();
}

async handlePong() {
async handlePong(frame) {
if (this.inbound)
throw new Error('Inbound socket sent a pong frame.');

Expand Down Expand Up @@ -595,6 +593,10 @@ class Socket extends EventEmitter {
* Packets
*/

/**
* @param {Packet} packet
*/

async handlePacket(packet) {
if (this.destroyed)
return undefined;
Expand Down Expand Up @@ -682,7 +684,8 @@ class Socket extends EventEmitter {
if (blacklist.hasOwnProperty(event))
throw new Error(`Cannot emit blacklisted event: ${event}.`);

this.events.emit(...args);
const rest = args.slice(1);
this.events.emit(event, ...rest);
} catch (e) {
this.emit('error', e);
this.sendError(-1, e);
Expand Down Expand Up @@ -734,7 +737,7 @@ class Socket extends EventEmitter {
const code = castCode(err.code);

if (id === -1) {
const e = new Error(msg);
const e = new SocketError(msg);
e.name = name;
e.type = type;
e.code = code;
Expand All @@ -749,7 +752,7 @@ class Socket extends EventEmitter {

this.jobs.delete(id);

const e = new Error(msg);
const e = new SocketError(msg);
e.name = name;
e.type = type;
e.code = code;
Expand Down Expand Up @@ -984,8 +987,11 @@ function readBinary(data) {
return;
}

// @ts-ignore
if (typeof Blob !== 'undefined' && Blob) {
// @ts-ignore
if (data instanceof Blob) {
// @ts-ignore
const reader = new FileReader();
reader.onloadend = () => {
const result = Buffer.from(reader.result);
Expand All @@ -1000,8 +1006,20 @@ function readBinary(data) {
});
}

class SocketError extends Error {
constructor(message, reason, code) {
super(message);
this.type = 'SocketError';
this.reason = reason || '';
this.code = code || 0;

if (Error.captureStackTrace)
Error.captureStackTrace(this, SocketError);
}
}

/*
* Expose
*/

module.exports = Socket;
exports.Socket = Socket;
1 change: 1 addition & 0 deletions lib/uws.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-nocheck
'use strict';

const assert = require('bsert');
Expand Down
9 changes: 8 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
"main": "./lib/bsock.js",
"scripts": {
"lint": "eslint lib/ test/socket-test.js",
"lint-types": "tsc -p .",
"test": "bmocha --reporter spec test/*-test.js"
},
"dependencies": {
"bsert": "~0.0.12"
},
"devDependencies": {
"bmocha": "^2.1.8"
"bmocha": "^2.1.8",
"bts-type-deps": "^0.0.3"
},
"engines": {
"node": ">=8.0.0"
Expand Down
31 changes: 31 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"include": [
"lib/**/*.js"
],
"compilerOptions": {
"rootDir": ".",
"target": "ES2020",
"lib": [
"ES2020"
],

"noEmit": true,

"allowJs": true,
"checkJs": true,
"maxNodeModuleJsDepth": 10,

"module": "commonjs",
"moduleResolution": "node",
"resolveJsonModule": true,

"stripInternal": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"noUnusedParameters": false,

"typeRoots": [
"node_modules/bts-type-deps/types"
]
}
}
1 change: 1 addition & 0 deletions vendor/faye-websocket.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-nocheck
/*!
* faye-websocket@0.11.4 - Standards-compliant WebSocket server and client
* Copyright (c) 2023, James Coglan (Apache-2.0)
Expand Down

0 comments on commit 2cc9ad3

Please sign in to comment.