@speckle/objectloader
Version:
Simple API helper to stream in objects from the Speckle Server.
2 lines (1 loc) • 16.6 kB
JavaScript
import"core-js/modules/es.symbol.description.js";import"core-js/modules/es.array.flat.js";import"core-js/modules/es.array.flat-map.js";import"core-js/modules/es.array.includes.js";import"core-js/modules/es.array.reduce.js";import"core-js/modules/es.array.reduce-right.js";import"core-js/modules/es.array.sort.js";import"core-js/modules/es.array.unscopables.flat.js";import"core-js/modules/es.array.unscopables.flat-map.js";import"core-js/modules/es.math.hypot.js";import"core-js/modules/es.object.from-entries.js";import"core-js/modules/es.promise.js";import"core-js/modules/es.promise.finally.js";import"core-js/modules/es.regexp.constructor.js";import"core-js/modules/es.regexp.exec.js";import"core-js/modules/es.regexp.flags.js";import"core-js/modules/es.string.replace.js";import"core-js/modules/es.typed-array.float32-array.js";import"core-js/modules/es.typed-array.float64-array.js";import"core-js/modules/es.typed-array.int8-array.js";import"core-js/modules/es.typed-array.int16-array.js";import"core-js/modules/es.typed-array.int32-array.js";import"core-js/modules/es.typed-array.uint8-array.js";import"core-js/modules/es.typed-array.uint8-clamped-array.js";import"core-js/modules/es.typed-array.uint16-array.js";import"core-js/modules/es.typed-array.uint32-array.js";import"core-js/modules/es.typed-array.from.js";import"core-js/modules/es.typed-array.of.js";import"core-js/modules/es.typed-array.set.js";import"core-js/modules/es.typed-array.sort.js";import"core-js/modules/esnext.aggregate-error.js";import"core-js/modules/esnext.array.last-index.js";import"core-js/modules/esnext.array.last-item.js";import"core-js/modules/esnext.composite-key.js";import"core-js/modules/esnext.composite-symbol.js";import"core-js/modules/esnext.global-this.js";import"core-js/modules/esnext.map.delete-all.js";import"core-js/modules/esnext.map.every.js";import"core-js/modules/esnext.map.filter.js";import"core-js/modules/esnext.map.find.js";import"core-js/modules/esnext.map.find-key.js";import"core-js/modules/esnext.map.from.js";import"core-js/modules/esnext.map.group-by.js";import"core-js/modules/esnext.map.includes.js";import"core-js/modules/esnext.map.key-by.js";import"core-js/modules/esnext.map.key-of.js";import"core-js/modules/esnext.map.map-keys.js";import"core-js/modules/esnext.map.map-values.js";import"core-js/modules/esnext.map.merge.js";import"core-js/modules/esnext.map.of.js";import"core-js/modules/esnext.map.reduce.js";import"core-js/modules/esnext.map.some.js";import"core-js/modules/esnext.map.update.js";import"core-js/modules/esnext.math.clamp.js";import"core-js/modules/esnext.math.deg-per-rad.js";import"core-js/modules/esnext.math.degrees.js";import"core-js/modules/esnext.math.fscale.js";import"core-js/modules/esnext.math.iaddh.js";import"core-js/modules/esnext.math.imulh.js";import"core-js/modules/esnext.math.isubh.js";import"core-js/modules/esnext.math.rad-per-deg.js";import"core-js/modules/esnext.math.radians.js";import"core-js/modules/esnext.math.scale.js";import"core-js/modules/esnext.math.seeded-prng.js";import"core-js/modules/esnext.math.signbit.js";import"core-js/modules/esnext.math.umulh.js";import"core-js/modules/esnext.number.from-string.js";import"core-js/modules/esnext.observable.js";import"core-js/modules/esnext.promise.all-settled.js";import"core-js/modules/esnext.promise.any.js";import"core-js/modules/esnext.promise.try.js";import"core-js/modules/esnext.reflect.define-metadata.js";import"core-js/modules/esnext.reflect.delete-metadata.js";import"core-js/modules/esnext.reflect.get-metadata.js";import"core-js/modules/esnext.reflect.get-metadata-keys.js";import"core-js/modules/esnext.reflect.get-own-metadata.js";import"core-js/modules/esnext.reflect.get-own-metadata-keys.js";import"core-js/modules/esnext.reflect.has-metadata.js";import"core-js/modules/esnext.reflect.has-own-metadata.js";import"core-js/modules/esnext.reflect.metadata.js";import"core-js/modules/esnext.set.add-all.js";import"core-js/modules/esnext.set.delete-all.js";import"core-js/modules/esnext.set.difference.js";import"core-js/modules/esnext.set.every.js";import"core-js/modules/esnext.set.filter.js";import"core-js/modules/esnext.set.find.js";import"core-js/modules/esnext.set.from.js";import"core-js/modules/esnext.set.intersection.js";import"core-js/modules/esnext.set.is-disjoint-from.js";import"core-js/modules/esnext.set.is-subset-of.js";import"core-js/modules/esnext.set.is-superset-of.js";import"core-js/modules/esnext.set.join.js";import"core-js/modules/esnext.set.map.js";import"core-js/modules/esnext.set.of.js";import"core-js/modules/esnext.set.reduce.js";import"core-js/modules/esnext.set.some.js";import"core-js/modules/esnext.set.symmetric-difference.js";import"core-js/modules/esnext.set.union.js";import"core-js/modules/esnext.string.at.js";import"core-js/modules/esnext.string.code-points.js";import"core-js/modules/esnext.string.match-all.js";import"core-js/modules/esnext.string.replace-all.js";import"core-js/modules/esnext.symbol.dispose.js";import"core-js/modules/esnext.symbol.observable.js";import"core-js/modules/esnext.symbol.pattern-match.js";import"core-js/modules/esnext.weak-map.delete-all.js";import"core-js/modules/esnext.weak-map.from.js";import"core-js/modules/esnext.weak-map.of.js";import"core-js/modules/esnext.weak-set.add-all.js";import"core-js/modules/esnext.weak-set.delete-all.js";import"core-js/modules/esnext.weak-set.from.js";import"core-js/modules/esnext.weak-set.of.js";import"core-js/modules/web.dom-collections.iterator.js";import"core-js/modules/web.immediate.js";import"core-js/modules/web.queue-microtask.js";import"core-js/modules/web.url.js";import"core-js/modules/web.url.to-json.js";import"core-js/modules/web.url-search-params.js";import{chunk as e,isString as t}from"#lodash";function s(e,t,s){return t in e?Object.defineProperty(e,t,{value:s,enumerable:!0,configurable:!0,writable:!0}):e[t]=s,e}class o extends Error{constructor(e,t){e||(e=new.target.defaultMessage),super(e,t)}}s(o,"defaultMessage","Unexpected error occurred");class r extends o{}s(r,"defaultMessage","Object loader configured incorrectly!");class i extends o{}s(i,"defaultMessage","Object loader encountered a runtime problem!");class a{constructor(e){var t;let{serverUrl:s,streamId:o,token:a,objectId:n,options:c={enableCaching:!0,fullyTraverseArrays:!1,excludeProps:[],fetch:null,customLogger:void 0,customWarner:void 0}}=e;if(this.logger=c.customLogger||console.log,this.warner=c.customWarner||console.warn,this.INTERVAL_MS=20,this.TIMEOUT_MS=18e4,this.serverUrl=s||(null===globalThis||void 0===globalThis||null===(t=globalThis.location)||void 0===t?void 0:t.origin),!this.serverUrl)throw new r("Invalid serverUrl specified!");if(this.streamId=o,this.objectId=n,!this.streamId)throw new r("Invalid streamId specified!");if(!this.objectId)throw new r("Invalid objectId specified!");this.logger("Object loader constructor called!"),this.token=a,this.headers={Accept:"text/plain"},this.token&&(this.headers.Authorization="Bearer ".concat(this.token)),this.requestUrlRootObj="".concat(this.serverUrl,"/objects/").concat(this.streamId,"/").concat(this.objectId,"/single"),this.requestUrlChildren="".concat(this.serverUrl,"/api/getobjects/").concat(this.streamId),this.promises=[],this.intervals={},this.buffer=[],this.isLoading=!1,this.totalChildrenCount=0,this.traversedReferencesCount=0,this.options=c,this.options.numConnections=this.options.numConnections||4,this.cacheDB=null,this.lastAsyncPause=Date.now(),this.existingAsyncPause=null,this.preferredFetch=c.fetch,this.fetch=function(){const e=this.preferredFetch||fetch;if(!e)throw new i("Couldn't find fetch implementation! If running in a node environment, make sure you pass it in through the constructor!");return e(...arguments)}}static createFromObjects(e){const t=e[0];return new class extends a{constructor(){super({serverUrl:"dummy",streamId:"dummy",undefined:void 0,objectId:t.id}),this.objectId=t.id}async getRootObject(){return t}async getTotalObjectCount(){return Object.keys((null==t?void 0:t.__closure)||{}).length}async*getObjectIterator(){const t=Date.now();let s=0;for await(const{id:t,obj:o}of this.getRawObjectIterator(e))this.buffer[t]=o,s+=1,yield o;this.logger("Loaded ".concat(s," objects in: ").concat((Date.now()-t)/1e3))}async*getRawObjectIterator(e){yield{id:e[0].id,obj:e[0]};if(e[0].__closure)for(const t of e)yield{id:t.id,obj:t}}}}static createFromJSON(e){const t=performance.now(),s=JSON.parse(e);return console.warn("JSON Parse Time -> ",performance.now()-t),this.createFromObjects(s)}async asyncPause(){Date.now()-this.lastAsyncPause>=100&&(this.lastAsyncPause=Date.now(),this.existingAsyncPause=new Promise((e=>setTimeout(e,0))),await this.existingAsyncPause,this.existingAsyncPause=null,Date.now()-this.lastAsyncPause>500&&this.logger("Loader Event loop lag: ",Date.now()-this.lastAsyncPause))}dispose(){this.buffer=[],this.promises=[],Object.values(this.intervals).forEach((e=>clearInterval(e.interval)))}async getTotalObjectCount(){const e=await this.getRootObject();return Object.keys((null==e?void 0:e.__closure)||{}).length}async getRootObject(){const e=await this.getRawRootObject();let t;try{t=JSON.parse(e)}catch(t){throw new Error('Error parsing root object. "'.concat(t.message,"\". Root object: '").concat(e,"'"))}return t}async getAndConstructObject(e){await this.downloadObjectsInBuffer(e);const t=await this.getObject(this.objectId);return this.traverseAndConstruct(t,e)}async downloadObjectsInBuffer(e){let t=!0,s=0;for await(const o of this.getObjectIterator())t&&(this.totalChildrenCount=o.totalChildrenCount,t=!1,this.isLoading=!0),s++,e&&e({stage:"download",current:s,total:this.totalChildrenCount});this.isLoading=!1}async traverseAndConstruct(e,t){if(e){if("object"!=typeof e)return e;if(Array.isArray(e)&&0!==e.length){var s,o;const r=[];for(const s of e){if(!s)continue;if("object"!=typeof s&&!this.options.fullyTraverseArrays)return e;const o=s.referencedId?await this.getObject(s.referencedId):s;s.referencedId&&t&&t({stage:"construction",current:++this.traversedReferencesCount>this.totalChildrenCount?this.totalChildrenCount:this.traversedReferencesCount,total:this.totalChildrenCount}),r.push(await this.traverseAndConstruct(o,t))}return null!==(s=r[0])&&void 0!==s&&null!==(o=s.speckle_type)&&void 0!==o&&o.toLowerCase().includes("datachunk")?r.reduce(((e,t)=>e.concat(t.data)),[]):r}for(const t of this.options.excludeProps)delete e[t];for(const s in e)"object"==typeof e[s]&&null!==e[s]&&(e[s].referencedId&&(e[s]=await this.getObject(e[s].referencedId),t&&t({stage:"construction",current:++this.traversedReferencesCount>this.totalChildrenCount?this.totalChildrenCount:this.traversedReferencesCount,total:this.totalChildrenCount})),e[s]=await this.traverseAndConstruct(e[s],t));return e}}async getObject(e){if(this.buffer[e])return this.buffer[e];return new Promise(((t,s)=>{if(this.promises.push({id:e,resolve:t,reject:s}),this.intervals[e])this.intervals[e].elapsed=0;else{const t=setInterval(this.tryResolvePromise.bind(this),this.INTERVAL_MS,e);this.intervals[e]={interval:t,elapsed:0}}}))}tryResolvePromise(e){if(this.intervals[e].elapsed+=this.INTERVAL_MS,this.buffer[e]){for(const t of this.promises.filter((t=>t.id===e)))t.resolve(this.buffer[e]);return clearInterval(this.intervals[e].interval),void delete this.intervals[e]}this.intervals[e].elapsed>this.TIMEOUT_MS&&(this.warner("Timeout resolving ".concat(e,". HIC SVNT DRACONES.")),clearInterval(this.intervals[e].interval),this.promises.filter((t=>t.id===e)).forEach((e=>e.reject())),this.promises=this.promises.filter((e=>e.id!=e.id)))}async*getObjectIterator(){const e=Date.now();let t=0;for await(const e of this.getRawObjectIterator()){const{id:s,obj:o}=this.processLine(e);this.buffer[s]=o,t+=1,yield o}this.logger("Loaded ".concat(t," objects in: ").concat((Date.now()-e)/1e3))}processLine(e){const t=e.split("\t"),[s,o]=t;let r;try{r=JSON.parse(o)}catch(e){throw new Error("Error parsing object ".concat(s,": ").concat(e.message))}return{id:s,obj:r}}supportsCache(){return!(!this.options.enableCaching||!globalThis.indexedDB)}async setupCacheDb(){if(!this.supportsCache()||null!==this.cacheDB)return;await function(){if(navigator.userAgentData||!/Safari\//.test(navigator.userAgent)||/Chrom(e|ium)\//.test(navigator.userAgent)||!indexedDB.databases)return Promise.resolve();let e;return new Promise((t=>{const s=()=>indexedDB.databases().finally(t);e=setInterval(s,100),s()})).finally((()=>clearInterval(e)))}();const e=indexedDB.open("speckle-object-cache",1);e.onupgradeneeded=()=>e.result.createObjectStore("objects"),this.cacheDB=await this.promisifyIdbRequest(e)}async*getRawObjectIterator(){await this.setupCacheDb();const t=await this.getRawRootObject();yield"".concat(this.objectId,"\t").concat(t);const s=JSON.parse(t);if(!s.__closure)return;let o=Object.keys(s.__closure).filter((e=>!e.includes("blob"))).sort(((e,t)=>s.__closure[e]-s.__closure[t]));if(0===o.length)return;let r=[];if(o.length>50){const t=[[],[],[],[]];let i=0;for(;i<.05*o.length;i++)t[0].push(o[i]);for(;i<.2*o.length;i++)t[1].push(o[i]);for(;i<.6*o.length;i++)t[2].push(o[i]);for(;i<o.length;i++)t[3].push(o[i]);this.logger("Cache check for: ",t);const a=[];let n=this.cacheGetObjects(t[0]);for(let o=0;o<4;o++){const r=await n;o<3&&(n=this.cacheGetObjects(t[o+1]));const i=Object.keys(r).sort(((e,t)=>s.__closure[e]-s.__closure[t]));for(const e of i)yield"".concat(e,"\t").concat(r[e]);const c=t[o].filter((e=>!(e in r))),l=e(c,500);for(let e=0;e<l.length;e++)a.push.apply(a,l[e])}if(0===a.length)return;if(a.length<=50)r.push(a);else{for(r=[[],[],[],[]],i=0;i<.05*a.length;i++)r[0].push(a[i]);for(;i<.2*a.length;i++)r[1].push(a[i]);for(;i<.6*a.length;i++)r[2].push(a[i]);for(;i<a.length;i++)r[3].push(a[i])}}else{const e=await this.cacheGetObjects(o),t=Object.keys(e).sort(((e,t)=>s.__closure[e]-s.__closure[t]));for(const s of t)yield"".concat(s,"\t").concat(e[s]);if(o=o.filter((t=>!(t in e))),0===o.length)return;r.push(o)}const i=[],a=[],n=[],c=[],l=[];for(let e=0;e<r.length;e++)i.push(new TextDecoder),a.push(null),n.push(null),c.push(""),l.push(!1),this.fetch(this.requestUrlChildren,{method:"POST",headers:{...this.headers,"Content-Type":"application/json"},body:JSON.stringify({objects:JSON.stringify(r[e])})}).then((t=>{t.body.getReader&&(t.body.iterator=async function*(){const e=this.getReader();for(;;){const t=await e.read();if(t.done)return t.value;yield t.value}});const s=t.body.iterator();a[e]=s;const o=s.next().then((t=>(t.reqId=e,t)));n[e]=o}));for(;;){const e=n.filter((e=>!!e));if(0===e.length){if(l.every((e=>e)))break;await new Promise((e=>{setTimeout(e,10)}));continue}const t=await Promise.any(e);let{value:s,done:o,reqId:r}=t;if(l[r]=o,o)c[r].length>0&&(yield c[r],c[r]=""),n[r]=null;else{const e=a[r].next().then((e=>(e.reqId=r,e)));n[r]=e}if(!s)continue;s=i[r].decode(s);const m=(c[r]+s).split(/\r\n|\n|\r/),d=m.pop();c[r]=d;for(const e of m)yield e;this.cacheStoreObjects(m)}}async getRawRootObject(){const e=await this.cacheGetObjects([this.objectId]);if(e[this.objectId])return e[this.objectId];const t=await this.fetch(this.requestUrlRootObj,{headers:this.headers});if(!t.ok){if([401,403].includes(t.status))throw new i("You do not have access to the root object! Object URI: '".concat(this.requestUrlRootObj,"', Token ID: '").concat(this.token.substring(0,10),"'. Response: '").concat(t.status," ").concat(t.statusText,"'"));throw new i("Failed to fetch root object. Object URI: '".concat(this.requestUrlRootObj,"'. Response: '").concat(t.status," ").concat(t.statusText,"'"))}const s=await t.text();return this.cacheStoreObjects(["".concat(this.objectId,"\t").concat(s)]),s}promisifyIdbRequest(e){return new Promise(((t,s)=>{e.oncomplete=e.onsuccess=()=>t(e.result),e.onabort=e.onerror=()=>s(e.error)}))}async cacheGetObjects(e){if(!this.supportsCache())return{};null===this.cacheDB&&await this.setupCacheDb();const s={};for(let o=0;o<e.length;o+=500){const r=e.slice(o,o+500),i=this.cacheDB.transaction("objects","readonly").objectStore("objects"),a=r.map((e=>this.promisifyIdbRequest(i.get(e)).then((t=>({id:e,data:t}))))),n=await Promise.all(a);for(const e of n)!e.data||t(e.data)&&e.data.startsWith("<html")||(s[e.id]=e.data)}return s}async cacheStoreObjects(e){if(!this.supportsCache())return{};null===this.cacheDB&&await this.setupCacheDb();try{const s=this.cacheDB.transaction("objects","readwrite").objectStore("objects");for(const o of e){const[e,r]=o.split("\t");r&&t(r)&&!r.startsWith("<html")&&s.put(r,e)}return this.promisifyIdbRequest(s.transaction)}catch(e){this.logger.error(e)}return Promise.resolve()}}export{a as default};