Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
schettn committed Jun 25, 2024
1 parent 3318325 commit 081f792
Showing 1 changed file with 22 additions and 57 deletions.
79 changes: 22 additions & 57 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Handler } from "hono";
import https from "https";
import http from "http";
import { createBunWebSocket } from "hono/bun";
import fetch from "node-fetch";

import dotenv from "dotenv";

Expand Down Expand Up @@ -50,71 +49,37 @@ function basicProxy(proxy_url = ""): Handler {
headers[key] = value;
});

const httpModule = url.startsWith("https") ? https : http;
const blob = await c.req.blob();

const agent = new httpModule.Agent({
rejectUnauthorized: false,
const response = await fetch(url, {
method: c.req.raw.method,
headers: headers,
body: blob.size > 0 ? blob : undefined,
});

// Construct options for the https.request
const requestOptions = {
method: c.req.raw.method,
headers,
rejectUnauthorized: false,
agent: agent,
};
// response

console.log("Request options", requestOptions);
console.log("Response", response.status, response.statusText);

const res = await fetch(url, {
agent: agent,
});
// Compress the response based on the Content-Encoding

console.log("Response TEST", res.status, res.statusText, res.headers);

// Perform the request using https.request
return new Promise(async (resolve, reject) => {
console.log("Requesting", url, requestOptions);
const req = httpModule.request(url, requestOptions, (res) => {
const chunks = [];
res.on("data", (chunk) => {
chunks.push(chunk);
});
res.on("end", () => {
const responseBody = Buffer.concat(chunks);

console.log(
"Response",
res.statusCode,
res.statusMessage,
res.headers
);

// Return the response
resolve(
new Response(responseBody, {
status: res.statusCode,
statusText: res.statusMessage,
headers: res.headers,
})
);
});
});
const contentEncoding = response.headers.get("Content-Encoding");

// Handle request errors
req.on("error", (err) => {
console.error("Error making request:", err);
reject(err);
});
let compressed: Uint8Array = new Uint8Array();

// Write request body if present
const buff = await c.req.arrayBuffer();
if (buff.byteLength > 0) {
// To Uint8Array
req.write(Buffer.from(buff));
}
if (contentEncoding === "deflate") {
const buffer = await response.arrayBuffer();
compressed = Bun.deflateSync(buffer);
} else if (contentEncoding === "gzip") {
const buffer = await response.arrayBuffer();
compressed = Bun.gzipSync(buffer);
} else {
}

req.end();
return new Response(compressed, {
status: response.status,
statusText: response.statusText,
headers: response.headers,
});
};
}
Expand Down

0 comments on commit 081f792

Please sign in to comment.