Skip to content

Commit c248be2

Browse files
committed
Initial commit
1 parent 9b6a2df commit c248be2

14 files changed

+420
-1
lines changed

.eslintrc.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"parserOptions": {
3+
"ecmaVersion": 5
4+
},
5+
"extends": "eslint:recommended",
6+
"env": {
7+
"commonjs": true,
8+
"browser": true
9+
},
10+
"rules": {
11+
"strict": [2, "global"],
12+
"block-scoped-var": 2,
13+
"consistent-return": 2,
14+
"eqeqeq": [2, "smart"],
15+
"guard-for-in": 2,
16+
"no-caller": 2,
17+
"no-extend-native": 2,
18+
"no-loop-func": 2,
19+
"no-new": 2,
20+
"no-param-reassign": 2,
21+
"no-return-assign": 2,
22+
"no-unused-expressions": 2,
23+
"no-use-before-define": 2,
24+
"radix": [2, "always"],
25+
"indent": [2, 2],
26+
"quotes": [2, "double"],
27+
"semi": [2, "always"]
28+
}
29+
}

.travis.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
language: node_js
2+
dist: trusty
3+
sudo: required
4+
node_js: stable
5+
install:
6+
- npm install -g bower
7+
- npm install
8+
- bower install
9+
script:
10+
- npm run -s build
11+
after_success:
12+
- >-
13+
test $TRAVIS_TAG &&
14+
echo $GITHUB_TOKEN | pulp login &&
15+
echo y | pulp publish --no-push

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# purescript-web-socket
1+
# purescript-web-socket

bower.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "purescript-web-socket",
3+
"homepage": "https://github.com/purescript-web/purescript-web-socket",
4+
"license": "MIT",
5+
"repository": {
6+
"type": "git",
7+
"url": "git://github.com/purescript-web/purescript-web-socket.git"
8+
},
9+
"ignore": [
10+
"**/.*",
11+
"bower_components",
12+
"node_modules",
13+
"output",
14+
"bower.json",
15+
"package.json"
16+
],
17+
"dependencies": {
18+
"purescript-arraybuffer-types": "^2.0.0",
19+
"purescript-web-file": "#compiler/0.12"
20+
}
21+
}

package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"private": true,
3+
"scripts": {
4+
"clean": "rimraf output && rimraf .pulp-cache",
5+
"build": "eslint src && pulp build -- --censor-lib --strict",
6+
"test": "pulp test --runtime phantomjs"
7+
},
8+
"devDependencies": {
9+
"eslint": "^3.19.0",
10+
"phantomjs-prebuilt": "^2.1.14",
11+
"pulp": "^11.0.0",
12+
"purescript-psa": "^0.5.0",
13+
"purescript": "^0.11.1",
14+
"rimraf": "^2.6.1"
15+
}
16+
}

src/Web/WebSocket/BinaryType.purs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
module Web.WebSocket.BinaryType where
2+
3+
import Prelude
4+
import Data.Enum (Cardinality(..), class BoundedEnum, defaultPred, defaultSucc, class Enum)
5+
import Data.Maybe (Maybe(..))
6+
7+
data BinaryType
8+
= Blob
9+
| ArrayBuffer
10+
11+
derive instance eqBinaryType :: Eq BinaryType
12+
derive instance ordBinaryType :: Ord BinaryType
13+
14+
instance boundedBinaryType :: Bounded BinaryType where
15+
bottom = Blob
16+
top = ArrayBuffer
17+
18+
instance enumBinaryType :: Enum BinaryType where
19+
succ = defaultSucc toEnumBinaryType fromEnumBinaryType
20+
pred = defaultPred toEnumBinaryType fromEnumBinaryType
21+
22+
instance boundedEnumBinaryType :: BoundedEnum BinaryType where
23+
cardinality = Cardinality 2
24+
toEnum = toEnumBinaryType
25+
fromEnum = fromEnumBinaryType
26+
27+
instance showBinaryType :: Show BinaryType where
28+
show Blob = "Blob"
29+
show ArrayBuffer = "ArrayBuffer"
30+
31+
toEnumBinaryType :: Int -> Maybe BinaryType
32+
toEnumBinaryType =
33+
case _ of
34+
0 -> Just Blob
35+
1 -> Just ArrayBuffer
36+
_ -> Nothing
37+
38+
fromEnumBinaryType :: BinaryType -> Int
39+
fromEnumBinaryType =
40+
case _ of
41+
Blob -> 0
42+
ArrayBuffer -> 1
43+
44+
printBinaryType :: BinaryType -> String
45+
printBinaryType =
46+
case _ of
47+
Blob -> "blob"
48+
ArrayBuffer -> "arraybuffer"

