You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
4.6 KiB

3 months ago
  1. # graceful-fs
  2. graceful-fs functions as a drop-in replacement for the fs module,
  3. making various improvements.
  4. The improvements are meant to normalize behavior across different
  5. platforms and environments, and to make filesystem access more
  6. resilient to errors.
  7. ## Improvements over [fs module](https://nodejs.org/api/fs.html)
  8. * Queues up `open` and `readdir` calls, and retries them once
  9. something closes if there is an EMFILE error from too many file
  10. descriptors.
  11. * fixes `lchmod` for Node versions prior to 0.6.2.
  12. * implements `fs.lutimes` if possible. Otherwise it becomes a noop.
  13. * ignores `EINVAL` and `EPERM` errors in `chown`, `fchown` or
  14. `lchown` if the user isn't root.
  15. * makes `lchmod` and `lchown` become noops, if not available.
  16. * retries reading a file if `read` results in EAGAIN error.
  17. On Windows, it retries renaming a file for up to one second if `EACCESS`
  18. or `EPERM` error occurs, likely because antivirus software has locked
  19. the directory.
  20. ## USAGE
  21. ```javascript
  22. // use just like fs
  23. var fs = require('graceful-fs')
  24. // now go and do stuff with it...
  25. fs.readFile('some-file-or-whatever', (err, data) => {
  26. // Do stuff here.
  27. })
  28. ```
  29. ## Sync methods
  30. This module cannot intercept or handle `EMFILE` or `ENFILE` errors from sync
  31. methods. If you use sync methods which open file descriptors then you are
  32. responsible for dealing with any errors.
  33. This is a known limitation, not a bug.
  34. ## Global Patching
  35. If you want to patch the global fs module (or any other fs-like
  36. module) you can do this:
  37. ```javascript
  38. // Make sure to read the caveat below.
  39. var realFs = require('fs')
  40. var gracefulFs = require('graceful-fs')
  41. gracefulFs.gracefulify(realFs)
  42. ```
  43. This should only ever be done at the top-level application layer, in
  44. order to delay on EMFILE errors from any fs-using dependencies. You
  45. should **not** do this in a library, because it can cause unexpected
  46. delays in other parts of the program.
  47. ## Changes
  48. This module is fairly stable at this point, and used by a lot of
  49. things. That being said, because it implements a subtle behavior
  50. change in a core part of the node API, even modest changes can be
  51. extremely breaking, and the versioning is thus biased towards
  52. bumping the major when in doubt.
  53. The main change between major versions has been switching between
  54. providing a fully-patched `fs` module vs monkey-patching the node core
  55. builtin, and the approach by which a non-monkey-patched `fs` was
  56. created.
  57. The goal is to trade `EMFILE` errors for slower fs operations. So, if
  58. you try to open a zillion files, rather than crashing, `open`
  59. operations will be queued up and wait for something else to `close`.
  60. There are advantages to each approach. Monkey-patching the fs means
  61. that no `EMFILE` errors can possibly occur anywhere in your
  62. application, because everything is using the same core `fs` module,
  63. which is patched. However, it can also obviously cause undesirable
  64. side-effects, especially if the module is loaded multiple times.
  65. Implementing a separate-but-identical patched `fs` module is more
  66. surgical (and doesn't run the risk of patching multiple times), but
  67. also imposes the challenge of keeping in sync with the core module.
  68. The current approach loads the `fs` module, and then creates a
  69. lookalike object that has all the same methods, except a few that are
  70. patched. It is safe to use in all versions of Node from 0.8 through
  71. 7.0.
  72. ### v4
  73. * Do not monkey-patch the fs module. This module may now be used as a
  74. drop-in dep, and users can opt into monkey-patching the fs builtin
  75. if their app requires it.
  76. ### v3
  77. * Monkey-patch fs, because the eval approach no longer works on recent
  78. node.
  79. * fixed possible type-error throw if rename fails on windows
  80. * verify that we *never* get EMFILE errors
  81. * Ignore ENOSYS from chmod/chown
  82. * clarify that graceful-fs must be used as a drop-in
  83. ### v2.1.0
  84. * Use eval rather than monkey-patching fs.
  85. * readdir: Always sort the results
  86. * win32: requeue a file if error has an OK status
  87. ### v2.0
  88. * A return to monkey patching
  89. * wrap process.cwd
  90. ### v1.1
  91. * wrap readFile
  92. * Wrap fs.writeFile.
  93. * readdir protection
  94. * Don't clobber the fs builtin
  95. * Handle fs.read EAGAIN errors by trying again
  96. * Expose the curOpen counter
  97. * No-op lchown/lchmod if not implemented
  98. * fs.rename patch only for win32
  99. * Patch fs.rename to handle AV software on Windows
  100. * Close #4 Chown should not fail on einval or eperm if non-root
  101. * Fix isaacs/fstream#1 Only wrap fs one time
  102. * Fix #3 Start at 1024 max files, then back off on EMFILE
  103. * lutimes that doens't blow up on Linux
  104. * A full on-rewrite using a queue instead of just swallowing the EMFILE error
  105. * Wrap Read/Write streams as well
  106. ### 1.0
  107. * Update engines for node 0.6
  108. * Be lstat-graceful on Windows
  109. * first