aboutsummaryrefslogtreecommitdiff
path: root/okio/src/jsMain/kotlin/okio/fs.kt
blob: 17f965ffa29aef62a28475689175496acd2837bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * Copyright (C) 2020 Square, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * This class declares the subset of Node.js file system APIs that we need in Okio.
 *
 *
 * Why not Dukat?
 * --------------
 *
 * This file does manually what ideally [Dukat] would do automatically.
 *
 * Dukat's generated stubs need awkward call sites to disambiguate overloads. For example, to call
 * `mkdirSync()` we must specify an options parameter even though we just want the default:
 *
 *   mkdirSync(dir.toString(), options = undefined as MakeDirectoryOptions?)
 *
 * By defining our own externals, we can omit the unwanted optional parameter from the declaration.
 * This leads to nicer calling code!
 *
 *   mkdirSync(dir.toString())
 *
 * Dukat also gets the nullability wrong for `Dirent.readSync()`.
 *
 *
 * Why not Kotlinx-nodejs?
 * -----------------------
 *
 * Even better than using Dukat directly would be to use the [official artifact][kotlinx_nodejs],
 * itself generated with Dukat. We also don't use the official Node.js artifact for the reasons
 * above, and also because it has an unstable API.
 *
 *
 * Updating this file
 * ------------------
 *
 * To declare new external APIs, run Dukat to generate a full set of Node stubs. The easiest way to
 * do this is to add an NPM dependency on `@types/node` in `jsMain`, like this:
 *
 * ```
 * jsMain {
 *   ...
 *   dependencies {
 *     implementation(npm('@types/node', '14.14.16', true))
 *     ...
 *   }
 * }
 * ```
 *
 * This will create a file with a full set of APIs to copy-paste from.
 *
 * ```
 * okio/build/externals/okio-parent-okio/src/fs.fs.module_node.kt
 * ```
 *
 * [Dukat]: https://github.com/kotlin/dukat
 * [kotlinx_nodejs]: https://github.com/Kotlin/kotlinx-nodejs
 */
@file:JsModule("fs")
@file:JsNonModule
package okio

import org.khronos.webgl.Uint8Array
import kotlin.js.Date

internal external fun closeSync(fd: Number)

internal external fun mkdirSync(path: String): String?

internal external fun openSync(path: String, flags: String): Number

internal external fun opendirSync(path: String): Dir

internal external fun readSync(fd: Number, buffer: Uint8Array, offset: Number, length: Number, position: Number?): Number

internal external fun realpathSync(path: String): dynamic /* String | Buffer */

internal external fun renameSync(oldPath: String, newPath: String)

internal external fun rmdirSync(path: String)

internal external fun statSync(path: String): Stats

internal external fun unlinkSync(path: String)

internal external fun writeSync(fd: Number, buffer: Uint8Array): Number

internal open external class Dir {
  open var path: String
  open fun closeSync()
  // Note that dukat's signature of readSync() returns a non-nullable Dirent; that's incorrect.
  open fun readSync(): Dirent?
}

internal open external class Dirent {
  open fun isFile(): Boolean
  open fun isDirectory(): Boolean
  open fun isBlockDevice(): Boolean
  open fun isCharacterDevice(): Boolean
  open fun isSymbolicLink(): Boolean
  open fun isFIFO(): Boolean
  open fun isSocket(): Boolean
  open var name: String
}

internal external interface StatsBase<T> {
  fun isFile(): Boolean
  fun isDirectory(): Boolean
  fun isBlockDevice(): Boolean
  fun isCharacterDevice(): Boolean
  fun isSymbolicLink(): Boolean
  fun isFIFO(): Boolean
  fun isSocket(): Boolean
  var dev: T
  var ino: T
  var mode: T
  var nlink: T
  var uid: T
  var gid: T
  var rdev: T
  var size: T
  var blksize: T
  var blocks: T
  var atimeMs: T
  var mtimeMs: T
  var ctimeMs: T
  var birthtimeMs: T
  var atime: Date
  var mtime: Date
  var ctime: Date
  var birthtime: Date
}

internal open external class Stats : StatsBase<Number> {
  override fun isFile(): Boolean
  override fun isDirectory(): Boolean
  override fun isBlockDevice(): Boolean
  override fun isCharacterDevice(): Boolean
  override fun isSymbolicLink(): Boolean
  override fun isFIFO(): Boolean
  override fun isSocket(): Boolean
  override var dev: Number
  override var ino: Number
  override var mode: Number
  override var nlink: Number
  override var uid: Number
  override var gid: Number
  override var rdev: Number
  override var size: Number
  override var blksize: Number
  override var blocks: Number
  override var atimeMs: Number
  override var mtimeMs: Number
  override var ctimeMs: Number
  override var birthtimeMs: Number
  override var atime: Date
  override var mtime: Date
  override var ctime: Date
  override var birthtime: Date
}