Skip to content

Commit f307326

Browse files
authored
Merge pull request #216 from LeoPlatform/feat/ES-2929-async-lambda-wrappers
feat(wrappers): convert Lambda handler exports to async for nodejs20.x+ compatibility
2 parents 220c307 + c9b4c5d commit f307326

5 files changed

Lines changed: 32 additions & 8 deletions

File tree

wrappers/cron.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"use strict";
22

3+
const wrapAsAsync = require("./wrapAsAsync");
34

45
let cachedHandler;
56
module.exports = function(configOverride, botHandler) {
@@ -100,7 +101,7 @@ module.exports = function(configOverride, botHandler) {
100101
}
101102

102103
Object.assign(config, configOverride);
103-
return function(event, context, callback) {
104+
return wrapAsAsync(function(event, context, callback) {
104105
context.callbackWaitsForEmptyEventLoop = false;
105106
context.resources = process.resources;
106107
context.botId = event.botId || botId;
@@ -252,6 +253,6 @@ module.exports = function(configOverride, botHandler) {
252253
}
253254
});
254255
}
255-
};
256+
});
256257
};
257258
module.exports.CronWrapper = module.exports;

wrappers/fanout.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11

2+
const wrapAsAsync = require("./wrapAsAsync");
23
const { Lambda } = require("@aws-sdk/client-lambda");
34
const async = require("async");
45
const eventIdFormat = "[z/]YYYY/MM/DD/HH/mm/";
@@ -110,7 +111,7 @@ const fanoutFactory = (handler, eventPartition, opts = {}) => {
110111

111112

112113
logger.log("Fanout Return", process.env.FANOUT_iid, process.env.FANOUT_icount, process.env.FANOUT_maxeid);
113-
return (event, context, callback) => {
114+
return wrapAsAsync((event, context, callback) => {
114115

115116
cronData = event.__cron || {};
116117

@@ -199,7 +200,7 @@ const fanoutFactory = (handler, eventPartition, opts = {}) => {
199200
return callback(err);
200201
});
201202
}
202-
};
203+
});
203204
};
204205

205206
function callCheckpointOnResponses(leoBotCheckpoint, callback) {

wrappers/resource.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
"use strict";
2+
const wrapAsAsync = require("./wrapAsAsync");
23
let config = require("../leoConfigure.js");
34
module.exports = function (configOverride, botHandler) {
45
if (!botHandler) {
56
botHandler = configOverride;
67
configOverride = {};
78
}
89
Object.assign(config, configOverride);
9-
return function (event, context, callback) {
10+
return wrapAsAsync(function (event, context, callback) {
1011
context.callbackWaitsForEmptyEventLoop = false;
1112
if (context.identity) { // Called Directly not via Api Gateway
1213
event = {
@@ -120,5 +121,5 @@ module.exports = function (configOverride, botHandler) {
120121
}
121122
});
122123
}
123-
};
124+
});
124125
};

wrappers/test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"use strict";
22

3+
const wrapAsAsync = require("./wrapAsAsync");
34

45
let cachedHandler;
56
module.exports = function(configOverride, botHandler) {
@@ -47,7 +48,7 @@ module.exports = function(configOverride, botHandler) {
4748
}
4849

4950
Object.assign(config, configOverride);
50-
return function(event, context, callback) {
51+
return wrapAsAsync(function(event, context, callback) {
5152
let cb = callback;
5253
callback = (err, data) => {
5354
assert.print();
@@ -97,5 +98,5 @@ module.exports = function(configOverride, botHandler) {
9798
}
9899

99100

100-
};
101+
});
101102
};

wrappers/wrapAsAsync.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"use strict";
2+
3+
/**
4+
* Converts a callback-style Lambda handler into an async handler that returns a Promise.
5+
* Newer Node.js Lambda runtimes (nodejs20.x+) no longer support the callback pattern,
6+
* so all exported handlers must be async.
7+
*
8+
* @param {function(event, context, callback)} handler - A callback-style Lambda handler
9+
* @returns {function(event, context): Promise} An async Lambda handler
10+
*/
11+
module.exports = function wrapAsAsync(handler) {
12+
return async function(event, context) {
13+
return new Promise((resolve, reject) => {
14+
handler(event, context, function(err, result) {
15+
if (err) reject(err);
16+
else resolve(result);
17+
});
18+
});
19+
};
20+
};

0 commit comments

Comments
 (0)