Skip to content

Commit

Permalink
add Proxy for got
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed Apr 17, 2024
1 parent 5140f07 commit 5b7f3c0
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 25 deletions.
3 changes: 1 addition & 2 deletions packages/@uppy/companion/src/server/Uploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
Expand Down
45 changes: 41 additions & 4 deletions packages/@uppy/companion/src/server/got.js
Original file line number Diff line number Diff line change
@@ -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')}

Check failure on line 19 in packages/@uppy/companion/src/server/got.js

View workflow job for this annotation

GitHub Actions / Type tests

The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("got")' call instead.
*/
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,
)
}
},
},
)

2 changes: 1 addition & 1 deletion packages/@uppy/companion/src/server/helpers/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/@uppy/companion/src/server/jobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
Expand Down
2 changes: 1 addition & 1 deletion packages/@uppy/companion/src/server/provider/box/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
6 changes: 3 additions & 3 deletions packages/@uppy/companion/src/server/provider/drive/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
})

Expand Down Expand Up @@ -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',
})
Expand Down
4 changes: 2 additions & 2 deletions packages/@uppy/companion/src/server/provider/dropbox/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
Expand Down Expand Up @@ -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`
Expand Down
6 changes: 3 additions & 3 deletions packages/@uppy/companion/src/server/provider/zoom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
Expand Down Expand Up @@ -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',
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/@uppy/companion/test/__tests__/providers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 5b7f3c0

Please sign in to comment.