Skip to content

Commit 1f7b2ae

Browse files
committed
feat: defaultMiddleware now encapsulates timeoutMiddleware.
fix: jests fixed chore: renamed to RPCResponseResult and RPCRequestParams
1 parent bfcd107 commit 1f7b2ae

File tree

4 files changed

+180
-107
lines changed

4 files changed

+180
-107
lines changed

src/middleware.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ function timeoutMiddlewareServer(
9595
>({
9696
transform: (chunk, controller) => {
9797
controller.enqueue(chunk);
98-
if (utils.isRPCRequestParams(chunk.params) && forwardFirst) {
98+
if (forwardFirst) {
9999
forwardFirst = false;
100-
const clientTimeout = chunk.params?.metadata?.timeout;
100+
const clientTimeout = chunk.metadata?.timeout;
101101

102102
if (clientTimeout == null) return;
103103
if (clientTimeout < currentTimeout) ctx.timer.reset(clientTimeout);
@@ -111,9 +111,9 @@ function timeoutMiddlewareServer(
111111
transform: (chunk, controller) => {
112112
if (reverseFirst) {
113113
reverseFirst = false;
114-
if ('result' in chunk && utils.isRPCResponseResult(chunk.result)) {
115-
if (chunk.result.metadata == null) chunk.result.metadata = {};
116-
chunk.result.metadata.timeout = currentTimeout;
114+
if ('result' in chunk) {
115+
if (chunk.metadata == null) chunk.metadata = {};
116+
chunk.metadata.timeout = currentTimeout;
117117
}
118118
}
119119
controller.enqueue(chunk);
@@ -139,16 +139,13 @@ function timeoutMiddlewareClient(
139139
let forwardFirst = true;
140140
let reverseFirst = true;
141141
return {
142-
forward: new TransformStream<
143-
JSONRPCRequest,
144-
JSONRPCRequest
145-
>({
142+
forward: new TransformStream<JSONRPCRequest, JSONRPCRequest>({
146143
transform: (chunk, controller) => {
147-
if (utils.isRPCRequestParams(chunk.params) && forwardFirst) {
144+
if (forwardFirst) {
148145
forwardFirst = false;
149-
if (chunk.params == null) chunk.params = {};
150-
if (chunk.params.metadata == null) chunk.params.metadata = {};
151-
(chunk.params.metadata as any).timeout = currentTimeout;
146+
if (chunk == null) chunk = { jsonrpc: '2.0', method: '' };
147+
if (chunk.metadata == null) chunk.metadata = {};
148+
(chunk.metadata as any).timeout = currentTimeout;
152149
}
153150
controller.enqueue(chunk);
154151
},
@@ -161,7 +158,7 @@ function timeoutMiddlewareClient(
161158
controller.enqueue(chunk);
162159
if (reverseFirst) {
163160
reverseFirst = false;
164-
if ('result' in chunk && utils.isRPCResponseResult(chunk.result)) {
161+
if ('result' in chunk) {
165162
const clientTimeout = chunk.result?.metadata?.timeout;
166163
if (clientTimeout == null) return;
167164
if (clientTimeout < currentTimeout) ctx.timer.reset(clientTimeout);

src/types.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type JSONRPCRequestMessage<T extends JSONValue = JSONValue> = {
3838
* SHOULD NOT contain fractional parts [2]
3939
*/
4040
id: string | number | null;
41-
};
41+
} & RPCResponseResult;
4242

4343
/**
4444
* This is the JSON RPC notification object. this is used for a request that
@@ -60,7 +60,7 @@ type JSONRPCRequestNotification<T extends JSONValue = JSONValue> = {
6060
* This member MAY be omitted.
6161
*/
6262
params?: T;
63-
};
63+
} & RPCResponseResult;
6464

6565
/**
6666
* This is the JSON RPC response result object. It contains the response data for a
@@ -84,7 +84,7 @@ type JSONRPCResponseResult<T extends JSONValue = JSONValue> = {
8484
* it MUST be Null.
8585
*/
8686
id: string | number | null;
87-
};
87+
} & RPCResponseResult;
8888

8989
/**
9090
* This is the JSON RPC response Error object. It contains any errors that have
@@ -118,19 +118,19 @@ type RPCRequestParams<T extends Record<string, JSONValue> = ObjectEmpty> = {
118118
[Key: string]: JSONValue;
119119
} & Partial<{
120120
authorization: string;
121-
timeout: number;
121+
timeout: number | null;
122122
}>;
123-
} & Omit <T, 'metadata'>;
123+
} & Omit<T, 'metadata'>;
124124

125125
// Prevent overwriting the metadata type with `Omit<>`
126126
type RPCResponseResult<T extends Record<string, JSONValue> = ObjectEmpty> = {
127127
metadata?: {
128128
[Key: string]: JSONValue;
129129
} & Partial<{
130130
authorization: string;
131-
timeout: number;
131+
timeout: number | null;
132132
}>;
133-
} & Omit <T, 'metadata'>;
133+
} & Omit<T, 'metadata'>;
134134

135135
/**
136136
* This is a JSON RPC error object, it encodes the error data for the JSONRPCResponseError object.

tests/RPCClient.test.ts

Lines changed: 80 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -123,18 +123,31 @@ describe(`${RPCClient.name}`, () => {
123123
}
124124
await writable.close();
125125

126-
const expectedMessages: Array<JSONRPCRequestMessage> = messages.map((v) => {
127-
const request: JSONRPCRequestMessage = {
128-
jsonrpc: '2.0',
129-
method: methodName,
130-
id: null,
131-
...(v.result === undefined ? {} : { params: v.result }),
132-
};
133-
return request;
134-
});
126+
const expectedMessages: Array<JSONRPCRequestMessage> = messages.map(
127+
(v, i) => {
128+
if (i === 0) {
129+
return {
130+
jsonrpc: '2.0',
131+
metadata: { timeout: null },
132+
method: methodName,
133+
id: null,
134+
...(v.result === undefined ? {} : { params: v.result }),
135+
};
136+
} else {
137+
return {
138+
jsonrpc: '2.0',
139+
method: methodName,
140+
id: null,
141+
...(v.result === undefined ? {} : { params: v.result }),
142+
};
143+
}
144+
},
145+
);
146+
135147
const outputMessages = (await outputResult).map((v) =>
136148
JSON.parse(v.toString()),
137149
);
150+
138151
expect(outputMessages).toStrictEqual(expectedMessages);
139152
});
140153
testProp(
@@ -171,6 +184,9 @@ describe(`${RPCClient.name}`, () => {
171184
jsonrpc: '2.0',
172185
id: null,
173186
params,
187+
metadata: {
188+
timeout: null,
189+
},
174190
}),
175191
);
176192
},
@@ -207,14 +223,25 @@ describe(`${RPCClient.name}`, () => {
207223
}
208224
await writer.close();
209225
expect(await output).toStrictEqual(message.result);
210-
const expectedOutput = params.map((v) =>
211-
JSON.stringify({
226+
const expectedOutput = params.map((v, i) => {
227+
const baseObject = {
212228
method: methodName,
213229
jsonrpc: '2.0',
214230
id: null,
215231
params: v,
216-
}),
217-
);
232+
};
233+
234+
// Add metadata only for the first element (i === 0)
235+
if (i === 0) {
236+
return JSON.stringify({
237+
...baseObject,
238+
metadata: { timeout: null },
239+
});
240+
} else {
241+
return JSON.stringify(baseObject);
242+
}
243+
});
244+
218245
expect((await outputResult).map((v) => v.toString())).toStrictEqual(
219246
expectedOutput,
220247
);
@@ -249,6 +276,7 @@ describe(`${RPCClient.name}`, () => {
249276
jsonrpc: '2.0',
250277
id: null,
251278
params: params,
279+
metadata: { timeout: null },
252280
}),
253281
);
254282
},
@@ -423,19 +451,30 @@ describe(`${RPCClient.name}`, () => {
423451
}
424452

425453
const expectedMessages: Array<JSONRPCRequestMessage> = messages.map(
426-
() => {
427-
const request: JSONRPCRequestMessage = {
428-
jsonrpc: '2.0',
429-
method: methodName,
430-
id: null,
431-
params: 'one',
432-
};
433-
return request;
454+
(_, i) => {
455+
if (i === 0) {
456+
return {
457+
jsonrpc: '2.0',
458+
metadata: { timeout: null },
459+
method: methodName,
460+
id: null,
461+
params: 'one',
462+
};
463+
} else {
464+
return {
465+
jsonrpc: '2.0',
466+
method: methodName,
467+
id: null,
468+
params: 'one',
469+
};
470+
}
434471
},
435472
);
473+
436474
const outputMessages = (await outputResult).map((v) =>
437475
JSON.parse(v.toString()),
438476
);
477+
439478
expect(outputMessages).toStrictEqual(expectedMessages);
440479
},
441480
);
@@ -527,6 +566,7 @@ describe(`${RPCClient.name}`, () => {
527566
jsonrpc: '2.0',
528567
id: null,
529568
params,
569+
metadata: { timeout: null },
530570
}),
531571
);
532572
},
@@ -562,14 +602,24 @@ describe(`${RPCClient.name}`, () => {
562602
}
563603
expect(await output).toStrictEqual(message.result);
564604
await writer.close();
565-
const expectedOutput = params.map((v) =>
566-
JSON.stringify({
567-
method: 'client',
568-
jsonrpc: '2.0',
569-
id: null,
570-
params: v,
571-
}),
572-
);
605+
const expectedOutput = params.map((v, i) => {
606+
if (i === 0) {
607+
return JSON.stringify({
608+
method: 'client',
609+
jsonrpc: '2.0',
610+
id: null,
611+
params: v,
612+
metadata: { timeout: null },
613+
});
614+
} else {
615+
return JSON.stringify({
616+
method: 'client',
617+
jsonrpc: '2.0',
618+
id: null,
619+
params: v,
620+
});
621+
}
622+
});
573623
expect((await outputResult).map((v) => v.toString())).toStrictEqual(
574624
expectedOutput,
575625
);
@@ -603,6 +653,7 @@ describe(`${RPCClient.name}`, () => {
603653
jsonrpc: '2.0',
604654
id: null,
605655
params: params,
656+
metadata: { timeout: null },
606657
}),
607658
);
608659
},

0 commit comments

Comments
 (0)