Skip to content

Instantly share code, notes, and snippets.

@bestickley
Last active March 5, 2021 14:45
Show Gist options
  • Save bestickley/430fc66e81f1f2b9306489d75be8297c to your computer and use it in GitHub Desktop.
Save bestickley/430fc66e81f1f2b9306489d75be8297c to your computer and use it in GitHub Desktop.
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