dwn-sdk-js icon indicating copy to clipboard operation
dwn-sdk-js copied to clipboard

Web app/browser usage of library

Open sondreb opened this issue 3 years ago • 5 comments

I'm attempting to rely on the library in an Angular project and not able to get it working when importing through the code base. Made another attempt by loading the full bundle, but that has some eval() calls which I won't allow in my key-management app.

The issue by importing the library directly, is related to use of import using JSON files. The resolveJsonModule and esModuleInterop options for TypeScript is not enough to make it work, unfortunately.

image

For anyone else looking into this and allows eval execution, you can include the bundle in angular.json using the scripts section:

"scripts": ["./node_modules/@tbd54566975/dwn-sdk-js/dist/bundles/index.cjs"],

image

Right now the library is used to host DWN instance and I don't think the focus is on consumer library yet? Is the plan to split some of the logic in a separate library that is cross-compatible with Node.js/Web and doesn't include the service-processing logic of the DWN instance?

sondreb avatar Oct 19 '22 13:10 sondreb

This was intended to be cross-env, so let's see if we can get that eval-using lib out of the deps.

csuwildcat avatar Oct 19 '22 13:10 csuwildcat

For TypeScript, I needed to set "module": "ESNext" in tsconfig.json under compilerOptions. See if that helps.

thehenrytsai avatar Oct 20 '22 00:10 thehenrytsai

For TypeScript, I needed to set "module": "ESNext" in tsconfig.json under compilerOptions. See if that helps.

Already tried that with no luck. I have previously used the import JSON feature within the same app, but this is first time I have an external dependency that does it and not sure why it fails. I'll make a small repo that reproduce the problem with basic skeleton of an Angular app.

sondreb avatar Oct 20 '22 13:10 sondreb

Repo of basic Angular app that imports the dwn-sdk-js and fails to compile: https://github.com/sondreb/angular-dwn

sondreb avatar Oct 20 '22 14:10 sondreb

Hey @sondreb, I am looking into this issue. I published a temporary release 0.0.10-unstable-20221028-2 to work around the assertion issue, but also saw the following issue when ng build:

image

Did you ever work around the polyfill issue above?

A workaround would be to just:

npm install util

thehenrytsai avatar Oct 28 '22 19:10 thehenrytsai

The main issue is that the whole repo depends on Node.js packages that won't run properly in the web browser, like levelup which includes C++ binding for LevelDB. I don't know which other dependencies won't run in the browser, but that doesn't have to be an issue as especially the ESM versions should allow for tree-shaking for consumers of the library, but I'm not 100% sure. Best would be to have separate library for the core DID/JWS/CID, etc.

sondreb avatar Oct 31 '22 13:10 sondreb

Hi @sondreb, @mistermoe checked in a PR to fix the package bundle meant to be used in the browser.

I have made a PR against your repo which shows that the issue is fixed https://github.com/sondreb/angular-dwn/pull/1 (I hope) with dummy usage of the DWN which should use browser-level under the hood.

The next SDK release will include this fix.

thehenrytsai avatar Oct 31 '22 21:10 thehenrytsai

Great work, thanks a lot! And I just noticed that the leveldb works cross-platform (Node/Web) as well, that's fantastic:

image

sondreb avatar Oct 31 '22 22:10 sondreb