Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

https: make https.globalAgent overridable also under ECMAScript Modules #48808

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions lib/https.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const {
FunctionPrototypeCall,
JSONStringify,
ObjectAssign,
ObjectDefineProperty,
ObjectSetPrototypeOf,
ReflectApply,
ReflectConstruct,
Expand Down Expand Up @@ -350,7 +351,7 @@ Agent.prototype._evictSession = function _evictSession(key) {
delete this._sessionCache.map[key];
};

const globalAgent = new Agent({ keepAlive: true, scheduling: 'lifo', timeout: 5000 });
let globalAgent = new Agent({ keepAlive: true, scheduling: 'lifo', timeout: 5000 });

/**
* Makes a request to a secure web server.
Expand Down Expand Up @@ -415,9 +416,21 @@ function get(input, options, cb) {

module.exports = {
Agent,
globalAgent,
Server,
createServer,
get,
request,
};

// Make https.globalAgent overridable also under ECMAScript Modules
ObjectDefineProperty(module.exports, 'globalAgent', {
__proto__: null,
configurable: true,
enumerable: true,
get() {
return globalAgent;
},
set(value) {
globalAgent = value;
},
});
43 changes: 43 additions & 0 deletions test/parallel/test-https-client-override-global-agent.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { hasCrypto, skip, mustCall } from '../common/index.mjs';
if (!hasCrypto) skip('missing crypto');
import { readKey } from '../common/fixtures.mjs';
import assert from 'assert';
// To override https.globalAgent we need to use namespace import
import * as https from 'https';

// Disable strict server certificate validation by the client
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';

const options = {
key: readKey('agent1-key.pem'),
cert: readKey('agent1-cert.pem'),
};

const server = https.Server(
options,
mustCall((req, res) => {
res.writeHead(200);
res.end('Hello, World!');
})
);

server.listen(
0,
mustCall(() => {
const agent = new https.Agent();
const name = agent.getName({ port: server.address().port });
// That is exactly what we want here:)
/* eslint-disable no-import-assign */
https.globalAgent = agent;

Check failure on line 31 in test/parallel/test-https-client-override-global-agent.mjs

View workflow job for this annotation

GitHub Actions / test-asan

--- stderr --- file:///home/runner/work/node/node/test/parallel/test-https-client-override-global-agent.mjs:31 https.globalAgent = agent; ^ TypeError: Cannot assign to read only property 'globalAgent' of object '[object Module]' at Server.<anonymous> (file:///home/runner/work/node/node/test/parallel/test-https-client-override-global-agent.mjs:31:23) at Server.<anonymous> (/home/runner/work/node/node/test/common/index.js:474:15) at Object.onceWrapper (node:events:628:28) at Server.emit (node:events:514:28) at emitListeningNT (node:net:1906:10) at process.processTicksAndRejections (node:internal/process/task_queues:81:21) Node.js v21.0.0-pre Command: out/Release/node /home/runner/work/node/node/test/parallel/test-https-client-override-global-agent.mjs

Check failure on line 31 in test/parallel/test-https-client-override-global-agent.mjs

View workflow job for this annotation

GitHub Actions / test-macOS

--- stderr --- file:///Users/runner/work/node/node/test/parallel/test-https-client-override-global-agent.mjs:31 https.globalAgent = agent; ^ TypeError: Cannot assign to read only property 'globalAgent' of object '[object Module]' at Server.<anonymous> (file:///Users/runner/work/node/node/test/parallel/test-https-client-override-global-agent.mjs:31:23) at Server.<anonymous> (/Users/runner/work/node/node/test/common/index.js:474:15) at Object.onceWrapper (node:events:628:28) at Server.emit (node:events:514:28) at emitListeningNT (node:net:1906:10) at process.processTicksAndRejections (node:internal/process/task_queues:81:21) Node.js v21.0.0-pre Command: out/Release/node /Users/runner/work/node/node/test/parallel/test-https-client-override-global-agent.mjs

Check failure on line 31 in test/parallel/test-https-client-override-global-agent.mjs

View workflow job for this annotation

GitHub Actions / test-linux

--- stderr --- file:///home/runner/work/node/node/test/parallel/test-https-client-override-global-agent.mjs:31 https.globalAgent = agent; ^ TypeError: Cannot assign to read only property 'globalAgent' of object '[object Module]' at Server.<anonymous> (file:///home/runner/work/node/node/test/parallel/test-https-client-override-global-agent.mjs:31:23) at Server.<anonymous> (/home/runner/work/node/node/test/common/index.js:474:15) at Object.onceWrapper (node:events:628:28) at Server.emit (node:events:514:28) at emitListeningNT (node:net:1906:10) at process.processTicksAndRejections (node:internal/process/task_queues:81:21) Node.js v21.0.0-pre Command: out/Release/node /home/runner/work/node/node/test/parallel/test-https-client-override-global-agent.mjs

makeRequest();
assert(name in agent.sockets); // Agent has indeed been used
})
);

function makeRequest() {
const req = https.get({
port: server.address().port,
});
req.on('close', () => server.close());
}
Loading