forked from fastify/fastify-static
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
199 lines (169 loc) · 5.53 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
'use strict'
const path = require('path')
const url = require('url')
const statSync = require('fs').statSync
const { PassThrough } = require('readable-stream')
const glob = require('glob')
const send = require('send')
const fp = require('fastify-plugin')
function fastifyStatic (fastify, opts, next) {
const error = checkRootPathForErrors(fastify, opts.root)
if (error !== undefined) return next(error)
const setHeaders = opts.setHeaders
if (setHeaders !== undefined && typeof setHeaders !== 'function') {
return next(new TypeError('The `setHeaders` option must be a function'))
}
const sendOptions = {
root: opts.root,
acceptRanges: opts.acceptRanges,
cacheControl: opts.cacheControl,
dotfiles: opts.dotfiles,
etag: opts.etag,
extensions: opts.extensions,
immutable: opts.immutable,
index: opts.index,
lastModified: opts.lastModified,
maxAge: opts.maxAge
}
function pumpSendToReply (request, reply, pathname) {
const stream = send(request.raw, pathname, sendOptions)
var resolvedFilename
stream.on('file', function (file) {
resolvedFilename = file
})
const wrap = new PassThrough({
flush (cb) {
this.finished = true
if (reply.res.statusCode === 304) {
reply.send('')
}
cb()
}
})
wrap.getHeader = reply.getHeader.bind(reply)
wrap.setHeader = reply.header.bind(reply)
wrap.socket = request.raw.socket
wrap.finished = false
Object.defineProperty(wrap, 'filename', {
get () {
return resolvedFilename
}
})
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)
}
if (opts.redirect === true) {
stream.on('directory', function (res, path) {
const parsed = url.parse(request.raw.url)
reply.redirect(301, parsed.pathname + '/' + (parsed.search || ''))
})
}
stream.on('error', function (err) {
if (err) {
if (err.code === 'ENOENT') {
return reply.callNotFound()
}
reply.send(err)
}
})
// we cannot use pump, because send error
// handling is not compatible
stream.pipe(wrap)
}
if (opts.prefix === undefined) opts.prefix = '/'
const prefix = opts.prefix[opts.prefix.length - 1] === '/' ? opts.prefix : (opts.prefix + '/')
// Set the schema hide property if defined in opts or true by default
const schema = { schema: { hide: typeof opts.schemaHide !== 'undefined' ? opts.schemaHide : true } }
if (opts.decorateReply !== false) {
fastify.decorateReply('sendFile', function (filePath) {
pumpSendToReply(this.request, this, filePath)
})
}
if (opts.serve !== false) {
if (opts.wildcard === undefined || opts.wildcard === true) {
fastify.get(prefix + '*', schema, function (req, reply) {
pumpSendToReply(req, reply, '/' + req.params['*'])
})
if (opts.redirect === true && prefix !== opts.prefix) {
fastify.get(opts.prefix, schema, function (req, reply) {
const parsed = url.parse(req.raw.url)
reply.redirect(301, parsed.pathname + '/' + (parsed.search || ''))
})
}
} else {
const globPattern = typeof opts.wildcard === 'string' ? opts.wildcard : '**/*'
glob(path.join(sendOptions.root, globPattern), { nodir: true }, function (err, files) {
if (err) {
return next(err)
}
const indexDirs = new Set()
const indexes = typeof opts.index === 'undefined' ? ['index.html'] : [].concat(opts.index || [])
for (let file of files) {
file = file.replace(sendOptions.root.replace(/\\/g, '/'), '').replace(/^\//, '')
const route = (prefix + file).replace(/\/\//g, '/')
fastify.get(route, schema, function (req, reply) {
pumpSendToReply(req, reply, '/' + file)
})
if (indexes.includes(path.posix.basename(route))) {
indexDirs.add(path.posix.dirname(route))
}
}
indexDirs.forEach(function (dirname) {
const pathname = dirname + (dirname.endsWith('/') ? '' : '/')
const file = '/' + pathname.replace(prefix, '')
fastify.get(pathname, schema, function (req, reply) {
pumpSendToReply(req, reply, file)
})
if (opts.redirect === true) {
fastify.get(pathname.replace(/\/$/, ''), schema, function (req, reply) {
pumpSendToReply(req, reply, file.replace(/\/$/, ''))
})
}
})
next()
})
// return early to avoid calling next afterwards
return
}
}
next()
}
function checkRootPathForErrors (fastify, rootPath) {
if (rootPath === undefined) {
return new Error('"root" option is required')
}
if (typeof rootPath !== 'string') {
return new Error('"root" option must be a string')
}
if (path.isAbsolute(rootPath) === false) {
return new Error('"root" option must be an absolute path')
}
var pathStat
try {
pathStat = statSync(rootPath)
} catch (e) {
if (e.code === 'ENOENT') {
fastify.log.warn(`"root" path "${rootPath}" must exist`)
return
}
return e
}
if (pathStat.isDirectory() === false) {
return new Error('"root" option must point to a directory')
}
}
module.exports = fp(fastifyStatic, {
fastify: '>=2.0.0',
name: 'fastify-static'
})