{
  "version": 3,
  "sources": ["../../src/aws-lambda/function.ts"],
  "sourcesContent": ["import assert from \"node:assert\";\n\nimport {\n  CfnFunction,\n  Code,\n  Function as BaseFunction,\n  FunctionProps as BaseFunctionProps,\n  Runtime,\n  verifyCodeConfig,\n} from \"aws-cdk-lib/aws-lambda\";\nimport { Construct } from \"constructs\";\n\nimport { AsyncDependenciesContext } from \"../core/index.js\";\n\nexport interface FunctionProps extends Omit<BaseFunctionProps, \"code\"> {\n  /**\n   * The source code of your Lambda function. You can point to a file in an\n   * Amazon Simple Storage Service (Amazon S3) bucket or specify your source\n   * code as inline text.\n   */\n  code: Code | Promise<Code>;\n}\n\n/**\n * Deploys a file from inside the construct library as a function.\n *\n * The supplied file is subject to the 4096 bytes limit of being embedded in a\n * CloudFormation template.\n *\n * The construct includes an associated role with the lambda.\n *\n * This construct does not yet reproduce all features from the underlying resource\n * library.\n */\nexport class Function extends BaseFunction {\n  constructor(scope: Construct, id: string, properties: FunctionProps) {\n    super(scope, id, {\n      ...properties,\n      // Use a dummy code object until we can get the code from the properties.\n      code: Code.fromInline(\" \"),\n      handler: \"index.handler\",\n      runtime: Runtime.NODEJS_18_X,\n    });\n\n    const promise = Promise.resolve(properties.code).then((code) => {\n      const codeConfig = code.bind(this);\n      verifyCodeConfig(codeConfig, {\n        ...properties,\n        code,\n      });\n\n      const resource = this.node.children.find(\n        (children): children is CfnFunction => children instanceof CfnFunction,\n      );\n      assert(resource, \"Could not find resource [CfnFunction]\");\n\n      resource.code = {\n        s3Bucket: codeConfig.s3Location?.bucketName,\n        s3Key: codeConfig.s3Location?.objectKey,\n        s3ObjectVersion: codeConfig.s3Location?.objectVersion,\n        zipFile: codeConfig.inlineCode,\n        imageUri: codeConfig.image?.imageUri,\n      };\n      code.bindToResource(resource);\n    });\n\n    const appDependencies = AsyncDependenciesContext.of(scope);\n    appDependencies.addAsyncDependency(promise);\n\n    let codeIsResolved = false;\n    let codeFailure: unknown | undefined;\n    promise\n      .catch((error) => {\n        codeFailure = error;\n      })\n      .finally(() => {\n        codeIsResolved = true;\n      });\n    this.node.addValidation({\n      validate() {\n        if (!codeIsResolved) {\n          return [\n            \"The code property must be resolved before the function can be deployed. Try calling `await cloudy.waitForAsyncDependencies(app)` before synthesizing, or `await cloudy.synth(app)`.\",\n          ];\n        }\n\n        if (codeFailure) {\n          if (codeFailure instanceof Error) {\n            return [codeFailure.message];\n          }\n\n          if (typeof codeFailure === \"string\") {\n            return [codeFailure];\n          }\n\n          return [\"Unknown error while resolving code.\"];\n        }\n\n        return [];\n      },\n    });\n  }\n}\n\n// function isPromise<T, S>(value: PromiseLike<T> | S): value is PromiseLike<T> {\n//   return !!value && (typeof value === 'object' || typeof value === 'function') && typeof (value as any).then === 'function';\n// }\n\n// function toPromise(value: Code | PromiseLike<Code>): Promise<Code> {\n//   if (isPromise(value)) {\n//     return value as Promise<Code>;\n//   }\n\n//   return Promise.resolve(value);\n// }\n"],
  "mappings": "0jBAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,cAAAE,IAAA,eAAAC,EAAAH,GAAA,IAAAI,EAAmB,4BAEnBC,EAOO,kCAGPC,EAAyC,4BAsBlC,MAAMJ,UAAiB,EAAAK,QAAa,CACzC,YAAYC,EAAkBC,EAAYC,EAA2B,CACnE,MAAMF,EAAOC,EAAI,CACf,GAAGC,EAEH,KAAM,OAAK,WAAW,GAAG,EACzB,QAAS,gBACT,QAAS,UAAQ,WACnB,CAAC,EAED,MAAMC,EAAU,QAAQ,QAAQD,EAAW,IAAI,EAAE,KAAME,GAAS,CAC9D,MAAMC,EAAaD,EAAK,KAAK,IAAI,KACjC,oBAAiBC,EAAY,CAC3B,GAAGH,EACH,KAAAE,CACF,CAAC,EAED,MAAME,EAAW,KAAK,KAAK,SAAS,KACjCC,GAAsCA,aAAoB,aAC7D,KACA,EAAAC,SAAOF,EAAU,uCAAuC,EAExDA,EAAS,KAAO,CACd,SAAUD,EAAW,YAAY,WACjC,MAAOA,EAAW,YAAY,UAC9B,gBAAiBA,EAAW,YAAY,cACxC,QAASA,EAAW,WACpB,SAAUA,EAAW,OAAO,QAC9B,EACAD,EAAK,eAAeE,CAAQ,CAC9B,CAAC,EAEuB,2BAAyB,GAAGN,CAAK,EACzC,mBAAmBG,CAAO,EAE1C,IAAIM,EAAiB,GACjBC,EACJP,EACG,MAAOQ,GAAU,CAChBD,EAAcC,CAChB,CAAC,EACA,QAAQ,IAAM,CACbF,EAAiB,EACnB,CAAC,EACH,KAAK,KAAK,cAAc,CACtB,UAAW,CACT,OAAKA,EAMDC,EACEA,aAAuB,MAClB,CAACA,EAAY,OAAO,EAGzB,OAAOA,GAAgB,SAClB,CAACA,CAAW,EAGd,CAAC,qCAAqC,EAGxC,CAAC,EAjBC,CACL,qLACF,CAgBJ,CACF,CAAC,CACH,CACF",
  "names": ["function_exports", "__export", "Function", "__toCommonJS", "import_node_assert", "import_aws_lambda", "import_core", "BaseFunction", "scope", "id", "properties", "promise", "code", "codeConfig", "resource", "children", "assert", "codeIsResolved", "codeFailure", "error"]
}
