Skip to content
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

Support for soap attachments in response. #1148

Merged
merged 14 commits into from
Jul 13, 2021
Merged
Show file tree
Hide file tree
Changes from 11 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
3 changes: 2 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,9 @@ The `options` argument allows you to customize the client with the following pro
- namespaceArrayElements: provides support for nonstandard array semantics. If true, JSON arrays of the form `{list: [{elem: 1}, {elem: 2}]}` are marshalled into xml as `<list><elem>1</elem></list> <list><elem>2</elem></list>`. If false, marshalls into `<list> <elem>1</elem> <elem>2</elem> </list>`. Default: `true`.
- stream: allows using a stream to parse the XML SOAP response. Default: `false`
- returnSaxStream: enables the library to return the sax stream, transferring to the end user the responsibility of parsing the XML. It can be used only in combination with *stream* argument set to `true`. Default: `false`
- parseReponseAttachments: Treat response as multipart/related response with MTOM attachment. Reach attachments on the `lastResponseAttachments` property of SoapClient. Default: `false`

Note: for versions of node >0.10.X, you may need to specify `{connection: 'keep-alive'}` in SOAP headers to avoid truncation of longer chunked responses.
Note: for versions of node >0.10.X, you may need to specify `{connection: 'keep-alive'}` in SOAP headers to avoid truncation of longer chunked responses.

