@speckle/objectloader 2.1.1 → 2.3.17-alpha.2819

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/.eslintrc.cjs ADDED
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Extends repo root config, only put changes here that are scoped to this specific package
3
+ * (if you already are - evaluate whether you really need package scoped linting rules)
4
+ */
5
+
6
+ /** @type {import("eslint").Linter.Config} */
7
+ const config = {
8
+ env: {
9
+ browser: true
10
+ },
11
+ parserOptions: {
12
+ sourceType: 'module'
13
+ }
14
+ }
15
+
16
+ module.exports = config
package/LICENSE ADDED
@@ -0,0 +1,208 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2020 AEC Systems
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
202
+
203
+ NOTICE: Unless otherwise described, the code in this repository is
204
+ licensed under the license above. Some modules, extensions or code herein
205
+ might be otherwise licensed. This is indicated either in the root of the
206
+ containing folder under a different license file, or in the respective
207
+ file's header. If you have any questions, don't hesitate to get in touch
208
+ with us via [email](mailto:hello@speckle.systems).
@@ -1,19 +1,21 @@
1
1
  <!DOCTYPE html>
2
2
  <html lang="en" class="no-js">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <meta name="viewport" content="width=device-width" />
3
6
 
4
- <head>
5
- <meta charset="UTF-8">
6
- <meta name="viewport" content="width=device-width">
7
+ <title>Object Loader Test</title>
8
+ </head>
7
9
 
8
- <title>Object Loader Test</title>
9
- </head>
10
+ <body>
11
+ <h1>This is a test.</h1>
12
+ <p>All the magic is in the console.</p>
13
+ <p>
14
+ To ensure this example runs correctly, please serve this file from a local http
15
+ server - if you manually open the file in a browser, it might not work.
16
+ </p>
17
+ <button onclick="loadData()">PRESS ME</button>
18
+ </body>
10
19
 
11
- <body>
12
- <h1>This is a test.</h1>
13
- <p>All the magic is in the console.</p>
14
- <p>To ensure this example runs correctly, please serve this file from a local http server - if you manually open the file in a browser, it might not work.</p>
15
- <button onclick="loadData()">PRESS ME</button>
16
- </body>
17
-
18
20
  <script src="script.js" type="module"></script>
19
- </html>
21
+ </html>
@@ -9,25 +9,23 @@ window.ObjectLoader = ObjectLoader
9
9
 
10
10
  // https://latest.speckle.dev/streams/92b620fb17/objects/7cd9d41b5b5f3c8908536aec2a05f1a1
11
11
  // let loader = new ObjectLoader({
12
- // serverUrl:"https://latest.speckle.dev",
13
- // streamId:"92b620fb17",
12
+ // serverUrl:"https://latest.speckle.dev",
13
+ // streamId:"92b620fb17",
14
14
  // objectId:"878c426bb213ddb4d580da74922a2b16"
15
15
  // })
16
16
 
17
17
  // https://latest.speckle.dev/streams/3ed8357f29/objects/0408ab9caaa2ebefb2dd7f1f671e7555
