hyperhyperspace / hyperhyperspace-core

A library to create p2p applications, using the browser as a full peer.
https://www.hyperhyperspace.org
MIT License
195 stars 12 forks source link

ES Module support #20

Closed jed1976 closed 2 years ago

jed1976 commented 2 years ago

Hello, I was wondering if you have any plans to provide an ES module that could be requested via a CDN?

I am having trouble building this on my local machine because it looks like the wrtc library only runs on Node 14 and under. I am on a machine that is running Node 15 currently.

This library looks fantastic and would love to explore it more!

sbazerque commented 2 years ago

Hello @joedakroub !

Just updated @hyper-hyper-space/node-env's repo to use the latest version of wrtc (0.4.7), which apparently supports node 15, but I do not have v15 installed to actually check. What I do know is that wrtc will not compile under the new apple chips (but that can be worked around by using node through the x86 emulator).

Maybe you can check out https://github.com/hyperhyperspace/hyperhyperspace-node-env and see if you can build it.

About ES modules support, there's the version that we use for the playgroynd, but I think this is a plain javascript file rather than a module:

https://www.hyperhyperspace.org/dist-browser/hhs.js

We are building that using rollup, maybe a different rollup config could produce what you need?

jed1976 commented 2 years ago

Thank you @sbazerque, I will checkout the version used in the playground for now.

Yes, I think a different rollup config that produces an ES module is doable. However, I wasn't able to npm install yet to get that far.

sbazerque commented 2 years ago

I'll leave this issue open as a reminder we need to create a 2nd set-up for rollup that produces a ES module.

Please ping back if the file from the playground does not work.

While this is WIP, I think we need to streamline things so folks can actually use the library. I'll also make new releases to NPM before the end of the month.

jed1976 commented 2 years ago

Thanks so much @sbazerque. I am able to load the library to test with. I was trying to load this lib via skypack or esm.sh, but then discovered that it doesn't compile on those sites. This is helpful and an ES module would really help with being to load your library from sites like skypack, esm, unpkg, etc.

jed1976 commented 2 years ago

When trying the Person example I am using the following code:

class Person extends HHS.HashedObject {
  static className = "hs/Person";

  constructor(name, age) {
    super();
    this.name = name;
    this.age = age;
  }

  validate(_references) {
    return this.name !== undefined && this.age !== undefined;
  }

  getClassName() {
    return Person.className;
  }
}

const p = new Person("Joe", Date.now())

const store = new HHS.Store(new HHS.IdbBackend("test"));
store.save(p);

console.log(
  await store.load("1KxD4lUkLepzv8mcdzPpSI39hOw=")
);

When I run this, I get the following error:

Uncaught Error: A local implementation of class 'Person' is necessary to deliteralize 1KxD4lUkLepzv8mcdzPpSI39hOw=
    at Function.deliteralizeInContext (hhs.js:2665:23)
    at Function.fromContext (hhs.js:2636:26)
    at Store.loadWithContext (hhs.js:13260:36)
    at async navigator.js:25:13

Looks like ClassRegistry is not included in https://www.hyperhyperspace.org/dist-browser/hhs.js

jed1976 commented 2 years ago

I installed nvm and was able to install core using yarn. When I build the browser version, it includes the ClassRegistry now, so I am better able to test.

sbazerque commented 2 years ago

Yes, sorry about that @joedakroub, good catch!

In the beforetime, when the published version of hhs.js was built, the class registry was part of HashedObject, and used to be called like this: HashedObject.registerClass(...

We need to release new versions to NPM and rebuild the playground :)

sbazerque commented 2 years ago

Just uploaded a newly built version of hhs.js to http://www.hyperhyperspace.org/dist-browser/hhs.js.

This version has the latest code, and doesn't need a Buffer polyfill anymore.

jed1976 commented 2 years ago

Thanks so much @sbazerque! Looking forward to trying it out soon.