Skip to content
This repository was archived by the owner on Nov 9, 2023. It is now read-only.

Commit 4edb9cc

Browse files
committed
update eslint and other packages to be aligned with core monorepo
1 parent 1c2c521 commit 4edb9cc

File tree

5 files changed

+990
-598
lines changed

5 files changed

+990
-598
lines changed

package.json

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,28 @@
3434
"@lavamoat/allow-scripts": "^2.3.1",
3535
"@lavamoat/preinstall-always-fail": "^1.0.0",
3636
"@metamask/auto-changelog": "^3.0.0",
37-
"@metamask/eslint-config": "^9.0.0",
38-
"@metamask/eslint-config-jest": "^9.0.0",
39-
"@metamask/eslint-config-nodejs": "^9.0.0",
40-
"@metamask/eslint-config-typescript": "^9.0.1",
37+
"@metamask/eslint-config": "^12.0.0",
38+
"@metamask/eslint-config-jest": "^12.0.0",
39+
"@metamask/eslint-config-nodejs": "^12.0.0",
40+
"@metamask/eslint-config-typescript": "^12.0.0",
4141
"@types/jest": "^26.0.13",
4242
"@types/node": "^17.0.23",
4343
"@types/readable-stream": "^2.3.9",
44-
"@typescript-eslint/eslint-plugin": "^4.21.0",
45-
"@typescript-eslint/parser": "^4.21.0",
44+
"@typescript-eslint/eslint-plugin": "^5.30.7",
45+
"@typescript-eslint/parser": "^5.30.7",
4646
"depcheck": "^1.4.5",
47-
"eslint": "^7.23.0",
48-
"eslint-config-prettier": "^8.1.0",
49-
"eslint-plugin-import": "^2.22.1",
50-
"eslint-plugin-jest": "^24.3.4",
51-
"eslint-plugin-jsdoc": "^36.1.0",
52-
"eslint-plugin-node": "^11.1.0",
53-
"eslint-plugin-prettier": "^3.3.1",
47+
"eslint": "^8.44.0",
48+
"eslint-config-prettier": "^8.5.0",
49+
"eslint-plugin-import": "^2.26.0",
50+
"eslint-plugin-jest": "^27.1.5",
51+
"eslint-plugin-jsdoc": "^39.3.3",
52+
"eslint-plugin-n": "^15.7.0",
53+
"eslint-plugin-prettier": "^4.2.1",
54+
"eslint-plugin-promise": "^6.1.1",
5455
"extension-port-stream": "^2.0.1",
5556
"jest": "^27.5.1",
5657
"jest-it-up": "^2.0.2",
57-
"prettier": "^2.2.1",
58+
"prettier": "^2.7.1",
5859
"prettier-plugin-packagejson": "^2.2.17",
5960
"rimraf": "^3.0.2",
6061
"ts-jest": "^27.1.4",

src/createEngineStream.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
import type { JsonRpcEngine } from '@metamask/json-rpc-engine';
2+
import type { JsonRpcRequest } from '@metamask/utils';
13
import { Duplex } from 'readable-stream';
2-
import { JsonRpcEngine } from '@metamask/json-rpc-engine';
3-
import { JsonRpcRequest, JsonRpcParams } from '@metamask/utils';
44

5-
interface EngineStreamOptions {
5+
type EngineStreamOptions = {
66
engine: JsonRpcEngine;
7-
}
7+
};
88