18
- let loader = new ObjectLoader({
19
- serverUrl:"https://latest.speckle.dev",
20
- streamId:"3ed8357f29",
21
- objectId:"0408ab9caaa2ebefb2dd7f1f671e7555"
18
+ const loader = new ObjectLoader({
19
+ serverUrl: 'https://latest.speckle.dev',
20
+ streamId: '3ed8357f29',
21
+ objectId: '0408ab9caaa2ebefb2dd7f1f671e7555'
22
22
  })
23
23
 
24
-
25
24
  window.loadData = async function loadData() {
26
-
27
- let obj = await loader.getAndConstructObject((e) =>{
25
+ const obj = await loader.getAndConstructObject((e) => {
28
26
  console.log(e) // log progress!
29
27
  })
30
28
 
31
29
  console.log('Done!')
32
- console.log( obj )
33
- }
30
+ console.log(obj)
31
+ }
@@ -1 +1,22 @@
1
- // NOTE: This lib is not working in node, because node-fetch returns node-native readable streams - we need a workaround first.
1
+ // Since Node v<18 does not provide fetch, we need to pass it in the options object. Note that fetch must return a WHATWG compliant stream, so cross-fetch won't work, but node/undici's implementation will.
2
+
3
+ import { fetch } from 'undici'
4
+ import ObjectLoader from '../../index.js'
5
+
6
+ const loader = new ObjectLoader({
7
+ serverUrl: 'https://latest.speckle.dev',
8
+ streamId: '3ed8357f29',
9
+ objectId: '0408ab9caaa2ebefb2dd7f1f671e7555',
10
+ options: { enableCaching: false, excludeProps: [], fetch }
11
+ })
12
+
13
+ const loadData = async function loadData() {
14
+ const obj = await loader.getAndConstructObject((e) => {
15
+ console.log(e) // log progress!
16
+ })
17
+
18
+ console.log('Done!')
19
+ console.log(obj)
20
+ }
21
+
22
+ loadData()
package/index.js CHANGED
@@ -1,34 +1,48 @@
1
1
  /**
2
- * Simple client that streams object info from a Speckle Server.
2
+ * Simple client that streams object info from a Speckle Server.
3
3
  * TODO: Object construction progress reporting is weird.
4
4
  */
5
5
 
6
-
7
-
8
6
  export default class ObjectLoader {
9
-
10
7
  /**
11
8
  * Creates a new object loader instance.
12
- * @param {*} param0
9
+ * @param {*} param0
13
10
  */
14
- constructor( { serverUrl, streamId, token, objectId, options = { fullyTraverseArrays: false, excludeProps: [ ] } } ) {
11
+ constructor({
12
+ serverUrl,
13
+ streamId,
14
+ token,
15
+ objectId,
16
+ options = {
17
+ enableCaching: true,
18
+ fullyTraverseArrays: false,
19
+ excludeProps: [],
20
+ fetch: null
21
+ }
22
+ }) {
15
23
  this.INTERVAL_MS = 20
16
24
  this.TIMEOUT_MS = 180000 // three mins
17
25
 
18
26
  this.serverUrl = serverUrl || window.location.origin
19
27
  this.streamId = streamId
20
28
  this.objectId = objectId
21
- this.token = token || localStorage.getItem( 'AuthToken' )
29
+ console.log('Object loader constructor called!')
30
+ try {
31
+ this.token = token || localStorage.getItem('AuthToken')
32
+ } catch (error) {
33
+ // Accessing localStorage may throw when executing on sandboxed document, ignore.
34
+ }
22
35
 
23
36
  this.headers = {
24
- 'Accept': 'text/plain'
37
+ Accept: 'text/plain'
25
38
  }
26
39
 
27
- if( token ) {
40
+ if (this.token) {
28
41
  this.headers['Authorization'] = `Bearer ${this.token}`
29
42
  }
30
43
 
31
- this.requestUrl = `${this.serverUrl}/objects/${this.streamId}/${this.objectId}`
44
+ this.requestUrlRootObj = `${this.serverUrl}/objects/${this.streamId}/${this.objectId}/single`
45
+ this.requestUrlChildren = `${this.serverUrl}/api/getobjects/${this.streamId}`
32
46
  this.promises = []
33
47
  this.intervals = {}
34
48
  this.buffer = []
@@ -36,180 +50,478 @@ export default class ObjectLoader {
36
50
  this.totalChildrenCount = 0
37
51
  this.traversedReferencesCount = 0
38
52
  this.options = options
53
+ this.options.numConnections = this.options.numConnections || 4
54
+
55
+ this.cacheDB = null
56
+
57
+ this.lastAsyncPause = Date.now()
58
+ this.existingAsyncPause = null
59
+
60
+ // we can't simply bind fetch to this.fetch, so instead we have to do some acrobatics:
61
+ // https://stackoverflow.com/questions/69337187/uncaught-in-promise-typeerror-failed-to-execute-fetch-on-workerglobalscope#comment124731316_69337187
62
+ this.preferredFetch = options.fetch
63
+ this.fetch = function (...args) {
64
+ const currentFetch = this.preferredFetch || fetch
65
+ return currentFetch(...args)
66
+ }
67
+ }
68
+
69
+ async asyncPause() {
70
+ // Don't freeze the UI
71
+ // while ( this.existingAsyncPause ) {
72
+ // await this.existingAsyncPause
73
+ // }
74
+ if (Date.now() - this.lastAsyncPause >= 100) {
75
+ this.lastAsyncPause = Date.now()
76
+ this.existingAsyncPause = new Promise((resolve) => setTimeout(resolve, 0))
77
+ await this.existingAsyncPause
78
+ this.existingAsyncPause = null
79
+ if (Date.now() - this.lastAsyncPause > 500)
80
+ console.log('Loader Event loop lag: ', Date.now() - this.lastAsyncPause)
81
+ }
39
82
  }
40
83
 
41
84
  dispose() {
42
85
  this.buffer = []
43
- this.intervals.forEach( i => clearInterval( i.interval ) )
86
+ this.intervals.forEach((i) => clearInterval(i.interval))
44
87
  }
45
88
 
46
89
  /**
47
90
  * Use this method to receive and construct the object. It will return the full, de-referenced and de-chunked original object.
48
- * @param {*} onProgress
49
- * @returns
91
+ * @param {*} onProgress
92
+ * @returns
50
93
  */
51
- async getAndConstructObject( onProgress ) {
52
-
53
- ;( await this.downloadObjectsInBuffer( onProgress ) ) // Fire and forget; PS: semicolon of doom
54
-
55
- let rootObject = await this.getObject( this.objectId )
56
- return this.traverseAndConstruct( rootObject, onProgress )
94
+ async getAndConstructObject(onProgress) {
95
+ await this.downloadObjectsInBuffer(onProgress) // Fire and forget; PS: semicolon of doom
96
+
97
+ const rootObject = await this.getObject(this.objectId)
98
+ return this.traverseAndConstruct(rootObject, onProgress)
57
99
  }
58
100
 
59
101
  /**
60
102
  * Internal function used to download all the objects in a local buffer.
61
- * @param {*} onProgress
103
+ * @param {*} onProgress
62
104
  */
63
- async downloadObjectsInBuffer( onProgress ) {
105
+ async downloadObjectsInBuffer(onProgress) {
64
106
  let first = true
65
107
  let downloadNum = 0
66
-
67
- for await ( let obj of this.getObjectIterator() ) {
68
- if( first ) {
108
+
109
+ for await (const obj of this.getObjectIterator()) {
110
+ if (first) {
69
111
  this.totalChildrenCount = obj.totalChildrenCount
70
112
  first = false
71
113
  this.isLoading = true
72
114
  }
73
115
  downloadNum++
74
- if( onProgress ) onProgress( { stage: 'download', current: downloadNum, total: this.totalChildrenCount } )
116
+ if (onProgress)
117
+ onProgress({
118
+ stage: 'download',
119
+ current: downloadNum,
120
+ total: this.totalChildrenCount
121
+ })
75
122
  }
76
123
  this.isLoading = false
77
124
  }
78
125
 
79
126
  /**
80
127
  * Internal function used to recursively traverse an object and populate its references and dechunk any arrays.
81
- * @param {*} obj
82
- * @param {*} onProgress
83
- * @returns
128
+ * @param {*} obj
129
+ * @param {*} onProgress
130
+ * @returns
84
131
  */
85
- async traverseAndConstruct( obj, onProgress ) {
86
- if( !obj ) return
87
- if ( typeof obj !== 'object' ) return obj
132
+ async traverseAndConstruct(obj, onProgress) {
133
+ if (!obj) return
134
+ if (typeof obj !== 'object') return obj
88
135
 
89
136
  // Handle arrays
90
- if ( Array.isArray( obj ) && obj.length !== 0 ) {
91
- let arr = []
92
- for ( let element of obj ) {
93
- if ( typeof element !== 'object' && ! this.options.fullyTraverseArrays ) return obj
94
-
137
+ if (Array.isArray(obj) && obj.length !== 0) {
138
+ const arr = []
139
+ for (const element of obj) {
140
+ if (typeof element !== 'object' && !this.options.fullyTraverseArrays) return obj
141
+
95
142
  // Dereference element if needed
96
- let deRef = element.referencedId ? await this.getObject( element.referencedId ) : element
97
- if( element.referencedId && onProgress ) onProgress( { stage: 'construction', current: ++this.traversedReferencesCount > this.totalChildrenCount ? this.totalChildrenCount : this.traversedReferencesCount, total: this.totalChildrenCount } )
98
-
143
+ const deRef = element.referencedId
144
+ ? await this.getObject(element.referencedId)
145
+ : element
146
+ if (element.referencedId && onProgress)
147
+ onProgress({
148
+ stage: 'construction',
149
+ current:
150
+ ++this.traversedReferencesCount > this.totalChildrenCount
151
+ ? this.totalChildrenCount
152
+ : this.traversedReferencesCount,
153
+ total: this.totalChildrenCount
154
+ })
155
+
99
156
  // Push the traversed object in the array
100
- arr.push( await this.traverseAndConstruct( deRef, onProgress ) )
157
+ arr.push(await this.traverseAndConstruct(deRef, onProgress))
101
158
  }
102
-
159
+
103
160
  // De-chunk
104
- if( arr[0]?.speckle_type?.toLowerCase().includes('datachunk') ) {
105
- return arr.reduce( ( prev, curr ) => prev.concat( curr.data ), [] )
161
+ if (arr[0]?.speckle_type?.toLowerCase().includes('datachunk')) {
162
+ return arr.reduce((prev, curr) => prev.concat(curr.data), [])
106
163
  }
107
-
164
+
108
165
  return arr
109
- }
166
+ }
110
167
 
111
168
  // Handle objects
112
169
  // 1) Purge ignored props
113
- for( let ignoredProp of this.options.excludeProps ) {
114
- delete obj[ ignoredProp ]
170
+ for (const ignoredProp of this.options.excludeProps) {
171
+ delete obj[ignoredProp]
115
172
  }
116
-
173
+
117
174
  // 2) Iterate through obj
118
- for( let prop in obj ) {
119
- if( typeof obj[prop] !== 'object' ) continue // leave alone primitive props
120
-
121
- if( obj[prop].referencedId ) {
122
- obj[prop] = await this.getObject( obj[prop].referencedId )
123
- if( onProgress ) onProgress( { stage: 'construction', current: ++this.traversedReferencesCount > this.totalChildrenCount ? this.totalChildrenCount : this.traversedReferencesCount, total: this.totalChildrenCount } )
175
+ for (const prop in obj) {
176
+ if (typeof obj[prop] !== 'object' || obj[prop] === null) continue // leave alone primitive props
177
+
178
+ if (obj[prop].referencedId) {
179
+ obj[prop] = await this.getObject(obj[prop].referencedId)
180
+ if (onProgress)
181
+ onProgress({
182
+ stage: 'construction',
183
+ current:
184
+ ++this.traversedReferencesCount > this.totalChildrenCount
185
+ ? this.totalChildrenCount
186
+ : this.traversedReferencesCount,
187
+ total: this.totalChildrenCount
188
+ })
124
189
  }
125
190
 
126
- obj[prop] = await this.traverseAndConstruct( obj[prop], onProgress )
191
+ obj[prop] = await this.traverseAndConstruct(obj[prop], onProgress)
127
192
  }
128
193
 
129
- return obj
194
+ return obj
130
195
  }
131
196
 
132
197
  /**
133
198
  * Internal function. Returns a promise that is resolved when the object id is loaded into the internal buffer.
134
- * @param {*} id
135
- * @returns
199
+ * @param {*} id
200
+ * @returns
136
201
  */
137
- async getObject( id ){
138
- if ( this.buffer[id] ) return this.buffer[id]
202
+ async getObject(id) {
203
+ if (this.buffer[id]) return this.buffer[id]
139
204
 
140
- let promise = new Promise( ( resolve, reject ) => {
141
- this.promises.push( { id, resolve, reject } )
205
+ const promise = new Promise((resolve, reject) => {
206
+ this.promises.push({ id, resolve, reject })
142
207
  // Only create a new interval checker if none is already present!
143
- if ( this.intervals[id] ) {
208
+ if (this.intervals[id]) {
144
209
  this.intervals[id].elapsed = 0 // reset elapsed
145
210
  } else {
146
- let intervalId = setInterval( this.tryResolvePromise.bind( this ), this.INTERVAL_MS, id )
211
+ const intervalId = setInterval(
212
+ this.tryResolvePromise.bind(this),
213
+ this.INTERVAL_MS,
214
+ id
215
+ )
147
216
  this.intervals[id] = { interval: intervalId, elapsed: 0 }
148
217
  }
149
- } )
218
+ })
150
219
  return promise
151
220
  }
152
221
 
153
- tryResolvePromise( id ) {
222
+ tryResolvePromise(id) {
154
223
  this.intervals[id].elapsed += this.INTERVAL_MS
155
- if ( this.buffer[id] ) {
156
- for ( let p of this.promises.filter( p => p.id === id ) ) {
157
- p.resolve( this.buffer[id] )
224
+ if (this.buffer[id]) {
225
+ for (const p of this.promises.filter((p) => p.id === id)) {
226
+ p.resolve(this.buffer[id])
158
227
  }
159
228
 
160
- clearInterval( this.intervals[id].interval )
229
+ clearInterval(this.intervals[id].interval)
161
230
  delete this.intervals[id]
162
231
  // this.promises = this.promises.filter( p => p.id !== p.id ) // clearing out promises too early seems to nuke loading
163
232
  return
164
233
  }
165
234
 
166
- if ( this.intervals[id].elapsed > this.TIMEOUT_MS ) {
167
- console.warn( `Timeout resolving ${id}. HIC SVNT DRACONES.` )
168
- clearInterval( this.intervals[id].interval )
169
- this.promises.filter( p => p.id === id ).forEach( p => p.reject() )
170
- this.promises = this.promises.filter( p => p.id !== p.id ) // clear out
235
+ if (this.intervals[id].elapsed > this.TIMEOUT_MS) {
236
+ console.warn(`Timeout resolving ${id}. HIC SVNT DRACONES.`)
237
+ clearInterval(this.intervals[id].interval)
238
+ this.promises.filter((p) => p.id === id).forEach((p) => p.reject())
239
+ this.promises = this.promises.filter((p) => p.id !== p.id) // clear out
171
240
  }
172
241
  }
173
242
 
174
- async * getObjectIterator( ) {
175
- for await ( let line of this.getRawObjectIterator() ) {
176
- let { id, obj } = this.processLine( line )
177
- this.buffer[ id ] = obj
243
+ async *getObjectIterator() {
244
+ const t0 = Date.now()
245
+ let count = 0
246
+ for await (const line of this.getRawObjectIterator()) {
247
+ const { id, obj } = this.processLine(line)
248
+ this.buffer[id] = obj
249
+ count += 1
178
250
  yield obj
179
251
  }
252
+ console.log(`Loaded ${count} objects in: ${(Date.now() - t0) / 1000}`)
180
253
  }
181
254
 
182
- processLine( chunk ) {
183
- var pieces = chunk.split( '\t' )
184
- return { id: pieces[0], obj: JSON.parse( pieces[1] ) }
255
+ processLine(chunk) {
256
+ const pieces = chunk.split('\t')
257
+ return { id: pieces[0], obj: JSON.parse(pieces[1]) }
185
258
  }
186
259
 
187
- async * getRawObjectIterator() {
188
- const decoder = new TextDecoder()
189
- const response = await fetch( this.requestUrl, { headers: this.headers } )
190
- const reader = response.body.getReader()
191
- let { value: chunk, done: readerDone } = await reader.read()
192
- chunk = chunk ? decoder.decode( chunk ) : ''
193
-
194
- let re = /\r\n|\n|\r/gm
195
- let startIndex = 0
196
-
197
- while ( true ) {
198
- let result = re.exec( chunk )
199
- if ( !result ) {
200
- if ( readerDone ) break
201
- let remainder = chunk.substr( startIndex )
202
- ;( { value: chunk, done: readerDone } = await reader.read() ) // PS: semicolon of doom
203
- chunk = remainder + ( chunk ? decoder.decode( chunk ) : '' )
204
- startIndex = re.lastIndex = 0
260
+ async *getRawObjectIterator() {
261
+ if (this.options.enableCaching && window.indexedDB && this.cacheDB === null) {
262
+ await safariFix()
263
+ const idbOpenRequest = indexedDB.open('speckle-object-cache', 1)
264
+ idbOpenRequest.onupgradeneeded = () =>
265
+ idbOpenRequest.result.createObjectStore('objects')
266
+ this.cacheDB = await this.promisifyIdbRequest(idbOpenRequest)
267
+ }
268
+
269
+ const rootObjJson = await this.getRawRootObject()
270
+ // console.log("Root in: ", Date.now() - tSTART)
271
+
272
+ yield `${this.objectId}\t${rootObjJson}`
273
+
274
+ const rootObj = JSON.parse(rootObjJson)
275
+ if (!rootObj.__closure) return
276
+
277
+ let childrenIds = Object.keys(rootObj.__closure).sort(
278
+ (a, b) => rootObj.__closure[a] - rootObj.__closure[b]
279
+ )
280
+ if (childrenIds.length === 0) return
281
+
282
+ let splitHttpRequests = []
283
+
284
+ if (childrenIds.length > 50) {
285
+ // split into 5%, 15%, 40%, 40% (5% for the high priority children: the ones with lower minDepth)
286
+ const splitBeforeCacheCheck = [[], [], [], []]
287
+ let crtChildIndex = 0
288
+
289
+ for (; crtChildIndex < 0.05 * childrenIds.length; crtChildIndex++) {
290
+ splitBeforeCacheCheck[0].push(childrenIds[crtChildIndex])
291
+ }
292
+ for (; crtChildIndex < 0.2 * childrenIds.length; crtChildIndex++) {
293
+ splitBeforeCacheCheck[1].push(childrenIds[crtChildIndex])
294
+ }
295
+ for (; crtChildIndex < 0.6 * childrenIds.length; crtChildIndex++) {
296
+ splitBeforeCacheCheck[2].push(childrenIds[crtChildIndex])
297
+ }
298
+ for (; crtChildIndex < childrenIds.length; crtChildIndex++) {
299
+ splitBeforeCacheCheck[3].push(childrenIds[crtChildIndex])
300
+ }
301
+
302
+ console.log('Cache check for: ', splitBeforeCacheCheck)
303
+
304
+ const newChildren = []
305
+ let nextCachePromise = this.cacheGetObjects(splitBeforeCacheCheck[0])
306
+
307
+ for (let i = 0; i < 4; i++) {
308
+ const cachedObjects = await nextCachePromise
309
+ if (i < 3) nextCachePromise = this.cacheGetObjects(splitBeforeCacheCheck[i + 1])
310
+
311
+ const sortedCachedKeys = Object.keys(cachedObjects).sort(
312
+ (a, b) => rootObj.__closure[a] - rootObj.__closure[b]
313
+ )
314
+ for (const id of sortedCachedKeys) {
315
+ yield `${id}\t${cachedObjects[id]}`
316
+ }
317
+ const newChildrenForBatch = splitBeforeCacheCheck[i].filter(
318
+ (id) => !(id in cachedObjects)
319
+ )
320
+ newChildren.push(...newChildrenForBatch)
321
+ }
322
+
323
+ if (newChildren.length === 0) return
324
+
325
+ if (newChildren.length <= 50) {
326
+ // we have almost all of children in the cache. do only 1 requests for the remaining new children
327
+ splitHttpRequests.push(newChildren)
328
+ } else {
329
+ // we now set up the batches for 4 http requests, starting from `newChildren` (already sorted by priority)
330
+ splitHttpRequests = [[], [], [], []]
331
+ crtChildIndex = 0
332
+
333
+ for (; crtChildIndex < 0.05 * newChildren.length; crtChildIndex++) {
334
+ splitHttpRequests[0].push(newChildren[crtChildIndex])
335
+ }
336
+ for (; crtChildIndex < 0.2 * newChildren.length; crtChildIndex++) {
337
+ splitHttpRequests[1].push(newChildren[crtChildIndex])
338
+ }
339
+ for (; crtChildIndex < 0.6 * newChildren.length; crtChildIndex++) {
340
+ splitHttpRequests[2].push(newChildren[crtChildIndex])
341
+ }
342
+ for (; crtChildIndex < newChildren.length; crtChildIndex++) {
343
+ splitHttpRequests[3].push(newChildren[crtChildIndex])
344
+ }
345
+ }
346
+ } else {
347
+ // small object with <= 50 children. check cache and make only 1 request
348
+ const cachedObjects = await this.cacheGetObjects(childrenIds)
349
+ const sortedCachedKeys = Object.keys(cachedObjects).sort(
350
+ (a, b) => rootObj.__closure[a] - rootObj.__closure[b]
351
+ )
352
+ for (const id of sortedCachedKeys) {
353
+ yield `${id}\t${cachedObjects[id]}`
354
+ }
355
+ childrenIds = childrenIds.filter((id) => !(id in cachedObjects))
356
+ if (childrenIds.length === 0) return
357
+
358
+ // only 1 http request with the remaining children ( <= 50 )
359
+ splitHttpRequests.push(childrenIds)
360
+ }
361
+
362
+ // Starting http requests for batches in `splitHttpRequests`
363
+
364
+ const decoders = []
365
+ const readers = []
366
+ const readPromises = []
367
+ const startIndexes = []
368
+ const readBuffers = []
369
+ const finishedRequests = []
370
+
371
+ for (let i = 0; i < splitHttpRequests.length; i++) {
372
+ decoders.push(new TextDecoder())
373
+ readers.push(null)
374
+ readPromises.push(null)
375
+ startIndexes.push(0)
376
+ readBuffers.push('')
377
+ finishedRequests.push(false)
378
+
379
+ this.fetch(this.requestUrlChildren, {
380
+ method: 'POST',
381
+ headers: { ...this.headers, 'Content-Type': 'application/json' },
382
+ body: JSON.stringify({ objects: JSON.stringify(splitHttpRequests[i]) })
383
+ }).then((crtResponse) => {
384
+ const crtReader = crtResponse.body.getReader()
385
+ readers[i] = crtReader
386
+ const crtReadPromise = crtReader.read().then((x) => {
387
+ x.reqId = i
388
+ return x
389
+ })
390
+ readPromises[i] = crtReadPromise
391
+ })
392
+ }
393
+
394
+ while (true) {
395
+ const validReadPromises = readPromises.filter((x) => !!x)
396
+ if (validReadPromises.length === 0) {
397
+ // Check if all requests finished
398
+ if (finishedRequests.every((x) => x)) {
399
+ break
400
+ }
401
+ // Sleep 10 ms
402
+ await new Promise((resolve) => {
403
+ setTimeout(resolve, 10)
404
+ })
205
405
  continue
206
406
  }
207
- yield chunk.substring( startIndex, result.index )
208
- startIndex = re.lastIndex
407
+
408
+ // Wait for data on any running request
409
+ const data = await Promise.any(validReadPromises)
410
+ // eslint-disable-next-line prefer-const
411
+ let { value: crtDataChunk, done: readerDone, reqId } = data
412
+ finishedRequests[reqId] = readerDone
413
+
414
+ // Replace read promise on this request with a new `read` call
415
+ if (!readerDone) {
416
+ const crtReadPromise = readers[reqId].read().then((x) => {
417
+ x.reqId = reqId
418
+ return x
419
+ })
420
+ readPromises[reqId] = crtReadPromise
421
+ } else {
422
+ // This request finished. "Flush any non-newline-terminated text"
423
+ if (readBuffers[reqId].length > 0) {
424
+ yield readBuffers[reqId]
425
+ readBuffers[reqId] = ''
426
+ }
427
+ // no other read calls for this request
428
+ readPromises[reqId] = null
429
+ }
430
+
431
+ if (!crtDataChunk) continue
432
+
433
+ crtDataChunk = decoders[reqId].decode(crtDataChunk)
434
+ const unprocessedText = readBuffers[reqId] + crtDataChunk
435
+ const unprocessedLines = unprocessedText.split(/\r\n|\n|\r/)
436
+ const remainderText = unprocessedLines.pop()
437
+ readBuffers[reqId] = remainderText
438
+
439
+ for (const line of unprocessedLines) {
440
+ yield line
441
+ }
442
+ this.cacheStoreObjects(unprocessedLines)
443
+ }
444
+ }
445
+
446
+ async getRawRootObject() {
447
+ const cachedRootObject = await this.cacheGetObjects([this.objectId])
448
+ if (cachedRootObject[this.objectId]) return cachedRootObject[this.objectId]
449
+ const response = await this.fetch(this.requestUrlRootObj, { headers: this.headers })
450
+ const responseText = await response.text()
451
+ this.cacheStoreObjects([`${this.objectId}\t${responseText}`])
452
+ return responseText
453
+ }
454
+
455
+ promisifyIdbRequest(request) {
456
+ return new Promise((resolve, reject) => {
457
+ request.oncomplete = request.onsuccess = () => resolve(request.result)
458
+ request.onabort = request.onerror = () => reject(request.error)
459
+ })
460
+ }
461
+
462
+ async cacheGetObjects(ids) {
463
+ if (!this.options.enableCaching || !window.indexedDB) {
464
+ return {}
465
+ }
466
+
467
+ const ret = {}
468
+
469
+ for (let i = 0; i < ids.length; i += 500) {
470
+ const idsChunk = ids.slice(i, i + 500)
471
+
472
+ const store = this.cacheDB
473
+ .transaction('objects', 'readonly')
474
+ .objectStore('objects')
475
+ const idbChildrenPromises = idsChunk.map((id) =>
476
+ this.promisifyIdbRequest(store.get(id)).then((data) => ({ id, data }))
477
+ )
478
+ const cachedData = await Promise.all(idbChildrenPromises)
479
+
480
+ // console.log("Cache check for : ", idsChunk.length, Date.now() - t0)
481
+
482
+ for (const cachedObj of cachedData) {
483
+ if (!cachedObj.data)
484
+ // non-existent objects are retrieved with `undefined` data
485
+ continue
486
+ ret[cachedObj.id] = cachedObj.data
487
+ }
209
488
  }
210
489
 
211
- if ( startIndex < chunk.length ) {
212
- yield chunk.substr( startIndex )
490
+ return ret
491
+ }
492
+
493
+ cacheStoreObjects(objects) {
494
+ if (!this.options.enableCaching || !window.indexedDB) {
495
+ return {}
213
496
  }
497
+
498
+ const store = this.cacheDB
499
+ .transaction('objects', 'readwrite')
500
+ .objectStore('objects')
501
+ for (const obj of objects) {
502
+ const idAndData = obj.split('\t')
503
+ store.put(idAndData[1], idAndData[0])
504
+ }
505
+
506
+ return this.promisifyIdbRequest(store.transaction)
214
507
  }
215
508
  }
509
+
510
+ // Credits and more info: https://github.com/jakearchibald/safari-14-idb-fix
511
+ function safariFix() {
512
+ const isSafari =
513
+ !navigator.userAgentData &&
514
+ /Safari\//.test(navigator.userAgent) &&
515
+ !/Chrom(e|ium)\//.test(navigator.userAgent)
516
+
517
+ // No point putting other browsers or older versions of Safari through this mess.
518
+ if (!isSafari || !indexedDB.databases) return Promise.resolve()
519
+
520
+ let intervalId
521
+
522
+ return new Promise((resolve) => {
523
+ const tryIdb = () => indexedDB.databases().finally(resolve)
524
+ intervalId = setInterval(tryIdb, 100)
525
+ tryIdb()
526
+ }).finally(() => clearInterval(intervalId))
527
+ }
package/jsconfig.json ADDED
@@ -0,0 +1,7 @@
1
+ {
2
+ "extends": "../../jsconfig.base.json",
3
+ "compilerOptions": {
4
+ "module": "es2020"
5
+ },
6
+ "include": ["index.js", "examples"]
7
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@speckle/objectloader",
3
- "version": "2.1.1",
3
+ "version": "2.3.17-alpha.2819",
4
4
  "description": "Simple API helper to stream in objects from the Speckle Server.",
5
5
  "main": "index.js",
6
6
  "homepage": "https://speckle.systems",
@@ -11,9 +11,21 @@
11
11
  "directory": "packages/objectloader"
12
12
  },
13
13
  "scripts": {
14
- "test": "echo \"Error: no test specified\" && exit 1"
14
+ "test": "echo \"Error: no test specified\" && exit 1",
15
+ "lint": "eslint . --ext .js,.ts"
15
16
  },
16
- "keywords": ["speckle", "aec", "speckle api"],
17
+ "keywords": [
18
+ "speckle",
19
+ "aec",
20
+ "speckle api"
21
+ ],
17
22
  "author": "AEC Systems",
18
- "license": "Apache-2.0"
23
+ "license": "Apache-2.0",
24
+ "devDependencies": {
25
+ "eslint": "^8.11.0",
26
+ "eslint-config-prettier": "^8.5.0",
27
+ "prettier": "^2.5.1",
28
+ "undici": "^4.14.1"
29
+ },
30
+ "gitHead": "78dc2b89ab4864e3e97988417bab06c906ef454a"
19
31
  }
package/readme.md CHANGED
@@ -10,9 +10,11 @@ Comprehensive developer and user documentation can be found in our:
10
10
 
11
11
  ## Getting started
12
12
 
13
- This is a small utility class that helps you stream an object and all its sub-components from the Speckle Server API. It is inteded to be used in contexts where you want to "download" the whole object, or iteratively traverse its whole tree.
13
+ This is a small utility class that helps you stream an object and all its sub-components from the Speckle Server API. It is intended to be used in contexts where you want to "download" the whole object, or iteratively traverse its whole tree.
14
14
 
15
- Here's a sample way on how to use it, pfilfered from the [3d viewer package](../viewer):
15
+ ### In the browser
16
+
17
+ Here's a sample way on how to use it, pilfered from the [3d viewer package](../viewer):
16
18
 
17
19
  ```js
18
20
 
@@ -36,23 +38,36 @@ async load( { serverUrl, token, streamId, objectId } ) {
36
38
 
37
39
  ```
38
40
 
39
- If you do not want to process the objects one by one as they are streamed to you, you can use the `getAndConstructObject()` method. Here's an example:
41
+ If you do not want to process the objects one by one as they are streamed to you, you can use the `getAndConstructObject()` method. Here's an example:
40
42
 
41
- ```js
43
+ ````js
42
44
 
43
45
  let loader = new ObjectLoader( {
44
- serverUrl: "https://latest.speckle.dev",
45
- streamId: "3ed8357f29",
46
+ serverUrl: "https://latest.speckle.dev",
47
+ streamId: "3ed8357f29",
46
48
  objectId: "0408ab9caaa2ebefb2dd7f1f671e7555",
47
49
  options: {
48
50
  fullyTraverseArrays: false, // Default: false. By default, if an array starts with a primitive type, it will not be traversed. Set it to true if you want to capture scenarios in which lists can have intersped objects and primitives, e.g. [ 1, 2, "a", { important object } ]
49
- excludeProps: [ 'displayValue', 'displayMesh', '__closure' ] // Default: []. Any prop names that you pass in here will be ignored from object construction traversal.
51
+ excludeProps: [ 'displayValue', 'displayMesh', '__closure' ] // Default: []. Any prop names that you pass in here will be ignored from object construction traversal.
50
52
  }
51
53
  } )
52
54
 
53
55
  let obj = await loader.getAndConstructObject( ( e ) => console.log( 'Progress', e ) )
54
56
 
55
- ```
57
+ ### On the server
58
+
59
+ Since Node.js does not yet support the [`fetch API`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch), you'll need to provide your own `fetch` function in the options object. Note that `fetch` must return a [Web Stream](https://nodejs.org/api/webstreams.html), so [node-fetch](https://github.com/node-fetch/node-fetch) won't work, but [node/undici's](https://undici.nodejs.org/) implementation will.
60
+
61
+ ```js
62
+ import { fetch } from 'undici'
63
+
64
+ let loader = new ObjectLoader({
65
+ serverUrl: 'https://latest.speckle.dev',
66
+ streamId: '3ed8357f29',
67
+ objectId: '0408ab9caaa2ebefb2dd7f1f671e7555',
68
+ options: { enableCaching: false, excludeProps: [], fetch },
69
+ })
70
+ ````
56
71
 
57
72
  ## Community
58
73