Last active
March 5, 2021 14:45
-
-
Save bestickley/430fc66e81f1f2b9306489d75be8297c to your computer and use it in GitHub Desktop.
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
import got, { BeforeRequestHook } from "got"; | |
import { SignatureV4 } from "@aws-sdk/signature-v4"; | |
import { HttpRequest } from "@aws-sdk/protocol-http"; | |
import { Sha256 } from "@aws-crypto/sha256-js"; | |
import { defaultProvider } from "@aws-sdk/credential-provider-node"; | |
import HttpAgent from "agentkeepalive"; | |
export function getSigV4BeforeHook(service: string): BeforeRequestHook { | |
const beforeHook: BeforeRequestHook = async (options) => { | |
console.log(JSON.stringify(options)); | |
const { body, hostname, method, protocol, pathname, port } = options; | |
const httpRequest = new HttpRequest({ | |
body, | |
headers: options.headers as Record<string, string>, | |
hostname, | |
method, | |
path: pathname, | |
port: port ? +port : 443, | |
protocol, | |
}); | |
const signer = new SignatureV4({ | |
credentials: defaultProvider(), | |
region: process.env.AWS_DEFAULT_REGION as string, | |
service, | |
sha256: Sha256, | |
}); | |
const signedReq = await signer.sign(httpRequest); | |
console.log(JSON.stringify(signedReq)); | |
options.headers = signedReq.headers; | |
}; | |
return beforeHook; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment