Git Product home page Git Product logo

james-elicx-cf-bindings-proxy's Introduction

cf-bindings-proxy

Experimental proxy for interfacing with bindings
in projects targeting Cloudflare Pages.


npm (tag) GitHub Workflow Status (with branch)


This library was written to accompany @cloudflare/next-on-pages, so that you can use bindings when developing Next.js apps locally through next dev.

It is intended to be used for frameworks that do not support Cloudflare bindings in a fast HMR environment.

Usage

Add the library to your project.

npm add cf-bindings-proxy

In a separate terminal window, run the following command to start the proxy, passing through your bindings are arguments.

npx cf-bindings-proxy --kv=MY_KV

In your project's code, import the binding function from cf-bindings-proxy and use it to interact with your bindings.

import { binding } from 'cf-bindings-proxy';

const value = await binding<KVNamespace>('MY_KV').get('key');

It is also possible to specify a custom fallback for use in production instead of process.env.

import { binding } from 'cf-bindings-proxy';

const value = await binding<KVNamespace>('MY_KV', { fallback: platform.env }).get('key');

How It Works

Starting the proxy spawns an instance of Wrangler using a template, passing through any commands and bindings that are supplied to the CLI. It uses port 8799.

In development mode, when interacting with a binding through the binding('BINDING_NAME') function, it sends HTTP requests to the proxy. These HTTP requests contain destructured function calls, which are then reconstructed and executed inside the proxy. The result is then returned to the client.

When building for production, binding('BINDING_NAME') simply calls process.env.BINDING_NAME to retrieve the binding instead. If you wish to use a custom fallback like platform.env instead of process.env, you can pass a custom fallback to the binding call with binding('BINDING_NAME', { fallback: platform.env }).

When It's Active

Calls to binding('BINDING_NAME') will try to use the proxy when either of the following two conditions are met:

  • The ENABLE_BINDINGS_PROXY environment variable is set to true. OR
  • The DISABLE_BINDINGS_PROXY environment variable is not set and NODE_ENV is set to development.

Supported

Note: Functionality and bindings not listed below may still work but have not been tested.

KV

  • put
  • get
  • list
  • getWithMetadata
  • delete

D1

  • prepare
    • bind
    • run
    • all
    • first
    • raw
  • batch
  • exec
  • dump

R2

  • put
    • writeHttpMetadata
    • value type: ArrayBuffer
    • value type: string
    • value type: Blob
    • value type: ReadableStream
  • get
    • writeHttpMetadata
    • text
    • json
    • arrayBuffer
    • blob
    • body
    • bodyUsed
  • head
    • writeHttpMetadata
  • list
    • writeHttpMetadata
  • delete
  • createMultipartUpload (needs more tests)
  • resumeMultipartUpload (needs more tests)

james-elicx-cf-bindings-proxy's People

Contributors

github-actions[bot] avatar iamvishnusankar avatar james-elicx avatar karibash 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.