"use strict";var mt=Object.defineProperty;var it=t=>{throw TypeError(t)};var wt=(t,e,r)=>e in t?mt(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r;var S=(t,e,r)=>wt(t,typeof e!="symbol"?e+"":e,r),X=(t,e,r)=>e.has(t)||it("Cannot "+r);var T=(t,e,r)=>(X(t,e,"read from private field"),r?r.call(t):e.get(t)),j=(t,e,r)=>e.has(t)?it("Cannot add the same private member more than once"):e instanceof WeakSet?e.add(t):e.set(t,r),b=(t,e,r,a)=>(X(t,e,"write to private field"),a?a.call(t,r):e.set(t,r),r),y=(t,e,r)=>(X(t,e,"access private method"),r);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});require("reflect-metadata");const st=require("@speckle/shared");function Y(t){function e(k,_){return k<<_|k>>>32-_}function r(k){let _="",z,w;for(z=7;z>=0;z--)w=k>>>z*4&15,_+=w.toString(16);return _}function a(k){k=k.replace(/\r\n/g,` `);let _="";for(let z=0;z127&&w<2048?(_+=String.fromCharCode(w>>6|192),_+=String.fromCharCode(w&63|128)):(_+=String.fromCharCode(w>>12|224),_+=String.fromCharCode(w>>6&63|128),_+=String.fromCharCode(w&63|128))}return _}let i,n,g;const d=new Array(80);let O=1732584193,P=4023233417,o=2562383102,l=271733878,v=3285377520,f,s,c,u,p,C;t=a(t);const m=t.length,$=[];for(n=0;n>>29),$.push(m<<3&4294967295),i=0;i<$.length;i+=16){for(n=0;n<16;n++)d[n]=$[i+n];for(n=16;n<=79;n++)d[n]=e(d[n-3]^d[n-8]^d[n-14]^d[n-16],1);for(f=O,s=P,c=o,u=l,p=v,n=0;n<=19;n++)C=e(f,5)+(s&c|~s&u)+p+d[n]+1518500249&4294967295,p=u,u=c,c=e(s,30),s=f,f=C;for(n=20;n<=39;n++)C=e(f,5)+(s^c^u)+p+d[n]+1859775393&4294967295,p=u,u=c,c=e(s,30),s=f,f=C;for(n=40;n<=59;n++)C=e(f,5)+(s&c|s&u|c&u)+p+d[n]+2400959708&4294967295,p=u,u=c,c=e(s,30),s=f,f=C;for(n=60;n<=79;n++)C=e(f,5)+(s^c^u)+p+d[n]+3395469782&4294967295,p=u,u=c,c=e(s,30),s=f,f=C;O=O+f&4294967295,P=P+s&4294967295,o=o+c&4294967295,l=l+u&4294967295,v=v+p&4294967295}return(r(O)+r(P)+r(o)+r(l)+r(v)).toLowerCase().substring(0,40)}var St=typeof global=="object"&&global&&global.Object===Object&&global,Tt=typeof self=="object"&&self&&self.Object===Object&&self,tt=St||Tt||Function("return this")(),D=tt.Symbol,ft=Object.prototype,vt=ft.hasOwnProperty,jt=ft.toString,M=D?D.toStringTag:void 0;function Ot(t){var e=vt.call(t,M),r=t[M];try{t[M]=void 0;var a=!0}catch{}var i=jt.call(t);return a&&(e?t[M]=r:delete t[M]),i}var kt=Object.prototype,zt=kt.toString;function At(t){return zt.call(t)}var Pt="[object Null]",$t="[object Undefined]",ot=D?D.toStringTag:void 0;function dt(t){return t==null?t===void 0?$t:Pt:ot&&ot in Object(t)?Ot(t):At(t)}function pt(t){return t!=null&&typeof t=="object"}var It="[object Symbol]";function et(t){return typeof t=="symbol"||pt(t)&&dt(t)==It}function xt(t,e){for(var r=-1,a=t==null?0:t.length,i=Array(a);++r-1}function me(t,e){var r=this.__data__,a=J(r,t);return a<0?(++this.size,r.push([t,e])):r[a][1]=e,this}function F(t){var e=-1,r=t==null?0:t.length;for(this.clear();++ept(t)&&!!Le(t,"speckle_type");var h,R,L,W,Q,Ct;class Be{constructor(e,r=1e3,a=Y){j(this,h);S(this,"chunkSize");S(this,"detachLineage");S(this,"lineage");S(this,"familyTree");S(this,"closureTable");S(this,"transport");S(this,"uniqueId");S(this,"hashingFunction");this.chunkSize=r,this.detachLineage=[!0],this.lineage=[],this.familyTree={},this.closureTable={},this.transport=e,this.uniqueId=0,this.hashingFunction=a||Y}async write(e){return await y(this,h,R).call(this,e,!0)}dispose(){this.detachLineage=[],this.lineage=[],this.familyTree={},this.closureTable={},this.transport=null}}h=new WeakSet,R=async function(e,r){const a=`${this.uniqueId++}-obj`;this.lineage.push(a);const i={speckle_type:e.speckle_type||"Base"};for(const o in e){const l=e[o];if(l===void 0||o==="id"||o.startsWith("_"))continue;if(typeof l!="object"){i[o]=l;continue}const v=o.startsWith("@")||Ue(e,o),f=Array.isArray(l),s=f?Ge(e,o)||o.match(/^@\((\d*)\)/):!1;if(f&&s&&l.length!==0&&typeof l[0]!="object"){let c=this.chunkSize;typeof s=="boolean"?c=Ke(e,o):c=s[1]!==""?parseInt(s[1]):this.chunkSize;const u=[];let p=new lt,C=0;for(const m of l)C===c&&(u.push(await y(this,h,Q).call(this,p)),p=new lt,C=0),p.data.push(m),C++;p.data.length!==0&&u.push(await y(this,h,Q).call(this,p)),typeof s=="boolean"?i[o]=u:i[o.replace(s[0],"")]=u;continue}if(l.speckle_type){const c=await y(this,h,L).call(this,{value:l,isDetached:v});i[o]=v?y(this,h,W).call(this,c.id):c;continue}i[o]=await y(this,h,L).call(this,{value:l,isDetached:v})}const n=this.detachLineage.pop(),g=this.lineage.pop();if(this.familyTree[g]){const o={};Object.entries(this.familyTree[g]).forEach(([l,v])=>{o[l]=v-this.detachLineage.length}),i.totalChildrenCount=Object.keys(o).length,i.totalChildrenCount&&(i.__closure=o)}const{hash:d,serializedObject:O,size:P}=y(this,h,Ct).call(this,i);return i.id=d,(n||r)&&this.transport&&await this.transport.write(O,P),r&&this.transport&&await this.transport.flush(),{hash:d,traversed:i}},L=async function({value:e,isDetached:r=!1}){if(typeof e!="object")return e;if(Array.isArray(e)){const a=e;if(a.length===0)return e;if(typeof a[0]!="object")return a;if(!r)return Promise.all(e.map(async n=>await y(this,h,L).call(this,{value:n})));const i=[];for(const n of e)if(qe(n)){this.detachLineage.push(r);const{hash:g}=await y(this,h,R).call(this,n,!1);i.push(y(this,h,W).call(this,g))}else i.push(await y(this,h,L).call(this,{value:n,isDetached:r}));return i}if(!e.speckle_type)return e;if(e.speckle_type)return this.detachLineage.push(r),(await y(this,h,R).call(this,e,!1)).traversed;throw new Error(`Unsupported type '${typeof e}': ${e}.`)},W=function(e){return this.lineage.forEach(r=>{this.familyTree[r]||(this.familyTree[r]={}),(!this.familyTree[r][e]||this.familyTree[r][e]>this.detachLineage.length)&&(this.familyTree[r][e]=this.detachLineage.length)}),{referencedId:e,speckle_type:"reference"}},Q=async function(e){this.detachLineage.push(!0);const{hash:r}=await y(this,h,R).call(this,e,!1);return y(this,h,W).call(this,r)},Ct=function(e){const r=JSON.stringify(e),a=this.hashingFunction(r),i=r.substring(0,1)+`"id":"${a}",`+r.substring(1);return{hash:Y(r),serializedObject:i,size:r.length}};class lt{constructor(){S(this,"speckle_type");S(this,"data");this.data=[],this.speckle_type="Speckle.Core.Models.DataChunk"}}var A,H,I,U,G,K,q,B;class We{constructor(e,r,a,i){j(this,A);j(this,H);j(this,I);j(this,U);j(this,G);j(this,K);j(this,q);j(this,B);b(this,H,(i==null?void 0:i.maxSize)||2e5),b(this,q,(i==null?void 0:i.flushRetryCount)||3),b(this,B,(i==null?void 0:i.flushTimeout)||2*60*1e3),b(this,I,0),b(this,U,e),b(this,G,r),b(this,K,a),b(this,A,[])}async write(e,r){T(this,A).push(e),b(this,I,T(this,I)+r),!(T(this,I)await Promise.race([fetch(a,{method:"POST",headers:{Authorization:`Bearer ${T(this,K)}`},body:e}),st.timeoutAt(T(this,B),"Object sender flush timed out")]),T(this,q),n=>n*1e3);if(i.status!==201)throw new Error(`Unexpected error when sending data. Expected status 200, got ${i.status}`);b(this,A,[]),b(this,I,0)}dispose(){b(this,A,[])}}A=new WeakMap,H=new WeakMap,I=new WeakMap,U=new WeakMap,G=new WeakMap,K=new WeakMap,q=new WeakMap,B=new WeakMap;class Je{constructor(e){S(this,"speckle_type");if(this.speckle_type=this.constructor.name,e)for(const r in e)this[r]=e[r]}}const Ve=async(t,{serverUrl:e="https://app.speckle.systems",projectId:r,token:a,logger:i=console})=>{const n=performance.now();i==null||i.log("Starting to send");const g=new We(e,r,a),d=new Be(g);let O;try{O=await d.write(t)}finally{g.dispose(),d.dispose()}const P=performance.now();return i.log(`Finished sending in ${(P-n)/1e3}s.`),O};exports.Base=Je;exports.Chunkable=He;exports.Detach=Ne;exports.send=Ve; //# sourceMappingURL=objectsender.cjs.map