UNPKG

five-server

Version:

Development Server with Live Reload Capability. (Maintained Fork of Live Server)

53 lines 2.58 kB
"use strict"; /** * @author Yannick Deubel (https://github.com/yandeu) * @copyright Copyright (c) 2021 Yannick Deubel * @license {@link https://github.com/yandeu/five-server/blob/main/LICENSE LICENSE} */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.nodeFetch = void 0; const misc_js_1 = require("./misc.js"); const http_1 = __importDefault(require("http")); const https_1 = __importDefault(require("https")); /** Reject will return statusCode and statusMessage { code: number, message: string } */ const nodeFetch = (url, options = {}, redirects = 0) => { const data = []; const module = /^https/.test(url) ? https_1.default : http_1.default; const isURL = /^https?:\/\//; const maxRedirect = 5; return new Promise((resolve, reject) => { if (!isURL.test(url)) return reject((0, misc_js_1.createHttpError)(400, `${url} is not a valid url`)); module .get(url, options, res => { const code = res.statusCode; const message = res.statusMessage; const location = res.headers.location; if (redirects >= maxRedirect) return reject((0, misc_js_1.createHttpError)(429, 'Too Many Requests')); if (!code) return reject((0, misc_js_1.createHttpError)(code, message)); if (code >= 400) return reject((0, misc_js_1.createHttpError)(code, message)); if (code >= 300 && typeof location !== 'string') return reject((0, misc_js_1.createHttpError)(code, 'location not found in headers')); if (code >= 300 && typeof location === 'string') { if (isURL.test(location)) (0, exports.nodeFetch)(location, options, (redirects += 1)).then(resolve, reject); else (0, exports.nodeFetch)(new URL(location, url).href, options, (redirects += 1)).then(resolve, reject); } if (code < 300) res .on('data', chunk => data.push(chunk)) .on('end', () => resolve(Buffer.concat(data))) .on('error', error => reject((0, misc_js_1.createHttpError)(500, error.message))); }) .on('error', error => reject((0, misc_js_1.createHttpError)(500, error.message))); }); }; exports.nodeFetch = nodeFetch; //# sourceMappingURL=nodeFetch.js.map