New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@cfworker/web

Package Overview
Dependencies
Maintainers
1
Versions
60
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@cfworker/web

Web framework for Cloudflare Workers and service workers, inspired by Koa

  • 1.1.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
35
decreased by-72.87%
Maintainers
1
Weekly downloads
 
Created
Source

@cfworker/web

Web framework for Cloudflare Workers and service workers, inspired by Koa.

For a compatible router, check out @cfworker/web-router.

import { Application, Middleware } from '@cfworker/web';

// Exception handler middleware example:
const exceptionHandler: Middleware = async (context, next) => {
  const { res, accepts } = context;
  try {
    await next();
  } catch (err) {
    res.status = 500;
    if (accepts.media('text/html')) {
      res.body = `<h1>Internal Server Error</h1><p><pre><code>${err.stack}</code></pre></p>`;
      res.headers.set('content-type', 'text/html');
    } else {
      res.body = `Internal Server Error\n${err.stack}`;
      res.headers.set('content-type', 'text/plain');
    }
  }
};

// Simple request handler middleware. Consider using @cfworker/web-router for more advanced scenarios.
const requestHandler: Middleware = context => {
  const { res, url } = context;

  // Throw an error to demonstrate the exceptionHandler middleware.
  if (url.searchParams.has('throw')) {
    // @ts-ignore
    self.thisMethodDoesNotExist('an exception will be thrown');
  }

  // Eject from middleware, use FetchEvent.respondWith directly.
  if (url.searchParams.has('eject')) {
    context.respondWith(new Response('Ejected!', { status: 200 }));
  }

  res.body = `
    <h1>Hello world!</h1>
    <img alt="Cloudflare global network" src="https://workers.cloudflare.com/resources/illustrations/global-network.svg">
    <p><a href="?throw">Throw an error</a> or <a href="?eject">eject from middleware stack</a></p>`;
  res.headers.set('content-type', 'text/html');
};

new Application()
  .use(exceptionHandler)
  .use(requestHandler)
  .listen();

To run this example:

git clone https://github.com/cfworker/cfworker
cd cfworker
yarn install
yarn workspace @cfworker/web cfworker run examples/worker.ts --watch

Keywords

FAQs

Package last updated on 05 Apr 2020

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc