Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
fix(gateway): disable compression
Browse files Browse the repository at this point in the history
This is an alternative to #2227
that does not hit datastore twice.

The underlying error was caused by multiple PassThrough/pipe calls.
Turns out go-ipfs does not compress anything by default, so we can
remove PassThrough responsible for streaming of compressed payload
and fix the issue while keeping optimization introduced by buffer-peek-stream.

Closes libp2p/js-libp2p#374
Closes libp2p/pull-mplex#13

License: MIT
Signed-off-by: Marcin Rataj <[email protected]>
  • Loading branch information
lidel committed Jul 12, 2019
1 parent 04aa63c commit 8869b2d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 20 deletions.
20 changes: 1 addition & 19 deletions src/http/gateway/resources/gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ log.error = debug('ipfs:http-gateway:error')

const fileType = require('file-type')
const mime = require('mime-types')
const { PassThrough } = require('readable-stream')
const Boom = require('@hapi/boom')
const Ammo = require('@hapi/ammo') // HTTP Range processing utilities
const peek = require('buffer-peek-stream')
Expand All @@ -32,20 +31,6 @@ function detectContentType (path, chunk) {
return mime.contentType(mimeType)
}

// Enable streaming of compressed payload
// https://github.com/hapijs/hapi/issues/3599
class ResponseStream extends PassThrough {
_read (size) {
super._read(size)
if (this._compressor) {
this._compressor.flush()
}
}
setCompressor (compressor) {
this._compressor = compressor
}
}

module.exports = {

async handler (request, h) {
Expand Down Expand Up @@ -149,7 +134,6 @@ module.exports = {
}

const rawStream = ipfs.catReadableStream(data.cid, catOptions)
const responseStream = new ResponseStream()

// Pass-through Content-Type sniffing over initial bytes
const { peekedStream, contentType } = await new Promise((resolve, reject) => {
Expand All @@ -163,9 +147,7 @@ module.exports = {
})
})

peekedStream.pipe(responseStream)

const res = h.response(responseStream).code(rangeResponse ? 206 : 200)
const res = h.response(peekedStream).code(rangeResponse ? 206 : 200)

// Etag maps directly to an identifier for a specific version of a resource
// and enables smart client-side caching thanks to If-None-Match
Expand Down
6 changes: 5 additions & 1 deletion src/http/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ class HttpApi {
// TODO: shouldn't, fix this
routes: {
cors: true
}
},
// Disable Compression
// Why? Streaming compression in Hapi is not stable enough,
// it requires bug-prone hacks such as https://github.com/hapijs/hapi/issues/3599
compression: false
})
server.app.ipfs = ipfs

Expand Down

0 comments on commit 8869b2d

Please sign in to comment.