rest.js
rest.js copied to clipboard
Proxy Support
What’s missing?
Proxy support. I googled "octokit proxy", and found 2-3 github issues describing adding proxy support to Octokit, but all of them led to 404 links (example 1 & 2).
Why?
In general, any HTTP client should support proxies.
Alternatives you tried
Here's what I tried with a Socks5 proxy:
import { SocksProxyAgent } from 'socks-proxy-agent';
const agent = new SocksProxyAgent({/* credentials */});
const requestWithProxy = request.defaults({ request: { agent } });
const o = new Octokit({
request: requestWithProxy,
});
But this doesn't work, the requests are still sent from my local machine's IP address.
Also the request
constructor option is an object, not a request
instance. Can you try this?
import { SocksProxyAgent } from 'socks-proxy-agent';
const agent = new SocksProxyAgent({/* credentials */});
const o = new Octokit({
request: { agent },
});
Octokit definitely works with proxies, see our test at https://github.com/octokit/core.js/blob/master/test/agent-proxy/agent-proxy-test.test.ts
closing due to inactivity
The same issue. Not working at all. tested with socks-proxy-agent and http-proxy-agent
But I found a solution, it seems working in nodejs environment
import { SocksProxyAgent } from 'socks-proxy-agent';
import fetch from 'node-fetch'; // import node-fetch
const agent = new SocksProxyAgent({/* credentials */});
const o = new Octokit({
request: { agent, fetch },
});
@etnperlong Sorry to hear that you're having issues with proxies in Octokit! Have you tried this without customising fetch
? I wonder if that could be part of the issue.
@timrogers, we have been needing to use the workaround below to be able to use the proxy settings, but with the breaking changes in 8.0.0 this now fails as well, preventing an upgrade to 8.0.0.
const { request } = require("@octokit/request");
const { Octokit } = require("@octokit/rest");
const { HttpsProxyAgent } = require('https-proxy-agent');
// configure the proxy if needed
if (process.env.HTTPS_PROXY) {
agent = new HttpsProxyAgent(process.env.HTTPS_PROXY);
} else if (process.env.https_proxy) {
agent = new HttpsProxyAgent(process.env.https_proxy);
}
// get a new client
const githubClient = new Octokit({
baseUrl: "https://api.github.com",
auth: process.env.GITHUB_TOKEN,
request: {
agent: agent,
timeout: 5000,
},
});
Yes, in v8 of @octokit/request
we switched to the Fetch API instead of node-fetch.
Please see our test for how to implement proxy usage https://github.com/octokit/core.js/blob/73ce7fba8ec4184025c3359744935bed0b020bdf/test/agent-proxy/agent-proxy-test.test.ts#L18
You have to use the ProxyAgent
from undici
All usages of NodeJS based http(s).Agent
will not work as they are incompatible with the fetch API.
Proxies definitely do work with Octokit, it is a functionality that we account for in our tests
Closing as this is not an issue. Proxies are supported, but due to the removal of node-fetch
require a little bit more code on the client side. See my comment above on how to do so with undici
For reference, here is how this is done with native fetch in NodeJS:
import { ProxyAgent, fetch as undiciFetch } from "undici";
import { Octokit } from "@octokit/rest";
const myFetch = (url, opts) => {
return undiciFetch(url, {
...opts,
dispatcher: new ProxyAgent({
uri: process.env.HTTPS_PROXY,
keepAliveTimeout: 10,
keepAliveMaxTimeout: 10,
}),
});
};
const octokit = new Octokit({
baseUrl: serverUrl,
request: { fetch: myFetch },
});