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

vercel-dart-runtime

v1.1.3-beta.2

Published

Vercel Runtime for Dart serverless functions

Downloads

11

Readme

A Vercel Runtime to write serverless functions in Dart.

Usage

A serverless function file should contain a shelf handler named handler. Behind the scenes, the runtime will combine the handler with an AWS runtime and compile it into a binary file using dart2native.

Example

Create a file called api/hello.dart with the following contents.

import 'dart:io' show Platform;
import 'package:shelf/shelf.dart';

Response handler(Request req) {
  final version = Platform.version.split(" ").first;
  return Response.ok('Hello, from Dart v$version!');
}

Next, select the vercel-dart-runtime runtime to handle serverless dart functions in your vercel.json file.

{
  "functions": {
    "api/**/*.dart": { "runtime": "[email protected]" }
  }
}

Demo: vercel-dart-runtime.vercel.app/api/hello

Examples

Check out the repository's api folder for more examples.

Reading Queries

import 'package:shelf/shelf.dart';

Response handler(Request req) {
  return Response.ok('Query is: ${req.requestedUri.query}');
}

Demo: vercel-dart.vercel.app/api/query

Custom Status Codes

import 'package:shelf/shelf.dart';

Response handler(Request req) =>
    Response.movedPermanently('https://youtu.be/dQw4w9WgXcQ');
    
// Response.movedPermanetly -> 301
// Response.notFound -> 404
// etc.

Demo: vercel-dart.vercel.app/api/redirect

Custom Content Types

JSON

import 'dart:convert';
import 'dart:io';
import 'package:shelf/shelf.dart';

Response handler(Request req) {
  final data = {
    'host': req.requestedUri.host,
    'path': req.requestedUri.path,
    'query': req.requestedUri.queryParameters
  };

  return Response.ok(
    jsonEncode(data),
    headers: {
      'content-type': ContentType.json.toString(),
    },
  );
}

Demo: vercel-dart.vercel.app/api/json

HTML

import 'dart:io';
import 'package:shelf/shelf.dart';

Response handler(Request req) {
  final html = '''
    <h1>Hello, World</h1>
  ''';

  return Response.ok(
    html,
    headers: {
      'content-type': ContentType.html.toString(),
    },
  );
}

Demo: vercel-dart.vercel.app/api/html

Configuration

Here are the build environment variables that you may configure for your serverless functions.

| Name | Description | Default | |----------------|-------------------------------------------------------|----------| | DART_CHANNEL | The dart channel that serverless function will use. | stable | | DART_VERSION | The dart version that serverless function will use. | 2.10.5 |

Note that you need to use dart version >= 2.6 for dart2native to work.

Learn more about dart channels and versions here.

FAQ

Can I use pubspec.yaml?

Yes, just make sure it is at the same directory level as the function. The runtime will automatically run pub get before building the binary file.

Is it fast?

I'm not sure because I don't know how to benchmark. But according to my experience, it feels like it's really fast. I may be biased so help me benchmark it if you know how to.

Can I run it with vercel dev locally?

Yes, but because it won't install dart during development, make sure your machine has a working version of dart >= 2.6 installed properly.

Acknowledgement

While I was trying to build this, because I didn't any prior experience with things, a lot of open-source libraries/projects were very useful to me. The prominent ones of them are now-rust, vercel-bash, vercel-deno and aws-lambda-dart-runtime.

License

MIT © Frenco