Skip to content

Commit 6184ea1

Browse files
committed
New module HTTP2
HTTP2 has with low-level `Effect` bindings and high-level `Aff` bindings. Add spago.dhall. Use purescript-spec for tests. Upgraded ci.yml. New function HTTP.onRequest.
1 parent 9baab9d commit 6184ea1

26 files changed

+2352
-188
lines changed

.github/workflows/ci.yml

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,30 @@ jobs:
1212
steps:
1313
- uses: actions/checkout@v2
1414

15-
- uses: purescript-contrib/setup-purescript@main
15+
- name: Set up a PureScript toolchain
16+
uses: purescript-contrib/setup-purescript@main
1617
with:
1718
purescript: "unstable"
19+
purs-tidy: "0.9.0"
1820

19-
- uses: actions/setup-node@v2
21+
- name: Cache PureScript dependencies
22+
uses: actions/cache@v2
2023
with:
21-
node-version: "14"
22-
24+
key: ${{ runner.os }}-spago-${{ hashFiles('**/*.dhall') }}
25+
path: |
26+
.spago
27+
output
2328
- name: Install dependencies
24-
run: |
25-
npm install -g bower
26-
npm install
27-
bower install --production
29+
run: spago install
30+
2831

2932
- name: Build source
30-
run: npm run-script build
33+
run: spago build --no-install --purs-args '--censor-lib --strict'
34+
3135

3236
- name: Run tests
33-
run: |
34-
bower install
35-
npm run-script test --if-present
37+
run: spago test --no-install
38+
39+
- name: Check formatting
40+
run: purs-tidy check src test
41+

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
/node_modules/
77
/output/
88
package-lock.json
9+
generated-docs/

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ Breaking changes:
88

99
New features:
1010

11+
- New module `HTTP2`. Solves #44. (#45 by @jamesdbrock)
12+
- Use __spec__ for tests. Upgraded `ci.yml`. Solves #35. (#45 by @jamesdbrock)
13+
- New function `HTTP.onRequest`. Solves #46. (#45 by @jamesdbrock)
14+
1115
Bugfixes:
1216

