123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884 |
- /* global Bare */
- const EventEmitter = require('bare-events')
- const os = require('bare-os')
- const path = require('bare-path')
- const { Readable, Writable } = require('streamx')
- const binding = require('./binding')
- const isWindows = os.platform() === 'win32'
- const constants = exports.constants = {
- O_RDWR: binding.O_RDWR,
- O_RDONLY: binding.O_RDONLY,
- O_WRONLY: binding.O_WRONLY,
- O_CREAT: binding.O_CREAT,
- O_TRUNC: binding.O_TRUNC,
- O_APPEND: binding.O_APPEND,
- F_OK: binding.F_OK || 0,
- R_OK: binding.R_OK || 0,
- W_OK: binding.W_OK || 0,
- X_OK: binding.X_OK || 0,
- S_IFMT: binding.S_IFMT,
- S_IFREG: binding.S_IFREG,
- S_IFDIR: binding.S_IFDIR,
- S_IFCHR: binding.S_IFCHR,
- S_IFLNK: binding.S_IFLNK,
- S_IFBLK: binding.S_IFBLK || 0,
- S_IFIFO: binding.S_IFIFO || 0,
- S_IFSOCK: binding.S_IFSOCK || 0,
- S_IRUSR: binding.S_IRUSR || 0,
- S_IWUSR: binding.S_IWUSR || 0,
- S_IXUSR: binding.S_IXUSR || 0,
- S_IRGRP: binding.S_IRGRP || 0,
- S_IWGRP: binding.S_IWGRP || 0,
- S_IXGRP: binding.S_IXGRP || 0,
- S_IROTH: binding.S_IROTH || 0,
- S_IWOTH: binding.S_IWOTH || 0,
- S_IXOTH: binding.S_IXOTH || 0,
- UV_DIRENT_UNKNOWN: binding.UV_DIRENT_UNKNOWN,
- UV_DIRENT_FILE: binding.UV_DIRENT_FILE,
- UV_DIRENT_DIR: binding.UV_DIRENT_DIR,
- UV_DIRENT_LINK: binding.UV_DIRENT_LINK,
- UV_DIRENT_FIFO: binding.UV_DIRENT_FIFO,
- UV_DIRENT_SOCKET: binding.UV_DIRENT_SOCKET,
- UV_DIRENT_CHAR: binding.UV_DIRENT_CHAR,
- UV_DIRENT_BLOCK: binding.UV_DIRENT_BLOCK,
- UV_FS_SYMLINK_DIR: binding.UV_FS_SYMLINK_DIR,
- UV_FS_SYMLINK_JUNCTION: binding.UV_FS_SYMLINK_JUNCTION
- }
- const reqs = []
- let used = 0
- const fs = {
- handle: Buffer.allocUnsafe(binding.sizeofFS)
- }
- binding.init(fs.handle, fs, onresponse)
- Bare.on('exit', () => binding.destroy(fs.handle))
- // Lightly-modified from the Node FS internal utils.
- function flagsToNumber (flags) {
- switch (flags) {
- case 'r' : return constants.O_RDONLY
- case 'rs' : // Fall through.
- case 'sr' : return constants.O_RDONLY | constants.O_SYNC
- case 'r+' : return constants.O_RDWR
- case 'rs+' : // Fall through.
- case 'sr+' : return constants.O_RDWR | constants.O_SYNC
- case 'w' : return constants.O_TRUNC | constants.O_CREAT | constants.O_WRONLY
- case 'wx' : // Fall through.
- case 'xw' : return constants.O_TRUNC | constants.O_CREAT | constants.O_WRONLY | constants.O_EXCL
- case 'w+' : return constants.O_TRUNC | constants.O_CREAT | constants.O_RDWR
- case 'wx+': // Fall through.
- case 'xw+': return constants.O_TRUNC | constants.O_CREAT | constants.O_RDWR | constants.O_EXCL
- case 'a' : return constants.O_APPEND | constants.O_CREAT | constants.O_WRONLY
- case 'ax' : // Fall through.
- case 'xa' : return constants.O_APPEND | constants.O_CREAT | constants.O_WRONLY | constants.O_EXCL
- case 'as' : // Fall through.
- case 'sa' : return constants.O_APPEND | constants.O_CREAT | constants.O_WRONLY | constants.O_SYNC
- case 'a+' : return constants.O_APPEND | constants.O_CREAT | constants.O_RDWR
- case 'ax+': // Fall through.
- case 'xa+': return constants.O_APPEND | constants.O_CREAT | constants.O_RDWR | constants.O_EXCL
- case 'as+': // Fall through.
- case 'sa+': return constants.O_APPEND | constants.O_CREAT | constants.O_RDWR | constants.O_SYNC
- }
- throw typeError('ERR_INVALID_ARG_VALUE', `Invalid value in flags: ${flags}`)
- }
- function modeToNumber (mode) {
- mode = parseInt(mode, 8)
- if (isNaN(mode)) throw typeError('ERR_INVALID_ARG_VALUE', 'Mode must be a number or octal string')
- return mode
- }
- function alloc () {
- const handle = Buffer.alloc(binding.sizeofFSReq)
- binding.initReq(fs.handle, handle)
- const view = new Uint32Array(handle.buffer, handle.byteOffset + binding.offsetofFSReqID, 1)
- view[0] = reqs.length
- const req = {
- handle,
- view,
- type: 0,
- callback: null
- }
- used++
- reqs.push(req)
- return req
- }
- function getReq () {
- return used === reqs.length ? alloc() : reqs[used++]
- }
- function onresponse (id, err, result) {
- const req = reqs[id]
- used--
- if (used !== id) {
- const u = reqs[used]
- reqs[u.view[0] = id] = u
- reqs[req.view[0] = used] = req
- }
- const callback = req.callback
- req.callback = null
- callback(err, result)
- }
- function open (filepath, flags, mode, cb) {
- if (typeof filepath !== 'string') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
- }
- if (typeof cb !== 'function') {
- if (typeof flags === 'function') {
- cb = flags
- flags = 'r'
- mode = 0o666
- } else if (typeof mode === 'function') {
- cb = mode
- mode = 0o666
- } else {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
- }
- }
- if (typeof flags === 'string') flags = flagsToNumber(flags)
- if (typeof mode === 'string') mode = modeToNumber(mode)
- const req = getReq()
- req.callback = cb
- binding.open(req.handle, filepath, flags, mode)
- }
- function openSync (filepath, flags = 'r', mode = 0o666) {
- if (typeof filepath !== 'string') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
- }
- if (typeof flags === 'string') flags = flagsToNumber(flags)
- if (typeof mode === 'string') mode = modeToNumber(mode)
- return binding.openSync(filepath, flags, mode)
- }
- function close (fd, cb = noop) {
- if (typeof fd !== 'number') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
- }
- if (fd < 0 || fd > 0x7fffffff) {
- throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
- }
- if (typeof cb !== 'function') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
- }
- const req = getReq()
- req.callback = cb
- binding.close(req.handle, fd)
- }
- function closeSync (fd) {
- if (typeof fd !== 'number') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
- }
- if (fd < 0 || fd > 0x7fffffff) {
- throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
- }
- return binding.closeSync(fd)
- }
- function access (filepath, mode, cb) {
- if (typeof filepath !== 'string') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
- }
- if (typeof cb !== 'function') {
- if (typeof mode === 'function') {
- cb = mode
- mode = constants.F_OK
- } else {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
- }
- }
- const req = getReq()
- req.callback = cb
- binding.access(req.handle, filepath, mode)
- }
- function accessSync (filepath, mode = constants.F_OK) {
- if (typeof filepath !== 'string') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
- }
- binding.accessSync(filepath, mode)
- }
- function exists (filepath, cb) {
- if (typeof filepath !== 'string') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
- }
- if (typeof cb !== 'function') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
- }
- return access(filepath, (err) => cb(!!err)) // eslint-disable-line n/no-callback-literal
- }
- function existsSync (filepath) {
- if (typeof filepath !== 'string') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
- }
- try {
- accessSync(filepath)
- return true
- } catch {
- return false
- }
- }
- function read (fd, buffer, offset, len, pos, cb) {
- if (typeof fd !== 'number') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
- }
- if (fd < 0 || fd > 0x7fffffff) {
- throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
- }
- if (!Buffer.isBuffer(buffer) && !ArrayBuffer.isView(buffer)) {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Buffer must be a buffer. Received type ' + (typeof buffer) + ' (' + buffer + ')')
- }
- if (typeof cb !== 'function') {
- if (typeof offset === 'function') {
- cb = offset
- offset = 0
- len = buffer.byteLength
- pos = -1
- } else if (typeof len === 'function') {
- cb = len
- len = buffer.byteLength - offset
- pos = -1
- } else if (typeof pos === 'function') {
- cb = pos
- pos = -1
- } else {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
- }
- }
- if (typeof pos !== 'number') pos = -1
- const req = getReq()
- req.callback = cb
- binding.read(req.handle, fd, buffer, offset, len, pos)
- }
- function readSync (fd, buffer, offset = 0, len = buffer.byteLength - offset, pos = -1) {
- if (typeof fd !== 'number') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
- }
- if (fd < 0 || fd > 0x7fffffff) {
- throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
- }
- if (!Buffer.isBuffer(buffer) && !ArrayBuffer.isView(buffer)) {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Buffer must be a buffer. Received type ' + (typeof buffer) + ' (' + buffer + ')')
- }
- return binding.readSync(fd, buffer, offset, len, pos)
- }
- function readv (fd, buffers, pos, cb) {
- if (typeof fd !== 'number') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
- }
- if (fd < 0 || fd > 0x7fffffff) {
- throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
- }
- if (typeof pos === 'function') {
- cb = pos
- pos = -1
- } else if (typeof cb !== 'function') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
- }
- if (typeof pos !== 'number') pos = -1
- const req = getReq()
- req.callback = cb
- binding.readv(req.handle, fd, buffers, pos)
- }
- function write (fd, buffer, offset, len, pos, cb) {
- if (typeof fd !== 'number') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
- }
- if (fd < 0 || fd > 0x7fffffff) {
- throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
- }
- if (!Buffer.isBuffer(buffer) && !ArrayBuffer.isView(buffer)) {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Buffer must be a buffer. Received type ' + (typeof buffer) + ' (' + buffer + ')')
- }
- if (typeof cb !== 'function') {
- if (typeof offset === 'function') {
- cb = offset
- offset = 0
- len = buffer.byteLength
- pos = -1
- } else if (typeof len === 'function') {
- cb = len
- len = buffer.byteLength - offset
- pos = -1
- } else if (typeof pos === 'function') {
- cb = pos
- pos = -1
- } else {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
- }
- }
- if (typeof pos !== 'number') pos = -1
- const req = getReq()
- req.callback = cb
- binding.write(req.handle, fd, buffer, offset, len, pos)
- }
- function writeSync (fd, buffer, offset = 0, len = buffer.byteLength - offset, pos = -1) {
- if (typeof fd !== 'number') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
- }
- if (fd < 0 || fd > 0x7fffffff) {
- throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
- }
- if (!Buffer.isBuffer(buffer) && !ArrayBuffer.isView(buffer)) {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Buffer must be a buffer. Received type ' + (typeof buffer) + ' (' + buffer + ')')
- }
- return binding.writeSync(fd, buffer, offset, len, pos)
- }
- function writev (fd, buffers, pos, cb) {
- if (typeof fd !== 'number') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
- }
- if (fd < 0 || fd > 0x7fffffff) {
- throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
- }
- if (typeof pos === 'function') {
- cb = pos
- pos = -1
- } else if (typeof cb !== 'function') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
- }
- if (typeof pos !== 'number') pos = -1
- const req = getReq()
- req.callback = cb
- binding.writev(req.handle, fd, buffers, pos)
- }
- function stat (filepath, cb) {
- if (typeof filepath !== 'string') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
- }
- if (typeof cb !== 'function') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
- }
- const data = new Array(Stats.length)
- const req = getReq()
- req.callback = function (err, _) {
- if (err) cb(err, null)
- else cb(null, new Stats(...data))
- }
- binding.stat(req.handle, filepath, data)
- }
- function statSync (filepath) {
- if (typeof filepath !== 'string') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
- }
- return new Stats(...binding.statSync(filepath))
- }
- function lstat (filepath, cb) {
- if (typeof filepath !== 'string') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
- }
- if (typeof cb !== 'function') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
- }
- const data = new Array(Stats.length)
- const req = getReq()
- req.callback = function (err, _) {
- if (err) cb(err, null)
- else cb(null, new Stats(...data))
- }
- binding.lstat(req.handle, filepath, data)
- }
- function lstatSync (filepath) {
- if (typeof filepath !== 'string') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
- }
- return new Stats(...binding.lstatSync(filepath))
- }
- function fstat (fd, cb) {
- if (typeof fd !== 'number') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
- }
- if (fd < 0 || fd > 0x7fffffff) {
- throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
- }
- if (typeof cb !== 'function') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
- }
- const data = new Array(Stats.length)
- const req = getReq()
- req.callback = function (err, _) {
- if (err) cb(err, null)
- else cb(null, new Stats(...data))
- }
- binding.fstat(req.handle, fd, data)
- }
- function fstatSync (fd) {
- if (typeof fd !== 'number') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
- }
- if (fd < 0 || fd > 0x7fffffff) {
- throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
- }
- return new Stats(...binding.fstatSync(fd))
- }
- function ftruncate (fd, len, cb) {
- if (typeof fd !== 'number') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
- }
- if (fd < 0 || fd > 0x7fffffff) {
- throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
- }
- if (typeof len === 'function') {
- cb = len
- len = 0
- } else if (typeof cb !== 'function') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
- }
- const req = getReq()
- req.callback = cb
- binding.ftruncate(req.handle, fd, len)
- }
- function chmod (filepath, mode, cb) {
- if (typeof filepath !== 'string') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
- }
- if (typeof mode === 'string') mode = modeToNumber(mode)
- const req = getReq()
- req.callback = cb
- binding.chmod(req.handle, filepath, mode)
- }
- function chmodSync (filepath, mode) {
- if (typeof filepath !== 'string') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
- }
- if (typeof mode === 'string') mode = modeToNumber(mode)
- binding.chmodSync(filepath, mode)
- }
- function fchmod (fd, mode, cb) {
- if (typeof fd !== 'number') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
- }
- if (fd < 0 || fd > 0x7fffffff) {
- throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
- }
- if (typeof mode === 'string') mode = modeToNumber(mode)
- const req = getReq()
- req.callback = cb
- binding.fchmod(req.handle, fd, mode)
- }
- function fchmodSync (fd, mode) {
- if (typeof fd !== 'number') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'File descriptor must be a number. Received type ' + (typeof fd) + ' (' + fd + ')')
- }
- if (fd < 0 || fd > 0x7fffffff) {
- throw typeError('ERR_OUT_OF_RANGE', 'File descriptor is out of range. It must be >= 0 && <= 2147483647. Received ' + fd)
- }
- if (typeof mode === 'string') mode = modeToNumber(mode)
- binding.fchmodSync(fd, mode)
- }
- function mkdirRecursive (filepath, mode, cb) {
- mkdir(filepath, { mode }, function (err) {
- if (err === null) return cb(null, 0, null)
- if (err.code !== 'ENOENT') {
- stat(filepath, function (e, st) {
- if (e) return cb(e, e.errno, null)
- if (st.isDirectory()) return cb(null, 0, null)
- cb(err, err.errno, null)
- })
- return
- }
- while (filepath.endsWith(path.sep)) filepath = filepath.slice(0, -1)
- const i = filepath.lastIndexOf(path.sep)
- if (i <= 0) return cb(err, err.errno, null)
- mkdirRecursive(filepath.slice(0, i), mode, function (err) {
- if (err) return cb(err, err.errno, null)
- mkdir(filepath, { mode }, function (err) {
- if (err === null) return cb(null, 0, null)
- stat(filepath, function (e, st) {
- if (e) return cb(e, e.errno, null)
- if (st.isDirectory()) return cb(null, 0, null)
- cb(err, err.errno, null)
- })
- })
- })
- })
- }
- function mkdir (filepath, opts, cb) {
- if (typeof filepath !== 'string') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
- }
- if (typeof opts === 'function') {
- cb = opts
- opts = { mode: 0o777 }
- } else if (typeof cb !== 'function') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
- }
- if (typeof opts === 'number') opts = { mode: opts }
- else if (!opts) opts = {}
- const mode = typeof opts.mode === 'number' ? opts.mode : 0o777
- if (opts.recursive) return mkdirRecursive(filepath.replace(/\//g, path.sep), mode, cb)
- const req = getReq()
- req.callback = cb
- binding.mkdir(req.handle, filepath, mode)
- }
- function mkdirRecursiveSync (filepath, mode) {
- try {
- mkdirSync(filepath, { mode })
- } catch (err) {
- if (err.code !== 'ENOENT' && statSync(filepath).isDirectory()) {
- return
- }
- while (filepath.endsWith(path.sep)) filepath = filepath.slice(0, -1)
- const i = filepath.lastIndexOf(path.sep)
- if (i <= 0) throw err
- mkdirRecursiveSync(filepath.slice(0, i), { mode })
- try {
- mkdirSync(filepath, { mode })
- } catch (err) {
- if (statSync(filepath).isDirectory()) {
- return
- }
- throw err
- }
- }
- }
- function mkdirSync (filepath, opts) {
- if (typeof filepath !== 'string') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
- }
- if (typeof opts === 'number') opts = { mode: opts }
- else if (!opts) opts = {}
- const mode = typeof opts.mode === 'number' ? opts.mode : 0o777
- if (opts.recursive) return mkdirRecursiveSync(filepath.replace(/\//g, path.sep), mode)
- binding.mkdirSync(filepath, mode)
- }
- function rmdir (filepath, cb) {
- if (typeof filepath !== 'string') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
- }
- if (typeof cb !== 'function') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
- }
- const req = getReq()
- req.callback = cb
- binding.rmdir(req.handle, filepath)
- }
- function rmdirSync (filepath) {
- if (typeof filepath !== 'string') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
- }
- binding.rmdirSync(filepath)
- }
- function rmRecursive (filepath, opts, cb) {
- rmdir(filepath, function (err) {
- if (err === null) return cb(null)
- if (err.code !== 'ENOTEMPTY') return cb(err)
- readdir(filepath, function (err, files) {
- if (err) return cb(err)
- if (files.length === 0) return rmdir(filepath, cb)
- let missing = files.length
- let done = false
- for (const file of files) {
- rm(filepath + path.sep + file, opts, function (err) {
- if (done) return
- if (err) {
- done = true
- return cb(err)
- }
- if (--missing === 0) rmdir(filepath, cb)
- })
- }
- })
- })
- }
- function rm (filepath, opts, cb) {
- if (typeof filepath !== 'string') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
- }
- if (typeof opts === 'function') {
- cb = opts
- opts = {}
- } else if (typeof cb !== 'function') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
- }
- if (!opts) opts = {}
- lstat(filepath, function (err, st) {
- if (err) {
- return cb(err.code === 'ENOENT' && opts.force ? null : err)
- }
- if (st.isDirectory()) {
- if (opts.recursive) return rmRecursive(filepath, opts, cb)
- const err = new Error('is a directory')
- err.code = 'EISDIR'
- return cb(err)
- }
- unlink(filepath, cb)
- })
- }
- function rmRecursiveSync (filepath, opts) {
- try {
- rmdirSync(filepath)
- } catch (err) {
- if (err.code !== 'ENOTEMPTY') throw err
- const files = readdirSync(filepath)
- for (const file of files) {
- rmSync(filepath + path.sep + file, opts)
- }
- rmdirSync(filepath)
- }
- }
- function rmSync (filepath, opts) {
- if (typeof filepath !== 'string') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
- }
- if (!opts) opts = {}
- try {
- const st = lstatSync(filepath)
- if (st.isDirectory()) {
- if (opts.recursive) return rmRecursiveSync(filepath, opts)
- const err = new Error('is a directory')
- err.code = 'EISDIR'
- throw err
- }
- unlinkSync(filepath)
- } catch (err) {
- if (err.code !== 'ENOENT' || !opts.force) throw err
- }
- }
- function unlink (filepath, cb) {
- if (typeof filepath !== 'string') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
- }
- if (typeof cb !== 'function') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
- }
- const req = getReq()
- req.callback = cb
- binding.unlink(req.handle, filepath)
- }
- function unlinkSync (filepath) {
- if (typeof filepath !== 'string') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
- }
- binding.unlinkSync(filepath)
- }
- function rename (src, dst, cb) {
- if (typeof src !== 'string') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof src) + ' (' + src + ')')
- }
- if (typeof dst !== 'string') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof dst) + ' (' + dst + ')')
- }
- if (typeof cb !== 'function') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
- }
- const req = getReq()
- req.callback = cb
- binding.rename(req.handle, src, dst)
- }
- function renameSync (src, dst) {
- if (typeof src !== 'string') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof src) + ' (' + src + ')')
- }
- if (typeof dst !== 'string') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof dst) + ' (' + dst + ')')
- }
- binding.renameSync(src, dst)
- }
- function realpath (filepath, opts, cb) {
- if (typeof filepath !== 'string') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
- }
- if (typeof opts === 'function') {
- cb = opts
- opts = {}
- } else if (typeof cb !== 'function') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
- }
- if (typeof opts === 'string') opts = { encoding: opts }
- else if (!opts) opts = {}
- const {
- encoding = 'utf8'
- } = opts
- const data = Buffer.allocUnsafe(binding.sizeofFSPath)
- const req = getReq()
- req.callback = function (err, _) {
- if (err) return cb(err, null)
- let path = data.subarray(0, data.indexOf(0))
- if (encoding !== 'buffer') path = path.toString(encoding)
- cb(null, path)
- }
- binding.realpath(req.handle, filepath, data)
- }
- function realpathSync (filepath, opts) {
- if (typeof filepath !== 'string') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
- }
- if (typeof opts === 'string') opts = { encoding: opts }
- else if (!opts) opts = {}
- const {
- encoding = 'utf8'
- } = opts
- const data = Buffer.allocUnsafe(binding.sizeofFSPath)
- binding.realpathSync(filepath, data)
- filepath = data.subarray(0, data.indexOf(0))
- if (encoding !== 'buffer') filepath = filepath.toString(encoding)
- return filepath
- }
- function readlink (filepath, opts, cb) {
- if (typeof filepath !== 'string') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
- }
- if (typeof opts === 'function') {
- cb = opts
- opts = {}
- } else if (typeof cb !== 'function') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
- }
- if (typeof opts === 'string') opts = { encoding: opts }
- else if (!opts) opts = {}
- const {
- encoding = 'utf8'
- } = opts
- const data = Buffer.allocUnsafe(binding.sizeofFSPath)
- const req = getReq()
- req.callback = function (err, _) {
- if (err) return cb(err, null)
- let path = data.subarray(0, data.indexOf(0))
- if (encoding !== 'buffer') path = path.toString(encoding)
- cb(null, path)
- }
- binding.readlink(req.handle, filepath, data)
- }
- function readlinkSync (filepath, opts) {
- if (typeof filepath !== 'string') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
- }
- if (typeof opts === 'string') opts = { encoding: opts }
- else if (!opts) opts = {}
- const {
- encoding = 'utf8'
- } = opts
- const data = Buffer.allocUnsafe(binding.sizeofFSPath)
- binding.readlinkSync(filepath, data)
- filepath = data.subarray(0, data.indexOf(0))
- if (encoding !== 'buffer') filepath = filepath.toString(encoding)
- return filepath
- }
- function normalizeSymlinkTarget (target, type, filepath) {
- if (isWindows) {
- if (type === 'junction') target = path.resolve(filepath, '..', target)
- if (path.isAbsolute(target)) return path.toNamespacedPath(target)
- return target.replace(/\//g, path.sep)
- }
- return target
- }
- function symlink (target, filepath, type, cb) {
- if (typeof target !== 'string') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Target must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
- }
- if (typeof filepath !== 'string') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
- }
- if (typeof type === 'function') {
- cb = type
- type = null
- } else if (typeof cb !== 'function') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
- }
- if (typeof type === 'string') {
- switch (type) {
- case 'file':
- type = 0
- break
- case 'dir':
- type = constants.UV_FS_SYMLINK_DIR
- break
- case 'junction':
- type = constants.UV_FS_SYMLINK_JUNCTION
- break
- default:
- throw typeError('ERR_FS_INVALID_SYMLINK_TYPE', 'Symlink type must be one of "dir", "file", or "junction". Received "' + type + '"')
- }
- } else if (typeof type !== 'number') {
- if (isWindows) {
- target = path.resolve(filepath, '..', target)
- stat(target, (err, st) => {
- type = err === null && st.isDirectory() ? constants.UV_FS_SYMLINK_DIR : constants.UV_FS_SYMLINK_JUNCTION
- symlink(target, filepath, type, cb)
- })
- return
- }
- type = 0
- }
- const req = getReq()
- req.callback = cb
- binding.symlink(req.handle, normalizeSymlinkTarget(target), path.toNamespacedPath(filepath), type)
- }
- function symlinkSync (target, filepath, type) {
- if (typeof target !== 'string') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Target must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
- }
- if (typeof filepath !== 'string') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
- }
- if (typeof type === 'string') {
- switch (type) {
- case 'file':
- type = 0
- break
- case 'dir':
- type = constants.UV_FS_SYMLINK_DIR
- break
- case 'junction':
- type = constants.UV_FS_SYMLINK_JUNCTION
- break
- default:
- throw typeError('ERR_FS_INVALID_SYMLINK_TYPE', 'Symlink type must be one of "dir", "file", or "junction". Received "' + type + '"')
- }
- } else if (typeof type !== 'number') {
- if (isWindows) {
- target = path.resolve(filepath, '..', target)
- type = statSync(target).isDirectory() ? constants.UV_FS_SYMLINK_DIR : constants.UV_FS_SYMLINK_JUNCTION
- } else {
- type = 0
- }
- }
- binding.symlinkSync(normalizeSymlinkTarget(target), path.toNamespacedPath(filepath), type)
- }
- function opendir (filepath, opts, cb) {
- if (typeof filepath !== 'string') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
- }
- if (typeof opts === 'function') {
- cb = opts
- opts = {}
- } else if (typeof cb !== 'function') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
- }
- if (typeof opts === 'string') opts = { encoding: opts }
- else if (!opts) opts = {}
- const data = Buffer.allocUnsafe(binding.sizeofFSDir)
- const req = getReq()
- req.callback = function (err, _) {
- if (err) return cb(err, null)
- cb(null, new Dir(filepath, data, opts))
- }
- binding.opendir(req.handle, filepath, data)
- }
- function opendirSync (filepath, opts) {
- if (typeof filepath !== 'string') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
- }
- if (typeof opts === 'string') opts = { encoding: opts }
- else if (!opts) opts = {}
- const data = Buffer.allocUnsafe(binding.sizeofFSDir)
- binding.opendirSync(filepath, data)
- return new Dir(filepath, data, opts)
- }
- function readdir (filepath, opts, cb) {
- if (typeof filepath !== 'string') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
- }
- if (typeof opts === 'function') {
- cb = opts
- opts = {}
- } else if (typeof cb !== 'function') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
- }
- if (typeof opts === 'string') opts = { encoding: opts }
- else if (!opts) opts = {}
- const {
- withFileTypes = false
- } = opts
- opendir(filepath, opts, async (err, dir) => {
- if (err) return cb(err, null)
- const result = []
- for await (const entry of dir) {
- result.push(withFileTypes ? entry : entry.name)
- }
- cb(null, result)
- })
- }
- function readdirSync (filepath, opts) {
- if (typeof filepath !== 'string') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
- }
- if (typeof opts === 'string') opts = { encoding: opts }
- else if (!opts) opts = {}
- const {
- withFileTypes = false
- } = opts
- const dir = opendirSync(filepath, opts)
- const result = []
- while (true) {
- const entry = dir.readSync()
- if (entry === null) break
- result.push(withFileTypes ? entry : entry.name)
- }
- return result
- }
- function readFile (filepath, opts, cb) {
- if (typeof filepath !== 'string') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
- }
- if (typeof opts === 'function') {
- cb = opts
- opts = {}
- } else if (typeof cb !== 'function') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
- }
- if (typeof opts === 'string') opts = { encoding: opts }
- else if (!opts) opts = {}
- const {
- encoding = 'buffer'
- } = opts
- open(filepath, opts.flag || 'r', function (err, fd) {
- if (err) return cb(err)
- fstat(fd, function (err, st) {
- if (err) return closeAndError(err)
- let buffer = Buffer.allocUnsafe(st.size)
- let len = 0
- read(fd, buffer, loop)
- function loop (err, r) {
- if (err) return closeAndError(err)
- len += r
- if (r === 0 || len === buffer.byteLength) return done()
- read(fd, buffer.subarray(len), loop)
- }
- function done () {
- if (len !== buffer.byteLength) buffer = buffer.subarray(0, len)
- close(fd, function (err) {
- if (err) return cb(err)
- if (encoding !== 'buffer') buffer = buffer.toString(encoding)
- cb(null, buffer)
- })
- }
- })
- function closeAndError (err) {
- close(fd, function () {
- cb(err)
- })
- }
- })
- }
- function readFileSync (filepath, opts) {
- if (typeof filepath !== 'string') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
- }
- if (typeof opts === 'string') opts = { encoding: opts }
- else if (!opts) opts = {}
- const {
- encoding = 'buffer'
- } = opts
- const fd = openSync(filepath, opts.flag || 'r')
- try {
- const st = fstatSync(fd)
- let buffer = Buffer.allocUnsafe(st.size)
- let len = 0
- while (true) {
- const r = readSync(fd, len ? buffer.subarray(len) : buffer)
- len += r
- if (r === 0 || len === buffer.byteLength) break
- }
- if (len !== buffer.byteLength) buffer = buffer.subarray(0, len)
- if (encoding !== 'buffer') buffer = buffer.toString(encoding)
- return buffer
- } finally {
- try {
- closeSync(fd)
- } catch {}
- }
- }
- function writeFile (filepath, data, opts, cb) {
- if (typeof filepath !== 'string') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
- }
- if (typeof data !== 'string' && !Buffer.isBuffer(data) && !ArrayBuffer.isView(data)) {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Data must be a string or buffer. Received type ' + (typeof data) + ' (' + data + ')')
- }
- if (typeof opts === 'function') {
- cb = opts
- opts = {}
- } else if (typeof cb !== 'function') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Callback must be a function. Received type ' + (typeof cb) + ' (' + cb + ')')
- }
- if (typeof opts === 'string') opts = { encoding: opts }
- else if (!opts) opts = {}
- if (typeof data === 'string') data = Buffer.from(data, opts.encoding)
- open(filepath, opts.flag || 'w', opts.mode || 0o666, function (err, fd) {
- if (err) return cb(err)
- write(fd, data, loop)
- function loop (err, w) {
- if (err) return closeAndError(err)
- if (w === data.byteLength) return done()
- write(fd, data.subarray(w), loop)
- }
- function done () {
- close(fd, function (err) {
- if (err) return cb(err)
- return cb(null)
- })
- }
- function closeAndError (err) {
- close(fd, function () {
- cb(err)
- })
- }
- })
- }
- function writeFileSync (filepath, data, opts) {
- if (typeof filepath !== 'string') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
- }
- if (typeof data !== 'string' && !Buffer.isBuffer(data) && !ArrayBuffer.isView(data)) {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Data must be a string or buffer. Received type ' + (typeof data) + ' (' + data + ')')
- }
- if (typeof opts === 'string') opts = { encoding: opts }
- else if (!opts) opts = {}
- if (typeof data === 'string') data = Buffer.from(data, opts.encoding)
- const fd = openSync(filepath, opts.flag || 'w', opts.mode)
- try {
- let len = 0
- while (true) {
- len += writeSync(fd, len ? data.subarray(len) : data)
- if (len === data.byteLength) break
- }
- } finally {
- try {
- closeSync(fd)
- } catch {}
- }
- }
- function watch (filepath, opts, cb) {
- if (typeof filepath !== 'string') {
- throw typeError('ERR_INVALID_ARG_TYPE', 'Path must be a string. Received type ' + (typeof filepath) + ' (' + filepath + ')')
- }
- if (typeof opts === 'function') {
- cb = opts
- opts = {}
- }
- if (typeof opts === 'string') opts = { encoding: opts }
- else if (!opts) opts = {}
- const watcher = new Watcher(filepath, opts)
- if (cb) watcher.on('change', cb)
- return watcher
- }
- class Stats {
- constructor (dev, mode, nlink, uid, gid, rdev, blksize, ino, size, blocks, atimeMs, mtimeMs, ctimeMs, birthtimeMs) {
- this.dev = dev
- this.mode = mode
- this.nlink = nlink
- this.uid = uid
- this.gid = gid
- this.rdev = rdev
- this.blksize = blksize
- this.ino = ino
- this.size = size
- this.blocks = blocks
- this.atimeMs = atimeMs
- this.mtimeMs = mtimeMs
- this.ctimeMs = ctimeMs
- this.birthtimeMs = birthtimeMs
- this.atime = new Date(atimeMs)
- this.mtime = new Date(mtimeMs)
- this.ctime = new Date(ctimeMs)
- this.birthtime = new Date(birthtimeMs)
- }
- isDirectory () {
- return (this.mode & constants.S_IFMT) === constants.S_IFDIR
- }
- isFile () {
- return (this.mode & constants.S_IFMT) === constants.S_IFREG
- }
- isBlockDevice () {
- return (this.mode & constants.S_IFMT) === constants.S_IFBLK
- }
- isCharacterDevice () {
- return (this.mode & constants.S_IFCHR) === constants.S_IFCHR
- }
- isFIFO () {
- return (this.mode & constants.S_IFMT) === constants.S_IFIFO
- }
- isSymbolicLink () {
- return (this.mode & constants.S_IFMT) === constants.S_IFLNK
- }
- isSocket () {
- return (this.mode & constants.S_IFMT) === constants.S_IFSOCK
- }
- }
- class Dir {
- constructor (path, handle, opts = {}) {
- const {
- encoding = 'utf8',
- bufferSize = 32
- } = opts
- this._handle = handle
- this._dirents = Buffer.allocUnsafe(binding.sizeofFSDirent * bufferSize)
- this._encoding = encoding
- this._buffer = []
- this._ended = false
- this.path = path
- }
- read (cb) {
- if (!cb) return promisify(this.read.bind(this))
- if (this._buffer.length) return queueMicrotask(() => cb(null, this._buffer.shift()))
- if (this._ended) return queueMicrotask(() => cb(null, null))
- const data = []
- const req = getReq()
- req.callback = (err, _) => {
- if (err) return cb(err, null)
- if (data.length === 0) this._ended = true
- else {
- for (const entry of data) {
- let name = Buffer.from(entry.name)
- if (this._encoding !== 'buffer') name = name.toString(this._encoding)
- this._buffer.push(new Dirent(this.path, name, entry.type))
- }
- }
- if (this._ended) return cb(null, null)
- cb(null, this._buffer.shift())
- }
- binding.readdir(req.handle, this._handle, this._dirents, data)
- }
- readSync () {
- if (this._buffer.length) return this._buffer.shift()
- if (this._ended) return null
- const data = []
- binding.readdirSync(this._handle, this._dirents, data)
- if (data.length === 0) this._ended = true
- else {
- for (const entry of data) {
- let name = Buffer.from(entry.name)
- if (this._encoding !== 'buffer') name = name.toString(this._encoding)
- this._buffer.push(new Dirent(this.path, name, entry.type))
- }
- }
- if (this._ended) return null
- return this._buffer.shift()
- }
- close (cb) {
- if (!cb) return promisify(this.close.bind(this))
- const req = getReq()
- req.callback = (err, _) => {
- this._handle = null
- cb(err)
- }
- binding.closedir(req.handle, this._handle)
- }
- closeSync () {
- binding.closedirSync(this._handle)
- this._handle = null
- }
- [Symbol.iterator] () {
- return {
- next: () => {
- if (this._buffer.length) {
- return { done: false, value: this._buffer.shift() }
- }
- if (this._ended) {
- return { done: true }
- }
- const entry = this.readSync()
- if (entry) {
- return { done: false, value: entry }
- }
- this.closeSync()
- return { done: true }
- }
- }
- }
- [Symbol.asyncIterator] () {
- return {
- next: () => new Promise((resolve, reject) => {
- if (this._buffer.length) {
- return resolve({ done: false, value: this._buffer.shift() })
- }
- if (this._ended) {
- return resolve({ done: true })
- }
- this.read((err, entry) => {
- if (err) return reject(err)
- if (entry) {
- return resolve({ done: false, value: entry })
- }
- this.close((err) => err ? reject(err) : resolve({ done: true }))
- })
- })
- }
- }
- }
- class Dirent {
- constructor (path, name, type) {
- this.type = type
- this.path = path
- this.name = name
- }
- isFile () {
- return this.type === constants.UV_DIRENT_FILE
- }
- isDirectory () {
- return this.type === constants.UV_DIRENT_DIR
- }
- isSymbolicLink () {
- return this.type === constants.UV_DIRENT_LINK
- }
- isFIFO () {
- return this.type === constants.UV_DIRENT_FIFO
- }
- isSocket () {
- return this.type === constants.UV_DIRENT_SOCKET
- }
- isCharacterDevice () {
- return this.type === constants.UV_DIRENT_CHAR
- }
- isBlockDevice () {
- return this.type === constants.UV_DIRENT_BLOCK
- }
- }
- class FileWriteStream extends Writable {
- constructor (path, opts = {}) {
- super({ map })
- this.path = path
- this.fd = 0
- this.flags = opts.flags || 'w'
- this.mode = opts.mode || 0o666
- }
- _open (cb) {
- open(this.path, this.flags, this.mode, (err, fd) => {
- if (err) return cb(err)
- this.fd = fd
- cb(null)
- })
- }
- _writev (datas, cb) {
- writev(this.fd, datas, cb)
- }
- _destroy (cb) {
- if (!this.fd) return cb(null)
- close(this.fd, () => cb(null))
- }
- }
- class FileReadStream extends Readable {
- constructor (path, opts = {}) {
- super()
- this.path = path
- this.fd = 0
- this._offset = opts.start || 0
- this._missing = 0
- if (opts.length) this._missing = opts.length
- else if (typeof opts.end === 'number') this._missing = opts.end - this._offset + 1
- else this._missing = -1
- }
- _open (cb) {
- open(this.path, constants.O_RDONLY, (err, fd) => {
- if (err) return cb(err)
- const onerror = (err) => close(fd, () => cb(err))
- fstat(fd, (err, st) => {
- if (err) return onerror(err)
- if (!st.isFile()) return onerror(new Error(this.path + ' is not a file'))
- this.fd = fd
- if (this._missing === -1) this._missing = st.size
- if (st.size < this._offset) {
- this._offset = st.size
- this._missing = 0
- return cb(null)
- }
- if (st.size < this._offset + this._missing) {
- this._missing = st.size - this._offset
- return cb(null)
- }
- cb(null)
- })
- })
- }
- _read (cb) {
- if (!this._missing) {
- this.push(null)
- return cb(null)
- }
- const data = Buffer.allocUnsafe(Math.min(this._missing, 65536))
- read(this.fd, data, 0, data.byteLength, this._offset, (err, read) => {
- if (err) return cb(err)
- if (!read) {
- this.push(null)
- return cb(null)
- }
- if (this._missing < read) read = this._missing
- this.push(data.subarray(0, read))
- this._missing -= read
- this._offset += read
- if (!this._missing) this.push(null)
- cb(null)
- })
- }
- _destroy (cb) {
- if (!this.fd) return cb(null)
- close(this.fd, () => cb(null))
- }
- }
- class Watcher extends EventEmitter {
- constructor (path, opts) {
- const {
- persistent = true,
- recursive = false,
- encoding = 'utf8'
- } = opts
- super()
- this._closed = false
- this._encoding = encoding
- this._handle = binding.watcherInit(path, recursive, this, this._onevent, this._onclose)
- if (!persistent) this.unref()
- }
- _onevent (err, events, filename) {
- if (err) {
- this.close()
- this.emit('error', err)
- } else {
- const path = this._encoding === 'buffer'
- ? Buffer.from(filename)
- : Buffer.from(filename).toString(this._encoding)
- if (events & binding.UV_RENAME) {
- this.emit('change', 'rename', path)
- }
- if (events & binding.UV_CHANGE) {
- this.emit('change', 'change', path)
- }
- }
- }
- _onclose () {
- this._handle = null
- this.emit('close')
- }
- close () {
- if (this._closed) return
- this._closed = true
- binding.watcherClose(this._handle)
- }
- ref () {
- if (this._handle) binding.watcherRef(this._handle)
- return this
- }
- unref () {
- if (this._handle) binding.watcherUnref(this._handle)
- return this
- }
- [Symbol.asyncIterator] () {
- const buffer = []
- let done = false
- let error = null
- let next = null
- this
- .on('change', (eventType, filename) => {
- if (next) {
- next.resolve({ done: false, value: { eventType, filename } })
- next = null
- } else {
- buffer.push({ eventType, filename })
- }
- })
- .on('error', (err) => {
- done = true
- error = err
- if (next) {
- next.reject(error)
- next = null
- }
- })
- .on('close', () => {
- done = true
- if (next) {
- next.resolve({ done })
- next = null
- }
- })
- return {
- next: () => new Promise((resolve, reject) => {
- if (error) return reject(error)
- if (buffer.length) return resolve({ done: false, value: buffer.shift() })
- if (done) return resolve({ done })
- next = { resolve, reject }
- })
- }
- }
- }
- exports.promises = {}
- function typeError (code, message) {
- const error = new TypeError(message)
- error.code = code
- return error
- }
- function noop () {}
- exports.access = access
- exports.chmod = chmod
- exports.close = close
- exports.exists = exists
- exports.fchmod = fchmod
- exports.fstat = fstat
- exports.ftruncate = ftruncate
- exports.lstat = lstat
- exports.mkdir = mkdir
- exports.open = open
- exports.opendir = opendir
- exports.read = read
- exports.readFile = readFile
- exports.readdir = readdir
- exports.readlink = readlink
- exports.readv = readv
- exports.realpath = realpath
- exports.rename = rename
- exports.rm = rm
- exports.rmdir = rmdir
- exports.stat = stat
- exports.symlink = symlink
- exports.unlink = unlink
- exports.watch = watch
- exports.write = write
- exports.writeFile = writeFile
- exports.writev = writev
- exports.accessSync = accessSync
- exports.chmodSync = chmodSync
- exports.closeSync = closeSync
- exports.existsSync = existsSync
- exports.fchmodSync = fchmodSync
- exports.fstatSync = fstatSync
- exports.lstatSync = lstatSync
- exports.mkdirSync = mkdirSync
- exports.openSync = openSync
- exports.opendirSync = opendirSync
- exports.readFileSync = readFileSync
- exports.readSync = readSync
- exports.readdirSync = readdirSync
- exports.readlinkSync = readlinkSync
- exports.realpathSync = realpathSync
- exports.renameSync = renameSync
- exports.rmSync = rmSync
- exports.rmdirSync = rmdirSync
- exports.statSync = statSync
- exports.symlinkSync = symlinkSync
- exports.unlinkSync = unlinkSync
- exports.writeFileSync = writeFileSync
- exports.writeSync = writeSync
- exports.promises.access = promisify(access)
- exports.promises.chmod = promisify(chmod)
- exports.promises.lstat = promisify(lstat)
- exports.promises.mkdir = promisify(mkdir)
- exports.promises.opendir = promisify(opendir)
- exports.promises.readFile = promisify(readFile)
- exports.promises.readdir = promisify(readdir)
- exports.promises.readlink = promisify(readlink)
- exports.promises.realpath = promisify(realpath)
- exports.promises.rename = promisify(rename)
- exports.promises.rm = promisify(rm)
- exports.promises.rmdir = promisify(rmdir)
- exports.promises.stat = promisify(stat)
- exports.promises.symlink = promisify(symlink)
- exports.promises.unlink = promisify(unlink)
- exports.promises.writeFile = promisify(writeFile)
- exports.promises.watch = watch // Already async iterable
- exports.Stats = Stats
- exports.Dir = Dir
- exports.Dirent = Dirent
- exports.Watcher = Watcher
- exports.ReadStream = FileReadStream
- exports.createReadStream = function createReadStream (path, opts) {
- return new FileReadStream(path, opts)
- }
- exports.WriteStream = FileWriteStream
- exports.createWriteStream = function createWriteStream (path, opts) {
- return new FileWriteStream(path, opts)
- }
- function promisify (fn) {
- return function (...args) {
- return new Promise((resolve, reject) => {
- fn(...args, function (err, res) {
- if (err) return reject(err)
- resolve(res)
- })
- })
- }
- }
- function map (data) {
- return typeof data === 'string' ? Buffer.from(data) : data
- }
|