From 15542fdf39586b99285ae6ca319a2367195b2a6d Mon Sep 17 00:00:00 2001 From: Bruno Brant <1890900+bruno-brant@users.noreply.github.com> Date: Sun, 21 Jun 2020 16:50:30 -0300 Subject: [PATCH] feat(Server): method called with applyMethod is "applied" instead of being called directly, which enables access to "this".The owning object for the method is the port, therefore, that's what is being passed as "this". --- package-lock.json | 2 +- src/server.ts | 42 ++++++++++++++++++++++-------------------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/package-lock.json b/package-lock.json index b06f44da4..f7ec2b4e7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "soap", - "version": "0.29.0", + "version": "0.31.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/src/server.ts b/src/server.ts index 6b3e0141e..e5ec153ef 100644 --- a/src/server.ts +++ b/src/server.ts @@ -6,7 +6,7 @@ import { EventEmitter } from 'events'; import * as http from 'http'; import * as url from 'url'; -import { IOneWayOptions, ISecurity, IServerOptions, IServices, ISoapFault, ISoapServiceMethod } from './types'; +import { IOneWayOptions, IServerOptions, IServicePort, IServices, ISoapFault, ISoapServiceMethod } from './types'; import { findPrefix } from './utils'; import { WSDL } from './wsdl'; import { BindingElement, IPort } from './wsdl/elements'; @@ -173,29 +173,29 @@ export class Server extends EventEmitter { private _processSoapHeader(soapHeader, name, namespace, xmlns) { switch (typeof soapHeader) { - case 'object': - return this.wsdl.objectToXML(soapHeader, name, namespace, xmlns, true); - case 'function': - const _this = this; - // arrow function does not support arguments variable - // tslint:disable-next-line - return function() { - const result = soapHeader.apply(null, arguments); - - if (typeof result === 'object') { - return _this.wsdl.objectToXML(result, name, namespace, xmlns, true); - } else { - return result; - } - }; - default: - return soapHeader; + case 'object': + return this.wsdl.objectToXML(soapHeader, name, namespace, xmlns, true); + case 'function': + const _this = this; + // arrow function does not support arguments variable + // tslint:disable-next-line + return function () { + const result = soapHeader.apply(null, arguments); + + if (typeof result === 'object') { + return _this.wsdl.objectToXML(result, name, namespace, xmlns, true); + } else { + return result; + } + }; + default: + return soapHeader; } } private _initializeOptions(options: IServerOptions) { this.wsdl.options.attributesKey = options.attributesKey || 'attributes'; - this.onewayOptions.statusCode = this.onewayOptions.responseCode || 200; + this.onewayOptions.statusCode = this.onewayOptions.responseCode || 200; this.onewayOptions.emptyBody = !!this.onewayOptions.emptyBody; } @@ -468,6 +468,7 @@ export class Server extends EventEmitter { includeTimestamp?, ) { options = options || {}; + let port: IServicePort; let method: ISoapServiceMethod; let body; let headers; @@ -490,6 +491,7 @@ export class Server extends EventEmitter { try { method = this.services[serviceName][portName][methodName]; + port = this.services[serviceName][portName]; } catch (error) { return callback(this._envelope('', headers, includeTimestamp)); } @@ -546,7 +548,7 @@ export class Server extends EventEmitter { handleResult(error, result); }; - const result = method(args, methodCallback, options.headers, req); + const result = method.apply(this, [args, methodCallback, options.headers, req]); if (typeof result !== 'undefined') { if (isPromiseLike(result)) { result.then((value) => {