### soap.listen(*server*, *path*, *services*, *wsdl*, *callback*) - create a new SOAP server that listens on *path* and provides *services*.
*server* can be a [http](https://nodejs.org/api/http.html) Server or [express](http://expressjs.com/) framework based server
Expand Down
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

155 changes: 80 additions & 75 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,77 +1,82 @@
{
"name": "soap",
"version": "0.40.0",
"description": "A minimal node SOAP client",
"engines": {
"node": ">=10.0.0"
},
"author": "Vinay Pulim <[email protected]>",
"dependencies": {
"axios": "^0.21.1",
"axios-ntlm": "^1.1.6",
"debug": "^4.3.1",
"get-stream": "^6.0.1",
"lodash": "^4.17.21",
"sax": ">=0.6",
"strip-bom": "^3.0.0",
"uuid": "^8.3.2",
"xml-crypto": "^2.1.0"
},
"repository": {
"type": "git",
"url": "https://github.com/vpulim/node-soap.git"
},
"main": "./index.js",
"types": "./lib/soap.d.ts",
"directories": {
"lib": "./lib",
"test": "./test"
},
"scripts": {
"prepare": "npm run build",
"build": "tsc -p .",
"clean": "rm -rf lib",
"watch": "tsc -w -p .",
"lint": "tslint -p tsconfig.json",
"toc": "./node_modules/.bin/doctoc Readme.md --github --maxlevel 3",
"cover": "nyc --extension=.ts --reporter=lcov --reporter=html --reporter=text mocha --timeout 15000 --exit test/*-test.js test/security/*.js",
"coveralls": "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js -v",
"docs": "typedoc --out docs",
"test": "mocha --timeout 15000 --bail --exit test/*-test.js test/security/*.js"
},
"keywords": [
"soap"
],
"license": "MIT",
"devDependencies": {
"@types/debug": "^4.1.2",
"@types/express": "^4.16.1",
"@types/lodash": "^4.14.169",
"@types/node": "^11.15.54",
"@types/request": "^2.48.1",
"@types/sax": "^1.0.1",
"@types/uuid": "^8.3.0",
"body-parser": "^1.15.2",
"colors": "^1.3.3",
"coveralls": "^3.0.5",
"diff": "^4.0.1",
"doctoc": "^1.4.0",
"duplexer": "~0.1.1",
"express": "^4.16.4",
"finalhandler": "^1.1.1",
"glob": "^7.1.7",
"jshint": "^2.10.1",
"mocha": "^6.1.4",
"nyc": "^14.1.1",
"readable-stream": "~2.0.2",
"semver": "^5.6.0",
"serve-static": "^1.14.1",
"should": "^13.2.3",
"sinon": "^1.17.7",
"source-map-support": "^0.5.10",
"timekeeper": "^2.1.2",
"tslint": "^5.18.0",
"typedoc": "^0.20.36",
"typescript": "^3.3.3333"
"name": "soap",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please revert the white space changes in this file.

Copy link
Contributor Author

@davefej davefej Jul 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi!
You compared with a previous commit, it is already fixed in the latest commit in master.
https://github.com/vpulim/node-soap/pull/1148/files

"version": "0.40.0",
"description": "A minimal node SOAP client",
"engines": {
"node": ">=10.0.0"
},
"author": "Vinay Pulim <[email protected]>",
"dependencies": {
"content-type-parser": "^1.0.2",
"formidable": "^1.2.2",
"httpntlm": "^1.5.2",
"request": ">=2.9.0",
"axios": "^0.21.1",
"axios-ntlm": "^1.1.6",
"debug": "^4.3.1",
"get-stream": "^6.0.1",
"lodash": "^4.17.21",
"sax": ">=0.6",
"strip-bom": "^3.0.0",
"uuid": "^8.3.2",
"xml-crypto": "^2.1.0"
},
"repository": {
"type": "git",
"url": "https://github.com/vpulim/node-soap.git"
},
"main": "./index.js",
"types": "./lib/soap.d.ts",
"directories": {
"lib": "./lib",
"test": "./test"
},
"scripts": {
"prepare": "npm run build",
"build": "tsc -p .",
"clean": "rm -rf lib",
"watch": "tsc -w -p .",
"lint": "tslint -p tsconfig.json",
"toc": "./node_modules/.bin/doctoc Readme.md --github --maxlevel 3",
"cover": "nyc --extension=.ts --reporter=lcov --reporter=html --reporter=text mocha --timeout 15000 --exit test/*-test.js test/security/*.js",
"coveralls": "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js -v",
"docs": "typedoc --out docs",
"test": "mocha --timeout 15000 --bail --exit test/*-test.js test/security/*.js"
},
"keywords": [
"soap"
],
"license": "MIT",
"devDependencies": {
"@types/debug": "^4.1.2",
"@types/express": "^4.16.1",
"@types/lodash": "^4.14.169",
"@types/node": "^11.15.54",
"@types/request": "^2.48.1",
"@types/sax": "^1.0.1",
"@types/uuid": "^8.3.0",
"body-parser": "^1.15.2",
"colors": "^1.3.3",
"coveralls": "^3.0.5",
"diff": "^4.0.1",
"doctoc": "^1.4.0",
"duplexer": "~0.1.1",
"express": "^4.16.4",
"finalhandler": "^1.1.1",
"glob": "^7.1.7",
"jshint": "^2.10.1",
"mocha": "^6.1.4",
"nyc": "^14.1.1",
"readable-stream": "~2.0.2",
"semver": "^5.6.0",
"serve-static": "^1.14.1",
"should": "^13.2.3",
"sinon": "^1.17.7",
"source-map-support": "^0.5.10",
"timekeeper": "^2.1.2",
"tslint": "^5.18.0",
"typedoc": "^0.20.36",
"typescript": "^3.3.3333"
}
}
}
17 changes: 10 additions & 7 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { IncomingHttpHeaders } from 'http';
import * as _ from 'lodash';
import { v4 as uuidv4 } from 'uuid';
import { HttpClient } from './http';
import { IHeaders, IHttpClient, IOptions, ISecurity, SoapMethod, SoapMethodAsync } from './types';
import { IHeaders, IHttpClient, IMTOMAttachments, IOptions, ISecurity, SoapMethod, SoapMethodAsync } from './types';
import { findPrefix } from './utils';
import { WSDL } from './wsdl';
import { IPort, OperationElement, ServiceElement } from './wsdl/elements';
Expand Down Expand Up @@ -53,6 +53,7 @@ export class Client extends EventEmitter {
public lastResponse?: any;
public lastResponseHeaders?: IncomingHttpHeaders;
public lastElapsedTime?: number;
public lastResponseAttachments: IMTOMAttachments;

private wsdl: WSDL;
private httpClient: IHttpClient;
Expand Down Expand Up @@ -230,11 +231,12 @@ export class Client extends EventEmitter {
rawResponse: any,
soapHeader: any,
rawRequest: any,
mtomAttachments: any,
) => {
if (err) {
reject(err);
} else {
resolve([result, rawResponse, soapHeader, rawRequest]);
resolve([result, rawResponse, soapHeader, rawRequest, mtomAttachments]);
}
};
method(
Expand Down Expand Up @@ -263,8 +265,8 @@ export class Client extends EventEmitter {
extraHeaders = options;
options = temp;
}
this._invoke(method, args, location, (error, result, rawResponse, soapHeader, rawRequest) => {
callback(error, result, rawResponse, soapHeader, rawRequest);
this._invoke(method, args, location, (error, result, rawResponse, soapHeader, rawRequest, mtomAttachments) => {
callback(error, result, rawResponse, soapHeader, rawRequest, mtomAttachments);
}, options, extraHeaders);
};
}
Expand Down Expand Up @@ -314,7 +316,7 @@ export class Client extends EventEmitter {

if (!output) {
// one-way, no output expected
return callback(null, null, body, obj.Header, xml);
return callback(null, null, body, obj.Header, xml, response.mtomResponseAttachments);
}

// If it's not HTML and Soap Body is empty
Expand Down Expand Up @@ -351,7 +353,7 @@ export class Client extends EventEmitter {
});
}

callback(null, result, body, obj.Header, xml);
callback(null, result, body, obj.Header, xml, response.mtomResponseAttachments);
};

const parseSync = (body, response) => {
Expand All @@ -372,7 +374,7 @@ export class Client extends EventEmitter {
error.response = response;
error.body = body;
this.emit('soapError', error, eid);
return callback(error, response, body, undefined, xml);
return callback(error, response, body, undefined, xml, response.mtomResponseAttachments);
}
return finish(obj, body, response);
};
Expand Down Expand Up @@ -552,6 +554,7 @@ export class Client extends EventEmitter {
if (response) {
this.lastResponseHeaders = response.headers;
this.lastElapsedTime = response.headers.date;
this.lastResponseAttachments = response.mtomResponseAttachments;
// Added mostly for testability, but possibly useful for debugging
this.lastRequestHeaders = response.config && response.config.headers;
}
Expand Down
43 changes: 40 additions & 3 deletions src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@

import * as req from 'axios';
import { NtlmClient } from 'axios-ntlm';
import * as contentTypeParser from 'content-type-parser';
import * as debugBuilder from 'debug';
import { ReadStream } from 'fs';
import * as url from 'url';

import { v4 as uuidv4 } from 'uuid';
import { IExOptions, IHeaders, IHttpClient, IOptions } from './types';
import { IExOptions, IHeaders, IHttpClient, IMTOMAttachments, IOptions } from './types';
import { parseMTOMResp } from './utils';

const debug = debugBuilder('node-soap');
const VERSION = require('../package.json').version;
Expand All @@ -29,10 +32,13 @@ export interface IAttachment {
* @constructor
*/
export class HttpClient implements IHttpClient {

private _request: req.AxiosInstance;
private options: IOptions;

constructor(options?: IOptions) {
options = options || {};
this.options = options;
this._request = options.request || req.default.create();
}

Expand Down Expand Up @@ -166,11 +172,42 @@ export class HttpClient implements IHttpClient {
});
req = ntlmReq(options);
} else {
if (this.options.parseReponseAttachments) {
options.responseType = 'arraybuffer';
options.responseEncoding = 'binary';
//options.encoding = null;
}
req = this._request(options);
}

const _this = this;
req.then((res) => {
res.data = this.handleResponse(req, res, res.data);
let body;
if (_this.options.parseReponseAttachments) {
const isMultipartResp = res.headers['content-type'] && res.headers['content-type'].toLowerCase().indexOf('multipart/related') > -1;
if (isMultipartResp) {
let boundary;
const parsedContentType = contentTypeParser(res.headers['content-type']);
if (parsedContentType && parsedContentType.parameterList) {
boundary = ((parsedContentType.parameterList as any[]).find((item) => item.key === 'boundary') || {}).value;
}
if (!boundary) {
return callback(new Error('Missing boundary from content-type'));
}
const multipartResponse = parseMTOMResp(res.data, boundary);

// first part is the soap response
const firstPart = multipartResponse.parts.shift();
if (!firstPart || !firstPart.body) {
return callback(new Error('Cannot parse multipart response'));
}
body = firstPart.body.toString('utf8');
(res as any).mtomResponseAttachments = multipartResponse;
} else {
body = res.data.toString('utf8');
}
}

res.data = this.handleResponse(req, res, body || res.data);
callback(null, res, res.data);
return res;
}, (err) => {
Expand Down
14 changes: 12 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ export interface IHttpClient {
export type ISoapMethod = SoapMethod;
export type SoapMethod = (
args: any,
callback: (err: any, result: any, rawResponse: any, soapHeader: any, rawRequest: any) => void,
callback: (err: any, result: any, rawResponse: any, soapHeader: any, rawRequest: any, mtomAttachments?: IMTOMAttachments) => void,
options?: any,
extraHeaders?: any,
mtomAttachments?: IMTOMAttachments,
) => void;

export type SoapMethodAsync = (
args: any,
options?: any,
extraHeaders?: any,
) => Promise<[any, any, any, any]>;
) => Promise<[any, any, any, any, IMTOMAttachments?]>;

export type ISoapServiceMethod = (args: any, callback?: (data: any) => void, headers?: any, req?: any, res?: any, sender?: any) => any;

Expand Down Expand Up @@ -133,6 +134,8 @@ export interface IOptions extends IWsdlBaseOptions {
overridePromiseSuffix?: string;
/** @internal */
WSDL_CACHE?;
/** handle MTOM soapAttachments in response */
parseReponseAttachments?: boolean;
}

export interface IOneWayOptions {
Expand All @@ -152,3 +155,10 @@ export interface IServerOptions extends IWsdlBaseOptions {
/** A boolean for controlling chunked transfer encoding in response. Some client (such as Windows 10's MDM enrollment SOAP client) is sensitive to transfer-encoding mode and can't accept chunked response. This option let user disable chunked transfer encoding for such a client. Default to true for backward compatibility. */
enableChunkedEncoding?: boolean;
}

export interface IMTOMAttachments {
parts: Array<{
body: Buffer,
headers: { [key: string]: string },
}>;
}
Loading