1 // Copyright Joyent, Inc. and other Node contributors.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 #include "pipe_wrap.h"
23
24 #include "async_wrap.h"
25 #include "connection_wrap.h"
26 #include "env-inl.h"
27 #include "handle_wrap.h"
28 #include "node.h"
29 #include "node_buffer.h"
30 #include "connect_wrap.h"
31 #include "stream_base-inl.h"
32 #include "stream_wrap.h"
33 #include "util-inl.h"
34
35 namespace node {
36
37 using v8::Context;
38 using v8::EscapableHandleScope;
39 using v8::Function;
40 using v8::FunctionCallbackInfo;
41 using v8::FunctionTemplate;
42 using v8::Int32;
43 using v8::Local;
44 using v8::MaybeLocal;
45 using v8::Object;
46 using v8::Value;
47
Instantiate(Environment * env,AsyncWrap * parent,PipeWrap::SocketType type)48 MaybeLocal<Object> PipeWrap::Instantiate(Environment* env,
49 AsyncWrap* parent,
50 PipeWrap::SocketType type) {
51 EscapableHandleScope handle_scope(env->isolate());
52 AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(parent);
53 CHECK_EQ(false, env->pipe_constructor_template().IsEmpty());
54 Local<Function> constructor = env->pipe_constructor_template()
55 ->GetFunction(env->context())
56 .ToLocalChecked();
57 CHECK_EQ(false, constructor.IsEmpty());
58 Local<Value> type_value = Int32::New(env->isolate(), type);
59 return handle_scope.EscapeMaybe(
60 constructor->NewInstance(env->context(), 1, &type_value));
61 }
62
63
Initialize(Local<Object> target,Local<Value> unused,Local<Context> context,void * priv)64 void PipeWrap::Initialize(Local<Object> target,
65 Local<Value> unused,
66 Local<Context> context,
67 void* priv) {
68 Environment* env = Environment::GetCurrent(context);
69
70 Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
71 t->InstanceTemplate()
72 ->SetInternalFieldCount(StreamBase::kInternalFieldCount);
73
74 t->Inherit(LibuvStreamWrap::GetConstructorTemplate(env));
75
76 env->SetProtoMethod(t, "bind", Bind);
77 env->SetProtoMethod(t, "listen", Listen);
78 env->SetProtoMethod(t, "connect", Connect);
79 env->SetProtoMethod(t, "open", Open);
80
81 #ifdef _WIN32
82 env->SetProtoMethod(t, "setPendingInstances", SetPendingInstances);
83 #endif
84
85 env->SetProtoMethod(t, "fchmod", Fchmod);
86
87 env->SetConstructorFunction(target, "Pipe", t);
88 env->set_pipe_constructor_template(t);
89
90 // Create FunctionTemplate for PipeConnectWrap.
91 auto cwt = BaseObject::MakeLazilyInitializedJSTemplate(env);
92 cwt->Inherit(AsyncWrap::GetConstructorTemplate(env));
93 env->SetConstructorFunction(target, "PipeConnectWrap", cwt);
94
95 // Define constants
96 Local<Object> constants = Object::New(env->isolate());
97 NODE_DEFINE_CONSTANT(constants, SOCKET);
98 NODE_DEFINE_CONSTANT(constants, SERVER);
99 NODE_DEFINE_CONSTANT(constants, IPC);
100 NODE_DEFINE_CONSTANT(constants, UV_READABLE);
101 NODE_DEFINE_CONSTANT(constants, UV_WRITABLE);
102 target->Set(context,
103 env->constants_string(),
104 constants).Check();
105 }
106
107
New(const FunctionCallbackInfo<Value> & args)108 void PipeWrap::New(const FunctionCallbackInfo<Value>& args) {
109 // This constructor should not be exposed to public javascript.
110 // Therefore we assert that we are not trying to call this as a
111 // normal function.
112 CHECK(args.IsConstructCall());
113 CHECK(args[0]->IsInt32());
114 Environment* env = Environment::GetCurrent(args);
115
116 int type_value = args[0].As<Int32>()->Value();
117 PipeWrap::SocketType type = static_cast<PipeWrap::SocketType>(type_value);
118
119 bool ipc;
120 ProviderType provider;
121 switch (type) {
122 case SOCKET:
123 provider = PROVIDER_PIPEWRAP;
124 ipc = false;
125 break;
126 case SERVER:
127 provider = PROVIDER_PIPESERVERWRAP;
128 ipc = false;
129 break;
130 case IPC:
131 provider = PROVIDER_PIPEWRAP;
132 ipc = true;
133 break;
134 default:
135 UNREACHABLE();
136 }
137
138 new PipeWrap(env, args.This(), provider, ipc);
139 }
140
141
PipeWrap(Environment * env,Local<Object> object,ProviderType provider,bool ipc)142 PipeWrap::PipeWrap(Environment* env,
143 Local<Object> object,
144 ProviderType provider,
145 bool ipc)
146 : ConnectionWrap(env, object, provider) {
147 int r = uv_pipe_init(env->event_loop(), &handle_, ipc);
148 CHECK_EQ(r, 0); // How do we proxy this error up to javascript?
149 // Suggestion: uv_pipe_init() returns void.
150 }
151
152
Bind(const FunctionCallbackInfo<Value> & args)153 void PipeWrap::Bind(const FunctionCallbackInfo<Value>& args) {
154 PipeWrap* wrap;
155 ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
156 node::Utf8Value name(args.GetIsolate(), args[0]);
157 int err = uv_pipe_bind(&wrap->handle_, *name);
158 args.GetReturnValue().Set(err);
159 }
160
161
162 #ifdef _WIN32
SetPendingInstances(const FunctionCallbackInfo<Value> & args)163 void PipeWrap::SetPendingInstances(const FunctionCallbackInfo<Value>& args) {
164 PipeWrap* wrap;
165 ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
166 CHECK(args[0]->IsInt32());
167 int instances = args[0].As<Int32>()->Value();
168 uv_pipe_pending_instances(&wrap->handle_, instances);
169 }
170 #endif
171
172
Fchmod(const v8::FunctionCallbackInfo<v8::Value> & args)173 void PipeWrap::Fchmod(const v8::FunctionCallbackInfo<v8::Value>& args) {
174 PipeWrap* wrap;
175 ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
176 CHECK(args[0]->IsInt32());
177 int mode = args[0].As<Int32>()->Value();
178 int err = uv_pipe_chmod(&wrap->handle_, mode);
179 args.GetReturnValue().Set(err);
180 }
181
182
Listen(const FunctionCallbackInfo<Value> & args)183 void PipeWrap::Listen(const FunctionCallbackInfo<Value>& args) {
184 PipeWrap* wrap;
185 ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
186 Environment* env = wrap->env();
187 int backlog;
188 if (!args[0]->Int32Value(env->context()).To(&backlog)) return;
189 int err = uv_listen(reinterpret_cast<uv_stream_t*>(&wrap->handle_),
190 backlog,
191 OnConnection);
192 args.GetReturnValue().Set(err);
193 }
194
195
Open(const FunctionCallbackInfo<Value> & args)196 void PipeWrap::Open(const FunctionCallbackInfo<Value>& args) {
197 Environment* env = Environment::GetCurrent(args);
198
199 PipeWrap* wrap;
200 ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
201
202 int fd;
203 if (!args[0]->Int32Value(env->context()).To(&fd)) return;
204
205 int err = uv_pipe_open(&wrap->handle_, fd);
206 wrap->set_fd(fd);
207
208 if (err != 0)
209 env->ThrowUVException(err, "uv_pipe_open");
210 }
211
212
Connect(const FunctionCallbackInfo<Value> & args)213 void PipeWrap::Connect(const FunctionCallbackInfo<Value>& args) {
214 Environment* env = Environment::GetCurrent(args);
215
216 PipeWrap* wrap;
217 ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
218
219 CHECK(args[0]->IsObject());
220 CHECK(args[1]->IsString());
221
222 Local<Object> req_wrap_obj = args[0].As<Object>();
223 node::Utf8Value name(env->isolate(), args[1]);
224
225 ConnectWrap* req_wrap =
226 new ConnectWrap(env, req_wrap_obj, AsyncWrap::PROVIDER_PIPECONNECTWRAP);
227 req_wrap->Dispatch(uv_pipe_connect,
228 &wrap->handle_,
229 *name,
230 AfterConnect);
231
232 args.GetReturnValue().Set(0); // uv_pipe_connect() doesn't return errors.
233 }
234
235
236 } // namespace node
237
238 NODE_MODULE_CONTEXT_AWARE_INTERNAL(pipe_wrap, node::PipeWrap::Initialize)
239