shelf icon indicating copy to clipboard operation
shelf copied to clipboard

Add context to `Response` for including `file` and `file_not_found` to be identified by other `shelf` `Middleware`.

Open gmpassos opened this issue 2 years ago • 5 comments

  • [x ] I’ve reviewed the contributor guide and applied the relevant portions to this PR.

Updated:

With the Response.context populated with the processed File, other Middleware can manipulate the Response in an optimized way and also ensure the correct processed File. Without that, another Middleware would need to guess the processed File, and in some cases, re-resolve File.stat and also fix the directory path to the "index.html" path.

gmpassos avatar Dec 05 '23 18:12 gmpassos

i see where you're going here.

I'd want to chat with @natebosch or @devoncarew on their thoughts

This change should be documented and tested, too!

kevmoo avatar Dec 05 '23 18:12 kevmoo

With the Response.context populated with the processed File, other Middleware can manipulate the Response in an optimized way and also ensure the correct processed File. Without that, another Middleware would need to guess the processed File, and in some cases, re-resolve File.stat and also fix the directory path to the "index.html" path.

gmpassos avatar Dec 05 '23 18:12 gmpassos

In my use case, I maintain an in-memory cache of shelf Responses. Then I need to verify if the cached Response File has changed using File.stat.

However, my Middleware currently has to recalculate the Response File before caching the Response. This leads to a doubling of the initial Response time due to the additional File resolution, involving calls to .existsSync and statSync, as well as the resolution of the Directory to index.html.

Keep in mind that the primary purpose of a cache is to enhance response time, and the negative impact of these extra calls to statSync is significant.

gmpassos avatar Dec 05 '23 18:12 gmpassos

What's the next step? Regards.

gmpassos avatar Dec 19 '23 05:12 gmpassos

need changelog entry and tests

kevmoo avatar Dec 19 '23 17:12 kevmoo

Is this still something you're interested in landing - can you take a look at adding tests?

natebosch avatar Apr 02 '24 22:04 natebosch

This is important.

I will try to add some tests, but I don't have time for that in the next 15 days.

gmpassos avatar Apr 02 '24 22:04 gmpassos

Can you give an example of how you plan to use this? Did you consider any alternative patterns, such as always storing the key shelf_static:file, and storing a separate boolean field for whether it was found?

Also cc @brianquinlan - can you think of any risks in keeping the File object in the heap for longer than we would have previously.

natebosch avatar Apr 03 '24 00:04 natebosch

Can you give an example of how you plan to use this? Did you consider any alternative patterns, such as always storing the key shelf_static:file, and storing a separate boolean field for whether it was found?

You can take a look at real-world code that is waiting for this: https://github.com/Colossus-Services/bones_api/blob/master/lib/src/bones_api_server.dart#L1788

Note that if 'shelf_static:file' and 'shelf_static:file_not_found' are not present, we need to recalculate the File path, validate it, and ensure (again) that it's inside the "root" directory (simulating the same shelf:createStaticHandler logic).

The context['file'] is then used by the cache "middleware", which will use the File instance to validate the cached files and dispose of the cached data if the File changes: https://github.com/Colossus-Services/bones_api/blob/master/lib/src/bones_api_server_cache.dart#L351

gmpassos avatar Apr 03 '24 01:04 gmpassos

Can you give an example of how you plan to use this? Did you consider any alternative patterns, such as always storing the key shelf_static:file, and storing a separate boolean field for whether it was found?

Also cc @brianquinlan - can you think of any risks in keeping the File object in the heap for longer than we would have previously.

A File object doesn't represent any native resources so keeping it alive shouldn't do anything except cost a bit of memory.

brianquinlan avatar Apr 11 '24 22:04 brianquinlan

@natebosch @brianquinlan Next step?

gmpassos avatar Jun 07 '24 21:06 gmpassos

This PR will continue at PR https://github.com/dart-lang/shelf/pull/436

gmpassos avatar Jun 14 '24 05:06 gmpassos