cline 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (299) hide show
  1. package/bin/cline +0 -0
  2. package/bin/cline-darwin-amd64 +0 -0
  3. package/bin/cline-darwin-arm64 +0 -0
  4. package/bin/cline-host +0 -0
  5. package/bin/cline-host-darwin-amd64 +0 -0
  6. package/bin/cline-host-darwin-arm64 +0 -0
  7. package/bin/cline-host-linux-amd64 +0 -0
  8. package/bin/cline-host-linux-arm64 +0 -0
  9. package/bin/cline-linux-amd64 +0 -0
  10. package/bin/cline-linux-arm64 +0 -0
  11. package/cline-core.js +24143 -16610
  12. package/extension/package.json +12 -4
  13. package/node_modules/b4a/LICENSE +201 -0
  14. package/node_modules/b4a/README.md +153 -0
  15. package/node_modules/b4a/browser.js +563 -0
  16. package/node_modules/b4a/index.js +188 -0
  17. package/node_modules/b4a/lib/ascii.js +31 -0
  18. package/node_modules/b4a/lib/base64.js +65 -0
  19. package/node_modules/b4a/lib/hex.js +51 -0
  20. package/node_modules/b4a/lib/utf16le.js +40 -0
  21. package/node_modules/b4a/lib/utf8.js +141 -0
  22. package/node_modules/b4a/package.json +49 -0
  23. package/node_modules/b4a/react-native.js +5 -0
  24. package/node_modules/bare-events/LICENSE +201 -0
  25. package/node_modules/bare-events/README.md +25 -0
  26. package/node_modules/bare-events/global.d.ts +15 -0
  27. package/node_modules/bare-events/global.js +5 -0
  28. package/node_modules/bare-events/index.d.ts +94 -0
  29. package/node_modules/bare-events/index.js +355 -0
  30. package/node_modules/bare-events/lib/errors.js +32 -0
  31. package/node_modules/bare-events/package.json +61 -0
  32. package/node_modules/bare-events/web.d.ts +79 -0
  33. package/node_modules/bare-events/web.js +347 -0
  34. package/node_modules/bare-fs/CMakeLists.txt +13 -0
  35. package/node_modules/bare-fs/LICENSE +201 -0
  36. package/node_modules/bare-fs/README.md +29 -0
  37. package/node_modules/bare-fs/binding.c +2238 -0
  38. package/node_modules/bare-fs/binding.js +1 -0
  39. package/node_modules/bare-fs/index.d.ts +1115 -0
  40. package/node_modules/bare-fs/index.js +2330 -0
  41. package/node_modules/bare-fs/lib/constants.d.ts +49 -0
  42. package/node_modules/bare-fs/lib/constants.js +49 -0
  43. package/node_modules/bare-fs/lib/errors.d.ts +11 -0
  44. package/node_modules/bare-fs/lib/errors.js +55 -0
  45. package/node_modules/bare-fs/package.json +71 -0
  46. package/node_modules/bare-fs/prebuilds/android-arm/bare-fs.bare +0 -0
  47. package/node_modules/bare-fs/prebuilds/android-arm64/bare-fs.bare +0 -0
  48. package/node_modules/bare-fs/prebuilds/android-ia32/bare-fs.bare +0 -0
  49. package/node_modules/bare-fs/prebuilds/android-x64/bare-fs.bare +0 -0
  50. package/node_modules/bare-fs/prebuilds/darwin-arm64/bare-fs.bare +0 -0
  51. package/node_modules/bare-fs/prebuilds/darwin-x64/bare-fs.bare +0 -0
  52. package/node_modules/bare-fs/prebuilds/ios-arm64/bare-fs.bare +0 -0
  53. package/node_modules/bare-fs/prebuilds/ios-arm64-simulator/bare-fs.bare +0 -0
  54. package/node_modules/bare-fs/prebuilds/ios-x64-simulator/bare-fs.bare +0 -0
  55. package/node_modules/bare-fs/prebuilds/linux-arm64/bare-fs.bare +0 -0
  56. package/node_modules/bare-fs/prebuilds/linux-x64/bare-fs.bare +0 -0
  57. package/node_modules/bare-fs/prebuilds/win32-arm64/bare-fs.bare +0 -0
  58. package/node_modules/bare-fs/prebuilds/win32-x64/bare-fs.bare +0 -0
  59. package/node_modules/bare-fs/promises.d.ts +282 -0
  60. package/node_modules/bare-fs/promises.js +90 -0
  61. package/node_modules/bare-os/CMakeLists.txt +13 -0
  62. package/node_modules/bare-os/LICENSE +201 -0
  63. package/node_modules/bare-os/README.md +11 -0
  64. package/node_modules/bare-os/binding.c +1119 -0
  65. package/node_modules/bare-os/binding.js +1 -0
  66. package/node_modules/bare-os/index.d.ts +109 -0
  67. package/node_modules/bare-os/index.js +99 -0
  68. package/node_modules/bare-os/lib/constants.js +6 -0
  69. package/node_modules/bare-os/lib/errors.js +22 -0
  70. package/node_modules/bare-os/package.json +46 -0
  71. package/node_modules/bare-os/prebuilds/android-arm/bare-os.bare +0 -0
  72. package/node_modules/bare-os/prebuilds/android-arm64/bare-os.bare +0 -0
  73. package/node_modules/bare-os/prebuilds/android-ia32/bare-os.bare +0 -0
  74. package/node_modules/bare-os/prebuilds/android-x64/bare-os.bare +0 -0
  75. package/node_modules/bare-os/prebuilds/darwin-arm64/bare-os.bare +0 -0
  76. package/node_modules/bare-os/prebuilds/darwin-x64/bare-os.bare +0 -0
  77. package/node_modules/bare-os/prebuilds/ios-arm64/bare-os.bare +0 -0
  78. package/node_modules/bare-os/prebuilds/ios-arm64-simulator/bare-os.bare +0 -0
  79. package/node_modules/bare-os/prebuilds/ios-x64-simulator/bare-os.bare +0 -0
  80. package/node_modules/bare-os/prebuilds/linux-arm64/bare-os.bare +0 -0
  81. package/node_modules/bare-os/prebuilds/linux-x64/bare-os.bare +0 -0
  82. package/node_modules/bare-os/prebuilds/win32-arm64/bare-os.bare +0 -0
  83. package/node_modules/bare-os/prebuilds/win32-x64/bare-os.bare +0 -0
  84. package/node_modules/bare-path/LICENSE +201 -0
  85. package/node_modules/bare-path/NOTICE +28 -0
  86. package/node_modules/bare-path/README.md +19 -0
  87. package/node_modules/bare-path/index.js +11 -0
  88. package/node_modules/bare-path/lib/constants.js +11 -0
  89. package/node_modules/bare-path/lib/posix.js +242 -0
  90. package/node_modules/bare-path/lib/shared.js +67 -0
  91. package/node_modules/bare-path/lib/win32.js +513 -0
  92. package/node_modules/bare-path/package.json +36 -0
  93. package/node_modules/bare-stream/LICENSE +201 -0
  94. package/node_modules/bare-stream/README.md +15 -0
  95. package/node_modules/bare-stream/global.js +8 -0
  96. package/node_modules/bare-stream/index.d.ts +237 -0
  97. package/node_modules/bare-stream/index.js +355 -0
  98. package/node_modules/bare-stream/package.json +61 -0
  99. package/node_modules/bare-stream/promises.js +3 -0
  100. package/node_modules/bare-stream/web.d.ts +73 -0
  101. package/node_modules/bare-stream/web.js +229 -0
  102. package/node_modules/bare-url/CMakeLists.txt +27 -0
  103. package/node_modules/bare-url/LICENSE +201 -0
  104. package/node_modules/bare-url/README.md +19 -0
  105. package/node_modules/bare-url/binding.c +188 -0
  106. package/node_modules/bare-url/binding.js +1 -0
  107. package/node_modules/bare-url/global.d.ts +12 -0
  108. package/node_modules/bare-url/global.js +2 -0
  109. package/node_modules/bare-url/index.d.ts +39 -0
  110. package/node_modules/bare-url/index.js +450 -0
  111. package/node_modules/bare-url/lib/errors.d.ts +8 -0
  112. package/node_modules/bare-url/lib/errors.js +38 -0
  113. package/node_modules/bare-url/lib/url-search-params.d.ts +21 -0
  114. package/node_modules/bare-url/lib/url-search-params.js +176 -0
  115. package/node_modules/bare-url/package.json +51 -0
  116. package/node_modules/bare-url/prebuilds/android-arm/bare-url.bare +0 -0
  117. package/node_modules/bare-url/prebuilds/android-arm64/bare-url.bare +0 -0
  118. package/node_modules/bare-url/prebuilds/android-ia32/bare-url.bare +0 -0
  119. package/node_modules/bare-url/prebuilds/android-x64/bare-url.bare +0 -0
  120. package/node_modules/bare-url/prebuilds/darwin-arm64/bare-url.bare +0 -0
  121. package/node_modules/bare-url/prebuilds/darwin-x64/bare-url.bare +0 -0
  122. package/node_modules/bare-url/prebuilds/ios-arm64/bare-url.bare +0 -0
  123. package/node_modules/bare-url/prebuilds/ios-arm64-simulator/bare-url.bare +0 -0
  124. package/node_modules/bare-url/prebuilds/ios-x64-simulator/bare-url.bare +0 -0
  125. package/node_modules/bare-url/prebuilds/linux-arm64/bare-url.bare +0 -0
  126. package/node_modules/bare-url/prebuilds/linux-x64/bare-url.bare +0 -0
  127. package/node_modules/bare-url/prebuilds/win32-arm64/bare-url.bare +0 -0
  128. package/node_modules/bare-url/prebuilds/win32-x64/bare-url.bare +0 -0
  129. package/node_modules/better-sqlite3/deps/download.sh +1 -1
  130. package/node_modules/better-sqlite3/deps/sqlite3/sqlite3.c +122 -81
  131. package/node_modules/better-sqlite3/deps/sqlite3/sqlite3.h +45 -45
  132. package/node_modules/better-sqlite3/lib/database.js +1 -1
  133. package/node_modules/better-sqlite3/package.json +4 -7
  134. package/node_modules/better-sqlite3/src/addon.cpp +47 -0
  135. package/node_modules/better-sqlite3/src/better_sqlite3.cpp +44 -2164
  136. package/node_modules/better-sqlite3/src/objects/backup.cpp +120 -0
  137. package/node_modules/better-sqlite3/src/objects/backup.hpp +36 -0
  138. package/node_modules/better-sqlite3/src/objects/database.cpp +417 -0
  139. package/node_modules/better-sqlite3/src/objects/database.hpp +103 -0
  140. package/node_modules/better-sqlite3/src/objects/statement-iterator.cpp +113 -0
  141. package/node_modules/better-sqlite3/src/objects/statement-iterator.hpp +50 -0
  142. package/node_modules/better-sqlite3/src/objects/statement.cpp +383 -0
  143. package/node_modules/better-sqlite3/src/objects/statement.hpp +58 -0
  144. package/node_modules/better-sqlite3/src/util/bind-map.cpp +73 -0
  145. package/node_modules/better-sqlite3/src/util/binder.cpp +193 -0
  146. package/node_modules/better-sqlite3/src/util/constants.cpp +172 -0
  147. package/node_modules/better-sqlite3/src/util/custom-aggregate.cpp +121 -0
  148. package/node_modules/better-sqlite3/src/util/custom-function.cpp +59 -0
  149. package/node_modules/better-sqlite3/src/util/custom-table.cpp +409 -0
  150. package/node_modules/better-sqlite3/src/util/data-converter.cpp +17 -0
  151. package/node_modules/better-sqlite3/src/util/data.cpp +194 -0
  152. package/node_modules/better-sqlite3/src/util/helpers.cpp +109 -0
  153. package/node_modules/better-sqlite3/src/util/macros.cpp +63 -0
  154. package/node_modules/better-sqlite3/src/util/query-macros.cpp +71 -0
  155. package/node_modules/better-sqlite3/src/util/row-builder.cpp +49 -0
  156. package/node_modules/events-universal/LICENSE +201 -0
  157. package/node_modules/events-universal/README.md +17 -0
  158. package/node_modules/events-universal/bare.js +1 -0
  159. package/node_modules/events-universal/default.js +1 -0
  160. package/node_modules/events-universal/index.js +1 -0
  161. package/node_modules/events-universal/package.json +39 -0
  162. package/node_modules/events-universal/react-native.js +1 -0
  163. package/node_modules/{fs-constants → fast-fifo}/LICENSE +1 -1
  164. package/node_modules/fast-fifo/README.md +78 -0
  165. package/node_modules/fast-fifo/fixed-size.js +39 -0
  166. package/node_modules/fast-fifo/index.js +48 -0
  167. package/node_modules/fast-fifo/package.json +28 -0
  168. package/node_modules/{base64-js → streamx}/LICENSE +1 -1
  169. package/node_modules/streamx/README.md +489 -0
  170. package/node_modules/streamx/index.js +1184 -0
  171. package/node_modules/streamx/package.json +34 -0
  172. package/node_modules/tar-fs/README.md +23 -34
  173. package/node_modules/tar-fs/index.js +211 -184
  174. package/node_modules/tar-fs/package.json +27 -7
  175. package/node_modules/tar-stream/README.md +34 -19
  176. package/node_modules/tar-stream/constants.js +14 -0
  177. package/node_modules/tar-stream/extract.js +341 -192
  178. package/node_modules/tar-stream/headers.js +201 -175
  179. package/node_modules/tar-stream/pack.js +226 -194
  180. package/node_modules/tar-stream/package.json +23 -46
  181. package/node_modules/text-decoder/LICENSE +201 -0
  182. package/node_modules/text-decoder/README.md +36 -0
  183. package/node_modules/text-decoder/index.js +64 -0
  184. package/node_modules/text-decoder/lib/browser-decoder.js +17 -0
  185. package/node_modules/text-decoder/lib/pass-through-decoder.js +19 -0
  186. package/node_modules/text-decoder/lib/utf8-decoder.js +104 -0
  187. package/node_modules/text-decoder/package.json +38 -0
  188. package/package.json +1 -1
  189. package/proto/cline/account.proto +10 -8
  190. package/proto/cline/browser.proto +3 -1
  191. package/proto/cline/checkpoints.proto +5 -3
  192. package/proto/cline/commands.proto +4 -2
  193. package/proto/cline/common.proto +5 -7
  194. package/proto/cline/dictation.proto +3 -1
  195. package/proto/cline/file.proto +36 -34
  196. package/proto/cline/hooks.proto +3 -2
  197. package/proto/cline/mcp.proto +6 -4
  198. package/proto/cline/models.proto +189 -19
  199. package/proto/cline/oca_account.proto +6 -7
  200. package/proto/cline/slash.proto +3 -1
  201. package/proto/cline/state.proto +144 -292
  202. package/proto/cline/task.proto +4 -2
  203. package/proto/cline/ui.proto +25 -24
  204. package/proto/cline/web.proto +3 -1
  205. package/proto/descriptor_set.pb +0 -0
  206. package/proto/host/diff.proto +5 -4
  207. package/proto/host/env.proto +8 -7
  208. package/proto/host/testing.proto +3 -3
  209. package/proto/host/window.proto +2 -2
  210. package/proto/host/workspace.proto +13 -12
  211. package/node_modules/base64-js/README.md +0 -34
  212. package/node_modules/base64-js/base64js.min.js +0 -1
  213. package/node_modules/base64-js/index.d.ts +0 -3
  214. package/node_modules/base64-js/index.js +0 -150
  215. package/node_modules/base64-js/package.json +0 -47
  216. package/node_modules/better-sqlite3/src/better_sqlite3.hpp +0 -1036
  217. package/node_modules/bl/.travis.yml +0 -17
  218. package/node_modules/bl/BufferList.js +0 -396
  219. package/node_modules/bl/LICENSE.md +0 -13
  220. package/node_modules/bl/README.md +0 -247
  221. package/node_modules/bl/bl.js +0 -84
  222. package/node_modules/bl/package.json +0 -37
  223. package/node_modules/bl/test/convert.js +0 -21
  224. package/node_modules/bl/test/indexOf.js +0 -492
  225. package/node_modules/bl/test/isBufferList.js +0 -32
  226. package/node_modules/bl/test/test.js +0 -869
  227. package/node_modules/buffer/AUTHORS.md +0 -70
  228. package/node_modules/buffer/LICENSE +0 -21
  229. package/node_modules/buffer/README.md +0 -410
  230. package/node_modules/buffer/index.d.ts +0 -186
  231. package/node_modules/buffer/index.js +0 -1817
  232. package/node_modules/buffer/package.json +0 -96
  233. package/node_modules/chownr/LICENSE +0 -15
  234. package/node_modules/chownr/README.md +0 -3
  235. package/node_modules/chownr/chownr.js +0 -167
  236. package/node_modules/chownr/package.json +0 -29
  237. package/node_modules/fs-constants/README.md +0 -26
  238. package/node_modules/fs-constants/browser.js +0 -1
  239. package/node_modules/fs-constants/index.js +0 -1
  240. package/node_modules/fs-constants/package.json +0 -19
  241. package/node_modules/ieee754/LICENSE +0 -11
  242. package/node_modules/ieee754/README.md +0 -51
  243. package/node_modules/ieee754/index.d.ts +0 -10
  244. package/node_modules/ieee754/index.js +0 -85
  245. package/node_modules/ieee754/package.json +0 -52
  246. package/node_modules/inherits/LICENSE +0 -16
  247. package/node_modules/inherits/README.md +0 -42
  248. package/node_modules/inherits/inherits.js +0 -9
  249. package/node_modules/inherits/inherits_browser.js +0 -27
  250. package/node_modules/inherits/package.json +0 -29
  251. package/node_modules/readable-stream/CONTRIBUTING.md +0 -38
  252. package/node_modules/readable-stream/GOVERNANCE.md +0 -136
  253. package/node_modules/readable-stream/LICENSE +0 -47
  254. package/node_modules/readable-stream/README.md +0 -106
  255. package/node_modules/readable-stream/errors-browser.js +0 -127
  256. package/node_modules/readable-stream/errors.js +0 -116
  257. package/node_modules/readable-stream/experimentalWarning.js +0 -17
  258. package/node_modules/readable-stream/lib/_stream_duplex.js +0 -126
  259. package/node_modules/readable-stream/lib/_stream_passthrough.js +0 -37
  260. package/node_modules/readable-stream/lib/_stream_readable.js +0 -1027
  261. package/node_modules/readable-stream/lib/_stream_transform.js +0 -190
  262. package/node_modules/readable-stream/lib/_stream_writable.js +0 -641
  263. package/node_modules/readable-stream/lib/internal/streams/async_iterator.js +0 -180
  264. package/node_modules/readable-stream/lib/internal/streams/buffer_list.js +0 -183
  265. package/node_modules/readable-stream/lib/internal/streams/destroy.js +0 -96
  266. package/node_modules/readable-stream/lib/internal/streams/end-of-stream.js +0 -86
  267. package/node_modules/readable-stream/lib/internal/streams/from-browser.js +0 -3
  268. package/node_modules/readable-stream/lib/internal/streams/from.js +0 -52
  269. package/node_modules/readable-stream/lib/internal/streams/pipeline.js +0 -86
  270. package/node_modules/readable-stream/lib/internal/streams/state.js +0 -22
  271. package/node_modules/readable-stream/lib/internal/streams/stream-browser.js +0 -1
  272. package/node_modules/readable-stream/lib/internal/streams/stream.js +0 -1
  273. package/node_modules/readable-stream/package.json +0 -68
  274. package/node_modules/readable-stream/readable-browser.js +0 -9
  275. package/node_modules/readable-stream/readable.js +0 -16
  276. package/node_modules/string_decoder/LICENSE +0 -48
  277. package/node_modules/string_decoder/README.md +0 -47
  278. package/node_modules/string_decoder/lib/string_decoder.js +0 -296
  279. package/node_modules/string_decoder/package.json +0 -34
  280. package/node_modules/tar-fs/.travis.yml +0 -6
  281. package/node_modules/tar-fs/test/fixtures/a/hello.txt +0 -1
  282. package/node_modules/tar-fs/test/fixtures/b/a/test.txt +0 -1
  283. package/node_modules/tar-fs/test/fixtures/d/file1 +0 -0
  284. package/node_modules/tar-fs/test/fixtures/d/file2 +0 -0
  285. package/node_modules/tar-fs/test/fixtures/d/sub-dir/file5 +0 -0
  286. package/node_modules/tar-fs/test/fixtures/d/sub-files/file3 +0 -0
  287. package/node_modules/tar-fs/test/fixtures/d/sub-files/file4 +0 -0
  288. package/node_modules/tar-fs/test/fixtures/e/directory/.ignore +0 -0
  289. package/node_modules/tar-fs/test/fixtures/e/file +0 -0
  290. package/node_modules/tar-fs/test/fixtures/invalid.tar +0 -0
  291. package/node_modules/tar-fs/test/index.js +0 -346
  292. package/node_modules/tar-stream/sandbox.js +0 -11
  293. package/node_modules/util-deprecate/History.md +0 -16
  294. package/node_modules/util-deprecate/LICENSE +0 -24
  295. package/node_modules/util-deprecate/README.md +0 -53
  296. package/node_modules/util-deprecate/browser.js +0 -67
  297. package/node_modules/util-deprecate/node.js +0 -6
  298. package/node_modules/util-deprecate/package.json +0 -27
  299. package/test-startup.sh +0 -14
