Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resource Timing: encoded/decoded body size for SW responses #33283

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions service-workers/service-worker/resource-timing-bodySize.https.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!DOCTYPE html>
<meta charset="utf-8">
<script src="/common/utils.js"></script>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<script>

function test_scenario(name) {
promise_test(async (t) => {
const uid = token();
const worker_url = `resources/fetch-response.js?uid=${uid}`;
const scope = `resources/fetch-response.html?uid=${uid}`;
const iframe = document.createElement('iframe');
iframe.src = `${scope}&path=${name}`;
const registration = await service_worker_unregister_and_register(t, worker_url, scope);
t.add_cleanup(() => registration.unregister());
t.add_cleanup(() => iframe.remove());
await wait_for_state(t, registration.installing, 'activated');
const waitForMessage = new Promise(resolve => window.addEventListener('message', ({data}) => resolve(data)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The multiple uses of data here are a bit confusing. Can you rename the "internal" data to buffer, or something similar?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

document.body.appendChild(iframe);
const {buffer, entry} = await waitForMessage;
assert_greater_than(buffer.byteLength, 0);
assert_equals(entry.decodedBodySize, buffer.byteLength);
assert_equals(entry.encodedBodySize, buffer.byteLength);
}, `Constructed response body size: ${name}`);
}

['constructed', 'forward', 'stream'].forEach(test_scenario);

</script>
12 changes: 12 additions & 0 deletions service-workers/service-worker/resources/fetch-response.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<meta charset="utf-8">

<script>
fetch(new URLSearchParams(location.search).get('path')).then(async response => {
const buffer = await response.arrayBuffer();
parent.postMessage({
buffer,
entry: performance.getEntriesByType('resource')[0].toJSON(),
}, '*');
});
</script>
32 changes: 32 additions & 0 deletions service-workers/service-worker/resources/fetch-response.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
self.addEventListener('fetch', event => {
const path = event.request.url.match(/\/(?<name>[^\/]+)$/);
switch (path.groups.name) {
case 'constructed':
event.respondWith(new Response(new Uint8Array([1, 2, 3])));
break;
case 'forward':
event.respondWith(fetch('/common/text-plain.txt'));
break;
case 'stream':
event.respondWith((async() => {
const res = await fetch('/common/text-plain.txt');
const body = await res.body;
const reader = await body.getReader();
const stream = new ReadableStream({
async start(controller) {
while (true) {
const {done, value} = await reader.read();
if (done)
break;

controller.enqueue(value);
}
controller.close();
reader.releaseLock();
}
});
return new Response(stream);
})());
break;
}
});