b04a7b6aa808c0464118db22682de1dde79e8308fc6039a6103ca50568438617d09b8772ec4406269e2b039f8ef0d6ff09f3edeb931dfcd7b8ce02b469c8e8 186 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473
  1. /**
  2. * The `node:fs` module enables interacting with the file system in a
  3. * way modeled on standard POSIX functions.
  4. *
  5. * To use the promise-based APIs:
  6. *
  7. * ```js
  8. * import * as fs from 'node:fs/promises';
  9. * ```
  10. *
  11. * To use the callback and sync APIs:
  12. *
  13. * ```js
  14. * import * as fs from 'node:fs';
  15. * ```
  16. *
  17. * All file system operations have synchronous, callback, and promise-based
  18. * forms, and are accessible using both CommonJS syntax and ES6 Modules (ESM).
  19. * @see [source](https://github.com/nodejs/node/blob/v24.x/lib/fs.js)
  20. */
  21. declare module "fs" {
  22. import * as stream from "node:stream";
  23. import { Abortable, EventEmitter } from "node:events";
  24. import { URL } from "node:url";
  25. import * as promises from "node:fs/promises";
  26. export { promises };
  27. /**
  28. * Valid types for path values in "fs".
  29. */
  30. export type PathLike = string | Buffer | URL;
  31. export type PathOrFileDescriptor = PathLike | number;
  32. export type TimeLike = string | number | Date;
  33. export type NoParamCallback = (err: NodeJS.ErrnoException | null) => void;
  34. export type BufferEncodingOption =
  35. | "buffer"
  36. | {
  37. encoding: "buffer";
  38. };
  39. export interface ObjectEncodingOptions {
  40. encoding?: BufferEncoding | null | undefined;
  41. }
  42. export type EncodingOption = ObjectEncodingOptions | BufferEncoding | undefined | null;
  43. export type OpenMode = number | string;
  44. export type Mode = number | string;
  45. export interface StatsBase<T> {
  46. isFile(): boolean;
  47. isDirectory(): boolean;
  48. isBlockDevice(): boolean;
  49. isCharacterDevice(): boolean;
  50. isSymbolicLink(): boolean;
  51. isFIFO(): boolean;
  52. isSocket(): boolean;
  53. dev: T;
  54. ino: T;
  55. mode: T;
  56. nlink: T;
  57. uid: T;
  58. gid: T;
  59. rdev: T;
  60. size: T;
  61. blksize: T;
  62. blocks: T;
  63. atimeMs: T;
  64. mtimeMs: T;
  65. ctimeMs: T;
  66. birthtimeMs: T;
  67. atime: Date;
  68. mtime: Date;
  69. ctime: Date;
  70. birthtime: Date;
  71. }
  72. export interface Stats extends StatsBase<number> {}
  73. /**
  74. * A `fs.Stats` object provides information about a file.
  75. *
  76. * Objects returned from {@link stat}, {@link lstat}, {@link fstat}, and
  77. * their synchronous counterparts are of this type.
  78. * If `bigint` in the `options` passed to those methods is true, the numeric values
  79. * will be `bigint` instead of `number`, and the object will contain additional
  80. * nanosecond-precision properties suffixed with `Ns`. `Stat` objects are not to be created directly using the `new` keyword.
  81. *
  82. * ```console
  83. * Stats {
  84. * dev: 2114,
  85. * ino: 48064969,
  86. * mode: 33188,
  87. * nlink: 1,
  88. * uid: 85,
  89. * gid: 100,
  90. * rdev: 0,
  91. * size: 527,
  92. * blksize: 4096,
  93. * blocks: 8,
  94. * atimeMs: 1318289051000.1,
  95. * mtimeMs: 1318289051000.1,
  96. * ctimeMs: 1318289051000.1,
  97. * birthtimeMs: 1318289051000.1,
  98. * atime: Mon, 10 Oct 2011 23:24:11 GMT,
  99. * mtime: Mon, 10 Oct 2011 23:24:11 GMT,
  100. * ctime: Mon, 10 Oct 2011 23:24:11 GMT,
  101. * birthtime: Mon, 10 Oct 2011 23:24:11 GMT }
  102. * ```
  103. *
  104. * `bigint` version:
  105. *
  106. * ```console
  107. * BigIntStats {
  108. * dev: 2114n,
  109. * ino: 48064969n,
  110. * mode: 33188n,
  111. * nlink: 1n,
  112. * uid: 85n,
  113. * gid: 100n,
  114. * rdev: 0n,
  115. * size: 527n,
  116. * blksize: 4096n,
  117. * blocks: 8n,
  118. * atimeMs: 1318289051000n,
  119. * mtimeMs: 1318289051000n,
  120. * ctimeMs: 1318289051000n,
  121. * birthtimeMs: 1318289051000n,
  122. * atimeNs: 1318289051000000000n,
  123. * mtimeNs: 1318289051000000000n,
  124. * ctimeNs: 1318289051000000000n,
  125. * birthtimeNs: 1318289051000000000n,
  126. * atime: Mon, 10 Oct 2011 23:24:11 GMT,
  127. * mtime: Mon, 10 Oct 2011 23:24:11 GMT,
  128. * ctime: Mon, 10 Oct 2011 23:24:11 GMT,
  129. * birthtime: Mon, 10 Oct 2011 23:24:11 GMT }
  130. * ```
  131. * @since v0.1.21
  132. */
  133. export class Stats {
  134. private constructor();
  135. }
  136. export interface StatsFsBase<T> {
  137. /** Type of file system. */
  138. type: T;
  139. /** Optimal transfer block size. */
  140. bsize: T;
  141. /** Total data blocks in file system. */
  142. blocks: T;
  143. /** Free blocks in file system. */
  144. bfree: T;
  145. /** Available blocks for unprivileged users */
  146. bavail: T;
  147. /** Total file nodes in file system. */
  148. files: T;
  149. /** Free file nodes in file system. */
  150. ffree: T;
  151. }
  152. export interface StatsFs extends StatsFsBase<number> {}
  153. /**
  154. * Provides information about a mounted file system.
  155. *
  156. * Objects returned from {@link statfs} and its synchronous counterpart are of
  157. * this type. If `bigint` in the `options` passed to those methods is `true`, the
  158. * numeric values will be `bigint` instead of `number`.
  159. *
  160. * ```console
  161. * StatFs {
  162. * type: 1397114950,
  163. * bsize: 4096,
  164. * blocks: 121938943,
  165. * bfree: 61058895,
  166. * bavail: 61058895,
  167. * files: 999,
  168. * ffree: 1000000
  169. * }
  170. * ```
  171. *
  172. * `bigint` version:
  173. *
  174. * ```console
  175. * StatFs {
  176. * type: 1397114950n,
  177. * bsize: 4096n,
  178. * blocks: 121938943n,
  179. * bfree: 61058895n,
  180. * bavail: 61058895n,
  181. * files: 999n,
  182. * ffree: 1000000n
  183. * }
  184. * ```
  185. * @since v19.6.0, v18.15.0
  186. */
  187. export class StatsFs {}
  188. export interface BigIntStatsFs extends StatsFsBase<bigint> {}
  189. export interface StatFsOptions {
  190. bigint?: boolean | undefined;
  191. }
  192. /**
  193. * A representation of a directory entry, which can be a file or a subdirectory
  194. * within the directory, as returned by reading from an `fs.Dir`. The
  195. * directory entry is a combination of the file name and file type pairs.
  196. *
  197. * Additionally, when {@link readdir} or {@link readdirSync} is called with
  198. * the `withFileTypes` option set to `true`, the resulting array is filled with `fs.Dirent` objects, rather than strings or `Buffer` s.
  199. * @since v10.10.0
  200. */
  201. export class Dirent<Name extends string | Buffer = string> {
  202. /**
  203. * Returns `true` if the `fs.Dirent` object describes a regular file.
  204. * @since v10.10.0
  205. */
  206. isFile(): boolean;
  207. /**
  208. * Returns `true` if the `fs.Dirent` object describes a file system
  209. * directory.
  210. * @since v10.10.0
  211. */
  212. isDirectory(): boolean;
  213. /**
  214. * Returns `true` if the `fs.Dirent` object describes a block device.
  215. * @since v10.10.0
  216. */
  217. isBlockDevice(): boolean;
  218. /**
  219. * Returns `true` if the `fs.Dirent` object describes a character device.
  220. * @since v10.10.0
  221. */
  222. isCharacterDevice(): boolean;
  223. /**
  224. * Returns `true` if the `fs.Dirent` object describes a symbolic link.
  225. * @since v10.10.0
  226. */
  227. isSymbolicLink(): boolean;
  228. /**
  229. * Returns `true` if the `fs.Dirent` object describes a first-in-first-out
  230. * (FIFO) pipe.
  231. * @since v10.10.0
  232. */
  233. isFIFO(): boolean;
  234. /**
  235. * Returns `true` if the `fs.Dirent` object describes a socket.
  236. * @since v10.10.0
  237. */
  238. isSocket(): boolean;
  239. /**
  240. * The file name that this `fs.Dirent` object refers to. The type of this
  241. * value is determined by the `options.encoding` passed to {@link readdir} or {@link readdirSync}.
  242. * @since v10.10.0
  243. */
  244. name: Name;
  245. /**
  246. * The path to the parent directory of the file this `fs.Dirent` object refers to.
  247. * @since v20.12.0, v18.20.0
  248. */
  249. parentPath: string;
  250. }
  251. /**
  252. * A class representing a directory stream.
  253. *
  254. * Created by {@link opendir}, {@link opendirSync}, or `fsPromises.opendir()`.
  255. *
  256. * ```js
  257. * import { opendir } from 'node:fs/promises';
  258. *
  259. * try {
  260. * const dir = await opendir('./');
  261. * for await (const dirent of dir)
  262. * console.log(dirent.name);
  263. * } catch (err) {
  264. * console.error(err);
  265. * }
  266. * ```
  267. *
  268. * When using the async iterator, the `fs.Dir` object will be automatically
  269. * closed after the iterator exits.
  270. * @since v12.12.0
  271. */
  272. export class Dir implements AsyncIterable<Dirent> {
  273. /**
  274. * The read-only path of this directory as was provided to {@link opendir},{@link opendirSync}, or `fsPromises.opendir()`.
  275. * @since v12.12.0
  276. */
  277. readonly path: string;
  278. /**
  279. * Asynchronously iterates over the directory via `readdir(3)` until all entries have been read.
  280. */
  281. [Symbol.asyncIterator](): NodeJS.AsyncIterator<Dirent>;
  282. /**
  283. * Asynchronously close the directory's underlying resource handle.
  284. * Subsequent reads will result in errors.
  285. *
  286. * A promise is returned that will be fulfilled after the resource has been
  287. * closed.
  288. * @since v12.12.0
  289. */
  290. close(): Promise<void>;
  291. close(cb: NoParamCallback): void;
  292. /**
  293. * Synchronously close the directory's underlying resource handle.
  294. * Subsequent reads will result in errors.
  295. * @since v12.12.0
  296. */
  297. closeSync(): void;
  298. /**
  299. * Asynchronously read the next directory entry via [`readdir(3)`](http://man7.org/linux/man-pages/man3/readdir.3.html) as an `fs.Dirent`.
  300. *
  301. * A promise is returned that will be fulfilled with an `fs.Dirent`, or `null` if there are no more directory entries to read.
  302. *
  303. * Directory entries returned by this function are in no particular order as
  304. * provided by the operating system's underlying directory mechanisms.
  305. * Entries added or removed while iterating over the directory might not be
  306. * included in the iteration results.
  307. * @since v12.12.0
  308. * @return containing {fs.Dirent|null}
  309. */
  310. read(): Promise<Dirent | null>;
  311. read(cb: (err: NodeJS.ErrnoException | null, dirEnt: Dirent | null) => void): void;
  312. /**
  313. * Synchronously read the next directory entry as an `fs.Dirent`. See the
  314. * POSIX [`readdir(3)`](http://man7.org/linux/man-pages/man3/readdir.3.html) documentation for more detail.
  315. *
  316. * If there are no more directory entries to read, `null` will be returned.
  317. *
  318. * Directory entries returned by this function are in no particular order as
  319. * provided by the operating system's underlying directory mechanisms.
  320. * Entries added or removed while iterating over the directory might not be
  321. * included in the iteration results.
  322. * @since v12.12.0
  323. */
  324. readSync(): Dirent | null;
  325. /**
  326. * Calls `dir.close()` if the directory handle is open, and returns a promise that
  327. * fulfills when disposal is complete.
  328. * @since v24.1.0
  329. */
  330. [Symbol.asyncDispose](): Promise<void>;
  331. /**
  332. * Calls `dir.closeSync()` if the directory handle is open, and returns
  333. * `undefined`.
  334. * @since v24.1.0
  335. */
  336. [Symbol.dispose](): void;
  337. }
  338. /**
  339. * Class: fs.StatWatcher
  340. * @since v14.3.0, v12.20.0
  341. * Extends `EventEmitter`
  342. * A successful call to {@link watchFile} method will return a new fs.StatWatcher object.
  343. */
  344. export interface StatWatcher extends EventEmitter {
  345. /**
  346. * When called, requests that the Node.js event loop _not_ exit so long as the `fs.StatWatcher` is active. Calling `watcher.ref()` multiple times will have
  347. * no effect.
  348. *
  349. * By default, all `fs.StatWatcher` objects are "ref'ed", making it normally
  350. * unnecessary to call `watcher.ref()` unless `watcher.unref()` had been
  351. * called previously.
  352. * @since v14.3.0, v12.20.0
  353. */
  354. ref(): this;
  355. /**
  356. * When called, the active `fs.StatWatcher` object will not require the Node.js
  357. * event loop to remain active. If there is no other activity keeping the
  358. * event loop running, the process may exit before the `fs.StatWatcher` object's
  359. * callback is invoked. Calling `watcher.unref()` multiple times will have
  360. * no effect.
  361. * @since v14.3.0, v12.20.0
  362. */
  363. unref(): this;
  364. }
  365. export interface FSWatcher extends EventEmitter {
  366. /**
  367. * Stop watching for changes on the given `fs.FSWatcher`. Once stopped, the `fs.FSWatcher` object is no longer usable.
  368. * @since v0.5.8
  369. */
  370. close(): void;
  371. /**
  372. * When called, requests that the Node.js event loop _not_ exit so long as the `fs.FSWatcher` is active. Calling `watcher.ref()` multiple times will have
  373. * no effect.
  374. *
  375. * By default, all `fs.FSWatcher` objects are "ref'ed", making it normally
  376. * unnecessary to call `watcher.ref()` unless `watcher.unref()` had been
  377. * called previously.
  378. * @since v14.3.0, v12.20.0
  379. */
  380. ref(): this;
  381. /**
  382. * When called, the active `fs.FSWatcher` object will not require the Node.js
  383. * event loop to remain active. If there is no other activity keeping the
  384. * event loop running, the process may exit before the `fs.FSWatcher` object's
  385. * callback is invoked. Calling `watcher.unref()` multiple times will have
  386. * no effect.
  387. * @since v14.3.0, v12.20.0
  388. */
  389. unref(): this;
  390. /**
  391. * events.EventEmitter
  392. * 1. change
  393. * 2. close
  394. * 3. error
  395. */
  396. addListener(event: string, listener: (...args: any[]) => void): this;
  397. addListener(event: "change", listener: (eventType: string, filename: string | Buffer) => void): this;
  398. addListener(event: "close", listener: () => void): this;
  399. addListener(event: "error", listener: (error: Error) => void): this;
  400. on(event: string, listener: (...args: any[]) => void): this;
  401. on(event: "change", listener: (eventType: string, filename: string | Buffer) => void): this;
  402. on(event: "close", listener: () => void): this;
  403. on(event: "error", listener: (error: Error) => void): this;
  404. once(event: string, listener: (...args: any[]) => void): this;
  405. once(event: "change", listener: (eventType: string, filename: string | Buffer) => void): this;
  406. once(event: "close", listener: () => void): this;
  407. once(event: "error", listener: (error: Error) => void): this;
  408. prependListener(event: string, listener: (...args: any[]) => void): this;
  409. prependListener(event: "change", listener: (eventType: string, filename: string | Buffer) => void): this;
  410. prependListener(event: "close", listener: () => void): this;
  411. prependListener(event: "error", listener: (error: Error) => void): this;
  412. prependOnceListener(event: string, listener: (...args: any[]) => void): this;
  413. prependOnceListener(event: "change", listener: (eventType: string, filename: string | Buffer) => void): this;
  414. prependOnceListener(event: "close", listener: () => void): this;
  415. prependOnceListener(event: "error", listener: (error: Error) => void): this;
  416. }
  417. /**
  418. * Instances of `fs.ReadStream` are created and returned using the {@link createReadStream} function.
  419. * @since v0.1.93
  420. */
  421. export class ReadStream extends stream.Readable {
  422. close(callback?: (err?: NodeJS.ErrnoException | null) => void): void;
  423. /**
  424. * The number of bytes that have been read so far.
  425. * @since v6.4.0
  426. */
  427. bytesRead: number;
  428. /**
  429. * The path to the file the stream is reading from as specified in the first
  430. * argument to `fs.createReadStream()`. If `path` is passed as a string, then`readStream.path` will be a string. If `path` is passed as a `Buffer`, then`readStream.path` will be a
  431. * `Buffer`. If `fd` is specified, then`readStream.path` will be `undefined`.
  432. * @since v0.1.93
  433. */
  434. path: string | Buffer;
  435. /**
  436. * This property is `true` if the underlying file has not been opened yet,
  437. * i.e. before the `'ready'` event is emitted.
  438. * @since v11.2.0, v10.16.0
  439. */
  440. pending: boolean;
  441. /**
  442. * events.EventEmitter
  443. * 1. open
  444. * 2. close
  445. * 3. ready
  446. */
  447. addListener<K extends keyof ReadStreamEvents>(event: K, listener: ReadStreamEvents[K]): this;
  448. on<K extends keyof ReadStreamEvents>(event: K, listener: ReadStreamEvents[K]): this;
  449. once<K extends keyof ReadStreamEvents>(event: K, listener: ReadStreamEvents[K]): this;
  450. prependListener<K extends keyof ReadStreamEvents>(event: K, listener: ReadStreamEvents[K]): this;
  451. prependOnceListener<K extends keyof ReadStreamEvents>(event: K, listener: ReadStreamEvents[K]): this;
  452. }
  453. /**
  454. * The Keys are events of the ReadStream and the values are the functions that are called when the event is emitted.
  455. */
  456. type ReadStreamEvents = {
  457. close: () => void;
  458. data: (chunk: Buffer | string) => void;
  459. end: () => void;
  460. error: (err: Error) => void;
  461. open: (fd: number) => void;
  462. pause: () => void;
  463. readable: () => void;
  464. ready: () => void;
  465. resume: () => void;
  466. } & CustomEvents;
  467. /**
  468. * string & {} allows to allow any kind of strings for the event
  469. * but still allows to have auto completion for the normal events.
  470. */
  471. type CustomEvents = { [Key in string & {} | symbol]: (...args: any[]) => void };
  472. /**
  473. * The Keys are events of the WriteStream and the values are the functions that are called when the event is emitted.
  474. */
  475. type WriteStreamEvents = {
  476. close: () => void;
  477. drain: () => void;
  478. error: (err: Error) => void;
  479. finish: () => void;
  480. open: (fd: number) => void;
  481. pipe: (src: stream.Readable) => void;
  482. ready: () => void;
  483. unpipe: (src: stream.Readable) => void;
  484. } & CustomEvents;
  485. /**
  486. * * Extends `stream.Writable`
  487. *
  488. * Instances of `fs.WriteStream` are created and returned using the {@link createWriteStream} function.
  489. * @since v0.1.93
  490. */
  491. export class WriteStream extends stream.Writable {
  492. /**
  493. * Closes `writeStream`. Optionally accepts a
  494. * callback that will be executed once the `writeStream`is closed.
  495. * @since v0.9.4
  496. */
  497. close(callback?: (err?: NodeJS.ErrnoException | null) => void): void;
  498. /**
  499. * The number of bytes written so far. Does not include data that is still queued
  500. * for writing.
  501. * @since v0.4.7
  502. */
  503. bytesWritten: number;
  504. /**
  505. * The path to the file the stream is writing to as specified in the first
  506. * argument to {@link createWriteStream}. If `path` is passed as a string, then`writeStream.path` will be a string. If `path` is passed as a `Buffer`, then`writeStream.path` will be a
  507. * `Buffer`.
  508. * @since v0.1.93
  509. */
  510. path: string | Buffer;
  511. /**
  512. * This property is `true` if the underlying file has not been opened yet,
  513. * i.e. before the `'ready'` event is emitted.
  514. * @since v11.2.0
  515. */
  516. pending: boolean;
  517. /**
  518. * events.EventEmitter
  519. * 1. open
  520. * 2. close
  521. * 3. ready
  522. */
  523. addListener<K extends keyof WriteStreamEvents>(event: K, listener: WriteStreamEvents[K]): this;
  524. on<K extends keyof WriteStreamEvents>(event: K, listener: WriteStreamEvents[K]): this;
  525. once<K extends keyof WriteStreamEvents>(event: K, listener: WriteStreamEvents[K]): this;
  526. prependListener<K extends keyof WriteStreamEvents>(event: K, listener: WriteStreamEvents[K]): this;
  527. prependOnceListener<K extends keyof WriteStreamEvents>(event: K, listener: WriteStreamEvents[K]): this;
  528. }
  529. /**
  530. * Asynchronously rename file at `oldPath` to the pathname provided
  531. * as `newPath`. In the case that `newPath` already exists, it will
  532. * be overwritten. If there is a directory at `newPath`, an error will
  533. * be raised instead. No arguments other than a possible exception are
  534. * given to the completion callback.
  535. *
  536. * See also: [`rename(2)`](http://man7.org/linux/man-pages/man2/rename.2.html).
  537. *
  538. * ```js
  539. * import { rename } from 'node:fs';
  540. *
  541. * rename('oldFile.txt', 'newFile.txt', (err) => {
  542. * if (err) throw err;
  543. * console.log('Rename complete!');
  544. * });
  545. * ```
  546. * @since v0.0.2
  547. */
  548. export function rename(oldPath: PathLike, newPath: PathLike, callback: NoParamCallback): void;
  549. export namespace rename {
  550. /**
  551. * Asynchronous rename(2) - Change the name or location of a file or directory.
  552. * @param oldPath A path to a file. If a URL is provided, it must use the `file:` protocol.
  553. * URL support is _experimental_.
  554. * @param newPath A path to a file. If a URL is provided, it must use the `file:` protocol.
  555. * URL support is _experimental_.
  556. */
  557. function __promisify__(oldPath: PathLike, newPath: PathLike): Promise<void>;
  558. }
  559. /**
  560. * Renames the file from `oldPath` to `newPath`. Returns `undefined`.
  561. *
  562. * See the POSIX [`rename(2)`](http://man7.org/linux/man-pages/man2/rename.2.html) documentation for more details.
  563. * @since v0.1.21
  564. */
  565. export function renameSync(oldPath: PathLike, newPath: PathLike): void;
  566. /**
  567. * Truncates the file. No arguments other than a possible exception are
  568. * given to the completion callback. A file descriptor can also be passed as the
  569. * first argument. In this case, `fs.ftruncate()` is called.
  570. *
  571. * ```js
  572. * import { truncate } from 'node:fs';
  573. * // Assuming that 'path/file.txt' is a regular file.
  574. * truncate('path/file.txt', (err) => {
  575. * if (err) throw err;
  576. * console.log('path/file.txt was truncated');
  577. * });
  578. * ```
  579. *
  580. * Passing a file descriptor is deprecated and may result in an error being thrown
  581. * in the future.
  582. *
  583. * See the POSIX [`truncate(2)`](http://man7.org/linux/man-pages/man2/truncate.2.html) documentation for more details.
  584. * @since v0.8.6
  585. * @param [len=0]
  586. */
  587. export function truncate(path: PathLike, len: number | undefined, callback: NoParamCallback): void;
  588. /**
  589. * Asynchronous truncate(2) - Truncate a file to a specified length.
  590. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  591. */
  592. export function truncate(path: PathLike, callback: NoParamCallback): void;
  593. export namespace truncate {
  594. /**
  595. * Asynchronous truncate(2) - Truncate a file to a specified length.
  596. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  597. * @param len If not specified, defaults to `0`.
  598. */
  599. function __promisify__(path: PathLike, len?: number): Promise<void>;
  600. }
  601. /**
  602. * Truncates the file. Returns `undefined`. A file descriptor can also be
  603. * passed as the first argument. In this case, `fs.ftruncateSync()` is called.
  604. *
  605. * Passing a file descriptor is deprecated and may result in an error being thrown
  606. * in the future.
  607. * @since v0.8.6
  608. * @param [len=0]
  609. */
  610. export function truncateSync(path: PathLike, len?: number): void;
  611. /**
  612. * Truncates the file descriptor. No arguments other than a possible exception are
  613. * given to the completion callback.
  614. *
  615. * See the POSIX [`ftruncate(2)`](http://man7.org/linux/man-pages/man2/ftruncate.2.html) documentation for more detail.
  616. *
  617. * If the file referred to by the file descriptor was larger than `len` bytes, only
  618. * the first `len` bytes will be retained in the file.
  619. *
  620. * For example, the following program retains only the first four bytes of the
  621. * file:
  622. *
  623. * ```js
  624. * import { open, close, ftruncate } from 'node:fs';
  625. *
  626. * function closeFd(fd) {
  627. * close(fd, (err) => {
  628. * if (err) throw err;
  629. * });
  630. * }
  631. *
  632. * open('temp.txt', 'r+', (err, fd) => {
  633. * if (err) throw err;
  634. *
  635. * try {
  636. * ftruncate(fd, 4, (err) => {
  637. * closeFd(fd);
  638. * if (err) throw err;
  639. * });
  640. * } catch (err) {
  641. * closeFd(fd);
  642. * if (err) throw err;
  643. * }
  644. * });
  645. * ```
  646. *
  647. * If the file previously was shorter than `len` bytes, it is extended, and the
  648. * extended part is filled with null bytes (`'\0'`):
  649. *
  650. * If `len` is negative then `0` will be used.
  651. * @since v0.8.6
  652. * @param [len=0]
  653. */
  654. export function ftruncate(fd: number, len: number | undefined, callback: NoParamCallback): void;
  655. /**
  656. * Asynchronous ftruncate(2) - Truncate a file to a specified length.
  657. * @param fd A file descriptor.
  658. */
  659. export function ftruncate(fd: number, callback: NoParamCallback): void;
  660. export namespace ftruncate {
  661. /**
  662. * Asynchronous ftruncate(2) - Truncate a file to a specified length.
  663. * @param fd A file descriptor.
  664. * @param len If not specified, defaults to `0`.
  665. */
  666. function __promisify__(fd: number, len?: number): Promise<void>;
  667. }
  668. /**
  669. * Truncates the file descriptor. Returns `undefined`.
  670. *
  671. * For detailed information, see the documentation of the asynchronous version of
  672. * this API: {@link ftruncate}.
  673. * @since v0.8.6
  674. * @param [len=0]
  675. */
  676. export function ftruncateSync(fd: number, len?: number): void;
  677. /**
  678. * Asynchronously changes owner and group of a file. No arguments other than a
  679. * possible exception are given to the completion callback.
  680. *
  681. * See the POSIX [`chown(2)`](http://man7.org/linux/man-pages/man2/chown.2.html) documentation for more detail.
  682. * @since v0.1.97
  683. */
  684. export function chown(path: PathLike, uid: number, gid: number, callback: NoParamCallback): void;
  685. export namespace chown {
  686. /**
  687. * Asynchronous chown(2) - Change ownership of a file.
  688. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  689. */
  690. function __promisify__(path: PathLike, uid: number, gid: number): Promise<void>;
  691. }
  692. /**
  693. * Synchronously changes owner and group of a file. Returns `undefined`.
  694. * This is the synchronous version of {@link chown}.
  695. *
  696. * See the POSIX [`chown(2)`](http://man7.org/linux/man-pages/man2/chown.2.html) documentation for more detail.
  697. * @since v0.1.97
  698. */
  699. export function chownSync(path: PathLike, uid: number, gid: number): void;
  700. /**
  701. * Sets the owner of the file. No arguments other than a possible exception are
  702. * given to the completion callback.
  703. *
  704. * See the POSIX [`fchown(2)`](http://man7.org/linux/man-pages/man2/fchown.2.html) documentation for more detail.
  705. * @since v0.4.7
  706. */
  707. export function fchown(fd: number, uid: number, gid: number, callback: NoParamCallback): void;
  708. export namespace fchown {
  709. /**
  710. * Asynchronous fchown(2) - Change ownership of a file.
  711. * @param fd A file descriptor.
  712. */
  713. function __promisify__(fd: number, uid: number, gid: number): Promise<void>;
  714. }
  715. /**
  716. * Sets the owner of the file. Returns `undefined`.
  717. *
  718. * See the POSIX [`fchown(2)`](http://man7.org/linux/man-pages/man2/fchown.2.html) documentation for more detail.
  719. * @since v0.4.7
  720. * @param uid The file's new owner's user id.
  721. * @param gid The file's new group's group id.
  722. */
  723. export function fchownSync(fd: number, uid: number, gid: number): void;
  724. /**
  725. * Set the owner of the symbolic link. No arguments other than a possible
  726. * exception are given to the completion callback.
  727. *
  728. * See the POSIX [`lchown(2)`](http://man7.org/linux/man-pages/man2/lchown.2.html) documentation for more detail.
  729. */
  730. export function lchown(path: PathLike, uid: number, gid: number, callback: NoParamCallback): void;
  731. export namespace lchown {
  732. /**
  733. * Asynchronous lchown(2) - Change ownership of a file. Does not dereference symbolic links.
  734. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  735. */
  736. function __promisify__(path: PathLike, uid: number, gid: number): Promise<void>;
  737. }
  738. /**
  739. * Set the owner for the path. Returns `undefined`.
  740. *
  741. * See the POSIX [`lchown(2)`](http://man7.org/linux/man-pages/man2/lchown.2.html) documentation for more details.
  742. * @param uid The file's new owner's user id.
  743. * @param gid The file's new group's group id.
  744. */
  745. export function lchownSync(path: PathLike, uid: number, gid: number): void;
  746. /**
  747. * Changes the access and modification times of a file in the same way as {@link utimes}, with the difference that if the path refers to a symbolic
  748. * link, then the link is not dereferenced: instead, the timestamps of the
  749. * symbolic link itself are changed.
  750. *
  751. * No arguments other than a possible exception are given to the completion
  752. * callback.
  753. * @since v14.5.0, v12.19.0
  754. */
  755. export function lutimes(path: PathLike, atime: TimeLike, mtime: TimeLike, callback: NoParamCallback): void;
  756. export namespace lutimes {
  757. /**
  758. * Changes the access and modification times of a file in the same way as `fsPromises.utimes()`,
  759. * with the difference that if the path refers to a symbolic link, then the link is not
  760. * dereferenced: instead, the timestamps of the symbolic link itself are changed.
  761. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  762. * @param atime The last access time. If a string is provided, it will be coerced to number.
  763. * @param mtime The last modified time. If a string is provided, it will be coerced to number.
  764. */
  765. function __promisify__(path: PathLike, atime: TimeLike, mtime: TimeLike): Promise<void>;
  766. }
  767. /**
  768. * Change the file system timestamps of the symbolic link referenced by `path`.
  769. * Returns `undefined`, or throws an exception when parameters are incorrect or
  770. * the operation fails. This is the synchronous version of {@link lutimes}.
  771. * @since v14.5.0, v12.19.0
  772. */
  773. export function lutimesSync(path: PathLike, atime: TimeLike, mtime: TimeLike): void;
  774. /**
  775. * Asynchronously changes the permissions of a file. No arguments other than a
  776. * possible exception are given to the completion callback.
  777. *
  778. * See the POSIX [`chmod(2)`](http://man7.org/linux/man-pages/man2/chmod.2.html) documentation for more detail.
  779. *
  780. * ```js
  781. * import { chmod } from 'node:fs';
  782. *
  783. * chmod('my_file.txt', 0o775, (err) => {
  784. * if (err) throw err;
  785. * console.log('The permissions for file "my_file.txt" have been changed!');
  786. * });
  787. * ```
  788. * @since v0.1.30
  789. */
  790. export function chmod(path: PathLike, mode: Mode, callback: NoParamCallback): void;
  791. export namespace chmod {
  792. /**
  793. * Asynchronous chmod(2) - Change permissions of a file.
  794. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  795. * @param mode A file mode. If a string is passed, it is parsed as an octal integer.
  796. */
  797. function __promisify__(path: PathLike, mode: Mode): Promise<void>;
  798. }
  799. /**
  800. * For detailed information, see the documentation of the asynchronous version of
  801. * this API: {@link chmod}.
  802. *
  803. * See the POSIX [`chmod(2)`](http://man7.org/linux/man-pages/man2/chmod.2.html) documentation for more detail.
  804. * @since v0.6.7
  805. */
  806. export function chmodSync(path: PathLike, mode: Mode): void;
  807. /**
  808. * Sets the permissions on the file. No arguments other than a possible exception
  809. * are given to the completion callback.
  810. *
  811. * See the POSIX [`fchmod(2)`](http://man7.org/linux/man-pages/man2/fchmod.2.html) documentation for more detail.
  812. * @since v0.4.7
  813. */
  814. export function fchmod(fd: number, mode: Mode, callback: NoParamCallback): void;
  815. export namespace fchmod {
  816. /**
  817. * Asynchronous fchmod(2) - Change permissions of a file.
  818. * @param fd A file descriptor.
  819. * @param mode A file mode. If a string is passed, it is parsed as an octal integer.
  820. */
  821. function __promisify__(fd: number, mode: Mode): Promise<void>;
  822. }
  823. /**
  824. * Sets the permissions on the file. Returns `undefined`.
  825. *
  826. * See the POSIX [`fchmod(2)`](http://man7.org/linux/man-pages/man2/fchmod.2.html) documentation for more detail.
  827. * @since v0.4.7
  828. */
  829. export function fchmodSync(fd: number, mode: Mode): void;
  830. /**
  831. * Changes the permissions on a symbolic link. No arguments other than a possible
  832. * exception are given to the completion callback.
  833. *
  834. * This method is only implemented on macOS.
  835. *
  836. * See the POSIX [`lchmod(2)`](https://www.freebsd.org/cgi/man.cgi?query=lchmod&sektion=2) documentation for more detail.
  837. * @deprecated Since v0.4.7
  838. */
  839. export function lchmod(path: PathLike, mode: Mode, callback: NoParamCallback): void;
  840. /** @deprecated */
  841. export namespace lchmod {
  842. /**
  843. * Asynchronous lchmod(2) - Change permissions of a file. Does not dereference symbolic links.
  844. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  845. * @param mode A file mode. If a string is passed, it is parsed as an octal integer.
  846. */
  847. function __promisify__(path: PathLike, mode: Mode): Promise<void>;
  848. }
  849. /**
  850. * Changes the permissions on a symbolic link. Returns `undefined`.
  851. *
  852. * This method is only implemented on macOS.
  853. *
  854. * See the POSIX [`lchmod(2)`](https://www.freebsd.org/cgi/man.cgi?query=lchmod&sektion=2) documentation for more detail.
  855. * @deprecated Since v0.4.7
  856. */
  857. export function lchmodSync(path: PathLike, mode: Mode): void;
  858. /**
  859. * Asynchronous [`stat(2)`](http://man7.org/linux/man-pages/man2/stat.2.html). The callback gets two arguments `(err, stats)` where`stats` is an `fs.Stats` object.
  860. *
  861. * In case of an error, the `err.code` will be one of `Common System Errors`.
  862. *
  863. * {@link stat} follows symbolic links. Use {@link lstat} to look at the
  864. * links themselves.
  865. *
  866. * Using `fs.stat()` to check for the existence of a file before calling`fs.open()`, `fs.readFile()`, or `fs.writeFile()` is not recommended.
  867. * Instead, user code should open/read/write the file directly and handle the
  868. * error raised if the file is not available.
  869. *
  870. * To check if a file exists without manipulating it afterwards, {@link access} is recommended.
  871. *
  872. * For example, given the following directory structure:
  873. *
  874. * ```text
  875. * - txtDir
  876. * -- file.txt
  877. * - app.js
  878. * ```
  879. *
  880. * The next program will check for the stats of the given paths:
  881. *
  882. * ```js
  883. * import { stat } from 'node:fs';
  884. *
  885. * const pathsToCheck = ['./txtDir', './txtDir/file.txt'];
  886. *
  887. * for (let i = 0; i < pathsToCheck.length; i++) {
  888. * stat(pathsToCheck[i], (err, stats) => {
  889. * console.log(stats.isDirectory());
  890. * console.log(stats);
  891. * });
  892. * }
  893. * ```
  894. *
  895. * The resulting output will resemble:
  896. *
  897. * ```console
  898. * true
  899. * Stats {
  900. * dev: 16777220,
  901. * mode: 16877,
  902. * nlink: 3,
  903. * uid: 501,
  904. * gid: 20,
  905. * rdev: 0,
  906. * blksize: 4096,
  907. * ino: 14214262,
  908. * size: 96,
  909. * blocks: 0,
  910. * atimeMs: 1561174653071.963,
  911. * mtimeMs: 1561174614583.3518,
  912. * ctimeMs: 1561174626623.5366,
  913. * birthtimeMs: 1561174126937.2893,
  914. * atime: 2019-06-22T03:37:33.072Z,
  915. * mtime: 2019-06-22T03:36:54.583Z,
  916. * ctime: 2019-06-22T03:37:06.624Z,
  917. * birthtime: 2019-06-22T03:28:46.937Z
  918. * }
  919. * false
  920. * Stats {
  921. * dev: 16777220,
  922. * mode: 33188,
  923. * nlink: 1,
  924. * uid: 501,
  925. * gid: 20,
  926. * rdev: 0,
  927. * blksize: 4096,
  928. * ino: 14214074,
  929. * size: 8,
  930. * blocks: 8,
  931. * atimeMs: 1561174616618.8555,
  932. * mtimeMs: 1561174614584,
  933. * ctimeMs: 1561174614583.8145,
  934. * birthtimeMs: 1561174007710.7478,
  935. * atime: 2019-06-22T03:36:56.619Z,
  936. * mtime: 2019-06-22T03:36:54.584Z,
  937. * ctime: 2019-06-22T03:36:54.584Z,
  938. * birthtime: 2019-06-22T03:26:47.711Z
  939. * }
  940. * ```
  941. * @since v0.0.2
  942. */
  943. export function stat(path: PathLike, callback: (err: NodeJS.ErrnoException | null, stats: Stats) => void): void;
  944. export function stat(
  945. path: PathLike,
  946. options:
  947. | (StatOptions & {
  948. bigint?: false | undefined;
  949. })
  950. | undefined,
  951. callback: (err: NodeJS.ErrnoException | null, stats: Stats) => void,
  952. ): void;
  953. export function stat(
  954. path: PathLike,
  955. options: StatOptions & {
  956. bigint: true;
  957. },
  958. callback: (err: NodeJS.ErrnoException | null, stats: BigIntStats) => void,
  959. ): void;
  960. export function stat(
  961. path: PathLike,
  962. options: StatOptions | undefined,
  963. callback: (err: NodeJS.ErrnoException | null, stats: Stats | BigIntStats) => void,
  964. ): void;
  965. export namespace stat {
  966. /**
  967. * Asynchronous stat(2) - Get file status.
  968. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  969. */
  970. function __promisify__(
  971. path: PathLike,
  972. options?: StatOptions & {
  973. bigint?: false | undefined;
  974. },
  975. ): Promise<Stats>;
  976. function __promisify__(
  977. path: PathLike,
  978. options: StatOptions & {
  979. bigint: true;
  980. },
  981. ): Promise<BigIntStats>;
  982. function __promisify__(path: PathLike, options?: StatOptions): Promise<Stats | BigIntStats>;
  983. }
  984. export interface StatSyncFn extends Function {
  985. (path: PathLike, options?: undefined): Stats;
  986. (
  987. path: PathLike,
  988. options?: StatSyncOptions & {
  989. bigint?: false | undefined;
  990. throwIfNoEntry: false;
  991. },
  992. ): Stats | undefined;
  993. (
  994. path: PathLike,
  995. options: StatSyncOptions & {
  996. bigint: true;
  997. throwIfNoEntry: false;
  998. },
  999. ): BigIntStats | undefined;
  1000. (
  1001. path: PathLike,
  1002. options?: StatSyncOptions & {
  1003. bigint?: false | undefined;
  1004. },
  1005. ): Stats;
  1006. (
  1007. path: PathLike,
  1008. options: StatSyncOptions & {
  1009. bigint: true;
  1010. },
  1011. ): BigIntStats;
  1012. (
  1013. path: PathLike,
  1014. options: StatSyncOptions & {
  1015. bigint: boolean;
  1016. throwIfNoEntry?: false | undefined;
  1017. },
  1018. ): Stats | BigIntStats;
  1019. (path: PathLike, options?: StatSyncOptions): Stats | BigIntStats | undefined;
  1020. }
  1021. /**
  1022. * Synchronous stat(2) - Get file status.
  1023. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  1024. */
  1025. export const statSync: StatSyncFn;
  1026. /**
  1027. * Invokes the callback with the `fs.Stats` for the file descriptor.
  1028. *
  1029. * See the POSIX [`fstat(2)`](http://man7.org/linux/man-pages/man2/fstat.2.html) documentation for more detail.
  1030. * @since v0.1.95
  1031. */
  1032. export function fstat(fd: number, callback: (err: NodeJS.ErrnoException | null, stats: Stats) => void): void;
  1033. export function fstat(
  1034. fd: number,
  1035. options:
  1036. | (StatOptions & {
  1037. bigint?: false | undefined;
  1038. })
  1039. | undefined,
  1040. callback: (err: NodeJS.ErrnoException | null, stats: Stats) => void,
  1041. ): void;
  1042. export function fstat(
  1043. fd: number,
  1044. options: StatOptions & {
  1045. bigint: true;
  1046. },
  1047. callback: (err: NodeJS.ErrnoException | null, stats: BigIntStats) => void,
  1048. ): void;
  1049. export function fstat(
  1050. fd: number,
  1051. options: StatOptions | undefined,
  1052. callback: (err: NodeJS.ErrnoException | null, stats: Stats | BigIntStats) => void,
  1053. ): void;
  1054. export namespace fstat {
  1055. /**
  1056. * Asynchronous fstat(2) - Get file status.
  1057. * @param fd A file descriptor.
  1058. */
  1059. function __promisify__(
  1060. fd: number,
  1061. options?: StatOptions & {
  1062. bigint?: false | undefined;
  1063. },
  1064. ): Promise<Stats>;
  1065. function __promisify__(
  1066. fd: number,
  1067. options: StatOptions & {
  1068. bigint: true;
  1069. },
  1070. ): Promise<BigIntStats>;
  1071. function __promisify__(fd: number, options?: StatOptions): Promise<Stats | BigIntStats>;
  1072. }
  1073. /**
  1074. * Retrieves the `fs.Stats` for the file descriptor.
  1075. *
  1076. * See the POSIX [`fstat(2)`](http://man7.org/linux/man-pages/man2/fstat.2.html) documentation for more detail.
  1077. * @since v0.1.95
  1078. */
  1079. export function fstatSync(
  1080. fd: number,
  1081. options?: StatOptions & {
  1082. bigint?: false | undefined;
  1083. },
  1084. ): Stats;
  1085. export function fstatSync(
  1086. fd: number,
  1087. options: StatOptions & {
  1088. bigint: true;
  1089. },
  1090. ): BigIntStats;
  1091. export function fstatSync(fd: number, options?: StatOptions): Stats | BigIntStats;
  1092. /**
  1093. * Retrieves the `fs.Stats` for the symbolic link referred to by the path.
  1094. * The callback gets two arguments `(err, stats)` where `stats` is a `fs.Stats` object. `lstat()` is identical to `stat()`, except that if `path` is a symbolic
  1095. * link, then the link itself is stat-ed, not the file that it refers to.
  1096. *
  1097. * See the POSIX [`lstat(2)`](http://man7.org/linux/man-pages/man2/lstat.2.html) documentation for more details.
  1098. * @since v0.1.30
  1099. */
  1100. export function lstat(path: PathLike, callback: (err: NodeJS.ErrnoException | null, stats: Stats) => void): void;
  1101. export function lstat(
  1102. path: PathLike,
  1103. options:
  1104. | (StatOptions & {
  1105. bigint?: false | undefined;
  1106. })
  1107. | undefined,
  1108. callback: (err: NodeJS.ErrnoException | null, stats: Stats) => void,
  1109. ): void;
  1110. export function lstat(
  1111. path: PathLike,
  1112. options: StatOptions & {
  1113. bigint: true;
  1114. },
  1115. callback: (err: NodeJS.ErrnoException | null, stats: BigIntStats) => void,
  1116. ): void;
  1117. export function lstat(
  1118. path: PathLike,
  1119. options: StatOptions | undefined,
  1120. callback: (err: NodeJS.ErrnoException | null, stats: Stats | BigIntStats) => void,
  1121. ): void;
  1122. export namespace lstat {
  1123. /**
  1124. * Asynchronous lstat(2) - Get file status. Does not dereference symbolic links.
  1125. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  1126. */
  1127. function __promisify__(
  1128. path: PathLike,
  1129. options?: StatOptions & {
  1130. bigint?: false | undefined;
  1131. },
  1132. ): Promise<Stats>;
  1133. function __promisify__(
  1134. path: PathLike,
  1135. options: StatOptions & {
  1136. bigint: true;
  1137. },
  1138. ): Promise<BigIntStats>;
  1139. function __promisify__(path: PathLike, options?: StatOptions): Promise<Stats | BigIntStats>;
  1140. }
  1141. /**
  1142. * Asynchronous [`statfs(2)`](http://man7.org/linux/man-pages/man2/statfs.2.html). Returns information about the mounted file system which
  1143. * contains `path`. The callback gets two arguments `(err, stats)` where `stats`is an `fs.StatFs` object.
  1144. *
  1145. * In case of an error, the `err.code` will be one of `Common System Errors`.
  1146. * @since v19.6.0, v18.15.0
  1147. * @param path A path to an existing file or directory on the file system to be queried.
  1148. */
  1149. export function statfs(path: PathLike, callback: (err: NodeJS.ErrnoException | null, stats: StatsFs) => void): void;
  1150. export function statfs(
  1151. path: PathLike,
  1152. options:
  1153. | (StatFsOptions & {
  1154. bigint?: false | undefined;
  1155. })
  1156. | undefined,
  1157. callback: (err: NodeJS.ErrnoException | null, stats: StatsFs) => void,
  1158. ): void;
  1159. export function statfs(
  1160. path: PathLike,
  1161. options: StatFsOptions & {
  1162. bigint: true;
  1163. },
  1164. callback: (err: NodeJS.ErrnoException | null, stats: BigIntStatsFs) => void,
  1165. ): void;
  1166. export function statfs(
  1167. path: PathLike,
  1168. options: StatFsOptions | undefined,
  1169. callback: (err: NodeJS.ErrnoException | null, stats: StatsFs | BigIntStatsFs) => void,
  1170. ): void;
  1171. export namespace statfs {
  1172. /**
  1173. * Asynchronous statfs(2) - Returns information about the mounted file system which contains path. The callback gets two arguments (err, stats) where stats is an <fs.StatFs> object.
  1174. * @param path A path to an existing file or directory on the file system to be queried.
  1175. */
  1176. function __promisify__(
  1177. path: PathLike,
  1178. options?: StatFsOptions & {
  1179. bigint?: false | undefined;
  1180. },
  1181. ): Promise<StatsFs>;
  1182. function __promisify__(
  1183. path: PathLike,
  1184. options: StatFsOptions & {
  1185. bigint: true;
  1186. },
  1187. ): Promise<BigIntStatsFs>;
  1188. function __promisify__(path: PathLike, options?: StatFsOptions): Promise<StatsFs | BigIntStatsFs>;
  1189. }
  1190. /**
  1191. * Synchronous [`statfs(2)`](http://man7.org/linux/man-pages/man2/statfs.2.html). Returns information about the mounted file system which
  1192. * contains `path`.
  1193. *
  1194. * In case of an error, the `err.code` will be one of `Common System Errors`.
  1195. * @since v19.6.0, v18.15.0
  1196. * @param path A path to an existing file or directory on the file system to be queried.
  1197. */
  1198. export function statfsSync(
  1199. path: PathLike,
  1200. options?: StatFsOptions & {
  1201. bigint?: false | undefined;
  1202. },
  1203. ): StatsFs;
  1204. export function statfsSync(
  1205. path: PathLike,
  1206. options: StatFsOptions & {
  1207. bigint: true;
  1208. },
  1209. ): BigIntStatsFs;
  1210. export function statfsSync(path: PathLike, options?: StatFsOptions): StatsFs | BigIntStatsFs;
  1211. /**
  1212. * Synchronous lstat(2) - Get file status. Does not dereference symbolic links.
  1213. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  1214. */
  1215. export const lstatSync: StatSyncFn;
  1216. /**
  1217. * Creates a new link from the `existingPath` to the `newPath`. See the POSIX [`link(2)`](http://man7.org/linux/man-pages/man2/link.2.html) documentation for more detail. No arguments other than
  1218. * a possible
  1219. * exception are given to the completion callback.
  1220. * @since v0.1.31
  1221. */
  1222. export function link(existingPath: PathLike, newPath: PathLike, callback: NoParamCallback): void;
  1223. export namespace link {
  1224. /**
  1225. * Asynchronous link(2) - Create a new link (also known as a hard link) to an existing file.
  1226. * @param existingPath A path to a file. If a URL is provided, it must use the `file:` protocol.
  1227. * @param newPath A path to a file. If a URL is provided, it must use the `file:` protocol.
  1228. */
  1229. function __promisify__(existingPath: PathLike, newPath: PathLike): Promise<void>;
  1230. }
  1231. /**
  1232. * Creates a new link from the `existingPath` to the `newPath`. See the POSIX [`link(2)`](http://man7.org/linux/man-pages/man2/link.2.html) documentation for more detail. Returns `undefined`.
  1233. * @since v0.1.31
  1234. */
  1235. export function linkSync(existingPath: PathLike, newPath: PathLike): void;
  1236. /**
  1237. * Creates the link called `path` pointing to `target`. No arguments other than a
  1238. * possible exception are given to the completion callback.
  1239. *
  1240. * See the POSIX [`symlink(2)`](http://man7.org/linux/man-pages/man2/symlink.2.html) documentation for more details.
  1241. *
  1242. * The `type` argument is only available on Windows and ignored on other platforms.
  1243. * It can be set to `'dir'`, `'file'`, or `'junction'`. If the `type` argument is
  1244. * not a string, Node.js will autodetect `target` type and use `'file'` or `'dir'`.
  1245. * If the `target` does not exist, `'file'` will be used. Windows junction points
  1246. * require the destination path to be absolute. When using `'junction'`, the`target` argument will automatically be normalized to absolute path. Junction
  1247. * points on NTFS volumes can only point to directories.
  1248. *
  1249. * Relative targets are relative to the link's parent directory.
  1250. *
  1251. * ```js
  1252. * import { symlink } from 'node:fs';
  1253. *
  1254. * symlink('./mew', './mewtwo', callback);
  1255. * ```
  1256. *
  1257. * The above example creates a symbolic link `mewtwo` which points to `mew` in the
  1258. * same directory:
  1259. *
  1260. * ```bash
  1261. * $ tree .
  1262. * .
  1263. * ├── mew
  1264. * └── mewtwo -> ./mew
  1265. * ```
  1266. * @since v0.1.31
  1267. * @param [type='null']
  1268. */
  1269. export function symlink(
  1270. target: PathLike,
  1271. path: PathLike,
  1272. type: symlink.Type | undefined | null,
  1273. callback: NoParamCallback,
  1274. ): void;
  1275. /**
  1276. * Asynchronous symlink(2) - Create a new symbolic link to an existing file.
  1277. * @param target A path to an existing file. If a URL is provided, it must use the `file:` protocol.
  1278. * @param path A path to the new symlink. If a URL is provided, it must use the `file:` protocol.
  1279. */
  1280. export function symlink(target: PathLike, path: PathLike, callback: NoParamCallback): void;
  1281. export namespace symlink {
  1282. /**
  1283. * Asynchronous symlink(2) - Create a new symbolic link to an existing file.
  1284. * @param target A path to an existing file. If a URL is provided, it must use the `file:` protocol.
  1285. * @param path A path to the new symlink. If a URL is provided, it must use the `file:` protocol.
  1286. * @param type May be set to `'dir'`, `'file'`, or `'junction'` (default is `'file'`) and is only available on Windows (ignored on other platforms).
  1287. * When using `'junction'`, the `target` argument will automatically be normalized to an absolute path.
  1288. */
  1289. function __promisify__(target: PathLike, path: PathLike, type?: string | null): Promise<void>;
  1290. type Type = "dir" | "file" | "junction";
  1291. }
  1292. /**
  1293. * Returns `undefined`.
  1294. *
  1295. * For detailed information, see the documentation of the asynchronous version of
  1296. * this API: {@link symlink}.
  1297. * @since v0.1.31
  1298. * @param [type='null']
  1299. */
  1300. export function symlinkSync(target: PathLike, path: PathLike, type?: symlink.Type | null): void;
  1301. /**
  1302. * Reads the contents of the symbolic link referred to by `path`. The callback gets
  1303. * two arguments `(err, linkString)`.
  1304. *
  1305. * See the POSIX [`readlink(2)`](http://man7.org/linux/man-pages/man2/readlink.2.html) documentation for more details.
  1306. *
  1307. * The optional `options` argument can be a string specifying an encoding, or an
  1308. * object with an `encoding` property specifying the character encoding to use for
  1309. * the link path passed to the callback. If the `encoding` is set to `'buffer'`,
  1310. * the link path returned will be passed as a `Buffer` object.
  1311. * @since v0.1.31
  1312. */
  1313. export function readlink(
  1314. path: PathLike,
  1315. options: EncodingOption,
  1316. callback: (err: NodeJS.ErrnoException | null, linkString: string) => void,
  1317. ): void;
  1318. /**
  1319. * Asynchronous readlink(2) - read value of a symbolic link.
  1320. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  1321. * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
  1322. */
  1323. export function readlink(
  1324. path: PathLike,
  1325. options: BufferEncodingOption,
  1326. callback: (err: NodeJS.ErrnoException | null, linkString: Buffer) => void,
  1327. ): void;
  1328. /**
  1329. * Asynchronous readlink(2) - read value of a symbolic link.
  1330. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  1331. * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
  1332. */
  1333. export function readlink(
  1334. path: PathLike,
  1335. options: EncodingOption,
  1336. callback: (err: NodeJS.ErrnoException | null, linkString: string | Buffer) => void,
  1337. ): void;
  1338. /**
  1339. * Asynchronous readlink(2) - read value of a symbolic link.
  1340. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  1341. */
  1342. export function readlink(
  1343. path: PathLike,
  1344. callback: (err: NodeJS.ErrnoException | null, linkString: string) => void,
  1345. ): void;
  1346. export namespace readlink {
  1347. /**
  1348. * Asynchronous readlink(2) - read value of a symbolic link.
  1349. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  1350. * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
  1351. */
  1352. function __promisify__(path: PathLike, options?: EncodingOption): Promise<string>;
  1353. /**
  1354. * Asynchronous readlink(2) - read value of a symbolic link.
  1355. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  1356. * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
  1357. */
  1358. function __promisify__(path: PathLike, options: BufferEncodingOption): Promise<Buffer>;
  1359. /**
  1360. * Asynchronous readlink(2) - read value of a symbolic link.
  1361. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  1362. * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
  1363. */
  1364. function __promisify__(path: PathLike, options?: EncodingOption): Promise<string | Buffer>;
  1365. }
  1366. /**
  1367. * Returns the symbolic link's string value.
  1368. *
  1369. * See the POSIX [`readlink(2)`](http://man7.org/linux/man-pages/man2/readlink.2.html) documentation for more details.
  1370. *
  1371. * The optional `options` argument can be a string specifying an encoding, or an
  1372. * object with an `encoding` property specifying the character encoding to use for
  1373. * the link path returned. If the `encoding` is set to `'buffer'`,
  1374. * the link path returned will be passed as a `Buffer` object.
  1375. * @since v0.1.31
  1376. */
  1377. export function readlinkSync(path: PathLike, options?: EncodingOption): string;
  1378. /**
  1379. * Synchronous readlink(2) - read value of a symbolic link.
  1380. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  1381. * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
  1382. */
  1383. export function readlinkSync(path: PathLike, options: BufferEncodingOption): Buffer;
  1384. /**
  1385. * Synchronous readlink(2) - read value of a symbolic link.
  1386. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  1387. * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
  1388. */
  1389. export function readlinkSync(path: PathLike, options?: EncodingOption): string | Buffer;
  1390. /**
  1391. * Asynchronously computes the canonical pathname by resolving `.`, `..`, and
  1392. * symbolic links.
  1393. *
  1394. * A canonical pathname is not necessarily unique. Hard links and bind mounts can
  1395. * expose a file system entity through many pathnames.
  1396. *
  1397. * This function behaves like [`realpath(3)`](http://man7.org/linux/man-pages/man3/realpath.3.html), with some exceptions:
  1398. *
  1399. * 1. No case conversion is performed on case-insensitive file systems.
  1400. * 2. The maximum number of symbolic links is platform-independent and generally
  1401. * (much) higher than what the native [`realpath(3)`](http://man7.org/linux/man-pages/man3/realpath.3.html) implementation supports.
  1402. *
  1403. * The `callback` gets two arguments `(err, resolvedPath)`. May use `process.cwd` to resolve relative paths.
  1404. *
  1405. * Only paths that can be converted to UTF8 strings are supported.
  1406. *
  1407. * The optional `options` argument can be a string specifying an encoding, or an
  1408. * object with an `encoding` property specifying the character encoding to use for
  1409. * the path passed to the callback. If the `encoding` is set to `'buffer'`,
  1410. * the path returned will be passed as a `Buffer` object.
  1411. *
  1412. * If `path` resolves to a socket or a pipe, the function will return a system
  1413. * dependent name for that object.
  1414. * @since v0.1.31
  1415. */
  1416. export function realpath(
  1417. path: PathLike,
  1418. options: EncodingOption,
  1419. callback: (err: NodeJS.ErrnoException | null, resolvedPath: string) => void,
  1420. ): void;
  1421. /**
  1422. * Asynchronous realpath(3) - return the canonicalized absolute pathname.
  1423. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  1424. * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
  1425. */
  1426. export function realpath(
  1427. path: PathLike,
  1428. options: BufferEncodingOption,
  1429. callback: (err: NodeJS.ErrnoException | null, resolvedPath: Buffer) => void,
  1430. ): void;
  1431. /**
  1432. * Asynchronous realpath(3) - return the canonicalized absolute pathname.
  1433. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  1434. * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
  1435. */
  1436. export function realpath(
  1437. path: PathLike,
  1438. options: EncodingOption,
  1439. callback: (err: NodeJS.ErrnoException | null, resolvedPath: string | Buffer) => void,
  1440. ): void;
  1441. /**
  1442. * Asynchronous realpath(3) - return the canonicalized absolute pathname.
  1443. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  1444. */
  1445. export function realpath(
  1446. path: PathLike,
  1447. callback: (err: NodeJS.ErrnoException | null, resolvedPath: string) => void,
  1448. ): void;
  1449. export namespace realpath {
  1450. /**
  1451. * Asynchronous realpath(3) - return the canonicalized absolute pathname.
  1452. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  1453. * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
  1454. */
  1455. function __promisify__(path: PathLike, options?: EncodingOption): Promise<string>;
  1456. /**
  1457. * Asynchronous realpath(3) - return the canonicalized absolute pathname.
  1458. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  1459. * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
  1460. */
  1461. function __promisify__(path: PathLike, options: BufferEncodingOption): Promise<Buffer>;
  1462. /**
  1463. * Asynchronous realpath(3) - return the canonicalized absolute pathname.
  1464. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  1465. * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
  1466. */
  1467. function __promisify__(path: PathLike, options?: EncodingOption): Promise<string | Buffer>;
  1468. /**
  1469. * Asynchronous [`realpath(3)`](http://man7.org/linux/man-pages/man3/realpath.3.html).
  1470. *
  1471. * The `callback` gets two arguments `(err, resolvedPath)`.
  1472. *
  1473. * Only paths that can be converted to UTF8 strings are supported.
  1474. *
  1475. * The optional `options` argument can be a string specifying an encoding, or an
  1476. * object with an `encoding` property specifying the character encoding to use for
  1477. * the path passed to the callback. If the `encoding` is set to `'buffer'`,
  1478. * the path returned will be passed as a `Buffer` object.
  1479. *
  1480. * On Linux, when Node.js is linked against musl libc, the procfs file system must
  1481. * be mounted on `/proc` in order for this function to work. Glibc does not have
  1482. * this restriction.
  1483. * @since v9.2.0
  1484. */
  1485. function native(
  1486. path: PathLike,
  1487. options: EncodingOption,
  1488. callback: (err: NodeJS.ErrnoException | null, resolvedPath: string) => void,
  1489. ): void;
  1490. function native(
  1491. path: PathLike,
  1492. options: BufferEncodingOption,
  1493. callback: (err: NodeJS.ErrnoException | null, resolvedPath: Buffer) => void,
  1494. ): void;
  1495. function native(
  1496. path: PathLike,
  1497. options: EncodingOption,
  1498. callback: (err: NodeJS.ErrnoException | null, resolvedPath: string | Buffer) => void,
  1499. ): void;
  1500. function native(
  1501. path: PathLike,
  1502. callback: (err: NodeJS.ErrnoException | null, resolvedPath: string) => void,
  1503. ): void;
  1504. }
  1505. /**
  1506. * Returns the resolved pathname.
  1507. *
  1508. * For detailed information, see the documentation of the asynchronous version of
  1509. * this API: {@link realpath}.
  1510. * @since v0.1.31
  1511. */
  1512. export function realpathSync(path: PathLike, options?: EncodingOption): string;
  1513. /**
  1514. * Synchronous realpath(3) - return the canonicalized absolute pathname.
  1515. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  1516. * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
  1517. */
  1518. export function realpathSync(path: PathLike, options: BufferEncodingOption): Buffer;
  1519. /**
  1520. * Synchronous realpath(3) - return the canonicalized absolute pathname.
  1521. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  1522. * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
  1523. */
  1524. export function realpathSync(path: PathLike, options?: EncodingOption): string | Buffer;
  1525. export namespace realpathSync {
  1526. function native(path: PathLike, options?: EncodingOption): string;
  1527. function native(path: PathLike, options: BufferEncodingOption): Buffer;
  1528. function native(path: PathLike, options?: EncodingOption): string | Buffer;
  1529. }
  1530. /**
  1531. * Asynchronously removes a file or symbolic link. No arguments other than a
  1532. * possible exception are given to the completion callback.
  1533. *
  1534. * ```js
  1535. * import { unlink } from 'node:fs';
  1536. * // Assuming that 'path/file.txt' is a regular file.
  1537. * unlink('path/file.txt', (err) => {
  1538. * if (err) throw err;
  1539. * console.log('path/file.txt was deleted');
  1540. * });
  1541. * ```
  1542. *
  1543. * `fs.unlink()` will not work on a directory, empty or otherwise. To remove a
  1544. * directory, use {@link rmdir}.
  1545. *
  1546. * See the POSIX [`unlink(2)`](http://man7.org/linux/man-pages/man2/unlink.2.html) documentation for more details.
  1547. * @since v0.0.2
  1548. */
  1549. export function unlink(path: PathLike, callback: NoParamCallback): void;
  1550. export namespace unlink {
  1551. /**
  1552. * Asynchronous unlink(2) - delete a name and possibly the file it refers to.
  1553. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  1554. */
  1555. function __promisify__(path: PathLike): Promise<void>;
  1556. }
  1557. /**
  1558. * Synchronous [`unlink(2)`](http://man7.org/linux/man-pages/man2/unlink.2.html). Returns `undefined`.
  1559. * @since v0.1.21
  1560. */
  1561. export function unlinkSync(path: PathLike): void;
  1562. export interface RmDirOptions {
  1563. /**
  1564. * If an `EBUSY`, `EMFILE`, `ENFILE`, `ENOTEMPTY`, or
  1565. * `EPERM` error is encountered, Node.js will retry the operation with a linear
  1566. * backoff wait of `retryDelay` ms longer on each try. This option represents the
  1567. * number of retries. This option is ignored if the `recursive` option is not
  1568. * `true`.
  1569. * @default 0
  1570. */
  1571. maxRetries?: number | undefined;
  1572. /**
  1573. * @deprecated since v14.14.0 In future versions of Node.js and will trigger a warning
  1574. * `fs.rmdir(path, { recursive: true })` will throw if `path` does not exist or is a file.
  1575. * Use `fs.rm(path, { recursive: true, force: true })` instead.
  1576. *
  1577. * If `true`, perform a recursive directory removal. In
  1578. * recursive mode, operations are retried on failure.
  1579. * @default false
  1580. */
  1581. recursive?: boolean | undefined;
  1582. /**
  1583. * The amount of time in milliseconds to wait between retries.
  1584. * This option is ignored if the `recursive` option is not `true`.
  1585. * @default 100
  1586. */
  1587. retryDelay?: number | undefined;
  1588. }
  1589. /**
  1590. * Asynchronous [`rmdir(2)`](http://man7.org/linux/man-pages/man2/rmdir.2.html). No arguments other than a possible exception are given
  1591. * to the completion callback.
  1592. *
  1593. * Using `fs.rmdir()` on a file (not a directory) results in an `ENOENT` error on
  1594. * Windows and an `ENOTDIR` error on POSIX.
  1595. *
  1596. * To get a behavior similar to the `rm -rf` Unix command, use {@link rm} with options `{ recursive: true, force: true }`.
  1597. * @since v0.0.2
  1598. */
  1599. export function rmdir(path: PathLike, callback: NoParamCallback): void;
  1600. export function rmdir(path: PathLike, options: RmDirOptions, callback: NoParamCallback): void;
  1601. export namespace rmdir {
  1602. /**
  1603. * Asynchronous rmdir(2) - delete a directory.
  1604. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  1605. */
  1606. function __promisify__(path: PathLike, options?: RmDirOptions): Promise<void>;
  1607. }
  1608. /**
  1609. * Synchronous [`rmdir(2)`](http://man7.org/linux/man-pages/man2/rmdir.2.html). Returns `undefined`.
  1610. *
  1611. * Using `fs.rmdirSync()` on a file (not a directory) results in an `ENOENT` error
  1612. * on Windows and an `ENOTDIR` error on POSIX.
  1613. *
  1614. * To get a behavior similar to the `rm -rf` Unix command, use {@link rmSync} with options `{ recursive: true, force: true }`.
  1615. * @since v0.1.21
  1616. */
  1617. export function rmdirSync(path: PathLike, options?: RmDirOptions): void;
  1618. export interface RmOptions {
  1619. /**
  1620. * When `true`, exceptions will be ignored if `path` does not exist.
  1621. * @default false
  1622. */
  1623. force?: boolean | undefined;
  1624. /**
  1625. * If an `EBUSY`, `EMFILE`, `ENFILE`, `ENOTEMPTY`, or
  1626. * `EPERM` error is encountered, Node.js will retry the operation with a linear
  1627. * backoff wait of `retryDelay` ms longer on each try. This option represents the
  1628. * number of retries. This option is ignored if the `recursive` option is not
  1629. * `true`.
  1630. * @default 0
  1631. */
  1632. maxRetries?: number | undefined;
  1633. /**
  1634. * If `true`, perform a recursive directory removal. In
  1635. * recursive mode, operations are retried on failure.
  1636. * @default false
  1637. */
  1638. recursive?: boolean | undefined;
  1639. /**
  1640. * The amount of time in milliseconds to wait between retries.
  1641. * This option is ignored if the `recursive` option is not `true`.
  1642. * @default 100
  1643. */
  1644. retryDelay?: number | undefined;
  1645. }
  1646. /**
  1647. * Asynchronously removes files and directories (modeled on the standard POSIX `rm` utility). No arguments other than a possible exception are given to the
  1648. * completion callback.
  1649. * @since v14.14.0
  1650. */
  1651. export function rm(path: PathLike, callback: NoParamCallback): void;
  1652. export function rm(path: PathLike, options: RmOptions, callback: NoParamCallback): void;
  1653. export namespace rm {
  1654. /**
  1655. * Asynchronously removes files and directories (modeled on the standard POSIX `rm` utility).
  1656. */
  1657. function __promisify__(path: PathLike, options?: RmOptions): Promise<void>;
  1658. }
  1659. /**
  1660. * Synchronously removes files and directories (modeled on the standard POSIX `rm` utility). Returns `undefined`.
  1661. * @since v14.14.0
  1662. */
  1663. export function rmSync(path: PathLike, options?: RmOptions): void;
  1664. export interface MakeDirectoryOptions {
  1665. /**
  1666. * Indicates whether parent folders should be created.
  1667. * If a folder was created, the path to the first created folder will be returned.
  1668. * @default false
  1669. */
  1670. recursive?: boolean | undefined;
  1671. /**
  1672. * A file mode. If a string is passed, it is parsed as an octal integer. If not specified
  1673. * @default 0o777
  1674. */
  1675. mode?: Mode | undefined;
  1676. }
  1677. /**
  1678. * Asynchronously creates a directory.
  1679. *
  1680. * The callback is given a possible exception and, if `recursive` is `true`, the
  1681. * first directory path created, `(err[, path])`.`path` can still be `undefined` when `recursive` is `true`, if no directory was
  1682. * created (for instance, if it was previously created).
  1683. *
  1684. * The optional `options` argument can be an integer specifying `mode` (permission
  1685. * and sticky bits), or an object with a `mode` property and a `recursive` property indicating whether parent directories should be created. Calling `fs.mkdir()` when `path` is a directory that
  1686. * exists results in an error only
  1687. * when `recursive` is false. If `recursive` is false and the directory exists,
  1688. * an `EEXIST` error occurs.
  1689. *
  1690. * ```js
  1691. * import { mkdir } from 'node:fs';
  1692. *
  1693. * // Create ./tmp/a/apple, regardless of whether ./tmp and ./tmp/a exist.
  1694. * mkdir('./tmp/a/apple', { recursive: true }, (err) => {
  1695. * if (err) throw err;
  1696. * });
  1697. * ```
  1698. *
  1699. * On Windows, using `fs.mkdir()` on the root directory even with recursion will
  1700. * result in an error:
  1701. *
  1702. * ```js
  1703. * import { mkdir } from 'node:fs';
  1704. *
  1705. * mkdir('/', { recursive: true }, (err) => {
  1706. * // => [Error: EPERM: operation not permitted, mkdir 'C:\']
  1707. * });
  1708. * ```
  1709. *
  1710. * See the POSIX [`mkdir(2)`](http://man7.org/linux/man-pages/man2/mkdir.2.html) documentation for more details.
  1711. * @since v0.1.8
  1712. */
  1713. export function mkdir(
  1714. path: PathLike,
  1715. options: MakeDirectoryOptions & {
  1716. recursive: true;
  1717. },
  1718. callback: (err: NodeJS.ErrnoException | null, path?: string) => void,
  1719. ): void;
  1720. /**
  1721. * Asynchronous mkdir(2) - create a directory.
  1722. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  1723. * @param options Either the file mode, or an object optionally specifying the file mode and whether parent folders
  1724. * should be created. If a string is passed, it is parsed as an octal integer. If not specified, defaults to `0o777`.
  1725. */
  1726. export function mkdir(
  1727. path: PathLike,
  1728. options:
  1729. | Mode
  1730. | (MakeDirectoryOptions & {
  1731. recursive?: false | undefined;
  1732. })
  1733. | null
  1734. | undefined,
  1735. callback: NoParamCallback,
  1736. ): void;
  1737. /**
  1738. * Asynchronous mkdir(2) - create a directory.
  1739. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  1740. * @param options Either the file mode, or an object optionally specifying the file mode and whether parent folders
  1741. * should be created. If a string is passed, it is parsed as an octal integer. If not specified, defaults to `0o777`.
  1742. */
  1743. export function mkdir(
  1744. path: PathLike,
  1745. options: Mode | MakeDirectoryOptions | null | undefined,
  1746. callback: (err: NodeJS.ErrnoException | null, path?: string) => void,
  1747. ): void;
  1748. /**
  1749. * Asynchronous mkdir(2) - create a directory with a mode of `0o777`.
  1750. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  1751. */
  1752. export function mkdir(path: PathLike, callback: NoParamCallback): void;
  1753. export namespace mkdir {
  1754. /**
  1755. * Asynchronous mkdir(2) - create a directory.
  1756. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  1757. * @param options Either the file mode, or an object optionally specifying the file mode and whether parent folders
  1758. * should be created. If a string is passed, it is parsed as an octal integer. If not specified, defaults to `0o777`.
  1759. */
  1760. function __promisify__(
  1761. path: PathLike,
  1762. options: MakeDirectoryOptions & {
  1763. recursive: true;
  1764. },
  1765. ): Promise<string | undefined>;
  1766. /**
  1767. * Asynchronous mkdir(2) - create a directory.
  1768. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  1769. * @param options Either the file mode, or an object optionally specifying the file mode and whether parent folders
  1770. * should be created. If a string is passed, it is parsed as an octal integer. If not specified, defaults to `0o777`.
  1771. */
  1772. function __promisify__(
  1773. path: PathLike,
  1774. options?:
  1775. | Mode
  1776. | (MakeDirectoryOptions & {
  1777. recursive?: false | undefined;
  1778. })
  1779. | null,
  1780. ): Promise<void>;
  1781. /**
  1782. * Asynchronous mkdir(2) - create a directory.
  1783. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  1784. * @param options Either the file mode, or an object optionally specifying the file mode and whether parent folders
  1785. * should be created. If a string is passed, it is parsed as an octal integer. If not specified, defaults to `0o777`.
  1786. */
  1787. function __promisify__(
  1788. path: PathLike,
  1789. options?: Mode | MakeDirectoryOptions | null,
  1790. ): Promise<string | undefined>;
  1791. }
  1792. /**
  1793. * Synchronously creates a directory. Returns `undefined`, or if `recursive` is `true`, the first directory path created.
  1794. * This is the synchronous version of {@link mkdir}.
  1795. *
  1796. * See the POSIX [`mkdir(2)`](http://man7.org/linux/man-pages/man2/mkdir.2.html) documentation for more details.
  1797. * @since v0.1.21
  1798. */
  1799. export function mkdirSync(
  1800. path: PathLike,
  1801. options: MakeDirectoryOptions & {
  1802. recursive: true;
  1803. },
  1804. ): string | undefined;
  1805. /**
  1806. * Synchronous mkdir(2) - create a directory.
  1807. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  1808. * @param options Either the file mode, or an object optionally specifying the file mode and whether parent folders
  1809. * should be created. If a string is passed, it is parsed as an octal integer. If not specified, defaults to `0o777`.
  1810. */
  1811. export function mkdirSync(
  1812. path: PathLike,
  1813. options?:
  1814. | Mode
  1815. | (MakeDirectoryOptions & {
  1816. recursive?: false | undefined;
  1817. })
  1818. | null,
  1819. ): void;
  1820. /**
  1821. * Synchronous mkdir(2) - create a directory.
  1822. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  1823. * @param options Either the file mode, or an object optionally specifying the file mode and whether parent folders
  1824. * should be created. If a string is passed, it is parsed as an octal integer. If not specified, defaults to `0o777`.
  1825. */
  1826. export function mkdirSync(path: PathLike, options?: Mode | MakeDirectoryOptions | null): string | undefined;
  1827. /**
  1828. * Creates a unique temporary directory.
  1829. *
  1830. * Generates six random characters to be appended behind a required `prefix` to create a unique temporary directory. Due to platform
  1831. * inconsistencies, avoid trailing `X` characters in `prefix`. Some platforms,
  1832. * notably the BSDs, can return more than six random characters, and replace
  1833. * trailing `X` characters in `prefix` with random characters.
  1834. *
  1835. * The created directory path is passed as a string to the callback's second
  1836. * parameter.
  1837. *
  1838. * The optional `options` argument can be a string specifying an encoding, or an
  1839. * object with an `encoding` property specifying the character encoding to use.
  1840. *
  1841. * ```js
  1842. * import { mkdtemp } from 'node:fs';
  1843. * import { join } from 'node:path';
  1844. * import { tmpdir } from 'node:os';
  1845. *
  1846. * mkdtemp(join(tmpdir(), 'foo-'), (err, directory) => {
  1847. * if (err) throw err;
  1848. * console.log(directory);
  1849. * // Prints: /tmp/foo-itXde2 or C:\Users\...\AppData\Local\Temp\foo-itXde2
  1850. * });
  1851. * ```
  1852. *
  1853. * The `fs.mkdtemp()` method will append the six randomly selected characters
  1854. * directly to the `prefix` string. For instance, given a directory `/tmp`, if the
  1855. * intention is to create a temporary directory _within_`/tmp`, the `prefix`must end with a trailing platform-specific path separator
  1856. * (`import { sep } from 'node:path'`).
  1857. *
  1858. * ```js
  1859. * import { tmpdir } from 'node:os';
  1860. * import { mkdtemp } from 'node:fs';
  1861. *
  1862. * // The parent directory for the new temporary directory
  1863. * const tmpDir = tmpdir();
  1864. *
  1865. * // This method is *INCORRECT*:
  1866. * mkdtemp(tmpDir, (err, directory) => {
  1867. * if (err) throw err;
  1868. * console.log(directory);
  1869. * // Will print something similar to `/tmpabc123`.
  1870. * // A new temporary directory is created at the file system root
  1871. * // rather than *within* the /tmp directory.
  1872. * });
  1873. *
  1874. * // This method is *CORRECT*:
  1875. * import { sep } from 'node:path';
  1876. * mkdtemp(`${tmpDir}${sep}`, (err, directory) => {
  1877. * if (err) throw err;
  1878. * console.log(directory);
  1879. * // Will print something similar to `/tmp/abc123`.
  1880. * // A new temporary directory is created within
  1881. * // the /tmp directory.
  1882. * });
  1883. * ```
  1884. * @since v5.10.0
  1885. */
  1886. export function mkdtemp(
  1887. prefix: string,
  1888. options: EncodingOption,
  1889. callback: (err: NodeJS.ErrnoException | null, folder: string) => void,
  1890. ): void;
  1891. /**
  1892. * Asynchronously creates a unique temporary directory.
  1893. * Generates six random characters to be appended behind a required prefix to create a unique temporary directory.
  1894. * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
  1895. */
  1896. export function mkdtemp(
  1897. prefix: string,
  1898. options:
  1899. | "buffer"
  1900. | {
  1901. encoding: "buffer";
  1902. },
  1903. callback: (err: NodeJS.ErrnoException | null, folder: Buffer) => void,
  1904. ): void;
  1905. /**
  1906. * Asynchronously creates a unique temporary directory.
  1907. * Generates six random characters to be appended behind a required prefix to create a unique temporary directory.
  1908. * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
  1909. */
  1910. export function mkdtemp(
  1911. prefix: string,
  1912. options: EncodingOption,
  1913. callback: (err: NodeJS.ErrnoException | null, folder: string | Buffer) => void,
  1914. ): void;
  1915. /**
  1916. * Asynchronously creates a unique temporary directory.
  1917. * Generates six random characters to be appended behind a required prefix to create a unique temporary directory.
  1918. */
  1919. export function mkdtemp(
  1920. prefix: string,
  1921. callback: (err: NodeJS.ErrnoException | null, folder: string) => void,
  1922. ): void;
  1923. export namespace mkdtemp {
  1924. /**
  1925. * Asynchronously creates a unique temporary directory.
  1926. * Generates six random characters to be appended behind a required prefix to create a unique temporary directory.
  1927. * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
  1928. */
  1929. function __promisify__(prefix: string, options?: EncodingOption): Promise<string>;
  1930. /**
  1931. * Asynchronously creates a unique temporary directory.
  1932. * Generates six random characters to be appended behind a required prefix to create a unique temporary directory.
  1933. * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
  1934. */
  1935. function __promisify__(prefix: string, options: BufferEncodingOption): Promise<Buffer>;
  1936. /**
  1937. * Asynchronously creates a unique temporary directory.
  1938. * Generates six random characters to be appended behind a required prefix to create a unique temporary directory.
  1939. * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
  1940. */
  1941. function __promisify__(prefix: string, options?: EncodingOption): Promise<string | Buffer>;
  1942. }
  1943. /**
  1944. * Returns the created directory path.
  1945. *
  1946. * For detailed information, see the documentation of the asynchronous version of
  1947. * this API: {@link mkdtemp}.
  1948. *
  1949. * The optional `options` argument can be a string specifying an encoding, or an
  1950. * object with an `encoding` property specifying the character encoding to use.
  1951. * @since v5.10.0
  1952. */
  1953. export function mkdtempSync(prefix: string, options?: EncodingOption): string;
  1954. /**
  1955. * Synchronously creates a unique temporary directory.
  1956. * Generates six random characters to be appended behind a required prefix to create a unique temporary directory.
  1957. * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
  1958. */
  1959. export function mkdtempSync(prefix: string, options: BufferEncodingOption): Buffer;
  1960. /**
  1961. * Synchronously creates a unique temporary directory.
  1962. * Generates six random characters to be appended behind a required prefix to create a unique temporary directory.
  1963. * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
  1964. */
  1965. export function mkdtempSync(prefix: string, options?: EncodingOption): string | Buffer;
  1966. export interface DisposableTempDir extends AsyncDisposable {
  1967. /**
  1968. * The path of the created directory.
  1969. */
  1970. path: string;
  1971. /**
  1972. * A function which removes the created directory.
  1973. */
  1974. remove(): Promise<void>;
  1975. /**
  1976. * The same as `remove`.
  1977. */
  1978. [Symbol.asyncDispose](): Promise<void>;
  1979. }
  1980. /**
  1981. * Returns a disposable object whose `path` property holds the created directory
  1982. * path. When the object is disposed, the directory and its contents will be
  1983. * removed if it still exists. If the directory cannot be deleted, disposal will
  1984. * throw an error. The object has a `remove()` method which will perform the same
  1985. * task.
  1986. *
  1987. * <!-- TODO: link MDN docs for disposables once https://github.com/mdn/content/pull/38027 lands -->
  1988. *
  1989. * For detailed information, see the documentation of `fs.mkdtemp()`.
  1990. *
  1991. * There is no callback-based version of this API because it is designed for use
  1992. * with the `using` syntax.
  1993. *
  1994. * The optional `options` argument can be a string specifying an encoding, or an
  1995. * object with an `encoding` property specifying the character encoding to use.
  1996. * @since v24.4.0
  1997. */
  1998. export function mkdtempDisposableSync(prefix: string, options?: EncodingOption): DisposableTempDir;
  1999. /**
  2000. * Reads the contents of a directory. The callback gets two arguments `(err, files)` where `files` is an array of the names of the files in the directory excluding `'.'` and `'..'`.
  2001. *
  2002. * See the POSIX [`readdir(3)`](http://man7.org/linux/man-pages/man3/readdir.3.html) documentation for more details.
  2003. *
  2004. * The optional `options` argument can be a string specifying an encoding, or an
  2005. * object with an `encoding` property specifying the character encoding to use for
  2006. * the filenames passed to the callback. If the `encoding` is set to `'buffer'`,
  2007. * the filenames returned will be passed as `Buffer` objects.
  2008. *
  2009. * If `options.withFileTypes` is set to `true`, the `files` array will contain `fs.Dirent` objects.
  2010. * @since v0.1.8
  2011. */
  2012. export function readdir(
  2013. path: PathLike,
  2014. options:
  2015. | {
  2016. encoding: BufferEncoding | null;
  2017. withFileTypes?: false | undefined;
  2018. recursive?: boolean | undefined;
  2019. }
  2020. | BufferEncoding
  2021. | undefined
  2022. | null,
  2023. callback: (err: NodeJS.ErrnoException | null, files: string[]) => void,
  2024. ): void;
  2025. /**
  2026. * Asynchronous readdir(3) - read a directory.
  2027. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  2028. * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
  2029. */
  2030. export function readdir(
  2031. path: PathLike,
  2032. options:
  2033. | {
  2034. encoding: "buffer";
  2035. withFileTypes?: false | undefined;
  2036. recursive?: boolean | undefined;
  2037. }
  2038. | "buffer",
  2039. callback: (err: NodeJS.ErrnoException | null, files: Buffer[]) => void,
  2040. ): void;
  2041. /**
  2042. * Asynchronous readdir(3) - read a directory.
  2043. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  2044. * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
  2045. */
  2046. export function readdir(
  2047. path: PathLike,
  2048. options:
  2049. | (ObjectEncodingOptions & {
  2050. withFileTypes?: false | undefined;
  2051. recursive?: boolean | undefined;
  2052. })
  2053. | BufferEncoding
  2054. | undefined
  2055. | null,
  2056. callback: (err: NodeJS.ErrnoException | null, files: string[] | Buffer[]) => void,
  2057. ): void;
  2058. /**
  2059. * Asynchronous readdir(3) - read a directory.
  2060. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  2061. */
  2062. export function readdir(
  2063. path: PathLike,
  2064. callback: (err: NodeJS.ErrnoException | null, files: string[]) => void,
  2065. ): void;
  2066. /**
  2067. * Asynchronous readdir(3) - read a directory.
  2068. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  2069. * @param options If called with `withFileTypes: true` the result data will be an array of Dirent.
  2070. */
  2071. export function readdir(
  2072. path: PathLike,
  2073. options: ObjectEncodingOptions & {
  2074. withFileTypes: true;
  2075. recursive?: boolean | undefined;
  2076. },
  2077. callback: (err: NodeJS.ErrnoException | null, files: Dirent[]) => void,
  2078. ): void;
  2079. /**
  2080. * Asynchronous readdir(3) - read a directory.
  2081. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  2082. * @param options Must include `withFileTypes: true` and `encoding: 'buffer'`.
  2083. */
  2084. export function readdir(
  2085. path: PathLike,
  2086. options: {
  2087. encoding: "buffer";
  2088. withFileTypes: true;
  2089. recursive?: boolean | undefined;
  2090. },
  2091. callback: (err: NodeJS.ErrnoException | null, files: Dirent<Buffer>[]) => void,
  2092. ): void;
  2093. export namespace readdir {
  2094. /**
  2095. * Asynchronous readdir(3) - read a directory.
  2096. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  2097. * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
  2098. */
  2099. function __promisify__(
  2100. path: PathLike,
  2101. options?:
  2102. | {
  2103. encoding: BufferEncoding | null;
  2104. withFileTypes?: false | undefined;
  2105. recursive?: boolean | undefined;
  2106. }
  2107. | BufferEncoding
  2108. | null,
  2109. ): Promise<string[]>;
  2110. /**
  2111. * Asynchronous readdir(3) - read a directory.
  2112. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  2113. * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
  2114. */
  2115. function __promisify__(
  2116. path: PathLike,
  2117. options:
  2118. | "buffer"
  2119. | {
  2120. encoding: "buffer";
  2121. withFileTypes?: false | undefined;
  2122. recursive?: boolean | undefined;
  2123. },
  2124. ): Promise<Buffer[]>;
  2125. /**
  2126. * Asynchronous readdir(3) - read a directory.
  2127. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  2128. * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
  2129. */
  2130. function __promisify__(
  2131. path: PathLike,
  2132. options?:
  2133. | (ObjectEncodingOptions & {
  2134. withFileTypes?: false | undefined;
  2135. recursive?: boolean | undefined;
  2136. })
  2137. | BufferEncoding
  2138. | null,
  2139. ): Promise<string[] | Buffer[]>;
  2140. /**
  2141. * Asynchronous readdir(3) - read a directory.
  2142. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  2143. * @param options If called with `withFileTypes: true` the result data will be an array of Dirent
  2144. */
  2145. function __promisify__(
  2146. path: PathLike,
  2147. options: ObjectEncodingOptions & {
  2148. withFileTypes: true;
  2149. recursive?: boolean | undefined;
  2150. },
  2151. ): Promise<Dirent[]>;
  2152. /**
  2153. * Asynchronous readdir(3) - read a directory.
  2154. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  2155. * @param options Must include `withFileTypes: true` and `encoding: 'buffer'`.
  2156. */
  2157. function __promisify__(
  2158. path: PathLike,
  2159. options: {
  2160. encoding: "buffer";
  2161. withFileTypes: true;
  2162. recursive?: boolean | undefined;
  2163. },
  2164. ): Promise<Dirent<Buffer>[]>;
  2165. }
  2166. /**
  2167. * Reads the contents of the directory.
  2168. *
  2169. * See the POSIX [`readdir(3)`](http://man7.org/linux/man-pages/man3/readdir.3.html) documentation for more details.
  2170. *
  2171. * The optional `options` argument can be a string specifying an encoding, or an
  2172. * object with an `encoding` property specifying the character encoding to use for
  2173. * the filenames returned. If the `encoding` is set to `'buffer'`,
  2174. * the filenames returned will be passed as `Buffer` objects.
  2175. *
  2176. * If `options.withFileTypes` is set to `true`, the result will contain `fs.Dirent` objects.
  2177. * @since v0.1.21
  2178. */
  2179. export function readdirSync(
  2180. path: PathLike,
  2181. options?:
  2182. | {
  2183. encoding: BufferEncoding | null;
  2184. withFileTypes?: false | undefined;
  2185. recursive?: boolean | undefined;
  2186. }
  2187. | BufferEncoding
  2188. | null,
  2189. ): string[];
  2190. /**
  2191. * Synchronous readdir(3) - read a directory.
  2192. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  2193. * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
  2194. */
  2195. export function readdirSync(
  2196. path: PathLike,
  2197. options:
  2198. | {
  2199. encoding: "buffer";
  2200. withFileTypes?: false | undefined;
  2201. recursive?: boolean | undefined;
  2202. }
  2203. | "buffer",
  2204. ): Buffer[];
  2205. /**
  2206. * Synchronous readdir(3) - read a directory.
  2207. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  2208. * @param options The encoding (or an object specifying the encoding), used as the encoding of the result. If not provided, `'utf8'` is used.
  2209. */
  2210. export function readdirSync(
  2211. path: PathLike,
  2212. options?:
  2213. | (ObjectEncodingOptions & {
  2214. withFileTypes?: false | undefined;
  2215. recursive?: boolean | undefined;
  2216. })
  2217. | BufferEncoding
  2218. | null,
  2219. ): string[] | Buffer[];
  2220. /**
  2221. * Synchronous readdir(3) - read a directory.
  2222. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  2223. * @param options If called with `withFileTypes: true` the result data will be an array of Dirent.
  2224. */
  2225. export function readdirSync(
  2226. path: PathLike,
  2227. options: ObjectEncodingOptions & {
  2228. withFileTypes: true;
  2229. recursive?: boolean | undefined;
  2230. },
  2231. ): Dirent[];
  2232. /**
  2233. * Synchronous readdir(3) - read a directory.
  2234. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  2235. * @param options Must include `withFileTypes: true` and `encoding: 'buffer'`.
  2236. */
  2237. export function readdirSync(
  2238. path: PathLike,
  2239. options: {
  2240. encoding: "buffer";
  2241. withFileTypes: true;
  2242. recursive?: boolean | undefined;
  2243. },
  2244. ): Dirent<Buffer>[];
  2245. /**
  2246. * Closes the file descriptor. No arguments other than a possible exception are
  2247. * given to the completion callback.
  2248. *
  2249. * Calling `fs.close()` on any file descriptor (`fd`) that is currently in use
  2250. * through any other `fs` operation may lead to undefined behavior.
  2251. *
  2252. * See the POSIX [`close(2)`](http://man7.org/linux/man-pages/man2/close.2.html) documentation for more detail.
  2253. * @since v0.0.2
  2254. */
  2255. export function close(fd: number, callback?: NoParamCallback): void;
  2256. export namespace close {
  2257. /**
  2258. * Asynchronous close(2) - close a file descriptor.
  2259. * @param fd A file descriptor.
  2260. */
  2261. function __promisify__(fd: number): Promise<void>;
  2262. }
  2263. /**
  2264. * Closes the file descriptor. Returns `undefined`.
  2265. *
  2266. * Calling `fs.closeSync()` on any file descriptor (`fd`) that is currently in use
  2267. * through any other `fs` operation may lead to undefined behavior.
  2268. *
  2269. * See the POSIX [`close(2)`](http://man7.org/linux/man-pages/man2/close.2.html) documentation for more detail.
  2270. * @since v0.1.21
  2271. */
  2272. export function closeSync(fd: number): void;
  2273. /**
  2274. * Asynchronous file open. See the POSIX [`open(2)`](http://man7.org/linux/man-pages/man2/open.2.html) documentation for more details.
  2275. *
  2276. * `mode` sets the file mode (permission and sticky bits), but only if the file was
  2277. * created. On Windows, only the write permission can be manipulated; see {@link chmod}.
  2278. *
  2279. * The callback gets two arguments `(err, fd)`.
  2280. *
  2281. * Some characters (`< > : " / \ | ? *`) are reserved under Windows as documented
  2282. * by [Naming Files, Paths, and Namespaces](https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file). Under NTFS, if the filename contains
  2283. * a colon, Node.js will open a file system stream, as described by [this MSDN page](https://docs.microsoft.com/en-us/windows/desktop/FileIO/using-streams).
  2284. *
  2285. * Functions based on `fs.open()` exhibit this behavior as well:`fs.writeFile()`, `fs.readFile()`, etc.
  2286. * @since v0.0.2
  2287. * @param [flags='r'] See `support of file system `flags``.
  2288. * @param [mode=0o666]
  2289. */
  2290. export function open(
  2291. path: PathLike,
  2292. flags: OpenMode | undefined,
  2293. mode: Mode | undefined | null,
  2294. callback: (err: NodeJS.ErrnoException | null, fd: number) => void,
  2295. ): void;
  2296. /**
  2297. * Asynchronous open(2) - open and possibly create a file. If the file is created, its mode will be `0o666`.
  2298. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  2299. * @param [flags='r'] See `support of file system `flags``.
  2300. */
  2301. export function open(
  2302. path: PathLike,
  2303. flags: OpenMode | undefined,
  2304. callback: (err: NodeJS.ErrnoException | null, fd: number) => void,
  2305. ): void;
  2306. /**
  2307. * Asynchronous open(2) - open and possibly create a file. If the file is created, its mode will be `0o666`.
  2308. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  2309. */
  2310. export function open(path: PathLike, callback: (err: NodeJS.ErrnoException | null, fd: number) => void): void;
  2311. export namespace open {
  2312. /**
  2313. * Asynchronous open(2) - open and possibly create a file.
  2314. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  2315. * @param mode A file mode. If a string is passed, it is parsed as an octal integer. If not supplied, defaults to `0o666`.
  2316. */
  2317. function __promisify__(path: PathLike, flags: OpenMode, mode?: Mode | null): Promise<number>;
  2318. }
  2319. /**
  2320. * Returns an integer representing the file descriptor.
  2321. *
  2322. * For detailed information, see the documentation of the asynchronous version of
  2323. * this API: {@link open}.
  2324. * @since v0.1.21
  2325. * @param [flags='r']
  2326. * @param [mode=0o666]
  2327. */
  2328. export function openSync(path: PathLike, flags: OpenMode, mode?: Mode | null): number;
  2329. /**
  2330. * Change the file system timestamps of the object referenced by `path`.
  2331. *
  2332. * The `atime` and `mtime` arguments follow these rules:
  2333. *
  2334. * * Values can be either numbers representing Unix epoch time in seconds, `Date`s, or a numeric string like `'123456789.0'`.
  2335. * * If the value can not be converted to a number, or is `NaN`, `Infinity`, or `-Infinity`, an `Error` will be thrown.
  2336. * @since v0.4.2
  2337. */
  2338. export function utimes(path: PathLike, atime: TimeLike, mtime: TimeLike, callback: NoParamCallback): void;
  2339. export namespace utimes {
  2340. /**
  2341. * Asynchronously change file timestamps of the file referenced by the supplied path.
  2342. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  2343. * @param atime The last access time. If a string is provided, it will be coerced to number.
  2344. * @param mtime The last modified time. If a string is provided, it will be coerced to number.
  2345. */
  2346. function __promisify__(path: PathLike, atime: TimeLike, mtime: TimeLike): Promise<void>;
  2347. }
  2348. /**
  2349. * Returns `undefined`.
  2350. *
  2351. * For detailed information, see the documentation of the asynchronous version of
  2352. * this API: {@link utimes}.
  2353. * @since v0.4.2
  2354. */
  2355. export function utimesSync(path: PathLike, atime: TimeLike, mtime: TimeLike): void;
  2356. /**
  2357. * Change the file system timestamps of the object referenced by the supplied file
  2358. * descriptor. See {@link utimes}.
  2359. * @since v0.4.2
  2360. */
  2361. export function futimes(fd: number, atime: TimeLike, mtime: TimeLike, callback: NoParamCallback): void;
  2362. export namespace futimes {
  2363. /**
  2364. * Asynchronously change file timestamps of the file referenced by the supplied file descriptor.
  2365. * @param fd A file descriptor.
  2366. * @param atime The last access time. If a string is provided, it will be coerced to number.
  2367. * @param mtime The last modified time. If a string is provided, it will be coerced to number.
  2368. */
  2369. function __promisify__(fd: number, atime: TimeLike, mtime: TimeLike): Promise<void>;
  2370. }
  2371. /**
  2372. * Synchronous version of {@link futimes}. Returns `undefined`.
  2373. * @since v0.4.2
  2374. */
  2375. export function futimesSync(fd: number, atime: TimeLike, mtime: TimeLike): void;
  2376. /**
  2377. * Request that all data for the open file descriptor is flushed to the storage
  2378. * device. The specific implementation is operating system and device specific.
  2379. * Refer to the POSIX [`fsync(2)`](http://man7.org/linux/man-pages/man2/fsync.2.html) documentation for more detail. No arguments other
  2380. * than a possible exception are given to the completion callback.
  2381. * @since v0.1.96
  2382. */
  2383. export function fsync(fd: number, callback: NoParamCallback): void;
  2384. export namespace fsync {
  2385. /**
  2386. * Asynchronous fsync(2) - synchronize a file's in-core state with the underlying storage device.
  2387. * @param fd A file descriptor.
  2388. */
  2389. function __promisify__(fd: number): Promise<void>;
  2390. }
  2391. /**
  2392. * Request that all data for the open file descriptor is flushed to the storage
  2393. * device. The specific implementation is operating system and device specific.
  2394. * Refer to the POSIX [`fsync(2)`](http://man7.org/linux/man-pages/man2/fsync.2.html) documentation for more detail. Returns `undefined`.
  2395. * @since v0.1.96
  2396. */
  2397. export function fsyncSync(fd: number): void;
  2398. export interface WriteOptions {
  2399. /**
  2400. * @default 0
  2401. */
  2402. offset?: number | undefined;
  2403. /**
  2404. * @default `buffer.byteLength - offset`
  2405. */
  2406. length?: number | undefined;
  2407. /**
  2408. * @default null
  2409. */
  2410. position?: number | undefined | null;
  2411. }
  2412. /**
  2413. * Write `buffer` to the file specified by `fd`.
  2414. *
  2415. * `offset` determines the part of the buffer to be written, and `length` is
  2416. * an integer specifying the number of bytes to write.
  2417. *
  2418. * `position` refers to the offset from the beginning of the file where this data
  2419. * should be written. If `typeof position !== 'number'`, the data will be written
  2420. * at the current position. See [`pwrite(2)`](http://man7.org/linux/man-pages/man2/pwrite.2.html).
  2421. *
  2422. * The callback will be given three arguments `(err, bytesWritten, buffer)` where `bytesWritten` specifies how many _bytes_ were written from `buffer`.
  2423. *
  2424. * If this method is invoked as its `util.promisify()` ed version, it returns
  2425. * a promise for an `Object` with `bytesWritten` and `buffer` properties.
  2426. *
  2427. * It is unsafe to use `fs.write()` multiple times on the same file without waiting
  2428. * for the callback. For this scenario, {@link createWriteStream} is
  2429. * recommended.
  2430. *
  2431. * On Linux, positional writes don't work when the file is opened in append mode.
  2432. * The kernel ignores the position argument and always appends the data to
  2433. * the end of the file.
  2434. * @since v0.0.2
  2435. * @param [offset=0]
  2436. * @param [length=buffer.byteLength - offset]
  2437. * @param [position='null']
  2438. */
  2439. export function write<TBuffer extends NodeJS.ArrayBufferView>(
  2440. fd: number,
  2441. buffer: TBuffer,
  2442. offset: number | undefined | null,
  2443. length: number | undefined | null,
  2444. position: number | undefined | null,
  2445. callback: (err: NodeJS.ErrnoException | null, written: number, buffer: TBuffer) => void,
  2446. ): void;
  2447. /**
  2448. * Asynchronously writes `buffer` to the file referenced by the supplied file descriptor.
  2449. * @param fd A file descriptor.
  2450. * @param offset The part of the buffer to be written. If not supplied, defaults to `0`.
  2451. * @param length The number of bytes to write. If not supplied, defaults to `buffer.length - offset`.
  2452. */
  2453. export function write<TBuffer extends NodeJS.ArrayBufferView>(
  2454. fd: number,
  2455. buffer: TBuffer,
  2456. offset: number | undefined | null,
  2457. length: number | undefined | null,
  2458. callback: (err: NodeJS.ErrnoException | null, written: number, buffer: TBuffer) => void,
  2459. ): void;
  2460. /**
  2461. * Asynchronously writes `buffer` to the file referenced by the supplied file descriptor.
  2462. * @param fd A file descriptor.
  2463. * @param offset The part of the buffer to be written. If not supplied, defaults to `0`.
  2464. */
  2465. export function write<TBuffer extends NodeJS.ArrayBufferView>(
  2466. fd: number,
  2467. buffer: TBuffer,
  2468. offset: number | undefined | null,
  2469. callback: (err: NodeJS.ErrnoException | null, written: number, buffer: TBuffer) => void,
  2470. ): void;
  2471. /**
  2472. * Asynchronously writes `buffer` to the file referenced by the supplied file descriptor.
  2473. * @param fd A file descriptor.
  2474. */
  2475. export function write<TBuffer extends NodeJS.ArrayBufferView>(
  2476. fd: number,
  2477. buffer: TBuffer,
  2478. callback: (err: NodeJS.ErrnoException | null, written: number, buffer: TBuffer) => void,
  2479. ): void;
  2480. /**
  2481. * Asynchronously writes `buffer` to the file referenced by the supplied file descriptor.
  2482. * @param fd A file descriptor.
  2483. * @param options An object with the following properties:
  2484. * * `offset` The part of the buffer to be written. If not supplied, defaults to `0`.
  2485. * * `length` The number of bytes to write. If not supplied, defaults to `buffer.length - offset`.
  2486. * * `position` The offset from the beginning of the file where this data should be written. If not supplied, defaults to the current position.
  2487. */
  2488. export function write<TBuffer extends NodeJS.ArrayBufferView>(
  2489. fd: number,
  2490. buffer: TBuffer,
  2491. options: WriteOptions,
  2492. callback: (err: NodeJS.ErrnoException | null, written: number, buffer: TBuffer) => void,
  2493. ): void;
  2494. /**
  2495. * Asynchronously writes `string` to the file referenced by the supplied file descriptor.
  2496. * @param fd A file descriptor.
  2497. * @param string A string to write.
  2498. * @param position The offset from the beginning of the file where this data should be written. If not supplied, defaults to the current position.
  2499. * @param encoding The expected string encoding.
  2500. */
  2501. export function write(
  2502. fd: number,
  2503. string: string,
  2504. position: number | undefined | null,
  2505. encoding: BufferEncoding | undefined | null,
  2506. callback: (err: NodeJS.ErrnoException | null, written: number, str: string) => void,
  2507. ): void;
  2508. /**
  2509. * Asynchronously writes `string` to the file referenced by the supplied file descriptor.
  2510. * @param fd A file descriptor.
  2511. * @param string A string to write.
  2512. * @param position The offset from the beginning of the file where this data should be written. If not supplied, defaults to the current position.
  2513. */
  2514. export function write(
  2515. fd: number,
  2516. string: string,
  2517. position: number | undefined | null,
  2518. callback: (err: NodeJS.ErrnoException | null, written: number, str: string) => void,
  2519. ): void;
  2520. /**
  2521. * Asynchronously writes `string` to the file referenced by the supplied file descriptor.
  2522. * @param fd A file descriptor.
  2523. * @param string A string to write.
  2524. */
  2525. export function write(
  2526. fd: number,
  2527. string: string,
  2528. callback: (err: NodeJS.ErrnoException | null, written: number, str: string) => void,
  2529. ): void;
  2530. export namespace write {
  2531. /**
  2532. * Asynchronously writes `buffer` to the file referenced by the supplied file descriptor.
  2533. * @param fd A file descriptor.
  2534. * @param offset The part of the buffer to be written. If not supplied, defaults to `0`.
  2535. * @param length The number of bytes to write. If not supplied, defaults to `buffer.length - offset`.
  2536. * @param position The offset from the beginning of the file where this data should be written. If not supplied, defaults to the current position.
  2537. */
  2538. function __promisify__<TBuffer extends NodeJS.ArrayBufferView>(
  2539. fd: number,
  2540. buffer?: TBuffer,
  2541. offset?: number,
  2542. length?: number,
  2543. position?: number | null,
  2544. ): Promise<{
  2545. bytesWritten: number;
  2546. buffer: TBuffer;
  2547. }>;
  2548. /**
  2549. * Asynchronously writes `buffer` to the file referenced by the supplied file descriptor.
  2550. * @param fd A file descriptor.
  2551. * @param options An object with the following properties:
  2552. * * `offset` The part of the buffer to be written. If not supplied, defaults to `0`.
  2553. * * `length` The number of bytes to write. If not supplied, defaults to `buffer.length - offset`.
  2554. * * `position` The offset from the beginning of the file where this data should be written. If not supplied, defaults to the current position.
  2555. */
  2556. function __promisify__<TBuffer extends NodeJS.ArrayBufferView>(
  2557. fd: number,
  2558. buffer?: TBuffer,
  2559. options?: WriteOptions,
  2560. ): Promise<{
  2561. bytesWritten: number;
  2562. buffer: TBuffer;
  2563. }>;
  2564. /**
  2565. * Asynchronously writes `string` to the file referenced by the supplied file descriptor.
  2566. * @param fd A file descriptor.
  2567. * @param string A string to write.
  2568. * @param position The offset from the beginning of the file where this data should be written. If not supplied, defaults to the current position.
  2569. * @param encoding The expected string encoding.
  2570. */
  2571. function __promisify__(
  2572. fd: number,
  2573. string: string,
  2574. position?: number | null,
  2575. encoding?: BufferEncoding | null,
  2576. ): Promise<{
  2577. bytesWritten: number;
  2578. buffer: string;
  2579. }>;
  2580. }
  2581. /**
  2582. * For detailed information, see the documentation of the asynchronous version of
  2583. * this API: {@link write}.
  2584. * @since v0.1.21
  2585. * @param [offset=0]
  2586. * @param [length=buffer.byteLength - offset]
  2587. * @param [position='null']
  2588. * @return The number of bytes written.
  2589. */
  2590. export function writeSync(
  2591. fd: number,
  2592. buffer: NodeJS.ArrayBufferView,
  2593. offset?: number | null,
  2594. length?: number | null,
  2595. position?: number | null,
  2596. ): number;
  2597. /**
  2598. * Synchronously writes `string` to the file referenced by the supplied file descriptor, returning the number of bytes written.
  2599. * @param fd A file descriptor.
  2600. * @param string A string to write.
  2601. * @param position The offset from the beginning of the file where this data should be written. If not supplied, defaults to the current position.
  2602. * @param encoding The expected string encoding.
  2603. */
  2604. export function writeSync(
  2605. fd: number,
  2606. string: string,
  2607. position?: number | null,
  2608. encoding?: BufferEncoding | null,
  2609. ): number;
  2610. export type ReadPosition = number | bigint;
  2611. export interface ReadSyncOptions {
  2612. /**
  2613. * @default 0
  2614. */
  2615. offset?: number | undefined;
  2616. /**
  2617. * @default `length of buffer`
  2618. */
  2619. length?: number | undefined;
  2620. /**
  2621. * @default null
  2622. */
  2623. position?: ReadPosition | null | undefined;
  2624. }
  2625. export interface ReadAsyncOptions<TBuffer extends NodeJS.ArrayBufferView> extends ReadSyncOptions {
  2626. buffer?: TBuffer;
  2627. }
  2628. /**
  2629. * Read data from the file specified by `fd`.
  2630. *
  2631. * The callback is given the three arguments, `(err, bytesRead, buffer)`.
  2632. *
  2633. * If the file is not modified concurrently, the end-of-file is reached when the
  2634. * number of bytes read is zero.
  2635. *
  2636. * If this method is invoked as its `util.promisify()` ed version, it returns
  2637. * a promise for an `Object` with `bytesRead` and `buffer` properties.
  2638. * @since v0.0.2
  2639. * @param buffer The buffer that the data will be written to.
  2640. * @param offset The position in `buffer` to write the data to.
  2641. * @param length The number of bytes to read.
  2642. * @param position Specifies where to begin reading from in the file. If `position` is `null` or `-1 `, data will be read from the current file position, and the file position will be updated. If
  2643. * `position` is an integer, the file position will be unchanged.
  2644. */
  2645. export function read<TBuffer extends NodeJS.ArrayBufferView>(
  2646. fd: number,
  2647. buffer: TBuffer,
  2648. offset: number,
  2649. length: number,
  2650. position: ReadPosition | null,
  2651. callback: (err: NodeJS.ErrnoException | null, bytesRead: number, buffer: TBuffer) => void,
  2652. ): void;
  2653. /**
  2654. * Similar to the above `fs.read` function, this version takes an optional `options` object.
  2655. * If not otherwise specified in an `options` object,
  2656. * `buffer` defaults to `Buffer.alloc(16384)`,
  2657. * `offset` defaults to `0`,
  2658. * `length` defaults to `buffer.byteLength`, `- offset` as of Node 17.6.0
  2659. * `position` defaults to `null`
  2660. * @since v12.17.0, 13.11.0
  2661. */
  2662. export function read<TBuffer extends NodeJS.ArrayBufferView>(
  2663. fd: number,
  2664. options: ReadAsyncOptions<TBuffer>,
  2665. callback: (err: NodeJS.ErrnoException | null, bytesRead: number, buffer: TBuffer) => void,
  2666. ): void;
  2667. export function read<TBuffer extends NodeJS.ArrayBufferView>(
  2668. fd: number,
  2669. buffer: TBuffer,
  2670. options: ReadSyncOptions,
  2671. callback: (err: NodeJS.ErrnoException | null, bytesRead: number, buffer: TBuffer) => void,
  2672. ): void;
  2673. export function read<TBuffer extends NodeJS.ArrayBufferView>(
  2674. fd: number,
  2675. buffer: TBuffer,
  2676. callback: (err: NodeJS.ErrnoException | null, bytesRead: number, buffer: TBuffer) => void,
  2677. ): void;
  2678. export function read(
  2679. fd: number,
  2680. callback: (err: NodeJS.ErrnoException | null, bytesRead: number, buffer: NodeJS.ArrayBufferView) => void,
  2681. ): void;
  2682. export namespace read {
  2683. /**
  2684. * @param fd A file descriptor.
  2685. * @param buffer The buffer that the data will be written to.
  2686. * @param offset The offset in the buffer at which to start writing.
  2687. * @param length The number of bytes to read.
  2688. * @param position The offset from the beginning of the file from which data should be read. If `null`, data will be read from the current position.
  2689. */
  2690. function __promisify__<TBuffer extends NodeJS.ArrayBufferView>(
  2691. fd: number,
  2692. buffer: TBuffer,
  2693. offset: number,
  2694. length: number,
  2695. position: ReadPosition | null,
  2696. ): Promise<{
  2697. bytesRead: number;
  2698. buffer: TBuffer;
  2699. }>;
  2700. function __promisify__<TBuffer extends NodeJS.ArrayBufferView>(
  2701. fd: number,
  2702. options: ReadAsyncOptions<TBuffer>,
  2703. ): Promise<{
  2704. bytesRead: number;
  2705. buffer: TBuffer;
  2706. }>;
  2707. function __promisify__(fd: number): Promise<{
  2708. bytesRead: number;
  2709. buffer: NodeJS.ArrayBufferView;
  2710. }>;
  2711. }
  2712. /**
  2713. * Returns the number of `bytesRead`.
  2714. *
  2715. * For detailed information, see the documentation of the asynchronous version of
  2716. * this API: {@link read}.
  2717. * @since v0.1.21
  2718. * @param [position='null']
  2719. */
  2720. export function readSync(
  2721. fd: number,
  2722. buffer: NodeJS.ArrayBufferView,
  2723. offset: number,
  2724. length: number,
  2725. position: ReadPosition | null,
  2726. ): number;
  2727. /**
  2728. * Similar to the above `fs.readSync` function, this version takes an optional `options` object.
  2729. * If no `options` object is specified, it will default with the above values.
  2730. */
  2731. export function readSync(fd: number, buffer: NodeJS.ArrayBufferView, opts?: ReadSyncOptions): number;
  2732. /**
  2733. * Asynchronously reads the entire contents of a file.
  2734. *
  2735. * ```js
  2736. * import { readFile } from 'node:fs';
  2737. *
  2738. * readFile('/etc/passwd', (err, data) => {
  2739. * if (err) throw err;
  2740. * console.log(data);
  2741. * });
  2742. * ```
  2743. *
  2744. * The callback is passed two arguments `(err, data)`, where `data` is the
  2745. * contents of the file.
  2746. *
  2747. * If no encoding is specified, then the raw buffer is returned.
  2748. *
  2749. * If `options` is a string, then it specifies the encoding:
  2750. *
  2751. * ```js
  2752. * import { readFile } from 'node:fs';
  2753. *
  2754. * readFile('/etc/passwd', 'utf8', callback);
  2755. * ```
  2756. *
  2757. * When the path is a directory, the behavior of `fs.readFile()` and {@link readFileSync} is platform-specific. On macOS, Linux, and Windows, an
  2758. * error will be returned. On FreeBSD, a representation of the directory's contents
  2759. * will be returned.
  2760. *
  2761. * ```js
  2762. * import { readFile } from 'node:fs';
  2763. *
  2764. * // macOS, Linux, and Windows
  2765. * readFile('<directory>', (err, data) => {
  2766. * // => [Error: EISDIR: illegal operation on a directory, read <directory>]
  2767. * });
  2768. *
  2769. * // FreeBSD
  2770. * readFile('<directory>', (err, data) => {
  2771. * // => null, <data>
  2772. * });
  2773. * ```
  2774. *
  2775. * It is possible to abort an ongoing request using an `AbortSignal`. If a
  2776. * request is aborted the callback is called with an `AbortError`:
  2777. *
  2778. * ```js
  2779. * import { readFile } from 'node:fs';
  2780. *
  2781. * const controller = new AbortController();
  2782. * const signal = controller.signal;
  2783. * readFile(fileInfo[0].name, { signal }, (err, buf) => {
  2784. * // ...
  2785. * });
  2786. * // When you want to abort the request
  2787. * controller.abort();
  2788. * ```
  2789. *
  2790. * The `fs.readFile()` function buffers the entire file. To minimize memory costs,
  2791. * when possible prefer streaming via `fs.createReadStream()`.
  2792. *
  2793. * Aborting an ongoing request does not abort individual operating
  2794. * system requests but rather the internal buffering `fs.readFile` performs.
  2795. * @since v0.1.29
  2796. * @param path filename or file descriptor
  2797. */
  2798. export function readFile(
  2799. path: PathOrFileDescriptor,
  2800. options:
  2801. | ({
  2802. encoding?: null | undefined;
  2803. flag?: string | undefined;
  2804. } & Abortable)
  2805. | undefined
  2806. | null,
  2807. callback: (err: NodeJS.ErrnoException | null, data: NonSharedBuffer) => void,
  2808. ): void;
  2809. /**
  2810. * Asynchronously reads the entire contents of a file.
  2811. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  2812. * If a file descriptor is provided, the underlying file will _not_ be closed automatically.
  2813. * @param options Either the encoding for the result, or an object that contains the encoding and an optional flag.
  2814. * If a flag is not provided, it defaults to `'r'`.
  2815. */
  2816. export function readFile(
  2817. path: PathOrFileDescriptor,
  2818. options:
  2819. | ({
  2820. encoding: BufferEncoding;
  2821. flag?: string | undefined;
  2822. } & Abortable)
  2823. | BufferEncoding,
  2824. callback: (err: NodeJS.ErrnoException | null, data: string) => void,
  2825. ): void;
  2826. /**
  2827. * Asynchronously reads the entire contents of a file.
  2828. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  2829. * If a file descriptor is provided, the underlying file will _not_ be closed automatically.
  2830. * @param options Either the encoding for the result, or an object that contains the encoding and an optional flag.
  2831. * If a flag is not provided, it defaults to `'r'`.
  2832. */
  2833. export function readFile(
  2834. path: PathOrFileDescriptor,
  2835. options:
  2836. | (ObjectEncodingOptions & {
  2837. flag?: string | undefined;
  2838. } & Abortable)
  2839. | BufferEncoding
  2840. | undefined
  2841. | null,
  2842. callback: (err: NodeJS.ErrnoException | null, data: string | NonSharedBuffer) => void,
  2843. ): void;
  2844. /**
  2845. * Asynchronously reads the entire contents of a file.
  2846. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  2847. * If a file descriptor is provided, the underlying file will _not_ be closed automatically.
  2848. */
  2849. export function readFile(
  2850. path: PathOrFileDescriptor,
  2851. callback: (err: NodeJS.ErrnoException | null, data: NonSharedBuffer) => void,
  2852. ): void;
  2853. export namespace readFile {
  2854. /**
  2855. * Asynchronously reads the entire contents of a file.
  2856. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  2857. * If a file descriptor is provided, the underlying file will _not_ be closed automatically.
  2858. * @param options An object that may contain an optional flag.
  2859. * If a flag is not provided, it defaults to `'r'`.
  2860. */
  2861. function __promisify__(
  2862. path: PathOrFileDescriptor,
  2863. options?: {
  2864. encoding?: null | undefined;
  2865. flag?: string | undefined;
  2866. } | null,
  2867. ): Promise<NonSharedBuffer>;
  2868. /**
  2869. * Asynchronously reads the entire contents of a file.
  2870. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  2871. * URL support is _experimental_.
  2872. * If a file descriptor is provided, the underlying file will _not_ be closed automatically.
  2873. * @param options Either the encoding for the result, or an object that contains the encoding and an optional flag.
  2874. * If a flag is not provided, it defaults to `'r'`.
  2875. */
  2876. function __promisify__(
  2877. path: PathOrFileDescriptor,
  2878. options:
  2879. | {
  2880. encoding: BufferEncoding;
  2881. flag?: string | undefined;
  2882. }
  2883. | BufferEncoding,
  2884. ): Promise<string>;
  2885. /**
  2886. * Asynchronously reads the entire contents of a file.
  2887. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  2888. * URL support is _experimental_.
  2889. * If a file descriptor is provided, the underlying file will _not_ be closed automatically.
  2890. * @param options Either the encoding for the result, or an object that contains the encoding and an optional flag.
  2891. * If a flag is not provided, it defaults to `'r'`.
  2892. */
  2893. function __promisify__(
  2894. path: PathOrFileDescriptor,
  2895. options?:
  2896. | (ObjectEncodingOptions & {
  2897. flag?: string | undefined;
  2898. })
  2899. | BufferEncoding
  2900. | null,
  2901. ): Promise<string | NonSharedBuffer>;
  2902. }
  2903. /**
  2904. * Returns the contents of the `path`.
  2905. *
  2906. * For detailed information, see the documentation of the asynchronous version of
  2907. * this API: {@link readFile}.
  2908. *
  2909. * If the `encoding` option is specified then this function returns a
  2910. * string. Otherwise it returns a buffer.
  2911. *
  2912. * Similar to {@link readFile}, when the path is a directory, the behavior of `fs.readFileSync()` is platform-specific.
  2913. *
  2914. * ```js
  2915. * import { readFileSync } from 'node:fs';
  2916. *
  2917. * // macOS, Linux, and Windows
  2918. * readFileSync('<directory>');
  2919. * // => [Error: EISDIR: illegal operation on a directory, read <directory>]
  2920. *
  2921. * // FreeBSD
  2922. * readFileSync('<directory>'); // => <data>
  2923. * ```
  2924. * @since v0.1.8
  2925. * @param path filename or file descriptor
  2926. */
  2927. export function readFileSync(
  2928. path: PathOrFileDescriptor,
  2929. options?: {
  2930. encoding?: null | undefined;
  2931. flag?: string | undefined;
  2932. } | null,
  2933. ): NonSharedBuffer;
  2934. /**
  2935. * Synchronously reads the entire contents of a file.
  2936. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  2937. * If a file descriptor is provided, the underlying file will _not_ be closed automatically.
  2938. * @param options Either the encoding for the result, or an object that contains the encoding and an optional flag.
  2939. * If a flag is not provided, it defaults to `'r'`.
  2940. */
  2941. export function readFileSync(
  2942. path: PathOrFileDescriptor,
  2943. options:
  2944. | {
  2945. encoding: BufferEncoding;
  2946. flag?: string | undefined;
  2947. }
  2948. | BufferEncoding,
  2949. ): string;
  2950. /**
  2951. * Synchronously reads the entire contents of a file.
  2952. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  2953. * If a file descriptor is provided, the underlying file will _not_ be closed automatically.
  2954. * @param options Either the encoding for the result, or an object that contains the encoding and an optional flag.
  2955. * If a flag is not provided, it defaults to `'r'`.
  2956. */
  2957. export function readFileSync(
  2958. path: PathOrFileDescriptor,
  2959. options?:
  2960. | (ObjectEncodingOptions & {
  2961. flag?: string | undefined;
  2962. })
  2963. | BufferEncoding
  2964. | null,
  2965. ): string | NonSharedBuffer;
  2966. export type WriteFileOptions =
  2967. | (
  2968. & ObjectEncodingOptions
  2969. & Abortable
  2970. & {
  2971. mode?: Mode | undefined;
  2972. flag?: string | undefined;
  2973. flush?: boolean | undefined;
  2974. }
  2975. )
  2976. | BufferEncoding
  2977. | null;
  2978. /**
  2979. * When `file` is a filename, asynchronously writes data to the file, replacing the
  2980. * file if it already exists. `data` can be a string or a buffer.
  2981. *
  2982. * When `file` is a file descriptor, the behavior is similar to calling `fs.write()` directly (which is recommended). See the notes below on using
  2983. * a file descriptor.
  2984. *
  2985. * The `encoding` option is ignored if `data` is a buffer.
  2986. *
  2987. * The `mode` option only affects the newly created file. See {@link open} for more details.
  2988. *
  2989. * ```js
  2990. * import { writeFile } from 'node:fs';
  2991. * import { Buffer } from 'node:buffer';
  2992. *
  2993. * const data = new Uint8Array(Buffer.from('Hello Node.js'));
  2994. * writeFile('message.txt', data, (err) => {
  2995. * if (err) throw err;
  2996. * console.log('The file has been saved!');
  2997. * });
  2998. * ```
  2999. *
  3000. * If `options` is a string, then it specifies the encoding:
  3001. *
  3002. * ```js
  3003. * import { writeFile } from 'node:fs';
  3004. *
  3005. * writeFile('message.txt', 'Hello Node.js', 'utf8', callback);
  3006. * ```
  3007. *
  3008. * It is unsafe to use `fs.writeFile()` multiple times on the same file without
  3009. * waiting for the callback. For this scenario, {@link createWriteStream} is
  3010. * recommended.
  3011. *
  3012. * Similarly to `fs.readFile` \- `fs.writeFile` is a convenience method that
  3013. * performs multiple `write` calls internally to write the buffer passed to it.
  3014. * For performance sensitive code consider using {@link createWriteStream}.
  3015. *
  3016. * It is possible to use an `AbortSignal` to cancel an `fs.writeFile()`.
  3017. * Cancelation is "best effort", and some amount of data is likely still
  3018. * to be written.
  3019. *
  3020. * ```js
  3021. * import { writeFile } from 'node:fs';
  3022. * import { Buffer } from 'node:buffer';
  3023. *
  3024. * const controller = new AbortController();
  3025. * const { signal } = controller;
  3026. * const data = new Uint8Array(Buffer.from('Hello Node.js'));
  3027. * writeFile('message.txt', data, { signal }, (err) => {
  3028. * // When a request is aborted - the callback is called with an AbortError
  3029. * });
  3030. * // When the request should be aborted
  3031. * controller.abort();
  3032. * ```
  3033. *
  3034. * Aborting an ongoing request does not abort individual operating
  3035. * system requests but rather the internal buffering `fs.writeFile` performs.
  3036. * @since v0.1.29
  3037. * @param file filename or file descriptor
  3038. */
  3039. export function writeFile(
  3040. file: PathOrFileDescriptor,
  3041. data: string | NodeJS.ArrayBufferView,
  3042. options: WriteFileOptions,
  3043. callback: NoParamCallback,
  3044. ): void;
  3045. /**
  3046. * Asynchronously writes data to a file, replacing the file if it already exists.
  3047. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  3048. * If a file descriptor is provided, the underlying file will _not_ be closed automatically.
  3049. * @param data The data to write. If something other than a Buffer or Uint8Array is provided, the value is coerced to a string.
  3050. */
  3051. export function writeFile(
  3052. path: PathOrFileDescriptor,
  3053. data: string | NodeJS.ArrayBufferView,
  3054. callback: NoParamCallback,
  3055. ): void;
  3056. export namespace writeFile {
  3057. /**
  3058. * Asynchronously writes data to a file, replacing the file if it already exists.
  3059. * @param path A path to a file. If a URL is provided, it must use the `file:` protocol.
  3060. * URL support is _experimental_.
  3061. * If a file descriptor is provided, the underlying file will _not_ be closed automatically.
  3062. * @param data The data to write. If something other than a Buffer or Uint8Array is provided, the value is coerced to a string.
  3063. * @param options Either the encoding for the file, or an object optionally specifying the encoding, file mode, and flag.
  3064. * If `encoding` is not supplied, the default of `'utf8'` is used.
  3065. * If `mode` is not supplied, the default of `0o666` is used.
  3066. * If `mode` is a string, it is parsed as an octal integer.
  3067. * If `flag` is not supplied, the default of `'w'` is used.
  3068. */
  3069. function __promisify__(
  3070. path: PathOrFileDescriptor,
  3071. data: string | NodeJS.ArrayBufferView,
  3072. options?: WriteFileOptions,
  3073. ): Promise<void>;
  3074. }
  3075. /**
  3076. * Returns `undefined`.
  3077. *
  3078. * The `mode` option only affects the newly created file. See {@link open} for more details.
  3079. *
  3080. * For detailed information, see the documentation of the asynchronous version of
  3081. * this API: {@link writeFile}.
  3082. * @since v0.1.29
  3083. * @param file filename or file descriptor
  3084. */
  3085. export function writeFileSync(
  3086. file: PathOrFileDescriptor,
  3087. data: string | NodeJS.ArrayBufferView,
  3088. options?: WriteFileOptions,
  3089. ): void;
  3090. /**
  3091. * Asynchronously append data to a file, creating the file if it does not yet
  3092. * exist. `data` can be a string or a `Buffer`.
  3093. *
  3094. * The `mode` option only affects the newly created file. See {@link open} for more details.
  3095. *
  3096. * ```js
  3097. * import { appendFile } from 'node:fs';
  3098. *
  3099. * appendFile('message.txt', 'data to append', (err) => {
  3100. * if (err) throw err;
  3101. * console.log('The "data to append" was appended to file!');
  3102. * });
  3103. * ```
  3104. *
  3105. * If `options` is a string, then it specifies the encoding:
  3106. *
  3107. * ```js
  3108. * import { appendFile } from 'node:fs';
  3109. *
  3110. * appendFile('message.txt', 'data to append', 'utf8', callback);
  3111. * ```
  3112. *
  3113. * The `path` may be specified as a numeric file descriptor that has been opened
  3114. * for appending (using `fs.open()` or `fs.openSync()`). The file descriptor will
  3115. * not be closed automatically.
  3116. *
  3117. * ```js
  3118. * import { open, close, appendFile } from 'node:fs';
  3119. *
  3120. * function closeFd(fd) {
  3121. * close(fd, (err) => {
  3122. * if (err) throw err;
  3123. * });
  3124. * }
  3125. *
  3126. * open('message.txt', 'a', (err, fd) => {
  3127. * if (err) throw err;
  3128. *
  3129. * try {
  3130. * appendFile(fd, 'data to append', 'utf8', (err) => {
  3131. * closeFd(fd);
  3132. * if (err) throw err;
  3133. * });
  3134. * } catch (err) {
  3135. * closeFd(fd);
  3136. * throw err;
  3137. * }
  3138. * });
  3139. * ```
  3140. * @since v0.6.7
  3141. * @param path filename or file descriptor
  3142. */
  3143. export function appendFile(
  3144. path: PathOrFileDescriptor,
  3145. data: string | Uint8Array,
  3146. options: WriteFileOptions,
  3147. callback: NoParamCallback,
  3148. ): void;
  3149. /**
  3150. * Asynchronously append data to a file, creating the file if it does not exist.
  3151. * @param file A path to a file. If a URL is provided, it must use the `file:` protocol.
  3152. * If a file descriptor is provided, the underlying file will _not_ be closed automatically.
  3153. * @param data The data to write. If something other than a Buffer or Uint8Array is provided, the value is coerced to a string.
  3154. */
  3155. export function appendFile(file: PathOrFileDescriptor, data: string | Uint8Array, callback: NoParamCallback): void;
  3156. export namespace appendFile {
  3157. /**
  3158. * Asynchronously append data to a file, creating the file if it does not exist.
  3159. * @param file A path to a file. If a URL is provided, it must use the `file:` protocol.
  3160. * URL support is _experimental_.
  3161. * If a file descriptor is provided, the underlying file will _not_ be closed automatically.
  3162. * @param data The data to write. If something other than a Buffer or Uint8Array is provided, the value is coerced to a string.
  3163. * @param options Either the encoding for the file, or an object optionally specifying the encoding, file mode, and flag.
  3164. * If `encoding` is not supplied, the default of `'utf8'` is used.
  3165. * If `mode` is not supplied, the default of `0o666` is used.
  3166. * If `mode` is a string, it is parsed as an octal integer.
  3167. * If `flag` is not supplied, the default of `'a'` is used.
  3168. */
  3169. function __promisify__(
  3170. file: PathOrFileDescriptor,
  3171. data: string | Uint8Array,
  3172. options?: WriteFileOptions,
  3173. ): Promise<void>;
  3174. }
  3175. /**
  3176. * Synchronously append data to a file, creating the file if it does not yet
  3177. * exist. `data` can be a string or a `Buffer`.
  3178. *
  3179. * The `mode` option only affects the newly created file. See {@link open} for more details.
  3180. *
  3181. * ```js
  3182. * import { appendFileSync } from 'node:fs';
  3183. *
  3184. * try {
  3185. * appendFileSync('message.txt', 'data to append');
  3186. * console.log('The "data to append" was appended to file!');
  3187. * } catch (err) {
  3188. * // Handle the error
  3189. * }
  3190. * ```
  3191. *
  3192. * If `options` is a string, then it specifies the encoding:
  3193. *
  3194. * ```js
  3195. * import { appendFileSync } from 'node:fs';
  3196. *
  3197. * appendFileSync('message.txt', 'data to append', 'utf8');
  3198. * ```
  3199. *
  3200. * The `path` may be specified as a numeric file descriptor that has been opened
  3201. * for appending (using `fs.open()` or `fs.openSync()`). The file descriptor will
  3202. * not be closed automatically.
  3203. *
  3204. * ```js
  3205. * import { openSync, closeSync, appendFileSync } from 'node:fs';
  3206. *
  3207. * let fd;
  3208. *
  3209. * try {
  3210. * fd = openSync('message.txt', 'a');
  3211. * appendFileSync(fd, 'data to append', 'utf8');
  3212. * } catch (err) {
  3213. * // Handle the error
  3214. * } finally {
  3215. * if (fd !== undefined)
  3216. * closeSync(fd);
  3217. * }
  3218. * ```
  3219. * @since v0.6.7
  3220. * @param path filename or file descriptor
  3221. */
  3222. export function appendFileSync(
  3223. path: PathOrFileDescriptor,
  3224. data: string | Uint8Array,
  3225. options?: WriteFileOptions,
  3226. ): void;
  3227. /**
  3228. * Watch for changes on `filename`. The callback `listener` will be called each
  3229. * time the file is accessed.
  3230. *
  3231. * The `options` argument may be omitted. If provided, it should be an object. The `options` object may contain a boolean named `persistent` that indicates
  3232. * whether the process should continue to run as long as files are being watched.
  3233. * The `options` object may specify an `interval` property indicating how often the
  3234. * target should be polled in milliseconds.
  3235. *
  3236. * The `listener` gets two arguments the current stat object and the previous
  3237. * stat object:
  3238. *
  3239. * ```js
  3240. * import { watchFile } from 'node:fs';
  3241. *
  3242. * watchFile('message.text', (curr, prev) => {
  3243. * console.log(`the current mtime is: ${curr.mtime}`);
  3244. * console.log(`the previous mtime was: ${prev.mtime}`);
  3245. * });
  3246. * ```
  3247. *
  3248. * These stat objects are instances of `fs.Stat`. If the `bigint` option is `true`,
  3249. * the numeric values in these objects are specified as `BigInt`s.
  3250. *
  3251. * To be notified when the file was modified, not just accessed, it is necessary
  3252. * to compare `curr.mtimeMs` and `prev.mtimeMs`.
  3253. *
  3254. * When an `fs.watchFile` operation results in an `ENOENT` error, it
  3255. * will invoke the listener once, with all the fields zeroed (or, for dates, the
  3256. * Unix Epoch). If the file is created later on, the listener will be called
  3257. * again, with the latest stat objects. This is a change in functionality since
  3258. * v0.10.
  3259. *
  3260. * Using {@link watch} is more efficient than `fs.watchFile` and `fs.unwatchFile`. `fs.watch` should be used instead of `fs.watchFile` and `fs.unwatchFile` when possible.
  3261. *
  3262. * When a file being watched by `fs.watchFile()` disappears and reappears,
  3263. * then the contents of `previous` in the second callback event (the file's
  3264. * reappearance) will be the same as the contents of `previous` in the first
  3265. * callback event (its disappearance).
  3266. *
  3267. * This happens when:
  3268. *
  3269. * * the file is deleted, followed by a restore
  3270. * * the file is renamed and then renamed a second time back to its original name
  3271. * @since v0.1.31
  3272. */
  3273. export interface WatchFileOptions {
  3274. bigint?: boolean | undefined;
  3275. persistent?: boolean | undefined;
  3276. interval?: number | undefined;
  3277. }
  3278. /**
  3279. * Watch for changes on `filename`. The callback `listener` will be called each
  3280. * time the file is accessed.
  3281. *
  3282. * The `options` argument may be omitted. If provided, it should be an object. The `options` object may contain a boolean named `persistent` that indicates
  3283. * whether the process should continue to run as long as files are being watched.
  3284. * The `options` object may specify an `interval` property indicating how often the
  3285. * target should be polled in milliseconds.
  3286. *
  3287. * The `listener` gets two arguments the current stat object and the previous
  3288. * stat object:
  3289. *
  3290. * ```js
  3291. * import { watchFile } from 'node:fs';
  3292. *
  3293. * watchFile('message.text', (curr, prev) => {
  3294. * console.log(`the current mtime is: ${curr.mtime}`);
  3295. * console.log(`the previous mtime was: ${prev.mtime}`);
  3296. * });
  3297. * ```
  3298. *
  3299. * These stat objects are instances of `fs.Stat`. If the `bigint` option is `true`,
  3300. * the numeric values in these objects are specified as `BigInt`s.
  3301. *
  3302. * To be notified when the file was modified, not just accessed, it is necessary
  3303. * to compare `curr.mtimeMs` and `prev.mtimeMs`.
  3304. *
  3305. * When an `fs.watchFile` operation results in an `ENOENT` error, it
  3306. * will invoke the listener once, with all the fields zeroed (or, for dates, the
  3307. * Unix Epoch). If the file is created later on, the listener will be called
  3308. * again, with the latest stat objects. This is a change in functionality since
  3309. * v0.10.
  3310. *
  3311. * Using {@link watch} is more efficient than `fs.watchFile` and `fs.unwatchFile`. `fs.watch` should be used instead of `fs.watchFile` and `fs.unwatchFile` when possible.
  3312. *
  3313. * When a file being watched by `fs.watchFile()` disappears and reappears,
  3314. * then the contents of `previous` in the second callback event (the file's
  3315. * reappearance) will be the same as the contents of `previous` in the first
  3316. * callback event (its disappearance).
  3317. *
  3318. * This happens when:
  3319. *
  3320. * * the file is deleted, followed by a restore
  3321. * * the file is renamed and then renamed a second time back to its original name
  3322. * @since v0.1.31
  3323. */
  3324. export function watchFile(
  3325. filename: PathLike,
  3326. options:
  3327. | (WatchFileOptions & {
  3328. bigint?: false | undefined;
  3329. })
  3330. | undefined,
  3331. listener: StatsListener,
  3332. ): StatWatcher;
  3333. export function watchFile(
  3334. filename: PathLike,
  3335. options:
  3336. | (WatchFileOptions & {
  3337. bigint: true;
  3338. })
  3339. | undefined,
  3340. listener: BigIntStatsListener,
  3341. ): StatWatcher;
  3342. /**
  3343. * Watch for changes on `filename`. The callback `listener` will be called each time the file is accessed.
  3344. * @param filename A path to a file or directory. If a URL is provided, it must use the `file:` protocol.
  3345. */
  3346. export function watchFile(filename: PathLike, listener: StatsListener): StatWatcher;
  3347. /**
  3348. * Stop watching for changes on `filename`. If `listener` is specified, only that
  3349. * particular listener is removed. Otherwise, _all_ listeners are removed,
  3350. * effectively stopping watching of `filename`.
  3351. *
  3352. * Calling `fs.unwatchFile()` with a filename that is not being watched is a
  3353. * no-op, not an error.
  3354. *
  3355. * Using {@link watch} is more efficient than `fs.watchFile()` and `fs.unwatchFile()`. `fs.watch()` should be used instead of `fs.watchFile()` and `fs.unwatchFile()` when possible.
  3356. * @since v0.1.31
  3357. * @param listener Optional, a listener previously attached using `fs.watchFile()`
  3358. */
  3359. export function unwatchFile(filename: PathLike, listener?: StatsListener): void;
  3360. export function unwatchFile(filename: PathLike, listener?: BigIntStatsListener): void;
  3361. export interface WatchOptions extends Abortable {
  3362. encoding?: BufferEncoding | "buffer" | undefined;
  3363. persistent?: boolean | undefined;
  3364. recursive?: boolean | undefined;
  3365. }
  3366. export interface WatchOptionsWithBufferEncoding extends WatchOptions {
  3367. encoding: "buffer";
  3368. }
  3369. export interface WatchOptionsWithStringEncoding extends WatchOptions {
  3370. encoding?: BufferEncoding | undefined;
  3371. }
  3372. export type WatchEventType = "rename" | "change";
  3373. export type WatchListener<T> = (event: WatchEventType, filename: T | null) => void;
  3374. export type StatsListener = (curr: Stats, prev: Stats) => void;
  3375. export type BigIntStatsListener = (curr: BigIntStats, prev: BigIntStats) => void;
  3376. /**
  3377. * Watch for changes on `filename`, where `filename` is either a file or a
  3378. * directory.
  3379. *
  3380. * The second argument is optional. If `options` is provided as a string, it
  3381. * specifies the `encoding`. Otherwise `options` should be passed as an object.
  3382. *
  3383. * The listener callback gets two arguments `(eventType, filename)`. `eventType`is either `'rename'` or `'change'`, and `filename` is the name of the file
  3384. * which triggered the event.
  3385. *
  3386. * On most platforms, `'rename'` is emitted whenever a filename appears or
  3387. * disappears in the directory.
  3388. *
  3389. * The listener callback is attached to the `'change'` event fired by `fs.FSWatcher`, but it is not the same thing as the `'change'` value of `eventType`.
  3390. *
  3391. * If a `signal` is passed, aborting the corresponding AbortController will close
  3392. * the returned `fs.FSWatcher`.
  3393. * @since v0.5.10
  3394. * @param listener
  3395. */
  3396. export function watch(
  3397. filename: PathLike,
  3398. options?: WatchOptionsWithStringEncoding | BufferEncoding | null,
  3399. listener?: WatchListener<string>,
  3400. ): FSWatcher;
  3401. export function watch(
  3402. filename: PathLike,
  3403. options: WatchOptionsWithBufferEncoding | "buffer",
  3404. listener: WatchListener<Buffer>,
  3405. ): FSWatcher;
  3406. export function watch(
  3407. filename: PathLike,
  3408. options: WatchOptions | BufferEncoding | "buffer" | null,
  3409. listener: WatchListener<string | Buffer>,
  3410. ): FSWatcher;
  3411. export function watch(filename: PathLike, listener: WatchListener<string>): FSWatcher;
  3412. /**
  3413. * Test whether or not the given path exists by checking with the file system.
  3414. * Then call the `callback` argument with either true or false:
  3415. *
  3416. * ```js
  3417. * import { exists } from 'node:fs';
  3418. *
  3419. * exists('/etc/passwd', (e) => {
  3420. * console.log(e ? 'it exists' : 'no passwd!');
  3421. * });
  3422. * ```
  3423. *
  3424. * **The parameters for this callback are not consistent with other Node.js**
  3425. * **callbacks.** Normally, the first parameter to a Node.js callback is an `err` parameter, optionally followed by other parameters. The `fs.exists()` callback
  3426. * has only one boolean parameter. This is one reason `fs.access()` is recommended
  3427. * instead of `fs.exists()`.
  3428. *
  3429. * Using `fs.exists()` to check for the existence of a file before calling `fs.open()`, `fs.readFile()`, or `fs.writeFile()` is not recommended. Doing
  3430. * so introduces a race condition, since other processes may change the file's
  3431. * state between the two calls. Instead, user code should open/read/write the
  3432. * file directly and handle the error raised if the file does not exist.
  3433. *
  3434. * **write (NOT RECOMMENDED)**
  3435. *
  3436. * ```js
  3437. * import { exists, open, close } from 'node:fs';
  3438. *
  3439. * exists('myfile', (e) => {
  3440. * if (e) {
  3441. * console.error('myfile already exists');
  3442. * } else {
  3443. * open('myfile', 'wx', (err, fd) => {
  3444. * if (err) throw err;
  3445. *
  3446. * try {
  3447. * writeMyData(fd);
  3448. * } finally {
  3449. * close(fd, (err) => {
  3450. * if (err) throw err;
  3451. * });
  3452. * }
  3453. * });
  3454. * }
  3455. * });
  3456. * ```
  3457. *
  3458. * **write (RECOMMENDED)**
  3459. *
  3460. * ```js
  3461. * import { open, close } from 'node:fs';
  3462. * open('myfile', 'wx', (err, fd) => {
  3463. * if (err) {
  3464. * if (err.code === 'EEXIST') {
  3465. * console.error('myfile already exists');
  3466. * return;
  3467. * }
  3468. *
  3469. * throw err;
  3470. * }
  3471. *
  3472. * try {
  3473. * writeMyData(fd);
  3474. * } finally {
  3475. * close(fd, (err) => {
  3476. * if (err) throw err;
  3477. * });
  3478. * }
  3479. * });
  3480. * ```
  3481. *
  3482. * **read (NOT RECOMMENDED)**
  3483. *
  3484. * ```js
  3485. * import { open, close, exists } from 'node:fs';
  3486. *
  3487. * exists('myfile', (e) => {
  3488. * if (e) {
  3489. * open('myfile', 'r', (err, fd) => {
  3490. * if (err) throw err;
  3491. *
  3492. * try {
  3493. * readMyData(fd);
  3494. * } finally {
  3495. * close(fd, (err) => {
  3496. * if (err) throw err;
  3497. * });
  3498. * }
  3499. * });
  3500. * } else {
  3501. * console.error('myfile does not exist');
  3502. * }
  3503. * });
  3504. * ```
  3505. *
  3506. * **read (RECOMMENDED)**
  3507. *
  3508. * ```js
  3509. * import { open, close } from 'node:fs';
  3510. *
  3511. * open('myfile', 'r', (err, fd) => {
  3512. * if (err) {
  3513. * if (err.code === 'ENOENT') {
  3514. * console.error('myfile does not exist');
  3515. * return;
  3516. * }
  3517. *
  3518. * throw err;
  3519. * }
  3520. *
  3521. * try {
  3522. * readMyData(fd);
  3523. * } finally {
  3524. * close(fd, (err) => {
  3525. * if (err) throw err;
  3526. * });
  3527. * }
  3528. * });
  3529. * ```
  3530. *
  3531. * The "not recommended" examples above check for existence and then use the
  3532. * file; the "recommended" examples are better because they use the file directly
  3533. * and handle the error, if any.
  3534. *
  3535. * In general, check for the existence of a file only if the file won't be
  3536. * used directly, for example when its existence is a signal from another
  3537. * process.
  3538. * @since v0.0.2
  3539. * @deprecated Since v1.0.0 - Use {@link stat} or {@link access} instead.
  3540. */
  3541. export function exists(path: PathLike, callback: (exists: boolean) => void): void;
  3542. /** @deprecated */
  3543. export namespace exists {
  3544. /**
  3545. * @param path A path to a file or directory. If a URL is provided, it must use the `file:` protocol.
  3546. * URL support is _experimental_.
  3547. */
  3548. function __promisify__(path: PathLike): Promise<boolean>;
  3549. }
  3550. /**
  3551. * Returns `true` if the path exists, `false` otherwise.
  3552. *
  3553. * For detailed information, see the documentation of the asynchronous version of
  3554. * this API: {@link exists}.
  3555. *
  3556. * `fs.exists()` is deprecated, but `fs.existsSync()` is not. The `callback` parameter to `fs.exists()` accepts parameters that are inconsistent with other
  3557. * Node.js callbacks. `fs.existsSync()` does not use a callback.
  3558. *
  3559. * ```js
  3560. * import { existsSync } from 'node:fs';
  3561. *
  3562. * if (existsSync('/etc/passwd'))
  3563. * console.log('The path exists.');
  3564. * ```
  3565. * @since v0.1.21
  3566. */
  3567. export function existsSync(path: PathLike): boolean;
  3568. export namespace constants {
  3569. // File Access Constants
  3570. /** Constant for fs.access(). File is visible to the calling process. */
  3571. const F_OK: number;
  3572. /** Constant for fs.access(). File can be read by the calling process. */
  3573. const R_OK: number;
  3574. /** Constant for fs.access(). File can be written by the calling process. */
  3575. const W_OK: number;
  3576. /** Constant for fs.access(). File can be executed by the calling process. */
  3577. const X_OK: number;
  3578. // File Copy Constants
  3579. /** Constant for fs.copyFile. Flag indicating the destination file should not be overwritten if it already exists. */
  3580. const COPYFILE_EXCL: number;
  3581. /**
  3582. * Constant for fs.copyFile. copy operation will attempt to create a copy-on-write reflink.
  3583. * If the underlying platform does not support copy-on-write, then a fallback copy mechanism is used.
  3584. */
  3585. const COPYFILE_FICLONE: number;
  3586. /**
  3587. * Constant for fs.copyFile. Copy operation will attempt to create a copy-on-write reflink.
  3588. * If the underlying platform does not support copy-on-write, then the operation will fail with an error.
  3589. */
  3590. const COPYFILE_FICLONE_FORCE: number;
  3591. // File Open Constants
  3592. /** Constant for fs.open(). Flag indicating to open a file for read-only access. */
  3593. const O_RDONLY: number;
  3594. /** Constant for fs.open(). Flag indicating to open a file for write-only access. */
  3595. const O_WRONLY: number;
  3596. /** Constant for fs.open(). Flag indicating to open a file for read-write access. */
  3597. const O_RDWR: number;
  3598. /** Constant for fs.open(). Flag indicating to create the file if it does not already exist. */
  3599. const O_CREAT: number;
  3600. /** Constant for fs.open(). Flag indicating that opening a file should fail if the O_CREAT flag is set and the file already exists. */
  3601. const O_EXCL: number;
  3602. /**
  3603. * Constant for fs.open(). Flag indicating that if path identifies a terminal device,
  3604. * opening the path shall not cause that terminal to become the controlling terminal for the process
  3605. * (if the process does not already have one).
  3606. */
  3607. const O_NOCTTY: number;
  3608. /** Constant for fs.open(). Flag indicating that if the file exists and is a regular file, and the file is opened successfully for write access, its length shall be truncated to zero. */
  3609. const O_TRUNC: number;
  3610. /** Constant for fs.open(). Flag indicating that data will be appended to the end of the file. */
  3611. const O_APPEND: number;
  3612. /** Constant for fs.open(). Flag indicating that the open should fail if the path is not a directory. */
  3613. const O_DIRECTORY: number;
  3614. /**
  3615. * constant for fs.open().
  3616. * Flag indicating reading accesses to the file system will no longer result in
  3617. * an update to the atime information associated with the file.
  3618. * This flag is available on Linux operating systems only.
  3619. */
  3620. const O_NOATIME: number;
  3621. /** Constant for fs.open(). Flag indicating that the open should fail if the path is a symbolic link. */
  3622. const O_NOFOLLOW: number;
  3623. /** Constant for fs.open(). Flag indicating that the file is opened for synchronous I/O. */
  3624. const O_SYNC: number;
  3625. /** Constant for fs.open(). Flag indicating that the file is opened for synchronous I/O with write operations waiting for data integrity. */
  3626. const O_DSYNC: number;
  3627. /** Constant for fs.open(). Flag indicating to open the symbolic link itself rather than the resource it is pointing to. */
  3628. const O_SYMLINK: number;
  3629. /** Constant for fs.open(). When set, an attempt will be made to minimize caching effects of file I/O. */
  3630. const O_DIRECT: number;
  3631. /** Constant for fs.open(). Flag indicating to open the file in nonblocking mode when possible. */
  3632. const O_NONBLOCK: number;
  3633. // File Type Constants
  3634. /** Constant for fs.Stats mode property for determining a file's type. Bit mask used to extract the file type code. */
  3635. const S_IFMT: number;
  3636. /** Constant for fs.Stats mode property for determining a file's type. File type constant for a regular file. */
  3637. const S_IFREG: number;
  3638. /** Constant for fs.Stats mode property for determining a file's type. File type constant for a directory. */
  3639. const S_IFDIR: number;
  3640. /** Constant for fs.Stats mode property for determining a file's type. File type constant for a character-oriented device file. */
  3641. const S_IFCHR: number;
  3642. /** Constant for fs.Stats mode property for determining a file's type. File type constant for a block-oriented device file. */
  3643. const S_IFBLK: number;
  3644. /** Constant for fs.Stats mode property for determining a file's type. File type constant for a FIFO/pipe. */
  3645. const S_IFIFO: number;
  3646. /** Constant for fs.Stats mode property for determining a file's type. File type constant for a symbolic link. */
  3647. const S_IFLNK: number;
  3648. /** Constant for fs.Stats mode property for determining a file's type. File type constant for a socket. */
  3649. const S_IFSOCK: number;
  3650. // File Mode Constants
  3651. /** Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating readable, writable and executable by owner. */
  3652. const S_IRWXU: number;
  3653. /** Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating readable by owner. */
  3654. const S_IRUSR: number;
  3655. /** Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating writable by owner. */
  3656. const S_IWUSR: number;
  3657. /** Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating executable by owner. */
  3658. const S_IXUSR: number;
  3659. /** Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating readable, writable and executable by group. */
  3660. const S_IRWXG: number;
  3661. /** Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating readable by group. */
  3662. const S_IRGRP: number;
  3663. /** Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating writable by group. */
  3664. const S_IWGRP: number;
  3665. /** Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating executable by group. */
  3666. const S_IXGRP: number;
  3667. /** Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating readable, writable and executable by others. */
  3668. const S_IRWXO: number;
  3669. /** Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating readable by others. */
  3670. const S_IROTH: number;
  3671. /** Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating writable by others. */
  3672. const S_IWOTH: number;
  3673. /** Constant for fs.Stats mode property for determining access permissions for a file. File mode indicating executable by others. */
  3674. const S_IXOTH: number;
  3675. /**
  3676. * When set, a memory file mapping is used to access the file. This flag
  3677. * is available on Windows operating systems only. On other operating systems,
  3678. * this flag is ignored.
  3679. */
  3680. const UV_FS_O_FILEMAP: number;
  3681. }
  3682. /**
  3683. * Tests a user's permissions for the file or directory specified by `path`.
  3684. * The `mode` argument is an optional integer that specifies the accessibility
  3685. * checks to be performed. `mode` should be either the value `fs.constants.F_OK` or a mask consisting of the bitwise OR of any of `fs.constants.R_OK`, `fs.constants.W_OK`, and `fs.constants.X_OK`
  3686. * (e.g.`fs.constants.W_OK | fs.constants.R_OK`). Check `File access constants` for
  3687. * possible values of `mode`.
  3688. *
  3689. * The final argument, `callback`, is a callback function that is invoked with
  3690. * a possible error argument. If any of the accessibility checks fail, the error
  3691. * argument will be an `Error` object. The following examples check if `package.json` exists, and if it is readable or writable.
  3692. *
  3693. * ```js
  3694. * import { access, constants } from 'node:fs';
  3695. *
  3696. * const file = 'package.json';
  3697. *
  3698. * // Check if the file exists in the current directory.
  3699. * access(file, constants.F_OK, (err) => {
  3700. * console.log(`${file} ${err ? 'does not exist' : 'exists'}`);
  3701. * });
  3702. *
  3703. * // Check if the file is readable.
  3704. * access(file, constants.R_OK, (err) => {
  3705. * console.log(`${file} ${err ? 'is not readable' : 'is readable'}`);
  3706. * });
  3707. *
  3708. * // Check if the file is writable.
  3709. * access(file, constants.W_OK, (err) => {
  3710. * console.log(`${file} ${err ? 'is not writable' : 'is writable'}`);
  3711. * });
  3712. *
  3713. * // Check if the file is readable and writable.
  3714. * access(file, constants.R_OK | constants.W_OK, (err) => {
  3715. * console.log(`${file} ${err ? 'is not' : 'is'} readable and writable`);
  3716. * });
  3717. * ```
  3718. *
  3719. * Do not use `fs.access()` to check for the accessibility of a file before calling `fs.open()`, `fs.readFile()`, or `fs.writeFile()`. Doing
  3720. * so introduces a race condition, since other processes may change the file's
  3721. * state between the two calls. Instead, user code should open/read/write the
  3722. * file directly and handle the error raised if the file is not accessible.
  3723. *
  3724. * **write (NOT RECOMMENDED)**
  3725. *
  3726. * ```js
  3727. * import { access, open, close } from 'node:fs';
  3728. *
  3729. * access('myfile', (err) => {
  3730. * if (!err) {
  3731. * console.error('myfile already exists');
  3732. * return;
  3733. * }
  3734. *
  3735. * open('myfile', 'wx', (err, fd) => {
  3736. * if (err) throw err;
  3737. *
  3738. * try {
  3739. * writeMyData(fd);
  3740. * } finally {
  3741. * close(fd, (err) => {
  3742. * if (err) throw err;
  3743. * });
  3744. * }
  3745. * });
  3746. * });
  3747. * ```
  3748. *
  3749. * **write (RECOMMENDED)**
  3750. *
  3751. * ```js
  3752. * import { open, close } from 'node:fs';
  3753. *
  3754. * open('myfile', 'wx', (err, fd) => {
  3755. * if (err) {
  3756. * if (err.code === 'EEXIST') {
  3757. * console.error('myfile already exists');
  3758. * return;
  3759. * }
  3760. *
  3761. * throw err;
  3762. * }
  3763. *
  3764. * try {
  3765. * writeMyData(fd);
  3766. * } finally {
  3767. * close(fd, (err) => {
  3768. * if (err) throw err;
  3769. * });
  3770. * }
  3771. * });
  3772. * ```
  3773. *
  3774. * **read (NOT RECOMMENDED)**
  3775. *
  3776. * ```js
  3777. * import { access, open, close } from 'node:fs';
  3778. * access('myfile', (err) => {
  3779. * if (err) {
  3780. * if (err.code === 'ENOENT') {
  3781. * console.error('myfile does not exist');
  3782. * return;
  3783. * }
  3784. *
  3785. * throw err;
  3786. * }
  3787. *
  3788. * open('myfile', 'r', (err, fd) => {
  3789. * if (err) throw err;
  3790. *
  3791. * try {
  3792. * readMyData(fd);
  3793. * } finally {
  3794. * close(fd, (err) => {
  3795. * if (err) throw err;
  3796. * });
  3797. * }
  3798. * });
  3799. * });
  3800. * ```
  3801. *
  3802. * **read (RECOMMENDED)**
  3803. *
  3804. * ```js
  3805. * import { open, close } from 'node:fs';
  3806. *
  3807. * open('myfile', 'r', (err, fd) => {
  3808. * if (err) {
  3809. * if (err.code === 'ENOENT') {
  3810. * console.error('myfile does not exist');
  3811. * return;
  3812. * }
  3813. *
  3814. * throw err;
  3815. * }
  3816. *
  3817. * try {
  3818. * readMyData(fd);
  3819. * } finally {
  3820. * close(fd, (err) => {
  3821. * if (err) throw err;
  3822. * });
  3823. * }
  3824. * });
  3825. * ```
  3826. *
  3827. * The "not recommended" examples above check for accessibility and then use the
  3828. * file; the "recommended" examples are better because they use the file directly
  3829. * and handle the error, if any.
  3830. *
  3831. * In general, check for the accessibility of a file only if the file will not be
  3832. * used directly, for example when its accessibility is a signal from another
  3833. * process.
  3834. *
  3835. * On Windows, access-control policies (ACLs) on a directory may limit access to
  3836. * a file or directory. The `fs.access()` function, however, does not check the
  3837. * ACL and therefore may report that a path is accessible even if the ACL restricts
  3838. * the user from reading or writing to it.
  3839. * @since v0.11.15
  3840. * @param [mode=fs.constants.F_OK]
  3841. */
  3842. export function access(path: PathLike, mode: number | undefined, callback: NoParamCallback): void;
  3843. /**
  3844. * Asynchronously tests a user's permissions for the file specified by path.
  3845. * @param path A path to a file or directory. If a URL is provided, it must use the `file:` protocol.
  3846. */
  3847. export function access(path: PathLike, callback: NoParamCallback): void;
  3848. export namespace access {
  3849. /**
  3850. * Asynchronously tests a user's permissions for the file specified by path.
  3851. * @param path A path to a file or directory. If a URL is provided, it must use the `file:` protocol.
  3852. * URL support is _experimental_.
  3853. */
  3854. function __promisify__(path: PathLike, mode?: number): Promise<void>;
  3855. }
  3856. /**
  3857. * Synchronously tests a user's permissions for the file or directory specified
  3858. * by `path`. The `mode` argument is an optional integer that specifies the
  3859. * accessibility checks to be performed. `mode` should be either the value `fs.constants.F_OK` or a mask consisting of the bitwise OR of any of `fs.constants.R_OK`, `fs.constants.W_OK`, and
  3860. * `fs.constants.X_OK` (e.g.`fs.constants.W_OK | fs.constants.R_OK`). Check `File access constants` for
  3861. * possible values of `mode`.
  3862. *
  3863. * If any of the accessibility checks fail, an `Error` will be thrown. Otherwise,
  3864. * the method will return `undefined`.
  3865. *
  3866. * ```js
  3867. * import { accessSync, constants } from 'node:fs';
  3868. *
  3869. * try {
  3870. * accessSync('etc/passwd', constants.R_OK | constants.W_OK);
  3871. * console.log('can read/write');
  3872. * } catch (err) {
  3873. * console.error('no access!');
  3874. * }
  3875. * ```
  3876. * @since v0.11.15
  3877. * @param [mode=fs.constants.F_OK]
  3878. */
  3879. export function accessSync(path: PathLike, mode?: number): void;
  3880. interface StreamOptions {
  3881. flags?: string | undefined;
  3882. encoding?: BufferEncoding | undefined;
  3883. fd?: number | promises.FileHandle | undefined;
  3884. mode?: number | undefined;
  3885. autoClose?: boolean | undefined;
  3886. emitClose?: boolean | undefined;
  3887. start?: number | undefined;
  3888. signal?: AbortSignal | null | undefined;
  3889. highWaterMark?: number | undefined;
  3890. }
  3891. interface FSImplementation {
  3892. open?: (...args: any[]) => any;
  3893. close?: (...args: any[]) => any;
  3894. }
  3895. interface CreateReadStreamFSImplementation extends FSImplementation {
  3896. read: (...args: any[]) => any;
  3897. }
  3898. interface CreateWriteStreamFSImplementation extends FSImplementation {
  3899. write: (...args: any[]) => any;
  3900. writev?: (...args: any[]) => any;
  3901. }
  3902. interface ReadStreamOptions extends StreamOptions {
  3903. fs?: CreateReadStreamFSImplementation | null | undefined;
  3904. end?: number | undefined;
  3905. }
  3906. interface WriteStreamOptions extends StreamOptions {
  3907. fs?: CreateWriteStreamFSImplementation | null | undefined;
  3908. flush?: boolean | undefined;
  3909. }
  3910. /**
  3911. * `options` can include `start` and `end` values to read a range of bytes from
  3912. * the file instead of the entire file. Both `start` and `end` are inclusive and
  3913. * start counting at 0, allowed values are in the
  3914. * \[0, [`Number.MAX_SAFE_INTEGER`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER)\] range. If `fd` is specified and `start` is
  3915. * omitted or `undefined`, `fs.createReadStream()` reads sequentially from the
  3916. * current file position. The `encoding` can be any one of those accepted by `Buffer`.
  3917. *
  3918. * If `fd` is specified, `ReadStream` will ignore the `path` argument and will use
  3919. * the specified file descriptor. This means that no `'open'` event will be
  3920. * emitted. `fd` should be blocking; non-blocking `fd`s should be passed to `net.Socket`.
  3921. *
  3922. * If `fd` points to a character device that only supports blocking reads
  3923. * (such as keyboard or sound card), read operations do not finish until data is
  3924. * available. This can prevent the process from exiting and the stream from
  3925. * closing naturally.
  3926. *
  3927. * By default, the stream will emit a `'close'` event after it has been
  3928. * destroyed. Set the `emitClose` option to `false` to change this behavior.
  3929. *
  3930. * By providing the `fs` option, it is possible to override the corresponding `fs` implementations for `open`, `read`, and `close`. When providing the `fs` option,
  3931. * an override for `read` is required. If no `fd` is provided, an override for `open` is also required. If `autoClose` is `true`, an override for `close` is
  3932. * also required.
  3933. *
  3934. * ```js
  3935. * import { createReadStream } from 'node:fs';
  3936. *
  3937. * // Create a stream from some character device.
  3938. * const stream = createReadStream('/dev/input/event0');
  3939. * setTimeout(() => {
  3940. * stream.close(); // This may not close the stream.
  3941. * // Artificially marking end-of-stream, as if the underlying resource had
  3942. * // indicated end-of-file by itself, allows the stream to close.
  3943. * // This does not cancel pending read operations, and if there is such an
  3944. * // operation, the process may still not be able to exit successfully
  3945. * // until it finishes.
  3946. * stream.push(null);
  3947. * stream.read(0);
  3948. * }, 100);
  3949. * ```
  3950. *
  3951. * If `autoClose` is false, then the file descriptor won't be closed, even if
  3952. * there's an error. It is the application's responsibility to close it and make
  3953. * sure there's no file descriptor leak. If `autoClose` is set to true (default
  3954. * behavior), on `'error'` or `'end'` the file descriptor will be closed
  3955. * automatically.
  3956. *
  3957. * `mode` sets the file mode (permission and sticky bits), but only if the
  3958. * file was created.
  3959. *
  3960. * An example to read the last 10 bytes of a file which is 100 bytes long:
  3961. *
  3962. * ```js
  3963. * import { createReadStream } from 'node:fs';
  3964. *
  3965. * createReadStream('sample.txt', { start: 90, end: 99 });
  3966. * ```
  3967. *
  3968. * If `options` is a string, then it specifies the encoding.
  3969. * @since v0.1.31
  3970. */
  3971. export function createReadStream(path: PathLike, options?: BufferEncoding | ReadStreamOptions): ReadStream;
  3972. /**
  3973. * `options` may also include a `start` option to allow writing data at some
  3974. * position past the beginning of the file, allowed values are in the
  3975. * \[0, [`Number.MAX_SAFE_INTEGER`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER)\] range. Modifying a file rather than
  3976. * replacing it may require the `flags` option to be set to `r+` rather than the
  3977. * default `w`. The `encoding` can be any one of those accepted by `Buffer`.
  3978. *
  3979. * If `autoClose` is set to true (default behavior) on `'error'` or `'finish'` the file descriptor will be closed automatically. If `autoClose` is false,
  3980. * then the file descriptor won't be closed, even if there's an error.
  3981. * It is the application's responsibility to close it and make sure there's no
  3982. * file descriptor leak.
  3983. *
  3984. * By default, the stream will emit a `'close'` event after it has been
  3985. * destroyed. Set the `emitClose` option to `false` to change this behavior.
  3986. *
  3987. * By providing the `fs` option it is possible to override the corresponding `fs` implementations for `open`, `write`, `writev`, and `close`. Overriding `write()` without `writev()` can reduce
  3988. * performance as some optimizations (`_writev()`)
  3989. * will be disabled. When providing the `fs` option, overrides for at least one of `write` and `writev` are required. If no `fd` option is supplied, an override
  3990. * for `open` is also required. If `autoClose` is `true`, an override for `close` is also required.
  3991. *
  3992. * Like `fs.ReadStream`, if `fd` is specified, `fs.WriteStream` will ignore the `path` argument and will use the specified file descriptor. This means that no `'open'` event will be
  3993. * emitted. `fd` should be blocking; non-blocking `fd`s
  3994. * should be passed to `net.Socket`.
  3995. *
  3996. * If `options` is a string, then it specifies the encoding.
  3997. * @since v0.1.31
  3998. */
  3999. export function createWriteStream(path: PathLike, options?: BufferEncoding | WriteStreamOptions): WriteStream;
  4000. /**
  4001. * Forces all currently queued I/O operations associated with the file to the
  4002. * operating system's synchronized I/O completion state. Refer to the POSIX [`fdatasync(2)`](http://man7.org/linux/man-pages/man2/fdatasync.2.html) documentation for details. No arguments other
  4003. * than a possible
  4004. * exception are given to the completion callback.
  4005. * @since v0.1.96
  4006. */
  4007. export function fdatasync(fd: number, callback: NoParamCallback): void;
  4008. export namespace fdatasync {
  4009. /**
  4010. * Asynchronous fdatasync(2) - synchronize a file's in-core state with storage device.
  4011. * @param fd A file descriptor.
  4012. */
  4013. function __promisify__(fd: number): Promise<void>;
  4014. }
  4015. /**
  4016. * Forces all currently queued I/O operations associated with the file to the
  4017. * operating system's synchronized I/O completion state. Refer to the POSIX [`fdatasync(2)`](http://man7.org/linux/man-pages/man2/fdatasync.2.html) documentation for details. Returns `undefined`.
  4018. * @since v0.1.96
  4019. */
  4020. export function fdatasyncSync(fd: number): void;
  4021. /**
  4022. * Asynchronously copies `src` to `dest`. By default, `dest` is overwritten if it
  4023. * already exists. No arguments other than a possible exception are given to the
  4024. * callback function. Node.js makes no guarantees about the atomicity of the copy
  4025. * operation. If an error occurs after the destination file has been opened for
  4026. * writing, Node.js will attempt to remove the destination.
  4027. *
  4028. * `mode` is an optional integer that specifies the behavior
  4029. * of the copy operation. It is possible to create a mask consisting of the bitwise
  4030. * OR of two or more values (e.g.`fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE`).
  4031. *
  4032. * * `fs.constants.COPYFILE_EXCL`: The copy operation will fail if `dest` already
  4033. * exists.
  4034. * * `fs.constants.COPYFILE_FICLONE`: The copy operation will attempt to create a
  4035. * copy-on-write reflink. If the platform does not support copy-on-write, then a
  4036. * fallback copy mechanism is used.
  4037. * * `fs.constants.COPYFILE_FICLONE_FORCE`: The copy operation will attempt to
  4038. * create a copy-on-write reflink. If the platform does not support
  4039. * copy-on-write, then the operation will fail.
  4040. *
  4041. * ```js
  4042. * import { copyFile, constants } from 'node:fs';
  4043. *
  4044. * function callback(err) {
  4045. * if (err) throw err;
  4046. * console.log('source.txt was copied to destination.txt');
  4047. * }
  4048. *
  4049. * // destination.txt will be created or overwritten by default.
  4050. * copyFile('source.txt', 'destination.txt', callback);
  4051. *
  4052. * // By using COPYFILE_EXCL, the operation will fail if destination.txt exists.
  4053. * copyFile('source.txt', 'destination.txt', constants.COPYFILE_EXCL, callback);
  4054. * ```
  4055. * @since v8.5.0
  4056. * @param src source filename to copy
  4057. * @param dest destination filename of the copy operation
  4058. * @param [mode=0] modifiers for copy operation.
  4059. */
  4060. export function copyFile(src: PathLike, dest: PathLike, callback: NoParamCallback): void;
  4061. export function copyFile(src: PathLike, dest: PathLike, mode: number, callback: NoParamCallback): void;
  4062. export namespace copyFile {
  4063. function __promisify__(src: PathLike, dst: PathLike, mode?: number): Promise<void>;
  4064. }
  4065. /**
  4066. * Synchronously copies `src` to `dest`. By default, `dest` is overwritten if it
  4067. * already exists. Returns `undefined`. Node.js makes no guarantees about the
  4068. * atomicity of the copy operation. If an error occurs after the destination file
  4069. * has been opened for writing, Node.js will attempt to remove the destination.
  4070. *
  4071. * `mode` is an optional integer that specifies the behavior
  4072. * of the copy operation. It is possible to create a mask consisting of the bitwise
  4073. * OR of two or more values (e.g.`fs.constants.COPYFILE_EXCL | fs.constants.COPYFILE_FICLONE`).
  4074. *
  4075. * * `fs.constants.COPYFILE_EXCL`: The copy operation will fail if `dest` already
  4076. * exists.
  4077. * * `fs.constants.COPYFILE_FICLONE`: The copy operation will attempt to create a
  4078. * copy-on-write reflink. If the platform does not support copy-on-write, then a
  4079. * fallback copy mechanism is used.
  4080. * * `fs.constants.COPYFILE_FICLONE_FORCE`: The copy operation will attempt to
  4081. * create a copy-on-write reflink. If the platform does not support
  4082. * copy-on-write, then the operation will fail.
  4083. *
  4084. * ```js
  4085. * import { copyFileSync, constants } from 'node:fs';
  4086. *
  4087. * // destination.txt will be created or overwritten by default.
  4088. * copyFileSync('source.txt', 'destination.txt');
  4089. * console.log('source.txt was copied to destination.txt');
  4090. *
  4091. * // By using COPYFILE_EXCL, the operation will fail if destination.txt exists.
  4092. * copyFileSync('source.txt', 'destination.txt', constants.COPYFILE_EXCL);
  4093. * ```
  4094. * @since v8.5.0
  4095. * @param src source filename to copy
  4096. * @param dest destination filename of the copy operation
  4097. * @param [mode=0] modifiers for copy operation.
  4098. */
  4099. export function copyFileSync(src: PathLike, dest: PathLike, mode?: number): void;
  4100. /**
  4101. * Write an array of `ArrayBufferView`s to the file specified by `fd` using `writev()`.
  4102. *
  4103. * `position` is the offset from the beginning of the file where this data
  4104. * should be written. If `typeof position !== 'number'`, the data will be written
  4105. * at the current position.
  4106. *
  4107. * The callback will be given three arguments: `err`, `bytesWritten`, and `buffers`. `bytesWritten` is how many bytes were written from `buffers`.
  4108. *
  4109. * If this method is `util.promisify()` ed, it returns a promise for an `Object` with `bytesWritten` and `buffers` properties.
  4110. *
  4111. * It is unsafe to use `fs.writev()` multiple times on the same file without
  4112. * waiting for the callback. For this scenario, use {@link createWriteStream}.
  4113. *
  4114. * On Linux, positional writes don't work when the file is opened in append mode.
  4115. * The kernel ignores the position argument and always appends the data to
  4116. * the end of the file.
  4117. * @since v12.9.0
  4118. * @param [position='null']
  4119. */
  4120. export function writev(
  4121. fd: number,
  4122. buffers: readonly NodeJS.ArrayBufferView[],
  4123. cb: (err: NodeJS.ErrnoException | null, bytesWritten: number, buffers: NodeJS.ArrayBufferView[]) => void,
  4124. ): void;
  4125. export function writev(
  4126. fd: number,
  4127. buffers: readonly NodeJS.ArrayBufferView[],
  4128. position: number | null,
  4129. cb: (err: NodeJS.ErrnoException | null, bytesWritten: number, buffers: NodeJS.ArrayBufferView[]) => void,
  4130. ): void;
  4131. export interface WriteVResult {
  4132. bytesWritten: number;
  4133. buffers: NodeJS.ArrayBufferView[];
  4134. }
  4135. export namespace writev {
  4136. function __promisify__(
  4137. fd: number,
  4138. buffers: readonly NodeJS.ArrayBufferView[],
  4139. position?: number,
  4140. ): Promise<WriteVResult>;
  4141. }
  4142. /**
  4143. * For detailed information, see the documentation of the asynchronous version of
  4144. * this API: {@link writev}.
  4145. * @since v12.9.0
  4146. * @param [position='null']
  4147. * @return The number of bytes written.
  4148. */
  4149. export function writevSync(fd: number, buffers: readonly NodeJS.ArrayBufferView[], position?: number): number;
  4150. /**
  4151. * Read from a file specified by `fd` and write to an array of `ArrayBufferView`s
  4152. * using `readv()`.
  4153. *
  4154. * `position` is the offset from the beginning of the file from where data
  4155. * should be read. If `typeof position !== 'number'`, the data will be read
  4156. * from the current position.
  4157. *
  4158. * The callback will be given three arguments: `err`, `bytesRead`, and `buffers`. `bytesRead` is how many bytes were read from the file.
  4159. *
  4160. * If this method is invoked as its `util.promisify()` ed version, it returns
  4161. * a promise for an `Object` with `bytesRead` and `buffers` properties.
  4162. * @since v13.13.0, v12.17.0
  4163. * @param [position='null']
  4164. */
  4165. export function readv(
  4166. fd: number,
  4167. buffers: readonly NodeJS.ArrayBufferView[],
  4168. cb: (err: NodeJS.ErrnoException | null, bytesRead: number, buffers: NodeJS.ArrayBufferView[]) => void,
  4169. ): void;
  4170. export function readv(
  4171. fd: number,
  4172. buffers: readonly NodeJS.ArrayBufferView[],
  4173. position: number | null,
  4174. cb: (err: NodeJS.ErrnoException | null, bytesRead: number, buffers: NodeJS.ArrayBufferView[]) => void,
  4175. ): void;
  4176. export interface ReadVResult {
  4177. bytesRead: number;
  4178. buffers: NodeJS.ArrayBufferView[];
  4179. }
  4180. export namespace readv {
  4181. function __promisify__(
  4182. fd: number,
  4183. buffers: readonly NodeJS.ArrayBufferView[],
  4184. position?: number,
  4185. ): Promise<ReadVResult>;
  4186. }
  4187. /**
  4188. * For detailed information, see the documentation of the asynchronous version of
  4189. * this API: {@link readv}.
  4190. * @since v13.13.0, v12.17.0
  4191. * @param [position='null']
  4192. * @return The number of bytes read.
  4193. */
  4194. export function readvSync(fd: number, buffers: readonly NodeJS.ArrayBufferView[], position?: number): number;
  4195. export interface OpenAsBlobOptions {
  4196. /**
  4197. * An optional mime type for the blob.
  4198. *
  4199. * @default 'undefined'
  4200. */
  4201. type?: string | undefined;
  4202. }
  4203. /**
  4204. * Returns a `Blob` whose data is backed by the given file.
  4205. *
  4206. * The file must not be modified after the `Blob` is created. Any modifications
  4207. * will cause reading the `Blob` data to fail with a `DOMException` error.
  4208. * Synchronous stat operations on the file when the `Blob` is created, and before
  4209. * each read in order to detect whether the file data has been modified on disk.
  4210. *
  4211. * ```js
  4212. * import { openAsBlob } from 'node:fs';
  4213. *
  4214. * const blob = await openAsBlob('the.file.txt');
  4215. * const ab = await blob.arrayBuffer();
  4216. * blob.stream();
  4217. * ```
  4218. * @since v19.8.0
  4219. */
  4220. export function openAsBlob(path: PathLike, options?: OpenAsBlobOptions): Promise<Blob>;
  4221. export interface OpenDirOptions {
  4222. /**
  4223. * @default 'utf8'
  4224. */
  4225. encoding?: BufferEncoding | undefined;
  4226. /**
  4227. * Number of directory entries that are buffered
  4228. * internally when reading from the directory. Higher values lead to better
  4229. * performance but higher memory usage.
  4230. * @default 32
  4231. */
  4232. bufferSize?: number | undefined;
  4233. /**
  4234. * @default false
  4235. */
  4236. recursive?: boolean;
  4237. }
  4238. /**
  4239. * Synchronously open a directory. See [`opendir(3)`](http://man7.org/linux/man-pages/man3/opendir.3.html).
  4240. *
  4241. * Creates an `fs.Dir`, which contains all further functions for reading from
  4242. * and cleaning up the directory.
  4243. *
  4244. * The `encoding` option sets the encoding for the `path` while opening the
  4245. * directory and subsequent read operations.
  4246. * @since v12.12.0
  4247. */
  4248. export function opendirSync(path: PathLike, options?: OpenDirOptions): Dir;
  4249. /**
  4250. * Asynchronously open a directory. See the POSIX [`opendir(3)`](http://man7.org/linux/man-pages/man3/opendir.3.html) documentation for
  4251. * more details.
  4252. *
  4253. * Creates an `fs.Dir`, which contains all further functions for reading from
  4254. * and cleaning up the directory.
  4255. *
  4256. * The `encoding` option sets the encoding for the `path` while opening the
  4257. * directory and subsequent read operations.
  4258. * @since v12.12.0
  4259. */
  4260. export function opendir(path: PathLike, cb: (err: NodeJS.ErrnoException | null, dir: Dir) => void): void;
  4261. export function opendir(
  4262. path: PathLike,
  4263. options: OpenDirOptions,
  4264. cb: (err: NodeJS.ErrnoException | null, dir: Dir) => void,
  4265. ): void;
  4266. export namespace opendir {
  4267. function __promisify__(path: PathLike, options?: OpenDirOptions): Promise<Dir>;
  4268. }
  4269. export interface BigIntStats extends StatsBase<bigint> {
  4270. atimeNs: bigint;
  4271. mtimeNs: bigint;
  4272. ctimeNs: bigint;
  4273. birthtimeNs: bigint;
  4274. }
  4275. export interface BigIntOptions {
  4276. bigint: true;
  4277. }
  4278. export interface StatOptions {
  4279. bigint?: boolean | undefined;
  4280. }
  4281. export interface StatSyncOptions extends StatOptions {
  4282. throwIfNoEntry?: boolean | undefined;
  4283. }
  4284. interface CopyOptionsBase {
  4285. /**
  4286. * Dereference symlinks
  4287. * @default false
  4288. */
  4289. dereference?: boolean;
  4290. /**
  4291. * When `force` is `false`, and the destination
  4292. * exists, throw an error.
  4293. * @default false
  4294. */
  4295. errorOnExist?: boolean;
  4296. /**
  4297. * Overwrite existing file or directory. _The copy
  4298. * operation will ignore errors if you set this to false and the destination
  4299. * exists. Use the `errorOnExist` option to change this behavior.
  4300. * @default true
  4301. */
  4302. force?: boolean;
  4303. /**
  4304. * Modifiers for copy operation. See `mode` flag of {@link copyFileSync()}
  4305. */
  4306. mode?: number;
  4307. /**
  4308. * When `true` timestamps from `src` will
  4309. * be preserved.
  4310. * @default false
  4311. */
  4312. preserveTimestamps?: boolean;
  4313. /**
  4314. * Copy directories recursively.
  4315. * @default false
  4316. */
  4317. recursive?: boolean;
  4318. /**
  4319. * When true, path resolution for symlinks will be skipped
  4320. * @default false
  4321. */
  4322. verbatimSymlinks?: boolean;
  4323. }
  4324. export interface CopyOptions extends CopyOptionsBase {
  4325. /**
  4326. * Function to filter copied files/directories. Return
  4327. * `true` to copy the item, `false` to ignore it.
  4328. */
  4329. filter?(source: string, destination: string): boolean | Promise<boolean>;
  4330. }
  4331. export interface CopySyncOptions extends CopyOptionsBase {
  4332. /**
  4333. * Function to filter copied files/directories. Return
  4334. * `true` to copy the item, `false` to ignore it.
  4335. */
  4336. filter?(source: string, destination: string): boolean;
  4337. }
  4338. /**
  4339. * Asynchronously copies the entire directory structure from `src` to `dest`,
  4340. * including subdirectories and files.
  4341. *
  4342. * When copying a directory to another directory, globs are not supported and
  4343. * behavior is similar to `cp dir1/ dir2/`.
  4344. * @since v16.7.0
  4345. * @experimental
  4346. * @param src source path to copy.
  4347. * @param dest destination path to copy to.
  4348. */
  4349. export function cp(
  4350. source: string | URL,
  4351. destination: string | URL,
  4352. callback: (err: NodeJS.ErrnoException | null) => void,
  4353. ): void;
  4354. export function cp(
  4355. source: string | URL,
  4356. destination: string | URL,
  4357. opts: CopyOptions,
  4358. callback: (err: NodeJS.ErrnoException | null) => void,
  4359. ): void;
  4360. /**
  4361. * Synchronously copies the entire directory structure from `src` to `dest`,
  4362. * including subdirectories and files.
  4363. *
  4364. * When copying a directory to another directory, globs are not supported and
  4365. * behavior is similar to `cp dir1/ dir2/`.
  4366. * @since v16.7.0
  4367. * @experimental
  4368. * @param src source path to copy.
  4369. * @param dest destination path to copy to.
  4370. */
  4371. export function cpSync(source: string | URL, destination: string | URL, opts?: CopySyncOptions): void;
  4372. interface _GlobOptions<T extends Dirent | string> {
  4373. /**
  4374. * Current working directory.
  4375. * @default process.cwd()
  4376. */
  4377. cwd?: string | URL | undefined;
  4378. /**
  4379. * `true` if the glob should return paths as `Dirent`s, `false` otherwise.
  4380. * @default false
  4381. * @since v22.2.0
  4382. */
  4383. withFileTypes?: boolean | undefined;
  4384. /**
  4385. * Function to filter out files/directories or a
  4386. * list of glob patterns to be excluded. If a function is provided, return
  4387. * `true` to exclude the item, `false` to include it.
  4388. * @default undefined
  4389. */
  4390. exclude?: ((fileName: T) => boolean) | readonly string[] | undefined;
  4391. }
  4392. export interface GlobOptions extends _GlobOptions<Dirent | string> {}
  4393. export interface GlobOptionsWithFileTypes extends _GlobOptions<Dirent> {
  4394. withFileTypes: true;
  4395. }
  4396. export interface GlobOptionsWithoutFileTypes extends _GlobOptions<string> {
  4397. withFileTypes?: false | undefined;
  4398. }
  4399. /**
  4400. * Retrieves the files matching the specified pattern.
  4401. *
  4402. * ```js
  4403. * import { glob } from 'node:fs';
  4404. *
  4405. * glob('*.js', (err, matches) => {
  4406. * if (err) throw err;
  4407. * console.log(matches);
  4408. * });
  4409. * ```
  4410. * @since v22.0.0
  4411. */
  4412. export function glob(
  4413. pattern: string | readonly string[],
  4414. callback: (err: NodeJS.ErrnoException | null, matches: string[]) => void,
  4415. ): void;
  4416. export function glob(
  4417. pattern: string | readonly string[],
  4418. options: GlobOptionsWithFileTypes,
  4419. callback: (
  4420. err: NodeJS.ErrnoException | null,
  4421. matches: Dirent[],
  4422. ) => void,
  4423. ): void;
  4424. export function glob(
  4425. pattern: string | readonly string[],
  4426. options: GlobOptionsWithoutFileTypes,
  4427. callback: (
  4428. err: NodeJS.ErrnoException | null,
  4429. matches: string[],
  4430. ) => void,
  4431. ): void;
  4432. export function glob(
  4433. pattern: string | readonly string[],
  4434. options: GlobOptions,
  4435. callback: (
  4436. err: NodeJS.ErrnoException | null,
  4437. matches: Dirent[] | string[],
  4438. ) => void,
  4439. ): void;
  4440. /**
  4441. * ```js
  4442. * import { globSync } from 'node:fs';
  4443. *
  4444. * console.log(globSync('*.js'));
  4445. * ```
  4446. * @since v22.0.0
  4447. * @returns paths of files that match the pattern.
  4448. */
  4449. export function globSync(pattern: string | readonly string[]): string[];
  4450. export function globSync(
  4451. pattern: string | readonly string[],
  4452. options: GlobOptionsWithFileTypes,
  4453. ): Dirent[];
  4454. export function globSync(
  4455. pattern: string | readonly string[],
  4456. options: GlobOptionsWithoutFileTypes,
  4457. ): string[];
  4458. export function globSync(
  4459. pattern: string | readonly string[],
  4460. options: GlobOptions,
  4461. ): Dirent[] | string[];
  4462. }
  4463. declare module "node:fs" {
  4464. export * from "fs";
  4465. }