2016-03-16 23:21:11 +01:00
|
|
|
'use strict';
|
|
|
|
// http://groups.google.com/group/nodejs/browse_thread/thread/f66cd3c960406919
|
|
|
|
const common = require('../common');
|
2025-01-22 14:19:38 -08:00
|
|
|
|
|
|
|
if (!common.hasCrypto) {
|
2016-05-11 15:34:52 -04:00
|
|
|
common.skip('missing crypto');
|
2025-01-22 14:19:38 -08:00
|
|
|
}
|
2017-07-01 02:29:09 +03:00
|
|
|
|
2025-01-22 14:19:38 -08:00
|
|
|
const fs = require('fs');
|
2017-07-01 02:29:09 +03:00
|
|
|
const assert = require('assert');
|
2016-03-16 23:21:11 +01:00
|
|
|
|
|
|
|
if (process.argv[2] === 'request') {
|
|
|
|
const http = require('http');
|
|
|
|
const options = {
|
2016-05-29 03:06:56 -04:00
|
|
|
port: +process.argv[3],
|
2016-05-04 22:20:27 -07:00
|
|
|
path: '/'
|
2016-03-16 23:21:11 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
http.get(options, (res) => {
|
|
|
|
res.pipe(process.stdout);
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (process.argv[2] === 'shasum') {
|
|
|
|
const crypto = require('crypto');
|
|
|
|
const shasum = crypto.createHash('sha1');
|
|
|
|
process.stdin.on('data', (d) => {
|
|
|
|
shasum.update(d);
|
|
|
|
});
|
|
|
|
|
|
|
|
process.stdin.on('close', () => {
|
|
|
|
process.stdout.write(shasum.digest('hex'));
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const http = require('http');
|
|
|
|
const cp = require('child_process');
|
|
|
|
|
2017-12-24 22:38:11 -08:00
|
|
|
const tmpdir = require('../common/tmpdir');
|
|
|
|
|
2023-08-15 22:45:34 +09:00
|
|
|
const filename = tmpdir.resolve('big');
|
2016-05-29 03:06:56 -04:00
|
|
|
let server;
|
2016-03-16 23:21:11 +01:00
|
|
|
|
|
|
|
function executeRequest(cb) {
|
2024-09-22 15:03:30 +02:00
|
|
|
// The execPath might contain chars that should be escaped in a shell context.
|
|
|
|
// On non-Windows, we can pass the path via the env; `"` is not a valid char on
|
|
|
|
// Windows, so we can simply pass the path.
|
|
|
|
const node = `"${common.isWindows ? process.execPath : '$NODE'}"`;
|
|
|
|
const file = `"${common.isWindows ? __filename : '$FILE'}"`;
|
|
|
|
const env = common.isWindows ? process.env : { ...process.env, NODE: process.execPath, FILE: __filename };
|
|
|
|
cp.exec([node,
|
|
|
|
file,
|
2016-03-16 23:21:11 +01:00
|
|
|
'request',
|
2016-05-29 03:06:56 -04:00
|
|
|
server.address().port,
|
2016-03-16 23:21:11 +01:00
|
|
|
'|',
|
2024-09-22 15:03:30 +02:00
|
|
|
node,
|
|
|
|
file,
|
2016-03-16 23:21:11 +01:00
|
|
|
'shasum' ].join(' '),
|
2024-09-22 15:03:30 +02:00
|
|
|
{ env },
|
2016-03-16 23:21:11 +01:00
|
|
|
(err, stdout, stderr) => {
|
2019-04-06 23:30:38 +02:00
|
|
|
if (stderr.trim() !== '') {
|
|
|
|
console.log(stderr);
|
|
|
|
}
|
2016-12-30 10:54:01 -05:00
|
|
|
assert.ifError(err);
|
2018-10-12 11:46:14 -07:00
|
|
|
assert.strictEqual(stdout.slice(0, 40),
|
|
|
|
'8c206a1a87599f532ce68675536f0b1546900d7a');
|
2016-03-16 23:21:11 +01:00
|
|
|
cb();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-24 22:38:11 -08:00
|
|
|
tmpdir.refresh();
|
2016-03-16 23:21:11 +01:00
|
|
|
|
2025-01-22 14:19:38 -08:00
|
|
|
|
|
|
|
// Create a zero-filled file.
|
|
|
|
const fd = fs.openSync(filename, 'w');
|
|
|
|
fs.ftruncateSync(fd, 10 * 1024 * 1024);
|
|
|
|
fs.closeSync(fd);
|
2016-03-16 23:21:11 +01:00
|
|
|
|
2018-10-10 11:24:51 -07:00
|
|
|
server = http.createServer(function(req, res) {
|
|
|
|
res.writeHead(200);
|
2016-03-16 23:21:11 +01:00
|
|
|
|
2018-10-10 11:24:51 -07:00
|
|
|
// Create the subprocess
|
|
|
|
const cat = cp.spawn('cat', [filename]);
|
2016-03-16 23:21:11 +01:00
|
|
|
|
2018-10-10 11:24:51 -07:00
|
|
|
// Stream the data through to the response as binary chunks
|
|
|
|
cat.stdout.on('data', (data) => {
|
|
|
|
res.write(data);
|
|
|
|
});
|
2016-03-16 23:21:11 +01:00
|
|
|
|
2018-10-10 11:24:51 -07:00
|
|
|
cat.stdout.on('end', () => res.end());
|
2016-03-16 23:21:11 +01:00
|
|
|
|
2018-10-10 11:24:51 -07:00
|
|
|
// End the response on exit (and log errors)
|
|
|
|
cat.on('exit', (code) => {
|
|
|
|
if (code !== 0) {
|
|
|
|
console.error(`subprocess exited with code ${code}`);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2016-03-16 23:21:11 +01:00
|
|
|
});
|
|
|
|
|
2018-10-10 11:24:51 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
server.listen(0, () => {
|
|
|
|
executeRequest(() => server.close());
|
2016-03-16 23:21:11 +01:00
|
|
|
});
|