telebit.js/lib/cli-common.js

109 lines
3.1 KiB
JavaScript
Raw Normal View History

2018-06-11 20:52:01 +00:00
'use strict';
var common = module.exports;
var path = require('path');
var mkdirp = require('mkdirp');
var os = require('os');
var homedir = os.homedir();
2018-06-12 10:36:37 +00:00
var localshare = '.local/share/telebit';
2018-06-11 20:52:01 +00:00
var localconf = '.config/telebit';
2018-06-14 07:26:32 +00:00
common.pipename = function (config, newApi) {
var _ipc = {
path: (config.sock || common.DEFAULT_SOCK_NAME)
, comment: (/^win/i.test(os.platform()) ? 'windows pipe' : 'unix socket')
, type: (/^win/i.test(os.platform()) ? 'pipe' : 'socket')
};
if ('pipe' === _ipc.type) {
2018-06-14 08:39:34 +00:00
_ipc.path = '\\\\?\\pipe' + _ipc.path.replace(/\//, '\\');
2018-06-11 20:52:01 +00:00
}
2018-06-14 07:26:32 +00:00
if (newApi) {
return _ipc;
}
return _ipc.path;
2018-06-11 20:52:01 +00:00
};
2018-06-14 07:26:32 +00:00
common.DEFAULT_SOCK_NAME = path.join(homedir, localshare, 'var', 'run', 'telebit.sock');
2018-06-11 20:52:01 +00:00
2018-06-16 01:11:02 +00:00
common.parseUrl = function (hostname) {
var url = require('url');
var location = url.parse(hostname);
if (!location.protocol || /\./.test(location.protocol)) {
hostname = 'https://' + hostname;
location = url.parse(hostname);
}
hostname = location.hostname + (location.port ? ':' + location.port : '');
hostname = location.protocol.replace(/https?/, 'https') + '//' + hostname + location.pathname;
return hostname;
};
common.apiDirectory = '_apis/telebit.cloud/index.json';
common.urequest = function (opts, cb) {
2018-06-16 01:47:32 +00:00
var https = require('https');
2018-06-16 01:11:02 +00:00
// request.js behavior:
// encoding: null + json ? unknown
// json => attempt to parse, fail silently
// encoding => buffer.toString(encoding)
// null === encoding => Buffer.concat(buffers)
2018-06-16 01:47:32 +00:00
https.get(opts.url, function (resp) {
2018-06-16 01:11:02 +00:00
var encoding = opts.encoding;
if (null === encoding) {
resp._body = [];
} else {
2018-06-16 01:47:32 +00:00
resp.body = '';
2018-06-16 01:11:02 +00:00
}
if (!resp.headers['content-length'] || 0 === parseInt(resp.headers['content-length'], 10)) {
cb(resp);
}
resp._bodyLength = 0;
resp.on('data', function (chunk) {
if ('string' === typeof resp.body) {
resp.body += chunk.toString(encoding);
} else {
resp._body.push(chunk);
resp._bodyLength += chunk.length;
}
});
resp.on('end', function () {
if ('string' !== typeof resp.body) {
if (1 === resp._body.length) {
resp.body = resp._body[0];
} else {
resp.body = Buffer.concat(resp._body, resp._bodyLength);
}
resp._body = null;
}
if (opts.json && 'string' === typeof resp.body) {
// TODO I would parse based on Content-Type
// but request.js doesn't do that.
try {
resp.body = JSON.parse(resp.body);
} catch(e) {
// ignore
}
}
cb(null, resp, resp.body);
});
}).on('error', function (e) {
cb(e);
});
};
2018-06-11 20:52:01 +00:00
try {
2018-06-12 10:36:37 +00:00
mkdirp.sync(path.join(__dirname, '..', 'var', 'log'));
mkdirp.sync(path.join(__dirname, '..', 'var', 'run'));
mkdirp.sync(path.join(__dirname, '..', 'etc'));
} catch(e) {
console.error(e);
}
try {
mkdirp.sync(path.join(homedir, localshare, 'var', 'log'));
mkdirp.sync(path.join(homedir, localshare, 'var', 'run'));
//mkdirp.sync(path.join(homedir, localshare, 'etc'));
2018-06-11 20:52:01 +00:00
mkdirp.sync(path.join(homedir, localconf));
} catch(e) {
console.error(e);
}