-
Notifications
You must be signed in to change notification settings - Fork 23
/
uploader.ts
304 lines (278 loc) · 9 KB
/
uploader.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import { ErrorHandler, ErrorType } from './error-handler';
import {
Metadata,
RequestHeaders,
RequestOptions,
ResponseBody,
UploadAction,
UploaderOptions,
UploadEvent,
UploadState,
UploadStatus,
UploadxControlEvent
} from './interfaces';
import { store } from './store';
import { createHash, DynamicChunk, isNumber, noop, unfunc } from './utils';
const actionToStatusMap: { [K in UploadAction]: UploadStatus } = {
pause: 'paused',
upload: 'queue',
cancel: 'cancelled'
};
/**
* Uploader Base Class
*/
export abstract class Uploader implements UploadState {
readonly name: string;
readonly size: number;
readonly uploadId: string;
response: ResponseBody = null;
responseStatus!: number;
progress!: number;
remaining!: number;
speed!: number;
/** Custom headers */
headers: RequestHeaders = {};
/** Metadata Object */
metadata: Metadata;
/** Upload endpoint */
endpoint = '/upload';
/** Chunk size in bytes */
chunkSize: number;
/** Auth token/tokenGetter */
token: UploadxControlEvent['token'];
/** Retries handler */
protected errorHandler = new ErrorHandler();
/** Active HttpRequest */
protected _xhr!: XMLHttpRequest;
/** byte offset within the whole file */
protected offset? = 0;
/** Set HttpRequest responseType */
protected responseType: XMLHttpRequestResponseType = '';
private readonly prerequest: (
req: RequestOptions
) => Promise<RequestOptions> | RequestOptions | void;
private startTime!: number;
private readonly stateChange: (evt: UploadEvent) => void;
private _url = '';
get url(): string {
return this._url || store.get(this.uploadId) || '';
}
set url(value: string) {
this._url !== value && store.set(this.uploadId, value);
this._url = value;
}
private _status!: UploadStatus;
get status(): UploadStatus {
return this._status;
}
set status(s: UploadStatus) {
if (this._status === 'cancelled' || (this._status === 'complete' && s !== 'cancelled')) {
return;
}
if (s !== this._status) {
s === 'paused' && this.abort();
this._status = s;
['cancelled', 'complete', 'error'].includes(s) && this.cleanup();
s === 'cancelled' ? this.onCancel() : this.stateChange(this);
}
}
constructor(readonly file: File, readonly options: UploaderOptions) {
this.name = file.name;
this.size = file.size;
this.metadata = {
name: file.name,
mimeType: file.type || 'application/octet-stream',
size: file.size,
lastModified: file.lastModified
};
const print = JSON.stringify({
...this.metadata,
type: this.constructor.name,
endpoint: options.endpoint
});
this.uploadId = createHash(print).toString(16);
this.stateChange = options.stateChange || noop;
this.prerequest = options.prerequest || noop;
this.chunkSize = options.chunkSize || this.size;
this.configure(options);
}
/**
* Configure uploader
*/
configure({ metadata = {}, headers = {}, token, endpoint, action }: UploadxControlEvent): void {
this.endpoint = endpoint || this.endpoint;
this.token = token || this.token;
this.metadata = { ...this.metadata, ...unfunc(metadata, this.file) };
this.headers = { ...this.headers, ...unfunc(headers, this.file) };
action && (this.status = actionToStatusMap[action]);
}
/**
* Starts uploading
*/
async upload(): Promise<void> {
this.status = 'uploading';
try {
await this.getToken();
this.offset = undefined;
this.startTime = new Date().getTime();
this.url = this.url || (await this.getFileUrl());
this.errorHandler.reset();
this.start().then(_ => {});
} catch (e) {
e instanceof Error && console.error(e);
if (this.errorHandler.kind(this.responseStatus) !== ErrorType.Fatal) {
this.status = 'retry';
await this.errorHandler.wait();
this.status = 'queue';
} else {
this.status = 'error';
}
}
}
/**
* Starts chunk upload
*/
async start(): Promise<void> {
while (this.status === 'uploading' || this.status === 'retry') {
if (this.offset !== this.size) {
try {
const offset = isNumber(this.offset)
? await this.sendFileContent()
: await this.getOffset();
if (offset === this.offset) {
// noinspection ExceptionCaughtLocallyJS
throw new Error('Content upload failed');
}
this.errorHandler.reset();
this.offset = offset;
} catch (e) {
e instanceof Error && console.error(e);
const errType = this.errorHandler.kind(this.responseStatus);
if (this.responseStatus === 413) {
DynamicChunk.maxSize = this.chunkSize /= 2;
} else if (errType === ErrorType.Fatal) {
this.status = 'error';
} else if (errType === ErrorType.NotFound) {
this.url = '';
this.status = 'queue';
} else if (errType === ErrorType.Auth) {
await this.getToken();
} else {
this.status = 'retry';
await this.errorHandler.wait();
this.offset = this.responseStatus >= 400 ? undefined : this.offset;
this.status = 'uploading';
}
}
} else {
this.progress = 100;
this.remaining = 0;
this.status = 'complete';
}
}
}
/**
* Performs http requests
*/
async request(req: RequestOptions): Promise<ProgressEvent> {
return this._request((await this.prerequest(req)) || req);
}
/**
* Get file URI
*/
protected abstract getFileUrl(): Promise<string>;
/**
* Send file content and return an offset for the next request
*/
protected abstract sendFileContent(): Promise<number | undefined>;
/**
* Get an offset for the next request
*/
protected abstract getOffset(): Promise<number | undefined>;
protected setAuth(token: string): void {
this.headers.Authorization = `Bearer ${token}`;
}
protected abort(): void {
this.offset = undefined;
this._xhr && this._xhr.abort();
}
protected onCancel(): void {
this.abort();
const stateChange = () => this.stateChange(this);
if (this.url) {
this.request({ method: 'DELETE' }).then(stateChange, stateChange);
} else {
stateChange();
}
}
/**
* Gets the value from the response
*/
protected getValueFromResponse(key: string): string | null {
return this._xhr.getResponseHeader(key);
}
/**
* Set auth token
*/
protected getToken(): Promise<string | void> {
return Promise.resolve(unfunc(this.token || '', this.responseStatus)).then(
token => token && this.setAuth(token)
);
}
protected getChunk(): { start: number; end: number; body: Blob } {
this.chunkSize = isNumber(this.options.chunkSize) ? this.chunkSize : DynamicChunk.size;
const start = this.offset || 0;
const end = Math.min(start + this.chunkSize, this.size);
const body = this.file.slice(this.offset, end);
return { start, end, body };
}
private _request(req: RequestOptions): Promise<ProgressEvent> {
return new Promise((resolve, reject) => {
const xhr = (this._xhr = new XMLHttpRequest());
xhr.open(req.method, req.url || this.url, true);
if (req.body instanceof Blob || (req.body && req.progress)) {
xhr.upload.onprogress = this.onProgress();
}
this.responseStatus = 0;
this.response = null;
this.responseType && (xhr.responseType = this.responseType);
this.options.withCredentials && (xhr.withCredentials = true);
const _headers = { ...this.headers, ...(req.headers || {}) };
Object.keys(_headers).forEach(key => xhr.setRequestHeader(key, String(_headers[key])));
xhr.onload = (evt: ProgressEvent) => {
this.responseStatus = xhr.status;
// TODO: check null body status
this.response = this.responseStatus !== 204 ? this.getResponseBody(xhr) : '';
this.responseStatus >= 400 ? reject(evt) : resolve(evt);
};
xhr.onerror = reject;
xhr.send(req.body);
});
}
private cleanup = () => store.delete(this.uploadId);
getResponseBody(xhr: XMLHttpRequest): ResponseBody {
let body = 'response' in (xhr as XMLHttpRequest) ? xhr.response : xhr.responseText;
if (body && this.responseType === 'json' && typeof body === 'string') {
try {
body = JSON.parse(body);
} catch {}
}
return body;
}
private onProgress(): (evt: ProgressEvent) => void {
let throttle = 0;
return ({ loaded }: ProgressEvent) => {
const now = new Date().getTime();
const uploaded = (this.offset as number) + loaded;
const elapsedTime = (now - this.startTime) / 1000;
this.speed = Math.round(uploaded / elapsedTime);
DynamicChunk.scale(this.speed);
if (!throttle) {
throttle = window.setTimeout(() => (throttle = 0), 500);
this.progress = +((uploaded / this.size) * 100).toFixed(2);
this.remaining = Math.ceil((this.size - uploaded) / this.speed);
this.stateChange(this);
}
};
}
}