Skip to content

Commit

Permalink
convert @ inside => functions
Browse files Browse the repository at this point in the history
  • Loading branch information
averissimo committed Jul 9, 2020
1 parent ed62319 commit 0656833
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 105 deletions.
24 changes: 14 additions & 10 deletions src/auth.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,16 @@ module.exports = class Auth
# first checks the database if we already have cookies, or else proceeds with login
getAuth: =>
log.debug 'getting auth...'
self = @
Q().then =>
Q.Promise (rs, rj) => @jar.getCookies OAUTH2_LOGIN_URL, plug(rs, rj)
Q.Promise (rs, rj) => self.jar.getCookies OAUTH2_LOGIN_URL, plug(rs, rj)
.then (cookies) =>
if cookies.length
log.debug 'using cached cookies'
Q()
else
log.debug 'proceeding to login'
@login()
self.login()
.then ->
# result
log.debug 'getAuth done'
Expand All @@ -65,14 +66,15 @@ module.exports = class Auth
Q.reject err

login: ->
self = @
Q().then =>
# fetch creds to inspect what we got to work with
@creds()
self.creds()
.then (creds) =>
if creds.auth
@oauthLogin(creds)
self.oauthLogin(creds)
else if creds.cookies
@providedCookies(creds)
self.providedCookies(creds)
else
throw new Error("No acceptable creds provided")

Expand All @@ -85,21 +87,22 @@ module.exports = class Auth


oauthLogin: ({auth}) =>
self = @
Q().then =>
# load the refresh-token from disk, and if found
# use to get an authentication token.
@loadRefreshToken().then (rtoken) =>
@authWithRefreshToken(rtoken) if rtoken
self.loadRefreshToken().then (rtoken) =>
self.authWithRefreshToken(rtoken) if rtoken
.then (atoken) =>
if atoken
# token from refresh-token. just use it.
atoken
else
# no loaded refresh-token. request auth code.
@requestAuthCode auth
self.requestAuthCode auth
.then (atoken) =>
# one way or another we have an atoken now
@getSessionCookies atoken
self.getSessionCookies atoken


loadRefreshToken: =>
Expand Down Expand Up @@ -151,6 +154,7 @@ module.exports = class Auth

requestAuthCode: (auth) =>
log.debug 'request auth code from user'
self = @
Q().then ->
auth()
.then (code) ->
Expand All @@ -170,7 +174,7 @@ module.exports = class Auth
log.debug 'auth with code success'
body = JSON.parse(res.body)
# save it and then return the access token
@saveRefreshToken(body.refresh_token).then ->
self.saveRefreshToken(body.refresh_token).then ->
body.access_token
else

Expand Down
132 changes: 69 additions & 63 deletions src/channel.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,12 @@ module.exports = class Channel
fetchSid: =>
auth = @authHeaders()
return Q.reject new Error("No auth headers") unless auth
self = @
Q().then =>
opts =
method: 'POST'
uri: op 'channel/bind'
jar: request.jar @jarstore
jar: request.jar self.jarstore
qs:
VER: 8
RID: 81187
Expand Down Expand Up @@ -139,10 +140,11 @@ module.exports = class Channel
@sid = null # ensures we get a new sid
@gsid = null
@subscribed = false
self = @
run = =>
# graceful stop of polling
return unless @running
@poll(retries).then ->
return unless self.running
self.poll(retries).then ->
# XXX we only reset to MAX_RETRIES after a full ended
# poll. this means in bad network conditions we get an
# edge case where retries never reset despite getting
Expand All @@ -158,11 +160,11 @@ module.exports = class Channel
if retries > 0
run()
else
@running = false
self.running = false
# resetting with error makes pushParser.allLines()
# resolve with that error, which in turn makes
# @getLines() propagate the error out.
@pushParser.reset(err)
# self.getLines() propagate the error out.
self.pushParser.reset(err)
run()
return null

Expand All @@ -179,72 +181,76 @@ module.exports = class Channel


