fastify-static
Advanced tools
Comparing version 2.4.0 to 2.5.0
@@ -22,3 +22,3 @@ // Definitions by: Jannik <https://github.com/jannikkeye> | ||
redirect?: boolean; | ||
wildcard?: boolean; | ||
wildcard?: boolean | string; | ||
@@ -25,0 +25,0 @@ // Passed on to `send` |
@@ -124,3 +124,4 @@ 'use strict' | ||
} else { | ||
glob(path.join(sendOptions.root, '**/*'), { nodir: true }, function (err, files) { | ||
const globPattern = typeof opts.wildcard === 'string' ? opts.wildcard : '**/*' | ||
glob(path.join(sendOptions.root, globPattern), { nodir: true }, function (err, files) { | ||
if (err) { | ||
@@ -127,0 +128,0 @@ return next(err) |
{ | ||
"name": "fastify-static", | ||
"version": "2.4.0", | ||
"version": "2.5.0", | ||
"description": "Plugin for serving static files as fast as possible.", | ||
@@ -33,13 +33,13 @@ "main": "index.js", | ||
"dependencies": { | ||
"fastify-plugin": "^1.5.0", | ||
"glob": "^7.1.3", | ||
"readable-stream": "^3.1.1", | ||
"fastify-plugin": "^1.6.0", | ||
"glob": "^7.1.4", | ||
"readable-stream": "^3.4.0", | ||
"send": "^0.16.0" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^10.12.27", | ||
"@types/node": "^10.14.9", | ||
"concat-stream": "^2.0.0", | ||
"coveralls": "^3.0.3", | ||
"coveralls": "^3.0.4", | ||
"eslint-plugin-typescript": "^0.14.0", | ||
"fastify": "^2.0.0", | ||
"fastify": "^2.5.0", | ||
"fastify-compress": "^0.8.1", | ||
@@ -51,6 +51,6 @@ "pre-commit": "^1.2.2", | ||
"standard": "^12.0.0", | ||
"tap": "^12.5.3", | ||
"typescript": "^3.2.1", | ||
"tap": "^12.7.0", | ||
"typescript": "^3.5.2", | ||
"typescript-eslint-parser": "^22.0.0" | ||
} | ||
} |
@@ -91,4 +91,5 @@ # fastify-static | ||
If set to `false`, `fastify-static` globs the filesystem for all defined | ||
files in the served folder, and just creates the routes needed for | ||
files in the served folder (`${root}/**/*`), and just creates the routes needed for | ||
those. | ||
If set to a glob `string` pattern, `fastify-static` will use the provided string when globing the filesystem (`${root}/${wildcard}`). | ||
@@ -95,0 +96,0 @@ The default options of https://www.npmjs.com/package/glob are applied |
@@ -685,2 +685,33 @@ 'use strict' | ||
t.test('maxAge option', t => { | ||
t.plan(5 + GENERIC_RESPONSE_CHECK_COUNT) | ||
const pluginOptions = { | ||
root: path.join(__dirname, 'static'), | ||
maxAge: 3600000 | ||
} | ||
const fastify = Fastify() | ||
fastify.register(fastifyStatic, pluginOptions) | ||
t.tearDown(fastify.close.bind(fastify)) | ||
fastify.listen(0, err => { | ||
t.error(err) | ||
fastify.server.unref() | ||
simple.concat({ | ||
method: 'GET', | ||
url: 'http://localhost:' + fastify.server.address().port + '/index.html', | ||
followRedirect: false | ||
}, (err, response, body) => { | ||
t.error(err) | ||
t.strictEqual(response.statusCode, 200) | ||
t.strictEqual(response.headers['cache-control'], 'public, max-age=3600') | ||
t.strictEqual(body.toString(), indexContent) | ||
genericResponseChecks(t, response) | ||
}) | ||
}) | ||
}) | ||
t.test('errors', t => { | ||
@@ -1130,2 +1161,226 @@ t.plan(5) | ||
t.test('register with wildcard "**/index.html"', t => { | ||
t.plan(8) | ||
const pluginOptions = { | ||
root: path.join(__dirname, '/static'), | ||
wildcard: '**/index.html' | ||
} | ||
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_ERROR_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) | ||
genericErrorResponseChecks(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(2 + GENERIC_ERROR_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) | ||
genericErrorResponseChecks(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' }) | ||
}) | ||
}) | ||
}) | ||
}) | ||
t.test('register with wildcard "**/foo.*"', t => { | ||
t.plan(8) | ||
const pluginOptions = { | ||
root: path.join(__dirname, '/static'), | ||
wildcard: '**/foo.*' | ||
} | ||
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_ERROR_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.deepEqual(JSON.parse(body), { hello: 'world' }) | ||
genericErrorResponseChecks(t, response) | ||
}) | ||
}) | ||
t.test('/index.css', t => { | ||
t.plan(2 + GENERIC_ERROR_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) | ||
genericErrorResponseChecks(t, response) | ||
}) | ||
}) | ||
t.test('/', t => { | ||
t.plan(3 + GENERIC_ERROR_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.deepEqual(JSON.parse(body), { hello: 'world' }) | ||
genericErrorResponseChecks(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_ERROR_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.deepEqual(JSON.parse(body), { hello: 'world' }) | ||
genericErrorResponseChecks(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' }) | ||
}) | ||
}) | ||
}) | ||
}) | ||
t.test('register with wildcard false and alternative index', t => { | ||
@@ -1132,0 +1387,0 @@ t.plan(8) |
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
70636
1862
138
Updatedfastify-plugin@^1.6.0
Updatedglob@^7.1.4
Updatedreadable-stream@^3.4.0