promises.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. "use strict";
  2. var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
  3. if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
  4. if (ar || !(i in from)) {
  5. if (!ar) ar = Array.prototype.slice.call(from, 0, i);
  6. ar[i] = from[i];
  7. }
  8. }
  9. return to.concat(ar || Array.prototype.slice.call(from));
  10. };
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. exports.FileHandle = void 0;
  13. function promisify(vol, fn, getResult) {
  14. if (getResult === void 0) { getResult = function (input) { return input; }; }
  15. return function () {
  16. var args = [];
  17. for (var _i = 0; _i < arguments.length; _i++) {
  18. args[_i] = arguments[_i];
  19. }
  20. return new Promise(function (resolve, reject) {
  21. vol[fn].bind(vol).apply(void 0, __spreadArray(__spreadArray([], args, false), [function (error, result) {
  22. if (error)
  23. return reject(error);
  24. return resolve(getResult(result));
  25. }], false));
  26. });
  27. };
  28. }
  29. var FileHandle = /** @class */ (function () {
  30. function FileHandle(vol, fd) {
  31. this.vol = vol;
  32. this.fd = fd;
  33. }
  34. FileHandle.prototype.appendFile = function (data, options) {
  35. return promisify(this.vol, 'appendFile')(this.fd, data, options);
  36. };
  37. FileHandle.prototype.chmod = function (mode) {
  38. return promisify(this.vol, 'fchmod')(this.fd, mode);
  39. };
  40. FileHandle.prototype.chown = function (uid, gid) {
  41. return promisify(this.vol, 'fchown')(this.fd, uid, gid);
  42. };
  43. FileHandle.prototype.close = function () {
  44. return promisify(this.vol, 'close')(this.fd);
  45. };
  46. FileHandle.prototype.datasync = function () {
  47. return promisify(this.vol, 'fdatasync')(this.fd);
  48. };
  49. FileHandle.prototype.read = function (buffer, offset, length, position) {
  50. return promisify(this.vol, 'read', function (bytesRead) { return ({ bytesRead: bytesRead, buffer: buffer }); })(this.fd, buffer, offset, length, position);
  51. };
  52. FileHandle.prototype.readFile = function (options) {
  53. return promisify(this.vol, 'readFile')(this.fd, options);
  54. };
  55. FileHandle.prototype.stat = function (options) {
  56. return promisify(this.vol, 'fstat')(this.fd, options);
  57. };
  58. FileHandle.prototype.sync = function () {
  59. return promisify(this.vol, 'fsync')(this.fd);
  60. };
  61. FileHandle.prototype.truncate = function (len) {
  62. return promisify(this.vol, 'ftruncate')(this.fd, len);
  63. };
  64. FileHandle.prototype.utimes = function (atime, mtime) {
  65. return promisify(this.vol, 'futimes')(this.fd, atime, mtime);
  66. };
  67. FileHandle.prototype.write = function (buffer, offset, length, position) {
  68. return promisify(this.vol, 'write', function (bytesWritten) { return ({ bytesWritten: bytesWritten, buffer: buffer }); })(this.fd, buffer, offset, length, position);
  69. };
  70. FileHandle.prototype.writeFile = function (data, options) {
  71. return promisify(this.vol, 'writeFile')(this.fd, data, options);
  72. };
  73. return FileHandle;
  74. }());
  75. exports.FileHandle = FileHandle;
  76. function createPromisesApi(vol) {
  77. if (typeof Promise === 'undefined')
  78. return null;
  79. return {
  80. FileHandle: FileHandle,
  81. access: function (path, mode) {
  82. return promisify(vol, 'access')(path, mode);
  83. },
  84. appendFile: function (path, data, options) {
  85. return promisify(vol, 'appendFile')(path instanceof FileHandle ? path.fd : path, data, options);
  86. },
  87. chmod: function (path, mode) {
  88. return promisify(vol, 'chmod')(path, mode);
  89. },
  90. chown: function (path, uid, gid) {
  91. return promisify(vol, 'chown')(path, uid, gid);
  92. },
  93. copyFile: function (src, dest, flags) {
  94. return promisify(vol, 'copyFile')(src, dest, flags);
  95. },
  96. lchmod: function (path, mode) {
  97. return promisify(vol, 'lchmod')(path, mode);
  98. },
  99. lchown: function (path, uid, gid) {
  100. return promisify(vol, 'lchown')(path, uid, gid);
  101. },
  102. link: function (existingPath, newPath) {
  103. return promisify(vol, 'link')(existingPath, newPath);
  104. },
  105. lstat: function (path, options) {
  106. return promisify(vol, 'lstat')(path, options);
  107. },
  108. mkdir: function (path, options) {
  109. return promisify(vol, 'mkdir')(path, options);
  110. },
  111. mkdtemp: function (prefix, options) {
  112. return promisify(vol, 'mkdtemp')(prefix, options);
  113. },
  114. open: function (path, flags, mode) {
  115. return promisify(vol, 'open', function (fd) { return new FileHandle(vol, fd); })(path, flags, mode);
  116. },
  117. readdir: function (path, options) {
  118. return promisify(vol, 'readdir')(path, options);
  119. },
  120. readFile: function (id, options) {
  121. return promisify(vol, 'readFile')(id instanceof FileHandle ? id.fd : id, options);
  122. },
  123. readlink: function (path, options) {
  124. return promisify(vol, 'readlink')(path, options);
  125. },
  126. realpath: function (path, options) {
  127. return promisify(vol, 'realpath')(path, options);
  128. },
  129. rename: function (oldPath, newPath) {
  130. return promisify(vol, 'rename')(oldPath, newPath);
  131. },
  132. rmdir: function (path) {
  133. return promisify(vol, 'rmdir')(path);
  134. },
  135. rm: function (path, options) {
  136. return promisify(vol, 'rm')(path, options);
  137. },
  138. stat: function (path, options) {
  139. return promisify(vol, 'stat')(path, options);
  140. },
  141. symlink: function (target, path, type) {
  142. return promisify(vol, 'symlink')(target, path, type);
  143. },
  144. truncate: function (path, len) {
  145. return promisify(vol, 'truncate')(path, len);
  146. },
  147. unlink: function (path) {
  148. return promisify(vol, 'unlink')(path);
  149. },
  150. utimes: function (path, atime, mtime) {
  151. return promisify(vol, 'utimes')(path, atime, mtime);
  152. },
  153. writeFile: function (id, data, options) {
  154. return promisify(vol, 'writeFile')(id instanceof FileHandle ? id.fd : id, data, options);
  155. },
  156. };
  157. }
  158. exports.default = createPromisesApi;