• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2// http://groups.google.com/group/nodejs/browse_thread/thread/f66cd3c960406919
3const common = require('../common');
4if (!common.hasCrypto)
5  common.skip('missing crypto');
6
7const assert = require('assert');
8
9if (process.argv[2] === 'request') {
10  const http = require('http');
11  const options = {
12    port: +process.argv[3],
13    path: '/'
14  };
15
16  http.get(options, (res) => {
17    res.pipe(process.stdout);
18  });
19
20  return;
21}
22
23if (process.argv[2] === 'shasum') {
24  const crypto = require('crypto');
25  const shasum = crypto.createHash('sha1');
26  process.stdin.on('data', (d) => {
27    shasum.update(d);
28  });
29
30  process.stdin.on('close', () => {
31    process.stdout.write(shasum.digest('hex'));
32  });
33
34  return;
35}
36
37const http = require('http');
38const cp = require('child_process');
39
40const tmpdir = require('../common/tmpdir');
41
42const filename = require('path').join(tmpdir.path, 'big');
43let server;
44
45function executeRequest(cb) {
46  cp.exec([`"${process.execPath}"`,
47           `"${__filename}"`,
48           'request',
49           server.address().port,
50           '|',
51           `"${process.execPath}"`,
52           `"${__filename}"`,
53           'shasum' ].join(' '),
54          (err, stdout, stderr) => {
55            if (stderr.trim() !== '') {
56              console.log(stderr);
57            }
58            assert.ifError(err);
59            assert.strictEqual(stdout.slice(0, 40),
60                               '8c206a1a87599f532ce68675536f0b1546900d7a');
61            cb();
62          }
63  );
64}
65
66
67tmpdir.refresh();
68
69common.createZeroFilledFile(filename);
70
71server = http.createServer(function(req, res) {
72  res.writeHead(200);
73
74  // Create the subprocess
75  const cat = cp.spawn('cat', [filename]);
76
77  // Stream the data through to the response as binary chunks
78  cat.stdout.on('data', (data) => {
79    res.write(data);
80  });
81
82  cat.stdout.on('end', () => res.end());
83
84  // End the response on exit (and log errors)
85  cat.on('exit', (code) => {
86    if (code !== 0) {
87      console.error(`subprocess exited with code ${code}`);
88      process.exit(1);
89    }
90  });
91
92});
93
94server.listen(0, () => {
95  executeRequest(() => server.close());
96});
97