@@ -18,7 +18,6 @@ import {deepCopy} from './deep-copy';
1818import { FirebaseApp } from '../firebase-app' ;
1919import { AppErrorCodes , FirebaseAppError } from './error' ;
2020import * as validator from './validator' ;
21- import { OutgoingHttpHeaders } from 'http' ;
2221
2322import http = require( 'http' ) ;
2423import https = require( 'https' ) ;
@@ -342,215 +341,6 @@ export class AuthorizedHttpClient extends HttpClient {
342341 }
343342}
344343
345- /**
346- * Base class for handling HTTP requests.
347- */
348- export class HttpRequestHandler {
349- /**
350- * Sends HTTP requests and returns a promise that resolves with the result.
351- * Will retry once if the first attempt encounters an AppErrorCodes.NETWORK_ERROR.
352- *
353- * @param {string } host The HTTP host.
354- * @param {number } port The port number.
355- * @param {string } path The endpoint path.
356- * @param {HttpMethod } httpMethod The http method.
357- * @param {object } [data] The request JSON.
358- * @param {object } [headers] The request headers.
359- * @param {number } [timeout] The request timeout in milliseconds.
360- * @return {Promise<object> } A promise that resolves with the response.
361- */
362- public sendRequest (
363- host : string ,
364- port : number ,
365- path : string ,
366- httpMethod : HttpMethod ,
367- data ?: object ,
368- headers ?: object ,
369- timeout ?: number ) : Promise < object > {
370- // Convenience for calling the real _sendRequest() method with the original params.
371- const sendOneRequest = ( ) => {
372- return this . _sendRequest ( host , port , path , httpMethod , data , headers , timeout ) ;
373- } ;
374-
375- return sendOneRequest ( )
376- . catch ( ( response : { statusCode : number , error : string | object } ) => {
377- // Retry if the request failed due to a network error.
378- if ( response . error instanceof FirebaseAppError ) {
379- if ( ( response . error as FirebaseAppError ) . hasCode ( AppErrorCodes . NETWORK_ERROR ) ) {
380- return sendOneRequest ( ) ;
381- }
382- }
383- return Promise . reject ( response ) ;
384- } ) ;
385- }
386-
387- /**
388- * Sends HTTP requests and returns a promise that resolves with the result.
389- *
390- * @param {string } host The HTTP host.
391- * @param {number } port The port number.
392- * @param {string } path The endpoint path.
393- * @param {HttpMethod } httpMethod The http method.
394- * @param {object } [data] The request JSON.
395- * @param {object } [headers] The request headers.
396- * @param {number } [timeout] The request timeout in milliseconds.
397- * @return {Promise<object> } A promise that resolves with the response.
398- */
399- private _sendRequest (
400- host : string ,
401- port : number ,
402- path : string ,
403- httpMethod : HttpMethod ,
404- data ?: object ,
405- headers ?: object ,
406- timeout ?: number ) : Promise < any > {
407- let requestData ;
408- if ( data ) {
409- try {
410- requestData = JSON . stringify ( data ) ;
411- } catch ( e ) {
412- return Promise . reject ( e ) ;
413- }
414- }
415- const options : https . RequestOptions = {
416- method : httpMethod ,
417- host,
418- port,
419- path,
420- headers : headers as OutgoingHttpHeaders ,
421- } ;
422- // Only https endpoints.
423- return new Promise ( ( resolve , reject ) => {
424- const req = https . request ( options , ( res ) => {
425- const buffers : Buffer [ ] = [ ] ;
426- res . on ( 'data' , ( buffer : Buffer ) => buffers . push ( buffer ) ) ;
427- res . on ( 'end' , ( ) => {
428- const response = Buffer . concat ( buffers ) . toString ( ) ;
429-
430- const statusCode = res . statusCode || 200 ;
431-
432- const responseHeaders = res . headers || { } ;
433- const contentType = responseHeaders [ 'content-type' ] || 'application/json' ;
434-
435- if ( contentType . indexOf ( 'text/html' ) !== - 1 || contentType . indexOf ( 'text/plain' ) !== - 1 ) {
436- // Text response
437- if ( statusCode >= 200 && statusCode < 300 ) {
438- resolve ( response ) ;
439- } else {
440- reject ( {
441- statusCode,
442- error : response ,
443- } ) ;
444- }
445- } else {
446- // JSON response
447- try {
448- const json = JSON . parse ( response ) ;
449- if ( statusCode >= 200 && statusCode < 300 ) {
450- resolve ( json ) ;
451- } else {
452- reject ( {
453- statusCode,
454- error : json ,
455- } ) ;
456- }
457- } catch ( error ) {
458- const parsingError = new FirebaseAppError (
459- AppErrorCodes . UNABLE_TO_PARSE_RESPONSE ,
460- `Failed to parse response data: "${ error . toString ( ) } ". Raw server` +
461- `response: "${ response } ". Status code: "${ res . statusCode } ". Outgoing ` +
462- `request: "${ options . method } ${ options . host } ${ options . path } "` ,
463- ) ;
464- reject ( {
465- statusCode,
466- error : parsingError ,
467- } ) ;
468- }
469- }
470- } ) ;
471- } ) ;
472-
473- if ( timeout ) {
474- // Listen to timeouts and throw a network error.
475- req . on ( 'socket' , ( socket ) => {
476- socket . setTimeout ( timeout ) ;
477- socket . on ( 'timeout' , ( ) => {
478- req . abort ( ) ;
479-
480- const networkTimeoutError = new FirebaseAppError (
481- AppErrorCodes . NETWORK_TIMEOUT ,
482- `${ host } network timeout. Please try again.` ,
483- ) ;
484- reject ( {
485- statusCode : 408 ,
486- error : networkTimeoutError ,
487- } ) ;
488- } ) ;
489- } ) ;
490- }
491-
492- req . on ( 'error' , ( error ) => {
493- const networkRequestError = new FirebaseAppError (
494- AppErrorCodes . NETWORK_ERROR ,
495- `A network request error has occurred: ${ error && error . message } ` ,
496- ) ;
497- reject ( {
498- statusCode : 502 ,
499- error : networkRequestError ,
500- } ) ;
501- } ) ;
502-
503- if ( requestData ) {
504- req . write ( requestData ) ;
505- }
506-
507- req . end ( ) ;
508- } ) ;
509- }
510- }
511-
512- /**
513- * Class that extends HttpRequestHandler and signs HTTP requests with a service
514- * credential access token.
515- *
516- * @param {Credential } credential The service account credential used to
517- * sign HTTP requests.
518- * @constructor
519- */
520- export class SignedApiRequestHandler extends HttpRequestHandler {
521- constructor ( private app_ : FirebaseApp ) {
522- super ( ) ;
523- }
524-
525- /**
526- * Sends HTTP requests and returns a promise that resolves with the result.
527- *
528- * @param {string } host The HTTP host.
529- * @param {number } port The port number.
530- * @param {string } path The endpoint path.
531- * @param {HttpMethod } httpMethod The http method.
532- * @param {object } data The request JSON.
533- * @param {object } headers The request headers.
534- * @param {number } timeout The request timeout in milliseconds.
535- * @return {Promise } A promise that resolves with the response.
536- */
537- public sendRequest (
538- host : string ,
539- port : number ,
540- path : string ,
541- httpMethod : HttpMethod ,
542- data ?: object ,
543- headers ?: object ,
544- timeout ?: number ) : Promise < object > {
545- return this . app_ . INTERNAL . getToken ( ) . then ( ( accessTokenObj ) => {
546- const headersCopy : object = ( headers && deepCopy ( headers ) ) || { } ;
547- const authorizationHeaderKey = 'Authorization' ;
548- headersCopy [ authorizationHeaderKey ] = 'Bearer ' + accessTokenObj . accessToken ;
549- return super . sendRequest ( host , port , path , httpMethod , data , headersCopy , timeout ) ;
550- } ) ;
551- }
552- }
553-
554344/**
555345 * Class that defines all the settings for the backend API endpoint.
556346 *
0 commit comments