UI for Zipcoin Blue

polyfills.js 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. var fs = require('./fs.js')
  2. var constants = require('constants')
  3. var origCwd = process.cwd
  4. var cwd = null
  5. var platform = process.env.GRACEFUL_FS_PLATFORM || process.platform
  6. process.cwd = function() {
  7. if (!cwd)
  8. cwd = origCwd.call(process)
  9. return cwd
  10. }
  11. try {
  12. process.cwd()
  13. } catch (er) {}
  14. var chdir = process.chdir
  15. process.chdir = function(d) {
  16. cwd = null
  17. chdir.call(process, d)
  18. }
  19. module.exports = patch
  20. function patch (fs) {
  21. // (re-)implement some things that are known busted or missing.
  22. // lchmod, broken prior to 0.6.2
  23. // back-port the fix here.
  24. if (constants.hasOwnProperty('O_SYMLINK') &&
  25. process.version.match(/^v0\.6\.[0-2]|^v0\.5\./)) {
  26. patchLchmod(fs)
  27. }
  28. // lutimes implementation, or no-op
  29. if (!fs.lutimes) {
  30. patchLutimes(fs)
  31. }
  32. // https://github.com/isaacs/node-graceful-fs/issues/4
  33. // Chown should not fail on einval or eperm if non-root.
  34. // It should not fail on enosys ever, as this just indicates
  35. // that a fs doesn't support the intended operation.
  36. fs.chown = chownFix(fs.chown)
  37. fs.fchown = chownFix(fs.fchown)
  38. fs.lchown = chownFix(fs.lchown)
  39. fs.chmod = chmodFix(fs.chmod)
  40. fs.fchmod = chmodFix(fs.fchmod)
  41. fs.lchmod = chmodFix(fs.lchmod)
  42. fs.chownSync = chownFixSync(fs.chownSync)
  43. fs.fchownSync = chownFixSync(fs.fchownSync)
  44. fs.lchownSync = chownFixSync(fs.lchownSync)
  45. fs.chmodSync = chmodFixSync(fs.chmodSync)
  46. fs.fchmodSync = chmodFixSync(fs.fchmodSync)
  47. fs.lchmodSync = chmodFixSync(fs.lchmodSync)
  48. fs.stat = statFix(fs.stat)
  49. fs.fstat = statFix(fs.fstat)
  50. fs.lstat = statFix(fs.lstat)
  51. fs.statSync = statFixSync(fs.statSync)
  52. fs.fstatSync = statFixSync(fs.fstatSync)
  53. fs.lstatSync = statFixSync(fs.lstatSync)
  54. // if lchmod/lchown do not exist, then make them no-ops
  55. if (!fs.lchmod) {
  56. fs.lchmod = function (path, mode, cb) {
  57. if (cb) process.nextTick(cb)
  58. }
  59. fs.lchmodSync = function () {}
  60. }
  61. if (!fs.lchown) {
  62. fs.lchown = function (path, uid, gid, cb) {
  63. if (cb) process.nextTick(cb)
  64. }
  65. fs.lchownSync = function () {}
  66. }
  67. // on Windows, A/V software can lock the directory, causing this
  68. // to fail with an EACCES or EPERM if the directory contains newly
  69. // created files. Try again on failure, for up to 60 seconds.
  70. // Set the timeout this long because some Windows Anti-Virus, such as Parity
  71. // bit9, may lock files for up to a minute, causing npm package install
  72. // failures. Also, take care to yield the scheduler. Windows scheduling gives
  73. // CPU to a busy looping process, which can cause the program causing the lock
  74. // contention to be starved of CPU by node, so the contention doesn't resolve.
  75. if (platform === "win32") {
  76. fs.rename = (function (fs$rename) { return function (from, to, cb) {
  77. var start = Date.now()
  78. var backoff = 0;
  79. fs$rename(from, to, function CB (er) {
  80. if (er
  81. && (er.code === "EACCES" || er.code === "EPERM")
  82. && Date.now() - start < 60000) {
  83. setTimeout(function() {
  84. fs.stat(to, function (stater, st) {
  85. if (stater && stater.code === "ENOENT")
  86. fs$rename(from, to, CB);
  87. else
  88. cb(er)
  89. })
  90. }, backoff)
  91. if (backoff < 100)
  92. backoff += 10;
  93. return;
  94. }
  95. if (cb) cb(er)
  96. })
  97. }})(fs.rename)
  98. }
  99. // if read() returns EAGAIN, then just try it again.
  100. fs.read = (function (fs$read) { return function (fd, buffer, offset, length, position, callback_) {
  101. var callback
  102. if (callback_ && typeof callback_ === 'function') {
  103. var eagCounter = 0
  104. callback = function (er, _, __) {
  105. if (er && er.code === 'EAGAIN' && eagCounter < 10) {
  106. eagCounter ++
  107. return fs$read.call(fs, fd, buffer, offset, length, position, callback)
  108. }
  109. callback_.apply(this, arguments)
  110. }
  111. }
  112. return fs$read.call(fs, fd, buffer, offset, length, position, callback)
  113. }})(fs.read)
  114. fs.readSync = (function (fs$readSync) { return function (fd, buffer, offset, length, position) {
  115. var eagCounter = 0
  116. while (true) {
  117. try {
  118. return fs$readSync.call(fs, fd, buffer, offset, length, position)
  119. } catch (er) {
  120. if (er.code === 'EAGAIN' && eagCounter < 10) {
  121. eagCounter ++
  122. continue
  123. }
  124. throw er
  125. }
  126. }
  127. }})(fs.readSync)
  128. }
  129. function patchLchmod (fs) {
  130. fs.lchmod = function (path, mode, callback) {
  131. fs.open( path
  132. , constants.O_WRONLY | constants.O_SYMLINK
  133. , mode
  134. , function (err, fd) {
  135. if (err) {
  136. if (callback) callback(err)
  137. return
  138. }
  139. // prefer to return the chmod error, if one occurs,
  140. // but still try to close, and report closing errors if they occur.
  141. fs.fchmod(fd, mode, function (err) {
  142. fs.close(fd, function(err2) {
  143. if (callback) callback(err || err2)
  144. })
  145. })
  146. })
  147. }
  148. fs.lchmodSync = function (path, mode) {
  149. var fd = fs.openSync(path, constants.O_WRONLY | constants.O_SYMLINK, mode)
  150. // prefer to return the chmod error, if one occurs,
  151. // but still try to close, and report closing errors if they occur.
  152. var threw = true
  153. var ret
  154. try {
  155. ret = fs.fchmodSync(fd, mode)
  156. threw = false
  157. } finally {
  158. if (threw) {
  159. try {
  160. fs.closeSync(fd)
  161. } catch (er) {}
  162. } else {
  163. fs.closeSync(fd)
  164. }
  165. }
  166. return ret
  167. }
  168. }
  169. function patchLutimes (fs) {
  170. if (constants.hasOwnProperty("O_SYMLINK")) {
  171. fs.lutimes = function (path, at, mt, cb) {
  172. fs.open(path, constants.O_SYMLINK, function (er, fd) {
  173. if (er) {
  174. if (cb) cb(er)
  175. return
  176. }
  177. fs.futimes(fd, at, mt, function (er) {
  178. fs.close(fd, function (er2) {
  179. if (cb) cb(er || er2)
  180. })
  181. })
  182. })
  183. }
  184. fs.lutimesSync = function (path, at, mt) {
  185. var fd = fs.openSync(path, constants.O_SYMLINK)
  186. var ret
  187. var threw = true
  188. try {
  189. ret = fs.futimesSync(fd, at, mt)
  190. threw = false
  191. } finally {
  192. if (threw) {
  193. try {
  194. fs.closeSync(fd)
  195. } catch (er) {}
  196. } else {
  197. fs.closeSync(fd)
  198. }
  199. }
  200. return ret
  201. }
  202. } else {
  203. fs.lutimes = function (_a, _b, _c, cb) { if (cb) process.nextTick(cb) }
  204. fs.lutimesSync = function () {}
  205. }
  206. }
  207. function chmodFix (orig) {
  208. if (!orig) return orig
  209. return function (target, mode, cb) {
  210. return orig.call(fs, target, mode, function (er) {
  211. if (chownErOk(er)) er = null
  212. if (cb) cb.apply(this, arguments)
  213. })
  214. }
  215. }
  216. function chmodFixSync (orig) {
  217. if (!orig) return orig
  218. return function (target, mode) {
  219. try {
  220. return orig.call(fs, target, mode)
  221. } catch (er) {
  222. if (!chownErOk(er)) throw er
  223. }
  224. }
  225. }
  226. function chownFix (orig) {
  227. if (!orig) return orig
  228. return function (target, uid, gid, cb) {
  229. return orig.call(fs, target, uid, gid, function (er) {
  230. if (chownErOk(er)) er = null
  231. if (cb) cb.apply(this, arguments)
  232. })
  233. }
  234. }
  235. function chownFixSync (orig) {
  236. if (!orig) return orig
  237. return function (target, uid, gid) {
  238. try {
  239. return orig.call(fs, target, uid, gid)
  240. } catch (er) {
  241. if (!chownErOk(er)) throw er
  242. }
  243. }
  244. }
  245. function statFix (orig) {
  246. if (!orig) return orig
  247. // Older versions of Node erroneously returned signed integers for
  248. // uid + gid.
  249. return function (target, cb) {
  250. return orig.call(fs, target, function (er, stats) {
  251. if (!stats) return cb.apply(this, arguments)
  252. if (stats.uid < 0) stats.uid += 0x100000000
  253. if (stats.gid < 0) stats.gid += 0x100000000
  254. if (cb) cb.apply(this, arguments)
  255. })
  256. }
  257. }
  258. function statFixSync (orig) {
  259. if (!orig) return orig
  260. // Older versions of Node erroneously returned signed integers for
  261. // uid + gid.
  262. return function (target) {
  263. var stats = orig.call(fs, target)
  264. if (stats.uid < 0) stats.uid += 0x100000000
  265. if (stats.gid < 0) stats.gid += 0x100000000
  266. return stats;
  267. }
  268. }
  269. // ENOSYS means that the fs doesn't support the op. Just ignore
  270. // that, because it doesn't matter.
  271. //
  272. // if there's no getuid, or if getuid() is something other
  273. // than 0, and the error is EINVAL or EPERM, then just ignore
  274. // it.
  275. //
  276. // This specific case is a silent failure in cp, install, tar,
  277. // and most other unix tools that manage permissions.
  278. //
  279. // When running as root, or if other types of errors are
  280. // encountered, then it's strict.
  281. function chownErOk (er) {
  282. if (!er)
  283. return true
  284. if (er.code === "ENOSYS")
  285. return true
  286. var nonroot = !process.getuid || process.getuid() !== 0
  287. if (nonroot) {
  288. if (er.code === "EINVAL" || er.code === "EPERM")
  289. return true
  290. }
  291. return false
  292. }