Skip to content

Commit

Permalink
Fix serverPath conflict with index files, fix paths output into build (
Browse files Browse the repository at this point in the history
  • Loading branch information
devinivy authored Jul 2, 2020
1 parent 33f4fa9 commit 91f8248
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 24 deletions.
50 changes: 28 additions & 22 deletions lib/serverless.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const Path = require('path');
const Util = require('util');
const Fs = require('fs');
const Bounce = require('@hapi/bounce');
const Hoek = require('@hapi/hoek');
const Somever = require('@hapi/somever');
const Rimraf = require('rimraf');
Expand Down Expand Up @@ -50,11 +49,7 @@ exports.Plugin = class {

async initialize() {

const { servicePath } = this.sls.config;

const rootOrPath = Path.resolve(servicePath, Hoek.reach(this.sls, 'service.custom.lalalambda.serverPath') || '');

const server = this.server = await internals.getServer(rootOrPath);
const server = this.server = await internals.getServer(this.serverPath);

Hoek.assert(server.plugins.lalalambda, 'Lalalambda needs to be registered as a plugin on your hapi server.');
}
Expand Down Expand Up @@ -103,18 +98,35 @@ exports.Plugin = class {
Hoek.assert(server, 'Lalalambda must be initialized.');

const { servicePath } = this.sls.config;
const { lambdas } = server.plugins.lalalambda;
const buildFolderPath = Path.join(servicePath, BUILD_FOLDER);

await this.cleanup();
await internals.mkdir(Path.join(servicePath, BUILD_FOLDER));
await internals.mkdir(buildFolderPath);

const { lambdas } = server.plugins.lalalambda;

for (const id of lambdas.keys()) {
await internals.writeFile(
Path.join(servicePath, BUILD_FOLDER, `${id}.js`),
internals.entrypoint(id, Hoek.reach(this.sls, 'service.custom.lalalambda.serverPath'))
Path.join(buildFolderPath, `${id}.js`),
// Path listed in entrypoint should be relative
// in order for the code to remain portable.
internals.entrypoint(id, Path.relative(buildFolderPath, this.serverPath))
);
}
}

get serverPath() {

const { servicePath } = this.sls.config;

const serverPath = Hoek.reach(
this.sls,
['service', 'custom', 'lalalambda', 'serverPath'],
{ default: 'server' }
);

return Path.resolve(servicePath, serverPath);
}
};

exports.handler = (id, path) => {
Expand Down Expand Up @@ -154,16 +166,7 @@ internals.writeFile = Util.promisify(Fs.writeFile);

internals.rimraf = Util.promisify(Rimraf);

internals.getServer = async (rootOrPath) => {

let path = Path.join(rootOrPath, 'server');

try {
path = require.resolve(rootOrPath);
}
catch (err) {
Bounce.rethrow(err, 'system');
}
internals.getServer = async (path) => {

try {

Expand All @@ -186,10 +189,13 @@ internals.getServer = async (rootOrPath) => {
};

// eslint-disable-next-line @hapi/hapi/scope-start
internals.entrypoint = (id, serverPath = '') => `'use strict';
internals.entrypoint = (id, path) => `'use strict';
const Path = require('path');
const Lalalambda = require('lalalambda');
exports.handler = Lalalambda.handler('${id}', Path.resolve(__dirname, '..', '${serverPath}'));
exports.handler = Lalalambda.handler('${internals.esc(id)}', Path.resolve(__dirname, '${internals.esc(path)}'));
`;

// Allow slashes and single-quotes in filenames
internals.esc = (str) => str.replace(/(\\|')/g, '\\$1');
18 changes: 18 additions & 0 deletions test/closet/invoke-custom-server-path-escaped/'.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

const { Hapi } = require('../../helpers');
const Lalalambda = require('../../..');

exports.deployment = async () => {

const server = Hapi.server();

await server.register(Lalalambda);

server.lambda({
id: 'invoke-lambda',
handler: () => ({ success: 'invoked' })
});

return server;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = require('../../../..');
12 changes: 12 additions & 0 deletions test/closet/invoke-custom-server-path-escaped/serverless.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
service: my-service

provider:
name: aws
runtime: nodejs12.x

custom:
lalalambda:
serverPath: "'.js"

plugins:
- lalalambda
2 changes: 2 additions & 0 deletions test/closet/invoke/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// This exists to ensure that when serverPath isn't specified, this file
// doesn't get picked-up as the default server export rather than server.js
15 changes: 13 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1313,7 +1313,7 @@ describe('Lalalambda', () => {
expect(output).to.contain(`"success": "invoked"`);
});

it('can locally invoke a lambda registered by hapi to a custom serverpath.', async () => {
it('can locally invoke a lambda registered by hapi to a custom serverPath.', async () => {

const serverless = Helpers.makeServerless('invoke-custom-server-path', ['invoke', 'local', '--function', 'invoke-lambda']);

Expand All @@ -1324,6 +1324,17 @@ describe('Lalalambda', () => {
expect(output).to.contain(`"success": "invoked"`);
});

it('can locally invoke a lambda registered by hapi to a custom serverPath containing a single quote.', async () => {

const serverless = Helpers.makeServerless('invoke-custom-server-path-escaped', ['invoke', 'local', '--function', 'invoke-lambda']);

await serverless.init();

const output = await serverless.run();

expect(output).to.contain(`"success": "invoked"`);
});

it('invokes lambdas registered by hapi with server and bound context.', async () => {

const serverless = Helpers.makeServerless('invoke-context', ['invoke', 'local', '--function', 'invoke-context-lambda', '--data', '{"an":"occurrence"}']);
Expand Down Expand Up @@ -1434,7 +1445,7 @@ describe('Lalalambda', () => {
const Path = require('path');
const Lalalambda = require('lalalambda');
exports.handler = Lalalambda.handler('package-lambda', Path.resolve(__dirname, '..', ''));
exports.handler = Lalalambda.handler('package-lambda', Path.resolve(__dirname, '../server'));
`));

const cfFile = await readFile(Path.join(__dirname, 'closet', 'package', '.serverless', 'cloudformation-template-update-stack.json'));
Expand Down

0 comments on commit 91f8248

Please sign in to comment.