Skip to content

Commit 6139af6

Browse files
committed
early return when signal is aborted
1 parent 7b53ebb commit 6139af6

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

packages/compass-import-export/src/export/export-json.spec.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -274,16 +274,12 @@ describe('exportJSON', function () {
274274
expect(result.docsWritten).to.equal(0);
275275
expect(result.aborted).to.be.true;
276276

277-
let resultText;
278277
try {
279-
resultText = await fs.promises.readFile(resultPath, 'utf8');
278+
await fs.promises.readFile(resultPath, 'utf8');
279+
expect.fail('Expected file to not exist');
280280
} catch (err) {
281-
console.log(resultPath);
282-
throw err;
281+
// noop
283282
}
284-
285-
const expectedText = '';
286-
expect(resultText).to.deep.equal(expectedText);
287283
});
288284

289285
it('exports aggregations', async function () {

packages/compass-import-export/src/export/export-json.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,15 @@ export async function exportJSON({
5252

5353
const ejsonOptions = getEJSONOptionsForVariant(variant);
5454

55-
if (!abortSignal?.aborted) {
56-
output.write('[');
55+
if (abortSignal?.aborted) {
56+
return {
57+
docsWritten,
58+
aborted: true,
59+
};
5760
}
5861

62+
output.write('[');
63+
5964
const docStream = new Transform({
6065
objectMode: true,
6166
transform: function (chunk: any, encoding, callback) {
@@ -88,21 +93,17 @@ export async function exportJSON({
8893
[inputStream, docStream, output],
8994
...(abortSignal ? [{ signal: abortSignal }] : [])
9095
);
91-
output.write(']\n', 'utf8');
9296
} catch (err: any) {
9397
if (err.code === 'ABORT_ERR') {
94-
// Finish the JSON file as the `final` of the docs stream didn't run.
95-
output.write(']\n', 'utf8');
96-
9798
return {
9899
docsWritten,
99100
aborted: true,
100101
};
101102
}
102-
output.write(']\n', 'utf8');
103-
104103
throw err;
105104
} finally {
105+
// Finish the array output
106+
output.write(']\n');
106107
void input.close();
107108
}
108109

0 commit comments

Comments
 (0)