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

create-cloudflare-worker

v1.2.1

Published

Create a Cloudflare worker

Downloads

29

Readme

create-cloudflare-worker create-cloudflare-worker

A Starter Kit for Building a Cloudflare Worker

Creating an App

To create a new worker with create-cloudflare-worker, run the following

npm init cloudflare-worker your-worker-name

or, with npx

npx create-cloudflare-worker your-worker-name

Once the installation is done, you can open your project folder:

cd my-app

Building and testing your new worker

To build the worker, run

npm run build

This outputs the worker as dist/main.js.

To continually build the worker on every change, run

npm run watch

And finally, to execute the integration and unit tests, run

npm test

Running the worker locally

To run the worker locally, run

npm start

This will start the worker on port 4000, and forward all requests to the upstream server at http://localhost:3000. This will allow you to develop your application in tandem with the worker that will front the application.

Deploying

This project may be used to deploy your newly built worker to cloudflare as follows:

CF_ACCOUNT=acct-id CF_WORKER_NAME=worker-name [email protected] CF_AUTH_KEY=auth-key npm run deploy

However, you may also just copy dist/main.js and paste it into the CF worker dashboard (or even terraform it)

Examples

We will add recipes here

  • Building a API service with react-router
  • Parsing Cookies

Adding a Key Value Store

This project can also mock an in memory KV Store. This can be done as follows

  • Add the kv store (ex: MYSTORE) to the list of global variables of the worker
  • When running the worker, start the worker with the KV_NAMESPACES as follows KV_NAMESPACES=MYSTORE,OTHER_STORE npm start
  • In tests, namespaces can be created as an option to createTestApp as follows: const app = createTestApp(workerContents, upstreamApp, { kvStores: ["MYSTORE"] }). The new store can be accessed in tests as follows: await app.stores.MYSTORE.put("key", "value"). await app.stores.MYSTORE.get("key")

When you are ready to deploy your new worker with a KV:

  • run curl to create the worker namespace (this should only be done once)
# If this fails, contact support
curl "https://api.cloudflare.com/client/v4/accounts/$CF_ACCOUNT/workers/namespaces" \
  -X POST \
  -H "X-Auth-Email: $CF_EMAIL" \
  -H "X-Auth-Key: $AUTH_KEY" \
  -H "Content-Type: application/json" \
  --data '{"title": "your-namespace"}'
# {"result":{"id": "<some-id>","title":"your-namespace"}}
  • Bind this namespace in the deploy script in package.json. Find the bindings section in the deploy script, and replace it with something like this
\\\"bindings\\\":[{\\\"name\\\":\\\"MYSTORE\\\",\\\"type\\\":\\\"kv_namespace\\\",\\\"namespace_id\\\":\\\"$MYSTORE_ID\\\"}]
  • Finally, run your deploy script with the MYSTORE_ID variable set to the id returned by the curl command.

Contributing

  • Fork this Repo first
  • Clone your Repo
  • Install dependencies by $ npm install
  • Checkout a feature branch
  • Feel free to add your features
  • Make sure your features are fully tested
  • Publish your local branch, Open a pull request
  • Enjoy hacking <3

ISC

This work is licensed under the ISC license.