From 76a14c183cd225cc3b73524795d4c0d566fb3a31 Mon Sep 17 00:00:00 2001 From: Yiming Luo Date: Tue, 22 Oct 2024 14:51:59 -0400 Subject: [PATCH] chore: Add explicit return type for most functions --- serverless/src/env.ts | 6 +++--- serverless/src/forwarder.ts | 32 +++++++++++++++++++++++--------- serverless/src/git.ts | 2 +- serverless/src/index.ts | 4 ++-- serverless/src/layer.ts | 29 +++++++++++++++++++++-------- serverless/src/redirect.ts | 6 +++--- serverless/src/tags.ts | 8 ++++---- serverless/src/tracing.ts | 6 +++--- 8 files changed, 60 insertions(+), 33 deletions(-) diff --git a/serverless/src/env.ts b/serverless/src/env.ts index c0c140a..659f453 100644 --- a/serverless/src/env.ts +++ b/serverless/src/env.ts @@ -234,7 +234,7 @@ export function getConfigFromCfnParams(params: CfnParams) { }; } -export function validateParameters(config: Configuration) { +export function validateParameters(config: Configuration): string[] { log.debug("Validating parameters..."); const errors: string[] = []; @@ -274,7 +274,7 @@ export function validateParameters(config: Configuration) { return errors; } -export function checkForMultipleApiKeys(config: Configuration) { +export function checkForMultipleApiKeys(config: Configuration): string | undefined { let multipleApiKeysMessage; if (config.apiKey !== undefined && config.apiKMSKey !== undefined && config.apiKeySecretArn !== undefined) { multipleApiKeysMessage = "`apiKey`, `apiKMSKey`, and `apiKeySecretArn`"; @@ -289,7 +289,7 @@ export function checkForMultipleApiKeys(config: Configuration) { return multipleApiKeysMessage; } -export function setEnvConfiguration(config: Configuration, lambdas: LambdaFunction[]) { +export function setEnvConfiguration(config: Configuration, lambdas: LambdaFunction[]): void { lambdas.forEach((lambda) => { const environment = lambda.properties.Environment ?? {}; const envVariables = environment.Variables ?? {}; diff --git a/serverless/src/forwarder.ts b/serverless/src/forwarder.ts index 8f1f48a..f72af0f 100644 --- a/serverless/src/forwarder.ts +++ b/serverless/src/forwarder.ts @@ -77,7 +77,7 @@ export async function addCloudWatchForwarderSubscriptions( stackName: string | undefined, forwarderArn: string, cloudWatchLogs: CloudWatchLogs, -) { +): Promise { let logGroupsOnStack: CloudWatchLogs.LogGroups | undefined; let logGroupsInTemplate: LogGroupDefinition[] | undefined; let subscriptionsInTemplate: SubscriptionDefinition[] | undefined; @@ -203,7 +203,10 @@ export async function addCloudWatchForwarderSubscriptions( } } -export async function findExistingLogGroupWithFunctionName(cloudWatchLogs: CloudWatchLogs, functionName: string) { +export async function findExistingLogGroupWithFunctionName( + cloudWatchLogs: CloudWatchLogs, + functionName: string, +): Promise { const logGroupName = `${LAMBDA_LOG_GROUP_PREFIX}${functionName}`; const args = { logGroupNamePrefix: logGroupName, @@ -216,7 +219,10 @@ export async function findExistingLogGroupWithFunctionName(cloudWatchLogs: Cloud return logGroups.find((lg) => lg.logGroupName === logGroupName); } -export async function getExistingLambdaLogGroupsOnStack(cloudWatchLogs: CloudWatchLogs, stackName: string) { +export async function getExistingLambdaLogGroupsOnStack( + cloudWatchLogs: CloudWatchLogs, + stackName: string, +): Promise { const logGroupNamePrefix = `${LAMBDA_LOG_GROUP_PREFIX}${stackName}-`; const args = { logGroupNamePrefix }; const response = await cloudWatchLogs.describeLogGroups(args).promise(); @@ -225,7 +231,7 @@ export async function getExistingLambdaLogGroupsOnStack(cloudWatchLogs: CloudWat return logGroups ?? []; } -export async function shouldSubscribeLogGroup(cloudWatchLogs: CloudWatchLogs, logGroupName: string) { +export async function shouldSubscribeLogGroup(cloudWatchLogs: CloudWatchLogs, logGroupName: string): Promise { const subscriptionFilters = await describeSubscriptionFilters(cloudWatchLogs, logGroupName); const numberOfActiveSubscriptionFilters = subscriptionFilters.length; if (numberOfActiveSubscriptionFilters >= MAX_ALLOWABLE_LOG_GROUP_SUBSCRIPTIONS) { @@ -243,7 +249,7 @@ export async function shouldSubscribeLogGroup(cloudWatchLogs: CloudWatchLogs, lo return true; } -async function describeSubscriptionFilters(cloudWatchLogs: CloudWatchLogs, logGroupName: string) { +async function describeSubscriptionFilters(cloudWatchLogs: CloudWatchLogs, logGroupName: string): Promise { const request = { logGroupName }; const response = await cloudWatchLogs.describeSubscriptionFilters(request).promise(); return response.subscriptionFilters ?? []; @@ -257,7 +263,11 @@ function findLogGroupsInTemplate(resources: Resources) { }); } -export function findDeclaredLogGroup(logGroups: LogGroupDefinition[], functionKey: string, functionName?: string) { +export function findDeclaredLogGroup( + logGroups: LogGroupDefinition[], + functionKey: string, + functionName?: string, +): LogGroupDefinition | undefined { for (const resource of Object.values(logGroups)) { const logGroupName = resource.logGroupResource.Properties.LogGroupName; @@ -298,7 +308,7 @@ function findDeclaredSub( subscriptions: SubscriptionDefinition[], logGroupName: string | { [fn: string]: any }, logGroupKey: string, -) { +): SubscriptionDefinition | undefined { for (const subscription of subscriptions) { const subscribedLogGroupName = subscription.subscriptionResource.Properties.LogGroupName; if (typeof subscribedLogGroupName === "string" && subscribedLogGroupName.includes(logGroupKey)) { @@ -310,7 +320,11 @@ function findDeclaredSub( } } -async function putSubscriptionFilter(cloudWatchLogs: CloudWatchLogs, forwarderArn: string, logGroupName: string) { +async function putSubscriptionFilter( + cloudWatchLogs: CloudWatchLogs, + forwarderArn: string, + logGroupName: string, +): Promise { const args = { destinationArn: forwarderArn, filterName: SUBSCRIPTION_FILTER_NAME, @@ -320,7 +334,7 @@ async function putSubscriptionFilter(cloudWatchLogs: CloudWatchLogs, forwarderAr await cloudWatchLogs.putSubscriptionFilter(args).promise(); } -async function createLogGroup(cloudWatchLogs: CloudWatchLogs, logGroupName: string) { +async function createLogGroup(cloudWatchLogs: CloudWatchLogs, logGroupName: string): Promise { const args = { logGroupName }; await cloudWatchLogs.createLogGroup(args).promise(); } diff --git a/serverless/src/git.ts b/serverless/src/git.ts index 9bae9ce..b5195a1 100644 --- a/serverless/src/git.ts +++ b/serverless/src/git.ts @@ -1,4 +1,4 @@ -export const filterSensitiveInfoFromRepository = (repositoryUrl: string | undefined) => { +export const filterSensitiveInfoFromRepository = (repositoryUrl: string | undefined): string | undefined => { try { if (!repositoryUrl) { return repositoryUrl; diff --git a/serverless/src/index.ts b/serverless/src/index.ts index 1a984a3..1108841 100644 --- a/serverless/src/index.ts +++ b/serverless/src/index.ts @@ -210,7 +210,7 @@ export const handler = async (event: InputEvent, _: any) => { * the 'FunctionName' property. In this case, it means at least one of the names * will be dynamically generated. */ -function lambdaHasDynamicallyGeneratedName(lambdas: LambdaFunction[]) { +function lambdaHasDynamicallyGeneratedName(lambdas: LambdaFunction[]): LambdaFunction[] { const dynmicallyNamedLambdas: LambdaFunction[] = []; for (const lambda of lambdas) { if (lambda.properties.FunctionName === undefined) { @@ -220,7 +220,7 @@ function lambdaHasDynamicallyGeneratedName(lambdas: LambdaFunction[]) { return dynmicallyNamedLambdas; } -export function getMissingStackNameErrorMsg(lambdaKeys: string[]) { +export function getMissingStackNameErrorMsg(lambdaKeys: string[]): string { return ( "A forwarder ARN was provided with one or more dynamically named lambda function resources," + " but the stack name was not provided. Without the stack name, the dynamically generated" + diff --git a/serverless/src/layer.ts b/serverless/src/layer.ts index 7fc008a..d55ccef 100644 --- a/serverless/src/layer.ts +++ b/serverless/src/layer.ts @@ -114,7 +114,7 @@ export const layerNameLookup: { [key in ArchitectureType]: { [key: string]: stri * Also assigns a general runtime type to the output lambdas. This helps to determine which lambda * layers to add & which handler to redirect to later on in the macro. */ -export function findLambdas(resources: Resources, templateParameterValues: Parameters) { +export function findLambdas(resources: Resources, templateParameterValues: Parameters): LambdaFunction[] { return Object.entries(resources) .map(([key, resource]) => { if (resource.Type !== LAMBDA_FUNCTION_RESOURCE_TYPE) { @@ -173,7 +173,7 @@ export function applyLayers( nodeLayerVersion?: number, dotnetLayerVersion?: number, javaLayerVersion?: number, -) { +): string[] { const errors: string[] = []; lambdas.forEach((lambda) => { if (lambda.runtimeType === RuntimeType.UNSUPPORTED) { @@ -230,7 +230,11 @@ export function applyLayers( return errors; } -export function applyExtensionLayer(region: string, lambdas: LambdaFunction[], extensionLayerVersion?: number) { +export function applyExtensionLayer( + region: string, + lambdas: LambdaFunction[], + extensionLayerVersion?: number, +): string[] { const errors: string[] = []; lambdas.forEach((lambda) => { let lambdaExtensionLayerArn; @@ -247,7 +251,7 @@ export function applyExtensionLayer(region: string, lambdas: LambdaFunction[], e return errors; } -function addLayer(layerArn: string, lambda: LambdaFunction) { +function addLayer(layerArn: string, lambda: LambdaFunction): void { if (layerArn === undefined) { return; } @@ -289,7 +293,12 @@ export function getNewLayers(layerArn: string, currentLayers: LambdaLayersProper return { [CFN_IF_FUNCTION_STRING]: [conditionalName, newLayersIfTrue, newLayersIfFalse] }; } -export function getLambdaLibraryLayerArn(region: string, version: number, runtime: string, architecture: string) { +export function getLambdaLibraryLayerArn( + region: string, + version: number, + runtime: string, + architecture: string, +): string { const layerName = layerNameLookup[architectureLookup[architecture]][runtime]; const isGovCloud = region === "us-gov-east-1" || region === "us-gov-west-1"; @@ -301,7 +310,7 @@ export function getLambdaLibraryLayerArn(region: string, version: number, runtim return `arn:aws:lambda:${region}:${DD_ACCOUNT_ID}:layer:${layerName}:${version}`; } -export function getExtensionLayerArn(region: string, version: number, architecture: string) { +export function getExtensionLayerArn(region: string, version: number, architecture: string): string { const layerName = architectureToExtensionLayerName[architecture]; const isGovCloud = region === "us-gov-east-1" || region === "us-gov-west-1"; @@ -314,14 +323,18 @@ export function getExtensionLayerArn(region: string, version: number, architectu return `arn:aws:lambda:${region}:${DD_ACCOUNT_ID}:layer:${layerName}:${version}`; } -export function getMissingLayerVersionErrorMsg(functionKey: string, formalRuntime: string, paramRuntime: string) { +export function getMissingLayerVersionErrorMsg( + functionKey: string, + formalRuntime: string, + paramRuntime: string, +): string { return ( `Resource ${functionKey} has a ${formalRuntime} runtime, but no ${formalRuntime} Lambda Library version was provided. ` + `Please add the '${paramRuntime}LayerVersion' parameter for the Datadog serverless macro.` ); } -export function getMissingExtensionLayerVersionErrorMsg(functionKey: string) { +export function getMissingExtensionLayerVersionErrorMsg(functionKey: string): string { return ( `Resource ${functionKey} has been configured to apply the extension laybe but no extension version was provided. ` + `Please add the 'extensionLayerVersion' parameter for the Datadog serverless macro` diff --git a/serverless/src/redirect.ts b/serverless/src/redirect.ts index e7d0d21..3950f02 100644 --- a/serverless/src/redirect.ts +++ b/serverless/src/redirect.ts @@ -13,7 +13,7 @@ export const JS_HANDLER = "node_modules/datadog-lambda-js/dist/handler.handler"; * 'DD_LAMBDA_HANDLER' is set to the original handler in the lambda's environment for the * replacement handler to find. */ -export function redirectHandlers(lambdas: LambdaFunction[], addLayers: boolean) { +export function redirectHandlers(lambdas: LambdaFunction[], addLayers: boolean): void { lambdas.forEach((lambda) => { if (lambda.runtimeType == RuntimeType.DOTNET || lambda.runtimeType == RuntimeType.JAVA) { setEnvDatadogWrapper(lambda); @@ -41,7 +41,7 @@ function getDDHandler(lambdaRuntime: RuntimeType | undefined, addLayers: boolean } } -function setEnvDatadogHandler(lambda: LambdaFunction) { +function setEnvDatadogHandler(lambda: LambdaFunction): void { const environment = lambda.properties.Environment ?? {}; const environmentVariables = environment.Variables ?? {}; @@ -52,7 +52,7 @@ function setEnvDatadogHandler(lambda: LambdaFunction) { lambda.properties.Environment = environment; } -function setEnvDatadogWrapper(lambda: LambdaFunction) { +function setEnvDatadogWrapper(lambda: LambdaFunction): void { const environment = lambda.properties.Environment ?? {}; const environmentVariables = environment.Variables ?? {}; diff --git a/serverless/src/tags.ts b/serverless/src/tags.ts index 4602cce..fe30319 100644 --- a/serverless/src/tags.ts +++ b/serverless/src/tags.ts @@ -8,7 +8,7 @@ const MACRO_VERSION = "dd_sls_macro"; // Following the same pattern from SAM const MACRO_BY = "dd_sls_macro_by"; -export function addDDTags(lambdas: LambdaFunction[], config: Configuration) { +export function addDDTags(lambdas: LambdaFunction[], config: Configuration): void { // Add the tags for each function, unless a tag already exists. lambdas.forEach((lambda) => { const tags = lambda.properties.Tags ?? []; @@ -41,7 +41,7 @@ export function addDDTags(lambdas: LambdaFunction[], config: Configuration) { }); } -export function addMacroTag(lambdas: LambdaFunction[], version: string | undefined) { +export function addMacroTag(lambdas: LambdaFunction[], version: string | undefined): void { if (!version) return; lambdas.forEach((lambda) => { @@ -52,7 +52,7 @@ export function addMacroTag(lambdas: LambdaFunction[], version: string | undefin }); } -export function addCDKTag(lambdas: LambdaFunction[]) { +export function addCDKTag(lambdas: LambdaFunction[]): void { lambdas.forEach((lambda) => { const tags = lambda.properties.Tags ?? []; tags.push({ Key: MACRO_BY, Value: "CDK" }); @@ -61,7 +61,7 @@ export function addCDKTag(lambdas: LambdaFunction[]) { }); } -export function addSAMTag(lambdas: LambdaFunction[]) { +export function addSAMTag(lambdas: LambdaFunction[]): void { lambdas.forEach((lambda) => { const tags = lambda.properties.Tags ?? []; diff --git a/serverless/src/tracing.ts b/serverless/src/tracing.ts index 26a9576..778cb20 100644 --- a/serverless/src/tracing.ts +++ b/serverless/src/tracing.ts @@ -48,7 +48,7 @@ export enum TracingMode { NONE, } -function findIamRole(resources: Resources, lambda: LambdaFunction) { +function findIamRole(resources: Resources, lambda: LambdaFunction): IamRoleProperties | undefined { const role = lambda.properties.Role; let roleKey; if (typeof role !== "string") { @@ -66,7 +66,7 @@ function findIamRole(resources: Resources, lambda: LambdaFunction) { } } -export function getTracingMode(config: Configuration) { +export function getTracingMode(config: Configuration): TracingMode { if (config.enableXrayTracing && config.enableDDTracing) { return TracingMode.HYBRID; } else if (config.enableDDTracing) { @@ -77,7 +77,7 @@ export function getTracingMode(config: Configuration) { return TracingMode.NONE; } -export function enableTracing(tracingMode: TracingMode, lambdas: LambdaFunction[], resources: Resources) { +export function enableTracing(tracingMode: TracingMode, lambdas: LambdaFunction[], resources: Resources): void { if (tracingMode === TracingMode.XRAY || tracingMode === TracingMode.HYBRID) { log.debug("Enabling Xray tracing..."); const xrayPolicies = {