mirror of
https://github.com/titaniumnetwork-dev/Ultraviolet.git
synced 2025-05-16 13:00:01 -04:00
33 lines
No EOL
873 B
JavaScript
33 lines
No EOL
873 B
JavaScript
import http from 'http';
|
|
import https from 'https';
|
|
import { json, prepareRequest, prepareResponse } from './util.js';
|
|
|
|
function request(request, response) {
|
|
try {
|
|
const data = prepareRequest(request);
|
|
const protocol = data.protocol === 'https:' ? https : http;
|
|
|
|
const remoteRequest = protocol.request(data);
|
|
|
|
remoteRequest.on('response', remoteResponse => {
|
|
const send = prepareResponse(remoteResponse);
|
|
|
|
response.writeHead(...send);
|
|
remoteResponse.pipe(response);
|
|
});
|
|
|
|
remoteRequest.on('error', e => {
|
|
json(response, 500, {
|
|
error: e.toString()
|
|
});
|
|
});
|
|
|
|
request.pipe(remoteRequest);
|
|
} catch(e) {
|
|
json(response, 500, {
|
|
error: e.toString()
|
|
});
|
|
};
|
|
};
|
|
|
|
export default request; |