版博士V2.0程序
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

node-fetch-native.0a3ae5c6.mjs 2.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { statSync, createReadStream, promises } from 'node:fs';
  2. import { basename } from 'node:path';
  3. import { _ as _Blob, F as File, n as nodeDomexception } from './node-fetch-native.2b047dc1.mjs';
  4. const { stat } = promises;
  5. /**
  6. * @param {string} path filepath on the disk
  7. * @param {string} [type] mimetype to use
  8. */
  9. const blobFromSync = (path, type) => fromBlob(statSync(path), path, type);
  10. /**
  11. * @param {string} path filepath on the disk
  12. * @param {string} [type] mimetype to use
  13. * @returns {Promise<Blob>}
  14. */
  15. const blobFrom = (path, type) => stat(path).then(stat => fromBlob(stat, path, type));
  16. /**
  17. * @param {string} path filepath on the disk
  18. * @param {string} [type] mimetype to use
  19. * @returns {Promise<File>}
  20. */
  21. const fileFrom = (path, type) => stat(path).then(stat => fromFile(stat, path, type));
  22. /**
  23. * @param {string} path filepath on the disk
  24. * @param {string} [type] mimetype to use
  25. */
  26. const fileFromSync = (path, type) => fromFile(statSync(path), path, type);
  27. // @ts-ignore
  28. const fromBlob = (stat, path, type = '') => new _Blob([new BlobDataItem({
  29. path,
  30. size: stat.size,
  31. lastModified: stat.mtimeMs,
  32. start: 0
  33. })], { type });
  34. // @ts-ignore
  35. const fromFile = (stat, path, type = '') => new File([new BlobDataItem({
  36. path,
  37. size: stat.size,
  38. lastModified: stat.mtimeMs,
  39. start: 0
  40. })], basename(path), { type, lastModified: stat.mtimeMs });
  41. /**
  42. * This is a blob backed up by a file on the disk
  43. * with minium requirement. Its wrapped around a Blob as a blobPart
  44. * so you have no direct access to this.
  45. *
  46. * @private
  47. */
  48. class BlobDataItem {
  49. #path
  50. #start
  51. constructor (options) {
  52. this.#path = options.path;
  53. this.#start = options.start;
  54. this.size = options.size;
  55. this.lastModified = options.lastModified;
  56. }
  57. /**
  58. * Slicing arguments is first validated and formatted
  59. * to not be out of range by Blob.prototype.slice
  60. */
  61. slice (start, end) {
  62. return new BlobDataItem({
  63. path: this.#path,
  64. lastModified: this.lastModified,
  65. size: end - start,
  66. start: this.#start + start
  67. })
  68. }
  69. async * stream () {
  70. const { mtimeMs } = await stat(this.#path);
  71. if (mtimeMs > this.lastModified) {
  72. throw new nodeDomexception('The requested file could not be read, typically due to permission problems that have occurred after a reference to a file was acquired.', 'NotReadableError')
  73. }
  74. yield * createReadStream(this.#path, {
  75. start: this.#start,
  76. end: this.#start + this.size - 1
  77. });
  78. }
  79. get [Symbol.toStringTag] () {
  80. return 'Blob'
  81. }
  82. }
  83. export { blobFromSync as a, blobFrom as b, fileFromSync as c, fileFrom as f };