[slightlyoff/ServiceWorker] ServiceWorker lifetime and respondWith() with a user-constructed ReadableStream (#882)

Branched from https://github.com/yutakahirano/fetch-with-streams/issues/63#issuecomment-207453714

* respondWith() with a user-constructed ReadableStream

When a user-constructed `ReadableStream` is being consumed by `respondWith()`, `respondWith()` doesn't know what's happening inside the `ReadableStream`. The `ReadableStream` interface of the instance facing `respondWith()` doesn't allow either scraping out its contents or knowing `ReadableStreamDefaultController.close()` is invoked (note that a `ReadableStream` doesn't always have `ReadableStreamDefaultController` behind it).

Until all the chunks are read out from the `ReadableStream`, it's holding some JS objects representing the underlying source and chunks in its queue. So, we want to keep the ServiceWorker alive until they're drained.

Also, note that only `respondWith()` can observe the `ReadableStream`'s state when `respondWith()` is consuming it. It could work but is not good that we tell the instance to wait for to SW like `evt.waitUntilClosed(aStream)` as Ben suggested at https://github.com/yutakahirano/fetch-with-streams/issues/63#issuecomment-207487623.

`evt.respondWith(response, { waitForBody: true });` (https://github.com/yutakahirano/fetch-with-streams/issues/63#issuecomment-207489289) by Ben looks better.

There we could have a timeout to abort receiving chunks even when `waitForBody` is specified so that badly behaving stream doesn't prolong SW lifetime forever.

* respondWith() with a ReadableStream constructed by Fetch API

I agree with Ben's point at https://github.com/yutakahirano/fetch-with-streams/issues/63#issuecomment-207483430. This is also related to the discussion how we realize efficient `pipeTo()` algorithm. I.e. for certain operations, streams would behave like a simple identifier of data source / destination like file descriptor.

I'll try to standardize all the interfaces for these issues by resuming the `pipeTo()` discussion threads.


---
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/slightlyoff/ServiceWorker/issues/882

Received on Wednesday, 13 April 2016 05:30:20 UTC