11import * as crypto from 'crypto' ;
2- import * as http from 'http' ;
3- import * as url from 'url' ;
42import { promisify } from 'util' ;
53
64import type { Binary , BSONSerializeOptions } from '../../bson' ;
@@ -12,7 +10,7 @@ import {
1210 MongoMissingCredentialsError ,
1311 MongoRuntimeError
1412} from '../../error' ;
15- import { ByteUtils , maxWireVersion , ns } from '../../utils' ;
13+ import { ByteUtils , maxWireVersion , ns , request } from '../../utils' ;
1614import { type AuthContext , AuthProvider } from './auth_provider' ;
1715import { MongoCredentials } from './mongo_credentials' ;
1816import { AuthMechanism } from './providers' ;
@@ -253,61 +251,3 @@ function deriveRegion(host: string) {
253251
254252 return parts [ 1 ] ;
255253}
256-
257- interface RequestOptions {
258- json ?: boolean ;
259- method ?: string ;
260- timeout ?: number ;
261- headers ?: http . OutgoingHttpHeaders ;
262- }
263-
264- async function request ( uri : string ) : Promise < Record < string , any > > ;
265- async function request (
266- uri : string ,
267- options ?: { json ?: true } & RequestOptions
268- ) : Promise < Record < string , any > > ;
269- async function request ( uri : string , options ?: { json : false } & RequestOptions ) : Promise < string > ;
270- async function request (
271- uri : string ,
272- options : RequestOptions = { }
273- ) : Promise < string | Record < string , any > > {
274- return new Promise < string | Record < string , any > > ( ( resolve , reject ) => {
275- const requestOptions = {
276- method : 'GET' ,
277- timeout : 10000 ,
278- json : true ,
279- ...url . parse ( uri ) ,
280- ...options
281- } ;
282-
283- const req = http . request ( requestOptions , res => {
284- res . setEncoding ( 'utf8' ) ;
285-
286- let data = '' ;
287- res . on ( 'data' , d => {
288- data += d ;
289- } ) ;
290-
291- res . once ( 'end' , ( ) => {
292- if ( options . json === false ) {
293- resolve ( data ) ;
294- return ;
295- }
296-
297- try {
298- const parsed = JSON . parse ( data ) ;
299- resolve ( parsed ) ;
300- } catch {
301- // TODO(NODE-3483)
302- reject ( new MongoRuntimeError ( `Invalid JSON response: "${ data } "` ) ) ;
303- }
304- } ) ;
305- } ) ;
306-
307- req . once ( 'timeout' , ( ) =>
308- req . destroy ( new MongoAWSError ( `AWS request to ${ uri } timed out after ${ options . timeout } ms` ) )
309- ) ;
310- req . once ( 'error' , error => reject ( error ) ) ;
311- req . end ( ) ;
312- } ) ;
313- }
0 commit comments