Skip to content
Open
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
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
74 changes: 36 additions & 38 deletions foam.js
Original file line number Diff line number Diff line change
@@ -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 = '<?xml version="1.0" encoding="UTF-8"?>';
let xml = '<?xml version="1.0" encoding="UTF-8"?>';
xml += '<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
'xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" ' + namespaces(options.namespaces) + '>';

Expand Down Expand Up @@ -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();
Expand All @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "foam",
"version": "0.3.0",
"version": "1.0.0",
"description": "A simple soap client",
"main": "foam.js",
"scripts": {
Expand Down