ArrayBuffer builder for Purescript

arraybuffer, purescript
psc-package install arraybuffer-builder



CI Pursuit

ArrayBuffer serialization in the “builder monoid” and “builder monad” style. In this style, we build up serialized data structures by appending to a monoid in a Writer monad with do-notation. This style of serialization has been used for a long time and we insist that it works really well.

This package provides a Builder monoid and a PutM monad which are roughly equivalent to types of the same name in the Haskell Data.Binary.Put module.

This package defines no typeclasses. Typeclass-based serialization/deserialization generally assumes that we are round-tripping some data structures out to storage and then back into our program. This package is designed for the case in which we are serializing some data to be sent to another program which expects a serialization format that we don't control.

Usage Examples

All ArrayBuffer building must occur in Effect.

Serialize an integer

Create a two-byte arraybuffer :: ArrayBuffer which contains the number -10 encoded as big-endian 16-bit two’s-complement.

  arraybuffer <- execPut $ putInt16be (-10)

Serialize three floats

Create a 24-byte arraybuffer :: ArrayBuffer which contains three big-endian IEEE-754 double-precision floats.

  arraybuffer <- execPut $ do
    putFloat64be 1.0
    putFloat64be 2.0
    putFloat64be 3.0

Serialize a String as UTF8

Encode a String as UTF8 with a length prefix.

We give this as an example, rather than supporting it in the library, because it depends on Data.TextEncoding.encodeUtf8.

putStringUtf8 :: forall m. (MonadEffect m) => String -> PutM m Unit
putStringUtf8 s = do
  let stringbuf = Data.ArrayBuffer.Typed.buffer $ Data.TextEncoding.encodeUtf8 s
  -- Put a 32-bit big-endian length prefix for the length of the utf8 string, in bytes.
  putUint32be $ Data.Uint.fromInt $ Data.ArrayBuffer.byteLength stringbuf
  putArrayBuffer stringbuf

  arraybuffer <- execPut $ putStringUtf8 "BLM"

Serialize an Array Int

Encode an Array Int with a length prefix in a way that's compatible with the Binary instance for [Int32] from the Haskell binary library.

putArrayInt32 :: forall m. (MonadEffect m) => Array Int -> PutM m Unit
putArrayInt32 xs = do
    -- Put a 64-bit big-endian length prefix for the length of the array.
    putUint32be 0
    putUint32be $ Data.Uint.fromInt $ Data.Array.length xs
    traverse_ putInt32be xs

  arraybuffer <- execPut $ putArrayInt32 [1,2,3]


This package is only for writing ArrayBuffers, not reading them. See purescript-parsing-dataview for a way to deserialize from ArrayBuffer back to Purescript data.


Alternative packages for serializing to an ArrayBuffer