@speckle/objectloader 2.3.0 → 2.4.0

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.
@@ -1 +1,24 @@
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
+ let loader = new ObjectLoader({
7
+ serverUrl:"https://latest.speckle.dev",
8
+ streamId:"3ed8357f29",
9
+ objectId:"0408ab9caaa2ebefb2dd7f1f671e7555",
10
+ options:{ enableCaching: false, excludeProps:[], fetch }
11
+ })
12
+
13
+
14
+ let loadData = async function loadData() {
15
+
16
+ let obj = await loader.getAndConstructObject((e) =>{
17
+ console.log(e) // log progress!
18
+ })
19
+
20
+ console.log('Done!')
21
+ console.log( obj )
22
+ }
23
+
24
+ loadData()
package/index.js CHANGED
@@ -10,7 +10,7 @@ export default class ObjectLoader {
10
10
  * Creates a new object loader instance.
11
11
  * @param {*} param0
12
12
  */
13
- constructor( { serverUrl, streamId, token, objectId, options = { enableCaching: true, fullyTraverseArrays: false, excludeProps: [ ] } } ) {
13
+ constructor( { serverUrl, streamId, token, objectId, options = { enableCaching: true, fullyTraverseArrays: false, excludeProps: [ ], fetch:null } } ) {
14
14
  this.INTERVAL_MS = 20
15
15
  this.TIMEOUT_MS = 180000 // three mins
16
16
 
@@ -48,6 +48,13 @@ export default class ObjectLoader {
48
48
  this.lastAsyncPause = Date.now()
49
49
  this.existingAsyncPause = null
50
50
 
51
+ // we can't simply bind fetch to this.fetch, so instead we have to do some acrobatics: https://stackoverflow.com/questions/69337187/uncaught-in-promise-typeerror-failed-to-execute-fetch-on-workerglobalscope#comment124731316_69337187
52
+ this.preferredFetch = options.fetch
53
+ this.fetch = function(...args) {
54
+ let currentFetch = this.preferredFetch || fetch
55
+ return currentFetch(...args)
56
+ }
57
+
51
58
  }
52
59
 
53
60
  async asyncPause() {
@@ -329,7 +336,7 @@ export default class ObjectLoader {
329
336
  readBuffers.push( '' )
330
337
  finishedRequests.push( false )
331
338
 
332
- fetch(
339
+ this.fetch(
333
340
  this.requestUrlChildren,
334
341
  {
335
342
  method: 'POST',
@@ -397,7 +404,7 @@ export default class ObjectLoader {
397
404
  const cachedRootObject = await this.cacheGetObjects( [ this.objectId ] )
398
405
  if ( cachedRootObject[ this.objectId ] )
399
406
  return cachedRootObject[ this.objectId ]
400
- const response = await fetch( this.requestUrlRootObj, { headers: this.headers } )
407
+ const response = await this.fetch( this.requestUrlRootObj, { headers: this.headers } )
401
408
  const responseText = await response.text()
402
409
  this.cacheStoreObjects( [ `${this.objectId}\t${responseText}` ] )
403
410
  return responseText
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@speckle/objectloader",
3
- "version": "2.3.0",
3
+ "version": "2.4.0",
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",
@@ -15,5 +15,8 @@
15
15
  },
16
16
  "keywords": ["speckle", "aec", "speckle api"],
17
17
  "author": "AEC Systems",
18
- "license": "Apache-2.0"
18
+ "license": "Apache-2.0",
19
+ "devDependencies": {
20
+ "undici": "^4.14.1"
21
+ }
19
22
  }
package/readme.md CHANGED
@@ -12,7 +12,9 @@ Comprehensive developer and user documentation can be found in our:
12
12
 
13
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
 
@@ -52,6 +54,19 @@ let loader = new ObjectLoader( {
52
54
 
53
55
  let obj = await loader.getAndConstructObject( ( e ) => console.log( 'Progress', e ) )
54
56
 
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
+ })
55
70
  ```
56
71
 
57
72
  ## Community