Make curl go through corsproxy

This commit is contained in:
Alex Gleason committed 2025-09-13 20:32:40 -05:00
1 parent c9069cfebd
commit 9b85c11803
2 files changed
+11 -7

No files matched your search

+4 -4
View File
@@ -76,7 +76,7 @@ describe('CurlCommand', () => {
expect(result.exitCode).toBe(0);
expect(result.stdout).toBe('Hello World');
expect(mockFetch).toHaveBeenCalledWith('https://example.com/', expect.objectContaining({
expect(mockFetch).toHaveBeenCalledWith('https://corsproxy.io/?url=https%3A%2F%2Fexample.com%2F', expect.objectContaining({
method: 'GET',
headers: expect.objectContaining({
'User-Agent': 'curl/8.0.0 (compatible; JavaScript fetch)'
@@ -103,7 +103,7 @@ describe('CurlCommand', () => {
expect(result.exitCode).toBe(0);
expect(result.stdout).toBe('{"success": true}');
expect(mockFetch).toHaveBeenCalledWith('https://example.com/api', expect.objectContaining({
expect(mockFetch).toHaveBeenCalledWith('https://corsproxy.io/?url=https%3A%2F%2Fexample.com%2Fapi', expect.objectContaining({
method: 'POST',
body: '{"name": "test"}',
headers: expect.objectContaining({
@@ -131,7 +131,7 @@ describe('CurlCommand', () => {
], '/test');
expect(result.exitCode).toBe(0);
expect(mockFetch).toHaveBeenCalledWith('https://example.com/', expect.objectContaining({
expect(mockFetch).toHaveBeenCalledWith('https://corsproxy.io/?url=https%3A%2F%2Fexample.com%2F', expect.objectContaining({
headers: expect.objectContaining({
'Authorization': 'Bearer token123',
'Content-Type': 'application/json',
@@ -245,7 +245,7 @@ describe('CurlCommand', () => {
], '/test');
expect(result.exitCode).toBe(0);
expect(mockFetch).toHaveBeenCalledWith('https://example.com/', expect.objectContaining({
expect(mockFetch).toHaveBeenCalledWith('https://corsproxy.io/?url=https%3A%2F%2Fexample.com%2F', expect.objectContaining({
method: 'DELETE',
headers: expect.objectContaining({
'User-Agent': 'curl/8.0.0 (compatible; JavaScript fetch)'
+7 -3
View File
@@ -59,6 +59,10 @@ export class CurlCommand implements ShellCommand {
return createErrorResult(`${this.name}: unsupported protocol: ${url.protocol}`);
}
// Proxy the URL through CorsProxy
const originalUrl = url.toString();
const proxiedUrl = `https://corsproxy.io/?url=${encodeURIComponent(originalUrl)}`;
// Prepare request
const requestInit: RequestInit = {
method: options.method,
@@ -93,7 +97,7 @@ export class CurlCommand implements ShellCommand {
try {
if (options.verbose && !options.silent) {
console.log(`* Trying ${url.hostname}...`);
console.log(`* Trying ${url.hostname}... (via CorsProxy)`);
console.log(`> ${options.method} ${url.pathname}${url.search} HTTP/1.1`);
console.log(`> Host: ${url.hostname}`);
for (const [key, value] of Object.entries(options.headers)) {
@@ -105,8 +109,8 @@ export class CurlCommand implements ShellCommand {
}
}
// Make the request
const response = await fetch(url.toString(), requestInit);
// Make the request using the proxied URL
const response = await fetch(proxiedUrl, requestInit);
if (timeoutId) {
clearTimeout(timeoutId);