Coder Social home page Coder Social logo

cloud-worker's Introduction

Cloud Worker

This project is based on the ideas from the Service Worker specification and the CloudFlare Worker project.

Outline

Create a single worker file that accepts a fetch event and calls event.respondWith with a promise (such as another fetch).

The module can be used as a standalone server (possibly to test CloudFlare Workers) or as middleware in your own projects or can be invoked manually.

Installation and usage

Cloud Worker requires Node and is installed using npm (which comes with node):

npm install cloud-worker

If you want to only write a cloud worker file, then you can use the prebuilt server in your npm scripts like this:

{
  "scripts": {
    "start": "worker index.js"
  }
}

…where index.js contains your worker. For example, this cloud-worker demo waits 3 seconds then sends all request the response from a example.com (source code).

Here's some recipes of what a cloud worker can do.

Direct usage

The module can be required and used directly, and the (current) API is:

const worker = require('cloud-worker');

Behind the scenes

The cloud worker uses Node's vm module to globally scope all of the required API that the service worker-like script would expect.

It waits for a single addEventListener for the fetch event and triggers that event when the worker.handler function is invoked passing in a event that contains a request object.

The functionality is provided via the following npm modules:

  • fetch (and related sub modules including Request, Response, Headers, FetchError) via node-fetch
  • Response.redirect is manually added
  • URL and URLSearchParams via node's internal url package
  • streams (ReadableStream, WritableStream and TransformStream) via web-streams-polyfill (though I believe this to be out of date - any user contribution would be grateful here)
  • encoding (TextEncoder and TextDecoder) via text-encoding
  • FetchEvent is manually added

Licence

cloud-worker's People

Contributors

remy avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.