Repository navigation
Node http2: statCheck() may also return false. - #20692
4 commits merged into
Conversation
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: - https://github.com/nodejs/node/blob/master/lib/internal/http2/core.js#L1621 - https://github.com/nodejs/node/blob/master/lib/internal/http2/core.js#L1671
|
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? |
|
Added tests. |
|
|
||
| export interface ServerStreamFileResponseOptions { | ||
| statCheck?: (stats: fs.Stats, headers: IncomingHttpHeaders, statOptions: StatOptions) => void; | ||
| statCheck?: (stats: fs.Stats, headers: IncomingHttpHeaders, statOptions: StatOptions) => void|false; |
There was a problem hiding this comment.
Would it might make more sense to just make statCheck return void|boolean?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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".
There was a problem hiding this comment.
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"
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Changed to void|boolean.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
That makes sense. Already changed to void|boolean.
|
@jinwoo Please fix the failures indicated in the Travis CI log. |
|
Lint error was fixed. |
|
The test failure is not related to this change
|
* Node http2: statCheck() may also return `false`. 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: - https://github.com/nodejs/node/blob/master/lib/internal/http2/core.js#L1621 - https://github.com/nodejs/node/blob/master/lib/internal/http2/core.js#L1671 * add tests for returning false from statCheck() * change `void|false` to `void|boolean` * fix lint error
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.
npm run lint package-name(ortscif notslint.jsonis present).Select one of these and delete the others:
If adding a new definition:
dts-gen --dt, not by basing it on an existing project.tslint.jsonshould be present, andtsconfig.jsonshould havenoImplicitAny,noImplicitThis, andstrictNullChecksset totrue.If changing an existing definition:
tslint.jsoncontaining{ "extends": "dtslint/dt.json" }.If removing a declaration:
notNeededPackages.json.