pg-query-stream
Advanced tools
Comparing version 0.7.0 to 1.0.0
@@ -35,6 +35,7 @@ var util = require('util') | ||
QueryStream.prototype.close = function() { | ||
QueryStream.prototype.close = function(cb) { | ||
this._closing = true | ||
var self = this | ||
Cursor.prototype.close.call(this, function(err) { | ||
if (cb) { cb(err); } | ||
if(err) return self.emit('error', err) | ||
@@ -55,2 +56,5 @@ process.nextTick(function() { | ||
} | ||
if (self._closing) { return; } | ||
if(!rows.length) { | ||
@@ -57,0 +61,0 @@ process.nextTick(function() { |
{ | ||
"name": "pg-query-stream", | ||
"version": "0.7.0", | ||
"version": "1.0.0", | ||
"description": "Postgres query result returned as readable stream", | ||
@@ -35,4 +35,4 @@ "main": "index.js", | ||
"pg-cursor": "1.0.0", | ||
"readable-stream": "^1.0.27-1" | ||
"readable-stream": "^2.0.4" | ||
} | ||
} |
# pg-query-stream | ||
[![Build Status](https://travis-ci.org/brianc/node-pg-query-stream.svg)](https://travis-ci.org/brianc/node-pg-query-stream) | ||
Receive result rows from [pg](https://github.com/brianc/node-postgres) as a readable (object) stream. | ||
@@ -4,0 +6,0 @@ |
@@ -36,1 +36,66 @@ var assert = require('assert') | ||
}) | ||
helper('should not throw errors after early close', function(client) { | ||
it('can be closed early without error', function(done) { | ||
var stream = new QueryStream('SELECT * FROM generate_series(0, 2000) num'); | ||
var query = client.query(stream); | ||
var fetchCount = 0; | ||
var errorCount = 0; | ||
function waitForErrors() { | ||
setTimeout(function () { | ||
assert(errorCount === 0, 'should not throw a ton of errors'); | ||
done(); | ||
}, 10); | ||
} | ||
// hack internal _fetch function to force query.close immediately after _fetch is called (simulating the race condition) | ||
// race condition: if close is called immediately after _fetch is called, but before results are returned, errors are thrown | ||
// when the fetch results are pushed to the readable stream after its already closed. | ||
query._fetch = (function (_fetch) { | ||
return function () { | ||
// wait for the second fetch. closing immediately after the first fetch throws an entirely different error :( | ||
if (fetchCount++ === 0) { | ||
return _fetch.apply(this, arguments); | ||
} | ||
var results = _fetch.apply(this, arguments); | ||
query.close(); | ||
waitForErrors(); | ||
query._fetch = _fetch; // we're done with our hack, so restore the original _fetch function. | ||
return results; | ||
} | ||
}(query._fetch)); | ||
query.on('error', function () { errorCount++; }); | ||
query.on('readable', function () { | ||
query.read(); | ||
}); | ||
}); | ||
}); | ||
helper('close callback', function (client) { | ||
it('notifies an optional callback when the conneciton is closed', function (done) { | ||
var stream = new QueryStream('SELECT * FROM generate_series(0, $1) num', [10], {batchSize: 2, highWaterMark: 2}); | ||
var query = client.query(stream); | ||
query.once('readable', function() { // only reading once | ||
query.read(); | ||
}); | ||
query.once('readable', function() { | ||
query.close(function () { | ||
// nothing to assert. This test will time out if the callback does not work. | ||
done(); | ||
}); | ||
}); | ||
query.on('close', function () { | ||
assert(false, "close event should not fire"); // no close event because we did not read to the end of the stream. | ||
}); | ||
}); | ||
}); |
@@ -13,3 +13,9 @@ var assert = require('assert') | ||
var res = stream.read() | ||
assert(res, 'should not return null on evented reader') | ||
if (result.length !== 201) { | ||
assert(res, 'should not return null on evented reader') | ||
} else { | ||
//a readable stream will emit a null datum when it finishes being readable | ||
//https://nodejs.org/api/stream.html#stream_event_readable | ||
assert.equal(res, null) | ||
} | ||
if(res) { | ||
@@ -16,0 +22,0 @@ result.push(res.num) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
15508
365
1
76
+ Addedisarray@1.0.0(transitive)
+ Addedprocess-nextick-args@2.0.1(transitive)
+ Addedreadable-stream@2.3.8(transitive)
+ Addedsafe-buffer@5.1.2(transitive)
+ Addedstring_decoder@1.1.1(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
- Removedisarray@0.0.1(transitive)
- Removedreadable-stream@1.1.14(transitive)
- Removedstring_decoder@0.10.31(transitive)
Updatedreadable-stream@^2.0.4