git.iko.soy/rowtype-yoga/purs-eval
on main help sign in
11 files changed+178−275
since splitsnapshots
unifiedsplit
.github/workflows/release.yml+7−8 .github/workflows/test.yml+3−3 .github/workflows/todo.yml+14 .github/workflows/update-flake-lock.yml+4−4 CHANGELOG.md+12 README.md+3−3 flake.lock+74−168 flake.nix+30−20 package-lock.json+2−2 package.json+1−1 test/Test/Compiler/ServerMock.purs+28−66
.github/workflows/release.yml+7−8
@@ -7,29 +7,28 @@ jobs:
77 release:
88 runs-on: ubuntu-latest
99 steps:
10− - uses: google-github-actions/release-please-action@v3
10+ - uses: googleapis/release-please-action@v5
1111 id: release
1212 with:
1313 release-type: node
14− package-name: purs-eval
15− - uses: actions/checkout@v3
14+ - uses: actions/checkout@v7
1615 if: ${{ steps.release.outputs.release_created }}
17− - uses: cachix/install-nix-action@v22
16+ - uses: cachix/install-nix-action@v31
1817 if: ${{ steps.release.outputs.release_created }}
1918 with:
2019 extra_nix_config: |
2120 access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}
22− - uses: cachix/cachix-action@v12
21+ - uses: cachix/cachix-action@v17
2322 if: ${{ steps.release.outputs.release_created }}
2423 with:
2524 name: klarkc
2625 authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'
2726 - run: |
28− nix build
27+ nix build .#npm
2928 mkdir -p dist
30− cp result/bin/purs-eval dist
29+ cp result dist/purs-eval
3130 if: ${{ steps.release.outputs.release_created }}
32− - uses: actions/setup-node@v3
31+ - uses: actions/setup-node@v7
3332 if: ${{ steps.release.outputs.release_created }}
3433 with:
3534 node-version: 18
.github/workflows/test.yml+3−3
@@ -5,12 +5,12 @@ jobs:
55 tests:
66 runs-on: ubuntu-latest
77 steps:
8− - uses: actions/checkout@v3
9− - uses: cachix/install-nix-action@v22
8+ - uses: actions/checkout@v7
9+ - uses: cachix/install-nix-action@v31
1010 with:
1111 extra_nix_config: |
1212 access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}
13− - uses: cachix/cachix-action@v12
13+ - uses: cachix/cachix-action@v17
1414 with:
1515 name: klarkc
1616 authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'
.github/workflows/todo.ymlnew file+14
@@ -0,0 +1,14 @@
1+name: "Run TODO to Issue"
2+on:
3+ push:
4+ branches:
5+ - main
6+jobs:
7+ build:
8+ runs-on: "ubuntu-latest"
9+ steps:
10+ - uses: "actions/checkout@v7"
11+ - name: "TODO to Issue"
12+ uses: "alstr/todo-to-issue-action@v5"
13+ with:
14+ IDENTIFIERS: '[{"name": "TODO", "labels": ["help wanted"]}, {"name": "FIXME", "labels": ["bug"]}]'
.github/workflows/update-flake-lock.yml+4−4
@@ -9,18 +9,18 @@ jobs:
99 runs-on: ubuntu-latest
1010 steps:
1111 - name: Checkout repository
12− uses: actions/checkout@v3
12+ uses: actions/checkout@v7
1313 - name: Install Nix
14− uses: cachix/install-nix-action@v22
14+ uses: cachix/install-nix-action@v31
1515 with:
1616 extra_nix_config: |
1717 access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}
18− - uses: cachix/cachix-action@v12
18+ - uses: cachix/cachix-action@v17
1919 with:
2020 name: klarkc
2121 authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'
2222 - name: Update flake.lock
23− uses: DeterminateSystems/update-flake-lock@v20
23+ uses: DeterminateSystems/update-flake-lock@v29
2424 with:
2525 commit-msg: "build(deps): Bump flake.lock dependencies"
2626 pr-title: "Update flake.lock"
CHANGELOG.md+12
@@ -1,5 +1,17 @@
11# Changelog
22
3+## [1.5.0](https://github.com/rowtype-yoga/purs-eval/compare/v1.4.1...v1.5.0) (2024-07-11)
4+
5+
6+### Features
7+
8+* upgrade purs-nix and change mock server to httpurple ([ddaad01](https://github.com/rowtype-yoga/purs-eval/commit/ddaad01eed68afa45f584c807ed577abc565c247))
9+
10+
11+### Bug Fixes
12+
13+* npm published version not running ([3f1a295](https://github.com/rowtype-yoga/purs-eval/commit/3f1a29567365000f57e6b2c9c54c55448c3a83c7))
14+
315## [1.4.1](https://github.com/klarkc/purs-eval/compare/v1.4.0...v1.4.1) (2023-08-21)
416
517
README.md+3−3
@@ -1,5 +1,5 @@
11# purs-eval
2−A simple PureScript expression evaluation tool
2+A simple PureScript expression evaluation tool. It uses [try.purescript.org](https://try.purescript.org) as a compilation backend; for this reason, it currently does not support embedding local files or modules.
33
44## Usage
55
@@ -10,10 +10,10 @@ EXAMPLE="module Main where\nimport Effect.Console (log)\nmain = log \"hello from
1010echo -e $EXAMPLE | npx purs-eval
1111
1212# With nix
13−echo -e $EXAMPLE | nix run github:klarkc/purs-eval
13+echo -e $EXAMPLE | nix run github:rowtype-yoga/purs-eval
1414
1515# Pipe node (>v18)
16−echo -e $EXAMPLE | nix run github:klarkc/purs-eval | nix run nixpkgs#nodejs -- --experimental-network-imports --input-type module
16+echo -e $EXAMPLE | nix run github:rowtype-yoga/purs-eval | nix run nixpkgs#nodejs -- --experimental-network-imports --input-type module
1717
1818# or with npx
1919echo -e $EXAMPLE | npx purs-eval | node --experimental-network-imports --input-type module
flake.lock+74−168
@@ -1,26 +1,5 @@
11{
22 "nodes": {
3− "deadnix": {
4− "inputs": {
5− "fenix": "fenix",
6− "naersk": "naersk",
7− "nixpkgs": "nixpkgs_5",
8− "utils": "utils"
9− },
10− "locked": {
11− "lastModified": 1656370114,
12− "narHash": "sha256-XBbSWeBuF6Ck0jc634yAp2hjPXWM2JyRDPCdK0dh3w4=",
13− "owner": "astro",
14− "repo": "deadnix",
15− "rev": "9f450f7250ad7680cb5f12ce5985cc18496c2d5f",
16− "type": "github"
17− },
18− "original": {
19− "owner": "astro",
20− "repo": "deadnix",
21− "type": "github"
22− }
23− },
243 "docs-search": {
254 "flake": false,
265 "locked": {
@@ -37,26 +16,22 @@
3716 "type": "github"
3817 }
3918 },
40− "fenix": {
41− "inputs": {
42− "nixpkgs": "nixpkgs_3",
43− "rust-analyzer-src": "rust-analyzer-src"
44− },
19+ "flake-utils": {
4520 "locked": {
46− "lastModified": 1655533500,
47− "narHash": "sha256-qJJmLVoMYfDLywI9MNL7sb0W/GsKQF9HDatdHm1tSl0=",
48− "owner": "nix-community",
49− "repo": "fenix",
50− "rev": "b6630603af13df17d0dd4df8629e9a24e6ba0fbd",
21+ "lastModified": 1644229661,
22+ "narHash": "sha256-1YdnJAsNy69bpcjuoKdOYQX0YxZBiCYZo4Twxerqv7k=",
23+ "owner": "numtide",
24+ "repo": "flake-utils",
25+ "rev": "3cecb5b042f7f209c56ffd8371b2711a290ec797",
5126 "type": "github"
5227 },
5328 "original": {
54− "owner": "nix-community",
55− "repo": "fenix",
29+ "owner": "numtide",
30+ "repo": "flake-utils",
5631 "type": "github"
5732 }
5833 },
59− "flake-utils": {
34+ "flake-utils_2": {
6035 "locked": {
6136 "lastModified": 1618217525,
6237 "narHash": "sha256-WGrhVczjXTiswQaoxQ+0PTfbLNeOQM6M36zvLn78AYg=",
@@ -71,7 +46,7 @@
7146 "type": "github"
7247 }
7348 },
74− "flake-utils_2": {
49+ "flake-utils_3": {
7550 "locked": {
7651 "lastModified": 1618217525,
7752 "narHash": "sha256-WGrhVczjXTiswQaoxQ+0PTfbLNeOQM6M36zvLn78AYg=",
@@ -86,7 +61,7 @@
8661 "type": "github"
8762 }
8863 },
89− "flake-utils_3": {
64+ "flake-utils_4": {
9065 "locked": {
9166 "lastModified": 1618217525,
9267 "narHash": "sha256-WGrhVczjXTiswQaoxQ+0PTfbLNeOQM6M36zvLn78AYg=",
@@ -116,6 +91,44 @@
11691 "type": "github"
11792 }
11893 },
94+ "httpurple-argonaut": {
95+ "flake": false,
96+ "locked": {
97+ "lastModified": 1655138308,
98+ "narHash": "sha256-tWTgc2gERJLkv+XzK4HtqJJbqvw10svVu1cmXFADtls=",
99+ "owner": "sigma-andex",
100+ "repo": "purescript-httpurple-argonaut",
101+ "rev": "3187fba4fe0682849b4acbf215e6a6fc1f6493de",
102+ "type": "github"
103+ },
104+ "original": {
105+ "owner": "sigma-andex",
106+ "repo": "purescript-httpurple-argonaut",
107+ "type": "github"
108+ }
109+ },
110+ "lint-utils": {
111+ "inputs": {
112+ "flake-utils": "flake-utils",
113+ "nixpkgs": [
114+ "purs-nix",
115+ "nixpkgs"
116+ ]
117+ },
118+ "locked": {
119+ "lastModified": 1707777931,
120+ "narHash": "sha256-PsPb5xMBZ9dPDP04o9vqKEUIEG80Z84/74fPuOMs0ZI=",
121+ "owner": "homotopic",
122+ "repo": "lint-utils",
123+ "rev": "5f11e3e51d8f1aa4ed62a89e90f05953931e105a",
124+ "type": "github"
125+ },
126+ "original": {
127+ "owner": "homotopic",
128+ "repo": "lint-utils",
129+ "type": "github"
130+ }
131+ },
119132 "make-shell": {
120133 "locked": {
121134 "lastModified": 1634940815,
@@ -148,31 +161,13 @@
148161 "type": "github"
149162 }
150163 },
151− "naersk": {
152− "inputs": {
153− "nixpkgs": "nixpkgs_4"
154− },
155− "locked": {
156− "lastModified": 1655042882,
157− "narHash": "sha256-9BX8Fuez5YJlN7cdPO63InoyBy7dm3VlJkkmTt6fS1A=",
158− "owner": "nix-community",
159− "repo": "naersk",
160− "rev": "cddffb5aa211f50c4b8750adbec0bbbdfb26bb9f",
161− "type": "github"
162− },
163− "original": {
164− "owner": "nix-community",
165− "repo": "naersk",
166− "type": "github"
167− }
168− },
169164 "nixpkgs": {
170165 "locked": {
171− "lastModified": 1693060755,
172− "narHash": "sha256-KNsbfqewEziFJEpPR0qvVz4rx0x6QXxw1CcunRhlFdk=",
166+ "lastModified": 1704161960,
167+ "narHash": "sha256-QGua89Pmq+FBAro8NriTuoO/wNaUtugt29/qqA8zeeM=",
173168 "owner": "NixOS",
174169 "repo": "nixpkgs",
175− "rev": "c66ccfa00c643751da2fd9290e096ceaa30493fc",
170+ "rev": "63143ac2c9186be6d9da6035fa22620018c85932",
176171 "type": "github"
177172 },
178173 "original": {
@@ -183,66 +178,6 @@
183178 }
184179 },
185180 "nixpkgs_2": {
186− "locked": {
187− "lastModified": 1646506091,
188− "narHash": "sha256-sWNAJE2m+HOh1jtXlHcnhxsj6/sXrHgbqVNcVRlveK4=",
189− "owner": "NixOS",
190− "repo": "nixpkgs",
191− "rev": "3e644bd62489b516292c816f70bf0052c693b3c7",
192− "type": "github"
193− },
194− "original": {
195− "owner": "NixOS",
196− "ref": "nixpkgs-unstable",
197− "repo": "nixpkgs",
198− "type": "github"
199− }
200− },
201− "nixpkgs_3": {
202− "locked": {
203− "lastModified": 1655400192,
204− "narHash": "sha256-49OBVVRgb9H/PSmNT9W61+NRdDbuSJVuDDflwXlaUKU=",
205− "owner": "nixos",
206− "repo": "nixpkgs",
207− "rev": "3d7435c638baffaa826b85459df0fff47f12317d",
208− "type": "github"
209− },
210− "original": {
211− "owner": "nixos",
212− "ref": "nixos-unstable",
213− "repo": "nixpkgs",
214− "type": "github"
215− }
216− },
217− "nixpkgs_4": {
218− "locked": {
219− "lastModified": 1655481042,
220− "narHash": "sha256-XHbcywq2vIQ5CeH1OK3TN793jkiNAAZsSctS1PFgseo=",
221− "owner": "NixOS",
222− "repo": "nixpkgs",
223− "rev": "103a4c0ae46afa9cf008c30744175315ca38e9f9",
224− "type": "github"
225− },
226− "original": {
227− "id": "nixpkgs",
228− "type": "indirect"
229− }
230− },
231− "nixpkgs_5": {
232− "locked": {
233− "lastModified": 1655481042,
234− "narHash": "sha256-XHbcywq2vIQ5CeH1OK3TN793jkiNAAZsSctS1PFgseo=",
235− "owner": "NixOS",
236− "repo": "nixpkgs",
237− "rev": "103a4c0ae46afa9cf008c30744175315ca38e9f9",
238− "type": "github"
239− },
240− "original": {
241− "id": "nixpkgs",
242− "type": "indirect"
243− }
244− },
245− "nixpkgs_6": {
246181 "locked": {
247182 "lastModified": 1656549732,
248183 "narHash": "sha256-eILutFZGjfk2bEzfim8S/qyYc//0S1KsCeO+OWbtoR0=",
@@ -291,17 +226,16 @@
291226 },
292227 "ps-tools": {
293228 "inputs": {
294− "deadnix": "deadnix",
295229 "make-shell": "make-shell_2",
296− "nixpkgs": "nixpkgs_6",
297− "utils": "utils_2"
230+ "nixpkgs": "nixpkgs_2",
231+ "utils": "utils"
298232 },
299233 "locked": {
300− "lastModified": 1691537451,
301− "narHash": "sha256-9dnX6E7eWVm8xhHDeA3AmkvhqcWRo6RJ4z6Vrn65cFs=",
234+ "lastModified": 1704567308,
235+ "narHash": "sha256-WbFPIkKLtyQOPBUjintckKIYnfs7MvIbmfVsLRSAPlc=",
302236 "owner": "purs-nix",
303237 "repo": "purescript-tools",
304− "rev": "675cd35bc9255f9c2b1f6f6784ef41b9773202e8",
238+ "rev": "ac626313141cbee78f06eb3c5e90359f695aef9b",
305239 "type": "github"
306240 },
307241 "original": {
@@ -314,74 +248,46 @@
314248 "inputs": {
315249 "docs-search": "docs-search",
316250 "get-flake": "get-flake",
251+ "lint-utils": "lint-utils",
317252 "make-shell": "make-shell",
318− "nixpkgs": "nixpkgs_2",
253+ "nixpkgs": "nixpkgs",
319254 "parsec": "parsec",
320255 "ps-tools": "ps-tools",
321− "utils": "utils_3"
256+ "utils": "utils_2"
322257 },
323258 "locked": {
324− "lastModified": 1691603627,
325− "narHash": "sha256-bsJh6PCDF9+hSuk/dbTtSXdma0VvjFejN6weHPt7cyU=",
259+ "lastModified": 1711996073,
260+ "narHash": "sha256-NDV8q9qtpKjHgIcMVBgdNqEfhpfK1d19rHYNa7yq0Qs=",
326261 "owner": "purs-nix",
327262 "repo": "purs-nix",
328− "rev": "8729b7fbb02822df3fb3988bae94b276e50ca6fc",
263+ "rev": "8caa40f7fe69b9fcd05baae04c4ec9eb43953e2e",
329264 "type": "github"
330265 },
331266 "original": {
332267 "owner": "purs-nix",
333− "ref": "ps-0.15",
334268 "repo": "purs-nix",
335269 "type": "github"
336270 }
337271 },
338272 "root": {
339273 "inputs": {
340− "nixpkgs": "nixpkgs",
274+ "httpurple-argonaut": "httpurple-argonaut",
275+ "nixpkgs": [
276+ "purs-nix",
277+ "nixpkgs"
278+ ],
341279 "npmlock2nix": "npmlock2nix",
342280 "ps-tools": [
343281 "purs-nix",
344282 "ps-tools"
345283 ],
346284 "purs-nix": "purs-nix",
347− "utils": "utils_4"
348− }
349− },
350− "rust-analyzer-src": {
351− "flake": false,
352− "locked": {
353− "lastModified": 1655507737,
354− "narHash": "sha256-o+AqNsjL6o2RHh4InZHQVpkmqg570YFJL4Db8mKq+fs=",
355− "owner": "rust-lang",
356− "repo": "rust-analyzer",
357− "rev": "12dd81092e37df28b7a3591cae9675e668927198",
358− "type": "github"
359− },
360− "original": {
361− "owner": "rust-lang",
362− "ref": "nightly",
363− "repo": "rust-analyzer",
364− "type": "github"
285+ "utils": "utils_3"
365286 }
366287 },
367288 "utils": {
368− "locked": {
369− "lastModified": 1653893745,
370− "narHash": "sha256-0jntwV3Z8//YwuOjzhV2sgJJPt+HY6KhU7VZUL0fKZQ=",
371− "owner": "numtide",
372− "repo": "flake-utils",
373− "rev": "1ed9fb1935d260de5fe1c2f7ee0ebaae17ed2fa1",
374− "type": "github"
375− },
376− "original": {
377− "owner": "numtide",
378− "repo": "flake-utils",
379− "type": "github"
380− }
381− },
382− "utils_2": {
383289 "inputs": {
384− "flake-utils": "flake-utils"
290+ "flake-utils": "flake-utils_2"
385291 },
386292 "locked": {
387293 "lastModified": 1656044990,
@@ -398,9 +304,9 @@
398304 "type": "github"
399305 }
400306 },
401− "utils_3": {
307+ "utils_2": {
402308 "inputs": {
403− "flake-utils": "flake-utils_2"
309+ "flake-utils": "flake-utils_3"
404310 },
405311 "locked": {
406312 "lastModified": 1656044990,
@@ -417,9 +323,9 @@
417323 "type": "github"
418324 }
419325 },
420− "utils_4": {
326+ "utils_3": {
421327 "inputs": {
422− "flake-utils": "flake-utils_3"
328+ "flake-utils": "flake-utils_4"
423329 },
424330 "locked": {
425331 "lastModified": 1658606827,
flake.nix+30−20
@@ -1,12 +1,18 @@
11{
22 inputs =
33 {
4− nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
5− npmlock2nix.url = "github:nix-community/npmlock2nix";
6− npmlock2nix.flake = false;
7− ps-tools.follows = "purs-nix/ps-tools";
8− purs-nix.url = "github:purs-nix/purs-nix/ps-0.15";
4+ purs-nix.url = "github:purs-nix/purs-nix";
5+ nixpkgs.follows = "purs-nix/nixpkgs";
96 utils.url = "github:ursi/flake-utils";
7+ ps-tools.follows = "purs-nix/ps-tools";
8+ npmlock2nix = {
9+ url = "github:nix-community/npmlock2nix";
10+ flake = false;
11+ };
12+ httpurple-argonaut = {
13+ url = "github:sigma-andex/purescript-httpurple-argonaut";
14+ flake = false;
15+ };
1016 };
1117
1218 outputs = { self, utils, ... }@inputs:
@@ -15,25 +21,24 @@
1521 inherit inputs;
1622 # Limited by ps-tools
1723 systems = [ "x86_64-linux" "x86_64-darwin" ];
18− make-pkgs = system: import inputs.nixpkgs {
19− inherit system;
20− # required by npmlock2nix
21− config.permittedInsecurePackages = [
22− "nodejs-16.20.2"
23− ];
24− };
2524 }
26− ({ pkgs, system, ... }:
25+ ({ pkgs, system, ... }@ctx:
2726 let
2827 inherit (pkgs) nodejs;
2928 npm = import inputs.npmlock2nix { inherit pkgs; };
3029 node_modules = npm.v2.node_modules { src = ./.; inherit nodejs; } + /node_modules;
31− ps-tools = inputs.ps-tools.legacyPackages.${system};
32− inherit (ps-tools.for-0_15) purescript purs-tidy purescript-language-server;
30+ inherit (ctx.ps-tools) purescript purs-tidy purescript-language-server;
3331 purs-nix = inputs.purs-nix { inherit system; };
3432 affjax-node_ = pkgs.lib.recursiveUpdate purs-nix.ps-pkgs.affjax-node {
3533 purs-nix-info.foreign."Affjax.Node" = { inherit node_modules; };
3634 };
35+ # TODO use httpurple-argonaut from official index
36+ httpurple-argonaut_ = purs-nix.build
37+ {
38+ name = "httpurple-argonaut";
39+ src.path = inputs.httpurple-argonaut;
40+ info.dependencies = [ "argonaut" "console" "effect" "either" "httpurple" "prelude"];
41+ };
3742 ps =
3843 purs-nix.purs
3944 {
@@ -48,10 +53,8 @@
4853 argonaut-codecs
4954 argonaut-generic
5055 effect
51− httpure
52− node-buffer
53− node-process
54− node-streams-aff
56+ httpurple
57+ httpurple-argonaut_
5558 test-unit
5659 parsing
5760 ];
@@ -68,9 +71,16 @@
6871 };
6972
7073 packages =
71− with ps;
7274 {
7375 default = app { inherit name; };
76+ # npm version does not publish a nix-store path
77+ npm = pkgs.runCommand
78+ "purs-eval-npm"
79+ {}
80+ ''
81+ echo "#!/usr/bin/env node" > $out
82+ tail -n +2 ${packages.default}/bin/purs-eval >> $out
83+ '';
7484 };
7585
7686 checks.test = test.check { };
package-lock.json+2−2
@@ -11,7 +11,7 @@
1111 "devDependencies": {
1212 "xhr2": "^0.2.1"
1313 },
14− "version": "1.4.1"
14+ "version": "1.5.0"
1515 },
1616 "node_modules/xhr2": {
1717 "version": "0.2.1",
@@ -31,5 +31,5 @@
3131 "dev": true
3232 }
3333 },
34− "version": "1.4.1"
34+ "version": "1.5.0"
3535}
package.json+1−1
@@ -4,5 +4,5 @@
44 "devDependencies": {
55 "xhr2": "^0.2.1"
66 },
7− "version": "1.4.1"
7+ "version": "1.5.0"
88}
test/Test/Compiler/ServerMock.purs+28−66
@@ -1,11 +1,7 @@
11module Test.Compiler.ServerMock (settings, setupSrv) where
22
3−import Prelude (Unit, ($), (==), (>>=), (<<<), unit, pure, bind, const, discard)
4−import Control.Apply ((*>))
5−import Control.Monad.Error.Class (throwError, liftMaybe)
6−import Control.Monad.Reader.Trans (ReaderT, ask, runReaderT)
7−import Control.Monad.Trans.Class (lift)
8−import Data.Argonaut.Core (Json, stringify)
3+import Prelude (Unit, ($), (<<<), unit, pure, bind, const)
4+import Data.Argonaut.Core (Json)
95import Data.Argonaut.Decode (JsonDecodeError, printJsonDecodeError)
106import Data.Argonaut.Decode.Class (class DecodeJson, decodeJson)
117import Data.Argonaut.Decode.Generic (genericDecodeJson)
@@ -17,19 +13,13 @@ import Data.Functor ((<$>))
1713import Data.Generic.Rep (class Generic)
1814import Data.Maybe (Maybe(Nothing))
1915import Data.Newtype (class Newtype, unwrap, wrap)
20−import Effect
16+import Node.HTTP.Types (ServerResponse)
17+import Effect (Effect)
2118import Effect.Aff (Aff, bracket)
2219import Effect.Class (liftEffect)
2320import Effect.Exception (Error, error)
24−import HTTPure.Request (Request)
25−import HTTPure.Response (ResponseM, Response, ok)
26−import HTTPure.Server (serve)
27−import HTTPure.Lookup ((!!))
28−import HTTPure.Method (Method(Post))
29−import HTTPure.Body (class Body)
30−import HTTPure.Headers (header)
31−import Node.Stream.Aff (write, end, fromStringUTF8)
32−import Node.HTTP (responseAsStream)
21+import HTTPurple (RouteDuplex', Method(Post), (/), ResponseHeaders, noArgs, mkRoute, serve, ok, notFound, toJson)
22+import HTTPurple.Json.Argonaut (jsonEncoder)
3323import Compiler (Settings, Code, SuccessResult)
3424
3525
@@ -51,26 +41,6 @@ successToJson = (encodeJson :: SuccessResult_ -> Json) <<< wrap
5141successFromJson_ :: Json -> Either Error SuccessResult
5242successFromJson_ json = unwrap <$> (error <<< printJsonDecodeError) `lmap` (decodeJson json :: Either JsonDecodeError SuccessResult_)
5343
54−newtype Json_ = Json_ Json
55−
56−derive instance Newtype Json_ _
57−
58−instance Body Json_ where
59− defaultHeaders _ = pure $ header "Content-Type" "application/json"
60− write json res = do
61− let stream = responseAsStream res
62− body' <- liftEffect $ fromStringUTF8 $ stringify $ unwrap $ json
63− write stream body'
64− end stream
65−
66−type ServerMock = ReaderT Code (ReaderT Request Aff)
67−
68−askRes :: ServerMock Code
69−askRes = ask
70−
71−askReq :: ServerMock Request
72−askReq = lift ask
73−
7444settings :: Settings
7545settings = { protocol: "http"
7646 , hostname: "localhost"
@@ -78,38 +48,30 @@ settings = { protocol: "http"
7848 , parser: successFromJson_
7949 }
8050
81−validatePath :: ServerMock Unit
82−validatePath = do
83− let invalidPath = error "invalid path"
84− missingPath = error "missing path"
85− req <- askReq
86− p <- liftMaybe missingPath $ req.path !! 0
87− case p == "compile" of
88− true -> pure unit
89− false -> throwError invalidPath
90−
91−validateMethod :: ServerMock Unit
92−validateMethod = askReq >>= case _ of
93− { method: Post } -> pure unit
94− _ -> throwError $ error "invalid method"
95−
96−validate :: ServerMock Unit
97−validate = validatePath *> validateMethod
98−
99−answer :: ServerMock Response
100−answer = do
101− res <- askRes
102− let json :: Json_
103− json = wrap $ successToJson { js: res, warnings: Nothing }
104− ok json
105−
106−runServerMock :: Code -> Request -> ResponseM
107−runServerMock res req = runReaderT (runReaderT (validate *> answer) res) req
51+data Route = Compile
52+
53+derive instance Generic Route _
54+
55+route :: RouteDuplex' Route
56+route = mkRoute
57+ { "Compile": "compile" / noArgs
58+ }
59+
60+
61+mkRouter
62+ :: forall a.
63+ String
64+ -> { method :: Method, route :: Route | a }
65+ -> Aff { headers :: ResponseHeaders, status :: Int , writeBody :: ServerResponse -> Aff Unit }
66+mkRouter js = case _ of
67+ { route: Compile, method: Post } -> ok $ toJson jsonEncoder $ successToJson { js, warnings: Nothing }
68+ _ -> notFound
10869
10970launchServerMock :: Code -> Aff (Effect Unit)
110−launchServerMock res = do
111− close <- liftEffect $ serve settings.port (\req -> runServerMock res req) $ pure unit
112− pure $ close $ pure unit
71+launchServerMock c = liftEffect do
72+ let router = mkRouter c
73+ close <- serve { port: settings.port } { route, router }
74+ pure $ close $ pure unit
11375
11476setupSrv :: forall b. Code -> Aff b -> Aff b
11577setupSrv code act = bracket (launchServerMock code) liftEffect (const act)