@uploadcare/upload-client 3.1.2-alpha.0 → 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -4
- package/dist/index.browser.js +1443 -1461
- package/dist/{index.js → index.node.js} +1470 -1478
- package/dist/index.react-native.js +1545 -0
- package/dist/types.d.ts +45 -37
- package/package.json +23 -55
- package/LICENSE +0 -21
- package/dist/index.browser.cjs +0 -1753
- package/dist/index.cjs +0 -1790
package/dist/index.browser.js
CHANGED
|
@@ -1,1547 +1,1529 @@
|
|
|
1
|
-
class UploadClientError extends Error {
|
|
2
|
-
constructor(message, code, request, response, headers) {
|
|
3
|
-
super();
|
|
4
|
-
this.name = 'UploadClientError';
|
|
5
|
-
this.message = message;
|
|
6
|
-
this.code = code;
|
|
7
|
-
this.request = request;
|
|
8
|
-
this.response = response;
|
|
9
|
-
this.headers = headers;
|
|
10
|
-
Object.setPrototypeOf(this, UploadClientError.prototype);
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
const cancelError = (message = 'Request canceled') => {
|
|
14
|
-
const error = new UploadClientError(message);
|
|
15
|
-
error.isCancel = true;
|
|
16
|
-
return error;
|
|
1
|
+
class UploadClientError extends Error {
|
|
2
|
+
constructor(message, code, request, response, headers) {
|
|
3
|
+
super();
|
|
4
|
+
this.name = 'UploadClientError';
|
|
5
|
+
this.message = message;
|
|
6
|
+
this.code = code;
|
|
7
|
+
this.request = request;
|
|
8
|
+
this.response = response;
|
|
9
|
+
this.headers = headers;
|
|
10
|
+
Object.setPrototypeOf(this, UploadClientError.prototype);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
const cancelError = (message = 'Request canceled') => {
|
|
14
|
+
const error = new UploadClientError(message);
|
|
15
|
+
error.isCancel = true;
|
|
16
|
+
return error;
|
|
17
17
|
};
|
|
18
18
|
|
|
19
|
-
const onCancel = (signal, callback) => {
|
|
20
|
-
if (signal) {
|
|
21
|
-
if (signal.aborted) {
|
|
22
|
-
Promise.resolve().then(callback);
|
|
23
|
-
}
|
|
24
|
-
else {
|
|
25
|
-
signal.addEventListener('abort', () => callback(), { once: true });
|
|
26
|
-
}
|
|
27
|
-
}
|
|
19
|
+
const onCancel = (signal, callback) => {
|
|
20
|
+
if (signal) {
|
|
21
|
+
if (signal.aborted) {
|
|
22
|
+
Promise.resolve().then(callback);
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
signal.addEventListener('abort', () => callback(), { once: true });
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
28
|
};
|
|
29
29
|
|
|
30
|
-
const request = ({ method, url, data, headers = {}, signal, onProgress }) => new Promise((resolve, reject) => {
|
|
31
|
-
const xhr = new XMLHttpRequest();
|
|
32
|
-
const requestMethod =
|
|
33
|
-
let aborted = false;
|
|
34
|
-
xhr.open(requestMethod, url);
|
|
35
|
-
if (headers) {
|
|
36
|
-
Object.entries(headers).forEach((entry) => {
|
|
37
|
-
const [key, value] = entry;
|
|
38
|
-
typeof value !== 'undefined' &&
|
|
39
|
-
!Array.isArray(value) &&
|
|
40
|
-
xhr.setRequestHeader(key, value);
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
xhr.responseType = 'text';
|
|
44
|
-
onCancel(signal, () => {
|
|
45
|
-
aborted = true;
|
|
46
|
-
xhr.abort();
|
|
47
|
-
reject(cancelError());
|
|
48
|
-
});
|
|
49
|
-
xhr.onload = () => {
|
|
50
|
-
if (xhr.status != 200) {
|
|
51
|
-
// analyze HTTP status of the response
|
|
52
|
-
reject(new Error(`Error ${xhr.status}: ${xhr.statusText}`)); // e.g. 404: Not Found
|
|
53
|
-
}
|
|
54
|
-
else {
|
|
55
|
-
const request = {
|
|
56
|
-
method: requestMethod,
|
|
57
|
-
url,
|
|
58
|
-
data,
|
|
59
|
-
headers: headers || undefined,
|
|
60
|
-
signal,
|
|
61
|
-
onProgress
|
|
62
|
-
};
|
|
63
|
-
// Convert the header string into an array
|
|
64
|
-
// of individual headers
|
|
65
|
-
const headersArray = xhr
|
|
66
|
-
.getAllResponseHeaders()
|
|
67
|
-
.trim()
|
|
68
|
-
.split(/[\r\n]+/);
|
|
69
|
-
// Create a map of header names to values
|
|
70
|
-
const responseHeaders = {};
|
|
71
|
-
headersArray.forEach(function (line) {
|
|
72
|
-
const parts = line.split(': ');
|
|
73
|
-
const header = parts.shift();
|
|
74
|
-
const value = parts.join(': ');
|
|
75
|
-
if (header && typeof header !== 'undefined') {
|
|
76
|
-
responseHeaders[header] = value;
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
const responseData = xhr.response;
|
|
80
|
-
const responseStatus = xhr.status;
|
|
81
|
-
resolve({
|
|
82
|
-
request,
|
|
83
|
-
data: responseData,
|
|
84
|
-
headers: responseHeaders,
|
|
85
|
-
status: responseStatus
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
};
|
|
89
|
-
xhr.onerror = () => {
|
|
90
|
-
if (aborted)
|
|
91
|
-
return;
|
|
92
|
-
// only triggers if the request couldn't be made at all
|
|
93
|
-
reject(new Error('Network error'));
|
|
94
|
-
};
|
|
95
|
-
if (onProgress && typeof onProgress === 'function') {
|
|
96
|
-
xhr.upload.onprogress = (event) => {
|
|
97
|
-
if (event.lengthComputable) {
|
|
98
|
-
onProgress({
|
|
99
|
-
isComputable: true,
|
|
100
|
-
value: event.loaded / event.total
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
else {
|
|
104
|
-
onProgress({ isComputable: false });
|
|
105
|
-
}
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
|
-
if (data) {
|
|
109
|
-
xhr.send(data);
|
|
110
|
-
}
|
|
111
|
-
else {
|
|
112
|
-
xhr.send();
|
|
113
|
-
}
|
|
30
|
+
const request = ({ method, url, data, headers = {}, signal, onProgress }) => new Promise((resolve, reject) => {
|
|
31
|
+
const xhr = new XMLHttpRequest();
|
|
32
|
+
const requestMethod = method?.toUpperCase() || 'GET';
|
|
33
|
+
let aborted = false;
|
|
34
|
+
xhr.open(requestMethod, url);
|
|
35
|
+
if (headers) {
|
|
36
|
+
Object.entries(headers).forEach((entry) => {
|
|
37
|
+
const [key, value] = entry;
|
|
38
|
+
typeof value !== 'undefined' &&
|
|
39
|
+
!Array.isArray(value) &&
|
|
40
|
+
xhr.setRequestHeader(key, value);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
xhr.responseType = 'text';
|
|
44
|
+
onCancel(signal, () => {
|
|
45
|
+
aborted = true;
|
|
46
|
+
xhr.abort();
|
|
47
|
+
reject(cancelError());
|
|
48
|
+
});
|
|
49
|
+
xhr.onload = () => {
|
|
50
|
+
if (xhr.status != 200) {
|
|
51
|
+
// analyze HTTP status of the response
|
|
52
|
+
reject(new Error(`Error ${xhr.status}: ${xhr.statusText}`)); // e.g. 404: Not Found
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
const request = {
|
|
56
|
+
method: requestMethod,
|
|
57
|
+
url,
|
|
58
|
+
data,
|
|
59
|
+
headers: headers || undefined,
|
|
60
|
+
signal,
|
|
61
|
+
onProgress
|
|
62
|
+
};
|
|
63
|
+
// Convert the header string into an array
|
|
64
|
+
// of individual headers
|
|
65
|
+
const headersArray = xhr
|
|
66
|
+
.getAllResponseHeaders()
|
|
67
|
+
.trim()
|
|
68
|
+
.split(/[\r\n]+/);
|
|
69
|
+
// Create a map of header names to values
|
|
70
|
+
const responseHeaders = {};
|
|
71
|
+
headersArray.forEach(function (line) {
|
|
72
|
+
const parts = line.split(': ');
|
|
73
|
+
const header = parts.shift();
|
|
74
|
+
const value = parts.join(': ');
|
|
75
|
+
if (header && typeof header !== 'undefined') {
|
|
76
|
+
responseHeaders[header] = value;
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
const responseData = xhr.response;
|
|
80
|
+
const responseStatus = xhr.status;
|
|
81
|
+
resolve({
|
|
82
|
+
request,
|
|
83
|
+
data: responseData,
|
|
84
|
+
headers: responseHeaders,
|
|
85
|
+
status: responseStatus
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
xhr.onerror = () => {
|
|
90
|
+
if (aborted)
|
|
91
|
+
return;
|
|
92
|
+
// only triggers if the request couldn't be made at all
|
|
93
|
+
reject(new Error('Network error'));
|
|
94
|
+
};
|
|
95
|
+
if (onProgress && typeof onProgress === 'function') {
|
|
96
|
+
xhr.upload.onprogress = (event) => {
|
|
97
|
+
if (event.lengthComputable) {
|
|
98
|
+
onProgress({
|
|
99
|
+
isComputable: true,
|
|
100
|
+
value: event.loaded / event.total
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
onProgress({ isComputable: false });
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
if (data) {
|
|
109
|
+
xhr.send(data);
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
xhr.send();
|
|
113
|
+
}
|
|
114
114
|
});
|
|
115
115
|
|
|
116
|
-
function identity(obj) {
|
|
117
|
-
return obj;
|
|
116
|
+
function identity(obj) {
|
|
117
|
+
return obj;
|
|
118
118
|
}
|
|
119
119
|
|
|
120
|
-
const getFileOptions = ({ name }) => name ? [name] : [];
|
|
121
|
-
const transformFile = identity;
|
|
120
|
+
const getFileOptions = ({ name }) => name ? [name] : [];
|
|
121
|
+
const transformFile = identity;
|
|
122
122
|
var getFormData = () => new FormData();
|
|
123
123
|
|
|
124
|
-
/**
|
|
125
|
-
* FileData type guard.
|
|
126
|
-
*/
|
|
127
|
-
const isFileData = (data) => {
|
|
128
|
-
return (data !== undefined &&
|
|
129
|
-
((typeof Blob !== 'undefined' && data instanceof Blob) ||
|
|
130
|
-
(typeof File !== 'undefined' && data instanceof File) ||
|
|
131
|
-
(typeof Buffer !== 'undefined' && data instanceof Buffer)));
|
|
132
|
-
};
|
|
133
|
-
/**
|
|
134
|
-
* Uuid type guard.
|
|
135
|
-
*/
|
|
136
|
-
const isUuid = (data) => {
|
|
137
|
-
const UUID_REGEX = '[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}';
|
|
138
|
-
const regExp = new RegExp(UUID_REGEX);
|
|
139
|
-
return !isFileData(data) && regExp.test(data);
|
|
140
|
-
};
|
|
141
|
-
/**
|
|
142
|
-
* Url type guard.
|
|
143
|
-
*
|
|
144
|
-
* @param {NodeFile | BrowserFile | Url | Uuid} data
|
|
145
|
-
*/
|
|
146
|
-
const isUrl = (data) => {
|
|
147
|
-
const URL_REGEX = '^(?:\\w+:)?\\/\\/([^\\s\\.]+\\.\\S{2}|localhost[\\:?\\d]*)\\S*$';
|
|
148
|
-
const regExp = new RegExp(URL_REGEX);
|
|
149
|
-
return !isFileData(data) && regExp.test(data);
|
|
124
|
+
/**
|
|
125
|
+
* FileData type guard.
|
|
126
|
+
*/
|
|
127
|
+
const isFileData = (data) => {
|
|
128
|
+
return (data !== undefined &&
|
|
129
|
+
((typeof Blob !== 'undefined' && data instanceof Blob) ||
|
|
130
|
+
(typeof File !== 'undefined' && data instanceof File) ||
|
|
131
|
+
(typeof Buffer !== 'undefined' && data instanceof Buffer)));
|
|
132
|
+
};
|
|
133
|
+
/**
|
|
134
|
+
* Uuid type guard.
|
|
135
|
+
*/
|
|
136
|
+
const isUuid = (data) => {
|
|
137
|
+
const UUID_REGEX = '[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}';
|
|
138
|
+
const regExp = new RegExp(UUID_REGEX);
|
|
139
|
+
return !isFileData(data) && regExp.test(data);
|
|
140
|
+
};
|
|
141
|
+
/**
|
|
142
|
+
* Url type guard.
|
|
143
|
+
*
|
|
144
|
+
* @param {NodeFile | BrowserFile | Url | Uuid} data
|
|
145
|
+
*/
|
|
146
|
+
const isUrl = (data) => {
|
|
147
|
+
const URL_REGEX = '^(?:\\w+:)?\\/\\/([^\\s\\.]+\\.\\S{2}|localhost[\\:?\\d]*)\\S*$';
|
|
148
|
+
const regExp = new RegExp(URL_REGEX);
|
|
149
|
+
return !isFileData(data) && regExp.test(data);
|
|
150
150
|
};
|
|
151
151
|
|
|
152
|
-
const isSimpleValue = (value) => {
|
|
153
|
-
return (typeof value === 'string' ||
|
|
154
|
-
typeof value === 'number' ||
|
|
155
|
-
typeof value === 'undefined');
|
|
156
|
-
};
|
|
157
|
-
const isObjectValue = (value) => {
|
|
158
|
-
return !!value && typeof value === 'object' && !Array.isArray(value);
|
|
159
|
-
};
|
|
160
|
-
const isFileValue = (value) => !!value &&
|
|
161
|
-
typeof value === 'object' &&
|
|
162
|
-
'data' in value &&
|
|
163
|
-
isFileData(value.data);
|
|
164
|
-
function collectParams(params, inputKey, inputValue) {
|
|
165
|
-
if (isFileValue(inputValue)) {
|
|
166
|
-
const { name, contentType } = inputValue;
|
|
167
|
-
const file = transformFile(inputValue.data); // lgtm [js/superfluous-trailing-arguments]
|
|
168
|
-
const options = getFileOptions({ name, contentType });
|
|
169
|
-
params.push([inputKey, file, ...options]);
|
|
170
|
-
}
|
|
171
|
-
else if (isObjectValue(inputValue)) {
|
|
172
|
-
for (const [key, value] of Object.entries(inputValue)) {
|
|
173
|
-
if (typeof value !== 'undefined') {
|
|
174
|
-
params.push([`${inputKey}[${key}]`, String(value)]);
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
else if (isSimpleValue(inputValue) && inputValue) {
|
|
179
|
-
params.push([inputKey, inputValue.toString()]);
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
function getFormDataParams(options) {
|
|
183
|
-
const params = [];
|
|
184
|
-
for (const [key, value] of Object.entries(options)) {
|
|
185
|
-
collectParams(params, key, value);
|
|
186
|
-
}
|
|
187
|
-
return params;
|
|
188
|
-
}
|
|
189
|
-
function buildFormData(options) {
|
|
190
|
-
const formData = getFormData();
|
|
191
|
-
const paramsList = getFormDataParams(options);
|
|
192
|
-
for (const params of paramsList) {
|
|
193
|
-
const [key, value, ...options] = params;
|
|
194
|
-
// node form-data has another signature for append
|
|
195
|
-
formData.append(key, value, ...options);
|
|
196
|
-
}
|
|
197
|
-
return formData;
|
|
152
|
+
const isSimpleValue = (value) => {
|
|
153
|
+
return (typeof value === 'string' ||
|
|
154
|
+
typeof value === 'number' ||
|
|
155
|
+
typeof value === 'undefined');
|
|
156
|
+
};
|
|
157
|
+
const isObjectValue = (value) => {
|
|
158
|
+
return !!value && typeof value === 'object' && !Array.isArray(value);
|
|
159
|
+
};
|
|
160
|
+
const isFileValue = (value) => !!value &&
|
|
161
|
+
typeof value === 'object' &&
|
|
162
|
+
'data' in value &&
|
|
163
|
+
isFileData(value.data);
|
|
164
|
+
function collectParams(params, inputKey, inputValue) {
|
|
165
|
+
if (isFileValue(inputValue)) {
|
|
166
|
+
const { name, contentType } = inputValue;
|
|
167
|
+
const file = transformFile(inputValue.data); // lgtm [js/superfluous-trailing-arguments]
|
|
168
|
+
const options = getFileOptions({ name, contentType });
|
|
169
|
+
params.push([inputKey, file, ...options]);
|
|
170
|
+
}
|
|
171
|
+
else if (isObjectValue(inputValue)) {
|
|
172
|
+
for (const [key, value] of Object.entries(inputValue)) {
|
|
173
|
+
if (typeof value !== 'undefined') {
|
|
174
|
+
params.push([`${inputKey}[${key}]`, String(value)]);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
else if (isSimpleValue(inputValue) && inputValue) {
|
|
179
|
+
params.push([inputKey, inputValue.toString()]);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
function getFormDataParams(options) {
|
|
183
|
+
const params = [];
|
|
184
|
+
for (const [key, value] of Object.entries(options)) {
|
|
185
|
+
collectParams(params, key, value);
|
|
186
|
+
}
|
|
187
|
+
return params;
|
|
188
|
+
}
|
|
189
|
+
function buildFormData(options) {
|
|
190
|
+
const formData = getFormData();
|
|
191
|
+
const paramsList = getFormDataParams(options);
|
|
192
|
+
for (const params of paramsList) {
|
|
193
|
+
const [key, value, ...options] = params;
|
|
194
|
+
// node form-data has another signature for append
|
|
195
|
+
formData.append(key, value, ...options);
|
|
196
|
+
}
|
|
197
|
+
return formData;
|
|
198
198
|
}
|
|
199
199
|
|
|
200
|
-
const
|
|
201
|
-
|
|
202
|
-
const
|
|
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
|
-
|
|
200
|
+
const buildSearchParams = (query) => {
|
|
201
|
+
const searchParams = new URLSearchParams();
|
|
202
|
+
for (const [key, value] of Object.entries(query)) {
|
|
203
|
+
if (value && typeof value === 'object' && !Array.isArray(value)) {
|
|
204
|
+
Object.entries(value)
|
|
205
|
+
.filter((entry) => entry[1] ?? false)
|
|
206
|
+
.forEach((entry) => searchParams.set(`${key}[${entry[0]}]`, String(entry[1])));
|
|
207
|
+
}
|
|
208
|
+
else if (Array.isArray(value)) {
|
|
209
|
+
value.forEach((val) => {
|
|
210
|
+
searchParams.append(`${key}[]`, val);
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
else if (typeof value === 'string' && value) {
|
|
214
|
+
searchParams.set(key, value);
|
|
215
|
+
}
|
|
216
|
+
else if (typeof value === 'number') {
|
|
217
|
+
searchParams.set(key, value.toString());
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
return searchParams.toString();
|
|
221
|
+
};
|
|
222
|
+
const getUrl = (base, path, query) => {
|
|
223
|
+
const url = new URL(base);
|
|
224
|
+
url.pathname = path;
|
|
225
|
+
if (query) {
|
|
226
|
+
url.search = buildSearchParams(query);
|
|
227
|
+
}
|
|
228
|
+
return url.toString();
|
|
229
|
+
};
|
|
228
230
|
|
|
229
|
-
/*
|
|
230
|
-
Settings for future support:
|
|
231
|
-
parallelDirectUploads: 10,
|
|
232
|
-
*/
|
|
233
|
-
const defaultSettings = {
|
|
234
|
-
baseCDN: 'https://ucarecdn.com',
|
|
235
|
-
baseURL: 'https://upload.uploadcare.com',
|
|
236
|
-
maxContentLength: 50 * 1024 * 1024,
|
|
237
|
-
retryThrottledRequestMaxTimes: 1,
|
|
238
|
-
multipartMinFileSize: 25 * 1024 * 1024,
|
|
239
|
-
multipartChunkSize: 5 * 1024 * 1024,
|
|
240
|
-
multipartMinLastPartSize: 1024 * 1024,
|
|
241
|
-
maxConcurrentRequests: 4,
|
|
242
|
-
multipartMaxAttempts: 3,
|
|
243
|
-
pollingTimeoutMilliseconds: 10000,
|
|
244
|
-
pusherKey: '79ae88bd931ea68464d9'
|
|
245
|
-
};
|
|
246
|
-
const defaultContentType = 'application/octet-stream';
|
|
231
|
+
/*
|
|
232
|
+
Settings for future support:
|
|
233
|
+
parallelDirectUploads: 10,
|
|
234
|
+
*/
|
|
235
|
+
const defaultSettings = {
|
|
236
|
+
baseCDN: 'https://ucarecdn.com',
|
|
237
|
+
baseURL: 'https://upload.uploadcare.com',
|
|
238
|
+
maxContentLength: 50 * 1024 * 1024,
|
|
239
|
+
retryThrottledRequestMaxTimes: 1,
|
|
240
|
+
multipartMinFileSize: 25 * 1024 * 1024,
|
|
241
|
+
multipartChunkSize: 5 * 1024 * 1024,
|
|
242
|
+
multipartMinLastPartSize: 1024 * 1024,
|
|
243
|
+
maxConcurrentRequests: 4,
|
|
244
|
+
multipartMaxAttempts: 3,
|
|
245
|
+
pollingTimeoutMilliseconds: 10000,
|
|
246
|
+
pusherKey: '79ae88bd931ea68464d9'
|
|
247
|
+
};
|
|
248
|
+
const defaultContentType = 'application/octet-stream';
|
|
247
249
|
const defaultFilename = 'original';
|
|
248
250
|
|
|
249
|
-
var version = '
|
|
250
|
-
|
|
251
|
-
/**
|
|
252
|
-
* Returns User Agent based on version and settings.
|
|
253
|
-
*/
|
|
254
|
-
function getUserAgent({ userAgent, publicKey = '', integration = '' } = {}) {
|
|
255
|
-
const libraryName = 'UploadcareUploadClient';
|
|
256
|
-
const libraryVersion = version;
|
|
257
|
-
const languageName = 'JavaScript';
|
|
258
|
-
if (typeof userAgent === 'string') {
|
|
259
|
-
return userAgent;
|
|
260
|
-
}
|
|
261
|
-
if (typeof userAgent === 'function') {
|
|
262
|
-
return userAgent({
|
|
263
|
-
publicKey,
|
|
264
|
-
libraryName,
|
|
265
|
-
libraryVersion,
|
|
266
|
-
languageName,
|
|
267
|
-
integration
|
|
268
|
-
});
|
|
269
|
-
}
|
|
270
|
-
const mainInfo = [libraryName, libraryVersion, publicKey]
|
|
271
|
-
.filter(Boolean)
|
|
272
|
-
.join('/');
|
|
273
|
-
const additionInfo = [languageName, integration].filter(Boolean).join('; ');
|
|
274
|
-
return `${mainInfo} (${additionInfo})`;
|
|
275
|
-
}
|
|
251
|
+
var version = '4.1.0';
|
|
276
252
|
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
}
|
|
253
|
+
/**
|
|
254
|
+
* Returns User Agent based on version and settings.
|
|
255
|
+
*/
|
|
256
|
+
function getUserAgent({ userAgent, publicKey = '', integration = '' } = {}) {
|
|
257
|
+
const libraryName = 'UploadcareUploadClient';
|
|
258
|
+
const libraryVersion = version;
|
|
259
|
+
const languageName = 'JavaScript';
|
|
260
|
+
if (typeof userAgent === 'string') {
|
|
261
|
+
return userAgent;
|
|
262
|
+
}
|
|
263
|
+
if (typeof userAgent === 'function') {
|
|
264
|
+
return userAgent({
|
|
265
|
+
publicKey,
|
|
266
|
+
libraryName,
|
|
267
|
+
libraryVersion,
|
|
268
|
+
languageName,
|
|
269
|
+
integration
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
const mainInfo = [libraryName, libraryVersion, publicKey]
|
|
273
|
+
.filter(Boolean)
|
|
274
|
+
.join('/');
|
|
275
|
+
const additionInfo = [languageName, integration].filter(Boolean).join('; ');
|
|
276
|
+
return `${mainInfo} (${additionInfo})`;
|
|
301
277
|
}
|
|
302
278
|
|
|
303
|
-
/**
|
|
304
|
-
* setTimeout as Promise.
|
|
305
|
-
*
|
|
306
|
-
* @param {number} ms Timeout in milliseconds.
|
|
307
|
-
*/
|
|
279
|
+
/**
|
|
280
|
+
* setTimeout as Promise.
|
|
281
|
+
*
|
|
282
|
+
* @param {number} ms Timeout in milliseconds.
|
|
283
|
+
*/
|
|
308
284
|
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
309
285
|
|
|
310
|
-
const
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
function
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
286
|
+
const SEPARATOR = /\W|_/g;
|
|
287
|
+
/**
|
|
288
|
+
* Transforms a string to camelCased.
|
|
289
|
+
*/
|
|
290
|
+
function camelize(text) {
|
|
291
|
+
return text
|
|
292
|
+
.split(SEPARATOR)
|
|
293
|
+
.map((word, index) => word.charAt(0)[index > 0 ? 'toUpperCase' : 'toLowerCase']() +
|
|
294
|
+
word.slice(1))
|
|
295
|
+
.join('');
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Transforms keys of an object to camelCased recursively.
|
|
299
|
+
*/
|
|
300
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
301
|
+
function camelizeKeys(source) {
|
|
302
|
+
if (!source || typeof source !== 'object') {
|
|
303
|
+
return source;
|
|
304
|
+
}
|
|
305
|
+
return Object.keys(source).reduce((accumulator, key) => {
|
|
306
|
+
accumulator[camelize(key)] =
|
|
307
|
+
typeof source[key] === 'object' ? camelizeKeys(source[key]) : source[key];
|
|
308
|
+
return accumulator;
|
|
309
|
+
}, {});
|
|
328
310
|
}
|
|
329
311
|
|
|
330
|
-
const
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
}
|
|
312
|
+
const defaultOptions = {
|
|
313
|
+
factor: 2,
|
|
314
|
+
time: 100
|
|
315
|
+
};
|
|
316
|
+
function retrier(fn, options = defaultOptions) {
|
|
317
|
+
let attempts = 0;
|
|
318
|
+
function runAttempt(fn) {
|
|
319
|
+
const defaultDelayTime = Math.round(options.time * options.factor ** attempts);
|
|
320
|
+
const retry = (ms) => delay(ms ?? defaultDelayTime).then(() => {
|
|
321
|
+
attempts += 1;
|
|
322
|
+
return runAttempt(fn);
|
|
323
|
+
});
|
|
324
|
+
return fn({
|
|
325
|
+
attempt: attempts,
|
|
326
|
+
retry
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
return runAttempt(fn);
|
|
347
330
|
}
|
|
348
331
|
|
|
349
|
-
|
|
350
|
-
|
|
332
|
+
const REQUEST_WAS_THROTTLED_CODE = 'RequestThrottledError';
|
|
333
|
+
const DEFAULT_RETRY_AFTER_TIMEOUT = 15000;
|
|
334
|
+
function getTimeoutFromThrottledRequest(error) {
|
|
335
|
+
const { headers } = error || {};
|
|
336
|
+
return ((headers &&
|
|
337
|
+
Number.parseInt(headers['x-throttle-wait-seconds']) * 1000) ||
|
|
338
|
+
DEFAULT_RETRY_AFTER_TIMEOUT);
|
|
339
|
+
}
|
|
340
|
+
function retryIfThrottled(fn, retryThrottledMaxTimes) {
|
|
341
|
+
return retrier(({ attempt, retry }) => fn().catch((error) => {
|
|
342
|
+
if ('response' in error &&
|
|
343
|
+
error?.code === REQUEST_WAS_THROTTLED_CODE &&
|
|
344
|
+
attempt < retryThrottledMaxTimes) {
|
|
345
|
+
return retry(getTimeoutFromThrottledRequest(error));
|
|
346
|
+
}
|
|
347
|
+
throw error;
|
|
348
|
+
}));
|
|
351
349
|
}
|
|
352
350
|
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
* Can be canceled and has progress.
|
|
356
|
-
*/
|
|
357
|
-
function base(file, { publicKey, fileName, contentType, baseURL = defaultSettings.baseURL, secureSignature, secureExpire, store, signal, onProgress, source = 'local', integration, userAgent, retryThrottledRequestMaxTimes = defaultSettings.retryThrottledRequestMaxTimes, metadata }) {
|
|
358
|
-
return retryIfThrottled(() => {
|
|
359
|
-
var _a;
|
|
360
|
-
return request({
|
|
361
|
-
method: 'POST',
|
|
362
|
-
url: getUrl(baseURL, '/base/', {
|
|
363
|
-
jsonerrors: 1
|
|
364
|
-
}),
|
|
365
|
-
headers: {
|
|
366
|
-
'X-UC-User-Agent': getUserAgent({ publicKey, integration, userAgent })
|
|
367
|
-
},
|
|
368
|
-
data: buildFormData({
|
|
369
|
-
file: {
|
|
370
|
-
data: file,
|
|
371
|
-
name: (_a = fileName !== null && fileName !== void 0 ? fileName : file.name) !== null && _a !== void 0 ? _a : defaultFilename,
|
|
372
|
-
contentType
|
|
373
|
-
},
|
|
374
|
-
UPLOADCARE_PUB_KEY: publicKey,
|
|
375
|
-
UPLOADCARE_STORE: getStoreValue(store),
|
|
376
|
-
signature: secureSignature,
|
|
377
|
-
expire: secureExpire,
|
|
378
|
-
source: source,
|
|
379
|
-
metadata
|
|
380
|
-
}),
|
|
381
|
-
signal,
|
|
382
|
-
onProgress
|
|
383
|
-
}).then(({ data, headers, request }) => {
|
|
384
|
-
const response = camelizeKeys(JSON.parse(data));
|
|
385
|
-
if ('error' in response) {
|
|
386
|
-
throw new UploadClientError(response.error.content, response.error.errorCode, request, response, headers);
|
|
387
|
-
}
|
|
388
|
-
else {
|
|
389
|
-
return response;
|
|
390
|
-
}
|
|
391
|
-
});
|
|
392
|
-
}, retryThrottledRequestMaxTimes);
|
|
351
|
+
function getStoreValue(store) {
|
|
352
|
+
return typeof store === 'undefined' ? 'auto' : store ? '1' : '0';
|
|
393
353
|
}
|
|
394
354
|
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
}).then(({ data, headers, request }) => {
|
|
424
|
-
const response = camelizeKeys(JSON.parse(data));
|
|
425
|
-
if ('error' in response) {
|
|
426
|
-
throw new UploadClientError(response.error.content, response.error.errorCode, request, response, headers);
|
|
427
|
-
}
|
|
428
|
-
else {
|
|
429
|
-
return response;
|
|
430
|
-
}
|
|
431
|
-
}), retryThrottledRequestMaxTimes);
|
|
355
|
+
/**
|
|
356
|
+
* Performs file uploading request to Uploadcare Upload API.
|
|
357
|
+
* Can be canceled and has progress.
|
|
358
|
+
*/
|
|
359
|
+
function base(file, { publicKey, fileName, contentType, baseURL = defaultSettings.baseURL, secureSignature, secureExpire, store, signal, onProgress, source = 'local', integration, userAgent, retryThrottledRequestMaxTimes = defaultSettings.retryThrottledRequestMaxTimes, metadata }) {
|
|
360
|
+
return retryIfThrottled(() => request({
|
|
361
|
+
method: 'POST',
|
|
362
|
+
url: getUrl(baseURL, '/base/', {
|
|
363
|
+
jsonerrors: 1
|
|
364
|
+
}),
|
|
365
|
+
headers: {
|
|
366
|
+
'X-UC-User-Agent': getUserAgent({ publicKey, integration, userAgent })
|
|
367
|
+
},
|
|
368
|
+
data: buildFormData({
|
|
369
|
+
file: {
|
|
370
|
+
data: file,
|
|
371
|
+
name: fileName ?? file.name ?? defaultFilename,
|
|
372
|
+
contentType
|
|
373
|
+
},
|
|
374
|
+
UPLOADCARE_PUB_KEY: publicKey,
|
|
375
|
+
UPLOADCARE_STORE: getStoreValue(store),
|
|
376
|
+
signature: secureSignature,
|
|
377
|
+
expire: secureExpire,
|
|
378
|
+
source: source,
|
|
379
|
+
metadata
|
|
380
|
+
}),
|
|
381
|
+
signal,
|
|
382
|
+
onProgress
|
|
383
|
+
}).then(({ data, headers, request }) => {
|
|
384
|
+
const response = camelizeKeys(JSON.parse(data));
|
|
385
|
+
if ('error' in response) {
|
|
386
|
+
throw new UploadClientError(response.error.content, response.error.errorCode, request, response, headers);
|
|
387
|
+
}
|
|
388
|
+
else {
|
|
389
|
+
return response;
|
|
390
|
+
}
|
|
391
|
+
}), retryThrottledRequestMaxTimes);
|
|
432
392
|
}
|
|
433
393
|
|
|
434
|
-
var
|
|
435
|
-
(function (
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
return
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
return response;
|
|
472
|
-
}
|
|
473
|
-
}), retryThrottledRequestMaxTimes);
|
|
394
|
+
var TypeEnum;
|
|
395
|
+
(function (TypeEnum) {
|
|
396
|
+
TypeEnum["Token"] = "token";
|
|
397
|
+
TypeEnum["FileInfo"] = "file_info";
|
|
398
|
+
})(TypeEnum || (TypeEnum = {}));
|
|
399
|
+
/**
|
|
400
|
+
* Uploading files from URL.
|
|
401
|
+
*/
|
|
402
|
+
function fromUrl(sourceUrl, { publicKey, baseURL = defaultSettings.baseURL, store, fileName, checkForUrlDuplicates, saveUrlForRecurrentUploads, secureSignature, secureExpire, source = 'url', signal, integration, userAgent, retryThrottledRequestMaxTimes = defaultSettings.retryThrottledRequestMaxTimes, metadata }) {
|
|
403
|
+
return retryIfThrottled(() => request({
|
|
404
|
+
method: 'POST',
|
|
405
|
+
headers: {
|
|
406
|
+
'X-UC-User-Agent': getUserAgent({ publicKey, integration, userAgent })
|
|
407
|
+
},
|
|
408
|
+
url: getUrl(baseURL, '/from_url/', {
|
|
409
|
+
jsonerrors: 1,
|
|
410
|
+
pub_key: publicKey,
|
|
411
|
+
source_url: sourceUrl,
|
|
412
|
+
store: getStoreValue(store),
|
|
413
|
+
filename: fileName,
|
|
414
|
+
check_URL_duplicates: checkForUrlDuplicates ? 1 : undefined,
|
|
415
|
+
save_URL_duplicates: saveUrlForRecurrentUploads ? 1 : undefined,
|
|
416
|
+
signature: secureSignature,
|
|
417
|
+
expire: secureExpire,
|
|
418
|
+
source: source,
|
|
419
|
+
metadata
|
|
420
|
+
}),
|
|
421
|
+
signal
|
|
422
|
+
}).then(({ data, headers, request }) => {
|
|
423
|
+
const response = camelizeKeys(JSON.parse(data));
|
|
424
|
+
if ('error' in response) {
|
|
425
|
+
throw new UploadClientError(response.error.content, response.error.errorCode, request, response, headers);
|
|
426
|
+
}
|
|
427
|
+
else {
|
|
428
|
+
return response;
|
|
429
|
+
}
|
|
430
|
+
}), retryThrottledRequestMaxTimes);
|
|
474
431
|
}
|
|
475
432
|
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
433
|
+
var Status;
|
|
434
|
+
(function (Status) {
|
|
435
|
+
Status["Unknown"] = "unknown";
|
|
436
|
+
Status["Waiting"] = "waiting";
|
|
437
|
+
Status["Progress"] = "progress";
|
|
438
|
+
Status["Error"] = "error";
|
|
439
|
+
Status["Success"] = "success";
|
|
440
|
+
})(Status || (Status = {}));
|
|
441
|
+
const isErrorResponse = (response) => {
|
|
442
|
+
return 'status' in response && response.status === Status.Error;
|
|
443
|
+
};
|
|
444
|
+
/**
|
|
445
|
+
* Checking upload status and working with file tokens.
|
|
446
|
+
*/
|
|
447
|
+
function fromUrlStatus(token, { publicKey, baseURL = defaultSettings.baseURL, signal, integration, userAgent, retryThrottledRequestMaxTimes = defaultSettings.retryThrottledRequestMaxTimes } = {}) {
|
|
448
|
+
return retryIfThrottled(() => request({
|
|
449
|
+
method: 'GET',
|
|
450
|
+
headers: publicKey
|
|
451
|
+
? {
|
|
452
|
+
'X-UC-User-Agent': getUserAgent({
|
|
453
|
+
publicKey,
|
|
454
|
+
integration,
|
|
455
|
+
userAgent
|
|
456
|
+
})
|
|
457
|
+
}
|
|
458
|
+
: undefined,
|
|
459
|
+
url: getUrl(baseURL, '/from_url/status/', {
|
|
460
|
+
jsonerrors: 1,
|
|
461
|
+
token
|
|
462
|
+
}),
|
|
463
|
+
signal
|
|
464
|
+
}).then(({ data, headers, request }) => {
|
|
465
|
+
const response = camelizeKeys(JSON.parse(data));
|
|
466
|
+
if ('error' in response && !isErrorResponse(response)) {
|
|
467
|
+
throw new UploadClientError(response.error.content, undefined, request, response, headers);
|
|
468
|
+
}
|
|
469
|
+
else {
|
|
470
|
+
return response;
|
|
471
|
+
}
|
|
472
|
+
}), retryThrottledRequestMaxTimes);
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
/**
|
|
476
|
+
* Create files group.
|
|
477
|
+
*/
|
|
478
|
+
function group(uuids, { publicKey, baseURL = defaultSettings.baseURL, jsonpCallback, secureSignature, secureExpire, signal, source, integration, userAgent, retryThrottledRequestMaxTimes = defaultSettings.retryThrottledRequestMaxTimes }) {
|
|
479
|
+
return retryIfThrottled(() => request({
|
|
480
|
+
method: 'POST',
|
|
481
|
+
headers: {
|
|
482
|
+
'X-UC-User-Agent': getUserAgent({ publicKey, integration, userAgent })
|
|
483
|
+
},
|
|
484
|
+
url: getUrl(baseURL, '/group/', {
|
|
485
|
+
jsonerrors: 1,
|
|
486
|
+
pub_key: publicKey,
|
|
487
|
+
files: uuids,
|
|
488
|
+
callback: jsonpCallback,
|
|
489
|
+
signature: secureSignature,
|
|
490
|
+
expire: secureExpire,
|
|
491
|
+
source
|
|
492
|
+
}),
|
|
493
|
+
signal
|
|
494
|
+
}).then(({ data, headers, request }) => {
|
|
495
|
+
const response = camelizeKeys(JSON.parse(data));
|
|
496
|
+
if ('error' in response) {
|
|
497
|
+
throw new UploadClientError(response.error.content, response.error.errorCode, request, response, headers);
|
|
498
|
+
}
|
|
499
|
+
else {
|
|
500
|
+
return response;
|
|
501
|
+
}
|
|
502
|
+
}), retryThrottledRequestMaxTimes);
|
|
504
503
|
}
|
|
505
504
|
|
|
506
|
-
/**
|
|
507
|
-
* Get info about group.
|
|
508
|
-
*/
|
|
509
|
-
function groupInfo(id, { publicKey, baseURL = defaultSettings.baseURL, signal, source, integration, userAgent, retryThrottledRequestMaxTimes = defaultSettings.retryThrottledRequestMaxTimes }) {
|
|
510
|
-
return retryIfThrottled(() => request({
|
|
511
|
-
method: 'GET',
|
|
512
|
-
headers: {
|
|
513
|
-
'X-UC-User-Agent': getUserAgent({ publicKey, integration, userAgent })
|
|
514
|
-
},
|
|
515
|
-
url: getUrl(baseURL, '/group/info/', {
|
|
516
|
-
jsonerrors: 1,
|
|
517
|
-
pub_key: publicKey,
|
|
518
|
-
group_id: id,
|
|
519
|
-
source
|
|
520
|
-
}),
|
|
521
|
-
signal
|
|
522
|
-
}).then(({ data, headers, request }) => {
|
|
523
|
-
const response = camelizeKeys(JSON.parse(data));
|
|
524
|
-
if ('error' in response) {
|
|
525
|
-
throw new UploadClientError(response.error.content, response.error.errorCode, request, response, headers);
|
|
526
|
-
}
|
|
527
|
-
else {
|
|
528
|
-
return response;
|
|
529
|
-
}
|
|
530
|
-
}), retryThrottledRequestMaxTimes);
|
|
505
|
+
/**
|
|
506
|
+
* Get info about group.
|
|
507
|
+
*/
|
|
508
|
+
function groupInfo(id, { publicKey, baseURL = defaultSettings.baseURL, signal, source, integration, userAgent, retryThrottledRequestMaxTimes = defaultSettings.retryThrottledRequestMaxTimes }) {
|
|
509
|
+
return retryIfThrottled(() => request({
|
|
510
|
+
method: 'GET',
|
|
511
|
+
headers: {
|
|
512
|
+
'X-UC-User-Agent': getUserAgent({ publicKey, integration, userAgent })
|
|
513
|
+
},
|
|
514
|
+
url: getUrl(baseURL, '/group/info/', {
|
|
515
|
+
jsonerrors: 1,
|
|
516
|
+
pub_key: publicKey,
|
|
517
|
+
group_id: id,
|
|
518
|
+
source
|
|
519
|
+
}),
|
|
520
|
+
signal
|
|
521
|
+
}).then(({ data, headers, request }) => {
|
|
522
|
+
const response = camelizeKeys(JSON.parse(data));
|
|
523
|
+
if ('error' in response) {
|
|
524
|
+
throw new UploadClientError(response.error.content, response.error.errorCode, request, response, headers);
|
|
525
|
+
}
|
|
526
|
+
else {
|
|
527
|
+
return response;
|
|
528
|
+
}
|
|
529
|
+
}), retryThrottledRequestMaxTimes);
|
|
531
530
|
}
|
|
532
531
|
|
|
533
|
-
/**
|
|
534
|
-
* Returns a JSON dictionary holding file info.
|
|
535
|
-
*/
|
|
536
|
-
function info(uuid, { publicKey, baseURL = defaultSettings.baseURL, signal, source, integration, userAgent, retryThrottledRequestMaxTimes = defaultSettings.retryThrottledRequestMaxTimes }) {
|
|
537
|
-
return retryIfThrottled(() => request({
|
|
538
|
-
method: 'GET',
|
|
539
|
-
headers: {
|
|
540
|
-
'X-UC-User-Agent': getUserAgent({ publicKey, integration, userAgent })
|
|
541
|
-
},
|
|
542
|
-
url: getUrl(baseURL, '/info/', {
|
|
543
|
-
jsonerrors: 1,
|
|
544
|
-
pub_key: publicKey,
|
|
545
|
-
file_id: uuid,
|
|
546
|
-
source
|
|
547
|
-
}),
|
|
548
|
-
signal
|
|
549
|
-
}).then(({ data, headers, request }) => {
|
|
550
|
-
const response = camelizeKeys(JSON.parse(data));
|
|
551
|
-
if ('error' in response) {
|
|
552
|
-
throw new UploadClientError(response.error.content, response.error.errorCode, request, response, headers);
|
|
553
|
-
}
|
|
554
|
-
else {
|
|
555
|
-
return response;
|
|
556
|
-
}
|
|
557
|
-
}), retryThrottledRequestMaxTimes);
|
|
532
|
+
/**
|
|
533
|
+
* Returns a JSON dictionary holding file info.
|
|
534
|
+
*/
|
|
535
|
+
function info(uuid, { publicKey, baseURL = defaultSettings.baseURL, signal, source, integration, userAgent, retryThrottledRequestMaxTimes = defaultSettings.retryThrottledRequestMaxTimes }) {
|
|
536
|
+
return retryIfThrottled(() => request({
|
|
537
|
+
method: 'GET',
|
|
538
|
+
headers: {
|
|
539
|
+
'X-UC-User-Agent': getUserAgent({ publicKey, integration, userAgent })
|
|
540
|
+
},
|
|
541
|
+
url: getUrl(baseURL, '/info/', {
|
|
542
|
+
jsonerrors: 1,
|
|
543
|
+
pub_key: publicKey,
|
|
544
|
+
file_id: uuid,
|
|
545
|
+
source
|
|
546
|
+
}),
|
|
547
|
+
signal
|
|
548
|
+
}).then(({ data, headers, request }) => {
|
|
549
|
+
const response = camelizeKeys(JSON.parse(data));
|
|
550
|
+
if ('error' in response) {
|
|
551
|
+
throw new UploadClientError(response.error.content, response.error.errorCode, request, response, headers);
|
|
552
|
+
}
|
|
553
|
+
else {
|
|
554
|
+
return response;
|
|
555
|
+
}
|
|
556
|
+
}), retryThrottledRequestMaxTimes);
|
|
558
557
|
}
|
|
559
558
|
|
|
560
|
-
/**
|
|
561
|
-
* Start multipart uploading.
|
|
562
|
-
*/
|
|
563
|
-
function multipartStart(size, { publicKey, contentType, fileName, multipartChunkSize = defaultSettings.multipartChunkSize, baseURL = '', secureSignature, secureExpire, store, signal, source = 'local', integration, userAgent, retryThrottledRequestMaxTimes = defaultSettings.retryThrottledRequestMaxTimes, metadata }) {
|
|
564
|
-
return retryIfThrottled(() => request({
|
|
565
|
-
method: 'POST',
|
|
566
|
-
url: getUrl(baseURL, '/multipart/start/', { jsonerrors: 1 }),
|
|
567
|
-
headers: {
|
|
568
|
-
'X-UC-User-Agent': getUserAgent({ publicKey, integration, userAgent })
|
|
569
|
-
},
|
|
570
|
-
data: buildFormData({
|
|
571
|
-
filename: fileName
|
|
572
|
-
size: size,
|
|
573
|
-
content_type: contentType
|
|
574
|
-
part_size: multipartChunkSize,
|
|
575
|
-
UPLOADCARE_STORE: getStoreValue(store),
|
|
576
|
-
UPLOADCARE_PUB_KEY: publicKey,
|
|
577
|
-
signature: secureSignature,
|
|
578
|
-
expire: secureExpire,
|
|
579
|
-
source: source,
|
|
580
|
-
metadata
|
|
581
|
-
}),
|
|
582
|
-
signal
|
|
583
|
-
}).then(({ data, headers, request }) => {
|
|
584
|
-
const response = camelizeKeys(JSON.parse(data));
|
|
585
|
-
if ('error' in response) {
|
|
586
|
-
throw new UploadClientError(response.error.content, response.error.errorCode, request, response, headers);
|
|
587
|
-
}
|
|
588
|
-
else {
|
|
589
|
-
// convert to array
|
|
590
|
-
response.parts = Object.keys(response.parts).map((key) => response.parts[key]);
|
|
591
|
-
return response;
|
|
592
|
-
}
|
|
593
|
-
}), retryThrottledRequestMaxTimes);
|
|
559
|
+
/**
|
|
560
|
+
* Start multipart uploading.
|
|
561
|
+
*/
|
|
562
|
+
function multipartStart(size, { publicKey, contentType, fileName, multipartChunkSize = defaultSettings.multipartChunkSize, baseURL = '', secureSignature, secureExpire, store, signal, source = 'local', integration, userAgent, retryThrottledRequestMaxTimes = defaultSettings.retryThrottledRequestMaxTimes, metadata }) {
|
|
563
|
+
return retryIfThrottled(() => request({
|
|
564
|
+
method: 'POST',
|
|
565
|
+
url: getUrl(baseURL, '/multipart/start/', { jsonerrors: 1 }),
|
|
566
|
+
headers: {
|
|
567
|
+
'X-UC-User-Agent': getUserAgent({ publicKey, integration, userAgent })
|
|
568
|
+
},
|
|
569
|
+
data: buildFormData({
|
|
570
|
+
filename: fileName ?? defaultFilename,
|
|
571
|
+
size: size,
|
|
572
|
+
content_type: contentType ?? defaultContentType,
|
|
573
|
+
part_size: multipartChunkSize,
|
|
574
|
+
UPLOADCARE_STORE: getStoreValue(store),
|
|
575
|
+
UPLOADCARE_PUB_KEY: publicKey,
|
|
576
|
+
signature: secureSignature,
|
|
577
|
+
expire: secureExpire,
|
|
578
|
+
source: source,
|
|
579
|
+
metadata
|
|
580
|
+
}),
|
|
581
|
+
signal
|
|
582
|
+
}).then(({ data, headers, request }) => {
|
|
583
|
+
const response = camelizeKeys(JSON.parse(data));
|
|
584
|
+
if ('error' in response) {
|
|
585
|
+
throw new UploadClientError(response.error.content, response.error.errorCode, request, response, headers);
|
|
586
|
+
}
|
|
587
|
+
else {
|
|
588
|
+
// convert to array
|
|
589
|
+
response.parts = Object.keys(response.parts).map((key) => response.parts[key]);
|
|
590
|
+
return response;
|
|
591
|
+
}
|
|
592
|
+
}), retryThrottledRequestMaxTimes);
|
|
594
593
|
}
|
|
595
594
|
|
|
596
|
-
/**
|
|
597
|
-
* Complete multipart uploading.
|
|
598
|
-
*/
|
|
599
|
-
function multipartUpload(part, url, { signal, onProgress }) {
|
|
600
|
-
return request({
|
|
601
|
-
method: 'PUT',
|
|
602
|
-
url,
|
|
603
|
-
data: part,
|
|
604
|
-
// Upload request can't be non-computable because we always know exact size
|
|
605
|
-
onProgress: onProgress,
|
|
606
|
-
signal
|
|
607
|
-
})
|
|
608
|
-
.then((result) => {
|
|
609
|
-
// hack for node ¯\_(ツ)_/¯
|
|
610
|
-
if (onProgress)
|
|
611
|
-
onProgress({
|
|
612
|
-
isComputable: true,
|
|
613
|
-
value: 1
|
|
614
|
-
});
|
|
615
|
-
return result;
|
|
616
|
-
})
|
|
617
|
-
.then(({ status }) => ({ code: status }));
|
|
595
|
+
/**
|
|
596
|
+
* Complete multipart uploading.
|
|
597
|
+
*/
|
|
598
|
+
function multipartUpload(part, url, { signal, onProgress }) {
|
|
599
|
+
return request({
|
|
600
|
+
method: 'PUT',
|
|
601
|
+
url,
|
|
602
|
+
data: part,
|
|
603
|
+
// Upload request can't be non-computable because we always know exact size
|
|
604
|
+
onProgress: onProgress,
|
|
605
|
+
signal
|
|
606
|
+
})
|
|
607
|
+
.then((result) => {
|
|
608
|
+
// hack for node ¯\_(ツ)_/¯
|
|
609
|
+
if (onProgress)
|
|
610
|
+
onProgress({
|
|
611
|
+
isComputable: true,
|
|
612
|
+
value: 1
|
|
613
|
+
});
|
|
614
|
+
return result;
|
|
615
|
+
})
|
|
616
|
+
.then(({ status }) => ({ code: status }));
|
|
618
617
|
}
|
|
619
618
|
|
|
620
|
-
/**
|
|
621
|
-
* Complete multipart uploading.
|
|
622
|
-
*/
|
|
623
|
-
function multipartComplete(uuid, { publicKey, baseURL = defaultSettings.baseURL, source = 'local', signal, integration, userAgent, retryThrottledRequestMaxTimes = defaultSettings.retryThrottledRequestMaxTimes }) {
|
|
624
|
-
return retryIfThrottled(() => request({
|
|
625
|
-
method: 'POST',
|
|
626
|
-
url: getUrl(baseURL, '/multipart/complete/', { jsonerrors: 1 }),
|
|
627
|
-
headers: {
|
|
628
|
-
'X-UC-User-Agent': getUserAgent({ publicKey, integration, userAgent })
|
|
629
|
-
},
|
|
630
|
-
data: buildFormData({
|
|
631
|
-
uuid: uuid,
|
|
632
|
-
UPLOADCARE_PUB_KEY: publicKey,
|
|
633
|
-
source: source
|
|
634
|
-
}),
|
|
635
|
-
signal
|
|
636
|
-
}).then(({ data, headers, request }) => {
|
|
637
|
-
const response = camelizeKeys(JSON.parse(data));
|
|
638
|
-
if ('error' in response) {
|
|
639
|
-
throw new UploadClientError(response.error.content, response.error.errorCode, request, response, headers);
|
|
640
|
-
}
|
|
641
|
-
else {
|
|
642
|
-
return response;
|
|
643
|
-
}
|
|
644
|
-
}), retryThrottledRequestMaxTimes);
|
|
619
|
+
/**
|
|
620
|
+
* Complete multipart uploading.
|
|
621
|
+
*/
|
|
622
|
+
function multipartComplete(uuid, { publicKey, baseURL = defaultSettings.baseURL, source = 'local', signal, integration, userAgent, retryThrottledRequestMaxTimes = defaultSettings.retryThrottledRequestMaxTimes }) {
|
|
623
|
+
return retryIfThrottled(() => request({
|
|
624
|
+
method: 'POST',
|
|
625
|
+
url: getUrl(baseURL, '/multipart/complete/', { jsonerrors: 1 }),
|
|
626
|
+
headers: {
|
|
627
|
+
'X-UC-User-Agent': getUserAgent({ publicKey, integration, userAgent })
|
|
628
|
+
},
|
|
629
|
+
data: buildFormData({
|
|
630
|
+
uuid: uuid,
|
|
631
|
+
UPLOADCARE_PUB_KEY: publicKey,
|
|
632
|
+
source: source
|
|
633
|
+
}),
|
|
634
|
+
signal
|
|
635
|
+
}).then(({ data, headers, request }) => {
|
|
636
|
+
const response = camelizeKeys(JSON.parse(data));
|
|
637
|
+
if ('error' in response) {
|
|
638
|
+
throw new UploadClientError(response.error.content, response.error.errorCode, request, response, headers);
|
|
639
|
+
}
|
|
640
|
+
else {
|
|
641
|
+
return response;
|
|
642
|
+
}
|
|
643
|
+
}), retryThrottledRequestMaxTimes);
|
|
645
644
|
}
|
|
646
645
|
|
|
647
|
-
class UploadcareFile {
|
|
648
|
-
constructor(fileInfo, { baseCDN,
|
|
649
|
-
this.name = null;
|
|
650
|
-
this.size = null;
|
|
651
|
-
this.isStored = null;
|
|
652
|
-
this.isImage = null;
|
|
653
|
-
this.mimeType = null;
|
|
654
|
-
this.cdnUrl = null;
|
|
655
|
-
this.
|
|
656
|
-
this.
|
|
657
|
-
this.
|
|
658
|
-
this.
|
|
659
|
-
this.
|
|
660
|
-
this.
|
|
661
|
-
this.
|
|
662
|
-
const { uuid, s3Bucket } = fileInfo;
|
|
663
|
-
const
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
this.
|
|
670
|
-
this.
|
|
671
|
-
this.
|
|
672
|
-
this.
|
|
673
|
-
this.
|
|
674
|
-
this.
|
|
675
|
-
this.
|
|
676
|
-
this.
|
|
677
|
-
this.
|
|
678
|
-
this.
|
|
679
|
-
this.
|
|
680
|
-
this.
|
|
681
|
-
|
|
682
|
-
this.metadata = fileInfo.metadata || null;
|
|
683
|
-
}
|
|
646
|
+
class UploadcareFile {
|
|
647
|
+
constructor(fileInfo, { baseCDN, fileName }) {
|
|
648
|
+
this.name = null;
|
|
649
|
+
this.size = null;
|
|
650
|
+
this.isStored = null;
|
|
651
|
+
this.isImage = null;
|
|
652
|
+
this.mimeType = null;
|
|
653
|
+
this.cdnUrl = null;
|
|
654
|
+
this.s3Url = null;
|
|
655
|
+
this.originalFilename = null;
|
|
656
|
+
this.imageInfo = null;
|
|
657
|
+
this.videoInfo = null;
|
|
658
|
+
this.contentInfo = null;
|
|
659
|
+
this.metadata = null;
|
|
660
|
+
this.s3Bucket = null;
|
|
661
|
+
const { uuid, s3Bucket } = fileInfo;
|
|
662
|
+
const cdnUrl = `${baseCDN}/${uuid}/`;
|
|
663
|
+
const s3Url = s3Bucket
|
|
664
|
+
? `https://${s3Bucket}.s3.amazonaws.com/${uuid}/${fileInfo.filename}`
|
|
665
|
+
: null;
|
|
666
|
+
this.uuid = uuid;
|
|
667
|
+
this.name = fileName || fileInfo.filename;
|
|
668
|
+
this.size = fileInfo.size;
|
|
669
|
+
this.isStored = fileInfo.isStored;
|
|
670
|
+
this.isImage = fileInfo.isImage;
|
|
671
|
+
this.mimeType = fileInfo.mimeType;
|
|
672
|
+
this.cdnUrl = cdnUrl;
|
|
673
|
+
this.originalFilename = fileInfo.originalFilename;
|
|
674
|
+
this.imageInfo = camelizeKeys(fileInfo.imageInfo);
|
|
675
|
+
this.videoInfo = camelizeKeys(fileInfo.videoInfo);
|
|
676
|
+
this.contentInfo = camelizeKeys(fileInfo.contentInfo);
|
|
677
|
+
this.metadata = fileInfo.metadata || null;
|
|
678
|
+
this.s3Bucket = s3Bucket || null;
|
|
679
|
+
this.s3Url = s3Url;
|
|
680
|
+
}
|
|
684
681
|
}
|
|
685
682
|
|
|
686
|
-
const DEFAULT_INTERVAL = 500;
|
|
687
|
-
const poll = ({ check, interval = DEFAULT_INTERVAL, signal }) => new Promise((resolve, reject) => {
|
|
688
|
-
let timeoutId;
|
|
689
|
-
onCancel(signal, () => {
|
|
690
|
-
timeoutId && clearTimeout(timeoutId);
|
|
691
|
-
reject(cancelError('Poll cancelled'));
|
|
692
|
-
});
|
|
693
|
-
const tick = () => {
|
|
694
|
-
try {
|
|
695
|
-
Promise.resolve(check(signal))
|
|
696
|
-
.then((result) => {
|
|
697
|
-
if (result) {
|
|
698
|
-
resolve(result);
|
|
699
|
-
}
|
|
700
|
-
else {
|
|
701
|
-
timeoutId = setTimeout(tick, interval);
|
|
702
|
-
}
|
|
703
|
-
})
|
|
704
|
-
.catch((error) => reject(error));
|
|
705
|
-
}
|
|
706
|
-
catch (error) {
|
|
707
|
-
reject(error);
|
|
708
|
-
}
|
|
709
|
-
};
|
|
710
|
-
timeoutId = setTimeout(tick, 0);
|
|
683
|
+
const DEFAULT_INTERVAL = 500;
|
|
684
|
+
const poll = ({ check, interval = DEFAULT_INTERVAL, signal }) => new Promise((resolve, reject) => {
|
|
685
|
+
let timeoutId;
|
|
686
|
+
onCancel(signal, () => {
|
|
687
|
+
timeoutId && clearTimeout(timeoutId);
|
|
688
|
+
reject(cancelError('Poll cancelled'));
|
|
689
|
+
});
|
|
690
|
+
const tick = () => {
|
|
691
|
+
try {
|
|
692
|
+
Promise.resolve(check(signal))
|
|
693
|
+
.then((result) => {
|
|
694
|
+
if (result) {
|
|
695
|
+
resolve(result);
|
|
696
|
+
}
|
|
697
|
+
else {
|
|
698
|
+
timeoutId = setTimeout(tick, interval);
|
|
699
|
+
}
|
|
700
|
+
})
|
|
701
|
+
.catch((error) => reject(error));
|
|
702
|
+
}
|
|
703
|
+
catch (error) {
|
|
704
|
+
reject(error);
|
|
705
|
+
}
|
|
706
|
+
};
|
|
707
|
+
timeoutId = setTimeout(tick, 0);
|
|
711
708
|
});
|
|
712
709
|
|
|
713
|
-
function isReadyPoll({ file, publicKey, baseURL, source, integration, userAgent, retryThrottledRequestMaxTimes, signal, onProgress }) {
|
|
714
|
-
return poll({
|
|
715
|
-
check: (signal) => info(file, {
|
|
716
|
-
publicKey,
|
|
717
|
-
baseURL,
|
|
718
|
-
signal,
|
|
719
|
-
source,
|
|
720
|
-
integration,
|
|
721
|
-
userAgent,
|
|
722
|
-
retryThrottledRequestMaxTimes
|
|
723
|
-
}).then((response) => {
|
|
724
|
-
if (response.isReady) {
|
|
725
|
-
return response;
|
|
726
|
-
}
|
|
727
|
-
onProgress && onProgress({ isComputable: true, value: 1 });
|
|
728
|
-
return false;
|
|
729
|
-
}),
|
|
730
|
-
signal
|
|
731
|
-
});
|
|
710
|
+
function isReadyPoll({ file, publicKey, baseURL, source, integration, userAgent, retryThrottledRequestMaxTimes, signal, onProgress }) {
|
|
711
|
+
return poll({
|
|
712
|
+
check: (signal) => info(file, {
|
|
713
|
+
publicKey,
|
|
714
|
+
baseURL,
|
|
715
|
+
signal,
|
|
716
|
+
source,
|
|
717
|
+
integration,
|
|
718
|
+
userAgent,
|
|
719
|
+
retryThrottledRequestMaxTimes
|
|
720
|
+
}).then((response) => {
|
|
721
|
+
if (response.isReady) {
|
|
722
|
+
return response;
|
|
723
|
+
}
|
|
724
|
+
onProgress && onProgress({ isComputable: true, value: 1 });
|
|
725
|
+
return false;
|
|
726
|
+
}),
|
|
727
|
+
signal
|
|
728
|
+
});
|
|
732
729
|
}
|
|
733
730
|
|
|
734
|
-
const
|
|
735
|
-
return base(file, {
|
|
736
|
-
publicKey,
|
|
737
|
-
fileName,
|
|
738
|
-
contentType,
|
|
739
|
-
baseURL,
|
|
740
|
-
secureSignature,
|
|
741
|
-
secureExpire,
|
|
742
|
-
store,
|
|
743
|
-
signal,
|
|
744
|
-
onProgress,
|
|
745
|
-
source,
|
|
746
|
-
integration,
|
|
747
|
-
userAgent,
|
|
748
|
-
retryThrottledRequestMaxTimes,
|
|
749
|
-
metadata
|
|
750
|
-
})
|
|
751
|
-
.then(({ file }) => {
|
|
752
|
-
return isReadyPoll({
|
|
753
|
-
file,
|
|
754
|
-
publicKey,
|
|
755
|
-
baseURL,
|
|
756
|
-
source,
|
|
757
|
-
integration,
|
|
758
|
-
userAgent,
|
|
759
|
-
retryThrottledRequestMaxTimes,
|
|
760
|
-
onProgress,
|
|
761
|
-
signal
|
|
762
|
-
});
|
|
763
|
-
})
|
|
764
|
-
.then((fileInfo) => new UploadcareFile(fileInfo, { baseCDN }));
|
|
731
|
+
const uploadDirect = (file, { publicKey, fileName, baseURL, secureSignature, secureExpire, store, contentType, signal, onProgress, source, integration, userAgent, retryThrottledRequestMaxTimes, baseCDN, metadata }) => {
|
|
732
|
+
return base(file, {
|
|
733
|
+
publicKey,
|
|
734
|
+
fileName,
|
|
735
|
+
contentType,
|
|
736
|
+
baseURL,
|
|
737
|
+
secureSignature,
|
|
738
|
+
secureExpire,
|
|
739
|
+
store,
|
|
740
|
+
signal,
|
|
741
|
+
onProgress,
|
|
742
|
+
source,
|
|
743
|
+
integration,
|
|
744
|
+
userAgent,
|
|
745
|
+
retryThrottledRequestMaxTimes,
|
|
746
|
+
metadata
|
|
747
|
+
})
|
|
748
|
+
.then(({ file }) => {
|
|
749
|
+
return isReadyPoll({
|
|
750
|
+
file,
|
|
751
|
+
publicKey,
|
|
752
|
+
baseURL,
|
|
753
|
+
source,
|
|
754
|
+
integration,
|
|
755
|
+
userAgent,
|
|
756
|
+
retryThrottledRequestMaxTimes,
|
|
757
|
+
onProgress,
|
|
758
|
+
signal
|
|
759
|
+
});
|
|
760
|
+
})
|
|
761
|
+
.then((fileInfo) => new UploadcareFile(fileInfo, { baseCDN }));
|
|
765
762
|
};
|
|
766
763
|
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
const
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
}
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
})).then((results) => {
|
|
799
|
-
if (winnerIndex === null) {
|
|
800
|
-
throw lastError;
|
|
801
|
-
}
|
|
802
|
-
else {
|
|
803
|
-
return results[winnerIndex];
|
|
804
|
-
}
|
|
805
|
-
});
|
|
764
|
+
const race = (fns, { signal } = {}) => {
|
|
765
|
+
let lastError = null;
|
|
766
|
+
let winnerIndex = null;
|
|
767
|
+
const controllers = fns.map(() => new AbortController());
|
|
768
|
+
const createStopRaceCallback = (i) => () => {
|
|
769
|
+
winnerIndex = i;
|
|
770
|
+
controllers.forEach((controller, index) => index !== i && controller.abort());
|
|
771
|
+
};
|
|
772
|
+
onCancel(signal, () => {
|
|
773
|
+
controllers.forEach((controller) => controller.abort());
|
|
774
|
+
});
|
|
775
|
+
return Promise.all(fns.map((fn, i) => {
|
|
776
|
+
const stopRace = createStopRaceCallback(i);
|
|
777
|
+
return Promise.resolve()
|
|
778
|
+
.then(() => fn({ stopRace, signal: controllers[i].signal }))
|
|
779
|
+
.then((result) => {
|
|
780
|
+
stopRace();
|
|
781
|
+
return result;
|
|
782
|
+
})
|
|
783
|
+
.catch((error) => {
|
|
784
|
+
lastError = error;
|
|
785
|
+
return null;
|
|
786
|
+
});
|
|
787
|
+
})).then((results) => {
|
|
788
|
+
if (winnerIndex === null) {
|
|
789
|
+
throw lastError;
|
|
790
|
+
}
|
|
791
|
+
else {
|
|
792
|
+
return results[winnerIndex];
|
|
793
|
+
}
|
|
794
|
+
});
|
|
806
795
|
};
|
|
807
796
|
|
|
808
797
|
var WebSocket = window.WebSocket;
|
|
809
798
|
|
|
810
|
-
class Events {
|
|
811
|
-
constructor() {
|
|
812
|
-
this.events = Object.create({});
|
|
813
|
-
}
|
|
814
|
-
emit(event, data) {
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
this.events[event]
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
}
|
|
799
|
+
class Events {
|
|
800
|
+
constructor() {
|
|
801
|
+
this.events = Object.create({});
|
|
802
|
+
}
|
|
803
|
+
emit(event, data) {
|
|
804
|
+
this.events[event]?.forEach((fn) => fn(data));
|
|
805
|
+
}
|
|
806
|
+
on(event, callback) {
|
|
807
|
+
this.events[event] = this.events[event] || [];
|
|
808
|
+
this.events[event].push(callback);
|
|
809
|
+
}
|
|
810
|
+
off(event, callback) {
|
|
811
|
+
if (callback) {
|
|
812
|
+
this.events[event] = this.events[event].filter((fn) => fn !== callback);
|
|
813
|
+
}
|
|
814
|
+
else {
|
|
815
|
+
this.events[event] = [];
|
|
816
|
+
}
|
|
817
|
+
}
|
|
830
818
|
}
|
|
831
819
|
|
|
832
|
-
const response = (type, data) => {
|
|
833
|
-
if (type === 'success') {
|
|
834
|
-
return
|
|
835
|
-
}
|
|
836
|
-
if (type === 'progress') {
|
|
837
|
-
return
|
|
838
|
-
}
|
|
839
|
-
return
|
|
840
|
-
};
|
|
841
|
-
class Pusher {
|
|
842
|
-
constructor(pusherKey, disconnectTime = 30000) {
|
|
843
|
-
this.ws = undefined;
|
|
844
|
-
this.queue = [];
|
|
845
|
-
this.isConnected = false;
|
|
846
|
-
this.subscribers = 0;
|
|
847
|
-
this.emmitter = new Events();
|
|
848
|
-
this.disconnectTimeoutId = null;
|
|
849
|
-
this.key = pusherKey;
|
|
850
|
-
this.disconnectTime = disconnectTime;
|
|
851
|
-
}
|
|
852
|
-
connect() {
|
|
853
|
-
this.disconnectTimeoutId && clearTimeout(this.disconnectTimeoutId);
|
|
854
|
-
if (!this.isConnected && !this.ws) {
|
|
855
|
-
const pusherUrl = `wss://ws.pusherapp.com/app/${this.key}?protocol=5&client=js&version=1.12.2`;
|
|
856
|
-
this.ws = new WebSocket(pusherUrl);
|
|
857
|
-
this.ws.addEventListener('error', (error) => {
|
|
858
|
-
this.emmitter.emit('error', new Error(error.message));
|
|
859
|
-
});
|
|
860
|
-
this.emmitter.on('connected', () => {
|
|
861
|
-
this.isConnected = true;
|
|
862
|
-
this.queue.forEach((message) => this.send(message.event, message.data));
|
|
863
|
-
this.queue = [];
|
|
864
|
-
});
|
|
865
|
-
this.ws.addEventListener('message', (e) => {
|
|
866
|
-
const data = JSON.parse(e.data.toString());
|
|
867
|
-
switch (data.event) {
|
|
868
|
-
case 'pusher:connection_established': {
|
|
869
|
-
this.emmitter.emit('connected', undefined);
|
|
870
|
-
break;
|
|
871
|
-
}
|
|
872
|
-
case 'pusher:ping': {
|
|
873
|
-
this.send('pusher:pong', {});
|
|
874
|
-
break;
|
|
875
|
-
}
|
|
876
|
-
case 'progress':
|
|
877
|
-
case 'success':
|
|
878
|
-
case 'fail': {
|
|
879
|
-
this.emmitter.emit(data.channel, response(data.event, JSON.parse(data.data)));
|
|
880
|
-
}
|
|
881
|
-
}
|
|
882
|
-
});
|
|
883
|
-
}
|
|
884
|
-
}
|
|
885
|
-
disconnect() {
|
|
886
|
-
const actualDisconect = () => {
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
this.
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
const preconnect = (key) => {
|
|
955
|
-
getPusher(key).connect();
|
|
820
|
+
const response = (type, data) => {
|
|
821
|
+
if (type === 'success') {
|
|
822
|
+
return { status: Status.Success, ...data };
|
|
823
|
+
}
|
|
824
|
+
if (type === 'progress') {
|
|
825
|
+
return { status: Status.Progress, ...data };
|
|
826
|
+
}
|
|
827
|
+
return { status: Status.Error, ...data };
|
|
828
|
+
};
|
|
829
|
+
class Pusher {
|
|
830
|
+
constructor(pusherKey, disconnectTime = 30000) {
|
|
831
|
+
this.ws = undefined;
|
|
832
|
+
this.queue = [];
|
|
833
|
+
this.isConnected = false;
|
|
834
|
+
this.subscribers = 0;
|
|
835
|
+
this.emmitter = new Events();
|
|
836
|
+
this.disconnectTimeoutId = null;
|
|
837
|
+
this.key = pusherKey;
|
|
838
|
+
this.disconnectTime = disconnectTime;
|
|
839
|
+
}
|
|
840
|
+
connect() {
|
|
841
|
+
this.disconnectTimeoutId && clearTimeout(this.disconnectTimeoutId);
|
|
842
|
+
if (!this.isConnected && !this.ws) {
|
|
843
|
+
const pusherUrl = `wss://ws.pusherapp.com/app/${this.key}?protocol=5&client=js&version=1.12.2`;
|
|
844
|
+
this.ws = new WebSocket(pusherUrl);
|
|
845
|
+
this.ws.addEventListener('error', (error) => {
|
|
846
|
+
this.emmitter.emit('error', new Error(error.message));
|
|
847
|
+
});
|
|
848
|
+
this.emmitter.on('connected', () => {
|
|
849
|
+
this.isConnected = true;
|
|
850
|
+
this.queue.forEach((message) => this.send(message.event, message.data));
|
|
851
|
+
this.queue = [];
|
|
852
|
+
});
|
|
853
|
+
this.ws.addEventListener('message', (e) => {
|
|
854
|
+
const data = JSON.parse(e.data.toString());
|
|
855
|
+
switch (data.event) {
|
|
856
|
+
case 'pusher:connection_established': {
|
|
857
|
+
this.emmitter.emit('connected', undefined);
|
|
858
|
+
break;
|
|
859
|
+
}
|
|
860
|
+
case 'pusher:ping': {
|
|
861
|
+
this.send('pusher:pong', {});
|
|
862
|
+
break;
|
|
863
|
+
}
|
|
864
|
+
case 'progress':
|
|
865
|
+
case 'success':
|
|
866
|
+
case 'fail': {
|
|
867
|
+
this.emmitter.emit(data.channel, response(data.event, JSON.parse(data.data)));
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
});
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
disconnect() {
|
|
874
|
+
const actualDisconect = () => {
|
|
875
|
+
this.ws?.close();
|
|
876
|
+
this.ws = undefined;
|
|
877
|
+
this.isConnected = false;
|
|
878
|
+
};
|
|
879
|
+
if (this.disconnectTime) {
|
|
880
|
+
this.disconnectTimeoutId = setTimeout(() => {
|
|
881
|
+
actualDisconect();
|
|
882
|
+
}, this.disconnectTime);
|
|
883
|
+
}
|
|
884
|
+
else {
|
|
885
|
+
actualDisconect();
|
|
886
|
+
}
|
|
887
|
+
}
|
|
888
|
+
send(event, data) {
|
|
889
|
+
const str = JSON.stringify({ event, data });
|
|
890
|
+
this.ws?.send(str);
|
|
891
|
+
}
|
|
892
|
+
subscribe(token, handler) {
|
|
893
|
+
this.subscribers += 1;
|
|
894
|
+
this.connect();
|
|
895
|
+
const channel = `task-status-${token}`;
|
|
896
|
+
const message = {
|
|
897
|
+
event: 'pusher:subscribe',
|
|
898
|
+
data: { channel }
|
|
899
|
+
};
|
|
900
|
+
this.emmitter.on(channel, handler);
|
|
901
|
+
if (this.isConnected) {
|
|
902
|
+
this.send(message.event, message.data);
|
|
903
|
+
}
|
|
904
|
+
else {
|
|
905
|
+
this.queue.push(message);
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
unsubscribe(token) {
|
|
909
|
+
this.subscribers -= 1;
|
|
910
|
+
const channel = `task-status-${token}`;
|
|
911
|
+
const message = {
|
|
912
|
+
event: 'pusher:unsubscribe',
|
|
913
|
+
data: { channel }
|
|
914
|
+
};
|
|
915
|
+
this.emmitter.off(channel);
|
|
916
|
+
if (this.isConnected) {
|
|
917
|
+
this.send(message.event, message.data);
|
|
918
|
+
}
|
|
919
|
+
else {
|
|
920
|
+
this.queue = this.queue.filter((msg) => msg.data.channel !== channel);
|
|
921
|
+
}
|
|
922
|
+
if (this.subscribers === 0) {
|
|
923
|
+
this.disconnect();
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
onError(callback) {
|
|
927
|
+
this.emmitter.on('error', callback);
|
|
928
|
+
return () => this.emmitter.off('error', callback);
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
let pusher = null;
|
|
932
|
+
const getPusher = (key) => {
|
|
933
|
+
if (!pusher) {
|
|
934
|
+
// no timeout for nodeJS and 30000 ms for browser
|
|
935
|
+
const disconectTimeout = typeof window === 'undefined' ? 0 : 30000;
|
|
936
|
+
pusher = new Pusher(key, disconectTimeout);
|
|
937
|
+
}
|
|
938
|
+
return pusher;
|
|
939
|
+
};
|
|
940
|
+
const preconnect = (key) => {
|
|
941
|
+
getPusher(key).connect();
|
|
956
942
|
};
|
|
957
943
|
|
|
958
|
-
function pollStrategy({ token, publicKey, baseURL, integration, userAgent, retryThrottledRequestMaxTimes, onProgress, signal }) {
|
|
959
|
-
return poll({
|
|
960
|
-
check: (signal) => fromUrlStatus(token, {
|
|
961
|
-
publicKey,
|
|
962
|
-
baseURL,
|
|
963
|
-
integration,
|
|
964
|
-
userAgent,
|
|
965
|
-
retryThrottledRequestMaxTimes,
|
|
966
|
-
signal
|
|
967
|
-
}).then((response) => {
|
|
968
|
-
switch (response.status) {
|
|
969
|
-
case Status.Error: {
|
|
970
|
-
return new UploadClientError(response.error, response.errorCode);
|
|
971
|
-
}
|
|
972
|
-
case Status.Waiting: {
|
|
973
|
-
return false;
|
|
974
|
-
}
|
|
975
|
-
case Status.Unknown: {
|
|
976
|
-
return new UploadClientError(`Token "${token}" was not found.`);
|
|
977
|
-
}
|
|
978
|
-
case Status.Progress: {
|
|
979
|
-
if (onProgress) {
|
|
980
|
-
if (response.total === 'unknown') {
|
|
981
|
-
onProgress({ isComputable: false });
|
|
982
|
-
}
|
|
983
|
-
else {
|
|
984
|
-
onProgress({
|
|
985
|
-
isComputable: true,
|
|
986
|
-
value: response.done / response.total
|
|
987
|
-
});
|
|
988
|
-
}
|
|
989
|
-
}
|
|
990
|
-
return false;
|
|
991
|
-
}
|
|
992
|
-
case Status.Success: {
|
|
993
|
-
if (onProgress)
|
|
994
|
-
onProgress({
|
|
995
|
-
isComputable: true,
|
|
996
|
-
value: response.done / response.total
|
|
997
|
-
});
|
|
998
|
-
return response;
|
|
999
|
-
}
|
|
1000
|
-
default: {
|
|
1001
|
-
throw new Error('Unknown status');
|
|
1002
|
-
}
|
|
1003
|
-
}
|
|
1004
|
-
}),
|
|
1005
|
-
signal
|
|
1006
|
-
});
|
|
1007
|
-
}
|
|
1008
|
-
const pushStrategy = ({ token, pusherKey, signal, onProgress }) => new Promise((resolve, reject) => {
|
|
1009
|
-
const pusher = getPusher(pusherKey);
|
|
1010
|
-
const unsubErrorHandler = pusher.onError(reject);
|
|
1011
|
-
const destroy = () => {
|
|
1012
|
-
unsubErrorHandler();
|
|
1013
|
-
pusher.unsubscribe(token);
|
|
1014
|
-
};
|
|
1015
|
-
onCancel(signal, () => {
|
|
1016
|
-
destroy();
|
|
1017
|
-
reject(cancelError('pusher cancelled'));
|
|
1018
|
-
});
|
|
1019
|
-
pusher.subscribe(token, (result) => {
|
|
1020
|
-
switch (result.status) {
|
|
1021
|
-
case Status.Progress: {
|
|
1022
|
-
if (onProgress) {
|
|
1023
|
-
if (result.total === 'unknown') {
|
|
1024
|
-
onProgress({ isComputable: false });
|
|
1025
|
-
}
|
|
1026
|
-
else {
|
|
1027
|
-
onProgress({
|
|
1028
|
-
isComputable: true,
|
|
1029
|
-
value: result.done / result.total
|
|
1030
|
-
});
|
|
1031
|
-
}
|
|
1032
|
-
}
|
|
1033
|
-
break;
|
|
1034
|
-
}
|
|
1035
|
-
case Status.Success: {
|
|
1036
|
-
destroy();
|
|
1037
|
-
if (onProgress)
|
|
1038
|
-
onProgress({
|
|
1039
|
-
isComputable: true,
|
|
1040
|
-
value: result.done / result.total
|
|
1041
|
-
});
|
|
1042
|
-
resolve(result);
|
|
1043
|
-
break;
|
|
1044
|
-
}
|
|
1045
|
-
case Status.Error: {
|
|
1046
|
-
destroy();
|
|
1047
|
-
reject(new UploadClientError(result.msg, result.error_code));
|
|
1048
|
-
}
|
|
1049
|
-
}
|
|
1050
|
-
});
|
|
1051
|
-
});
|
|
1052
|
-
const uploadFromUrl = (sourceUrl, { publicKey, fileName, baseURL, baseCDN, checkForUrlDuplicates, saveUrlForRecurrentUploads, secureSignature, secureExpire, store, signal, onProgress, source, integration, userAgent, retryThrottledRequestMaxTimes, pusherKey = defaultSettings.pusherKey, metadata }) => Promise.resolve(preconnect(pusherKey))
|
|
1053
|
-
.then(() => fromUrl(sourceUrl, {
|
|
1054
|
-
publicKey,
|
|
1055
|
-
fileName,
|
|
1056
|
-
baseURL,
|
|
1057
|
-
checkForUrlDuplicates,
|
|
1058
|
-
saveUrlForRecurrentUploads,
|
|
1059
|
-
secureSignature,
|
|
1060
|
-
secureExpire,
|
|
1061
|
-
store,
|
|
1062
|
-
signal,
|
|
1063
|
-
source,
|
|
1064
|
-
integration,
|
|
1065
|
-
userAgent,
|
|
1066
|
-
retryThrottledRequestMaxTimes,
|
|
1067
|
-
metadata
|
|
1068
|
-
}))
|
|
1069
|
-
.catch((error) => {
|
|
1070
|
-
const pusher = getPusher(pusherKey);
|
|
1071
|
-
pusher
|
|
1072
|
-
return Promise.reject(error);
|
|
1073
|
-
})
|
|
1074
|
-
.then((urlResponse) => {
|
|
1075
|
-
if (urlResponse.type === TypeEnum.FileInfo) {
|
|
1076
|
-
return urlResponse;
|
|
1077
|
-
}
|
|
1078
|
-
else {
|
|
1079
|
-
return race([
|
|
1080
|
-
({ signal }) => pollStrategy({
|
|
1081
|
-
token: urlResponse.token,
|
|
1082
|
-
publicKey,
|
|
1083
|
-
baseURL,
|
|
1084
|
-
integration,
|
|
1085
|
-
userAgent,
|
|
1086
|
-
retryThrottledRequestMaxTimes,
|
|
1087
|
-
onProgress,
|
|
1088
|
-
signal
|
|
1089
|
-
}),
|
|
1090
|
-
({ signal }) => pushStrategy({
|
|
1091
|
-
token: urlResponse.token,
|
|
1092
|
-
pusherKey,
|
|
1093
|
-
signal,
|
|
1094
|
-
onProgress
|
|
1095
|
-
})
|
|
1096
|
-
], { signal });
|
|
1097
|
-
}
|
|
1098
|
-
})
|
|
1099
|
-
.then((result) => {
|
|
1100
|
-
if (result instanceof UploadClientError)
|
|
1101
|
-
throw result;
|
|
1102
|
-
return result;
|
|
1103
|
-
})
|
|
1104
|
-
.then((result) => isReadyPoll({
|
|
1105
|
-
file: result.uuid,
|
|
1106
|
-
publicKey,
|
|
1107
|
-
baseURL,
|
|
1108
|
-
integration,
|
|
1109
|
-
userAgent,
|
|
1110
|
-
retryThrottledRequestMaxTimes,
|
|
1111
|
-
onProgress,
|
|
1112
|
-
signal
|
|
1113
|
-
}))
|
|
944
|
+
function pollStrategy({ token, publicKey, baseURL, integration, userAgent, retryThrottledRequestMaxTimes, onProgress, signal }) {
|
|
945
|
+
return poll({
|
|
946
|
+
check: (signal) => fromUrlStatus(token, {
|
|
947
|
+
publicKey,
|
|
948
|
+
baseURL,
|
|
949
|
+
integration,
|
|
950
|
+
userAgent,
|
|
951
|
+
retryThrottledRequestMaxTimes,
|
|
952
|
+
signal
|
|
953
|
+
}).then((response) => {
|
|
954
|
+
switch (response.status) {
|
|
955
|
+
case Status.Error: {
|
|
956
|
+
return new UploadClientError(response.error, response.errorCode);
|
|
957
|
+
}
|
|
958
|
+
case Status.Waiting: {
|
|
959
|
+
return false;
|
|
960
|
+
}
|
|
961
|
+
case Status.Unknown: {
|
|
962
|
+
return new UploadClientError(`Token "${token}" was not found.`);
|
|
963
|
+
}
|
|
964
|
+
case Status.Progress: {
|
|
965
|
+
if (onProgress) {
|
|
966
|
+
if (response.total === 'unknown') {
|
|
967
|
+
onProgress({ isComputable: false });
|
|
968
|
+
}
|
|
969
|
+
else {
|
|
970
|
+
onProgress({
|
|
971
|
+
isComputable: true,
|
|
972
|
+
value: response.done / response.total
|
|
973
|
+
});
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
return false;
|
|
977
|
+
}
|
|
978
|
+
case Status.Success: {
|
|
979
|
+
if (onProgress)
|
|
980
|
+
onProgress({
|
|
981
|
+
isComputable: true,
|
|
982
|
+
value: response.done / response.total
|
|
983
|
+
});
|
|
984
|
+
return response;
|
|
985
|
+
}
|
|
986
|
+
default: {
|
|
987
|
+
throw new Error('Unknown status');
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
}),
|
|
991
|
+
signal
|
|
992
|
+
});
|
|
993
|
+
}
|
|
994
|
+
const pushStrategy = ({ token, pusherKey, signal, onProgress }) => new Promise((resolve, reject) => {
|
|
995
|
+
const pusher = getPusher(pusherKey);
|
|
996
|
+
const unsubErrorHandler = pusher.onError(reject);
|
|
997
|
+
const destroy = () => {
|
|
998
|
+
unsubErrorHandler();
|
|
999
|
+
pusher.unsubscribe(token);
|
|
1000
|
+
};
|
|
1001
|
+
onCancel(signal, () => {
|
|
1002
|
+
destroy();
|
|
1003
|
+
reject(cancelError('pusher cancelled'));
|
|
1004
|
+
});
|
|
1005
|
+
pusher.subscribe(token, (result) => {
|
|
1006
|
+
switch (result.status) {
|
|
1007
|
+
case Status.Progress: {
|
|
1008
|
+
if (onProgress) {
|
|
1009
|
+
if (result.total === 'unknown') {
|
|
1010
|
+
onProgress({ isComputable: false });
|
|
1011
|
+
}
|
|
1012
|
+
else {
|
|
1013
|
+
onProgress({
|
|
1014
|
+
isComputable: true,
|
|
1015
|
+
value: result.done / result.total
|
|
1016
|
+
});
|
|
1017
|
+
}
|
|
1018
|
+
}
|
|
1019
|
+
break;
|
|
1020
|
+
}
|
|
1021
|
+
case Status.Success: {
|
|
1022
|
+
destroy();
|
|
1023
|
+
if (onProgress)
|
|
1024
|
+
onProgress({
|
|
1025
|
+
isComputable: true,
|
|
1026
|
+
value: result.done / result.total
|
|
1027
|
+
});
|
|
1028
|
+
resolve(result);
|
|
1029
|
+
break;
|
|
1030
|
+
}
|
|
1031
|
+
case Status.Error: {
|
|
1032
|
+
destroy();
|
|
1033
|
+
reject(new UploadClientError(result.msg, result.error_code));
|
|
1034
|
+
}
|
|
1035
|
+
}
|
|
1036
|
+
});
|
|
1037
|
+
});
|
|
1038
|
+
const uploadFromUrl = (sourceUrl, { publicKey, fileName, baseURL, baseCDN, checkForUrlDuplicates, saveUrlForRecurrentUploads, secureSignature, secureExpire, store, signal, onProgress, source, integration, userAgent, retryThrottledRequestMaxTimes, pusherKey = defaultSettings.pusherKey, metadata }) => Promise.resolve(preconnect(pusherKey))
|
|
1039
|
+
.then(() => fromUrl(sourceUrl, {
|
|
1040
|
+
publicKey,
|
|
1041
|
+
fileName,
|
|
1042
|
+
baseURL,
|
|
1043
|
+
checkForUrlDuplicates,
|
|
1044
|
+
saveUrlForRecurrentUploads,
|
|
1045
|
+
secureSignature,
|
|
1046
|
+
secureExpire,
|
|
1047
|
+
store,
|
|
1048
|
+
signal,
|
|
1049
|
+
source,
|
|
1050
|
+
integration,
|
|
1051
|
+
userAgent,
|
|
1052
|
+
retryThrottledRequestMaxTimes,
|
|
1053
|
+
metadata
|
|
1054
|
+
}))
|
|
1055
|
+
.catch((error) => {
|
|
1056
|
+
const pusher = getPusher(pusherKey);
|
|
1057
|
+
pusher?.disconnect();
|
|
1058
|
+
return Promise.reject(error);
|
|
1059
|
+
})
|
|
1060
|
+
.then((urlResponse) => {
|
|
1061
|
+
if (urlResponse.type === TypeEnum.FileInfo) {
|
|
1062
|
+
return urlResponse;
|
|
1063
|
+
}
|
|
1064
|
+
else {
|
|
1065
|
+
return race([
|
|
1066
|
+
({ signal }) => pollStrategy({
|
|
1067
|
+
token: urlResponse.token,
|
|
1068
|
+
publicKey,
|
|
1069
|
+
baseURL,
|
|
1070
|
+
integration,
|
|
1071
|
+
userAgent,
|
|
1072
|
+
retryThrottledRequestMaxTimes,
|
|
1073
|
+
onProgress,
|
|
1074
|
+
signal
|
|
1075
|
+
}),
|
|
1076
|
+
({ signal }) => pushStrategy({
|
|
1077
|
+
token: urlResponse.token,
|
|
1078
|
+
pusherKey,
|
|
1079
|
+
signal,
|
|
1080
|
+
onProgress
|
|
1081
|
+
})
|
|
1082
|
+
], { signal });
|
|
1083
|
+
}
|
|
1084
|
+
})
|
|
1085
|
+
.then((result) => {
|
|
1086
|
+
if (result instanceof UploadClientError)
|
|
1087
|
+
throw result;
|
|
1088
|
+
return result;
|
|
1089
|
+
})
|
|
1090
|
+
.then((result) => isReadyPoll({
|
|
1091
|
+
file: result.uuid,
|
|
1092
|
+
publicKey,
|
|
1093
|
+
baseURL,
|
|
1094
|
+
integration,
|
|
1095
|
+
userAgent,
|
|
1096
|
+
retryThrottledRequestMaxTimes,
|
|
1097
|
+
onProgress,
|
|
1098
|
+
signal
|
|
1099
|
+
}))
|
|
1114
1100
|
.then((fileInfo) => new UploadcareFile(fileInfo, { baseCDN }));
|
|
1115
1101
|
|
|
1116
|
-
const uploadFromUploaded = (uuid, { publicKey, fileName, baseURL, signal, onProgress, source, integration, userAgent, retryThrottledRequestMaxTimes, baseCDN }) => {
|
|
1117
|
-
return info(uuid, {
|
|
1118
|
-
publicKey,
|
|
1119
|
-
baseURL,
|
|
1120
|
-
signal,
|
|
1121
|
-
source,
|
|
1122
|
-
integration,
|
|
1123
|
-
userAgent,
|
|
1124
|
-
retryThrottledRequestMaxTimes
|
|
1125
|
-
})
|
|
1126
|
-
.then((fileInfo) => new UploadcareFile(fileInfo, { baseCDN, fileName }))
|
|
1127
|
-
.then((result) => {
|
|
1128
|
-
// hack for node ¯\_(ツ)_/¯
|
|
1129
|
-
if (onProgress)
|
|
1130
|
-
onProgress({
|
|
1131
|
-
isComputable: true,
|
|
1132
|
-
value: 1
|
|
1133
|
-
});
|
|
1134
|
-
return result;
|
|
1135
|
-
});
|
|
1102
|
+
const uploadFromUploaded = (uuid, { publicKey, fileName, baseURL, signal, onProgress, source, integration, userAgent, retryThrottledRequestMaxTimes, baseCDN }) => {
|
|
1103
|
+
return info(uuid, {
|
|
1104
|
+
publicKey,
|
|
1105
|
+
baseURL,
|
|
1106
|
+
signal,
|
|
1107
|
+
source,
|
|
1108
|
+
integration,
|
|
1109
|
+
userAgent,
|
|
1110
|
+
retryThrottledRequestMaxTimes
|
|
1111
|
+
})
|
|
1112
|
+
.then((fileInfo) => new UploadcareFile(fileInfo, { baseCDN, fileName }))
|
|
1113
|
+
.then((result) => {
|
|
1114
|
+
// hack for node ¯\_(ツ)_/¯
|
|
1115
|
+
if (onProgress)
|
|
1116
|
+
onProgress({
|
|
1117
|
+
isComputable: true,
|
|
1118
|
+
value: 1
|
|
1119
|
+
});
|
|
1120
|
+
return result;
|
|
1121
|
+
});
|
|
1136
1122
|
};
|
|
1137
1123
|
|
|
1138
|
-
/**
|
|
1139
|
-
* Get file size.
|
|
1140
|
-
*/
|
|
1141
|
-
const getFileSize = (file) => {
|
|
1142
|
-
return file.length || file.size;
|
|
1143
|
-
};
|
|
1144
|
-
/**
|
|
1145
|
-
* Check if FileData is multipart data.
|
|
1146
|
-
*/
|
|
1147
|
-
const isMultipart = (fileSize, multipartMinFileSize = defaultSettings.multipartMinFileSize) => {
|
|
1148
|
-
return fileSize >= multipartMinFileSize;
|
|
1124
|
+
/**
|
|
1125
|
+
* Get file size.
|
|
1126
|
+
*/
|
|
1127
|
+
const getFileSize = (file) => {
|
|
1128
|
+
return file.length || file.size;
|
|
1129
|
+
};
|
|
1130
|
+
/**
|
|
1131
|
+
* Check if FileData is multipart data.
|
|
1132
|
+
*/
|
|
1133
|
+
const isMultipart = (fileSize, multipartMinFileSize = defaultSettings.multipartMinFileSize) => {
|
|
1134
|
+
return fileSize >= multipartMinFileSize;
|
|
1149
1135
|
};
|
|
1150
1136
|
|
|
1151
|
-
const sliceChunk = (file, index, fileSize, chunkSize) => {
|
|
1152
|
-
const start = chunkSize * index;
|
|
1153
|
-
const end = Math.min(start + chunkSize, fileSize);
|
|
1154
|
-
return file.slice(start, end);
|
|
1137
|
+
const sliceChunk = (file, index, fileSize, chunkSize) => {
|
|
1138
|
+
const start = chunkSize * index;
|
|
1139
|
+
const end = Math.min(start + chunkSize, fileSize);
|
|
1140
|
+
return file.slice(start, end);
|
|
1155
1141
|
};
|
|
1156
1142
|
|
|
1157
|
-
function prepareChunks(file, fileSize, chunkSize) {
|
|
1158
|
-
return (index) => sliceChunk(file, index, fileSize, chunkSize);
|
|
1143
|
+
function prepareChunks(file, fileSize, chunkSize) {
|
|
1144
|
+
return (index) => sliceChunk(file, index, fileSize, chunkSize);
|
|
1159
1145
|
}
|
|
1160
1146
|
|
|
1161
|
-
const runWithConcurrency = (concurrency, tasks) => {
|
|
1162
|
-
return new Promise((resolve, reject) => {
|
|
1163
|
-
const results = [];
|
|
1164
|
-
let rejected = false;
|
|
1165
|
-
let settled = tasks.length;
|
|
1166
|
-
const forRun = [...tasks];
|
|
1167
|
-
const run = () => {
|
|
1168
|
-
const index = tasks.length - forRun.length;
|
|
1169
|
-
const next = forRun.shift();
|
|
1170
|
-
if (next) {
|
|
1171
|
-
next()
|
|
1172
|
-
.then((result) => {
|
|
1173
|
-
if (rejected)
|
|
1174
|
-
return;
|
|
1175
|
-
results[index] = result;
|
|
1176
|
-
settled -= 1;
|
|
1177
|
-
if (settled) {
|
|
1178
|
-
run();
|
|
1179
|
-
}
|
|
1180
|
-
else {
|
|
1181
|
-
resolve(results);
|
|
1182
|
-
}
|
|
1183
|
-
})
|
|
1184
|
-
.catch((error) => {
|
|
1185
|
-
rejected = true;
|
|
1186
|
-
reject(error);
|
|
1187
|
-
});
|
|
1188
|
-
}
|
|
1189
|
-
};
|
|
1190
|
-
for (let i = 0; i < concurrency; i++) {
|
|
1191
|
-
run();
|
|
1192
|
-
}
|
|
1193
|
-
});
|
|
1147
|
+
const runWithConcurrency = (concurrency, tasks) => {
|
|
1148
|
+
return new Promise((resolve, reject) => {
|
|
1149
|
+
const results = [];
|
|
1150
|
+
let rejected = false;
|
|
1151
|
+
let settled = tasks.length;
|
|
1152
|
+
const forRun = [...tasks];
|
|
1153
|
+
const run = () => {
|
|
1154
|
+
const index = tasks.length - forRun.length;
|
|
1155
|
+
const next = forRun.shift();
|
|
1156
|
+
if (next) {
|
|
1157
|
+
next()
|
|
1158
|
+
.then((result) => {
|
|
1159
|
+
if (rejected)
|
|
1160
|
+
return;
|
|
1161
|
+
results[index] = result;
|
|
1162
|
+
settled -= 1;
|
|
1163
|
+
if (settled) {
|
|
1164
|
+
run();
|
|
1165
|
+
}
|
|
1166
|
+
else {
|
|
1167
|
+
resolve(results);
|
|
1168
|
+
}
|
|
1169
|
+
})
|
|
1170
|
+
.catch((error) => {
|
|
1171
|
+
rejected = true;
|
|
1172
|
+
reject(error);
|
|
1173
|
+
});
|
|
1174
|
+
}
|
|
1175
|
+
};
|
|
1176
|
+
for (let i = 0; i < concurrency; i++) {
|
|
1177
|
+
run();
|
|
1178
|
+
}
|
|
1179
|
+
});
|
|
1194
1180
|
};
|
|
1195
1181
|
|
|
1196
|
-
const uploadPartWithRetry = (chunk, url, { publicKey, onProgress, signal, integration, multipartMaxAttempts }) => retrier(({ attempt, retry }) => multipartUpload(chunk, url, {
|
|
1197
|
-
publicKey,
|
|
1198
|
-
onProgress,
|
|
1199
|
-
signal,
|
|
1200
|
-
integration
|
|
1201
|
-
}).catch((error) => {
|
|
1202
|
-
if (attempt < multipartMaxAttempts) {
|
|
1203
|
-
return retry();
|
|
1204
|
-
}
|
|
1205
|
-
throw error;
|
|
1206
|
-
}));
|
|
1207
|
-
const uploadMultipart = (file, { publicKey, fileName, fileSize, baseURL, secureSignature, secureExpire, store, signal, onProgress, source, integration, userAgent, retryThrottledRequestMaxTimes, contentType, multipartChunkSize = defaultSettings.multipartChunkSize, maxConcurrentRequests = defaultSettings.maxConcurrentRequests, multipartMaxAttempts = defaultSettings.multipartMaxAttempts, baseCDN, metadata }) => {
|
|
1208
|
-
const size = fileSize || getFileSize(file);
|
|
1209
|
-
let progressValues;
|
|
1210
|
-
const createProgressHandler = (totalChunks, chunkIdx) => {
|
|
1211
|
-
if (!onProgress)
|
|
1212
|
-
return;
|
|
1213
|
-
if (!progressValues) {
|
|
1214
|
-
progressValues = Array(totalChunks).fill(0);
|
|
1215
|
-
}
|
|
1216
|
-
const sum = (values) => values.reduce((sum, next) => sum + next, 0);
|
|
1217
|
-
return (info) => {
|
|
1218
|
-
if (!info.isComputable) {
|
|
1219
|
-
return;
|
|
1220
|
-
}
|
|
1221
|
-
progressValues[chunkIdx] = info.value;
|
|
1222
|
-
onProgress({
|
|
1223
|
-
isComputable: true,
|
|
1224
|
-
value: sum(progressValues) / totalChunks
|
|
1225
|
-
});
|
|
1226
|
-
};
|
|
1227
|
-
};
|
|
1228
|
-
return multipartStart(size, {
|
|
1229
|
-
publicKey,
|
|
1230
|
-
contentType,
|
|
1231
|
-
fileName: fileName
|
|
1232
|
-
baseURL,
|
|
1233
|
-
secureSignature,
|
|
1234
|
-
secureExpire,
|
|
1235
|
-
store,
|
|
1236
|
-
signal,
|
|
1237
|
-
source,
|
|
1238
|
-
integration,
|
|
1239
|
-
userAgent,
|
|
1240
|
-
retryThrottledRequestMaxTimes,
|
|
1241
|
-
metadata
|
|
1242
|
-
})
|
|
1243
|
-
.then(({ uuid, parts }) => {
|
|
1244
|
-
const getChunk = prepareChunks(file, size, multipartChunkSize);
|
|
1245
|
-
return Promise.all([
|
|
1246
|
-
uuid,
|
|
1247
|
-
runWithConcurrency(maxConcurrentRequests, parts.map((url, index) => () => uploadPartWithRetry(getChunk(index), url, {
|
|
1248
|
-
publicKey,
|
|
1249
|
-
onProgress: createProgressHandler(parts.length, index),
|
|
1250
|
-
signal,
|
|
1251
|
-
integration,
|
|
1252
|
-
multipartMaxAttempts
|
|
1253
|
-
})))
|
|
1254
|
-
]);
|
|
1255
|
-
})
|
|
1256
|
-
.then(([uuid]) => multipartComplete(uuid, {
|
|
1257
|
-
publicKey,
|
|
1258
|
-
baseURL,
|
|
1259
|
-
source,
|
|
1260
|
-
integration,
|
|
1261
|
-
userAgent,
|
|
1262
|
-
retryThrottledRequestMaxTimes
|
|
1263
|
-
}))
|
|
1264
|
-
.then((fileInfo) => {
|
|
1265
|
-
if (fileInfo.isReady) {
|
|
1266
|
-
return fileInfo;
|
|
1267
|
-
}
|
|
1268
|
-
else {
|
|
1269
|
-
return isReadyPoll({
|
|
1270
|
-
file: fileInfo.uuid,
|
|
1271
|
-
publicKey,
|
|
1272
|
-
baseURL,
|
|
1273
|
-
source,
|
|
1274
|
-
integration,
|
|
1275
|
-
userAgent,
|
|
1276
|
-
retryThrottledRequestMaxTimes,
|
|
1277
|
-
onProgress,
|
|
1278
|
-
signal
|
|
1279
|
-
});
|
|
1280
|
-
}
|
|
1281
|
-
})
|
|
1282
|
-
.then((fileInfo) => new UploadcareFile(fileInfo, { baseCDN }));
|
|
1182
|
+
const uploadPartWithRetry = (chunk, url, { publicKey, onProgress, signal, integration, multipartMaxAttempts }) => retrier(({ attempt, retry }) => multipartUpload(chunk, url, {
|
|
1183
|
+
publicKey,
|
|
1184
|
+
onProgress,
|
|
1185
|
+
signal,
|
|
1186
|
+
integration
|
|
1187
|
+
}).catch((error) => {
|
|
1188
|
+
if (attempt < multipartMaxAttempts) {
|
|
1189
|
+
return retry();
|
|
1190
|
+
}
|
|
1191
|
+
throw error;
|
|
1192
|
+
}));
|
|
1193
|
+
const uploadMultipart = (file, { publicKey, fileName, fileSize, baseURL, secureSignature, secureExpire, store, signal, onProgress, source, integration, userAgent, retryThrottledRequestMaxTimes, contentType, multipartChunkSize = defaultSettings.multipartChunkSize, maxConcurrentRequests = defaultSettings.maxConcurrentRequests, multipartMaxAttempts = defaultSettings.multipartMaxAttempts, baseCDN, metadata }) => {
|
|
1194
|
+
const size = fileSize || getFileSize(file);
|
|
1195
|
+
let progressValues;
|
|
1196
|
+
const createProgressHandler = (totalChunks, chunkIdx) => {
|
|
1197
|
+
if (!onProgress)
|
|
1198
|
+
return;
|
|
1199
|
+
if (!progressValues) {
|
|
1200
|
+
progressValues = Array(totalChunks).fill(0);
|
|
1201
|
+
}
|
|
1202
|
+
const sum = (values) => values.reduce((sum, next) => sum + next, 0);
|
|
1203
|
+
return (info) => {
|
|
1204
|
+
if (!info.isComputable) {
|
|
1205
|
+
return;
|
|
1206
|
+
}
|
|
1207
|
+
progressValues[chunkIdx] = info.value;
|
|
1208
|
+
onProgress({
|
|
1209
|
+
isComputable: true,
|
|
1210
|
+
value: sum(progressValues) / totalChunks
|
|
1211
|
+
});
|
|
1212
|
+
};
|
|
1213
|
+
};
|
|
1214
|
+
return multipartStart(size, {
|
|
1215
|
+
publicKey,
|
|
1216
|
+
contentType,
|
|
1217
|
+
fileName: fileName ?? file.name,
|
|
1218
|
+
baseURL,
|
|
1219
|
+
secureSignature,
|
|
1220
|
+
secureExpire,
|
|
1221
|
+
store,
|
|
1222
|
+
signal,
|
|
1223
|
+
source,
|
|
1224
|
+
integration,
|
|
1225
|
+
userAgent,
|
|
1226
|
+
retryThrottledRequestMaxTimes,
|
|
1227
|
+
metadata
|
|
1228
|
+
})
|
|
1229
|
+
.then(({ uuid, parts }) => {
|
|
1230
|
+
const getChunk = prepareChunks(file, size, multipartChunkSize);
|
|
1231
|
+
return Promise.all([
|
|
1232
|
+
uuid,
|
|
1233
|
+
runWithConcurrency(maxConcurrentRequests, parts.map((url, index) => () => uploadPartWithRetry(getChunk(index), url, {
|
|
1234
|
+
publicKey,
|
|
1235
|
+
onProgress: createProgressHandler(parts.length, index),
|
|
1236
|
+
signal,
|
|
1237
|
+
integration,
|
|
1238
|
+
multipartMaxAttempts
|
|
1239
|
+
})))
|
|
1240
|
+
]);
|
|
1241
|
+
})
|
|
1242
|
+
.then(([uuid]) => multipartComplete(uuid, {
|
|
1243
|
+
publicKey,
|
|
1244
|
+
baseURL,
|
|
1245
|
+
source,
|
|
1246
|
+
integration,
|
|
1247
|
+
userAgent,
|
|
1248
|
+
retryThrottledRequestMaxTimes
|
|
1249
|
+
}))
|
|
1250
|
+
.then((fileInfo) => {
|
|
1251
|
+
if (fileInfo.isReady) {
|
|
1252
|
+
return fileInfo;
|
|
1253
|
+
}
|
|
1254
|
+
else {
|
|
1255
|
+
return isReadyPoll({
|
|
1256
|
+
file: fileInfo.uuid,
|
|
1257
|
+
publicKey,
|
|
1258
|
+
baseURL,
|
|
1259
|
+
source,
|
|
1260
|
+
integration,
|
|
1261
|
+
userAgent,
|
|
1262
|
+
retryThrottledRequestMaxTimes,
|
|
1263
|
+
onProgress,
|
|
1264
|
+
signal
|
|
1265
|
+
});
|
|
1266
|
+
}
|
|
1267
|
+
})
|
|
1268
|
+
.then((fileInfo) => new UploadcareFile(fileInfo, { baseCDN }));
|
|
1283
1269
|
};
|
|
1284
1270
|
|
|
1285
|
-
/**
|
|
1286
|
-
* Uploads file from provided data.
|
|
1287
|
-
*/
|
|
1288
|
-
function uploadFile(data, { publicKey, fileName, baseURL = defaultSettings.baseURL, secureSignature, secureExpire, store, signal, onProgress, source, integration, userAgent, retryThrottledRequestMaxTimes, contentType, multipartMinFileSize, multipartChunkSize, multipartMaxAttempts, maxConcurrentRequests, baseCDN = defaultSettings.baseCDN, checkForUrlDuplicates, saveUrlForRecurrentUploads, pusherKey, metadata }) {
|
|
1289
|
-
if (isFileData(data)) {
|
|
1290
|
-
const fileSize = getFileSize(data);
|
|
1291
|
-
if (isMultipart(fileSize, multipartMinFileSize)) {
|
|
1292
|
-
return uploadMultipart(data, {
|
|
1293
|
-
publicKey,
|
|
1294
|
-
contentType,
|
|
1295
|
-
multipartChunkSize,
|
|
1296
|
-
multipartMaxAttempts,
|
|
1297
|
-
fileName,
|
|
1298
|
-
baseURL,
|
|
1299
|
-
secureSignature,
|
|
1300
|
-
secureExpire,
|
|
1301
|
-
store,
|
|
1302
|
-
signal,
|
|
1303
|
-
onProgress,
|
|
1304
|
-
source,
|
|
1305
|
-
integration,
|
|
1306
|
-
userAgent,
|
|
1307
|
-
maxConcurrentRequests,
|
|
1308
|
-
retryThrottledRequestMaxTimes,
|
|
1309
|
-
baseCDN,
|
|
1310
|
-
metadata
|
|
1311
|
-
});
|
|
1312
|
-
}
|
|
1313
|
-
return
|
|
1314
|
-
publicKey,
|
|
1315
|
-
fileName,
|
|
1316
|
-
contentType,
|
|
1317
|
-
baseURL,
|
|
1318
|
-
secureSignature,
|
|
1319
|
-
secureExpire,
|
|
1320
|
-
store,
|
|
1321
|
-
signal,
|
|
1322
|
-
onProgress,
|
|
1323
|
-
source,
|
|
1324
|
-
integration,
|
|
1325
|
-
userAgent,
|
|
1326
|
-
retryThrottledRequestMaxTimes,
|
|
1327
|
-
baseCDN,
|
|
1328
|
-
metadata
|
|
1329
|
-
});
|
|
1330
|
-
}
|
|
1331
|
-
if (isUrl(data)) {
|
|
1332
|
-
return uploadFromUrl(data, {
|
|
1333
|
-
publicKey,
|
|
1334
|
-
fileName,
|
|
1335
|
-
baseURL,
|
|
1336
|
-
baseCDN,
|
|
1337
|
-
checkForUrlDuplicates,
|
|
1338
|
-
saveUrlForRecurrentUploads,
|
|
1339
|
-
secureSignature,
|
|
1340
|
-
secureExpire,
|
|
1341
|
-
store,
|
|
1342
|
-
signal,
|
|
1343
|
-
onProgress,
|
|
1344
|
-
source,
|
|
1345
|
-
integration,
|
|
1346
|
-
userAgent,
|
|
1347
|
-
retryThrottledRequestMaxTimes,
|
|
1348
|
-
pusherKey,
|
|
1349
|
-
metadata
|
|
1350
|
-
});
|
|
1351
|
-
}
|
|
1352
|
-
if (isUuid(data)) {
|
|
1353
|
-
return uploadFromUploaded(data, {
|
|
1354
|
-
publicKey,
|
|
1355
|
-
fileName,
|
|
1356
|
-
baseURL,
|
|
1357
|
-
signal,
|
|
1358
|
-
onProgress,
|
|
1359
|
-
source,
|
|
1360
|
-
integration,
|
|
1361
|
-
userAgent,
|
|
1362
|
-
retryThrottledRequestMaxTimes,
|
|
1363
|
-
baseCDN
|
|
1364
|
-
});
|
|
1365
|
-
}
|
|
1366
|
-
throw new TypeError(`File uploading from "${data}" is not supported`);
|
|
1271
|
+
/**
|
|
1272
|
+
* Uploads file from provided data.
|
|
1273
|
+
*/
|
|
1274
|
+
function uploadFile(data, { publicKey, fileName, baseURL = defaultSettings.baseURL, secureSignature, secureExpire, store, signal, onProgress, source, integration, userAgent, retryThrottledRequestMaxTimes, contentType, multipartMinFileSize, multipartChunkSize, multipartMaxAttempts, maxConcurrentRequests, baseCDN = defaultSettings.baseCDN, checkForUrlDuplicates, saveUrlForRecurrentUploads, pusherKey, metadata }) {
|
|
1275
|
+
if (isFileData(data)) {
|
|
1276
|
+
const fileSize = getFileSize(data);
|
|
1277
|
+
if (isMultipart(fileSize, multipartMinFileSize)) {
|
|
1278
|
+
return uploadMultipart(data, {
|
|
1279
|
+
publicKey,
|
|
1280
|
+
contentType,
|
|
1281
|
+
multipartChunkSize,
|
|
1282
|
+
multipartMaxAttempts,
|
|
1283
|
+
fileName,
|
|
1284
|
+
baseURL,
|
|
1285
|
+
secureSignature,
|
|
1286
|
+
secureExpire,
|
|
1287
|
+
store,
|
|
1288
|
+
signal,
|
|
1289
|
+
onProgress,
|
|
1290
|
+
source,
|
|
1291
|
+
integration,
|
|
1292
|
+
userAgent,
|
|
1293
|
+
maxConcurrentRequests,
|
|
1294
|
+
retryThrottledRequestMaxTimes,
|
|
1295
|
+
baseCDN,
|
|
1296
|
+
metadata
|
|
1297
|
+
});
|
|
1298
|
+
}
|
|
1299
|
+
return uploadDirect(data, {
|
|
1300
|
+
publicKey,
|
|
1301
|
+
fileName,
|
|
1302
|
+
contentType,
|
|
1303
|
+
baseURL,
|
|
1304
|
+
secureSignature,
|
|
1305
|
+
secureExpire,
|
|
1306
|
+
store,
|
|
1307
|
+
signal,
|
|
1308
|
+
onProgress,
|
|
1309
|
+
source,
|
|
1310
|
+
integration,
|
|
1311
|
+
userAgent,
|
|
1312
|
+
retryThrottledRequestMaxTimes,
|
|
1313
|
+
baseCDN,
|
|
1314
|
+
metadata
|
|
1315
|
+
});
|
|
1316
|
+
}
|
|
1317
|
+
if (isUrl(data)) {
|
|
1318
|
+
return uploadFromUrl(data, {
|
|
1319
|
+
publicKey,
|
|
1320
|
+
fileName,
|
|
1321
|
+
baseURL,
|
|
1322
|
+
baseCDN,
|
|
1323
|
+
checkForUrlDuplicates,
|
|
1324
|
+
saveUrlForRecurrentUploads,
|
|
1325
|
+
secureSignature,
|
|
1326
|
+
secureExpire,
|
|
1327
|
+
store,
|
|
1328
|
+
signal,
|
|
1329
|
+
onProgress,
|
|
1330
|
+
source,
|
|
1331
|
+
integration,
|
|
1332
|
+
userAgent,
|
|
1333
|
+
retryThrottledRequestMaxTimes,
|
|
1334
|
+
pusherKey,
|
|
1335
|
+
metadata
|
|
1336
|
+
});
|
|
1337
|
+
}
|
|
1338
|
+
if (isUuid(data)) {
|
|
1339
|
+
return uploadFromUploaded(data, {
|
|
1340
|
+
publicKey,
|
|
1341
|
+
fileName,
|
|
1342
|
+
baseURL,
|
|
1343
|
+
signal,
|
|
1344
|
+
onProgress,
|
|
1345
|
+
source,
|
|
1346
|
+
integration,
|
|
1347
|
+
userAgent,
|
|
1348
|
+
retryThrottledRequestMaxTimes,
|
|
1349
|
+
baseCDN
|
|
1350
|
+
});
|
|
1351
|
+
}
|
|
1352
|
+
throw new TypeError(`File uploading from "${data}" is not supported`);
|
|
1367
1353
|
}
|
|
1368
1354
|
|
|
1369
|
-
class UploadcareGroup {
|
|
1370
|
-
constructor(groupInfo, files) {
|
|
1371
|
-
this.storedAt = null;
|
|
1372
|
-
this.uuid = groupInfo.id;
|
|
1373
|
-
this.filesCount = groupInfo.filesCount;
|
|
1374
|
-
this.totalSize = Object.values(groupInfo.files).reduce((acc, file) => acc + file.size, 0);
|
|
1375
|
-
this.isStored = !!groupInfo.datetimeStored;
|
|
1376
|
-
this.isImage = !!Object.values(groupInfo.files).filter((file) => file.isImage).length;
|
|
1377
|
-
this.cdnUrl = groupInfo.cdnUrl;
|
|
1378
|
-
this.files = files;
|
|
1379
|
-
this.createdAt = groupInfo.datetimeCreated;
|
|
1380
|
-
this.storedAt = groupInfo.datetimeStored;
|
|
1381
|
-
}
|
|
1355
|
+
class UploadcareGroup {
|
|
1356
|
+
constructor(groupInfo, files) {
|
|
1357
|
+
this.storedAt = null;
|
|
1358
|
+
this.uuid = groupInfo.id;
|
|
1359
|
+
this.filesCount = groupInfo.filesCount;
|
|
1360
|
+
this.totalSize = Object.values(groupInfo.files).reduce((acc, file) => acc + file.size, 0);
|
|
1361
|
+
this.isStored = !!groupInfo.datetimeStored;
|
|
1362
|
+
this.isImage = !!Object.values(groupInfo.files).filter((file) => file.isImage).length;
|
|
1363
|
+
this.cdnUrl = groupInfo.cdnUrl;
|
|
1364
|
+
this.files = files;
|
|
1365
|
+
this.createdAt = groupInfo.datetimeCreated;
|
|
1366
|
+
this.storedAt = groupInfo.datetimeStored;
|
|
1367
|
+
}
|
|
1382
1368
|
}
|
|
1383
1369
|
|
|
1384
|
-
/**
|
|
1385
|
-
* FileData type guard.
|
|
1386
|
-
*/
|
|
1387
|
-
const isFileDataArray = (data) => {
|
|
1388
|
-
for (const item of data) {
|
|
1389
|
-
if (!isFileData(item)) {
|
|
1390
|
-
return false;
|
|
1391
|
-
}
|
|
1392
|
-
}
|
|
1393
|
-
return true;
|
|
1394
|
-
};
|
|
1395
|
-
/**
|
|
1396
|
-
* Uuid type guard.
|
|
1397
|
-
*/
|
|
1398
|
-
const isUuidArray = (data) => {
|
|
1399
|
-
for (const item of data) {
|
|
1400
|
-
if (!isUuid(item)) {
|
|
1401
|
-
return false;
|
|
1402
|
-
}
|
|
1403
|
-
}
|
|
1404
|
-
return true;
|
|
1405
|
-
};
|
|
1406
|
-
/**
|
|
1407
|
-
* Url type guard.
|
|
1408
|
-
*/
|
|
1409
|
-
const isUrlArray = (data) => {
|
|
1410
|
-
for (const item of data) {
|
|
1411
|
-
if (!isUrl(item)) {
|
|
1412
|
-
return false;
|
|
1413
|
-
}
|
|
1414
|
-
}
|
|
1415
|
-
return true;
|
|
1370
|
+
/**
|
|
1371
|
+
* FileData type guard.
|
|
1372
|
+
*/
|
|
1373
|
+
const isFileDataArray = (data) => {
|
|
1374
|
+
for (const item of data) {
|
|
1375
|
+
if (!isFileData(item)) {
|
|
1376
|
+
return false;
|
|
1377
|
+
}
|
|
1378
|
+
}
|
|
1379
|
+
return true;
|
|
1380
|
+
};
|
|
1381
|
+
/**
|
|
1382
|
+
* Uuid type guard.
|
|
1383
|
+
*/
|
|
1384
|
+
const isUuidArray = (data) => {
|
|
1385
|
+
for (const item of data) {
|
|
1386
|
+
if (!isUuid(item)) {
|
|
1387
|
+
return false;
|
|
1388
|
+
}
|
|
1389
|
+
}
|
|
1390
|
+
return true;
|
|
1391
|
+
};
|
|
1392
|
+
/**
|
|
1393
|
+
* Url type guard.
|
|
1394
|
+
*/
|
|
1395
|
+
const isUrlArray = (data) => {
|
|
1396
|
+
for (const item of data) {
|
|
1397
|
+
if (!isUrl(item)) {
|
|
1398
|
+
return false;
|
|
1399
|
+
}
|
|
1400
|
+
}
|
|
1401
|
+
return true;
|
|
1416
1402
|
};
|
|
1417
1403
|
|
|
1418
|
-
function uploadFileGroup(data, { publicKey, fileName, baseURL = defaultSettings.baseURL, secureSignature, secureExpire, store, signal, onProgress, source, integration, userAgent, retryThrottledRequestMaxTimes, contentType, multipartChunkSize = defaultSettings.multipartChunkSize, baseCDN = defaultSettings.baseCDN, jsonpCallback
|
|
1419
|
-
if (!isFileDataArray(data) && !isUrlArray(data) && !isUuidArray(data)) {
|
|
1420
|
-
throw new TypeError(`Group uploading from "${data}" is not supported`);
|
|
1421
|
-
}
|
|
1422
|
-
let progressValues;
|
|
1423
|
-
let isStillComputable = true;
|
|
1424
|
-
const filesCount = data.length;
|
|
1425
|
-
const createProgressHandler = (size, index) => {
|
|
1426
|
-
if (!onProgress)
|
|
1427
|
-
return;
|
|
1428
|
-
if (!progressValues) {
|
|
1429
|
-
progressValues = Array(size).fill(0);
|
|
1430
|
-
}
|
|
1431
|
-
const normalize = (values) => values.reduce((sum, next) => sum + next) / size;
|
|
1432
|
-
return (info) => {
|
|
1433
|
-
if (!info.isComputable || !isStillComputable) {
|
|
1434
|
-
isStillComputable = false;
|
|
1435
|
-
onProgress({ isComputable: false });
|
|
1436
|
-
return;
|
|
1437
|
-
}
|
|
1438
|
-
progressValues[index] = info.value;
|
|
1439
|
-
onProgress({ isComputable: true, value: normalize(progressValues) });
|
|
1440
|
-
};
|
|
1441
|
-
};
|
|
1442
|
-
return Promise.all(data.map((file, index) => uploadFile(file, {
|
|
1443
|
-
publicKey,
|
|
1444
|
-
fileName,
|
|
1445
|
-
baseURL,
|
|
1446
|
-
secureSignature,
|
|
1447
|
-
secureExpire,
|
|
1448
|
-
store,
|
|
1449
|
-
signal,
|
|
1450
|
-
onProgress: createProgressHandler(filesCount, index),
|
|
1451
|
-
source,
|
|
1452
|
-
integration,
|
|
1453
|
-
userAgent,
|
|
1454
|
-
retryThrottledRequestMaxTimes,
|
|
1455
|
-
contentType,
|
|
1456
|
-
multipartChunkSize,
|
|
1457
|
-
baseCDN
|
|
1458
|
-
}))).then((files) => {
|
|
1459
|
-
const uuids = files.map((file) => file.uuid);
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
})
|
|
1479
|
-
.then((groupInfo) => new UploadcareGroup(groupInfo, filesInGroup))
|
|
1480
|
-
.then((group) => {
|
|
1481
|
-
onProgress && onProgress({ isComputable: true, value: 1 });
|
|
1482
|
-
return group;
|
|
1483
|
-
});
|
|
1484
|
-
});
|
|
1404
|
+
function uploadFileGroup(data, { publicKey, fileName, baseURL = defaultSettings.baseURL, secureSignature, secureExpire, store, signal, onProgress, source, integration, userAgent, retryThrottledRequestMaxTimes, contentType, multipartChunkSize = defaultSettings.multipartChunkSize, baseCDN = defaultSettings.baseCDN, jsonpCallback }) {
|
|
1405
|
+
if (!isFileDataArray(data) && !isUrlArray(data) && !isUuidArray(data)) {
|
|
1406
|
+
throw new TypeError(`Group uploading from "${data}" is not supported`);
|
|
1407
|
+
}
|
|
1408
|
+
let progressValues;
|
|
1409
|
+
let isStillComputable = true;
|
|
1410
|
+
const filesCount = data.length;
|
|
1411
|
+
const createProgressHandler = (size, index) => {
|
|
1412
|
+
if (!onProgress)
|
|
1413
|
+
return;
|
|
1414
|
+
if (!progressValues) {
|
|
1415
|
+
progressValues = Array(size).fill(0);
|
|
1416
|
+
}
|
|
1417
|
+
const normalize = (values) => values.reduce((sum, next) => sum + next) / size;
|
|
1418
|
+
return (info) => {
|
|
1419
|
+
if (!info.isComputable || !isStillComputable) {
|
|
1420
|
+
isStillComputable = false;
|
|
1421
|
+
onProgress({ isComputable: false });
|
|
1422
|
+
return;
|
|
1423
|
+
}
|
|
1424
|
+
progressValues[index] = info.value;
|
|
1425
|
+
onProgress({ isComputable: true, value: normalize(progressValues) });
|
|
1426
|
+
};
|
|
1427
|
+
};
|
|
1428
|
+
return Promise.all(data.map((file, index) => uploadFile(file, {
|
|
1429
|
+
publicKey,
|
|
1430
|
+
fileName,
|
|
1431
|
+
baseURL,
|
|
1432
|
+
secureSignature,
|
|
1433
|
+
secureExpire,
|
|
1434
|
+
store,
|
|
1435
|
+
signal,
|
|
1436
|
+
onProgress: createProgressHandler(filesCount, index),
|
|
1437
|
+
source,
|
|
1438
|
+
integration,
|
|
1439
|
+
userAgent,
|
|
1440
|
+
retryThrottledRequestMaxTimes,
|
|
1441
|
+
contentType,
|
|
1442
|
+
multipartChunkSize,
|
|
1443
|
+
baseCDN
|
|
1444
|
+
}))).then((files) => {
|
|
1445
|
+
const uuids = files.map((file) => file.uuid);
|
|
1446
|
+
return group(uuids, {
|
|
1447
|
+
publicKey,
|
|
1448
|
+
baseURL,
|
|
1449
|
+
jsonpCallback,
|
|
1450
|
+
secureSignature,
|
|
1451
|
+
secureExpire,
|
|
1452
|
+
signal,
|
|
1453
|
+
source,
|
|
1454
|
+
integration,
|
|
1455
|
+
userAgent,
|
|
1456
|
+
retryThrottledRequestMaxTimes
|
|
1457
|
+
})
|
|
1458
|
+
.then((groupInfo) => new UploadcareGroup(groupInfo, files))
|
|
1459
|
+
.then((group) => {
|
|
1460
|
+
onProgress && onProgress({ isComputable: true, value: 1 });
|
|
1461
|
+
return group;
|
|
1462
|
+
});
|
|
1463
|
+
});
|
|
1485
1464
|
}
|
|
1486
1465
|
|
|
1487
|
-
/**
|
|
1488
|
-
* Populate options with settings.
|
|
1489
|
-
*/
|
|
1490
|
-
const populateOptionsWithSettings = (options, settings) => (
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
this.settings = Object.assign(
|
|
1497
|
-
}
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
}
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
}
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
}
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
}
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
}
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
}
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
}
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
}
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
}
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
}
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
}
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
}
|
|
1466
|
+
/**
|
|
1467
|
+
* Populate options with settings.
|
|
1468
|
+
*/
|
|
1469
|
+
const populateOptionsWithSettings = (options, settings) => ({
|
|
1470
|
+
...settings,
|
|
1471
|
+
...options
|
|
1472
|
+
});
|
|
1473
|
+
class UploadClient {
|
|
1474
|
+
constructor(settings) {
|
|
1475
|
+
this.settings = Object.assign({}, defaultSettings, settings);
|
|
1476
|
+
}
|
|
1477
|
+
updateSettings(newSettings) {
|
|
1478
|
+
this.settings = Object.assign(this.settings, newSettings);
|
|
1479
|
+
}
|
|
1480
|
+
getSettings() {
|
|
1481
|
+
return this.settings;
|
|
1482
|
+
}
|
|
1483
|
+
base(file, options = {}) {
|
|
1484
|
+
const settings = this.getSettings();
|
|
1485
|
+
return base(file, populateOptionsWithSettings(options, settings));
|
|
1486
|
+
}
|
|
1487
|
+
info(uuid, options = {}) {
|
|
1488
|
+
const settings = this.getSettings();
|
|
1489
|
+
return info(uuid, populateOptionsWithSettings(options, settings));
|
|
1490
|
+
}
|
|
1491
|
+
fromUrl(sourceUrl, options = {}) {
|
|
1492
|
+
const settings = this.getSettings();
|
|
1493
|
+
return fromUrl(sourceUrl, populateOptionsWithSettings(options, settings));
|
|
1494
|
+
}
|
|
1495
|
+
fromUrlStatus(token, options = {}) {
|
|
1496
|
+
const settings = this.getSettings();
|
|
1497
|
+
return fromUrlStatus(token, populateOptionsWithSettings(options, settings));
|
|
1498
|
+
}
|
|
1499
|
+
group(uuids, options = {}) {
|
|
1500
|
+
const settings = this.getSettings();
|
|
1501
|
+
return group(uuids, populateOptionsWithSettings(options, settings));
|
|
1502
|
+
}
|
|
1503
|
+
groupInfo(id, options = {}) {
|
|
1504
|
+
const settings = this.getSettings();
|
|
1505
|
+
return groupInfo(id, populateOptionsWithSettings(options, settings));
|
|
1506
|
+
}
|
|
1507
|
+
multipartStart(size, options = {}) {
|
|
1508
|
+
const settings = this.getSettings();
|
|
1509
|
+
return multipartStart(size, populateOptionsWithSettings(options, settings));
|
|
1510
|
+
}
|
|
1511
|
+
multipartUpload(part, url, options = {}) {
|
|
1512
|
+
const settings = this.getSettings();
|
|
1513
|
+
return multipartUpload(part, url, populateOptionsWithSettings(options, settings));
|
|
1514
|
+
}
|
|
1515
|
+
multipartComplete(uuid, options = {}) {
|
|
1516
|
+
const settings = this.getSettings();
|
|
1517
|
+
return multipartComplete(uuid, populateOptionsWithSettings(options, settings));
|
|
1518
|
+
}
|
|
1519
|
+
uploadFile(data, options = {}) {
|
|
1520
|
+
const settings = this.getSettings();
|
|
1521
|
+
return uploadFile(data, populateOptionsWithSettings(options, settings));
|
|
1522
|
+
}
|
|
1523
|
+
uploadFileGroup(data, options = {}) {
|
|
1524
|
+
const settings = this.getSettings();
|
|
1525
|
+
return uploadFileGroup(data, populateOptionsWithSettings(options, settings));
|
|
1526
|
+
}
|
|
1545
1527
|
}
|
|
1546
1528
|
|
|
1547
|
-
export {
|
|
1529
|
+
export { UploadClient, UploadClientError, UploadcareFile, UploadcareGroup, base, fromUrl, fromUrlStatus, group, groupInfo, info, multipartComplete, multipartStart, multipartUpload, uploadDirect, uploadFile, uploadFileGroup, uploadFromUploaded, uploadFromUrl, uploadMultipart };
|