@cliz/inlets
Version:
Cloud Native Tunnel
74 lines (73 loc) • 3.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const cli_1 = require("@cliz/cli");
const config_1 = require("../config");
const server_1 = require("../core/server");
exports.default = (0, cli_1.defineSubCommand)(createCommand => {
return createCommand('inlets server')
.option('-d, --domain', 'Domain for server', { required: true })
.option('-p, --port <port>', 'Port for server (default 8080)', { default: 8080 })
.option('--tcp-port <tcpPort>', 'TCP Port for server (default 8443)', { default: config_1.default.defaultTCPPort })
.option('-s, --secure', 'Server with https, only for url', {
default: config_1.default.defaultSecure,
})
.option('-t, --token <token>', 'Token for authentication', { required: false })
.option('-c, --config <config>', 'Config file', {
default: cli_1.api.path.configfile('inlets.yml'),
})
.action(async ({ args, options: _options, program }) => {
const options = _options;
let config;
if (await cli_1.api.fs.isExist(options.config)) {
config = await cli_1.api.fs.yml.load(options.config);
}
if (!options.token && !(config === null || config === void 0 ? void 0 : config.token) && !(config === null || config === void 0 ? void 0 : config.clients)) {
throw new Error(`token or clients is required in ${options.configFilePath}`);
}
const token = async (authType, clientId, _options) => {
if (authType === 'credentials') {
const client = config === null || config === void 0 ? void 0 : config.clients.find(c => c.clientId === clientId);
return {
authType,
token: client === null || client === void 0 ? void 0 : client.clientSecret,
};
}
if (authType === 'public') {
if ((_options === null || _options === void 0 ? void 0 : _options.type) !== 'http') {
throw new Error(`auth type publi is only allow by http type`);
}
return {
authType,
token: 'public',
};
}
const token = options.token || (config === null || config === void 0 ? void 0 : config.token);
if (!token) {
throw new Error(`unsupport auth type: ${authType}`);
}
return {
authType,
token: token,
config: {
version: program.getVersion(),
notification: config === null || config === void 0 ? void 0 : config.notification,
},
};
};
const get = (name) => {
return options[name] || (config === null || config === void 0 ? void 0 : config[name]);
};
const _opt = {
...args,
...options,
domain: get('domain'),
port: get('port'),
tcpPort: get('tcpPort'),
secure: get('secure'),
version: program.getVersion(),
token,
notification: config === null || config === void 0 ? void 0 : config.notification,
};
return (0, server_1.server)(_opt);
});
});