-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.mjs
107 lines (106 loc) · 2.71 KB
/
utils.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
* Returns a promise that resolve when
* props[field] match the given value.
* @param {Object} props
* @param {String} field
* @param {unknown} value
* @param {'eq'|'lte'|'gte'|'neq'|'lt'|'gt'} operator
*/
export const waitUntilValueMatch = async (
props,
field,
value,
operator = 'eq',
) => {
let attempts = 0;
while (attempts < 20000 / 50) {
switch (operator) {
case 'eq':
if (props[field] === value) {
return;
}
break;
case 'neq':
if (props[field] !== value) {
return;
}
break;
case 'lte':
if (props[field] <= value) {
return;
}
break;
case 'gte':
if (props[field] >= value) {
return;
}
break;
case 'lt':
if (props[field] < value) {
return;
}
break;
case 'gt':
if (props[field] > value) {
return;
}
break;
default:
throw new Error(`Invalid operator: ${operator}`);
}
attempts += 1;
await new Promise((r) => setTimeout(r, 50));
}
};
/**
* @param {import('./zipper.mjs').ZipperOptions} options
* @throws {Error}
*/
export const validateZipperOptions = (options) => {
if (options.maxConcurrentDownloads < options.minConcurrentDownloads) {
throw new Error(
'maxConcurrentDownloads must be greater than or equal to minConcurrentDownloads',
);
}
if (options.minConcurrentDownloads < 1) {
throw new Error('minConcurrentDownloads must be greater than 0');
}
if (options.maxConcurrentDownloads < 1) {
throw new Error('maxConcurrentDownloads must be greater than 0');
}
if (!options.sourceFiles.length) {
throw new Error(
'sourceFiles must contain at least one file to download and zip',
);
}
if (!options.destinationBucket) {
throw new Error('destinationBucket must be provided');
}
if (!options.destinationKey) {
throw new Error('destinationKey must be provided');
}
if (!options.s3Client) {
throw new Error('s3Client must be provided');
}
if (!options.onComplete) {
throw new Error('onComplete must be provided');
}
if (options.onError && typeof options.onError !== 'function') {
throw new Error('onError must be a function');
}
if (
options.onHttpUploadProgress &&
typeof options.onHttpUploadProgress !== 'function'
) {
throw new Error('onProgress must be a function');
}
if (options.onFileMissing && typeof options.onFileMissing !== 'function') {
throw new Error('onFileMissing must be a function');
}
if (
options.onFileDownloaded &&
typeof options.onFileDownloaded !== 'function'
) {
throw new Error('onFileDownloaded must be a function');
}
};