Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support for using a custom port #20

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/light-points-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'cf-bindings-proxy': minor
---

Support for using a custom port with the environment variable `BINDINGS_PROXY_PORT`.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const value = await binding<KVNamespace>('MY_KV').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`.
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` by default, but can be configured with the environment variable `BINDINGS_PROXY_PORT`.

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.

Expand Down
3 changes: 3 additions & 0 deletions env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ declare global {
NODE_ENV?: string;
DISABLE_BINDINGS_PROXY?: string;
ENABLE_BINDINGS_PROXY?: string;
BINDINGS_PROXY_PROTOCOL?: string;
BINDINGS_PROXY_HOST?: string;
BINDINGS_PROXY_PORT?: string;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"build:watch": "npm run build --watch",
"lint": "eslint \"./**/*.{cjs,js,jsx,ts,tsx}\"",
"prettier": "prettier --ignore-unknown --ignore-path=.gitignore --check .",
"prettier:format": "prettier --ignore-unknown --ignore-path=.gitignore --write .",
"prettier:fix": "prettier --ignore-unknown --ignore-path=.gitignore --write .",
"tsc": "tsc --noEmit",
"test": "vitest run",
"test:kill": "rm -rf .wrangler; sudo kill -9 `sudo lsof -i :8799 -t`",
Expand Down
10 changes: 9 additions & 1 deletion src/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { spawn } from 'node:child_process';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { PORT } from '../constants';

// eslint-disable-next-line @typescript-eslint/naming-convention
const __filename = fileURLToPath(import.meta.url);
Expand Down Expand Up @@ -39,7 +40,14 @@ Please report any issues to https://github.com/james-elicx/cf-bindings-proxy

const wrangler = spawn(
executor,
['wrangler', 'pages', 'dev', resolve(__dirname, 'template'), '--port=8799', ...passThroughArgs],
[
'wrangler',
'pages',
'dev',
resolve(__dirname, 'template'),
`--port=${PORT}`,
...passThroughArgs,
],
{ stdio: 'inherit' },
);

Expand Down
3 changes: 3 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const PROTOCOL = process.env.BINDINGS_PROXY_PROTOCOL || 'http';
export const HOST = process.env.BINDINGS_PROXY_HOST || '127.0.0.1';
export const PORT = Number(process.env.BINDINGS_PROXY_PORT) || 8799;
3 changes: 2 additions & 1 deletion src/proxy.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { HOST, PORT, PROTOCOL } from './constants';
import { transformData } from './transform';

export type BindingResponse =
Expand All @@ -13,7 +14,7 @@ export type BindingResponse =
const fetchData = async (call: BindingRequest): Promise<unknown> => {
let resp: Response;
try {
resp = await fetch('http://127.0.0.1:8799', {
resp = await fetch(`${PROTOCOL}://${HOST}:${PORT}`, {
body: JSON.stringify(call),
method: 'POST',
cache: 'no-store',
Expand Down
Loading