From 601b95667f201e2b6088da44ef2a42682daaaee4 Mon Sep 17 00:00:00 2001 From: Tim Perry Date: Wed, 29 Jan 2025 17:21:20 +0100 Subject: [PATCH] Remove lots of angle type casts, which break Node 23's new TS support --- src/admin/mockttp-admin-model.ts | 2 +- src/client/admin-client.ts | 2 +- src/rules/requests/request-handler-definitions.ts | 5 +++-- src/rules/requests/request-handlers.ts | 8 ++++---- src/util/request-utils.ts | 2 +- test/integration/manual-rule-building.spec.ts | 2 +- .../integration/subscriptions/client-error-events.spec.ts | 8 ++++---- test/integration/subscriptions/tls-error-events.spec.ts | 4 ++-- 8 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/admin/mockttp-admin-model.ts b/src/admin/mockttp-admin-model.ts index ec84ceff0..56eaf7350 100644 --- a/src/admin/mockttp-admin-model.ts +++ b/src/admin/mockttp-admin-model.ts @@ -138,7 +138,7 @@ export function buildAdminServerModel( }) }); - return { + return { Query: { mockedEndpoints: async (): Promise => { return Promise.all((await mockServer.getMockedEndpoints()).map(buildMockedEndpointData)); diff --git a/src/client/admin-client.ts b/src/client/admin-client.ts index 457dbaaf0..a002e988c 100644 --- a/src/client/admin-client.ts +++ b/src/client/admin-client.ts @@ -638,7 +638,7 @@ export class AdminClient this.subscriptionClient!.request(query).subscribe({ next: async (value) => { if (value.data) { - const response = ( value.data)[fieldName]; + const response = value.data[fieldName]; const result = query.transformResponse ? await query.transformResponse(response, { adminClient: this }) : response as Result; diff --git a/src/rules/requests/request-handler-definitions.ts b/src/rules/requests/request-handler-definitions.ts index be7404cfa..4776efce1 100644 --- a/src/rules/requests/request-handler-definitions.ts +++ b/src/rules/requests/request-handler-definitions.ts @@ -413,8 +413,9 @@ export class StreamHandlerDefinition extends Serializable implements RequestHand let serializedEventData: StreamHandlerEventMessage | false = _.isString(chunk) ? { type: 'string', value: chunk } : _.isBuffer(chunk) ? { type: 'buffer', value: chunk.toString('base64') } : - (_.isArrayBuffer(chunk) || _.isTypedArray(chunk)) ? { type: 'arraybuffer', value: encodeBase64( chunk) } : - _.isNil(chunk) && { type: 'nil' }; + (_.isArrayBuffer(chunk) || _.isTypedArray(chunk)) + ? { type: 'arraybuffer', value: encodeBase64(chunk) } + : _.isNil(chunk) && { type: 'nil' }; if (!serializedEventData) { callback(new Error(`Can't serialize streamed value: ${chunk.toString()}. Streaming must output strings, buffers or array buffers`)); diff --git a/src/rules/requests/request-handlers.ts b/src/rules/requests/request-handlers.ts index 6418cd84c..e28f4b9c5 100644 --- a/src/rules/requests/request-handlers.ts +++ b/src/rules/requests/request-handlers.ts @@ -160,7 +160,7 @@ export class SimpleHandler extends SimpleHandlerDefinition { writeHead(response, this.status, this.statusMessage, this.headers); if (isSerializedBuffer(this.data)) { - this.data = Buffer.from( this.data); + this.data = Buffer.from(this.data as any); } if (this.trailers) { @@ -408,7 +408,7 @@ export class PassThroughHandler extends PassThroughHandlerDefinition { let { protocol, hostname, port, path } = url.parse(reqUrl); // Check if this request is a request loop: - if (isSocketLoop(this.outgoingSockets, ( clientReq).socket)) { + if (isSocketLoop(this.outgoingSockets, (clientReq as any).socket)) { throw new Error(oneLine` Passthrough loop detected. This probably means you're sending a request directly to a passthrough endpoint, which is forwarding it to the target URL, which is a @@ -588,7 +588,7 @@ export class PassThroughHandler extends PassThroughHandlerDefinition { if (modifiedReq?.response) { if (modifiedReq.response === 'close') { - const socket: net.Socket = ( clientReq).socket; + const socket: net.Socket = (clientReq as any).socket; socket.end(); throw new AbortError('Connection closed intentionally by rule'); } else if (modifiedReq.response === 'reset') { @@ -1301,7 +1301,7 @@ export class PassThroughHandler extends PassThroughHandlerDefinition { export class CloseConnectionHandler extends CloseConnectionHandlerDefinition { async handle(request: OngoingRequest) { - const socket: net.Socket = ( request).socket; + const socket: net.Socket = (request as any).socket; socket.end(); throw new AbortError('Connection closed intentionally by rule'); } diff --git a/src/util/request-utils.ts b/src/util/request-utils.ts index 4ed8c0399..a66e11516 100644 --- a/src/util/request-utils.ts +++ b/src/util/request-utils.ts @@ -272,7 +272,7 @@ export const parseRequestBody = ( req: http.IncomingMessage | http2.Http2ServerRequest, options: { maxSize: number } ) => { - let transformedRequest = req; + let transformedRequest = req as any as OngoingRequest; transformedRequest.body = parseBodyStream(req, options.maxSize, () => req.headers); }; diff --git a/test/integration/manual-rule-building.spec.ts b/test/integration/manual-rule-building.spec.ts index 86e0b626d..332b393ba 100644 --- a/test/integration/manual-rule-building.spec.ts +++ b/test/integration/manual-rule-building.spec.ts @@ -106,7 +106,7 @@ describe("Mockttp rule building", function () { return expect((async () => { // Funky setup to handle sync & async failure for node & browser await server.addRequestRules({ matchers: [new matchers.SimplePathMatcher('/')], - handler: null + handler: null as any }) })()).to.be.rejectedWith('Cannot create a rule with no handler'); }); diff --git a/test/integration/subscriptions/client-error-events.spec.ts b/test/integration/subscriptions/client-error-events.spec.ts index 8a87018a5..1b1daabad 100644 --- a/test/integration/subscriptions/client-error-events.spec.ts +++ b/test/integration/subscriptions/client-error-events.spec.ts @@ -323,7 +323,7 @@ describe("Client error subscription", () => { await server.on('client-error', (e) => errorPromise.resolve(e)); await server.forGet("http://example.com/endpoint").thenReply(200, "Mock data"); - const response = await fetch("http://example.com/endpoint", { + const response = await fetch("http://example.com/endpoint", { agent: new HttpsProxyAgent({ protocol: 'http', host: 'localhost', @@ -332,7 +332,7 @@ describe("Client error subscription", () => { headers: { "long-value": TOO_LONG_HEADER_VALUE } - }); + } as any); expect(response.status).to.equal(431); @@ -357,7 +357,7 @@ describe("Client error subscription", () => { await server.on('client-error', (e) => errorPromise.resolve(e)); await server.forGet("https://example.com/endpoint").thenReply(200, "Mock data"); - const response = await fetch("https://example.com/endpoint", { + const response = await fetch("https://example.com/endpoint", { agent: new HttpsProxyAgent({ protocol: 'https', host: 'localhost', @@ -369,7 +369,7 @@ describe("Client error subscription", () => { 'host': 'example.com', "long-value": TOO_LONG_HEADER_VALUE } - }); + } as any); expect(response.status).to.equal(431); diff --git a/test/integration/subscriptions/tls-error-events.spec.ts b/test/integration/subscriptions/tls-error-events.spec.ts index da52f567d..5406afe4a 100644 --- a/test/integration/subscriptions/tls-error-events.spec.ts +++ b/test/integration/subscriptions/tls-error-events.spec.ts @@ -119,14 +119,14 @@ describe("TLS error subscriptions", () => { await badServer.forAnyRequest().thenPassThrough(); await expect( - fetch(goodServer.urlFor("/"), { + fetch(goodServer.urlFor("/"), { // Ignores proxy cert issues by using the proxy via plain HTTP agent: new HttpsProxyAgent({ protocol: 'http', host: 'localhost', port: badServer.port }) - }) + } as any) ).to.be.rejectedWith(/certificate/); const tlsError = await seenTlsErrorPromise;