@@ -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,26 @@ 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
57+import Effect.Class (class MonadEffect, liftEffect)
58+import Data.UInt (fromInt)
59+import Data.ArrayBuffer.Types (ArrayBuffer(..))
56−import Data.ArrayBuffer.Builder (PutM, putArrayBuffer, execPut)
60+import Data.ArrayBuffer.Builder (PutM, putArrayBuffer, execPut, putUint32be)
57import Data.ArrayBuffer.Typed (buffer)
61import Data.ArrayBuffer.Typed (buffer)
58−import Data.TextEncoding (encodeUtf8)
59import Data.ArrayBuffer.ArrayBuffer (byteLength)
62import Data.ArrayBuffer.ArrayBuffer (byteLength)
63+import Web.Encoding.TextEncoder (new, TextEncoder, encode)
61putStringUtf8 :: forall m. MonadEffect m => String -> PutM m Unit
65putStringUtf8 :: forall m. MonadEffect m => String -> PutM m Unit
62putStringUtf8 s = do
66putStringUtf8 s = do
67+ textEncoder <- liftEffect new
63− let stringbuf = buffer $ encodeUtf8 s
68+ let stringbuf = buffer $ encode s textEncoder
64− -- Put a 32-bit big-endian length prefix for the length of the utf8 string, in bytes.
69+ -- Put a 32-bit big-endian length for the utf8 string, in bytes.
65− putInt32be $ byteLength stringbuf
70+ putUint32be $ fromInt $ byteLength stringbuf
66 putArrayBuffer stringbuf
71 putArrayBuffer stringbuf
69− arraybuffer :: ArrayBuffer <- execPut $ putStringUtf8 "BLM"
73+arraybuffer :: ArrayBuffer <- execPut $ putStringUtf8 "🦝"
72### Serialize an `Array Int`
76### Serialize an `Array Int`
@@ -85,7 +89,7 @@ import Data.Array (length)
86putArrayInt32 :: forall m. MonadEffect m => Array Int -> PutM m Unit
90putArrayInt32 :: forall m. MonadEffect m => Array Int -> PutM m Unit
87putArrayInt32 xs = do
91putArrayInt32 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.
89 putInt32be 0
93 putInt32be 0
90 putInt32be $ length xs
94 putInt32be $ length xs
91 traverse_ putInt32be xs
95 traverse_ putInt32be xs
@@ -105,7 +109,7 @@ import Data.Array (foldRecM)
106putArrayInt32 :: forall m. MonadEffect m => MonadRec m => Array Int -> PutM m Unit
110putArrayInt32 :: forall m. MonadEffect m => MonadRec m => Array Int -> PutM m Unit
107putArrayInt32 xs = do
111putArrayInt32 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.
109 putInt32be 0
113 putInt32be 0
110 putInt32be $ length xs
114 putInt32be $ length xs
111 foldRecM (\_ x -> putInt32be x) unit xs
115 foldRecM (\_ x -> putInt32be x) unit xs
@@ -120,7 +124,7 @@ of the `PutM` builder expression are stack-safe.
121This package is only for writing `ArrayBuffer`s, not reading them.
125This package is only for writing `ArrayBuffer`s, not reading them.
123−[__purescript-parsing-dataview__](https://pursuit.purescript.org/packages/purescript-parsing-dataview/)
127+[__parsing-dataview__](https://pursuit.purescript.org/packages/purescript-parsing-dataview/)
124for a way to deserialize from `ArrayBuffer` back to Purescript data.
128for a way to deserialize from `ArrayBuffer` back to Purescript data.
126## Alternative packages for serializing to an `ArrayBuffer`
130## Alternative packages for serializing to an `ArrayBuffer`
@@ -129,6 +133,7 @@ for a way to deserialize from `ArrayBuffer` back to Purescript data.
129* [__node-buffer__](https://pursuit.purescript.org/packages/purescript-node-buffer)
133* [__node-buffer__](https://pursuit.purescript.org/packages/purescript-node-buffer)
130* [__arraybuffer-class__](https://pursuit.purescript.org/packages/purescript-arraybuffer-class)
134* [__arraybuffer-class__](https://pursuit.purescript.org/packages/purescript-arraybuffer-class)
131* [__bytestrings__](https://pursuit.purescript.org/packages/purescript-bytestrings/)
135* [__bytestrings__](https://pursuit.purescript.org/packages/purescript-bytestrings/)
136+* [__array-builder__](https://pursuit.purescript.org/packages/purescript-array-builder)
133## Development
138## Development