@@ -1,8 +1,7 @@
1# purescript-arraybuffer-builder
1# purescript-arraybuffer-builder
3−[](https://github.com/rowtype-yoga/purescript-arraybuffer-builder/actions)
3+[](https://github.com/jamesdbrock/purescript-arraybuffer-builder/actions)
4[](http://pursuit.purescript.org/packages/purescript-arraybuffer-builder/)
4[](http://pursuit.purescript.org/packages/purescript-arraybuffer-builder/)
5−[](https://github.com/jamesdbrock)
8[`ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer)
7[`ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer)
@@ -51,26 +50,23 @@ Encode a `String` as UTF8 with a length prefix into our `Builder`.
52We give this as an example, rather than supporting it in the library, because
51We give this as an example, rather than supporting it in the library, because
53it depends on
52it depends on
54−[`Web.Encoding.TextEncoder`](https://pursuit.purescript.org/packages/purescript-web-encoding/docs/Web.Encoding.TextEncoder).
53+[`Data.TextEncoding.encodeUtf8`](https://pursuit.purescript.org/packages/purescript-text-encoding/docs/Data.TextEncoding#v:encodeUtf8).
56```purescript
55```purescript
57−import Effect.Class (class MonadEffect, liftEffect)
58−import Data.UInt (fromInt)
59−import Data.ArrayBuffer.Types (ArrayBuffer(..))
60−import Data.ArrayBuffer.Builder (PutM, putArrayBuffer, execPut, putUint32be)
56+import Data.ArrayBuffer.Builder (PutM, putArrayBuffer, execPut)
61import Data.ArrayBuffer.Typed (buffer)
57import Data.ArrayBuffer.Typed (buffer)
58+import Data.TextEncoding (encodeUtf8)
62import Data.ArrayBuffer.ArrayBuffer (byteLength)
59import Data.ArrayBuffer.ArrayBuffer (byteLength)
63−import Web.Encoding.TextEncoder (new, TextEncoder, encode)
65putStringUtf8 :: forall m. MonadEffect m => String -> PutM m Unit
61putStringUtf8 :: forall m. MonadEffect m => String -> PutM m Unit
66putStringUtf8 s = do
62putStringUtf8 s = do
67− textEncoder <- liftEffect new
68− let stringbuf = buffer $ encode s textEncoder
63+ let stringbuf = buffer $ encodeUtf8 s
69− -- Put a 32-bit big-endian length for the utf8 string, in bytes.
64+ -- Put a 32-bit big-endian length prefix for the length of the utf8 string, in bytes.
70− putUint32be $ fromInt $ byteLength stringbuf
65+ putInt32be $ byteLength stringbuf
71 putArrayBuffer stringbuf
66 putArrayBuffer stringbuf
73−arraybuffer :: ArrayBuffer <- execPut $ putStringUtf8 "🦝"
69+ arraybuffer :: ArrayBuffer <- execPut $ putStringUtf8 "BLM"
76### Serialize an `Array Int`
72### Serialize an `Array Int`
@@ -89,7 +85,7 @@ import Data.Array (length)
90putArrayInt32 :: forall m. MonadEffect m => Array Int -> PutM m Unit
86putArrayInt32 :: forall m. MonadEffect m => Array Int -> PutM m Unit
91putArrayInt32 xs = do
87putArrayInt32 xs = do
92− -- Put a 64-bit big-endian length prefix for the array.
88+ -- Put a 64-bit big-endian length prefix for the length of the array.
93 putInt32be 0
89 putInt32be 0
94 putInt32be $ length xs
90 putInt32be $ length xs
95 traverse_ putInt32be xs
91 traverse_ putInt32be xs
@@ -109,7 +105,7 @@ import Data.Array (foldRecM)
110putArrayInt32 :: forall m. MonadEffect m => MonadRec m => Array Int -> PutM m Unit
106putArrayInt32 :: forall m. MonadEffect m => MonadRec m => Array Int -> PutM m Unit
111putArrayInt32 xs = do
107putArrayInt32 xs = do
112− -- Put a 64-bit big-endian length prefix for the array.
108+ -- Put a 64-bit big-endian length prefix for the length of the array.
113 putInt32be 0
109 putInt32be 0
114 putInt32be $ length xs
110 putInt32be $ length xs
115 foldRecM (\_ x -> putInt32be x) unit xs
111 foldRecM (\_ x -> putInt32be x) unit xs
@@ -124,7 +120,7 @@ of the `PutM` builder expression are stack-safe.
125This package is only for writing `ArrayBuffer`s, not reading them.
121This package is only for writing `ArrayBuffer`s, not reading them.
127−[__parsing-dataview__](https://pursuit.purescript.org/packages/purescript-parsing-dataview/)
123+[__purescript-parsing-dataview__](https://pursuit.purescript.org/packages/purescript-parsing-dataview/)
128for a way to deserialize from `ArrayBuffer` back to Purescript data.
124for a way to deserialize from `ArrayBuffer` back to Purescript data.
130## Alternative packages for serializing to an `ArrayBuffer`
126## Alternative packages for serializing to an `ArrayBuffer`
@@ -133,7 +129,6 @@ for a way to deserialize from `ArrayBuffer` back to Purescript data.
133* [__node-buffer__](https://pursuit.purescript.org/packages/purescript-node-buffer)
129* [__node-buffer__](https://pursuit.purescript.org/packages/purescript-node-buffer)
134* [__arraybuffer-class__](https://pursuit.purescript.org/packages/purescript-arraybuffer-class)
130* [__arraybuffer-class__](https://pursuit.purescript.org/packages/purescript-arraybuffer-class)
135* [__bytestrings__](https://pursuit.purescript.org/packages/purescript-bytestrings/)
131* [__bytestrings__](https://pursuit.purescript.org/packages/purescript-bytestrings/)
136−* [__array-builder__](https://pursuit.purescript.org/packages/purescript-array-builder)
138## Development
133## Development