fastify-static
Advanced tools
Comparing version 2.0.0 to 2.1.0
39
index.js
@@ -6,2 +6,3 @@ 'use strict' | ||
const { PassThrough } = require('readable-stream') | ||
const glob = require('glob') | ||
@@ -98,8 +99,2 @@ const send = require('send') | ||
if (opts.serve !== false) { | ||
fastify.get(prefix + '*', schema, function (req, reply) { | ||
pumpSendToReply(req, reply, '/' + req.params['*']) | ||
}) | ||
} | ||
if (opts.decorateReply !== false) { | ||
@@ -111,2 +106,34 @@ fastify.decorateReply('sendFile', function (filePath) { | ||
if (opts.serve !== false) { | ||
if (opts.wildcard === undefined || opts.wildcard === true) { | ||
fastify.get(prefix + '*', schema, function (req, reply) { | ||
pumpSendToReply(req, reply, '/' + req.params['*']) | ||
}) | ||
} else { | ||
glob(path.join(sendOptions.root, '**/*'), function (err, files) { | ||
if (err) { | ||
return next(err) | ||
} | ||
for (let file of files) { | ||
file = file.replace(sendOptions.root.replace(/\\/g, '/'), '') | ||
const route = (prefix + file).replace(/^\/\//, '/') | ||
fastify.get(route, schema, function (req, reply) { | ||
pumpSendToReply(req, reply, '/' + file) | ||
}) | ||
if (file.match(/index\.html$/)) { | ||
const route2 = route.replace(/index\.html$/, '') | ||
fastify.get(route2, schema, function (req, reply) { | ||
pumpSendToReply(req, reply, '/' + file) | ||
}) | ||
} | ||
} | ||
next() | ||
}) | ||
// return early to avoid calling next afterwards | ||
return | ||
} | ||
} | ||
next() | ||
@@ -113,0 +140,0 @@ } |
{ | ||
"name": "fastify-static", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Plugin for serving static files as fast as possible.", | ||
@@ -30,2 +30,3 @@ "main": "index.js", | ||
"fastify-plugin": "^1.2.0", | ||
"glob": "^7.1.3", | ||
"readable-stream": "^3.0.2", | ||
@@ -36,3 +37,3 @@ "send": "^0.16.0" | ||
"coveralls": "^3.0.2", | ||
"fastify": "2.0.0-rc.1", | ||
"fastify": "2.0.0-rc.3", | ||
"fastify-compress": "^0.7.0", | ||
@@ -39,0 +40,0 @@ "pre-commit": "^1.2.2", |
@@ -76,2 +76,14 @@ # fastify-static | ||
#### `wildcard` | ||
Default: `true` | ||
If set to `true`, `fastify-static` adds a wildcard route to serve files. | ||
If set to `false`, `fastify-static` globs the filesystem for all defined | ||
files in the served folder, and just creates the routes needed for | ||
those. | ||
The default options of https://www.npmjs.com/package/glob are applied | ||
for getting the file list. | ||
#### Disable serving | ||
@@ -78,0 +90,0 @@ |
@@ -977,1 +977,113 @@ 'use strict' | ||
}) | ||
t.test('register with wildcard false', t => { | ||
t.plan(8) | ||
const pluginOptions = { | ||
root: path.join(__dirname, '/static'), | ||
wildcard: false | ||
} | ||
const fastify = Fastify() | ||
fastify.register(fastifyStatic, pluginOptions) | ||
fastify.get('/*', (request, reply) => { | ||
reply.send({ hello: 'world' }) | ||
}) | ||
t.tearDown(fastify.close.bind(fastify)) | ||
fastify.listen(0, err => { | ||
t.error(err) | ||
fastify.server.unref() | ||
t.test('/index.html', t => { | ||
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT) | ||
simple.concat({ | ||
method: 'GET', | ||
url: 'http://localhost:' + fastify.server.address().port + '/index.html' | ||
}, (err, response, body) => { | ||
t.error(err) | ||
t.strictEqual(response.statusCode, 200) | ||
t.strictEqual(body.toString(), indexContent) | ||
genericResponseChecks(t, response) | ||
}) | ||
}) | ||
t.test('/index.css', t => { | ||
t.plan(2 + GENERIC_RESPONSE_CHECK_COUNT) | ||
simple.concat({ | ||
method: 'GET', | ||
url: 'http://localhost:' + fastify.server.address().port + '/index.css' | ||
}, (err, response, body) => { | ||
t.error(err) | ||
t.strictEqual(response.statusCode, 200) | ||
genericResponseChecks(t, response) | ||
}) | ||
}) | ||
t.test('/', t => { | ||
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT) | ||
simple.concat({ | ||
method: 'GET', | ||
url: 'http://localhost:' + fastify.server.address().port | ||
}, (err, response, body) => { | ||
t.error(err) | ||
t.strictEqual(response.statusCode, 200) | ||
t.strictEqual(body.toString(), indexContent) | ||
genericResponseChecks(t, response) | ||
}) | ||
}) | ||
t.test('/not-defined', t => { | ||
t.plan(3) | ||
simple.concat({ | ||
method: 'GET', | ||
url: 'http://localhost:' + fastify.server.address().port + '/not-defined' | ||
}, (err, response, body) => { | ||
t.error(err) | ||
t.strictEqual(response.statusCode, 200) | ||
t.deepEqual(JSON.parse(body), { hello: 'world' }) | ||
}) | ||
}) | ||
t.test('/deep/path/for/test/purpose/foo.html', t => { | ||
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT) | ||
simple.concat({ | ||
method: 'GET', | ||
url: 'http://localhost:' + fastify.server.address().port + '/deep/path/for/test/purpose/foo.html' | ||
}, (err, response, body) => { | ||
t.error(err) | ||
t.strictEqual(response.statusCode, 200) | ||
t.strictEqual(body.toString(), deepContent) | ||
genericResponseChecks(t, response) | ||
}) | ||
}) | ||
t.test('/deep/path/for/test/', t => { | ||
t.plan(3 + GENERIC_RESPONSE_CHECK_COUNT) | ||
simple.concat({ | ||
method: 'GET', | ||
url: 'http://localhost:' + fastify.server.address().port + '/deep/path/for/test/' | ||
}, (err, response, body) => { | ||
t.error(err) | ||
t.strictEqual(response.statusCode, 200) | ||
t.strictEqual(body.toString(), innerIndex) | ||
genericResponseChecks(t, response) | ||
}) | ||
}) | ||
t.test('/../index.js', t => { | ||
t.plan(3) | ||
simple.concat({ | ||
method: 'GET', | ||
url: 'http://localhost:' + fastify.server.address().port + '/../index.js', | ||
followRedirect: false | ||
}, (err, response, body) => { | ||
t.error(err) | ||
t.strictEqual(response.statusCode, 200) | ||
t.deepEqual(JSON.parse(body), { hello: 'world' }) | ||
}) | ||
}) | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
43440
1103
125
4
+ Addedglob@^7.1.3
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbrace-expansion@1.1.11(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addedfs.realpath@1.0.0(transitive)
+ Addedglob@7.2.3(transitive)
+ Addedinflight@1.0.6(transitive)
+ Addedminimatch@3.1.2(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedpath-is-absolute@1.0.1(transitive)
+ Addedwrappy@1.0.2(transitive)