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

@mewhhaha/little-worker

v0.1.7

Published

Bundle of useful packages when working with a cloudflare worker

Downloads

4

Readme

Little Worker

This is mostly convenience package that bundles most of the packages in this repository, so there's only one import required. But this also includes a CLI to generate routes and put them in a router from a folder structure.

Re-exports the following packages:

  • @mewhhaha/typed-response
  • @mewhhaha/typed-request
  • @mewhhaha/little-fetcher
  • @mewhhaha/little-router
  • @mewhhaha/json-string

Adds the following utilities since I keep re-using them in a lot of workers:

// Sign data
hmac("secret key data", "message");

// Encode as trimmed base64
encode("hello world");

// Decode trimmed bse64
decode("hello world");

// Assert invariants in code
invariant(1 === 1, "1 is always equal to 1");

// Creating tagged type from other types that are distinguishable from each other
type DateISOString = TaggedType<string, "DateISOString">;

CLI to generate routes

Routes can be generated by using the little-worker routes [--target app/routes] command. This will generate a _router.ts and a _pattern.ts file in the target folder based on the files therein. The default target folder is app/routes. Any files fitting the format [method].[segment].$[parameter].ts will be included in the router.

The dots (.) will be transformed into slashes (/) and the dollar signs ($) will be changed into colons (:). This transformation will make it match the format in the router.

Ending with a dollar sign ($) will be interpreted as a splat route, and converted to a asterisk (*)

Square brackets ([, ]) in a segment will escape any inside characters that would normally get removed or interpreted differently, for example this is useful when the route includes a dot (.) for a file ending.

Also supports flat folders containing a route.ts or a route.tsx file. This can be helpful to co-locate code to a specific route in separate files.

Here are some examples of files and their generated equivalences in the router:

  • get.users.ts => .get("/users", ...)
  • get.users.$userId.ts => .get("/users/:userId", ...)
  • get.users.$userId.bananas.ts => .get("/users/:userId/bananas", ...)
  • get.$.ts => .get("/*", ...)
  • post.users.ts => .post("/users", ...)
  • all.users.ts => .all("/users", ...)
  • get.file.[manifest.js] => .get("/file/manifest.js", ...)

These will be automatically sorted when the router is generated to make sure that they appear in the correct order of specificity.

Define a route

In each of the files you created it is expected that you export a route definition as the default export. You can import the function route from @mewhhaha/little-worker (or @mewhhaha/little-router) that you can use to properly type your export.

Use the eslint plugin from @mewhhaha/little-worker/eslint-plugin to ensure that the route is always correct.

// In file get.users.$userId.ts
import { route, ok } from "@mewhhaha/little-worker";

export default route("/get/users/:userId", [], ({ params }) => {
  // Notice how we can access userId typed in this context
  return ok(200, { id: params.userId });
});

Ensure correctness with lint

// eslint.config.mjs
import worker from "@mewhhaha/little-worker/eslint-plugin";

export default [worker];