diff --git a/packages.dhall b/packages.dhall index 62f06fd..06c6e1e 100644 --- a/packages.dhall +++ b/packages.dhall @@ -116,10 +116,9 @@ let additions = } ------------------------------- -} - - let upstream = - https://github.com/purescript/package-sets/releases/download/psc-0.13.6-20200423/packages.dhall sha256:c180a06bb5444fd950f8cbdd6605c644fd246deb397e62572b8f4a6b9dbcaf22 + https://github.com/purescript/package-sets/releases/download/psc-0.15.0-20220516/packages.dhall + sha256:b0bf932de16a10b7d69c6bbbb31ec9ca575237c43a999fa32e59e35eb8c024a1 let overrides = {=} diff --git a/spago.dhall b/spago.dhall index dd7ab78..78743a2 100644 --- a/spago.dhall +++ b/spago.dhall @@ -5,13 +5,21 @@ You can edit this file as you like. { name = "subtlecrypto" , dependencies = [ "aff" + , "aff-promise" + , "arraybuffer" , "arraybuffer-types" , "console" , "effect" + , "either" + , "exceptions" , "foreign" + , "functions" + , "maybe" , "prelude" - , "promises" - , "psci-support" + , "transformers" + , "tuples" + , "unsafe-coerce" + , "web-encoding" ] , packages = ./packages.dhall , sources = [ "src/**/*.purs", "test/**/*.purs" ] diff --git a/src/Crypto/Random.js b/src/Crypto/Random.js new file mode 100644 index 0000000..11389d9 --- /dev/null +++ b/src/Crypto/Random.js @@ -0,0 +1,6 @@ +export function getRandomValuesImpl (typedArray) { + return crypto.getRandomValues(typedArray); +}; +export function randomUUID () { + return crypto.randomUUID(); +}; \ No newline at end of file diff --git a/src/Crypto/Random.purs b/src/Crypto/Random.purs new file mode 100644 index 0000000..b26a019 --- /dev/null +++ b/src/Crypto/Random.purs @@ -0,0 +1,18 @@ +module Crypto.Random + ( getRandomValues + , randomUUID + ) + where + + +import Data.ArrayBuffer.Typed (class TypedArray) +import Data.ArrayBuffer.Types (ArrayView) +import Effect (Effect) +import Effect.Uncurried (EffectFn1, runEffectFn1) + +foreign import getRandomValuesImpl :: forall a . EffectFn1 (ArrayView a) (ArrayView a) + +foreign import randomUUID :: Effect String + +getRandomValues :: forall a t. TypedArray a t => ArrayView a -> Effect (ArrayView a) +getRandomValues ta = runEffectFn1 getRandomValuesImpl ta diff --git a/src/Crypto/Subtle/Encrypt.js b/src/Crypto/Subtle/Encrypt.js index 7fc15e8..ba91c7f 100644 --- a/src/Crypto/Subtle/Encrypt.js +++ b/src/Crypto/Subtle/Encrypt.js @@ -1,8 +1,6 @@ -"use strict"; - -exports.encryptImpl = function encryptImpl (a,k,x) { +export function encryptImpl (a,k,x) { return crypto.subtle.encrypt(a,k,x); }; -exports.decryptImpl = function decryptImpl (a,k,x) { +export function decryptImpl (a,k,x) { return crypto.subtle.decrypt(a,k,x); }; diff --git a/src/Crypto/Subtle/Encrypt.purs b/src/Crypto/Subtle/Encrypt.purs index 72ff2fb..76e666a 100644 --- a/src/Crypto/Subtle/Encrypt.purs +++ b/src/Crypto/Subtle/Encrypt.purs @@ -3,38 +3,39 @@ module Crypto.Subtle.Encrypt , EncryptAlgorithm, rsaOAEP, aesCTR, aesCBC, aesGCM, aesKW ) where -import Crypto.Subtle.Key.Types (CryptoKey) +import Control.Promise (Promise, toAff') import Crypto.Subtle.Constants.AES (AESTagLength) - -import Prelude ((<<<), (<$)) -import Data.Function.Uncurried (Fn3, runFn3) -import Data.Tuple (Tuple (..)) -import Data.Maybe (Maybe (..)) -import Data.Either (Either (..)) +import Crypto.Subtle.Key.Types (CryptoKey, errorFromDOMException) import Data.ArrayBuffer.Types (ArrayBuffer) -import Effect.Promise (Promise, runPromise) -import Effect.Aff (Aff, makeAff, nonCanceler) +import Data.Function.Uncurried (Fn3, runFn3) +import Data.Maybe (Maybe(..)) +import Data.Tuple (Tuple(..)) +import Effect.Aff (Aff) import Unsafe.Coerce (unsafeCoerce) foreign import data EncryptAlgorithm :: Type +-- | https://developer.mozilla.org/en-US/docs/Web/API/RsaOaepParams rsaOAEP :: Maybe ArrayBuffer -- ^ Label -> EncryptAlgorithm rsaOAEP mL = case mL of Nothing -> unsafeCoerce {name: "RSA_OAEP"} Just l -> unsafeCoerce {name: "RSA_OAEP", label: l} +-- | https://developer.mozilla.org/en-US/docs/Web/API/AesCtrParams aesCTR :: ArrayBuffer -- ^ Counter -> Int -- ^ Counter length -> EncryptAlgorithm aesCTR c l = unsafeCoerce {name: "AES-CTR", counter: c, length: l} +-- | https://developer.mozilla.org/en-US/docs/Web/API/AesCbcParams aesCBC :: ArrayBuffer -- ^ Initialization vector -> EncryptAlgorithm aesCBC i = unsafeCoerce {name: "AES-CBC", iv: i} +-- | https://developer.mozilla.org/en-US/docs/Web/API/AesGcmParams aesGCM :: ArrayBuffer -- ^ Initialization vector -> Maybe ArrayBuffer -- ^ Additional data -> Maybe AESTagLength -- ^ Tag length @@ -53,20 +54,20 @@ aesKW = unsafeCoerce {name: "AES-KW"} foreign import encryptImpl :: Fn3 EncryptAlgorithm CryptoKey ArrayBuffer (Promise ArrayBuffer) +-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/encrypt encrypt :: EncryptAlgorithm -> CryptoKey -> ArrayBuffer -> Aff ArrayBuffer -encrypt a k x = makeAff \resolve -> - nonCanceler <$ runPromise (resolve <<< Right) (resolve <<< Left) (runFn3 encryptImpl a k x) +encrypt a k x = toAff' errorFromDOMException (runFn3 encryptImpl a k x) foreign import decryptImpl :: Fn3 EncryptAlgorithm CryptoKey ArrayBuffer (Promise ArrayBuffer) +-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/decrypt decrypt :: EncryptAlgorithm -> CryptoKey -> ArrayBuffer - -> Aff (Maybe ArrayBuffer) -decrypt a k x = makeAff \resolve -> - nonCanceler <$ runPromise (resolve <<< Right <<< Just) (\_ -> resolve (Right Nothing)) (runFn3 decryptImpl a k x) + -> Aff ArrayBuffer +decrypt a k x = toAff' errorFromDOMException (runFn3 decryptImpl a k x) diff --git a/src/Crypto/Subtle/Hash.js b/src/Crypto/Subtle/Hash.js index 3f0b9f4..168acc9 100644 --- a/src/Crypto/Subtle/Hash.js +++ b/src/Crypto/Subtle/Hash.js @@ -1,5 +1,3 @@ -"use strict"; - -exports.digestImpl = function digestImpl (h,x) { +export function digestImpl (h,x) { return crypto.subtle.digest(h,x); }; diff --git a/src/Crypto/Subtle/Hash.purs b/src/Crypto/Subtle/Hash.purs index a0398d1..0fe4529 100644 --- a/src/Crypto/Subtle/Hash.purs +++ b/src/Crypto/Subtle/Hash.purs @@ -1,11 +1,11 @@ module Crypto.Subtle.Hash (HashingFunction, sha1, sha256, sha384, sha512, digest) where -import Prelude ((<<<), (<$), class Eq) +import Control.Promise (Promise, toAff') +import Crypto.Subtle.Key.Types (errorFromDOMException) import Data.ArrayBuffer.Types (ArrayBuffer) import Data.Function.Uncurried (Fn2, runFn2) -import Data.Either (Either (..)) -import Effect.Promise (Promise, runPromise) -import Effect.Aff (Aff, makeAff, nonCanceler) +import Effect.Aff (Aff) +import Prelude (class Eq) @@ -25,11 +25,8 @@ sha384 = HashingFunction "SHA-384" sha512 :: HashingFunction sha512 = HashingFunction "SHA-512" - foreign import digestImpl :: Fn2 HashingFunction ArrayBuffer (Promise ArrayBuffer) - +-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest digest :: HashingFunction -> ArrayBuffer -> Aff ArrayBuffer -digest h x = - let p = runFn2 digestImpl h x - in makeAff \resolve -> nonCanceler <$ runPromise (resolve <<< Right) (resolve <<< Left) p +digest h x = toAff' errorFromDOMException (runFn2 digestImpl h x) diff --git a/src/Crypto/Subtle/Key/Derive.js b/src/Crypto/Subtle/Key/Derive.js index fea90dd..9a7b9af 100644 --- a/src/Crypto/Subtle/Key/Derive.js +++ b/src/Crypto/Subtle/Key/Derive.js @@ -1,8 +1,6 @@ -"use strict"; - -exports.deriveKeyImpl = function deriveKeyImpl (a,k,t,e,u) { +export function deriveKeyImpl (a,k,t,e,u) { return crypto.subtle.deriveKey(a,k,t,e,u); }; -exports.deriveBitsImpl = function deriveBitsImpl (a,k,l) { +export function deriveBitsImpl (a,k,l) { return crypto.subtle.deriveBits(a,k,l); }; diff --git a/src/Crypto/Subtle/Key/Derive.purs b/src/Crypto/Subtle/Key/Derive.purs index c479da0..fcb0bda 100644 --- a/src/Crypto/Subtle/Key/Derive.purs +++ b/src/Crypto/Subtle/Key/Derive.purs @@ -5,17 +5,14 @@ module Crypto.Subtle.Key.Derive , DeriveTargetAlgorithm, hmac, aes ) where -import Crypto.Subtle.Key.Types (CryptoKey, CryptoKeyUsage) -import Crypto.Subtle.Hash (HashingFunction) -import Crypto.Subtle.Constants.EC (ECAlgorithm) +import Control.Promise (Promise, toAff') import Crypto.Subtle.Constants.AES (AESAlgorithm, AESBitLength) - -import Prelude ((<<<), (<$)) -import Data.Function.Uncurried (Fn3, Fn5, runFn3, runFn5) +import Crypto.Subtle.Constants.EC (ECAlgorithm) +import Crypto.Subtle.Hash (HashingFunction) +import Crypto.Subtle.Key.Types (CryptoKey, CryptoKeyUsage, errorFromDOMException) import Data.ArrayBuffer.Types (ArrayBuffer) -import Data.Either (Either (..)) -import Effect.Promise (Promise, runPromise) -import Effect.Aff (Aff, makeAff, nonCanceler) +import Data.Function.Uncurried (Fn3, Fn5, runFn3, runFn5) +import Effect.Aff (Aff) import Unsafe.Coerce (unsafeCoerce) @@ -24,38 +21,41 @@ foreign import deriveKeyImpl :: Fn5 DeriveAlgorithm CryptoKey DeriveTargetAlgori foreign import deriveBitsImpl :: Fn3 DeriveAlgorithm CryptoKey Int (Promise ArrayBuffer) +-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/deriveKey deriveKey :: DeriveAlgorithm -> CryptoKey -- ^ Base key -> DeriveTargetAlgorithm -> Boolean -- ^ Extractable -> Array CryptoKeyUsage -> Aff CryptoKey -deriveKey a k t e u = makeAff \resolve -> - nonCanceler <$ runPromise (resolve <<< Right) (resolve <<< Left) (runFn5 deriveKeyImpl a k t e u) +deriveKey a k t e u = toAff' errorFromDOMException (runFn5 deriveKeyImpl a k t e u) +-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/deriveBits deriveBits :: DeriveAlgorithm -> CryptoKey -- ^ Base key -> Int -- ^ Length in bits -> Aff ArrayBuffer -deriveBits a k l = makeAff \resolve -> - nonCanceler <$ runPromise (resolve <<< Right) (resolve <<< Left) (runFn3 deriveBitsImpl a k l) +deriveBits a k l = toAff' errorFromDOMException (runFn3 deriveBitsImpl a k l) foreign import data DeriveAlgorithm :: Type foreign import data DeriveTargetAlgorithm :: Type +-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/deriveKey#ecdh ec :: ECAlgorithm -> CryptoKey -- ^ Public key of the other entity -> DeriveAlgorithm ec e k = unsafeCoerce {name: e, public: k} +-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/deriveKey#hkdf hkdf :: HashingFunction -> ArrayBuffer -- ^ Salt -> ArrayBuffer -- ^ Info -> DeriveAlgorithm hkdf h s i = unsafeCoerce {name: "HKDF", hash: h, salt: s, info: i} +-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/deriveKey#pbkdf2 pbkdf2 :: HashingFunction -> ArrayBuffer -- ^ Salt -> Int -- ^ Iterations @@ -65,8 +65,10 @@ pbkdf2 h s i = unsafeCoerce {name: "PBKDF2", hash: h, salt: s, iterations: i} +-- | https://developer.mozilla.org/en-US/docs/Web/API/HmacKeyGenParams hmac :: HashingFunction -> DeriveTargetAlgorithm hmac h = unsafeCoerce {name: "HMAC", hash: h} +-- | https://developer.mozilla.org/en-US/docs/Web/API/AesKeyGenParams aes :: AESAlgorithm -> AESBitLength -> DeriveTargetAlgorithm aes a l = unsafeCoerce {name: a, length: l} diff --git a/src/Crypto/Subtle/Key/Generate.js b/src/Crypto/Subtle/Key/Generate.js index d28a8d2..5f55c45 100644 --- a/src/Crypto/Subtle/Key/Generate.js +++ b/src/Crypto/Subtle/Key/Generate.js @@ -1,6 +1,4 @@ -"use strict"; - -exports.generateKeyImpl = function generateKeyImpl (a,e,u) { +export function generateKeyImpl (a,e,u) { return crypto.subtle.generateKey(a,e,u); }; -exports.exp65537 = new Uint8Array([0x01,0x00,0x01]); +export const exp65537 = new Uint8Array([0x01,0x00,0x01]); diff --git a/src/Crypto/Subtle/Key/Generate.purs b/src/Crypto/Subtle/Key/Generate.purs index 9ea6275..ca142bc 100644 --- a/src/Crypto/Subtle/Key/Generate.purs +++ b/src/Crypto/Subtle/Key/Generate.purs @@ -5,18 +5,15 @@ module Crypto.Subtle.Key.Generate , exp65537 ) where -import Crypto.Subtle.Key.Types (CryptoKey, CryptoKeyPair, CryptoKeyUsage) -import Crypto.Subtle.Hash (HashingFunction) -import Crypto.Subtle.Constants.RSA (RSAAlgorithm) -import Crypto.Subtle.Constants.EC (ECAlgorithm, ECCurve) +import Control.Promise (Promise, toAff') import Crypto.Subtle.Constants.AES (AESAlgorithm, AESBitLength) - -import Prelude ((<<<), (<$)) -import Data.Function.Uncurried (Fn3, runFn3) +import Crypto.Subtle.Constants.EC (ECAlgorithm, ECCurve) +import Crypto.Subtle.Constants.RSA (RSAAlgorithm) +import Crypto.Subtle.Hash (HashingFunction) +import Crypto.Subtle.Key.Types (CryptoKey, CryptoKeyPair, CryptoKeyUsage, errorFromDOMException) import Data.ArrayBuffer.Types (Uint8Array) -import Data.Either (Either (..)) -import Effect.Promise (Promise, runPromise) -import Effect.Aff (Aff, makeAff, nonCanceler) +import Data.Function.Uncurried (Fn3, runFn3) +import Effect.Aff (Aff) import Unsafe.Coerce (unsafeCoerce) @@ -25,20 +22,22 @@ foreign import generateKeyImpl :: forall a b. Fn3 a Boolean (Array CryptoKeyUsag -- | Generate a symmetric key +-- | +-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/generateKey generateKey :: SymmetricAlgorithm -> Boolean -- ^ Extractable -> Array CryptoKeyUsage -> Aff CryptoKey -generateKey a e u = makeAff \resolve -> - nonCanceler <$ runPromise (resolve <<< Right) (resolve <<< Left) (runFn3 generateKeyImpl a e u) +generateKey a e u = toAff' errorFromDOMException (runFn3 generateKeyImpl a e u) -- | Generate an asymmetric keypair +-- | +-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/generateKey generateKeyPair :: AsymmetricAlgorithm -> Boolean -- ^ Extractable -> Array CryptoKeyUsage -> Aff CryptoKeyPair -generateKeyPair a e u = makeAff \resolve -> - nonCanceler <$ runPromise (resolve <<< Right) (resolve <<< Left) (runFn3 generateKeyImpl a e u) +generateKeyPair a e u = toAff' errorFromDOMException (runFn3 generateKeyImpl a e u) foreign import data SymmetricAlgorithm :: Type @@ -48,6 +47,7 @@ foreign import data AsymmetricAlgorithm :: Type foreign import exp65537 :: Uint8Array +-- | https://developer.mozilla.org/en-US/docs/Web/API/RsaHashedKeyGenParams rsa :: RSAAlgorithm -> Int -- ^ Modulus length. Should be at least 2048, or 4096 according to some bozo -> Uint8Array -- ^ Public exponent. Just use `exp65537`. @@ -55,12 +55,15 @@ rsa :: RSAAlgorithm -> AsymmetricAlgorithm rsa r l e h = unsafeCoerce {name: r, modulusLength: l, publicExponent: e, hash: h} +-- | https://developer.mozilla.org/en-US/docs/Web/API/EcKeyGenParams ec :: ECAlgorithm -> ECCurve -> AsymmetricAlgorithm ec e c = unsafeCoerce {name: e, namedCurve: c} +-- | https://developer.mozilla.org/en-US/docs/Web/API/HmacKeyGenParams hmac :: HashingFunction -> SymmetricAlgorithm hmac h = unsafeCoerce {name: "HMAC", hash: h} +-- | https://developer.mozilla.org/en-US/docs/Web/API/AesKeyGenParams aes :: AESAlgorithm -> AESBitLength -> SymmetricAlgorithm diff --git a/src/Crypto/Subtle/Key/Import.js b/src/Crypto/Subtle/Key/Import.js index fb73401..2b82c8a 100644 --- a/src/Crypto/Subtle/Key/Import.js +++ b/src/Crypto/Subtle/Key/Import.js @@ -1,5 +1,3 @@ -"use strict"; - -exports.importKeyImpl = function importKeyImpl (f,x,a,e,u) { +export function importKeyImpl (f,x,a,e,u) { return crypto.subtle.importKey(f,x,a,e,u); }; diff --git a/src/Crypto/Subtle/Key/Import.purs b/src/Crypto/Subtle/Key/Import.purs index 84e54f6..a2d1b36 100644 --- a/src/Crypto/Subtle/Key/Import.purs +++ b/src/Crypto/Subtle/Key/Import.purs @@ -1,20 +1,24 @@ module Crypto.Subtle.Key.Import - ( importKey - , ImportAlgorithm, rsa, ec, hmac, aes - ) where - -import Crypto.Subtle.Key.Types (CryptoKey, CryptoKeyUsage, ExternalFormat) -import Crypto.Subtle.Hash (HashingFunction) -import Crypto.Subtle.Constants.RSA (RSAAlgorithm) -import Crypto.Subtle.Constants.EC (ECAlgorithm, ECCurve) + ( ImportAlgorithm + , aes + , ec + , hkdf + , hmac + , importKey + , pbkdf2 + , rsa + ) + where + +import Control.Promise (Promise, toAff') import Crypto.Subtle.Constants.AES (AESAlgorithm) - -import Prelude ((<<<), (<$)) -import Data.Function.Uncurried (Fn5, runFn5) +import Crypto.Subtle.Constants.EC (ECAlgorithm, ECCurve) +import Crypto.Subtle.Constants.RSA (RSAAlgorithm) +import Crypto.Subtle.Hash (HashingFunction) +import Crypto.Subtle.Key.Types (CryptoKey, CryptoKeyUsage, ExternalFormat, errorFromDOMException) import Data.ArrayBuffer.Types (ArrayBuffer) -import Data.Either (Either (..)) -import Effect.Promise (Promise, runPromise) -import Effect.Aff (Aff, makeAff, nonCanceler) +import Data.Function.Uncurried (Fn5, runFn5) +import Effect.Aff (Aff) import Unsafe.Coerce (unsafeCoerce) @@ -22,16 +26,14 @@ import Unsafe.Coerce (unsafeCoerce) foreign import importKeyImpl :: Fn5 ExternalFormat ArrayBuffer ImportAlgorithm Boolean (Array CryptoKeyUsage) (Promise CryptoKey) +-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/importKey importKey :: ExternalFormat -> ArrayBuffer -- ^ Key data -> ImportAlgorithm -> Boolean -- ^ Extractable -> Array CryptoKeyUsage -> Aff CryptoKey -importKey f x a e u = makeAff \resolve -> - nonCanceler <$ runPromise (resolve <<< Right) (resolve <<< Left) (runFn5 importKeyImpl f x a e u) - - +importKey f x a e u = toAff' errorFromDOMException (runFn5 importKeyImpl f x a e u) foreign import data ImportAlgorithm :: Type @@ -48,3 +50,9 @@ hmac h = unsafeCoerce {name: "HMAC", hash: h} aes :: AESAlgorithm -> ImportAlgorithm aes a = unsafeCoerce {name: a} + +pbkdf2 :: ImportAlgorithm +pbkdf2 = unsafeCoerce { name: "PBKDF2" } + +hkdf :: ImportAlgorithm +hkdf = unsafeCoerce { name: "HKDF" } \ No newline at end of file diff --git a/src/Crypto/Subtle/Key/Types.js b/src/Crypto/Subtle/Key/Types.js index c7e4d4b..28c54f4 100644 --- a/src/Crypto/Subtle/Key/Types.js +++ b/src/Crypto/Subtle/Key/Types.js @@ -1,5 +1,3 @@ -"use strict"; - -exports.exportKeyImpl = function exportKeyImpl (f,x) { +export function exportKeyImpl (f,x) { return crypto.subtle.exportKey(f,x); }; diff --git a/src/Crypto/Subtle/Key/Types.purs b/src/Crypto/Subtle/Key/Types.purs index 232489a..6a4bf29 100644 --- a/src/Crypto/Subtle/Key/Types.purs +++ b/src/Crypto/Subtle/Key/Types.purs @@ -1,24 +1,43 @@ module Crypto.Subtle.Key.Types - ( CryptoKeyType - , secret, public, private + ( CryptoKey + , CryptoKeyPair(..) + , CryptoKeyType , CryptoKeyUsage - , encrypt, decrypt, sign, verify, deriveKey, deriveBits, unwrapKey, wrapKey + , ExternalFormat , allUsages - , CryptoKey - , getType, getExtractable, getAlgorithm, getUsages + , decrypt + , deriveBits + , deriveKey + , encrypt + , errorFromDOMException , exportKey - , CryptoKeyPair (..) - , ExternalFormat, raw, pkcs8, spki, jwk - ) where - - -import Prelude ((<<<), (<$), class Eq) -import Data.Either (Either (..)) -import Data.Function.Uncurried (Fn2, runFn2) + , getAlgorithm + , getExtractable + , getType + , getUsages + , jwk + , pkcs8 + , private + , public + , raw + , secret + , sign + , spki + , unwrapKey + , verify + , wrapKey + ) + where + +import Prelude ((>>=), class Eq) +import Control.Monad.Except (runExcept) +import Control.Promise (Promise, toAff') import Data.ArrayBuffer.Types (ArrayBuffer) -import Foreign (Foreign) -import Effect.Aff (Aff, makeAff, nonCanceler) -import Effect.Promise (Promise, runPromise) +import Data.Either (Either(..)) +import Data.Function.Uncurried (Fn2, runFn2) +import Effect.Aff (Aff, Error, error) +import Foreign (Foreign, readString) +import Foreign.Index (readProp) import Unsafe.Coerce (unsafeCoerce) @@ -103,8 +122,17 @@ jwk = ExternalFormat "jwk" foreign import exportKeyImpl :: Fn2 ExternalFormat CryptoKey (Promise ArrayBuffer) +-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/exportKey exportKey :: ExternalFormat -> CryptoKey -> Aff ArrayBuffer -exportKey f x = makeAff \resolve -> - nonCanceler <$ runPromise (resolve <<< Right) (resolve <<< Left) (runFn2 exportKeyImpl f x) +exportKey f x = toAff' errorFromDOMException (runFn2 exportKeyImpl f x) + +-- Most of the Promises throw DOMException, so read the message from the DOMException. +-- https://developer.mozilla.org/en-US/docs/Web/API/DOMException +-- Some of the Promises throw TypeError, which also has a message property. +-- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError +errorFromDOMException :: Foreign -> Error +errorFromDOMException x = case runExcept (readProp "message" x >>= readString) of + Left _ -> error "Not a DOMException" + Right m -> error m diff --git a/src/Crypto/Subtle/Key/Wrap.js b/src/Crypto/Subtle/Key/Wrap.js index 193b762..5d2e0ef 100644 --- a/src/Crypto/Subtle/Key/Wrap.js +++ b/src/Crypto/Subtle/Key/Wrap.js @@ -1,8 +1,6 @@ -"use strict"; - -exports.wrapKeyImpl = function wrapKeyImpl (f,x,k,a) { +export function wrapKeyImpl (f,x,k,a) { return crypto.subtle.wrapKey(f,x,k,a); }; -exports.unwrapKeyImpl = function unwrapKeyImpl (f,x,k,a,i,e,u) { +export function unwrapKeyImpl (f,x,k,a,i,e,u) { return crypto.subtle.unwrapKey(f,x,k,a,i,e,u); }; diff --git a/src/Crypto/Subtle/Key/Wrap.purs b/src/Crypto/Subtle/Key/Wrap.purs index a570005..6812f77 100644 --- a/src/Crypto/Subtle/Key/Wrap.purs +++ b/src/Crypto/Subtle/Key/Wrap.purs @@ -1,33 +1,30 @@ module Crypto.Subtle.Key.Wrap where -import Crypto.Subtle.Key.Types (CryptoKey, ExternalFormat, CryptoKeyUsage) -import Crypto.Subtle.Key.Import (ImportAlgorithm) +import Control.Promise (Promise, toAff') import Crypto.Subtle.Encrypt (EncryptAlgorithm) - -import Prelude ((<<<), (<$)) -import Data.Function.Uncurried (Fn4, Fn7, runFn4, runFn7) -import Data.Maybe (Maybe (..)) -import Data.Either (Either (..)) +import Crypto.Subtle.Key.Import (ImportAlgorithm) +import Crypto.Subtle.Key.Types (CryptoKey, CryptoKeyUsage, ExternalFormat, errorFromDOMException) import Data.ArrayBuffer.Types (ArrayBuffer) -import Effect.Promise (Promise, runPromise) -import Effect.Aff (Aff, makeAff, nonCanceler) +import Data.Function.Uncurried (Fn4, Fn7, runFn4, runFn7) +import Effect.Aff (Aff) foreign import wrapKeyImpl :: Fn4 ExternalFormat CryptoKey CryptoKey EncryptAlgorithm (Promise ArrayBuffer) +-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/wrapKey wrapKey :: ExternalFormat -> CryptoKey -- ^ Subject key -> CryptoKey -- ^ Wrapping key -> EncryptAlgorithm -> Aff ArrayBuffer -wrapKey f x k a = makeAff \resolve -> - nonCanceler <$ runPromise (resolve <<< Right) (resolve <<< Left) (runFn4 wrapKeyImpl f x k a) +wrapKey f x k a = toAff' errorFromDOMException (runFn4 wrapKeyImpl f x k a) foreign import unwrapKeyImpl :: Fn7 ExternalFormat ArrayBuffer CryptoKey EncryptAlgorithm ImportAlgorithm Boolean (Array CryptoKeyUsage) (Promise CryptoKey) +-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/unwrapKey unwrapKey :: ExternalFormat -> ArrayBuffer -- ^ Wrapped key -> CryptoKey -- ^ Unwrapping key @@ -35,6 +32,6 @@ unwrapKey :: ExternalFormat -> ImportAlgorithm -> Boolean -- ^ Extractable -> Array CryptoKeyUsage - -> Aff (Maybe CryptoKey) -unwrapKey f x k a i e u = makeAff \resolve -> - nonCanceler <$ runPromise (resolve <<< Right <<< Just) (\_ -> resolve (Right Nothing)) (runFn7 unwrapKeyImpl f x k a i e u) + -- -> Aff (Maybe CryptoKey) + -> Aff CryptoKey +unwrapKey f x k a i e u = toAff' errorFromDOMException (runFn7 unwrapKeyImpl f x k a i e u) diff --git a/src/Crypto/Subtle/Sign.js b/src/Crypto/Subtle/Sign.js index 21114b0..ddeee99 100644 --- a/src/Crypto/Subtle/Sign.js +++ b/src/Crypto/Subtle/Sign.js @@ -1,8 +1,6 @@ -"use strict"; - -exports.signImpl = function signImpl (a,k,x) { +export function signImpl (a,k,x) { return crypto.subtle.sign(a,k,x); }; -exports.verifyImpl = function verifyImpl (a,k,s,x) { - return crypto.subtle.verify(a,k,s,x); -}; +export function verifyImpl(a, k, s, x) { + return crypto.subtle.verify(a, k, s, x); +} diff --git a/src/Crypto/Subtle/Sign.purs b/src/Crypto/Subtle/Sign.purs index 478deb4..695a0b3 100644 --- a/src/Crypto/Subtle/Sign.purs +++ b/src/Crypto/Subtle/Sign.purs @@ -3,15 +3,12 @@ module Crypto.Subtle.Sign , SignAlgorithm, rsaPKCS1, rsaPSS, ecdsa, hmac ) where -import Crypto.Subtle.Key.Types (CryptoKey) +import Control.Promise (Promise, toAff') import Crypto.Subtle.Hash (HashingFunction) - -import Prelude ((<<<), (<$)) -import Data.Function.Uncurried (Fn3, Fn4, runFn3, runFn4) -import Data.Either (Either (..)) +import Crypto.Subtle.Key.Types (CryptoKey, errorFromDOMException) import Data.ArrayBuffer.Types (ArrayBuffer) -import Effect.Promise (Promise, runPromise) -import Effect.Aff (Aff, makeAff, nonCanceler) +import Data.Function.Uncurried (Fn3, Fn4, runFn3, runFn4) +import Effect.Aff (Aff) import Unsafe.Coerce (unsafeCoerce) @@ -21,10 +18,12 @@ foreign import data SignAlgorithm :: Type rsaPKCS1 :: SignAlgorithm rsaPKCS1 = unsafeCoerce {name: "RSASSA-PKCS1-v1_5"} +-- | https://developer.mozilla.org/en-US/docs/Web/API/RsaPssParams rsaPSS :: Int -- ^ Salt length -> SignAlgorithm rsaPSS l = unsafeCoerce {name: "RSA-PSS", saltLength: l} +-- | https://developer.mozilla.org/en-US/docs/Web/API/EcdsaParams ecdsa :: HashingFunction -> SignAlgorithm ecdsa h = unsafeCoerce {name: "ECDSA", hash: h} @@ -35,20 +34,20 @@ hmac = unsafeCoerce {name: "HMAC"} foreign import signImpl :: Fn3 SignAlgorithm CryptoKey ArrayBuffer (Promise ArrayBuffer) +-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/sign sign :: SignAlgorithm -> CryptoKey -> ArrayBuffer -> Aff ArrayBuffer -sign a k x = makeAff \resolve -> - nonCanceler <$ runPromise (resolve <<< Right) (resolve <<< Left) (runFn3 signImpl a k x) +sign a k x = toAff' errorFromDOMException (runFn3 signImpl a k x) foreign import verifyImpl :: Fn4 SignAlgorithm CryptoKey ArrayBuffer ArrayBuffer (Promise Boolean) +-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/verify verify :: SignAlgorithm -> CryptoKey -> ArrayBuffer -- ^ Signature -> ArrayBuffer -- ^ Subject data -> Aff Boolean -verify a k s x = makeAff \resolve -> - nonCanceler <$ runPromise (resolve <<< Right) (resolve <<< Left) (runFn4 verifyImpl a k s x) +verify a k s x = toAff' errorFromDOMException (runFn4 verifyImpl a k s x) diff --git a/test.browser.js b/test.browser.js index 525be76..b2ed1a6 100644 --- a/test.browser.js +++ b/test.browser.js @@ -1,493 +1,939 @@ -// Generated by purs bundle 0.13.6 -var PS = {}; -(function($PS) { - // Generated by purs version 0.13.6 - "use strict"; - $PS["Control.Apply"] = $PS["Control.Apply"] || {}; - var exports = $PS["Control.Apply"]; - var Apply = function (Functor0, apply) { - this.Functor0 = Functor0; - this.apply = apply; - }; - var apply = function (dict) { - return dict.apply; - }; - exports["Apply"] = Apply; - exports["apply"] = apply; -})(PS); -(function($PS) { - // Generated by purs version 0.13.6 - "use strict"; - $PS["Control.Applicative"] = $PS["Control.Applicative"] || {}; - var exports = $PS["Control.Applicative"]; - var Control_Apply = $PS["Control.Apply"]; - var Applicative = function (Apply0, pure) { - this.Apply0 = Apply0; - this.pure = pure; - }; - var pure = function (dict) { - return dict.pure; - }; - var liftA1 = function (dictApplicative) { - return function (f) { - return function (a) { - return Control_Apply.apply(dictApplicative.Apply0())(pure(dictApplicative)(f))(a); - }; +(() => { + // output/Control.Semigroupoid/index.js + var semigroupoidFn = { + compose: function(f) { + return function(g) { + return function(x) { + return f(g(x)); + }; }; + } }; - exports["Applicative"] = Applicative; - exports["pure"] = pure; - exports["liftA1"] = liftA1; -})(PS); -(function($PS) { - // Generated by purs version 0.13.6 - "use strict"; - $PS["Data.Function"] = $PS["Data.Function"] || {}; - var exports = $PS["Data.Function"]; - var flip = function (f) { - return function (b) { - return function (a) { - return f(a)(b); - }; - }; + + // output/Control.Category/index.js + var identity = function(dict) { + return dict.identity; }; - var $$const = function (a) { - return function (v) { - return a; - }; + var categoryFn = { + identity: function(x) { + return x; + }, + Semigroupoid0: function() { + return semigroupoidFn; + } }; - exports["flip"] = flip; - exports["const"] = $$const; -})(PS); -(function($PS) { - // Generated by purs version 0.13.6 - "use strict"; - $PS["Control.Bind"] = $PS["Control.Bind"] || {}; - var exports = $PS["Control.Bind"]; - var Data_Function = $PS["Data.Function"]; - var Discard = function (discard) { - this.discard = discard; - }; - var Bind = function (Apply0, bind) { - this.Apply0 = Apply0; - this.bind = bind; - }; - var discard = function (dict) { - return dict.discard; - }; - var bind = function (dict) { - return dict.bind; - }; - var bindFlipped = function (dictBind) { - return Data_Function.flip(bind(dictBind)); - }; - var discardUnit = new Discard(function (dictBind) { - return bind(dictBind); - }); - exports["Bind"] = Bind; - exports["bind"] = bind; - exports["bindFlipped"] = bindFlipped; - exports["discard"] = discard; - exports["discardUnit"] = discardUnit; -})(PS); -(function($PS) { - // Generated by purs version 0.13.6 - "use strict"; - $PS["Control.Monad"] = $PS["Control.Monad"] || {}; - var exports = $PS["Control.Monad"]; - var Control_Applicative = $PS["Control.Applicative"]; - var Control_Bind = $PS["Control.Bind"]; - var Monad = function (Applicative0, Bind1) { - this.Applicative0 = Applicative0; - this.Bind1 = Bind1; - }; - var ap = function (dictMonad) { - return function (f) { - return function (a) { - return Control_Bind.bind(dictMonad.Bind1())(f)(function (f$prime) { - return Control_Bind.bind(dictMonad.Bind1())(a)(function (a$prime) { - return Control_Applicative.pure(dictMonad.Applicative0())(f$prime(a$prime)); - }); - }); - }; + + // output/Data.Boolean/index.js + var otherwise = true; + + // output/Data.Function/index.js + var flip = function(f) { + return function(b) { + return function(a) { + return f(a)(b); }; + }; }; - exports["Monad"] = Monad; - exports["ap"] = ap; -})(PS); -(function($PS) { - // Generated by purs version 0.13.6 - "use strict"; - $PS["Data.Either"] = $PS["Data.Either"] || {}; - var exports = $PS["Data.Either"]; - var Left = (function () { - function Left(value0) { - this.value0 = value0; - }; - Left.create = function (value0) { - return new Left(value0); - }; - return Left; - })(); - var Right = (function () { - function Right(value0) { - this.value0 = value0; - }; - Right.create = function (value0) { - return new Right(value0); + var $$const = function(a) { + return function(v) { + return a; + }; + }; + + // output/Data.Unit/foreign.js + var unit = void 0; + + // output/Data.Functor/index.js + var map = function(dict) { + return dict.map; + }; + var $$void = function(dictFunctor) { + return map(dictFunctor)($$const(unit)); + }; + var voidRight = function(dictFunctor) { + var map1 = map(dictFunctor); + return function(x) { + return map1($$const(x)); + }; + }; + + // output/Control.Apply/index.js + var identity2 = /* @__PURE__ */ identity(categoryFn); + var apply = function(dict) { + return dict.apply; + }; + var applySecond = function(dictApply) { + var apply1 = apply(dictApply); + var map3 = map(dictApply.Functor0()); + return function(a) { + return function(b) { + return apply1(map3($$const(identity2))(a))(b); }; - return Right; - })(); - exports["Left"] = Left; - exports["Right"] = Right; -})(PS); -(function(exports) { - "use strict"; - - exports.unit = {}; -})(PS["Data.Unit"] = PS["Data.Unit"] || {}); -(function($PS) { - // Generated by purs version 0.13.6 - "use strict"; - $PS["Data.Unit"] = $PS["Data.Unit"] || {}; - var exports = $PS["Data.Unit"]; - var $foreign = $PS["Data.Unit"]; - exports["unit"] = $foreign.unit; -})(PS); -(function($PS) { - // Generated by purs version 0.13.6 - "use strict"; - $PS["Data.Functor"] = $PS["Data.Functor"] || {}; - var exports = $PS["Data.Functor"]; - var Data_Function = $PS["Data.Function"]; - var Data_Unit = $PS["Data.Unit"]; - var Functor = function (map) { - this.map = map; - }; - var map = function (dict) { - return dict.map; - }; - var $$void = function (dictFunctor) { - return map(dictFunctor)(Data_Function["const"](Data_Unit.unit)); - }; - var voidRight = function (dictFunctor) { - return function (x) { - return map(dictFunctor)(Data_Function["const"](x)); + }; + }; + + // output/Control.Applicative/index.js + var pure = function(dict) { + return dict.pure; + }; + var liftA1 = function(dictApplicative) { + var apply2 = apply(dictApplicative.Apply0()); + var pure1 = pure(dictApplicative); + return function(f) { + return function(a) { + return apply2(pure1(f))(a); }; + }; }; - exports["Functor"] = Functor; - exports["map"] = map; - exports["void"] = $$void; - exports["voidRight"] = voidRight; -})(PS); -(function($PS) { - // Generated by purs version 0.13.6 - "use strict"; - $PS["Control.Monad.Error.Class"] = $PS["Control.Monad.Error.Class"] || {}; - var exports = $PS["Control.Monad.Error.Class"]; - var Control_Applicative = $PS["Control.Applicative"]; - var Data_Either = $PS["Data.Either"]; - var Data_Functor = $PS["Data.Functor"]; - var MonadThrow = function (Monad0, throwError) { - this.Monad0 = Monad0; - this.throwError = throwError; - }; - var MonadError = function (MonadThrow0, catchError) { - this.MonadThrow0 = MonadThrow0; - this.catchError = catchError; - }; - var catchError = function (dict) { - return dict.catchError; - }; - var $$try = function (dictMonadError) { - return function (a) { - return catchError(dictMonadError)(Data_Functor.map(((((dictMonadError.MonadThrow0()).Monad0()).Bind1()).Apply0()).Functor0())(Data_Either.Right.create)(a))((function () { - var $17 = Control_Applicative.pure(((dictMonadError.MonadThrow0()).Monad0()).Applicative0()); - return function ($18) { - return $17(Data_Either.Left.create($18)); - }; - })()); + + // output/Control.Bind/index.js + var discard = function(dict) { + return dict.discard; + }; + var bind = function(dict) { + return dict.bind; + }; + var bindFlipped = function(dictBind) { + return flip(bind(dictBind)); + }; + var discardUnit = { + discard: function(dictBind) { + return bind(dictBind); + } + }; + + // output/Crypto.Random/foreign.js + function getRandomValuesImpl(typedArray) { + return crypto.getRandomValues(typedArray); + } + function randomUUID() { + return crypto.randomUUID(); + } + + // output/Crypto.Random/index.js + var getRandomValues = function(dictTypedArray) { + return function(ta) { + return function() { + return getRandomValuesImpl(ta); }; + }; }; - exports["MonadThrow"] = MonadThrow; - exports["MonadError"] = MonadError; - exports["try"] = $$try; -})(PS); -(function($PS) { - // Generated by purs version 0.13.6 - "use strict"; - $PS["Crypto.Subtle.Constants.AES"] = $PS["Crypto.Subtle.Constants.AES"] || {}; - var exports = $PS["Crypto.Subtle.Constants.AES"]; + + // output/Crypto.Subtle.Constants.AES/index.js var l256 = 256; var l192 = 192; - var l128 = 128; + var l128 = 128; var aesKW = "AES-KW"; var aesGCM = "AES-GCM"; var aesCTR = "AES-CTR"; var aesCBC = "AES-CBC"; - exports["aesCTR"] = aesCTR; - exports["aesCBC"] = aesCBC; - exports["aesGCM"] = aesGCM; - exports["aesKW"] = aesKW; - exports["l128"] = l128; - exports["l192"] = l192; - exports["l256"] = l256; -})(PS); -(function($PS) { - // Generated by purs version 0.13.6 - "use strict"; - $PS["Crypto.Subtle.Constants.EC"] = $PS["Crypto.Subtle.Constants.EC"] || {}; - var exports = $PS["Crypto.Subtle.Constants.EC"]; + + // output/Crypto.Subtle.Constants.EC/index.js var p521 = "P-521"; var p384 = "P-384"; - var p256 = "P-256"; + var p256 = "P-256"; var ecdsa = "ECDSA"; var ecdh = "ECDH"; - exports["ecdsa"] = ecdsa; - exports["ecdh"] = ecdh; - exports["p256"] = p256; - exports["p384"] = p384; - exports["p521"] = p521; -})(PS); -(function($PS) { - // Generated by purs version 0.13.6 - "use strict"; - $PS["Crypto.Subtle.Constants.RSA"] = $PS["Crypto.Subtle.Constants.RSA"] || {}; - var exports = $PS["Crypto.Subtle.Constants.RSA"]; + + // output/Crypto.Subtle.Constants.RSA/index.js var rsaPSS = "RSA-PSS"; var rsaPKCS1 = "RSASSA-PKCS1-v1_5"; var rsaOAEP = "RSA-OAEP"; - exports["rsaPKCS1"] = rsaPKCS1; - exports["rsaPSS"] = rsaPSS; - exports["rsaOAEP"] = rsaOAEP; -})(PS); -(function($PS) { - // Generated by purs version 0.13.6 - "use strict"; - $PS["Crypto.Subtle.Hash"] = $PS["Crypto.Subtle.Hash"] || {}; - var exports = $PS["Crypto.Subtle.Hash"]; - var sha512 = "SHA-512"; - var sha384 = "SHA-384"; - var sha256 = "SHA-256"; - var sha1 = "SHA-1"; - exports["sha1"] = sha1; - exports["sha256"] = sha256; - exports["sha384"] = sha384; - exports["sha512"] = sha512; -})(PS); -(function(exports) { - "use strict"; - - exports.generateKeyImpl = function generateKeyImpl (a,e,u) { - return crypto.subtle.generateKey(a,e,u); - }; - exports.exp65537 = new Uint8Array([0x01,0x00,0x01]); -})(PS["Crypto.Subtle.Key.Generate"] = PS["Crypto.Subtle.Key.Generate"] || {}); -(function(exports) { - "use strict"; - - exports.pureE = function (a) { - return function () { - return a; + + // output/Control.Promise/foreign.js + function thenImpl(promise2) { + return function(errCB) { + return function(succCB) { + return function() { + promise2.then(succCB, errCB); + }; + }; + }; + } + + // output/Data.Semigroup/index.js + var append = function(dict) { + return dict.append; + }; + + // output/Data.Bounded/foreign.js + var topChar = String.fromCharCode(65535); + var bottomChar = String.fromCharCode(0); + var topNumber = Number.POSITIVE_INFINITY; + var bottomNumber = Number.NEGATIVE_INFINITY; + + // output/Data.Either/index.js + var Left = /* @__PURE__ */ function() { + function Left2(value0) { + this.value0 = value0; + } + ; + Left2.create = function(value0) { + return new Left2(value0); + }; + return Left2; + }(); + var Right = /* @__PURE__ */ function() { + function Right2(value0) { + this.value0 = value0; + } + ; + Right2.create = function(value0) { + return new Right2(value0); + }; + return Right2; + }(); + var functorEither = { + map: function(f) { + return function(m) { + if (m instanceof Left) { + return new Left(m.value0); + } + ; + if (m instanceof Right) { + return new Right(f(m.value0)); + } + ; + throw new Error("Failed pattern match at Data.Either (line 0, column 0 - line 0, column 0): " + [m.constructor.name]); + }; + } + }; + var either = function(v) { + return function(v1) { + return function(v2) { + if (v2 instanceof Left) { + return v(v2.value0); + } + ; + if (v2 instanceof Right) { + return v1(v2.value0); + } + ; + throw new Error("Failed pattern match at Data.Either (line 208, column 1 - line 208, column 64): " + [v.constructor.name, v1.constructor.name, v2.constructor.name]); + }; }; }; - exports.bindE = function (a) { - return function (f) { - return function () { + // output/Effect/foreign.js + var pureE = function(a) { + return function() { + return a; + }; + }; + var bindE = function(a) { + return function(f) { + return function() { return f(a())(); }; }; }; -})(PS["Effect"] = PS["Effect"] || {}); -(function($PS) { - // Generated by purs version 0.13.6 - "use strict"; - $PS["Effect"] = $PS["Effect"] || {}; - var exports = $PS["Effect"]; - var $foreign = $PS["Effect"]; - var Control_Applicative = $PS["Control.Applicative"]; - var Control_Apply = $PS["Control.Apply"]; - var Control_Bind = $PS["Control.Bind"]; - var Control_Monad = $PS["Control.Monad"]; - var Data_Functor = $PS["Data.Functor"]; - var monadEffect = new Control_Monad.Monad(function () { + + // output/Control.Monad/index.js + var ap = function(dictMonad) { + var bind4 = bind(dictMonad.Bind1()); + var pure4 = pure(dictMonad.Applicative0()); + return function(f) { + return function(a) { + return bind4(f)(function(f$prime) { + return bind4(a)(function(a$prime) { + return pure4(f$prime(a$prime)); + }); + }); + }; + }; + }; + + // output/Data.Monoid/index.js + var mempty = function(dict) { + return dict.mempty; + }; + + // output/Effect/index.js + var $runtime_lazy = function(name2, moduleName, init2) { + var state2 = 0; + var val; + return function(lineNumber) { + if (state2 === 2) + return val; + if (state2 === 1) + throw new ReferenceError(name2 + " was needed before it finished initializing (module " + moduleName + ", line " + lineNumber + ")", moduleName, lineNumber); + state2 = 1; + val = init2(); + state2 = 2; + return val; + }; + }; + var monadEffect = { + Applicative0: function() { return applicativeEffect; - }, function () { + }, + Bind1: function() { return bindEffect; + } + }; + var bindEffect = { + bind: bindE, + Apply0: function() { + return $lazy_applyEffect(0); + } + }; + var applicativeEffect = { + pure: pureE, + Apply0: function() { + return $lazy_applyEffect(0); + } + }; + var $lazy_functorEffect = /* @__PURE__ */ $runtime_lazy("functorEffect", "Effect", function() { + return { + map: liftA1(applicativeEffect) + }; }); - var bindEffect = new Control_Bind.Bind(function () { - return applyEffect; - }, $foreign.bindE); - var applyEffect = new Control_Apply.Apply(function () { - return functorEffect; - }, Control_Monad.ap(monadEffect)); - var applicativeEffect = new Control_Applicative.Applicative(function () { - return applyEffect; - }, $foreign.pureE); - var functorEffect = new Data_Functor.Functor(Control_Applicative.liftA1(applicativeEffect)); - exports["functorEffect"] = functorEffect; - exports["applicativeEffect"] = applicativeEffect; -})(PS); -(function(exports) { - /* globals setImmediate, clearImmediate, setTimeout, clearTimeout */ - /* jshint -W083, -W098, -W003 */ - "use strict"; - - var Aff = function () { - // A unique value for empty. - var EMPTY = {}; + var $lazy_applyEffect = /* @__PURE__ */ $runtime_lazy("applyEffect", "Effect", function() { + return { + apply: ap(monadEffect), + Functor0: function() { + return $lazy_functorEffect(0); + } + }; + }); + var functorEffect = /* @__PURE__ */ $lazy_functorEffect(20); + + // output/Effect.Exception/foreign.js + function error(msg) { + return new Error(msg); + } + function throwException(e) { + return function() { + throw e; + }; + } - /* - - An awkward approximation. We elide evidence we would otherwise need in PS for - efficiency sake. - - data Aff eff a - = Pure a - | Throw Error - | Catch (Aff eff a) (Error -> Aff eff a) - | Sync (Eff eff a) - | Async ((Either Error a -> Eff eff Unit) -> Eff eff (Canceler eff)) - | forall b. Bind (Aff eff b) (b -> Aff eff a) - | forall b. Bracket (Aff eff b) (BracketConditions eff b) (b -> Aff eff a) - | forall b. Fork Boolean (Aff eff b) ?(Fiber eff b -> a) - | Sequential (ParAff aff a) - - */ - var PURE = "Pure"; - var THROW = "Throw"; - var CATCH = "Catch"; - var SYNC = "Sync"; - var ASYNC = "Async"; - var BIND = "Bind"; - var BRACKET = "Bracket"; - var FORK = "Fork"; - var SEQ = "Sequential"; + // output/Control.Monad.Error.Class/index.js + var throwError = function(dict) { + return dict.throwError; + }; + var catchError = function(dict) { + return dict.catchError; + }; + var $$try = function(dictMonadError) { + var catchError1 = catchError(dictMonadError); + var Monad0 = dictMonadError.MonadThrow0().Monad0(); + var map3 = map(Monad0.Bind1().Apply0().Functor0()); + var pure4 = pure(Monad0.Applicative0()); + return function(a) { + return catchError1(map3(Right.create)(a))(function($52) { + return pure4(Left.create($52)); + }); + }; + }; + + // output/Data.Identity/index.js + var Identity = function(x) { + return x; + }; + var functorIdentity = { + map: function(f) { + return function(m) { + return f(m); + }; + } + }; + var applyIdentity = { + apply: function(v) { + return function(v1) { + return v(v1); + }; + }, + Functor0: function() { + return functorIdentity; + } + }; + var bindIdentity = { + bind: function(v) { + return function(f) { + return f(v); + }; + }, + Apply0: function() { + return applyIdentity; + } + }; + var applicativeIdentity = { + pure: Identity, + Apply0: function() { + return applyIdentity; + } + }; + var monadIdentity = { + Applicative0: function() { + return applicativeIdentity; + }, + Bind1: function() { + return bindIdentity; + } + }; + + // output/Effect.Class/index.js + var liftEffect = function(dict) { + return dict.liftEffect; + }; + + // output/Control.Monad.Except.Trans/index.js + var map2 = /* @__PURE__ */ map(functorEither); + var ExceptT = function(x) { + return x; + }; + var runExceptT = function(v) { + return v; + }; + var mapExceptT = function(f) { + return function(v) { + return f(v); + }; + }; + var functorExceptT = function(dictFunctor) { + var map1 = map(dictFunctor); + return { + map: function(f) { + return mapExceptT(map1(map2(f))); + } + }; + }; + var monadExceptT = function(dictMonad) { + return { + Applicative0: function() { + return applicativeExceptT(dictMonad); + }, + Bind1: function() { + return bindExceptT(dictMonad); + } + }; + }; + var bindExceptT = function(dictMonad) { + var bind4 = bind(dictMonad.Bind1()); + var pure4 = pure(dictMonad.Applicative0()); + return { + bind: function(v) { + return function(k) { + return bind4(v)(either(function($187) { + return pure4(Left.create($187)); + })(function(a) { + var v1 = k(a); + return v1; + })); + }; + }, + Apply0: function() { + return applyExceptT(dictMonad); + } + }; + }; + var applyExceptT = function(dictMonad) { + var functorExceptT1 = functorExceptT(dictMonad.Bind1().Apply0().Functor0()); + return { + apply: ap(monadExceptT(dictMonad)), + Functor0: function() { + return functorExceptT1; + } + }; + }; + var applicativeExceptT = function(dictMonad) { + return { + pure: function() { + var $188 = pure(dictMonad.Applicative0()); + return function($189) { + return ExceptT($188(Right.create($189))); + }; + }(), + Apply0: function() { + return applyExceptT(dictMonad); + } + }; + }; + var monadThrowExceptT = function(dictMonad) { + var monadExceptT1 = monadExceptT(dictMonad); + return { + throwError: function() { + var $198 = pure(dictMonad.Applicative0()); + return function($199) { + return ExceptT($198(Left.create($199))); + }; + }(), + Monad0: function() { + return monadExceptT1; + } + }; + }; - /* + // output/Unsafe.Coerce/foreign.js + var unsafeCoerce2 = function(x) { + return x; + }; + + // output/Safe.Coerce/index.js + var coerce = function() { + return unsafeCoerce2; + }; + + // output/Data.Newtype/index.js + var coerce2 = /* @__PURE__ */ coerce(); + var unwrap = function() { + return coerce2; + }; - data ParAff eff a - = forall b. Map (b -> a) (ParAff eff b) - | forall b. Apply (ParAff eff (b -> a)) (ParAff eff b) - | Alt (ParAff eff a) (ParAff eff a) - | ?Par (Aff eff a) + // output/Control.Monad.Except/index.js + var unwrap2 = /* @__PURE__ */ unwrap(); + var runExcept = function($3) { + return unwrap2(runExceptT($3)); + }; - */ - var MAP = "Map"; + // output/Data.Foldable/foreign.js + var foldrArray = function(f) { + return function(init2) { + return function(xs) { + var acc = init2; + var len = xs.length; + for (var i = len - 1; i >= 0; i--) { + acc = f(xs[i])(acc); + } + return acc; + }; + }; + }; + var foldlArray = function(f) { + return function(init2) { + return function(xs) { + var acc = init2; + var len = xs.length; + for (var i = 0; i < len; i++) { + acc = f(acc)(xs[i]); + } + return acc; + }; + }; + }; + + // output/Control.Plus/index.js + var empty = function(dict) { + return dict.empty; + }; + + // output/Data.Foldable/index.js + var foldr = function(dict) { + return dict.foldr; + }; + var traverse_ = function(dictApplicative) { + var applySecond2 = applySecond(dictApplicative.Apply0()); + var pure4 = pure(dictApplicative); + return function(dictFoldable) { + var foldr22 = foldr(dictFoldable); + return function(f) { + return foldr22(function($454) { + return applySecond2(f($454)); + })(pure4(unit)); + }; + }; + }; + var foldl = function(dict) { + return dict.foldl; + }; + var foldMapDefaultR = function(dictFoldable) { + var foldr22 = foldr(dictFoldable); + return function(dictMonoid) { + var append2 = append(dictMonoid.Semigroup0()); + var mempty3 = mempty(dictMonoid); + return function(f) { + return foldr22(function(x) { + return function(acc) { + return append2(f(x))(acc); + }; + })(mempty3); + }; + }; + }; + var foldableArray = { + foldr: foldrArray, + foldl: foldlArray, + foldMap: function(dictMonoid) { + return foldMapDefaultR(foldableArray)(dictMonoid); + } + }; + + // output/Data.Traversable/foreign.js + var traverseArrayImpl = function() { + function array1(a) { + return [a]; + } + function array2(a) { + return function(b) { + return [a, b]; + }; + } + function array3(a) { + return function(b) { + return function(c) { + return [a, b, c]; + }; + }; + } + function concat2(xs) { + return function(ys) { + return xs.concat(ys); + }; + } + return function(apply2) { + return function(map3) { + return function(pure4) { + return function(f) { + return function(array) { + function go(bot, top2) { + switch (top2 - bot) { + case 0: + return pure4([]); + case 1: + return map3(array1)(f(array[bot])); + case 2: + return apply2(map3(array2)(f(array[bot])))(f(array[bot + 1])); + case 3: + return apply2(apply2(map3(array3)(f(array[bot])))(f(array[bot + 1])))(f(array[bot + 2])); + default: + var pivot = bot + Math.floor((top2 - bot) / 4) * 2; + return apply2(map3(concat2)(go(bot, pivot)))(go(pivot, top2)); + } + } + return go(0, array.length); + }; + }; + }; + }; + }; + }(); + + // output/Data.NonEmpty/index.js + var NonEmpty = /* @__PURE__ */ function() { + function NonEmpty2(value0, value1) { + this.value0 = value0; + this.value1 = value1; + } + ; + NonEmpty2.create = function(value0) { + return function(value1) { + return new NonEmpty2(value0, value1); + }; + }; + return NonEmpty2; + }(); + var singleton2 = function(dictPlus) { + var empty4 = empty(dictPlus); + return function(a) { + return new NonEmpty(a, empty4); + }; + }; + + // output/Data.List.Types/index.js + var Nil = /* @__PURE__ */ function() { + function Nil2() { + } + ; + Nil2.value = new Nil2(); + return Nil2; + }(); + var Cons = /* @__PURE__ */ function() { + function Cons2(value0, value1) { + this.value0 = value0; + this.value1 = value1; + } + ; + Cons2.create = function(value0) { + return function(value1) { + return new Cons2(value0, value1); + }; + }; + return Cons2; + }(); + var NonEmptyList = function(x) { + return x; + }; + var listMap = function(f) { + var chunkedRevMap = function($copy_v) { + return function($copy_v1) { + var $tco_var_v = $copy_v; + var $tco_done = false; + var $tco_result; + function $tco_loop(v, v1) { + if (v1 instanceof Cons && (v1.value1 instanceof Cons && v1.value1.value1 instanceof Cons)) { + $tco_var_v = new Cons(v1, v); + $copy_v1 = v1.value1.value1.value1; + return; + } + ; + var unrolledMap = function(v2) { + if (v2 instanceof Cons && (v2.value1 instanceof Cons && v2.value1.value1 instanceof Nil)) { + return new Cons(f(v2.value0), new Cons(f(v2.value1.value0), Nil.value)); + } + ; + if (v2 instanceof Cons && v2.value1 instanceof Nil) { + return new Cons(f(v2.value0), Nil.value); + } + ; + return Nil.value; + }; + var reverseUnrolledMap = function($copy_v2) { + return function($copy_v3) { + var $tco_var_v2 = $copy_v2; + var $tco_done1 = false; + var $tco_result2; + function $tco_loop2(v2, v3) { + if (v2 instanceof Cons && (v2.value0 instanceof Cons && (v2.value0.value1 instanceof Cons && v2.value0.value1.value1 instanceof Cons))) { + $tco_var_v2 = v2.value1; + $copy_v3 = new Cons(f(v2.value0.value0), new Cons(f(v2.value0.value1.value0), new Cons(f(v2.value0.value1.value1.value0), v3))); + return; + } + ; + $tco_done1 = true; + return v3; + } + ; + while (!$tco_done1) { + $tco_result2 = $tco_loop2($tco_var_v2, $copy_v3); + } + ; + return $tco_result2; + }; + }; + $tco_done = true; + return reverseUnrolledMap(v)(unrolledMap(v1)); + } + ; + while (!$tco_done) { + $tco_result = $tco_loop($tco_var_v, $copy_v1); + } + ; + return $tco_result; + }; + }; + return chunkedRevMap(Nil.value); + }; + var functorList = { + map: listMap + }; + var foldableList = { + foldr: function(f) { + return function(b) { + var rev = function() { + var go = function($copy_v) { + return function($copy_v1) { + var $tco_var_v = $copy_v; + var $tco_done = false; + var $tco_result; + function $tco_loop(v, v1) { + if (v1 instanceof Nil) { + $tco_done = true; + return v; + } + ; + if (v1 instanceof Cons) { + $tco_var_v = new Cons(v1.value0, v); + $copy_v1 = v1.value1; + return; + } + ; + throw new Error("Failed pattern match at Data.List.Types (line 107, column 7 - line 107, column 23): " + [v.constructor.name, v1.constructor.name]); + } + ; + while (!$tco_done) { + $tco_result = $tco_loop($tco_var_v, $copy_v1); + } + ; + return $tco_result; + }; + }; + return go(Nil.value); + }(); + var $284 = foldl(foldableList)(flip(f))(b); + return function($285) { + return $284(rev($285)); + }; + }; + }, + foldl: function(f) { + var go = function($copy_b) { + return function($copy_v) { + var $tco_var_b = $copy_b; + var $tco_done1 = false; + var $tco_result; + function $tco_loop(b, v) { + if (v instanceof Nil) { + $tco_done1 = true; + return b; + } + ; + if (v instanceof Cons) { + $tco_var_b = f(b)(v.value0); + $copy_v = v.value1; + return; + } + ; + throw new Error("Failed pattern match at Data.List.Types (line 111, column 12 - line 113, column 30): " + [v.constructor.name]); + } + ; + while (!$tco_done1) { + $tco_result = $tco_loop($tco_var_b, $copy_v); + } + ; + return $tco_result; + }; + }; + return go; + }, + foldMap: function(dictMonoid) { + var append2 = append(dictMonoid.Semigroup0()); + var mempty3 = mempty(dictMonoid); + return function(f) { + return foldl(foldableList)(function(acc) { + var $286 = append2(acc); + return function($287) { + return $286(f($287)); + }; + })(mempty3); + }; + } + }; + var foldr2 = /* @__PURE__ */ foldr(foldableList); + var semigroupList = { + append: function(xs) { + return function(ys) { + return foldr2(Cons.create)(ys)(xs); + }; + } + }; + var append1 = /* @__PURE__ */ append(semigroupList); + var altList = { + alt: append1, + Functor0: function() { + return functorList; + } + }; + var plusList = /* @__PURE__ */ function() { + return { + empty: Nil.value, + Alt0: function() { + return altList; + } + }; + }(); + + // output/Effect.Aff/foreign.js + var Aff = function() { + var EMPTY = {}; + var PURE = "Pure"; + var THROW = "Throw"; + var CATCH = "Catch"; + var SYNC = "Sync"; + var ASYNC = "Async"; + var BIND = "Bind"; + var BRACKET = "Bracket"; + var FORK = "Fork"; + var SEQ = "Sequential"; + var MAP = "Map"; var APPLY = "Apply"; - var ALT = "Alt"; - - // Various constructors used in interpretation - var CONS = "Cons"; // Cons-list, for stacks - var RESUME = "Resume"; // Continue indiscriminately - var RELEASE = "Release"; // Continue with bracket finalizers - var FINALIZER = "Finalizer"; // A non-interruptible effect - var FINALIZED = "Finalized"; // Marker for finalization - var FORKED = "Forked"; // Reference to a forked fiber, with resumption stack - var FIBER = "Fiber"; // Actual fiber reference - var THUNK = "Thunk"; // Primed effect, ready to invoke - - function Aff(tag, _1, _2, _3) { + var ALT = "Alt"; + var CONS = "Cons"; + var RESUME = "Resume"; + var RELEASE = "Release"; + var FINALIZER = "Finalizer"; + var FINALIZED = "Finalized"; + var FORKED = "Forked"; + var FIBER = "Fiber"; + var THUNK = "Thunk"; + function Aff2(tag, _1, _2, _3) { this.tag = tag; - this._1 = _1; - this._2 = _2; - this._3 = _3; + this._1 = _1; + this._2 = _2; + this._3 = _3; } - function AffCtr(tag) { - var fn = function (_1, _2, _3) { - return new Aff(tag, _1, _2, _3); + var fn = function(_1, _2, _3) { + return new Aff2(tag, _1, _2, _3); }; fn.tag = tag; return fn; } - - function nonCanceler(error) { - return new Aff(PURE, void 0); + function nonCanceler2(error3) { + return new Aff2(PURE, void 0); } - function runEff(eff) { try { eff(); - } catch (error) { - setTimeout(function () { - throw error; + } catch (error3) { + setTimeout(function() { + throw error3; }, 0); } } - function runSync(left, right, eff) { try { return right(eff()); - } catch (error) { - return left(error); + } catch (error3) { + return left(error3); } } - function runAsync(left, eff, k) { try { return eff(k)(); - } catch (error) { - k(left(error))(); - return nonCanceler; + } catch (error3) { + k(left(error3))(); + return nonCanceler2; } } - - var Scheduler = function () { - var limit = 1024; - var size = 0; - var ix = 0; - var queue = new Array(limit); + var Scheduler = function() { + var limit = 1024; + var size = 0; + var ix = 0; + var queue = new Array(limit); var draining = false; - function drain() { var thunk; draining = true; while (size !== 0) { size--; - thunk = queue[ix]; + thunk = queue[ix]; queue[ix] = void 0; - ix = (ix + 1) % limit; + ix = (ix + 1) % limit; thunk(); } draining = false; } - return { - isDraining: function () { + isDraining: function() { return draining; }, - enqueue: function (cb) { + enqueue: function(cb) { var i, tmp; if (size === limit) { tmp = draining; drain(); draining = tmp; } - queue[(ix + size) % limit] = cb; size++; - if (!draining) { drain(); } } }; }(); - function Supervisor(util) { - var fibers = {}; + var fibers = {}; var fiberId = 0; - var count = 0; - + var count = 0; return { - register: function (fiber) { + register: function(fiber) { var fid = fiberId++; fiber.onComplete({ rethrow: true, - handler: function (result) { - return function () { + handler: function(result) { + return function() { count--; delete fibers[fid]; }; @@ -496,25 +942,23 @@ var PS = {}; fibers[fid] = fiber; count++; }, - isEmpty: function () { + isEmpty: function() { return count === 0; }, - killAll: function (killError, cb) { - return function () { + killAll: function(killError, cb) { + return function() { if (count === 0) { return cb(); } - var killCount = 0; - var kills = {}; - + var kills = {}; function kill(fid) { - kills[fid] = fibers[fid].kill(killError, function (result) { - return function () { + kills[fid] = fibers[fid].kill(killError, function(result) { + return function() { delete kills[fid]; killCount--; if (util.isLeft(result) && util.fromLeft(result)) { - setTimeout(function () { + setTimeout(function() { throw util.fromLeft(result); }, 0); } @@ -524,23 +968,20 @@ var PS = {}; }; })(); } - for (var k in fibers) { if (fibers.hasOwnProperty(k)) { killCount++; kill(k); } } - - fibers = {}; + fibers = {}; fiberId = 0; - count = 0; - - return function (error) { - return new Aff(SYNC, function () { - for (var k in kills) { - if (kills.hasOwnProperty(k)) { - kills[k](); + count = 0; + return function(error3) { + return new Aff2(SYNC, function() { + for (var k2 in kills) { + if (kills.hasOwnProperty(k2)) { + kills[k2](); } } }); @@ -549,343 +990,256 @@ var PS = {}; } }; } - - // Fiber state machine - var SUSPENDED = 0; // Suspended, pending a join. - var CONTINUE = 1; // Interpret the next instruction. - var STEP_BIND = 2; // Apply the next bind. - var STEP_RESULT = 3; // Handle potential failure from a result. - var PENDING = 4; // An async effect is running. - var RETURN = 5; // The current stack has returned. - var COMPLETED = 6; // The entire fiber has completed. - + var SUSPENDED = 0; + var CONTINUE = 1; + var STEP_BIND = 2; + var STEP_RESULT = 3; + var PENDING = 4; + var RETURN = 5; + var COMPLETED = 6; function Fiber(util, supervisor, aff) { - // Monotonically increasing tick, increased on each asynchronous turn. var runTick = 0; - - // The current branch of the state machine. var status = SUSPENDED; - - // The current point of interest for the state machine branch. - var step = aff; // Successful step - var fail = null; // Failure step - var interrupt = null; // Asynchronous interrupt - - // Stack of continuations for the current fiber. + var step = aff; + var fail2 = null; + var interrupt = null; var bhead = null; var btail = null; - - // Stack of attempts and finalizers for error recovery. Every `Cons` is also - // tagged with current `interrupt` state. We use this to track which items - // should be ignored or evaluated as a result of a kill. var attempts = null; - - // A special state is needed for Bracket, because it cannot be killed. When - // we enter a bracket acquisition or finalizer, we increment the counter, - // and then decrement once complete. var bracketCount = 0; - - // Each join gets a new id so they can be revoked. - var joinId = 0; - var joins = null; + var joinId = 0; + var joins = null; var rethrow = true; - - // Each invocation of `run` requires a tick. When an asynchronous effect is - // resolved, we must check that the local tick coincides with the fiber - // tick before resuming. This prevents multiple async continuations from - // accidentally resuming the same fiber. A common example may be invoking - // the provided callback in `makeAff` more than once, but it may also be an - // async effect resuming after the fiber was already cancelled. - function run(localRunTick) { + function run3(localRunTick) { var tmp, result, attempt; while (true) { - tmp = null; - result = null; - attempt = null; - + tmp = null; + result = null; + attempt = null; switch (status) { - case STEP_BIND: - status = CONTINUE; - try { - step = bhead(step); - if (btail === null) { - bhead = null; - } else { - bhead = btail._1; - btail = btail._2; - } - } catch (e) { - status = RETURN; - fail = util.left(e); - step = null; - } - break; - - case STEP_RESULT: - if (util.isLeft(step)) { - status = RETURN; - fail = step; - step = null; - } else if (bhead === null) { - status = RETURN; - } else { - status = STEP_BIND; - step = util.fromRight(step); - } - break; - - case CONTINUE: - switch (step.tag) { - case BIND: - if (bhead) { - btail = new Aff(CONS, bhead, btail); - } - bhead = step._2; + case STEP_BIND: status = CONTINUE; - step = step._1; - break; - - case PURE: - if (bhead === null) { + try { + step = bhead(step); + if (btail === null) { + bhead = null; + } else { + bhead = btail._1; + btail = btail._2; + } + } catch (e) { status = RETURN; - step = util.right(step._1); - } else { - status = STEP_BIND; - step = step._1; - } - break; - - case SYNC: - status = STEP_RESULT; - step = runSync(util.left, util.right, step._1); - break; - - case ASYNC: - status = PENDING; - step = runAsync(util.left, step._1, function (result) { - return function () { - if (runTick !== localRunTick) { - return; - } - runTick++; - Scheduler.enqueue(function () { - // It's possible to interrupt the fiber between enqueuing and - // resuming, so we need to check that the runTick is still - // valid. - if (runTick !== localRunTick + 1) { - return; - } - status = STEP_RESULT; - step = result; - run(runTick); - }); - }; - }); - return; - - case THROW: - status = RETURN; - fail = util.left(step._1); - step = null; - break; - - // Enqueue the Catch so that we can call the error handler later on - // in case of an exception. - case CATCH: - if (bhead === null) { - attempts = new Aff(CONS, step, attempts, interrupt); - } else { - attempts = new Aff(CONS, step, new Aff(CONS, new Aff(RESUME, bhead, btail), attempts, interrupt), interrupt); - } - bhead = null; - btail = null; - status = CONTINUE; - step = step._1; - break; - - // Enqueue the Bracket so that we can call the appropriate handlers - // after resource acquisition. - case BRACKET: - bracketCount++; - if (bhead === null) { - attempts = new Aff(CONS, step, attempts, interrupt); - } else { - attempts = new Aff(CONS, step, new Aff(CONS, new Aff(RESUME, bhead, btail), attempts, interrupt), interrupt); + fail2 = util.left(e); + step = null; } - bhead = null; - btail = null; - status = CONTINUE; - step = step._1; break; - - case FORK: - status = STEP_RESULT; - tmp = Fiber(util, supervisor, step._2); - if (supervisor) { - supervisor.register(tmp); - } - if (step._1) { - tmp.run(); + case STEP_RESULT: + if (util.isLeft(step)) { + status = RETURN; + fail2 = step; + step = null; + } else if (bhead === null) { + status = RETURN; + } else { + status = STEP_BIND; + step = util.fromRight(step); } - step = util.right(tmp); - break; - - case SEQ: - status = CONTINUE; - step = sequential(util, supervisor, step._1); break; - } - break; - - case RETURN: - bhead = null; - btail = null; - // If the current stack has returned, and we have no other stacks to - // resume or finalizers to run, the fiber has halted and we can - // invoke all join callbacks. Otherwise we need to resume. - if (attempts === null) { - status = COMPLETED; - step = interrupt || fail || step; - } else { - // The interrupt status for the enqueued item. - tmp = attempts._3; - attempt = attempts._1; - attempts = attempts._2; - - switch (attempt.tag) { - // We cannot recover from an unmasked interrupt. Otherwise we should - // continue stepping, or run the exception handler if an exception - // was raised. - case CATCH: - // We should compare the interrupt status as well because we - // only want it to apply if there has been an interrupt since - // enqueuing the catch. - if (interrupt && interrupt !== tmp && bracketCount === 0) { - status = RETURN; - } else if (fail) { + case CONTINUE: + switch (step.tag) { + case BIND: + if (bhead) { + btail = new Aff2(CONS, bhead, btail); + } + bhead = step._2; status = CONTINUE; - step = attempt._2(util.fromLeft(fail)); - fail = null; - } - break; - - // We cannot resume from an unmasked interrupt or exception. - case RESUME: - // As with Catch, we only want to ignore in the case of an - // interrupt since enqueing the item. - if (interrupt && interrupt !== tmp && bracketCount === 0 || fail) { + step = step._1; + break; + case PURE: + if (bhead === null) { + status = RETURN; + step = util.right(step._1); + } else { + status = STEP_BIND; + step = step._1; + } + break; + case SYNC: + status = STEP_RESULT; + step = runSync(util.left, util.right, step._1); + break; + case ASYNC: + status = PENDING; + step = runAsync(util.left, step._1, function(result2) { + return function() { + if (runTick !== localRunTick) { + return; + } + runTick++; + Scheduler.enqueue(function() { + if (runTick !== localRunTick + 1) { + return; + } + status = STEP_RESULT; + step = result2; + run3(runTick); + }); + }; + }); + return; + case THROW: status = RETURN; - } else { - bhead = attempt._1; - btail = attempt._2; - status = STEP_BIND; - step = util.fromRight(step); - } - break; - - // If we have a bracket, we should enqueue the handlers, - // and continue with the success branch only if the fiber has - // not been interrupted. If the bracket acquisition failed, we - // should not run either. - case BRACKET: - bracketCount--; - if (fail === null) { - result = util.fromRight(step); - // We need to enqueue the Release with the same interrupt - // status as the Bracket that is initiating it. - attempts = new Aff(CONS, new Aff(RELEASE, attempt._2, result), attempts, tmp); - // We should only coninue as long as the interrupt status has not changed or - // we are currently within a non-interruptable finalizer. - if (interrupt === tmp || bracketCount > 0) { - status = CONTINUE; - step = attempt._3(result); + fail2 = util.left(step._1); + step = null; + break; + case CATCH: + if (bhead === null) { + attempts = new Aff2(CONS, step, attempts, interrupt); + } else { + attempts = new Aff2(CONS, step, new Aff2(CONS, new Aff2(RESUME, bhead, btail), attempts, interrupt), interrupt); + } + bhead = null; + btail = null; + status = CONTINUE; + step = step._1; + break; + case BRACKET: + bracketCount++; + if (bhead === null) { + attempts = new Aff2(CONS, step, attempts, interrupt); + } else { + attempts = new Aff2(CONS, step, new Aff2(CONS, new Aff2(RESUME, bhead, btail), attempts, interrupt), interrupt); + } + bhead = null; + btail = null; + status = CONTINUE; + step = step._1; + break; + case FORK: + status = STEP_RESULT; + tmp = Fiber(util, supervisor, step._2); + if (supervisor) { + supervisor.register(tmp); + } + if (step._1) { + tmp.run(); } + step = util.right(tmp); + break; + case SEQ: + status = CONTINUE; + step = sequential2(util, supervisor, step._1); + break; + } + break; + case RETURN: + bhead = null; + btail = null; + if (attempts === null) { + status = COMPLETED; + step = interrupt || fail2 || step; + } else { + tmp = attempts._3; + attempt = attempts._1; + attempts = attempts._2; + switch (attempt.tag) { + case CATCH: + if (interrupt && interrupt !== tmp && bracketCount === 0) { + status = RETURN; + } else if (fail2) { + status = CONTINUE; + step = attempt._2(util.fromLeft(fail2)); + fail2 = null; + } + break; + case RESUME: + if (interrupt && interrupt !== tmp && bracketCount === 0 || fail2) { + status = RETURN; + } else { + bhead = attempt._1; + btail = attempt._2; + status = STEP_BIND; + step = util.fromRight(step); + } + break; + case BRACKET: + bracketCount--; + if (fail2 === null) { + result = util.fromRight(step); + attempts = new Aff2(CONS, new Aff2(RELEASE, attempt._2, result), attempts, tmp); + if (interrupt === tmp || bracketCount > 0) { + status = CONTINUE; + step = attempt._3(result); + } + } + break; + case RELEASE: + attempts = new Aff2(CONS, new Aff2(FINALIZED, step, fail2), attempts, interrupt); + status = CONTINUE; + if (interrupt && interrupt !== tmp && bracketCount === 0) { + step = attempt._1.killed(util.fromLeft(interrupt))(attempt._2); + } else if (fail2) { + step = attempt._1.failed(util.fromLeft(fail2))(attempt._2); + } else { + step = attempt._1.completed(util.fromRight(step))(attempt._2); + } + fail2 = null; + bracketCount++; + break; + case FINALIZER: + bracketCount++; + attempts = new Aff2(CONS, new Aff2(FINALIZED, step, fail2), attempts, interrupt); + status = CONTINUE; + step = attempt._1; + break; + case FINALIZED: + bracketCount--; + status = RETURN; + step = attempt._1; + fail2 = attempt._2; + break; } - break; - - // Enqueue the appropriate handler. We increase the bracket count - // because it should not be cancelled. - case RELEASE: - attempts = new Aff(CONS, new Aff(FINALIZED, step, fail), attempts, interrupt); - status = CONTINUE; - // It has only been killed if the interrupt status has changed - // since we enqueued the item, and the bracket count is 0. If the - // bracket count is non-zero then we are in a masked state so it's - // impossible to be killed. - if (interrupt && interrupt !== tmp && bracketCount === 0) { - step = attempt._1.killed(util.fromLeft(interrupt))(attempt._2); - } else if (fail) { - step = attempt._1.failed(util.fromLeft(fail))(attempt._2); - } else { - step = attempt._1.completed(util.fromRight(step))(attempt._2); + } + break; + case COMPLETED: + for (var k in joins) { + if (joins.hasOwnProperty(k)) { + rethrow = rethrow && joins[k].rethrow; + runEff(joins[k].handler(step)); } - fail = null; - bracketCount++; - break; - - case FINALIZER: - bracketCount++; - attempts = new Aff(CONS, new Aff(FINALIZED, step, fail), attempts, interrupt); - status = CONTINUE; - step = attempt._1; - break; - - case FINALIZED: - bracketCount--; - status = RETURN; - step = attempt._1; - fail = attempt._2; - break; } - } - break; - - case COMPLETED: - for (var k in joins) { - if (joins.hasOwnProperty(k)) { - rethrow = rethrow && joins[k].rethrow; - runEff(joins[k].handler(step)); + joins = null; + if (interrupt && fail2) { + setTimeout(function() { + throw util.fromLeft(fail2); + }, 0); + } else if (util.isLeft(step) && rethrow) { + setTimeout(function() { + if (rethrow) { + throw util.fromLeft(step); + } + }, 0); } - } - joins = null; - // If we have an interrupt and a fail, then the thread threw while - // running finalizers. This should always rethrow in a fresh stack. - if (interrupt && fail) { - setTimeout(function () { - throw util.fromLeft(fail); - }, 0); - // If we have an unhandled exception, and no other fiber has joined - // then we need to throw the exception in a fresh stack. - } else if (util.isLeft(step) && rethrow) { - setTimeout(function () { - // Guard on reathrow because a completely synchronous fiber can - // still have an observer which was added after-the-fact. - if (rethrow) { - throw util.fromLeft(step); - } - }, 0); - } - return; - case SUSPENDED: - status = CONTINUE; - break; - case PENDING: return; + return; + case SUSPENDED: + status = CONTINUE; + break; + case PENDING: + return; } } } - - function onComplete(join) { - return function () { + function onComplete(join3) { + return function() { if (status === COMPLETED) { - rethrow = rethrow && join.rethrow; - join.handler(step)(); - return function () {}; + rethrow = rethrow && join3.rethrow; + join3.handler(step)(); + return function() { + }; } - - var jid = joinId++; - joins = joins || {}; - joins[jid] = join; - + var jid = joinId++; + joins = joins || {}; + joins[jid] = join3; return function() { if (joins !== null) { delete joins[jid]; @@ -893,415 +1247,343 @@ var PS = {}; }; }; } - - function kill(error, cb) { - return function () { + function kill(error3, cb) { + return function() { if (status === COMPLETED) { cb(util.right(void 0))(); - return function () {}; + return function() { + }; } - var canceler = onComplete({ rethrow: false, - handler: function (/* unused */) { + handler: function() { return cb(util.right(void 0)); } })(); - switch (status) { - case SUSPENDED: - interrupt = util.left(error); - status = COMPLETED; - step = interrupt; - run(runTick); - break; - case PENDING: - if (interrupt === null) { - interrupt = util.left(error); - } - if (bracketCount === 0) { - if (status === PENDING) { - attempts = new Aff(CONS, new Aff(FINALIZER, step(error)), attempts, interrupt); + case SUSPENDED: + interrupt = util.left(error3); + status = COMPLETED; + step = interrupt; + run3(runTick); + break; + case PENDING: + if (interrupt === null) { + interrupt = util.left(error3); + } + if (bracketCount === 0) { + if (status === PENDING) { + attempts = new Aff2(CONS, new Aff2(FINALIZER, step(error3)), attempts, interrupt); + } + status = RETURN; + step = null; + fail2 = null; + run3(++runTick); + } + break; + default: + if (interrupt === null) { + interrupt = util.left(error3); + } + if (bracketCount === 0) { + status = RETURN; + step = null; + fail2 = null; } - status = RETURN; - step = null; - fail = null; - run(++runTick); - } - break; - default: - if (interrupt === null) { - interrupt = util.left(error); - } - if (bracketCount === 0) { - status = RETURN; - step = null; - fail = null; - } } - return canceler; }; } - - function join(cb) { - return function () { + function join2(cb) { + return function() { var canceler = onComplete({ rethrow: false, handler: cb })(); if (status === SUSPENDED) { - run(runTick); + run3(runTick); } return canceler; }; } - return { - kill: kill, - join: join, - onComplete: onComplete, - isSuspended: function () { + kill, + join: join2, + onComplete, + isSuspended: function() { return status === SUSPENDED; }, - run: function () { + run: function() { if (status === SUSPENDED) { if (!Scheduler.isDraining()) { - Scheduler.enqueue(function () { - run(runTick); + Scheduler.enqueue(function() { + run3(runTick); }); } else { - run(runTick); + run3(runTick); } } } }; } - function runPar(util, supervisor, par, cb) { - // Table of all forked fibers. - var fiberId = 0; - var fibers = {}; - - // Table of currently running cancelers, as a product of `Alt` behavior. - var killId = 0; - var kills = {}; - - // Error used for early cancelation on Alt branches. - var early = new Error("[ParAff] Early exit"); - - // Error used to kill the entire tree. + var fiberId = 0; + var fibers = {}; + var killId = 0; + var kills = {}; + var early = new Error("[ParAff] Early exit"); var interrupt = null; - - // The root pointer of the tree. - var root = EMPTY; - - // Walks a tree, invoking all the cancelers. Returns the table of pending - // cancellation fibers. - function kill(error, par, cb) { - var step = par; - var head = null; - var tail = null; + var root = EMPTY; + function kill(error3, par2, cb2) { + var step = par2; + var head = null; + var tail = null; var count = 0; - var kills = {}; + var kills2 = {}; var tmp, kid; - - loop: while (true) { - tmp = null; - - switch (step.tag) { - case FORKED: - if (step._3 === EMPTY) { - tmp = fibers[step._1]; - kills[count++] = tmp.kill(error, function (result) { - return function () { - count--; - if (count === 0) { - cb(result)(); - } - }; - }); - } - // Terminal case. - if (head === null) { - break loop; - } - // Go down the right side of the tree. - step = head._2; - if (tail === null) { - head = null; - } else { - head = tail._1; - tail = tail._2; - } - break; - case MAP: - step = step._2; - break; - case APPLY: - case ALT: - if (head) { - tail = new Aff(CONS, head, tail); + loop: + while (true) { + tmp = null; + switch (step.tag) { + case FORKED: + if (step._3 === EMPTY) { + tmp = fibers[step._1]; + kills2[count++] = tmp.kill(error3, function(result) { + return function() { + count--; + if (count === 0) { + cb2(result)(); + } + }; + }); + } + if (head === null) { + break loop; + } + step = head._2; + if (tail === null) { + head = null; + } else { + head = tail._1; + tail = tail._2; + } + break; + case MAP: + step = step._2; + break; + case APPLY: + case ALT: + if (head) { + tail = new Aff2(CONS, head, tail); + } + head = step; + step = step._1; + break; } - head = step; - step = step._1; - break; } - } - if (count === 0) { - cb(util.right(void 0))(); + cb2(util.right(void 0))(); } else { - // Run the cancelation effects. We alias `count` because it's mutable. kid = 0; tmp = count; for (; kid < tmp; kid++) { - kills[kid] = kills[kid](); + kills2[kid] = kills2[kid](); } } - - return kills; + return kills2; } - - // When a fiber resolves, we need to bubble back up the tree with the - // result, computing the applicative nodes. - function join(result, head, tail) { - var fail, step, lhs, rhs, tmp, kid; - + function join2(result, head, tail) { + var fail2, step, lhs, rhs, tmp, kid; if (util.isLeft(result)) { - fail = result; + fail2 = result; step = null; } else { step = result; - fail = null; + fail2 = null; } - - loop: while (true) { - lhs = null; - rhs = null; - tmp = null; - kid = null; - - // We should never continue if the entire tree has been interrupted. - if (interrupt !== null) { - return; - } - - // We've made it all the way to the root of the tree, which means - // the tree has fully evaluated. - if (head === null) { - cb(fail || step)(); - return; - } - - // The tree has already been computed, so we shouldn't try to do it - // again. This should never happen. - // TODO: Remove this? - if (head._3 !== EMPTY) { - return; - } - - switch (head.tag) { - case MAP: - if (fail === null) { - head._3 = util.right(head._1(util.fromRight(step))); - step = head._3; - } else { - head._3 = fail; + loop: + while (true) { + lhs = null; + rhs = null; + tmp = null; + kid = null; + if (interrupt !== null) { + return; } - break; - case APPLY: - lhs = head._1._3; - rhs = head._2._3; - // If we have a failure we should kill the other side because we - // can't possible yield a result anymore. - if (fail) { - head._3 = fail; - tmp = true; - kid = killId++; - - kills[kid] = kill(early, fail === lhs ? head._2 : head._1, function (/* unused */) { - return function () { - delete kills[kid]; - if (tmp) { - tmp = false; - } else if (tail === null) { - join(fail, null, null); - } else { - join(fail, tail._1, tail._2); - } - }; - }); - - if (tmp) { - tmp = false; - return; - } - } else if (lhs === EMPTY || rhs === EMPTY) { - // We can only proceed if both sides have resolved. + if (head === null) { + cb(fail2 || step)(); return; - } else { - step = util.right(util.fromRight(lhs)(util.fromRight(rhs))); - head._3 = step; } - break; - case ALT: - lhs = head._1._3; - rhs = head._2._3; - // We can only proceed if both have resolved or we have a success - if (lhs === EMPTY && util.isLeft(rhs) || rhs === EMPTY && util.isLeft(lhs)) { + if (head._3 !== EMPTY) { return; } - // If both sides resolve with an error, we should continue with the - // first error - if (lhs !== EMPTY && util.isLeft(lhs) && rhs !== EMPTY && util.isLeft(rhs)) { - fail = step === lhs ? rhs : lhs; - step = null; - head._3 = fail; - } else { - head._3 = step; - tmp = true; - kid = killId++; - // Once a side has resolved, we need to cancel the side that is still - // pending before we can continue. - kills[kid] = kill(early, step === lhs ? head._2 : head._1, function (/* unused */) { - return function () { - delete kills[kid]; + switch (head.tag) { + case MAP: + if (fail2 === null) { + head._3 = util.right(head._1(util.fromRight(step))); + step = head._3; + } else { + head._3 = fail2; + } + break; + case APPLY: + lhs = head._1._3; + rhs = head._2._3; + if (fail2) { + head._3 = fail2; + tmp = true; + kid = killId++; + kills[kid] = kill(early, fail2 === lhs ? head._2 : head._1, function() { + return function() { + delete kills[kid]; + if (tmp) { + tmp = false; + } else if (tail === null) { + join2(fail2, null, null); + } else { + join2(fail2, tail._1, tail._2); + } + }; + }); if (tmp) { tmp = false; - } else if (tail === null) { - join(step, null, null); - } else { - join(step, tail._1, tail._2); + return; } - }; - }); - - if (tmp) { - tmp = false; - return; - } + } else if (lhs === EMPTY || rhs === EMPTY) { + return; + } else { + step = util.right(util.fromRight(lhs)(util.fromRight(rhs))); + head._3 = step; + } + break; + case ALT: + lhs = head._1._3; + rhs = head._2._3; + if (lhs === EMPTY && util.isLeft(rhs) || rhs === EMPTY && util.isLeft(lhs)) { + return; + } + if (lhs !== EMPTY && util.isLeft(lhs) && rhs !== EMPTY && util.isLeft(rhs)) { + fail2 = step === lhs ? rhs : lhs; + step = null; + head._3 = fail2; + } else { + head._3 = step; + tmp = true; + kid = killId++; + kills[kid] = kill(early, step === lhs ? head._2 : head._1, function() { + return function() { + delete kills[kid]; + if (tmp) { + tmp = false; + } else if (tail === null) { + join2(step, null, null); + } else { + join2(step, tail._1, tail._2); + } + }; + }); + if (tmp) { + tmp = false; + return; + } + } + break; + } + if (tail === null) { + head = null; + } else { + head = tail._1; + tail = tail._2; } - break; - } - - if (tail === null) { - head = null; - } else { - head = tail._1; - tail = tail._2; } - } } - function resolve(fiber) { - return function (result) { - return function () { + return function(result) { + return function() { delete fibers[fiber._1]; fiber._3 = result; - join(result, fiber._2._1, fiber._2._2); + join2(result, fiber._2._1, fiber._2._2); }; }; } - - // Walks the applicative tree, substituting non-applicative nodes with - // `FORKED` nodes. In this tree, all applicative nodes use the `_3` slot - // as a mutable slot for memoization. In an unresolved state, the `_3` - // slot is `EMPTY`. In the cases of `ALT` and `APPLY`, we always walk - // the left side first, because both operations are left-associative. As - // we `RETURN` from those branches, we then walk the right side. - function run() { + function run3() { var status = CONTINUE; - var step = par; - var head = null; - var tail = null; + var step = par; + var head = null; + var tail = null; var tmp, fid; - - loop: while (true) { - tmp = null; - fid = null; - - switch (status) { - case CONTINUE: - switch (step.tag) { - case MAP: - if (head) { - tail = new Aff(CONS, head, tail); - } - head = new Aff(MAP, step._1, EMPTY, EMPTY); - step = step._2; - break; - case APPLY: - if (head) { - tail = new Aff(CONS, head, tail); - } - head = new Aff(APPLY, EMPTY, step._2, EMPTY); - step = step._1; - break; - case ALT: - if (head) { - tail = new Aff(CONS, head, tail); - } - head = new Aff(ALT, EMPTY, step._2, EMPTY); - step = step._1; - break; - default: - // When we hit a leaf value, we suspend the stack in the `FORKED`. - // When the fiber resolves, it can bubble back up the tree. - fid = fiberId++; - status = RETURN; - tmp = step; - step = new Aff(FORKED, fid, new Aff(CONS, head, tail), EMPTY); - tmp = Fiber(util, supervisor, tmp); - tmp.onComplete({ - rethrow: false, - handler: resolve(step) - })(); - fibers[fid] = tmp; - if (supervisor) { - supervisor.register(tmp); - } - } - break; - case RETURN: - // Terminal case, we are back at the root. - if (head === null) { - break loop; - } - // If we are done with the right side, we need to continue down the - // left. Otherwise we should continue up the stack. - if (head._1 === EMPTY) { - head._1 = step; - status = CONTINUE; - step = head._2; - head._2 = EMPTY; - } else { - head._2 = step; - step = head; - if (tail === null) { - head = null; - } else { - head = tail._1; - tail = tail._2; - } + loop: + while (true) { + tmp = null; + fid = null; + switch (status) { + case CONTINUE: + switch (step.tag) { + case MAP: + if (head) { + tail = new Aff2(CONS, head, tail); + } + head = new Aff2(MAP, step._1, EMPTY, EMPTY); + step = step._2; + break; + case APPLY: + if (head) { + tail = new Aff2(CONS, head, tail); + } + head = new Aff2(APPLY, EMPTY, step._2, EMPTY); + step = step._1; + break; + case ALT: + if (head) { + tail = new Aff2(CONS, head, tail); + } + head = new Aff2(ALT, EMPTY, step._2, EMPTY); + step = step._1; + break; + default: + fid = fiberId++; + status = RETURN; + tmp = step; + step = new Aff2(FORKED, fid, new Aff2(CONS, head, tail), EMPTY); + tmp = Fiber(util, supervisor, tmp); + tmp.onComplete({ + rethrow: false, + handler: resolve(step) + })(); + fibers[fid] = tmp; + if (supervisor) { + supervisor.register(tmp); + } + } + break; + case RETURN: + if (head === null) { + break loop; + } + if (head._1 === EMPTY) { + head._1 = step; + status = CONTINUE; + step = head._2; + head._2 = EMPTY; + } else { + head._2 = step; + step = head; + if (tail === null) { + head = null; + } else { + head = tail._1; + tail = tail._2; + } + } } } - } - - // Keep a reference to the tree root so it can be cancelled. root = step; - for (fid = 0; fid < fiberId; fid++) { fibers[fid].run(); } } - - // Cancels the entire tree. If there are already subtrees being canceled, - // we need to first cancel those joins. We will then add fresh joins for - // all pending branches including those that were in the process of being - // canceled. - function cancel(error, cb) { - interrupt = util.left(error); + function cancel(error3, cb2) { + interrupt = util.left(error3); var innerKills; for (var kid in kills) { if (kills.hasOwnProperty(kid)) { @@ -1313,694 +1595,1052 @@ var PS = {}; } } } - kills = null; - var newKills = kill(error, root, cb); - - return function (killError) { - return new Aff(ASYNC, function (killCb) { - return function () { - for (var kid in newKills) { - if (newKills.hasOwnProperty(kid)) { - newKills[kid](); + var newKills = kill(error3, root, cb2); + return function(killError) { + return new Aff2(ASYNC, function(killCb) { + return function() { + for (var kid2 in newKills) { + if (newKills.hasOwnProperty(kid2)) { + newKills[kid2](); } } - return nonCanceler; + return nonCanceler2; }; }); }; } - - run(); - - return function (killError) { - return new Aff(ASYNC, function (killCb) { - return function () { + run3(); + return function(killError) { + return new Aff2(ASYNC, function(killCb) { + return function() { return cancel(killError, killCb); }; }); }; } - - function sequential(util, supervisor, par) { - return new Aff(ASYNC, function (cb) { - return function () { + function sequential2(util, supervisor, par) { + return new Aff2(ASYNC, function(cb) { + return function() { return runPar(util, supervisor, par, cb); }; }); } - - Aff.EMPTY = EMPTY; - Aff.Pure = AffCtr(PURE); - Aff.Throw = AffCtr(THROW); - Aff.Catch = AffCtr(CATCH); - Aff.Sync = AffCtr(SYNC); - Aff.Async = AffCtr(ASYNC); - Aff.Bind = AffCtr(BIND); - Aff.Bracket = AffCtr(BRACKET); - Aff.Fork = AffCtr(FORK); - Aff.Seq = AffCtr(SEQ); - Aff.ParMap = AffCtr(MAP); - Aff.ParApply = AffCtr(APPLY); - Aff.ParAlt = AffCtr(ALT); - Aff.Fiber = Fiber; - Aff.Supervisor = Supervisor; - Aff.Scheduler = Scheduler; - Aff.nonCanceler = nonCanceler; - - return Aff; + Aff2.EMPTY = EMPTY; + Aff2.Pure = AffCtr(PURE); + Aff2.Throw = AffCtr(THROW); + Aff2.Catch = AffCtr(CATCH); + Aff2.Sync = AffCtr(SYNC); + Aff2.Async = AffCtr(ASYNC); + Aff2.Bind = AffCtr(BIND); + Aff2.Bracket = AffCtr(BRACKET); + Aff2.Fork = AffCtr(FORK); + Aff2.Seq = AffCtr(SEQ); + Aff2.ParMap = AffCtr(MAP); + Aff2.ParApply = AffCtr(APPLY); + Aff2.ParAlt = AffCtr(ALT); + Aff2.Fiber = Fiber; + Aff2.Supervisor = Supervisor; + Aff2.Scheduler = Scheduler; + Aff2.nonCanceler = nonCanceler2; + return Aff2; + }(); + var _pure = Aff.Pure; + var _throwError = Aff.Throw; + function _catchError(aff) { + return function(k) { + return Aff.Catch(aff, k); + }; + } + function _map(f) { + return function(aff) { + if (aff.tag === Aff.Pure.tag) { + return Aff.Pure(f(aff._1)); + } else { + return Aff.Bind(aff, function(value) { + return Aff.Pure(f(value)); + }); + } + }; + } + function _bind(aff) { + return function(k) { + return Aff.Bind(aff, k); + }; + } + var _liftEffect = Aff.Sync; + function _parAffMap(f) { + return function(aff) { + return Aff.ParMap(f, aff); + }; + } + function _parAffApply(aff1) { + return function(aff2) { + return Aff.ParApply(aff1, aff2); + }; + } + var makeAff = Aff.Async; + function _makeFiber(util, aff) { + return function() { + return Aff.Fiber(util, null, aff); + }; + } + var _delay = function() { + function setDelay(n, k) { + if (n === 0 && typeof setImmediate !== "undefined") { + return setImmediate(k); + } else { + return setTimeout(k, n); + } + } + function clearDelay(n, t) { + if (n === 0 && typeof clearImmediate !== "undefined") { + return clearImmediate(t); + } else { + return clearTimeout(t); + } + } + return function(right, ms) { + return Aff.Async(function(cb) { + return function() { + var timer = setDelay(ms, cb(right())); + return function() { + return Aff.Sync(function() { + return right(clearDelay(ms, timer)); + }); + }; + }; + }); + }; }(); + var _sequential = Aff.Seq; - exports._pure = Aff.Pure; - - exports._throwError = Aff.Throw; - - exports._catchError = function (aff) { - return function (k) { - return Aff.Catch(aff, k); - }; + // output/Control.Parallel.Class/index.js + var sequential = function(dict) { + return dict.sequential; + }; + var parallel = function(dict) { + return dict.parallel; }; - exports._map = function (f) { - return function (aff) { - if (aff.tag === Aff.Pure.tag) { - return Aff.Pure(f(aff._1)); - } else { - return Aff.Bind(aff, function (value) { - return Aff.Pure(f(value)); + // output/Control.Parallel/index.js + var identity3 = /* @__PURE__ */ identity(categoryFn); + var parTraverse_ = function(dictParallel) { + var sequential2 = sequential(dictParallel); + var traverse_2 = traverse_(dictParallel.Applicative1()); + var parallel2 = parallel(dictParallel); + return function(dictFoldable) { + var traverse_1 = traverse_2(dictFoldable); + return function(f) { + var $48 = traverse_1(function($50) { + return parallel2(f($50)); }); - } + return function($49) { + return sequential2($48($49)); + }; + }; }; }; - - exports._bind = function (aff) { - return function (k) { - return Aff.Bind(aff, k); + var parSequence_ = function(dictParallel) { + var parTraverse_1 = parTraverse_(dictParallel); + return function(dictFoldable) { + return parTraverse_1(dictFoldable)(identity3); }; }; - exports._liftEffect = Aff.Sync; + // output/Partial.Unsafe/foreign.js + var _unsafePartial = function(f) { + return f(); + }; + + // output/Partial/foreign.js + var _crashWith = function(msg) { + throw new Error(msg); + }; - exports.makeAff = Aff.Async; + // output/Partial/index.js + var crashWith = function() { + return _crashWith; + }; - exports._makeFiber = function (util, aff) { - return function () { - return Aff.Fiber(util, null, aff); + // output/Partial.Unsafe/index.js + var crashWith2 = /* @__PURE__ */ crashWith(); + var unsafePartial = _unsafePartial; + var unsafeCrashWith = function(msg) { + return unsafePartial(function() { + return crashWith2(msg); + }); + }; + + // output/Effect.Aff/index.js + var $runtime_lazy2 = function(name2, moduleName, init2) { + var state2 = 0; + var val; + return function(lineNumber) { + if (state2 === 2) + return val; + if (state2 === 1) + throw new ReferenceError(name2 + " was needed before it finished initializing (module " + moduleName + ", line " + lineNumber + ")", moduleName, lineNumber); + state2 = 1; + val = init2(); + state2 = 2; + return val; }; }; -})(PS["Effect.Aff"] = PS["Effect.Aff"] || {}); -(function($PS) { - // Generated by purs version 0.13.6 - "use strict"; - $PS["Effect.Class"] = $PS["Effect.Class"] || {}; - var exports = $PS["Effect.Class"]; - var MonadEffect = function (Monad0, liftEffect) { - this.Monad0 = Monad0; - this.liftEffect = liftEffect; - }; - var liftEffect = function (dict) { - return dict.liftEffect; - }; - exports["liftEffect"] = liftEffect; - exports["MonadEffect"] = MonadEffect; -})(PS); -(function(exports) { - "use strict"; - - // module Partial.Unsafe - - exports.unsafePartial = function (f) { - return f(); + var $$void2 = /* @__PURE__ */ $$void(functorEffect); + var functorParAff = { + map: _parAffMap }; -})(PS["Partial.Unsafe"] = PS["Partial.Unsafe"] || {}); -(function(exports) { - "use strict"; - - // module Partial - - exports.crashWith = function () { - return function (msg) { - throw new Error(msg); - }; - }; -})(PS["Partial"] = PS["Partial"] || {}); -(function($PS) { - // Generated by purs version 0.13.6 - "use strict"; - $PS["Partial"] = $PS["Partial"] || {}; - var exports = $PS["Partial"]; - var $foreign = $PS["Partial"]; - exports["crashWith"] = $foreign.crashWith; -})(PS); -(function($PS) { - // Generated by purs version 0.13.6 - "use strict"; - $PS["Partial.Unsafe"] = $PS["Partial.Unsafe"] || {}; - var exports = $PS["Partial.Unsafe"]; - var $foreign = $PS["Partial.Unsafe"]; - var Partial = $PS["Partial"]; - var unsafeCrashWith = function (msg) { - return $foreign.unsafePartial(function (dictPartial) { - return Partial.crashWith()(msg); - }); + var functorAff = { + map: _map }; - exports["unsafeCrashWith"] = unsafeCrashWith; -})(PS); -(function($PS) { - // Generated by purs version 0.13.6 - "use strict"; - $PS["Effect.Aff"] = $PS["Effect.Aff"] || {}; - var exports = $PS["Effect.Aff"]; - var $foreign = $PS["Effect.Aff"]; - var Control_Applicative = $PS["Control.Applicative"]; - var Control_Apply = $PS["Control.Apply"]; - var Control_Bind = $PS["Control.Bind"]; - var Control_Monad = $PS["Control.Monad"]; - var Control_Monad_Error_Class = $PS["Control.Monad.Error.Class"]; - var Data_Either = $PS["Data.Either"]; - var Data_Function = $PS["Data.Function"]; - var Data_Functor = $PS["Data.Functor"]; - var Data_Unit = $PS["Data.Unit"]; - var Effect = $PS["Effect"]; - var Effect_Class = $PS["Effect.Class"]; - var Partial_Unsafe = $PS["Partial.Unsafe"]; - var functorAff = new Data_Functor.Functor($foreign["_map"]); - var ffiUtil = (function () { - var unsafeFromRight = function (v) { - if (v instanceof Data_Either.Right) { - return v.value0; - }; - if (v instanceof Data_Either.Left) { - return Partial_Unsafe.unsafeCrashWith("unsafeFromRight: Left"); - }; - throw new Error("Failed pattern match at Effect.Aff (line 400, column 21 - line 402, column 54): " + [ v.constructor.name ]); - }; - var unsafeFromLeft = function (v) { - if (v instanceof Data_Either.Left) { - return v.value0; - }; - if (v instanceof Data_Either.Right) { - return Partial_Unsafe.unsafeCrashWith("unsafeFromLeft: Right"); - }; - throw new Error("Failed pattern match at Effect.Aff (line 395, column 20 - line 397, column 54): " + [ v.constructor.name ]); - }; - var isLeft = function (v) { - if (v instanceof Data_Either.Left) { - return true; - }; - if (v instanceof Data_Either.Right) { - return false; - }; - throw new Error("Failed pattern match at Effect.Aff (line 390, column 12 - line 392, column 20): " + [ v.constructor.name ]); - }; - return { - isLeft: isLeft, - fromLeft: unsafeFromLeft, - fromRight: unsafeFromRight, - left: Data_Either.Left.create, - right: Data_Either.Right.create - }; - })(); - var makeFiber = function (aff) { - return $foreign["_makeFiber"](ffiUtil, aff); - }; - var launchAff = function (aff) { - return function __do() { - var fiber = makeFiber(aff)(); - fiber.run(); - return fiber; - }; + var ffiUtil = /* @__PURE__ */ function() { + var unsafeFromRight = function(v) { + if (v instanceof Right) { + return v.value0; + } + ; + if (v instanceof Left) { + return unsafeCrashWith("unsafeFromRight: Left"); + } + ; + throw new Error("Failed pattern match at Effect.Aff (line 407, column 21 - line 409, column 54): " + [v.constructor.name]); + }; + var unsafeFromLeft = function(v) { + if (v instanceof Left) { + return v.value0; + } + ; + if (v instanceof Right) { + return unsafeCrashWith("unsafeFromLeft: Right"); + } + ; + throw new Error("Failed pattern match at Effect.Aff (line 402, column 20 - line 404, column 55): " + [v.constructor.name]); + }; + var isLeft = function(v) { + if (v instanceof Left) { + return true; + } + ; + if (v instanceof Right) { + return false; + } + ; + throw new Error("Failed pattern match at Effect.Aff (line 397, column 12 - line 399, column 21): " + [v.constructor.name]); + }; + return { + isLeft, + fromLeft: unsafeFromLeft, + fromRight: unsafeFromRight, + left: Left.create, + right: Right.create + }; + }(); + var makeFiber = function(aff) { + return _makeFiber(ffiUtil, aff); + }; + var launchAff = function(aff) { + return function __do() { + var fiber = makeFiber(aff)(); + fiber.run(); + return fiber; + }; + }; + var applyParAff = { + apply: _parAffApply, + Functor0: function() { + return functorParAff; + } }; - var monadAff = new Control_Monad.Monad(function () { + var monadAff = { + Applicative0: function() { return applicativeAff; - }, function () { + }, + Bind1: function() { return bindAff; + } + }; + var bindAff = { + bind: _bind, + Apply0: function() { + return $lazy_applyAff(0); + } + }; + var applicativeAff = { + pure: _pure, + Apply0: function() { + return $lazy_applyAff(0); + } + }; + var $lazy_applyAff = /* @__PURE__ */ $runtime_lazy2("applyAff", "Effect.Aff", function() { + return { + apply: ap(monadAff), + Functor0: function() { + return functorAff; + } + }; }); - var bindAff = new Control_Bind.Bind(function () { - return applyAff; - }, $foreign["_bind"]); - var applyAff = new Control_Apply.Apply(function () { - return functorAff; - }, Control_Monad.ap(monadAff)); - var applicativeAff = new Control_Applicative.Applicative(function () { - return applyAff; - }, $foreign["_pure"]); - var monadEffectAff = new Effect_Class.MonadEffect(function () { + var pure2 = /* @__PURE__ */ pure(applicativeAff); + var bindFlipped2 = /* @__PURE__ */ bindFlipped(bindAff); + var monadEffectAff = { + liftEffect: _liftEffect, + Monad0: function() { return monadAff; - }, $foreign["_liftEffect"]); - var monadThrowAff = new Control_Monad_Error_Class.MonadThrow(function () { + } + }; + var liftEffect2 = /* @__PURE__ */ liftEffect(monadEffectAff); + var monadThrowAff = { + throwError: _throwError, + Monad0: function() { return monadAff; - }, $foreign["_throwError"]); - var monadErrorAff = new Control_Monad_Error_Class.MonadError(function () { + } + }; + var monadErrorAff = { + catchError: _catchError, + MonadThrow0: function() { return monadThrowAff; - }, $foreign["_catchError"]); - var runAff = function (k) { - return function (aff) { - return launchAff(Control_Bind.bindFlipped(bindAff)((function () { - var $49 = Effect_Class.liftEffect(monadEffectAff); - return function ($50) { - return $49(k($50)); - }; - })())(Control_Monad_Error_Class["try"](monadErrorAff)(aff))); - }; + } }; - var runAff_ = function (k) { - return function (aff) { - return Data_Functor["void"](Effect.functorEffect)(runAff(k)(aff)); - }; + var $$try2 = /* @__PURE__ */ $$try(monadErrorAff); + var runAff = function(k) { + return function(aff) { + return launchAff(bindFlipped2(function($77) { + return liftEffect2(k($77)); + })($$try2(aff))); + }; }; - var nonCanceler = Data_Function["const"](Control_Applicative.pure(applicativeAff)(Data_Unit.unit)); - exports["runAff_"] = runAff_; - exports["nonCanceler"] = nonCanceler; - exports["bindAff"] = bindAff; - exports["monadEffectAff"] = monadEffectAff; - exports["makeAff"] = $foreign.makeAff; -})(PS); -(function(exports) { - - - exports.promiseToEffectImpl = function (promise, onFulfilled, onRejected) { - return function () { - return promise.then(function (a) { - return onFulfilled(a)(); - }, function (err) { - return onRejected(err)(); - }); + var runAff_ = function(k) { + return function(aff) { + return $$void2(runAff(k)(aff)); }; }; -})(PS["Effect.Promise"] = PS["Effect.Promise"] || {}); -(function(exports) { - exports.undefer = function (f) { - return f(); + var parallelAff = { + parallel: unsafeCoerce2, + sequential: _sequential, + Monad0: function() { + return monadAff; + }, + Applicative1: function() { + return $lazy_applicativeParAff(0); + } }; -})(PS["Effect.Promise.Unsafe"] = PS["Effect.Promise.Unsafe"] || {}); -(function($PS) { - // Generated by purs version 0.13.6 - "use strict"; - $PS["Effect.Promise.Unsafe"] = $PS["Effect.Promise.Unsafe"] || {}; - var exports = $PS["Effect.Promise.Unsafe"]; - var $foreign = $PS["Effect.Promise.Unsafe"]; - exports["undefer"] = $foreign.undefer; -})(PS); -(function($PS) { - // Generated by purs version 0.13.6 - "use strict"; - $PS["Effect.Promise"] = $PS["Effect.Promise"] || {}; - var exports = $PS["Effect.Promise"]; - var $foreign = $PS["Effect.Promise"]; - var Effect_Promise_Unsafe = $PS["Effect.Promise.Unsafe"]; - var runPromise = function (onSucc) { - return function (onErr) { - return function (p) { - return $foreign.promiseToEffectImpl(Effect_Promise_Unsafe.undefer(function (dictDeferred) { - return p(); - }), onSucc, onErr); - }; + var $lazy_applicativeParAff = /* @__PURE__ */ $runtime_lazy2("applicativeParAff", "Effect.Aff", function() { + return { + pure: function() { + var $79 = parallel(parallelAff); + return function($80) { + return $79(pure2($80)); + }; + }(), + Apply0: function() { + return applyParAff; + } + }; + }); + var parSequence_2 = /* @__PURE__ */ parSequence_(parallelAff)(foldableArray); + var semigroupCanceler = { + append: function(v) { + return function(v1) { + return function(err) { + return parSequence_2([v(err), v1(err)]); + }; }; + } }; - exports["runPromise"] = runPromise; -})(PS); -(function($PS) { - // Generated by purs version 0.13.6 - "use strict"; - $PS["Crypto.Subtle.Key.Generate"] = $PS["Crypto.Subtle.Key.Generate"] || {}; - var exports = $PS["Crypto.Subtle.Key.Generate"]; - var $foreign = $PS["Crypto.Subtle.Key.Generate"]; - var Data_Either = $PS["Data.Either"]; - var Data_Functor = $PS["Data.Functor"]; - var Effect = $PS["Effect"]; - var Effect_Aff = $PS["Effect.Aff"]; - var Effect_Promise = $PS["Effect.Promise"]; - var rsa = function (r) { - return function (l) { - return function (e) { - return function (h) { - return { - name: r, - modulusLength: l, - publicExponent: e, - hash: h - }; - }; - }; - }; + var nonCanceler = /* @__PURE__ */ $$const(/* @__PURE__ */ pure2(unit)); + var monoidCanceler = { + mempty: nonCanceler, + Semigroup0: function() { + return semigroupCanceler; + } }; - var hmac = function (h) { - return { - name: "HMAC", - hash: h - }; + + // output/Foreign/foreign.js + function typeOf(value) { + return typeof value; + } + function tagOf(value) { + return Object.prototype.toString.call(value).slice(8, -1); + } + var isArray = Array.isArray || function(value) { + return Object.prototype.toString.call(value) === "[object Array]"; }; - var generateKeyPair = function (a) { - return function (e) { - return function (u) { - return Effect_Aff.makeAff(function (resolve) { - return Data_Functor.voidRight(Effect.functorEffect)(Effect_Aff.nonCanceler)(Effect_Promise.runPromise(function ($2) { - return resolve(Data_Either.Right.create($2)); - })(function ($3) { - return resolve(Data_Either.Left.create($3)); - })(function (dictDeferred) { - return $foreign.generateKeyImpl(a, e, u); - })); - }); - }; + + // output/Data.List.NonEmpty/index.js + var singleton3 = /* @__PURE__ */ function() { + var $200 = singleton2(plusList); + return function($201) { + return NonEmptyList($200($201)); + }; + }(); + + // output/Foreign/index.js + var TypeMismatch = /* @__PURE__ */ function() { + function TypeMismatch2(value0, value1) { + this.value0 = value0; + this.value1 = value1; + } + ; + TypeMismatch2.create = function(value0) { + return function(value1) { + return new TypeMismatch2(value0, value1); }; + }; + return TypeMismatch2; + }(); + var unsafeFromForeign = unsafeCoerce2; + var fail = function(dictMonad) { + var $153 = throwError(monadThrowExceptT(dictMonad)); + return function($154) { + return $153(singleton3($154)); + }; }; - var generateKey = function (a) { - return function (e) { - return function (u) { - return Effect_Aff.makeAff(function (resolve) { - return Data_Functor.voidRight(Effect.functorEffect)(Effect_Aff.nonCanceler)(Effect_Promise.runPromise(function ($4) { - return resolve(Data_Either.Right.create($4)); - })(function ($5) { - return resolve(Data_Either.Left.create($5)); - })(function (dictDeferred) { - return $foreign.generateKeyImpl(a, e, u); - })); - }); - }; + var unsafeReadTagged = function(dictMonad) { + var pure1 = pure(applicativeExceptT(dictMonad)); + var fail1 = fail(dictMonad); + return function(tag) { + return function(value) { + if (tagOf(value) === tag) { + return pure1(unsafeFromForeign(value)); + } + ; + if (otherwise) { + return fail1(new TypeMismatch(tag, tagOf(value))); + } + ; + throw new Error("Failed pattern match at Foreign (line 123, column 1 - line 123, column 104): " + [tag.constructor.name, value.constructor.name]); }; + }; }; - var ec = function (e) { - return function (c) { - return { - name: e, - namedCurve: c - }; - }; + var readString = function(dictMonad) { + return unsafeReadTagged(dictMonad)("String"); }; - var aes = function (a) { - return function (l) { - return { - name: a, - length: l - }; + + // output/Control.Promise/index.js + var voidRight2 = /* @__PURE__ */ voidRight(functorEffect); + var mempty2 = /* @__PURE__ */ mempty(monoidCanceler); + var toAff$prime = function(customCoerce) { + return function(p) { + return makeAff(function(cb) { + return voidRight2(mempty2)(thenImpl(p)(function($14) { + return cb(Left.create(customCoerce($14)))(); + })(function($15) { + return cb(Right.create($15))(); + })); + }); + }; + }; + + // output/Crypto.Subtle.Key.Types/foreign.js + function exportKeyImpl(f, x) { + return crypto.subtle.exportKey(f, x); + } + + // output/Foreign.Index/foreign.js + function unsafeReadPropImpl(f, s, key, value) { + return value == null ? f : s(value[key]); + } + + // output/Foreign.Index/index.js + var unsafeReadProp = function(dictMonad) { + var fail2 = fail(dictMonad); + var pure4 = pure(applicativeExceptT(dictMonad)); + return function(k) { + return function(value) { + return unsafeReadPropImpl(fail2(new TypeMismatch("object", typeOf(value))), pure4, k, value); }; + }; + }; + var readProp = function(dictMonad) { + return unsafeReadProp(dictMonad); }; - exports["generateKey"] = generateKey; - exports["generateKeyPair"] = generateKeyPair; - exports["rsa"] = rsa; - exports["ec"] = ec; - exports["hmac"] = hmac; - exports["aes"] = aes; - exports["exp65537"] = $foreign.exp65537; -})(PS); -(function(exports) { - "use strict"; - - exports.exportKeyImpl = function exportKeyImpl (f,x) { - return crypto.subtle.exportKey(f,x); - }; -})(PS["Crypto.Subtle.Key.Types"] = PS["Crypto.Subtle.Key.Types"] || {}); -(function($PS) { - // Generated by purs version 0.13.6 - "use strict"; - $PS["Crypto.Subtle.Key.Types"] = $PS["Crypto.Subtle.Key.Types"] || {}; - var exports = $PS["Crypto.Subtle.Key.Types"]; - var $foreign = $PS["Crypto.Subtle.Key.Types"]; - var Data_Either = $PS["Data.Either"]; - var Data_Functor = $PS["Data.Functor"]; - var Effect = $PS["Effect"]; - var Effect_Aff = $PS["Effect.Aff"]; - var Effect_Promise = $PS["Effect.Promise"]; + + // output/Crypto.Subtle.Key.Types/index.js + var bind2 = /* @__PURE__ */ bind(/* @__PURE__ */ bindExceptT(monadIdentity)); + var readProp2 = /* @__PURE__ */ readProp(monadIdentity); + var readString2 = /* @__PURE__ */ readString(monadIdentity); var wrapKey = "wrapKey"; var verify = "verify"; var unwrapKey = "unwrapKey"; - var sign = "sign"; + var sign2 = "sign"; var raw = "raw"; - var exportKey = function (f) { - return function (x) { - return Effect_Aff.makeAff(function (resolve) { - return Data_Functor.voidRight(Effect.functorEffect)(Effect_Aff.nonCanceler)(Effect_Promise.runPromise(function ($1) { - return resolve(Data_Either.Right.create($1)); - })(function ($2) { - return resolve(Data_Either.Left.create($2)); - })(function (dictDeferred) { - return $foreign.exportKeyImpl(f, x); - })); - }); - }; - }; + var errorFromDOMException = function(x) { + var v = runExcept(bind2(readProp2("message")(x))(readString2)); + if (v instanceof Left) { + return error("Not a DOMException"); + } + ; + if (v instanceof Right) { + return error(v.value0); + } + ; + throw new Error("Failed pattern match at Crypto.Subtle.Key.Types (line 136, column 27 - line 138, column 21): " + [v.constructor.name]); + }; + var exportKey = function(f) { + return function(x) { + return toAff$prime(errorFromDOMException)(exportKeyImpl(f, x)); + }; + }; var encrypt = "encrypt"; var deriveKey = "deriveKey"; var deriveBits = "deriveBits"; var decrypt = "decrypt"; - var allUsages = [ encrypt, decrypt, sign, verify, deriveKey, deriveBits, wrapKey, unwrapKey ]; - exports["allUsages"] = allUsages; - exports["exportKey"] = exportKey; - exports["raw"] = raw; -})(PS); -(function(exports) { - "use strict"; - - exports.log = function (s) { - return function () { - console.log(s); - return {}; - }; - }; -})(PS["Effect.Console"] = PS["Effect.Console"] || {}); -(function($PS) { - // Generated by purs version 0.13.6 - "use strict"; - $PS["Effect.Console"] = $PS["Effect.Console"] || {}; - var exports = $PS["Effect.Console"]; - var $foreign = $PS["Effect.Console"]; - exports["log"] = $foreign.log; -})(PS); -(function(exports) { - "use strict"; - - exports.throwException = function (e) { - return function () { - throw e; + + // output/Crypto.Subtle.Hash/index.js + var sha512 = "SHA-512"; + var sha384 = "SHA-384"; + var sha256 = "SHA-256"; + var sha1 = "SHA-1"; + + // output/Crypto.Subtle.Key.Generate/foreign.js + function generateKeyImpl(a, e, u) { + return crypto.subtle.generateKey(a, e, u); + } + var exp65537 = new Uint8Array([1, 0, 1]); + + // output/Crypto.Subtle.Key.Generate/index.js + var rsa = function(r) { + return function(l) { + return function(e) { + return function(h) { + return { + name: r, + modulusLength: l, + publicExponent: e, + hash: h + }; + }; + }; + }; + }; + var hmac = function(h) { + return { + name: "HMAC", + hash: h + }; + }; + var generateKeyPair = function(a) { + return function(e) { + return function(u) { + return toAff$prime(errorFromDOMException)(generateKeyImpl(a, e, u)); + }; + }; + }; + var generateKey = function(a) { + return function(e) { + return function(u) { + return toAff$prime(errorFromDOMException)(generateKeyImpl(a, e, u)); + }; + }; + }; + var ec = function(e) { + return function(c) { + return { + name: e, + namedCurve: c + }; }; }; -})(PS["Effect.Exception"] = PS["Effect.Exception"] || {}); -(function($PS) { - // Generated by purs version 0.13.6 - "use strict"; - $PS["Effect.Exception"] = $PS["Effect.Exception"] || {}; - var exports = $PS["Effect.Exception"]; - var $foreign = $PS["Effect.Exception"]; - exports["throwException"] = $foreign.throwException; -})(PS); -(function($PS) { - // Generated by purs version 0.13.6 - "use strict"; - $PS["Test.Main"] = $PS["Test.Main"] || {}; - var exports = $PS["Test.Main"]; - var Control_Applicative = $PS["Control.Applicative"]; - var Control_Bind = $PS["Control.Bind"]; - var Crypto_Subtle_Constants_AES = $PS["Crypto.Subtle.Constants.AES"]; - var Crypto_Subtle_Constants_EC = $PS["Crypto.Subtle.Constants.EC"]; - var Crypto_Subtle_Constants_RSA = $PS["Crypto.Subtle.Constants.RSA"]; - var Crypto_Subtle_Hash = $PS["Crypto.Subtle.Hash"]; - var Crypto_Subtle_Key_Generate = $PS["Crypto.Subtle.Key.Generate"]; - var Crypto_Subtle_Key_Types = $PS["Crypto.Subtle.Key.Types"]; - var Data_Either = $PS["Data.Either"]; - var Data_Unit = $PS["Data.Unit"]; - var Effect = $PS["Effect"]; - var Effect_Aff = $PS["Effect.Aff"]; - var Effect_Class = $PS["Effect.Class"]; - var Effect_Console = $PS["Effect.Console"]; - var Effect_Exception = $PS["Effect.Exception"]; - var log$prime = (function () { - var $31 = Effect_Class.liftEffect(Effect_Aff.monadEffectAff); - return function ($32) { - return $31(Effect_Console.log($32)); + var aes = function(a) { + return function(l) { + return { + name: a, + length: l }; - })(); - var main = (function () { - var resolve = function (eX) { - if (eX instanceof Data_Either.Left) { - return Effect_Exception.throwException(eX.value0); + }; + }; + + // output/Crypto.Subtle.Key.Import/foreign.js + function importKeyImpl(f, x, a, e, u) { + return crypto.subtle.importKey(f, x, a, e, u); + } + + // output/Crypto.Subtle.Key.Import/index.js + var pbkdf2 = { + name: "PBKDF2" + }; + var importKey = function(f) { + return function(x) { + return function(a) { + return function(e) { + return function(u) { + return toAff$prime(errorFromDOMException)(importKeyImpl(f, x, a, e, u)); }; - if (eX instanceof Data_Either.Right) { - return Control_Applicative.pure(Effect.applicativeEffect)(Data_Unit.unit); + }; + }; + }; + }; + var hkdf = { + name: "HKDF" + }; + + // output/Data.ArrayBuffer.Typed/foreign.js + function buffer(v) { + return v.buffer; + } + function newArray(f) { + return function newArray_(a, mb, mc) { + if (mb === null) + return new f(a); + var l = a.byteLength; + var eb = f.BYTES_PER_ELEMENT; + var off = Math.min(l, mb >>> 0); + if (mc === null) + return new f(a, off); + var len = Math.min((l - off) / eb, mc); + return new f(a, off, len); + }; + } + var newUint8ClampedArray = newArray(Uint8ClampedArray); + var newUint32Array = newArray(Uint32Array); + var newUint16Array = newArray(Uint16Array); + var newUint8Array = newArray(Uint8Array); + var newInt32Array = newArray(Int32Array); + var newInt16Array = newArray(Int16Array); + var newInt8Array = newArray(Int8Array); + var newFloat32Array = newArray(Float32Array); + var newFloat64Array = newArray(Float64Array); + + // output/Data.Array/foreign.js + var replicateFill = function(count) { + return function(value) { + if (count < 1) { + return []; + } + var result = new Array(count); + return result.fill(value); + }; + }; + var replicatePolyfill = function(count) { + return function(value) { + var result = []; + var n = 0; + for (var i = 0; i < count; i++) { + result[n++] = value; + } + return result; + }; + }; + var replicate = typeof Array.prototype.fill === "function" ? replicateFill : replicatePolyfill; + var fromFoldableImpl = function() { + function Cons2(head, tail) { + this.head = head; + this.tail = tail; + } + var emptyList = {}; + function curryCons(head) { + return function(tail) { + return new Cons2(head, tail); + }; + } + function listToArray(list) { + var result = []; + var count = 0; + var xs = list; + while (xs !== emptyList) { + result[count++] = xs.head; + xs = xs.tail; + } + return result; + } + return function(foldr3) { + return function(xs) { + return listToArray(foldr3(curryCons)(emptyList)(xs)); + }; + }; + }(); + var sortByImpl = function() { + function mergeFromTo(compare2, fromOrdering, xs1, xs2, from2, to) { + var mid; + var i; + var j; + var k; + var x; + var y; + var c; + mid = from2 + (to - from2 >> 1); + if (mid - from2 > 1) + mergeFromTo(compare2, fromOrdering, xs2, xs1, from2, mid); + if (to - mid > 1) + mergeFromTo(compare2, fromOrdering, xs2, xs1, mid, to); + i = from2; + j = mid; + k = from2; + while (i < mid && j < to) { + x = xs2[i]; + y = xs2[j]; + c = fromOrdering(compare2(x)(y)); + if (c > 0) { + xs1[k++] = y; + ++j; + } else { + xs1[k++] = x; + ++i; + } + } + while (i < mid) { + xs1[k++] = xs2[i++]; + } + while (j < to) { + xs1[k++] = xs2[j++]; + } + } + return function(compare2) { + return function(fromOrdering) { + return function(xs) { + var out; + if (xs.length < 2) + return xs; + out = xs.slice(0); + mergeFromTo(compare2, fromOrdering, out, xs.slice(0), 0, xs.length); + return out; + }; + }; + }; + }(); + + // output/Data.Array.ST/foreign.js + var sortByImpl2 = function() { + function mergeFromTo(compare2, fromOrdering, xs1, xs2, from2, to) { + var mid; + var i; + var j; + var k; + var x; + var y; + var c; + mid = from2 + (to - from2 >> 1); + if (mid - from2 > 1) + mergeFromTo(compare2, fromOrdering, xs2, xs1, from2, mid); + if (to - mid > 1) + mergeFromTo(compare2, fromOrdering, xs2, xs1, mid, to); + i = from2; + j = mid; + k = from2; + while (i < mid && j < to) { + x = xs2[i]; + y = xs2[j]; + c = fromOrdering(compare2(x)(y)); + if (c > 0) { + xs1[k++] = y; + ++j; + } else { + xs1[k++] = x; + ++i; + } + } + while (i < mid) { + xs1[k++] = xs2[i++]; + } + while (j < to) { + xs1[k++] = xs2[j++]; + } + } + return function(compare2) { + return function(fromOrdering) { + return function(xs) { + return function() { + if (xs.length < 2) + return xs; + mergeFromTo(compare2, fromOrdering, xs, xs.slice(0), 0, xs.length); + return xs; }; - throw new Error("Failed pattern match at Test.Main (line 24, column 20 - line 26, column 29): " + [ eX.constructor.name ]); + }; }; - return Effect_Aff.runAff_(resolve)(Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime("Generating Keys..."))(function () { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)((function () { - var genAES = function (mode) { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(Control_Bind.bind(Effect_Aff.bindAff)(Control_Bind.bindFlipped(Effect_Aff.bindAff)(Crypto_Subtle_Key_Types.exportKey(Crypto_Subtle_Key_Types.raw))(Crypto_Subtle_Key_Generate.generateKey(Crypto_Subtle_Key_Generate.aes(mode)(Crypto_Subtle_Constants_AES.l128))(true)(Crypto_Subtle_Key_Types.allUsages)))(function (k) { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime(" - 128"))(function () { - return log$prime(k); - }); - }))(function () { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(Control_Bind.bind(Effect_Aff.bindAff)(Control_Bind.bindFlipped(Effect_Aff.bindAff)(Crypto_Subtle_Key_Types.exportKey(Crypto_Subtle_Key_Types.raw))(Crypto_Subtle_Key_Generate.generateKey(Crypto_Subtle_Key_Generate.aes(mode)(Crypto_Subtle_Constants_AES.l192))(true)(Crypto_Subtle_Key_Types.allUsages)))(function (k) { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime(" - 192"))(function () { - return log$prime(k); - }); - }))(function () { - return Control_Bind.bind(Effect_Aff.bindAff)(Control_Bind.bindFlipped(Effect_Aff.bindAff)(Crypto_Subtle_Key_Types.exportKey(Crypto_Subtle_Key_Types.raw))(Crypto_Subtle_Key_Generate.generateKey(Crypto_Subtle_Key_Generate.aes(mode)(Crypto_Subtle_Constants_AES.l256))(true)(Crypto_Subtle_Key_Types.allUsages)))(function (k) { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime(" - 256"))(function () { - return log$prime(k); - }); + }; + }(); + + // output/Data.Nullable/foreign.js + var nullImpl = null; + + // output/Data.ArrayBuffer.Typed/index.js + var typedArrayUint8 = { + create: newUint8Array, + BinaryValue0: function() { + return void 0; + } + }; + var create = function(dict) { + return dict.create; + }; + var empty2 = function(dictTypedArray) { + var create1 = create(dictTypedArray); + return function(n) { + return function() { + return create1(n, nullImpl, nullImpl); + }; + }; + }; + + // output/Effect.Console/foreign.js + var log2 = function(s) { + return function() { + console.log(s); + }; + }; + + // output/Web.Encoding.TextEncoder/foreign.js + var newImpl = function() { + return new TextEncoder(); + }; + function encode(text) { + return function(encoder) { + return encoder.encode(text); + }; + } + + // output/Test.Main/index.js + var liftEffect3 = /* @__PURE__ */ liftEffect(monadEffectAff); + var pure3 = /* @__PURE__ */ pure(applicativeEffect); + var discard2 = /* @__PURE__ */ discard(discardUnit)(bindAff); + var bind3 = /* @__PURE__ */ bind(bindAff); + var bindFlipped3 = /* @__PURE__ */ bindFlipped(bindAff); + var empty3 = /* @__PURE__ */ empty2(typedArrayUint8); + var getRandomValues2 = /* @__PURE__ */ getRandomValues(typedArrayUint8); + var log$prime = function($41) { + return liftEffect3(log2($41)); + }; + var main = /* @__PURE__ */ function() { + var resolve = function(eX) { + if (eX instanceof Left) { + return throwException(eX.value0); + } + ; + if (eX instanceof Right) { + return pure3(unit); + } + ; + throw new Error("Failed pattern match at Test.Main (line 27, column 18 - line 29, column 27): " + [eX.constructor.name]); + }; + return runAff_(resolve)(discard2(log$prime("Generating Keys..."))(function() { + return discard2(function() { + var genAES = function(mode) { + return function(usages) { + return discard2(discard2(log$prime(" - 128"))(function() { + return bind3(bindFlipped3(exportKey(raw))(generateKey(aes(mode)(l128))(true)(usages)))(function(k) { + return log$prime(k); + }); + }))(function() { + return discard2(discard2(log$prime(" - 192"))(function() { + return bind3(bindFlipped3(exportKey(raw))(generateKey(aes(mode)(l192))(true)(usages)))(function(k) { + return log$prime(k); + }); + }))(function() { + return discard2(log$prime(" - 256"))(function() { + return bind3(bindFlipped3(exportKey(raw))(generateKey(aes(mode)(l256))(true)(usages)))(function(k) { + return log$prime(k); + }); + }); + }); + }); + }; + }; + return discard2(log$prime("- AES-CTR"))(function() { + return discard2(genAES(aesCTR)([encrypt, decrypt, wrapKey, unwrapKey]))(function() { + return discard2(log$prime("- AES-CBC"))(function() { + return discard2(genAES(aesCBC)([encrypt, decrypt, wrapKey, unwrapKey]))(function() { + return discard2(log$prime("- AES-GCM"))(function() { + return discard2(genAES(aesGCM)([encrypt, decrypt, wrapKey, unwrapKey]))(function() { + return discard2(log$prime("- AES-KW"))(function() { + return genAES(aesKW)([wrapKey, unwrapKey]); + }); + }); + }); + }); + }); + }); + }); + }())(function() { + return discard2(discard2(log$prime("HMAC"))(function() { + var usages = [sign2, verify]; + return discard2(bind3(bindFlipped3(exportKey(raw))(generateKey(hmac(sha1))(true)(usages)))(function(k) { + return discard2(log$prime(" - SHA-1"))(function() { + return log$prime(k); + }); + }))(function() { + return discard2(bind3(bindFlipped3(exportKey(raw))(generateKey(hmac(sha256))(true)(usages)))(function(k) { + return discard2(log$prime(" - SHA-256"))(function() { + return log$prime(k); + }); + }))(function() { + return discard2(bind3(bindFlipped3(exportKey(raw))(generateKey(hmac(sha384))(true)(usages)))(function(k) { + return discard2(log$prime(" - SHA-384"))(function() { + return log$prime(k); + }); + }))(function() { + return bind3(bindFlipped3(exportKey(raw))(generateKey(hmac(sha512))(true)(usages)))(function(k) { + return discard2(log$prime(" - SHA-512"))(function() { + return log$prime(k); + }); + }); + }); + }); + }); + }))(function() { + return discard2(function() { + var genEC = function(alg) { + return function(usages) { + return discard2(discard2(log$prime(" - P-256"))(function() { + return bind3(generateKeyPair(ec(alg)(p256))(true)(usages))(function(v) { + return discard2(log$prime(" - privateKey"))(function() { + return discard2(log$prime(v.privateKey))(function() { + return discard2(log$prime(" - publicKey"))(function() { + return bind3(exportKey(raw)(v.publicKey))(function(publicKey$prime) { + return log$prime(publicKey$prime); }); + }); }); + }); }); - }; - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime("- AES-CTR"))(function () { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(genAES(Crypto_Subtle_Constants_AES.aesCTR))(function () { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime("- AES-CBC"))(function () { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(genAES(Crypto_Subtle_Constants_AES.aesCBC))(function () { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime("- AES-GCM"))(function () { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(genAES(Crypto_Subtle_Constants_AES.aesGCM))(function () { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime("- AES-KW"))(function () { - return genAES(Crypto_Subtle_Constants_AES.aesKW); - }); - }); + }))(function() { + return discard2(discard2(log$prime(" - P-384"))(function() { + return bind3(generateKeyPair(ec(alg)(p384))(true)(usages))(function(v) { + return discard2(log$prime(" - privateKey"))(function() { + return discard2(log$prime(v.privateKey))(function() { + return discard2(log$prime(" - publicKey"))(function() { + return bind3(exportKey(raw)(v.publicKey))(function(publicKey$prime) { + return log$prime(publicKey$prime); + }); + }); + }); + }); + }); + }))(function() { + return discard2(log$prime(" - P-521"))(function() { + return bind3(generateKeyPair(ec(alg)(p521))(true)(usages))(function(v) { + return discard2(log$prime(" - privateKey"))(function() { + return discard2(log$prime(v.privateKey))(function() { + return discard2(log$prime(" - publicKey"))(function() { + return bind3(exportKey(raw)(v.publicKey))(function(publicKey$prime) { + return log$prime(publicKey$prime); }); + }); }); + }); }); + }); }); + }); + }; + }; + return discard2(log$prime("- ECDSA"))(function() { + return discard2(genEC(ecdsa)([sign2, verify]))(function() { + return discard2(log$prime("- ECDH"))(function() { + return genEC(ecdh)([deriveKey]); + }); }); - })())(function () { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime("HMAC"))(function () { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(Control_Bind.bind(Effect_Aff.bindAff)(Control_Bind.bindFlipped(Effect_Aff.bindAff)(Crypto_Subtle_Key_Types.exportKey(Crypto_Subtle_Key_Types.raw))(Crypto_Subtle_Key_Generate.generateKey(Crypto_Subtle_Key_Generate.hmac(Crypto_Subtle_Hash.sha1))(true)(Crypto_Subtle_Key_Types.allUsages)))(function (k) { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime(" - SHA-1"))(function () { - return log$prime(k); - }); - }))(function () { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(Control_Bind.bind(Effect_Aff.bindAff)(Control_Bind.bindFlipped(Effect_Aff.bindAff)(Crypto_Subtle_Key_Types.exportKey(Crypto_Subtle_Key_Types.raw))(Crypto_Subtle_Key_Generate.generateKey(Crypto_Subtle_Key_Generate.hmac(Crypto_Subtle_Hash.sha256))(true)(Crypto_Subtle_Key_Types.allUsages)))(function (k) { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime(" - SHA-256"))(function () { - return log$prime(k); + }); + }())(function() { + return discard2(function() { + var genRSA = function(alg) { + return function(usages) { + return discard2(discard2(log$prime(" - SHA-1"))(function() { + return bind3(generateKeyPair(rsa(alg)(2048)(exp65537)(sha1))(true)(usages))(function(v) { + return discard2(log$prime(" - privateKey"))(function() { + return discard2(log$prime(v.privateKey))(function() { + return discard2(log$prime(" - publicKey"))(function() { + return log$prime(v.publicKey); }); - }))(function () { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(Control_Bind.bind(Effect_Aff.bindAff)(Control_Bind.bindFlipped(Effect_Aff.bindAff)(Crypto_Subtle_Key_Types.exportKey(Crypto_Subtle_Key_Types.raw))(Crypto_Subtle_Key_Generate.generateKey(Crypto_Subtle_Key_Generate.hmac(Crypto_Subtle_Hash.sha384))(true)(Crypto_Subtle_Key_Types.allUsages)))(function (k) { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime(" - SHA-384"))(function () { - return log$prime(k); - }); - }))(function () { - return Control_Bind.bind(Effect_Aff.bindAff)(Control_Bind.bindFlipped(Effect_Aff.bindAff)(Crypto_Subtle_Key_Types.exportKey(Crypto_Subtle_Key_Types.raw))(Crypto_Subtle_Key_Generate.generateKey(Crypto_Subtle_Key_Generate.hmac(Crypto_Subtle_Hash.sha512))(true)(Crypto_Subtle_Key_Types.allUsages)))(function (k) { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime(" - SHA-512"))(function () { - return log$prime(k); - }); - }); + }); + }); + }); + }))(function() { + return discard2(discard2(log$prime(" - SHA-256"))(function() { + return bind3(generateKeyPair(rsa(alg)(2048)(exp65537)(sha256))(true)(usages))(function(v) { + return discard2(log$prime(" - privateKey"))(function() { + return discard2(log$prime(v.privateKey))(function() { + return discard2(log$prime(" - publicKey"))(function() { + return log$prime(v.publicKey); + }); }); + }); }); - }); - }))(function () { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)((function () { - var genEC = function (alg) { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(Control_Bind.bind(Effect_Aff.bindAff)(Crypto_Subtle_Key_Generate.generateKeyPair(Crypto_Subtle_Key_Generate.ec(alg)(Crypto_Subtle_Constants_EC.p256))(true)(Crypto_Subtle_Key_Types.allUsages))(function (v) { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime(" - P-256"))(function () { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime(" - privateKey"))(function () { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime(v.privateKey))(function () { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime(" - publicKey"))(function () { - return Control_Bind.bind(Effect_Aff.bindAff)(Crypto_Subtle_Key_Types.exportKey(Crypto_Subtle_Key_Types.raw)(v.publicKey))(function (publicKey$prime) { - return log$prime(publicKey$prime); - }); - }); - }); - }); - }); - }))(function () { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(Control_Bind.bind(Effect_Aff.bindAff)(Crypto_Subtle_Key_Generate.generateKeyPair(Crypto_Subtle_Key_Generate.ec(alg)(Crypto_Subtle_Constants_EC.p384))(true)(Crypto_Subtle_Key_Types.allUsages))(function (v) { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime(" - P-384"))(function () { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime(" - privateKey"))(function () { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime(v.privateKey))(function () { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime(" - publicKey"))(function () { - return Control_Bind.bind(Effect_Aff.bindAff)(Crypto_Subtle_Key_Types.exportKey(Crypto_Subtle_Key_Types.raw)(v.publicKey))(function (publicKey$prime) { - return log$prime(publicKey$prime); - }); - }); - }); - }); - }); - }))(function () { - return Control_Bind.bind(Effect_Aff.bindAff)(Crypto_Subtle_Key_Generate.generateKeyPair(Crypto_Subtle_Key_Generate.ec(alg)(Crypto_Subtle_Constants_EC.p521))(true)(Crypto_Subtle_Key_Types.allUsages))(function (v) { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime(" - P-521"))(function () { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime(" - privateKey"))(function () { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime(v.privateKey))(function () { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime(" - publicKey"))(function () { - return Control_Bind.bind(Effect_Aff.bindAff)(Crypto_Subtle_Key_Types.exportKey(Crypto_Subtle_Key_Types.raw)(v.publicKey))(function (publicKey$prime) { - return log$prime(publicKey$prime); - }); - }); - }); - }); - }); - }); + }))(function() { + return discard2(discard2(log$prime(" - SHA-384"))(function() { + return bind3(generateKeyPair(rsa(alg)(2048)(exp65537)(sha384))(true)(usages))(function(v) { + return discard2(log$prime(" - privateKey"))(function() { + return discard2(log$prime(v.privateKey))(function() { + return discard2(log$prime(" - publicKey"))(function() { + return log$prime(v.publicKey); }); + }); }); - }; - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime("- ECDSA"))(function () { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(genEC(Crypto_Subtle_Constants_EC.ecdsa))(function () { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime("- ECDH"))(function () { - return genEC(Crypto_Subtle_Constants_EC.ecdh); + }); + }))(function() { + return discard2(log$prime(" - SHA-512"))(function() { + return bind3(generateKeyPair(rsa(alg)(2048)(exp65537)(sha512))(true)(usages))(function(v) { + return discard2(log$prime(" - privateKey"))(function() { + return discard2(log$prime(v.privateKey))(function() { + return discard2(log$prime(" - publicKey"))(function() { + return log$prime(v.publicKey); + }); }); + }); }); + }); }); - })())(function () { - var genRSA = function (alg) { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(Control_Bind.bind(Effect_Aff.bindAff)(Crypto_Subtle_Key_Generate.generateKeyPair(Crypto_Subtle_Key_Generate.rsa(alg)(2048)(Crypto_Subtle_Key_Generate.exp65537)(Crypto_Subtle_Hash.sha1))(true)(Crypto_Subtle_Key_Types.allUsages))(function (v) { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime(" - SHA-1"))(function () { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime(" - privateKey"))(function () { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime(v.privateKey))(function () { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime(" - publicKey"))(function () { - return log$prime(v.publicKey); - }); - }); - }); - }); - }))(function () { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(Control_Bind.bind(Effect_Aff.bindAff)(Crypto_Subtle_Key_Generate.generateKeyPair(Crypto_Subtle_Key_Generate.rsa(alg)(2048)(Crypto_Subtle_Key_Generate.exp65537)(Crypto_Subtle_Hash.sha256))(true)(Crypto_Subtle_Key_Types.allUsages))(function (v) { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime(" - SHA-256"))(function () { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime(" - privateKey"))(function () { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime(v.privateKey))(function () { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime(" - publicKey"))(function () { - return log$prime(v.publicKey); - }); - }); - }); - }); - }))(function () { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(Control_Bind.bind(Effect_Aff.bindAff)(Crypto_Subtle_Key_Generate.generateKeyPair(Crypto_Subtle_Key_Generate.rsa(alg)(2048)(Crypto_Subtle_Key_Generate.exp65537)(Crypto_Subtle_Hash.sha384))(true)(Crypto_Subtle_Key_Types.allUsages))(function (v) { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime(" - SHA-384"))(function () { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime(" - privateKey"))(function () { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime(v.privateKey))(function () { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime(" - publicKey"))(function () { - return log$prime(v.publicKey); - }); - }); - }); - }); - }))(function () { - return Control_Bind.bind(Effect_Aff.bindAff)(Crypto_Subtle_Key_Generate.generateKeyPair(Crypto_Subtle_Key_Generate.rsa(alg)(2048)(Crypto_Subtle_Key_Generate.exp65537)(Crypto_Subtle_Hash.sha512))(true)(Crypto_Subtle_Key_Types.allUsages))(function (v) { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime(" - SHA-512"))(function () { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime(" - privateKey"))(function () { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime(v.privateKey))(function () { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime(" - publicKey"))(function () { - return log$prime(v.publicKey); - }); - }); - }); - }); - }); - }); - }); - }); - }; - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime("- RSASSA-PKCS1-v1_5"))(function () { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(genRSA(Crypto_Subtle_Constants_RSA.rsaPKCS1))(function () { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime("- RSA-PSS"))(function () { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(genRSA(Crypto_Subtle_Constants_RSA.rsaPSS))(function () { - return Control_Bind.discard(Control_Bind.discardUnit)(Effect_Aff.bindAff)(log$prime("- RSA-OAEP"))(function () { - return genRSA(Crypto_Subtle_Constants_RSA.rsaOAEP); - }); - }); + }); + }); + }; + }; + return discard2(log$prime("- RSASSA-PKCS1-v1_5"))(function() { + return discard2(genRSA(rsaPKCS1)([sign2, verify]))(function() { + return discard2(log$prime("- RSA-PSS"))(function() { + return discard2(genRSA(rsaPSS)([sign2, verify]))(function() { + return discard2(log$prime("- RSA-OAEP"))(function() { + return genRSA(rsaOAEP)([encrypt, decrypt]); + }); + }); + }); + }); + }); + }())(function() { + return discard2(discard2(log$prime("KDF"))(function() { + return bind3(liftEffect3(newImpl))(function(enc) { + var sk = encode("a special key")(enc); + var importTest = function(txt) { + return function(importType) { + return discard2(log$prime(" - extractable " + txt))(function() { + return bind3(importKey(raw)(buffer(sk))(importType)(true)([deriveBits]))(function(kexp) { + return discard2(log$prime(kexp))(function() { + return discard2(log$prime(" - " + txt))(function() { + return bind3(importKey(raw)(buffer(sk))(importType)(false)([deriveBits]))(function(k) { + return log$prime(k); }); + }); }); + }); + }); + }; + }; + return discard2(importTest("PBKDF2")(pbkdf2))(function() { + return importTest("HKDF")(hkdf); + }); + }); + }))(function() { + return discard2(log$prime("Random"))(function() { + return discard2(discard2(log$prime(" - UUID"))(function() { + return bind3(liftEffect3(randomUUID))(function(ruuid) { + return log$prime(ruuid); + }); + }))(function() { + return discard2(log$prime(" - Random Values"))(function() { + return bind3(liftEffect3(empty3(30)))(function(em) { + return bind3(liftEffect3(getRandomValues2(em)))(function(rand) { + return log$prime(rand); + }); }); + }); }); + }); }); + }); }); - })); - })(); - exports["main"] = main; - exports["log'"] = log$prime; -})(PS); -PS["Test.Main"].main(); \ No newline at end of file + }); + }); + })); + }(); + + // + main(); +})(); diff --git a/test/Main.purs b/test/Main.purs index 5be51a2..894abc1 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -1,120 +1,173 @@ module Test.Main where -import Crypto.Subtle.Key.Types (allUsages, CryptoKeyPair (..), exportKey, raw) -import Crypto.Subtle.Key.Generate (generateKey, generateKeyPair, aes, hmac, ec, rsa, exp65537) +import Prelude + +import Crypto.Random (getRandomValues, randomUUID) import Crypto.Subtle.Constants.AES as AES -import Crypto.Subtle.Constants.RSA as RSA import Crypto.Subtle.Constants.EC as EC +import Crypto.Subtle.Constants.RSA as RSA import Crypto.Subtle.Hash as Hash - -import Prelude -import Data.Either (Either (..)) +import Crypto.Subtle.Key.Generate (generateKey, generateKeyPair, aes, hmac, ec, rsa, exp65537) +import Crypto.Subtle.Key.Import (hkdf, importKey, pbkdf2) +import Crypto.Subtle.Key.Types (CryptoKeyPair(..), decrypt, deriveBits, deriveKey, encrypt, exportKey, raw, sign, unwrapKey, verify, wrapKey) +import Data.ArrayBuffer.Typed (buffer, empty) +import Data.ArrayBuffer.Types (Uint8Array) +import Data.Either (Either(..)) import Effect (Effect) -import Effect.Exception (throwException) -import Effect.Class (liftEffect) import Effect.Aff (Aff, runAff_) +import Effect.Class (liftEffect) import Effect.Console (log) +import Effect.Exception (throwException) import Unsafe.Coerce (unsafeCoerce) - - - +import Web.Encoding.TextEncoder (new, encode) main :: Effect Unit main = do - let resolve eX = case eX of - Left e -> throwException e - Right _ -> pure unit + let + resolve eX = case eX of + Left e -> throwException e + Right _ -> pure unit runAff_ resolve do log' "Generating Keys..." - do let genAES mode = do - do k <- exportKey raw =<< generateKey (aes mode AES.l128) true allUsages - log' " - 128" - log' (unsafeCoerce k) - do k <- exportKey raw =<< generateKey (aes mode AES.l192) true allUsages - log' " - 192" - log' (unsafeCoerce k) - do k <- exportKey raw =<< generateKey (aes mode AES.l256) true allUsages - log' " - 256" - log' (unsafeCoerce k) - log' "- AES-CTR" - genAES AES.aesCTR - log' "- AES-CBC" - genAES AES.aesCBC - log' "- AES-GCM" - genAES AES.aesGCM - log' "- AES-KW" - genAES AES.aesKW - do log' "HMAC" - do k <- exportKey raw =<< generateKey (hmac Hash.sha1) true allUsages - log' " - SHA-1" + do + let + genAES mode usages = do + do + log' " - 128" + k <- exportKey raw =<< generateKey (aes mode AES.l128) true usages log' (unsafeCoerce k) - do k <- exportKey raw =<< generateKey (hmac Hash.sha256) true allUsages - log' " - SHA-256" + -- As of 2023 these will work in Firefox only + do + log' " - 192" + k <- exportKey raw =<< generateKey (aes mode AES.l192) true usages log' (unsafeCoerce k) - do k <- exportKey raw =<< generateKey (hmac Hash.sha384) true allUsages - log' " - SHA-384" + do + log' " - 256" + k <- exportKey raw =<< generateKey (aes mode AES.l256) true usages log' (unsafeCoerce k) - do k <- exportKey raw =<< generateKey (hmac Hash.sha512) true allUsages + log' "- AES-CTR" + genAES AES.aesCTR [ encrypt, decrypt, wrapKey, unwrapKey ] + log' "- AES-CBC" + genAES AES.aesCBC [ encrypt, decrypt, wrapKey, unwrapKey ] + log' "- AES-GCM" + genAES AES.aesGCM [ encrypt, decrypt, wrapKey, unwrapKey ] + log' "- AES-KW" + genAES AES.aesKW [ wrapKey, unwrapKey ] + do + log' "HMAC" + let usages = [ sign, verify ] + do + k <- exportKey raw =<< generateKey (hmac Hash.sha1) true usages + log' " - SHA-1" + log' (unsafeCoerce k) + do + k <- exportKey raw =<< generateKey (hmac Hash.sha256) true usages + log' " - SHA-256" + log' (unsafeCoerce k) + do + k <- exportKey raw =<< generateKey (hmac Hash.sha384) true usages + log' " - SHA-384" + log' (unsafeCoerce k) + do + k <- exportKey raw =<< generateKey (hmac Hash.sha512) true usages + log' " - SHA-512" + log' (unsafeCoerce k) + do + let + genEC alg usages = do + do + log' " - P-256" + CryptoKeyPair { privateKey, publicKey } <- generateKeyPair (ec alg EC.p256) true usages + log' " - privateKey" + log' (unsafeCoerce privateKey) + log' " - publicKey" + publicKey' <- exportKey raw publicKey + log' (unsafeCoerce publicKey') + do + log' " - P-384" + CryptoKeyPair { privateKey, publicKey } <- generateKeyPair (ec alg EC.p384) true usages + log' " - privateKey" + log' (unsafeCoerce privateKey) + log' " - publicKey" + publicKey' <- exportKey raw publicKey + log' (unsafeCoerce publicKey') + do + log' " - P-521" + CryptoKeyPair { privateKey, publicKey } <- generateKeyPair (ec alg EC.p521) true usages + log' " - privateKey" + log' (unsafeCoerce privateKey) + log' " - publicKey" + publicKey' <- exportKey raw publicKey + log' (unsafeCoerce publicKey') + log' "- ECDSA" + genEC EC.ecdsa [ sign, verify ] + log' "- ECDH" + genEC EC.ecdh [ deriveKey ] + do + let + genRSA alg usages = do + do + log' " - SHA-1" + CryptoKeyPair { privateKey, publicKey } <- generateKeyPair (rsa alg 2048 exp65537 Hash.sha1) true usages + log' " - privateKey" + log' (unsafeCoerce privateKey) + log' " - publicKey" + log' (unsafeCoerce publicKey) + do + log' " - SHA-256" + CryptoKeyPair { privateKey, publicKey } <- generateKeyPair (rsa alg 2048 exp65537 Hash.sha256) true usages + log' " - privateKey" + log' (unsafeCoerce privateKey) + log' " - publicKey" + log' (unsafeCoerce publicKey) + do + log' " - SHA-384" + CryptoKeyPair { privateKey, publicKey } <- generateKeyPair (rsa alg 2048 exp65537 Hash.sha384) true usages + log' " - privateKey" + log' (unsafeCoerce privateKey) + log' " - publicKey" + log' (unsafeCoerce publicKey) + do log' " - SHA-512" - log' (unsafeCoerce k) - do let genEC alg = do - do CryptoKeyPair {privateKey,publicKey} <- generateKeyPair (ec alg EC.p256) true allUsages - log' " - P-256" - log' " - privateKey" - log' (unsafeCoerce privateKey) - log' " - publicKey" - publicKey' <- exportKey raw publicKey - log' (unsafeCoerce publicKey') - do CryptoKeyPair {privateKey,publicKey} <- generateKeyPair (ec alg EC.p384) true allUsages - log' " - P-384" - log' " - privateKey" - log' (unsafeCoerce privateKey) - log' " - publicKey" - publicKey' <- exportKey raw publicKey - log' (unsafeCoerce publicKey') - do CryptoKeyPair {privateKey,publicKey} <- generateKeyPair (ec alg EC.p521) true allUsages - log' " - P-521" - log' " - privateKey" - log' (unsafeCoerce privateKey) - log' " - publicKey" - publicKey' <- exportKey raw publicKey - log' (unsafeCoerce publicKey') - log' "- ECDSA" - genEC EC.ecdsa - log' "- ECDH" - genEC EC.ecdh - do let genRSA alg = do - do CryptoKeyPair {privateKey,publicKey} <- generateKeyPair (rsa alg 2048 exp65537 Hash.sha1) true allUsages - log' " - SHA-1" - log' " - privateKey" - log' (unsafeCoerce privateKey) - log' " - publicKey" - log' (unsafeCoerce publicKey) - do CryptoKeyPair {privateKey,publicKey} <- generateKeyPair (rsa alg 2048 exp65537 Hash.sha256) true allUsages - log' " - SHA-256" - log' " - privateKey" - log' (unsafeCoerce privateKey) - log' " - publicKey" - log' (unsafeCoerce publicKey) - do CryptoKeyPair {privateKey,publicKey} <- generateKeyPair (rsa alg 2048 exp65537 Hash.sha384) true allUsages - log' " - SHA-384" - log' " - privateKey" - log' (unsafeCoerce privateKey) - log' " - publicKey" - log' (unsafeCoerce publicKey) - do CryptoKeyPair {privateKey,publicKey} <- generateKeyPair (rsa alg 2048 exp65537 Hash.sha512) true allUsages - log' " - SHA-512" - log' " - privateKey" - log' (unsafeCoerce privateKey) - log' " - publicKey" - log' (unsafeCoerce publicKey) - log' "- RSASSA-PKCS1-v1_5" - genRSA RSA.rsaPKCS1 - log' "- RSA-PSS" - genRSA RSA.rsaPSS - log' "- RSA-OAEP" - genRSA RSA.rsaOAEP + CryptoKeyPair { privateKey, publicKey } <- generateKeyPair (rsa alg 2048 exp65537 Hash.sha512) true usages + log' " - privateKey" + log' (unsafeCoerce privateKey) + log' " - publicKey" + log' (unsafeCoerce publicKey) + log' "- RSASSA-PKCS1-v1_5" + genRSA RSA.rsaPKCS1 [ sign, verify ] + log' "- RSA-PSS" + genRSA RSA.rsaPSS [ sign, verify ] + log' "- RSA-OAEP" + genRSA RSA.rsaOAEP [ encrypt, decrypt ] + do + log' "KDF" + do + enc <- liftEffect new + let sk = encode "a special key" enc + let + importTest txt importType = + do + log' (" - extractable " <> txt) + kexp <- importKey raw (buffer sk) importType true [ deriveBits ] + log' (unsafeCoerce kexp) + log' (" - " <> txt) + k <- importKey raw (buffer sk) importType false [ deriveBits ] + log' (unsafeCoerce k) + importTest "PBKDF2" pbkdf2 + importTest "HKDF" hkdf + do + log' "Random" + do + log' " - UUID" + ruuid <- liftEffect randomUUID + log' ruuid + do + log' " - Random Values" + em <- liftEffect (empty 30) :: Aff Uint8Array + rand <- liftEffect (getRandomValues em) + log' (unsafeCoerce rand) log' :: String -> Aff Unit log' = liftEffect <<< log