fileFromPath.js 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
  2. if (kind === "m") throw new TypeError("Private method is not writable");
  3. if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
  4. if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
  5. return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
  6. };
  7. var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
  8. if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
  9. if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
  10. return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
  11. };
  12. var _FileFromPath_path, _FileFromPath_start;
  13. import { statSync, createReadStream, promises as fs } from "fs";
  14. import { basename } from "path";
  15. import DOMException from "node-domexception";
  16. import { File } from "./File.js";
  17. import isPlainObject from "./isPlainObject.js";
  18. export * from "./isFile.js";
  19. const MESSAGE = "The requested file could not be read, "
  20. + "typically due to permission problems that have occurred after a reference "
  21. + "to a file was acquired.";
  22. class FileFromPath {
  23. constructor(input) {
  24. _FileFromPath_path.set(this, void 0);
  25. _FileFromPath_start.set(this, void 0);
  26. __classPrivateFieldSet(this, _FileFromPath_path, input.path, "f");
  27. __classPrivateFieldSet(this, _FileFromPath_start, input.start || 0, "f");
  28. this.name = basename(__classPrivateFieldGet(this, _FileFromPath_path, "f"));
  29. this.size = input.size;
  30. this.lastModified = input.lastModified;
  31. }
  32. slice(start, end) {
  33. return new FileFromPath({
  34. path: __classPrivateFieldGet(this, _FileFromPath_path, "f"),
  35. lastModified: this.lastModified,
  36. size: end - start,
  37. start
  38. });
  39. }
  40. async *stream() {
  41. const { mtimeMs } = await fs.stat(__classPrivateFieldGet(this, _FileFromPath_path, "f"));
  42. if (mtimeMs > this.lastModified) {
  43. throw new DOMException(MESSAGE, "NotReadableError");
  44. }
  45. if (this.size) {
  46. yield* createReadStream(__classPrivateFieldGet(this, _FileFromPath_path, "f"), {
  47. start: __classPrivateFieldGet(this, _FileFromPath_start, "f"),
  48. end: __classPrivateFieldGet(this, _FileFromPath_start, "f") + this.size - 1
  49. });
  50. }
  51. }
  52. get [(_FileFromPath_path = new WeakMap(), _FileFromPath_start = new WeakMap(), Symbol.toStringTag)]() {
  53. return "File";
  54. }
  55. }
  56. function createFileFromPath(path, { mtimeMs, size }, filenameOrOptions, options = {}) {
  57. let filename;
  58. if (isPlainObject(filenameOrOptions)) {
  59. [options, filename] = [filenameOrOptions, undefined];
  60. }
  61. else {
  62. filename = filenameOrOptions;
  63. }
  64. const file = new FileFromPath({ path, size, lastModified: mtimeMs });
  65. if (!filename) {
  66. filename = file.name;
  67. }
  68. return new File([file], filename, {
  69. ...options, lastModified: file.lastModified
  70. });
  71. }
  72. export function fileFromPathSync(path, filenameOrOptions, options = {}) {
  73. const stats = statSync(path);
  74. return createFileFromPath(path, stats, filenameOrOptions, options);
  75. }
  76. export async function fileFromPath(path, filenameOrOptions, options) {
  77. const stats = await fs.stat(path);
  78. return createFileFromPath(path, stats, filenameOrOptions, options);
  79. }