Skip to content

Node http2: statCheck() may also return false. - #20692

Merged
4 commits merged into
DefinitelyTyped:masterfrom
jinwoo:master
Oct 19, 2017
Merged

4 commits merged into
DefinitelyTyped:masterfrom
jinwoo:master

Conversation

@jinwoo

@jinwoo jinwoo commented Oct 18, 2017

Copy link
Copy Markdown
Contributor

https://nodejs.org/dist/latest-v8.x/docs/api/http2.html#http2_http2stream_respondwithfile_path_headers_options
says:
The options.statCheck function may also be used to cancel the send operation by
returning false

Related node code:

Please fill in this template.

  • Use a meaningful title for the pull request. Include the name of the package modified.
  • Test the change in your own code. (Compile and run.)
  • Follow the advice from the readme.
  • Avoid common mistakes.
  • Run npm run lint package-name (or tsc if no tslint.json is present).

Select one of these and delete the others:

If adding a new definition:

  • The package does not provide its own types, and you can not add them.
  • If this is for an NPM package, match the name. If not, do not conflict with the name of an NPM package.
  • Create it with dts-gen --dt, not by basing it on an existing project.
  • tslint.json should be present, and tsconfig.json should have noImplicitAny, noImplicitThis, and strictNullChecks set to true.

If changing an existing definition:

If removing a declaration:

  • If a package was never on DefinitelyTyped, you don't need to do anything. (If you wrote a package and provided types, you don't need to register it with us.)
  • Delete the package's directory.
  • Add it to notNeededPackages.json.

@dt-bot

dt-bot commented Oct 18, 2017

Copy link
Copy Markdown
Member

types/node/index.d.ts

to authors (@DefinitelyTyped/DefinitelyTyped @parambirs @tellnes @WilcoBakker @octo-sniffle @smac89 @Flarna @mwiktorczyk @wwwy3y3 @DeividasBakanas @kjin @alvis Microsoft TypeScript (account can't be detected)). Could you review this PR?
👍 or 👎?

@typescript-bot typescript-bot added the Popular package This PR affects a popular package (as counted by NPM download counts). label Oct 18, 2017
@jinwoo

jinwoo commented Oct 18, 2017

Copy link
Copy Markdown
Contributor Author

Added tests.

Comment thread types/node/index.d.ts Outdated

export interface ServerStreamFileResponseOptions {
statCheck?: (stats: fs.Stats, headers: IncomingHttpHeaders, statOptions: StatOptions) => void;
statCheck?: (stats: fs.Stats, headers: IncomingHttpHeaders, statOptions: StatOptions) => void|false;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would it might make more sense to just make statCheck return void|boolean?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I want to make it explicitly false because the node code compares with === false rather than !statCheck(...). I.e. returning things like true doesn't make any sense there. What do you think?

@kjin kjin Oct 18, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think it makes sense to return true -- from the source code it looks like statCheck is essentially being treated as a predicate function. The === false suggests that boolean return type is expected/preferred; using that instead of !statCheck(...) ensures that if a user happens to use a void return type function instead, it won't be interpreted to mean "never stream any file".

@kjin kjin Oct 18, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In fact, I would suggest changing it to only boolean, as the purpose of the method is roughly "given a file stat object, return whether it should be streamed"

@jinwoo jinwoo Oct 18, 2017 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I disagree. My interpretation of the document and the code is that statCheck() is normally used just as a hook, not as a predicate, for the client to be able to send headers like last-modified. But in addition to that, it can also cancel the file transfer by returning false from that hook. false is just a special value that is checked. It could be a totally different value, say 42, in which case the type would be void|42.

I don't want to make it only boolean because it'll force the client code unnecessarily to return true most of the cases.

@kjin kjin Oct 18, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ok, I'm convinced that void is a reasonable return type (given that statCheck is intended to be used, like you said, as a hook).

I disagree that false is a special value being checked. 42 is an arbitrary value, but false is not; it specifically means that some boolean predicate is not satisfied (that predicate being "based on the input arguments, this file should be streamed").

Looking from a user's perspective, to enforce that statCheck only returns false is to ask them to write a function that looks like

function statCheck1(stats, headers, statOptions): void|false {
  if (stats.lastModified < Date.now() - 1000) { // or something like that
    return;
  } else {
    // mutate headers
    return false;
  }
}

when it is more idiomatic to write

function statCheck2(stats, headers, statOptions): boolean {
  if (stats.lastModified < Date.now() - 1000) {
    return true;
  } else {
    // mutate headers
    return false;
  }
}

Both functions end up with the same result. Again, the === false just prevents statCheck1's falsey undefined return value from being interpreted as false.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Normally the client doesn't cancel the transfer. See the example code in the node document: https://nodejs.org/dist/latest-v8.x/docs/api/http2.html

function statCheck(stat, headers) {
  headers['last-modified'] = stat.mtime.toUTCString();
}

It's just a hook for adding the last-modified header.

If we make it only boolean, the client must return true even in that case. I'm ok with void|boolean if you feel strongly though I still prefer void|false because returning true isn't very meaningful.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Changed to void|boolean.

@kjin kjin Oct 18, 2017 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yep, I agree now that having the return type be only boolean is not suitable.

It's true that in this particular case, true doesn't hold much meaning. That being said, I still advocate void|boolean, because if a function returns false in some cases, it is generally expected to return true in other cases. I would consider a function that sometimes returns false, and other times returns nothing, to be bad practice -- that is my sole reservation, and it's agnostic to how the caller will end up using the return value.

@jinwoo jinwoo Oct 18, 2017 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That makes sense. Already changed to void|boolean.

@kjin kjin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks!

@typescript-bot

Copy link
Copy Markdown
Contributor

@jinwoo Please fix the failures indicated in the Travis CI log.

@jinwoo

jinwoo commented Oct 18, 2017

Copy link
Copy Markdown
Contributor Author

Lint error was fixed.

@jinwoo

jinwoo commented Oct 19, 2017

Copy link
Copy Markdown
Contributor Author

The test failure is not related to this change

The job exceeded the maximum time limit for jobs, and has been terminated.

@ghost
ghost merged commit e86a56d into DefinitelyTyped:master Oct 19, 2017
@teppeis teppeis mentioned this pull request Nov 5, 2017
6 of 8 tasks
KSXGitHub pushed a commit to KSXGitHub/DefinitelyTyped that referenced this pull request May 12, 2018
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Popular package This PR affects a popular package (as counted by NPM download counts).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants