Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
language: node_js
dist: trusty
sudo: required
node_js: 6
node_js: stable
install:
- TAG=$(wget -q -O - https://github.com/purescript/purescript/releases/latest --server-response --max-redirect 0 2>&1 | sed -n -e 's/.*Location:.*tag\///p')
- wget -O $HOME/purescript.tar.gz https://github.com/purescript/purescript/releases/download/$TAG/linux64.tar.gz
- tar -xvf $HOME/purescript.tar.gz -C $HOME/
- chmod a+x $HOME/purescript
- npm install -g bower
- npm install
- bower install
script:
- bower install --production
- npm run -s build
- bower install
- npm -s test
after_success:
- >-
test $TRAVIS_TAG &&
Expand Down
10 changes: 5 additions & 5 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
"url": "git://github.com/purescript-node/purescript-node-buffer"
},
"dependencies": {
"purescript-eff": "^3.0.0",
"purescript-maybe": "^3.0.0",
"purescript-effect": "#compiler/0.12",
"purescript-maybe": "#compiler/0.12",
"purescript-arraybuffer-types": "^2.0.0"
},
"devDependencies": {
"purescript-assert": "^3.0.0",
"purescript-console": "^3.0.0",
"purescript-foldable-traversable": "^3.0.0"
"purescript-assert": "#compiler/0.12",
"purescript-console": "#compiler/0.12",
"purescript-foldable-traversable": "#compiler/0.12"
}
}
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
"private": true,
"scripts": {
"clean": "rimraf output && rimraf .pulp-cache",
"build": "pulp build -- --censor-lib --strict"
"build": "pulp build -- --censor-lib --strict",
"test": "pulp test"
},
"devDependencies": {
"pulp": "^11.0.0",
"purescript-psa": "^0.5.0",
"purescript": "^0.11.1",
"rimraf": "^2.6.1"
"pulp": "^12.2.0",
"purescript-psa": "^0.6.0",
"rimraf": "^2.6.2"
}
}
6 changes: 3 additions & 3 deletions src/Node/Buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ exports.showImpl = require('util').inspect;

exports.create = function (size) {
return function() {
return new Buffer(size);
return Buffer.alloc(size);
};
};

exports.fromArray = function (octets) {
return function() {
return new Buffer(octets);
return Buffer.from(octets);
};
};

exports.fromStringImpl = function (str) {
return function (encoding) {
return function() {
return new Buffer(str, encoding);
return Buffer.from(str, encoding);
};
};
};
Expand Down
58 changes: 26 additions & 32 deletions src/Node/Buffer.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module Node.Buffer
( Octet()
, Offset()
, Buffer()
, BUFFER()
, BufferValueType(..)
, create
, fromArray
Expand All @@ -26,7 +25,7 @@ module Node.Buffer

import Prelude

import Control.Monad.Eff (Eff, kind Effect)
import Effect (Effect)
import Data.ArrayBuffer.Types (ArrayBuffer)
import Data.Maybe (Maybe(..))
import Node.Encoding (Encoding, encodingToNode)
Expand All @@ -46,9 +45,6 @@ instance showBuffer :: Show Buffer where

foreign import showImpl :: Buffer -> String

-- | Effect for buffer creation, reading, or writing.
foreign import data BUFFER :: Effect

-- | Enumeration of the numeric types that can be written to a buffer.
data BufferValueType
= UInt8
Expand Down Expand Up @@ -83,88 +79,86 @@ instance showBufferValueType :: Show BufferValueType where
show DoubleBE = "DoubleBE"

-- | Creates a new buffer of the specified size.
foreign import create :: forall e. Int -> Eff (buffer :: BUFFER | e) Buffer
foreign import create :: Int -> Effect Buffer

-- | Creates a new buffer from an array of octets, sized to match the array.
foreign import fromArray :: forall e. Array Octet -> Eff (buffer :: BUFFER | e) Buffer
foreign import fromArray :: Array Octet -> Effect Buffer

-- | Creates a buffer view from a JS ArrayByffer without copying data.
--
-- Requires Node >= v5.10.0
foreign import fromArrayBuffer :: forall e. ArrayBuffer -> Eff (buffer :: BUFFER | e) Buffer
foreign import fromArrayBuffer :: ArrayBuffer -> Effect Buffer

-- | Creates a new buffer from a string with the specified encoding, sized to
-- | match the string.
fromString :: forall e. String -> Encoding -> Eff (buffer :: BUFFER | e) Buffer
fromString :: String -> Encoding -> Effect Buffer
fromString str = fromStringImpl str <<< encodingToNode

foreign import fromStringImpl :: forall e. String -> String -> Eff (buffer :: BUFFER | e) Buffer
foreign import fromStringImpl :: String -> String -> Effect Buffer

foreign import toArrayBuffer :: forall e. Buffer -> Eff (buffer :: BUFFER | e) ArrayBuffer
foreign import toArrayBuffer :: Buffer -> Effect ArrayBuffer

-- | Reads a numeric value from a buffer at the specified offset.
read :: forall e. BufferValueType -> Offset -> Buffer -> Eff (buffer :: BUFFER | e) Int
read :: BufferValueType -> Offset -> Buffer -> Effect Int
read = readImpl <<< show

foreign import readImpl :: forall e. String -> Offset -> Buffer -> Eff (buffer :: BUFFER | e) Int
foreign import readImpl :: String -> Offset -> Buffer -> Effect Int

-- | Reads a section of a buffer as a string with the specified encoding.
readString :: forall e. Encoding -> Offset -> Offset -> Buffer -> Eff (buffer :: BUFFER | e) String
readString :: Encoding -> Offset -> Offset -> Buffer -> Effect String
readString = readStringImpl <<< encodingToNode

foreign import readStringImpl ::
forall e. String -> Offset -> Offset -> Buffer -> Eff (buffer :: BUFFER | e) String
String -> Offset -> Offset -> Buffer -> Effect String

-- | Reads the buffer as a string with the specified encoding.
toString :: forall e. Encoding -> Buffer -> Eff (buffer :: BUFFER | e) String
toString :: Encoding -> Buffer -> Effect String
toString = toStringImpl <<< encodingToNode

foreign import toStringImpl :: forall e. String -> Buffer -> Eff (buffer :: BUFFER | e) String
foreign import toStringImpl :: String -> Buffer -> Effect String

-- | Writes a numeric value to a buffer at the specified offset.
write :: forall e. BufferValueType -> Int -> Offset -> Buffer -> Eff (buffer :: BUFFER | e) Unit
write :: BufferValueType -> Int -> Offset -> Buffer -> Effect Unit
write = writeImpl <<< show

foreign import writeImpl ::
forall e. String -> Int -> Offset -> Buffer -> Eff (buffer :: BUFFER | e) Unit
foreign import writeImpl :: String -> Int -> Offset -> Buffer -> Effect Unit

-- | Writes octets from a string to a buffer at the specified offset. Multi-byte
-- | characters will not be written to the buffer if there is not enough capacity
-- | to write them fully. The number of bytes written is returned.
writeString :: forall e. Encoding -> Offset -> Int -> String -> Buffer -> Eff (buffer :: BUFFER | e) Int
writeString :: Encoding -> Offset -> Int -> String -> Buffer -> Effect Int
writeString = writeStringImpl <<< encodingToNode

foreign import writeStringImpl ::
forall e. String -> Offset -> Int -> String -> Buffer -> Eff (buffer :: BUFFER | e) Int
String -> Offset -> Int -> String -> Buffer -> Effect Int

-- | Creates an array of octets from a buffer's contents.
foreign import toArray :: forall e. Buffer -> Eff (buffer :: BUFFER | e) (Array Octet)
foreign import toArray :: Buffer -> Effect (Array Octet)

-- | Reads an octet from a buffer at the specified offset.
getAtOffset :: forall e. Offset -> Buffer -> Eff (buffer :: BUFFER | e) (Maybe Octet)
getAtOffset :: Offset -> Buffer -> Effect (Maybe Octet)
getAtOffset = getAtOffsetImpl Just Nothing

foreign import getAtOffsetImpl ::
forall e. (Octet -> Maybe Octet) -> Maybe Octet -> Offset -> Buffer -> Eff (buffer :: BUFFER | e) (Maybe Octet)
(Octet -> Maybe Octet) -> Maybe Octet -> Offset -> Buffer -> Effect (Maybe Octet)

-- | Writes an octet in the buffer at the specified offset.
foreign import setAtOffset ::
forall e. Octet -> Offset -> Buffer -> Eff (buffer :: BUFFER | e) Unit
foreign import setAtOffset :: Octet -> Offset -> Buffer -> Effect Unit

-- | Returns the size of a buffer.
foreign import size :: forall e. Buffer -> Eff (buffer :: BUFFER | e) Int
foreign import size :: Buffer -> Effect Int

-- | Concatenates a list of buffers.
foreign import concat :: forall e. Array Buffer -> Eff (buffer :: BUFFER | e) Buffer
foreign import concat :: Array Buffer -> Effect Buffer

-- | Concatenates a list of buffers, combining them into a new buffer of the
-- | specified length.
foreign import concat' :: forall e. Array Buffer -> Int -> Eff (buffer :: BUFFER | e) Buffer
foreign import concat' :: Array Buffer -> Int -> Effect Buffer

-- | Copies a section of a source buffer into a target buffer at the specified
-- | offset, and returns the number of octets copied.
foreign import copy :: forall e. Offset -> Offset -> Buffer -> Offset -> Buffer -> Eff (buffer :: BUFFER | e) Int
foreign import copy :: Offset -> Offset -> Buffer -> Offset -> Buffer -> Effect Int

-- | Fills a range in a buffer with the specified octet.
foreign import fill ::
forall e. Octet -> Offset -> Offset -> Buffer -> Eff (buffer :: BUFFER | e) Unit
Octet -> Offset -> Offset -> Buffer -> Effect Unit
34 changes: 16 additions & 18 deletions test/Main.purs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
module Test.Main where

import Prelude
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (log, CONSOLE())
import Effect (Effect)
import Effect.Console (log)
import Data.Maybe (Maybe(..))
import Data.Traversable (traverse)
import Node.Buffer (BUFFER, BufferValueType(..), toArray, concat', fromArray, fill, copy, readString, fromString, toString, read, write, create, getAtOffset)
import Node.Buffer (BufferValueType(..), toArray, concat', fromArray, fill, copy, readString, fromString, toString, read, write, create, getAtOffset)
import Node.Encoding (Encoding(..))
import Test.Assert (ASSERT, assert')
import Test.Assert (assert')

type Test = forall e. Eff (assert :: ASSERT, buffer :: BUFFER, console :: CONSOLE | e) Unit

main :: Test
main :: Effect Unit
main = do
log "Testing..."

Expand Down Expand Up @@ -45,7 +43,7 @@ main = do
log "getAtOffset"
testGetAtOffset

testReadWrite :: Test
testReadWrite :: Effect Unit
testReadWrite = do
buf <- create 1
let val = 42
Expand All @@ -54,14 +52,14 @@ testReadWrite = do

assertEq val readVal

testFromArray :: Test
testFromArray :: Effect Unit
testFromArray = do
buf <- fromArray [1,2,3,4,5]
readVal <- read UInt8 2 buf

assertEq 3 readVal

testToArray :: Test
testToArray :: Effect Unit
testToArray = do
let val = [1,2,67,3,3,7,8,3,4,237]

Expand All @@ -70,7 +68,7 @@ testToArray = do

assertEq val valOut

testFromString :: Test
testFromString :: Effect Unit
testFromString = do
let str = "hello, world"

Expand All @@ -79,7 +77,7 @@ testFromString = do

assertEq val 32 -- ASCII space

testToString :: Test
testToString :: Effect Unit
testToString = do
let str = "hello, world"

Expand All @@ -88,7 +86,7 @@ testToString = do

assertEq str strOut

testReadString :: Test
testReadString :: Effect Unit
testReadString = do
let str = "hello, world"

Expand All @@ -97,7 +95,7 @@ testReadString = do

assertEq "world" strOut

testCopy :: Test
testCopy :: Effect Unit
testCopy = do
buf1 <- fromArray [1,2,3,4,5]
buf2 <- fromArray [10,9,8,7,6]
Expand All @@ -108,30 +106,30 @@ testCopy = do
assertEq copied 3
assertEq out [10,9,1,2,3]

testFill :: Test
testFill :: Effect Unit
testFill = do
buf <- fromArray [1,1,1,1,1]
fill 42 2 4 buf
out <- toArray buf

assertEq [1,1,42,42,1] out

testConcat' :: Test
testConcat' :: Effect Unit
testConcat' = do
bufs <- traverse fromArray $ map (\x -> [x, x+1, x+2]) [0,3,6,9,12]
buf <- concat' bufs 15
out <- toArray buf

assertEq [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14] out

testGetAtOffset :: Test
testGetAtOffset :: Effect Unit
testGetAtOffset = do
buf <- fromArray [1, 2, 3, 4]
assertEq (Just 2) =<< getAtOffset 1 buf
assertEq Nothing =<< getAtOffset 4 buf
assertEq Nothing =<< getAtOffset (-1) buf

assertEq :: forall a. Eq a => Show a => a -> a -> Test
assertEq :: forall a. Eq a => Show a => a -> a -> Effect Unit
assertEq x y =
if x == y
then pure unit
Expand Down