Skip to content

Commit 74fce99

Browse files
committed
Make Node.Process.platform safe
Rather than assuming that the return value of `process.platform` will be in the set we recognise, make `Node.Process.platform` return `Nothing` if the platform is not recognised (for example, if a future version of Node.js adds more possibilities).
1 parent f5cd7bd commit 74fce99

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

src/Node/Platform.purs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,49 @@ 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
26-
fromString "darwin" = Just Darwin
27-
fromString "freebsd" = Just FreeBSD
28-
fromString "openbsd" = Just OpenBSD
29-
fromString "linux" = Just Linux
30-
fromString "sunos" = Just SunOS
31-
fromString "win32" = Just Win32
32-
fromString _ = Nothing
33+
fromString "aix" = AIX
34+
fromString "darwin" = Darwin
35+
fromString "freebsd" = FreeBSD
36+
fromString "linux" = Linux
37+
fromString "openbsd" = OpenBSD
38+
fromString "sunos" = SunOS
39+
fromString "win32" = Win32
40+
fromString "android" = Android
3341

3442
instance showPlatform :: Show Platform where
43+
show AIX = "AIX"
3544
show Darwin = "Darwin"
3645
show FreeBSD = "FreeBSD"
37-
show OpenBSD = "OpenBSD"
3846
show Linux = "Linux"
47+
show OpenBSD = "OpenBSD"
3948
show SunOS = "SunOS"
4049
show Win32 = "Win32"
50+
show Android = "Android"
4151

4252
derive instance eqPlatform :: Eq Platform
4353
derive instance ordPlatform :: Ord Platform

src/Node/Process.purs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,11 @@ foreign import setEnv :: forall eff. String -> String -> Eff (process :: PROCESS
114114
pid :: Pid
115115
pid = process.pid
116116

117-
platform :: Platform
118-
platform = unsafePartial $ fromJust $ Platform.fromString process.platform
117+
platform :: Maybe Platform
118+
platform = Platform.fromString platformStr
119+
120+
platformStr :: String
121+
platformStr = process.platform
119122

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

0 commit comments

Comments
 (0)