@@ -6,6 +6,7 @@ import path from 'node:path'
6
6
import readline from 'node:readline'
7
7
8
8
import abortSignal from '@socketsecurity/registry/lib/constants/abort-signal'
9
+ import { hasOwn , isObjectObject } from '@socketsecurity/registry/lib/objects'
9
10
import { pRetry } from '@socketsecurity/registry/lib/promises'
10
11
11
12
// @ts -ignore: Avoid TS import attributes error.
@@ -32,8 +33,9 @@ export type Agent = HttpsAgent | HttpAgent | ClientHttp2Session
32
33
export type BatchPackageFetchResultType = SocketSdkResult < 'batchPackageFetch' >
33
34
34
35
export type BatchPackageStreamOptions = {
35
- chunkSize : number
36
- concurrencyLimit : number
36
+ chunkSize ?: number | undefined
37
+ concurrencyLimit ?: number | undefined
38
+ queryParams ?: QueryParams | undefined
37
39
}
38
40
39
41
export type GotOptions = {
@@ -42,6 +44,8 @@ export type GotOptions = {
42
44
http2 ?: ClientHttp2Session | undefined
43
45
}
44
46
47
+ export type QueryParams = Record < string , any >
48
+
45
49
export type RequestOptions =
46
50
| HttpsRequestOptions
47
51
| HttpRequestOptions
@@ -423,14 +427,14 @@ function queryToSearchParams(
423
427
init ?:
424
428
| URLSearchParams
425
429
| string
426
- | Record < string , any >
430
+ | QueryParams
427
431
| Iterable < [ string , any ] >
428
432
| ReadonlyArray < [ string , any ] >
429
433
| null
430
434
| undefined
431
435
) : URLSearchParams {
432
436
const params = new URLSearchParams ( init ?? '' )
433
- const normalized = { __proto__ : null } as unknown as Record < string , string >
437
+ const normalized = { __proto__ : null } as unknown as QueryParams
434
438
const entries : Iterable < [ string , any ] > = params . entries ( )
435
439
for ( const entry of entries ) {
436
440
let key = entry [ 0 ]
@@ -514,8 +518,8 @@ export class SocketSdk {
514
518
}
515
519
516
520
async #createBatchPurlRequest(
517
- queryParams : Record < string , string > | null | undefined ,
518
- componentsObj : { components : Array < { purl : string } > }
521
+ componentsObj : { components : Array < { purl : string } > } ,
522
+ queryParams ?: QueryParams | undefined
519
523
) : Promise < IncomingMessage > {
520
524
// Adds the first 'abort' listener to abortSignal.
521
525
const req = getHttpModule ( this . #baseUrl)
@@ -528,13 +532,13 @@ export class SocketSdk {
528
532
}
529
533
530
534
async * #createBatchPurlGenerator(
531
- queryParams : Record < string , string > | null | undefined ,
532
- componentsObj : { components : Array < { purl : string } > }
535
+ componentsObj : { components : Array < { purl : string } > } ,
536
+ queryParams ?: QueryParams | undefined
533
537
) : AsyncGenerator < BatchPackageFetchResultType > {
534
538
let res : IncomingMessage | undefined
535
539
try {
536
540
res = await pRetry (
537
- ( ) => this . #createBatchPurlRequest( queryParams , componentsObj ) ,
541
+ ( ) => this . #createBatchPurlRequest( componentsObj , queryParams ) ,
538
542
{
539
543
retries : 4 ,
540
544
onRetryRethrow : true ,
@@ -609,12 +613,19 @@ export class SocketSdk {
609
613
}
610
614
611
615
async batchPackageFetch (
612
- queryParams : Record < string , string > | null | undefined ,
613
- componentsObj : { components : Array < { purl : string } > }
616
+ componentsObj : { components : Array < { purl : string } > } ,
617
+ queryParams ?: QueryParams | undefined
614
618
) : Promise < BatchPackageFetchResultType > {
619
+ // Support previous argument signature.
620
+ if ( isObjectObject ( componentsObj ) && ! hasOwn ( componentsObj , 'components' ) ) {
621
+ const oldParam1 = componentsObj
622
+ const oldParam2 = queryParams
623
+ queryParams = oldParam1 as typeof oldParam2
624
+ componentsObj = oldParam2 as unknown as typeof oldParam1
625
+ }
615
626
let res : IncomingMessage | undefined
616
627
try {
617
- res = await this . #createBatchPurlRequest( queryParams , componentsObj )
628
+ res = await this . #createBatchPurlRequest( componentsObj , queryParams )
618
629
} catch ( e ) {
619
630
return await this . #handleApiError< 'batchPackageFetch' > ( e )
620
631
}
@@ -633,10 +644,29 @@ export class SocketSdk {
633
644
}
634
645
635
646
async * batchPackageStream (
636
- queryParams : Record < string , string > | null | undefined ,
637
647
componentsObj : { components : Array < { purl : string } > } ,
638
648
options ?: BatchPackageStreamOptions | undefined
639
649
) : AsyncGenerator < BatchPackageFetchResultType > {
650
+ // Support previous argument signature.
651
+ if ( isObjectObject ( componentsObj ) && ! hasOwn ( componentsObj , 'components' ) ) {
652
+ const oldParam1 = componentsObj
653
+ const oldParam2 = options
654
+ componentsObj = oldParam2 as unknown as typeof oldParam1
655
+ options = {
656
+ queryParams : oldParam1 as QueryParams ,
657
+ ...arguments [ 2 ]
658
+ } as BatchPackageStreamOptions
659
+ }
660
+
661
+ const {
662
+ chunkSize = 100 ,
663
+ concurrencyLimit = 10 ,
664
+ queryParams
665
+ } = {
666
+ __proto__ : null ,
667
+ ...options
668
+ } as BatchPackageStreamOptions
669
+
640
670
type GeneratorStep = {
641
671
generator : AsyncGenerator < BatchPackageFetchResultType >
642
672
iteratorResult : IteratorResult < BatchPackageFetchResultType >
@@ -646,10 +676,6 @@ export class SocketSdk {
646
676
promise : Promise < GeneratorStep >
647
677
}
648
678
649
- const { chunkSize = 100 , concurrencyLimit = 10 } = {
650
- __proto__ : null ,
651
- ...options
652
- } as BatchPackageStreamOptions
653
679
// The createBatchPurlGenerator method will add 2 'abort' event listeners to
654
680
// abortSignal so we multiply the concurrencyLimit by 2.
655
681
const neededMaxListeners = concurrencyLimit * 2
@@ -669,10 +695,13 @@ export class SocketSdk {
669
695
// No more work to do.
670
696
return
671
697
}
672
- const generator = this . #createBatchPurlGenerator( queryParams , {
673
- // Chunk components.
674
- components : components . slice ( index , index + chunkSize )
675
- } )
698
+ const generator = this . #createBatchPurlGenerator(
699
+ {
700
+ // Chunk components.
701
+ components : components . slice ( index , index + chunkSize )
702
+ } ,
703
+ queryParams
704
+ )
676
705
continueGen ( generator )
677
706
index += chunkSize
678
707
}
@@ -728,10 +757,19 @@ export class SocketSdk {
728
757
}
729
758
730
759
async createDependenciesSnapshot (
731
- queryParams : Record < string , string > ,
732
760
filepaths : string [ ] ,
733
- pathsRelativeTo = '.'
761
+ pathsRelativeTo = '.' ,
762
+ queryParams ?: QueryParams | undefined
734
763
) : Promise < SocketSdkResult < 'createDependenciesSnapshot' > > {
764
+ // Support previous argument signature.
765
+ if ( isObjectObject ( filepaths ) ) {
766
+ const oldParam1 = filepaths
767
+ const oldParam2 = pathsRelativeTo
768
+ const oldParam3 = queryParams
769
+ queryParams = oldParam1 as typeof oldParam3
770
+ filepaths = oldParam2 as unknown as typeof oldParam1
771
+ pathsRelativeTo = oldParam3 as unknown as typeof oldParam2
772
+ }
735
773
const basePath = resolveBasePath ( pathsRelativeTo )
736
774
const absFilepaths = resolveAbsPaths ( filepaths , basePath )
737
775
try {
@@ -751,10 +789,19 @@ export class SocketSdk {
751
789
752
790
async createOrgFullScan (
753
791
orgSlug : string ,
754
- queryParams : Record < string , string > | null | undefined ,
755
792
filepaths : string [ ] ,
756
- pathsRelativeTo : string = '.'
793
+ pathsRelativeTo : string = '.' ,
794
+ queryParams ?: QueryParams | undefined
757
795
) : Promise < SocketSdkResult < 'CreateOrgFullScan' > > {
796
+ // Support previous argument signature.
797
+ if ( isObjectObject ( filepaths ) ) {
798
+ const oldParam2 = filepaths
799
+ const oldParam3 = pathsRelativeTo
800
+ const oldParam4 = queryParams
801
+ queryParams = oldParam2 as typeof oldParam4
802
+ filepaths = oldParam3 as unknown as typeof oldParam2
803
+ pathsRelativeTo = oldParam4 as unknown as typeof oldParam3
804
+ }
758
805
const basePath = resolveBasePath ( pathsRelativeTo )
759
806
const absFilepaths = resolveAbsPaths ( filepaths , basePath )
760
807
try {
@@ -774,7 +821,7 @@ export class SocketSdk {
774
821
775
822
async createOrgRepo (
776
823
orgSlug : string ,
777
- queryParams : Record < string , string >
824
+ queryParams ?: QueryParams | undefined
778
825
) : Promise < SocketSdkResult < 'createOrgRepo' > > {
779
826
try {
780
827
const data = await getResponseJson (
@@ -857,7 +904,7 @@ export class SocketSdk {
857
904
858
905
async getAuditLogEvents (
859
906
orgSlug : string ,
860
- queryParams ?: Record < string , string > | null | undefined
907
+ queryParams ?: QueryParams | undefined
861
908
) : Promise < SocketSdkResult < 'getAuditLogEvents' > > {
862
909
try {
863
910
const data = await getResponseJson (
@@ -948,7 +995,7 @@ export class SocketSdk {
948
995
949
996
async getOrgFullScanList (
950
997
orgSlug : string ,
951
- queryParams ?: Record < string , string > | null | undefined
998
+ queryParams ?: QueryParams | undefined
952
999
) : Promise < SocketSdkResult < 'getOrgFullScanList' > > {
953
1000
try {
954
1001
const data = await getResponseJson (
@@ -1022,7 +1069,7 @@ export class SocketSdk {
1022
1069
1023
1070
async getOrgRepoList (
1024
1071
orgSlug : string ,
1025
- queryParams ?: Record < string , string > | null | undefined
1072
+ queryParams ?: QueryParams | undefined
1026
1073
) : Promise < SocketSdkResult < 'getOrgRepoList' > > {
1027
1074
try {
1028
1075
const data = await getResponseJson (
@@ -1164,7 +1211,7 @@ export class SocketSdk {
1164
1211
}
1165
1212
1166
1213
async searchDependencies (
1167
- queryParams : Record < string , number >
1214
+ queryParams ?: QueryParams | undefined
1168
1215
) : Promise < SocketSdkResult < 'searchDependencies' > > {
1169
1216
try {
1170
1217
const data = await getResponseJson (
@@ -1184,7 +1231,7 @@ export class SocketSdk {
1184
1231
async updateOrgRepo (
1185
1232
orgSlug : string ,
1186
1233
repoSlug : string ,
1187
- queryParams : Record < string , string >
1234
+ queryParams ?: QueryParams | undefined
1188
1235
) : Promise < SocketSdkResult < 'updateOrgRepo' > > {
1189
1236
try {
1190
1237
const data = await getResponseJson (
0 commit comments