@@ -27,45 +27,56 @@ export function calculateHMAC(key: string | BinaryLike | KeyObject, message: str
2727 * @param timestamp request timestamp from `Date.now()`
2828 * @param statusCode Only set for HTTP responses, leave blank for requests
2929 * @param method request method
30- * @returns {string }
30+ * @param authVersion authentication version (2 or 3)
31+ * @returns {string | Buffer }
3132 */
32- export function calculateHMACSubject ( {
33+ export function calculateHMACSubject < T extends string | Buffer = string > ( {
3334 urlPath,
3435 text,
3536 timestamp,
3637 statusCode,
3738 method,
3839 authVersion,
39- } : CalculateHmacSubjectOptions ) : string {
40+ } : CalculateHmacSubjectOptions < T > ) : T {
4041 /* Normalize legacy 'del' to 'delete' for backward compatibility */
4142 if ( method === 'del' ) {
4243 method = 'delete' ;
4344 }
4445 const urlDetails = urlLib . parse ( urlPath ) ;
4546 const queryPath = urlDetails . query && urlDetails . query . length > 0 ? urlDetails . path : urlDetails . pathname ;
47+
48+ let prefixedText : string ;
4649 if ( statusCode !== undefined && isFinite ( statusCode ) && Number . isInteger ( statusCode ) ) {
47- if ( authVersion === 3 ) {
48- return [ method . toUpperCase ( ) , timestamp , queryPath , statusCode , text ] . join ( '|' ) ;
49- }
50- return [ timestamp , queryPath , statusCode , text ] . join ( '|' ) ;
50+ prefixedText =
51+ authVersion === 3
52+ ? [ method . toUpperCase ( ) , timestamp , queryPath , statusCode ] . join ( '|' )
53+ : [ timestamp , queryPath , statusCode ] . join ( '|' ) ;
54+ } else {
55+ prefixedText =
56+ authVersion === 3
57+ ? [ method . toUpperCase ( ) , timestamp , '3.0' , queryPath ] . join ( '|' )
58+ : [ timestamp , queryPath ] . join ( '|' ) ;
5159 }
52- if ( authVersion === 3 ) {
53- return [ method . toUpperCase ( ) , timestamp , '3.0' , queryPath , text ] . join ( '|' ) ;
60+ prefixedText += '|' ;
61+
62+ const isBuffer = Buffer . isBuffer ( text ) ;
63+ if ( isBuffer ) {
64+ return Buffer . concat ( [ Buffer . from ( prefixedText , 'utf-8' ) , text ] ) as T ;
5465 }
55- return [ timestamp , queryPath , text ] . join ( '|' ) ;
66+ return ( prefixedText + text ) as T ;
5667}
5768
5869/**
5970 * Calculate the HMAC for an HTTP request
6071 */
61- export function calculateRequestHMAC ( {
72+ export function calculateRequestHMAC < T extends string | Buffer = string > ( {
6273 url : urlPath ,
6374 text,
6475 timestamp,
6576 token,
6677 method,
6778 authVersion,
68- } : CalculateRequestHmacOptions ) : string {
79+ } : CalculateRequestHmacOptions < T > ) : string {
6980 const signatureSubject = calculateHMACSubject ( { urlPath, text, timestamp, method, authVersion } ) ;
7081
7182 // calculate the HMAC
@@ -75,13 +86,13 @@ export function calculateRequestHMAC({
7586/**
7687 * Calculate request headers with HMAC
7788 */
78- export function calculateRequestHeaders ( {
89+ export function calculateRequestHeaders < T extends string | Buffer = string > ( {
7990 url,
8091 text,
8192 token,
8293 method,
8394 authVersion,
84- } : CalculateRequestHeadersOptions ) : RequestHeaders {
95+ } : CalculateRequestHeadersOptions < T > ) : RequestHeaders {
8596 const timestamp = Date . now ( ) ;
8697 const hmac = calculateRequestHMAC ( { url, text, timestamp, token, method, authVersion } ) ;
8798
@@ -98,7 +109,7 @@ export function calculateRequestHeaders({
98109/**
99110 * Verify the HMAC for an HTTP response
100111 */
101- export function verifyResponse ( {
112+ export function verifyResponse < T extends string | Buffer = string > ( {
102113 url : urlPath ,
103114 statusCode,
104115 text,
@@ -107,7 +118,7 @@ export function verifyResponse({
107118 hmac,
108119 method,
109120 authVersion,
110- } : VerifyResponseOptions ) : VerifyResponseInfo {
121+ } : VerifyResponseOptions < T > ) : VerifyResponseInfo < T > {
111122 const signatureSubject = calculateHMACSubject ( {
112123 urlPath,
113124 text,
0 commit comments