Skip to content

Commit c674969

Browse files
committed
add sync handler
1 parent 2c7a4d0 commit c674969

File tree

11 files changed

+71
-34
lines changed

11 files changed

+71
-34
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55

66
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
77

8+
9+
## [0.1.12] 2023-06-28
10+
11+
- publish via CI
12+
- Fix: auto-loader paths
13+
814
## [0.1.11] 2023-07-22
915

1016
- publish via CI

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ app.setDefaultFunctionProps({
162162
1. Add the baselime-node layer - `arn:aws:lambda:${region:097948374213:layer:baselime-node:3`
163163
2. Add the baselime-extension layer - `arn:aws:lambda:${region}:097948374213:layer:baselime-extension-${'x86_64' || 'arm64'}:1`
164164
3. Set the handler to `/opt/nodejs/node_modules/@baselime/lambda-node-opentelemetry/handler.handler`
165-
4. Set the BASELIME_ORIGINAL_HANDLER environment variable to the original path of your lambda
165+
4. Set the BASELIME_ACTUAL_HANDLER environment variable to the original path of your lambda
166166
5. Set the BASELIME_KEY environment variable with the value of your environments baselime api key
167167

168168
### SST

handler.cjs

Lines changed: 0 additions & 6 deletions
This file was deleted.

handler.mjs

Lines changed: 0 additions & 20 deletions
This file was deleted.

handlers/handler.cjs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
const { loadSync } = require('./loader');
2+
const { wrap } = require('./index');
23

3-
exports.handler = function (...args) {
4+
exports.handler = async function (...args) {
5+
const actualHandler = process.env.BASELIME_ACTUAL_HANDLER;
6+
const taskRoot = process.env.LAMBDA_TASK_ROOT;
47

5-
loadSync();
8+
if(!taskRoot) {
9+
throw Error('LAMBDA_TASK_ROOT is not defined');
10+
}
11+
12+
if(!actualHandler) {
13+
throw Error('BASELIME_ACTUAL_HANDLER is not defined');
14+
}
15+
16+
const handler = await loadSync(taskRoot, actualHandler);
17+
18+
const [event, context] = args
19+
return wrap(handler)(event, context);
620
};

loader.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@baselime/lambda-node-opentelemetry",
3-
"version": "0.1.11",
3+
"version": "0.1.12",
44
"description": "OpenTelemetry auto tracer for Node.JS based AWS Lambda functions",
55
"keywords": [
66
"nodejs",

src/loader.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@ async function _tryImport(path:string) {
66
} catch(err) {
77
return false;
88
}
9+
}
910

11+
function _tryRequire(path: string) {
12+
try {
13+
return require(path);
14+
} catch(err) {
15+
return false;
16+
}
1017
}
1118
export async function load(taskRoot: string, originalHandler: string) {
1219
if (originalHandler.includes('..')) {
@@ -26,6 +33,20 @@ export async function load(taskRoot: string, originalHandler: string) {
2633
return lambda[functionName];
2734
}
2835

29-
export function loadSync() {
30-
throw Error('only esm works just now');
36+
export function loadSync(taskRoot: string, originalHandler: string) {
37+
if (originalHandler.includes('..')) {
38+
throw Error(`${originalHandler} is not a valid handler, it must not contain '..'`);
39+
}
40+
const pathDetails = path.parse(originalHandler);
41+
42+
const functionName = pathDetails.ext.slice(1);
43+
44+
const functionPath = path.resolve(taskRoot, pathDetails.dir, pathDetails.name);
45+
46+
const lambda =_tryRequire(functionPath + '.js') || _tryRequire(functionPath + '.cjs')
47+
48+
if (!lambda) {
49+
throw Error(`Could not load ${functionPath}.js or ${functionPath}.cjs`);
50+
}
51+
return lambda[functionName];
3152
}

tests/data/original-handler.cjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
3+
exports.handler = function handler(e) {
4+
return {
5+
statusCode: 200,
6+
body: JSON.stringify({
7+
message: 'Hello World!',
8+
})
9+
}
10+
}

tests/load.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ describe('load()', () => {
88
const originalHandler = 'original-handler.handler';
99

1010
const handler = await load(taskRoot, originalHandler);
11-
console.log(handler)
1211
expect(handler).toBeInstanceOf(Function);
1312

1413
})

0 commit comments

Comments
 (0)