GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — http: DRY ClientRequest.prototype._deferToConnect by ahoym · Pull Request #2769 · nodejs/node

Conversation

@ahoym

@ahoym ahoym commented Sep 9, 2015

Copy link
Copy Markdown
Contributor

Came across some repetitive lines when reviewing code.

Logic for calling the passed in socket method and/or cb was duplicated. Extracted these calls into a separate function, callSocketMethod, and invoked this new function in place of the replaced code.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@ahoym
http: DRY ClientRequest.prototype._deferToConnect
073ec80
Logic for calling the passed in socket method and/or callback was duplicated.
cjihrig
cjihrig reviewed Sep 9, 2015
View reviewed changes
Comment thread lib/_http_client.js Outdated

@cjihrig cjihrig Sep 9, 2015

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Can you make this function callSocketMethod()

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@mscdex mscdex added the http Issues and PRs related to the http subsystem. label Sep 9, 2015
@ahoym
Prefer fn declaration and braceless if statements
66ddd88
@ahoym

ahoym commented Sep 9, 2015

Copy link
Copy Markdown
Contributor Author

Comments have been addressed. Of course, any more are welcome.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ahoym
Remove unnecessary semi-colon
1f20e30
@kahnvex

kahnvex commented Sep 9, 2015

Copy link
Copy Markdown

🍇

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

brendanashworth
brendanashworth reviewed Sep 10, 2015
View reviewed changes
Comment thread lib/_http_client.js

@brendanashworth brendanashworth Sep 10, 2015

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

While you're here, self doesn't seem to be necessary. Mind removing it? It can be replaced with, as far as I can tell, a var socket = this.socket. The bottom reference to self can just be used as this.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ahoym ahoym Sep 10, 2015

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I converted the code as you suggested (good catch btw), and that caused the error

cannot read property 'writable' of null

if (socket.writable) { // socket used to be self.socket
  ...

This seems to stem from the initial _deferToConnect call, where this.socket is undefined. When the ClientRequest catches the 'socket' event, the socket variable in onSocket() refers to the initial undefined value which was scoped in. As a result, everything blows up when trying to read the writable property of that.

A work around that I found was to have onSocket() accept a sock argument (given by the socket event), and update socket with that parameter. So this piece of code works:

ClientRequest.prototype._deferToConnect = function(method, arguments_, cb) {
  // ...
  var socket = this.socket;
  function callSocketMethod() {
    if (method)
      socket[method].apply(socket, arguments_);
    if (typeof cb === 'function')
      cb();
  }
  var onSocket = function(sock) { // Now accepts the socket from the 'socket' event.
    socket = sock || socket; // Update socket reference.
    if (socket.writable) {
      callSocketMethod();
    } else {
      socket.once('connect', callSocketMethod);
    }
  };
  if (!socket) {
    this.once('socket', onSocket);
  } else {
    onSocket();
  }
};

However I'm not completely sure on the repercussions of this. Maybe leaving the existing implementation would be for the better? I'm interested in what you think

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@brendanashworth brendanashworth Sep 10, 2015

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Hmm, you're right, that is something I missed originally. I'd say let's keep the current implementation, as that new one wouldn't really improve the quality of the code. Sorry about the run around!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ahoym ahoym Sep 10, 2015

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Sounds good, then I'll leave this PR as it is. And no need to apologize, we got some documentation and a deeper understanding of the code here 😄

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@brendanashworth

brendanashworth commented Sep 12, 2015

Copy link
Copy Markdown
Contributor

CI: https://ci.nodejs.org/view/iojs/job/node-test-pull-request/283/

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ahoym

ahoym commented Sep 14, 2015

Copy link
Copy Markdown
Contributor Author

Seems like the build failed?

Looks like only the node-test-commit-arm (#603) job failed.
Digging deeper, the pi1-raspbian-wheezy-1_of_2 and pi2-raspbian-wheezy-2_of_2 builds are both erroring out with this:

returned status code 128:
stdout: 
stderr: error: object directory /Users/rvagg/git/iojs/io.js/.git/objects does not exist; check .git/objects/info/alternates.
error: refs/remotes/origin/archived-io.js-v0.10 does not point to a valid object!
error: refs/remotes/origin/archived-io.js-v0.12 does not point to a valid object!
error: refs/remotes/origin/mergev010/base does not point to a valid object!
...
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rvagg

rvagg commented Sep 15, 2015

Copy link
Copy Markdown
Member

https://ci.nodejs.org/job/node-test-pull-request/302/

those arm errors were fixed yesterday

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@brendanashworth

brendanashworth commented Sep 16, 2015

Copy link
Copy Markdown
Contributor

CI is happy, this LGTM. @cjihrig ?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@cjihrig

cjihrig commented Sep 16, 2015

Copy link
Copy Markdown
Contributor

Yep, LGTM

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ahoym

ahoym commented Sep 21, 2015

Copy link
Copy Markdown
Contributor Author

Bump

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

cjihrig pushed a commit that referenced this pull request Sep 22, 2015
@ahoym @cjihrig
http: remove redundant code in _deferToConnect
79d2c4e
Logic for calling the passed in socket method and/or callback
was duplicated. This commit refactors the relevant code to
remove the redundancy.

PR-URL: #2769
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Brendan Ashworth <[email protected]>
@cjihrig

cjihrig commented Sep 22, 2015

Copy link
Copy Markdown
Contributor

Thanks! Landed in 79d2c4e after squashing.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@cjihrig cjihrig closed this Sep 22, 2015
rvagg pushed a commit that referenced this pull request Sep 22, 2015
@ahoym @rvagg
http: remove redundant code in _deferToConnect
f68fed2
Logic for calling the passed in socket method and/or callback
was duplicated. This commit refactors the relevant code to
remove the redundancy.

PR-URL: #2769
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Brendan Ashworth <[email protected]>
@rvagg rvagg mentioned this pull request Sep 22, 2015
Release proposal: v4.1.1 #2995
Merged
@ahoym
ahoym deleted the DRY-ClientRequest-_deferToConnect branch September 30, 2015 01:58
@leonardoadame leonardoadame mentioned this pull request Jul 8, 2023
[Snyk] Security upgrade semantic-release from 20.1.3 to 21.0.2 leonardoadame/node#96
Open
@aliscco aliscco mentioned this pull request Jul 9, 2023
[Snyk] Security upgrade semantic-release from 17.4.7 to 21.0.2 aliscco/alisco-node#627
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

http Issues and PRs related to the http subsystem.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

6 participants

@ahoym @kahnvex @brendanashworth @rvagg @cjihrig @mscdex
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.