Skip to content

Commit 55f91c1

Browse files
Add JsonCodec for SRIHash
1 parent 2b8324d commit 55f91c1

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

app/src/Registry/Json.purs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import Data.Array.NonEmpty (NonEmptyArray)
5050
import Data.Array.NonEmpty as NEA
5151
import Data.Bifunctor (lmap)
5252
import Data.Bitraversable (ltraverse)
53+
import Data.Codec.Argonaut as CA
5354
import Data.Either (Either(..), note)
5455
import Data.Int as Int
5556
import Data.Map (Map)
@@ -228,8 +229,8 @@ instance (EncodeRecord row list, DecodeRecord row list, RL.RowToList row list) =
228229
Just object -> decodeRecord object (Proxy :: Proxy list)
229230

230231
instance RegistryJson SRIHash where
231-
encode = encode <<< SRIHash.print
232-
decode = SRIHash.parse <=< decode
232+
encode = CA.encode SRIHash.codec
233+
decode = lmap CA.printJsonDecodeError <<< CA.decode SRIHash.codec
233234

234235
---------
235236

lib/spago.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package:
33
publish:
44
license: BSD-3-Clause
55
dependencies:
6+
- codec-argonaut
67
- effect
78
- node-buffer
89
- node-fs-aff
@@ -14,3 +15,4 @@ package:
1415
- argonaut-core
1516
- spec
1617
- sunde
18+

lib/src/Registry/SRIHash.purs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
-- | https://github.com/purescript/registry-dev/blob/master/SPEC.md#srihash
55
module Registry.SRIHash
66
( SRIHash
7+
, codec
78
, hashBuffer
89
, hashFile
910
, hashString
@@ -16,7 +17,10 @@ import Prelude
1617

1718
import Data.Array as Array
1819
import Data.Bifunctor (lmap)
20+
import Data.Codec.Argonaut (JsonCodec)
21+
import Data.Codec.Argonaut as CA
1922
import Data.Either (Either)
23+
import Data.Either as Either
2024
import Data.List.Lazy as List.Lazy
2125
import Data.String.CodeUnits as String.CodeUnits
2226
import Effect (Effect)
@@ -37,6 +41,10 @@ newtype SRIHash = SRIHash { sri :: String, hash :: String }
3741

3842
derive instance Eq SRIHash
3943

44+
codec :: JsonCodec SRIHash
45+
codec = CA.prismaticCodec "SRIHash" (Either.hush <<< parse) print CA.string
46+
47+
-- | Print a SRIHash as a subresource integrity hash using sha256
4048
print :: SRIHash -> String
4149
print (SRIHash { sri, hash }) = sri <> "-" <> hash
4250

0 commit comments

Comments
 (0)