Skip to content

Refactor Connection.js, transports, and Toolkit.js #42

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

Merged
merged 6 commits into from
Mar 11, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
node_modules/
package-lock.json
.vscode
node_modules/
package-lock.json
.vscode
200 changes: 91 additions & 109 deletions lib/Connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,81 +16,68 @@
// 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 iXml = require('./ixml');
const { iRestHttp } = require('./irest');
const { db2Call } = require('./istoredp');
const {
iXmlNodeHead, iXmlNodeScriptOpen, iXmlNodeScriptClose,
} = require('./ixml');

const I_TRANSPORT_REST = 'REST';
const I_TRANSPORT_DB2 = 'DB2';
const { iRestHttp } = require('./transports/irest');
const { db2Call } = require('./transports/istoredp');

const availableTransports = {
idb: db2Call,
rest: iRestHttp,
};

class Connection {
/**
* @description Creates a new Connection object
* @constructor
* @param {string} db
* @param {object | string} optionsOrDb
* @param {string} user
* @param {string} pwd
* @param {object} [option]
* @param {object} [restConfig]
*/
constructor(db, user, pwd, option) {
this.conn = {};
this.cmds = [];
this.timeout = 5000; // Default timeout is 5 seconds for sync mode.
this.I_DEBUG_VERBOSE = false;
this.conn.I_TRANSPORT_DB2_DATABASE = db; // Required Field
this.conn.I_TRANSPORT_DB2_USER = user; // Required Field
this.conn.I_TRANSPORT_DB2_PASSWORD = pwd; // Required Field
this.conn.I_XML_SERVICE_LIB = 'QXMLSERV'; // Default XML Service library
this.conn.I_TRANSPORT_CTL = '*here';
this.conn.I_TRANSPORT_IPC = '*NA';

if (typeof db === 'object') {
this.conn.I_TRANSPORT = I_TRANSPORT_DB2;
return;
}

if (option && typeof option === 'object') {
if (option.host) {
this.conn.I_TRANSPORT = I_TRANSPORT_REST;
this.conn.I_TRANSPORT_REST_HOST = option.host;
if (option.port) {
this.conn.I_TRANSPORT_REST_PORT = option.port;
} else {
this.conn.I_TRANSPORT_REST_PORT = 80;
}
if (option.path) {
this.conn.I_TRANSPORT_REST_PATH = option.path;
} else {
this.conn.I_TRANSPORT_REST_PATH = '/';
}
} else {
this.conn.I_TRANSPORT = I_TRANSPORT_DB2;
if (option.xslib && option.xslib.length > 0) {
this.conn.I_XML_SERVICE_LIB = option.xslib;
constructor(optionsOrDb, username, password, restOptions) {
this.commandList = [];
this.returnError = true;
this.verbose = false;
let options = optionsOrDb;

// maintain compatibility with versions < 1.0
if (typeof options !== 'object') {
options = {
returnError: false,
transport: 'idb',
transportOptions: {
database: optionsOrDb,
username,
password,
},
};
if (restOptions && typeof restOptions === 'object') {
if (restOptions.host) {
// pre v1.0 falls back to idb transport when host is not specified
options.transport = 'rest';
}
options.transportOptions = { ...options.transportOptions, ...restOptions };
}
}

if (option.ctl && option.ctl.length > 0) {
this.conn.I_TRANSPORT_CTL = option.ctl;
}
if (option.ipc && option.ipc.length > 0) {
this.conn.I_TRANSPORT_IPC = option.ipc;
}
if (Number.isNaN(option.buf)) {
this.conn.I_TRANSPORT_REST_XML_OUT_SIZE = option.buf.toString();
} else {
this.conn.I_TRANSPORT_REST_XML_OUT_SIZE = '500000';
}
} else { this.conn.I_TRANSPORT = I_TRANSPORT_DB2; }
}
this.transport = options.transport;

/**
* @description override the default timeout value for sync mode.
* @param {number} seconds
*/
setTimeout(seconds) {
if (typeof seconds === 'number') {
this.timeout = seconds * 1000;
if (!(this.transport in availableTransports)) {
throw new Error(`${this.transport} is not a valid transport`);
}

this.transportCall = availableTransports[this.transport];
this.transportOptions = options.transportOptions || {};

if (typeof options.returnError === 'boolean') {
this.returnError = options.returnError;
}

if (typeof options.verbose === 'boolean') {
this.verbose = options.verbose;
}
}

Expand All @@ -103,18 +90,18 @@ class Connection {
*/
debug(flag) {
if (typeof flag === 'boolean') {
this.I_DEBUG_VERBOSE = flag;
this.verbose = flag;
}
return this.I_DEBUG_VERBOSE;
return this.verbose;
}


/**
* @description returns conn property from Connection object.
* @returns {object} the conn property from Connection object.
* @description returns transportOptions property from Connection object.
* @returns {object} the transportOptions property from Connection object.
*/
getConnection() {
return this.conn;
getTransportOptions() {
return this.transportOptions;
}

/**
Expand All @@ -123,60 +110,55 @@ class Connection {
*/
add(xml) {
if (typeof xml === 'object') {
this.cmds.push(xml.toXML());
this.commandList.push(xml.toXML());
} else if (xml && typeof xml === 'string') {
this.cmds.push(xml);
this.commandList.push(xml);
}
}

/**
* @description
* Invokes transport with XML input from command list
* Calls user provided callback with the XML output
* @param {fuction} callback
* @param {boolean} sync
* Calls user provided callback with the [error] and XML output
* @param {function} callback
*/
run(callback) {
// Build the XML document.
let xml = iXml.iXmlNodeHead() + iXml.iXmlNodeScriptOpen();
for (let i = 0; i < this.cmds.length; i += 1) {
xml += this.cmds[i];
if (typeof callback !== 'function') {
throw new Error('Passing a callback function is required');
}
if (!this.commandList.length) {
throw new Error('Command list is empty. Make sure you add commands first');
}
xml += iXml.iXmlNodeScriptClose();
this.cmds = [];

// Build the XML document.
const xmlInput = iXmlNodeHead() + iXmlNodeScriptOpen() + this.commandList.join()
+ iXmlNodeScriptClose();
// reset the command list
this.commandList = [];
this.transportOptions.verbose = this.verbose;

// Print the XML document if in debug mode.
if (this.I_DEBUG_VERBOSE) {
if (this.verbose) {
console.log('============\nINPUT XML\n============');
console.log(xml);
console.log('============\nOUTPUT XML\n============');
}
// Post the XML document to XML service program.
if (this.conn.I_TRANSPORT === I_TRANSPORT_REST) {
iRestHttp(callback,
this.conn.I_TRANSPORT_REST_HOST,
this.conn.I_TRANSPORT_REST_PORT,
this.conn.I_TRANSPORT_REST_PATH,
this.conn.I_TRANSPORT_DB2_DATABASE,
this.conn.I_TRANSPORT_DB2_USER,
this.conn.I_TRANSPORT_DB2_PASSWORD,
this.conn.I_TRANSPORT_IPC,
this.conn.I_TRANSPORT_CTL,
xml,
this.conn.I_TRANSPORT_REST_XML_OUT_SIZE);
} else if (this.conn.I_TRANSPORT === I_TRANSPORT_DB2) {
db2Call(callback,
this.conn.I_XML_SERVICE_LIB,
this.conn.I_TRANSPORT_DB2_DATABASE,
this.conn.I_TRANSPORT_DB2_USER,
this.conn.I_TRANSPORT_DB2_PASSWORD,
this.conn.I_TRANSPORT_IPC,
this.conn.I_TRANSPORT_CTL,
xml,
this.conn.I_TRANSPORT_REST_XML_OUT_SIZE,
this.I_DEBUG_VERBOSE);
} else {
callback(iXml.iXmlNodeHead() + iXml.iXmlNodeScriptOpen() + iXml.iXmlNodeError('***Transport error no data***') + iXml.iXmlNodeScriptClose());
console.log(xmlInput);
}

this.transportCall(this.transportOptions, xmlInput, (error, xmlOutput) => {
if (this.verbose) {
console.log('============\nOUTPUT XML\n============');
console.log(xmlOutput);
}
// create an Error object if error returned from transport is not one already
let transportError = error;
if (error && !(error instanceof Error)) {
transportError = new Error(error);
}
if (this.returnError) {
callback(transportError, xmlOutput);
} else {
callback(xmlOutput);
}
});
}
}

Expand Down
Loading