Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a service for a common http interface #720

Merged
merged 5 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion backend/src/scripts/listRegistryImages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import https from 'node:https'
import fetch from 'node-fetch'

import { getAccessToken } from '../routes/v1/registryAuth.js'
import { getHttpsAgent } from '../services/v2/http.js'
import config from '../utils/config.js'
import { connectToMongoose, disconnectFromMongoose } from '../utils/database.js'
import logger from '../utils/logger.js'

const httpsAgent = new https.Agent({
const httpsAgent = getHttpsAgent({
rejectUnauthorized: !config.registry.insecure,
})

Expand Down
7 changes: 7 additions & 0 deletions backend/src/services/v2/http.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import https from 'node:https'

// This function has the same syntax as 'https.Agent', but is centralised throughout
// the application in case it needs to be altered.
export function getHttpsAgent(config?: https.AgentOptions) {
return new https.Agent({ ...config })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to create a new agent on every call to this function or should we be reusing a single agent?

Could do something like:

// Not sure what the correct type is off the top of my head
let httpsAgent: HttpsAgent | undefined

export function getHttpsAgent(config?: https.AgentOptions) {
  if (!httpsAgent) {
    httpsAgent = new https.Agent({ ...config })
  }
  return httpsAgent
}

}
11 changes: 11 additions & 0 deletions backend/test/services/http.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import https from 'node:http'

import { describe, expect, test } from 'vitest'

import { getHttpsAgent } from '../../src/services/v2/http.js'

describe('services > http', () => {
test('getHttpsAgent', () => {
expect(getHttpsAgent()).toBeInstanceOf(https.Agent)
})
})