Skip to content

Commit 9669b40

Browse files
committed
Use right http module for the baseUrl
1 parent 56f122f commit 9669b40

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/index.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import events from 'node:events'
22
import { createReadStream, createWriteStream } from 'node:fs'
3+
import http from 'node:http'
34
import https from 'node:https'
45
import path from 'node:path'
56
import readline from 'node:readline'
@@ -67,7 +68,7 @@ async function createDeleteRequest(
6768
urlPath: string,
6869
options: RequestOptions
6970
): Promise<IncomingMessage> {
70-
const req = https
71+
const req = getHttpModule(baseUrl)
7172
.request(`${baseUrl}${urlPath}`, {
7273
method: 'DELETE',
7374
...options
@@ -81,7 +82,7 @@ async function createGetRequest(
8182
urlPath: string,
8283
options: RequestOptions
8384
): Promise<IncomingMessage> {
84-
const req = https
85+
const req = getHttpModule(baseUrl)
8586
.request(`${baseUrl}${urlPath}`, {
8687
method: 'GET',
8788
...options
@@ -96,7 +97,7 @@ async function createPostRequest(
9697
postJson: any,
9798
options: RequestOptions
9899
): Promise<IncomingMessage> {
99-
const req = https
100+
const req = getHttpModule(baseUrl)
100101
.request(`${baseUrl}${urlPath}`, {
101102
method: 'POST',
102103
...options
@@ -153,7 +154,7 @@ async function createUploadRequest(
153154
: [boundarySep]),
154155
`--${boundary}--\n`
155156
]
156-
const req = https.request(`${baseUrl}${urlPath}`, {
157+
const req = getHttpModule(baseUrl).request(`${baseUrl}${urlPath}`, {
157158
method: 'POST',
158159
...options,
159160
headers: {
@@ -182,6 +183,11 @@ async function createUploadRequest(
182183
return await getResponse(req)
183184
}
184185

186+
function getHttpModule(baseUrl: string): typeof http | typeof https {
187+
const { protocol } = new URL(baseUrl)
188+
return protocol === 'https:' ? https : http
189+
}
190+
185191
async function getResponse(req: ClientRequest): Promise<IncomingMessage> {
186192
try {
187193
const { 0: res } = (await events.once(req, 'response', {
@@ -277,7 +283,7 @@ export class SocketSdk {
277283
componentsObj: { components: Array<{ purl: string }> }
278284
): Promise<IncomingMessage> {
279285
// Adds the first 'abort' listener to abortSignal.
280-
const req = https
286+
const req = getHttpModule(this.#baseUrl)
281287
.request(
282288
`${this.#baseUrl}purl?${new URLSearchParams(queryParams ?? '')}`,
283289
{
@@ -659,7 +665,7 @@ export class SocketSdk {
659665
file?: string
660666
): Promise<SocketSdkResultType<'getOrgFullScan'>> {
661667
try {
662-
const req = https
668+
const req = getHttpModule(this.#baseUrl)
663669
.request(
664670
`${this.#baseUrl}orgs/${encodeURIComponent(orgSlug)}/full-scans/${encodeURIComponent(fullScanId)}`,
665671
{

0 commit comments

Comments
 (0)