Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gracefully exit login and publish commands on Ctrl+C (SIGINT) in the new webAuthn flow #5243

Merged
merged 4 commits into from Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/utils/open-url-prompt.js
Expand Up @@ -34,6 +34,11 @@ const promptOpen = async (npm, url, title, prompt, emitter) => {
})

const tryOpen = await new Promise(resolve => {
rl.on('SIGINT', () => {
rl.close()
resolve('SIGINT')
})

rl.question(prompt, () => {
resolve(true)
})
Expand All @@ -50,6 +55,10 @@ const promptOpen = async (npm, url, title, prompt, emitter) => {
}
})

if (tryOpen === 'SIGINT') {
throw new Error('canceled')
}

if (!tryOpen) {
return
}
Expand Down
29 changes: 29 additions & 0 deletions test/lib/utils/open-url-prompt.js
Expand Up @@ -28,6 +28,8 @@ const opener = (url, opts, cb) => {
}

let questionShouldResolve = true
let openUrlPromptInterrupted = false

const readline = {
createInterface: () => ({
question: (_q, cb) => {
Expand All @@ -36,6 +38,11 @@ const readline = {
}
},
close: () => {},
on: (_signal, cb) => {
if (openUrlPromptInterrupted && _signal === 'SIGINT') {
cb()
}
},
}),
}

Expand Down Expand Up @@ -148,3 +155,25 @@ t.test('returns error when opener errors', async t => {
)
t.equal(openerUrl, 'https://www.npmjs.com', 'did not open')
})

t.test('throws "canceled" error on SIGINT', async t => {
t.teardown(() => {
openerUrl = null
openerOpts = null
OUTPUT.length = 0
questionShouldResolve = true
openUrlPromptInterrupted = false
})

questionShouldResolve = false
openUrlPromptInterrupted = true
const emitter = new EventEmitter()

const open = openUrlPrompt(npm, 'https://www.npmjs.com', 'npm home', 'prompt', emitter)

try {
await open
} catch (err) {
t.equal(err.message, 'canceled')
}
})