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

160 строки
6.3 KiB

  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. const whatwg_mimetype_1 = __importDefault(require("whatwg-mimetype"));
  7. const whatwg_encoding_1 = __importDefault(require("whatwg-encoding"));
  8. const ProgressEvent_1 = __importDefault(require("../event/events/ProgressEvent"));
  9. const DOMException_1 = __importDefault(require("../exception/DOMException"));
  10. const DOMExceptionNameEnum_1 = __importDefault(require("../exception/DOMExceptionNameEnum"));
  11. const FileReaderReadyStateEnum_1 = __importDefault(require("./FileReaderReadyStateEnum"));
  12. const FileReaderFormatEnum_1 = __importDefault(require("./FileReaderFormatEnum"));
  13. const EventTarget_1 = __importDefault(require("../event/EventTarget"));
  14. const FileReaderEventTypeEnum_1 = __importDefault(require("./FileReaderEventTypeEnum"));
  15. /**
  16. * Reference:
  17. * https://developer.mozilla.org/sv-SE/docs/Web/API/FileReader.
  18. *
  19. * Based on:
  20. * https://github.com/jsdom/jsdom/blob/master/lib/jsdom/living/file-api/FileReader-impl.js (MIT licensed).
  21. */
  22. class FileReader extends EventTarget_1.default {
  23. /**
  24. * Constructor.
  25. */
  26. constructor() {
  27. super();
  28. this.error = null;
  29. this.result = null;
  30. this.readyState = FileReaderReadyStateEnum_1.default.empty;
  31. this.onabort = null;
  32. this.onerror = null;
  33. this.onload = null;
  34. this.onloadstart = null;
  35. this.onloadend = null;
  36. this.onprogress = null;
  37. this._ownerDocument = null;
  38. this._isTerminated = false;
  39. this._loadTimeout = null;
  40. this._parseTimeout = null;
  41. this._ownerDocument = this.constructor._ownerDocument;
  42. }
  43. /**
  44. * Reads as ArrayBuffer.
  45. *
  46. * @param blob Blob.
  47. */
  48. readAsArrayBuffer(blob) {
  49. this._readFile(blob, FileReaderFormatEnum_1.default.buffer);
  50. }
  51. /**
  52. * Reads as binary string.
  53. *
  54. * @param blob Blob.
  55. */
  56. readAsBinaryString(blob) {
  57. this._readFile(blob, FileReaderFormatEnum_1.default.binaryString);
  58. }
  59. /**
  60. * Reads as data URL.
  61. *
  62. * @param blob Blob.
  63. */
  64. readAsDataURL(blob) {
  65. this._readFile(blob, FileReaderFormatEnum_1.default.dataURL);
  66. }
  67. /**
  68. * Reads as text.
  69. *
  70. * @param blob Blob.
  71. * @param [encoding] Encoding.
  72. */
  73. readAsText(blob, encoding = null) {
  74. this._readFile(blob, FileReaderFormatEnum_1.default.text, whatwg_encoding_1.default.labelToName(encoding) || 'UTF-8');
  75. }
  76. /**
  77. * Aborts the file reader.
  78. */
  79. abort() {
  80. const window = this._ownerDocument.defaultView;
  81. window.clearTimeout(this._loadTimeout);
  82. window.clearTimeout(this._parseTimeout);
  83. if (this.readyState === FileReaderReadyStateEnum_1.default.empty ||
  84. this.readyState === FileReaderReadyStateEnum_1.default.done) {
  85. this.result = null;
  86. return;
  87. }
  88. if (this.readyState === FileReaderReadyStateEnum_1.default.loading) {
  89. this.readyState = FileReaderReadyStateEnum_1.default.done;
  90. this.result = null;
  91. }
  92. this._isTerminated = true;
  93. this.dispatchEvent(new ProgressEvent_1.default(FileReaderEventTypeEnum_1.default.abort));
  94. this.dispatchEvent(new ProgressEvent_1.default(FileReaderEventTypeEnum_1.default.loadend));
  95. }
  96. /**
  97. * Reads a file.
  98. *
  99. * @param blob Blob.
  100. * @param format Format.
  101. * @param [encoding] Encoding.
  102. */
  103. _readFile(blob, format, encoding = null) {
  104. const window = this._ownerDocument.defaultView;
  105. if (this.readyState === FileReaderReadyStateEnum_1.default.loading) {
  106. throw new DOMException_1.default('The object is in an invalid state.', DOMExceptionNameEnum_1.default.invalidStateError);
  107. }
  108. this.readyState = FileReaderReadyStateEnum_1.default.loading;
  109. this._loadTimeout = window.setTimeout(() => {
  110. if (this._isTerminated) {
  111. this._isTerminated = false;
  112. return;
  113. }
  114. this.dispatchEvent(new ProgressEvent_1.default(FileReaderEventTypeEnum_1.default.loadstart));
  115. let data = blob._buffer;
  116. if (!data) {
  117. data = Buffer.alloc(0);
  118. }
  119. this.dispatchEvent(new ProgressEvent_1.default(FileReaderEventTypeEnum_1.default.loadstart, {
  120. lengthComputable: !isNaN(blob.size),
  121. total: blob.size,
  122. loaded: data.length
  123. }));
  124. this._parseTimeout = window.setTimeout(() => {
  125. if (this._isTerminated) {
  126. this._isTerminated = false;
  127. return;
  128. }
  129. switch (format) {
  130. default:
  131. case FileReaderFormatEnum_1.default.buffer: {
  132. this.result = new Uint8Array(data).buffer;
  133. break;
  134. }
  135. case FileReaderFormatEnum_1.default.binaryString: {
  136. this.result = data.toString('binary');
  137. break;
  138. }
  139. case FileReaderFormatEnum_1.default.dataURL: {
  140. // Spec seems very unclear here; see https://github.com/w3c/FileAPI/issues/104.
  141. const contentType = whatwg_mimetype_1.default.parse(blob.type) || 'application/octet-stream';
  142. (this.result) = `data:${contentType};base64,${data.toString('base64')}`;
  143. break;
  144. }
  145. case FileReaderFormatEnum_1.default.text: {
  146. this.result = whatwg_encoding_1.default.decode(data, encoding);
  147. break;
  148. }
  149. }
  150. this.readyState = FileReaderReadyStateEnum_1.default.done;
  151. this.dispatchEvent(new ProgressEvent_1.default(FileReaderEventTypeEnum_1.default.load));
  152. this.dispatchEvent(new ProgressEvent_1.default(FileReaderEventTypeEnum_1.default.loadend));
  153. });
  154. });
  155. }
  156. }
  157. exports.default = FileReader;
  158. // Owner document is set by a sub-class in the Window constructor
  159. FileReader._ownerDocument = null;
  160. //# sourceMappingURL=FileReader.js.map