npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jacobmischka/now-rust

v0.2.11

Published

Community based builder for using rust on the now/zeit platform

Downloads

15

Readme

now-rust

Community based builder for using rust on the now/zeit platform

This is a now builder which allows you to run your rust code as lambdas on the now platform!

This was originally provided officially by ZEIT's now-builders monorepo, but has since been moved to a community-maintained project.

Usage

If you're unfamiliar with now builders, please read the builder docs first. To use this builder, you can use it as you would use any other builder.

{
	"version": 2,
	"builds": [{ "src": "Cargo.toml", "use": "now-rust" }]
}

That's the simplest way to use this builder! Below you'll find more complex and advanced patterns.

Entry point

The entry point file can either be a .rs source file or a Cargo.toml file.

.rs entrypoint

When you use one or multiple .rs files as an entry point for this Builder, Now will setup the serverless environment for you.

The requirements for this entry point is to expose a handler function and not to have a main function.

If a Cargo.toml exists in the project relating to the entry point, the dependencies will be installed for the Rust project.

Cargo.toml entrypoint

When using a Cargo.toml file as an entry point for this Builder, Now will use cargo read-manifest to build each binary within the project. As a result, cargo workspaces` are not supported as an entry point for Now—you should read the cargo workspace workaround for further information.

This entry point method is an advanced method of using this Builder and requires Rust files to assemble their own runtimes.

Defining a Cargo.toml file as an entry point requires a Rust file at src/main.rs or files defined as a [[bin]] target.

An example src/main.rs Rust file within a project including a Cargo.toml file acting as the entry point:

use http::{StatusCode, header};
use now_lambda::{error::NowError, lambda, IntoResponse, Request, Response};
use std::error::Error;

fn handler(request: Request) -> Result<impl IntoResponse, NowError> {
	let uri = request.uri();
	let response = Response::builder()
		.status(StatusCode::OK)
		.header(header::CONTENT_TYPE, "text/html")
		.body(format!(
				"You made a request to the following URL: {}",
				uri
		))
		.expect("failed to render response");

	Ok(response)
}

// Start the runtime with the handler
fn main() -> Result<(), Box<dyn Error>> {
	Ok(lambda!(handler))
}

This requires one dependency, with the example above using another dependency, http.

The required dependency is now_lambda which provides all of the resources needed to provide the serverless runtime.

The Cargo.toml entry point for the example above is the following:

[package]
name = "rust-project"
version = "0.1.0"
edition = "2018"

[dependencies]
http = "0.1"
now_lambda = "0.1"

Rust version

This builder uses rustup to install rust and cargo. By default, the latest stable version of rust will be installed. To see what the current stable version of rust is, please see the official website.

If you need to use a different version of rust other than the latest stable version, you can specify a version of rust in your build's configuration. Accepted values are the same as rustup's channel definition, which is stable | latest | nightly | <version>.

{
	"version": 2,
	"builds": [
		{ "src": "Cargo.toml", "use": "now-rust", "config": { "rust": "1.31" } }
	]
}

Dependencies

This Builder supports installing dependencies defined in the Cargo.toml file.

Furthermore, more system dependencies can be installed at build time with the presence of a shell build.sh file in the same directory as the entry point file.

By default, openssl is installed by the Builder due to its common usage with Rust projects.

FAQ

Are cargo workspaces supported?

Not quite. Cargo's workspaces feature is a great tool when working on multiple binaries and libraries in a single project. If a cargo workspace is found in the entrypoint, however, now-rust will fail to build.

To get around this limitation, create build entries in your now.json file for each Cargo.toml that represents a lambda function within your workspace. In your .nowignore, you'll want to add any binary or library project folders that aren't needed for your lambdas to speed up the build process like your Cargo.toml workspace.

It's also recommended to have a Cargo.lock alongside your lambda Cargo.toml files to speed up the build process. You can do this by running cargo check or a similar command within each project folder that contains a lambda.

If you have a compelling case for workspaces to be supported by now-rust which are too cumbersome with this workaround, please submit an issue! We're always looking for feedback.

How do I use this during local development?

The now dev command allows you to develop lambdas locally on your machine. With now dev and now-rust you can develop your rust-based lamdas on your own machine.

During local development with now dev, the assumption is that rust and cargo are already installed and available in your PATH since they will not be installed automatically. The recommended way to install rust and cargo on your machine is with rustup.

Can I use musl/static linking?

Unfortunately, the AWS lambda runtime for rust relies (tangentially) on proc_macro, which won't compile on musl targets. Without musl, all linking must be dynamic. If you have a crate that relies on system libraries like postgres or mysql, you can include those library files with the includeFiles config option and set the proper environment variables, config, etc. that you need to get the library to compile.

For more info, please see issue #2.

Why does this project use tabs over spaces?

Please refer to this tweet.

Contibuting

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

Issues and pull requests are welcome!

Setup

Since this project contains both rust and node code, you need to install the relevant dependencies. If you're only working on the javascript side, you only need to install those dependencies. The oppoosite is true for the rust side.

# install node dependencies
npm install

# install cargo dependencies
cargo fetch

At this point, you're all set up and can start making edits!

Note: During the migration period, tests will be broken until we get CI set up!

License

MIT

Contributors ✨

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification. Contributions of any kind welcome!