@@ -1,7 +1,8 @@
11# purescript-arraybuffer-builder
22
3−[](https://github.com/jamesdbrock/purescript-arraybuffer-builder/actions)
3+[](https://github.com/rowtype-yoga/purescript-arraybuffer-builder/actions)
44[](http://pursuit.purescript.org/packages/purescript-arraybuffer-builder/)
5+[](https://github.com/jamesdbrock)
56
67
78[`ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer)
@@ -50,23 +51,26 @@ Encode a `String` as UTF8 with a length prefix into our `Builder`.
5051
5152We give this as an example, rather than supporting it in the library, because
5253it depends on
53−[`Data.TextEncoding.encodeUtf8`](https://pursuit.purescript.org/packages/purescript-text-encoding/docs/Data.TextEncoding#v:encodeUtf8).
54+[`Web.Encoding.TextEncoder`](https://pursuit.purescript.org/packages/purescript-web-encoding/docs/Web.Encoding.TextEncoder).
5455
5556```purescript
56−import Data.ArrayBuffer.Builder (PutM, putArrayBuffer, execPut)
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)
5761import Data.ArrayBuffer.Typed (buffer)
58−import Data.TextEncoding (encodeUtf8)
5962import Data.ArrayBuffer.ArrayBuffer (byteLength)
63+import Web.Encoding.TextEncoder (new, TextEncoder, encode)
6064
6165putStringUtf8 :: forall m. MonadEffect m => String -> PutM m Unit
6266putStringUtf8 s = do
63− let stringbuf = buffer $ encodeUtf8 s
64− -- Put a 32-bit big-endian length prefix for the length of the utf8 string, in bytes.
65− putInt32be $ byteLength stringbuf
67+ textEncoder <- liftEffect new
68+ let stringbuf = buffer $ encode s textEncoder
69+ -- Put a 32-bit big-endian length for the utf8 string, in bytes.
70+ putUint32be $ fromInt $ byteLength stringbuf
6671 putArrayBuffer stringbuf
6772
68−do
69− arraybuffer :: ArrayBuffer <- execPut $ putStringUtf8 "BLM"
73+arraybuffer :: ArrayBuffer <- execPut $ putStringUtf8 "🦝"
7074```
7175
7276### Serialize an `Array Int`
@@ -85,7 +89,7 @@ import Data.Array (length)
8589
8690putArrayInt32 :: forall m. MonadEffect m => Array Int -> PutM m Unit
8791putArrayInt32 xs = do
88− -- Put a 64-bit big-endian length prefix for the length of the array.
92+ -- Put a 64-bit big-endian length prefix for the array.
8993 putInt32be 0
9094 putInt32be $ length xs
9195 traverse_ putInt32be xs
@@ -105,7 +109,7 @@ import Data.Array (foldRecM)
105109
106110putArrayInt32 :: forall m. MonadEffect m => MonadRec m => Array Int -> PutM m Unit
107111putArrayInt32 xs = do
108− -- Put a 64-bit big-endian length prefix for the length of the array.
112+ -- Put a 64-bit big-endian length prefix for the array.
109113 putInt32be 0
110114 putInt32be $ length xs
111115 foldRecM (\_ x -> putInt32be x) unit xs
@@ -120,7 +124,7 @@ of the `PutM` builder expression are stack-safe.
120124
121125This package is only for writing `ArrayBuffer`s, not reading them.
122126See
123−[__purescript-parsing-dataview__](https://pursuit.purescript.org/packages/purescript-parsing-dataview/)
127+[__parsing-dataview__](https://pursuit.purescript.org/packages/purescript-parsing-dataview/)
124128for a way to deserialize from `ArrayBuffer` back to Purescript data.
125129
126130## Alternative packages for serializing to an `ArrayBuffer`