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

feat: add createServer option #38

Merged
merged 4 commits into from
Apr 24, 2023
Merged

feat: add createServer option #38

merged 4 commits into from
Apr 24, 2023

Conversation

yusukebe
Copy link
Member

This PR introduces the createServer option for both serve and createAdaptorServer. With this option, you can use the createServer function imported from node:https. This feature enables SSL support.

import { createServer } from 'node:https'
import fs from 'node:fs'

//...

serve({
  fetch: app.fetch,
  createServer: createServer,
  serverOptions: {
    key: fs.readFileSync('test/fixtures/keys/agent1-key.pem'),
    cert: fs.readFileSync('test/fixtures/keys/agent1-cert.pem'),
  },
})

Resolve #37

@yusukebe
Copy link
Member Author

Hi @tangye1234 @schonert !

I believe this is a good feature. You don't need to go into detail, but could you please review it?

src/server.ts Outdated Show resolved Hide resolved
src/types.ts Outdated Show resolved Hide resolved
serve({
fetch: app.fetch,
createServer: createServer,
serverOptions: {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest leaving the serverOptions out by simply using the createServer as a function. Not seen the serverOption documented or a similar pattern used by other adaptors. Could potentially be a breaking change tho.

serve({
  fetch: app.fetch,
  createServer: (options = {}) => {
    return createServer({ 
        ...options, 
        key: fs.readFileSync('test/fixtures/keys/agent1-key.pem'),
        cert: fs.readFileSync('test/fixtures/keys/agent1-cert.pem'),
      })
    },
})

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay! I think you are right.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, if we don't use serverOptions, we have to pass requestListener to createServer as an argument:

  const server = createAdaptorServer({
    fetch: app.fetch,
    createServer: (options = {}, requestListener: RequestListener) => {
      return createServerHTTPS(
        {
          ...options,
          key: fs.readFileSync('test/fixtures/keys/agent1-key.pem'),
          cert: fs.readFileSync('test/fixtures/keys/agent1-cert.pem'),
        },
        requestListener // <---
      )
    },
  })

It's a little bit annoying, so, keep using serverOptions.

@schonert
Copy link

Brilliant! I only had one comment :)

Copy link
Contributor

@tangye1234 tangye1234 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This functionality can indeed resolve the HTTPS server problem, and the API suits me well. Looking ahead, it may require additional instrumentation like OpenTelemetry, so exposing the server creation option is an effective means of facilitating integration.

@yusukebe
Copy link
Member Author

@schonert @tangye1234

Thanks for reviewing. Let's go with it!

@yusukebe yusukebe merged commit be1abea into main Apr 24, 2023
@yusukebe yusukebe deleted the feat/create-server branch April 24, 2023 13:14
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

Successfully merging this pull request may close these issues.

SSL support
4 participants