Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add web target to pkg builder in makefile #39

Closed
wants to merge 1 commit into from
Closed

Conversation

theref
Copy link
Contributor

@theref theref commented Dec 14, 2022

Aims to fix nucypher/taco-web#120

@codecov-commenter
Copy link

Codecov Report

Merging #39 (4bcf876) into main (300cb8d) will decrease coverage by 3.70%.
The diff coverage is n/a.

@@            Coverage Diff             @@
##             main      #39      +/-   ##
==========================================
- Coverage   16.45%   12.75%   -3.71%     
==========================================
  Files          12       12              
  Lines         468      447      -21     
==========================================
- Hits           77       57      -20     
+ Misses        391      390       -1     
Impacted Files Coverage Δ
nucypher-core/src/reencryption.rs 11.59% <0.00%> (-19.87%) ⬇️
nucypher-core/src/message_kit.rs 0.00% <0.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@piotr-roslaniec
Copy link
Contributor

I assume this must be related to nucypher/taco-web#120

Does this technique fix the aforementioned issue? Did we test that locally?

@theref
Copy link
Contributor Author

theref commented Dec 14, 2022

I assume this must be related to nucypher/nucypher-ts#120

Does this technique fix the aforementioned issue? Did we test that locally?

yeah that's what I'm trying out now

@theref
Copy link
Contributor Author

theref commented Dec 14, 2022

If you run make pkg in nucypher-core-wasm it will produce the required output in nucypher-core-wasm/pkg/pkg-web

I added a simple test.html within the same directory:

<html>
  <head>
    <script src="./nucypher_core_wasm.js"></script>
  </head>
  <body>
    <script>
      fetch("./nucypher_core_wasm_bg.wasm")
        .then(response => response.arrayBuffer())
        .then(buffer => WebAssembly.instantiate(buffer))
        .then(wasm => {
          const cond = Condition();
        });
    </script>
  </body>
</html>

This can then be opened with python -m SimpleHTTPServer (to avoid CORS nonsense). Unfortunately, I then get the error: Uncaught SyntaxError: Unexpected token 'export' (at nucypher_core_wasm.js:219:1)

I'm not really sure why the export is unexpected @piotr-roslaniec

@piotr-roslaniec
Copy link
Contributor

@theref
It probably means that the browser doesn't recognize the module structure of nucypher_core_wasm.js file.
We could try fixing it by adding a script type attribute:

<script type="module" src="./nucypher_core_wasm.js"></script>

We can find confirmation for this workaround in wasm-pack example for --target web compilation target.

@theref
Copy link
Contributor Author

theref commented Dec 15, 2022

@piotr-roslaniec Thank you, that link was really helpful and everything is working!

test.html:

<html>
  <head>
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type"/>
  </head>
  <body>
    <script type="module">
      import init, { PublicKey } from './nucypher_core_wasm.js';

      async function run() {  
        await init();
        const pk = new PublicKey();
        console.log(pk);
      }
      run();
    </script>
  </body>
</html>

There is a bit of work to do to get the MIME types working correctly with the server. So create wasm_server.py:

from http.server import HTTPServer, SimpleHTTPRequestHandler

class MyRequestHandler(SimpleHTTPRequestHandler):
    # Set the MIME type for .wasm files to application/wasm
    MIMETypes = {
        ".wasm": "application/wasm"
    }

# Start the HTTP server on port 8000
httpd = HTTPServer(('localhost', 8000), MyRequestHandler)
httpd.serve_forever()

And then python3 wasm_server.py, open test.html and in the console:
Screenshot 2022-12-15 at 11 53 01

@piotr-roslaniec
Copy link
Contributor

@theref I'm glad to see that it works. Could we also get a confirmation that it simplifies nucypher-ts loading? I.e. re-use this setup to load newly packaged nucypher-core as a local dependency to nucypher-ts and try to load nucypher-ts into a webpage without WASM-aware bundler.

@piotr-roslaniec
Copy link
Contributor

Rebased over #40

@fjarri
Copy link
Contributor

fjarri commented Jan 17, 2023

Needs a few minor things:

  • a changelog entry
  • add the new directory to package.template.json

Copy link
Contributor

@piotr-roslaniec piotr-roslaniec left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's get it in 👌

@fjarri
Copy link
Contributor

fjarri commented Jan 17, 2023

@theref, could you put the changes under Unreleased section in the changelog? Note that 0.5.0 has been released already, so perhaps rebase too. I will make a patch release shortly since I need to add some stuff to satisfy nucypher/nucypher#3049

@fjarri
Copy link
Contributor

fjarri commented Jan 17, 2023

Disregard that, merged in #42

@fjarri fjarri closed this Jan 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make easy the WASM artifact load from nucypher-core
4 participants