Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
module.exports = (options, input) => {
const emitter = new EventEmitter();
const redirects = [];
let currentRequest;
let requestUrl;
let redirectString;
let uploadBodySize;
let retryCount = 0;
let shouldAbort = false;
const setCookie = options.cookieJar ? util.promisify(options.cookieJar.setCookie.bind(options.cookieJar)) : null;
const getCookieString = options.cookieJar ? util.promisify(options.cookieJar.getCookieString.bind(options.cookieJar)) : null;
const agents = is.object(options.agent) ? options.agent : null;
const emitError = async error => {
try {
for (const hook of options.hooks.beforeError) {
// eslint-disable-next-line no-await-in-loop
error = await hook(error);
}
emitter.emit('error', error);
} catch (error2) {
emitter.emit('error', error2);
}
};
const get = async options => {
const currentUrl = redirectString || requestUrl;
!is.nodeStream(options.body) &&
!is.string(options.body) &&
!is.buffer(options.body) &&
!(is.object(options.body) && isFormData(options.body))
) {
throw new TypeError('The `body` option must be a stream.Readable, string or Buffer');
}
if (isForm && !is.object(options.form)) {
throw new TypeError('The `form` option must be an Object');
}
}
if (options.body) {
// Special case for https://github.com/form-data/form-data
if (is.object(options.body) && isFormData(options.body) && noContentType) {
headers['content-type'] = `multipart/form-data; boundary=${options.body.getBoundary()}`;
}
} else if (options.form) {
if (noContentType) {
headers['content-type'] = 'application/x-www-form-urlencoded';
}
options.body = (new URLSearchParams(options.form as Record)).toString();
} else if (options.json) {
if (noContentType) {
headers['content-type'] = 'application/json';
}
options.body = JSON.stringify(options.json);
}
options.method = options.method.toUpperCase() as Method;
} else {
options.method = defaults?.method ?? 'GET';
}
// Better memory management, so we don't have to generate a new object every time
if (options.cache) {
(options as NormalizedOptions).cacheableRequest = new CacheableRequest(
// @ts-ignore Cannot properly type a function with multiple definitions yet
(requestOptions, handler) => requestOptions.request(requestOptions, handler),
options.cache
);
}
// `options.cookieJar`
if (is.object(options.cookieJar)) {
let {setCookie, getCookieString} = options.cookieJar;
// Horrible `tough-cookie` check
if (setCookie.length === 4 && getCookieString.length === 0) {
if (!Reflect.has(setCookie, promisify.custom)) {
// @ts-ignore TS is dumb.
setCookie = promisify(setCookie.bind(options.cookieJar));
getCookieString = promisify(getCookieString.bind(options.cookieJar));
}
} else if (setCookie.length !== 2) {
throw new TypeError('`options.cookieJar.setCookie` needs to be an async function with 2 arguments');
} else if (getCookieString.length !== 1) {
throw new TypeError('`options.cookieJar.getCookieString` needs to be an async function with 1 argument');
}
options.cookieJar = {setCookie, getCookieString};
export const fetchVisualMediaAttachments = async ({
conversationId,
WhisperMessageCollection,
}: {
conversationId: string;
WhisperMessageCollection: BackboneCollection;
}): Promise> => {
if (!is.string(conversationId)) {
throw new TypeError("'conversationId' is required");
}
if (!is.object(WhisperMessageCollection)) {
throw new TypeError("'WhisperMessageCollection' is required");
}
const collection = new WhisperMessageCollection();
const lowerReceivedAt = 0;
const upperReceivedAt = Number.MAX_VALUE;
const hasVisualMediaAttachments = 1;
await deferredToPromise(
collection.fetch({
index: {
name: 'hasVisualMediaAttachments',
lower: [conversationId, lowerReceivedAt, hasVisualMediaAttachments],
upper: [conversationId, upperReceivedAt, hasVisualMediaAttachments],
order: 'desc',
},
limit: 50,
function normalizeArguments(url, opts) {
if (!is.string(url) && !is.object(url)) {
throw new TypeError(`Parameter \`url\` must be a string or object, not ${is(url)}`);
} else if (is.string(url)) {
url = url.replace(/^unix:/, 'http://$&');
try {
decodeURI(url);
} catch (err) {
throw new Error('Parameter `url` must contain valid UTF-8 character sequences');
}
url = urlParseLax(url);
if (url.auth) {
throw new Error('Basic authentication must be done with the `auth` option');
}
} else if (isURL.lenient(url)) {
url = urlToOptions(url);
const normalize = (url, options, defaults) => {
if (is.plainObject(url)) {
options = {...url, ...options};
url = options.url || {};
delete options.url;
}
if (defaults) {
options = merge({}, defaults.options, options ? preNormalize(options, defaults.options) : {});
} else {
options = merge({}, preNormalize(options));
}
if (!is.string(url) && !is.object(url)) {
throw new TypeError(`Parameter \`url\` must be a string or object, not ${is(url)}`);
}
if (is.string(url)) {
if (options.baseUrl) {
if (url.toString().startsWith('/')) {
url = url.toString().slice(1);
}
url = urlToOptions(new URL(url, options.baseUrl));
} else {
url = url.replace(/^unix:/, 'http://$&');
url = urlParseLax(url);
}
} else if (is(url) === 'URL') {
url = urlToOptions(url);
function normalizeArguments(url, opts) {
if (!is.string(url) && !is.object(url)) {
throw new TypeError(`Parameter \`url\` must be a string or object, not ${is(url)}`);
} else if (is.string(url)) {
url = url.replace(/^unix:/, 'http://$&');
try {
decodeURI(url);
} catch (err) {
throw new Error('Parameter `url` must contain valid UTF-8 character sequences');
}
url = urlParseLax(url);
if (url.auth) {
throw new Error('Basic authentication must be done with the `auth` option');
}
} else if (isURL.lenient(url)) {
url = urlToOptions(url);
const {retry} = options;
if (defaults) {
options.retry = {...defaults.retry};
} else {
options.retry = {
calculateDelay: retryObject => retryObject.computedValue,
limit: 0,
methods: [],
statusCodes: [],
errorCodes: [],
maxRetryAfter: undefined
};
}
if (is.object(retry)) {
options.retry = {
...options.retry,
...retry
};
} else if (is.number(retry)) {
options.retry.limit = retry;
}
if (options.retry.maxRetryAfter === undefined) {
options.retry.maxRetryAfter = Math.min(
...[options.timeout.request, options.timeout.connect].filter((n): n is number => !is.nullOrUndefined(n))
);
}
options.retry.methods = [...new Set(options.retry.methods!.map(method => method.toUpperCase() as Method))];
options.retry.statusCodes = [...new Set(options.retry.statusCodes)];