New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

speedcf-libs

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

speedcf-libs

> collection of optimization done in Cloudflare Workers

latest
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

Cloudflare Speed's libraries

collection of optimization done in Cloudflare Workers

Demo

Edge side includes, Google font optimizations, h2 default priorities, 3rd party rewrite and serve stale-while-revalidate in a worker:

// overrides URL constructor:
// to encode % so Worker's URL parser won't throw
import "@cloudflare/patch-workers-url";
import { EdgeSideIncludesOptimization } from "@cloudflare/edge-side-includes";
import { GoogleFontOptimization } from "@cloudflare/google-fast-fonts";
import { Rewrite3rdPartyOptimization } from "@cloudflare/rewrite-3rd-party";
import { h2DefaultPrioritiesOptimization } from "@cloudflare/h2-default-priorities";
import { StaleWhileRevalidateOptimization } from "@cloudflare/stale-while-revalidate";

addEventListener("fetch", (event) => {
  event.respondWith(handleRequest(event));
});

async function handleRequest(event) {
  const request = event.request;
  const esi = new EdgeSideIncludesOptimization(request);
  const fonts = new GoogleFontOptimization(request);
  const thirdParty = new Rewrite3rdPartyOptimization(request);
  const h2Prio = new h2DefaultPrioritiesOptimization(request);
  const swr = new StaleWhileRevalidateOptimization(request);

  if (fonts.isGoogleFontRequest()) {
    return fonts.serveGoogleFont();
  }

  if (thirdParty.isProxiedScriptRequest()) {
    return thirdParty.proxyScriptRequest();
  }

  let res = await swr.fetchWithCache(event);
  res = h2Prio.withPriority(res);
  return transform(request, { thirdParty, esi, fonts }, res);
}

function transform(request, { thirdParty, esi, fonts }, response) {
  const rewriter = thirdParty.withThirdPartyTransform(
    fonts.withGoogleFontsTransform(
      response,
      esi.withESITransform(new HTMLRewriter())
    )
  );
  return rewriter.transform(response);
}

Contributing

Install

yarn
yarn bootstrap

Run tests

yarn test

FAQs

Package last updated on 15 Feb 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts