-
Notifications
You must be signed in to change notification settings - Fork 59
Adding warning callback handlers and isAsync detection #144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const shouldWarnOnCallbackFunctionUse = (metadata) => { | ||
return ( | ||
process.env.AWS_LAMBDA_NODEJS_DISABLE_CALLBACK_WARNING === undefined && | ||
metadata !== undefined && | ||
metadata.argsNum == 3 && | ||
metadata.isAsync == false && | ||
metadata.streaming == false | ||
); | ||
}; | ||
|
||
module.exports.checkForDeprecatedCallback = function (metadata) { | ||
if (shouldWarnOnCallbackFunctionUse(metadata)) { | ||
console.warn( | ||
`AWS Lambda plans to remove support for callback-based function handlers starting with Node.js 24. You will need to update this function to use an async handler to use Node.js 24 or later. For more information and to provide feedback on this change, see https://github.com/aws/aws-lambda-nodejs-runtime-interface-client/issues/137. To disable this warning, set the AWS_LAMBDA_NODEJS_DISABLE_CALLBACK_WARNING environment variable.`, | ||
); | ||
} | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved. */ | ||
|
||
export const handlerAsync = async () => { | ||
const response = { | ||
statusCode: 200, | ||
body: JSON.stringify('Hello from Lambda!'), | ||
}; | ||
return response; | ||
}; | ||
|
||
export const handlerNotAsync = () => { | ||
const response = { | ||
statusCode: 200, | ||
body: JSON.stringify('Hello from Lambda!'), | ||
}; | ||
return response; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved. */ | ||
|
||
'use strict'; | ||
|
||
exports.handler = (_event, _context, callback) => { | ||
callback(null, { | ||
statusCode: 200, | ||
body: JSON.stringify({ | ||
message: 'hello world', | ||
}), | ||
}); | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved. */ | ||
|
||
'use strict'; | ||
|
||
require('should'); | ||
const path = require('path'); | ||
const UserFunction = require('lambda-runtime/UserFunction.js'); | ||
|
||
const TEST_ROOT = path.join(__dirname, '../'); | ||
const HANDLERS_ROOT = path.join(TEST_ROOT, 'handlers'); | ||
|
||
describe('isAsync tests', () => { | ||
it('is async should be true', async () => { | ||
const handlerFunc = await UserFunction.load( | ||
HANDLERS_ROOT, | ||
'isAsync.handlerAsync', | ||
); | ||
const metadata = UserFunction.getHandlerMetadata(handlerFunc); | ||
metadata.isAsync.should.be.true(); | ||
}); | ||
it('is async should be false', async () => { | ||
const handlerFunc = await UserFunction.load( | ||
HANDLERS_ROOT, | ||
'isAsync.handlerNotAsync', | ||
); | ||
const metadata = UserFunction.getHandlerMetadata(handlerFunc); | ||
metadata.isAsync.should.be.false(); | ||
}); | ||
it('is async should be false since it is a callback', async () => { | ||
const handlerFunc = await UserFunction.load( | ||
HANDLERS_ROOT, | ||
'isAsyncCallback.handler', | ||
); | ||
const metadata = UserFunction.getHandlerMetadata(handlerFunc); | ||
metadata.isAsync.should.be.false(); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
'use strict'; | ||
|
||
require('should'); | ||
|
||
let { captureStream, consoleSnapshot } = require('./LoggingGlobals'); | ||
|
||
let { | ||
checkForDeprecatedCallback, | ||
} = require('../../src/WarningForCallbackHandlers.js'); | ||
|
||
let LogPatch = require('lambda-runtime/LogPatch'); | ||
const UserFunction = require('lambda-runtime/UserFunction.js'); | ||
|
||
const path = require('path'); | ||
const TEST_ROOT = path.join(__dirname, '../'); | ||
const HANDLERS_ROOT = path.join(TEST_ROOT, 'handlers'); | ||
|
||
describe('Formatted Error Logging', () => { | ||
let restoreConsole = consoleSnapshot(); | ||
let capturedStdout = captureStream(process.stdout); | ||
|
||
beforeEach( | ||
'delete env var', | ||
() => delete process.env.AWS_LAMBDA_NODEJS_DISABLE_CALLBACK_WARNING, | ||
); | ||
beforeEach('capture stdout', () => capturedStdout.hook()); | ||
beforeEach('apply console patch', () => LogPatch.patchConsole()); | ||
afterEach('remove console patch', () => restoreConsole()); | ||
afterEach('unhook stdout', () => capturedStdout.unhook()); | ||
|
||
const expectedString = | ||
'AWS Lambda plans to remove support for callback-based function handlers'; | ||
|
||
const tests = [ | ||
{ args: [false, 'isAsyncCallback.handler'], expected: true }, | ||
{ args: [true, 'isAsyncCallback.handler'], expected: false }, | ||
{ args: [false, 'isAsync.handlerAsync'], expected: false }, | ||
{ args: [true, 'isAsync.handlerAsync'], expected: false }, | ||
{ args: [false, 'defaultHandler.default'], expected: false }, | ||
{ args: [true, 'defaultHandler.default'], expected: false }, | ||
]; | ||
|
||
tests.forEach(({ args, expected }) => { | ||
const shouldDeclareEnv = args[0]; | ||
const handler = args[1]; | ||
it(`When AWS_LAMBDA_NODEJS_DISABLE_CALLBACK_WARNING=${shouldDeclareEnv} expecting ${ | ||
expected ? 'no ' : '' | ||
}warning logs for handler ${handler}`, async () => { | ||
if (shouldDeclareEnv) { | ||
process.env.AWS_LAMBDA_NODEJS_DISABLE_CALLBACK_WARNING = 1; | ||
} | ||
const handlerFunc = await UserFunction.load(HANDLERS_ROOT, handler); | ||
const metadata = UserFunction.getHandlerMetadata(handlerFunc); | ||
|
||
checkForDeprecatedCallback(metadata); | ||
if (expected) { | ||
capturedStdout.captured().should.containEql(expectedString); | ||
} else { | ||
capturedStdout.captured().should.not.containEql(expectedString); | ||
} | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Do we use this or:
src/WarningForCallbackHandlers.js
has the other licence line.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch! let's use the later as it's year-agnostic
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
feel free to merge after this change @fabisev