@sanity/export 2.21.11-reference-updates.62 → 2.21.12-purple-unicorn.1305
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/jest.config.js +3 -2
- package/lib/AssetHandler.js +138 -199
- package/lib/export.js +160 -178
- package/lib/filterDocumentTypes.js +2 -2
- package/lib/filterDrafts.js +2 -2
- package/lib/filterSystemDocuments.js +3 -3
- package/lib/getDocumentsStream.js +10 -17
- package/lib/logFirstChunk.js +4 -4
- package/lib/rejectOnApiError.js +1 -1
- package/lib/requestStream.js +42 -85
- package/lib/stringifyStream.js +1 -1
- package/lib/tryParseJson.js +4 -4
- package/lib/validateOptions.js +7 -7
- package/package.json +12 -11
- package/src/export.js +18 -2
- package/src/requestStream.js +10 -34
package/lib/export.js
CHANGED
|
@@ -1,65 +1,61 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
const os = require('os');
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
const path = require('path');
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
const zlib = require('zlib');
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
const fse = require('fs-extra');
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
const miss = require('mississippi');
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
const split = require('split2');
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
const archiver = require('archiver');
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
const debug = require('./debug');
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
const AssetHandler = require('./AssetHandler');
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
const stringifyStream = require('./stringifyStream');
|
|
22
22
|
|
|
23
|
-
|
|
23
|
+
const validateOptions = require('./validateOptions');
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
const rejectOnApiError = require('./rejectOnApiError');
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
const getDocumentsStream = require('./getDocumentsStream');
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
const filterSystemDocuments = require('./filterSystemDocuments');
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
const filterDocumentTypes = require('./filterDocumentTypes');
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
const filterDrafts = require('./filterDrafts');
|
|
34
34
|
|
|
35
|
-
|
|
35
|
+
const logFirstChunk = require('./logFirstChunk');
|
|
36
36
|
|
|
37
|
-
|
|
37
|
+
const tryParseJson = require('./tryParseJson');
|
|
38
38
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
var tryParseJson = require('./tryParseJson');
|
|
42
|
-
|
|
43
|
-
var noop = () => null;
|
|
39
|
+
const noop = () => null;
|
|
44
40
|
|
|
45
41
|
function exportDataset(opts) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
42
|
+
const options = validateOptions(opts);
|
|
43
|
+
const onProgress = options.onProgress || noop;
|
|
44
|
+
const archive = archiver('tar', {
|
|
49
45
|
gzip: true,
|
|
50
46
|
gzipOptions: {
|
|
51
47
|
level: options.compress ? zlib.Z_DEFAULT_COMPRESSION : zlib.Z_NO_COMPRESSION
|
|
52
48
|
}
|
|
53
49
|
});
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
50
|
+
const slugDate = new Date().toISOString().replace(/[^a-z0-9]/gi, '-').toLowerCase();
|
|
51
|
+
const prefix = "".concat(opts.dataset, "-export-").concat(slugDate);
|
|
52
|
+
const tmpDir = path.join(os.tmpdir(), prefix);
|
|
57
53
|
|
|
58
|
-
|
|
54
|
+
const cleanup = () => fse.remove(tmpDir).catch(err => {
|
|
59
55
|
debug("Error while cleaning up temporary files: ".concat(err.message));
|
|
60
56
|
});
|
|
61
57
|
|
|
62
|
-
|
|
58
|
+
const assetHandler = new AssetHandler({
|
|
63
59
|
client: options.client,
|
|
64
60
|
tmpDir,
|
|
65
61
|
prefix,
|
|
@@ -67,172 +63,158 @@ function exportDataset(opts) {
|
|
|
67
63
|
});
|
|
68
64
|
debug('Outputting assets (temporarily) to %s', tmpDir);
|
|
69
65
|
debug('Outputting to %s', options.outputPath === '-' ? 'stdout' : options.outputPath);
|
|
70
|
-
|
|
71
|
-
|
|
66
|
+
let outputStream;
|
|
67
|
+
|
|
68
|
+
if (isWritableStream(options.outputPath)) {
|
|
69
|
+
outputStream = options.outputPath;
|
|
70
|
+
} else {
|
|
71
|
+
outputStream = options.outputPath === '-' ? process.stdout : fse.createWriteStream(options.outputPath);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
let assetStreamHandler = assetHandler.noop;
|
|
72
75
|
|
|
73
76
|
if (!options.raw) {
|
|
74
77
|
assetStreamHandler = options.assets ? assetHandler.rewriteAssets : assetHandler.stripAssets;
|
|
75
78
|
}
|
|
76
79
|
|
|
77
|
-
return new Promise(
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
80
|
+
return new Promise(async (resolve, reject) => {
|
|
81
|
+
miss.finished(archive, async archiveErr => {
|
|
82
|
+
if (archiveErr) {
|
|
83
|
+
debug('Archiving errored! %s', archiveErr.stack);
|
|
84
|
+
await cleanup();
|
|
85
|
+
reject(archiveErr);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
debug('Archive finished!');
|
|
90
|
+
});
|
|
91
|
+
debug('Getting dataset export stream');
|
|
92
|
+
onProgress({
|
|
93
|
+
step: 'Exporting documents...'
|
|
94
|
+
});
|
|
95
|
+
let documentCount = 0;
|
|
96
|
+
let lastReported = Date.now();
|
|
97
|
+
|
|
98
|
+
const reportDocumentCount = (chunk, enc, cb) => {
|
|
99
|
+
++documentCount;
|
|
100
|
+
const now = Date.now();
|
|
101
|
+
|
|
102
|
+
if (now - lastReported > 50) {
|
|
103
|
+
onProgress({
|
|
104
|
+
step: 'Exporting documents...',
|
|
105
|
+
current: documentCount,
|
|
106
|
+
total: '?',
|
|
107
|
+
update: true
|
|
89
108
|
});
|
|
109
|
+
lastReported = now;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
cb(null, chunk);
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
const inputStream = await getDocumentsStream(options.client, options.dataset);
|
|
116
|
+
debug('Got HTTP %d', inputStream.statusCode);
|
|
117
|
+
debug('Response headers: %o', inputStream.headers);
|
|
118
|
+
const jsonStream = miss.pipeline(inputStream, logFirstChunk(), split(tryParseJson), rejectOnApiError(), filterSystemDocuments(), assetStreamHandler, filterDocumentTypes(options.types), options.drafts ? miss.through.obj() : filterDrafts(), stringifyStream(), miss.through(reportDocumentCount));
|
|
119
|
+
miss.finished(jsonStream, async err => {
|
|
120
|
+
if (err) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
90
123
|
|
|
91
|
-
return function (_x3) {
|
|
92
|
-
return _ref2.apply(this, arguments);
|
|
93
|
-
};
|
|
94
|
-
}());
|
|
95
|
-
debug('Getting dataset export stream');
|
|
96
124
|
onProgress({
|
|
97
|
-
step: 'Exporting documents...'
|
|
125
|
+
step: 'Exporting documents...',
|
|
126
|
+
current: documentCount,
|
|
127
|
+
total: documentCount,
|
|
128
|
+
update: true
|
|
98
129
|
});
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
});
|
|
113
|
-
lastReported = now;
|
|
130
|
+
|
|
131
|
+
if (!options.raw && options.assets) {
|
|
132
|
+
onProgress({
|
|
133
|
+
step: 'Downloading assets...'
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
let prevCompleted = 0;
|
|
138
|
+
const progressInterval = setInterval(() => {
|
|
139
|
+
const completed = assetHandler.queueSize - assetHandler.queue.size - assetHandler.queue.pending;
|
|
140
|
+
|
|
141
|
+
if (prevCompleted === completed) {
|
|
142
|
+
return;
|
|
114
143
|
}
|
|
115
144
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
var jsonStream = miss.pipeline(inputStream, logFirstChunk(), split(tryParseJson), rejectOnApiError(), filterSystemDocuments(), assetStreamHandler, filterDocumentTypes(options.types), options.drafts ? miss.through.obj() : filterDrafts(), stringifyStream(), miss.through(reportDocumentCount));
|
|
123
|
-
miss.finished(jsonStream, /*#__PURE__*/function () {
|
|
124
|
-
var _ref3 = _asyncToGenerator(function* (err) {
|
|
125
|
-
if (err) {
|
|
126
|
-
return;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
onProgress({
|
|
130
|
-
step: 'Exporting documents...',
|
|
131
|
-
current: documentCount,
|
|
132
|
-
total: documentCount,
|
|
133
|
-
update: true
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
if (!options.raw && options.assets) {
|
|
137
|
-
onProgress({
|
|
138
|
-
step: 'Downloading assets...'
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
var prevCompleted = 0;
|
|
143
|
-
var progressInterval = setInterval(() => {
|
|
144
|
-
var completed = assetHandler.queueSize - assetHandler.queue.size - assetHandler.queue.pending;
|
|
145
|
-
|
|
146
|
-
if (prevCompleted === completed) {
|
|
147
|
-
return;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
prevCompleted = completed;
|
|
151
|
-
onProgress({
|
|
152
|
-
step: 'Downloading assets...',
|
|
153
|
-
current: completed,
|
|
154
|
-
total: assetHandler.queueSize,
|
|
155
|
-
update: true
|
|
156
|
-
});
|
|
157
|
-
}, 500);
|
|
158
|
-
debug('Waiting for asset handler to complete downloads');
|
|
159
|
-
|
|
160
|
-
try {
|
|
161
|
-
var assetMap = yield assetHandler.finish(); // Make sure we mark the progress as done (eg 100/100 instead of 99/100)
|
|
162
|
-
|
|
163
|
-
onProgress({
|
|
164
|
-
step: 'Downloading assets...',
|
|
165
|
-
current: assetHandler.queueSize,
|
|
166
|
-
total: assetHandler.queueSize,
|
|
167
|
-
update: true
|
|
168
|
-
});
|
|
169
|
-
archive.append(JSON.stringify(assetMap), {
|
|
170
|
-
name: 'assets.json',
|
|
171
|
-
prefix
|
|
172
|
-
});
|
|
173
|
-
clearInterval(progressInterval);
|
|
174
|
-
} catch (assetErr) {
|
|
175
|
-
clearInterval(progressInterval);
|
|
176
|
-
yield cleanup();
|
|
177
|
-
reject(assetErr);
|
|
178
|
-
return;
|
|
179
|
-
} // Add all downloaded assets to archive
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
archive.directory(path.join(tmpDir, 'files'), "".concat(prefix, "/files"), {
|
|
183
|
-
store: true
|
|
184
|
-
});
|
|
185
|
-
archive.directory(path.join(tmpDir, 'images'), "".concat(prefix, "/images"), {
|
|
186
|
-
store: true
|
|
187
|
-
});
|
|
188
|
-
debug('Finalizing archive, flushing streams');
|
|
189
|
-
onProgress({
|
|
190
|
-
step: 'Adding assets to archive...'
|
|
191
|
-
});
|
|
192
|
-
archive.finalize();
|
|
145
|
+
prevCompleted = completed;
|
|
146
|
+
onProgress({
|
|
147
|
+
step: 'Downloading assets...',
|
|
148
|
+
current: completed,
|
|
149
|
+
total: assetHandler.queueSize,
|
|
150
|
+
update: true
|
|
193
151
|
});
|
|
152
|
+
}, 500);
|
|
153
|
+
debug('Waiting for asset handler to complete downloads');
|
|
154
|
+
|
|
155
|
+
try {
|
|
156
|
+
const assetMap = await assetHandler.finish(); // Make sure we mark the progress as done (eg 100/100 instead of 99/100)
|
|
194
157
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
158
|
+
onProgress({
|
|
159
|
+
step: 'Downloading assets...',
|
|
160
|
+
current: assetHandler.queueSize,
|
|
161
|
+
total: assetHandler.queueSize,
|
|
162
|
+
update: true
|
|
163
|
+
});
|
|
164
|
+
archive.append(JSON.stringify(assetMap), {
|
|
165
|
+
name: 'assets.json',
|
|
166
|
+
prefix
|
|
167
|
+
});
|
|
168
|
+
clearInterval(progressInterval);
|
|
169
|
+
} catch (assetErr) {
|
|
170
|
+
clearInterval(progressInterval);
|
|
171
|
+
await cleanup();
|
|
172
|
+
reject(assetErr);
|
|
173
|
+
return;
|
|
174
|
+
} // Add all downloaded assets to archive
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
archive.directory(path.join(tmpDir, 'files'), "".concat(prefix, "/files"), {
|
|
178
|
+
store: true
|
|
179
|
+
});
|
|
180
|
+
archive.directory(path.join(tmpDir, 'images'), "".concat(prefix, "/images"), {
|
|
181
|
+
store: true
|
|
201
182
|
});
|
|
202
|
-
archive
|
|
203
|
-
|
|
204
|
-
|
|
183
|
+
debug('Finalizing archive, flushing streams');
|
|
184
|
+
onProgress({
|
|
185
|
+
step: 'Adding assets to archive...'
|
|
205
186
|
});
|
|
206
|
-
|
|
187
|
+
archive.finalize();
|
|
188
|
+
});
|
|
189
|
+
archive.on('warning', err => {
|
|
190
|
+
debug('Archive warning: %s', err.message);
|
|
191
|
+
});
|
|
192
|
+
archive.append(jsonStream, {
|
|
193
|
+
name: 'data.ndjson',
|
|
194
|
+
prefix
|
|
195
|
+
});
|
|
196
|
+
miss.pipe(archive, outputStream, onComplete);
|
|
207
197
|
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
198
|
+
async function onComplete(err) {
|
|
199
|
+
onProgress({
|
|
200
|
+
step: 'Clearing temporary files...'
|
|
201
|
+
});
|
|
202
|
+
await cleanup();
|
|
211
203
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
step: 'Clearing temporary files...'
|
|
216
|
-
});
|
|
217
|
-
yield cleanup();
|
|
218
|
-
|
|
219
|
-
if (!err) {
|
|
220
|
-
resolve();
|
|
221
|
-
return;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
debug('Error during streaming: %s', err.stack);
|
|
225
|
-
assetHandler.clear();
|
|
226
|
-
reject(err);
|
|
227
|
-
});
|
|
228
|
-
return _onComplete.apply(this, arguments);
|
|
204
|
+
if (!err) {
|
|
205
|
+
resolve();
|
|
206
|
+
return;
|
|
229
207
|
}
|
|
230
|
-
});
|
|
231
208
|
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
209
|
+
debug('Error during streaming: %s', err.stack);
|
|
210
|
+
assetHandler.clear();
|
|
211
|
+
reject(err);
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function isWritableStream(val) {
|
|
217
|
+
return val !== null && typeof val === 'object' && typeof val.pipe === 'function' && typeof val._write === 'function' && typeof val._writableState === 'object';
|
|
236
218
|
}
|
|
237
219
|
|
|
238
220
|
module.exports = exportDataset;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
const miss = require('mississippi');
|
|
4
4
|
|
|
5
5
|
module.exports = allowedTypes => allowedTypes ? miss.through.obj((doc, enc, callback) => {
|
|
6
|
-
|
|
6
|
+
const type = doc && doc._type;
|
|
7
7
|
|
|
8
8
|
if (allowedTypes.includes(type)) {
|
|
9
9
|
callback(null, doc);
|
package/lib/filterDrafts.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
const miss = require('mississippi');
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
const isDraft = doc => doc && doc._id && doc._id.indexOf('drafts.') === 0;
|
|
6
6
|
|
|
7
7
|
module.exports = () => miss.through.obj((doc, enc, callback) => {
|
|
8
8
|
if (isDraft(doc)) {
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
const miss = require('mississippi');
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
const debug = require('./debug');
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
const isSystemDocument = doc => doc && doc._id && doc._id.indexOf('_.') === 0;
|
|
8
8
|
|
|
9
9
|
module.exports = () => miss.through.obj((doc, enc, callback) => {
|
|
10
10
|
if (isSystemDocument(doc)) {
|
|
@@ -1,27 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
const pkg = require('../package.json');
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
8
|
-
|
|
9
|
-
var pkg = require('../package.json');
|
|
10
|
-
|
|
11
|
-
var requestStream = require('./requestStream');
|
|
5
|
+
const requestStream = require('./requestStream');
|
|
12
6
|
|
|
13
7
|
module.exports = (client, dataset) => {
|
|
14
8
|
// Sanity client doesn't handle streams natively since we want to support node/browser
|
|
15
9
|
// with same API. We're just using it here to get hold of URLs and tokens.
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
24
|
-
|
|
10
|
+
const url = client.getUrl("/data/export/".concat(dataset));
|
|
11
|
+
const token = client.config().token;
|
|
12
|
+
const headers = {
|
|
13
|
+
'User-Agent': "".concat(pkg.name, "@").concat(pkg.version),
|
|
14
|
+
...(token ? {
|
|
15
|
+
Authorization: "Bearer ".concat(token)
|
|
16
|
+
} : {})
|
|
17
|
+
};
|
|
25
18
|
return requestStream({
|
|
26
19
|
url,
|
|
27
20
|
headers
|
package/lib/logFirstChunk.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
const miss = require('mississippi');
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
const debug = require('./debug');
|
|
6
6
|
|
|
7
7
|
module.exports = () => {
|
|
8
|
-
|
|
8
|
+
let firstChunk = true;
|
|
9
9
|
return miss.through((chunk, enc, callback) => {
|
|
10
10
|
if (firstChunk) {
|
|
11
|
-
|
|
11
|
+
const string = chunk.toString('utf8').split('\n')[0];
|
|
12
12
|
debug('First chunk received: %s', string.slice(0, 300));
|
|
13
13
|
firstChunk = false;
|
|
14
14
|
}
|
package/lib/rejectOnApiError.js
CHANGED
package/lib/requestStream.js
CHANGED
|
@@ -1,54 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
const getIt = require('get-it');
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
const {
|
|
6
|
+
keepAlive,
|
|
7
|
+
promise
|
|
8
|
+
} = require('get-it/middleware');
|
|
6
9
|
|
|
7
|
-
|
|
10
|
+
const debug = require('./debug');
|
|
8
11
|
|
|
9
|
-
|
|
12
|
+
const request = getIt([keepAlive(), promise({
|
|
13
|
+
onlyBody: true
|
|
14
|
+
})]);
|
|
15
|
+
const socketsWithTimeout = new WeakSet();
|
|
16
|
+
const CONNECTION_TIMEOUT = 15 * 1000; // 15 seconds
|
|
10
17
|
|
|
11
|
-
|
|
18
|
+
const READ_TIMEOUT = 3 * 60 * 1000; // 3 minutes
|
|
12
19
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
var HttpAgent = require('agentkeepalive');
|
|
16
|
-
|
|
17
|
-
var debug = require('./debug');
|
|
18
|
-
|
|
19
|
-
var HttpsAgent = HttpAgent.HttpsAgent;
|
|
20
|
-
var httpAgent = new HttpAgent();
|
|
21
|
-
var httpsAgent = new HttpsAgent();
|
|
22
|
-
var socketsWithTimeout = new WeakSet();
|
|
23
|
-
var CONNECTION_TIMEOUT = 15 * 1000; // 15 seconds
|
|
24
|
-
|
|
25
|
-
var READ_TIMEOUT = 3 * 60 * 1000; // 3 minutes
|
|
26
|
-
|
|
27
|
-
var MAX_RETRIES = 5; // Just a promisified simpleGet
|
|
28
|
-
|
|
29
|
-
function getStream(options) {
|
|
30
|
-
return new Promise((resolve, reject) => {
|
|
31
|
-
var rejected = false;
|
|
32
|
-
var openTimeout = setTimeout(() => {
|
|
33
|
-
rejected = true;
|
|
34
|
-
reject(new Error("Connection timed out after ".concat(CONNECTION_TIMEOUT, " ms")));
|
|
35
|
-
}, CONNECTION_TIMEOUT);
|
|
36
|
-
simpleGet(options, (err, res) => {
|
|
37
|
-
clearTimeout(openTimeout);
|
|
38
|
-
|
|
39
|
-
if (rejected) {
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
if (err) {
|
|
44
|
-
reject(err);
|
|
45
|
-
return;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
resolve(res);
|
|
49
|
-
});
|
|
50
|
-
});
|
|
51
|
-
}
|
|
20
|
+
const MAX_RETRIES = 5;
|
|
52
21
|
|
|
53
22
|
function delay(ms) {
|
|
54
23
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
@@ -56,51 +25,39 @@ function delay(ms) {
|
|
|
56
25
|
/* eslint-disable no-await-in-loop, max-depth */
|
|
57
26
|
|
|
58
27
|
|
|
59
|
-
module.exports =
|
|
60
|
-
|
|
61
|
-
var agent = options.url.startsWith('https:') ? httpsAgent : httpAgent;
|
|
62
|
-
|
|
63
|
-
var reqOptions = _objectSpread(_objectSpread({}, options), {}, {
|
|
64
|
-
followRedirects: false,
|
|
65
|
-
agent
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
var error;
|
|
69
|
-
|
|
70
|
-
for (var i = 0; i < MAX_RETRIES; i++) {
|
|
71
|
-
try {
|
|
72
|
-
var _ret = yield* function* () {
|
|
73
|
-
var response = yield getStream(reqOptions);
|
|
74
|
-
|
|
75
|
-
if (response.connection && typeof response.connection.setTimeout === 'function' && !socketsWithTimeout.has(response.connection)) {
|
|
76
|
-
socketsWithTimeout.add(response.connection);
|
|
77
|
-
response.connection.setTimeout(READ_TIMEOUT, () => {
|
|
78
|
-
response.destroy(new Error("Read timeout: No data received on socket for ".concat(READ_TIMEOUT, " ms")));
|
|
79
|
-
});
|
|
80
|
-
}
|
|
28
|
+
module.exports = async options => {
|
|
29
|
+
let error;
|
|
81
30
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
31
|
+
for (let i = 0; i < MAX_RETRIES; i++) {
|
|
32
|
+
try {
|
|
33
|
+
const response = await request({ ...options,
|
|
34
|
+
stream: true,
|
|
35
|
+
maxRedirects: 0,
|
|
36
|
+
timeout: {
|
|
37
|
+
connect: CONNECTION_TIMEOUT,
|
|
38
|
+
socket: READ_TIMEOUT
|
|
39
|
+
}
|
|
40
|
+
});
|
|
86
41
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
42
|
+
if (response.connection && typeof response.connection.setTimeout === 'function' && !socketsWithTimeout.has(response.connection)) {
|
|
43
|
+
socketsWithTimeout.add(response.connection);
|
|
44
|
+
response.connection.setTimeout(READ_TIMEOUT, () => {
|
|
45
|
+
response.destroy(new Error("Read timeout: No data received on socket for ".concat(READ_TIMEOUT, " ms")));
|
|
46
|
+
});
|
|
47
|
+
}
|
|
90
48
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
49
|
+
return response;
|
|
50
|
+
} catch (err) {
|
|
51
|
+
error = err;
|
|
94
52
|
|
|
95
|
-
|
|
96
|
-
|
|
53
|
+
if (err.response && err.response.statusCode && err.response.statusCode < 500) {
|
|
54
|
+
break;
|
|
97
55
|
}
|
|
98
|
-
}
|
|
99
56
|
|
|
100
|
-
|
|
101
|
-
|
|
57
|
+
debug('Error, retrying after 1500ms: %s', err.message);
|
|
58
|
+
await delay(1500);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
102
61
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
};
|
|
106
|
-
}();
|
|
62
|
+
throw error;
|
|
63
|
+
};
|
package/lib/stringifyStream.js
CHANGED