keeweb/app/scripts/comp/browser/transport.js

94 lines
3.6 KiB
JavaScript
Raw Normal View History

2019-09-15 14:16:32 +02:00
import { Launcher } from 'comp/launcher';
import { Logger } from 'util/logger';
2019-09-18 07:08:23 +02:00
import { noop } from 'util/fn';
2015-12-12 09:53:50 +01:00
2017-01-31 07:50:28 +01:00
const logger = new Logger('transport');
2015-11-13 20:56:22 +01:00
2017-01-31 07:50:28 +01:00
const Transport = {
2019-08-18 10:17:09 +02:00
httpGet(config) {
2017-01-31 07:50:28 +01:00
let tmpFile;
const fs = Launcher.req('fs');
2015-11-13 20:56:22 +01:00
if (config.file) {
tmpFile = Launcher.getTempPath(config.file);
if (fs.existsSync(tmpFile)) {
try {
if (config.cache && fs.statSync(tmpFile).size > 0) {
2015-12-12 09:53:50 +01:00
logger.info('File already downloaded ' + config.url);
2015-11-13 20:56:22 +01:00
return config.success(tmpFile);
} else {
fs.unlinkSync(tmpFile);
}
} catch (e) {
2019-09-18 07:08:23 +02:00
fs.unlink(tmpFile, noop);
2015-11-13 20:56:22 +01:00
}
}
}
2017-01-31 07:50:28 +01:00
const proto = config.url.split(':')[0];
2015-12-12 09:53:50 +01:00
logger.info('GET ' + config.url);
2017-01-31 07:50:28 +01:00
const opts = Launcher.req('url').parse(config.url);
2015-11-14 17:58:34 +01:00
opts.headers = { 'User-Agent': navigator.userAgent };
2020-06-01 16:53:51 +02:00
Launcher.resolveProxy(config.url, (proxy) => {
2019-08-16 23:05:39 +02:00
logger.info(
'Request to ' +
config.url +
' ' +
(proxy ? 'using proxy ' + proxy.host + ':' + proxy.port : 'without proxy')
);
if (proxy) {
opts.headers.Host = opts.host;
opts.host = proxy.host;
opts.port = proxy.port;
opts.path = config.url;
}
2019-08-16 23:05:39 +02:00
Launcher.req(proto)
2020-06-01 16:53:51 +02:00
.get(opts, (res) => {
2019-08-16 23:05:39 +02:00
logger.info('Response from ' + config.url + ': ' + res.statusCode);
if (res.statusCode === 200) {
if (config.file) {
const file = fs.createWriteStream(tmpFile);
res.pipe(file);
file.on('finish', () => {
file.close(() => {
config.success(tmpFile);
});
});
2020-06-01 16:53:51 +02:00
file.on('error', (err) => {
2019-08-16 23:05:39 +02:00
config.error(err);
});
} else {
let data = [];
2020-06-01 16:53:51 +02:00
res.on('data', (chunk) => {
2019-08-16 23:05:39 +02:00
data.push(chunk);
});
res.on('end', () => {
data = window.Buffer.concat(data);
if (config.utf8) {
data = data.toString('utf8');
}
config.success(data);
});
}
} else if (res.headers.location && [301, 302].indexOf(res.statusCode) >= 0) {
if (config.noRedirect) {
return config.error('Too many redirects');
}
config.url = res.headers.location;
config.noRedirect = true;
Transport.httpGet(config);
} else {
2019-08-16 23:05:39 +02:00
config.error('HTTP status ' + res.statusCode);
}
2019-08-16 23:05:39 +02:00
})
2020-06-01 16:53:51 +02:00
.on('error', (e) => {
2019-08-16 23:05:39 +02:00
logger.error('Cannot GET ' + config.url, e);
if (tmpFile) {
2019-09-18 07:08:23 +02:00
fs.unlink(tmpFile, noop);
}
2019-08-16 23:05:39 +02:00
config.error(e);
});
2015-11-13 20:56:22 +01:00
});
}
};
2019-09-15 14:16:32 +02:00
export { Transport };