@@ -762,16 +762,12 @@ export class Webhook {
762
762
}
763
763
764
764
public verify (
765
- payload : string ,
765
+ payload : string | Buffer ,
766
766
headers_ :
767
767
| WebhookRequiredHeaders
768
768
| WebhookUnbrandedRequiredHeaders
769
769
| Record < string , string >
770
770
) : unknown {
771
- if ( typeof payload !== "string" ) {
772
- throw new Error ( "Expected payload to be of type string. Please refer to https://docs.svix.com/receiving/verifying-payloads/how for more information." ) ;
773
- }
774
-
775
771
const headers : Record < string , string > = { } ;
776
772
for ( const key of Object . keys ( headers_ ) ) {
777
773
headers [ key . toLowerCase ( ) ] = ( headers_ as Record < string , string > ) [ key ] ;
@@ -806,13 +802,21 @@ export class Webhook {
806
802
}
807
803
808
804
if ( timingSafeEqual ( encoder . encode ( signature ) , encoder . encode ( expectedSignature ) ) ) {
809
- return JSON . parse ( payload ) ;
805
+ return JSON . parse ( payload . toString ( ) ) ;
810
806
}
811
807
}
812
808
throw new WebhookVerificationError ( "No matching signature found" ) ;
813
809
}
814
810
815
- public sign ( msgId : string , timestamp : Date , payload : string ) : string {
811
+ public sign ( msgId : string , timestamp : Date , payload : string | Buffer ) : string {
812
+ if ( typeof payload === "string" ) {
813
+ // Do nothing, already a string
814
+ } else if ( payload . constructor . name === "Buffer" ) {
815
+ payload = payload . toString ( ) ;
816
+ } else {
817
+ throw new Error ( "Expected payload to be of type string or Buffer. Please refer to https://docs.svix.com/receiving/verifying-payloads/how for more information." ) ;
818
+ }
819
+
816
820
const encoder = new TextEncoder ( ) ;
817
821
const timestampNumber = Math . floor ( timestamp . getTime ( ) / 1000 ) ;
818
822
const toSign = encoder . encode ( `${ msgId } .${ timestampNumber } .${ payload } ` ) ;
0 commit comments