|
| 1 | +-- | Mutable buffers and associated operations. |
1 | 2 | module Node.Buffer |
2 | | - ( Octet() |
3 | | - , Offset() |
4 | | - , Buffer() |
5 | | - , BufferValueType(..) |
6 | | - , create |
7 | | - , fromArray |
8 | | - , fromString |
9 | | - , fromArrayBuffer |
10 | | - , toArrayBuffer |
11 | | - , read |
12 | | - , readString |
13 | | - , toString |
14 | | - , write |
15 | | - , writeString |
16 | | - , toArray |
17 | | - , getAtOffset |
18 | | - , setAtOffset |
19 | | - , size |
20 | | - , concat |
21 | | - , concat' |
22 | | - , copy |
23 | | - , fill |
| 3 | + ( Buffer |
| 4 | + , module TypesExports |
| 5 | + , module Class |
24 | 6 | ) where |
25 | 7 |
|
26 | | -import Prelude |
27 | | - |
28 | 8 | import Effect (Effect) |
29 | | -import Data.ArrayBuffer.Types (ArrayBuffer) |
30 | | -import Data.Maybe (Maybe(..)) |
31 | | -import Node.Encoding (Encoding, encodingToNode) |
32 | | - |
33 | | --- | Type synonym indicating the value should be an octet (0-255). If the value |
34 | | --- | provided is outside this range it will be used as modulo 256. |
35 | | -type Octet = Int |
| 9 | +import Node.Buffer.Class (class MutableBuffer) |
| 10 | +import Node.Buffer.Class (class MutableBuffer, concat, concat', copy, create, fill, freeze, fromArray, fromArrayBuffer, fromString, getAtOffset, read, readString, setAtOffset, size, slice, thaw, toArray, toArrayBuffer, toString, unsafeFreeze, unsafeThaw, write, writeString) as Class |
| 11 | +import Node.Buffer.Internal as Internal |
| 12 | +import Node.Buffer.Types (BufferValueType(..), Octet, Offset) as TypesExports |
36 | 13 |
|
37 | | --- | Type synonym indicating the value refers to an offset in a buffer. |
38 | | -type Offset = Int |
39 | | - |
40 | | --- | An instance of Node's Buffer class. |
| 14 | +-- | A reference to a mutable buffer for use with `Effect` |
41 | 15 | foreign import data Buffer :: Type |
42 | 16 |
|
43 | | -instance showBuffer :: Show Buffer where |
44 | | - show = showImpl |
45 | | - |
46 | | -foreign import showImpl :: Buffer -> String |
47 | | - |
48 | | --- | Enumeration of the numeric types that can be written to a buffer. |
49 | | -data BufferValueType |
50 | | - = UInt8 |
51 | | - | UInt16LE |
52 | | - | UInt16BE |
53 | | - | UInt32LE |
54 | | - | UInt32BE |
55 | | - | Int8 |
56 | | - | Int16LE |
57 | | - | Int16BE |
58 | | - | Int32LE |
59 | | - | Int32BE |
60 | | - | FloatLE |
61 | | - | FloatBE |
62 | | - | DoubleLE |
63 | | - | DoubleBE |
64 | | - |
65 | | -instance showBufferValueType :: Show BufferValueType where |
66 | | - show UInt8 = "UInt8" |
67 | | - show UInt16LE = "UInt16LE" |
68 | | - show UInt16BE = "UInt16BE" |
69 | | - show UInt32LE = "UInt32LE" |
70 | | - show UInt32BE = "UInt32BE" |
71 | | - show Int8 = "Int8" |
72 | | - show Int16LE = "Int16LE" |
73 | | - show Int16BE = "Int16BE" |
74 | | - show Int32LE = "Int32LE" |
75 | | - show Int32BE = "Int32BE" |
76 | | - show FloatLE = "FloatLE" |
77 | | - show FloatBE = "FloatBE" |
78 | | - show DoubleLE = "DoubleLE" |
79 | | - show DoubleBE = "DoubleBE" |
80 | | - |
81 | | --- | Creates a new buffer of the specified size. |
82 | | -foreign import create :: Int -> Effect Buffer |
83 | | - |
84 | | --- | Creates a new buffer from an array of octets, sized to match the array. |
85 | | -foreign import fromArray :: Array Octet -> Effect Buffer |
86 | | - |
87 | | --- | Creates a buffer view from a JS ArrayByffer without copying data. |
88 | | --- |
89 | | --- Requires Node >= v5.10.0 |
90 | | -foreign import fromArrayBuffer :: ArrayBuffer -> Effect Buffer |
91 | | - |
92 | | --- | Creates a new buffer from a string with the specified encoding, sized to |
93 | | --- | match the string. |
94 | | -fromString :: String -> Encoding -> Effect Buffer |
95 | | -fromString str = fromStringImpl str <<< encodingToNode |
96 | | - |
97 | | -foreign import fromStringImpl :: String -> String -> Effect Buffer |
98 | | - |
99 | | -foreign import toArrayBuffer :: Buffer -> Effect ArrayBuffer |
100 | | - |
101 | | --- | Reads a numeric value from a buffer at the specified offset. |
102 | | -read :: BufferValueType -> Offset -> Buffer -> Effect Int |
103 | | -read = readImpl <<< show |
104 | | - |
105 | | -foreign import readImpl :: String -> Offset -> Buffer -> Effect Int |
106 | | - |
107 | | --- | Reads a section of a buffer as a string with the specified encoding. |
108 | | -readString :: Encoding -> Offset -> Offset -> Buffer -> Effect String |
109 | | -readString = readStringImpl <<< encodingToNode |
110 | | - |
111 | | -foreign import readStringImpl :: |
112 | | - String -> Offset -> Offset -> Buffer -> Effect String |
113 | | - |
114 | | --- | Reads the buffer as a string with the specified encoding. |
115 | | -toString :: Encoding -> Buffer -> Effect String |
116 | | -toString = toStringImpl <<< encodingToNode |
117 | | - |
118 | | -foreign import toStringImpl :: String -> Buffer -> Effect String |
119 | | - |
120 | | --- | Writes a numeric value to a buffer at the specified offset. |
121 | | -write :: BufferValueType -> Int -> Offset -> Buffer -> Effect Unit |
122 | | -write = writeImpl <<< show |
123 | | - |
124 | | -foreign import writeImpl :: String -> Int -> Offset -> Buffer -> Effect Unit |
125 | | - |
126 | | --- | Writes octets from a string to a buffer at the specified offset. Multi-byte |
127 | | --- | characters will not be written to the buffer if there is not enough capacity |
128 | | --- | to write them fully. The number of bytes written is returned. |
129 | | -writeString :: Encoding -> Offset -> Int -> String -> Buffer -> Effect Int |
130 | | -writeString = writeStringImpl <<< encodingToNode |
131 | | - |
132 | | -foreign import writeStringImpl :: |
133 | | - String -> Offset -> Int -> String -> Buffer -> Effect Int |
134 | | - |
135 | | --- | Creates an array of octets from a buffer's contents. |
136 | | -foreign import toArray :: Buffer -> Effect (Array Octet) |
137 | | - |
138 | | --- | Reads an octet from a buffer at the specified offset. |
139 | | -getAtOffset :: Offset -> Buffer -> Effect (Maybe Octet) |
140 | | -getAtOffset = getAtOffsetImpl Just Nothing |
141 | | - |
142 | | -foreign import getAtOffsetImpl :: |
143 | | - (Octet -> Maybe Octet) -> Maybe Octet -> Offset -> Buffer -> Effect (Maybe Octet) |
144 | | - |
145 | | --- | Writes an octet in the buffer at the specified offset. |
146 | | -foreign import setAtOffset :: Octet -> Offset -> Buffer -> Effect Unit |
147 | | - |
148 | | --- | Returns the size of a buffer. |
149 | | -foreign import size :: Buffer -> Effect Int |
150 | | - |
151 | | --- | Concatenates a list of buffers. |
152 | | -foreign import concat :: Array Buffer -> Effect Buffer |
153 | | - |
154 | | --- | Concatenates a list of buffers, combining them into a new buffer of the |
155 | | --- | specified length. |
156 | | -foreign import concat' :: Array Buffer -> Int -> Effect Buffer |
157 | | - |
158 | | --- | Copies a section of a source buffer into a target buffer at the specified |
159 | | --- | offset, and returns the number of octets copied. |
160 | | -foreign import copy :: Offset -> Offset -> Buffer -> Offset -> Buffer -> Effect Int |
161 | | - |
162 | | --- | Fills a range in a buffer with the specified octet. |
163 | | -foreign import fill :: |
164 | | - Octet -> Offset -> Offset -> Buffer -> Effect Unit |
| 17 | +instance mutableBufferEffect :: MutableBuffer Buffer Effect where |
| 18 | + create = Internal.create |
| 19 | + freeze = Internal.copyAll |
| 20 | + unsafeFreeze = Internal.unsafeFreeze |
| 21 | + thaw = Internal.copyAll |
| 22 | + unsafeThaw = Internal.unsafeThaw |
| 23 | + fromArray = Internal.fromArray |
| 24 | + fromString = Internal.fromString |
| 25 | + fromArrayBuffer = Internal.fromArrayBuffer |
| 26 | + toArrayBuffer = Internal.toArrayBuffer |
| 27 | + read = Internal.read |
| 28 | + readString = Internal.readString |
| 29 | + toString = Internal.toString |
| 30 | + write = Internal.write |
| 31 | + writeString = Internal.writeString |
| 32 | + toArray = Internal.toArray |
| 33 | + getAtOffset = Internal.getAtOffset |
| 34 | + setAtOffset = Internal.setAtOffset |
| 35 | + slice = Internal.slice |
| 36 | + size = Internal.size |
| 37 | + concat = Internal.concat |
| 38 | + concat' = Internal.concat' |
| 39 | + copy = Internal.copy |
| 40 | + fill = Internal.fill |
0 commit comments