@aws-amplify/core
Version:
Core category of aws-amplify
33 lines (32 loc) • 1.32 kB
JavaScript
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { EMPTY_HASH, UNSIGNED_PAYLOAD } from '../constants';
import { getHashedDataAsHex } from './dataHashHelpers';
/**
* Returns the hashed payload.
*
* @param body `body` (payload) from the request.
* @returns String created using the payload in the body of the HTTP request as input to a hash function. This string
* uses lowercase hexadecimal characters. If the payload is empty, return precalculated result of an empty hash.
*
* @internal
*/
export var getHashedPayload = function (body) {
// return precalculated empty hash if body is undefined or null
if (body == null) {
return EMPTY_HASH;
}
if (isSourceData(body)) {
var hashedData = getHashedDataAsHex(null, body);
return hashedData;
}
// Defined body is not signable. Return unsigned payload which may or may not be accepted by the service.
return UNSIGNED_PAYLOAD;
};
var isSourceData = function (body) {
return typeof body === 'string' || ArrayBuffer.isView(body) || isArrayBuffer(body);
};
var isArrayBuffer = function (arg) {
return (typeof ArrayBuffer === 'function' && arg instanceof ArrayBuffer) ||
Object.prototype.toString.call(arg) === '[object ArrayBuffer]';
};