Skip to content

Commit

Permalink
fix: fixes stringifying of large asset maps by streaming the JSON to …
Browse files Browse the repository at this point in the history
…file.
  • Loading branch information
atombender committed Dec 19, 2024
1 parent 2dff75b commit f8e24c9
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"rimraf": "^6.0.1",
"split2": "^4.2.0",
"tar": "^7.0.1",
"yaml": "^2.4.2"
"yaml": "^2.4.2",
"json-stream-stringify": "^2.0.2"
},
"devDependencies": {
"@jest/globals": "^29.7.0",
Expand Down
7 changes: 6 additions & 1 deletion src/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ const zlib = require('zlib')
const archiver = require('archiver')
const miss = require('mississippi')
const split = require('split2')
const JsonStreamStringify = require('json-stream-stringify')
const AssetHandler = require('./AssetHandler')
const debug = require('./debug')
const pipeAsync = require('./util/pipeAsync')
const filterDocumentTypes = require('./filterDocumentTypes')
const filterDrafts = require('./filterDrafts')
const filterSystemDocuments = require('./filterSystemDocuments')
Expand Down Expand Up @@ -43,6 +45,7 @@ async function exportDataset(opts) {
const tmpDir = path.join(os.tmpdir(), prefix)
fs.mkdirSync(tmpDir, {recursive: true})
const dataPath = path.join(tmpDir, 'data.ndjson')
const assetsPath = path.join(tmpDir, 'assets.json')

const cleanup = () =>
rimraf(tmpDir).catch((err) => {
Expand Down Expand Up @@ -209,7 +212,9 @@ async function exportDataset(opts) {
update: true,
})

archive.append(JSON.stringify(assetMap), {name: 'assets.json', prefix})
const assetsStream = fs.createWriteStream(assetsPath)
await pipeAsync(new JsonStreamStringify(assetMap), assetsStream)
archive.file(assetsPath, {name: 'assets.json', prefix})
clearInterval(progressInterval)
} catch (assetErr) {
clearInterval(progressInterval)
Expand Down
17 changes: 17 additions & 0 deletions src/util/pipeAsync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const miss = require('mississippi')

module.exports = async (readable, writable) => {
return new Promise((resolve, reject) => {
try {
miss.pipe(readable, writable, (jsonErr) => {
if (jsonErr) {
reject(jsonErr)
} else {
resolve()
}
})
} catch (assetErr) {
reject(assetErr)
}
})
}

0 comments on commit f8e24c9

Please sign in to comment.