Skip to content

Commit aeffe6f

Browse files
authored
Merge pull request #46 from fastify/support-compress
Support compressed assets
2 parents 0d82378 + 8dc096f commit aeffe6f

File tree

5 files changed

+189
-100
lines changed

5 files changed

+189
-100
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ node_js:
44
- "9"
55
- "8"
66
- "6"
7-
- "4"
87

98
script:
109
- npm run coveralls

example/server-compress.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict'
2+
3+
const path = require('path')
4+
const fastify = require('fastify')({ logger: { level: 'trace' } })
5+
6+
fastify
7+
// compress everything
8+
.register(require('fastify-compress'), { threshold: 0 })
9+
.register(require('../'), { root: path.join(__dirname, '/public') })
10+
.listen(3000, err => {
11+
if (err) throw err
12+
})

index.js

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const path = require('path')
44
const statSync = require('fs').statSync
5+
const { PassThrough } = require('readable-stream')
56

67
const send = require('send')
78

@@ -31,17 +32,46 @@ function fastifyStatic (fastify, opts, next) {
3132
}
3233

3334
function pumpSendToReply (request, reply, pathname) {
34-
const stream = send(request.req, pathname, sendOptions)
35-
36-
// this is needed because fastify automatically
37-
// set the type to application/octet-stream
38-
stream.on('headers', removeType)
35+
const stream = send(request.raw, pathname, sendOptions)
36+
37+
const wrap = new PassThrough({
38+
flush (cb) {
39+
this.finished = true
40+
cb()
41+
}
42+
})
43+
44+
wrap.getHeader = reply.getHeader.bind(reply)
45+
wrap.setHeader = reply.header.bind(reply)
46+
wrap.socket = request.raw.socket
47+
wrap.finished = false
48+
49+
Object.defineProperty(wrap, 'statusCode', {
50+
get () {
51+
return reply.res.statusCode
52+
},
53+
set (code) {
54+
reply.code(code)
55+
}
56+
})
57+
58+
wrap.on('pipe', function () {
59+
reply.send(wrap)
60+
})
3961

4062
if (setHeaders !== undefined) {
4163
stream.on('headers', setHeaders)
4264
}
4365

44-
reply.send(stream)
66+
stream.on('error', function (err) {
67+
if (err) {
68+
reply.send(err)
69+
}
70+
})
71+
72+
// we cannot use pump, because send error
73+
// handling is not compatible
74+
stream.pipe(wrap)
4575
}
4676

4777
if (opts.prefix === undefined) opts.prefix = '/'
@@ -82,11 +112,7 @@ function checkRootPathForErrors (rootPath) {
82112
}
83113
}
84114

85-
function removeType (res) {
86-
res.setHeader('Content-Type', '')
87-
}
88-
89115
module.exports = fp(fastifyStatic, {
90-
fastify: '>= 0.42.0',
116+
fastify: '>= 1.2.0',
91117
name: 'fastify-static'
92118
})

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,16 @@
2727
"homepage": "https://github.com/fastify/fastify-static",
2828
"dependencies": {
2929
"fastify-plugin": "^0.2.1",
30+
"readable-stream": "^2.3.6",
3031
"send": "^0.16.0"
3132
},
3233
"devDependencies": {
3334
"coveralls": "^3.0.0",
3435
"fastify": "^1.1.1",
36+
"fastify-compress": "^0.5.1",
3537
"pre-commit": "^1.2.2",
3638
"proxyquire": "^2.0.0",
37-
"request": "2.83.0",
39+
"simple-get": "^2.7.0",
3840
"snazzy": "^7.0.0",
3941
"standard": "^11.0.0",
4042
"tap": "^11.0.1"

0 commit comments

Comments
 (0)