aws-sigv4-sign
Version:
SignatureV4 sign function implemented with the official AWS SDK
142 lines (136 loc) • 4.85 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
getDefaultCredentialProvider: () => getDefaultCredentialProvider,
parseRequest: () => parseRequest,
signRequest: () => signRequest
});
module.exports = __toCommonJS(index_exports);
// src/sign-request.ts
var import_sha256_js = require("@aws-crypto/sha256-js");
var import_protocol_http = require("@smithy/protocol-http");
var import_signature_v4 = require("@smithy/signature-v4");
// src/credential-provider.ts
function isBrowser() {
return typeof window !== "undefined" && typeof document !== "undefined";
}
async function getDefaultCredentialProvider() {
if (isBrowser())
throw new Error(
"AWS credentials provider is not available in browser environments. You must provide credentials explicitly when calling signRequest in a browser."
);
try {
const { defaultProvider } = await import("@aws-sdk/credential-provider-node");
return defaultProvider();
} catch (error) {
throw new Error("AWS credentials provider could not be loaded. You must provide credentials explicitly.");
}
}
// src/parse-request.ts
var copyHeaders = (headers) => {
const headersMap = /* @__PURE__ */ new Map();
headers.forEach((value, key) => headersMap.set(key.toLowerCase(), value));
return Object.fromEntries(headersMap.entries());
};
var parseRequest = async (input, init) => {
let request;
if (typeof input === "string" || input instanceof URL) {
const url2 = input instanceof URL ? input.href : input;
request = new Request(url2, init);
} else {
request = new Request(input.clone(), init);
}
const method = request.method.toUpperCase();
const url = new URL(request.url);
const headers = copyHeaders(request.headers);
const buffer = await request.arrayBuffer();
const body = buffer.byteLength > 0 ? buffer : void 0;
return {
method,
headers,
url,
body
};
};
// src/sign-request.ts
async function signRequest(...args) {
let input;
let init;
let options;
if (args.length === 2) {
input = args[0];
options = args[1];
} else {
input = args[0];
init = args[1];
options = args[2];
}
const { url, method, headers, body } = await parseRequest(input, init);
headers["host"] = url.host;
const service = options.service;
const region = options.region || "us-east-1";
const credentials = options.credentials || await getDefaultCredentialProvider();
const httpRequest = new import_protocol_http.HttpRequest({
method,
body,
headers,
hostname: url.hostname,
path: url.pathname,
protocol: url.protocol,
port: url.port ? Number(url.port) : void 0,
username: url.username,
password: url.password,
fragment: url.hash,
// Query paramaters accept multiple values per key
query: Array.from(url.searchParams.entries()).reduce((acc, [key, value]) => {
if (Array.isArray(acc[key])) {
acc[key].push(value);
} else {
acc[key] = [value];
}
return acc;
}, {})
});
const signer = new import_signature_v4.SignatureV4({
credentials,
service,
region,
sha256: import_sha256_js.Sha256
});
const { headers: signedHeaders } = await signer.sign(httpRequest);
return new Request(input, { ...init, headers: signedHeaders });
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
getDefaultCredentialProvider,
parseRequest,
signRequest
});