@fgiova/aws-signature
Version:
[](https://www.npmjs.com/package/@fgiova/aws-signature)  [ • 1.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createCanonicalRequest = createCanonicalRequest;
const getPayloadHash_1 = require("./getPayloadHash");
const getCanonicalQuery_1 = require("./getCanonicalQuery");
function getCanonicalPath({ path }, isS3) {
if (!isS3) {
// Non-S3 services, we normalize the path and then double URI encode it.
// Ref: "Remove Dot Segments" https://datatracker.ietf.org/doc/html/rfc3986#section-5.2.4
const normalizedPathSegments = [];
for (const pathSegment of path.split("/")) {
if (pathSegment?.length === 0)
continue;
if (pathSegment === ".")
continue;
if (pathSegment === "..") {
normalizedPathSegments.pop();
}
else {
normalizedPathSegments.push(pathSegment);
}
}
// Joining by single slashes to remove consecutive slashes.
const normalizedPath = `${path?.startsWith("/") ? "/" : ""}${normalizedPathSegments.join("/")}${normalizedPathSegments.length > 0 && path?.endsWith("/") ? "/" : ""}`;
const doubleEncoded = encodeURIComponent(normalizedPath);
return doubleEncoded.replace(/%2F/g, "/");
}
// For S3, we shouldn't normalize the path. For example, object name
// my-object//example//photo.user should not be normalized to
// my-object/example/photo.user
return path;
}
function createCanonicalRequest(request, canonicalHeaders, isS3 = false) {
const sortedHeaders = Object.keys(canonicalHeaders).sort();
return `${request.method}
${getCanonicalPath(request, isS3)}
${(0, getCanonicalQuery_1.getCanonicalQuery)(request)}
${sortedHeaders.map((name) => `${name}:${canonicalHeaders[name]}`).join("\n")}
${sortedHeaders.join(";")}
${(0, getPayloadHash_1.getPayloadHash)(request)}`;
}