Skip to content

Commit 7f7ef18

Browse files
committed
prettier eslint and the like
1 parent 6fefe5e commit 7f7ef18

13 files changed

+1457
-223
lines changed

.editorconfig

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
root = true
2+
3+
[*]
4+
end_of_line = lf
5+
insert_final_newline = true
6+
7+
[*.{js,ts}]
8+
indent_style = space
9+
indent_size = 2
10+

.eslintignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.d.ts
2+
api.js

.eslintrc.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module.exports = {
2+
extends: ['airbnb', 'plugin:@typescript-eslint/recommended'],
3+
parser: '@typescript-eslint/parser',
4+
plugins: ['@typescript-eslint', 'prettier'],
5+
settings: {
6+
'import/parsers': {
7+
'@typescript-eslint/parser': ['.ts'],
8+
},
9+
'import/resolver': {
10+
typescript: {},
11+
},
12+
},
13+
rules: {
14+
'import/prefer-default-export': 'off',
15+
indent: 'off',
16+
'@typescript-eslint/indent': ['error', 2],
17+
'max-len': ['error', { code: 120 }],
18+
'@typescript-eslint/explicit-function-return-type': 'off',
19+
'@typescript-eslint/no-non-null-assertion': 'error',
20+
'operator-linebreak': 'off',
21+
'no-param-reassign': ['error', { props: false }],
22+
'object-curly-newline': 'off',
23+
},
24+
};

.prettierrc.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
trailingComma: 'all',
3+
tabWidth: 2,
4+
semi: true,
5+
singleQuote: true,
6+
printWidth: 100,
7+
bracketSpacing: true,
8+
arrowParens: 'always',
9+
};

.vscode/settings.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "eslint.validate": ["javascript", { "language": "typescript", "autoFix": true }] }

EIOCompat.ts

+68-49
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* global Event CloseEvent MessageEvent */
2+
13
import * as eio from 'engine.io-client';
24
import * as urllib from 'url';
35

@@ -28,24 +30,41 @@ const readyStateStringToValue = new Map([
2830
['closed', 3],
2931
]);
3032

33+
Object.keys(readyStateStringToValue);
34+
3135
export class EIOCompat {
32-
public onclose: ((ev: CloseEvent) => any) | null;
33-
public onerror: ((ev: Event) => any) | null;
34-
public onmessage: ((ev: MessageEvent) => any) | null;
35-
public onopen: ((ev: Event) => any) | null;
36+
public onclose: ((ev: CloseEvent) => unknown) | null;
37+
38+
public onerror: ((ev: Event) => unknown) | null;
39+
40+
public onmessage: ((ev: MessageEvent) => unknown) | null;
41+
42+
public onopen: ((ev: Event) => unknown) | null;
43+
3644
public url: string;
45+
3746
public extensions: string;
47+
3848
public protocol: string;
49+
3950
public bufferedAmount: number;
51+
4052
public binaryType: BinaryType;
53+
4154
public readyState: number;
55+
4256
public incomingSequence: number;
57+
4358
public outOfOrderQueue: { [sequence: number]: ArrayBuffer };
59+
4460
public outgoingSequence: number;
4561

4662
readonly CLOSED = 3;
63+
4764
readonly CLOSING = 2;
65+
4866
readonly OPEN = 1;
67+
4968
readonly CONNECTING = 0;
5069

5170
private eioSocket: eio.Socket;
@@ -78,7 +97,7 @@ export class EIOCompat {
7897
}
7998
});
8099

81-
this.eioSocket.on('close', reason => {
100+
this.eioSocket.on('close', (reason) => {
82101
this.setReadyState();
83102
if (this.onclose != null) {
84103
const event = new CloseEvent('close', {
@@ -91,49 +110,52 @@ export class EIOCompat {
91110
}
92111
});
93112

94-
this.eioSocket.on('message', data => {
113+
this.eioSocket.on('message', (data) => {
95114
this.setReadyState();
96-
if (this.onmessage != null) {
97-
if (typeof data === 'string') {
98-
throw new Error('expected data to be ArrayBuffer not string');
99-
}
115+
if (this.onmessage == null) {
116+
return;
117+
}
118+
119+
if (typeof data === 'string') {
120+
throw new Error('expected data to be ArrayBuffer not string');
121+
}
122+
123+
const view = new DataView(data);
124+
const sequence = view.getUint32(0);
125+
126+
if (this.incomingSequence !== sequence) {
127+
// We didn't get the message we expected
128+
// put it in the queue until we get the expected message
129+
this.outOfOrderQueue[sequence] = data.slice(sequenceBytesCount);
130+
131+
return;
132+
}
133+
134+
this.incomingSequence = sequence + 1;
100135

101-
const view = new DataView(data);
102-
const sequence = view.getUint32(0);
136+
const message = new MessageEvent('message', {
137+
data: data.slice(sequenceBytesCount),
138+
});
103139

104-
if (this.incomingSequence !== sequence) {
105-
// We didn't get the message we expected
106-
// put it in the queue until we get the expected message
107-
this.outOfOrderQueue[sequence] = data.slice(sequenceBytesCount);
140+
const onmessage = this.onmessage.bind(this);
108141

109-
return;
110-
}
142+
onmessage(message);
111143

112-
this.incomingSequence = sequence + 1;
144+
const queuedSequences = Object.keys(this.outOfOrderQueue);
145+
if (queuedSequences.length > 0) {
146+
// We got the message we expected but we have other messages
147+
// that were out of order and queued up
148+
queuedSequences.sort().forEach((seq) => {
149+
onmessage(
150+
new MessageEvent('message', {
151+
data: this.outOfOrderQueue[+seq],
152+
}),
153+
);
113154

114-
const message = new MessageEvent('message', {
115-
data: data.slice(sequenceBytesCount),
155+
this.incomingSequence = +seq + 1;
116156
});
117157

118-
this.onmessage.call(this, message);
119-
120-
const queuedSequences = Object.keys(this.outOfOrderQueue);
121-
if (queuedSequences.length > 0) {
122-
// We got the message we expected but we have other messages
123-
// that were out of order and queued up
124-
for (let seq of queuedSequences.sort()) {
125-
this.onmessage.call(
126-
this,
127-
new MessageEvent('message', {
128-
data: this.outOfOrderQueue[+seq],
129-
}),
130-
);
131-
132-
this.incomingSequence = +seq + 1;
133-
}
134-
135-
this.outOfOrderQueue = {};
136-
}
158+
this.outOfOrderQueue = {};
137159
}
138160
});
139161

@@ -147,21 +169,18 @@ export class EIOCompat {
147169
}
148170

149171
setReadyState() {
150-
// @ts-ignore
172+
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
173+
// @ts-ignore engine.io-client doesn't have typing for readyState :/
151174
this.readyState = readyStateStringToValue.get(this.eioSocket.readyState);
152175
}
153176

154177
send(buffer: ArrayBuffer) {
155-
const sequencedBuffer = new ArrayBuffer(
156-
sequenceBytesCount + buffer.byteLength,
157-
);
158-
new Uint8Array(sequencedBuffer).set(
159-
new Uint8Array(buffer),
160-
sequenceBytesCount,
161-
);
178+
const sequencedBuffer = new ArrayBuffer(sequenceBytesCount + buffer.byteLength);
179+
new Uint8Array(sequencedBuffer).set(new Uint8Array(buffer), sequenceBytesCount);
162180

163181
const view = new DataView(sequencedBuffer);
164-
view.setUint32(0, this.outgoingSequence++);
182+
view.setUint32(0, this.outgoingSequence);
183+
this.outgoingSequence += 1;
165184

166185
this.eioSocket.send(sequencedBuffer);
167186
this.setReadyState();

channel.ts

+15-13
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
import { api } from './api';
21
import { EventEmitter } from 'events';
2+
import { api } from './api';
33
import { createDeferred, Deferred } from './deferred';
44

55
class Channel extends EventEmitter {
6-
public state:
7-
| api.OpenChannelRes.State.CREATED
8-
| api.OpenChannelRes.State.ATTACHED
9-
| null;
6+
public state: api.OpenChannelRes.State.CREATED | api.OpenChannelRes.State.ATTACHED | null;
7+
108
public id: number | null;
9+
1110
public isOpen: boolean;
1211

1312
private sendQueue: Array<api.ICommand>;
13+
1414
private sendToClient: (cmd: api.Command) => void;
15+
1516
private requestMap: { [ref: string]: Deferred<api.Command> };
1617

1718
constructor() {
@@ -31,7 +32,7 @@ class Channel extends EventEmitter {
3132
* @param cmdJson shape of a command see [[api.ICommand]]
3233
*/
3334
public send = (cmdJson: api.ICommand) => {
34-
cmdJson['channel'] = this.id;
35+
cmdJson.channel = this.id;
3536
this.sendToClient(api.Command.create(cmdJson));
3637
};
3738

@@ -47,10 +48,10 @@ class Channel extends EventEmitter {
4748
.toString()
4849
.split('.')[1],
4950
).toString(36);
50-
const deferred = createDeferred();
51+
const deferred = createDeferred<api.Command>();
5152
this.requestMap[ref] = deferred;
5253

53-
cmdJson['ref'] = ref;
54+
cmdJson.ref = ref;
5455
this.send(cmdJson);
5556

5657
return deferred.promise;
@@ -100,11 +101,12 @@ class Channel extends EventEmitter {
100101
this.state = state;
101102
this.isOpen = true;
102103

103-
let cmd;
104-
// tslint:disable-next-line no-conditional-assignment
105-
while ((cmd = this.sendQueue.shift())) {
104+
let cmd = this.sendQueue.shift();
105+
while (cmd) {
106106
// It will set the right channel id and send it
107107
this.send(cmd);
108+
109+
cmd = this.sendQueue.shift();
108110
}
109111

110112
this.emit('open');
@@ -139,10 +141,10 @@ class Channel extends EventEmitter {
139141
* Called when the channel is or client is closed
140142
*/
141143
public onClose = () => {
142-
for (const ref of Object.keys(this.requestMap)) {
144+
Object.keys(this.requestMap).forEach((ref) => {
143145
this.requestMap[ref].reject(new Error('Channel closed'));
144146
delete this.requestMap[ref];
145-
}
147+
});
146148

147149
this.isOpen = false;
148150
this.emit('close');

0 commit comments

Comments
 (0)