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

https config cannot be asynchronous #319

Open
juliendargelos opened this issue Jul 2, 2019 · 0 comments
Open

https config cannot be asynchronous #319

juliendargelos opened this issue Jul 2, 2019 · 0 comments

Comments

@juliendargelos
Copy link

The https configuration requires a synchronous export from a module. But in some cases it would be useful to export a promise:

Using fs asynchronous functions

With fs callback functions (kind of ugly):

const fs = require('fs')

module.exports = Promise.all([
  new Promise((resolve, reject) => {
    fs.readFile(__dirname + '/server.cert', (error, cert) => error ? reject(error) : resolve(cert))
  }),
  new Promise((resolve, reject) => {
    fs.readFile(__dirname + '/server.key', (error, key) => error ? reject(error) : resolve(key))
  })
]).then(([cert, key]) => ({ cert, key, passphrase: '12345' }))

With fs experimental promises or fs-extra:

const fs = require('fs').promises // const fs = require('fs-extra')

module.exports = Promise.all([
  fs.readFile(__dirname + '/server.cert'),
  fs.readFile(__dirname + '/server.key')
]).then(([cert, key]) => ({ cert, key, passphrase: '12345' }))

Using devcert:

const devcert = require('devcert')

module.exports = (async () => ({
  ...(await devcert.certificateFor('localhost')),
  passphrase: '12345'
})()

Can be one-lined when passphrase is not needed:

module.exports = require('devcert').certificateFor('localhost')

Personally, I would prefer being able to pass an object to the https option, so asynchronous stuff can be handled by the user, outside live-server:

const liveServer = require('live-server')
const devcert = require('devcert')

;(async () => liveServer.start({
  // ...
  https: await devcert.certificateFor('localhost')
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant