index.js 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884
  1. /* global Bare */
  2. const EventEmitter = require('bare-events')
  3. const os = require('bare-os')
  4. const path = require('bare-path')
  5. const { Readable, Writable } = require('streamx')
  6. const binding = require('./binding')
  7. const isWindows = os.platform() === 'win32'
  8. const constants = exports.constants = {
  9. O_RDWR: binding.O_RDWR,
  10. O_RDONLY: binding.O_RDONLY,
  11. O_WRONLY: binding.O_WRONLY,
  12. O_CREAT: binding.O_CREAT,
  13. O_TRUNC: binding.O_TRUNC,
  14. O_APPEND: binding.O_APPEND,
  15. F_OK: binding.F_OK || 0,
  16. R_OK: binding.R_OK || 0,
  17. W_OK: binding.W_OK || 0,
  18. X_OK: binding.X_OK || 0,
  19. S_IFMT: binding.S_IFMT,
  20. S_IFREG: binding.S_IFREG,
  21. S_IFDIR: binding.S_IFDIR,
  22. S_IFCHR: binding.S_IFCHR,
  23. S_IFLNK: binding.S_IFLNK,
  24. S_IFBLK: binding.S_IFBLK || 0,
  25. S_IFIFO: binding.S_IFIFO || 0,
  26. S_IFSOCK: binding.S_IFSOCK || 0,
  27. S_IRUSR: binding.S_IRUSR || 0,
  28. S_IWUSR: binding.S_IWUSR || 0,
  29. S_IXUSR: binding.S_IXUSR || 0,
  30. S_IRGRP: binding.S_IRGRP || 0,
  31. S_IWGRP: binding.S_IWGRP || 0,
  32. S_IXGRP: binding.S_IXGRP || 0,
  33. S_IROTH: binding.S_IROTH || 0,
  34. S_IWOTH: binding.S_IWOTH || 0,
  35. S_IXOTH: binding.S_IXOTH || 0,
  36. UV_DIRENT_UNKNOWN: binding.UV_DIRENT_UNKNOWN,
  37. UV_DIRENT_FILE: binding.UV_DIRENT_FILE,
  38. UV_DIRENT_DIR: binding.UV_DIRENT_DIR,
  39. UV_DIRENT_LINK: binding.UV_DIRENT_LINK,
  40. UV_DIRENT_FIFO: binding.UV_DIRENT_FIFO,
  41. UV_DIRENT_SOCKET: binding.UV_DIRENT_SOCKET,
  42. UV_DIRENT_CHAR: binding.UV_DIRENT_CHAR,
  43. UV_DIRENT_BLOCK: binding.UV_DIRENT_BLOCK,
  44. UV_FS_SYMLINK_DIR: binding.UV_FS_SYMLINK_DIR,
  45. UV_FS_SYMLINK_JUNCTION: binding.UV_FS_SYMLINK_JUNCTION
  46. }
  47. const reqs = []
  48. let used = 0
  49. const fs = {
  50. handle: Buffer.allocUnsafe(binding.sizeofFS)
  51. }
  52. binding.init(fs.handle, fs, onresponse)
  53. Bare.on('exit', () => binding.destroy(fs.handle))
  54. // Lightly-modified from the Node FS internal utils.
  55. function flagsToNumber (flags) {
  56. switch (flags) {
  57. case 'r' : return constants.O_RDONLY
  58. case 'rs' : // Fall through.
  59. case 'sr' : return constants.O_RDONLY | constants.O_SYNC
  60. case 'r+' : return constants.O_RDWR
  61. case 'rs+' : // Fall through.
  62. case 'sr+' : return constants.O_RDWR | constants.O_SYNC
  63. case 'w' : return constants.O_TRUNC | constants.O_CREAT | constants.O_WRONLY
  64. case 'wx' : // Fall through.
  65. case 'xw' : return constants.O_TRUNC | constants.O_CREAT | constants.O_WRONLY | constants.O_EXCL
  66. case 'w+' : return constants.O_TRUNC | constants.O_CREAT | constants.O_RDWR
  67. case 'wx+': // Fall through.
  68. case 'xw+': return constants.O_TRUNC | constants.O_CREAT | constants.O_RDWR | constants.O_EXCL
  69. case 'a' : return constants.O_APPEND | constants.O_CREAT | constants.O_WRONLY
  70. case 'ax' : // Fall through.
  71. case 'xa' : return constants.O_APPEND | constants.O_CREAT | constants.O_WRONLY | constants.O_EXCL
  72. case 'as' : // Fall through.
  73. case 'sa' : return constants.O_APPEND | constants.O_CREAT | constants.O_WRONLY | constants.O_SYNC
  74. case 'a+' : return constants.O_APPEND | constants.O_CREAT | constants.O_RDWR
  75. case 'ax+': // Fall through.
  76. case 'xa+': return constants.O_APPEND | constants.O_CREAT | constants.O_RDWR | constants.O_EXCL
  77. case 'as+': // Fall through.
  78. case 'sa+': return constants.O_APPEND | constants.O_CREAT | constants.O_RDWR | constants.O_SYNC
  79. }
  80. throw typeError('ERR_INVALID_ARG_VALUE', `Invalid value in flags: ${flags}`)
  81. }
  82. function modeToNumber (mode) {
  83. mode = parseInt(mode, 8)
  84. if (isNaN(mode)) throw typeError('ERR_INVALID_ARG_VALUE', 'Mode must be a number or octal string')
  85. return mode
  86. }
  87. function alloc () {
  88. const handle = Buffer.alloc(binding.sizeofFSReq)
  89. binding.initReq(fs.handle, handle)
  90. const view = new Uint32Array(handle.buffer, handle.byteOffset + binding.offsetofFSReqID, 1)
  91. view[0] = reqs.length
  92. const req = {
  93. handle,
  94. view,
  95. type: 0,
  96. callback: null
  97. }
  98. used++
  99. reqs.push(req)
  100. return req
  101. }
  102. function getReq () {
  103. return used === reqs.length ? alloc() : reqs[used++]
  104. }
  105. function onresponse (id, err, result) {
  106. const req = reqs[id]
  107. used--
  108. if (used !== id) {
  109. const u = reqs[used]
  110. reqs[u.view[0] = id] = u
  111. reqs[req.view[0] = used] = req
  112. }
  113. const callback = req.callback
  114. req.callback = null
  115. callback(err, result)
  116. }
  117. function open (filepath, flags, mode, cb) {
  118. if (typeof filepath !== 'string') {
  119. throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
  120. }
  121. if (typeof cb !== 'function') {
  122. if (typeof flags === 'function') {
  123. cb = flags
  124. flags = 'r'
  125. mode = 0o666
  126. } else if (typeof mode === 'function') {
  127. cb = mode
  128. mode = 0o666
  129. } else {
  130. throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
  131. }
  132. }
  133. if (typeof flags === 'string') flags = flagsToNumber(flags)
  134. if (typeof mode === 'string') mode = modeToNumber(mode)
  135. const req = getReq()
  136. req.callback = cb
  137. binding.open(req.handle, filepath, flags, mode)
  138. }
  139. function openSync (filepath, flags = 'r', mode = 0o666) {
  140. if (typeof filepath !== 'string') {
  141. throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
  142. }
  143. if (typeof flags === 'string') flags = flagsToNumber(flags)
  144. if (typeof mode === 'string') mode = modeToNumber(mode)
  145. return binding.openSync(filepath, flags, mode)
  146. }
  147. function close (fd, cb = noop) {
  148. if (typeof fd !== 'number') {
  149. throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
  150. }
  151. if (fd < 0 || fd > 0x7fffffff) {
  152. throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
  153. }
  154. if (typeof cb !== 'function') {
  155. throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
  156. }
  157. const req = getReq()
  158. req.callback = cb
  159. binding.close(req.handle, fd)
  160. }
  161. function closeSync (fd) {
  162. if (typeof fd !== 'number') {
  163. throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
  164. }
  165. if (fd < 0 || fd > 0x7fffffff) {
  166. throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
  167. }
  168. return binding.closeSync(fd)
  169. }
  170. function access (filepath, mode, cb) {
  171. if (typeof filepath !== 'string') {
  172. throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
  173. }
  174. if (typeof cb !== 'function') {
  175. if (typeof mode === 'function') {
  176. cb = mode
  177. mode = constants.F_OK
  178. } else {
  179. throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
  180. }
  181. }
  182. const req = getReq()
  183. req.callback = cb
  184. binding.access(req.handle, filepath, mode)
  185. }
  186. function accessSync (filepath, mode = constants.F_OK) {
  187. if (typeof filepath !== 'string') {
  188. throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
  189. }
  190. binding.accessSync(filepath, mode)
  191. }
  192. function exists (filepath, cb) {
  193. if (typeof filepath !== 'string') {
  194. throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
  195. }
  196. if (typeof cb !== 'function') {
  197. throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
  198. }
  199. return access(filepath, (err) => cb(!!err)) // eslint-disable-line n/no-callback-literal
  200. }
  201. function existsSync (filepath) {
  202. if (typeof filepath !== 'string') {
  203. throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
  204. }
  205. try {
  206. accessSync(filepath)
  207. return true
  208. } catch {
  209. return false
  210. }
  211. }
  212. function read (fd, buffer, offset, len, pos, cb) {
  213. if (typeof fd !== 'number') {
  214. throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
  215. }
  216. if (fd < 0 || fd > 0x7fffffff) {
  217. throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
  218. }
  219. if (!Buffer.isBuffer(buffer) && !ArrayBuffer.isView(buffer)) {
  220. throw typeError('ERR_INVALID_ARG_TYPE', 'Buffer must be a buffer. Received type ' + (typeof buffer) + ' (' + buffer + ')')
  221. }
  222. if (typeof cb !== 'function') {
  223. if (typeof offset === 'function') {
  224. cb = offset
  225. offset = 0
  226. len = buffer.byteLength
  227. pos = -1
  228. } else if (typeof len === 'function') {
  229. cb = len
  230. len = buffer.byteLength - offset
  231. pos = -1
  232. } else if (typeof pos === 'function') {
  233. cb = pos
  234. pos = -1
  235. } else {
  236. throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
  237. }
  238. }
  239. if (typeof pos !== 'number') pos = -1
  240. const req = getReq()
  241. req.callback = cb
  242. binding.read(req.handle, fd, buffer, offset, len, pos)
  243. }
  244. function readSync (fd, buffer, offset = 0, len = buffer.byteLength - offset, pos = -1) {
  245. if (typeof fd !== 'number') {
  246. throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
  247. }
  248. if (fd < 0 || fd > 0x7fffffff) {
  249. throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
  250. }
  251. if (!Buffer.isBuffer(buffer) && !ArrayBuffer.isView(buffer)) {
  252. throw typeError('ERR_INVALID_ARG_TYPE', 'Buffer must be a buffer. Received type ' + (typeof buffer) + ' (' + buffer + ')')
  253. }
  254. return binding.readSync(fd, buffer, offset, len, pos)
  255. }
  256. function readv (fd, buffers, pos, cb) {
  257. if (typeof fd !== 'number') {
  258. throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
  259. }
  260. if (fd < 0 || fd > 0x7fffffff) {
  261. throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
  262. }
  263. if (typeof pos === 'function') {
  264. cb = pos
  265. pos = -1
  266. } else if (typeof cb !== 'function') {
  267. throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
  268. }
  269. if (typeof pos !== 'number') pos = -1
  270. const req = getReq()
  271. req.callback = cb
  272. binding.readv(req.handle, fd, buffers, pos)
  273. }
  274. function write (fd, buffer, offset, len, pos, cb) {
  275. if (typeof fd !== 'number') {
  276. throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
  277. }
  278. if (fd < 0 || fd > 0x7fffffff) {
  279. throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
  280. }
  281. if (!Buffer.isBuffer(buffer) && !ArrayBuffer.isView(buffer)) {
  282. throw typeError('ERR_INVALID_ARG_TYPE', 'Buffer must be a buffer. Received type ' + (typeof buffer) + ' (' + buffer + ')')
  283. }
  284. if (typeof cb !== 'function') {
  285. if (typeof offset === 'function') {
  286. cb = offset
  287. offset = 0
  288. len = buffer.byteLength
  289. pos = -1
  290. } else if (typeof len === 'function') {
  291. cb = len
  292. len = buffer.byteLength - offset
  293. pos = -1
  294. } else if (typeof pos === 'function') {
  295. cb = pos
  296. pos = -1
  297. } else {
  298. throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
  299. }
  300. }
  301. if (typeof pos !== 'number') pos = -1
  302. const req = getReq()
  303. req.callback = cb
  304. binding.write(req.handle, fd, buffer, offset, len, pos)
  305. }
  306. function writeSync (fd, buffer, offset = 0, len = buffer.byteLength - offset, pos = -1) {
  307. if (typeof fd !== 'number') {
  308. throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
  309. }
  310. if (fd < 0 || fd > 0x7fffffff) {
  311. throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
  312. }
  313. if (!Buffer.isBuffer(buffer) && !ArrayBuffer.isView(buffer)) {
  314. throw typeError('ERR_INVALID_ARG_TYPE', 'Buffer must be a buffer. Received type ' + (typeof buffer) + ' (' + buffer + ')')
  315. }
  316. return binding.writeSync(fd, buffer, offset, len, pos)
  317. }
  318. function writev (fd, buffers, pos, cb) {
  319. if (typeof fd !== 'number') {
  320. throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
  321. }
  322. if (fd < 0 || fd > 0x7fffffff) {
  323. throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
  324. }
  325. if (typeof pos === 'function') {
  326. cb = pos
  327. pos = -1
  328. } else if (typeof cb !== 'function') {
  329. throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
  330. }
  331. if (typeof pos !== 'number') pos = -1
  332. const req = getReq()
  333. req.callback = cb
  334. binding.writev(req.handle, fd, buffers, pos)
  335. }
  336. function stat (filepath, cb) {
  337. if (typeof filepath !== 'string') {
  338. throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
  339. }
  340. if (typeof cb !== 'function') {
  341. throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
  342. }
  343. const data = new Array(Stats.length)
  344. const req = getReq()
  345. req.callback = function (err, _) {
  346. if (err) cb(err, null)
  347. else cb(null, new Stats(...data))
  348. }
  349. binding.stat(req.handle, filepath, data)
  350. }
  351. function statSync (filepath) {
  352. if (typeof filepath !== 'string') {
  353. throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
  354. }
  355. return new Stats(...binding.statSync(filepath))
  356. }
  357. function lstat (filepath, cb) {
  358. if (typeof filepath !== 'string') {
  359. throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
  360. }
  361. if (typeof cb !== 'function') {
  362. throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
  363. }
  364. const data = new Array(Stats.length)
  365. const req = getReq()
  366. req.callback = function (err, _) {
  367. if (err) cb(err, null)
  368. else cb(null, new Stats(...data))
  369. }
  370. binding.lstat(req.handle, filepath, data)
  371. }
  372. function lstatSync (filepath) {
  373. if (typeof filepath !== 'string') {
  374. throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
  375. }
  376. return new Stats(...binding.lstatSync(filepath))
  377. }
  378. function fstat (fd, cb) {
  379. if (typeof fd !== 'number') {
  380. throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
  381. }
  382. if (fd < 0 || fd > 0x7fffffff) {
  383. throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
  384. }
  385. if (typeof cb !== 'function') {
  386. throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
  387. }
  388. const data = new Array(Stats.length)
  389. const req = getReq()
  390. req.callback = function (err, _) {
  391. if (err) cb(err, null)
  392. else cb(null, new Stats(...data))
  393. }
  394. binding.fstat(req.handle, fd, data)
  395. }
  396. function fstatSync (fd) {
  397. if (typeof fd !== 'number') {
  398. throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
  399. }
  400. if (fd < 0 || fd > 0x7fffffff) {
  401. throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
  402. }
  403. return new Stats(...binding.fstatSync(fd))
  404. }
  405. function ftruncate (fd, len, cb) {
  406. if (typeof fd !== 'number') {
  407. throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
  408. }
  409. if (fd < 0 || fd > 0x7fffffff) {
  410. throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
  411. }
  412. if (typeof len === 'function') {
  413. cb = len
  414. len = 0
  415. } else if (typeof cb !== 'function') {
  416. throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
  417. }
  418. const req = getReq()
  419. req.callback = cb
  420. binding.ftruncate(req.handle, fd, len)
  421. }
  422. function chmod (filepath, mode, cb) {
  423. if (typeof filepath !== 'string') {
  424. throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
  425. }
  426. if (typeof mode === 'string') mode = modeToNumber(mode)
  427. const req = getReq()
  428. req.callback = cb
  429. binding.chmod(req.handle, filepath, mode)
  430. }
  431. function chmodSync (filepath, mode) {
  432. if (typeof filepath !== 'string') {
  433. throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
  434. }
  435. if (typeof mode === 'string') mode = modeToNumber(mode)
  436. binding.chmodSync(filepath, mode)
  437. }
  438. function fchmod (fd, mode, cb) {
  439. if (typeof fd !== 'number') {
  440. throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
  441. }
  442. if (fd < 0 || fd > 0x7fffffff) {
  443. throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
  444. }
  445. if (typeof mode === 'string') mode = modeToNumber(mode)
  446. const req = getReq()
  447. req.callback = cb
  448. binding.fchmod(req.handle, fd, mode)
  449. }
  450. function fchmodSync (fd, mode) {
  451. if (typeof fd !== 'number') {
  452. throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
  453. }
  454. if (fd < 0 || fd > 0x7fffffff) {
  455. throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
  456. }
  457. if (typeof mode === 'string') mode = modeToNumber(mode)
  458. binding.fchmodSync(fd, mode)
  459. }
  460. function mkdirRecursive (filepath, mode, cb) {
  461. mkdir(filepath, { mode }, function (err) {
  462. if (err === null) return cb(null, 0, null)
  463. if (err.code !== 'ENOENT') {
  464. stat(filepath, function (e, st) {
  465. if (e) return cb(e, e.errno, null)
  466. if (st.isDirectory()) return cb(null, 0, null)
  467. cb(err, err.errno, null)
  468. })
  469. return
  470. }
  471. while (filepath.endsWith(path.sep)) filepath = filepath.slice(0, -1)
  472. const i = filepath.lastIndexOf(path.sep)
  473. if (i <= 0) return cb(err, err.errno, null)
  474. mkdirRecursive(filepath.slice(0, i), mode, function (err) {
  475. if (err) return cb(err, err.errno, null)
  476. mkdir(filepath, { mode }, function (err) {
  477. if (err === null) return cb(null, 0, null)
  478. stat(filepath, function (e, st) {
  479. if (e) return cb(e, e.errno, null)
  480. if (st.isDirectory()) return cb(null, 0, null)
  481. cb(err, err.errno, null)
  482. })
  483. })
  484. })
  485. })
  486. }
  487. function mkdir (filepath, opts, cb) {
  488. if (typeof filepath !== 'string') {
  489. throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
  490. }
  491. if (typeof opts === 'function') {
  492. cb = opts
  493. opts = { mode: 0o777 }
  494. } else if (typeof cb !== 'function') {
  495. throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
  496. }
  497. if (typeof opts === 'number') opts = { mode: opts }
  498. else if (!opts) opts = {}
  499. const mode = typeof opts.mode === 'number' ? opts.mode : 0o777
  500. if (opts.recursive) return mkdirRecursive(filepath.replace(/\//g, path.sep), mode, cb)
  501. const req = getReq()
  502. req.callback = cb
  503. binding.mkdir(req.handle, filepath, mode)
  504. }
  505. function mkdirRecursiveSync (filepath, mode) {
  506. try {
  507. mkdirSync(filepath, { mode })
  508. } catch (err) {
  509. if (err.code !== 'ENOENT' && statSync(filepath).isDirectory()) {
  510. return
  511. }
  512. while (filepath.endsWith(path.sep)) filepath = filepath.slice(0, -1)
  513. const i = filepath.lastIndexOf(path.sep)
  514. if (i <= 0) throw err
  515. mkdirRecursiveSync(filepath.slice(0, i), { mode })
  516. try {
  517. mkdirSync(filepath, { mode })
  518. } catch (err) {
  519. if (statSync(filepath).isDirectory()) {
  520. return
  521. }
  522. throw err
  523. }
  524. }
  525. }
  526. function mkdirSync (filepath, opts) {
  527. if (typeof filepath !== 'string') {
  528. throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
  529. }
  530. if (typeof opts === 'number') opts = { mode: opts }
  531. else if (!opts) opts = {}
  532. const mode = typeof opts.mode === 'number' ? opts.mode : 0o777
  533. if (opts.recursive) return mkdirRecursiveSync(filepath.replace(/\//g, path.sep), mode)
  534. binding.mkdirSync(filepath, mode)
  535. }
  536. function rmdir (filepath, cb) {
  537. if (typeof filepath !== 'string') {
  538. throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
  539. }
  540. if (typeof cb !== 'function') {
  541. throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
  542. }
  543. const req = getReq()
  544. req.callback = cb
  545. binding.rmdir(req.handle, filepath)
  546. }
  547. function rmdirSync (filepath) {
  548. if (typeof filepath !== 'string') {
  549. throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
  550. }
  551. binding.rmdirSync(filepath)
  552. }
  553. function rmRecursive (filepath, opts, cb) {
  554. rmdir(filepath, function (err) {
  555. if (err === null) return cb(null)
  556. if (err.code !== 'ENOTEMPTY') return cb(err)
  557. readdir(filepath, function (err, files) {
  558. if (err) return cb(err)
  559. if (files.length === 0) return rmdir(filepath, cb)
  560. let missing = files.length
  561. let done = false
  562. for (const file of files) {
  563. rm(filepath + path.sep + file, opts, function (err) {
  564. if (done) return
  565. if (err) {
  566. done = true
  567. return cb(err)
  568. }
  569. if (--missing === 0) rmdir(filepath, cb)
  570. })
  571. }
  572. })
  573. })
  574. }
  575. function rm (filepath, opts, cb) {
  576. if (typeof filepath !== 'string') {
  577. throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
  578. }
  579. if (typeof opts === 'function') {
  580. cb = opts
  581. opts = {}
  582. } else if (typeof cb !== 'function') {
  583. throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
  584. }
  585. if (!opts) opts = {}
  586. lstat(filepath, function (err, st) {
  587. if (err) {
  588. return cb(err.code === 'ENOENT' && opts.force ? null : err)
  589. }
  590. if (st.isDirectory()) {
  591. if (opts.recursive) return rmRecursive(filepath, opts, cb)
  592. const err = new Error('is a directory')
  593. err.code = 'EISDIR'
  594. return cb(err)
  595. }
  596. unlink(filepath, cb)
  597. })
  598. }
  599. function rmRecursiveSync (filepath, opts) {
  600. try {
  601. rmdirSync(filepath)
  602. } catch (err) {
  603. if (err.code !== 'ENOTEMPTY') throw err
  604. const files = readdirSync(filepath)
  605. for (const file of files) {
  606. rmSync(filepath + path.sep + file, opts)
  607. }
  608. rmdirSync(filepath)
  609. }
  610. }
  611. function rmSync (filepath, opts) {
  612. if (typeof filepath !== 'string') {
  613. throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
  614. }
  615. if (!opts) opts = {}
  616. try {
  617. const st = lstatSync(filepath)
  618. if (st.isDirectory()) {
  619. if (opts.recursive) return rmRecursiveSync(filepath, opts)
  620. const err = new Error('is a directory')
  621. err.code = 'EISDIR'
  622. throw err
  623. }
  624. unlinkSync(filepath)
  625. } catch (err) {
  626. if (err.code !== 'ENOENT' || !opts.force) throw err
  627. }
  628. }
  629. function unlink (filepath, cb) {
  630. if (typeof filepath !== 'string') {
  631. throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
  632. }
  633. if (typeof cb !== 'function') {
  634. throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
  635. }
  636. const req = getReq()
  637. req.callback = cb
  638. binding.unlink(req.handle, filepath)
  639. }
  640. function unlinkSync (filepath) {
  641. if (typeof filepath !== 'string') {
  642. throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
  643. }
  644. binding.unlinkSync(filepath)
  645. }
  646. function rename (src, dst, cb) {
  647. if (typeof src !== 'string') {
  648. throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof src) + ' (' + src + ')')
  649. }
  650. if (typeof dst !== 'string') {
  651. throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof dst) + ' (' + dst + ')')
  652. }
  653. if (typeof cb !== 'function') {
  654. throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
  655. }
  656. const req = getReq()
  657. req.callback = cb
  658. binding.rename(req.handle, src, dst)
  659. }
  660. function renameSync (src, dst) {
  661. if (typeof src !== 'string') {
  662. throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof src) + ' (' + src + ')')
  663. }
  664. if (typeof dst !== 'string') {
  665. throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof dst) + ' (' + dst + ')')
  666. }
  667. binding.renameSync(src, dst)
  668. }
  669. function realpath (filepath, opts, cb) {
  670. if (typeof filepath !== 'string') {
  671. throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
  672. }
  673. if (typeof opts === 'function') {
  674. cb = opts
  675. opts = {}
  676. } else if (typeof cb !== 'function') {
  677. throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
  678. }
  679. if (typeof opts === 'string') opts = { encoding: opts }
  680. else if (!opts) opts = {}
  681. const {
  682. encoding = 'utf8'
  683. } = opts
  684. const data = Buffer.allocUnsafe(binding.sizeofFSPath)
  685. const req = getReq()
  686. req.callback = function (err, _) {
  687. if (err) return cb(err, null)
  688. let path = data.subarray(0, data.indexOf(0))
  689. if (encoding !== 'buffer') path = path.toString(encoding)
  690. cb(null, path)
  691. }
  692. binding.realpath(req.handle, filepath, data)
  693. }
  694. function realpathSync (filepath, opts) {
  695. if (typeof filepath !== 'string') {
  696. throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
  697. }
  698. if (typeof opts === 'string') opts = { encoding: opts }
  699. else if (!opts) opts = {}
  700. const {
  701. encoding = 'utf8'
  702. } = opts
  703. const data = Buffer.allocUnsafe(binding.sizeofFSPath)
  704. binding.realpathSync(filepath, data)
  705. filepath = data.subarray(0, data.indexOf(0))
  706. if (encoding !== 'buffer') filepath = filepath.toString(encoding)
  707. return filepath
  708. }
  709. function readlink (filepath, opts, cb) {
  710. if (typeof filepath !== 'string') {
  711. throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
  712. }
  713. if (typeof opts === 'function') {
  714. cb = opts
  715. opts = {}
  716. } else if (typeof cb !== 'function') {
  717. throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
  718. }
  719. if (typeof opts === 'string') opts = { encoding: opts }
  720. else if (!opts) opts = {}
  721. const {
  722. encoding = 'utf8'
  723. } = opts
  724. const data = Buffer.allocUnsafe(binding.sizeofFSPath)
  725. const req = getReq()
  726. req.callback = function (err, _) {
  727. if (err) return cb(err, null)
  728. let path = data.subarray(0, data.indexOf(0))
  729. if (encoding !== 'buffer') path = path.toString(encoding)
  730. cb(null, path)
  731. }
  732. binding.readlink(req.handle, filepath, data)
  733. }
  734. function readlinkSync (filepath, opts) {
  735. if (typeof filepath !== 'string') {
  736. throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
  737. }
  738. if (typeof opts === 'string') opts = { encoding: opts }
  739. else if (!opts) opts = {}
  740. const {
  741. encoding = 'utf8'
  742. } = opts
  743. const data = Buffer.allocUnsafe(binding.sizeofFSPath)
  744. binding.readlinkSync(filepath, data)
  745. filepath = data.subarray(0, data.indexOf(0))
  746. if (encoding !== 'buffer') filepath = filepath.toString(encoding)
  747. return filepath
  748. }
  749. function normalizeSymlinkTarget (target, type, filepath) {
  750. if (isWindows) {
  751. if (type === 'junction') target = path.resolve(filepath, '..', target)
  752. if (path.isAbsolute(target)) return path.toNamespacedPath(target)
  753. return target.replace(/\//g, path.sep)
  754. }
  755. return target
  756. }
  757. function symlink (target, filepath, type, cb) {
  758. if (typeof target !== 'string') {
  759. throw typeError('ERR_INVALID_ARG_TYPE', 'Target must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
  760. }
  761. if (typeof filepath !== 'string') {
  762. throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
  763. }
  764. if (typeof type === 'function') {
  765. cb = type
  766. type = null
  767. } else if (typeof cb !== 'function') {
  768. throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
  769. }
  770. if (typeof type === 'string') {
  771. switch (type) {
  772. case 'file':
  773. type = 0
  774. break
  775. case 'dir':
  776. type = constants.UV_FS_SYMLINK_DIR
  777. break
  778. case 'junction':
  779. type = constants.UV_FS_SYMLINK_JUNCTION
  780. break
  781. default:
  782. throw typeError('ERR_FS_INVALID_SYMLINK_TYPE', 'Symlink type must be one of "dir", "file", or "junction". Received "' + type + '"')
  783. }
  784. } else if (typeof type !== 'number') {
  785. if (isWindows) {
  786. target = path.resolve(filepath, '..', target)
  787. stat(target, (err, st) => {
  788. type = err === null && st.isDirectory() ? constants.UV_FS_SYMLINK_DIR : constants.UV_FS_SYMLINK_JUNCTION
  789. symlink(target, filepath, type, cb)
  790. })
  791. return
  792. }
  793. type = 0
  794. }
  795. const req = getReq()
  796. req.callback = cb
  797. binding.symlink(req.handle, normalizeSymlinkTarget(target), path.toNamespacedPath(filepath), type)
  798. }
  799. function symlinkSync (target, filepath, type) {
  800. if (typeof target !== 'string') {
  801. throw typeError('ERR_INVALID_ARG_TYPE', 'Target must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
  802. }
  803. if (typeof filepath !== 'string') {
  804. throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
  805. }
  806. if (typeof type === 'string') {
  807. switch (type) {
  808. case 'file':
  809. type = 0
  810. break
  811. case 'dir':
  812. type = constants.UV_FS_SYMLINK_DIR
  813. break
  814. case 'junction':
  815. type = constants.UV_FS_SYMLINK_JUNCTION
  816. break
  817. default:
  818. throw typeError('ERR_FS_INVALID_SYMLINK_TYPE', 'Symlink type must be one of "dir", "file", or "junction". Received "' + type + '"')
  819. }
  820. } else if (typeof type !== 'number') {
  821. if (isWindows) {
  822. target = path.resolve(filepath, '..', target)
  823. type = statSync(target).isDirectory() ? constants.UV_FS_SYMLINK_DIR : constants.UV_FS_SYMLINK_JUNCTION
  824. } else {
  825. type = 0
  826. }
  827. }
  828. binding.symlinkSync(normalizeSymlinkTarget(target), path.toNamespacedPath(filepath), type)
  829. }
  830. function opendir (filepath, opts, cb) {
  831. if (typeof filepath !== 'string') {
  832. throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
  833. }
  834. if (typeof opts === 'function') {
  835. cb = opts
  836. opts = {}
  837. } else if (typeof cb !== 'function') {
  838. throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
  839. }
  840. if (typeof opts === 'string') opts = { encoding: opts }
  841. else if (!opts) opts = {}
  842. const data = Buffer.allocUnsafe(binding.sizeofFSDir)
  843. const req = getReq()
  844. req.callback = function (err, _) {
  845. if (err) return cb(err, null)
  846. cb(null, new Dir(filepath, data, opts))
  847. }
  848. binding.opendir(req.handle, filepath, data)
  849. }
  850. function opendirSync (filepath, opts) {
  851. if (typeof filepath !== 'string') {
  852. throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
  853. }
  854. if (typeof opts === 'string') opts = { encoding: opts }
  855. else if (!opts) opts = {}
  856. const data = Buffer.allocUnsafe(binding.sizeofFSDir)
  857. binding.opendirSync(filepath, data)
  858. return new Dir(filepath, data, opts)
  859. }
  860. function readdir (filepath, opts, cb) {
  861. if (typeof filepath !== 'string') {
  862. throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
  863. }
  864. if (typeof opts === 'function') {
  865. cb = opts
  866. opts = {}
  867. } else if (typeof cb !== 'function') {
  868. throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
  869. }
  870. if (typeof opts === 'string') opts = { encoding: opts }
  871. else if (!opts) opts = {}
  872. const {
  873. withFileTypes = false
  874. } = opts
  875. opendir(filepath, opts, async (err, dir) => {
  876. if (err) return cb(err, null)
  877. const result = []
  878. for await (const entry of dir) {
  879. result.push(withFileTypes ? entry : entry.name)
  880. }
  881. cb(null, result)
  882. })
  883. }
  884. function readdirSync (filepath, opts) {
  885. if (typeof filepath !== 'string') {
  886. throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
  887. }
  888. if (typeof opts === 'string') opts = { encoding: opts }
  889. else if (!opts) opts = {}
  890. const {
  891. withFileTypes = false
  892. } = opts
  893. const dir = opendirSync(filepath, opts)
  894. const result = []
  895. while (true) {
  896. const entry = dir.readSync()
  897. if (entry === null) break
  898. result.push(withFileTypes ? entry : entry.name)
  899. }
  900. return result
  901. }
  902. function readFile (filepath, opts, cb) {
  903. if (typeof filepath !== 'string') {
  904. throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
  905. }
  906. if (typeof opts === 'function') {
  907. cb = opts
  908. opts = {}
  909. } else if (typeof cb !== 'function') {
  910. throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
  911. }
  912. if (typeof opts === 'string') opts = { encoding: opts }
  913. else if (!opts) opts = {}
  914. const {
  915. encoding = 'buffer'
  916. } = opts
  917. open(filepath, opts.flag || 'r', function (err, fd) {
  918. if (err) return cb(err)
  919. fstat(fd, function (err, st) {
  920. if (err) return closeAndError(err)
  921. let buffer = Buffer.allocUnsafe(st.size)
  922. let len = 0
  923. read(fd, buffer, loop)
  924. function loop (err, r) {
  925. if (err) return closeAndError(err)
  926. len += r
  927. if (r === 0 || len === buffer.byteLength) return done()
  928. read(fd, buffer.subarray(len), loop)
  929. }
  930. function done () {
  931. if (len !== buffer.byteLength) buffer = buffer.subarray(0, len)
  932. close(fd, function (err) {
  933. if (err) return cb(err)
  934. if (encoding !== 'buffer') buffer = buffer.toString(encoding)
  935. cb(null, buffer)
  936. })
  937. }
  938. })
  939. function closeAndError (err) {
  940. close(fd, function () {
  941. cb(err)
  942. })
  943. }
  944. })
  945. }
  946. function readFileSync (filepath, opts) {
  947. if (typeof filepath !== 'string') {
  948. throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
  949. }
  950. if (typeof opts === 'string') opts = { encoding: opts }
  951. else if (!opts) opts = {}
  952. const {
  953. encoding = 'buffer'
  954. } = opts
  955. const fd = openSync(filepath, opts.flag || 'r')
  956. try {
  957. const st = fstatSync(fd)
  958. let buffer = Buffer.allocUnsafe(st.size)
  959. let len = 0
  960. while (true) {
  961. const r = readSync(fd, len ? buffer.subarray(len) : buffer)
  962. len += r
  963. if (r === 0 || len === buffer.byteLength) break
  964. }
  965. if (len !== buffer.byteLength) buffer = buffer.subarray(0, len)
  966. if (encoding !== 'buffer') buffer = buffer.toString(encoding)
  967. return buffer
  968. } finally {
  969. try {
  970. closeSync(fd)
  971. } catch {}
  972. }
  973. }
  974. function writeFile (filepath, data, opts, cb) {
  975. if (typeof filepath !== 'string') {
  976. throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
  977. }
  978. if (typeof data !== 'string' && !Buffer.isBuffer(data) && !ArrayBuffer.isView(data)) {
  979. throw typeError('ERR_INVALID_ARG_TYPE', 'Data must be a string or buffer. Received type ' + (typeof data) + ' (' + data + ')')
  980. }
  981. if (typeof opts === 'function') {
  982. cb = opts
  983. opts = {}
  984. } else if (typeof cb !== 'function') {
  985. throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
  986. }
  987. if (typeof opts === 'string') opts = { encoding: opts }
  988. else if (!opts) opts = {}
  989. if (typeof data === 'string') data = Buffer.from(data, opts.encoding)
  990. open(filepath, opts.flag || 'w', opts.mode || 0o666, function (err, fd) {
  991. if (err) return cb(err)
  992. write(fd, data, loop)
  993. function loop (err, w) {
  994. if (err) return closeAndError(err)
  995. if (w === data.byteLength) return done()
  996. write(fd, data.subarray(w), loop)
  997. }
  998. function done () {
  999. close(fd, function (err) {
  1000. if (err) return cb(err)
  1001. return cb(null)
  1002. })
  1003. }
  1004. function closeAndError (err) {
  1005. close(fd, function () {
  1006. cb(err)
  1007. })
  1008. }
  1009. })
  1010. }
  1011. function writeFileSync (filepath, data, opts) {
  1012. if (typeof filepath !== 'string') {
  1013. throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
  1014. }
  1015. if (typeof data !== 'string' && !Buffer.isBuffer(data) && !ArrayBuffer.isView(data)) {
  1016. throw typeError('ERR_INVALID_ARG_TYPE', 'Data must be a string or buffer. Received type ' + (typeof data) + ' (' + data + ')')
  1017. }
  1018. if (typeof opts === 'string') opts = { encoding: opts }
  1019. else if (!opts) opts = {}
  1020. if (typeof data === 'string') data = Buffer.from(data, opts.encoding)
  1021. const fd = openSync(filepath, opts.flag || 'w', opts.mode)
  1022. try {
  1023. let len = 0
  1024. while (true) {
  1025. len += writeSync(fd, len ? data.subarray(len) : data)
  1026. if (len === data.byteLength) break
  1027. }
  1028. } finally {
  1029. try {
  1030. closeSync(fd)
  1031. } catch {}
  1032. }
  1033. }
  1034. function watch (filepath, opts, cb) {
  1035. if (typeof filepath !== 'string') {
  1036. throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
  1037. }
  1038. if (typeof opts === 'function') {
  1039. cb = opts
  1040. opts = {}
  1041. }
  1042. if (typeof opts === 'string') opts = { encoding: opts }
  1043. else if (!opts) opts = {}
  1044. const watcher = new Watcher(filepath, opts)
  1045. if (cb) watcher.on('change', cb)
  1046. return watcher
  1047. }
  1048. class Stats {
  1049. constructor (dev, mode, nlink, uid, gid, rdev, blksize, ino, size, blocks, atimeMs, mtimeMs, ctimeMs, birthtimeMs) {
  1050. this.dev = dev
  1051. this.mode = mode
  1052. this.nlink = nlink
  1053. this.uid = uid
  1054. this.gid = gid
  1055. this.rdev = rdev
  1056. this.blksize = blksize
  1057. this.ino = ino
  1058. this.size = size
  1059. this.blocks = blocks
  1060. this.atimeMs = atimeMs
  1061. this.mtimeMs = mtimeMs
  1062. this.ctimeMs = ctimeMs
  1063. this.birthtimeMs = birthtimeMs
  1064. this.atime = new Date(atimeMs)
  1065. this.mtime = new Date(mtimeMs)
  1066. this.ctime = new Date(ctimeMs)
  1067. this.birthtime = new Date(birthtimeMs)
  1068. }
  1069. isDirectory () {
  1070. return (this.mode & constants.S_IFMT) === constants.S_IFDIR
  1071. }
  1072. isFile () {
  1073. return (this.mode & constants.S_IFMT) === constants.S_IFREG
  1074. }
  1075. isBlockDevice () {
  1076. return (this.mode & constants.S_IFMT) === constants.S_IFBLK
  1077. }
  1078. isCharacterDevice () {
  1079. return (this.mode & constants.S_IFCHR) === constants.S_IFCHR
  1080. }
  1081. isFIFO () {
  1082. return (this.mode & constants.S_IFMT) === constants.S_IFIFO
  1083. }
  1084. isSymbolicLink () {
  1085. return (this.mode & constants.S_IFMT) === constants.S_IFLNK
  1086. }
  1087. isSocket () {
  1088. return (this.mode & constants.S_IFMT) === constants.S_IFSOCK
  1089. }
  1090. }
  1091. class Dir {
  1092. constructor (path, handle, opts = {}) {
  1093. const {
  1094. encoding = 'utf8',
  1095. bufferSize = 32
  1096. } = opts
  1097. this._handle = handle
  1098. this._dirents = Buffer.allocUnsafe(binding.sizeofFSDirent * bufferSize)
  1099. this._encoding = encoding
  1100. this._buffer = []
  1101. this._ended = false
  1102. this.path = path
  1103. }
  1104. read (cb) {
  1105. if (!cb) return promisify(this.read.bind(this))
  1106. if (this._buffer.length) return queueMicrotask(() => cb(null, this._buffer.shift()))
  1107. if (this._ended) return queueMicrotask(() => cb(null, null))
  1108. const data = []
  1109. const req = getReq()
  1110. req.callback = (err, _) => {
  1111. if (err) return cb(err, null)
  1112. if (data.length === 0) this._ended = true
  1113. else {
  1114. for (const entry of data) {
  1115. let name = Buffer.from(entry.name)
  1116. if (this._encoding !== 'buffer') name = name.toString(this._encoding)
  1117. this._buffer.push(new Dirent(this.path, name, entry.type))
  1118. }
  1119. }
  1120. if (this._ended) return cb(null, null)
  1121. cb(null, this._buffer.shift())
  1122. }
  1123. binding.readdir(req.handle, this._handle, this._dirents, data)
  1124. }
  1125. readSync () {
  1126. if (this._buffer.length) return this._buffer.shift()
  1127. if (this._ended) return null
  1128. const data = []
  1129. binding.readdirSync(this._handle, this._dirents, data)
  1130. if (data.length === 0) this._ended = true
  1131. else {
  1132. for (const entry of data) {
  1133. let name = Buffer.from(entry.name)
  1134. if (this._encoding !== 'buffer') name = name.toString(this._encoding)
  1135. this._buffer.push(new Dirent(this.path, name, entry.type))
  1136. }
  1137. }
  1138. if (this._ended) return null
  1139. return this._buffer.shift()
  1140. }
  1141. close (cb) {
  1142. if (!cb) return promisify(this.close.bind(this))
  1143. const req = getReq()
  1144. req.callback = (err, _) => {
  1145. this._handle = null
  1146. cb(err)
  1147. }
  1148. binding.closedir(req.handle, this._handle)
  1149. }
  1150. closeSync () {
  1151. binding.closedirSync(this._handle)
  1152. this._handle = null
  1153. }
  1154. [Symbol.iterator] () {
  1155. return {
  1156. next: () => {
  1157. if (this._buffer.length) {
  1158. return { done: false, value: this._buffer.shift() }
  1159. }
  1160. if (this._ended) {
  1161. return { done: true }
  1162. }
  1163. const entry = this.readSync()
  1164. if (entry) {
  1165. return { done: false, value: entry }
  1166. }
  1167. this.closeSync()
  1168. return { done: true }
  1169. }
  1170. }
  1171. }
  1172. [Symbol.asyncIterator] () {
  1173. return {
  1174. next: () => new Promise((resolve, reject) => {
  1175. if (this._buffer.length) {
  1176. return resolve({ done: false, value: this._buffer.shift() })
  1177. }
  1178. if (this._ended) {
  1179. return resolve({ done: true })
  1180. }
  1181. this.read((err, entry) => {
  1182. if (err) return reject(err)
  1183. if (entry) {
  1184. return resolve({ done: false, value: entry })
  1185. }
  1186. this.close((err) => err ? reject(err) : resolve({ done: true }))
  1187. })
  1188. })
  1189. }
  1190. }
  1191. }
  1192. class Dirent {
  1193. constructor (path, name, type) {
  1194. this.type = type
  1195. this.path = path
  1196. this.name = name
  1197. }
  1198. isFile () {
  1199. return this.type === constants.UV_DIRENT_FILE
  1200. }
  1201. isDirectory () {
  1202. return this.type === constants.UV_DIRENT_DIR
  1203. }
  1204. isSymbolicLink () {
  1205. return this.type === constants.UV_DIRENT_LINK
  1206. }
  1207. isFIFO () {
  1208. return this.type === constants.UV_DIRENT_FIFO
  1209. }
  1210. isSocket () {
  1211. return this.type === constants.UV_DIRENT_SOCKET
  1212. }
  1213. isCharacterDevice () {
  1214. return this.type === constants.UV_DIRENT_CHAR
  1215. }
  1216. isBlockDevice () {
  1217. return this.type === constants.UV_DIRENT_BLOCK
  1218. }
  1219. }
  1220. class FileWriteStream extends Writable {
  1221. constructor (path, opts = {}) {
  1222. super({ map })
  1223. this.path = path
  1224. this.fd = 0
  1225. this.flags = opts.flags || 'w'
  1226. this.mode = opts.mode || 0o666
  1227. }
  1228. _open (cb) {
  1229. open(this.path, this.flags, this.mode, (err, fd) => {
  1230. if (err) return cb(err)
  1231. this.fd = fd
  1232. cb(null)
  1233. })
  1234. }
  1235. _writev (datas, cb) {
  1236. writev(this.fd, datas, cb)
  1237. }
  1238. _destroy (cb) {
  1239. if (!this.fd) return cb(null)
  1240. close(this.fd, () => cb(null))
  1241. }
  1242. }
  1243. class FileReadStream extends Readable {
  1244. constructor (path, opts = {}) {
  1245. super()
  1246. this.path = path
  1247. this.fd = 0
  1248. this._offset = opts.start || 0
  1249. this._missing = 0
  1250. if (opts.length) this._missing = opts.length
  1251. else if (typeof opts.end === 'number') this._missing = opts.end - this._offset + 1
  1252. else this._missing = -1
  1253. }
  1254. _open (cb) {
  1255. open(this.path, constants.O_RDONLY, (err, fd) => {
  1256. if (err) return cb(err)
  1257. const onerror = (err) => close(fd, () => cb(err))
  1258. fstat(fd, (err, st) => {
  1259. if (err) return onerror(err)
  1260. if (!st.isFile()) return onerror(new Error(this.path + ' is not a file'))
  1261. this.fd = fd
  1262. if (this._missing === -1) this._missing = st.size
  1263. if (st.size < this._offset) {
  1264. this._offset = st.size
  1265. this._missing = 0
  1266. return cb(null)
  1267. }
  1268. if (st.size < this._offset + this._missing) {
  1269. this._missing = st.size - this._offset
  1270. return cb(null)
  1271. }
  1272. cb(null)
  1273. })
  1274. })
  1275. }
  1276. _read (cb) {
  1277. if (!this._missing) {
  1278. this.push(null)
  1279. return cb(null)
  1280. }
  1281. const data = Buffer.allocUnsafe(Math.min(this._missing, 65536))
  1282. read(this.fd, data, 0, data.byteLength, this._offset, (err, read) => {
  1283. if (err) return cb(err)
  1284. if (!read) {
  1285. this.push(null)
  1286. return cb(null)
  1287. }
  1288. if (this._missing < read) read = this._missing
  1289. this.push(data.subarray(0, read))
  1290. this._missing -= read
  1291. this._offset += read
  1292. if (!this._missing) this.push(null)
  1293. cb(null)
  1294. })
  1295. }
  1296. _destroy (cb) {
  1297. if (!this.fd) return cb(null)
  1298. close(this.fd, () => cb(null))
  1299. }
  1300. }
  1301. class Watcher extends EventEmitter {
  1302. constructor (path, opts) {
  1303. const {
  1304. persistent = true,
  1305. recursive = false,
  1306. encoding = 'utf8'
  1307. } = opts
  1308. super()
  1309. this._closed = false
  1310. this._encoding = encoding
  1311. this._handle = binding.watcherInit(path, recursive, this, this._onevent, this._onclose)
  1312. if (!persistent) this.unref()
  1313. }
  1314. _onevent (err, events, filename) {
  1315. if (err) {
  1316. this.close()
  1317. this.emit('error', err)
  1318. } else {
  1319. const path = this._encoding === 'buffer'
  1320. ? Buffer.from(filename)
  1321. : Buffer.from(filename).toString(this._encoding)
  1322. if (events & binding.UV_RENAME) {
  1323. this.emit('change', 'rename', path)
  1324. }
  1325. if (events & binding.UV_CHANGE) {
  1326. this.emit('change', 'change', path)
  1327. }
  1328. }
  1329. }
  1330. _onclose () {
  1331. this._handle = null
  1332. this.emit('close')
  1333. }
  1334. close () {
  1335. if (this._closed) return
  1336. this._closed = true
  1337. binding.watcherClose(this._handle)
  1338. }
  1339. ref () {
  1340. if (this._handle) binding.watcherRef(this._handle)
  1341. return this
  1342. }
  1343. unref () {
  1344. if (this._handle) binding.watcherUnref(this._handle)
  1345. return this
  1346. }
  1347. [Symbol.asyncIterator] () {
  1348. const buffer = []
  1349. let done = false
  1350. let error = null
  1351. let next = null
  1352. this
  1353. .on('change', (eventType, filename) => {
  1354. if (next) {
  1355. next.resolve({ done: false, value: { eventType, filename } })
  1356. next = null
  1357. } else {
  1358. buffer.push({ eventType, filename })
  1359. }
  1360. })
  1361. .on('error', (err) => {
  1362. done = true
  1363. error = err
  1364. if (next) {
  1365. next.reject(error)
  1366. next = null
  1367. }
  1368. })
  1369. .on('close', () => {
  1370. done = true
  1371. if (next) {
  1372. next.resolve({ done })
  1373. next = null
  1374. }
  1375. })
  1376. return {
  1377. next: () => new Promise((resolve, reject) => {
  1378. if (error) return reject(error)
  1379. if (buffer.length) return resolve({ done: false, value: buffer.shift() })
  1380. if (done) return resolve({ done })
  1381. next = { resolve, reject }
  1382. })
  1383. }
  1384. }
  1385. }
  1386. exports.promises = {}
  1387. function typeError (code, message) {
  1388. const error = new TypeError(message)
  1389. error.code = code
  1390. return error
  1391. }
  1392. function noop () {}
  1393. exports.access = access
  1394. exports.chmod = chmod
  1395. exports.close = close
  1396. exports.exists = exists
  1397. exports.fchmod = fchmod
  1398. exports.fstat = fstat
  1399. exports.ftruncate = ftruncate
  1400. exports.lstat = lstat
  1401. exports.mkdir = mkdir
  1402. exports.open = open
  1403. exports.opendir = opendir
  1404. exports.read = read
  1405. exports.readFile = readFile
  1406. exports.readdir = readdir
  1407. exports.readlink = readlink
  1408. exports.readv = readv
  1409. exports.realpath = realpath
  1410. exports.rename = rename
  1411. exports.rm = rm
  1412. exports.rmdir = rmdir
  1413. exports.stat = stat
  1414. exports.symlink = symlink
  1415. exports.unlink = unlink
  1416. exports.watch = watch
  1417. exports.write = write
  1418. exports.writeFile = writeFile
  1419. exports.writev = writev
  1420. exports.accessSync = accessSync
  1421. exports.chmodSync = chmodSync
  1422. exports.closeSync = closeSync
  1423. exports.existsSync = existsSync
  1424. exports.fchmodSync = fchmodSync
  1425. exports.fstatSync = fstatSync
  1426. exports.lstatSync = lstatSync
  1427. exports.mkdirSync = mkdirSync
  1428. exports.openSync = openSync
  1429. exports.opendirSync = opendirSync
  1430. exports.readFileSync = readFileSync
  1431. exports.readSync = readSync
  1432. exports.readdirSync = readdirSync
  1433. exports.readlinkSync = readlinkSync
  1434. exports.realpathSync = realpathSync
  1435. exports.renameSync = renameSync
  1436. exports.rmSync = rmSync
  1437. exports.rmdirSync = rmdirSync
  1438. exports.statSync = statSync
  1439. exports.symlinkSync = symlinkSync
  1440. exports.unlinkSync = unlinkSync
  1441. exports.writeFileSync = writeFileSync
  1442. exports.writeSync = writeSync
  1443. exports.promises.access = promisify(access)
  1444. exports.promises.chmod = promisify(chmod)
  1445. exports.promises.lstat = promisify(lstat)
  1446. exports.promises.mkdir = promisify(mkdir)
  1447. exports.promises.opendir = promisify(opendir)
  1448. exports.promises.readFile = promisify(readFile)
  1449. exports.promises.readdir = promisify(readdir)
  1450. exports.promises.readlink = promisify(readlink)
  1451. exports.promises.realpath = promisify(realpath)
  1452. exports.promises.rename = promisify(rename)
  1453. exports.promises.rm = promisify(rm)
  1454. exports.promises.rmdir = promisify(rmdir)
  1455. exports.promises.stat = promisify(stat)
  1456. exports.promises.symlink = promisify(symlink)
  1457. exports.promises.unlink = promisify(unlink)
  1458. exports.promises.writeFile = promisify(writeFile)
  1459. exports.promises.watch = watch // Already async iterable
  1460. exports.Stats = Stats
  1461. exports.Dir = Dir
  1462. exports.Dirent = Dirent
  1463. exports.Watcher = Watcher
  1464. exports.ReadStream = FileReadStream
  1465. exports.createReadStream = function createReadStream (path, opts) {
  1466. return new FileReadStream(path, opts)
  1467. }
  1468. exports.WriteStream = FileWriteStream
  1469. exports.createWriteStream = function createWriteStream (path, opts) {
  1470. return new FileWriteStream(path, opts)
  1471. }
  1472. function promisify (fn) {
  1473. return function (...args) {
  1474. return new Promise((resolve, reject) => {
  1475. fn(...args, function (err, res) {
  1476. if (err) return reject(err)
  1477. resolve(res)
  1478. })
  1479. })
  1480. }
  1481. }
  1482. function map (data) {
  1483. return typeof data === 'string' ? Buffer.from(data) : data
  1484. }