1317
Other improvements:
@@ -32,7 +36,7 @@ New features:
3236
Other improvements:
3337
- Migrated CI to GitHub Actions, updated installation instructions to use Spago, and migrated from `jshint` to `eslint` (#30)
3438
- Added a changelog and pull request template (#34)
35-
39+
3640
## [v5.0.2](https://github.com/purescript-node/purescript-node-http/releases/tag/v5.0.2) - 2019-07-24
3741

3842
- Relaxed upper bounds on `node-buffer`

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[![Build status](https://github.com/purescript-node/purescript-node-http/workflows/CI/badge.svg?branch=master)](https://github.com/purescript-node/purescript-node-http/actions?query=workflow%3ACI+branch%3Amaster)
55
[![Pursuit](https://pursuit.purescript.org/packages/purescript-node-http/badge)](https://pursuit.purescript.org/packages/purescript-node-http)
66

7-
A wrapper for Node's HTTP APIs.
7+
A wrapper for Node’s [HTTP](https://nodejs.org/docs/latest/api/http.html) and [HTTP/2](https://nodejs.org/docs/latest/api/http2.html) APIs.
88

99
## Installation
1010

bower.json

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,47 @@
11
{
2-
"name": "purescript-node-http",
3-
"license": "MIT",
4-
"ignore": [
5-
"**/.*",
6-
"node_modules",
7-
"bower_components",
8-
"output"
9-
],
10-
"repository": {
11-
"type": "git",
12-
"url": "https://github.com/purescript-node/purescript-node-http.git"
13-
},
14-
"devDependencies": {
15-
"purescript-console": "^6.0.0"
16-
},
17-
"dependencies": {
18-
"purescript-arraybuffer-types": "^3.0.2",
19-
"purescript-contravariant": "^6.0.0",
20-
"purescript-effect": "^4.0.0",
21-
"purescript-foreign": "^7.0.0",
22-
"purescript-foreign-object": "^4.0.0",
23-
"purescript-maybe": "^6.0.0",
24-
"purescript-node-buffer": "^8.0.0",
25-
"purescript-node-net": "^4.0.0",
26-
"purescript-node-streams": "^7.0.0",
27-
"purescript-node-url": "^6.0.0",
28-
"purescript-nullable": "^6.0.0",
29-
"purescript-options": "^7.0.0",
30-
"purescript-prelude": "^6.0.0",
31-
"purescript-unsafe-coerce": "^6.0.0"
32-
}
2+
"name": "purescript-node-http",
3+
"license": [
4+
"MIT"
5+
],
6+
"repository": {
7+
"type": "git",
8+
"url": "https://github.com/purescript-node/purescript-node-http"
9+
},
10+
"ignore": [
11+
"**/.*",
12+
"node_modules",
13+
"bower_components",
14+
"output"
15+
],
16+
"dependencies": {
17+
"purescript-aff": "^v7.1.0",
18+
"purescript-arraybuffer-types": "^v3.0.2",
19+
"purescript-console": "^v6.0.0",
20+
"purescript-contravariant": "^v6.0.0",
21+
"purescript-control": "^v6.0.0",
22+
"purescript-effect": "^v4.0.0",
23+
"purescript-either": "^v6.1.0",
24+
"purescript-exceptions": "^v6.0.0",
25+
"purescript-foldable-traversable": "^v6.0.0",
26+
"purescript-foreign": "^v7.0.0",
27+
"purescript-foreign-object": "^v4.1.0",
28+
"purescript-maybe": "^v6.0.0",
29+
"purescript-node-buffer": "^v8.0.0",
30+
"purescript-node-net": "^v4.0.0",
31+
"purescript-node-streams": "^v7.0.0",
32+
"purescript-node-streams-aff": "https://github.com/purescript-node/purescript-node-streams-aff.git#v4.0.0",
33+
"purescript-node-url": "^v6.0.0",
34+
"purescript-nullable": "^v6.0.0",
35+
"purescript-options": "^v7.0.0",
36+
"purescript-parallel": "^v6.0.0",
37+
"purescript-partial": "^v4.0.0",
38+
"purescript-prelude": "^v6.0.1",
39+
"purescript-spec": "^v7.1.0",
40+
"purescript-st": "^v6.2.0",
41+
"purescript-strings": "^v6.0.1",
42+
"purescript-transformers": "^v6.0.0",
43+
"purescript-tuples": "^v7.0.0",
44+
"purescript-unsafe-coerce": "^v6.0.0",
45+
"purescript-web-fetch": "https://github.com/purescript-web/purescript-web-fetch.git#v3.0.0"
46+
}
3347
}

packages.dhall

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
let upstream =
2+
https://github.com/purescript/package-sets/releases/download/psc-0.15.4-20221102/packages.dhall
3+
sha256:8628e413718876ce26983db1d0ce9d9e1588129117fa3bb8ed9f618db6914127
4+
5+
in upstream

spago.dhall

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{ name = "node-http"
2+
, dependencies =
3+
[ "aff"
4+
, "arraybuffer-types"
5+
, "console"
6+
, "contravariant"
7+
, "control"
8+
, "effect"
9+
, "either"
10+
, "exceptions"
11+
, "foldable-traversable"
12+
, "foreign"
13+
, "foreign-object"
14+
, "maybe"
15+
, "newtype"
16+
, "node-buffer"
17+
, "node-net"
18+
, "node-process"
19+
, "node-streams"
20+
, "node-streams-aff"
21+
, "node-url"
22+
, "nullable"
23+
, "options"
24+
, "parallel"
25+
, "partial"
26+
, "prelude"
27+
, "spec"
28+
, "st"
29+
, "strings"
30+
, "transformers"
31+
, "tuples"
32+
, "unsafe-coerce"
33+
, "web-fetch"
34+
]
35+
, packages = ./packages.dhall
36+
, sources = [ "src/**/*.purs", "test/**/*.purs" ]
37+
, license = "MIT"
38+
, repository = "https://github.com/purescript-node/purescript-node-http"
39+
}

src/Node/HTTP.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ export function onUpgrade(server) {
6464
};
6565
}
6666

67+
export function onRequest(server) {
68+
return function (cb) {
69+
return function () {
70+
server.on("request", function (req, res) {
71+
return cb(req)(res)();
72+
});
73+
};
74+
};
75+
}
76+
6777
export function setHeader(res) {
6878
return function (key) {
6979
return function (value) {

src/Node/HTTP.purs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module Node.HTTP
1212
, listenSocket
1313
, onConnect
1414
, onUpgrade
15+
, onRequest
1516

1617
, httpVersion
1718
, requestHeaders
@@ -77,6 +78,9 @@ foreign import onConnect :: Server -> (Request -> Socket -> Buffer -> Effect Uni
7778
-- | Listen to `upgrade` events on the server
7879
foreign import onUpgrade :: Server -> (Request -> Socket -> Buffer -> Effect Unit) -> Effect Unit
7980

81+
-- | Listen to `request` events on the server
82+
foreign import onRequest :: Server -> (Request -> Response -> Effect Unit) -> Effect Unit
83+
8084
-- | Get the request HTTP version
8185
httpVersion :: Request -> String
8286
httpVersion = _.httpVersion <<< unsafeCoerce

src/Node/HTTP/Secure.purs

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,17 @@ import Unsafe.Coerce (unsafeCoerce)
9090

9191
-- | Create an HTTPS server, given the SSL options and a function to be executed
9292
-- | when a request is received.
93-
foreign import createServerImpl ::
94-
Foreign ->
95-
(Request -> Response -> Effect Unit) ->
96-
Effect Server
93+
foreign import createServerImpl
94+
:: Foreign
95+
-> (Request -> Response -> Effect Unit)
96+
-> Effect Server
9797

9898
-- | Create an HTTPS server, given the SSL options and a function to be executed
9999
-- | when a request is received.
100-
createServer :: Options SSLOptions ->
101-
(Request -> Response -> Effect Unit) ->
102-
Effect Server
100+
createServer
101+
:: Options SSLOptions
102+
-> (Request -> Response -> Effect Unit)
103+
-> Effect Server
103104
createServer = createServerImpl <<< options
104105

105106
-- | The type of HTTPS server options
@@ -120,16 +121,22 @@ rejectUnauthorized = opt "rejectUnauthorized"
120121
-- | The npnProtocols option can be a String, a Buffer, a Uint8Array, or an
121122
-- | array of any of those types.
122123
data NPNProtocols
124+
123125
npnProtocolsString :: String -> NPNProtocols
124126
npnProtocolsString = unsafeCoerce
127+
125128
npnProtocolsBuffer :: Buffer -> NPNProtocols
126129
npnProtocolsBuffer = unsafeCoerce
130+
127131
npnProtocolsUint8Array :: Uint8Array -> NPNProtocols
128132
npnProtocolsUint8Array = unsafeCoerce
133+
129134
npnProtocolsStringArray :: Array String -> NPNProtocols
130135
npnProtocolsStringArray = unsafeCoerce
136+
131137
npnProtocolsBufferArray :: Array Buffer -> NPNProtocols
132138
npnProtocolsBufferArray = unsafeCoerce
139+
133140
npnProtocolsUint8ArrayArray :: Array Uint8Array -> NPNProtocols
134141
npnProtocolsUint8ArrayArray = unsafeCoerce
135142

@@ -140,16 +147,22 @@ npnProtocols = opt "NPNProtocols"
140147
-- | The alpnProtocols option can be a String, a Buffer, a Uint8Array, or an
141148
-- | array of any of those types.
142149
data ALPNProtocols
150+
143151
alpnProtocolsString :: String -> ALPNProtocols
144152
alpnProtocolsString = unsafeCoerce
153+
145154
alpnProtocolsBuffer :: Buffer -> ALPNProtocols
146155
alpnProtocolsBuffer = unsafeCoerce
156+
147157
alpnProtocolsUint8Array :: Uint8Array -> ALPNProtocols
148158
alpnProtocolsUint8Array = unsafeCoerce
159+
149160
alpnProtocolsStringArray :: Array String -> ALPNProtocols
150161
alpnProtocolsStringArray = unsafeCoerce
162+
151163
alpnProtocolsBufferArray :: Array Buffer -> ALPNProtocols
152164
alpnProtocolsBufferArray = unsafeCoerce
165+
153166
alpnProtocolsUint8ArrayArray :: Array Uint8Array -> ALPNProtocols
154167
alpnProtocolsUint8ArrayArray = unsafeCoerce
155168

@@ -167,8 +180,10 @@ ticketKeys = opt "ticketKeys"
167180

168181
-- | The PFX option can take either a String or a Buffer
169182
data PFX
183+
170184
pfxString :: String -> PFX
171185
pfxString = unsafeCoerce
186+
172187
pfxBuffer :: Buffer -> PFX
173188
pfxBuffer = unsafeCoerce
174189

@@ -179,12 +194,16 @@ pfx = opt "pfx"
179194
-- | The key option can be a String, a Buffer, an array of strings, or an array
180195
-- | of buffers.
181196
data Key
197+
182198
keyString :: String -> Key
183199
keyString = unsafeCoerce
200+
184201
keyBuffer :: Buffer -> Key
185202
keyBuffer = unsafeCoerce
203+
186204
keyStringArray :: Array String -> Key
187205
keyStringArray = unsafeCoerce
206+
188207
keyBufferArray :: Array Buffer -> Key
189208
keyBufferArray = unsafeCoerce
190209

@@ -199,12 +218,16 @@ passphrase = opt "passphrase"
199218
-- | The cert option can be a String, a Buffer, an array of strings, or an array
200219
-- | of buffers.
201220
data Cert
221+
202222
certString :: String -> Cert
203223
certString = unsafeCoerce
224+
204225
certBuffer :: Buffer -> Cert
205226
certBuffer = unsafeCoerce
227+
206228
certStringArray :: Array String -> Cert
207229
certStringArray = unsafeCoerce
230+
208231
certBufferArray :: Array Buffer -> Cert
209232
certBufferArray = unsafeCoerce
210233

@@ -215,12 +238,16 @@ cert = opt "cert"
215238
-- | The CA option can be a String, a Buffer, an array of strings, or an array
216239
-- | of buffers.
217240
data CA
241+
218242
caString :: String -> CA
219243
caString = unsafeCoerce
244+
220245
caBuffer :: Buffer -> CA
221246
caBuffer = unsafeCoerce
247+
222248
caStringArray :: Array String -> CA
223249
caStringArray = unsafeCoerce
250+
224251
caBufferArray :: Array Buffer -> CA
225252
caBufferArray = unsafeCoerce
226253

@@ -231,12 +258,16 @@ ca = opt "ca"
231258
-- | The CRL option can be a String, a Buffer, an array of strings, or an array
232259
-- | of buffers.
233260
data CRL
261+
234262
crlString :: String -> CRL
235263
crlString = unsafeCoerce
264+
236265
crlBuffer :: Buffer -> CRL
237266
crlBuffer = unsafeCoerce
267+
238268
crlStringArray :: Array String -> CRL
239269
crlStringArray = unsafeCoerce
270+
240271
crlBufferArray :: Array Buffer -> CRL
241272
crlBufferArray = unsafeCoerce
242273

@@ -258,8 +289,10 @@ ecdhCurve = opt "ecdhCurve"
258289

259290
-- | The DHParam option can take either a String or a Buffer
260291
data DHParam
292+
261293
dhparamString :: String -> DHParam
262294
dhparamString = unsafeCoerce
295+
263296
dhparamBuffer :: Buffer -> DHParam
264297
dhparamBuffer = unsafeCoerce
265298

0 commit comments

Comments
 (0)