-
Notifications
You must be signed in to change notification settings - Fork 23
/
uploader.ts
272 lines (251 loc) · 7.88 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
import { Ajax, AjaxRequestConfig } from './ajax';
import { Canceler } from './canceler';
import {
AuthorizeRequest,
Metadata,
PreRequest,
RequestConfig,
RequestHeaders,
RequestOptions,
ResponseBody,
UploadAction,
UploaderOptions,
UploadState,
UploadStatus,
UploadxControlEvent
} from './interfaces';
import { ErrorType, RetryHandler } from './retry-handler';
import { store } from './store';
import { DynamicChunk, isNumber, unfunc } from './utils';
const actionToStatusMap: { [K in UploadAction]: UploadStatus } = {
pause: 'paused',
upload: 'queue',
cancel: 'cancelled'
};
/**
* Uploader Base Class
*/
export abstract class Uploader implements UploadState {
name: string;
readonly size: number;
readonly uploadId!: string;
response: ResponseBody = null;
responseStatus = 0;
responseHeaders: Record<string, string> = {};
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'];
/** Byte offset within the whole file */
offset? = 0;
/** Retries handler */
retry: RetryHandler;
canceler = new Canceler();
/** Set HttpRequest responseType */
protected responseType?: 'json' | 'text';
private readonly _authorize: AuthorizeRequest;
private readonly _prerequest: PreRequest;
private startTime!: number;
private _token!: string;
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) {
this.status === 'retry' && this.retry.cancel();
this._status = s;
s === 'paused' && this.abort();
['cancelled', 'complete', 'error'].indexOf(s) !== -1 && this.cleanup();
s === 'cancelled' ? this.cancel() : this.stateChange(this);
}
}
constructor(
readonly file: File,
readonly options: Readonly<UploaderOptions>,
readonly stateChange: (evt: UploadState) => void,
readonly ajax: Ajax
) {
this.retry = new RetryHandler(options.retryConfig);
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 || (file as File & { lastModifiedDate: Date }).lastModifiedDate.getTime()
};
this.chunkSize = options.chunkSize || this.size;
this._prerequest = options.prerequest || (req => req);
this._authorize = options.authorize || (req => req);
this.configure(options);
}
/**
* Configure uploader
*/
configure({ metadata, headers, token, endpoint, action }: UploadxControlEvent): void {
endpoint && (this.endpoint = endpoint);
token && (this.token = token);
metadata && Object.assign(this.metadata, unfunc(metadata, this.file));
headers && Object.assign(this.headers, unfunc(headers, this.file));
action && (this.status = actionToStatusMap[action]);
}
/**
* Starts uploading
*/
async upload(): Promise<void> {
this._status = 'uploading';
this.startTime = new Date().getTime();
await this.updateToken();
while (this.status === 'uploading' || this.status === 'retry') {
this.status = 'uploading';
try {
this.url = this.url || (await this.getFileUrl());
this.offset = isNumber(this.offset) ? await this.sendFileContent() : await this.getOffset();
this.retry.reset();
if (this.offset === this.size) {
this.remaining = 0;
this.progress = 100;
this.status = 'complete';
}
} catch (e) {
e instanceof Error && console.error(e);
if (this.status !== 'uploading') {
return;
}
switch (this.retry.kind(this.responseStatus)) {
case ErrorType.Fatal:
this.status = 'error';
return;
case ErrorType.NotFound:
this.url = '';
break;
case ErrorType.Auth:
await this.updateToken();
break;
default:
this.responseStatus >= 400 && (this.offset = undefined);
this.status = 'retry';
await this.retry.wait();
}
}
}
}
/**
* Performs http requests
*/
async request(requestOptions: RequestOptions): Promise<void> {
this.responseStatus = 0;
this.response = null;
this.responseHeaders = {};
let req: RequestConfig = {
body: requestOptions.body || null,
canceler: this.canceler,
headers: { ...this.headers, ...requestOptions.headers },
method: requestOptions.method || 'GET',
url: requestOptions.url || this.url
};
req = await this._authorize(req, this._token);
const { body = null, headers, method, url = req.url } = (await this._prerequest(req)) || req;
const ajaxRequestConfig: AjaxRequestConfig = {
method,
headers: { ...req.headers, ...headers },
url,
data: body,
responseType: this.responseType,
withCredentials: !!this.options.withCredentials,
canceler: this.canceler,
validateStatus: () => true
};
if (body && typeof body !== 'string') {
ajaxRequestConfig.onUploadProgress = this.onProgress();
}
const response = await this.ajax.request(ajaxRequestConfig);
this.response = response.data;
this.responseHeaders = response.headers;
this.responseStatus = response.status;
if (response.status >= 400) {
return Promise.reject();
}
}
/**
* Set auth token cache
*/
updateToken = async (): Promise<string | void> => {
this._token = await unfunc(this.token || '', this.responseStatus);
};
/**
* 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 abort(): void {
this.offset = undefined;
this.canceler.cancel();
}
protected async cancel(): Promise<void> {
this.abort();
if (this.url) {
await this.request({ method: 'DELETE' }).catch(() => {});
}
this.stateChange(this);
}
/**
* Gets the value from the response
*/
protected getValueFromResponse(key: string): string | null {
return this.responseHeaders[key.toLowerCase()] || null;
}
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 cleanup = () => store.delete(this.uploadId);
private onProgress(): (evt: ProgressEvent) => void {
let throttle: ReturnType<typeof setTimeout> | undefined;
return ({ loaded }) => {
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 = setTimeout(() => (throttle = undefined), 500);
this.progress = +((uploaded / this.size) * 100).toFixed(2);
this.remaining = Math.ceil((this.size - uploaded) / this.speed);
this.stateChange(this);
}
};
}
}