1
1
import events from 'node:events'
2
2
import { createReadStream , createWriteStream } from 'node:fs'
3
+ import http from 'node:http'
3
4
import https from 'node:https'
4
5
import path from 'node:path'
5
6
import readline from 'node:readline'
@@ -67,7 +68,7 @@ async function createDeleteRequest(
67
68
urlPath : string ,
68
69
options : RequestOptions
69
70
) : Promise < IncomingMessage > {
70
- const req = https
71
+ const req = getHttpModule ( baseUrl )
71
72
. request ( `${ baseUrl } ${ urlPath } ` , {
72
73
method : 'DELETE' ,
73
74
...options
@@ -81,7 +82,7 @@ async function createGetRequest(
81
82
urlPath : string ,
82
83
options : RequestOptions
83
84
) : Promise < IncomingMessage > {
84
- const req = https
85
+ const req = getHttpModule ( baseUrl )
85
86
. request ( `${ baseUrl } ${ urlPath } ` , {
86
87
method : 'GET' ,
87
88
...options
@@ -96,7 +97,7 @@ async function createPostRequest(
96
97
postJson : any ,
97
98
options : RequestOptions
98
99
) : Promise < IncomingMessage > {
99
- const req = https
100
+ const req = getHttpModule ( baseUrl )
100
101
. request ( `${ baseUrl } ${ urlPath } ` , {
101
102
method : 'POST' ,
102
103
...options
@@ -153,7 +154,7 @@ async function createUploadRequest(
153
154
: [ boundarySep ] ) ,
154
155
`--${ boundary } --\n`
155
156
]
156
- const req = https . request ( `${ baseUrl } ${ urlPath } ` , {
157
+ const req = getHttpModule ( baseUrl ) . request ( `${ baseUrl } ${ urlPath } ` , {
157
158
method : 'POST' ,
158
159
...options ,
159
160
headers : {
@@ -182,6 +183,11 @@ async function createUploadRequest(
182
183
return await getResponse ( req )
183
184
}
184
185
186
+ function getHttpModule ( baseUrl : string ) : typeof http | typeof https {
187
+ const { protocol } = new URL ( baseUrl )
188
+ return protocol === 'https:' ? https : http
189
+ }
190
+
185
191
async function getResponse ( req : ClientRequest ) : Promise < IncomingMessage > {
186
192
try {
187
193
const { 0 : res } = ( await events . once ( req , 'response' , {
@@ -277,7 +283,7 @@ export class SocketSdk {
277
283
componentsObj : { components : Array < { purl : string } > }
278
284
) : Promise < IncomingMessage > {
279
285
// Adds the first 'abort' listener to abortSignal.
280
- const req = https
286
+ const req = getHttpModule ( this . #baseUrl )
281
287
. request (
282
288
`${ this . #baseUrl} purl?${ new URLSearchParams ( queryParams ?? '' ) } ` ,
283
289
{
@@ -659,7 +665,7 @@ export class SocketSdk {
659
665
file ?: string
660
666
) : Promise < SocketSdkResultType < 'getOrgFullScan' > > {
661
667
try {
662
- const req = https
668
+ const req = getHttpModule ( this . #baseUrl )
663
669
. request (
664
670
`${ this . #baseUrl} orgs/${ encodeURIComponent ( orgSlug ) } /full-scans/${ encodeURIComponent ( fullScanId ) } ` ,
665
671
{
0 commit comments