d2f70ee2a7
Co-authored-by: Cursor <cursoragent@cursor.com>
81 lines
2.4 KiB
JavaScript
81 lines
2.4 KiB
JavaScript
'use strict';
|
|
|
|
const http = require('http');
|
|
const https = require('https');
|
|
const { URL } = require('url');
|
|
|
|
function requestBuffer(options) {
|
|
return new Promise((resolve, reject) => {
|
|
const url = new URL(options.url);
|
|
const transport = url.protocol === 'https:' ? https : http;
|
|
const reqOptions = {
|
|
hostname: url.hostname,
|
|
port: url.port || (url.protocol === 'https:' ? 443 : 80),
|
|
path: url.pathname + url.search,
|
|
method: options.method || 'GET',
|
|
headers: options.headers || {},
|
|
rejectUnauthorized: options.rejectUnauthorized !== false
|
|
};
|
|
|
|
const req = transport.request(reqOptions, (res) => {
|
|
const chunks = [];
|
|
res.on('data', (chunk) => chunks.push(chunk));
|
|
res.on('end', () => {
|
|
resolve({
|
|
statusCode: res.statusCode,
|
|
headers: res.headers,
|
|
body: Buffer.concat(chunks)
|
|
});
|
|
});
|
|
});
|
|
|
|
req.on('error', reject);
|
|
if (options.body) {
|
|
req.write(options.body);
|
|
}
|
|
req.end();
|
|
});
|
|
}
|
|
|
|
function requestStream(options, writeBody) {
|
|
return new Promise((resolve, reject) => {
|
|
const url = new URL(options.url);
|
|
const transport = url.protocol === 'https:' ? https : http;
|
|
const reqOptions = {
|
|
hostname: url.hostname,
|
|
port: url.port || (url.protocol === 'https:' ? 443 : 80),
|
|
path: url.pathname + url.search,
|
|
method: options.method || 'POST',
|
|
headers: options.headers || {},
|
|
rejectUnauthorized: options.rejectUnauthorized !== false
|
|
};
|
|
|
|
const req = transport.request(reqOptions, (res) => {
|
|
const chunks = [];
|
|
res.on('data', (chunk) => chunks.push(chunk));
|
|
res.on('end', () => {
|
|
resolve({
|
|
statusCode: res.statusCode,
|
|
headers: res.headers,
|
|
body: Buffer.concat(chunks)
|
|
});
|
|
});
|
|
});
|
|
|
|
req.on('error', reject);
|
|
if (writeBody) {
|
|
writeBody(req);
|
|
} else if (options.body) {
|
|
req.write(options.body);
|
|
req.end();
|
|
} else {
|
|
req.end();
|
|
}
|
|
});
|
|
}
|
|
|
|
module.exports = {
|
|
requestBuffer,
|
|
requestStream
|
|
};
|