Giter Club home page Giter Club logo

rustwasm-worker-template's Introduction

Getting Started

A template for kick starting a Cloudflare worker project using workers-rs.

This template is designed for compiling Rust to WebAssembly and publishing the resulting worker to Cloudflare's edge infrastructure.

Usage

This template starts you off with a src/lib.rs file, acting as an entrypoint for requests hitting your Worker. Feel free to add more code in this file, or create Rust modules anywhere else for this project to use.

With wrangler, you can build, test, and deploy your Worker with the following commands:

# compiles your project to WebAssembly and will warn of any issues
wrangler build 

# run your Worker in an ideal development workflow (with a local server, file watcher & more)
wrangler dev

# deploy your Worker globally to the Cloudflare network (update your wrangler.toml file for configuration)
wrangler publish

Read the latest worker crate documentation here: https://docs.rs/worker

WebAssembly

workers-rs (the Rust SDK for Cloudflare Workers used in this template) is meant to be executed as compiled WebAssembly, and as such so must all the code you write and depend upon. All crates and modules used in Rust-based Workers projects have to compile to the wasm32-unknown-unknown triple.

Read more about this on the workers-rs project README.

Issues

If you have any problems with the worker crate, please open an issue on the upstream project issue tracker on the workers-rs repository.

rustwasm-worker-template's People

Contributors

ashleygwilliams avatar everlastingbugstopper avatar exvuma avatar gnp avatar ispivey avatar mbstavola avatar nilslice avatar rita3ko avatar seokminhong avatar steveklabnik avatar vladinator1000 avatar zebp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rustwasm-worker-template's Issues

Package fails to build unless authors attribute is specified

By default, wrangler populates the Cargo.toml with an author attribute under [package].

However, when attempting to build a project, it seems as though wrangler expects an authors attribute instead.

See:
Screen Shot 2019-03-28 at 8 49 33 PM

If you fix the attribute, it'll actually build.

tests not working with the default setup

I couldn't get tests to work without:

  • changing wasm-bindgen-test = "0.2" to wasm-bindgen-test = "0.3" (with v0.2 I just get no tests to run!)
  • and, either removing --headless and running them manually in a browser or using wasm-pack test --chrome --headless

panicked at 'already borrowed: BorrowMutError'

Hi πŸ‘‹

Not sure this is the right place for this issue but figured it would be a good start.

I have found that under high load accessing Cloudflare KV and Cache API from Rust causes the following panic:

panicked at 'already borrowed: BorrowMutError',
/Users/graemer/.cargo/registry/src/github.com-1ecc6299db9ec823/wasm-bindgen-futures-0.4.23/src/lib.rs:133:39

A sample project to reproduce this can be found cfw-borrowmuterror. To simulate load I have been using wrk, for example:

wrk -c 128 -t 4 -d 60s --latency https://<host>/rs

I am then using wrangler tail to capture the logs. I can see a combination of the following:

{"exceptions":[{"name":"Error","message":"The script will never generate a response.","timestamp":1621326408885}],"logs":[]}
{"exceptions":[{"name":"Error","message":"Promise will never complete.","timestamp":1621326409567}],"logs":[]}

...and if you are lucky:

{"exceptions":[{"name":"Error","message":"The script will never generate a response.","timestamp":1621326408887}],"logs":[{"message":["panicked at 'already borrowed: BorrowMutError', /Users/graemer/.cargo/registry/src/github.com-1ecc6299db9ec823/wasm-bindgen-futures-0.4.23/src/lib.rs:133:39\n\nStack:\n\nError\n    at imports.wbg.__wbg_new_59cb74e423758ede (worker.js:288:19)\n    at <anonymous>:wasm-function[28]:0x3a77\n    at <anonymous>:wasm-function[240]:0x946b\n    at <anonymous>:wasm-function[64]:0x686b\n    at <anonymous>:wasm-function[85]:0x75f4\n    at <anonymous>:wasm-function[160]:0x8cd5\n    at <anonymous>:wasm-function[139]:0x88d5\n    at <anonymous>:wasm-function[162]:0x8d38\n    at <anonymous>:wasm-function[91]:0x78c0\n    at <anonymous>:wasm-function[60]:0x6539\n\n"],"level":"error","timestamp":1621326408887}]}

The sample project has two endpoints:

  • /js which fetches the foo key using just JavaScript
  • /rs which does the same, but using Rust

Applying the same load to the /js endpoint always works. Only the /rs endpoint exhibits the issue.

I would love to be able to dig further but at the current limits of my knowledge on how to proceed. Any help gratefully received.

is repo abandoned?

I was about to fork repo & do some updates, but I see 6 pending PRs that make some of the fixes I was going to work on -- updating wrangler, dependencies, etc.

Has Cloudflare abandoned this repository?

Failed to find "undefined" in <github URL>

wrangler generate example https://github.com/cloudflare/rustwasm-worker-template                                           ⛅️ wrangler 2.11.0
--------------------
Creating a worker in example from https://github.com/cloudflare/rustwasm-worker-template

✘ [ERROR] Failed to find "undefined" in https://github.com/cloudflare/rustwasm-worker-template.git


If you think this is a bug then please create an issue at https://github.com/cloudflare/workers-sdk/issues/new/choose

Seems more likely to be an issue with the template than wrangler itself.

