UNPKG

@openinventor/remoteviz-client

Version:

Open Inventor RemoteViz client to integrate advanced 3D visualization into your web-based applications.

45 lines (44 loc) 20.6 kB
/** * Open Inventor RemoteViz Client HTML5 * @version v8.8.2 */ 'use strict';(function(J,G){"function"===typeof define&&define.amd?define([],G):"object"===typeof exports?module.exports=G():J.RemoteVizRenderArea=G()})(this,function(){function J(d,f,h,r){let K;K="string"===typeof d?document.getElementById(d):d;if("DIV"!==K.nodeName)throw"Provided HTML element must be of type div";let U=null,e=null,u=new Image,ca=null,V=!1,l=null,Q=null,F=[],R=!1,W=!1,da=!1,D=0,E=0;const X=r||"FIT";let ea=f,fa=h,v=0,w=0,y=0,z=0,Y=0,ha=0,ia=0,L=!1,ja=!1,oa=!1,pa=null,ka=null;const xa= 0<=navigator.platform.toUpperCase().indexOf("MAC");let A={};const p=function(a,b){A.hasOwnProperty(a)?A[a].push(b):A[a]=[b]},q=function(a,b){A.hasOwnProperty(a)&&(b=A[a].indexOf(b),-1!=b&&A[a].splice(b,1))};this.addServiceListener=function(a){p("ServiceEvent",a)};this.removeServiceListener=function(a){q("ServiceEvent",a)};this.addReceivedFrameListener=function(a){p("ReceivedFrameEvent",a)};this.removeReceivedFrameListener=function(a){q("ReceivedFrameEvent",a)};this.addDecodedFrameListener=function(a){p("DecodedFrameEvent", a)};this.removeDecodedFrameListener=function(a){q("DecodedFrameEvent",a)};this.addResizeListener=function(a){p("ResizeEvent",a)};this.removeResizeListener=function(a){q("ResizeEvent",a)};this.addMessageListener=function(a){p("MessageEvent",a)};this.removeMessageListener=function(a){q("MessageEvent",a)};this.addMouseMoveListener=function(a){p("MouseMoveEvent",a)};this.removeMouseMoveListener=function(a){q("MouseMoveEvent",a)};this.addMouseDownListener=function(a){p("MouseDownEvent",a)};this.removeMouseDownListener= function(a){q("MouseDownEvent",a)};this.addMouseUpListener=function(a){p("MouseUpEvent",a)};this.removeMouseUpListener=function(a){q("MouseUpEvent",a)};this.addMouseOverListener=function(a){p("MouseOverEvent",a)};this.removeMouseOverListener=function(a){q("MouseOverEvent",a)};this.addMouseOutListener=function(a){p("MouseOutEvent",a)};this.removeMouseOutListener=function(a){q("MouseOutEvent",a)};this.addMouseWheelListener=function(a){p("MouseWheelEvent",a)};this.removeMouseWheelListener=function(a){q("MouseWheelEvent", a)};this.addTouchStartListener=function(a){p("TouchStartEvent",a)};this.removeTouchStartListener=function(a){q("TouchStartEvent",a)};this.addTouchEndListener=function(a){p("TouchEndEvent",a)};this.removeTouchEndListener=function(a){q("TouchEndEvent",a)};this.addTouchCancelListener=function(a){p("TouchCancelEvent",a)};this.removeTouchCancelListener=function(a){q("TouchCancelEvent",a)};this.addTouchMoveListener=function(a){p("TouchMoveEvent",a)};this.removeTouchMoveListener=function(a){q("TouchMoveEvent", a)};this.addKeyUpListener=function(a){p("KeyUpEvent",a)};this.removeKeyUpListener=function(a){q("KeyUpEvent",a)};this.addKeyDownListener=function(a){p("KeyDownEvent",a)};this.removeKeyDownListener=function(a){q("KeyDownEvent",a)};const t=function(a,b){if(!A.hasOwnProperty(b))return!0;b=A[b];const g=b.length;if(0==g)return!0;let c=!1;for(let k=0;k<g;k++)c|=b[k](a);return c},M=function(a,b){if(A.hasOwnProperty(a)){b&&b.length||(b=[]);a=A[a];var g=a.length;for(let c=0;c<g;c++)a[c].apply(null,b)}},x= function(a){M("ServiceEvent",a)},la=function(){const a=navigator.userAgent.toLowerCase();let b=!1;try{b=/constructor/i.test(window.HTMLElement)||"[object SafariRemoteNotification]"===(!window.safari||safari.pushNotification).toString()}catch(g){}return b=b||-1!=a.indexOf("safari")&&-1==a.indexOf("chrome")&&-1!=a.indexOf("version/")};if(-1!=navigator.userAgent.toLowerCase().indexOf("msie")||-1!=navigator.userAgent.toLowerCase().indexOf("trident/"))throw"Internet explorer is not supported.";window.WebSocket= window.WebSocket||window.MozWebSocket;if(!window.WebSocket)throw"WebSocket not supported";window.MediaSource=window.MediaSource||window.WebKitMediaSource;this.sendMessage=function(a){if(this.isConnected()){a=a instanceof Uint8Array?{type:"command",messageType:"binary",message:a}:{type:"command",messageType:"string",message:a.toString()};const b=new n;this.websocket.send(b.encode(a))}};this.requestRenderAreaSize=function(a,b){this.isConnected()&&(a={type:"requestsize",width:parseInt(a),height:parseInt(b)}, b=new n,this.websocket.send(b.encode(a)))};this.getContainerWidth=function(){return null==e?0:e.width};this.getContainerHeight=function(){return null==e?0:e.height};this.getRenderAreaWidth=function(){return D};this.getRenderAreaHeight=function(){return E};this.resizeRenderAreaContainer=function(a,b){null!=e&&(e.width=a,e.height=b,ea=a,fa=b,ma(!0),this.isConnected()&&(a={type:"resizecontainer",width:parseInt(a),height:parseInt(b)},b=new n,this.websocket.send(b.encode(a))))};this.isImageStreamingSupported= function(){const a=document.createElement("canvas");return!(!a.getContext||!a.getContext("2d"))};this.isVideoStreamingSupported=function(){var a;if(a=-1!=navigator.userAgent.toLowerCase().indexOf("edge/"))a=-1==navigator.userAgent.toLowerCase().indexOf("android");return!a&&document.createElement("video")&&(window.MediaSource&&MediaSource.isTypeSupported('video/mp4;codecs="avc1.42C014"')||window.MediaSource&&MediaSource.isTypeSupported('video/mp4;codecs="vp09.00.10.08"')&&!la())?!0:!1};this.getStreamingMode= function(){if(null!=e){if("CANVAS"===e.nodeName)return"image";if("VIDEO"===e.nodeName)return"video"}return"unknown"};const H=function(a,b){"mousedown"==a?(ja=!0,e.focus()):"mouseup"==a&&(ja=!1);var g=e.getBoundingClientRect(),c=Math.round((b.clientX-g.left-y)*D/v);g=Math.round((e.height-(b.clientY-g.top)-z)*E/w);ha=c;ia=g;this.isConnected()&&(a={type:"mouseevent",event:a,x:c,y:g,button:b.button,alt:b.altKey,ctrl:b.ctrlKey,shift:b.shiftKey},c=new n,this.websocket.send(c.encode(a)));b.preventDefault()}.bind(this), na=function(a,b){if(this.isConnected()){var g=b.keyCode;xa&&(224==g&&(g=91),17==g?g=91:91==g&&(g=17));oa="keydown"==a;if(b.repeat){b.preventDefault();return}a={type:"keyevent",event:a,key:g,x:ha,y:ia,alt:b.altKey,ctrl:b.ctrlKey,shift:b.shiftKey};g=new n;this.websocket.send(g.encode(a))}b.preventDefault()}.bind(this),Z=function(a,b){const g=b.changedTouches;"touchstart"==a&&e.focus();for(let N=0;N<g.length;N++){var c=g[N],k=e.getBoundingClientRect(),B=Math.round((c.clientX-k.left-y)*D/v);k=Math.round((e.height- (c.clientY-k.top)-z)*E/w);this.isConnected()&&(c={type:"touchevent",event:a,id:c.identifier,x:B,y:k,alt:b.altKey,ctrl:b.ctrlKey,shift:b.shiftKey},B=new n,this.websocket.send(B.encode(c)))}b.preventDefault()}.bind(this),ya=function(a){if(this.isConnected()){const b={type:"mouseevent",event:"mousewheel",delta:0>a.detail||0<a.wheelDelta||0>a.deltaY?120:-120,x:ha,y:ia,alt:a.altKey,ctrl:a.ctrlKey,shift:a.shiftKey},g=new n;this.websocket.send(g.encode(b))}a.preventDefault()}.bind(this),ma=function(a){if(null!= e){if("STRETCH"==X)v=e.width,w=e.height,z=y=0;else if("CROP"==X){var b=D/e.width,g=E/e.height;b>g?(v=Math.round(D/g),w=e.height,y=Math.round((e.width-v)/2),z=0):b<g?(v=e.width,w=Math.round(E/b),y=0,z=Math.round((e.height-w)/2)):(v=e.width,w=e.height,z=y=0)}else b=D/e.width,g=E/e.height,b>g?(v=e.width,w=Math.round(E/b),y=0,z=Math.round((e.height-w)/2),V=!0):b<g?(v=Math.round(D/g),w=e.height,y=Math.round((e.width-v)/2),z=0,V=!0):(v=e.width,w=e.height,z=y=0);"VIDEO"===e.nodeName?"STRETCH"==X?e.setAttribute("style", "object-fit:fill;"):"CROP"==X?e.setAttribute("style","object-fit:cover;"):e.setAttribute("style","object-fit:contain;"):a&&qa()}},qa=function(){V&&(ca.clearRect(0,0,e.width,e.height),V=!1);switch(U){case "image/png":case "image/jpeg":ca.drawImage(u,y,z,v,w)}};u.onload=function(){aa.stopTime();M("DecodedFrameEvent",[[u.frameId,u.isLossless,u.message]]);qa()};const za=function(){var a=function(b){t(b,"TouchStartEvent")&&Z("touchstart",b)}.bind(this);e.addEventListener("touchstart",a,!1);a=function(b){t(b, "TouchMoveEvent")&&Z("touchmove",b)}.bind(this);e.addEventListener("touchmove",a,!1);a=function(b){t(b,"TouchEndEvent")&&Z("touchend",b)}.bind(this);e.addEventListener("touchend",a,!1);a=function(b){t(b,"TouchCancelEvent")&&Z("touchend",b)}.bind(this);e.addEventListener("touchcancel",a,!1);e.onmousedown=function(b){t(b,"MouseDownEvent")&&H("mousedown",b)}.bind(this);e.onmouseover=function(b){t(b,"MouseOverEvent")&&H("mouseenter",b)}.bind(this);e.onmouseout=function(b){t(b,"MouseOutEvent")&&H("mouseleave", b)}.bind(this);e.onmouseup=function(b){t(b,"MouseUpEvent")&&H("mouseup",b)}.bind(this);e.ondblclick=function(b){t(b,"DoubleClickEvent")&&H("mousedoubleclick",b)}.bind(this);e.onmousemove=function(b){t(b,"MouseMoveEvent")&&H("mousemove",b)}.bind(this);a=function(b){t(b,"MouseWheelEvent")&&ya(b)}.bind(this);e.addEventListener("onwheel"in document.createElement("div")?"wheel":void 0!==document.onmousewheel?"mousewheel":"DOMMouseScroll",a,!1);a=function(b){ja&&t(b,"MouseUpEvent")&&H("mouseup",b)}.bind(this); window.addEventListener("mouseup",a,!1);e.onkeyup=function(b){t(b,"KeyUpEvent")&&na("keyup",b)}.bind(this);e.onkeydown=function(b){t(b,"KeyDownEvent")&&na("keydown",b)}.bind(this);a=function(b){oa&&t(b,"KeyUpEvent")&&na("keyup",b)}.bind(this);window.addEventListener("keyup",a,!1);e.setAttribute("tabindex","0");e.focus();e.ondragstart=function(){return!1};"undefined"!=typeof e.style.msTouchAction&&(e.style.msTouchAction="none");"undefined"!=typeof e.style.TouchAction&&(e.style.TouchAction="none")}.bind(this), Aa=function(){if(this.isConnected()){const a=new n;this.websocket.send(a.encode({type:"alive"}))}}.bind(this),Ba=function(){let a=(new Date).getTime();return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(b){const g=(a+16*Math.random())%16|0;a=Math.floor(a/16);return("x"==b?g:g&7|8).toString(16)})},ra=function(a){if(32768>a.length)return String.fromCharCode.apply(null,a);{let b="";const g=a.length;for(let c=0;c<g;c+=32768)b+=String.fromCharCode.apply(null,a.subarray(c,c+32768));return b}}, Ca=function(){let a=[];this.isImageStreamingSupported()&&(a.push("JPEG"),a.push("PNG"));return a}.bind(this);this.wsonopen=function(){void 0==J.staticId&&(J.staticId=Ba());var a=J.staticId;var b=window.location.host,g=navigator.userAgent,c=Ca();var k=[];window.MediaSource&&MediaSource.isTypeSupported('video/mp4;codecs="avc1.42C014"')&&k.push("MP4_H264");window.MediaSource&&MediaSource.isTypeSupported('video/mp4;codecs="vp09.00.10.08"')&&!la()&&k.push("MP4_VP9");a={type:"connection",protocolversion:8, id:a,applicationname:b,environment:g,supportedimagecodecs:c,supportedvideocodecs:k,containerwidth:ea,containerheight:fa};b=new n;this.websocket.send(b.encode(a))};this.wsonclose=function(){L&&(L=!1,setTimeout(()=>{x([["disconnected","NETWORKFAILURE"]])},0))};const Da=function(a){const b=new n;a={type:"ack",frameId:a};this.isConnected()&&(this.websocket.send(b.encode(a)),Y--)}.bind(this),sa=function(){const a=(new n).encode({type:"requestFrame"});return function(){this.isConnected()&&5>Y&&(this.websocket.send(a), Y++)}}().bind(this),Ea=function(){const a=(new n).encode({type:"initializedVideo"});return function(){this.isConnected()&&this.websocket.send(a)}}().bind(this),Fa=function(){if(null!=l){aa.stopTime();var a=l.frameId,b=l.isLossless,g=l.message;if(0<F.length&&!l.updating){const c=F.shift();l.frameId=c.frameId;l.isLossless=c.isLossless;l.message=c.message;l.appendBuffer(c.stream)}M("DecodedFrameEvent",[[a,b,g]])}},ta=function(a,b,g,c){if(0<a.length&&102==a[4]&&116==a[5]&&121==a[6]&&112==a[7]){if(!window.MediaSource)throw"MediaSource API is not available !"; if(!MediaSource.isTypeSupported(U))throw"Unsupported MIME type or codec !";Q=new MediaSource;l=null;F=[];da=W=R=!1;e.src=window.URL.createObjectURL(Q);e.autoplay=!1;Q.addEventListener("sourceopen",function(){l=Q.addSourceBuffer(U);l.addEventListener("updateend",Fa,!1);la()?Q.duration=Infinity:l.mode="sequence";F.unshift({frameId:b,isLossless:g,stream:a,message:c});R=!0;ta([],null,null,null)},!1)}else{if(R&&!W){W=!0;var k=F.shift();l.frameId=k.frameId;l.isLossless=k.isLossless;l.message=k.message; l.appendBuffer(k.stream);e.paused&&(k=e.play(),void 0!==k&&k.then(function(){}).catch(function(){}))}else if(R&&W&&0<a.length)if(0<F.length||l.updating)F.push({frameId:b,isLossless:g,stream:a,message:c});else{l.frameId=b;l.isLossless=g;l.message=c;try{l.appendBuffer(a)}catch(B){}}else 0<a.length&&F.push({frameId:b,isLossless:g,stream:a,message:c});1==e.seekable.length&&(e.paused||e.currentTime>e.seekable.end(0))&&(e.currentTime=e.seekable.end(0));R&&!da&&1<e.readyState&&(da=!0,Ea())}},Ga=function(a){function b(m){m= {type:"requestcalibrationbuffer",size:m};m=(new n).encode(m);B=C();c.websocket.send(m)}function g(){var m=O.length;m=Math.round(O[m-1][0]/Math.max(O[m-1][1]-S,1)*1E3);c.websocket.onmessage=c.wsonmessage.bind(c);const I=8*m;m={type:"networkcapabilities",bandwidth:I,latency:S};const P=new n;c.websocket.send(P.encode(m));setTimeout(()=>{x([["finishedBandwidthCalibration",I]])},0);setTimeout(()=>{x([["finishedNetworkCalibration",I,S]])},0);ka=setInterval(ua,3E3)}const c=this;setTimeout(()=>{x([["startNetworkCalibration"]])}, 0);setTimeout(()=>{x([["startLatencyCalibration"]])},0);let k,B,N,T=5E5,va=!0,S=0,O=[];this.websocket.onmessage=function(m){var I=C(),P=(new G).parse(new Uint8Array(m.data));I=C()-I;"networkcalibration"==P.type&&"buffer"==P.status?(N=C(),m=N-B-I,va?(S=m,va=!1,setTimeout(()=>{x([["finishedLatencyCalibration",S]])},0),setTimeout(()=>{x([["startBandwidthCalibration"]])},0)):O.push([T,m]),P=a-(C()-k),1.5*m>P&&0<O.length||5E7==T?g():(0<O.length&&(T=Math.min(2*T,5E7)),b(T))):c.wsonmessage(m)}.bind(this); k=C();b(1)}.bind(this),C=function(){return(new Date).getTime()};d=function(a,b,g){let c;this.isEnabled=!1;this.durationToRemove=0;this.startTime=function(){this.isEnabled&&(c=C())};this.stopTime=function(){if(this.isEnabled){var k=C()-c-this.durationToRemove;let B={type:b};B[g]=k;k=new n;a.websocket.send(k.encode(B))}}};const ba=new d(this,"networklatency","latency"),aa=new d(this,"decodingtime","time"),wa=function(a){ba.isEnabled=-1!=a.indexOf("networklatency");aa.isEnabled=-1!=a.indexOf("decodingtime")}, ua=function(){if(this.isConnected()){const a=(new n).encode({type:"requestcalibrationbuffer",size:1});ba.startTime();this.websocket.send(a)}}.bind(this),Ha=function(a){if(null==e){switch(a){case 'video/mp4;codecs="avc1.42C014"':case 'video/mp4;codecs="vp09.00.10.08"':e=document.createElement("video");e.controls=!1;e.muted=!0;break;case "image/png":case "image/jpeg":e=document.createElement("canvas");ca=e.getContext("2d");break;default:throw"Unknown content type: "+a;}e.width=ea;e.height=fa;ma(!1); za();K.appendChild(e)}};this.wsonmessage=function(a){if(null!=this.websocket){const b=new G,g=C(),c=b.parse(new Uint8Array(a.data));a=C()-g;if("accept"==c.type)for(L=!0,D=c.width,E=c.height,this.networkCalibration=c.networkcalibration,wa(c.metrics),this.networkCalibration||(ka=setInterval(ua,3E3)),pa=setInterval(Aa,5E3),setTimeout(()=>{x([["connected",c.width,c.height]])},0),Y=0,a=0;5>a;a++)sa();else if(L)if("frame"==c.type){Ha(c.contenttype);Da(c.frameId);sa();aa.startTime();U=c.contenttype;switch(c.contenttype){case 'video/mp4;codecs="avc1.42C014"':case 'video/mp4;codecs="vp09.00.10.08"':ta(c.data, c.frameId,!1,c.message);break;case "image/jpeg":u.isLossless=!1;u.frameId=c.frameId;u.message=c.message;u.src="data:"+c.contenttype+";base64,"+window.btoa(ra(c.data));break;case "image/png":u.isLossless=!0,u.frameId=c.frameId,u.message=c.message,u.src="data:"+c.contenttype+";base64,"+window.btoa(ra(c.data))}M("ReceivedFrameEvent",[[c.frameId,c.data.length,c.message]])}else"command"==c.type?setTimeout(()=>{M("MessageEvent",[c.message])},0):"resize"==c.type?(D=c.width,E=c.height,ma(!1),setTimeout(()=> {M("ResizeEvent",[[c.width,c.height]])},0)):"enabledclientmetrics"==c.type?wa(c.metrics):"networkcalibration"==c.type&&("start"==c.status?Ga("number"===typeof c.duration?c.duration:1E4):"buffer"==c.status?(ba.durationToRemove=a,ba.stopTime()):"pending"==c.status?setTimeout(()=>{x([["pendingNetworkCalibration"]])},0):"finished"==c.status&&setTimeout(()=>{x([["finishedNetworkCalibration",c.bandwidth,c.latency]])},0));else"disconnect"==c.type&&setTimeout(()=>{x([["disconnected",c.reason]])},0)}};this.wsonerror= function(a){setTimeout(()=>{x([["connectionFailed",a]])},0)};this.connectTo=function(a){try{this.websocket=new WebSocket(a),this.websocket.binaryType="arraybuffer",this.websocket.onopen=this.wsonopen.bind(this),this.websocket.onclose=this.wsonclose.bind(this),this.websocket.onmessage=this.wsonmessage.bind(this),this.websocket.onerror=this.wsonerror.bind(this)}catch(b){this.wsonerror(b)}};this.disconnect=function(){clearInterval(pa);clearInterval(ka);null!=this.websocket&&(this.websocket.close(),this.websocket= null,L=!1);null!=e&&(K.removeChild(e),e=null)};this.isConnected=function(){return L&&null!=this.websocket?1==this.websocket.readyState:!1}}class G{constructor(){this.offset=0;this.data=!1}readByte(){const d=this.data[this.offset];this.offset++;return d}readInt32(){let d;d=0|this.data[this.offset+3]<<24;d|=this.data[this.offset+2]<<16;d|=this.data[this.offset+1]<<8;d|=this.data[this.offset];this.offset+=4;return d}readInt64(){var d=0;if(0==this.data[this.offset+7]&&0==this.data[this.offset+6]&&0== this.data[this.offset+5]&&0==this.data[this.offset+4])d|=this.data[this.offset+3]<<24,d|=this.data[this.offset+2]<<16,d|=this.data[this.offset+1]<<8,d|=this.data[this.offset];else{d=function(h){return("000000000"+h.toString(2)).substr(-8)};let f;f=""+d(this.data[this.offset+7]);f+=d(this.data[this.offset+6]);f+=d(this.data[this.offset+5]);f+=d(this.data[this.offset+4]);f+=d(this.data[this.offset+3]);f+=d(this.data[this.offset+2]);f+=d(this.data[this.offset+1]);f+=d(this.data[this.offset]);d=parseInt(f, 2)}this.offset+=8;return d}reset(d){this.length=this.offset=0;this.data=d}abToStr(d,f){let h="";for(let r=0;r<f;r++)h+=String.fromCharCode(d[r]);return h}parse(d){if(!(d instanceof Uint8Array))throw"The input type must be a Uint8Array";this.reset(d);this.length=this.readInt32();this.length-=this.offset;return this.parseItem()}parseItem(){let d={};for(;this.offset<this.length-1;){var f=this.readByte();if(2==f){f=this.parseKey();var h=this.parseString();d[f]=h}else if(4==f){f=this.parseKey();h=(new G).parse(new Uint8Array(this.data.buffer.slice(this.offset))); this.offset+=this.readInt32();let r=[];for(let K in h)r.push(h[K]);h=r;d[f]=h}else if(5==f)f=this.parseKey(),h=this.parseBinary(),d[f]=h;else if(8==f)f=this.parseKey(),h=1==this.readByte(),d[f]=h;else if(10==f)f=this.parseKey(),d[f]=null;else if(16==f)f=this.parseKey(),h=this.readInt32(),d[f]=h;else if(18==f)f=this.parseKey(),h=this.readInt64(),d[f]=h;else{if(0==f)break;throw"Unrecognized data type 0x"+f.toString(16);}}return d}parseKey(){let d="";for(let f=0;256>f;f++){const h=this.readByte();if(0== h)break;d+=String.fromCharCode(h)}return d}parseString(){const d=this.readInt32(),f=new Uint8Array(this.data.buffer,this.offset,d);this.offset+=d;return this.abToStr(f,d-1)}parseBinary(){const d=this.readInt32();this.offset++;const f=new Uint8Array(this.data.buffer,this.offset,d);this.offset+=d;return f}}class n{constructor(){this.buffer=[];this.offset=0}writeByte(d){this.buffer.push(d&255)}writeBytes(d){const f=d.byteLength;for(let h=0;h<f;h++)this.writeByte(d[h])}writeInt32(d){this.buffer.push(d); this.buffer.push(d>>8);this.buffer.push(d>>16);this.buffer.push(d>>24)}writeKey(d){const f=d.length;for(let h=0;h<f;h++)this.writeByte(d.charCodeAt(h));this.writeByte(0)}writeString(d){const f=d.length;this.writeInt32(f+1);for(let h=0;h<f;h++)this.writeByte(d.charCodeAt(h));this.writeByte(0)}writeBinary(d){this.writeInt32(d.byteLength);this.writeByte(0);this.writeBytes(d)}pack(){const d=this.buffer.length;var f=d+5;let h=new Uint8Array(f),r=0;h[r++]=f>>0&255;h[r++]=f>>8&255;h[r++]=f>>16&255;h[r++]= f>>24&255;for(f=0;f<d;f++)h[r++]=this.buffer[f];h[r++]=0;return h}encode(d){for(let f in d)this.encodeItem(f,d[f]);return this.pack()}encodeItem(d,f){if("string"==typeof f)this.writeByte(2),this.writeKey(d),this.writeString(f);else if(f instanceof ArrayBuffer||f.buffer instanceof ArrayBuffer)this.writeByte(5),this.writeKey(d),this.writeBinary(f);else if(null==f||"undefined"==typeof f)this.writeByte(10),this.writeKey(d);else if(f instanceof Array)this.writeByte(4),this.writeKey(d),this.writeBytes((new n).encode(f)); else if("object"==typeof f)this.writeByte(3),this.writeKey(d),this.writeBytes((new n).encode(f));else if("number"==typeof f)this.writeByte(16),this.writeKey(d),this.writeInt32(f);else if("boolean"==typeof f)this.writeByte(8),this.writeKey(d),1==f?this.writeByte(1):this.writeByte(0);else throw"Unrecognized encode data type";}}return J});