Skip to content

Commit

Permalink
Support compressed assets
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Apr 10, 2018
1 parent 0d82378 commit a37bb0c
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 99 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict'

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

fastify
// compress everything
.register(require('fastify-compress'), { global: true, threshold: 0 })
.register(require('../'), { root: path.join(__dirname, '/public') })
.listen(3000, err => {
if (err) throw err
})
48 changes: 37 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
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'
})
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@
"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
Loading

0 comments on commit a37bb0c

Please sign in to comment.