Skip to content

Conversation

@humphd
Copy link
Contributor

@humphd humphd commented Sep 24, 2020

Fixes #87. This adds the .socket property to request, mirroring the .connection property

Checklist

Copy link
Member

@Eomm Eomm left a comment

Choose a reason for hiding this comment

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

Could you add a test case?

lib/request.js Outdated
Comment on lines 67 to 70
this.connection = {
remoteAddress: options.remoteAddress || '127.0.0.1'
}
this.socket = this.connection
Copy link
Member

Choose a reason for hiding this comment

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

Should there be some sort of deprecation here? We'd have to follow the upstream deprecation cycle.

Copy link
Member

Choose a reason for hiding this comment

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

I think you can add the deprecate warning in the typing to something like this

interface Request extends stream.Readable {
...
// @deprecated
connection: object
...

Copy link
Member

Choose a reason for hiding this comment

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

I agree with @jsumners.
According to the Node.js docs, .connection has been deprecated in favor of .socket.
I think we should do something like this:

this.socket = {
  remoteAddress: options.remoteAddress || '127.0.0.1'
}

Object.defineProperty(Request, 'connection', {
  get () {
    // if node major ≥13 emit a warning with https://github.com/fastify/fastify-warning
    return this.socket
  }
})

Copy link
Member

Choose a reason for hiding this comment

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

+1!

@humphd
Copy link
Contributor Author

humphd commented Sep 25, 2020

Thanks for all the feedback. I've tried to make the changes/updates that were requested. Let me know if I've done it incorrectly, or there are other things I should consider.

lib/request.js Outdated

Object.defineProperty(this, 'connection', {
get () {
// if node major ≥13 emit a warning with https://github.com/fastify/fastify-warning
Copy link
Member

Choose a reason for hiding this comment

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

The comment here is not sufficient. An actual test against against process.version is needed along with a warning issued through the interface that fastify-warning provides.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Understood. Maybe something like https://github.com/fastify/fastify-plugin/blob/d3b2ca47404e622f7c474493bb0624bf95a2e4f4/test/esm/index.test.js#L6, and then use fastify-warning to actually log that message?

Copy link
Member

Choose a reason for hiding this comment

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

Correct. Fastify itself uses fastify-warning to issue deprecation notices. You can learn from there as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great, thanks @jsumners, this is really helpful. I'll dig around and update this to do it correctly.

@mcollina
Copy link
Member

mcollina commented Oct 1, 2020

Any update on this @humphd ?

@humphd
Copy link
Contributor Author

humphd commented Oct 1, 2020

I've submitted another fix, which uses semver to do the check against the node version, and fastify-warning to emit the deprecation warning. Thanks to @jsumners for pointing out how I should do this.

A couple things I wasn't sure about related to the way I named things in warning.create(), specifically the CODE and if there is a standard you want to use. Also, I noticed that in some places you create all the warnings in a single file. I did it in the request.js file, but could move it.

Also, I ran into a small issue with naming on the code, where I had lowercase letters for the plugin name, but everything gets converted to uppercase. I'll send a PR to the docs for fastify-warning to make that more clear.

lib/request.js Outdated

Object.defineProperty(this, 'connection', {
get () {
if (semver.gte(process.versions.node, '13.0.0')) {
Copy link
Member

Choose a reason for hiding this comment

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

let's just emit the deprecation, I prefer we do not add another dependency here.

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good to me.

@humphd
Copy link
Contributor Author

humphd commented Oct 1, 2020

Updated to remove semver and do the warning on .connection regardless of version.

Copy link
Member

@jsumners jsumners left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@mcollina mcollina merged commit a0838df into fastify:master Oct 1, 2020
airhorns added a commit to airhorns/light-my-request that referenced this pull request Oct 5, 2020
Before this change, if a Fastify user attached an `onTimeout` hook to their server, an error would be thrown in test mode when injecting requests with light-my-request

```
TypeError: request.raw.socket.on is not a function

      at Object.routeHandler [as handler] (../../node_modules/fastify/lib/route.js:350:28)
      at Router.lookup (../../node_modules/find-my-way/index.js:356:14)
      at ../../node_modules/light-my-request/index.js:101:38
      at Request.prepare (../../node_modules/light-my-request/lib/request.js:110:12)
      at ../../node_modules/light-my-request/index.js:101:11
      at doInject (../../node_modules/light-my-request/index.js:97:12)
      at Chain.<computed> [as then] (../../node_modules/light-my-request/index.js:187:23)
          at runMicrotasks (<anonymous>)
```

This fixes the socket object to have an `on` function (as well as the rest of the EventEmitter interface) so that things that add timeout handlers can at least add them without erroring. This doesn't implement actual timeout injection, but I think that could be done in userland by emitting events on the socket manually.

Similar to fastify#99.
mcollina pushed a commit that referenced this pull request Oct 6, 2020
…#101)

Before this change, if a Fastify user attached an `onTimeout` hook to their server, an error would be thrown in test mode when injecting requests with light-my-request

```
TypeError: request.raw.socket.on is not a function

      at Object.routeHandler [as handler] (../../node_modules/fastify/lib/route.js:350:28)
      at Router.lookup (../../node_modules/find-my-way/index.js:356:14)
      at ../../node_modules/light-my-request/index.js:101:38
      at Request.prepare (../../node_modules/light-my-request/lib/request.js:110:12)
      at ../../node_modules/light-my-request/index.js:101:11
      at doInject (../../node_modules/light-my-request/index.js:97:12)
      at Chain.<computed> [as then] (../../node_modules/light-my-request/index.js:187:23)
          at runMicrotasks (<anonymous>)
```

This fixes the socket object to have an `on` function (as well as the rest of the EventEmitter interface) so that things that add timeout handlers can at least add them without erroring. This doesn't implement actual timeout injection, but I think that could be done in userland by emitting events on the socket manually.

Similar to #99.
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.

Missing socket

6 participants