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

@my-liminal-space/cw-site-assets

v1.0.3

Published

A library for pulling Cloudflare Workers Site assets from the site KV based on local path.

Downloads

3

Readme

cw-site-assets

Introduction

Access arbitrary site assets from a Cloudflare Workers Site. Helps you dynamically control (using Workers hosted code) which "page" (from the pages published in your site) to return for a given user request, for example by returning the contents of a "not authorised" page if the user issuing the request has not logged in.

This code was found to be useful as the default behaviour of Workers Site is heavily oriented towards serving the originally requested page (and doesn't make it easy to fetch a different page instead).

The primary method is:

async function fetchSiteAssetFromKv(assetPath)

This method fetches a site asset from site KV based on its path.

The path is absolute path within the site with no leading '/', no protocol and no hostname, so it's of the form:

app-dir/page-a.html

Returns a "Response" object with the body set as an 'arrayBuffer' created from the result of awaiting a 'get' of the asset from the _STATIC_CONTENT KV namespace (as per getAssetFromKV).

Throws an exception if assetPath isn't a key in __STATIC_CONTENT_MANIFEST.

Uses 'await' on call to KV, so this method needs to be awaited...

NOTE, Current implementation:

  • Does not set any headers (such as Content-Type)
  • Assumes site content is in the default namespace
  • Does not handle URL encoded paths
  • Doesn't try to do any of the caching stuff that getAssetFromKV does
  • Relies upon the Workers Site platform setting up global vars
    • __STATIC_CONTENT_MANIFEST
    • __STATIC_CONTENT

There are a couple of useful subsidiary methods:

async function fetchSiteAssetFromKvAsHtmlPage(assetPath)

    adds a Content-Type header with value text/html

function addStandardResponseHeaders(response)

    adds the headers that the workers site example adds

Development

The code lives in this GitHub repo.

Testing code that depends upon features of the Cloudflare Workers Site platform (such as the voodoo setup of __STATIC_CONTENT_MANIFEST and _STATIC_CONTENT) is "interesting". In order to build confidence that the code will work as expected, the approach taken is to test by deploying the lib along with a test site into Cloudflare Workers. The index.js file handles requests coming into the test site.

If you want to replicate the test environment, you will need to modify wrangler.toml to use your own values.

Distribution

When packaged for deployment (using pkg.sh), a new folder structure is created that is sets up a package focussed on distribtion, which means:

  • only cw-site-assets.js is included (renamed as index.js)
  • an alternative package.json is included, pointing to index.js