import { MsSubtleCrypto } from '@aws-crypto/ie11-detection' ; type MsSubtleFunctions = keyof MsSubtleCrypto ; export default function promisifyMsSubtleCrypto ( backend ) { const usages = [ 'decrypt' , 'digest' , 'encrypt' , 'exportKey' , 'generateKey' , 'importKey' , 'sign' , 'verify' , ] ; const decorateUsage = ( fakeBackend , usage ) => decorate ( backend , fakeBackend , usage ) ; return usages . reduce ( decorateUsage , { } ) as SubtleCrypto ; } function decorate ( subtle , fakeBackend , name ) { fakeBackend [ name ] = async ( ... args ) => { return new Promise ( ( resolve , reject ) => { const operation = subtle [ name ] ( ... args ) ; operation . oncomplete = ( ) => resolve ( operation . result ) ; operation . onerror = reject ; } ) ; } ; return fakeBackend ; }