Skip to content

Commit aa82de0

Browse files
authoredJun 8, 2022
feat: Allow web-login donecheck to cancel opener promise (#50)
1 parent 6bdd233 commit aa82de0

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed
 

‎lib/index.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const fetch = require('npm-registry-fetch')
44
const { HttpErrorBase } = require('npm-registry-fetch/lib/errors')
5+
const EventEmitter = require('events')
56
const os = require('os')
67
const { URL } = require('url')
78
const log = require('proc-log')
@@ -73,8 +74,23 @@ const webAuth = (opener, opts, body) => {
7374
return content
7475
}).then(({ doneUrl, loginUrl }) => {
7576
log.verbose('web auth', 'opening url pair')
76-
return opener(loginUrl).then(
77-
() => webAuthCheckLogin(doneUrl, { ...opts, cache: false })
77+
78+
const doneEmitter = new EventEmitter()
79+
80+
const openPromise = opener(loginUrl, doneEmitter)
81+
const webAuthCheckPromise = webAuthCheckLogin(doneUrl, { ...opts, cache: false })
82+
.then(authResult => {
83+
log.verbose('web auth', 'done-check finished')
84+
85+
// cancel open prompt if it's present
86+
doneEmitter.emit('abort')
87+
88+
return authResult
89+
})
90+
91+
return Promise.all([openPromise, webAuthCheckPromise]).then(
92+
// pick the auth result and pass it along
93+
([, authResult]) => authResult
7894
)
7995
}).catch(er => {
8096
if ((er.statusCode >= 400 && er.statusCode <= 499) || er.statusCode === 500) {

0 commit comments

Comments
 (0)
Please sign in to comment.