[ServiceWorker] FetchEvent.respondWith() should check the used flag of the response (#567)

In the current spec, the `used flag` is not checked in `respondWith()`.
So we can read the body of the response before responding to the FetchEvent.

But I think if the `used flag` it true, the all data in the response is consumed.
So respondWith() should set the `respond-with error flag`.

To avoid this error, we can use `clone()` method.

```JavaScript
self.addEventListener('fetch', function(event) {
    event.respondWith(new Promise(function(resolve, reject) {
      fetch(event.request)
        .then(function(response) {
            response.text()
              .then(function(body) {
                  console.log(body);
                })
            resolve(response);  // This should cause an error.
          });
    }));
  });
```
```JavaScript
self.addEventListener('fetch', function(event) {
    event.respondWith(new Promise(function(resolve, reject) {
      fetch(event.request)
        .then(function(response) {
            response.clone().text()
              .then(function(body) {
                  console.log(body);
                })
            resolve(response);  // OK.
          });
    }));
  });
```

---
Reply to this email directly or view it on GitHub:
https://github.com/slightlyoff/ServiceWorker/issues/567

Received on Tuesday, 25 November 2014 02:01:30 UTC