Skip to content

feat: allow passing of headers to delete from source #358

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

Open
wants to merge 1 commit into
base: master
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
8 changes: 7 additions & 1 deletion bin/smee.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ program
)
.option("-p, --port <n>", "Local HTTP server port", process.env.PORT || 3000)
.option("-P, --path <path>", "URL path to post proxied requests to`", "/")
.option("-h, --deleteHeaders <headers>", "List of headers to remove from request, comma separated", "")
.parse(process.argv);

const opts = program.opts();
Expand All @@ -29,12 +30,17 @@ const { target = `http://127.0.0.1:${opts.port}${opts.path}` } = opts;

async function setup() {
let source = opts.url;
let headersToDelete;

if(opts.deleteHeaders){
headersToDelete = opts.deleteHeaders.split(",");
}

if (!source) {
source = await Client.createChannel();
}

const client = new Client({ source, target });
const client = new Client({ source, target, deleteHeaders: headersToDelete});
client.start();
}

Expand Down
7 changes: 7 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface Options {
target: string;
logger?: Pick<Console, Severity>;
fetch?: any;
deleteHeaders?: Array<string>;
}

const proxyAgent = new EnvHttpProxyAgent();
Expand All @@ -24,19 +25,24 @@ class Client {
#source: string;
#target: string;
#fetch: typeof undiciFetch;
#deleteHeaders: Array<string>;

#logger: Pick<Console, Severity>;
#events!: EventSource;


constructor({
source,
target,
logger = console,
fetch = undiciFetch,
deleteHeaders = []
}: Options) {
this.#source = source;
this.#target = target;
this.#logger = logger!;
this.#fetch = fetch;
this.#deleteHeaders = deleteHeaders;

if (!validator.isURL(this.#source)) {
throw new Error("The provided URL is invalid.");
Expand Down Expand Up @@ -78,6 +84,7 @@ class Client {
// See https://github.com/probot/smee-client/issues/295
// See https://github.com/probot/smee-client/issues/187
delete headers["host"];
this.#deleteHeaders.forEach((header) => delete headers[header]);
headers["content-length"] = Buffer.byteLength(body);
headers["content-type"] = "application/json";

Expand Down