file.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  4. return new (P || (P = Promise))(function (resolve, reject) {
  5. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  6. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  7. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  8. step((generator = generator.apply(thisArg, _arguments || [])).next());
  9. });
  10. };
  11. var __importDefault = (this && this.__importDefault) || function (mod) {
  12. return (mod && mod.__esModule) ? mod : { "default": mod };
  13. };
  14. Object.defineProperty(exports, "__esModule", { value: true });
  15. const debug_1 = __importDefault(require("debug"));
  16. const fs_1 = require("fs");
  17. const fs_extra_1 = require("fs-extra");
  18. const file_uri_to_path_1 = __importDefault(require("file-uri-to-path"));
  19. const notfound_1 = __importDefault(require("./notfound"));
  20. const notmodified_1 = __importDefault(require("./notmodified"));
  21. const debug = debug_1.default('get-uri:file');
  22. /**
  23. * Returns a `fs.ReadStream` instance from a "file:" URI.
  24. */
  25. function get({ href: uri }, opts) {
  26. return __awaiter(this, void 0, void 0, function* () {
  27. const { cache, flags = 'r', mode = 438 // =0666
  28. } = opts;
  29. try {
  30. // Convert URI → Path
  31. const filepath = file_uri_to_path_1.default(uri);
  32. debug('Normalized pathname: %o', filepath);
  33. // `open()` first to get a file descriptor and ensure that the file
  34. // exists.
  35. const fd = yield fs_extra_1.open(filepath, flags, mode);
  36. // Now `fstat()` to check the `mtime` and store the stat object for
  37. // the cache.
  38. const stat = yield fs_extra_1.fstat(fd);
  39. // if a `cache` was provided, check if the file has not been modified
  40. if (cache && cache.stat && stat && isNotModified(cache.stat, stat)) {
  41. throw new notmodified_1.default();
  42. }
  43. // `fs.ReadStream` takes care of calling `fs.close()` on the
  44. // fd after it's done reading
  45. // @ts-ignore - `@types/node` doesn't allow `null` as file path :/
  46. const rs = fs_1.createReadStream(null, Object.assign(Object.assign({ autoClose: true }, opts), { fd }));
  47. rs.stat = stat;
  48. return rs;
  49. }
  50. catch (err) {
  51. if (err.code === 'ENOENT') {
  52. throw new notfound_1.default();
  53. }
  54. throw err;
  55. }
  56. });
  57. }
  58. exports.default = get;
  59. // returns `true` if the `mtime` of the 2 stat objects are equal
  60. function isNotModified(prev, curr) {
  61. return +prev.mtime === +curr.mtime;
  62. }
  63. //# sourceMappingURL=file.js.map