Read cookies from within a Service Worker? - service-worker

I would like to read a cookie within my service worker to use it when setting up my caches, but can't find any way of doing it.
Is there any way of doing that, or will I need to duplicate cookie data into IDB or similar?

Currently, you can't access them. There's a discussion in the W3C ServiceWorker repo about adding methods to access them in the future.

Related

How should I clear the Relay JS store when logging out?

I'm using react-relay v12.
When logging out, should I create a new relay environment (rather than e.g. clearing the store somehow)? I have been doing just that, but I'm now having problems because the Relay environment in the RelayEnvironmentProvider context is not updated.
In theory I could manually update this using a hook, but this is difficult in certain places (e.g. relay network middleware, state containers)
The easiest solution seems to be to avoid creating a new store and instead clear the old one. Is that possible? Or is there a better solution?

Using Parameters of One Request to Dynamically Change the Response of Another

I have been using response templating to give dynamic responses, given that all the request and query parameters are associated with that request itself. However, I wanted to make a POST request with several parameters, and later use those parameters in a stubbed GET method's body response by using response templating. Is this something possible to do in wiremock? Any input is greatly appreciated, thank you!
Storing state between requests is not a default feature of WireMock outside of mocking the behavior through Stateful Behaviour, which is different from being actually stateful.
Without a custom plugin being able to share information between several requests is therefor not possible. In the WireMock documentation there is a section in the documentation on how to create such a plugin yourself. With a little development experience this is certainly doable.
On GitHub there are several plugin that create a storage mechanism to store information
WireMockCsv: store and retrieve information using HSQL Database.
wiremock-redis-extension does something similar using Redis.
An alternative to these approaches is to create mappings/data just before the test starts. For example generating all the responses beforehand and then using Templated BodyFileName tag to retrieve the just-in-time created file. Another way of achieving this result is to use the Admin API to create the mappings themselves directly.

Can I have Faraday HTTP cache respond with cache when origin is down?

Is it possible to have the faraday-http-cache middleware return a cached resource when the origin is down, sort of like what Cloudflare does? I ask because our Rails application is currently doubling as a sort of shared cache for our CMS's REST API and we would like to serve cached content if the CMS goes down.
There was a hack for this posted in a GitHub issue from 2016. The conversation suggests the use of the max-stale cache directive because there were no concrete plans to implement the stale-if-error directive from RFC 5861. Does anyone know for sure if this still works?
Thanks.
After running some tests, it appears that if the origin domain is non-existent then you will just get a network error, even if dummy data for the fake site is stored in the cache. I had to implement new middleware for recovering from these errors.

What is the use of auth.restTokenPrivateKey in gerrit's secure.config for?

I came across this variable which got generated and stored in secure.config file on gerrit review_site, Is it possible for me to make use of this restTokenPrivateKey value to call rest api to gerrit instance?
Iff4f6f9a1c302dfd9e2b7fe52f2ddbebe06d9967 removed it. According to the other change referenced there, it was never really used for anything.
The Authentication section of the REST API docs says you can prefix the endpoint URL with /a/ to use HTTP authentication instead.

Stream remote file to client in ruby/rails 4/unicorn/nginx

I am trying to stream a file from a remote storage service (not s3 :-)) to the client using Ruby on Rails 4.2.
My server needs to stay in the middle of things to authenticate the client request but also to build up the request to the remote storage service since all requests to that service need to be authenticated using a custom header param. This makes it not possible to do a simple redirect_to and let the client download the file directly (but do let me know if this IS in fact possible using rails!). Also I want to keep the url of the file cloaked for the client.
Up until now I am using a gem called ZipLine but this also does not work as it still buffers the remote file before sending it to the client. As I am using unicorn/nginx, this might also be due to a setting in either of those two, that prevents proper streaming.
As per rails doc's instructions I have tried adding
listen 3000, tcp_nopush: false
to config/unicorn.rb but to no avail.
A solution might be to cache the remote file locally for a certain period and just serve that file. This would make some things easier but also creating new headaches like keeping the remote and cached files in sync, setting the right triggers for cache expiration, etc.
So to sum up:
1) How do I accomplish the scenario above?
2) If this is not a intelligent/efficient way of doing things, should I just cache a remote copy?
3) What are your experiences/recommendations in given scenario?
I have come across various solutions scattered around the interweb but none inspire a complete solution.
Thanks!
I am assuming you the third party storage service has an HTTP access. If you did consider using redirect_to, I assume the service also provides a means to allow per download authorization. Like unique key in the header that expires and does not expose your secret api keys or HMAC signed URL with expiration time as a param.
Anyhow, most cloud storage services provide this kind of file access. I would highly recommend let the service stream the file. Your app should simply authorize the user and redirect to the service. Rails allows you to add custom headers while redirecting. It is discussed in Rails guides.
10.2.1 Setting Custom Headers
If you want to set custom headers for a response then response.headers
is the place to do it. The headers attribute is a hash which maps
header names to their values, and Rails will set some of them
automatically. If you want to add or change a header, just assign it
to response.headers
So your action code would end up being something like this:
def download
# do_auth_check
response.headers["Your-API-Auth-Key"] = "SOME-RANDOM-STRING"
redirect_to url
end
Don't use up unnecessary server resources by streaming through them all those downloads. We are paying cloud services to that after all :)

Resources