deno.land / std@0.166.0 / node / internal_binding / pipe_wrap.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.// Copyright Joyent, Inc. and other Node contributors.//// Permission is hereby granted, free of charge, to any person obtaining a// copy of this software and associated documentation files (the// "Software"), to deal in the Software without restriction, including// without limitation the rights to use, copy, modify, merge, publish,// distribute, sublicense, and/or sell copies of the Software, and to permit// persons to whom the Software is furnished to do so, subject to the// following conditions://// The above copyright notice and this permission notice shall be included// in all copies or substantial portions of the Software.//// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE// USE OR OTHER DEALINGS IN THE SOFTWARE.
// This module ports:// - https://github.com/nodejs/node/blob/master/src/pipe_wrap.cc// - https://github.com/nodejs/node/blob/master/src/pipe_wrap.h
import { notImplemented } from "../_utils.ts";import { unreachable } from "../../_util/asserts.ts";import { ConnectionWrap } from "./connection_wrap.ts";import { AsyncWrap, providerType } from "./async_wrap.ts";import { LibuvStreamWrap } from "./stream_wrap.ts";import { codeMap } from "./uv.ts";import { delay } from "../../async/mod.ts";import { kStreamBaseField } from "./stream_wrap.ts";import { ceilPowOf2, INITIAL_ACCEPT_BACKOFF_DELAY, MAX_ACCEPT_BACKOFF_DELAY,} from "./_listen.ts";import { isWindows } from "../../_util/os.ts";import { fs } from "./constants.ts";
export enum socketType { SOCKET, SERVER, IPC,}
export class Pipe extends ConnectionWrap { override reading = false; ipc: boolean;
// REF: https://github.com/nodejs/node/blob/master/deps/uv/src/win/pipe.c#L48 #pendingInstances = 4;
#address?: string;
#backlog?: number; #listener!: Deno.Listener; #connections = 0;
#closed = false; #acceptBackoffDelay?: number;
constructor(type: number, conn?: Deno.UnixConn) { let provider: providerType; let ipc: boolean;
switch (type) { case socketType.SOCKET: { provider = providerType.PIPEWRAP; ipc = false;
break; } case socketType.SERVER: { provider = providerType.PIPESERVERWRAP; ipc = false;
break; } case socketType.IPC: { provider = providerType.PIPEWRAP; ipc = true;
break; } default: { unreachable(); } }
super(provider, conn);
this.ipc = ipc;
if (conn && provider === providerType.PIPEWRAP) { const localAddr = conn.localAddr as Deno.UnixAddr; this.#address = localAddr.path; } }
open(_fd: number): number { // REF: https://github.com/denoland/deno/issues/6529 notImplemented("Pipe.prototype.open"); }
/** * Bind to a Unix domain or Windows named pipe. * @param name Unix domain or Windows named pipe the server should listen to. * @return An error status code. */ bind(name: string) { // Deno doesn't currently separate bind from connect. For now we noop under // the assumption we will connect shortly. // REF: https://doc.deno.land/deno/unstable/~/Deno.connect
this.#address = name;
return 0; }
/** * Connect to a Unix domain or Windows named pipe. * @param req A PipeConnectWrap instance. * @param address Unix domain or Windows named pipe the server should connect to. * @return An error status code. */ connect(req: PipeConnectWrap, address: string) { if (isWindows) { // REF: https://github.com/denoland/deno/issues/10244 notImplemented("Pipe.prototype.connect - Windows"); }
const connectOptions: Deno.UnixConnectOptions = { path: address, transport: "unix", };
Deno.connect(connectOptions).then( (conn: Deno.UnixConn) => { const localAddr = conn.localAddr as Deno.UnixAddr;
this.#address = req.address = localAddr.path; this[kStreamBaseField] = conn;
try { this.afterConnect(req, 0); } catch { // swallow callback errors. } }, (e) => { // TODO(cmorten): correct mapping of connection error to status code. let code: number;
if (e instanceof Deno.errors.NotFound) { code = codeMap.get("ENOENT")!; } else if (e instanceof Deno.errors.PermissionDenied) { code = codeMap.get("EACCES")!; } else { code = codeMap.get("ECONNREFUSED")!; }
try { this.afterConnect(req, code); } catch { // swallow callback errors. } }, );
return 0; }
/** * Listen for new connections. * @param backlog The maximum length of the queue of pending connections. * @return An error status code. */ listen(backlog: number): number { if (isWindows) { // REF: https://github.com/denoland/deno/issues/10244 notImplemented("Pipe.prototype.listen - Windows"); }
this.#backlog = isWindows ? this.#pendingInstances : ceilPowOf2(backlog + 1);
const listenOptions = { path: this.#address!, transport: "unix" as const, };
let listener;
try { listener = Deno.listen(listenOptions); } catch (e) { if (e instanceof Deno.errors.AddrInUse) { return codeMap.get("EADDRINUSE")!; } else if (e instanceof Deno.errors.AddrNotAvailable) { return codeMap.get("EADDRNOTAVAIL")!; }
// TODO(cmorten): map errors to appropriate error codes. return codeMap.get("UNKNOWN")!; }
const address = listener.addr as Deno.UnixAddr; this.#address = address.path;
this.#listener = listener; this.#accept();
return 0; }
override ref() { if (this.#listener) { this.#listener.ref(); } }
override unref() { if (this.#listener) { this.#listener.unref(); } }
/** * Set the number of pending pipe instance handles when the pipe server is * waiting for connections. This setting applies to Windows only. * @param instances Number of pending pipe instances. */ setPendingInstances(instances: number) { this.#pendingInstances = instances; }
/** * Alters pipe permissions, allowing it to be accessed from processes run by * different users. Makes the pipe writable or readable by all users. Mode * can be `UV_WRITABLE`, `UV_READABLE` or `UV_WRITABLE | UV_READABLE`. This * function is blocking. * @param mode Pipe permissions mode. * @return An error status code. */ fchmod(mode: number) { if ( mode != constants.UV_READABLE && mode != constants.UV_WRITABLE && mode != (constants.UV_WRITABLE | constants.UV_READABLE) ) { return codeMap.get("EINVAL"); }
let desired_mode = 0;
if (mode & constants.UV_READABLE) { desired_mode |= fs.S_IRUSR | fs.S_IRGRP | fs.S_IROTH; } if (mode & constants.UV_WRITABLE) { desired_mode |= fs.S_IWUSR | fs.S_IWGRP | fs.S_IWOTH; }
// TODO(cmorten): this will incorrectly throw on Windows // REF: https://github.com/denoland/deno/issues/4357 try { Deno.chmodSync(this.#address!, desired_mode); } catch { // TODO(cmorten): map errors to appropriate error codes. return codeMap.get("UNKNOWN")!; }
return 0; }
/** Handle backoff delays following an unsuccessful accept. */ async #acceptBackoff() { // Backoff after transient errors to allow time for the system to // recover, and avoid blocking up the event loop with a continuously // running loop. if (!this.#acceptBackoffDelay) { this.#acceptBackoffDelay = INITIAL_ACCEPT_BACKOFF_DELAY; } else { this.#acceptBackoffDelay *= 2; }
if (this.#acceptBackoffDelay >= MAX_ACCEPT_BACKOFF_DELAY) { this.#acceptBackoffDelay = MAX_ACCEPT_BACKOFF_DELAY; }
await delay(this.#acceptBackoffDelay);
this.#accept(); }
/** Accept new connections. */ async #accept(): Promise<void> { if (this.#closed) { return; }
if (this.#connections > this.#backlog!) { this.#acceptBackoff();
return; }
let connection: Deno.Conn;
try { connection = await this.#listener.accept(); } catch (e) { if (e instanceof Deno.errors.BadResource && this.#closed) { // Listener and server has closed. return; }
try { // TODO(cmorten): map errors to appropriate error codes. this.onconnection!(codeMap.get("UNKNOWN")!, undefined); } catch { // swallow callback errors. }
this.#acceptBackoff();
return; }
// Reset the backoff delay upon successful accept. this.#acceptBackoffDelay = undefined;
const connectionHandle = new Pipe(socketType.SOCKET, connection); this.#connections++;
try { this.onconnection!(0, connectionHandle); } catch { // swallow callback errors. }
return this.#accept(); }
/** Handle server closure. */ override _onClose(): number { this.#closed = true; this.reading = false;
this.#address = undefined;
this.#backlog = undefined; this.#connections = 0; this.#acceptBackoffDelay = undefined;
if (this.provider === providerType.PIPESERVERWRAP) { try { this.#listener.close(); } catch { // listener already closed } }
return LibuvStreamWrap.prototype._onClose.call(this); }}
export class PipeConnectWrap extends AsyncWrap { oncomplete!: ( status: number, handle: ConnectionWrap, req: PipeConnectWrap, readable: boolean, writeable: boolean, ) => void; address!: string;
constructor() { super(providerType.PIPECONNECTWRAP); }}
export enum constants { SOCKET = socketType.SOCKET, SERVER = socketType.SERVER, IPC = socketType.IPC, UV_READABLE = 1, UV_WRITABLE = 2,}
std

Version Info

Tagged at
a year ago