poll: (retries) =>
self = @
Q().then ->
backoffTime = 2 * (MAX_RETRIES - retries) * 1000
log.debug 'backing off for', backoffTime, 'ms' if backoffTime
wait backoffTime
.then =>
Q.reject ABORT unless @running
Q.reject ABORT unless self.running
.then =>
unless @sid
@fetchSid().then (o) =>
unless self.sid
self.fetchSid().then (o) =>
merge this, o # set on this
@pushParser.reset() # ensure no half data
self.pushParser.reset() # ensure no half data
.then =>
@reqpoll()
self.reqpoll()


# long polling
reqpoll: => Q.Promise (rs, rj) =>
log.debug 'long poll req'
opts =
method: 'GET'
uri: op 'channel/bind'
jar: request.jar @jarstore
qs:
VER: 8
gsessionid: @gsid
RID: 'rpc'
t: 1
SID: @sid
CI: 0
ctype: 'hangouts'
TYPE: 'xmlhttp'
headers: @authHeaders()
encoding: null # get body as buffer
timeout: 30000 # 30 seconds timeout in connect attempt
withCredentials: true
ok = false
@currentReq = request(opts).on 'response', (res) =>
log.debug 'long poll response', res.statusCode, res.statusMessage
if res.statusCode == 200
return ok = true
else if isUnknownSID(res)
ok = false
log.debug 'sid became invalid'
@sid = null
@gsid = null
@subscribed = false
rj NetworkError.forRes(res)
.on 'data', (chunk) =>
if ok
# log.debug 'long poll chunk\n' + require('hexy').hexy(chunk)
@pushParser.parse chunk
# subscribe on first data received
@subscribe() unless @subscribed
.on 'error', (err) =>
log.debug 'long poll error', err
rj err
.on 'end', ->
log.debug 'long poll end'
rs()
reqpoll: =>
self = @
Q.Promise (rs, rj) =>
log.debug 'long poll req'
opts =
method: 'GET'
uri: op 'channel/bind'
jar: request.jar self.jarstore
qs:
VER: 8
gsessionid: self.gsid
RID: 'rpc'
t: 1
SID: self.sid
CI: 0
ctype: 'hangouts'
TYPE: 'xmlhttp'
headers: self.authHeaders()
encoding: null # get body as buffer
timeout: 30000 # 30 seconds timeout in connect attempt
withCredentials: true
ok = false
self.currentReq = request(opts).on 'response', (res) =>
log.debug 'long poll response', res.statusCode, res.statusMessage
if res.statusCode == 200
return ok = true
else if isUnknownSID(res)
ok = false
log.debug 'sid became invalid'
self.sid = null
self.gsid = null
self.subscribed = false
rj NetworkError.forRes(res)
.on 'data', (chunk) =>
if ok
# log.debug 'long poll chunk\n' + require('hexy').hexy(chunk)
self.pushParser.parse chunk
# subscribe on first data received
self.subscribe() unless self.subscribed
.on 'error', (err) =>
log.debug 'long poll error', err
rj err
.on 'end', ->
log.debug 'long poll end'
rs()


# Subscribes the channel to receive relevant events. Only needs to
# be called when a new channel (SID/gsessionid) is opened.
subscribe: =>
return if @subscribed
@subscribed = true
self = @
Q().then ->
wait(1000) # https://github.com/tdryer/hangups/issues/58
.then =>
Expand All @@ -258,15 +264,15 @@ module.exports = class Channel
opts =
method: 'POST'
uri: op 'channel/bind'
jar: request.jar @jarstore
proxy: @proxy
jar: request.jar self.jarstore
proxy: self.proxy
qs:
VER: 8
RID: 81188
ctype: 'hangouts'
gsessionid: @gsid
SID: @sid
headers: @authHeaders()
gsessionid: self.gsid
SID: self.sid
headers: self.authHeaders()
timeout: 30000 # 30 seconds timeout in connect attempt
form: formMap
withCredentials: true
Expand All @@ -277,11 +283,11 @@ module.exports = class Channel
else if isUnknownSID(res)
ok = false
log.debug 'sid became invalid'
@sid = null
@gsid = null
@subscribed = false
self.sid = null
self.gsid = null
self.subscribed = false
Q.reject NetworkError.forRes(res)
.fail (err) =>
log.info 'subscribe failed', fmterr(err)
@subscribed = false
self.subscribed = false
Q.reject err
Loading

0 comments on commit 0656833

Please sign in to comment.