Skip to content

Commit 97845c9

Browse files
authored
Merge pull request #13 from purescript-node/safe-platform
Make Node.Process.platform safe
2 parents dd006a9 + 5ffc117 commit 97845c9

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

src/Node/Platform.purs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,50 @@ module Node.Platform where
55
import Prelude
66
import Data.Maybe (Maybe(..))
77

8+
-- | See [the Node docs](https://nodejs.org/dist/latest-v6.x/docs/api/os.html#os_os_platform).
89
data Platform
9-
= Darwin
10+
= AIX
11+
| Darwin
1012
| FreeBSD
11-
| OpenBSD
1213
| Linux
14+
| OpenBSD
1315
| SunOS
1416
| Win32
17+
| Android
1518

1619
-- | The String representation for a platform, recognised by Node.js.
1720
toString :: Platform -> String
21+
toString AIX = "aix"
1822
toString Darwin = "darwin"
1923
toString FreeBSD = "freebsd"
20-
toString OpenBSD = "openbsd"
2124
toString Linux = "linux"
25+
toString OpenBSD = "openbsd"
2226
toString SunOS = "sunos"
2327
toString Win32 = "win32"
28+
toString Android = "android"
2429

30+
-- | Attempt to parse a `Platform` value from a string, in the format returned
31+
-- | by Node.js' `process.platform`.
2532
fromString :: String -> Maybe Platform
33+
fromString "aix" = Just AIX
2634
fromString "darwin" = Just Darwin
2735
fromString "freebsd" = Just FreeBSD
28-
fromString "openbsd" = Just OpenBSD
2936
fromString "linux" = Just Linux
37+
fromString "openbsd" = Just OpenBSD
3038
fromString "sunos" = Just SunOS
3139
fromString "win32" = Just Win32
40+
fromString "android" = Just Android
3241
fromString _ = Nothing
3342

3443
instance showPlatform :: Show Platform where
44+
show AIX = "AIX"
3545
show Darwin = "Darwin"
3646
show FreeBSD = "FreeBSD"
37-
show OpenBSD = "OpenBSD"
3847
show Linux = "Linux"
48+
show OpenBSD = "OpenBSD"
3949
show SunOS = "SunOS"
4050
show Win32 = "Win32"
51+
show Android = "Android"
4152

4253
derive instance eqPlatform :: Eq Platform
4354
derive instance ordPlatform :: Ord Platform

src/Node/Process.purs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import Control.Monad.Eff (Eff, kind Effect)
2929
import Control.Monad.Eff.Console (CONSOLE)
3030
import Control.Monad.Eff.Exception (EXCEPTION)
3131

32-
import Data.Maybe (Maybe, fromJust)
32+
import Data.Maybe (Maybe)
3333
import Data.Posix (Pid)
3434
import Data.Posix.Signal (Signal)
3535
import Data.Posix.Signal as Signal
@@ -40,8 +40,6 @@ import Node.Platform (Platform)
4040
import Node.Platform as Platform
4141
import Node.Stream (Readable, Writable)
4242

43-
import Partial.Unsafe (unsafePartial)
44-
4543
import Unsafe.Coerce (unsafeCoerce)
4644

4745
-- | An effect tracking interaction with the global `process` object.
@@ -114,8 +112,11 @@ foreign import setEnv :: forall eff. String -> String -> Eff (process :: PROCESS
114112
pid :: Pid
115113
pid = process.pid
116114

117-
platform :: Platform
118-
platform = unsafePartial $ fromJust $ Platform.fromString process.platform
115+
platform :: Maybe Platform
116+
platform = Platform.fromString platformStr
117+
118+
platformStr :: String
119+
platformStr = process.platform
119120

120121
-- | Cause the process to exit with the supplied integer code. An exit code
121122
-- | of 0 is normally considered successful, and anything else is considered a

0 commit comments

Comments
 (0)