-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a5cc46
commit e0b854c
Showing
3 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
`use strict` | ||
|
||
const process = require('process') | ||
const readline = require('readline') | ||
const SecureString = require('./secure-string') | ||
|
||
function ask (prompt, callback) { | ||
const secret = new SecureString() | ||
const stdin = process.stdin | ||
const stdout = process.stdout | ||
const rl = readline.createInterface({ | ||
input: stdin, | ||
output: null | ||
}) | ||
|
||
function keypress(s, key) { | ||
if (s === '\r' || s === '\n') { | ||
stdin.removeListener('keypress', keypress) | ||
callback(null, secret) | ||
} else if (s !== undefined) { | ||
secret.appendCodePoint(s.codePointAt(0)) | ||
} | ||
} | ||
|
||
stdin.on('keypress', keypress) | ||
readline.emitKeypressEvents(stdin) | ||
if (stdin.isTTY) | ||
process.stdin.setRawMode(true) | ||
|
||
if (stdout) | ||
stdout.write(prompt) | ||
stdin.resume() | ||
} | ||
|
||
module.exports = ask |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* eslint-env mocha */ | ||
|
||
'use strict' | ||
|
||
const chai = require('chai') | ||
const expect = chai.expect | ||
chai.use(require('dirty-chai')) | ||
const SecureString = require('..') | ||
const Readable = require('stream').Readable; | ||
|
||
describe('ask', () => { | ||
it('static', () => { | ||
const password = new SecureString() | ||
expect(SecureString.ask).to.exist() | ||
}) | ||
|
||
it.skip('returns a secure string', done => { | ||
SecureString.ask('password', (err, answer) => { | ||
expect(err).to.not.exist() | ||
expect(answer).to.exist() | ||
expect(answer).to.be.instanceOf(SecureString) | ||
answer.value(plainText => { | ||
expect(plainText).to.exist(); | ||
expect(plainText).to.be.instanceOf(Buffer) | ||
expect(plainText.toString()).to.equal('') | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) |