From 55da792b5b145cef951d8ff688c96306073a9e14 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Thu, 6 May 2021 12:41:49 +0200 Subject: [PATCH] Avoid a closure --- index.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 6495af3d..8385ab99 100644 --- a/index.js +++ b/index.js @@ -91,13 +91,11 @@ async function fastifyStatic (fastify, opts) { } }) - wrap.on('pipe', function () { - if (request.method !== 'HEAD') { reply.send(wrap) } - }) - - wrap.on('finish', function () { - if (request.method === 'HEAD') { reply.send() } - }) + if (request.method === 'HEAD') { + wrap.on('finish', reply.send.bind(reply)) + } else { + wrap.on('pipe', reply.send.bind(reply, wrap)) + } if (setHeaders !== undefined) { stream.on('headers', setHeaders)