@sanity/export 0.136.3-gql-rtb.372 → 0.136.3-purple-unicorn-patch.5627
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/LICENSE +1 -1
- package/README.md +8 -5
- package/lib/AssetHandler.js +213 -178
- package/lib/export.js +145 -140
- package/lib/filterDrafts.js +1 -1
- package/lib/filterSystemDocuments.js +1 -1
- package/lib/getDocumentsStream.js +6 -3
- package/lib/logFirstChunk.js +18 -0
- package/lib/rejectOnApiError.js +6 -1
- package/lib/requestStream.js +60 -4
- package/lib/stringifyStream.js +1 -1
- package/lib/tryParseJson.js +25 -0
- package/lib/validateOptions.js +8 -4
- package/package.json +18 -17
- package/src/AssetHandler.js +168 -52
- package/src/export.js +59 -22
- package/src/filterDocumentTypes.js +1 -1
- package/src/filterDrafts.js +8 -7
- package/src/filterSystemDocuments.js +9 -8
- package/src/getDocumentsStream.js +3 -2
- package/src/logFirstChunk.js +15 -0
- package/src/rejectOnApiError.js +13 -7
- package/src/requestStream.js +54 -5
- package/src/stringifyStream.js +2 -3
- package/src/tryParseJson.js +21 -0
- package/src/validateOptions.js +7 -3
package/src/requestStream.js
CHANGED
|
@@ -1,6 +1,55 @@
|
|
|
1
|
-
const
|
|
1
|
+
const getIt = require('get-it')
|
|
2
|
+
const {keepAlive, promise} = require('get-it/middleware')
|
|
3
|
+
const debug = require('./debug')
|
|
2
4
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
const request = getIt([keepAlive(), promise({onlyBody: true})])
|
|
6
|
+
const socketsWithTimeout = new WeakSet()
|
|
7
|
+
|
|
8
|
+
const CONNECTION_TIMEOUT = 15 * 1000 // 15 seconds
|
|
9
|
+
const READ_TIMEOUT = 3 * 60 * 1000 // 3 minutes
|
|
10
|
+
const MAX_RETRIES = 5
|
|
11
|
+
|
|
12
|
+
function delay(ms) {
|
|
13
|
+
return new Promise((resolve) => setTimeout(resolve, ms))
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/* eslint-disable no-await-in-loop, max-depth */
|
|
17
|
+
module.exports = async (options) => {
|
|
18
|
+
let error
|
|
19
|
+
for (let i = 0; i < MAX_RETRIES; i++) {
|
|
20
|
+
try {
|
|
21
|
+
const response = await request({
|
|
22
|
+
...options,
|
|
23
|
+
stream: true,
|
|
24
|
+
maxRedirects: 0,
|
|
25
|
+
timeout: {connect: CONNECTION_TIMEOUT, socket: READ_TIMEOUT},
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
if (
|
|
29
|
+
response.connection &&
|
|
30
|
+
typeof response.connection.setTimeout === 'function' &&
|
|
31
|
+
!socketsWithTimeout.has(response.connection)
|
|
32
|
+
) {
|
|
33
|
+
socketsWithTimeout.add(response.connection)
|
|
34
|
+
response.connection.setTimeout(READ_TIMEOUT, () => {
|
|
35
|
+
response.destroy(
|
|
36
|
+
new Error(`Read timeout: No data received on socket for ${READ_TIMEOUT} ms`)
|
|
37
|
+
)
|
|
38
|
+
})
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return response
|
|
42
|
+
} catch (err) {
|
|
43
|
+
error = err
|
|
44
|
+
|
|
45
|
+
if (err.response && err.response.statusCode && err.response.statusCode < 500) {
|
|
46
|
+
break
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
debug('Error, retrying after 1500ms: %s', err.message)
|
|
50
|
+
await delay(1500)
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
throw error
|
|
55
|
+
}
|
package/src/stringifyStream.js
CHANGED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
module.exports = (line) => {
|
|
2
|
+
try {
|
|
3
|
+
return JSON.parse(line)
|
|
4
|
+
} catch (err) {
|
|
5
|
+
// Catch half-done lines with an error at the end
|
|
6
|
+
const errorPosition = line.lastIndexOf('{"error":')
|
|
7
|
+
if (errorPosition === -1) {
|
|
8
|
+
err.message = `${err.message} (${line})`
|
|
9
|
+
throw err
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const errorJson = line.slice(errorPosition)
|
|
13
|
+
const errorLine = JSON.parse(errorJson)
|
|
14
|
+
const error = errorLine && errorLine.error
|
|
15
|
+
if (error && error.description) {
|
|
16
|
+
throw new Error(`Error streaming dataset: ${error.description}\n\n${errorJson}\n`)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
throw err
|
|
20
|
+
}
|
|
21
|
+
}
|
package/src/validateOptions.js
CHANGED
|
@@ -6,7 +6,7 @@ const exportDefaults = {
|
|
|
6
6
|
compress: true,
|
|
7
7
|
drafts: true,
|
|
8
8
|
assets: true,
|
|
9
|
-
raw: false
|
|
9
|
+
raw: false,
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
function validateOptions(opts) {
|
|
@@ -24,7 +24,7 @@ function validateOptions(opts) {
|
|
|
24
24
|
throw new Error('`options.client` must be set to an instance of @sanity/client')
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
const missing = clientMethods.find(key => typeof options.client[key] !== 'function')
|
|
27
|
+
const missing = clientMethods.find((key) => typeof options.client[key] !== 'function')
|
|
28
28
|
if (missing) {
|
|
29
29
|
throw new Error(
|
|
30
30
|
`\`options.client\` is not a valid @sanity/client instance - no "${missing}" method found`
|
|
@@ -36,7 +36,7 @@ function validateOptions(opts) {
|
|
|
36
36
|
throw new Error('Client is not instantiated with a `token`')
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
booleanFlags.forEach(flag => {
|
|
39
|
+
booleanFlags.forEach((flag) => {
|
|
40
40
|
if (typeof options[flag] !== 'boolean') {
|
|
41
41
|
throw new Error(`Flag ${flag} must be a boolean (true/false)`)
|
|
42
42
|
}
|
|
@@ -46,6 +46,10 @@ function validateOptions(opts) {
|
|
|
46
46
|
throw new Error('outputPath must be specified (- for stdout)')
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
+
if (options.assetConcurrency && (options.assetConcurrency < 1 || options.assetConcurrency > 24)) {
|
|
50
|
+
throw new Error('`assetConcurrency` must be between 1 and 24')
|
|
51
|
+
}
|
|
52
|
+
|
|
49
53
|
return options
|
|
50
54
|
}
|
|
51
55
|
|