@speckle/objectloader 2.18.15 → 2.18.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/objectloader.esm.js +1 -1
- package/dist/objectloader.js +1 -1
- package/package.json +2 -2
- package/src/index.js +11 -6
- package/types/index.d.ts +1 -1
package/dist/objectloader.esm.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
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{SafeLocalStorage as e}from"@speckle/shared";import{chunk as s}from"lodash";function t(e,s,t){return s in e?Object.defineProperty(e,s,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[s]=t,e}class o extends Error{constructor(e,s){e||(e=new.target.defaultMessage),super(e,s)}}t(o,"defaultMessage","Unexpected error occurred");class r extends o{}t(r,"defaultMessage","Object loader configured incorrectly!");class i extends o{}t(i,"defaultMessage","Object loader encountered a runtime problem!");class n{constructor(s){var t;let{serverUrl:o,streamId:n,token:a,objectId:c,options:l={enableCaching:!0,fullyTraverseArrays:!1,excludeProps:[],fetch:null,customLogger:void 0,customWarner:void 0}}=s;if(this.logger=l.customLogger||console.log,this.warner=l.customWarner||console.warn,this.INTERVAL_MS=20,this.TIMEOUT_MS=18e4,this.serverUrl=o||(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=n,this.objectId=c,!this.streamId)throw new r("Invalid streamId specified!");if(!this.objectId)throw new r("Invalid objectId specified!");this.logger("Object loader constructor called!");try{this.token=a||e.get("AuthToken")}catch(e){}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=l,this.options.numConnections=this.options.numConnections||4,this.cacheDB=null,this.lastAsyncPause=Date.now(),this.existingAsyncPause=null,this.preferredFetch=l.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)}}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 getAndConstructObject(e){await this.downloadObjectsInBuffer(e);const s=await this.getObject(this.objectId);return this.traverseAndConstruct(s,e)}async downloadObjectsInBuffer(e){let s=!0,t=0;for await(const o of this.getObjectIterator())s&&(this.totalChildrenCount=o.totalChildrenCount,s=!1,this.isLoading=!0),t++,e&&e({stage:"download",current:t,total:this.totalChildrenCount});this.isLoading=!1}async traverseAndConstruct(e,s){if(e){if("object"!=typeof e)return e;if(Array.isArray(e)&&0!==e.length){var t,o;const r=[];for(const t of e){if(!t)continue;if("object"!=typeof t&&!this.options.fullyTraverseArrays)return e;const o=t.referencedId?await this.getObject(t.referencedId):t;t.referencedId&&s&&s({stage:"construction",current:++this.traversedReferencesCount>this.totalChildrenCount?this.totalChildrenCount:this.traversedReferencesCount,total:this.totalChildrenCount}),r.push(await this.traverseAndConstruct(o,s))}return null!==(t=r[0])&&void 0!==t&&null!==(o=t.speckle_type)&&void 0!==o&&o.toLowerCase().includes("datachunk")?r.reduce(((e,s)=>e.concat(s.data)),[]):r}for(const s of this.options.excludeProps)delete e[s];for(const t in e)"object"==typeof e[t]&&null!==e[t]&&(e[t].referencedId&&(e[t]=await this.getObject(e[t].referencedId),s&&s({stage:"construction",current:++this.traversedReferencesCount>this.totalChildrenCount?this.totalChildrenCount:this.traversedReferencesCount,total:this.totalChildrenCount})),e[t]=await this.traverseAndConstruct(e[t],s));return e}}async getObject(e){if(this.buffer[e])return this.buffer[e];return new Promise(((s,t)=>{if(this.promises.push({id:e,resolve:s,reject:t}),this.intervals[e])this.intervals[e].elapsed=0;else{const s=setInterval(this.tryResolvePromise.bind(this),this.INTERVAL_MS,e);this.intervals[e]={interval:s,elapsed:0}}}))}tryResolvePromise(e){if(this.intervals[e].elapsed+=this.INTERVAL_MS,this.buffer[e]){for(const s of this.promises.filter((s=>s.id===e)))s.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((s=>s.id===e)).forEach((e=>e.reject())),this.promises=this.promises.filter((e=>e.id!=e.id)))}async*getObjectIterator(){const e=Date.now();let s=0;for await(const e of this.getRawObjectIterator()){const{id:t,obj:o}=this.processLine(e);this.buffer[t]=o,s+=1,yield o}this.logger("Loaded ".concat(s," objects in: ").concat((Date.now()-e)/1e3))}processLine(e){const s=e.split("\t"),[t,o]=s;let r;try{r=JSON.parse(o)}catch(e){throw new Error("Error parsing object ".concat(t,": ").concat(e.message))}return{id:t,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((s=>{const t=()=>indexedDB.databases().finally(s);e=setInterval(t,100),t()})).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 e=await this.getRawRootObject();yield"".concat(this.objectId,"\t").concat(e);const t=JSON.parse(e);if(!t.__closure)return;let o=Object.keys(t.__closure).filter((e=>!e.includes("blob"))).sort(((e,s)=>t.__closure[e]-t.__closure[s]));if(0===o.length)return;let r=[];if(o.length>50){const e=[[],[],[],[]];let i=0;for(;i<.05*o.length;i++)e[0].push(o[i]);for(;i<.2*o.length;i++)e[1].push(o[i]);for(;i<.6*o.length;i++)e[2].push(o[i]);for(;i<o.length;i++)e[3].push(o[i]);this.logger("Cache check for: ",e);const n=[];let a=this.cacheGetObjects(e[0]);for(let o=0;o<4;o++){const r=await a;o<3&&(a=this.cacheGetObjects(e[o+1]));const i=Object.keys(r).sort(((e,s)=>t.__closure[e]-t.__closure[s]));for(const e of i)yield"".concat(e,"\t").concat(r[e]);const c=e[o].filter((e=>!(e in r))),l=s(c,500);for(let e=0;e<l.length;e++)n.push.apply(n,l[e])}if(0===n.length)return;if(n.length<=50)r.push(n);else{for(r=[[],[],[],[]],i=0;i<.05*n.length;i++)r[0].push(n[i]);for(;i<.2*n.length;i++)r[1].push(n[i]);for(;i<.6*n.length;i++)r[2].push(n[i]);for(;i<n.length;i++)r[3].push(n[i])}}else{const e=await this.cacheGetObjects(o),s=Object.keys(e).sort(((e,s)=>t.__closure[e]-t.__closure[s]));for(const t of s)yield"".concat(t,"\t").concat(e[t]);if(o=o.filter((s=>!(s in e))),0===o.length)return;r.push(o)}const i=[],n=[],a=[],c=[],l=[];for(let e=0;e<r.length;e++)i.push(new TextDecoder),n.push(null),a.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((s=>{s.body.getReader&&(s.body.iterator=async function*(){const e=this.getReader();for(;;){const s=await e.read();if(s.done)return s.value;yield s.value}});const t=s.body.iterator();n[e]=t;const o=t.next().then((s=>(s.reqId=e,s)));a[e]=o}));for(;;){const e=a.filter((e=>!!e));if(0===e.length){if(l.every((e=>e)))break;await new Promise((e=>{setTimeout(e,10)}));continue}const s=await Promise.any(e);let{value:t,done:o,reqId:r}=s;if(l[r]=o,o)c[r].length>0&&(yield c[r],c[r]=""),a[r]=null;else{const e=n[r].next().then((e=>(e.reqId=r,e)));a[r]=e}if(!t)continue;t=i[r].decode(t);const m=(c[r]+t).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 s=await this.fetch(this.requestUrlRootObj,{headers:this.headers}),t=await s.text();if([401,403].includes(s.status))throw new i("You do not have access to the root object!");return this.cacheStoreObjects(["".concat(this.objectId,"\t").concat(t)]),t}promisifyIdbRequest(e){return new Promise(((s,t)=>{e.oncomplete=e.onsuccess=()=>s(e.result),e.onabort=e.onerror=()=>t(e.error)}))}async cacheGetObjects(e){if(!this.supportsCache())return{};const s={};for(let t=0;t<e.length;t+=500){const o=e.slice(t,t+500),r=this.cacheDB.transaction("objects","readonly").objectStore("objects"),i=o.map((e=>this.promisifyIdbRequest(r.get(e)).then((s=>({id:e,data:s}))))),n=await Promise.all(i);for(const e of n)e.data&&(s[e.id]=e.data)}return s}cacheStoreObjects(e){if(!this.supportsCache())return{};const s=this.cacheDB.transaction("objects","readwrite").objectStore("objects");for(const t of e){const e=t.split("\t");s.put(e[1],e[0])}return this.promisifyIdbRequest(s.transaction)}}export{n as default};
|
|
1
|
+
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}from"lodash";function s(e,s,t){return s in e?Object.defineProperty(e,s,{value:t,enumerable:!0,configurable:!0,writable:!0}):e[s]=t,e}class t extends Error{constructor(e,s){e||(e=new.target.defaultMessage),super(e,s)}}s(t,"defaultMessage","Unexpected error occurred");class o extends t{}s(o,"defaultMessage","Object loader configured incorrectly!");class r extends t{}s(r,"defaultMessage","Object loader encountered a runtime problem!");class i{constructor(e){var s;let{serverUrl:t,streamId:i,token:n,objectId:a,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=t||(null===globalThis||void 0===globalThis||null===(s=globalThis.location)||void 0===s?void 0:s.origin),!this.serverUrl)throw new o("Invalid serverUrl specified!");if(this.streamId=i,this.objectId=a,!this.streamId)throw new o("Invalid streamId specified!");if(!this.objectId)throw new o("Invalid objectId specified!");this.logger("Object loader constructor called!"),this.token=n,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 r("Couldn't find fetch implementation! If running in a node environment, make sure you pass it in through the constructor!");return e(...arguments)}}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 getAndConstructObject(e){await this.downloadObjectsInBuffer(e);const s=await this.getObject(this.objectId);return this.traverseAndConstruct(s,e)}async downloadObjectsInBuffer(e){let s=!0,t=0;for await(const o of this.getObjectIterator())s&&(this.totalChildrenCount=o.totalChildrenCount,s=!1,this.isLoading=!0),t++,e&&e({stage:"download",current:t,total:this.totalChildrenCount});this.isLoading=!1}async traverseAndConstruct(e,s){if(e){if("object"!=typeof e)return e;if(Array.isArray(e)&&0!==e.length){var t,o;const r=[];for(const t of e){if(!t)continue;if("object"!=typeof t&&!this.options.fullyTraverseArrays)return e;const o=t.referencedId?await this.getObject(t.referencedId):t;t.referencedId&&s&&s({stage:"construction",current:++this.traversedReferencesCount>this.totalChildrenCount?this.totalChildrenCount:this.traversedReferencesCount,total:this.totalChildrenCount}),r.push(await this.traverseAndConstruct(o,s))}return null!==(t=r[0])&&void 0!==t&&null!==(o=t.speckle_type)&&void 0!==o&&o.toLowerCase().includes("datachunk")?r.reduce(((e,s)=>e.concat(s.data)),[]):r}for(const s of this.options.excludeProps)delete e[s];for(const t in e)"object"==typeof e[t]&&null!==e[t]&&(e[t].referencedId&&(e[t]=await this.getObject(e[t].referencedId),s&&s({stage:"construction",current:++this.traversedReferencesCount>this.totalChildrenCount?this.totalChildrenCount:this.traversedReferencesCount,total:this.totalChildrenCount})),e[t]=await this.traverseAndConstruct(e[t],s));return e}}async getObject(e){if(this.buffer[e])return this.buffer[e];return new Promise(((s,t)=>{if(this.promises.push({id:e,resolve:s,reject:t}),this.intervals[e])this.intervals[e].elapsed=0;else{const s=setInterval(this.tryResolvePromise.bind(this),this.INTERVAL_MS,e);this.intervals[e]={interval:s,elapsed:0}}}))}tryResolvePromise(e){if(this.intervals[e].elapsed+=this.INTERVAL_MS,this.buffer[e]){for(const s of this.promises.filter((s=>s.id===e)))s.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((s=>s.id===e)).forEach((e=>e.reject())),this.promises=this.promises.filter((e=>e.id!=e.id)))}async*getObjectIterator(){const e=Date.now();let s=0;for await(const e of this.getRawObjectIterator()){const{id:t,obj:o}=this.processLine(e);this.buffer[t]=o,s+=1,yield o}this.logger("Loaded ".concat(s," objects in: ").concat((Date.now()-e)/1e3))}processLine(e){const s=e.split("\t"),[t,o]=s;let r;try{r=JSON.parse(o)}catch(e){throw new Error("Error parsing object ".concat(t,": ").concat(e.message))}return{id:t,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((s=>{const t=()=>indexedDB.databases().finally(s);e=setInterval(t,100),t()})).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 s=await this.getRawRootObject();yield"".concat(this.objectId,"\t").concat(s);const t=JSON.parse(s);if(!t.__closure)return;let o=Object.keys(t.__closure).filter((e=>!e.includes("blob"))).sort(((e,s)=>t.__closure[e]-t.__closure[s]));if(0===o.length)return;let r=[];if(o.length>50){const s=[[],[],[],[]];let i=0;for(;i<.05*o.length;i++)s[0].push(o[i]);for(;i<.2*o.length;i++)s[1].push(o[i]);for(;i<.6*o.length;i++)s[2].push(o[i]);for(;i<o.length;i++)s[3].push(o[i]);this.logger("Cache check for: ",s);const n=[];let a=this.cacheGetObjects(s[0]);for(let o=0;o<4;o++){const r=await a;o<3&&(a=this.cacheGetObjects(s[o+1]));const i=Object.keys(r).sort(((e,s)=>t.__closure[e]-t.__closure[s]));for(const e of i)yield"".concat(e,"\t").concat(r[e]);const c=s[o].filter((e=>!(e in r))),l=e(c,500);for(let e=0;e<l.length;e++)n.push.apply(n,l[e])}if(0===n.length)return;if(n.length<=50)r.push(n);else{for(r=[[],[],[],[]],i=0;i<.05*n.length;i++)r[0].push(n[i]);for(;i<.2*n.length;i++)r[1].push(n[i]);for(;i<.6*n.length;i++)r[2].push(n[i]);for(;i<n.length;i++)r[3].push(n[i])}}else{const e=await this.cacheGetObjects(o),s=Object.keys(e).sort(((e,s)=>t.__closure[e]-t.__closure[s]));for(const t of s)yield"".concat(t,"\t").concat(e[t]);if(o=o.filter((s=>!(s in e))),0===o.length)return;r.push(o)}const i=[],n=[],a=[],c=[],l=[];for(let e=0;e<r.length;e++)i.push(new TextDecoder),n.push(null),a.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((s=>{s.body.getReader&&(s.body.iterator=async function*(){const e=this.getReader();for(;;){const s=await e.read();if(s.done)return s.value;yield s.value}});const t=s.body.iterator();n[e]=t;const o=t.next().then((s=>(s.reqId=e,s)));a[e]=o}));for(;;){const e=a.filter((e=>!!e));if(0===e.length){if(l.every((e=>e)))break;await new Promise((e=>{setTimeout(e,10)}));continue}const s=await Promise.any(e);let{value:t,done:o,reqId:r}=s;if(l[r]=o,o)c[r].length>0&&(yield c[r],c[r]=""),a[r]=null;else{const e=n[r].next().then((e=>(e.reqId=r,e)));a[r]=e}if(!t)continue;t=i[r].decode(t);const m=(c[r]+t).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 s=await this.fetch(this.requestUrlRootObj,{headers:this.headers}),t=await s.text();if([401,403].includes(s.status))throw new r("You do not have access to the root object!");return this.cacheStoreObjects(["".concat(this.objectId,"\t").concat(t)]),t}promisifyIdbRequest(e){return new Promise(((s,t)=>{e.oncomplete=e.onsuccess=()=>s(e.result),e.onabort=e.onerror=()=>t(e.error)}))}async cacheGetObjects(e){if(!this.supportsCache())return{};const s={};for(let t=0;t<e.length;t+=500){const o=e.slice(t,t+500),r=this.cacheDB.transaction("objects","readonly").objectStore("objects"),i=o.map((e=>this.promisifyIdbRequest(r.get(e)).then((s=>({id:e,data:s}))))),n=await Promise.all(i);for(const e of n)e.data&&(s[e.id]=e.data)}return s}cacheStoreObjects(e){if(!this.supportsCache())return{};const s=this.cacheDB.transaction("objects","readwrite").objectStore("objects");for(const t of e){const e=t.split("\t");s.put(e[1],e[0])}return this.promisifyIdbRequest(s.transaction)}}export{i as default};
|
package/dist/objectloader.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";require("core-js/modules/es.symbol.description.js"),require("core-js/modules/es.array.flat.js"),require("core-js/modules/es.array.flat-map.js"),require("core-js/modules/es.array.includes.js"),require("core-js/modules/es.array.reduce.js"),require("core-js/modules/es.array.reduce-right.js"),require("core-js/modules/es.array.sort.js"),require("core-js/modules/es.array.unscopables.flat.js"),require("core-js/modules/es.array.unscopables.flat-map.js"),require("core-js/modules/es.math.hypot.js"),require("core-js/modules/es.object.from-entries.js"),require("core-js/modules/es.promise.js"),require("core-js/modules/es.promise.finally.js"),require("core-js/modules/es.regexp.constructor.js"),require("core-js/modules/es.regexp.exec.js"),require("core-js/modules/es.regexp.flags.js"),require("core-js/modules/es.string.replace.js"),require("core-js/modules/es.typed-array.float32-array.js"),require("core-js/modules/es.typed-array.float64-array.js"),require("core-js/modules/es.typed-array.int8-array.js"),require("core-js/modules/es.typed-array.int16-array.js"),require("core-js/modules/es.typed-array.int32-array.js"),require("core-js/modules/es.typed-array.uint8-array.js"),require("core-js/modules/es.typed-array.uint8-clamped-array.js"),require("core-js/modules/es.typed-array.uint16-array.js"),require("core-js/modules/es.typed-array.uint32-array.js"),require("core-js/modules/es.typed-array.from.js"),require("core-js/modules/es.typed-array.of.js"),require("core-js/modules/es.typed-array.set.js"),require("core-js/modules/es.typed-array.sort.js"),require("core-js/modules/esnext.aggregate-error.js"),require("core-js/modules/esnext.array.last-index.js"),require("core-js/modules/esnext.array.last-item.js"),require("core-js/modules/esnext.composite-key.js"),require("core-js/modules/esnext.composite-symbol.js"),require("core-js/modules/esnext.global-this.js"),require("core-js/modules/esnext.map.delete-all.js"),require("core-js/modules/esnext.map.every.js"),require("core-js/modules/esnext.map.filter.js"),require("core-js/modules/esnext.map.find.js"),require("core-js/modules/esnext.map.find-key.js"),require("core-js/modules/esnext.map.from.js"),require("core-js/modules/esnext.map.group-by.js"),require("core-js/modules/esnext.map.includes.js"),require("core-js/modules/esnext.map.key-by.js"),require("core-js/modules/esnext.map.key-of.js"),require("core-js/modules/esnext.map.map-keys.js"),require("core-js/modules/esnext.map.map-values.js"),require("core-js/modules/esnext.map.merge.js"),require("core-js/modules/esnext.map.of.js"),require("core-js/modules/esnext.map.reduce.js"),require("core-js/modules/esnext.map.some.js"),require("core-js/modules/esnext.map.update.js"),require("core-js/modules/esnext.math.clamp.js"),require("core-js/modules/esnext.math.deg-per-rad.js"),require("core-js/modules/esnext.math.degrees.js"),require("core-js/modules/esnext.math.fscale.js"),require("core-js/modules/esnext.math.iaddh.js"),require("core-js/modules/esnext.math.imulh.js"),require("core-js/modules/esnext.math.isubh.js"),require("core-js/modules/esnext.math.rad-per-deg.js"),require("core-js/modules/esnext.math.radians.js"),require("core-js/modules/esnext.math.scale.js"),require("core-js/modules/esnext.math.seeded-prng.js"),require("core-js/modules/esnext.math.signbit.js"),require("core-js/modules/esnext.math.umulh.js"),require("core-js/modules/esnext.number.from-string.js"),require("core-js/modules/esnext.observable.js"),require("core-js/modules/esnext.promise.all-settled.js"),require("core-js/modules/esnext.promise.any.js"),require("core-js/modules/esnext.promise.try.js"),require("core-js/modules/esnext.reflect.define-metadata.js"),require("core-js/modules/esnext.reflect.delete-metadata.js"),require("core-js/modules/esnext.reflect.get-metadata.js"),require("core-js/modules/esnext.reflect.get-metadata-keys.js"),require("core-js/modules/esnext.reflect.get-own-metadata.js"),require("core-js/modules/esnext.reflect.get-own-metadata-keys.js"),require("core-js/modules/esnext.reflect.has-metadata.js"),require("core-js/modules/esnext.reflect.has-own-metadata.js"),require("core-js/modules/esnext.reflect.metadata.js"),require("core-js/modules/esnext.set.add-all.js"),require("core-js/modules/esnext.set.delete-all.js"),require("core-js/modules/esnext.set.difference.js"),require("core-js/modules/esnext.set.every.js"),require("core-js/modules/esnext.set.filter.js"),require("core-js/modules/esnext.set.find.js"),require("core-js/modules/esnext.set.from.js"),require("core-js/modules/esnext.set.intersection.js"),require("core-js/modules/esnext.set.is-disjoint-from.js"),require("core-js/modules/esnext.set.is-subset-of.js"),require("core-js/modules/esnext.set.is-superset-of.js"),require("core-js/modules/esnext.set.join.js"),require("core-js/modules/esnext.set.map.js"),require("core-js/modules/esnext.set.of.js"),require("core-js/modules/esnext.set.reduce.js"),require("core-js/modules/esnext.set.some.js"),require("core-js/modules/esnext.set.symmetric-difference.js"),require("core-js/modules/esnext.set.union.js"),require("core-js/modules/esnext.string.at.js"),require("core-js/modules/esnext.string.code-points.js"),require("core-js/modules/esnext.string.match-all.js"),require("core-js/modules/esnext.string.replace-all.js"),require("core-js/modules/esnext.symbol.dispose.js"),require("core-js/modules/esnext.symbol.observable.js"),require("core-js/modules/esnext.symbol.pattern-match.js"),require("core-js/modules/esnext.weak-map.delete-all.js"),require("core-js/modules/esnext.weak-map.from.js"),require("core-js/modules/esnext.weak-map.of.js"),require("core-js/modules/esnext.weak-set.add-all.js"),require("core-js/modules/esnext.weak-set.delete-all.js"),require("core-js/modules/esnext.weak-set.from.js"),require("core-js/modules/esnext.weak-set.of.js"),require("core-js/modules/web.dom-collections.iterator.js"),require("core-js/modules/web.immediate.js"),require("core-js/modules/web.queue-microtask.js"),require("core-js/modules/web.url.js"),require("core-js/modules/web.url.to-json.js"),require("core-js/modules/web.url-search-params.js");var e=require("@speckle/shared"),s=require("lodash");function r(e,s,r){return s in e?Object.defineProperty(e,s,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[s]=r,e}class t extends Error{constructor(e,s){e||(e=new.target.defaultMessage),super(e,s)}}r(t,"defaultMessage","Unexpected error occurred");class o extends t{}r(o,"defaultMessage","Object loader configured incorrectly!");class i extends t{}r(i,"defaultMessage","Object loader encountered a runtime problem!");module.exports=class{constructor(s){var r;let{serverUrl:t,streamId:a,token:n,objectId:c,options:u={enableCaching:!0,fullyTraverseArrays:!1,excludeProps:[],fetch:null,customLogger:void 0,customWarner:void 0}}=s;if(this.logger=u.customLogger||console.log,this.warner=u.customWarner||console.warn,this.INTERVAL_MS=20,this.TIMEOUT_MS=18e4,this.serverUrl=t||(null===globalThis||void 0===globalThis||null===(r=globalThis.location)||void 0===r?void 0:r.origin),!this.serverUrl)throw new o("Invalid serverUrl specified!");if(this.streamId=a,this.objectId=c,!this.streamId)throw new o("Invalid streamId specified!");if(!this.objectId)throw new o("Invalid objectId specified!");this.logger("Object loader constructor called!");try{this.token=n||e.SafeLocalStorage.get("AuthToken")}catch(e){}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=u,this.options.numConnections=this.options.numConnections||4,this.cacheDB=null,this.lastAsyncPause=Date.now(),this.existingAsyncPause=null,this.preferredFetch=u.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)}}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 getAndConstructObject(e){await this.downloadObjectsInBuffer(e);const s=await this.getObject(this.objectId);return this.traverseAndConstruct(s,e)}async downloadObjectsInBuffer(e){let s=!0,r=0;for await(const t of this.getObjectIterator())s&&(this.totalChildrenCount=t.totalChildrenCount,s=!1,this.isLoading=!0),r++,e&&e({stage:"download",current:r,total:this.totalChildrenCount});this.isLoading=!1}async traverseAndConstruct(e,s){if(e){if("object"!=typeof e)return e;if(Array.isArray(e)&&0!==e.length){var r,t;const o=[];for(const r of e){if(!r)continue;if("object"!=typeof r&&!this.options.fullyTraverseArrays)return e;const t=r.referencedId?await this.getObject(r.referencedId):r;r.referencedId&&s&&s({stage:"construction",current:++this.traversedReferencesCount>this.totalChildrenCount?this.totalChildrenCount:this.traversedReferencesCount,total:this.totalChildrenCount}),o.push(await this.traverseAndConstruct(t,s))}return null!==(r=o[0])&&void 0!==r&&null!==(t=r.speckle_type)&&void 0!==t&&t.toLowerCase().includes("datachunk")?o.reduce(((e,s)=>e.concat(s.data)),[]):o}for(const s of this.options.excludeProps)delete e[s];for(const r in e)"object"==typeof e[r]&&null!==e[r]&&(e[r].referencedId&&(e[r]=await this.getObject(e[r].referencedId),s&&s({stage:"construction",current:++this.traversedReferencesCount>this.totalChildrenCount?this.totalChildrenCount:this.traversedReferencesCount,total:this.totalChildrenCount})),e[r]=await this.traverseAndConstruct(e[r],s));return e}}async getObject(e){if(this.buffer[e])return this.buffer[e];return new Promise(((s,r)=>{if(this.promises.push({id:e,resolve:s,reject:r}),this.intervals[e])this.intervals[e].elapsed=0;else{const s=setInterval(this.tryResolvePromise.bind(this),this.INTERVAL_MS,e);this.intervals[e]={interval:s,elapsed:0}}}))}tryResolvePromise(e){if(this.intervals[e].elapsed+=this.INTERVAL_MS,this.buffer[e]){for(const s of this.promises.filter((s=>s.id===e)))s.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((s=>s.id===e)).forEach((e=>e.reject())),this.promises=this.promises.filter((e=>e.id!=e.id)))}async*getObjectIterator(){const e=Date.now();let s=0;for await(const e of this.getRawObjectIterator()){const{id:r,obj:t}=this.processLine(e);this.buffer[r]=t,s+=1,yield t}this.logger("Loaded ".concat(s," objects in: ").concat((Date.now()-e)/1e3))}processLine(e){const s=e.split("\t"),[r,t]=s;let o;try{o=JSON.parse(t)}catch(e){throw new Error("Error parsing object ".concat(r,": ").concat(e.message))}return{id:r,obj:o}}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((s=>{const r=()=>indexedDB.databases().finally(s);e=setInterval(r,100),r()})).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 e=await this.getRawRootObject();yield"".concat(this.objectId,"\t").concat(e);const r=JSON.parse(e);if(!r.__closure)return;let t=Object.keys(r.__closure).filter((e=>!e.includes("blob"))).sort(((e,s)=>r.__closure[e]-r.__closure[s]));if(0===t.length)return;let o=[];if(t.length>50){const e=[[],[],[],[]];let i=0;for(;i<.05*t.length;i++)e[0].push(t[i]);for(;i<.2*t.length;i++)e[1].push(t[i]);for(;i<.6*t.length;i++)e[2].push(t[i]);for(;i<t.length;i++)e[3].push(t[i]);this.logger("Cache check for: ",e);const a=[];let n=this.cacheGetObjects(e[0]);for(let t=0;t<4;t++){const o=await n;t<3&&(n=this.cacheGetObjects(e[t+1]));const i=Object.keys(o).sort(((e,s)=>r.__closure[e]-r.__closure[s]));for(const e of i)yield"".concat(e,"\t").concat(o[e]);const c=e[t].filter((e=>!(e in o))),u=500,l=s.chunk(c,u);for(let e=0;e<l.length;e++)a.push.apply(a,l[e])}if(0===a.length)return;if(a.length<=50)o.push(a);else{for(o=[[],[],[],[]],i=0;i<.05*a.length;i++)o[0].push(a[i]);for(;i<.2*a.length;i++)o[1].push(a[i]);for(;i<.6*a.length;i++)o[2].push(a[i]);for(;i<a.length;i++)o[3].push(a[i])}}else{const e=await this.cacheGetObjects(t),s=Object.keys(e).sort(((e,s)=>r.__closure[e]-r.__closure[s]));for(const r of s)yield"".concat(r,"\t").concat(e[r]);if(t=t.filter((s=>!(s in e))),0===t.length)return;o.push(t)}const i=[],a=[],n=[],c=[],u=[];for(let e=0;e<o.length;e++)i.push(new TextDecoder),a.push(null),n.push(null),c.push(""),u.push(!1),this.fetch(this.requestUrlChildren,{method:"POST",headers:{...this.headers,"Content-Type":"application/json"},body:JSON.stringify({objects:JSON.stringify(o[e])})}).then((s=>{s.body.getReader&&(s.body.iterator=async function*(){const e=this.getReader();for(;;){const s=await e.read();if(s.done)return s.value;yield s.value}});const r=s.body.iterator();a[e]=r;const t=r.next().then((s=>(s.reqId=e,s)));n[e]=t}));for(;;){const e=n.filter((e=>!!e));if(0===e.length){if(u.every((e=>e)))break;await new Promise((e=>{setTimeout(e,10)}));continue}const s=await Promise.any(e);let{value:r,done:t,reqId:o}=s;if(u[o]=t,t)c[o].length>0&&(yield c[o],c[o]=""),n[o]=null;else{const e=a[o].next().then((e=>(e.reqId=o,e)));n[o]=e}if(!r)continue;r=i[o].decode(r);const l=(c[o]+r).split(/\r\n|\n|\r/),d=l.pop();c[o]=d;for(const e of l)yield e;this.cacheStoreObjects(l)}}async getRawRootObject(){const e=await this.cacheGetObjects([this.objectId]);if(e[this.objectId])return e[this.objectId];const s=await this.fetch(this.requestUrlRootObj,{headers:this.headers}),r=await s.text();if([401,403].includes(s.status))throw new i("You do not have access to the root object!");return this.cacheStoreObjects(["".concat(this.objectId,"\t").concat(r)]),r}promisifyIdbRequest(e){return new Promise(((s,r)=>{e.oncomplete=e.onsuccess=()=>s(e.result),e.onabort=e.onerror=()=>r(e.error)}))}async cacheGetObjects(e){if(!this.supportsCache())return{};const s={};for(let r=0;r<e.length;r+=500){const t=e.slice(r,r+500),o=this.cacheDB.transaction("objects","readonly").objectStore("objects"),i=t.map((e=>this.promisifyIdbRequest(o.get(e)).then((s=>({id:e,data:s}))))),a=await Promise.all(i);for(const e of a)e.data&&(s[e.id]=e.data)}return s}cacheStoreObjects(e){if(!this.supportsCache())return{};const s=this.cacheDB.transaction("objects","readwrite").objectStore("objects");for(const r of e){const e=r.split("\t");s.put(e[1],e[0])}return this.promisifyIdbRequest(s.transaction)}};
|
|
1
|
+
"use strict";require("core-js/modules/es.symbol.description.js"),require("core-js/modules/es.array.flat.js"),require("core-js/modules/es.array.flat-map.js"),require("core-js/modules/es.array.includes.js"),require("core-js/modules/es.array.reduce.js"),require("core-js/modules/es.array.reduce-right.js"),require("core-js/modules/es.array.sort.js"),require("core-js/modules/es.array.unscopables.flat.js"),require("core-js/modules/es.array.unscopables.flat-map.js"),require("core-js/modules/es.math.hypot.js"),require("core-js/modules/es.object.from-entries.js"),require("core-js/modules/es.promise.js"),require("core-js/modules/es.promise.finally.js"),require("core-js/modules/es.regexp.constructor.js"),require("core-js/modules/es.regexp.exec.js"),require("core-js/modules/es.regexp.flags.js"),require("core-js/modules/es.string.replace.js"),require("core-js/modules/es.typed-array.float32-array.js"),require("core-js/modules/es.typed-array.float64-array.js"),require("core-js/modules/es.typed-array.int8-array.js"),require("core-js/modules/es.typed-array.int16-array.js"),require("core-js/modules/es.typed-array.int32-array.js"),require("core-js/modules/es.typed-array.uint8-array.js"),require("core-js/modules/es.typed-array.uint8-clamped-array.js"),require("core-js/modules/es.typed-array.uint16-array.js"),require("core-js/modules/es.typed-array.uint32-array.js"),require("core-js/modules/es.typed-array.from.js"),require("core-js/modules/es.typed-array.of.js"),require("core-js/modules/es.typed-array.set.js"),require("core-js/modules/es.typed-array.sort.js"),require("core-js/modules/esnext.aggregate-error.js"),require("core-js/modules/esnext.array.last-index.js"),require("core-js/modules/esnext.array.last-item.js"),require("core-js/modules/esnext.composite-key.js"),require("core-js/modules/esnext.composite-symbol.js"),require("core-js/modules/esnext.global-this.js"),require("core-js/modules/esnext.map.delete-all.js"),require("core-js/modules/esnext.map.every.js"),require("core-js/modules/esnext.map.filter.js"),require("core-js/modules/esnext.map.find.js"),require("core-js/modules/esnext.map.find-key.js"),require("core-js/modules/esnext.map.from.js"),require("core-js/modules/esnext.map.group-by.js"),require("core-js/modules/esnext.map.includes.js"),require("core-js/modules/esnext.map.key-by.js"),require("core-js/modules/esnext.map.key-of.js"),require("core-js/modules/esnext.map.map-keys.js"),require("core-js/modules/esnext.map.map-values.js"),require("core-js/modules/esnext.map.merge.js"),require("core-js/modules/esnext.map.of.js"),require("core-js/modules/esnext.map.reduce.js"),require("core-js/modules/esnext.map.some.js"),require("core-js/modules/esnext.map.update.js"),require("core-js/modules/esnext.math.clamp.js"),require("core-js/modules/esnext.math.deg-per-rad.js"),require("core-js/modules/esnext.math.degrees.js"),require("core-js/modules/esnext.math.fscale.js"),require("core-js/modules/esnext.math.iaddh.js"),require("core-js/modules/esnext.math.imulh.js"),require("core-js/modules/esnext.math.isubh.js"),require("core-js/modules/esnext.math.rad-per-deg.js"),require("core-js/modules/esnext.math.radians.js"),require("core-js/modules/esnext.math.scale.js"),require("core-js/modules/esnext.math.seeded-prng.js"),require("core-js/modules/esnext.math.signbit.js"),require("core-js/modules/esnext.math.umulh.js"),require("core-js/modules/esnext.number.from-string.js"),require("core-js/modules/esnext.observable.js"),require("core-js/modules/esnext.promise.all-settled.js"),require("core-js/modules/esnext.promise.any.js"),require("core-js/modules/esnext.promise.try.js"),require("core-js/modules/esnext.reflect.define-metadata.js"),require("core-js/modules/esnext.reflect.delete-metadata.js"),require("core-js/modules/esnext.reflect.get-metadata.js"),require("core-js/modules/esnext.reflect.get-metadata-keys.js"),require("core-js/modules/esnext.reflect.get-own-metadata.js"),require("core-js/modules/esnext.reflect.get-own-metadata-keys.js"),require("core-js/modules/esnext.reflect.has-metadata.js"),require("core-js/modules/esnext.reflect.has-own-metadata.js"),require("core-js/modules/esnext.reflect.metadata.js"),require("core-js/modules/esnext.set.add-all.js"),require("core-js/modules/esnext.set.delete-all.js"),require("core-js/modules/esnext.set.difference.js"),require("core-js/modules/esnext.set.every.js"),require("core-js/modules/esnext.set.filter.js"),require("core-js/modules/esnext.set.find.js"),require("core-js/modules/esnext.set.from.js"),require("core-js/modules/esnext.set.intersection.js"),require("core-js/modules/esnext.set.is-disjoint-from.js"),require("core-js/modules/esnext.set.is-subset-of.js"),require("core-js/modules/esnext.set.is-superset-of.js"),require("core-js/modules/esnext.set.join.js"),require("core-js/modules/esnext.set.map.js"),require("core-js/modules/esnext.set.of.js"),require("core-js/modules/esnext.set.reduce.js"),require("core-js/modules/esnext.set.some.js"),require("core-js/modules/esnext.set.symmetric-difference.js"),require("core-js/modules/esnext.set.union.js"),require("core-js/modules/esnext.string.at.js"),require("core-js/modules/esnext.string.code-points.js"),require("core-js/modules/esnext.string.match-all.js"),require("core-js/modules/esnext.string.replace-all.js"),require("core-js/modules/esnext.symbol.dispose.js"),require("core-js/modules/esnext.symbol.observable.js"),require("core-js/modules/esnext.symbol.pattern-match.js"),require("core-js/modules/esnext.weak-map.delete-all.js"),require("core-js/modules/esnext.weak-map.from.js"),require("core-js/modules/esnext.weak-map.of.js"),require("core-js/modules/esnext.weak-set.add-all.js"),require("core-js/modules/esnext.weak-set.delete-all.js"),require("core-js/modules/esnext.weak-set.from.js"),require("core-js/modules/esnext.weak-set.of.js"),require("core-js/modules/web.dom-collections.iterator.js"),require("core-js/modules/web.immediate.js"),require("core-js/modules/web.queue-microtask.js"),require("core-js/modules/web.url.js"),require("core-js/modules/web.url.to-json.js"),require("core-js/modules/web.url-search-params.js");var e=require("lodash");function s(e,s,r){return s in e?Object.defineProperty(e,s,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[s]=r,e}class r extends Error{constructor(e,s){e||(e=new.target.defaultMessage),super(e,s)}}s(r,"defaultMessage","Unexpected error occurred");class t extends r{}s(t,"defaultMessage","Object loader configured incorrectly!");class o extends r{}s(o,"defaultMessage","Object loader encountered a runtime problem!");module.exports=class{constructor(e){var s;let{serverUrl:r,streamId:i,token:n,objectId:a,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=r||(null===globalThis||void 0===globalThis||null===(s=globalThis.location)||void 0===s?void 0:s.origin),!this.serverUrl)throw new t("Invalid serverUrl specified!");if(this.streamId=i,this.objectId=a,!this.streamId)throw new t("Invalid streamId specified!");if(!this.objectId)throw new t("Invalid objectId specified!");this.logger("Object loader constructor called!"),this.token=n,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 o("Couldn't find fetch implementation! If running in a node environment, make sure you pass it in through the constructor!");return e(...arguments)}}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 getAndConstructObject(e){await this.downloadObjectsInBuffer(e);const s=await this.getObject(this.objectId);return this.traverseAndConstruct(s,e)}async downloadObjectsInBuffer(e){let s=!0,r=0;for await(const t of this.getObjectIterator())s&&(this.totalChildrenCount=t.totalChildrenCount,s=!1,this.isLoading=!0),r++,e&&e({stage:"download",current:r,total:this.totalChildrenCount});this.isLoading=!1}async traverseAndConstruct(e,s){if(e){if("object"!=typeof e)return e;if(Array.isArray(e)&&0!==e.length){var r,t;const o=[];for(const r of e){if(!r)continue;if("object"!=typeof r&&!this.options.fullyTraverseArrays)return e;const t=r.referencedId?await this.getObject(r.referencedId):r;r.referencedId&&s&&s({stage:"construction",current:++this.traversedReferencesCount>this.totalChildrenCount?this.totalChildrenCount:this.traversedReferencesCount,total:this.totalChildrenCount}),o.push(await this.traverseAndConstruct(t,s))}return null!==(r=o[0])&&void 0!==r&&null!==(t=r.speckle_type)&&void 0!==t&&t.toLowerCase().includes("datachunk")?o.reduce(((e,s)=>e.concat(s.data)),[]):o}for(const s of this.options.excludeProps)delete e[s];for(const r in e)"object"==typeof e[r]&&null!==e[r]&&(e[r].referencedId&&(e[r]=await this.getObject(e[r].referencedId),s&&s({stage:"construction",current:++this.traversedReferencesCount>this.totalChildrenCount?this.totalChildrenCount:this.traversedReferencesCount,total:this.totalChildrenCount})),e[r]=await this.traverseAndConstruct(e[r],s));return e}}async getObject(e){if(this.buffer[e])return this.buffer[e];return new Promise(((s,r)=>{if(this.promises.push({id:e,resolve:s,reject:r}),this.intervals[e])this.intervals[e].elapsed=0;else{const s=setInterval(this.tryResolvePromise.bind(this),this.INTERVAL_MS,e);this.intervals[e]={interval:s,elapsed:0}}}))}tryResolvePromise(e){if(this.intervals[e].elapsed+=this.INTERVAL_MS,this.buffer[e]){for(const s of this.promises.filter((s=>s.id===e)))s.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((s=>s.id===e)).forEach((e=>e.reject())),this.promises=this.promises.filter((e=>e.id!=e.id)))}async*getObjectIterator(){const e=Date.now();let s=0;for await(const e of this.getRawObjectIterator()){const{id:r,obj:t}=this.processLine(e);this.buffer[r]=t,s+=1,yield t}this.logger("Loaded ".concat(s," objects in: ").concat((Date.now()-e)/1e3))}processLine(e){const s=e.split("\t"),[r,t]=s;let o;try{o=JSON.parse(t)}catch(e){throw new Error("Error parsing object ".concat(r,": ").concat(e.message))}return{id:r,obj:o}}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((s=>{const r=()=>indexedDB.databases().finally(s);e=setInterval(r,100),r()})).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 s=await this.getRawRootObject();yield"".concat(this.objectId,"\t").concat(s);const r=JSON.parse(s);if(!r.__closure)return;let t=Object.keys(r.__closure).filter((e=>!e.includes("blob"))).sort(((e,s)=>r.__closure[e]-r.__closure[s]));if(0===t.length)return;let o=[];if(t.length>50){const s=[[],[],[],[]];let i=0;for(;i<.05*t.length;i++)s[0].push(t[i]);for(;i<.2*t.length;i++)s[1].push(t[i]);for(;i<.6*t.length;i++)s[2].push(t[i]);for(;i<t.length;i++)s[3].push(t[i]);this.logger("Cache check for: ",s);const n=[];let a=this.cacheGetObjects(s[0]);for(let t=0;t<4;t++){const o=await a;t<3&&(a=this.cacheGetObjects(s[t+1]));const i=Object.keys(o).sort(((e,s)=>r.__closure[e]-r.__closure[s]));for(const e of i)yield"".concat(e,"\t").concat(o[e]);const c=s[t].filter((e=>!(e in o))),u=500,l=e.chunk(c,u);for(let e=0;e<l.length;e++)n.push.apply(n,l[e])}if(0===n.length)return;if(n.length<=50)o.push(n);else{for(o=[[],[],[],[]],i=0;i<.05*n.length;i++)o[0].push(n[i]);for(;i<.2*n.length;i++)o[1].push(n[i]);for(;i<.6*n.length;i++)o[2].push(n[i]);for(;i<n.length;i++)o[3].push(n[i])}}else{const e=await this.cacheGetObjects(t),s=Object.keys(e).sort(((e,s)=>r.__closure[e]-r.__closure[s]));for(const r of s)yield"".concat(r,"\t").concat(e[r]);if(t=t.filter((s=>!(s in e))),0===t.length)return;o.push(t)}const i=[],n=[],a=[],c=[],u=[];for(let e=0;e<o.length;e++)i.push(new TextDecoder),n.push(null),a.push(null),c.push(""),u.push(!1),this.fetch(this.requestUrlChildren,{method:"POST",headers:{...this.headers,"Content-Type":"application/json"},body:JSON.stringify({objects:JSON.stringify(o[e])})}).then((s=>{s.body.getReader&&(s.body.iterator=async function*(){const e=this.getReader();for(;;){const s=await e.read();if(s.done)return s.value;yield s.value}});const r=s.body.iterator();n[e]=r;const t=r.next().then((s=>(s.reqId=e,s)));a[e]=t}));for(;;){const e=a.filter((e=>!!e));if(0===e.length){if(u.every((e=>e)))break;await new Promise((e=>{setTimeout(e,10)}));continue}const s=await Promise.any(e);let{value:r,done:t,reqId:o}=s;if(u[o]=t,t)c[o].length>0&&(yield c[o],c[o]=""),a[o]=null;else{const e=n[o].next().then((e=>(e.reqId=o,e)));a[o]=e}if(!r)continue;r=i[o].decode(r);const l=(c[o]+r).split(/\r\n|\n|\r/),d=l.pop();c[o]=d;for(const e of l)yield e;this.cacheStoreObjects(l)}}async getRawRootObject(){const e=await this.cacheGetObjects([this.objectId]);if(e[this.objectId])return e[this.objectId];const s=await this.fetch(this.requestUrlRootObj,{headers:this.headers}),r=await s.text();if([401,403].includes(s.status))throw new o("You do not have access to the root object!");return this.cacheStoreObjects(["".concat(this.objectId,"\t").concat(r)]),r}promisifyIdbRequest(e){return new Promise(((s,r)=>{e.oncomplete=e.onsuccess=()=>s(e.result),e.onabort=e.onerror=()=>r(e.error)}))}async cacheGetObjects(e){if(!this.supportsCache())return{};const s={};for(let r=0;r<e.length;r+=500){const t=e.slice(r,r+500),o=this.cacheDB.transaction("objects","readonly").objectStore("objects"),i=t.map((e=>this.promisifyIdbRequest(o.get(e)).then((s=>({id:e,data:s}))))),n=await Promise.all(i);for(const e of n)e.data&&(s[e.id]=e.data)}return s}cacheStoreObjects(e){if(!this.supportsCache())return{};const s=this.cacheDB.transaction("objects","readwrite").objectStore("objects");for(const r of e){const e=r.split("\t");s.put(e[1],e[0])}return this.promisifyIdbRequest(s.transaction)}};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@speckle/objectloader",
|
|
3
|
-
"version": "2.18.
|
|
3
|
+
"version": "2.18.16",
|
|
4
4
|
"description": "Simple API helper to stream in objects from the Speckle Server.",
|
|
5
5
|
"main": "dist/objectloader.js",
|
|
6
6
|
"module": "dist/objectloader.esm.js",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"license": "Apache-2.0",
|
|
32
32
|
"dependencies": {
|
|
33
33
|
"@babel/core": "^7.17.9",
|
|
34
|
-
"@speckle/shared": "^2.18.
|
|
34
|
+
"@speckle/shared": "^2.18.16",
|
|
35
35
|
"core-js": "^3.21.1",
|
|
36
36
|
"regenerator-runtime": "^0.13.7"
|
|
37
37
|
},
|
package/src/index.js
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
import 'core-js'
|
|
3
3
|
import 'regenerator-runtime/runtime'
|
|
4
4
|
|
|
5
|
-
import { SafeLocalStorage } from '@speckle/shared'
|
|
6
5
|
import {
|
|
7
6
|
ObjectLoaderConfigurationError,
|
|
8
7
|
ObjectLoaderRuntimeError
|
|
@@ -54,11 +53,17 @@ export default class ObjectLoader {
|
|
|
54
53
|
}
|
|
55
54
|
|
|
56
55
|
this.logger('Object loader constructor called!')
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
56
|
+
|
|
57
|
+
/** I don't think the object-loader should read the token from local storage, since there is no
|
|
58
|
+
* builtin mechanism that sets it in the first place. So you're reading a key from the local storage
|
|
59
|
+
* and hoping it will magically be there.
|
|
60
|
+
*/
|
|
61
|
+
// try {
|
|
62
|
+
// this.token = token || SafeLocalStorage.get('AuthToken')
|
|
63
|
+
// } catch (error) {
|
|
64
|
+
// // Accessing localStorage may throw when executing on sandboxed document, ignore.
|
|
65
|
+
// }
|
|
66
|
+
this.token = token
|
|
62
67
|
|
|
63
68
|
this.headers = {
|
|
64
69
|
Accept: 'text/plain'
|
package/types/index.d.ts
CHANGED