1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-22 15:06:54 -05:00
denoland-deno/ext
Matt Mastracci 576d0db372
fix(ext/http): ensure request body resource lives as long as response is alive (#20206)
Deno.serve's fast streaming implementation was not keeping the request
body resource ID alive. We were taking the `Rc<Resource>` from the
resource table during the response, so a hairpin duplex response that
fed back the request body would work.

However, if any JS code attempted to read from the request body (which
requires the resource ID to be valid), the response would fail with a
difficult-to-diagnose "EOF" error.

This was affecting more complex duplex uses of `Deno.fetch` (though as
far as I can tell was unreported).

Simple test:

```ts
        const reader = request.body.getReader();
        return new Response(
          new ReadableStream({
            async pull(controller) {
              const { done, value } = await reader.read();
              if (done) {
                controller.close();
              } else {
                controller.enqueue(value);
              }
            },
          }),
```

And then attempt to use the stream in duplex mode:

```ts

async function testDuplex(
  reader: ReadableStreamDefaultReader<Uint8Array>,
  writable: WritableStreamDefaultWriter<Uint8Array>,
) {
  await writable.write(new Uint8Array([1]));
  const chunk1 = await reader.read();
  assert(!chunk1.done);
  assertEquals(chunk1.value, new Uint8Array([1]));
  await writable.write(new Uint8Array([2]));
  const chunk2 = await reader.read();
  assert(!chunk2.done);
  assertEquals(chunk2.value, new Uint8Array([2]));
  await writable.close();
  const chunk3 = await reader.read();
  assert(chunk3.done);
}
```

In older versions of Deno, this would just lock up. I believe after
23ff0e722e, it started throwing a more
explicit error:

```
httpServerStreamDuplexJavascript => ./cli/tests/unit/serve_test.ts:1339:6
error: TypeError: request or response body error: error reading a body from connection: Connection reset by peer (os error 54)
    at async Object.pull (ext:deno_web/06_streams.js:810:27)
```
2023-08-21 01:35:26 +00:00
..
broadcast_channel chore: forward v1.36.1 to main (#20119) 2023-08-10 16:44:41 +03:00
cache chore: forward v1.36.1 to main (#20119) 2023-08-10 16:44:41 +03:00
console chore: forward v1.36.1 to main (#20119) 2023-08-10 16:44:41 +03:00
crypto chore: forward v1.36.1 to main (#20119) 2023-08-10 16:44:41 +03:00
fetch fix: release ReadeableStream in fetch (#17365) 2023-08-16 14:02:15 +02:00
ffi chore: forward v1.36.1 to main (#20119) 2023-08-10 16:44:41 +03:00
fs chore: forward v1.36.1 to main (#20119) 2023-08-10 16:44:41 +03:00
http fix(ext/http): ensure request body resource lives as long as response is alive (#20206) 2023-08-21 01:35:26 +00:00
io chore: deno_core -> 0.201.0 (#20135) 2023-08-12 19:04:45 +00:00
kv feat(ext/kv): key expiration (#20091) 2023-08-18 17:34:16 +08:00
napi chore: forward v1.36.1 to main (#20119) 2023-08-10 16:44:41 +03:00
net fix(ext/net): implement a graceful error on an invalid SSL certificate (#20157) 2023-08-15 00:11:12 +00:00
node fix(node/http): emit error when addr in use (#20200) 2023-08-18 13:48:18 +02:00
tls chore: forward v1.36.1 to main (#20119) 2023-08-10 16:44:41 +03:00
url perf(ext/urlpattern): optimize URLPattern.exec (#20170) 2023-08-16 12:58:03 +02:00
web perf(ext/event): always set timeStamp to 0 (#20191) 2023-08-20 10:02:47 +00:00
webidl chore: forward v1.36.1 to main (#20119) 2023-08-10 16:44:41 +03:00
websocket perf(ext/event): always set timeStamp to 0 (#20191) 2023-08-20 10:02:47 +00:00
webstorage chore: forward v1.36.1 to main (#20119) 2023-08-10 16:44:41 +03:00