版博士V2.0程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

102 строки
2.7 KiB

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