Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: destroy ResourceStream with pre-flight error #236

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
65 changes: 36 additions & 29 deletions src/resource-stream.ts
Expand Up @@ -59,43 +59,50 @@ export class ResourceStream<T> extends Transform implements ResourceEvents<T> {

this._reading = true;

this._requestFn(
this._nextQuery,
(err: Error | null, results: T[], nextQuery: {} | null) => {
if (err) {
this.destroy(err);
return;
}
// Wrap in a try/catch to catch input linting errors, e.g.
// an invalid BigQuery query. These errors are thrown in an
// async fashion, which makes them un-catchable by the user.
try {
this._requestFn(
this._nextQuery,
(err: Error | null, results: T[], nextQuery: {} | null) => {
if (err) {
this.destroy(err);
return;
}

this._nextQuery = nextQuery;
this._nextQuery = nextQuery;

if (this._resultsToSend !== Infinity) {
results = results.splice(0, this._resultsToSend);
this._resultsToSend -= results.length;
}
if (this._resultsToSend !== Infinity) {
results = results.splice(0, this._resultsToSend);
this._resultsToSend -= results.length;
}

let more = true;
let more = true;

for (const result of results) {
if (this._ended) {
break;
for (const result of results) {
if (this._ended) {
break;
}
more = this.push(result);
}
more = this.push(result);
}

const isFinished = !this._nextQuery || this._resultsToSend < 1;
const madeMaxCalls = ++this._requestsMade >= this._maxApiCalls;
const isFinished = !this._nextQuery || this._resultsToSend < 1;
const madeMaxCalls = ++this._requestsMade >= this._maxApiCalls;

if (isFinished || madeMaxCalls) {
this.end();
}
if (isFinished || madeMaxCalls) {
this.end();
}

if (more && !this._ended) {
setImmediate(() => this._read());
}
if (more && !this._ended) {
setImmediate(() => this._read());
}

this._reading = false;
}
);
this._reading = false;
}
);
} catch (e) {
this.destroy(e);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know streams very well. Should we be emitting an error event or something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will do the same, it is basically:

this.emit('error', error);
this.end();

}
}
}
12 changes: 12 additions & 0 deletions test/resource-stream.ts
Expand Up @@ -280,5 +280,17 @@ describe('ResourceStream', () => {

assert.strictEqual(stream._reading, false);
});

it('should destroy the stream if the request method throws', done => {
const error = new Error('Error.');
stream._requestFn = () => {
throw error;
};
stream.on('error', err => {
assert.strictEqual(err, error);
done();
});
stream._read();
});
});
});