UNPKG

@decaf-ts/utils

Version:

module management utils for decaf-ts

77 lines 2.94 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.HttpClient = void 0; /* istanbul ignore file */ const https_1 = __importDefault(require("https")); const logging_1 = require("@decaf-ts/logging"); /** * @description A simple HTTP client for downloading files. * @summary This class provides functionality to download files from HTTPS URLs. * It uses Node.js built-in https module to make requests. * * @class HttpClient */ class HttpClient { static { this.log = logging_1.Logging.for(HttpClient); } /** * @description Downloads a file from a given URL. * @summary This method sends a GET request to the specified URL and returns the response body as a string. * It handles different scenarios such as non-200 status codes and network errors. * * @param url - The URL of the file to download. * @return A promise that resolves with the file content as a string. * * @mermaid * sequenceDiagram * participant Client * participant HttpClient * participant HTTPS * participant Server * Client->>HttpClient: downloadFile(url) * HttpClient->>HTTPS: get(url) * HTTPS->>Server: GET request * Server-->>HTTPS: Response * HTTPS-->>HttpClient: Response object * alt Status code is 200 * loop For each data chunk * HTTPS->>HttpClient: 'data' event * HttpClient->>HttpClient: Accumulate data * end * HTTPS->>HttpClient: 'end' event * HttpClient-->>Client: Resolve with data * else Status code is not 200 * HttpClient-->>Client: Reject with error * end */ static async downloadFile(url) { return new Promise((resolve, reject) => { function request(url) { url = encodeURI(url); https_1.default.get(url, (res) => { if (res.statusCode === 301 || res.statusCode === 307) return request(res.headers.location); if (res.statusCode !== 200) { HttpClient.log.error(`Failed to fetch ${url} (status: ${res.statusCode})`); return reject(new Error(`Failed to fetch ${url}`)); } let data = ""; res.on("data", (chunk) => { data += chunk; }); res.on("error", (error) => { reject(error); }); res.on("end", () => { resolve(data); }); }); } request(url); }); } } exports.HttpClient = HttpClient; //# sourceMappingURL=http.js.map