Skip to content

Commit

Permalink
fix: improve automatic task kills
Browse files Browse the repository at this point in the history
  • Loading branch information
marionebl committed Jun 23, 2016
1 parent dd2a2e4 commit ee478d4
Showing 1 changed file with 75 additions and 26 deletions.
101 changes: 75 additions & 26 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const http = require('http');
const os = require('os');
const path = require('path');
const url = require('url');
const util = require('util');

const copyPaste = require('copy-paste');
const chalk = require('chalk');
Expand All @@ -21,6 +22,8 @@ const mime = require('mime');
const portscanner = require('portscanner');
const generate = require('project-name-generator');

const log = util.debuglog('share-cli');

const cli = meow(`
Usage
$ share [file]
Expand All @@ -45,14 +48,17 @@ const stdin = process.stdin;

// Kill process after 5 minutes of inactivity
function resetTimer() {
log('Reseting activitiy timer');
killTimer();
setTimer();
}

function setTimer() {
log('Setting activitiy timer, timing out after 5 minutes');
__timer = setTimeout(() => {
log('Timeout without after 5 minutes without activitiy, killing process');
process.exit(0);
}, 30000);
}, 300000);
}

function killTimer() {
Expand Down Expand Up @@ -130,19 +136,39 @@ function getOpenPort() {
* @param {String} [options.filePath] - Absolute path of file to read
* @param {String} [options.fileName] - Basename of file to read, defaults to path.basename(option.filePath)
*
* @return {File}
* @return {Promise<File>}
*/
function getFile(options) {
const stream = options.isStdin ?
stdin : fs.createReadStream(options.filePath);

const name = options.isStdin ?
options.fileName : path.basename(options.filePath);
return new Promise((resolve, reject) => {
const stream = options.isStdin ?
stdin : fs.createReadStream(options.filePath);

const name = options.isStdin ?
options.fileName : path.basename(options.filePath);

if (options.isStdin) {
return resolve({
stream,
name,
size: null,
ino: null,
mtime: null
});
}

return {
stream,
name
};
fs.stat(options.filePath, (error, stat) => {
if (error) {
return reject(error);
}
resolve({
stream,
name,
size: stat.size,
ino: stat.ino,
mtime: Date.parse(stat.mtime)
});
});
});
}

/**
Expand All @@ -164,22 +190,43 @@ function serve(options) {
.split('/')
.filter(Boolean)[0];

if (id === options.id) {
resetTimer();
const downloadName = file.name || options.id;
response.setHeader('Content-Type', mime.lookup(downloadName));
response.setHeader('Content-Disposition', `attachment; filename=${downloadName}`);
// Only HEAD and GET are allowed
if (['GET', 'HEAD'].indexOf(request.method) === -1) {
response.writeHead(405);
return response.end('Method not Allowed.');
}

file.stream.pipe(response);
if (id !== options.id) {
response.writeHead(404);
return response.end('Not found');
}

resetTimer();
const downloadName = file.name || options.id;
response.setHeader('Content-Type', mime.lookup(downloadName));

if (file.size) {
response.setHeader('Content-Length', file.size);
}

// Do not send a body for HEAD requests
if (request.method === 'HEAD') {
response.setHeader('Connection', 'close');
return response.end();
}

// Kill the process when download completed
file.stream.on('close', () => {
response.setHeader('Content-Disposition', `attachment; filename=${downloadName}`);

file.stream.on('data', () => {
resetTimer();
});

file.stream.pipe(response)
.on('finish', () => {
log('Download completed, killing process');
// Kill the process when download completed
process.exit(0);
});
} else {
response.writeHead(404);
response.end('Not found');
}
});

server.on('error', reject);
Expand Down Expand Up @@ -236,14 +283,16 @@ function main(filePath, args) {
const isStdin = stdin.isTTY !== true && typeof filePath === 'undefined';

// Get a file object
const file = getFile({
const gettingFile = getFile({
isStdin,
filePath,
fileName: args.name
});

const address = serveFile(file);
resolve(address);
gettingFile
.then(serveFile)
.then(resolve)
.catch(reject);
});
}

Expand Down

0 comments on commit ee478d4

Please sign in to comment.