|
| 1 | +module Node.Buffer |
| 2 | + ( Octet() |
| 3 | + , Offset() |
| 4 | + , Buffer() |
| 5 | + , BufferWrite() |
| 6 | + , BufferValueType(..) |
| 7 | + , create |
| 8 | + , fromArray |
| 9 | + , fromString |
| 10 | + , read |
| 11 | + , readString |
| 12 | + , toString |
| 13 | + , write |
| 14 | + , writeString |
| 15 | + , toArray |
| 16 | + , getAtOffset |
| 17 | + , setAtOffset |
| 18 | + , size |
| 19 | + , concat |
| 20 | + , concat' |
| 21 | + , copy |
| 22 | + , fill |
| 23 | + ) where |
| 24 | + |
| 25 | +import Control.Monad.Eff |
| 26 | +import Data.Maybe |
| 27 | +import Node.Encoding |
| 28 | + |
| 29 | +-- | |
| 30 | +-- Type synonym indicating the value should be an octet (0-255). If the value |
| 31 | +-- provided is outside this range it will be used as modulo 255. |
| 32 | +-- |
| 33 | +type Octet = Number |
| 34 | + |
| 35 | +-- | |
| 36 | +-- Type synonym indicating the value refers to an offset in a buffer. |
| 37 | +-- |
| 38 | +type Offset = Number |
| 39 | + |
| 40 | +-- | |
| 41 | +-- An instance of Node's Buffer class. |
| 42 | +-- |
| 43 | +foreign import data Buffer :: * |
| 44 | + |
| 45 | +instance showBuffer :: Show Buffer where |
| 46 | + show = showImpl |
| 47 | + |
| 48 | +foreign import showImpl "var showImpl = require('util').inspect;" :: Buffer -> String |
| 49 | + |
| 50 | +-- | |
| 51 | +-- Effect for buffer modification. |
| 52 | +-- |
| 53 | +foreign import data BufferWrite :: ! |
| 54 | + |
| 55 | +-- | |
| 56 | +-- Enumeration of the numeric types that can be written to a buffer. |
| 57 | +-- |
| 58 | +data BufferValueType |
| 59 | + = UInt8 |
| 60 | + | UInt16LE |
| 61 | + | UInt16BE |
| 62 | + | UInt32LE |
| 63 | + | UInt32BE |
| 64 | + | Int8 |
| 65 | + | Int16LE |
| 66 | + | Int16BE |
| 67 | + | Int32LE |
| 68 | + | Int32BE |
| 69 | + | FloatLE |
| 70 | + | FloatBE |
| 71 | + | DoubleLE |
| 72 | + | DoubleBE |
| 73 | + |
| 74 | +instance showBufferValueType :: Show BufferValueType where |
| 75 | + show UInt8 = "UInt8" |
| 76 | + show UInt16LE = "UInt16LE" |
| 77 | + show UInt16BE = "UInt16BE" |
| 78 | + show UInt32LE = "UInt32LE" |
| 79 | + show UInt32BE = "UInt32BE" |
| 80 | + show Int8 = "Int8" |
| 81 | + show Int16LE = "Int16LE" |
| 82 | + show Int16BE = "Int16BE" |
| 83 | + show Int32LE = "Int32LE" |
| 84 | + show Int32BE = "Int32BE" |
| 85 | + show FloatLE = "FloatLE" |
| 86 | + show FloatBE = "FloatBE" |
| 87 | + show DoubleLE = "DoubleLE" |
| 88 | + show DoubleBE = "DoubleBE" |
| 89 | + |
| 90 | +-- | |
| 91 | +-- Creates a new buffer of the specified size. |
| 92 | +-- |
| 93 | +foreign import create |
| 94 | + "function create (size) { \ |
| 95 | + \ return new Buffer(size); \ |
| 96 | + \}" :: Number -> Buffer |
| 97 | + |
| 98 | +-- | |
| 99 | +-- Creates a new buffer from an array of octets, sized to match the array. |
| 100 | +-- |
| 101 | +foreign import fromArray |
| 102 | + "function fromArray (octets) { \ |
| 103 | + \ return new Buffer(octets); \ |
| 104 | + \}" :: [Octet] -> Buffer |
| 105 | + |
| 106 | +-- | |
| 107 | +-- Creates a new buffer from a string with the specified encoding, sized to |
| 108 | +-- match the string. |
| 109 | +-- |
| 110 | +fromString :: String -> Encoding -> Buffer |
| 111 | +fromString str = fromStringImpl str <<< show |
| 112 | + |
| 113 | +foreign import fromStringImpl |
| 114 | + "function fromString (str) { \ |
| 115 | + \ return function (encoding) { \ |
| 116 | + \ return new Buffer(str, encoding); \ |
| 117 | + \ }; \ |
| 118 | + \}" :: String -> String -> Buffer |
| 119 | + |
| 120 | +-- | |
| 121 | +-- Reads a numeric value from a buffer at the specified offset. |
| 122 | +-- |
| 123 | +read :: BufferValueType -> Offset -> Buffer -> Number |
| 124 | +read = readImpl <<< show |
| 125 | + |
| 126 | +foreign import readImpl |
| 127 | + "function readImpl (ty) { \ |
| 128 | + \ return function (offset) { \ |
| 129 | + \ return function (buf) { \ |
| 130 | + \ return buf['read' + ty](offset); \ |
| 131 | + \ }; \ |
| 132 | + \ }; \ |
| 133 | + \}" :: String -> Offset -> Buffer -> Number |
| 134 | + |
| 135 | + |
| 136 | +-- | |
| 137 | +-- Reads a section of a buffer as a string with the specified encoding. |
| 138 | +-- |
| 139 | +readString :: forall e. Encoding -> Offset -> Offset -> Buffer -> String |
| 140 | +readString = readStringImpl <<< show |
| 141 | + |
| 142 | +foreign import readStringImpl |
| 143 | + "function readStringImpl (enc) { \ |
| 144 | + \ return function (start) { \ |
| 145 | + \ return function (end) { \ |
| 146 | + \ return function (buff) { \ |
| 147 | + \ return buff.toString(enc, start, end); \ |
| 148 | + \ } \ |
| 149 | + \ } \ |
| 150 | + \ } \ |
| 151 | + \}":: String -> Offset -> Offset -> Buffer -> String |
| 152 | + |
| 153 | +-- | |
| 154 | +-- Reads the buffer as a string with the specified encoding. |
| 155 | +-- |
| 156 | +toString :: forall e. Encoding -> Buffer -> String |
| 157 | +toString = toStringImpl <<< show |
| 158 | + |
| 159 | +foreign import toStringImpl |
| 160 | + "function toStringImpl (enc) { \ |
| 161 | + \ return function (buff) { \ |
| 162 | + \ return buff.toString(enc); \ |
| 163 | + \ } \ |
| 164 | + \}":: String -> Buffer -> String |
| 165 | + |
| 166 | +-- | |
| 167 | +-- Writes a numeric value to a buffer at the specified offset. |
| 168 | +-- |
| 169 | +write :: forall e. BufferValueType -> Number -> Offset -> Buffer -> Eff (buffer :: BufferWrite | e) {} |
| 170 | +write = writeImpl <<< show |
| 171 | + |
| 172 | +foreign import writeImpl |
| 173 | + "function writeImpl (ty) { \ |
| 174 | + \ return function (value) { \ |
| 175 | + \ return function (offset) { \ |
| 176 | + \ return function (buf) { \ |
| 177 | + \ buf['write' + ty](value, offset); \ |
| 178 | + \ return {}; \ |
| 179 | + \ }; \ |
| 180 | + \ }; \ |
| 181 | + \ }; \ |
| 182 | + \}" :: forall e. String -> Number -> Offset -> Buffer -> Eff (buffer :: BufferWrite | e) {} |
| 183 | + |
| 184 | +-- | |
| 185 | +-- Writes octets from a string to a buffer at the specified offset. Multi-byte |
| 186 | +-- characters will not be written to the buffer if there is not enough capacity |
| 187 | +-- to write them fully. The number of bytes written is returned. |
| 188 | +-- |
| 189 | +writeString :: forall e. Encoding -> Offset -> Number -> String -> Buffer -> Eff (buffer :: BufferWrite | e) Number |
| 190 | +writeString = writeStringImpl <<< show |
| 191 | + |
| 192 | +foreign import writeStringImpl |
| 193 | + "function writeStringImpl (enc) { \ |
| 194 | + \ return function (offset) { \ |
| 195 | + \ return function (length) { \ |
| 196 | + \ return function (value) { \ |
| 197 | + \ return function (buff) { \ |
| 198 | + \ return buff.write(value, offset, length, encoding); \ |
| 199 | + \ }; \ |
| 200 | + \ }; \ |
| 201 | + \ }; \ |
| 202 | + \ }; \ |
| 203 | + \}" :: forall e. String -> Offset -> Number -> String -> Buffer -> Eff (buffer :: BufferWrite | e) Number |
| 204 | + |
| 205 | +-- | |
| 206 | +-- Creates an array of octets from a buffer's contents. |
| 207 | +-- |
| 208 | +foreign import toArray |
| 209 | + "function toArray (buff) { \ |
| 210 | + \ return buff.toJSON(); \ |
| 211 | + \}" :: Buffer -> [Octet] |
| 212 | + |
| 213 | +-- | |
| 214 | +-- Reads an octet from a buffer at the specified offset. |
| 215 | +-- |
| 216 | +foreign import getAtOffset |
| 217 | + "function getAtOffset (buff) { \ |
| 218 | + \ return function (offset) { \ |
| 219 | + \ var octet = buff[offset]; \ |
| 220 | + \ return octet == null ? _ps.Data_Maybe.Nothing \ |
| 221 | + \ : _ps.Data_Maybe.Just(buff[i]); \ |
| 222 | + \ } \ |
| 223 | + \}" :: Offset -> Buffer -> Maybe Octet |
| 224 | + |
| 225 | +-- | |
| 226 | +-- Writes an octet in the buffer at the specified offset. |
| 227 | +-- |
| 228 | +foreign import setAtOffset |
| 229 | + "function setAtOffset (value) { \ |
| 230 | + \ return function (offset) { \ |
| 231 | + \ return function (buff) { \ |
| 232 | + \ buff[offset] = value; \ |
| 233 | + \ return {}; \ |
| 234 | + \ } \ |
| 235 | + \ } \ |
| 236 | + \}" :: forall e. Octet -> Offset -> Buffer -> Eff (buffer :: BufferWrite | e) {} |
| 237 | + |
| 238 | +-- | |
| 239 | +-- Returns the size of a buffer. |
| 240 | +-- |
| 241 | +foreign import size |
| 242 | + "function size (buff) { \ |
| 243 | + \ return buff.length; \ |
| 244 | + \}" :: forall e. Buffer -> Number |
| 245 | + |
| 246 | +-- | |
| 247 | +-- Concatenates a list of buffers. |
| 248 | +-- |
| 249 | +foreign import concat |
| 250 | + "function concat (buffs) { \ |
| 251 | + \ return Buffer.concat(buffs); \ |
| 252 | + \}" :: [Buffer] -> Buffer |
| 253 | + |
| 254 | +-- | |
| 255 | +-- Concatenates a list of buffers, combining them into a new buffer of the |
| 256 | +-- specified length. |
| 257 | +-- |
| 258 | +foreign import concat' |
| 259 | + "function concat$prime (buffs) { \ |
| 260 | + \ return function (totalLength) { \ |
| 261 | + \ return Buffer.concat(buffs, totalLength); \ |
| 262 | + \ } \ |
| 263 | + \}" :: [Buffer] -> Number -> Buffer |
| 264 | + |
| 265 | +-- | |
| 266 | +-- Copies a section of a source buffer into a target buffer at the specified |
| 267 | +-- offset. |
| 268 | +-- |
| 269 | +foreign import copy |
| 270 | + "function copy (srcStart) { \ |
| 271 | + \ return function (srcEnd) { \ |
| 272 | + \ return function (src) { \ |
| 273 | + \ return function (targStart) { \ |
| 274 | + \ return function (targ) { \ |
| 275 | + \ return src.copy(targ, targStart, srcStart, strcEnd); \ |
| 276 | + \ } \ |
| 277 | + \ } \ |
| 278 | + \ } \ |
| 279 | + \ } \ |
| 280 | + \}" :: Offset -> Offset -> Buffer -> Offset -> Buffer -> Buffer |
| 281 | + |
| 282 | +-- | |
| 283 | +-- Fills a range in a buffer with the specified octet. |
| 284 | +-- |
| 285 | +foreign import fill |
| 286 | + "function fill (buff) { \ |
| 287 | + \ return function (octet) { \ |
| 288 | + \ return function (start) { \ |
| 289 | + \ return function (end) { \ |
| 290 | + \ buff.fill(octet, start, end); \ |
| 291 | + \ return {}; \ |
| 292 | + \ } \ |
| 293 | + \ } \ |
| 294 | + \ } \ |
| 295 | + \}" :: forall e. Octet -> Offset -> Offset -> Buffer -> Eff (buffer :: BufferWrite | e) {} |
0 commit comments