Skip to content

Commit

Permalink
fix(idb): Improve cleanup handling (#233)
Browse files Browse the repository at this point in the history
By wrapping the user-specified callback in our own cleanup callback, we
can ensure that no matter how we called the callback, cleanup is always
done.

Also, ensure we return after the callback. Without this, we would
continue onward when we did not mean to.
  • Loading branch information
kadler committed Apr 6, 2020
1 parent c2197ab commit c2bb115
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions lib/transports/idbTransport.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

const idbCall = (config, xmlInput, done) => {
const idbCall = (config, xmlInput, cb) => {
const {
dbconn, dbstmt, IN, CLOB, CHAR, SQL_ATTR_DBC_SYS_NAMING, SQL_FALSE,
} = require('idb-connector');
Expand Down Expand Up @@ -48,7 +48,8 @@ const idbCall = (config, xmlInput, done) => {
conn.conn(database);
}
} catch (error) {
done(error, null);
cb(error, null);
return;
}
// eslint-disable-next-line new-cap
const stmt = new dbstmt(conn);
Expand All @@ -59,51 +60,48 @@ const idbCall = (config, xmlInput, done) => {
[xmlInput, IN, CLOB],
];

function clean(connection, statement) {
statement.close();
connection.disconn();
connection.close();
// Before returning to caller, we must clean up
const done = (err, val) => {
stmt.close();
conn.disconn();
conn.close();

cb(err, val);
}

stmt.prepare(sql, (prepareError) => {
if (prepareError) {
clean(conn, stmt);
done(prepareError, null);
return;
}
stmt.bindParam(parameters, (bindError) => {
if (bindError) {
clean(conn, stmt);
done(bindError, null);
return;
}
stmt.execute((outArray, executeError) => {
if (executeError) {
clean(conn, stmt);
done(executeError, null);
return;
}
stmt.fetchAll((results, fetchError) => {
if (fetchError) {
clean(conn, stmt);
done(fetchError, null);
return;
}
if (!results.length) {
clean(conn, stmt);
done('Empty result set was returned', null);
return;
}
if (results.length === 1) {
clean(conn, stmt);
done(null, results[0].OUT151);
return;
}
results.forEach((chunk) => {
xmlOutput += chunk.OUT151;
});
done(null, xmlOutput);
clean(conn, stmt);
return;
});
});
});
Expand Down

0 comments on commit c2bb115

Please sign in to comment.