99
/**
1010
* Takes a JsonRpcEngine and returns a Duplex stream wrapping it.
@@ -14,7 +14,7 @@ interface EngineStreamOptions {
1414
* @returns The stream wrapping the engine.
1515
*/
1616
export default function createEngineStream(opts: EngineStreamOptions): Duplex {
17-
if (!opts || !opts.engine) {
17+
if (!opts?.engine) {
1818
throw new Error('Missing engine parameter!');
1919
}
2020

@@ -33,16 +33,16 @@ export default function createEngineStream(opts: EngineStreamOptions): Duplex {
3333
*
3434
* @param req - The JSON-rpc request.
3535
* @param _encoding - The stream encoding, not used.
36-
* @param cb - The stream write callback.
36+
* @param streamWriteCallback - The stream write callback.
3737
*/
3838
function write(
39-
req: JsonRpcRequest<JsonRpcParams>,
39+
req: JsonRpcRequest,
4040
_encoding: unknown,
41-
cb: (error?: Error | null) => void,
41+
streamWriteCallback: (error?: Error | null) => void,
4242
) {
4343
engine.handle(req, (_err, res) => {
4444
stream.push(res);
4545
});
46-
cb();
46+
streamWriteCallback();
4747
}
4848
}

src/createStreamMiddleware.ts

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
1-
import SafeEventEmitter from '@metamask/safe-event-emitter';
2-
import { Duplex } from 'readable-stream';
31
import type {
42
JsonRpcEngineNextCallback,
53
JsonRpcEngineEndCallback,
64
JsonRpcMiddleware,
75
} from '@metamask/json-rpc-engine';
8-
6+
import SafeEventEmitter from '@metamask/safe-event-emitter';
97
import type {
108
JsonRpcNotification,
119
JsonRpcParams,
1210
JsonRpcRequest,
1311
PendingJsonRpcResponse,
1412
} from '@metamask/utils';
13+
import { Duplex } from 'readable-stream';
1514

16-
interface IdMapValue {
17-
req: JsonRpcRequest<JsonRpcParams>;
15+
type IdMapValue = {
16+
req: JsonRpcRequest;
1817
res: PendingJsonRpcResponse<JsonRpcParams>;
1918
next: JsonRpcEngineNextCallback;
2019
end: JsonRpcEngineEndCallback;
2120
retryCount?: number;
22-
}
21+
};
2322

24-
interface IdMap {
23+
type IdMap = {
2524
[requestId: string]: IdMapValue;
26-
}
25+
};
2726

28-
interface Options {
27+
type Options = {
2928
retryOnMessage?: string;
30-
}
29+
};
3130

3231
/**
3332
* Creates a JsonRpcEngine middleware with an associated Duplex stream and
@@ -67,7 +66,7 @@ export default function createStreamMiddleware(options: Options = {}) {
6766
*
6867
* @param req - The JSON-RPC request object.
6968
*/
70-
function sendToStream(req: JsonRpcRequest<JsonRpcParams>) {
69+
function sendToStream(req: JsonRpcRequest) {
7170
// TODO: limiting retries could be implemented here
7271
stream.push(req);
7372
}
@@ -77,28 +76,26 @@ export default function createStreamMiddleware(options: Options = {}) {
7776
*
7877
* @param res - The JSON-RPC response object.
7978
* @param _encoding - The stream encoding, not used.
80-
* @param cb - The stream write callback.
79+
* @param streamWriteCallback - The stream write callback.
8180
*/
8281
function processMessage(
8382
res: PendingJsonRpcResponse<JsonRpcParams>,
8483
_encoding: unknown,
85-
cb: (error?: Error | null) => void,
84+
streamWriteCallback: (error?: Error | null) => void,
8685
) {
87-
let err: Error | null = null;
86+
let errorObj: Error | null = null;
8887
try {
8988
const isNotification = !res.id;
9089
if (isNotification) {
91-
processNotification(
92-
res as unknown as JsonRpcNotification<JsonRpcParams>,
93-
);
90+
processNotification(res as unknown as JsonRpcNotification);
9491
} else {
9592
processResponse(res);
9693
}
9794
} catch (_err) {
98-
err = _err as Error;
95+
errorObj = _err as Error;
9996
}
10097
// continue processing stream
101-
cb(err);
98+
streamWriteCallback(errorObj);
10299
}
103100

104101
/**
@@ -107,13 +104,14 @@ export default function createStreamMiddleware(options: Options = {}) {
107104
* @param res - The response to process.
108105
*/
109106
function processResponse(res: PendingJsonRpcResponse<JsonRpcParams>) {
110-
const context = idMap[res.id as unknown as string];
107+
const responseId = res.id as unknown as string;
108+
const context = idMap[responseId];
111109
if (!context) {
112-
console.warn(`StreamMiddleware - Unknown response id "${res.id}"`);
110+
console.warn(`StreamMiddleware - Unknown response id "${responseId}"`);
113111
return;
114112
}
115113

116-
delete idMap[res.id as unknown as string];
114+
delete idMap[responseId];
117115
// copy whole res onto original res
118116
Object.assign(context.res, res);
119117
// run callback on empty stack,
@@ -126,7 +124,7 @@ export default function createStreamMiddleware(options: Options = {}) {
126124
*
127125
* @param notif - The notification to process.
128126
*/
129-
function processNotification(notif: JsonRpcNotification<JsonRpcParams>) {
127+
function processNotification(notif: JsonRpcNotification) {
130128
if (options?.retryOnMessage && notif.method === options.retryOnMessage) {
131129
retryStuckRequests();
132130
}

src/index.test.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import { Duplex } from 'stream';
21
import { JsonRpcEngine } from '@metamask/json-rpc-engine';
32
import PortStream from 'extension-port-stream';
3+
/* eslint import/no-nodejs-modules: ["error", {"allow": ["stream"]}] */
4+
import type { Duplex } from 'stream';
45
import type { Runtime } from 'webextension-polyfill-ts';
6+
57
import { createStreamMiddleware, createEngineStream } from '.';
68

7-
const artificialDelay = (t = 0) =>
8-
new Promise((resolve) => setTimeout(resolve, t));
9+
const artificialDelay = async (time = 0) =>
10+
new Promise((resolve) => setTimeout(resolve, time));
911
// eslint-disable-next-line @typescript-eslint/no-empty-function
1012
const noop = function (_a: any) {};
1113

@@ -35,10 +37,10 @@ describe('createStreamMiddleware', () => {
3537
() => {
3638
reject(new Error('should not call next'));
3739
},
38-
(err) => {
40+
(errorObj) => {
3941
try {
4042
// eslint-disable-next-line jest/no-restricted-matchers
41-
expect(err).toBeFalsy();
43+
expect(errorObj).toBeFalsy();
4244
expect(initRes).toStrictEqual(res);
4345
} catch (error) {
4446
return reject(error);
@@ -73,8 +75,8 @@ describe('createEngineStream', () => {
7375
return resolve();
7476
});
7577

76-
stream.on('error', (err) => {
77-
reject(err);
78+
stream.on('error', (errorObj) => {
79+
reject(errorObj);
7880
});
7981

8082
stream.write(req);
@@ -128,15 +130,15 @@ describe('retry logic in middleware connected to a port', () => {
128130
messages = [];
129131
const extensionPort = {
130132
onMessage: {
131-
addListener: (cb: any) => {
132-
messageConsumer = cb;
133+
addListener: (messageCallback: any) => {
134+
messageConsumer = messageCallback;
133135
},
134136
},
135137
onDisconnect: {
136138
addListener: noop,
137139
},
138-
postMessage(m: any) {
139-
messages.push(m);
140+
postMessage(message: any) {
141+
messages.push(message);
140142
},
141143
};
142144

@@ -159,6 +161,8 @@ describe('retry logic in middleware connected to a port', () => {
159161

160162
// Initially sent once
161163
const responsePromise1 = engineA?.handle(req1);
164+
// intentionally not awaited
165+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
162166
engineA?.handle(req2);
163167
await artificialDelay();
164168

@@ -193,6 +197,8 @@ describe('retry logic in middleware connected to a port', () => {
193197
const req = { id: 1, jsonrpc, method: 'test' };
194198

195199
// Initially sent once, message count at 1
200+
// intentionally not awaited
201+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
196202
engineA?.handle(req);
197203
await artificialDelay();
198204
expect(messages).toHaveLength(1);
@@ -242,6 +248,8 @@ describe('retry logic in middleware connected to a port', () => {
242248
const req = { id: undefined, jsonrpc, method: 'test' };
243249

244250
// Initially sent once, message count at 1
251+
// intentionally not awaited
252+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
245253
engineA?.handle(req);
246254
await artificialDelay();
247255
expect(messages).toHaveLength(1);

0 commit comments

Comments
 (0)