Skip to content

Commit

Permalink
[Runtime] fix(tests): fixed lambda handler tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alekitto committed Nov 13, 2023
1 parent c0b3930 commit 178ba28
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 46 deletions.
5 changes: 2 additions & 3 deletions fixtures/lambda-handler.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const { basename } = require('path');
require('./autoload');
const { basename, dirname } = require('path');

process.env.LAMBDA_TASK_ROOT = __dirname;
process.env.LAMBDA_TASK_ROOT = dirname(__dirname);
process.env._HANDLER = basename(__filename) + '.handler';
const { lambda } = require('../lambda');

Expand Down
6 changes: 0 additions & 6 deletions fixtures/package.json

This file was deleted.

41 changes: 4 additions & 37 deletions lambda.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
const { basename, dirname, join } = require('path');
const { compile } = require('@jymfony/compiler');
const { runInThisContext } = require('vm');

const awsLambdaTaskRoot = process.env.LAMBDA_TASK_ROOT;
const awsHandler = process.env._HANDLER;
const FUNCTION_EXPR = /^([^.]*)\.(.*)$/;

try {
require('@jymfony/autoloader');
Expand All @@ -21,42 +18,12 @@ try {
const nullish = (value, cond) => ((undefined === value) || (null === value)) ? cond() : value;
const AwsLambdaRunnerInterface = Jymfony.Component.Runtime.Runner.AwsLambdaRunnerInterface;

/**
* Break the full handler string into two pieces, the module root and the actual
* handler string.
* Given './somepath/something/module.nestedobj.handler' this returns
* ['./somepath/something', 'module.nestedobj.handler']
*/
const _moduleRootAndHandler = function(fullHandlerString) {
const handlerString = basename(fullHandlerString);
const moduleRoot = fullHandlerString.substring(
0,
fullHandlerString.indexOf(handlerString),
);
return [ moduleRoot, handlerString ];
};

/**
* Split the handler string into two pieces: the module name and the path to
* the handler function.
*/
const _splitHandlerString = function(handler) {
const match = handler.match(FUNCTION_EXPR);
if (!match || 3 != match.length) {
throw new Error('Bad handler');
}

return [ match[1], match[2] ]; // [module, function-path]
};

const [ moduleRoot, moduleAndHandler ] = _moduleRootAndHandler(awsHandler);
const [ moduleName ] = _splitHandlerString(moduleAndHandler);

const file = require.resolve(join(awsLambdaTaskRoot, moduleRoot, moduleName));

let runtime = nullish(process.env.APP_RUNTIME, () => Jymfony.Component.Runtime.JymfonyRuntime);
const reflection = new ReflectionClass(runtime);
runtime = reflection.newInstance(Object.assign({ project_dir: dirname(file) }, nullish(globalThis.APP_RUNTIME_OPTIONS, () => ({}))));

runtime = reflection.newInstance(Object.assign({
project_dir: __jymfony.autoload.finder.findRoot(),
}, nullish(globalThis.APP_RUNTIME_OPTIONS, () => ({}))));

exports.lambda = function (init) {
init = runInThisContext(compile('(' + init.toString() + ')', null, { asFunction: false, debug: false }));
Expand Down

0 comments on commit 178ba28

Please sign in to comment.