UNPKG

@stencila/jesta

Version:

Stencila plugin for executable documents using JavaScript

67 lines (66 loc) 2.1 kB
"use strict"; /** * A utility module for HTTP requests * * Uses `got` to enable RFC 7234 compliant HTTP caching * * @module util/http */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.download = exports.get = exports.client = void 0; const logga_1 = require("@stencila/logga"); const fs_1 = __importDefault(require("fs")); const got_1 = __importDefault(require("got")); const stream_1 = __importDefault(require("stream")); const util_1 = __importDefault(require("util")); const cache_1 = require("./cache"); const pipeline = util_1.default.promisify(stream_1.default.pipeline); const log = logga_1.getLogger('jesta:get'); /** * A `got` instance with default options for HTTP requests. * * User agent is set, and includes a `mailto`, for politeness: * https://github.com/CrossRef/rest-api-doc#good-manners--more-reliable-service */ exports.client = got_1.default.extend({ cache: cache_1.cache, retry: { limit: 5, }, headers: { 'user-agent': 'Stencila (https://github.com/stencila/jesta; mailto:hello@stenci.la)', 'accept-encoding': 'gzip, deflate', }, }); /** * Get content from a URL * * @param url The URL to get * @param options Options to pass to `got` */ async function get(url, options = {}) { var _a; try { return (await exports.client.get(url, options)); } catch (error) { const { message, response } = error; const body = (_a = response === null || response === void 0 ? void 0 : response.body) !== null && _a !== void 0 ? _a : ''; log.warn(`Unable to get ${url}: ${message}: ${body.slice(0, 500)}`); return response; } } exports.get = get; /** * Download a file * * @param from The URL to download from * @param to The file path to download to */ async function download(from, to) { return pipeline(exports.client.stream(from), fs_1.default.createWriteStream(to)); } exports.download = download;