@@ -0,0 +1,2330 @@
1
+ const FIFO = require('fast-fifo')
2
+ const EventEmitter = require('bare-events')
3
+ const path = require('bare-path')
4
+ const { fileURLToPath } = require('bare-url')
5
+ const { Readable, Writable } = require('bare-stream')
6
+ const binding = require('./binding')
7
+ const constants = require('./lib/constants')
8
+ const FileError = require('./lib/errors')
9
+
10
+ const isWindows = Bare.platform === 'win32'
11
+
12
+ exports.constants = constants
13
+
14
+ class FileRequest {
15
+ static _free = []
16
+
17
+ static borrow() {
18
+ if (this._free.length > 0) return this._free.pop()
19
+ return new FileRequest()
20
+ }
21
+
22
+ static return(req) {
23
+ if (this._free.length < 32) this._free.push(req.reset())
24
+ else req.destroy()
25
+ }
26
+
27
+ constructor() {
28
+ this._reset()
29
+ this._handle = binding.requestInit(this, this._onresult)
30
+ }
31
+
32
+ get handle() {
33
+ return this._handle
34
+ }
35
+
36
+ retain(value) {
37
+ this._retain = value // Tie the lifetime of `value` to the lifetime of `this`
38
+ }
39
+
40
+ reset() {
41
+ if (this._handle === null) return this
42
+
43
+ binding.requestReset(this._handle)
44
+
45
+ this._reset()
46
+
47
+ return this
48
+ }
49
+
50
+ destroy() {
51
+ if (this._handle === null) return this
52
+
53
+ binding.requestDestroy(this._handle)
54
+
55
+ this._reset()
56
+ this._handle = null
57
+
58
+ return this
59
+ }
60
+
61
+ then(resolve, reject) {
62
+ return this._promise.then(resolve, reject)
63
+ }
64
+
65
+ return() {
66
+ if (this._handle === null) return this
67
+
68
+ FileRequest.return(this)
69
+
70
+ return this
71
+ }
72
+
73
+ [Symbol.dispose]() {
74
+ this.return()
75
+ }
76
+
77
+ _reset() {
78
+ const { promise, resolve, reject } = Promise.withResolvers()
79
+
80
+ this._promise = promise
81
+ this._resolve = resolve
82
+ this._reject = reject
83
+ this._retain = null
84
+ }
85
+
86
+ _onresult(err, status) {
87
+ if (err) this._reject(err)
88
+ else this._resolve(status)
89
+ }
90
+ }
91
+
92
+ function ok(result, cb) {
93
+ if (typeof result === 'function') {
94
+ cb = result
95
+ result = undefined
96
+ }
97
+
98
+ if (cb) cb(null, result)
99
+ else return result
100
+ }
101
+
102
+ function fail(err, cb) {
103
+ if (cb) cb(err)
104
+ else throw err
105
+ }
106
+
107
+ function done(err, result, cb) {
108
+ if (typeof result === 'function') {
109
+ cb = result
110
+ result = undefined
111
+ }
112
+
113
+ if (err) fail(err, cb)
114
+ else return ok(result, cb)
115
+ }
116
+
117
+ async function open(filepath, flags = 'r', mode = 0o666, cb) {
118
+ if (typeof flags === 'function') {
119
+ cb = flags
120
+ flags = 'r'
121
+ mode = 0o666
122
+ } else if (typeof mode === 'function') {
123
+ cb = mode
124
+ mode = 0o666
125
+ }
126
+
127
+ if (typeof flags === 'string') flags = toFlags(flags)
128
+ if (typeof mode === 'string') mode = toMode(mode)
129
+
130
+ filepath = toNamespacedPath(filepath)
131
+
132
+ using req = FileRequest.borrow()
133
+
134
+ let fd
135
+ let err = null
136
+ try {
137
+ binding.open(req.handle, filepath, flags, mode)
138
+
139
+ fd = await req
140
+ } catch (e) {
141
+ err = new FileError(e.message, {
142
+ operation: 'open',
143
+ code: e.code,
144
+ path: filepath
145
+ })
146
+ }
147
+
148
+ return done(err, fd, cb)
149
+ }
150
+
151
+ function openSync(filepath, flags = 'r', mode = 0o666) {
152
+ if (typeof flags === 'string') flags = toFlags(flags)
153
+ if (typeof mode === 'string') mode = toMode(mode)
154
+
155
+ filepath = toNamespacedPath(filepath)
156
+
157
+ using req = FileRequest.borrow()
158
+
159
+ try {
160
+ return binding.openSync(req.handle, filepath, flags, mode)
161
+ } catch (e) {
162
+ throw new FileError(e.message, {
163
+ operation: 'open',
164
+ code: e.code,
165
+ path: filepath
166
+ })
167
+ }
168
+ }
169
+
170
+ async function close(fd, cb) {
171
+ using req = FileRequest.borrow()
172
+
173
+ let err = null
174
+ try {
175
+ binding.close(req.handle, fd)
176
+
177
+ await req
178
+ } catch (e) {
179
+ err = new FileError(e.message, { operation: 'close', code: e.code, fd })
180
+ }
181
+
182
+ return done(err, cb)
183
+ }
184
+
185
+ function closeSync(fd) {
186
+ using req = FileRequest.borrow()
187
+
188
+ try {
189
+ binding.closeSync(req.handle, fd)
190
+ } catch (e) {
191
+ throw new FileError(e.message, { operation: 'close', code: e.code, fd })
192
+ }
193
+ }
194
+
195
+ async function access(filepath, mode = constants.F_OK, cb) {
196
+ if (typeof mode === 'function') {
197
+ cb = mode
198
+ mode = constants.F_OK
199
+ }
200
+
201
+ filepath = toNamespacedPath(filepath)
202
+
203
+ using req = FileRequest.borrow()
204
+
205
+ let err = null
206
+ try {
207
+ binding.access(req.handle, filepath, mode)
208
+
209
+ await req
210
+ } catch (e) {
211
+ err = new FileError(e.message, {
212
+ operation: 'access',
213
+ code: e.code,
214
+ path: filepath
215
+ })
216
+ }
217
+
218
+ return done(err, cb)
219
+ }
220
+
221
+ function accessSync(filepath, mode = constants.F_OK) {
222
+ filepath = toNamespacedPath(filepath)
223
+
224
+ using req = FileRequest.borrow()
225
+
226
+ try {
227
+ binding.accessSync(req.handle, filepath, mode)
228
+ } catch (e) {
229
+ throw new FileError(e.message, {
230
+ operation: 'access',
231
+ code: e.code,
232
+ path: filepath
233
+ })
234
+ }
235
+ }
236
+
237
+ async function exists(filepath, cb) {
238
+ let ok = true
239
+ try {
240
+ await access(filepath)
241
+ } catch {
242
+ ok = false
243
+ }
244
+
245
+ return done(null, ok, cb)
246
+ }
247
+
248
+ function existsSync(filepath) {
249
+ try {
250
+ accessSync(filepath)
251
+ } catch {
252
+ return false
253
+ }
254
+
255
+ return true
256
+ }
257
+
258
+ async function read(fd, buffer, offset = 0, len = buffer.byteLength - offset, pos = -1, cb) {
259
+ if (typeof offset === 'function') {
260
+ cb = offset
261
+ offset = 0
262
+ len = buffer.byteLength
263
+ pos = -1
264
+ } else if (typeof len === 'function') {
265
+ cb = len
266
+ len = buffer.byteLength - offset
267
+ pos = -1
268
+ } else if (typeof pos === 'function') {
269
+ cb = pos
270
+ pos = -1
271
+ }
272
+
273
+ if (typeof pos !== 'number') pos = -1
274
+
275
+ using req = FileRequest.borrow()
276
+
277
+ let bytes
278
+ let err = null
279
+ try {
280
+ binding.read(req.handle, fd, buffer, offset, len, pos)
281
+
282
+ bytes = await req
283
+ } catch (e) {
284
+ err = new FileError(e.message, { operation: 'read', code: e.code, fd })
285
+ }
286
+
287
+ return done(err, bytes, cb)
288
+ }
289
+
290
+ function readSync(fd, buffer, offset = 0, len = buffer.byteLength - offset, pos = -1) {
291
+ using req = FileRequest.borrow()
292
+
293
+ try {
294
+ return binding.readSync(req.handle, fd, buffer, offset, len, pos)
295
+ } catch (e) {
296
+ throw new FileError(e.message, { operation: 'read', code: e.code, fd })
297
+ }
298
+ }
299
+
300
+ async function readv(fd, buffers, pos = -1, cb) {
301
+ if (typeof pos === 'function') {
302
+ cb = pos
303
+ pos = -1
304
+ }
305
+
306
+ if (typeof pos !== 'number') pos = -1
307
+
308
+ using req = FileRequest.borrow()
309
+
310
+ let bytes
311
+ let err = null
312
+ try {
313
+ binding.readv(req.handle, fd, buffers, pos)
314
+
315
+ bytes = await req
316
+ } catch (e) {
317
+ err = new FileError(e.message, { operation: 'readv', code: e.code, fd })
318
+ }
319
+
320
+ return done(err, bytes, cb)
321
+ }
322
+
323
+ function readvSync(fd, buffers, pos = -1) {
324
+ if (typeof pos !== 'number') pos = -1
325
+
326
+ using req = FileRequest.borrow()
327
+
328
+ try {
329
+ return binding.readvSync(req.handle, fd, buffers, pos)
330
+ } catch (e) {
331
+ throw new FileError(e.message, { operation: 'readv', code: e.code, fd })
332
+ }
333
+ }
334
+
335
+ async function write(fd, data, offset = 0, len, pos = -1, cb) {
336
+ if (typeof data === 'string') {
337
+ let encoding = len
338
+ cb = pos
339
+ pos = offset
340
+
341
+ if (typeof pos === 'function') {
342
+ cb = pos
343
+ pos = -1
344
+ encoding = 'utf8'
345
+ } else if (typeof encoding === 'function') {
346
+ cb = encoding
347
+ encoding = 'utf8'
348
+ }
349
+
350
+ if (typeof pos === 'string') {
351
+ encoding = pos
352
+ pos = -1
353
+ }
354
+
355
+ data = Buffer.from(data, encoding)
356
+ offset = 0
357
+ len = data.byteLength
358
+ } else if (typeof offset === 'function') {
359
+ cb = offset
360
+ offset = 0
361
+ len = data.byteLength
362
+ pos = -1
363
+ } else if (typeof len === 'function') {
364
+ cb = len
365
+ len = data.byteLength - offset
366
+ pos = -1
367
+ } else if (typeof pos === 'function') {
368
+ cb = pos
369
+ pos = -1
370
+ }
371
+
372
+ if (typeof len !== 'number') len = data.byteLength - offset
373
+ if (typeof pos !== 'number') pos = -1
374
+
375
+ using req = FileRequest.borrow()
376
+
377
+ let bytes
378
+ let err = null
379
+ try {
380
+ binding.write(req.handle, fd, data, offset, len, pos)
381
+
382
+ bytes = await req
383
+ } catch (e) {
384
+ err = new FileError(e.message, { operation: 'write', code: e.code, fd })
385
+ }
386
+
387
+ return done(err, bytes, cb)
388
+ }
389
+
390
+ function writeSync(fd, data, offset = 0, len, pos = -1) {
391
+ if (typeof data === 'string') {
392
+ let encoding = len
393
+ pos = offset
394
+
395
+ if (typeof pos === 'string') {
396
+ encoding = pos
397
+ pos = -1
398
+ }
399
+
400
+ data = Buffer.from(data, encoding)
401
+ offset = 0
402
+ len = data.byteLength
403
+ }
404
+
405
+ if (typeof len !== 'number') len = data.byteLength - offset
406
+ if (typeof pos !== 'number') pos = -1
407
+
408
+ using req = FileRequest.borrow()
409
+
410
+ try {
411
+ return binding.writeSync(req.handle, fd, data, offset, len, pos)
412
+ } catch (e) {
413
+ throw new FileError(e.message, { operation: 'write', code: e.code, fd })
414
+ }
415
+ }
416
+
417
+ async function writev(fd, buffers, pos = -1, cb) {
418
+ if (typeof pos === 'function') {
419
+ cb = pos
420
+ pos = -1
421
+ }
422
+
423
+ if (typeof pos !== 'number') pos = -1
424
+
425
+ using req = FileRequest.borrow()
426
+
427
+ let bytes
428
+ let err = null
429
+ try {
430
+ binding.writev(req.handle, fd, buffers, pos)
431
+
432
+ bytes = await req
433
+ } catch (e) {
434
+ err = new FileError(e.message, { operation: 'writev', code: e.code, fd })
435
+ }
436
+
437
+ return done(err, bytes, cb)
438
+ }
439
+
440
+ function writevSync(fd, buffers, pos = -1) {
441
+ if (typeof pos !== 'number') pos = -1
442
+
443
+ using req = FileRequest.borrow()
444
+
445
+ try {
446
+ return binding.writevSync(req.handle, fd, buffers, pos)
447
+ } catch (e) {
448
+ throw new FileError(e.message, { operation: 'writev', code: e.code, fd })
449
+ }
450
+ }
451
+
452
+ async function stat(filepath, cb) {
453
+ filepath = toNamespacedPath(filepath)
454
+
455
+ using req = FileRequest.borrow()
456
+
457
+ let st
458
+ let err = null
459
+ try {
460
+ binding.stat(req.handle, filepath)
461
+
462
+ await req
463
+
464
+ st = new Stats(...binding.requestResultStat(req.handle))
465
+ } catch (e) {
466
+ err = new FileError(e.message, {
467
+ operation: 'stat',
468
+ code: e.code,
469
+ path: filepath
470
+ })
471
+ }
472
+
473
+ return done(err, st, cb)
474
+ }
475
+
476
+ function statSync(filepath) {
477
+ filepath = toNamespacedPath(filepath)
478
+
479
+ using req = FileRequest.borrow()
480
+
481
+ try {
482
+ binding.statSync(req.handle, filepath)
483
+
484
+ return new Stats(...binding.requestResultStat(req.handle))
485
+ } catch (e) {
486
+ throw new FileError(e.message, {
487
+ operation: 'stat',
488
+ code: e.code,
489
+ path: filepath
490
+ })
491
+ }
492
+ }
493
+
494
+ async function lstat(filepath, cb) {
495
+ filepath = toNamespacedPath(filepath)
496
+
497
+ using req = FileRequest.borrow()
498
+
499
+ let st
500
+ let err = null
501
+ try {
502
+ binding.lstat(req.handle, filepath)
503
+
504
+ await req
505
+
506
+ st = new Stats(...binding.requestResultStat(req.handle))
507
+ } catch (e) {
508
+ err = new FileError(e.message, {
509
+ operation: 'lstat',
510
+ code: e.code,
511
+ path: filepath
512
+ })
513
+ }
514
+
515
+ return done(err, st, cb)
516
+ }
517
+
518
+ function lstatSync(filepath) {
519
+ filepath = toNamespacedPath(filepath)
520
+
521
+ using req = FileRequest.borrow()
522
+
523
+ try {
524
+ binding.lstatSync(req.handle, filepath)
525
+
526
+ return new Stats(...binding.requestResultStat(req.handle))
527
+ } catch (e) {
528
+ throw new FileError(e.message, {
529
+ operation: 'lstat',
530
+ code: e.code,
531
+ path: filepath
532
+ })
533
+ }
534
+ }
535
+
536
+ async function fstat(fd, cb) {
537
+ using req = FileRequest.borrow()
538
+
539
+ let st
540
+ let err = null
541
+ try {
542
+ binding.fstat(req.handle, fd)
543
+
544
+ await req
545
+
546
+ st = new Stats(...binding.requestResultStat(req.handle))
547
+ } catch (e) {
548
+ err = new FileError(e.message, { operation: 'fstat', code: e.code, fd })
549
+ }
550
+
551
+ return done(err, st, cb)
552
+ }
553
+
554
+ function fstatSync(fd) {
555
+ using req = FileRequest.borrow()
556
+
557
+ try {
558
+ binding.fstatSync(req.handle, fd)
559
+
560
+ return new Stats(...binding.requestResultStat(req.handle))
561
+ } catch (e) {
562
+ throw new FileError(e.message, { operation: 'fstat', code: e.code, fd })
563
+ }
564
+ }
565
+
566
+ async function ftruncate(fd, len = 0, cb) {
567
+ if (typeof len === 'function') {
568
+ cb = len
569
+ len = 0
570
+ }
571
+
572
+ if (typeof len !== 'number') len = 0
573
+
574
+ using req = FileRequest.borrow()
575
+
576
+ let err = null
577
+ try {
578
+ binding.ftruncate(req.handle, fd, len)
579
+
580
+ await req
581
+ } catch (e) {
582
+ err = new FileError(e.message, { operation: 'ftruncate', code: e.code, fd })
583
+ }
584
+
585
+ return done(err, cb)
586
+ }
587
+
588
+ function ftruncateSync(fd, len = 0) {
589
+ if (typeof len !== 'number') len = 0
590
+
591
+ using req = FileRequest.borrow()
592
+
593
+ try {
594
+ binding.ftruncateSync(req.handle, fd, len)
595
+ } catch (e) {
596
+ throw new FileError(e.message, { operation: 'ftruncate', code: e.code, fd })
597
+ }
598
+ }
599
+
600
+ async function chmod(filepath, mode, cb) {
601
+ if (typeof mode === 'string') mode = toMode(mode)
602
+
603
+ filepath = toNamespacedPath(filepath)
604
+
605
+ using req = FileRequest.borrow()
606
+
607
+ let err = null
608
+ try {
609
+ binding.chmod(req.handle, filepath, mode)
610
+
611
+ await req
612
+ } catch (e) {
613
+ err = new FileError(e.message, {
614
+ operation: 'chmod',
615
+ code: e.code,
616
+ path: filepath
617
+ })
618
+ }
619
+
620
+ return done(err, cb)
621
+ }
622
+
623
+ function chmodSync(filepath, mode) {
624
+ if (typeof mode === 'string') mode = toMode(mode)
625
+
626
+ filepath = toNamespacedPath(filepath)
627
+
628
+ using req = FileRequest.borrow()
629
+
630
+ try {
631
+ binding.chmodSync(req.handle, filepath, mode)
632
+ } catch (e) {
633
+ throw new FileError(e.message, {
634
+ operation: 'chmod',
635
+ code: e.code,
636
+ path: filepath
637
+ })
638
+ }
639
+ }
640
+
641
+ async function fchmod(fd, mode, cb) {
642
+ if (typeof mode === 'string') mode = toMode(mode)
643
+
644
+ using req = FileRequest.borrow()
645
+
646
+ let err = null
647
+ try {
648
+ binding.fchmod(req.handle, fd, mode)
649
+
650
+ await req
651
+ } catch (e) {
652
+ err = new FileError(e.message, { operation: 'fchmod', code: e.code, fd })
653
+ }
654
+
655
+ return done(err, cb)
656
+ }
657
+
658
+ function fchmodSync(fd, mode) {
659
+ if (typeof mode === 'string') mode = toMode(mode)
660
+
661
+ using req = FileRequest.borrow()
662
+
663
+ try {
664
+ binding.fchmodSync(req.handle, fd, mode)
665
+ } catch (e) {
666
+ throw new FileError(e.message, { operation: 'fchmod', code: e.code, fd })
667
+ }
668
+ }
669
+
670
+ async function utimes(filepath, atime, mtime, cb) {
671
+ if (typeof atime !== 'number') atime = atime.getTime() / 1000
672
+ if (typeof mtime !== 'number') mtime = mtime.getTime() / 1000
673
+
674
+ filepath = toNamespacedPath(filepath)
675
+
676
+ using req = FileRequest.borrow()
677
+
678
+ let err = null
679
+ try {
680
+ binding.utimes(req.handle, filepath, atime, mtime)
681
+
682
+ await req
683
+ } catch (e) {
684
+ err = new FileError(e.message, {
685
+ operation: 'utimes',
686
+ code: e.code,
687
+ path: filepath
688
+ })
689
+ }
690
+
691
+ return done(err, cb)
692
+ }
693
+
694
+ function utimesSync(filepath, atime, mtime) {
695
+ if (typeof atime !== 'number') atime = atime.getTime() / 1000
696
+ if (typeof mtime !== 'number') mtime = mtime.getTime() / 1000
697
+
698
+ filepath = toNamespacedPath(filepath)
699
+
700
+ using req = FileRequest.borrow()
701
+
702
+ try {
703
+ binding.utimesSync(req.handle, filepath, atime, mtime)
704
+ } catch (e) {
705
+ throw new FileError(e.message, {
706
+ operation: 'utimes',
707
+ code: e.code,
708
+ path: filepath
709
+ })
710
+ }
711
+ }
712
+
713
+ async function mkdir(filepath, opts, cb) {
714
+ if (typeof opts === 'function') {
715
+ cb = opts
716
+ opts = { mode: 0o777 }
717
+ }
718
+
719
+ if (typeof opts === 'number') opts = { mode: opts }
720
+ else if (!opts) opts = {}
721
+
722
+ const mode = typeof opts.mode === 'number' ? opts.mode : 0o777
723
+
724
+ filepath = toNamespacedPath(filepath)
725
+
726
+ if (opts.recursive) {
727
+ let err = null
728
+ try {
729
+ try {
730
+ await mkdir(filepath, { mode })
731
+ } catch (err) {
732
+ if (err.code !== 'ENOENT') {
733
+ if (!(await stat(filepath)).isDirectory()) throw err
734
+ } else {
735
+ while (filepath.endsWith(path.sep)) filepath = filepath.slice(0, -1)
736
+ const i = filepath.lastIndexOf(path.sep)
737
+ if (i <= 0) throw err
738
+
739
+ await mkdir(filepath.slice(0, i), { mode, recursive: true })
740
+
741
+ try {
742
+ await mkdir(filepath, { mode })
743
+ } catch (err) {
744
+ if (!(await stat(filepath)).isDirectory()) throw err
745
+ }
746
+ }
747
+ }
748
+ } catch (e) {
749
+ err = e
750
+ }
751
+
752
+ return done(err, cb)
753
+ }
754
+
755
+ using req = FileRequest.borrow()
756
+
757
+ let err = null
758
+ try {
759
+ binding.mkdir(req.handle, filepath, mode)
760
+
761
+ await req
762
+ } catch (e) {
763
+ err = new FileError(e.message, {
764
+ operation: 'mkdir',
765
+ code: e.code,
766
+ path: filepath
767
+ })
768
+ }
769
+
770
+ return done(err, cb)
771
+ }
772
+
773
+ function mkdirSync(filepath, opts) {
774
+ if (typeof opts === 'number') opts = { mode: opts }
775
+ else if (!opts) opts = {}
776
+
777
+ const mode = typeof opts.mode === 'number' ? opts.mode : 0o777
778
+
779
+ filepath = toNamespacedPath(filepath)
780
+
781
+ if (opts.recursive) {
782
+ try {
783
+ mkdirSync(filepath, { mode })
784
+ } catch (err) {
785
+ if (err.code !== 'ENOENT') {
786
+ if (!statSync(filepath).isDirectory()) throw err
787
+ } else {
788
+ while (filepath.endsWith(path.sep)) filepath = filepath.slice(0, -1)
789
+ const i = filepath.lastIndexOf(path.sep)
790
+ if (i <= 0) throw err
791
+
792
+ mkdirSync(filepath.slice(0, i), { mode, recursive: true })
793
+
794
+ try {
795
+ mkdirSync(filepath, { mode })
796
+ } catch (err) {
797
+ if (!statSync(filepath).isDirectory()) throw err
798
+ }
799
+ }
800
+ }
801
+
802
+ return
803
+ }
804
+
805
+ using req = FileRequest.borrow()
806
+
807
+ try {
808
+ binding.mkdirSync(req.handle, filepath, mode)
809
+ } catch (e) {
810
+ throw new FileError(e.message, {
811
+ operation: 'mkdir',
812
+ code: e.code,
813
+ path: filepath
814
+ })
815
+ }
816
+ }
817
+
818
+ async function rmdir(filepath, cb) {
819
+ filepath = toNamespacedPath(filepath)
820
+
821
+ using req = FileRequest.borrow()
822
+
823
+ let err = null
824
+ try {
825
+ binding.rmdir(req.handle, filepath)
826
+
827
+ await req
828
+ } catch (e) {
829
+ err = new FileError(e.message, {
830
+ operation: 'rmdir',
831
+ code: e.code,
832
+ path: filepath
833
+ })
834
+ }
835
+
836
+ return done(err, cb)
837
+ }
838
+
839
+ function rmdirSync(filepath) {
840
+ filepath = toNamespacedPath(filepath)
841
+
842
+ using req = FileRequest.borrow()
843
+
844
+ try {
845
+ binding.rmdirSync(req.handle, filepath)
846
+ } catch (e) {
847
+ throw new FileError(e.message, {
848
+ operation: 'rmdir',
849
+ code: e.code,
850
+ path: filepath
851
+ })
852
+ }
853
+ }
854
+
855
+ async function rm(filepath, opts, cb) {
856
+ if (typeof opts === 'function') {
857
+ cb = opts
858
+ opts = {}
859
+ }
860
+
861
+ if (!opts) opts = {}
862
+
863
+ filepath = toNamespacedPath(filepath)
864
+
865
+ let err = null
866
+ try {
867
+ const st = await lstat(filepath)
868
+
869
+ if (st.isDirectory()) {
870
+ if (opts.recursive) {
871
+ try {
872
+ await rmdir(filepath)
873
+ } catch (err) {
874
+ if (err.code !== 'ENOTEMPTY') throw err
875
+
876
+ const files = await readdir(filepath)
877
+
878
+ for (const file of files) {
879
+ await rm(filepath + path.sep + file, opts)
880
+ }
881
+
882
+ await rmdir(filepath)
883
+ }
884
+ } else {
885
+ throw new FileError('is a directory', {
886
+ operation: 'rm',
887
+ code: 'EISDIR',
888
+ path: filepath
889
+ })
890
+ }
891
+ } else {
892
+ await unlink(filepath)
893
+ }
894
+ } catch (e) {
895
+ if (e.code !== 'ENOENT' || !opts.force) err = e
896
+ }
897
+
898
+ return done(err, cb)
899
+ }
900
+
901
+ function rmSync(filepath, opts) {
902
+ if (!opts) opts = {}
903
+
904
+ filepath = toNamespacedPath(filepath)
905
+
906
+ try {
907
+ const st = lstatSync(filepath)
908
+
909
+ if (st.isDirectory()) {
910
+ if (opts.recursive) {
911
+ try {
912
+ rmdirSync(filepath)
913
+ } catch (err) {
914
+ if (err.code !== 'ENOTEMPTY') throw err
915
+
916
+ const files = readdirSync(filepath)
917
+
918
+ for (const file of files) {
919
+ rmSync(filepath + path.sep + file, opts)
920
+ }
921
+
922
+ rmdirSync(filepath)
923
+ }
924
+ } else {
925
+ throw new FileError('is a directory', {
926
+ operation: 'rm',
927
+ code: 'EISDIR',
928
+ path: filepath
929
+ })
930
+ }
931
+ } else {
932
+ unlinkSync(filepath)
933
+ }
934
+ } catch (err) {
935
+ if (err.code !== 'ENOENT' || !opts.force) throw err
936
+ }
937
+ }
938
+
939
+ async function unlink(filepath, cb) {
940
+ filepath = toNamespacedPath(filepath)
941
+
942
+ using req = FileRequest.borrow()
943
+
944
+ let err = null
945
+ try {
946
+ binding.unlink(req.handle, filepath)
947
+
948
+ await req
949
+ } catch (e) {
950
+ err = new FileError(e.message, {
951
+ operation: 'unlink',
952
+ code: e.code,
953
+ path: filepath
954
+ })
955
+ }
956
+
957
+ return done(err, cb)
958
+ }
959
+
960
+ function unlinkSync(filepath) {
961
+ filepath = toNamespacedPath(filepath)
962
+
963
+ using req = FileRequest.borrow()
964
+
965
+ try {
966
+ binding.unlinkSync(req.handle, filepath)
967
+ } catch (e) {
968
+ throw new FileError(e.message, {
969
+ operation: 'unlink',
970
+ code: e.code,
971
+ path: filepath
972
+ })
973
+ }
974
+ }
975
+
976
+ async function rename(src, dst, cb) {
977
+ src = toNamespacedPath(src)
978
+ dst = toNamespacedPath(dst)
979
+
980
+ using req = FileRequest.borrow()
981
+
982
+ let err = null
983
+ try {
984
+ binding.rename(req.handle, src, dst)
985
+
986
+ await req
987
+ } catch (e) {
988
+ err = new FileError(e.message, {
989
+ operation: 'rename',
990
+ code: e.code,
991
+ path: src,
992
+ destination: dst
993
+ })
994
+ }
995
+
996
+ return done(err, cb)
997
+ }
998
+
999
+ function renameSync(src, dst) {
1000
+ src = toNamespacedPath(src)
1001
+ dst = toNamespacedPath(dst)
1002
+
1003
+ using req = FileRequest.borrow()
1004
+
1005
+ try {
1006
+ binding.renameSync(req.handle, src, dst)
1007
+ } catch (e) {
1008
+ throw new FileError(e.message, {
1009
+ operation: 'rename',
1010
+ code: e.code,
1011
+ path: src,
1012
+ destination: dst
1013
+ })
1014
+ }
1015
+ }
1016
+
1017
+ async function copyFile(src, dst, mode = 0, cb) {
1018
+ if (typeof mode === 'function') {
1019
+ cb = mode
1020
+ mode = 0
1021
+ }
1022
+
1023
+ src = toNamespacedPath(src)
1024
+ dst = toNamespacedPath(dst)
1025
+
1026
+ using req = FileRequest.borrow()
1027
+
1028
+ let err = null
1029
+ try {
1030
+ binding.copyfile(req.handle, src, dst, mode)
1031
+
1032
+ await req
1033
+ } catch (e) {
1034
+ err = new FileError(e.message, {
1035
+ operation: 'copyfile',
1036
+ code: e.code,
1037
+ path: src,
1038
+ destination: dst
1039
+ })
1040
+ }
1041
+
1042
+ return done(err, cb)
1043
+ }
1044
+
1045
+ function copyFileSync(src, dst, mode = 0) {
1046
+ src = toNamespacedPath(src)
1047
+ dst = toNamespacedPath(dst)
1048
+
1049
+ using req = FileRequest.borrow()
1050
+
1051
+ try {
1052
+ binding.copyfileSync(req.handle, src, dst, mode)
1053
+ } catch (e) {
1054
+ throw new FileError(e.message, {
1055
+ operation: 'copyfile',
1056
+ code: e.code,
1057
+ path: src,
1058
+ destination: dst
1059
+ })
1060
+ }
1061
+ }
1062
+
1063
+ async function cp(src, dst, opts, cb) {
1064
+ if (typeof opts === 'function') {
1065
+ cb = opts
1066
+ opts = {}
1067
+ }
1068
+
1069
+ if (!opts) opts = {}
1070
+
1071
+ src = toNamespacedPath(src)
1072
+ dst = toNamespacedPath(dst)
1073
+
1074
+ let err = null
1075
+ try {
1076
+ const st = await lstat(src)
1077
+
1078
+ if (st.isDirectory()) {
1079
+ if (opts.recursive !== true) {
1080
+ throw new FileError('is a directory', {
1081
+ operation: 'cp',
1082
+ code: 'EISDIR',
1083
+ path: src
1084
+ })
1085
+ }
1086
+
1087
+ try {
1088
+ await lstat(dst)
1089
+ } catch (e) {
1090
+ if (e.code === 'ENOENT') {
1091
+ await mkdir(dst, { mode: st.mode, recursive: true })
1092
+ } else {
1093
+ throw e
1094
+ }
1095
+ }
1096
+
1097
+ const dir = await opendir(src)
1098
+ for await (const { name } of dir) {
1099
+ await cp(path.join(src, name), path.join(dst, name), opts)
1100
+ }
1101
+ } else if (st.isFile()) {
1102
+ await copyFile(src, dst)
1103
+ await chmod(dst, st.mode)
1104
+ }
1105
+ } catch (e) {
1106
+ err = e
1107
+ }
1108
+
1109
+ return done(err, cb)
1110
+ }
1111
+
1112
+ async function realpath(filepath, opts, cb) {
1113
+ if (typeof opts === 'function') {
1114
+ cb = opts
1115
+ opts = {}
1116
+ }
1117
+
1118
+ if (typeof opts === 'string') opts = { encoding: opts }
1119
+ else if (!opts) opts = {}
1120
+
1121
+ const { encoding = 'utf8' } = opts
1122
+
1123
+ filepath = toNamespacedPath(filepath)
1124
+
1125
+ using req = FileRequest.borrow()
1126
+
1127
+ let res
1128
+ let err = null
1129
+ try {
1130
+ binding.realpath(req.handle, filepath)
1131
+
1132
+ await req
1133
+
1134
+ res = Buffer.from(binding.requestResultString(req.handle))
1135
+
1136
+ if (encoding !== 'buffer') res = res.toString(encoding)
1137
+ } catch (e) {
1138
+ err = new FileError(e.message, {
1139
+ operation: 'realpath',
1140
+ code: e.code,
1141
+ path: filepath
1142
+ })
1143
+ }
1144
+
1145
+ return done(err, res, cb)
1146
+ }
1147
+
1148
+ function realpathSync(filepath, opts) {
1149
+ if (typeof opts === 'string') opts = { encoding: opts }
1150
+ else if (!opts) opts = {}
1151
+
1152
+ const { encoding = 'utf8' } = opts
1153
+
1154
+ filepath = toNamespacedPath(filepath)
1155
+
1156
+ using req = FileRequest.borrow()
1157
+
1158
+ try {
1159
+ binding.realpathSync(req.handle, filepath)
1160
+
1161
+ let res = Buffer.from(binding.requestResultString(req.handle))
1162
+
1163
+ if (encoding !== 'buffer') res = res.toString(encoding)
1164
+
1165
+ return res
1166
+ } catch (e) {
1167
+ throw new FileError(e.message, {
1168
+ operation: 'realpath',
1169
+ code: e.code,
1170
+ path: filepath
1171
+ })
1172
+ }
1173
+ }
1174
+
1175
+ async function readlink(filepath, opts, cb) {
1176
+ if (typeof opts === 'function') {
1177
+ cb = opts
1178
+ opts = {}
1179
+ }
1180
+
1181
+ if (typeof opts === 'string') opts = { encoding: opts }
1182
+ else if (!opts) opts = {}
1183
+
1184
+ const { encoding = 'utf8' } = opts
1185
+
1186
+ filepath = toNamespacedPath(filepath)
1187
+
1188
+ using req = FileRequest.borrow()
1189
+
1190
+ let res
1191
+ let err = null
1192
+ try {
1193
+ binding.readlink(req.handle, filepath)
1194
+
1195
+ await req
1196
+
1197
+ res = Buffer.from(binding.requestResultString(req.handle))
1198
+
1199
+ if (encoding !== 'buffer') res = res.toString(encoding)
1200
+ } catch (e) {
1201
+ err = new FileError(e.message, {
1202
+ operation: 'readlink',
1203
+ code: e.code,
1204
+ path: filepath
1205
+ })
1206
+ }
1207
+
1208
+ return done(err, res, cb)
1209
+ }
1210
+
1211
+ function readlinkSync(filepath, opts) {
1212
+ if (typeof opts === 'string') opts = { encoding: opts }
1213
+ else if (!opts) opts = {}
1214
+
1215
+ const { encoding = 'utf8' } = opts
1216
+
1217
+ filepath = toNamespacedPath(filepath)
1218
+
1219
+ using req = FileRequest.borrow()
1220
+
1221
+ try {
1222
+ binding.readlinkSync(req.handle, filepath)
1223
+
1224
+ let res = Buffer.from(binding.requestResultString(req.handle))
1225
+
1226
+ if (encoding !== 'buffer') res = res.toString(encoding)
1227
+
1228
+ return res
1229
+ } catch (e) {
1230
+ throw new FileError(e.message, {
1231
+ operation: 'readlink',
1232
+ code: e.code,
1233
+ path: filepath
1234
+ })
1235
+ }
1236
+ }
1237
+
1238
+ function normalizeSymlinkTarget(target, type, filepath) {
1239
+ if (isWindows) {
1240
+ if (type === 'junction') target = path.resolve(filepath, '..', target)
1241
+
1242
+ if (path.isAbsolute(target)) return path.toNamespacedPath(target)
1243
+
1244
+ return target.replace(/\//g, path.sep)
1245
+ }
1246
+
1247
+ return target
1248
+ }
1249
+
1250
+ async function symlink(target, filepath, type, cb) {
1251
+ if (typeof type === 'function') {
1252
+ cb = type
1253
+ type = null
1254
+ }
1255
+
1256
+ filepath = toNamespacedPath(filepath)
1257
+
1258
+ if (typeof type === 'string') {
1259
+ switch (type) {
1260
+ case 'file':
1261
+ default:
1262
+ type = 0
1263
+ break
1264
+ case 'dir':
1265
+ type = constants.UV_FS_SYMLINK_DIR
1266
+ break
1267
+ case 'junction':
1268
+ type = constants.UV_FS_SYMLINK_JUNCTION
1269
+ break
1270
+ }
1271
+ } else if (typeof type !== 'number') {
1272
+ if (isWindows) {
1273
+ target = path.resolve(filepath, '..', target)
1274
+
1275
+ try {
1276
+ type = (await stat(target)).isDirectory()
1277
+ ? constants.UV_FS_SYMLINK_DIR
1278
+ : constants.UV_FS_SYMLINK_JUNCTION
1279
+ } catch {
1280
+ type = 0
1281
+ }
1282
+ } else {
1283
+ type = 0
1284
+ }
1285
+ }
1286
+
1287
+ target = normalizeSymlinkTarget(target)
1288
+
1289
+ using req = FileRequest.borrow()
1290
+
1291
+ let err = null
1292
+ try {
1293
+ binding.symlink(req.handle, target, filepath, type)
1294
+
1295
+ await req
1296
+ } catch (e) {
1297
+ err = new FileError(e.message, {
1298
+ operation: 'symlink',
1299
+ code: e.code,
1300
+ path: target,
1301
+ destination: filepath
1302
+ })
1303
+ }
1304
+
1305
+ return done(err, cb)
1306
+ }
1307
+
1308
+ function symlinkSync(target, filepath, type) {
1309
+ filepath = toNamespacedPath(filepath)
1310
+
1311
+ if (typeof type === 'string') {
1312
+ switch (type) {
1313
+ case 'file':
1314
+ default:
1315
+ type = 0
1316
+ break
1317
+ case 'dir':
1318
+ type = constants.UV_FS_SYMLINK_DIR
1319
+ break
1320
+ case 'junction':
1321
+ type = constants.UV_FS_SYMLINK_JUNCTION
1322
+ break
1323
+ }
1324
+ } else if (typeof type !== 'number') {
1325
+ if (isWindows) {
1326
+ target = path.resolve(filepath, '..', target)
1327
+
1328
+ try {
1329
+ type = statSync(target).isDirectory()
1330
+ ? constants.UV_FS_SYMLINK_DIR
1331
+ : constants.UV_FS_SYMLINK_JUNCTION
1332
+ } catch {
1333
+ type = 0
1334
+ }
1335
+ } else {
1336
+ type = 0
1337
+ }
1338
+ }
1339
+
1340
+ target = normalizeSymlinkTarget(target)
1341
+
1342
+ using req = FileRequest.borrow()
1343
+
1344
+ try {
1345
+ binding.symlinkSync(req.handle, target, filepath, type)
1346
+ } catch (e) {
1347
+ throw new FileError(e.message, {
1348
+ operation: 'symlink',
1349
+ code: e.code,
1350
+ path: target,
1351
+ destination: filepath
1352
+ })
1353
+ }
1354
+ }
1355
+
1356
+ async function opendir(filepath, opts, cb) {
1357
+ if (typeof opts === 'function') {
1358
+ cb = opts
1359
+ opts = {}
1360
+ }
1361
+
1362
+ if (typeof opts === 'string') opts = { encoding: opts }
1363
+ else if (!opts) opts = {}
1364
+
1365
+ filepath = toNamespacedPath(filepath)
1366
+
1367
+ using req = FileRequest.borrow()
1368
+
1369
+ let dir
1370
+ let err = null
1371
+ try {
1372
+ binding.opendir(req.handle, filepath)
1373
+
1374
+ await req
1375
+
1376
+ dir = new Dir(filepath, binding.requestResultDir(req.handle), opts)
1377
+ } catch (e) {
1378
+ err = new FileError(e.message, {
1379
+ operation: 'opendir',
1380
+ code: e.code,
1381
+ path: filepath
1382
+ })
1383
+ }
1384
+
1385
+ return done(err, dir, cb)
1386
+ }
1387
+
1388
+ function opendirSync(filepath, opts) {
1389
+ if (typeof opts === 'string') opts = { encoding: opts }
1390
+ else if (!opts) opts = {}
1391
+
1392
+ filepath = toNamespacedPath(filepath)
1393
+
1394
+ using req = FileRequest.borrow()
1395
+
1396
+ try {
1397
+ binding.opendirSync(req.handle, filepath)
1398
+
1399
+ return new Dir(filepath, binding.requestResultDir(req.handle), opts)
1400
+ } catch (e) {
1401
+ throw new FileError(e.message, {
1402
+ operation: 'opendir',
1403
+ code: e.code,
1404
+ path: filepath
1405
+ })
1406
+ }
1407
+ }
1408
+
1409
+ async function readdir(filepath, opts, cb) {
1410
+ if (typeof opts === 'function') {
1411
+ cb = opts
1412
+ opts = {}
1413
+ }
1414
+
1415
+ if (typeof opts === 'string') opts = { encoding: opts }
1416
+ else if (!opts) opts = {}
1417
+
1418
+ const { withFileTypes = false } = opts
1419
+
1420
+ filepath = toNamespacedPath(filepath)
1421
+
1422
+ let result = []
1423
+ let err = null
1424
+ try {
1425
+ const dir = await opendir(filepath)
1426
+
1427
+ for await (const entry of dir) {
1428
+ result.push(withFileTypes ? entry : entry.name)
1429
+ }
1430
+ } catch (e) {
1431
+ result = []
1432
+ err = e
1433
+ }
1434
+
1435
+ return done(err, result, cb)
1436
+ }
1437
+
1438
+ function readdirSync(filepath, opts) {
1439
+ if (typeof opts === 'string') opts = { encoding: opts }
1440
+ else if (!opts) opts = {}
1441
+
1442
+ const { withFileTypes = false } = opts
1443
+
1444
+ filepath = toNamespacedPath(filepath)
1445
+
1446
+ const dir = opendirSync(filepath, opts)
1447
+ const result = []
1448
+
1449
+ for (const entry of dir) {
1450
+ result.push(withFileTypes ? entry : entry.name)
1451
+ }
1452
+
1453
+ return result
1454
+ }
1455
+
1456
+ async function readFile(filepath, opts, cb) {
1457
+ if (typeof opts === 'function') {
1458
+ cb = opts
1459
+ opts = {}
1460
+ }
1461
+
1462
+ if (typeof opts === 'string') opts = { encoding: opts }
1463
+ else if (!opts) opts = {}
1464
+
1465
+ const { encoding = 'buffer' } = opts
1466
+
1467
+ let fd = -1
1468
+ let buffer = null
1469
+ let err = null
1470
+ try {
1471
+ fd = await open(filepath, opts.flag || 'r')
1472
+
1473
+ const st = await fstat(fd)
1474
+
1475
+ let len = 0
1476
+
1477
+ if (st.size === 0) {
1478
+ const buffers = []
1479
+
1480
+ while (true) {
1481
+ buffer = Buffer.allocUnsafe(8192)
1482
+ const r = await read(fd, buffer)
1483
+ len += r
1484
+ if (r === 0) break
1485
+ buffers.push(buffer.subarray(0, r))
1486
+ }
1487
+
1488
+ buffer = Buffer.concat(buffers)
1489
+ } else {
1490
+ buffer = Buffer.allocUnsafe(st.size)
1491
+
1492
+ while (true) {
1493
+ const r = await read(fd, len ? buffer.subarray(len) : buffer)
1494
+ len += r
1495
+ if (r === 0 || len === buffer.byteLength) break
1496
+ }
1497
+
1498
+ if (len !== buffer.byteLength) buffer = buffer.subarray(0, len)
1499
+ }
1500
+
1501
+ if (encoding !== 'buffer') buffer = buffer.toString(encoding)
1502
+ } catch (e) {
1503
+ err = e
1504
+ } finally {
1505
+ if (fd !== -1) await close(fd)
1506
+ }
1507
+
1508
+ return done(err, buffer, cb)
1509
+ }
1510
+
1511
+ function readFileSync(filepath, opts) {
1512
+ if (typeof opts === 'string') opts = { encoding: opts }
1513
+ else if (!opts) opts = {}
1514
+
1515
+ const { encoding = 'buffer' } = opts
1516
+
1517
+ let fd = -1
1518
+ try {
1519
+ fd = openSync(filepath, opts.flag || 'r')
1520
+
1521
+ const st = fstatSync(fd)
1522
+
1523
+ let buffer
1524
+ let len = 0
1525
+
1526
+ if (st.size === 0) {
1527
+ const buffers = []
1528
+
1529
+ while (true) {
1530
+ buffer = Buffer.allocUnsafe(8192)
1531
+ const r = readSync(fd, buffer)
1532
+ len += r
1533
+ if (r === 0) break
1534
+ buffers.push(buffer.subarray(0, r))
1535
+ }
1536
+
1537
+ buffer = Buffer.concat(buffers)
1538
+ } else {
1539
+ buffer = Buffer.allocUnsafe(st.size)
1540
+
1541
+ while (true) {
1542
+ const r = readSync(fd, len ? buffer.subarray(len) : buffer)
1543
+ len += r
1544
+ if (r === 0 || len === buffer.byteLength) break
1545
+ }
1546
+
1547
+ if (len !== buffer.byteLength) buffer = buffer.subarray(0, len)
1548
+ }
1549
+
1550
+ if (encoding !== 'buffer') buffer = buffer.toString(encoding)
1551
+
1552
+ return buffer
1553
+ } finally {
1554
+ if (fd !== -1) closeSync(fd)
1555
+ }
1556
+ }
1557
+
1558
+ async function writeFile(filepath, data, opts, cb) {
1559
+ if (typeof opts === 'function') {
1560
+ cb = opts
1561
+ opts = {}
1562
+ }
1563
+
1564
+ if (typeof opts === 'string') opts = { encoding: opts }
1565
+ else if (!opts) opts = {}
1566
+
1567
+ if (typeof data === 'string') data = Buffer.from(data, opts.encoding)
1568
+
1569
+ let fd = -1
1570
+ let len = 0
1571
+ let err = null
1572
+ try {
1573
+ fd = await open(filepath, opts.flag || 'w', opts.mode || 0o666)
1574
+
1575
+ while (true) {
1576
+ len += await write(fd, len ? data.subarray(len) : data)
1577
+ if (len === data.byteLength) break
1578
+ }
1579
+ } catch (err) {
1580
+ err = e
1581
+ } finally {
1582
+ if (fd !== -1) await close(fd)
1583
+ }
1584
+
1585
+ return done(err, len, cb)
1586
+ }
1587
+
1588
+ function writeFileSync(filepath, data, opts) {
1589
+ if (typeof opts === 'string') opts = { encoding: opts }
1590
+ else if (!opts) opts = {}
1591
+
1592
+ if (typeof data === 'string') data = Buffer.from(data, opts.encoding)
1593
+
1594
+ let fd = -1
1595
+ try {
1596
+ fd = openSync(filepath, opts.flag || 'w', opts.mode || 0o666)
1597
+
1598
+ let len = 0
1599
+
1600
+ while (true) {
1601
+ len += writeSync(fd, len ? data.subarray(len) : data)
1602
+ if (len === data.byteLength) break
1603
+ }
1604
+ } finally {
1605
+ if (fd !== -1) closeSync(fd)
1606
+ }
1607
+ }
1608
+
1609
+ function appendFile(filepath, data, opts, cb) {
1610
+ if (typeof opts === 'function') {
1611
+ cb = opts
1612
+ opts = {}
1613
+ }
1614
+
1615
+ if (typeof opts === 'string') opts = { encoding: opts }
1616
+ else if (!opts) opts = {}
1617
+
1618
+ if (!opts.flag) opts = { ...opts, flag: 'a' }
1619
+
1620
+ return writeFile(filepath, data, opts, cb)
1621
+ }
1622
+
1623
+ function appendFileSync(filepath, data, opts) {
1624
+ if (typeof opts === 'string') opts = { encoding: opts }
1625
+ else if (!opts) opts = {}
1626
+
1627
+ if (!opts.flag) opts = { ...opts, flag: 'a' }
1628
+
1629
+ return writeFileSync(filepath, data, opts)
1630
+ }
1631
+
1632
+ function watch(filepath, opts, cb) {
1633
+ if (typeof opts === 'function') {
1634
+ cb = opts
1635
+ opts = {}
1636
+ }
1637
+
1638
+ if (typeof opts === 'string') opts = { encoding: opts }
1639
+ else if (!opts) opts = {}
1640
+
1641
+ filepath = toNamespacedPath(filepath)
1642
+
1643
+ return new Watcher(filepath, opts, cb)
1644
+ }
1645
+
1646
+ class Stats {
1647
+ constructor(
1648
+ dev,
1649
+ mode,
1650
+ nlink,
1651
+ uid,
1652
+ gid,
1653
+ rdev,
1654
+ blksize,
1655
+ ino,
1656
+ size,
1657
+ blocks,
1658
+ atimeMs,
1659
+ mtimeMs,
1660
+ ctimeMs,
1661
+ birthtimeMs
1662
+ ) {
1663
+ this.dev = dev
1664
+ this.mode = mode
1665
+ this.nlink = nlink
1666
+ this.uid = uid
1667
+ this.gid = gid
1668
+ this.rdev = rdev
1669
+ this.blksize = blksize
1670
+ this.ino = ino
1671
+ this.size = size
1672
+ this.blocks = blocks
1673
+ this.atimeMs = atimeMs
1674
+ this.mtimeMs = mtimeMs
1675
+ this.ctimeMs = ctimeMs
1676
+ this.birthtimeMs = birthtimeMs
1677
+ this.atime = new Date(atimeMs)
1678
+ this.mtime = new Date(mtimeMs)
1679
+ this.ctime = new Date(ctimeMs)
1680
+ this.birthtime = new Date(birthtimeMs)
1681
+ }
1682
+
1683
+ isDirectory() {
1684
+ return (this.mode & constants.S_IFMT) === constants.S_IFDIR
1685
+ }
1686
+
1687
+ isFile() {
1688
+ return (this.mode & constants.S_IFMT) === constants.S_IFREG
1689
+ }
1690
+
1691
+ isBlockDevice() {
1692
+ return (this.mode & constants.S_IFMT) === constants.S_IFBLK
1693
+ }
1694
+
1695
+ isCharacterDevice() {
1696
+ return (this.mode & constants.S_IFCHR) === constants.S_IFCHR
1697
+ }
1698
+
1699
+ isFIFO() {
1700
+ return (this.mode & constants.S_IFMT) === constants.S_IFIFO
1701
+ }
1702
+
1703
+ isSymbolicLink() {
1704
+ return (this.mode & constants.S_IFMT) === constants.S_IFLNK
1705
+ }
1706
+
1707
+ isSocket() {
1708
+ return (this.mode & constants.S_IFMT) === constants.S_IFSOCK
1709
+ }
1710
+ }
1711
+
1712
+ class Dir {
1713
+ constructor(path, handle, opts = {}) {
1714
+ const { encoding = 'utf8', bufferSize = 32 } = opts
1715
+
1716
+ this.path = path
1717
+
1718
+ this._encoding = encoding
1719
+ this._capacity = bufferSize
1720
+ this._buffer = new FIFO()
1721
+ this._ended = false
1722
+ this._handle = handle
1723
+ }
1724
+
1725
+ async read(cb) {
1726
+ if (this._buffer.length) return ok(this._buffer.shift(), cb)
1727
+ if (this._ended) return ok(null, cb)
1728
+
1729
+ using req = FileRequest.borrow()
1730
+
1731
+ let entries
1732
+ let err = null
1733
+ try {
1734
+ req.retain(binding.readdir(req.handle, this._handle, this._capacity))
1735
+
1736
+ await req
1737
+
1738
+ entries = binding.requestResultDirents(req.handle)
1739
+ } catch (e) {
1740
+ err = new FileError(e.message, {
1741
+ operation: 'readdir',
1742
+ code: e.code,
1743
+ path: this.path
1744
+ })
1745
+ }
1746
+
1747
+ if (err) return fail(err, cb)
1748
+
1749
+ if (entries.length === 0) {
1750
+ this._ended = true
1751
+
1752
+ return ok(null, cb)
1753
+ }
1754
+
1755
+ for (const entry of entries) {
1756
+ let name = Buffer.from(entry.name)
1757
+
1758
+ if (this._encoding !== 'buffer') name = name.toString(this._encoding)
1759
+
1760
+ this._buffer.push(new Dirent(this.path, name, entry.type))
1761
+ }
1762
+
1763
+ return ok(this._buffer.shift(), cb)
1764
+ }
1765
+
1766
+ readSync() {
1767
+ if (this._buffer.length) return this._buffer.shift()
1768
+ if (this._ended) return null
1769
+
1770
+ using req = FileRequest.borrow()
1771
+
1772
+ let entries
1773
+ try {
1774
+ req.retain(binding.readdirSync(req.handle, this._handle, this._capacity))
1775
+
1776
+ entries = binding.requestResultDirents(req.handle)
1777
+ } catch (e) {
1778
+ throw new FileError(e.message, {
1779
+ operation: 'readdir',
1780
+ code: e.code,
1781
+ path: this.path
1782
+ })
1783
+ }
1784
+
1785
+ if (entries.length === 0) {
1786
+ this._ended = true
1787
+
1788
+ return null
1789
+ }
1790
+
1791
+ for (const entry of entries) {
1792
+ let name = Buffer.from(entry.name)
1793
+
1794
+ if (this._encoding !== 'buffer') name = name.toString(this._encoding)
1795
+
1796
+ this._buffer.push(new Dirent(this.path, name, entry.type))
1797
+ }
1798
+
1799
+ return this._buffer.shift()
1800
+ }
1801
+
1802
+ async close(cb) {
1803
+ using req = FileRequest.borrow()
1804
+
1805
+ let err = null
1806
+ try {
1807
+ binding.closedir(req.handle, this._handle)
1808
+
1809
+ await req
1810
+ } catch (e) {
1811
+ err = new FileError(e.message, {
1812
+ operation: 'closedir',
1813
+ code: e.code,
1814
+ path: this.path
1815
+ })
1816
+ }
1817
+
1818
+ this._handle = null
1819
+
1820
+ return done(err, cb)
1821
+ }
1822
+
1823
+ closeSync() {
1824
+ using req = FileRequest.borrow()
1825
+
1826
+ try {
1827
+ binding.closedirSync(req.handle, this._handle)
1828
+ } catch (e) {
1829
+ throw new FileError(e.message, {
1830
+ operation: 'closedir',
1831
+ code: e.code,
1832
+ path: this.path
1833
+ })
1834
+ }
1835
+
1836
+ this._handle = null
1837
+ }
1838
+
1839
+ [Symbol.dispose]() {
1840
+ this.closeSync()
1841
+ }
1842
+
1843
+ async [Symbol.asyncDispose]() {
1844
+ await this.close()
1845
+ }
1846
+
1847
+ *[Symbol.iterator]() {
1848
+ while (true) {
1849
+ const entry = this.readSync()
1850
+ if (entry === null) break
1851
+ yield entry
1852
+ }
1853
+
1854
+ this.closeSync()
1855
+ }
1856
+
1857
+ async *[Symbol.asyncIterator]() {
1858
+ while (true) {
1859
+ const entry = await this.read()
1860
+ if (entry === null) break
1861
+ yield entry
1862
+ }
1863
+
1864
+ await this.close()
1865
+ }
1866
+ }
1867
+
1868
+ class Dirent {
1869
+ constructor(path, name, type) {
1870
+ this.path = path
1871
+ this.name = name
1872
+ this.type = type
1873
+ }
1874
+
1875
+ isFile() {
1876
+ return this.type === constants.UV_DIRENT_FILE
1877
+ }
1878
+
1879
+ isDirectory() {
1880
+ return this.type === constants.UV_DIRENT_DIR
1881
+ }
1882
+
1883
+ isSymbolicLink() {
1884
+ return this.type === constants.UV_DIRENT_LINK
1885
+ }
1886
+
1887
+ isFIFO() {
1888
+ return this.type === constants.UV_DIRENT_FIFO
1889
+ }
1890
+
1891
+ isSocket() {
1892
+ return this.type === constants.UV_DIRENT_SOCKET
1893
+ }
1894
+
1895
+ isCharacterDevice() {
1896
+ return this.type === constants.UV_DIRENT_CHAR
1897
+ }
1898
+
1899
+ isBlockDevice() {
1900
+ return this.type === constants.UV_DIRENT_BLOCK
1901
+ }
1902
+ }
1903
+
1904
+ class FileReadStream extends Readable {
1905
+ constructor(path, opts = {}) {
1906
+ const { eagerOpen = true } = opts
1907
+
1908
+ super({ eagerOpen, ...opts })
1909
+
1910
+ this.path = path
1911
+ this.fd = typeof opts.fd === 'number' ? opts.fd : -1
1912
+ this.flags = opts.flags || 'r'
1913
+ this.mode = opts.mode || 0o666
1914
+
1915
+ this._offset = opts.start || 0
1916
+ this._missing = 0
1917
+
1918
+ if (opts.length) {
1919
+ this._missing = opts.length
1920
+ } else if (typeof opts.end === 'number') {
1921
+ this._missing = opts.end - this._offset + 1
1922
+ } else {
1923
+ this._missing = -1
1924
+ }
1925
+ }
1926
+
1927
+ async _open(cb) {
1928
+ let err
1929
+
1930
+ if (this.fd === -1) {
1931
+ err = null
1932
+ try {
1933
+ this.fd = await open(this.path, this.flags, this.mode)
1934
+ } catch (e) {
1935
+ err = e
1936
+ }
1937
+
1938
+ if (err) return cb(err)
1939
+ }
1940
+
1941
+ let st
1942
+ err = null
1943
+ try {
1944
+ st = await fstat(this.fd)
1945
+ } catch (e) {
1946
+ err = e
1947
+ }
1948
+
1949
+ if (err) return cb(err)
1950
+
1951
+ if (this._missing === -1) this._missing = st.size
1952
+
1953
+ if (st.size < this._offset) {
1954
+ this._offset = st.size
1955
+ this._missing = 0
1956
+ } else if (st.size < this._offset + this._missing) {
1957
+ this._missing = st.size - this._offset
1958
+ }
1959
+
1960
+ cb(null)
1961
+ }
1962
+
1963
+ async _read(size) {
1964
+ if (this._missing <= 0) return this.push(null)
1965
+
1966
+ const data = Buffer.allocUnsafe(Math.min(this._missing, size))
1967
+
1968
+ let len
1969
+ let err = null
1970
+ try {
1971
+ len = await read(this.fd, data, 0, data.byteLength, this._offset)
1972
+ } catch (e) {
1973
+ err = e
1974
+ }
1975
+
1976
+ if (err) return this.destroy(err)
1977
+
1978
+ if (len === 0) return this.push(null)
1979
+
1980
+ if (this._missing < len) len = this._missing
1981
+
1982
+ this._missing -= len
1983
+ this._offset += len
1984
+
1985
+ this.push(data.subarray(0, len))
1986
+ }
1987
+
1988
+ async _destroy(err, cb) {
1989
+ if (this.fd === -1) return cb(err)
1990
+
1991
+ err = null
1992
+ try {
1993
+ await close(this.fd)
1994
+ } catch (e) {
1995
+ err = e
1996
+ }
1997
+
1998
+ cb(err)
1999
+ }
2000
+ }
2001
+
2002
+ class FileWriteStream extends Writable {
2003
+ constructor(path, opts = {}) {
2004
+ const { eagerOpen = true } = opts
2005
+
2006
+ super({ eagerOpen, ...opts })
2007
+
2008
+ this.path = path
2009
+ this.fd = typeof opts.fd === 'number' ? opts.fd : -1
2010
+ this.flags = opts.flags || 'w'
2011
+ this.mode = opts.mode || 0o666
2012
+ }
2013
+
2014
+ async _open(cb) {
2015
+ if (this.fd !== -1) return cb(null)
2016
+
2017
+ let err = null
2018
+ try {
2019
+ this.fd = await open(this.path, this.flags, this.mode)
2020
+ } catch (e) {
2021
+ err = e
2022
+ }
2023
+
2024
+ cb(err)
2025
+ }
2026
+
2027
+ async _writev(batch, cb) {
2028
+ let err = null
2029
+ try {
2030
+ await writev(
2031
+ this.fd,
2032
+ batch.map(({ chunk }) => chunk)
2033
+ )
2034
+ } catch (e) {
2035
+ err = e
2036
+ }
2037
+
2038
+ cb(err)
2039
+ }
2040
+
2041
+ async _destroy(err, cb) {
2042
+ if (this.fd === -1) return cb(err)
2043
+
2044
+ err = null
2045
+ try {
2046
+ await close(this.fd)
2047
+ } catch (e) {
2048
+ err = e
2049
+ }
2050
+
2051
+ cb(err)
2052
+ }
2053
+ }
2054
+
2055
+ class Watcher extends EventEmitter {
2056
+ constructor(path, opts, onchange) {
2057
+ if (typeof opts === 'function') {
2058
+ onchange = opts
2059
+ opts = {}
2060
+ }
2061
+
2062
+ if (!opts) opts = {}
2063
+
2064
+ const { persistent = true, recursive = false, encoding = 'utf8' } = opts
2065
+
2066
+ super()
2067
+
2068
+ this._closed = false
2069
+ this._encoding = encoding
2070
+ this._handle = binding.watcherInit(path, recursive, this, this._onevent, this._onclose)
2071
+
2072
+ if (!persistent) this.unref()
2073
+
2074
+ if (onchange) this.on('change', onchange)
2075
+ }
2076
+
2077
+ close() {
2078
+ if (this._closed) return
2079
+ this._closed = true
2080
+
2081
+ binding.watcherClose(this._handle)
2082
+ }
2083
+
2084
+ ref() {
2085
+ if (this._handle) binding.watcherRef(this._handle)
2086
+ return this
2087
+ }
2088
+
2089
+ unref() {
2090
+ if (this._handle) binding.watcherUnref(this._handle)
2091
+ return this
2092
+ }
2093
+
2094
+ [Symbol.asyncIterator]() {
2095
+ const buffer = []
2096
+ let done = false
2097
+ let error = null
2098
+ let next = null
2099
+
2100
+ this.on('change', (eventType, filename) => {
2101
+ if (next) {
2102
+ next.resolve({ done: false, value: { eventType, filename } })
2103
+ next = null
2104
+ } else {
2105
+ buffer.push({ eventType, filename })
2106
+ }
2107
+ })
2108
+ .on('error', (err) => {
2109
+ done = true
2110
+ error = err
2111
+
2112
+ if (next) {
2113
+ next.reject(error)
2114
+ next = null
2115
+ }
2116
+ })
2117
+ .on('close', () => {
2118
+ done = true
2119
+
2120
+ if (next) {
2121
+ next.resolve({ done })
2122
+ next = null
2123
+ }
2124
+ })
2125
+
2126
+ return {
2127
+ next: () =>
2128
+ new Promise((resolve, reject) => {
2129
+ if (error) return reject(error)
2130
+
2131
+ if (buffer.length) return resolve({ done: false, value: buffer.shift() })
2132
+
2133
+ if (done) return resolve({ done })
2134
+
2135
+ next = { resolve, reject }
2136
+ })
2137
+ }
2138
+ }
2139
+
2140
+ _onevent(err, events, filename) {
2141
+ if (err) {
2142
+ this.close()
2143
+ this.emit('error', err)
2144
+ } else {
2145
+ const path =
2146
+ this._encoding === 'buffer'
2147
+ ? Buffer.from(filename)
2148
+ : Buffer.from(filename).toString(this._encoding)
2149
+
2150
+ if (events & binding.UV_RENAME) {
2151
+ this.emit('change', 'rename', path)
2152
+ }
2153
+
2154
+ if (events & binding.UV_CHANGE) {
2155
+ this.emit('change', 'change', path)
2156
+ }
2157
+ }
2158
+ }
2159
+
2160
+ _onclose() {
2161
+ this._handle = null
2162
+
2163
+ this.emit('close')
2164
+ }
2165
+ }
2166
+
2167
+ exports.access = access
2168
+ exports.appendFile = appendFile
2169
+ exports.chmod = chmod
2170
+ // exports.chown = chown TODO
2171
+ exports.close = close
2172
+ exports.copyFile = copyFile
2173
+ exports.cp = cp
2174
+ exports.exists = exists
2175
+ exports.fchmod = fchmod
2176
+ // exports.fchown = fchown TODO
2177
+ // exports.fdatasync = fdatasync TODO
2178
+ exports.fstat = fstat
2179
+ // exports.fsync = fsync TODO
2180
+ exports.ftruncate = ftruncate
2181
+ // exports.futimes = futimes TODO
2182
+ // exports.lchmod = lchmod TODO
2183
+ // exports.lchown = lchown TODO
2184
+ // exports.lutimes = lutimes TODO
2185
+ // exports.link = link TODO
2186
+ exports.lstat = lstat
2187
+ exports.mkdir = mkdir
2188
+ // exports.mkdtemp = mkdtemp TODO
2189
+ exports.open = open
2190
+ exports.opendir = opendir
2191
+ exports.read = read
2192
+ exports.readFile = readFile
2193
+ exports.readdir = readdir
2194
+ exports.readlink = readlink
2195
+ exports.readv = readv
2196
+ exports.realpath = realpath
2197
+ exports.rename = rename
2198
+ exports.rm = rm
2199
+ exports.rmdir = rmdir
2200
+ exports.stat = stat
2201
+ // exports.statfs = statfs TODO
2202
+ exports.symlink = symlink
2203
+ // exports.truncate = truncate TODO
2204
+ exports.unlink = unlink
2205
+ exports.utimes = utimes
2206
+ exports.watch = watch
2207
+ exports.write = write
2208
+ exports.writeFile = writeFile
2209
+ exports.writev = writev
2210
+
2211
+ exports.accessSync = accessSync
2212
+ exports.appendFileSync = appendFileSync
2213
+ exports.chmodSync = chmodSync
2214
+ // exports.chownSync = chownSync TODO
2215
+ exports.closeSync = closeSync
2216
+ exports.copyFileSync = copyFileSync
2217
+ exports.existsSync = existsSync
2218
+ exports.fchmodSync = fchmodSync
2219
+ // exports.fchownSync = fchownSync TODO
2220
+ // exports.fdatasyncSync = fdatasyncSync TODO
2221
+ exports.fstatSync = fstatSync
2222
+ // exports.fsyncSync = fsyncSync TODO
2223
+ exports.ftruncateSync = ftruncateSync
2224
+ // exports.futimesSync = futimesSync TODO
2225
+ // exports.lchmodSync = lchmodSync TODO
2226
+ // exports.lchownSync = lchownSync TODO
2227
+ // exports.lutimesSync = lutimesSync TODO
2228
+ // exports.linkSync = linkSync TODO
2229
+ exports.lstatSync = lstatSync
2230
+ exports.mkdirSync = mkdirSync
2231
+ // exports.mkdtempSync = mkdtempSync TODO
2232
+ exports.openSync = openSync
2233
+ exports.opendirSync = opendirSync
2234
+ exports.readFileSync = readFileSync
2235
+ exports.readSync = readSync
2236
+ exports.readdirSync = readdirSync
2237
+ exports.readlinkSync = readlinkSync
2238
+ exports.readvSync = readvSync
2239
+ exports.realpathSync = realpathSync
2240
+ exports.renameSync = renameSync
2241
+ exports.rmSync = rmSync
2242
+ exports.rmdirSync = rmdirSync
2243
+ exports.statSync = statSync
2244
+ // exports.statfsSync = statfsSync TODO
2245
+ exports.symlinkSync = symlinkSync
2246
+ // exports.truncateSync = truncateSync TODO
2247
+ exports.unlinkSync = unlinkSync
2248
+ exports.utimesSync = utimesSync
2249
+ exports.writeFileSync = writeFileSync
2250
+ exports.writeSync = writeSync
2251
+ exports.writevSync = writevSync
2252
+
2253
+ exports.promises = require('./promises')
2254
+
2255
+ exports.Stats = Stats
2256
+ exports.Dir = Dir
2257
+ exports.Dirent = Dirent
2258
+ exports.Watcher = Watcher
2259
+
2260
+ exports.ReadStream = FileReadStream
2261
+
2262
+ exports.createReadStream = function createReadStream(path, opts) {
2263
+ return new FileReadStream(path, opts)
2264
+ }
2265
+
2266
+ exports.WriteStream = FileWriteStream
2267
+
2268
+ exports.createWriteStream = function createWriteStream(path, opts) {
2269
+ return new FileWriteStream(path, opts)
2270
+ }
2271
+
2272
+ function toNamespacedPath(filepath) {
2273
+ if (typeof filepath !== 'string') {
2274
+ if (URL.isURL(filepath)) filepath = fileURLToPath(filepath)
2275
+ else filepath = filepath.toString()
2276
+ }
2277
+
2278
+ return path.toNamespacedPath(filepath)
2279
+ }
2280
+
2281
+ function toFlags(flags) {
2282
+ switch (flags) {
2283
+ case 'r':
2284
+ return constants.O_RDONLY
2285
+ case 'rs': // Fall through.
2286
+ case 'sr':
2287
+ return constants.O_RDONLY | constants.O_SYNC
2288
+ case 'r+':
2289
+ return constants.O_RDWR
2290
+ case 'rs+': // Fall through.
2291
+ case 'sr+':
2292
+ return constants.O_RDWR | constants.O_SYNC
2293
+
2294
+ case 'w':
2295
+ return constants.O_TRUNC | constants.O_CREAT | constants.O_WRONLY
2296
+ case 'wx': // Fall through.
2297
+ case 'xw':
2298
+ return constants.O_TRUNC | constants.O_CREAT | constants.O_WRONLY | constants.O_EXCL
2299
+
2300
+ case 'w+':
2301
+ return constants.O_TRUNC | constants.O_CREAT | constants.O_RDWR
2302
+ case 'wx+': // Fall through.
2303
+ case 'xw+':
2304
+ return constants.O_TRUNC | constants.O_CREAT | constants.O_RDWR | constants.O_EXCL
2305
+
2306
+ case 'a':
2307
+ return constants.O_APPEND | constants.O_CREAT | constants.O_WRONLY
2308
+ case 'ax': // Fall through.
2309
+ case 'xa':
2310
+ return constants.O_APPEND | constants.O_CREAT | constants.O_WRONLY | constants.O_EXCL
2311
+ case 'as': // Fall through.
2312
+ case 'sa':
2313
+ return constants.O_APPEND | constants.O_CREAT | constants.O_WRONLY | constants.O_SYNC
2314
+
2315
+ case 'a+':
2316
+ return constants.O_APPEND | constants.O_CREAT | constants.O_RDWR
2317
+ case 'ax+': // Fall through.
2318
+ case 'xa+':
2319
+ return constants.O_APPEND | constants.O_CREAT | constants.O_RDWR | constants.O_EXCL
2320
+ case 'as+': // Fall through.
2321
+ case 'sa+':
2322
+ return constants.O_APPEND | constants.O_CREAT | constants.O_RDWR | constants.O_SYNC
2323
+ default:
2324
+ return 0
2325
+ }
2326
+ }
2327
+
2328
+ function toMode(mode) {
2329
+ return parseInt(mode, 8)
2330
+ }