-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use conditional dynamic import for aws-sdk (#558)
- Loading branch information
1 parent
cd7d7cf
commit fee8405
Showing
9 changed files
with
967 additions
and
28 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
"author": "OpenZeppelin Defender <[email protected]>", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"@aws-sdk/client-lambda": "^3.563.0", | ||
"@types/async-retry": "^1.4.4", | ||
"aws-sdk": "^2.1366.0" | ||
}, | ||
|
This file contains 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 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,78 @@ | ||
import { version } from 'node:process'; | ||
|
||
const NODE_MIN_VERSION_FOR_V3 = 18; | ||
|
||
export type InvokeResponse = { | ||
FunctionError?: string; | ||
Payload: PayloadResponseV2 | PayloadResponseV3; | ||
}; | ||
|
||
export type InvokeResponseV2 = { | ||
promise: () => Promise<InvokeResponse>; | ||
}; | ||
|
||
export type PayloadResponseV3 = { | ||
transformToString: () => string; | ||
}; | ||
|
||
export type PayloadResponseV2 = string | Buffer | Uint8Array | Blob; | ||
|
||
export type LambdaV2 = { | ||
invoke: (params: { FunctionName: string; Payload: string; InvocationType: string }) => InvokeResponseV2; | ||
}; | ||
|
||
export type LambdaV3 = { | ||
invoke: (params: { FunctionName: string; Payload: string; InvocationType: string }) => Promise<InvokeResponse>; | ||
}; | ||
|
||
export type LambdaLike = LambdaV2 | LambdaV3; | ||
|
||
export type LambdaCredentials = { | ||
AccessKeyId: string; | ||
SecretAccessKey: string; | ||
SessionToken: string; | ||
}; | ||
|
||
function isLambdaV3Compatible(): boolean { | ||
// example version: v14.17.0 | ||
const majorVersion = version.slice(1).split('.')[0]; | ||
if (!majorVersion) return false; | ||
return parseInt(majorVersion, 10) >= NODE_MIN_VERSION_FOR_V3; | ||
} | ||
|
||
export function isLambdaV3(lambda: LambdaLike): lambda is LambdaV3 { | ||
return isLambdaV3Compatible(); | ||
} | ||
|
||
export function isV3ResponsePayload(payload: PayloadResponseV2 | PayloadResponseV3): payload is PayloadResponseV3 { | ||
return (payload as PayloadResponseV3).transformToString !== undefined; | ||
} | ||
|
||
export function getLambdaFromCredentials(credentials: string): LambdaLike { | ||
const creds: LambdaCredentials = credentials ? JSON.parse(credentials) : undefined; | ||
if (isLambdaV3Compatible()) { | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const { Lambda } = require('@aws-sdk/client-lambda'); | ||
return new Lambda({ | ||
credentials: { | ||
accessKeyId: creds.AccessKeyId, | ||
secretAccessKey: creds.SecretAccessKey, | ||
sessionToken: creds.SessionToken, | ||
}, | ||
}); | ||
} else { | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const Lambda = require('aws-sdk/clients/lambda'); | ||
return new Lambda( | ||
creds | ||
? { | ||
credentials: { | ||
accessKeyId: creds.AccessKeyId, | ||
secretAccessKey: creds.SecretAccessKey, | ||
sessionToken: creds.SessionToken, | ||
}, | ||
} | ||
: undefined, | ||
); | ||
} | ||
} |
This file contains 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,11 @@ | ||
const Lambda = jest.fn(() => ({ | ||
invoke: jest.fn(() => | ||
Promise.resolve({ | ||
Payload: { | ||
transformToString: () => JSON.stringify({ result: 'result' }), | ||
}, | ||
}), | ||
), | ||
})); | ||
|
||
export { Lambda }; |
This file contains 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 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,11 @@ | ||
const Lambda = jest.fn(() => ({ | ||
invoke: jest.fn(() => | ||
Promise.resolve({ | ||
Payload: { | ||
transformToString: () => JSON.stringify({ result: 'result' }), | ||
}, | ||
}), | ||
), | ||
})); | ||
|
||
export { Lambda }; |
This file contains 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 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
Oops, something went wrong.