Skip to content

Commit 6294812

Browse files
committed
Attempt to use node:stream pipeline instead of pump - failing tests
1 parent 71fbad4 commit 6294812

File tree

1 file changed

+8
-16
lines changed

1 file changed

+8
-16
lines changed

index.js

+8-16
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ const { inherits, format } = require('node:util')
55

66
const fp = require('fastify-plugin')
77
const encodingNegotiator = require('@fastify/accept-negotiator')
8-
const pump = require('pump')
98
const mimedb = require('mime-db')
109
const peek = require('peek-stream')
1110
const { Minipass } = require('minipass')
1211
const pumpify = require('pumpify')
1312
const { Readable } = require('readable-stream')
13+
const { pipeline } = require('node:stream')
1414

1515
const { isStream, isGzip, isDeflate, intoAsyncIterator } = require('./lib/utils')
1616

@@ -267,15 +267,12 @@ function buildRouteCompress (fastify, params, routeOptions, decorateOnly) {
267267
encoding === undefined
268268
? reply.removeHeader('Content-Encoding')
269269
: reply.header('Content-Encoding', 'identity')
270-
pump(stream, payload = unzipStream(params.uncompressStream), onEnd.bind(reply))
270+
pipeline(stream, payload = unzipStream(params.uncompressStream), onEnd.bind(reply))
271271
}
272272
return next(null, payload)
273273
}
274-
if (payload instanceof ReadableStream) {
275-
payload = require('node:stream').Readable.fromWeb(payload)
276-
}
277274

278-
if (typeof payload.pipe !== 'function') {
275+
if (typeof payload.pipe !== 'function' && !(payload instanceof ReadableStream)) {
279276
if (Buffer.byteLength(payload) < params.threshold) {
280277
return next()
281278
}
@@ -289,7 +286,7 @@ function buildRouteCompress (fastify, params, routeOptions, decorateOnly) {
289286
}
290287

291288
stream = zipStream(params.compressStream, encoding)
292-
pump(payload, stream, onEnd.bind(reply))
289+
pipeline(payload, stream, onEnd.bind(reply))
293290
next(null, stream)
294291
}
295292
}
@@ -351,7 +348,7 @@ function buildRouteDecompress (fastify, params, routeOptions) {
351348
raw.on('data', trackEncodedLength.bind(decompresser))
352349
raw.on('end', removeEncodedLengthTracking)
353350

354-
next(null, pump(raw, decompresser))
351+
next(null, pipeline(raw, decompresser))
355352
}
356353
}
357354

@@ -388,22 +385,17 @@ function compress (params) {
388385
encoding === undefined
389386
? this.removeHeader('Content-Encoding')
390387
: this.header('Content-Encoding', 'identity')
391-
pump(stream, payload = unzipStream(params.uncompressStream), onEnd.bind(this))
388+
pipeline(stream, payload = unzipStream(params.uncompressStream), onEnd.bind(this))
392389
}
393390
return this.send(payload)
394391
}
395392

396-
if (payload instanceof ReadableStream) {
397-
payload = require('node:stream').Readable.fromWeb(payload)
398-
}
393+
if (typeof payload.pipe !== 'function' && !(payload instanceof ReadableStream)) {
399394

400-
if (typeof payload.pipe !== 'function') {
401395
if (!Buffer.isBuffer(payload) && typeof payload !== 'string') {
402396
payload = this.serialize(payload)
403397
}
404-
}
405398

406-
if (typeof payload.pipe !== 'function') {
407399
if (Buffer.byteLength(payload) < params.threshold) {
408400
return this.send(payload)
409401
}
@@ -417,7 +409,7 @@ function compress (params) {
417409
}
418410

419411
stream = zipStream(params.compressStream, encoding)
420-
pump(payload, stream, onEnd.bind(this))
412+
pipeline(payload, stream, onEnd.bind(this))
421413
this.send(stream)
422414
}
423415
}

0 commit comments

Comments
 (0)