ts-postgres
Advanced tools
Comparing version
# Changes | ||
1.0.2 (2019-02-08) | ||
------------------ | ||
- Connection state variable 'closed' is now public. This should be | ||
checked before using the connection to make sure that an unexpected | ||
error has not occurred which would close the connection. | ||
- Handle protocol errors gracefully, passing error to open data | ||
handlers and marking the connection as 'closed'. | ||
1.0.1 (2019-01-13) | ||
@@ -4,0 +14,0 @@ ------------------ |
@@ -56,3 +56,2 @@ import { postgresqlErrorCodes } from './errors'; | ||
private ending; | ||
private closed; | ||
private connected; | ||
@@ -79,2 +78,3 @@ private connecting; | ||
private activeDataHandlerInfo; | ||
closed: boolean; | ||
processId: number | null; | ||
@@ -81,0 +81,0 @@ secretKey: number | null; |
@@ -33,3 +33,2 @@ "use strict"; | ||
this.ending = false; | ||
this.closed = false; | ||
this.connected = false; | ||
@@ -55,2 +54,3 @@ this.connecting = false; | ||
this.activeDataHandlerInfo = null; | ||
this.closed = false; | ||
this.processId = null; | ||
@@ -100,5 +100,31 @@ this.secretKey = null; | ||
} | ||
const read = this.receive(this.buffer, this.offset, size); | ||
this.offset += read; | ||
this.remaining = size - read; | ||
try { | ||
const read = this.receive(this.buffer, this.offset, size); | ||
this.offset += read; | ||
this.remaining = size - read; | ||
} | ||
catch (error) { | ||
const active = this.activeDataHandlerInfo; | ||
if (active) { | ||
active.handler(error); | ||
} | ||
while (!this.bindQueue.isEmpty()) { | ||
const info = this.bindQueue.shift(); | ||
if (info) { | ||
info.handler(error); | ||
} | ||
} | ||
while (!this.preFlightQueue.isEmpty()) { | ||
const handler = this.preFlightQueue.shift().dataHandler; | ||
if (handler) { | ||
handler(error); | ||
} | ||
} | ||
// Mark connection as not connected. | ||
this.connected = false; | ||
this.ready = false; | ||
this.closed = true; | ||
this.error = true; | ||
this.stream.destroy(error); | ||
} | ||
}); | ||
@@ -122,2 +148,5 @@ this.stream.on('error', (error) => { | ||
} | ||
if (this.error) { | ||
throw new Error('Can\'t connect in error state'); | ||
} | ||
this.connecting = true; | ||
@@ -255,2 +284,5 @@ let p = this.events.connect.once(); | ||
execute(query) { | ||
if (this.closed && !this.connecting) { | ||
throw new Error('Connection is closed.'); | ||
} | ||
const text = query.text; | ||
@@ -257,0 +289,0 @@ const values = query.values || []; |
{ | ||
"name": "ts-postgres", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "PostgreSQL client in TypeScript", | ||
@@ -5,0 +5,0 @@ "declaration": true, |
@@ -141,4 +141,7 @@ # ts-postgres | ||
return client.end().then(() => { }) | ||
}, | ||
validate: (client: Client) => { | ||
return Promise.resolve(!client.closed); | ||
} | ||
}); | ||
}, { testOnBorrow: true }); | ||
@@ -145,0 +148,0 @@ pool.use(...) |
@@ -163,3 +163,2 @@ import { ECONNRESET } from 'constants'; | ||
private ending = false; | ||
private closed = false; | ||
private connected = false; | ||
@@ -191,2 +190,3 @@ private connecting = false; | ||
public closed = false; | ||
public processId: number | null = null; | ||
@@ -250,5 +250,31 @@ public secretKey: number | null = null; | ||
const read = this.receive(this.buffer, this.offset, size); | ||
this.offset += read; | ||
this.remaining = size - read; | ||
try { | ||
const read = this.receive(this.buffer, this.offset, size); | ||
this.offset += read; | ||
this.remaining = size - read; | ||
} catch (error) { | ||
const active = this.activeDataHandlerInfo; | ||
if (active) { | ||
active.handler(error); | ||
} | ||
while (!this.bindQueue.isEmpty()) { | ||
const info = this.bindQueue.shift(); | ||
if (info) { | ||
info.handler(error); | ||
} | ||
} | ||
while (!this.preFlightQueue.isEmpty()) { | ||
const handler = this.preFlightQueue.shift().dataHandler; | ||
if (handler) { | ||
handler(error); | ||
} | ||
} | ||
// Mark connection as not connected. | ||
this.connected = false; | ||
this.ready = false; | ||
this.closed = true; | ||
this.error = true; | ||
this.stream.destroy(error); | ||
} | ||
}); | ||
@@ -276,2 +302,6 @@ | ||
if (this.error) { | ||
throw new Error('Can\'t connect in error state'); | ||
} | ||
this.connecting = true; | ||
@@ -482,2 +512,6 @@ | ||
private execute(query: Query): ResultIterator { | ||
if (this.closed && !this.connecting) { | ||
throw new Error('Connection is closed.'); | ||
} | ||
const text = query.text; | ||
@@ -484,0 +518,0 @@ const values = query.values || []; |
Sorry, the diff of this file is not supported yet
299344
1.26%5363
1.19%183
1.67%