From a9edceaebdab8819f6c4867da8936ac0a268d426 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Trob=C3=A4ck?= Date: Thu, 8 Sep 2016 16:13:58 +0200 Subject: [PATCH 1/2] Promises and ES6 --- README.md | 10 ++++---- foam.js | 74 +++++++++++++++++++++++++++---------------------------- 2 files changed, 41 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index 7df7e6e..d0d4367 100644 --- a/README.md +++ b/README.md @@ -22,11 +22,11 @@ var operation = 'CelsiusToFahrenheit' var foam = require('foam'); -foam(uri, operation, action, message, {namespace: namespace}, - function (err, result) { - console.log(result.CelsiusToFahrenheitResponse.CelsiusToFahrenheitResult); - } -); +foam(uri, operation, action, message, {namespace: namespace}).then((result) => { + console.log(result.CelsiusToFahrenheitResponse.CelsiusToFahrenheitResult); +}).catch((err) => { + console.log(err); +}); ``` ### Parameters diff --git a/foam.js b/foam.js index e31e177..2c1f482 100644 --- a/foam.js +++ b/foam.js @@ -1,47 +1,45 @@ -var hyperquest = require('hyperquest') +const hyperquest = require('hyperquest') , XML = require('simple-xml') , StringStream = require('stream-ext').StringStream , zlib = require('zlib') ; -module.exports = function soap (uri, operation, action, message, options, callback) { - if (typeof options === 'function') { - callback = options; - options = {}; - } - var xml = envelope(operation, message, options); - if (options.benchmark) console.time('soap request: ' + uri); +module.exports = function soap (uri, operation, action, message, options = {}) { + return new Promise((resolve, reject) => { + const xml = envelope(operation, message, options); + if (options.benchmark) console.time('soap request: ' + uri); - var stream = new StringStream(); - stream.on('error', callback); - stream.on('end', function (data) { - if (options.benchmark) console.timeEnd('soap request: ' + uri); - try { - var obj = XML.parse(data)['Envelope']['Body']; - callback(null, obj); - } - catch (err) { - callback(err); - } - }); + const stream = new StringStream(); + stream.on('error', reject); + stream.on('end', (data) => { + if (options.benchmark) console.timeEnd('soap request: ' + uri); + try { + const obj = XML.parse(data)['Envelope']['Body']; + resolve(obj); + } + catch (err) { + reject(err); + } + }); - var req = hyperquest.post(uri, { - headers: headers(action, xml.length), - rejectUnauthorized: options.rejectUnauthorized, - secureProtocol: options.secureProtocol - }); - req.on('error', callback); - req.on('response', function (res) { - if (isGzipped(res)) - res.pipe(gunzip(callback)).pipe(stream); - else - res.pipe(stream); + const req = hyperquest.post(uri, { + headers: headers(action, xml.length), + rejectUnauthorized: options.rejectUnauthorized, + secureProtocol: options.secureProtocol + }); + req.on('error', reject); + req.on('response', (res) => { + if (isGzipped(res)) + res.pipe(gunzip(reject)).pipe(stream); + else + res.pipe(stream); + }); + req.end(xml); }); - req.end(xml); }; function envelope (operation, message, options) { - var xml = ''; + let xml = ''; xml += ''; @@ -72,8 +70,8 @@ function headers (schema, length) { } function namespaces (ns) { - var attributes = ''; - for (var name in ns) { + const attributes = ''; + for (const name in ns) { attributes += name + '="' + ns[name] + '" '; } return attributes.trim(); @@ -83,9 +81,9 @@ function serializeOperation (operation, options) { return '<' + operation + (options.namespace ? ' xmlns="' + options.namespace + '"' : '') + '>'; } -function gunzip (callback) { - var gunzip = zlib.createGunzip(); - gunzip.on('error', callback); +function gunzip (reject) { + const gunzip = zlib.createGunzip(); + gunzip.on('error', reject); return gunzip; } From dde51e2bea693130f940367243e6f59505abaebc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Trob=C3=A4ck?= Date: Thu, 8 Sep 2016 16:16:13 +0200 Subject: [PATCH 2/2] Bumped major version in package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c2a48c4..134152f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "foam", - "version": "0.3.0", + "version": "1.0.0", "description": "A simple soap client", "main": "foam.js", "scripts": {