Skip to content

Commit

Permalink
Don't resolve against files.relativeTo multiple times. Closes #125
Browse files Browse the repository at this point in the history
  • Loading branch information
kanongil committed Apr 2, 2019
1 parent fc143b3 commit 4514a5e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
31 changes: 10 additions & 21 deletions lib/directory.js
Expand Up @@ -27,33 +27,21 @@ internals.schema = Joi.object({
});


internals.normalizePaths = function (basePath, paths) {

return paths.map((path) => {

return Path.isAbsolute(path) ? path : Path.join(basePath, path);
});
};


internals.resolvePathOption = function (basePath, result) {
internals.resolvePathOption = function (result) {

if (result instanceof Error) {
throw result;
}

let paths;
if (typeof result === 'string') {
paths = [result];
}
else if (Array.isArray(result)) {
paths = result;
return [result];
}
else {
throw Boom.internal('Invalid path function');

if (Array.isArray(result)) {
return result;
}

return internals.normalizePaths(basePath, paths);
throw Boom.internal('Invalid path function');
};


Expand All @@ -64,14 +52,15 @@ exports.handler = function (route, options) {

const paramName = /\w+/.exec(route.path.slice(route.path.lastIndexOf('{')))[0];
const basePath = route.settings.files.relativeTo;
const normalized = (Array.isArray(settings.path) ? internals.normalizePaths(basePath, settings.path) : null); // Array or function

const normalized = (Array.isArray(settings.path) ? settings.path : null); // Array or function
const indexNames = (settings.index === true) ? ['index.html'] : (settings.index || []);

// Declare handler

const handler = async (request, reply) => {

const paths = normalized || internals.resolvePathOption(basePath, settings.path.call(null, request));
const paths = normalized || internals.resolvePathOption(settings.path.call(null, request));

// Append parameter

Expand Down Expand Up @@ -152,7 +141,7 @@ exports.handler = function (route, options) {
// None of the index files were found

if (settings.listing) {
return internals.generateListing(Path.join(baseDir, path), resource, selection, hasTrailingSlash, settings, request);
return internals.generateListing(Path.join(basePath, baseDir, path), resource, selection, hasTrailingSlash, settings, request);
}
}

Expand Down
9 changes: 9 additions & 0 deletions test/directory.js
Expand Up @@ -625,6 +625,15 @@ describe('directory', () => {
expect(res.statusCode).to.equal(200);
});

it('resolves relative pathnames from relative relativeTo', async () => {

const server = await provisionServer({ routes: { files: { relativeTo: './test' } }, router: { stripTrailingSlash: false } });
server.route({ method: 'GET', path: '/test/{path*}', handler: { directory: { path: Path.join('.', 'directory') } } });

const res = await server.inject('/test/index.html');
expect(res.statusCode).to.equal(200);
});

it('returns error when path function returns error', async () => {

const path = () => {
Expand Down

0 comments on commit 4514a5e

Please sign in to comment.