src/Web/WebSocket/Event/CloseEvent.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"use strict";
2+
3+
exports.code = function (e) {
4+
return e.code;
5+
};
6+
7+
exports.reason = function (e) {
8+
return e.reason;
9+
};
10+
11+
exports.wasClean = function (e) {
12+
return e.wasClean;
13+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module Web.WebSocket.Event.CloseEvent where
2+
3+
import Data.Maybe (Maybe)
4+
import Unsafe.Coerce (unsafeCoerce)
5+
import Web.Event.Event (Event)
6+
import Web.Internal.FFI (unsafeReadProtoTagged)
7+
8+
foreign import data CloseEvent :: Type
9+
10+
fromEvent :: Event -> Maybe CloseEvent
11+
fromEvent = unsafeReadProtoTagged "CloseEvent"
12+
13+
toEvent :: CloseEvent -> Event
14+
toEvent = unsafeCoerce
15+
16+
foreign import code :: CloseEvent -> Int
17+
18+
foreign import reason :: CloseEvent -> String
19+
20+
foreign import wasClean :: CloseEvent -> Boolean
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module Web.WebSocket.Event.EventTypes where
2+
3+
import Web.Event.Event (EventType(..))
4+
5+
onOpen :: EventType
6+
onOpen = EventType "open"
7+
8+
onMessage :: EventType
9+
onMessage = EventType "message"
10+
11+
onError :: EventType
12+
onError = EventType "error"
13+
14+
onClose :: EventType
15+
onClose = EventType "close"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"use strict";
2+
3+
exports.data_ = function (e) {
4+
return e.data;
5+
};
6+
7+
exports.origin = function (e) {
8+
return e.origin;
9+
};
10+
11+
exports.lastEventId = function (e) {
12+
return e.lastEventId;
13+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module Web.WebSocket.Event.MessageEvent where
2+
3+
import Data.Maybe (Maybe)
4+
import Foreign (Foreign)
5+
import Unsafe.Coerce (unsafeCoerce)
6+
import Web.Event.Event (Event)
7+
import Web.Internal.FFI (unsafeReadProtoTagged)
8+
9+
foreign import data MessageEvent :: Type
10+
11+
fromEvent :: Event -> Maybe MessageEvent
12+
fromEvent = unsafeReadProtoTagged "MessageEvent"
13+
14+
toEvent :: MessageEvent -> Event
15+
toEvent = unsafeCoerce
16+
17+
foreign import data_ :: MessageEvent -> Foreign
18+
19+
foreign import origin :: MessageEvent -> String
20+
21+
foreign import lastEventId :: MessageEvent -> String

src/Web/WebSocket/ReadyState.purs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
module Web.WebSocket.ReadyState where
2+
3+
import Prelude
4+
import Data.Enum (Cardinality(..), class BoundedEnum, defaultPred, defaultSucc, class Enum)
5+
import Data.Maybe (Maybe(..))
6+
7+
data ReadyState
8+
= Connecting
9+
| Open
10+
| Closing
11+
| Closed
12+
13+
derive instance eqReadyState :: Eq ReadyState
14+
derive instance ordReadyState :: Ord ReadyState
15+
16+
instance boundedReadyState :: Bounded ReadyState where
17+
bottom = Connecting
18+
top = Closed
19+
20+
instance enumReadyState :: Enum ReadyState where
21+
succ = defaultSucc toEnumReadyState fromEnumReadyState
22+
pred = defaultPred toEnumReadyState fromEnumReadyState
23+
24+
instance boundedEnumReadyState :: BoundedEnum ReadyState where
25+
cardinality = Cardinality 4
26+
toEnum = toEnumReadyState
27+
fromEnum = fromEnumReadyState
28+
29+
instance showReadyState :: Show ReadyState where
30+
show Connecting = "Connecting"
31+
show Open = "Open"
32+
show Closing = "Closing"
33+
show Closed = "Closed"
34+
35+
toEnumReadyState :: Int -> Maybe ReadyState
36+
toEnumReadyState =
37+
case _ of
38+
0 -> Just Connecting
39+
1 -> Just Open
40+
2 -> Just Closing
41+
3 -> Just Closed
42+
_ -> Nothing
43+
44+
fromEnumReadyState :: ReadyState -> Int
45+
fromEnumReadyState =
46+
case _ of
47+
Connecting -> 0
48+
Open -> 1
49+
Closing -> 2
50+
Closed -> 3

src/Web/WebSocket/WebSocket.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"use strict";
2+
3+
exports.create = function (url) {
4+
return function (protocols) {
5+
return function () {
6+
return new WebSocket(url, protocols);
7+
};
8+
};
9+
};
10+
11+
exports.url = function (ws) {
12+
return function () {
13+
return ws.url;
14+
};
15+
};
16+
17+
exports.readyStateImpl = function (ws) {
18+
return function () {
19+
return ws.readyState;
20+
};
21+
};
22+
23+
exports.bufferedAmount = function (ws) {
24+
return function () {
25+
return ws.bufferedAmount;
26+
};
27+
};
28+
29+
exports.extensions = function (ws) {
30+
return function () {
31+
return ws.extensions;
32+
};
33+
};
34+
35+
exports.protocol = function (ws) {
36+
return function () {
37+
return ws.protocol;
38+
};
39+
};
40+
41+
exports.close = function (ws) {
42+
return function () {
43+
return ws.close();
44+
};
45+
};
46+
47+
exports.getBinaryTypeImpl = function (ws) {
48+
return function () {
49+
return ws.binaryType;
50+
};
51+
};
52+
53+
exports.setBinaryTypeImpl = function (ws) {
54+
return function (bt) {
55+
return function () {
56+
ws.binaryType = bt;
57+
};
58+
};
59+
};
60+
61+
exports.sendImpl = function (ws) {
62+
return function (value) {
63+
return function () {
64+
ws.send(value);
65+
};
66+
};
67+
};

0 commit comments

Comments
 (0)