electron/spec/fixtures/api/utility-process/net.js
Robo 9b166b3ed4
feat: support app#login event for utility process net requests (#42631)
* feat: support app#login event for utility process net requests

* chore: address review feedback

* GlobalRequestID: Avoid unwanted inlining and narrowing int conversions

Refs https://chromium-review.googlesource.com/c/chromium/src/+/5702737
2024-08-14 11:36:47 +09:00

44 lines
1.3 KiB
JavaScript

const { net } = require('electron');
const serverUrl = process.argv[2].split('=')[1];
let configurableArg = null;
if (process.argv[3]) {
configurableArg = process.argv[3].split('=')[0];
}
const data = [];
let request = null;
if (configurableArg === '--omit-credentials') {
request = net.request({ method: 'GET', url: serverUrl, credentials: 'omit' });
} else if (configurableArg === '--use-fetch-api') {
net.fetch(serverUrl).then((response) => {
process.parentPort.postMessage([response.status, response.headers]);
});
} else {
request = net.request({ method: 'GET', url: serverUrl });
}
if (request) {
if (configurableArg === '--use-net-login-event') {
request.on('login', (authInfo, cb) => {
process.parentPort.postMessage(authInfo);
cb('user', 'pass');
});
}
request.on('response', (response) => {
process.parentPort.postMessage([response.statusCode, response.headers]);
response.on('data', (chunk) => data.push(chunk));
response.on('end', (chunk) => {
if (chunk) data.push(chunk);
process.parentPort.postMessage(Buffer.concat(data).toString());
});
});
if (configurableArg === '--request-data') {
process.parentPort.on('message', (e) => {
request.write(e.data);
request.end();
});
process.parentPort.postMessage('get-request-data');
} else {
request.end();
}
}