Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return async (url, options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
}
let files
try {
const parsedUrl = new URL(url)
const res = await fetch(url)
if (!res.ok) {
throw new Error('unexpected status code: ' + res.status)
}
// TODO: use res.body when supported
const content = Buffer.from(await res.arrayBuffer())
const path = decodeURIComponent(parsedUrl.pathname.split('/').pop())
files = await self.add({ content, path }, options)
} catch (err) {
if (callback) {
return callback(err)
}
throw err
const requestWithRedirect = (url, opts, sendOneFile, callback) => {
const parsedUrl = new URL(url)
const req = getRequest(parsedUrl, (res) => {
if (res.statusCode >= 400) {
return callback(new Error(`Failed to download with ${res.statusCode}`))
}
const redirection = res.headers.location
if (res.statusCode >= 300 && res.statusCode < 400 && redirection) {
if (!validUrl(redirection)) {
return callback(new Error('redirection url must be an http(s) url'))
}
requestWithRedirect(redirection, opts, sendOneFile, callback)
} else {
const requestOpts = {