summaryrefslogtreecommitdiff
path: root/includes/external/addressbook/node_modules/open-graph-scraper/dist/lib/request.js
blob: 5d8ee9e878c518f4143e9fe1a32a6f7ef9248337 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const chardet_1 = require("chardet");
const iconv_lite_1 = require("iconv-lite");
const utils_1 = require("./utils");
/**
 * performs the got request and formats the body for ogs
 *
 * @param {object} gotOptions - options for got
 * @param {object} ogsOptions - options for ogs
 * @return {object} formatted request body and response
 *
 */
async function requestAndResultsFormatter(gotOptions, ogsOptions) {
    const got = await (0, utils_1.gotClient)(ogsOptions.downloadLimit);
    return got(gotOptions)
        .then((response) => {
        if (response && response.headers && response.headers['content-type'] && !response.headers['content-type'].includes('text/')) {
            throw new Error('Page must return a header content-type with text/');
        }
        if (response && response.statusCode && (response.statusCode.toString().substring(0, 1) === '4' || response.statusCode.toString().substring(0, 1) === '5')) {
            throw new Error('Server has returned a 400/500 error code');
        }
        if (response.body === undefined || response.body === '') {
            throw new Error('Page not found');
        }
        const char = chardet_1.default.detect(response.rawBody);
        let decodedBody = response.rawBody.toString();
        if (char && typeof response.rawBody === 'object') {
            decodedBody = (0, iconv_lite_1.decode)(response.rawBody, char);
        }
        if (!decodedBody) {
            throw new Error('Page not found');
        }
        return { decodedBody, response };
    })
        .catch((error) => {
        if (error instanceof Error)
            throw error;
        throw new Error(error);
    });
}
exports.default = requestAndResultsFormatter;