NodeFS.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.NodeFS = void 0;
  4. const tslib_1 = require("tslib");
  5. const fs_1 = tslib_1.__importDefault(require("fs"));
  6. const FakeFS_1 = require("./FakeFS");
  7. const errors_1 = require("./errors");
  8. const path_1 = require("./path");
  9. class NodeFS extends FakeFS_1.BasePortableFakeFS {
  10. constructor(realFs = fs_1.default) {
  11. super();
  12. this.realFs = realFs;
  13. // @ts-expect-error
  14. if (typeof this.realFs.lutimes !== `undefined`) {
  15. this.lutimesPromise = this.lutimesPromiseImpl;
  16. this.lutimesSync = this.lutimesSyncImpl;
  17. }
  18. }
  19. getExtractHint() {
  20. return false;
  21. }
  22. getRealPath() {
  23. return path_1.PortablePath.root;
  24. }
  25. resolve(p) {
  26. return path_1.ppath.resolve(p);
  27. }
  28. async openPromise(p, flags, mode) {
  29. return await new Promise((resolve, reject) => {
  30. this.realFs.open(path_1.npath.fromPortablePath(p), flags, mode, this.makeCallback(resolve, reject));
  31. });
  32. }
  33. openSync(p, flags, mode) {
  34. return this.realFs.openSync(path_1.npath.fromPortablePath(p), flags, mode);
  35. }
  36. async opendirPromise(p, opts) {
  37. return await new Promise((resolve, reject) => {
  38. if (typeof opts !== `undefined`) {
  39. this.realFs.opendir(path_1.npath.fromPortablePath(p), opts, this.makeCallback(resolve, reject));
  40. }
  41. else {
  42. this.realFs.opendir(path_1.npath.fromPortablePath(p), this.makeCallback(resolve, reject));
  43. }
  44. }).then(dir => {
  45. return Object.defineProperty(dir, `path`, { value: p, configurable: true, writable: true });
  46. });
  47. }
  48. opendirSync(p, opts) {
  49. const dir = typeof opts !== `undefined`
  50. ? this.realFs.opendirSync(path_1.npath.fromPortablePath(p), opts)
  51. : this.realFs.opendirSync(path_1.npath.fromPortablePath(p));
  52. return Object.defineProperty(dir, `path`, { value: p, configurable: true, writable: true });
  53. }
  54. async readPromise(fd, buffer, offset = 0, length = 0, position = -1) {
  55. return await new Promise((resolve, reject) => {
  56. this.realFs.read(fd, buffer, offset, length, position, (error, bytesRead) => {
  57. if (error) {
  58. reject(error);
  59. }
  60. else {
  61. resolve(bytesRead);
  62. }
  63. });
  64. });
  65. }
  66. readSync(fd, buffer, offset, length, position) {
  67. return this.realFs.readSync(fd, buffer, offset, length, position);
  68. }
  69. async writePromise(fd, buffer, offset, length, position) {
  70. return await new Promise((resolve, reject) => {
  71. if (typeof buffer === `string`) {
  72. return this.realFs.write(fd, buffer, offset, this.makeCallback(resolve, reject));
  73. }
  74. else {
  75. return this.realFs.write(fd, buffer, offset, length, position, this.makeCallback(resolve, reject));
  76. }
  77. });
  78. }
  79. writeSync(fd, buffer, offset, length, position) {
  80. if (typeof buffer === `string`) {
  81. return this.realFs.writeSync(fd, buffer, offset);
  82. }
  83. else {
  84. return this.realFs.writeSync(fd, buffer, offset, length, position);
  85. }
  86. }
  87. async closePromise(fd) {
  88. await new Promise((resolve, reject) => {
  89. this.realFs.close(fd, this.makeCallback(resolve, reject));
  90. });
  91. }
  92. closeSync(fd) {
  93. this.realFs.closeSync(fd);
  94. }
  95. createReadStream(p, opts) {
  96. const realPath = (p !== null ? path_1.npath.fromPortablePath(p) : p);
  97. return this.realFs.createReadStream(realPath, opts);
  98. }
  99. createWriteStream(p, opts) {
  100. const realPath = (p !== null ? path_1.npath.fromPortablePath(p) : p);
  101. return this.realFs.createWriteStream(realPath, opts);
  102. }
  103. async realpathPromise(p) {
  104. return await new Promise((resolve, reject) => {
  105. this.realFs.realpath(path_1.npath.fromPortablePath(p), {}, this.makeCallback(resolve, reject));
  106. }).then(path => {
  107. return path_1.npath.toPortablePath(path);
  108. });
  109. }
  110. realpathSync(p) {
  111. return path_1.npath.toPortablePath(this.realFs.realpathSync(path_1.npath.fromPortablePath(p), {}));
  112. }
  113. async existsPromise(p) {
  114. return await new Promise(resolve => {
  115. this.realFs.exists(path_1.npath.fromPortablePath(p), resolve);
  116. });
  117. }
  118. accessSync(p, mode) {
  119. return this.realFs.accessSync(path_1.npath.fromPortablePath(p), mode);
  120. }
  121. async accessPromise(p, mode) {
  122. return await new Promise((resolve, reject) => {
  123. this.realFs.access(path_1.npath.fromPortablePath(p), mode, this.makeCallback(resolve, reject));
  124. });
  125. }
  126. existsSync(p) {
  127. return this.realFs.existsSync(path_1.npath.fromPortablePath(p));
  128. }
  129. async statPromise(p, opts) {
  130. return await new Promise((resolve, reject) => {
  131. if (opts) {
  132. // @ts-expect-error The node types are out of date
  133. this.realFs.stat(path_1.npath.fromPortablePath(p), opts, this.makeCallback(resolve, reject));
  134. }
  135. else {
  136. this.realFs.stat(path_1.npath.fromPortablePath(p), this.makeCallback(resolve, reject));
  137. }
  138. });
  139. }
  140. statSync(p, opts) {
  141. if (opts) {
  142. // @ts-expect-error The node types are out of date
  143. return this.realFs.statSync(path_1.npath.fromPortablePath(p), opts);
  144. }
  145. else {
  146. return this.realFs.statSync(path_1.npath.fromPortablePath(p));
  147. }
  148. }
  149. async fstatPromise(fd, opts) {
  150. return await new Promise((resolve, reject) => {
  151. if (opts) {
  152. // @ts-expect-error - The node typings doesn't know about the options
  153. this.realFs.fstat(fd, opts, this.makeCallback(resolve, reject));
  154. }
  155. else {
  156. this.realFs.fstat(fd, this.makeCallback(resolve, reject));
  157. }
  158. });
  159. }
  160. fstatSync(fd, opts) {
  161. if (opts) {
  162. // @ts-expect-error - The node typings doesn't know about the options
  163. return this.realFs.fstatSync(fd, opts);
  164. }
  165. else {
  166. return this.realFs.fstatSync(fd);
  167. }
  168. }
  169. async lstatPromise(p, opts) {
  170. return await new Promise((resolve, reject) => {
  171. if (opts) {
  172. // @ts-expect-error - TS does not know this takes options
  173. this.realFs.lstat(path_1.npath.fromPortablePath(p), opts, this.makeCallback(resolve, reject));
  174. }
  175. else {
  176. this.realFs.lstat(path_1.npath.fromPortablePath(p), this.makeCallback(resolve, reject));
  177. }
  178. });
  179. }
  180. lstatSync(p, opts) {
  181. if (opts) {
  182. // @ts-expect-error - TS does not know this takes options
  183. return this.realFs.lstatSync(path_1.npath.fromPortablePath(p), opts);
  184. }
  185. else {
  186. return this.realFs.lstatSync(path_1.npath.fromPortablePath(p));
  187. }
  188. }
  189. async fchmodPromise(fd, mask) {
  190. return await new Promise((resolve, reject) => {
  191. this.realFs.fchmod(fd, mask, this.makeCallback(resolve, reject));
  192. });
  193. }
  194. fchmodSync(fd, mask) {
  195. return this.realFs.fchmodSync(fd, mask);
  196. }
  197. async chmodPromise(p, mask) {
  198. return await new Promise((resolve, reject) => {
  199. this.realFs.chmod(path_1.npath.fromPortablePath(p), mask, this.makeCallback(resolve, reject));
  200. });
  201. }
  202. chmodSync(p, mask) {
  203. return this.realFs.chmodSync(path_1.npath.fromPortablePath(p), mask);
  204. }
  205. async fchownPromise(fd, uid, gid) {
  206. return await new Promise((resolve, reject) => {
  207. this.realFs.fchown(fd, uid, gid, this.makeCallback(resolve, reject));
  208. });
  209. }
  210. fchownSync(fd, uid, gid) {
  211. return this.realFs.fchownSync(fd, uid, gid);
  212. }
  213. async chownPromise(p, uid, gid) {
  214. return await new Promise((resolve, reject) => {
  215. this.realFs.chown(path_1.npath.fromPortablePath(p), uid, gid, this.makeCallback(resolve, reject));
  216. });
  217. }
  218. chownSync(p, uid, gid) {
  219. return this.realFs.chownSync(path_1.npath.fromPortablePath(p), uid, gid);
  220. }
  221. async renamePromise(oldP, newP) {
  222. return await new Promise((resolve, reject) => {
  223. this.realFs.rename(path_1.npath.fromPortablePath(oldP), path_1.npath.fromPortablePath(newP), this.makeCallback(resolve, reject));
  224. });
  225. }
  226. renameSync(oldP, newP) {
  227. return this.realFs.renameSync(path_1.npath.fromPortablePath(oldP), path_1.npath.fromPortablePath(newP));
  228. }
  229. async copyFilePromise(sourceP, destP, flags = 0) {
  230. return await new Promise((resolve, reject) => {
  231. this.realFs.copyFile(path_1.npath.fromPortablePath(sourceP), path_1.npath.fromPortablePath(destP), flags, this.makeCallback(resolve, reject));
  232. });
  233. }
  234. copyFileSync(sourceP, destP, flags = 0) {
  235. return this.realFs.copyFileSync(path_1.npath.fromPortablePath(sourceP), path_1.npath.fromPortablePath(destP), flags);
  236. }
  237. async appendFilePromise(p, content, opts) {
  238. return await new Promise((resolve, reject) => {
  239. const fsNativePath = typeof p === `string` ? path_1.npath.fromPortablePath(p) : p;
  240. if (opts) {
  241. this.realFs.appendFile(fsNativePath, content, opts, this.makeCallback(resolve, reject));
  242. }
  243. else {
  244. this.realFs.appendFile(fsNativePath, content, this.makeCallback(resolve, reject));
  245. }
  246. });
  247. }
  248. appendFileSync(p, content, opts) {
  249. const fsNativePath = typeof p === `string` ? path_1.npath.fromPortablePath(p) : p;
  250. if (opts) {
  251. this.realFs.appendFileSync(fsNativePath, content, opts);
  252. }
  253. else {
  254. this.realFs.appendFileSync(fsNativePath, content);
  255. }
  256. }
  257. async writeFilePromise(p, content, opts) {
  258. return await new Promise((resolve, reject) => {
  259. const fsNativePath = typeof p === `string` ? path_1.npath.fromPortablePath(p) : p;
  260. if (opts) {
  261. this.realFs.writeFile(fsNativePath, content, opts, this.makeCallback(resolve, reject));
  262. }
  263. else {
  264. this.realFs.writeFile(fsNativePath, content, this.makeCallback(resolve, reject));
  265. }
  266. });
  267. }
  268. writeFileSync(p, content, opts) {
  269. const fsNativePath = typeof p === `string` ? path_1.npath.fromPortablePath(p) : p;
  270. if (opts) {
  271. this.realFs.writeFileSync(fsNativePath, content, opts);
  272. }
  273. else {
  274. this.realFs.writeFileSync(fsNativePath, content);
  275. }
  276. }
  277. async unlinkPromise(p) {
  278. return await new Promise((resolve, reject) => {
  279. this.realFs.unlink(path_1.npath.fromPortablePath(p), this.makeCallback(resolve, reject));
  280. });
  281. }
  282. unlinkSync(p) {
  283. return this.realFs.unlinkSync(path_1.npath.fromPortablePath(p));
  284. }
  285. async utimesPromise(p, atime, mtime) {
  286. return await new Promise((resolve, reject) => {
  287. this.realFs.utimes(path_1.npath.fromPortablePath(p), atime, mtime, this.makeCallback(resolve, reject));
  288. });
  289. }
  290. utimesSync(p, atime, mtime) {
  291. this.realFs.utimesSync(path_1.npath.fromPortablePath(p), atime, mtime);
  292. }
  293. async lutimesPromiseImpl(p, atime, mtime) {
  294. // @ts-expect-error: Not yet in DefinitelyTyped
  295. const lutimes = this.realFs.lutimes;
  296. if (typeof lutimes === `undefined`)
  297. throw (0, errors_1.ENOSYS)(`unavailable Node binding`, `lutimes '${p}'`);
  298. return await new Promise((resolve, reject) => {
  299. lutimes.call(this.realFs, path_1.npath.fromPortablePath(p), atime, mtime, this.makeCallback(resolve, reject));
  300. });
  301. }
  302. lutimesSyncImpl(p, atime, mtime) {
  303. // @ts-expect-error: Not yet in DefinitelyTyped
  304. const lutimesSync = this.realFs.lutimesSync;
  305. if (typeof lutimesSync === `undefined`)
  306. throw (0, errors_1.ENOSYS)(`unavailable Node binding`, `lutimes '${p}'`);
  307. lutimesSync.call(this.realFs, path_1.npath.fromPortablePath(p), atime, mtime);
  308. }
  309. async mkdirPromise(p, opts) {
  310. return await new Promise((resolve, reject) => {
  311. // @ts-expect-error - Types are outdated, the second argument in the callback is either a string or undefined
  312. this.realFs.mkdir(path_1.npath.fromPortablePath(p), opts, this.makeCallback(resolve, reject));
  313. });
  314. }
  315. mkdirSync(p, opts) {
  316. // @ts-expect-error - Types are outdated, returns either a string or undefined
  317. return this.realFs.mkdirSync(path_1.npath.fromPortablePath(p), opts);
  318. }
  319. async rmdirPromise(p, opts) {
  320. return await new Promise((resolve, reject) => {
  321. // TODO: always pass opts when min node version is 12.10+
  322. if (opts) {
  323. this.realFs.rmdir(path_1.npath.fromPortablePath(p), opts, this.makeCallback(resolve, reject));
  324. }
  325. else {
  326. this.realFs.rmdir(path_1.npath.fromPortablePath(p), this.makeCallback(resolve, reject));
  327. }
  328. });
  329. }
  330. rmdirSync(p, opts) {
  331. return this.realFs.rmdirSync(path_1.npath.fromPortablePath(p), opts);
  332. }
  333. async linkPromise(existingP, newP) {
  334. return await new Promise((resolve, reject) => {
  335. this.realFs.link(path_1.npath.fromPortablePath(existingP), path_1.npath.fromPortablePath(newP), this.makeCallback(resolve, reject));
  336. });
  337. }
  338. linkSync(existingP, newP) {
  339. return this.realFs.linkSync(path_1.npath.fromPortablePath(existingP), path_1.npath.fromPortablePath(newP));
  340. }
  341. async symlinkPromise(target, p, type) {
  342. return await new Promise((resolve, reject) => {
  343. this.realFs.symlink(path_1.npath.fromPortablePath(target.replace(/\/+$/, ``)), path_1.npath.fromPortablePath(p), type, this.makeCallback(resolve, reject));
  344. });
  345. }
  346. symlinkSync(target, p, type) {
  347. return this.realFs.symlinkSync(path_1.npath.fromPortablePath(target.replace(/\/+$/, ``)), path_1.npath.fromPortablePath(p), type);
  348. }
  349. async readFilePromise(p, encoding) {
  350. return await new Promise((resolve, reject) => {
  351. const fsNativePath = typeof p === `string` ? path_1.npath.fromPortablePath(p) : p;
  352. this.realFs.readFile(fsNativePath, encoding, this.makeCallback(resolve, reject));
  353. });
  354. }
  355. readFileSync(p, encoding) {
  356. const fsNativePath = typeof p === `string` ? path_1.npath.fromPortablePath(p) : p;
  357. return this.realFs.readFileSync(fsNativePath, encoding);
  358. }
  359. async readdirPromise(p, opts) {
  360. return await new Promise((resolve, reject) => {
  361. if (opts === null || opts === void 0 ? void 0 : opts.withFileTypes) {
  362. this.realFs.readdir(path_1.npath.fromPortablePath(p), { withFileTypes: true }, this.makeCallback(resolve, reject));
  363. }
  364. else {
  365. this.realFs.readdir(path_1.npath.fromPortablePath(p), this.makeCallback(value => resolve(value), reject));
  366. }
  367. });
  368. }
  369. readdirSync(p, opts) {
  370. if (opts === null || opts === void 0 ? void 0 : opts.withFileTypes) {
  371. return this.realFs.readdirSync(path_1.npath.fromPortablePath(p), { withFileTypes: true });
  372. }
  373. else {
  374. return this.realFs.readdirSync(path_1.npath.fromPortablePath(p));
  375. }
  376. }
  377. async readlinkPromise(p) {
  378. return await new Promise((resolve, reject) => {
  379. this.realFs.readlink(path_1.npath.fromPortablePath(p), this.makeCallback(resolve, reject));
  380. }).then(path => {
  381. return path_1.npath.toPortablePath(path);
  382. });
  383. }
  384. readlinkSync(p) {
  385. return path_1.npath.toPortablePath(this.realFs.readlinkSync(path_1.npath.fromPortablePath(p)));
  386. }
  387. async truncatePromise(p, len) {
  388. return await new Promise((resolve, reject) => {
  389. this.realFs.truncate(path_1.npath.fromPortablePath(p), len, this.makeCallback(resolve, reject));
  390. });
  391. }
  392. truncateSync(p, len) {
  393. return this.realFs.truncateSync(path_1.npath.fromPortablePath(p), len);
  394. }
  395. async ftruncatePromise(fd, len) {
  396. return await new Promise((resolve, reject) => {
  397. this.realFs.ftruncate(fd, len, this.makeCallback(resolve, reject));
  398. });
  399. }
  400. ftruncateSync(fd, len) {
  401. return this.realFs.ftruncateSync(fd, len);
  402. }
  403. watch(p, a, b) {
  404. return this.realFs.watch(path_1.npath.fromPortablePath(p),
  405. // @ts-expect-error
  406. a, b);
  407. }
  408. watchFile(p, a, b) {
  409. return this.realFs.watchFile(path_1.npath.fromPortablePath(p),
  410. // @ts-expect-error
  411. a, b);
  412. }
  413. unwatchFile(p, cb) {
  414. return this.realFs.unwatchFile(path_1.npath.fromPortablePath(p), cb);
  415. }
  416. makeCallback(resolve, reject) {
  417. return (err, result) => {
  418. if (err) {
  419. reject(err);
  420. }
  421. else {
  422. resolve(result);
  423. }
  424. };
  425. }
  426. }
  427. exports.NodeFS = NodeFS;