Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support compressed assets #46

Merged
merged 3 commits into from Apr 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Expand Up @@ -4,7 +4,6 @@ node_js:
- "9"
- "8"
- "6"
- "4"

script:
- npm run coveralls
Expand Down
12 changes: 12 additions & 0 deletions example/server-compress.js
@@ -0,0 +1,12 @@
'use strict'

const path = require('path')
const fastify = require('fastify')({ logger: { level: 'trace' } })

fastify
// compress everything
.register(require('fastify-compress'), { threshold: 0 })
.register(require('../'), { root: path.join(__dirname, '/public') })
.listen(3000, err => {
if (err) throw err
})
48 changes: 37 additions & 11 deletions index.js
Expand Up @@ -2,6 +2,7 @@

const path = require('path')
const statSync = require('fs').statSync
const { PassThrough } = require('readable-stream')

const send = require('send')

Expand Down Expand Up @@ -31,17 +32,46 @@ function fastifyStatic (fastify, opts, next) {
}

function pumpSendToReply (request, reply, pathname) {
const stream = send(request.req, pathname, sendOptions)

// this is needed because fastify automatically
// set the type to application/octet-stream
stream.on('headers', removeType)
const stream = send(request.raw, pathname, sendOptions)

const wrap = new PassThrough({
flush (cb) {
this.finished = true
cb()
}
})

wrap.getHeader = reply.getHeader.bind(reply)
wrap.setHeader = reply.header.bind(reply)
wrap.socket = request.raw.socket
wrap.finished = false

Object.defineProperty(wrap, 'statusCode', {
get () {
return reply.res.statusCode
},
set (code) {
reply.code(code)
}
})

wrap.on('pipe', function () {
reply.send(wrap)
})

if (setHeaders !== undefined) {
stream.on('headers', setHeaders)
}

reply.send(stream)
stream.on('error', function (err) {
if (err) {
reply.send(err)
}
})

// we cannot use pump, because send error
// handling is not compatible
stream.pipe(wrap)
}

if (opts.prefix === undefined) opts.prefix = '/'
Expand Down Expand Up @@ -82,11 +112,7 @@ function checkRootPathForErrors (rootPath) {
}
}

function removeType (res) {
res.setHeader('Content-Type', '')
}

module.exports = fp(fastifyStatic, {
fastify: '>= 0.42.0',
fastify: '>= 1.2.0',
name: 'fastify-static'
})
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -27,14 +27,16 @@
"homepage": "https://github.com/fastify/fastify-static",
"dependencies": {
"fastify-plugin": "^0.2.1",
"readable-stream": "^2.3.6",
"send": "^0.16.0"
},
"devDependencies": {
"coveralls": "^3.0.0",
"fastify": "^1.1.1",
"fastify-compress": "^0.5.1",
"pre-commit": "^1.2.2",
"proxyquire": "^2.0.0",
"request": "2.83.0",
"simple-get": "^2.7.0",
"snazzy": "^7.0.0",
"standard": "^11.0.0",
"tap": "^11.0.1"
Expand Down