UNPKG

@fgiova/aws-signature

Version:

[![NPM version](https://img.shields.io/npm/v/@fgiova/aws-signature.svg?style=flat)](https://www.npmjs.com/package/@fgiova/aws-signature) ![CI workflow](https://github.com/fgiova/aws-signature/actions/workflows/node.js.yml/badge.svg) [![TypeScript](https:/

43 lines (41 loc) 1.9 kB
"use strict"; 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)}`; }