Could not resolve "./index_bg.wasm"

using wrangler 3.4.0, wasm-pack 0.12.1, and this template.

✘ [ERROR] Could not resolve "./index_bg.wasm"

    index_bg.js:1:22:
      1 β”‚ import * as wasm from './index_bg.wasm';
        β•΅                       ~~~~~~~~~~~~~~~~~

1 error
Error: esbuild exited with status exit status: 1

✘ [ERROR] Command failed with exit code 1: cargo install -q worker-build && worker-build --release

note that the template had a bunch of warnings so following them this is what I ended up with as wrangler.toml:

name = "..."
workers_dev = true
compatibility_date = "2022-01-20"
main = "build/worker/shim.mjs"

[vars]
WORKERS_RS_VERSION = "0.0.9"

[build]
command = "cargo install -q worker-build && worker-build --release" # required

[[rules]]
globs = ["**/*.wasm"]
type = "CompiledWasm"

wrangler dev fails with [API code: 10021]

Error: Something went wrong with the request to Cloudflare...
The compatibility flag formdata_parser_supports_files became the default as of 2021-11-03 so does not need to be specified anymore.
[API code: 10021]

When I run wrangler dev I got the above error. The fix is pretty clear, just removing the compatibility flag from the wrangler.toml.

"wrangler preview --watch" error

Hi,

I generate wrangler project with --type=rust, modified wrangler.toml accordingly and tried to launch:
wrangler preview --watch just to receive this output:

πŸŒ€  Compiling your project to WebAssembly...
[INFO]: 🎯  Checking for the Wasm target...
[INFO]: πŸŒ€  Compiling to Wasm...
    Finished release [optimized] target(s) in 0.06s
[INFO]: ⬇️  Installing wasm-bindgen...
[INFO]: Optional fields missing from Cargo.toml: 'description', 'repository', and 'license'. These are not necessary, but recommended
[INFO]: ✨   Done in 0.20s
[INFO]: πŸ“¦   Your wasm pkg is ready to publish at ./pkg.
Error: Your JavaScript project is missing a `package.json` file; is `./` the wrong directory?

What am I missing? Why is it looking for package.json if it’s a rustwasm project?

Thanks!

Can't get started easily, please add instructions to the readme on status and intended use.

I followed the getting started as introduced by https://developers.cloudflare.com/workers/tutorials/hello-world-rust/
and then ended up with wrangler 3.1.1.

I followed the rest of the tutorial, by doing a git clone of this repository and ended up with:

╰─>$ wrangler dev
 ⛅️ wrangler 3.1.1
------------------
wrangler dev now uses local mode by default, powered by πŸ”₯ Miniflare and πŸ‘· workerd.
To run an edge preview session for your Worker, use wrangler dev --remote
β–² [WARNING] Processing wrangler.toml configuration:

    - 😢 Ignored: "type":
      Most common features now work out of the box with wrangler, including modules, jsx,
  typescript, etc. If you need anything more, use a custom build.
    - Deprecation: "build.upload.format":
      The format is inferred automatically from the code.
    - Deprecation: "build.upload.main":
      Delete the `build.upload.main` and `build.upload.dir` fields.
      Then add the top level `main` field to your configuration file:
      ```
      main = "build/worker/shim.mjs"
      ```
    - Deprecation: "build.upload.dir":
      Use the top level "main" field or a command-line argument to specify the entry-point for the
  Worker.
    - Deprecation: The `build.upload.rules` config field is no longer used, the rules should be
  specified via the `rules` config field. Delete the `build.upload` field from the configuration
  file, and add this:
      ```
      [[rules]]
      globs = [ "**/*.wasm" ]
      type = "CompiledWasm"
      ```



✘ [ERROR] Processing wrangler.toml configuration:

    - Expected "name" to be of type string, alphanumeric and lowercase with dashes only but got
  "{{project-name}}".


If you think this is a bug then please create an issue at https://github.com/cloudflare/workers-sdk/issues/new/choose

For the deprecated fields, there are some PRs open already, so maybe it's time to say goodbye to wrangler v1 for this repository as it's meant as a template for the getting started guide (afaik)??

Secondly, the {{project-name}} templating variable is something that was probably meant to be fixed by using wrangler generate but that has already been deprecated in v3.

Would it be possible to update the README with the intentional use of this repo: should I clone and update, use a cookie cutter tool, what is the expected wrangler version?

Adding a `/` route for template

Hello πŸ‘‹

I've just tried workers with native Rust, and I'm much surprised by its blazingly fast speed! However, I was a little confused at the first trial because 404 Not found errors occurred when I tried to test.

So, in my opinion, how about adding a simple response or basic instructions for the / route?

Getting build error on wrangler generated code

I used the wrangler generator for this code:
wrangler generate wasm-worker https://github.com/cloudflare/rustwasm-worker-template.git

When I run wasm-pack build, I get the following error:

Fatal: error in validating input
Error: failed to execute `wasm-opt`: exited with exit code: 1
  full command: "/Users/su/Library/Caches/.wasm-pack/wasm-opt-a528729925722b63/wasm-opt" "/Users/su/Desktop/temp/wasm-worker/pkg/wasm_worker_bg.wasm" "-o" "/Users/su/Desktop/temp/wasm-worker/pkg/wasm_worker_bg.wasm-opt.wasm" "-O"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.