Socket
Socket
Sign inDemoInstall

agent-base

Package Overview
Dependencies
2
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 7.0.1 to 7.0.2

63

./dist/index.js

@@ -41,21 +41,37 @@ "use strict";

}
const INTERNAL = Symbol('AgentBaseInternalState');
class Agent extends http.Agent {
constructor(opts) {
super(opts);
this._defaultPort = undefined;
this._protocol = undefined;
this[INTERNAL] = {};
}
createSocket(req, options, cb) {
const o = {
...options,
secureEndpoint: options.secureEndpoint ?? isSecureEndpoint(),
};
// Need to determine whether this is an `http` or `https` request.
// First check the `secureEndpoint` property explicitly, since this
// means that a parent `Agent` is "passing through" to this instance.
let secureEndpoint = typeof options.secureEndpoint === 'boolean'
? options.secureEndpoint
: undefined;
// If no explicit `secure` endpoint, check if `protocol` property is
// set. This will usually be the case since using a full string URL
// or `URL` instance should be the most common case.
if (typeof secureEndpoint === 'undefined' &&
typeof options.protocol === 'string') {
secureEndpoint = options.protocol === 'https:';
}
// Finally, if no `protocol` property was set, then fall back to
// checking the stack trace of the current call stack, and try to
// detect the "https" module.
if (typeof secureEndpoint === 'undefined') {
secureEndpoint = isSecureEndpoint();
}
const connectOpts = { ...options, secureEndpoint };
Promise.resolve()
.then(() => this.connect(req, o))
.then(() => this.connect(req, connectOpts))
.then((socket) => {
if (socket instanceof http.Agent) {
// @ts-expect-error `addRequest()` isn't defined in `@types/node`
return socket.addRequest(req, o);
return socket.addRequest(req, connectOpts);
}
this._currentSocket = socket;
this[INTERNAL].currentSocket = socket;
// @ts-expect-error `createSocket()` isn't defined in `@types/node`

@@ -66,26 +82,25 @@ super.createSocket(req, options, cb);

createConnection() {
if (!this._currentSocket) {
throw new Error('no socket');
const socket = this[INTERNAL].currentSocket;
this[INTERNAL].currentSocket = undefined;
if (!socket) {
throw new Error('No socket was returned in the `connect()` function');
}
return this._currentSocket;
return socket;
}
get defaultPort() {
if (typeof this._defaultPort === 'number') {
return this._defaultPort;
}
const port = this.protocol === 'https:' ? 443 : 80;
return port;
return (this[INTERNAL].defaultPort ??
(this.protocol === 'https:' ? 443 : 80));
}
set defaultPort(v) {
this._defaultPort = v;
if (this[INTERNAL]) {
this[INTERNAL].defaultPort = v;
}
}
get protocol() {
if (typeof this._protocol === 'string') {
return this._protocol;
}
const p = isSecureEndpoint() ? 'https:' : 'http:';
return p;
return (this[INTERNAL].protocol ?? (isSecureEndpoint() ? 'https:' : 'http:'));
}
set protocol(v) {
this._protocol = v;
if (this[INTERNAL]) {
this[INTERNAL].protocol = v;
}
}

@@ -92,0 +107,0 @@ }

@@ -54,7 +54,7 @@ "use strict";

function req(url, opts = {}) {
let req;
const href = typeof url === 'string' ? url : url.href;
const req = (href.startsWith('https:') ? https : http).request(url, opts);
const promise = new Promise((resolve, reject) => {
const href = typeof url === 'string' ? url : url.href;
req = (href.startsWith('https:') ? https : http)
.request(url, opts, resolve)
req
.once('response', resolve)
.once('error', reject)

@@ -61,0 +61,0 @@ .end();

@@ -12,12 +12,13 @@ /// <reference types="node" />

secureEndpoint: false;
protocol?: string;
}
interface HttpsConnectOpts extends tls.ConnectionOptions {
secureEndpoint: true;
protocol?: string;
port: number;
secureEndpoint: true;
}
export type AgentConnectOpts = HttpConnectOpts | HttpsConnectOpts;
declare const INTERNAL: unique symbol;
export declare abstract class Agent extends http.Agent {
_defaultPort?: number;
_protocol?: string;
_currentSocket?: Duplex;
private [INTERNAL];
options: Partial<net.TcpNetConnectOpts & tls.ConnectionOptions>;

@@ -24,0 +25,0 @@ keepAlive: boolean;

@@ -41,21 +41,37 @@ "use strict";

}
const INTERNAL = Symbol('AgentBaseInternalState');
class Agent extends http.Agent {
constructor(opts) {
super(opts);
this._defaultPort = undefined;
this._protocol = undefined;
this[INTERNAL] = {};
}
createSocket(req, options, cb) {
const o = {
...options,
secureEndpoint: options.secureEndpoint ?? isSecureEndpoint(),
};
// Need to determine whether this is an `http` or `https` request.
// First check the `secureEndpoint` property explicitly, since this
// means that a parent `Agent` is "passing through" to this instance.
let secureEndpoint = typeof options.secureEndpoint === 'boolean'
? options.secureEndpoint
: undefined;
// If no explicit `secure` endpoint, check if `protocol` property is
// set. This will usually be the case since using a full string URL
// or `URL` instance should be the most common case.
if (typeof secureEndpoint === 'undefined' &&
typeof options.protocol === 'string') {
secureEndpoint = options.protocol === 'https:';
}
// Finally, if no `protocol` property was set, then fall back to
// checking the stack trace of the current call stack, and try to
// detect the "https" module.
if (typeof secureEndpoint === 'undefined') {
secureEndpoint = isSecureEndpoint();
}
const connectOpts = { ...options, secureEndpoint };
Promise.resolve()
.then(() => this.connect(req, o))
.then(() => this.connect(req, connectOpts))
.then((socket) => {
if (socket instanceof http.Agent) {
// @ts-expect-error `addRequest()` isn't defined in `@types/node`
return socket.addRequest(req, o);
return socket.addRequest(req, connectOpts);
}
this._currentSocket = socket;
this[INTERNAL].currentSocket = socket;
// @ts-expect-error `createSocket()` isn't defined in `@types/node`

@@ -66,26 +82,25 @@ super.createSocket(req, options, cb);

createConnection() {
if (!this._currentSocket) {
throw new Error('no socket');
const socket = this[INTERNAL].currentSocket;
this[INTERNAL].currentSocket = undefined;
if (!socket) {
throw new Error('No socket was returned in the `connect()` function');
}
return this._currentSocket;
return socket;
}
get defaultPort() {
if (typeof this._defaultPort === 'number') {
return this._defaultPort;
}
const port = this.protocol === 'https:' ? 443 : 80;
return port;
return (this[INTERNAL].defaultPort ??
(this.protocol === 'https:' ? 443 : 80));
}
set defaultPort(v) {
this._defaultPort = v;
if (this[INTERNAL]) {
this[INTERNAL].defaultPort = v;
}
}
get protocol() {
if (typeof this._protocol === 'string') {
return this._protocol;
}
const p = isSecureEndpoint() ? 'https:' : 'http:';
return p;
return (this[INTERNAL].protocol ?? (isSecureEndpoint() ? 'https:' : 'http:'));
}
set protocol(v) {
this._protocol = v;
if (this[INTERNAL]) {
this[INTERNAL].protocol = v;
}
}

@@ -92,0 +107,0 @@ }

{
"name": "agent-base",
"version": "7.0.1",
"version": "7.0.2",
"description": "Turn a function into an `http.Agent` instance",

@@ -5,0 +5,0 @@ "main": "./dist/index.js",

@@ -7,3 +7,3 @@ agent-base

It provides an absract class that must define a `connect()` function,
It provides an abstract class that must define a `connect()` function,
which is responsible for creating the underlying socket that the HTTP

@@ -23,3 +23,3 @@ client requests will use.

Here's some more interesting uses of `agent-base`.
Here are some more interesting uses of `agent-base`.
Send a pull request to list yours!

@@ -26,0 +26,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc