/src/node/src/stream_pipe.cc
Line | Count | Source (jump to first uncovered line) |
1 | | #include "stream_pipe.h" |
2 | | #include "stream_base-inl.h" |
3 | | #include "node_buffer.h" |
4 | | #include "util-inl.h" |
5 | | |
6 | | namespace node { |
7 | | |
8 | | using v8::BackingStore; |
9 | | using v8::Context; |
10 | | using v8::Function; |
11 | | using v8::FunctionCallbackInfo; |
12 | | using v8::FunctionTemplate; |
13 | | using v8::HandleScope; |
14 | | using v8::Isolate; |
15 | | using v8::Just; |
16 | | using v8::Local; |
17 | | using v8::Maybe; |
18 | | using v8::Nothing; |
19 | | using v8::Object; |
20 | | using v8::Value; |
21 | | |
22 | | StreamPipe::StreamPipe(StreamBase* source, |
23 | | StreamBase* sink, |
24 | | Local<Object> obj) |
25 | 0 | : AsyncWrap(source->stream_env(), obj, AsyncWrap::PROVIDER_STREAMPIPE) { |
26 | 0 | MakeWeak(); |
27 | |
|
28 | 0 | CHECK_NOT_NULL(sink); |
29 | 0 | CHECK_NOT_NULL(source); |
30 | | |
31 | 0 | source->PushStreamListener(&readable_listener_); |
32 | 0 | sink->PushStreamListener(&writable_listener_); |
33 | |
|
34 | 0 | uses_wants_write_ = sink->HasWantsWrite(); |
35 | 0 | } |
36 | | |
37 | 0 | StreamPipe::~StreamPipe() { |
38 | 0 | Unpipe(true); |
39 | 0 | } |
40 | | |
41 | 0 | StreamBase* StreamPipe::source() { |
42 | 0 | return static_cast<StreamBase*>(readable_listener_.stream()); |
43 | 0 | } |
44 | | |
45 | 0 | StreamBase* StreamPipe::sink() { |
46 | 0 | return static_cast<StreamBase*>(writable_listener_.stream()); |
47 | 0 | } |
48 | | |
49 | 0 | void StreamPipe::Unpipe(bool is_in_deletion) { |
50 | 0 | if (is_closed_) |
51 | 0 | return; |
52 | | |
53 | | // Note that we possibly cannot use virtual methods on `source` and `sink` |
54 | | // here, because this function can be called from their destructors via |
55 | | // `OnStreamDestroy()`. |
56 | 0 | if (!source_destroyed_) |
57 | 0 | source()->ReadStop(); |
58 | |
|
59 | 0 | is_closed_ = true; |
60 | 0 | is_reading_ = false; |
61 | 0 | source()->RemoveStreamListener(&readable_listener_); |
62 | 0 | if (pending_writes_ == 0) |
63 | 0 | sink()->RemoveStreamListener(&writable_listener_); |
64 | |
|
65 | 0 | if (is_in_deletion) return; |
66 | | |
67 | | // Delay the JS-facing part with SetImmediate, because this might be from |
68 | | // inside the garbage collector, so we can’t run JS here. |
69 | 0 | HandleScope handle_scope(env()->isolate()); |
70 | 0 | BaseObjectPtr<StreamPipe> strong_ref{this}; |
71 | 0 | env()->SetImmediate([this, strong_ref](Environment* env) { |
72 | 0 | HandleScope handle_scope(env->isolate()); |
73 | 0 | Context::Scope context_scope(env->context()); |
74 | 0 | Local<Object> object = this->object(); |
75 | |
|
76 | 0 | Local<Value> onunpipe; |
77 | 0 | if (!object->Get(env->context(), env->onunpipe_string()).ToLocal(&onunpipe)) |
78 | 0 | return; |
79 | 0 | if (onunpipe->IsFunction() && |
80 | 0 | MakeCallback(onunpipe.As<Function>(), 0, nullptr).IsEmpty()) { |
81 | 0 | return; |
82 | 0 | } |
83 | | |
84 | | // Set all the links established in the constructor to `null`. |
85 | 0 | Local<Value> null = Null(env->isolate()); |
86 | |
|
87 | 0 | Local<Value> source_v; |
88 | 0 | Local<Value> sink_v; |
89 | 0 | if (!object->Get(env->context(), env->source_string()).ToLocal(&source_v) || |
90 | 0 | !object->Get(env->context(), env->sink_string()).ToLocal(&sink_v) || |
91 | 0 | !source_v->IsObject() || !sink_v->IsObject()) { |
92 | 0 | return; |
93 | 0 | } |
94 | | |
95 | 0 | if (object->Set(env->context(), env->source_string(), null).IsNothing() || |
96 | 0 | object->Set(env->context(), env->sink_string(), null).IsNothing() || |
97 | 0 | source_v.As<Object>() |
98 | 0 | ->Set(env->context(), env->pipe_target_string(), null) |
99 | 0 | .IsNothing() || |
100 | 0 | sink_v.As<Object>() |
101 | 0 | ->Set(env->context(), env->pipe_source_string(), null) |
102 | 0 | .IsNothing()) { |
103 | 0 | return; |
104 | 0 | } |
105 | 0 | }); |
106 | 0 | } |
107 | | |
108 | 0 | uv_buf_t StreamPipe::ReadableListener::OnStreamAlloc(size_t suggested_size) { |
109 | 0 | StreamPipe* pipe = ContainerOf(&StreamPipe::readable_listener_, this); |
110 | 0 | size_t size = std::min(suggested_size, pipe->wanted_data_); |
111 | 0 | CHECK_GT(size, 0); |
112 | 0 | return pipe->env()->allocate_managed_buffer(size); |
113 | 0 | } |
114 | | |
115 | | void StreamPipe::ReadableListener::OnStreamRead(ssize_t nread, |
116 | 0 | const uv_buf_t& buf_) { |
117 | 0 | StreamPipe* pipe = ContainerOf(&StreamPipe::readable_listener_, this); |
118 | 0 | std::unique_ptr<BackingStore> bs = pipe->env()->release_managed_buffer(buf_); |
119 | 0 | if (nread < 0) { |
120 | | // EOF or error; stop reading and pass the error to the previous listener |
121 | | // (which might end up in JS). |
122 | 0 | pipe->is_eof_ = true; |
123 | | // Cache `sink()` here because the previous listener might do things |
124 | | // that eventually lead to an `Unpipe()` call. |
125 | 0 | StreamBase* sink = pipe->sink(); |
126 | 0 | stream()->ReadStop(); |
127 | 0 | CHECK_NOT_NULL(previous_listener_); |
128 | 0 | previous_listener_->OnStreamRead(nread, uv_buf_init(nullptr, 0)); |
129 | | // If we’re not writing, close now. Otherwise, we’ll do that in |
130 | | // `OnStreamAfterWrite()`. |
131 | 0 | if (pipe->pending_writes_ == 0) { |
132 | 0 | sink->Shutdown(); |
133 | 0 | pipe->Unpipe(); |
134 | 0 | } |
135 | 0 | return; |
136 | 0 | } |
137 | | |
138 | 0 | pipe->ProcessData(nread, std::move(bs)); |
139 | 0 | } |
140 | | |
141 | | void StreamPipe::ProcessData(size_t nread, |
142 | 0 | std::unique_ptr<BackingStore> bs) { |
143 | 0 | CHECK(uses_wants_write_ || pending_writes_ == 0); |
144 | 0 | uv_buf_t buffer = uv_buf_init(static_cast<char*>(bs->Data()), nread); |
145 | 0 | StreamWriteResult res = sink()->Write(&buffer, 1); |
146 | 0 | pending_writes_++; |
147 | 0 | if (!res.async) { |
148 | 0 | writable_listener_.OnStreamAfterWrite(nullptr, res.err); |
149 | 0 | } else { |
150 | 0 | is_reading_ = false; |
151 | 0 | res.wrap->SetBackingStore(std::move(bs)); |
152 | 0 | if (source() != nullptr) |
153 | 0 | source()->ReadStop(); |
154 | 0 | } |
155 | 0 | } |
156 | | |
157 | | void StreamPipe::WritableListener::OnStreamAfterWrite(WriteWrap* w, |
158 | 0 | int status) { |
159 | 0 | StreamPipe* pipe = ContainerOf(&StreamPipe::writable_listener_, this); |
160 | 0 | pipe->pending_writes_--; |
161 | 0 | if (pipe->is_closed_) { |
162 | 0 | if (pipe->pending_writes_ == 0) { |
163 | 0 | Environment* env = pipe->env(); |
164 | 0 | HandleScope handle_scope(env->isolate()); |
165 | 0 | Context::Scope context_scope(env->context()); |
166 | 0 | if (pipe->MakeCallback(env->oncomplete_string(), 0, nullptr).IsEmpty()) |
167 | 0 | return; |
168 | 0 | stream()->RemoveStreamListener(this); |
169 | 0 | } |
170 | 0 | return; |
171 | 0 | } |
172 | | |
173 | 0 | if (pipe->is_eof_) { |
174 | 0 | HandleScope handle_scope(pipe->env()->isolate()); |
175 | 0 | InternalCallbackScope callback_scope(pipe, |
176 | 0 | InternalCallbackScope::kSkipTaskQueues); |
177 | 0 | pipe->sink()->Shutdown(); |
178 | 0 | pipe->Unpipe(); |
179 | 0 | return; |
180 | 0 | } |
181 | | |
182 | 0 | if (status != 0) { |
183 | 0 | CHECK_NOT_NULL(previous_listener_); |
184 | 0 | StreamListener* prev = previous_listener_; |
185 | 0 | pipe->Unpipe(); |
186 | 0 | prev->OnStreamAfterWrite(w, status); |
187 | 0 | return; |
188 | 0 | } |
189 | | |
190 | 0 | if (!pipe->uses_wants_write_) { |
191 | 0 | OnStreamWantsWrite(65536); |
192 | 0 | } |
193 | 0 | } |
194 | | |
195 | | void StreamPipe::WritableListener::OnStreamAfterShutdown(ShutdownWrap* w, |
196 | 0 | int status) { |
197 | 0 | StreamPipe* pipe = ContainerOf(&StreamPipe::writable_listener_, this); |
198 | 0 | CHECK_NOT_NULL(previous_listener_); |
199 | 0 | StreamListener* prev = previous_listener_; |
200 | 0 | pipe->Unpipe(); |
201 | 0 | prev->OnStreamAfterShutdown(w, status); |
202 | 0 | } |
203 | | |
204 | 0 | void StreamPipe::ReadableListener::OnStreamDestroy() { |
205 | 0 | StreamPipe* pipe = ContainerOf(&StreamPipe::readable_listener_, this); |
206 | 0 | pipe->source_destroyed_ = true; |
207 | 0 | if (!pipe->is_eof_) { |
208 | 0 | OnStreamRead(UV_EPIPE, uv_buf_init(nullptr, 0)); |
209 | 0 | } |
210 | 0 | } |
211 | | |
212 | 0 | void StreamPipe::WritableListener::OnStreamDestroy() { |
213 | 0 | StreamPipe* pipe = ContainerOf(&StreamPipe::writable_listener_, this); |
214 | 0 | pipe->sink_destroyed_ = true; |
215 | 0 | pipe->is_eof_ = true; |
216 | 0 | pipe->pending_writes_ = 0; |
217 | 0 | pipe->Unpipe(); |
218 | 0 | } |
219 | | |
220 | 0 | void StreamPipe::WritableListener::OnStreamWantsWrite(size_t suggested_size) { |
221 | 0 | StreamPipe* pipe = ContainerOf(&StreamPipe::writable_listener_, this); |
222 | 0 | pipe->wanted_data_ = suggested_size; |
223 | 0 | if (pipe->is_reading_ || pipe->is_closed_) |
224 | 0 | return; |
225 | 0 | HandleScope handle_scope(pipe->env()->isolate()); |
226 | 0 | InternalCallbackScope callback_scope(pipe, |
227 | 0 | InternalCallbackScope::kSkipTaskQueues); |
228 | 0 | pipe->is_reading_ = true; |
229 | 0 | pipe->source()->ReadStart(); |
230 | 0 | } |
231 | | |
232 | 0 | uv_buf_t StreamPipe::WritableListener::OnStreamAlloc(size_t suggested_size) { |
233 | 0 | CHECK_NOT_NULL(previous_listener_); |
234 | 0 | return previous_listener_->OnStreamAlloc(suggested_size); |
235 | 0 | } |
236 | | |
237 | | void StreamPipe::WritableListener::OnStreamRead(ssize_t nread, |
238 | 0 | const uv_buf_t& buf) { |
239 | 0 | CHECK_NOT_NULL(previous_listener_); |
240 | 0 | return previous_listener_->OnStreamRead(nread, buf); |
241 | 0 | } |
242 | | |
243 | | Maybe<StreamPipe*> StreamPipe::New(StreamBase* source, |
244 | | StreamBase* sink, |
245 | 0 | Local<Object> obj) { |
246 | 0 | std::unique_ptr<StreamPipe> stream_pipe(new StreamPipe(source, sink, obj)); |
247 | | |
248 | | // Set up links between this object and the source/sink objects. |
249 | | // In particular, this makes sure that they are garbage collected as a group, |
250 | | // if that applies to the given streams (for example, Http2Streams use |
251 | | // weak references). |
252 | 0 | Environment* env = source->stream_env(); |
253 | 0 | if (obj->Set(env->context(), env->source_string(), source->GetObject()) |
254 | 0 | .IsNothing()) { |
255 | 0 | return Nothing<StreamPipe*>(); |
256 | 0 | } |
257 | 0 | if (source->GetObject() |
258 | 0 | ->Set(env->context(), env->pipe_target_string(), obj) |
259 | 0 | .IsNothing()) { |
260 | 0 | return Nothing<StreamPipe*>(); |
261 | 0 | } |
262 | 0 | if (obj->Set(env->context(), env->sink_string(), sink->GetObject()) |
263 | 0 | .IsNothing()) { |
264 | 0 | return Nothing<StreamPipe*>(); |
265 | 0 | } |
266 | 0 | if (sink->GetObject() |
267 | 0 | ->Set(env->context(), env->pipe_source_string(), obj) |
268 | 0 | .IsNothing()) { |
269 | 0 | return Nothing<StreamPipe*>(); |
270 | 0 | } |
271 | | |
272 | 0 | return Just(stream_pipe.release()); |
273 | 0 | } |
274 | | |
275 | 0 | void StreamPipe::New(const FunctionCallbackInfo<Value>& args) { |
276 | 0 | CHECK(args.IsConstructCall()); |
277 | 0 | CHECK(args[0]->IsObject()); |
278 | 0 | CHECK(args[1]->IsObject()); |
279 | 0 | StreamBase* source = StreamBase::FromObject(args[0].As<Object>()); |
280 | 0 | StreamBase* sink = StreamBase::FromObject(args[1].As<Object>()); |
281 | |
|
282 | 0 | if (StreamPipe::New(source, sink, args.This()).IsNothing()) return; |
283 | 0 | } |
284 | | |
285 | 0 | void StreamPipe::Start(const FunctionCallbackInfo<Value>& args) { |
286 | 0 | StreamPipe* pipe; |
287 | 0 | ASSIGN_OR_RETURN_UNWRAP(&pipe, args.Holder()); |
288 | 0 | pipe->is_closed_ = false; |
289 | 0 | pipe->writable_listener_.OnStreamWantsWrite(65536); |
290 | 0 | } |
291 | | |
292 | 0 | void StreamPipe::Unpipe(const FunctionCallbackInfo<Value>& args) { |
293 | 0 | StreamPipe* pipe; |
294 | 0 | ASSIGN_OR_RETURN_UNWRAP(&pipe, args.Holder()); |
295 | 0 | pipe->Unpipe(); |
296 | 0 | } |
297 | | |
298 | 0 | void StreamPipe::IsClosed(const FunctionCallbackInfo<Value>& args) { |
299 | 0 | StreamPipe* pipe; |
300 | 0 | ASSIGN_OR_RETURN_UNWRAP(&pipe, args.Holder()); |
301 | 0 | args.GetReturnValue().Set(pipe->is_closed_); |
302 | 0 | } |
303 | | |
304 | 0 | void StreamPipe::PendingWrites(const FunctionCallbackInfo<Value>& args) { |
305 | 0 | StreamPipe* pipe; |
306 | 0 | ASSIGN_OR_RETURN_UNWRAP(&pipe, args.Holder()); |
307 | 0 | args.GetReturnValue().Set(pipe->pending_writes_); |
308 | 0 | } |
309 | | |
310 | | namespace { |
311 | | |
312 | | void InitializeStreamPipe(Local<Object> target, |
313 | | Local<Value> unused, |
314 | | Local<Context> context, |
315 | 0 | void* priv) { |
316 | 0 | Environment* env = Environment::GetCurrent(context); |
317 | 0 | Isolate* isolate = env->isolate(); |
318 | | |
319 | | // Create FunctionTemplate for FileHandle::CloseReq |
320 | 0 | Local<FunctionTemplate> pipe = NewFunctionTemplate(isolate, StreamPipe::New); |
321 | 0 | SetProtoMethod(isolate, pipe, "unpipe", StreamPipe::Unpipe); |
322 | 0 | SetProtoMethod(isolate, pipe, "start", StreamPipe::Start); |
323 | 0 | SetProtoMethod(isolate, pipe, "isClosed", StreamPipe::IsClosed); |
324 | 0 | SetProtoMethod(isolate, pipe, "pendingWrites", StreamPipe::PendingWrites); |
325 | 0 | pipe->Inherit(AsyncWrap::GetConstructorTemplate(env)); |
326 | 0 | pipe->InstanceTemplate()->SetInternalFieldCount( |
327 | 0 | StreamPipe::kInternalFieldCount); |
328 | 0 | SetConstructorFunction(context, target, "StreamPipe", pipe); |
329 | 0 | } |
330 | | |
331 | | } // anonymous namespace |
332 | | |
333 | | } // namespace node |
334 | | |
335 | | NODE_BINDING_CONTEXT_AWARE_INTERNAL(stream_pipe, node::InitializeStreamPipe) |