replace with custom superagent based on fetch API
This commit is contained in:
+10
-13
@@ -16,7 +16,7 @@ const assert = require('assert'),
|
||||
debug = require('debug')('box:applinks'),
|
||||
jsdom = require('jsdom'),
|
||||
safe = require('safetydance'),
|
||||
superagent = require('superagent'),
|
||||
superagent = require('./superagent.js'),
|
||||
uuid = require('uuid');
|
||||
|
||||
const APPLINKS_FIELDS= [ 'id', 'accessRestrictionJson', 'creationTime', 'updateTime', 'ts', 'label', 'tagsJson', 'icon', 'upstreamUri' ].join(',');
|
||||
@@ -90,15 +90,12 @@ async function listByUser(user) {
|
||||
async function detectMetaInfo(upstreamUri) {
|
||||
assert.strictEqual(typeof upstreamUri, 'string');
|
||||
|
||||
const [error, response] = await safe(superagent.get(upstreamUri).set('User-Agent', 'Mozilla').timeout(10*1000));
|
||||
if (error || !response.text) {
|
||||
debug('detectMetaInfo: Unable to fetch upstreamUri to detect icon and title', error.statusCode);
|
||||
const [upstreamError, upstreamRespose] = await safe(superagent.get(upstreamUri).set('User-Agent', 'Mozilla').timeout(10*1000));
|
||||
if (upstreamError) {
|
||||
debug(`detectMetaInfo: error fetching ${upstreamUri}: ${upstreamError.status}`);
|
||||
return null;
|
||||
}
|
||||
|
||||
// set redirected URI if any for favicon url
|
||||
const redirectUri = (response.redirects && response.redirects.length) ? response.redirects[0] : null;
|
||||
|
||||
const virtualConsole = new jsdom.VirtualConsole();
|
||||
virtualConsole.on('error', () => {
|
||||
// No-op to skip console errors.
|
||||
@@ -134,14 +131,14 @@ async function detectMetaInfo(upstreamUri) {
|
||||
if (!favicon && dom.window.document.querySelector('meta[itemprop="image"]')) favicon = dom.window.document.querySelector('meta[itemprop="image"]').content;
|
||||
|
||||
if (favicon) {
|
||||
favicon = new URL(favicon, redirectUri || upstreamUri).toString();
|
||||
favicon = new URL(favicon, upstreamRespose.url).toString();
|
||||
|
||||
debug(`detectMetaInfo: found icon: ${favicon}`);
|
||||
|
||||
const [error, response] = await safe(superagent.get(favicon).timeout(10*1000));
|
||||
if (error) debug(`Failed to fetch icon ${favicon}: `, error);
|
||||
else if (response.ok && response.headers['content-type'].indexOf('image') !== -1) icon = response.body || response.text;
|
||||
else debug(`Failed to fetch icon ${favicon}: statusCode=${response.status}`);
|
||||
if (error) debug(`Failed to fetch icon ${favicon}: ${error.message} ${error.status}`);
|
||||
else if (response.headers['content-type']?.indexOf('image') !== -1) icon = response.body || response.text;
|
||||
else debug(`Failed to fetch icon ${favicon}: status=${response.status}`);
|
||||
}
|
||||
|
||||
if (!favicon) {
|
||||
@@ -149,8 +146,8 @@ async function detectMetaInfo(upstreamUri) {
|
||||
|
||||
const [error, response] = await safe(superagent.get(`${upstreamUri}/favicon.ico`).timeout(10*1000));
|
||||
if (error) debug(`Failed to fetch icon ${favicon}: `, error);
|
||||
else if (response.ok && response.headers['content-type'].indexOf('image') !== -1) icon = response.body || response.text;
|
||||
else debug(`Failed to fetch icon ${favicon}: statusCode=${response.status} content type ${response.headers['content-type']}`);
|
||||
else if (response.headers['content-type']?.indexOf('image') !== -1) icon = response.body || response.text;
|
||||
else debug(`Failed to fetch icon ${favicon}: status=${response.status} content type ${response.headers['content-type']}`);
|
||||
}
|
||||
|
||||
// detect label
|
||||
|
||||
Reference in New Issue
Block a user