@iotize/tap
Version:
IoTize Device client for Javascript
16 lines (14 loc) • 10.7 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@iotize/tap/protocol/core"),require("rxjs"),require("rxjs/operators"),require("@iotize/common/debug"),require("@iotize/common/byte-converter"),require("@iotize/tap/client/api"),require("@iotize/tap/client/impl"),require("@iotize/common/promise"),require("@iotize/tap"),require("@iotize/tap/service/core")):"function"==typeof define&&define.amd?define("@iotize/tap/testing",["exports","@iotize/tap/protocol/core","rxjs","rxjs/operators","@iotize/common/debug","@iotize/common/byte-converter","@iotize/tap/client/api","@iotize/tap/client/impl","@iotize/common/promise","@iotize/tap","@iotize/tap/service/core"],t):t(((e="undefined"!=typeof globalThis?globalThis:e||self).iotize=e.iotize||{},e.iotize.tap=e.iotize.tap||{},e.iotize.tap.testing={}),e.iotize.tap.protocol.core,e.rxjs,e.rxjs.operators,e.debug$1,e.byteConverter,e.iotize.tap.client.api,e.iotize.tap.client.impl,e.promise,e.iotize.tap)}(this,(function(e,t,n,o,r,i,c,u,a,s){"use strict";
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */var p=function(e,t){return p=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])},p(e,t)};function l(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function n(){this.constructor=e}p(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}function f(e,t){var n,o,r,i,c={label:0,sent:function(){if(1&r[0])throw r[1];return r[1]},trys:[],ops:[]};return i={next:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function u(i){return function(u){return function(i){if(n)throw new TypeError("Generator is already executing.");for(;c;)try{if(n=1,o&&(r=2&i[0]?o.return:i[0]?o.throw||((r=o.return)&&r.call(o),0):o.next)&&!(r=r.call(o,i[1])).done)return r;switch(o=0,r&&(i=[2&i[0],r.value]),i[0]){case 0:case 1:r=i;break;case 4:return c.label++,{value:i[1],done:!1};case 5:c.label++,o=i[1],i=[0];continue;case 7:i=c.ops.pop(),c.trys.pop();continue;default:if(!(r=c.trys,(r=r.length>0&&r[r.length-1])||6!==i[0]&&2!==i[0])){c=0;continue}if(3===i[0]&&(!r||i[1]>r[0]&&i[1]<r[3])){c.label=i[1];break}if(6===i[0]&&c.label<r[1]){c.label=r[1],r=i;break}if(r&&c.label<r[2]){c.label=r[2],c.ops.push(i);break}r[2]&&c.ops.pop(),c.trys.pop();continue}i=t.call(e,c)}catch(e){i=[6,e],o=0}finally{n=r=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,u])}}}Object.create;function d(e,t){var n="function"==typeof Symbol&&e[Symbol.iterator];if(!n)return e;var o,r,i=n.call(e),c=[];try{for(;(void 0===t||t-- >0)&&!(o=i.next()).done;)c.push(o.value)}catch(e){r={error:e}}finally{try{o&&!o.done&&(n=i.return)&&n.call(i)}finally{if(r)throw r.error}}return c}function y(e,t,n){if(n||2===arguments.length)for(var o,r=0,i=t.length;r<i;r++)!o&&r in t||(o||(o=Array.prototype.slice.call(t,0,r)),o[r]=t[r]);return e.concat(o||Array.prototype.slice.call(t))}Object.create;var h=r.createDebugger("@iotize/tap/testing"),v=function(){function e(){this._routes=[]}return Object.defineProperty(e.prototype,"routes",{get:function(){return this._routes},enumerable:!1,configurable:!0}),e.prototype.findRoute=function(e){var t=this._routes.filter((function(t){return t.predicate(e)}));return 0===t.length?void 0:t[0].adapter(e)},e.prototype.addRoute=function(e,t){return this._routes.push({predicate:e,adapter:t}),this},e.prototype.clearRoutes=function(){return this._routes=[],this},e}(),m=this&&this.__awaiter||function(e,t,n,o){return new(n||(n=Promise))((function(r,i){function c(e){try{a(o.next(e))}catch(e){i(e)}}function u(e){try{a(o.throw(e))}catch(e){i(e)}}function a(e){var t;e.done?r(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(c,u)}a((o=o.apply(e,t||[])).next())}))};function b(){return new w}var w=function(e){function t(){var t=e.call(this)||this;return t.mockOptions={connect:{delay:100},disconnect:{delay:100},write:{delay:1}},t.input=new n.Subject,t.output=new n.Subject,t._router=new v,t}return l(t,e),t.MIRROR=function(){var e=new t;return e.input.subscribe((function(t){e.setOutputData(t)})),e},t.prototype.setConnectionState=function(e){return h("MockProtocol","setConnectionState: "+e),this.connectionState=e,this},t.prototype.setConnectDelay=function(e){return this.mockOptions.connect.delay=e,this},t.prototype.getDisconnectDelay=function(){return this.mockOptions.disconnect.delay},t.prototype.setDisconnectDelay=function(e){return this.mockOptions.disconnect.delay=e,this},t.prototype.setOutputData=function(e){this.lastTranseiveData=e,this.output.next(e)},t.prototype.setInputData=function(e){this.lastReceivedData=e,this.input.next(e)},t.prototype.setCallback=function(e){this.callback=e},t.prototype._connect=function(e){return n.of(void 0).pipe(o.delay(this.mockOptions.connect.delay))},t.prototype._disconnect=function(e){return n.of(void 0).pipe(o.delay(this.mockOptions.disconnect.delay))},t.prototype.write=function(e){return m(this,void 0,void 0,(function(){var t,n=this;return f(this,(function(o){switch(o.label){case 0:return o.trys.push([0,5,,6]),this.callback?[4,this.callback.beforeWrite(e)]:[3,2];case 1:o.sent(),o.label=2;case 2:return this.setInputData(e),this.callback?[4,this.callback.afterWrite(e)]:[3,4];case 3:o.sent(),o.label=4;case 4:return[2,new Promise((function(e){return setTimeout(e,n.mockOptions.write.delay)}))];case 5:return t=o.sent(),[2,new Promise((function(e,o){return setTimeout((function(){return o(t)}),n.mockOptions.write.delay)}))];case 6:return[2]}}))}))},t.prototype.read=function(){try{return this.callback&&this.callback.beforeRead(),this.output.pipe(o.first()).toPromise().then((function(e){return e||new Uint8Array}))}catch(e){return Promise.reject(e)}},t}(t.QueueComProtocol),R=/^(GET|PUT|POST)\s+(\/[0-9]+\/[0-9]*\/[0-9]+)\s?(\s+(0x[a-zA-Z0-9]+))?$/i;var k=function(e){return function(t){return u.TapRequestHelper.toString(e)===u.TapRequestHelper.toString(t)}};function O(e){return new T({isConnected:!!(null==e?void 0:e.connected)})}var T=function(e){function t(o,r){void 0===o&&(o={}),void 0===r&&(r=new v);var i=e.call(this,new u.TapApduRequestConverter,new u.TapApduResponseConverter)||this;return i.router=r,i.input=new n.Subject,i.output=new n.Subject,i.options=Object.assign(Object.assign({},t.DEFAULT_OPTIONS),o),i}return l(t,e),t.prototype.isConnected=function(){return this.options.isConnected},t.createWithMockProtocol=function(){var e=new t({isConnected:!1});return e.addComProtocol(b()),e},t.prototype.connect=function(){var e=this,t=n.of(void 0).pipe(o.delay(this.options.connect.delay));return t.subscribe((function(){e.options.isConnected=!0})),t},t.prototype.disconnect=function(){var e=this,t=n.of(void 0).pipe(o.delay(this.options.disconnect.delay));return t.subscribe((function(){e.options.isConnected=!1})),t},t.prototype.mockResponse=function(e){this.output.next(e)},t.prototype.request=function(e){if(!this.isConnected())return n.throwError(u.TapClientError.notConnectedError());var t=this.output.pipe(o.first(),o.delay(this.options.command.delay),o.timeout(this.options.command.timeout)).toPromise();return this.input.next(e),n.from(t)},t.prototype.computeResponse=function(e){var t=this.router.findRoute(e);return t||(h("MockClient","MockClient","command "+e+" has not been mapped. Returning 404 not found"),u.TapResponseFrameBuilder.ERROR(c.ResultCode.NOT_FOUND))},t}(u.TapClient);function C(e){return void 0===e&&(e=c.ResultCode.OK),new s.TapResponse(u.TapResponseFrameBuilder.create(e))}function g(e,t){void 0===e&&(e=c.ResultCode.OK);var n=new s.TapResponse(u.TapResponseFrameBuilder.create(e));return t instanceof Uint8Array&&(n._tapResponse.data=t),t&&n.setBody(t),n}T.DEFAULT_OPTIONS={isConnected:!1,connect:{delay:100},disconnect:{delay:100},command:{delay:100,timeout:500},encryption:!0};var S=function(e){function t(){return e.call(this,new T)||this}return l(t,e),t}(s.ServiceCallRunner);var z=function(e){function t(t){return e.call(this,O(t))||this}return l(t,e),Object.defineProperty(t.prototype,"client",{get:function(){return this._client},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"lwm2m",{get:function(){return this._lwm2m},enumerable:!1,configurable:!0}),t}(s.Tap);Array.prototype.findSafe=function(e){var t=this.find(e);if(void 0===t)throw new Error("Cannot find element with predicate: "+e);return t},e.MockClient=T,e.MockProtocol=w,e.MockServiceCallRunner=S,e.MockTap=z,e.createMockClient=O,e.createMockProtocol=b,e.createMockService=function(e){return new e(new S)},e.createMockServiceFunction=function(e,t){return void 0===e&&(e=c.ResultCode.OK),void 0===t&&(t={responseDelay:10}),function(){var n=C(e);return a.promiseDelay(n,t.responseDelay)}},e.createMockServiceFunctionCustom=function(e,t){return void 0===t&&(t={responseDelay:10}),function(){for(var n=[],o=0;o<arguments.length;o++)n[o]=arguments[o];return a.promiseDelay(e.apply(void 0,y([],d(n))),t.responseDelay)}},e.createMockServiceFunctionWithBody=function(e,t,n){return void 0===e&&(e=c.ResultCode.OK),void 0===n&&(n={responseDelay:10}),function(){var o=g(e,t);return a.promiseDelay(o,n.responseDelay)}},e.createMockServiceResponse=C,e.createMockServiceResponseError=function(e){return new s.TapResponse(u.TapResponseFrameBuilder.ERROR(e))},e.createMockServiceResponseWithBody=g,e.createMockTap=function(e){return new z(e)},e.createTapRequestFromString=function(e){var t=R.exec(e);if(t){var n=t[1],o=t[2],r=t[4]?i.hexStringToBuffer(t[4]):void 0;return u.TapRequestFrameBuilder.create(c.TapRequestFrame.MethodType[n],o,r)}throw new Error('Invalid tap request string format: "'+e+'".')},e["ɵ0"]=k,e["ɵa"]=v,Object.defineProperty(e,"__esModule",{value:!0})}));
//# sourceMappingURL=iotize-tap-testing.umd.min.js.map