xref: /aosp_15_r20/external/okio/okio/src/jsMain/kotlin/okio/JsPlatform.kt (revision f9742813c14b702d71392179818a9e591da8620c)
1 /*
2  * Copyright (C) 2020 Square, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package okio
17 
18 /*
19  * This file exposes Node.js `os` and `path` APIs to Kotlin/JS, using reasonable default behaviors
20  * if those symbols aren't available.
21  *
22  * This was originally implemented using a Kotlin/JS [JsModule], but that broke browser builds
23  * because modules weren't available to browsers.
24  *
25  * https://nodejs.org/api/os.html
26  * https://github.com/browserify/path-browserify/blob/master/index.js
27  * https://github.com/CoderPuppy/os-browserify/blob/master/browser.js
28  */
29 
30 internal actual val PLATFORM_DIRECTORY_SEPARATOR: String
31   get() = (path?.sep) as? String ?: "/"
32 
33 private val os: dynamic
34   get() {
35     return try {
36       js("require('os')")
37     } catch (t: Throwable) {
38       null
39     }
40   }
41 
42 private val path: dynamic
43   get() {
44     return try {
45       js("require('path')")
46     } catch (t: Throwable) {
47       null
48     }
49   }
50 
51 internal val tmpdir: String
52   get() = os?.tmpdir() as? String ?: "/tmp"
53