@sanity/export 0.136.3-gql-rtb.375 → 0.136.3-purple-unicorn-patch.5629

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.
@@ -1,6 +1,55 @@
1
- const simpleGet = require('simple-get')
1
+ const getIt = require('get-it')
2
+ const {keepAlive, promise} = require('get-it/middleware')
3
+ const debug = require('./debug')
2
4
 
3
- module.exports = options =>
4
- new Promise((resolve, reject) => {
5
- simpleGet(options, (err, res) => (err ? reject(err) : resolve(res)))
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
+ }
@@ -1,5 +1,4 @@
1
1
  const miss = require('mississippi')
2
2
 
3
- module.exports = miss.through.obj((doc, enc, callback) =>
4
- callback(null, `${JSON.stringify(doc)}\n`)
5
- )
3
+ module.exports = () =>
4
+ miss.through.obj((doc, enc, callback) => callback(null, `${JSON.stringify(doc)}\n`))
@@ -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
+ }
@@ -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