Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

pg-query-stream

Package Overview
Dependencies
Maintainers
1
Versions
60
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pg-query-stream - npm Package Compare versions

Comparing version 0.7.0 to 1.0.0

6

index.js

@@ -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() {

4

package.json
{
"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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc