Write a function that orders a server's responses by their RFC 9218 priorities.
A server speaking HTTP/2 or HTTP/3 sends many responses over one connection, so it has to choose whose bytes go next. Browsers tell it with the priority request header of RFC 9218, Extensible Priorities: Chrome sends u=0 for a stylesheet, u=1, i for an image with fetchpriority="high", and no header at all for an async script. Write scheduleResponses(requests), the scheduler of a small server. requests lists the requests in the order they arrived on the connection, all at once, as { name, priority, chunks }: priority is the header's value as a string, or undefined when the request had none, and chunks is how many equal chunks the response takes, 0 or more. Return the names in the order the server sends the chunks, one entry per chunk, and leave requests as it is. Follow these rules:
Reading the header: RFC 9218 writes it as a Structured Fields dictionary
members are separated by commas, with optional spaces around them
a member is key=value, or a bare key, which means the boolean ?1 (true)
when a key appears more than once, only its last value counts
u, the urgency, counts only as an integer from 0 to 7, written in digits
i, incremental, counts only as a boolean: ?1 or a bare i is true, ?0 is false
everything else is ignored: other keys, and a u or an i whose value is of
another type or out of range, like u=9, u=-1, u=1.5, u="1", a bare u, or i=1
a missing or ignored u is 3, a missing or ignored i is false, so no header,
or an empty one, means urgency 3 and not incremental
Sending: what RFC 9218 recommends, and one choice it leaves to the server
every chunk of a lower urgency number goes before any chunk of a higher one
responses of one urgency that aren't incremental go one after another,
each whole, in the order they arrived
incremental responses of one urgency take turns, one chunk each, in the
order they arrived, round after round; one that has sent its last chunk
leaves the turns
this server's own choice: when one urgency has both kinds, every response
that isn't incremental goes before the incremental ones
Every header in the tests is well formed: no trailing commas and no spaces around =.
scheduleResponses([
{ name: "mint.jpg", priority: "u=2, i", chunks: 3 },
{ name: "reviews.js", priority: undefined, chunks: 2 },
{ name: "shop.css", priority: "u=0", chunks: 2 },
{ name: "black.jpg", priority: "u=2, i", chunks: 2 },
{ name: "menu.js", priority: "u=1", chunks: 1 },
]);
// ["shop.css", "shop.css", "menu.js", "mint.jpg", "black.jpg", "mint.jpg",
// "black.jpg", "mint.jpg", "reviews.js", "reviews.js"]
solution.jsfunction scheduleResponses(requests) {
// Today the server sends each response whole, in the order the requests arrived.
const order = [];
for (const request of requests) {
for (let chunk = 0; chunk < request.chunks; chunk++) order.push(request.name);
}
return order;
}
- the Mint tea page's requests go out by urgency, whole or in turns
- every chunk of a lower urgency number goes first, whatever order the requests came in
- no header, or an empty one, means urgency 3 and not incremental
- incremental responses of one urgency take turns, and leave the turns when they're done
- within one urgency, whole responses go one after another before the ones that take turns
- reads a bare i, ?1 and ?0, members in any order, and spaces around commas
- ignores other keys and values of the wrong type or out of range, and takes a key's last value
- sends nothing for no requests or for responses with no chunks, and leaves the requests as they were
The answer
Write it yourself first. Then compare it with a reference solution and the checks it passes.
Checking your sign-in…