2019-02-14 13:29:21 +08:00
const SteamCommunity = require ( '../index.js' ) ;
2016-03-05 07:26:47 +08:00
2016-03-05 08:35:04 +08:00
SteamCommunity . prototype . httpRequest = function ( uri , options , callback , source ) {
2019-09-24 16:43:51 +08:00
if ( typeof uri == 'object' ) {
2016-03-05 08:35:04 +08:00
source = callback ;
2016-03-05 07:26:47 +08:00
callback = options ;
options = uri ;
uri = options . url || options . uri ;
2019-09-24 16:43:51 +08:00
} else if ( typeof options == 'function' ) {
2016-03-05 12:50:49 +08:00
source = callback ;
callback = options ;
options = { } ;
2016-03-05 07:26:47 +08:00
}
options . url = options . uri = uri ;
if ( this . _httpRequestConvenienceMethod ) {
options . method = this . _httpRequestConvenienceMethod ;
delete this . _httpRequestConvenienceMethod ;
}
2019-09-24 16:43:51 +08:00
let requestID = ++ this . _httpRequestID ;
source = source || '' ;
2016-03-05 08:35:04 +08:00
2019-09-24 16:43:51 +08:00
let continued = false ;
2016-03-05 13:14:36 +08:00
2019-09-24 16:43:51 +08:00
let continueRequest = ( err ) => {
2016-03-05 13:14:36 +08:00
if ( continued ) {
return ;
}
2016-03-05 07:26:47 +08:00
2016-03-05 13:14:36 +08:00
continued = true ;
if ( err ) {
2016-03-08 14:37:20 +08:00
if ( callback ) {
callback ( err ) ;
}
2016-03-05 13:14:36 +08:00
return ;
2016-03-05 07:26:47 +08:00
}
2019-09-24 16:43:51 +08:00
this . request ( options , ( err , response , body ) => {
let hasCallback = ! ! callback ;
let httpError = options . checkHttpError !== false && this . _checkHttpError ( err , response , callback , body ) ;
let communityError = ! options . json && options . checkCommunityError !== false && this . _checkCommunityError ( body , httpError ? noop : callback ) ; // don't fire the callback if hasHttpError did it already
let tradeError = ! options . json && options . checkTradeError !== false && this . _checkTradeError ( body , httpError || communityError ? noop : callback ) ; // don't fire the callback if either of the previous already did
2021-07-22 16:22:29 +08:00
let jsonError = options . json && options . checkJsonError !== false && ! body ? new Error ( 'Malformed JSON response' ) : null ;
2016-03-05 13:14:36 +08:00
2019-09-24 16:43:51 +08:00
this . emit ( 'postHttpRequest' , requestID , source , options , httpError || communityError || tradeError || jsonError || null , response , body , {
2021-07-22 16:22:29 +08:00
hasCallback ,
httpError ,
communityError ,
tradeError ,
jsonError
2016-03-05 13:14:36 +08:00
} ) ;
2016-08-11 06:55:23 +08:00
if ( hasCallback && ! ( httpError || communityError || tradeError ) ) {
2016-08-10 03:45:21 +08:00
if ( jsonError ) {
2019-09-24 16:43:51 +08:00
callback . call ( this , jsonError , response ) ;
2016-08-11 06:55:23 +08:00
} else {
2019-09-24 16:43:51 +08:00
callback . apply ( this , arguments ) ;
2016-08-10 03:45:21 +08:00
}
2016-03-05 13:14:36 +08:00
}
} ) ;
2019-09-24 16:43:51 +08:00
} ;
if ( ! this . onPreHttpRequest || ! this . onPreHttpRequest ( requestID , source , options , continueRequest ) ) {
// No pre-hook, or the pre-hook doesn't want to delay the request.
continueRequest ( null ) ;
2016-03-05 13:14:36 +08:00
}
2016-03-05 07:26:47 +08:00
} ;
SteamCommunity . prototype . httpRequestGet = function ( ) {
2021-07-22 16:22:29 +08:00
this . _httpRequestConvenienceMethod = 'GET' ;
2016-03-05 07:26:47 +08:00
return this . httpRequest . apply ( this , arguments ) ;
} ;
SteamCommunity . prototype . httpRequestPost = function ( ) {
2021-07-22 16:22:29 +08:00
this . _httpRequestConvenienceMethod = 'POST' ;
2016-03-05 07:26:47 +08:00
return this . httpRequest . apply ( this , arguments ) ;
} ;
2016-03-07 14:32:02 +08:00
SteamCommunity . prototype . _notifySessionExpired = function ( err ) {
this . emit ( 'sessionExpired' , err ) ;
} ;
2016-12-13 13:45:38 +08:00
SteamCommunity . prototype . _checkHttpError = function ( err , response , callback , body ) {
2016-06-30 11:08:40 +08:00
if ( err ) {
2016-12-13 13:45:38 +08:00
callback ( err , response , body ) ;
2016-03-05 07:26:47 +08:00
return err ;
}
2016-06-30 11:08:40 +08:00
if ( response . statusCode >= 300 && response . statusCode <= 399 && response . headers . location . indexOf ( '/login' ) != - 1 ) {
2021-07-22 16:22:29 +08:00
err = new Error ( 'Not Logged In' ) ;
2016-12-13 13:45:38 +08:00
callback ( err , response , body ) ;
2016-03-07 14:32:02 +08:00
this . _notifySessionExpired ( err ) ;
2016-03-05 07:26:47 +08:00
return err ;
}
2019-09-24 16:43:51 +08:00
if ( response . statusCode == 403 && typeof response . body == 'string' && response . body . match ( /<div id="parental_notice_instructions">Enter your PIN below to exit Family View.<\/div>/ ) ) {
2021-07-22 16:22:29 +08:00
err = new Error ( 'Family View Restricted' ) ;
2016-12-13 13:45:38 +08:00
callback ( err , response , body ) ;
2016-06-30 11:08:40 +08:00
return err ;
}
if ( response . statusCode >= 400 ) {
2021-07-22 16:22:29 +08:00
err = new Error ( ` HTTP error ${ response . statusCode } ` ) ;
2016-03-05 07:26:47 +08:00
err . code = response . statusCode ;
2016-12-13 13:45:38 +08:00
callback ( err , response , body ) ;
2016-03-05 07:26:47 +08:00
return err ;
}
return false ;
} ;
SteamCommunity . prototype . _checkCommunityError = function ( html , callback ) {
2019-09-24 16:43:51 +08:00
let err ;
2016-03-06 07:58:39 +08:00
2019-09-24 16:43:51 +08:00
if ( typeof html == 'string' && html . match ( /<h1>Sorry!<\/h1>/ ) ) {
let match = html . match ( /<h3>(.+)<\/h3>/ ) ;
2021-07-22 16:22:29 +08:00
err = new Error ( match ? match [ 1 ] : 'Unknown error occurred' ) ;
2016-03-06 07:58:39 +08:00
callback ( err ) ;
return err ;
}
2019-09-24 16:43:51 +08:00
if ( typeof html == 'string' && html . match ( /g_steamID = false;/ ) && html . match ( /<h1>Sign In<\/h1>/ ) ) {
2021-07-22 16:22:29 +08:00
err = new Error ( 'Not Logged In' ) ;
2016-03-05 07:26:47 +08:00
callback ( err ) ;
2016-03-07 14:32:02 +08:00
this . _notifySessionExpired ( err ) ;
2016-03-05 07:26:47 +08:00
return err ;
}
return false ;
} ;
SteamCommunity . prototype . _checkTradeError = function ( html , callback ) {
2016-03-15 09:39:45 +08:00
if ( typeof html !== 'string' ) {
2016-03-05 13:40:45 +08:00
return false ;
}
2019-09-24 16:43:51 +08:00
let match = html . match ( /<div id="error_msg">\s*([^<]+)\s*<\/div>/ ) ;
2016-03-05 07:26:47 +08:00
if ( match ) {
2019-09-24 16:43:51 +08:00
let err = new Error ( match [ 1 ] . trim ( ) ) ;
2016-06-04 08:10:33 +08:00
callback ( err ) ;
2016-03-05 07:26:47 +08:00
return err ;
}
return false ;
} ;
2019-09-24 16:43:51 +08:00
function noop ( ) { }