@iotize/tap
Version:
IoTize Device client for Javascript
16 lines (14 loc) • 23.9 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@iotize/tap"),require("@iotize/tap/client/api"),require("@iotize/tap/client/impl"),require("@iotize/tap/service/core"),require("@iotize/common/error"),require("@iotize/common/byte-converter"),require("@iotize/common/crypto"),require("@iotize/tap/auth"),require("@iotize/tap/crypto"),require("@iotize/common/task-manager"),require("@iotize/common/utility"),require("@iotize/common/debug"),require("@iotize/common/array"),require("@iotize/common/byte-stream")):"function"==typeof define&&define.amd?define("@iotize/tap/service/impl/single-packet",["exports","@iotize/tap","@iotize/tap/client/api","@iotize/tap/client/impl","@iotize/tap/service/core","@iotize/common/error","@iotize/common/byte-converter","@iotize/common/crypto","@iotize/tap/auth","@iotize/tap/crypto","@iotize/common/task-manager","@iotize/common/utility","@iotize/common/debug","@iotize/common/array","@iotize/common/byte-stream"],t):t(((e="undefined"!=typeof globalThis?globalThis:e||self).iotize=e.iotize||{},e.iotize.tap=e.iotize.tap||{},e.iotize.tap.service=e.iotize.tap.service||{},e.iotize.tap.service.impl=e.iotize.tap.service.impl||{},e.iotize.tap.service.impl["single-packet"]={}),e.iotize.tap,e.iotize.tap.client.api,e.iotize.tap.client.impl,e.iotize.tap.service.core,e.error,e.byteConverter,e.crypto$1,e.iotize.tap.auth,e.iotize.tap.crypto,e.taskManager,e.utility,e.debug$1,e.array,e.byteStream)}(this,(function(e,t,r,n,i,a,o,c,s,l,u,d,p,h,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 g=function(e,t){return g=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])},g(e,t)};function y(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function r(){this.constructor=e}g(e,t),e.prototype=null===t?Object.create(t):(r.prototype=t.prototype,new r)}function f(e,t){var r,n,i,a,o={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return a={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function c(a){return function(c){return function(a){if(r)throw new TypeError("Generator is already executing.");for(;o;)try{if(r=1,n&&(i=2&a[0]?n.return:a[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,a[1])).done)return i;switch(n=0,i&&(a=[2&a[0],i.value]),a[0]){case 0:case 1:i=a;break;case 4:return o.label++,{value:a[1],done:!1};case 5:o.label++,n=a[1],a=[0];continue;case 7:a=o.ops.pop(),o.trys.pop();continue;default:if(!(i=o.trys,(i=i.length>0&&i[i.length-1])||6!==a[0]&&2!==a[0])){o=0;continue}if(3===a[0]&&(!i||a[1]>i[0]&&a[1]<i[3])){o.label=a[1];break}if(6===a[0]&&o.label<i[1]){o.label=i[1],i=a;break}if(i&&o.label<i[2]){o.label=i[2],o.ops.push(a);break}i[2]&&o.ops.pop(),o.trys.pop();continue}a=t.call(e,o)}catch(e){a=[6,e],n=0}finally{r=i=0}if(5&a[0])throw a[1];return{value:a[0]?a[1]:void 0,done:!0}}([a,c])}}}Object.create;function m(e,t){var r="function"==typeof Symbol&&e[Symbol.iterator];if(!r)return e;var n,i,a=r.call(e),o=[];try{for(;(void 0===t||t-- >0)&&!(n=a.next()).done;)o.push(n.value)}catch(e){i={error:e}}finally{try{n&&!n.done&&(r=a.return)&&r.call(a)}finally{if(i)throw i.error}}return o}function k(e,t,r){if(r||2===arguments.length)for(var n,i=0,a=t.length;i<a;i++)!n&&i in t||(n||(n=Array.prototype.slice.call(t,0,i)),n[i]=t[i]);return e.concat(n||Array.prototype.slice.call(t))}var E,P,T,v,w,C;Object.create;e.SinglePacketStoreInfo=void 0,P=e.SinglePacketStoreInfo||(e.SinglePacketStoreInfo={}),(E=P.State||(P.State={}))[E.UNAVAILABLE=0]="UNAVAILABLE",E[E.EMPTY=1]="EMPTY",E[E.USED=2]="USED",E[E.FULL=3]="FULL",E[E.ERROR=4]="ERROR",e.SinglePacket=void 0,v=e.SinglePacket||(e.SinglePacket={}),(T=v.PacketType||(v.PacketType={}))[T.DATA_LOG=1]="DATA_LOG",T[T.COMMAND=2]="COMMAND",T[T.CODE_EXEC=3]="CODE_EXEC",T[T.CONF_UPDATE=4]="CONF_UPDATE",T[T.SECURITY_TOKEN=5]="SECURITY_TOKEN",T[T.TARGET_FW_UPDATE=6]="TARGET_FW_UPDATE",T[T.MASK=7]="MASK",e.SinglePacketChunk=void 0,C=e.SinglePacketChunk||(e.SinglePacketChunk={}),(w=C.Format||(C.Format={}))[w.OFFSET_ON_2_BYTES=0]="OFFSET_ON_2_BYTES",w[w.OFFSET_ON_4_BYTES=8]="OFFSET_ON_4_BYTES",n.TapStreamReader.prototype.readSinglePacketStoreInfo=function(){var e={};return this.forwardBits(24),e.state=this.readUnsigned(1),e},n.TapStreamReader.prototype.readSinglePacket=function(){var e={};return e.sendTime=this.readUnsigned(4),e.header=this.readSinglePacketHeader(),e.payload=this.readSinglePacketPayload(),e},n.TapStreamReader.prototype.readSinglePacketHeader=function(){var e={};return e.packetLength=this.readUnsigned(2),e.packetId=this.readUnsigned(2),e.configVersion=this.readUnsigned(4),e.messageType=this.readBits(4),this.forwardBits(1),e.isExtended=this.readBoolean(1),e.encryption=this.readBoolean(1),e.ack=this.readBoolean(1),e.senderId=this.readUnsigned(1),e.salt=this.readUnsigned(2),e},n.TapStreamReader.prototype.readSinglePacketPayload=function(){var e={};return e.logTime=this.readUnsigned(4),e.dataSize=this.readUnsigned(2),e.data=this.readBytes(0==(void 0===e.dataSize?0:e.dataSize)?this.getStreamSize()-4:void 0===e.dataSize?0:e.dataSize),e.padding=this.readBytes((4-(e.data.length+10)%4)%4),e.crc=this.readUnsigned(4),e},n.TapStreamReader.prototype.readSinglePacketChunk=function(){var e={};return e.format=this.readBits(4),e.length=this.readBits(12),e.offset=this.readUnsigned(8==e.format?4:2),e.data=this.readBytes(16*(void 0===e.length?0:e.length)),e},n.TapStreamWriter.prototype.writeSinglePacketStoreInfo=function(t){return this.forwardBits(24),this.writeBitsInt(n.enumKeyOrValueToNumber(t.state,e.SinglePacketStoreInfo.State),8),this},n.TapStreamWriter.prototype.writeSinglePacket=function(e){return this.writeUnsigned(e.sendTime,4),this.writeSinglePacketHeader(e.header),this.writeSinglePacketPayload(e.payload),this},n.TapStreamWriter.prototype.writeSinglePacketHeader=function(t){return this.writeUnsigned(t.packetLength,2),this.writeUnsigned(t.packetId,2),this.writeUnsigned(t.configVersion,4),this.writeBitsInt(n.enumKeyOrValueToNumber(t.messageType,e.SinglePacket.PacketType),4),this.forwardBits(1),this.writeBoolean(t.isExtended,1),this.writeBoolean(t.encryption,1),this.writeBoolean(t.ack,1),this.writeUnsigned(t.senderId,1),this.writeUnsigned(t.salt,2),this},n.TapStreamWriter.prototype.writeSinglePacketPayload=function(e){return this.writeUnsigned(e.logTime,4),this.writeUnsigned(void 0!==e.dataSize?e.dataSize:e.data.length>65535?0:e.data.length,2),this.writeBytes(e.data,0==(void 0===e.dataSize?0:e.dataSize)?e.data.length:void 0===e.dataSize?0:e.dataSize),this.writeFunction("padding",void 0,e.padding,(4-(e.data.length+10)%4)%4),this.writeFunction("crc32",{offset:4},e.crc,4),this},n.TapStreamWriter.prototype.writeSinglePacketChunk=function(t){return this.writeBitsInt(n.enumKeyOrValueToNumber(t.format,e.SinglePacketChunk.Format),4),this.writeBits(void 0!==t.length?t.length:t.data.length/16,12),this.writeUnsigned(t.offset,8==t.format?4:2),this.writeBytes(t.data,0==(void 0===t.length?0:t.length)?t.data.length:16*(void 0===t.length?0:t.length)),this};var _,A,z=function(){function e(){}return e.prototype.encode=function(e,t){return void 0===t&&(t=new n.TapStreamWriter),t.writeSinglePacketStoreInfo(e),t.toBytes},e.prototype.decode=function(e){return(e instanceof n.TapStreamReader?e:n.TapStreamReader.create(e)).readSinglePacketStoreInfo()},e}(),I=function(){function e(){}return e.prototype.encode=function(e,t){return void 0===t&&(t=new n.TapStreamWriter),t.writeSinglePacket(e),t.toBytes},e.prototype.decode=function(e){return(e instanceof n.TapStreamReader?e:n.TapStreamReader.create(e)).readSinglePacket()},e}(),b=function(){function e(){}return e.prototype.encode=function(e,t){return void 0===t&&(t=new n.TapStreamWriter),t.writeSinglePacketChunk(e),t.toBytes},e.prototype.decode=function(e){return(e instanceof n.TapStreamReader?e:n.TapStreamReader.create(e)).readSinglePacketChunk()},e}(),R=new z,x=new I,O=new b,B=Object.freeze({__proto__:null,singlePacketStoreInfo:R,singlePacket:x,singlePacketChunk:O}),M=Object.assign(Object.assign({},i.converters),B),F={getInfo:{method:r.TapRequestFrame.MethodType.GET,pathAlias:"/single-packet/info",path:"/1024//81",responseBodyDecoder:M.singlePacketStoreInfo},writeAndExecuteBytes:{method:r.TapRequestFrame.MethodType.POST,pathAlias:"/single-packet/write-and-execute-bytes",path:"/1024//80"},writeAndExecute:{method:r.TapRequestFrame.MethodType.POST,pathAlias:"/single-packet/write-and-execute",path:"/1024//80",bodyEncoder:M.singlePacket},writeChunk:{method:r.TapRequestFrame.MethodType.POST,pathAlias:"/single-packet/write-chunk",path:"/1024//82",bodyEncoder:M.singlePacketChunk},executeSinglePacketInStore:{method:r.TapRequestFrame.MethodType.POST,pathAlias:"/single-packet/execute-single-packet-in-store",path:"/1024//83",bodyEncoder:M.uint32}},U=function(e){function t(){var t=e.apply(this,k([],m(arguments)))||this;return t.resources=F,t}return y(t,e),t.prototype.getInfo=function(){return this.serviceCallRunner.execute(this.getInfoCall())},t.prototype.getInfoCall=function(){return Object.assign({},this.resources.getInfo)},t.prototype.writeAndExecuteBytes=function(e){return this.serviceCallRunner.execute(this.writeAndExecuteBytesCall(e))},t.prototype.writeAndExecuteBytesCall=function(e){var t=Object.assign({},this.resources.writeAndExecuteBytes);return t.body=e,t},t.prototype.writeAndExecute=function(e){return this.serviceCallRunner.execute(this.writeAndExecuteCall(e))},t.prototype.writeAndExecuteCall=function(e){var t=Object.assign({},this.resources.writeAndExecute);return t.body=e,t},t.prototype.writeChunk=function(e){return this.serviceCallRunner.execute(this.writeChunkCall(e))},t.prototype.writeChunkCall=function(e){var t=Object.assign({},this.resources.writeChunk);return t.body=e,t},t.prototype.executeSinglePacketInStore=function(e){return this.serviceCallRunner.execute(this.executeSinglePacketInStoreCall(e))},t.prototype.executeSinglePacketInStoreCall=function(e){var t=Object.assign({},this.resources.executeSinglePacketInStore);return t.body=e,t},t}(t.AbstractService),L=t.extendServiceContainer("singlePacket",U);e.SinglePacketError=function(e){function t(t,r){return e.call(this,r,t)||this}return y(t,e),t.authSessionRequiredToEncryptSinglePacket=function(){return new t(t.Code.AuthSessionRequiredToEncryptSinglePacket,"A valid authenticated Tap session is required to encrypt single packet")},t.credentialsRequiredForEncryptedSinglePacket=function(){return new t(t.Code.CredentialsRequiredForEncryptedSinglePacket,"Single packet encryption is enabled, you need to provid encryption credentials")},t.internalError=function(e){return new t(t.Code.SinglePacketInternalError,"Single packet related internal error: "+e)},t.maxSinglePacketPartSizeExceeded=function(e,r){return new t(t.Code.MaxSinglePacketPartSizeExceeded,"Single packet part size of "+e.data.length+" bytes is above single packet maximum size of "+r+" bytes")},t.maxSinglePacketSizeExceeded=function(e,r){return new t(t.Code.MaxSinglePacketSizeExceeded,"Single packet size of "+e+" bytes is above single packet maximum size of "+r+" bytes")},t}(a.CodeError),A=e.SinglePacketError||(e.SinglePacketError={}),(_=A.Code||(A.Code={})).SinglePacketInternalError="SinglePacketErrorInternalError",_.AuthSessionRequiredToEncryptSinglePacket="SinglePacketErrorAuthSessionRequiredToEncryptSinglePacket",_.MaxSinglePacketPartSizeExceeded="SinglePacketErrorMaxSinglePacketPartSizeExceeded",_.MaxSinglePacketSizeExceeded="SinglePacketErrorMaxSinglePacketSizeExceeded",_.CredentialsRequiredForEncryptedSinglePacket="SinglePacketErrorCredentialsRequiredForEncryptedSinglePacket";var D=function(){function e(){}return e.prototype.generate=function(e){var t=new n.TapStreamWriter;if(e.username.length>16)throw new Error("Invalid username too long: "+e.username);if(t.writeStr(e.username),18!==e.serialNumber.length)throw new Error("Invalid sn: "+e.serialNumber+". Length must be 18");if(t.writeStr(e.serialNumber,18),12!==e.headerBytes.length)throw new Error("Invalid header length "+e.headerBytes.length+". Expected 12");t.writeBytes(e.headerBytes);var r=t.toBytes;return l.hmacSHA256(r,e.hashKey).slice(0,16)},e}();n.TapStreamReader.prototype.readSinglePacketPayloadFullData=function(){var e={};return e.logTime=this.readU4(),e.dataSize=this.readU2(),e.data=this.readBytes(-4),e.crc=this.readU4(),e};var K=function(){function e(){}return e.fromBlankKey=function(e){return{hashKey:new Uint8Array(32),serialNumber:"IoTize_FWUpdate_SP",username:e}},e.fromHashKey=function(e,t){return{hashKey:e,serialNumber:"IoTize_FWUpdate_SP",username:t}},e.fromUserCredentials=function(t,r){var n=s.ScramAuth.computeBaseKeys(t.password,r);return e.fromCurrentSessionData({username:t.username,serverKey:n.serverKey,storedKey:n.storedKey})},e.fromCurrentSessionData=function(e){var t=n.TapStreamWriter.create(32).writeBytes(e.storedKey).writeBytes(e.serverKey).toBytes;if(32!==t.length)throw new Error("Single packet encryption not available");return{hashKey:t,serialNumber:"IoTize_FWUpdate_SP",username:e.username}},e}(),N=function(){function e(e,t,r){void 0===t&&(t=new c.AesEcb128Converter),void 0===r&&(r=new D),this.options=e,this.encryptionAlgo=t,this.keyGenerator=r}return e.createWithBlankKey=function(){return new e(K.fromBlankKey("IoTize"))},e.prototype.setEncryptionOptions=function(e){this.options=Object.assign(Object.assign({},this.options),e)},e.prototype.encode=function(e){var t=new n.TapStreamWriter;t.writeU4(e.sendTime),t.writeSinglePacketHeader(e.header);var r=t.pos;if(t.writeSinglePacketPayload(e.payload),e.header.encryption){var i=t.toBytes,a=this._getEncryptionAlgo(e.header),o=i.slice(r);o=a.encode(o),t.pos=r,t.writeBytes(o)}return t.toBytes},e.prototype.decode=function(e){var t,r=n.TapStreamReader.fromArray(e),i=r.readU4(),a=r.readSinglePacketHeader();if(a.encryption){var o=r.readBytesFull(),c=this._getEncryptionAlgo(a).decode(o);t=n.TapStreamReader.fromArray(c).readSinglePacketPayloadFullData()}else t=r.readSinglePacketPayload();return{sendTime:i,header:a,payload:t}},e.prototype._getEncryptionAlgo=function(e){var t=this.computeKey(e);return this.encryptionAlgo.setOptions({iv:"00000000000000000000000000000000",key:o.bufferToHexString(t)}),this.encryptionAlgo},e.prototype.computeKey=function(e){e instanceof Uint8Array||(e=new n.TapStreamWriter(12).writeSinglePacketHeader(e).toBytes);var t=Object.assign(Object.assign({},this.options),{headerBytes:e});return this.keyGenerator.generate(t)},e}(),q=p.createDebugger("@iotize/tap/single-packet");function H(e,t){return(t-e%t)%t}function Z(e){if(e>65535)throw new Error("Invalid user id. Should be between 0 and 65535");if(e>127&&e<65408)throw new Error("Invalid user id. Should be between 0 and 0 and 127 or 65535 and 65408");return 255&e}var W=function(e){return new Uint8Array(e)},j=function(){function t(e,r){void 0===r&&(r=W),this.paddingGenerator=r,this.model={sendTime:0,header:{ack:!1,configVersion:0,encryption:!1,isExtended:!1,messageType:e,packetId:0,packetLength:0,salt:Math.round(65535*Math.random()),senderId:0},payload:{data:new Uint8Array,dataSize:0,logTime:t.getTimestampInSeconds(),padding:void 0}}}return t.getTimestampInSeconds=function(){return Math.round((new Date).getTime()/1e3)},t.getLengthMultiplicator=function(t){return t.header.isExtended?256:t.header.encryption||t.header.messageType===e.SinglePacket.PacketType.CODE_EXEC?16:1},t.create=function(e,r,n){void 0===n&&(n=W);var i=new t(r,n);return i.setData(e),i},t.prototype.setLogTime=function(e){return this.model.payload.logTime=e,this},t.prototype.setPacketId=function(e){return this.model.header.packetId=e,this},t.prototype.setSalt=function(e){return this.model.header.salt=e,this},t.prototype.setExtended=function(e){return this.model.header.isExtended=e,this},t.prototype.setEncryption=function(e){return this.model.header.encryption=e,this},t.prototype.setSenderId=function(e){return this.model.header.senderId=e,this},t.prototype.setSendTime=function(e){return this.model.sendTime=e,this},t.prototype.setConfigVersion=function(e){return this.model.header.configVersion=e,this},t.prototype.setData=function(e){return this.model.payload.data=e,this},t.prototype.getLengthMultiplicator=function(){return t.getLengthMultiplicator(this.model)},t.prototype.build=function(){this.model.sendTime||(this.model.sendTime=t.getTimestampInSeconds());var r=this.getLengthMultiplicator();this.model.payload.data.length>65535*r&&(this.model.header.isExtended=!0),r=this.getLengthMultiplicator();var n=Math.max(4,r),i=H(this.model.payload.data.length+t.PACKET_PAYLOAD_HEADER_SIZE,n);this.model.payload.padding=this.paddingGenerator(i);var a=this.model.payload.data.length+t.PACKET_PAYLOAD_HEADER_SIZE+this.model.payload.padding.length,o=t.PACKET_HEADER_SIZE+this.model.payload.data.length+this.model.payload.padding.length;if(this.model.payload.dataSize=this.model.payload.data.length>65535?void 0:this.model.payload.data.length,a%r!=0)throw e.SinglePacketError.internalError("Packet length "+o+" must be a multiple of "+r);if(o>t.MAX_FULL_SIZE)throw e.SinglePacketError.maxSinglePacketSizeExceeded(o,t.MAX_FULL_SIZE);return this.model.header.packetLength=a/r,1===r&&(this.model.header.packetLength+=t.PACKET_OUTER_HEADER_SIZE),this.model},t}();j.MAX_FULL_SIZE=2097152,j.MAX_PART_SIZE=192,j.PACKET_PAYLOAD_HEADER_SIZE=10,j.PACKET_OUTER_HEADER_SIZE=12,j.PACKET_HEADER_SIZE=j.PACKET_OUTER_HEADER_SIZE+j.PACKET_PAYLOAD_HEADER_SIZE,j.PACKET_HEADER_SIZE_WITH_SEND_TIME=j.PACKET_HEADER_SIZE+4,j.MAX_DATA_SIZE=j.MAX_FULL_SIZE-(j.PACKET_HEADER_SIZE_WITH_SEND_TIME+255);var V=function(){function t(r){void 0===r&&(r={}),r.sizeMultiplicator||(r.sizeMultiplicator=t.SIZE_MULTIPLICATOR),r.chunkSize||(r.chunkSize=j.MAX_PART_SIZE),r.format||(r.format=e.SinglePacketChunk.Format.OFFSET_ON_2_BYTES),r.pad||(r.pad=!1),this.options=r,this.checkOptions()}return Object.defineProperty(t.prototype,"sizeMultiplicator",{get:function(){return this.options.sizeMultiplicator},enumerable:!1,configurable:!0}),t.prototype.checkOptions=function(){if(this.options.chunkSize<this.sizeMultiplicator)throw new Error("Chunk size must be greater than "+this.sizeMultiplicator+". Given: "+this.options.chunkSize);if(!this.options.pad&&this.options.chunkSize%this.sizeMultiplicator!=0)throw new Error('Chunk size "'+this.options.chunkSize+'" is not a multiple of length multiplicator "'+this.sizeMultiplicator+'"')},t.prototype.chunk=function(e){var t=this;if(this.options.pad){var r=H(e.length,this.sizeMultiplicator);r>0&&(e=S.KaitaiStreamWriter.mergeArrays(e,new Uint8Array(r)))}else if(e.length%this.sizeMultiplicator!=0)throw new Error("Padding is disabled and data length ("+e.length+" byte(s)) is not a multiple of length multiplicator "+this.sizeMultiplicator+")");var n=h.chunkArray(Array.from(e),this.options.chunkSize).map((function(e){return Uint8Array.from(e)})),i=0;return n.map((function(e){var r={data:e,length:Math.ceil(e.length/t.sizeMultiplicator),offset:Math.ceil(i/t.sizeMultiplicator),format:t.options.format};return i+=e.length,r}))},t}();V.SIZE_MULTIPLICATOR=16;var Y,X,G=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(i,a){function o(e){try{s(n.next(e))}catch(e){a(e)}}function c(e){try{s(n.throw(e))}catch(e){a(e)}}function s(e){var t;e.done?i(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(o,c)}s((n=n.apply(e,t||[])).next())}))};e.SinglePacketStoreLoader=function(i){function a(e,t){var r=i.call(this,u.StepOperations.createSequentialContainers(e))||this;return r.singlePacketService=t,r}return y(a,i),a.createOperationsFromBytes=function(t,r,n){var i=(null==n?void 0:n.chunkSize)||j.MAX_PART_SIZE,o=t.length/i<=65535,c=new V({chunkSize:i,format:o?e.SinglePacketChunk.Format.OFFSET_ON_2_BYTES:e.SinglePacketChunk.Format.OFFSET_ON_4_BYTES,pad:!n||!("pad"in n)||n.pad}).chunk(t);return a.createOperationsFromChunk(c,r,n)},a.createFromBytes=function(e,t,r){var n=a.createOperationsFromBytes(e,t,r);return new a(n,t)},a.createOperationsFromChunk=function(i,o,c,s){var l=this;void 0===s&&(s={expectedCRC:0}),s.expectedCRC=n.CRC.FIRST_VALUE;var u=i.map((function(u,p){return function(h,S){return G(l,void 0,void 0,(function(){var l,S,g;return f(this,(function(y){switch(y.label){case 0:if(u.data.length>j.MAX_PART_SIZE)throw e.SinglePacketError.maxSinglePacketPartSizeExceeded(u,j.MAX_PART_SIZE);q("createSinglePacketLoadOperations","sendSinglePacket","sending packet "+(p+1)+"/"+i.length+" offset: "+u.offset+"..."+(u.offset+(u.length||0))),l=0,S=(null==c?void 0:c.maxRetry)||3,s.expectedCRC=n.CRC.fromBytes(u.data,s.expectedCRC),y.label=1;case 1:return l++,[4,o.writeChunk(u)];case 2:return g=y.sent(),l<S&&(g.status===r.ResultCode.SERVICE_UNAVAILABLE||g.status===r.ResultCode.NVM_ERROR)?(q("createSinglePacketLoadOperations","Attempt "+(l+1)+"/"+S+" failed with error "+t.TapError.reponseStatusError(g).toString()),[4,d.sleep((null==c?void 0:c.retryDelay)||500)]):[3,4];case 3:return y.sent(),[3,1];case 4:return g.successful(),h.next({step:a.Step.loadPacketChunk,progress:{total:i.length,loaded:p+1}}),[2];case 5:return[2]}}))}))}}));return(null==c?void 0:c.execute)&&u.push((function(e,t){return G(l,void 0,void 0,(function(){return f(this,(function(t){switch(t.label){case 0:return e.next({step:a.Step.executeSinglePacket,expectedCRC:s.expectedCRC}),[4,o.executeSinglePacketInStore(s.expectedCRC)];case 1:return t.sent().successful(),[2]}}))}))})),u},a.createFromChunks=function(e,t,r){return new a(a.createOperationsFromChunk(e,t,r),t)},a.computeCRC=function(e){return e instanceof Uint8Array?n.CRC.mpeg2().compute(e):e.reduce((function(e,t){return n.CRC.mpeg2().compute(t.data,e)}),n.CRC.FIRST_VALUE)},a}(u.StepOperations),X=e.SinglePacketStoreLoader||(e.SinglePacketStoreLoader={}),(Y=X.Step||(X.Step={})).loadPacketChunk="loadPacketChunk",Y.executeSinglePacket="executeSinglePacket",U.prototype.createStoreLoader=function(t,r,n){var i=N.createWithBlankKey();if(n)i.setEncryptionOptions(n),t.header.encryption=!0,t.header.senderId=function(e){switch(e){case"IoTize":return 253;case"admin":return Z(65535);case"anonymous":return Z(0);case"supervisor":return Z(65534);default:return 0}}(n.username);else if(t.header.encryption)throw e.SinglePacketError.credentialsRequiredForEncryptedSinglePacket();var a=i.encode(t);return this.createStoreLoaderFromBytes(a,r)},U.prototype.createStoreLoaderFromBytes=function(t,r){return e.SinglePacketStoreLoader.createFromBytes(t,this,r)};e.EncryptedSinglePacketConverter=N,e.SINGLEPACKET_SERVICE_CALLS=F,e.SinglePacketBuilder=j,e.SinglePacketChunkConverter=b,e.SinglePacketConverter=I,e.SinglePacketResourceMetaData={},e.SinglePacketService=U,e.SinglePacketSplitter=V,e.SinglePacketStoreInfoConverter=z,e._TAP_SERVICE_EXTENSION_SINGLEPACKET_=L,e.singlePacketConverters=B,e["ɵa"]=K,Object.defineProperty(e,"__esModule",{value:!0})}));
//# sourceMappingURL=iotize-tap-service-impl-single-packet.umd.min.js.map