Giter Site home page Giter Site logo

ergonomic-service-bindings's Introduction

ergonomic-service-bindings

Ergonomic Service Bindings for Cloudflare Workers: no more HTTP calls—directly call functions from your other workers

Service Bindings let your different Cloudflare workers talk to each other, quickly. However, it requires sending a mock HTTP request to the worker with which you're trying to communicate—which gives a lot of flexibility, but is sometimes more overhead than needed. This library simplifies that by letting you directly call functions on other Cloudflare workers.

In essenced, your code will go from looking like this:

await env.test.fetch('http://workerb/hello', {
    method: 'POST',
    body: JSON.stringify({params: 'World'})
})

to looking like this:

await env.test.hello('World')

Concepts

  • Each worker is either a service worker, or a consumer worker.
  • A consumer worker can call functions from the service workers it's bound to.
  • A service worker can only provide functions for consumer workers to call

Getting Started

For service workers

npm i ergonomic-service-bindings

Add the following section to your wrangler.toml:

[build]
command = "npm run build"

and change the main section to point to:

main = "esb/index.js"

Next, in your package.json, add a "build" script:

    "build": "ergonomic-service-bindings service"

Finally, ensure you have a file called src/service.ts, which is where you'll be writing all your functions. Just export functions from here, and when you deploy your worker they'll be available to all services that bind your worker. For example, here's a hello world function:

export async function hello(name: string) {
  return `Hello ${name}`;
}

For consumer workers

npm i ergonomic-service-bindings

Add the following section to your wrangler.toml:

[build]
command = "npm run build"

and change the main section to point to:

main = "esb/index.js"

You'll also want to setup any service bindings to other workers you want to use. For instance, this binds to the worker called test:

services = [
  { binding = "test", service = "test", environment = "production"}
]

Next, in your package.json, add a "build" script:

    "build": "ergonomic-service-bindings consumer"

And that's it! Here's an example of calling the function hello on a worker bound to test:

export default {
  async fetch(
    request: Request,
    env: Env,
    ctx: ExecutionContext
  ): Promise<Response> {
    return new Response(await env.test.hello('World'));
  }
};

Typing

You don't need types for the functionality to work, but they definitely make the TS experience more pleasant. Simply define your env type within your consumer worker as follows:

export interface Env {
  test: TestService;
}

where TestService is imported from the autogenerated file at the path esb/service.types.ts within your service worker directory!

ergonomic-service-bindings's People

Contributors

penalosa avatar

Stargazers

 avatar  avatar  avatar

Watchers

 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.