feat: manage MongoDB SOCKS5 proxy settings (#11731)

This commit is contained in:
Kishan Kumar 2025-10-21 02:59:32 +05:45 committed by GitHub
parent 8692da2b69
commit d7867ebff1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 63 additions and 0 deletions

View File

@ -303,4 +303,24 @@ export interface MongoConnectionOptions extends BaseDataSourceOptions {
* Please use the {@link writeConcern} option instead
*/
readonly wtimeoutMS?: number
/**
* Configures a Socks5 proxy host used for creating TCP connections.
*/
readonly proxyHost?: string
/**
* Configures a Socks5 proxy port used for creating TCP connections.
*/
readonly proxyPort?: number
/**
* Configures a Socks5 proxy username when the proxy in proxyHost requires username/password authentication.
*/
readonly proxyUsername?: string
/**
* Configures a Socks5 proxy password when the proxy in proxyHost requires username/password authentication.
*/
readonly proxyPassword?: string
}

View File

@ -203,6 +203,11 @@ export class MongoDriver implements Driver {
"w",
"writeConcern",
"wtimeoutMS",
// Proxy configuration for Socks5
"proxyHost",
"proxyPort",
"proxyUsername",
"proxyPassword",
// Undocumented deprecated options
// todo: remove next major version
"appname",

View File

@ -58,4 +58,42 @@ describe("MongoDriver", () => {
expect(connectionUrl).to.eql(url)
})
})
describe("proxy options", () => {
it("should pass proxy options to MongoClient.connect", async () => {
const options = {
type: "mongodb",
host: "someHost",
port: 27017,
database: "myDatabase",
proxyHost: "127.0.0.1",
proxyPort: 1080,
proxyUsername: "proxyUser",
proxyPassword: "proxyPass",
}
const driver = new MongoDriver({
options,
} as DataSource)
// replace MongoClient.connect with a fake that resolves so connect() completes
const connect = sinon.fake.resolves({})
driver.mongodb = {
...driver.mongodb,
MongoClient: {
connect,
},
}
await driver.connect()
// the second argument passed to MongoClient.connect should contain our proxy settings
const passedOptions = connect.args[0][1]
expect(passedOptions).to.have.property("proxyHost", "127.0.0.1")
expect(passedOptions).to.have.property("proxyPort", 1080)
expect(passedOptions).to.have.property("proxyUsername", "proxyUser")
expect(passedOptions).to.have.property("proxyPassword", "proxyPass")
})
})
})