diff --git a/packages/@uppy/companion/src/server/Uploader.js b/packages/@uppy/companion/src/server/Uploader.js index 46728543a7..5921422cab 100644 --- a/packages/@uppy/companion/src/server/Uploader.js +++ b/packages/@uppy/companion/src/server/Uploader.js @@ -632,9 +632,8 @@ class Uploader { try { const httpMethod = (this.options.httpMethod || '').toUpperCase() === 'PUT' ? 'put' : 'post' - const runRequest = (await got)[httpMethod] - const response = await runRequest(url, reqOptions) + const response = await got[httpMethod](url, reqOptions) if (bytesUploaded !== this.size) { const errMsg = `uploaded only ${bytesUploaded} of ${this.size} with status: ${response.statusCode}` diff --git a/packages/@uppy/companion/src/server/got.js b/packages/@uppy/companion/src/server/got.js index b761d7c6c0..508f6ff9a2 100644 --- a/packages/@uppy/companion/src/server/got.js +++ b/packages/@uppy/companion/src/server/got.js @@ -1,4 +1,41 @@ -// eslint-disable-next-line import/no-unresolved -const gotPromise = import('got') - -module.exports = gotPromise.then((got) => got.default) +const loadingModule = import('got').then(({ default: got }) => { + module.exports = got + return got + }) + + const handler = { + __proto__: null, + get(target, key) { + return async (...args) => { + const module = await loadingModule + let intermediate = module[target.key](...target.args) + if (key === 'then') intermediate = Promise.resolve(intermediate) + return intermediate[key](...args) + } + }, + } + + /** + * @typedef {typeof import('got')} + */ + module.exports = new Proxy( + {}, + { + // @ts-expect-error __proto__ is fine here. + __proto__: null, + get(target, key) { + return (...args) => { + return key === 'then' + ? loadingModule.then(...args) + : new Proxy( + { + key, + args, + }, + handler, + ) + } + }, + }, + ) + \ No newline at end of file diff --git a/packages/@uppy/companion/src/server/helpers/request.js b/packages/@uppy/companion/src/server/helpers/request.js index 9de9f5283d..9f1303fbac 100644 --- a/packages/@uppy/companion/src/server/helpers/request.js +++ b/packages/@uppy/companion/src/server/helpers/request.js @@ -93,7 +93,7 @@ async function getProtectedGot ({ blockLocalIPs }) { // @ts-ignore - return (await got).extend({ agent: { http: httpAgent, https: httpsAgent } }) + return got.extend({ agent: { http: httpAgent, https: httpsAgent } }) } module.exports.getProtectedGot = getProtectedGot diff --git a/packages/@uppy/companion/src/server/jobs.js b/packages/@uppy/companion/src/server/jobs.js index 68fde58210..b1dab62801 100644 --- a/packages/@uppy/companion/src/server/jobs.js +++ b/packages/@uppy/companion/src/server/jobs.js @@ -66,7 +66,7 @@ async function runPeriodicPing ({ urls, payload, requestTimeout }) { // Run requests in parallel await Promise.all(urls.map(async (url) => { try { - await (await got).post(url, { json: payload, timeout: { request: requestTimeout } }) + await got.post(url, { json: payload, timeout: { request: requestTimeout } }) } catch (err) { logger.warn(err, 'jobs.periodic.ping') } diff --git a/packages/@uppy/companion/src/server/provider/box/index.js b/packages/@uppy/companion/src/server/provider/box/index.js index 923884e35c..b858fa8d05 100644 --- a/packages/@uppy/companion/src/server/provider/box/index.js +++ b/packages/@uppy/companion/src/server/provider/box/index.js @@ -8,7 +8,7 @@ const got = require('../../got') const BOX_FILES_FIELDS = 'id,modified_at,name,permissions,size,type' const BOX_THUMBNAIL_SIZE = 256 -const getClient = async ({ token }) => (await got).extend({ +const getClient = async ({ token }) => got.extend({ prefixUrl: 'https://api.box.com/2.0', headers: { authorization: `Bearer ${token}`, diff --git a/packages/@uppy/companion/src/server/provider/credentials.js b/packages/@uppy/companion/src/server/provider/credentials.js index 166327fdd3..75e1291ed9 100644 --- a/packages/@uppy/companion/src/server/provider/credentials.js +++ b/packages/@uppy/companion/src/server/provider/credentials.js @@ -14,7 +14,7 @@ const got = require('../got') */ async function fetchKeys (url, providerName, credentialRequestParams) { try { - const { credentials } = await (await got).post(url, { + const { credentials } = await got.post(url, { json: { provider: providerName, parameters: credentialRequestParams }, }).json() diff --git a/packages/@uppy/companion/src/server/provider/drive/index.js b/packages/@uppy/companion/src/server/provider/drive/index.js index 95b4561b7e..220c8c1f63 100644 --- a/packages/@uppy/companion/src/server/provider/drive/index.js +++ b/packages/@uppy/companion/src/server/provider/drive/index.js @@ -21,14 +21,14 @@ const DRIVE_FILES_FIELDS = `kind,nextPageToken,incompleteSearch,files(${DRIVE_FI // using wildcard to get all 'drive' fields because specifying fields seems no to work for the /drives endpoint const SHARED_DRIVE_FIELDS = '*' -const getClient = async ({ token }) => (await got).extend({ +const getClient = async ({ token }) => got.extend({ prefixUrl: 'https://www.googleapis.com/drive/v3', headers: { authorization: `Bearer ${token}`, }, }) -const getOauthClient = async () => (await got).extend({ +const getOauthClient = async () => got.extend({ prefixUrl: 'https://oauth2.googleapis.com', }) @@ -178,7 +178,7 @@ class Drive extends Provider { logout ({ token }) { return this.#withErrorHandling('provider.drive.logout.error', async () => { - await (await got).post('https://accounts.google.com/o/oauth2/revoke', { + await got.post('https://accounts.google.com/o/oauth2/revoke', { searchParams: { token }, responseType: 'json', }) diff --git a/packages/@uppy/companion/src/server/provider/dropbox/index.js b/packages/@uppy/companion/src/server/provider/dropbox/index.js index 236bb0419c..cb633b5aef 100644 --- a/packages/@uppy/companion/src/server/provider/dropbox/index.js +++ b/packages/@uppy/companion/src/server/provider/dropbox/index.js @@ -18,14 +18,14 @@ function httpHeaderSafeJson (v) { }) } -const getClient = async ({ token }) => (await got).extend({ +const getClient = async ({ token }) => got.extend({ prefixUrl: 'https://api.dropboxapi.com/2', headers: { authorization: `Bearer ${token}`, }, }) -const getOauthClient = async () => (await got).extend({ +const getOauthClient = async () => got.extend({ prefixUrl: 'https://api.dropboxapi.com/oauth2', }) diff --git a/packages/@uppy/companion/src/server/provider/facebook/index.js b/packages/@uppy/companion/src/server/provider/facebook/index.js index 22e6367137..9d83c5e200 100644 --- a/packages/@uppy/companion/src/server/provider/facebook/index.js +++ b/packages/@uppy/companion/src/server/provider/facebook/index.js @@ -7,7 +7,7 @@ const { prepareStream } = require('../../helpers/utils') const got = require('../../got') -const getClient = async ({ token }) => (await got).extend({ +const getClient = async ({ token }) => got.extend({ prefixUrl: 'https://graph.facebook.com', headers: { authorization: `Bearer ${token}`, diff --git a/packages/@uppy/companion/src/server/provider/instagram/graph/index.js b/packages/@uppy/companion/src/server/provider/instagram/graph/index.js index 13c52aa055..78435fdab9 100644 --- a/packages/@uppy/companion/src/server/provider/instagram/graph/index.js +++ b/packages/@uppy/companion/src/server/provider/instagram/graph/index.js @@ -7,7 +7,7 @@ const { prepareStream } = require('../../../helpers/utils') const got = require('../../../got') -const getClient = async ({ token }) => (await got).extend({ +const getClient = async ({ token }) => got.extend({ prefixUrl: 'https://graph.instagram.com', headers: { authorization: `Bearer ${token}`, diff --git a/packages/@uppy/companion/src/server/provider/onedrive/index.js b/packages/@uppy/companion/src/server/provider/onedrive/index.js index 2d89efaf23..99c0b3c59a 100644 --- a/packages/@uppy/companion/src/server/provider/onedrive/index.js +++ b/packages/@uppy/companion/src/server/provider/onedrive/index.js @@ -6,14 +6,14 @@ const { prepareStream } = require('../../helpers/utils') const got = require('../../got') -const getClient = async ({ token }) => (await got).extend({ +const getClient = async ({ token }) => got.extend({ prefixUrl: 'https://graph.microsoft.com/v1.0', headers: { authorization: `Bearer ${token}`, }, }) -const getOauthClient = async () => (await got).extend({ +const getOauthClient = async () => got.extend({ prefixUrl: 'https://login.live.com', }) diff --git a/packages/@uppy/companion/src/server/provider/unsplash/index.js b/packages/@uppy/companion/src/server/provider/unsplash/index.js index 77c0d5c001..6fe7c2cbec 100644 --- a/packages/@uppy/companion/src/server/provider/unsplash/index.js +++ b/packages/@uppy/companion/src/server/provider/unsplash/index.js @@ -9,7 +9,7 @@ const got = require('../../got') const BASE_URL = 'https://api.unsplash.com' -const getClient = async ({ token }) => (await got).extend({ +const getClient = async ({ token }) => got.extend({ prefixUrl: BASE_URL, headers: { authorization: `Client-ID ${token}`, @@ -42,7 +42,7 @@ class Unsplash extends Provider { const { links: { download: url, download_location: attributionUrl } } = await getPhotoMeta(client, id) - const stream = (await got).stream.get(url, { responseType: 'json' }) + const stream = got.stream.get(url, { responseType: 'json' }) await prepareStream(stream) // To attribute the author of the image, we call the `download_location` diff --git a/packages/@uppy/companion/src/server/provider/zoom/index.js b/packages/@uppy/companion/src/server/provider/zoom/index.js index 7cf2284185..4d3ed86205 100644 --- a/packages/@uppy/companion/src/server/provider/zoom/index.js +++ b/packages/@uppy/companion/src/server/provider/zoom/index.js @@ -11,7 +11,7 @@ const BASE_URL = 'https://zoom.us/v2' const PAGE_SIZE = 300 const DEAUTH_EVENT_NAME = 'app_deauthorized' -const getClient = async ({ token }) => (await got).extend({ +const getClient = async ({ token }) => got.extend({ prefixUrl: BASE_URL, headers: { authorization: `Bearer ${token}`, @@ -114,7 +114,7 @@ class Zoom extends Provider { return this.#withErrorHandling('provider.zoom.logout.error', async () => { const { key, secret } = await companion.getProviderCredentials() - const { status } = await (await got).post('https://zoom.us/oauth/revoke', { + const { status } = await got.post('https://zoom.us/oauth/revoke', { searchParams: { token }, headers: { Authorization: getBasicAuthHeader(key, secret) }, responseType: 'json', @@ -137,7 +137,7 @@ class Zoom extends Provider { return { data: {}, status: 400 } } - await (await got).post('https://api.zoom.us/oauth/data/compliance', { + await got.post('https://api.zoom.us/oauth/data/compliance', { headers: { Authorization: getBasicAuthHeader(key, secret) }, json: { client_id: key, diff --git a/packages/@uppy/companion/test/__tests__/providers.js b/packages/@uppy/companion/test/__tests__/providers.js index 98c472e55e..530df82961 100644 --- a/packages/@uppy/companion/test/__tests__/providers.js +++ b/packages/@uppy/companion/test/__tests__/providers.js @@ -295,7 +295,7 @@ describe('list provider files', () => { }) }) -describe('provider file gets downloaded from', () => { +describe.only('provider file gets downloaded from', () => { async function runTest (providerName) { const providerFixtures = fixtures.providers[providerName].expects const res = await request(authServer)