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

Commit e29d451

Browse files
committed
push -> addMiddleware
1 parent 99fd157 commit e29d451

3 files changed

Lines changed: 41 additions & 31 deletions

File tree

src/DuplexJsonRpcEngine.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export class DuplexJsonRpcEngine {
5454
pushReceiverMiddleware(
5555
middleware: JsonRpcMiddleware<unknown, unknown>,
5656
): void {
57-
this.#receiver.push(middleware as JsonRpcMiddleware<unknown, unknown>);
57+
this.#receiver.addMiddleware(middleware as JsonRpcMiddleware<unknown, unknown>);
5858
}
5959

6060
/**
@@ -63,7 +63,7 @@ export class DuplexJsonRpcEngine {
6363
* @param middleware - The middleware function to add.
6464
*/
6565
pushSenderMiddleware(middleware: JsonRpcMiddleware<unknown, unknown>): void {
66-
this.#sender.push(middleware as JsonRpcMiddleware<unknown, unknown>);
66+
this.#sender.addMiddleware(middleware as JsonRpcMiddleware<unknown, unknown>);
6767
}
6868

6969
/**

src/JsonRpcEngine.test.ts

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe('JsonRpcEngine', () => {
5151
const middleware = jest.fn();
5252
const notificationHandler = jest.fn();
5353
const engine = new JsonRpcEngine({ notificationHandler });
54-
engine.push(middleware);
54+
engine.addMiddleware(middleware);
5555

5656
expect(
5757
await engine.handle({ jsonrpc, method: true } as any),
@@ -66,7 +66,7 @@ describe('JsonRpcEngine', () => {
6666
end();
6767
});
6868
const engine = new JsonRpcEngine();
69-
engine.push(middleware);
69+
engine.addMiddleware(middleware);
7070

7171
expect(await engine.handle({ jsonrpc, method: 'foo' })).toStrictEqual({
7272
jsonrpc,
@@ -80,7 +80,7 @@ describe('JsonRpcEngine', () => {
8080
const middleware = jest.fn();
8181
const notificationHandler = jest.fn();
8282
const engine = new JsonRpcEngine({ notificationHandler });
83-
engine.push(middleware);
83+
engine.addMiddleware(middleware);
8484

8585
expect(await engine.handle({ jsonrpc, method: 'foo' })).toBeUndefined();
8686
expect(notificationHandler).toHaveBeenCalledTimes(1);
@@ -131,7 +131,7 @@ describe('JsonRpcEngine', () => {
131131
it('handle: basic middleware test 1', async () => {
132132
const engine = new JsonRpcEngine();
133133

134-
engine.push(function (_req, res, _next, end) {
134+
engine.addMiddleware(function (_req, res, _next, end) {
135135
res.result = 42;
136136
end();
137137
});
@@ -151,7 +151,7 @@ describe('JsonRpcEngine', () => {
151151
it('handle: basic middleware test 2', async () => {
152152
const engine = new JsonRpcEngine();
153153

154-
engine.push(function (req, res, _next, end) {
154+
engine.addMiddleware(function (req, res, _next, end) {
155155
req.method = 'banana';
156156
res.result = 42;
157157
end();
@@ -173,7 +173,7 @@ describe('JsonRpcEngine', () => {
173173
it('handle (async): basic middleware test', async () => {
174174
const engine = new JsonRpcEngine();
175175

176-
engine.push(function (_req, res, _next, end) {
176+
engine.addMiddleware(function (_req, res, _next, end) {
177177
res.result = 42;
178178
end();
179179
});
@@ -188,7 +188,7 @@ describe('JsonRpcEngine', () => {
188188
it('allow null result', async () => {
189189
const engine = new JsonRpcEngine();
190190

191-
engine.push(function (_req, res, _next, end) {
191+
engine.addMiddleware(function (_req, res, _next, end) {
192192
res.result = null;
193193
end();
194194
});
@@ -208,12 +208,12 @@ describe('JsonRpcEngine', () => {
208208
it('interacting middleware test', async () => {
209209
const engine = new JsonRpcEngine();
210210

211-
engine.push(function (req: any, _res, next, _end) {
211+
engine.addMiddleware(function (req: any, _res, next, _end) {
212212
req.resultShouldBe = 42;
213213
next();
214214
});
215215

216-
engine.push(function (req: any, res, _next, end) {
216+
engine.addMiddleware(function (req: any, res, _next, end) {
217217
res.result = req.resultShouldBe;
218218
end();
219219
});
@@ -233,12 +233,12 @@ describe('JsonRpcEngine', () => {
233233
it('middleware ending request before all middlewares applied', async () => {
234234
const engine = new JsonRpcEngine();
235235

236-
engine.push(function (_req, res, _next, end) {
236+
engine.addMiddleware(function (_req, res, _next, end) {
237237
res.result = 42;
238238
end();
239239
});
240240

241-
engine.push(function (_req, _res, _next, _end) {
241+
engine.addMiddleware(function (_req, _res, _next, _end) {
242242
throw new Error('Test should have ended already.');
243243
});
244244

@@ -257,7 +257,7 @@ describe('JsonRpcEngine', () => {
257257
it('erroring middleware test: end(error)', async () => {
258258
const engine = new JsonRpcEngine();
259259

260-
engine.push(function (_req, _res, _next, end) {
260+
engine.addMiddleware(function (_req, _res, _next, end) {
261261
end(new Error('no bueno'));
262262
});
263263

@@ -277,7 +277,7 @@ describe('JsonRpcEngine', () => {
277277
it('erroring middleware test: res.error -> next()', async () => {
278278
const engine = new JsonRpcEngine();
279279

280-
engine.push(function (_req, res, next, _end) {
280+
engine.addMiddleware(function (_req, res, next, _end) {
281281
res.error = ethErrors.rpc.internal({ message: 'foobar' });
282282
next();
283283
});
@@ -298,7 +298,7 @@ describe('JsonRpcEngine', () => {
298298
it('erroring middleware test: res.error -> end()', async () => {
299299
const engine = new JsonRpcEngine();
300300

301-
engine.push(function (_req, res, _next, end) {
301+
engine.addMiddleware(function (_req, res, _next, end) {
302302
res.error = ethErrors.rpc.internal({ message: 'foobar' });
303303
end();
304304
});
@@ -319,7 +319,7 @@ describe('JsonRpcEngine', () => {
319319
it('erroring middleware test: non-function passsed to next()', async () => {
320320
const engine = new JsonRpcEngine();
321321

322-
engine.push(function (_req, _res, next, _end) {
322+
engine.addMiddleware(function (_req, _res, next, _end) {
323323
next(true as any);
324324
});
325325

@@ -358,7 +358,7 @@ describe('JsonRpcEngine', () => {
358358
it('handle: batch payloads', async () => {
359359
const engine = new JsonRpcEngine();
360360

361-
engine.push(function (req, res, _next, end) {
361+
engine.addMiddleware(function (req, res, _next, end) {
362362
if (req.id === 4) {
363363
delete res.result;
364364
res.error = ethErrors.rpc.internal({ message: 'foobar' });
@@ -393,7 +393,7 @@ describe('JsonRpcEngine', () => {
393393
it('handle: batch payloads (async signature)', async () => {
394394
const engine = new JsonRpcEngine();
395395

396-
engine.push(function (req, res, _next, end) {
396+
engine.addMiddleware(function (req, res, _next, end) {
397397
if (req.id === 4) {
398398
delete res.result;
399399
res.error = ethErrors.rpc.internal({ message: 'foobar' });
@@ -423,7 +423,7 @@ describe('JsonRpcEngine', () => {
423423
it('handle: batch payload with bad request object', async () => {
424424
const engine = new JsonRpcEngine();
425425

426-
engine.push(function (req, res, _next, end) {
426+
engine.addMiddleware(function (req, res, _next, end) {
427427
res.result = req.id;
428428
return end();
429429
});
@@ -456,14 +456,14 @@ describe('JsonRpcEngine', () => {
456456
it('return handlers test', async () => {
457457
const engine = new JsonRpcEngine();
458458

459-
engine.push(function (_req, res: any, next, _end) {
459+
engine.addMiddleware(function (_req, res: any, next, _end) {
460460
next(function (cb) {
461461
res.sawReturnHandler = true;
462462
cb();
463463
});
464464
});
465465

466-
engine.push(function (_req, res, _next, end) {
466+
engine.addMiddleware(function (_req, res, _next, end) {
467467
res.result = true;
468468
end();
469469
});
@@ -485,23 +485,23 @@ describe('JsonRpcEngine', () => {
485485

486486
const events: string[] = [];
487487

488-
engine.push(function (_req, _res, next, _end) {
488+
engine.addMiddleware(function (_req, _res, next, _end) {
489489
events.push('1-next');
490490
next(function (cb) {
491491
events.push('1-return');
492492
cb();
493493
});
494494
});
495495

496-
engine.push(function (_req, _res, next, _end) {
496+
engine.addMiddleware(function (_req, _res, next, _end) {
497497
events.push('2-next');
498498
next(function (cb) {
499499
events.push('2-return');
500500
cb();
501501
});
502502
});
503503

504-
engine.push(function (_req, res, _next, end) {
504+
engine.addMiddleware(function (_req, res, _next, end) {
505505
events.push('3-end');
506506
res.result = true;
507507
end();
@@ -527,14 +527,14 @@ describe('JsonRpcEngine', () => {
527527

528528
let sawNextReturnHandlerCalled = false;
529529

530-
engine.push(function (_req, _res, next, _end) {
530+
engine.addMiddleware(function (_req, _res, next, _end) {
531531
next(function (cb) {
532532
sawNextReturnHandlerCalled = true;
533533
cb();
534534
});
535535
});
536536

537-
engine.push(function (_req, _res, _next, end) {
537+
engine.addMiddleware(function (_req, _res, _next, end) {
538538
end(new Error('boom'));
539539
});
540540

@@ -552,13 +552,13 @@ describe('JsonRpcEngine', () => {
552552
it('handles error in next handler', async () => {
553553
const engine = new JsonRpcEngine();
554554

555-
engine.push(function (_req, _res, next, _end) {
555+
engine.addMiddleware(function (_req, _res, next, _end) {
556556
next(function (_cb) {
557557
throw new Error('foo');
558558
});
559559
});
560560

561-
engine.push(function (_req, res, _next, end) {
561+
engine.addMiddleware(function (_req, res, _next, end) {
562562
res.result = 42;
563563
end();
564564
});
@@ -577,7 +577,7 @@ describe('JsonRpcEngine', () => {
577577
it('handles failure to end request', async () => {
578578
const engine = new JsonRpcEngine();
579579

580-
engine.push(function (_req, res, next, _end) {
580+
engine.addMiddleware(function (_req, res, next, _end) {
581581
res.result = 42;
582582
next();
583583
});

src/JsonRpcEngine.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,20 @@ export class JsonRpcEngine extends SafeEventEmitter {
8686
*
8787
* @param middleware - The middleware function to add.
8888
*/
89-
push<Params, Result>(middleware: JsonRpcMiddleware<Params, Result>): void {
89+
addMiddleware<Params, Result>(middleware: JsonRpcMiddleware<Params, Result>): void {
9090
this._middleware.push(middleware as JsonRpcMiddleware<unknown, unknown>);
9191
}
9292

93+
/**
94+
* Add a middleware function to the engine's middleware stack.
95+
*
96+
* @deprecated Use {@link JsonRpcEngine.addMiddleware} instead.
97+
* @param middleware - The middleware function to add.
98+
*/
99+
push<Params, Result>(middleware: JsonRpcMiddleware<Params, Result>): void {
100+
return this.addMiddleware(middleware);
101+
}
102+
93103
/**
94104
* Handle a JSON-RPC request, and return a response.
95105
*

0 commit comments

Comments
 (0)