ericvicenti / ssh-keygen

Generates SSH key-pairs in node.js
MIT License
67 stars 49 forks source link

Support for Promises, (async/await) #21

Open misogihagi opened 4 years ago

misogihagi commented 4 years ago

keygen function is normal function not Promise or async function. I confused for a while and realized it may want some async methods.

code

keygen({
  location: location,
  comment: comment,
  password: password,
  read: true,
  format: format
}, function(err, out){
    console.log('111')
    fs.writeFile('aaa',out.key)
});
console.log('222')

result

222
111
DeanKamali commented 3 years ago

If you are trying to use async / await, you could use pify package to Promisify a callback-style function, here is an example

const pify = require('pify');

(async () => {
    let sshkeys = await pify(keygen)({
            location: location,
            comment: comment,
            password: password,
            read: true,
            format: format
        });
   console.log('sshkeys', sshkeys)
})();
micalevisk commented 2 years ago

You can use a promise-based API in my fork as well as the callback-based one

const keygen = require('@micalevisk/ssh-keygen')
async function runPromiseVersion() {
  console.log('Generating key pair')

  try {
    const out = await keygen({
      comment: 'john@doe.com',
      read: true,
    }); // just supply the second parameter here (as usual) if you don't want to return a Promise

    console.log('Done generating key pairs');
    console.log(out.key);
    console.log(out.pubKey);
  } catch (err) {
    console.log('There was a problem:', err);
  }
}

it doesn't have any dependencies.