remotestorage / remotestorage.js

⬡ JavaScript client library for integrating remoteStorage in apps
https://remotestoragejs.readthedocs.io
MIT License
2.3k stars 141 forks source link

Migrating to 2.0.0-beta.6 issues #1288

Closed rosano closed 1 year ago

rosano commented 1 year ago

I'm finally trying to migrate from 1.x and am having some trouble with my libraries that include rs.js via nodejs. Basically my mocha tests are all timing out because they're waiting for rs.js to return or resolve promises.

It seems to me that on nodejs:

Any pointers as to what might have changed for nodejs?

raucao commented 1 year ago

Hmm, I know at least one instance of 2.0 (beta.3) running with node.js in production:

https://github.com/67P/hubot-remotestorage-logger/blob/master/package.json#L22

Can you link the code to reproduce it perhaps?

rosano commented 1 year ago

It seems to me that in the following code:

var Bookmarks = { name: 'bookmarks', builder: function(privateClient, publicClient) {
  return {
    exports: {
        addBookmark: function(bookmark) {
            return privateClient.storeFile('application/json', bookmark.id, bookmark);
        }
    }
  }
}};

const remoteStorage = new RemoteStorage({ modules: [ Bookmarks ] });

'not-connected,connected,disconnected,error,features-loaded,connecting,authing,wire-busy,wire-done,sync-req-done,sync-done,network-offline,network-online,sync-interval-change'.split(',').forEach(function (e) {
    remoteStorage.on(e, function() {
      console.log('did emit ' + e);
    });
});

remoteStorage.access.claim('bookmarks', 'rw');

console.log('did write', await remoteStorage.bookmarks.addBookmark({
    id: 'alfa',
  url: 'http://unhosted.org',
  description: 'Unhosted Adventures',
  tags: ['unhosted', 'remotestorage', 'no-backend']
}));

Here is a small package if that's helpful: Archive.zip

I hotfixed the release/remotestorage.js file (replacing exports=XMLHttpRequest with exports=(typeof XMLHttpRequest === 'undefined' ? null : XMLHttpRequest)) to avoid crashing on nodejs, maybe it wasn't a good idea?

raucao commented 1 year ago

Looks like we lost the documentation about bringing your own XMLHttpRequest or Fetch when using node.js. Here's an adapted version, which also logs and uses the ready event and it works just fine on node.js 16:

https://gist.github.com/raucao/c2d3c4d95fbd9bc138df13010b891a32

This updated version also shows that the bookmark is there (in memory).

DougReeder commented 1 year ago

Our documentation should also note that Node v18 includes fetch natively.

rosano commented 1 year ago

Ok, should I try a PR to check for node < 18 and at log/warn/throw? Or is this the kind of thing that should only be in documentation?

raucao commented 1 year ago

Either way, I think it should be documented in https://remotestoragejs.readthedocs.io/en/latest/nodejs.html.