-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for exporting documents with "inconsistent" cursor
- Loading branch information
Showing
5 changed files
with
236 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
const {Transform} = require('node:stream') | ||
|
||
const pkg = require('../package.json') | ||
const requestStream = require('./requestStream') | ||
|
||
module.exports = async (options) => { | ||
let streamsInflight = 0 | ||
const stream = new Transform({ | ||
async transform(chunk, encoding, callback) { | ||
if (encoding !== 'buffer' && encoding !== 'string') { | ||
callback(null, chunk) | ||
return | ||
} | ||
|
||
let parsedChunk = null | ||
try { | ||
parsedChunk = JSON.parse(chunk.toString()) | ||
} catch (err) { | ||
// Ignore JSON parse errors | ||
// this can happen if the chunk is not a JSON object. We just pass it through and let the caller handle it. | ||
} | ||
|
||
if ( | ||
parsedChunk !== null && | ||
typeof parsedChunk === 'object' && | ||
'nextCursor' in parsedChunk && | ||
typeof parsedChunk.nextCursor === 'string' && | ||
!('_id' in parsedChunk) | ||
) { | ||
streamsInflight++ | ||
|
||
const reqStream = await startStream(options, parsedChunk.nextCursor) | ||
reqStream.on('end', () => { | ||
streamsInflight-- | ||
if (streamsInflight === 0) { | ||
stream.end() | ||
} | ||
}) | ||
reqStream.pipe(this, {end: false}) | ||
|
||
callback() | ||
return | ||
} | ||
|
||
callback(null, chunk) | ||
}, | ||
}) | ||
|
||
streamsInflight++ | ||
const reqStream = await startStream(options, '') | ||
reqStream.on('end', () => { | ||
streamsInflight-- | ||
if (streamsInflight === 0) { | ||
stream.end() | ||
} | ||
}) | ||
|
||
reqStream.pipe(stream, {end: false}) | ||
return stream | ||
} | ||
|
||
function startStream(options, nextCursor) { | ||
const url = options.client.getUrl( | ||
`/data/export/${options.dataset}?nextCursor=${encodeURIComponent(nextCursor)}`, | ||
) | ||
const token = options.client.config().token | ||
const headers = { | ||
'User-Agent': `${pkg.name}@${pkg.version}`, | ||
...(token ? {Authorization: `Bearer ${token}`} : {}), | ||
} | ||
|
||
return requestStream({url, headers, maxRetries: options.maxRetries}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters