|
7 | 7 |
|
8 | 8 | namespace workerd { |
9 | 9 |
|
10 | | -// TODO(now): implement |
| 10 | +// ======================================================================================= |
| 11 | +// ReadableStream handling |
| 12 | + |
| 13 | +namespace { |
| 14 | + |
| 15 | +// TODO(cleanup): These classes have been copied from streams/readable.c++. The copies there can be |
| 16 | +// deleted as soon as we've switched from StreamSink to ExternalPusher and can delete all the |
| 17 | +// StreamSink-related code. For now I'm not trying to avoid duplication. |
| 18 | + |
| 19 | +// HACK: We need as async pipe, like kj::newOneWayPipe(), except supporting explicit end(). So we |
| 20 | +// wrap the two ends of the pipe in special adapters that track whether end() was called. |
| 21 | +class ExplicitEndOutputPipeAdapter final: public capnp::ExplicitEndOutputStream { |
| 22 | + public: |
| 23 | + ExplicitEndOutputPipeAdapter( |
| 24 | + kj::Own<kj::AsyncOutputStream> inner, kj::Own<kj::RefcountedWrapper<bool>> ended) |
| 25 | + : inner(kj::mv(inner)), |
| 26 | + ended(kj::mv(ended)) {} |
| 27 | + |
| 28 | + kj::Promise<void> write(kj::ArrayPtr<const byte> buffer) override { |
| 29 | + return KJ_REQUIRE_NONNULL(inner)->write(buffer); |
| 30 | + } |
| 31 | + kj::Promise<void> write(kj::ArrayPtr<const kj::ArrayPtr<const byte>> pieces) override { |
| 32 | + return KJ_REQUIRE_NONNULL(inner)->write(pieces); |
| 33 | + } |
| 34 | + |
| 35 | + kj::Maybe<kj::Promise<uint64_t>> tryPumpFrom( |
| 36 | + kj::AsyncInputStream& input, uint64_t amount) override { |
| 37 | + return KJ_REQUIRE_NONNULL(inner)->tryPumpFrom(input, amount); |
| 38 | + } |
| 39 | + |
| 40 | + kj::Promise<void> whenWriteDisconnected() override { |
| 41 | + return KJ_REQUIRE_NONNULL(inner)->whenWriteDisconnected(); |
| 42 | + } |
| 43 | + |
| 44 | + kj::Promise<void> end() override { |
| 45 | + // Signal to the other side that end() was actually called. |
| 46 | + ended->getWrapped() = true; |
| 47 | + inner = kj::none; |
| 48 | + return kj::READY_NOW; |
| 49 | + } |
| 50 | + |
| 51 | + private: |
| 52 | + kj::Maybe<kj::Own<kj::AsyncOutputStream>> inner; |
| 53 | + kj::Own<kj::RefcountedWrapper<bool>> ended; |
| 54 | +}; |
| 55 | + |
| 56 | +class ExplicitEndInputPipeAdapter final: public kj::AsyncInputStream { |
| 57 | + public: |
| 58 | + ExplicitEndInputPipeAdapter(kj::Own<kj::AsyncInputStream> inner, |
| 59 | + kj::Own<kj::RefcountedWrapper<bool>> ended, |
| 60 | + kj::Maybe<uint64_t> expectedLength) |
| 61 | + : inner(kj::mv(inner)), |
| 62 | + ended(kj::mv(ended)), |
| 63 | + expectedLength(expectedLength) {} |
| 64 | + |
| 65 | + kj::Promise<size_t> tryRead(void* buffer, size_t minBytes, size_t maxBytes) override { |
| 66 | + size_t result = co_await inner->tryRead(buffer, minBytes, maxBytes); |
| 67 | + |
| 68 | + KJ_IF_SOME(l, expectedLength) { |
| 69 | + KJ_ASSERT(result <= l); |
| 70 | + l -= result; |
| 71 | + if (l == 0) { |
| 72 | + // If we got all the bytes we expected, we treat this as a successful end, because the |
| 73 | + // underlying KJ pipe is not actually going to wait for the other side to drop. This is |
| 74 | + // consistent with the behavior of Content-Length in HTTP anyway. |
| 75 | + ended->getWrapped() = true; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + if (result < minBytes) { |
| 80 | + // Verify that end() was called. |
| 81 | + if (!ended->getWrapped()) { |
| 82 | + JSG_FAIL_REQUIRE(Error, "ReadableStream received over RPC disconnected prematurely."); |
| 83 | + } |
| 84 | + } |
| 85 | + co_return result; |
| 86 | + } |
| 87 | + |
| 88 | + kj::Maybe<uint64_t> tryGetLength() override { |
| 89 | + return inner->tryGetLength(); |
| 90 | + } |
| 91 | + |
| 92 | + kj::Promise<uint64_t> pumpTo(kj::AsyncOutputStream& output, uint64_t amount) override { |
| 93 | + return inner->pumpTo(output, amount); |
| 94 | + } |
| 95 | + |
| 96 | + private: |
| 97 | + kj::Own<kj::AsyncInputStream> inner; |
| 98 | + kj::Own<kj::RefcountedWrapper<bool>> ended; |
| 99 | + kj::Maybe<uint64_t> expectedLength; |
| 100 | +}; |
| 101 | + |
| 102 | +} // namespace |
| 103 | + |
| 104 | +class ExternalPusherImpl::InputStreamImpl final: public ExternalPusher::InputStream::Server { |
| 105 | + public: |
| 106 | + InputStreamImpl(kj::Own<kj::AsyncInputStream> stream): stream(kj::mv(stream)) {} |
| 107 | + |
| 108 | + kj::Maybe<kj::Own<kj::AsyncInputStream>> stream; |
| 109 | +}; |
| 110 | + |
| 111 | +kj::Promise<void> ExternalPusherImpl::pushByteStream(PushByteStreamContext context) { |
| 112 | + kj::Maybe<uint64_t> expectedLength; |
| 113 | + auto lp1 = context.getParams().getLengthPlusOne(); |
| 114 | + if (lp1 > 0) { |
| 115 | + expectedLength = lp1 - 1; |
| 116 | + } |
| 117 | + |
| 118 | + auto pipe = kj::newOneWayPipe(expectedLength); |
| 119 | + |
| 120 | + auto endedFlag = kj::refcounted<kj::RefcountedWrapper<bool>>(false); |
| 121 | + |
| 122 | + auto out = kj::heap<ExplicitEndOutputPipeAdapter>(kj::mv(pipe.out), kj::addRef(*endedFlag)); |
| 123 | + auto in = |
| 124 | + kj::heap<ExplicitEndInputPipeAdapter>(kj::mv(pipe.in), kj::mv(endedFlag), expectedLength); |
| 125 | + |
| 126 | + auto results = context.initResults(capnp::MessageSize{4, 2}); |
| 127 | + |
| 128 | + results.setSource(inputStreamSet.add(kj::heap<InputStreamImpl>(kj::mv(in)))); |
| 129 | + results.setSink(byteStreamFactory.kjToCapnp(kj::mv(out))); |
| 130 | + return kj::READY_NOW; |
| 131 | +} |
| 132 | + |
| 133 | +kj::Own<kj::AsyncInputStream> ExternalPusherImpl::unwrapStream( |
| 134 | + ExternalPusher::InputStream::Client cap) { |
| 135 | + auto& unwrapped = KJ_REQUIRE_NONNULL( |
| 136 | + inputStreamSet.tryGetLocalServerSync(cap), "pushed external is not a byte stream"); |
| 137 | + |
| 138 | + return KJ_REQUIRE_NONNULL(kj::mv(kj::downcast<InputStreamImpl>(unwrapped).stream), |
| 139 | + "pushed byte stream has already been consumed"); |
| 140 | +} |
11 | 141 |
|
12 | 142 | } // namespace workerd |
0 commit comments