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