@@ -1,7 +1,8 @@
1# purescript-arraybuffer-builder
1# purescript-arraybuffer-builder
3−[](https://github.com/jamesdbrock/purescript-arraybuffer-builder/actions)
3+[](https://github.com/rowtype-yoga/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)
7[`ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer)
8[`ArrayBuffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer)
@@ -50,23 +51,23 @@ Encode a `String` as UTF8 with a length prefix into our `Builder`.
51We give this as an example, rather than supporting it in the library, because
52We give this as an example, rather than supporting it in the library, because
52it depends on
53it 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).
55```purescript
56```purescript
56import Data.ArrayBuffer.Builder (PutM, putArrayBuffer, execPut)
57import Data.ArrayBuffer.Builder (PutM, putArrayBuffer, execPut)
57import Data.ArrayBuffer.Typed (buffer)
58import Data.ArrayBuffer.Typed (buffer)
58−import Data.TextEncoding (encodeUtf8)
59import Data.ArrayBuffer.ArrayBuffer (byteLength)
59import Data.ArrayBuffer.ArrayBuffer (byteLength)
60+import Web.Encoding.TextEncoder (new, textEncoder)
61putStringUtf8 :: forall m. MonadEffect m => String -> PutM m Unit
62putStringUtf8 :: forall m. MonadEffect m => String -> PutM m Unit
62putStringUtf8 s = do
63putStringUtf8 s = do
64+ textEncoder <- liftEffect new
63− let stringbuf = buffer $ encodeUtf8 s
65+ let stringbuf = buffer $ encode s textEncoder
64− -- Put a 32-bit big-endian length prefix for the length of the utf8 string, in bytes.
66+ -- Put a 32-bit big-endian length for the utf8 string, in bytes.
65 putInt32be $ byteLength stringbuf
67 putInt32be $ byteLength stringbuf
66 putArrayBuffer stringbuf
68 putArrayBuffer stringbuf
69− arraybuffer :: ArrayBuffer <- execPut $ putStringUtf8 "BLM"
70+arraybuffer :: ArrayBuffer <- execPut $ putStringUtf8 "🦝"
72### Serialize an `Array Int`
73### Serialize an `Array Int`
@@ -85,7 +86,7 @@ import Data.Array (length)
86putArrayInt32 :: forall m. MonadEffect m => Array Int -> PutM m Unit
87putArrayInt32 :: forall m. MonadEffect m => Array Int -> PutM m Unit
87putArrayInt32 xs = do
88putArrayInt32 xs = do
88− -- Put a 64-bit big-endian length prefix for the length of the array.
89+ -- Put a 64-bit big-endian length prefix for the array.
89 putInt32be 0
90 putInt32be 0
90 putInt32be $ length xs
91 putInt32be $ length xs
91 traverse_ putInt32be xs
92 traverse_ putInt32be xs
@@ -105,7 +106,7 @@ import Data.Array (foldRecM)
106putArrayInt32 :: forall m. MonadEffect m => MonadRec m => Array Int -> PutM m Unit
107putArrayInt32 :: forall m. MonadEffect m => MonadRec m => Array Int -> PutM m Unit
107putArrayInt32 xs = do
108putArrayInt32 xs = do
108− -- Put a 64-bit big-endian length prefix for the length of the array.
109+ -- Put a 64-bit big-endian length prefix for the array.
109 putInt32be 0
110 putInt32be 0
110 putInt32be $ length xs
111 putInt32be $ length xs
111 foldRecM (\_ x -> putInt32be x) unit xs
112 foldRecM (\_ x -> putInt32be x) unit xs