haste-compiler-0.5.5.0: Haskell To ECMAScript compiler

Safe HaskellNone
LanguageHaskell98

Haste.Binary

Description

Handling of Javascript-native binary blobs.

Generics borrowed from the binary package by Lennart Kolmodin (released under BSD3)

Synopsis

Documentation

type Put = PutM ()

putJSString :: JSString -> Put

Serialize a JSString as UTF-16 (somewhat) efficiently.

data Get a

skip :: Int -> Get ()

Skip n bytes of input.

class Monad m => MonadBlob m where

Methods

getBlobData :: Blob -> m BlobData

Retrieve the raw data from a blob.

getBlobText' :: Blob -> m JSString

Interpret a blob as UTF-8 text, as a JSString.

class Binary a where

Somewhat efficient serializationdeserialization tofrom binary Blobs. The layout of the binaries producedread by getput and encode/decode may change between versions. If you need a stable binary format, you should make your own using the primitives in Haste.Binary.Get/Put.

Minimal complete definition

Nothing

Methods

get :: Get a

put :: a -> Put

getBlobText :: MonadBlob m => Blob -> m String

Interpret a blob as UTF-8 text.

data Blob

A JavaScript Blob on the client, a ByteString on the server.

data BlobData

In a browser context, BlobData is essentially a DataView, with an accompanying offset and length for fast slicing. In a server context, it is simply a ByteString.

blobSize :: Blob -> Int

The size, in bytes, of the contents of the given blob.

blobDataSize :: BlobData -> Int

The size, in bytes, of the contents of the given blob data.

toByteString :: BlobData -> ByteString

Convert a BlobData to a ByteString. Only usable server-side.

fromByteString :: ByteString -> BlobData

Convert a ByteString to a BlobData. Only usable server-side.

toBlob :: BlobData -> Blob

Convert a piece of BlobData back into a Blob.

strToBlob :: JSString -> Blob

Create a Blob from a JSString.

encode :: Binary a => a -> Blob

Encode any serializable data into a Blob.

decode :: Binary a => BlobData -> Either String a

Decode any deserializable data from a BlobData.

decodeBlob :: (MonadBlob m, Binary a) => Blob -> m (Either String a)

Decode a Blob into some deserializable value, inconveniently locked up inside the CIO monad (or any other concurrent monad) due to the somewhat special way JavaScript uses to deal with binary data.