Code Monkey home page Code Monkey logo

Comments (4)

PandaWorker avatar PandaWorker commented on August 17, 2024

It was also impossible to implement AsyncDisposable for DispatcherBase. That would solve the try finally problem.

This would solve the problem of cleaning up resources without using try finally.

import { Agent, request } from 'undici';
import { buildHttpProxyConnector } from '@undicijs/proxy';

// Connectors only for wrapping socket connection for distonation host
const httpProxyConnector = buildHttpProxyConnector('http://3proxy:8080');

// disposable agent
class DisposableAgent extends Agent implements AsyncDisposable {
	[Symbol.asyncDispose]() {
		return this.close();
	}
}

/**
 * get ip address with disposable agent via httpProxyConnector
 */
async function getIp() {
	await using dispatcher = new DisposableAgent({
		connect: httpProxyConnector,
		connections: 1
	});

	const ip = await request('https://api.ipify.org/', { dispatcher }).then(
		resp => resp.body.text()
	);

	return { ip };
} // disposing: close socket connections openned with dispatcher (DisposableAgent)

/**
 * get ip address with agent via httpProxyConnector
 */
async function getIpTryFinally() {
	const dispatcher = new Agent({ connect: httpProxyConnector, connections: 1 });

	try {
		const ip = await request('https://api.ipify.org/', { dispatcher }).then(
			resp => resp.body.text()
		);
		return { ip };
	} finally {
		await dispatcher.close();
	}
}

from undici.

metcoder95 avatar metcoder95 commented on August 17, 2024

The approach you used indeed seems interesting, and I see the benefit of that; nevertheless, I'm not sure a rewrite of the ProxyAgent will be an option here as the agent has a specific purpose and handles several other pieces beyond only connecting to the proxy.

It might be good to document this possibility, as I can see it can be overlooked easily; but not convinced about the rewrite.

About Symbol.asyncDispose, it could be an interesting addition. Stills in stage 3.

from undici.

ronag avatar ronag commented on August 17, 2024

We could implement ProxyAgent using these connector?

from undici.

metcoder95 avatar metcoder95 commented on August 17, 2024

Yeah, definitely; if we can add a lightweight Proxy that is tight to a single origin, that might help to ease the possibility of having Proxy interceptor as well.

Just the idea of rewriting the current one completely its what I'm not 100% convinced of

from undici.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.