Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions lib/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,11 @@ function buildMiddlewareInstructions(rootDir, config) {
var middlewareConfig = cloneDeep(config);
middlewareConfig.phase = phase;

if (middlewareConfig.params) {
middlewareConfig.params = resolveMiddlewareParams(
rootDir, middlewareConfig.params);
}

var item = {
sourceFile: resolved.sourceFile,
config: middlewareConfig
Expand Down Expand Up @@ -448,3 +453,16 @@ function resolveMiddlewarePath(rootDir, middleware) {
}
throw err;
}

// Match values starting with `$!./` or `$!../`
var MIDDLEWARE_PATH_PARAM_REGEX = /^\$!(\.\/|\.\.\/)/;

function resolveMiddlewareParams(rootDir, params) {
return cloneDeep(params, function resolvePathParam(value) {
if (typeof value === 'string' && MIDDLEWARE_PATH_PARAM_REGEX.test(value)) {
return path.resolve(rootDir, value.slice(2));
} else {
return undefined; // no change
}
});
}
86 changes: 85 additions & 1 deletion test/compiler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ describe('compiler', function() {

var instructions = boot.compile(appdir.PATH);

expect(instructions.middleware.middleware[0].config.params).to.eql({
expectFirstMiddlewareParams(instructions).to.eql({
key: 'custom value'
});
});
Expand Down Expand Up @@ -954,9 +954,93 @@ describe('compiler', function() {
'sourceFile',
appdir.resolve(HANDLER_FILE));
});

describe('config with relative paths in params', function() {
var RELATIVE_PATH_PARAMS = [
'$!./here',
'$!../there'
];

var absolutePathParams;
beforeEach(function resolveRelativePathParams() {
absolutePathParams = RELATIVE_PATH_PARAMS.map(function(p) {
return appdir.resolve(p.slice(2));
});
});

it('converts paths in top-level array items', function() {
givenMiddlewareEntrySync({ params: RELATIVE_PATH_PARAMS });

var instructions = boot.compile(appdir.PATH);

expectFirstMiddlewareParams(instructions)
.to.eql(absolutePathParams);
});

it('converts paths in top-level object properties', function() {
givenMiddlewareEntrySync({ params: {
path: RELATIVE_PATH_PARAMS[0],
}});

var instructions = boot.compile(appdir.PATH);

expectFirstMiddlewareParams(instructions)
.to.eql({ path: absolutePathParams[0] });
});

it('converts path value when params is a string', function() {
givenMiddlewareEntrySync({ params: RELATIVE_PATH_PARAMS[0] });

var instructions = boot.compile(appdir.PATH);

expectFirstMiddlewareParams(instructions)
.to.eql(absolutePathParams[0]);
});

it('converts paths in nested properties', function() {
givenMiddlewareEntrySync({ params: {
nestedObject: {
path: RELATIVE_PATH_PARAMS[0]
},
nestedArray: RELATIVE_PATH_PARAMS
}});

var instructions = boot.compile(appdir.PATH);

expectFirstMiddlewareParams(instructions)
.to.eql({
nestedObject: {
path: absolutePathParams[0]
},
nestedArray: absolutePathParams
});
});

it('does not convert values not starting with `./` or `../`', function() {
var PARAMS = ['$!.somerc', '$!/root', '$!hello!'];
givenMiddlewareEntrySync({ params: PARAMS});

var instructions = boot.compile(appdir.PATH);

expectFirstMiddlewareParams(instructions).to.eql(PARAMS);
});
});
});
});

function getNameProperty(obj) {
return obj.name;
}

function givenMiddlewareEntrySync(config) {
appdir.writeConfigFileSync('middleware.json', {
initial: {
// resolves to ./middleware.json
'./middleware': config
}
});
}

function expectFirstMiddlewareParams(instructions) {
return expect(instructions.middleware.middleware[0].config.params);
}