Permalink
Browse files

refactor, remove hooks, config.

  • Loading branch information...
1 parent 247b452 commit d7b14c8bbc17e0cfa2a2ac8cda81226d643cc8ea @chjj committed Jun 19, 2012
Showing with 279 additions and 109 deletions.
  1. +25 −14 README.md
  2. +25 −14 lib/config.js
  3. +57 −0 lib/logger.js
  4. +123 −72 lib/tty.js
  5. +30 −9 static/term.js
  6. +19 −0 static/tty.js
View
@@ -31,6 +31,28 @@ Bellard's vt100 for [jslinux](http://bellard.org/jslinux/).
$ npm install tty.js
```
+## Usage
+
+tty.js is an app, but it's also possible to hook into it programatically
+
+``` js
+var tty = require('tty.js');
+
+var app = tty.createServer({
+ shell: 'bash',
+ users: {
+ foo: 'bar'
+ },
+ port: 8000
+});
+
+app.get('/foo', function(req, res, next) {
+ res.send('bar');
+});
+
+app.listen();
+```
+
## Configuration
Configuration is stored in `~/.tty.js/config.json` or `~/.tty.js` as a single
@@ -52,8 +74,10 @@ JSON file. An example configuration file looks like:
"static": "./static",
"limitGlobal": 10000,
"limitPerUser": 1000,
- "hooks": "./hooks.js",
"cwd": ".",
+ "syncSession": true,
+ "sessionTimeout": 600000,
+ "log": true,
"term": {
"termName": "xterm",
"geometry": [80, 30],
@@ -91,19 +115,6 @@ Usernames and passwords can be plaintext or sha1 hashes.
If tty.js fails to check your terminfo properly, you can force your `TERM`
to `xterm-256color` by setting `"termName": "xterm-256color"` in your config.
-### Example Hooks File
-
-``` js
-var db = require('./db');
-
-module.exports = {
- auth: function(user, pass, next) {
- // Do database auth
- next(null, pass === password);
- }
-};
-```
-
## Security
tty.js currently has https as an option. It also has express' default basic
View
@@ -4,7 +4,8 @@
*/
var path = require('path')
- , fs = require('fs');
+ , fs = require('fs')
+ , logger = require('./logger');
/**
* Default Config
@@ -23,7 +24,9 @@ var schema = {
// static: './static',
// limitGlobal: 10000,
// limitPerUser: 1000,
- // hooks: './hooks.js',
+ // syncSession: true,
+ // sessionTimeout: 10 * 60 * 1000,
+ // log: true,
// cwd: '.',
term: {
// termName: 'xterm',
@@ -157,9 +160,11 @@ function checkConfig(conf) {
conf.term.termName = conf.termName || conf.term.termName;
if (!conf.term.termName) {
// tput -Txterm-256color longname
- conf.term.termName = exists('/usr/share/terminfo/x/xterm+256color')
- ? 'xterm-256color'
- : 'xterm';
+ conf.term.termName =
+ exists('/usr/share/terminfo/x/xterm+256color')
+ || exists('/usr/share/terminfo/x/xterm-256color')
+ ? 'xterm-256color'
+ : 'xterm';
}
conf.termName = conf.term.termName;
@@ -172,9 +177,6 @@ function checkConfig(conf) {
delete conf.users;
}
- // hooks
- conf.hooks = conf.hooks || tryRequire(dir, 'hooks.js');
-
// cwd
if (conf.cwd) {
conf.cwd = path.resolve(dir, conf.cwd);
@@ -194,7 +196,7 @@ function checkLegacy(conf) {
if (conf.auth && conf.auth.username && !conf.auth.disabled) {
conf.users[conf.auth.username] = conf.auth.password;
}
- console.error('`auth` is deprecated, please use `users` instead.');
+ logger('error', '`auth` is deprecated, please use `users` instead.');
}
if (conf.https && conf.https.key) {
@@ -227,13 +229,16 @@ function checkLegacy(conf) {
}
if (conf.hooks) {
- conf.hooks = tryRequire(conf.dir, conf.hooks);
+ out.push(''
+ + '`hooks` is deprecated, please programmatically '
+ + 'hook into your tty.js server instead.');
}
if (out.length) {
- out = out.join('\n');
- console.error(out);
- console.error('Exiting...');
+ out.forEach(function(out) {
+ logger('error', out);
+ });
+ logger('error', 'Exiting.');
process.exit(1);
}
}
@@ -297,12 +302,18 @@ function parseArg() {
function getarg() {
var arg = argv.shift();
- if (arg && arg.indexOf('--') === 0) {
+ if (!arg) return arg;
+ if (arg.indexOf('--') === 0) {
arg = arg.split('=');
if (arg.length > 1) {
argv.unshift(arg.slice(1).join('='));
}
return arg[0];
+ } else if (arg[0] === '-' && arg.length > 2) {
+ argv = arg.substring(1).split('').map(function(ch) {
+ return '-' + ch;
+ }).concat(argv);
+ arg = argv.shift();
}
return arg;
}
View
@@ -0,0 +1,57 @@
+/**
+ * tty.js: logger.js
+ * Copyright (c) 2012, Christopher Jeffrey (MIT License)
+ */
+
+/**
+ * Logger
+ */
+
+var slice = Array.prototype.slice;
+
+var isatty = require('tty').isatty;
+isatty = [isatty(0), isatty(1), isatty(2)];
+
+var levels = {
+ 'log': [34, 'log'],
+ 'error': [41, 'error'],
+ 'warning': [31, 'error'] // 31, 33, 91
+};
+
+function logger(level) {
+ var args = Array.prototype.slice.call(arguments, 1);
+
+ if (typeof args[0] !== 'string') args.unshift('');
+
+ level = levels[level];
+
+ args[0] = '\x1b['
+ + level[0]
+ + 'm['
+ + logger.prefix
+ + ']\x1b[m '
+ + args[0];
+
+ if ((level[1] === 'log' && !isatty[1])
+ || (level[1] === 'error' && !isatty[2])) {
+ args[0] = args[0].replace(/\x1b\[(?:\d+(?:;\d+)*)?m/g, '');
+ }
+
+ return console[level[1]].apply(console, args);
+}
+
+logger.prefix = 'tty.js';
+
+logger.log = function() {
+ return logger('log', slice.call(arguments));
+};
+
+logger.warning = function() {
+ return logger('warning', slice.call(arguments));
+};
+
+logger.error = function() {
+ return logger('error', slice.call(arguments));
+};
+
+module.exports = logger;
Oops, something went wrong.

0 comments on commit d7b14c8

Please sign in to comment.