• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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// See test/simple/test-sendfd.js for a complete description of what this
23// script is doing and how it fits into the test as a whole.
24
25const net = require('net');
26
27var receivedData = [];
28var receivedFDs = [];
29var numSentMessages = 0;
30
31function processData(s) {
32  if (receivedData.length == 0 || receivedFDs.length == 0) {
33    return;
34  }
35
36  var fd = receivedFDs.shift();
37  var d = receivedData.shift();
38
39  // Augment our received object before sending it back across the pipe.
40  d.pid = process.pid;
41
42  // Create a stream around the FD that we received and send a serialized
43  // version of our modified object back. Clean up when we're done.
44  var pipeStream = new net.Stream(fd);
45
46  var drainFunc = function() {
47    pipeStream.destroy();
48
49    if (++numSentMessages == 2) {
50      s.destroy();
51    }
52  };
53
54  pipeStream.on('drain', drainFunc);
55  pipeStream.resume();
56
57  if (pipeStream.write(JSON.stringify(d) + '\n')) {
58    drainFunc();
59  }
60}
61
62// Create a UNIX socket to the path defined by argv[2] and read a file
63// descriptor and misc data from it.
64var s = new net.Stream();
65s.on('fd', function(fd) {
66  receivedFDs.unshift(fd);
67  processData(s);
68});
69s.on('data', function(data) {
70  data.toString('utf8').trim().split('\n').forEach(function(d) {
71    receivedData.unshift(JSON.parse(d));
72  });
73  processData(s);
74});
75s.connect(process.argv[2]);
76
77// vim:ts=2 sw=2 et
78