Skip to content

Commit

Permalink
[export] Fix bug where streams would be reused across invocations (#1146
Browse files Browse the repository at this point in the history
)
  • Loading branch information
rexxars committed Jan 4, 2019
1 parent 2b7f011 commit 0c29077
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 27 deletions.
13 changes: 9 additions & 4 deletions packages/@sanity/export/src/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const getDocumentsStream = require('./getDocumentsStream')
const filterSystemDocuments = require('./filterSystemDocuments')
const filterDocumentTypes = require('./filterDocumentTypes')
const filterDrafts = require('./filterDrafts')
const logFirstChunk = require('./logFirstChunk')

const noop = () => null

Expand Down Expand Up @@ -83,15 +84,19 @@ function exportDataset(opts) {
}

const inputStream = await getDocumentsStream(options.client, options.dataset)
debug('Got HTTP %d', inputStream.statusCode)
debug('Response headers: %o', inputStream.headers)

const jsonStream = miss.pipeline(
inputStream,
logFirstChunk(),
split(JSON.parse),
rejectOnApiError,
filterSystemDocuments,
rejectOnApiError(),
filterSystemDocuments(),
assetStreamHandler,
filterDocumentTypes(options.types),
options.drafts ? miss.through.obj() : filterDrafts,
stringifyStream,
options.drafts ? miss.through.obj() : filterDrafts(),
stringifyStream(),
miss.through(reportDocumentCount)
)

Expand Down
13 changes: 7 additions & 6 deletions packages/@sanity/export/src/filterDrafts.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ const miss = require('mississippi')

const isDraft = doc => doc && doc._id && doc._id.indexOf('drafts.') === 0

module.exports = miss.through.obj((doc, enc, callback) => {
if (isDraft(doc)) {
return callback()
}
module.exports = () =>
miss.through.obj((doc, enc, callback) => {
if (isDraft(doc)) {
return callback()
}

return callback(null, doc)
})
return callback(null, doc)
})
15 changes: 8 additions & 7 deletions packages/@sanity/export/src/filterSystemDocuments.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ const debug = require('./debug')

const isSystemDocument = doc => doc && doc._id && doc._id.indexOf('_.') === 0

module.exports = miss.through.obj((doc, enc, callback) => {
if (isSystemDocument(doc)) {
debug('%s is a system document, skipping', doc && doc._id)
return callback()
}
module.exports = () =>
miss.through.obj((doc, enc, callback) => {
if (isSystemDocument(doc)) {
debug('%s is a system document, skipping', doc && doc._id)
return callback()
}

return callback(null, doc)
})
return callback(null, doc)
})
15 changes: 15 additions & 0 deletions packages/@sanity/export/src/logFirstChunk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const miss = require('mississippi')
const debug = require('./debug')

module.exports = () => {
let firstChunk = true
return miss.through((chunk, enc, callback) => {
if (firstChunk) {
const string = chunk.toString('utf8').split('\n')[0]
debug('First chunk received: %s', string.slice(0, 300))
firstChunk = false
}

callback(null, chunk)
})
}
15 changes: 8 additions & 7 deletions packages/@sanity/export/src/rejectOnApiError.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const miss = require('mississippi')

module.exports = miss.through.obj((doc, enc, callback) => {
if (doc.error && doc.statusCode) {
callback(new Error([doc.statusCode, doc.error].join(': ')))
return
}
module.exports = () =>
miss.through.obj((doc, enc, callback) => {
if (doc.error && doc.statusCode) {
callback(new Error([doc.statusCode, doc.error].join(': ')))
return
}

callback(null, doc)
})
callback(null, doc)
})
5 changes: 2 additions & 3 deletions packages/@sanity/export/src/stringifyStream.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const miss = require('mississippi')

module.exports = miss.through.obj((doc, enc, callback) =>
callback(null, `${JSON.stringify(doc)}\n`)
)
module.exports = () =>
miss.through.obj((doc, enc, callback) => callback(null, `${JSON.stringify(doc)}\n`))

0 comments on commit 0c29077

Please sign in to comment.