You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
3.3 KiB

11 years ago
11 years ago
12 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. #!/usr/bin/env node
  2. "use strict";
  3. require("more-console");
  4. var connect = require("connect"),
  5. http = require("http"),
  6. util = require("util"),
  7. Thing = require("nature").Thing,
  8. w = require("wodge");
  9. var usage = "usage: ws [--directory|-d <directory>] [--port|-p <port>] [--log-format|-f dev|default|short|tiny]";
  10. function halt(message){
  11. console.red.log("Error: %s", message);
  12. console.log(usage);
  13. process.exit(1);
  14. }
  15. /**
  16. parse command-line args
  17. */
  18. var argv = new Thing()
  19. .define({ name: "port", alias: "p", type: "number", defaultOption: true, value: 8000 })
  20. .define({ name: "log-format", alias: "f", type: "string" })
  21. .define({ name: "help", alias: "h", type: "boolean" })
  22. .define({ name: "directory", alias: "d", type: "string", value: process.cwd() })
  23. .on("error", function(err){
  24. halt(err.message);
  25. })
  26. .set(process.argv);
  27. function handleServerError(err){
  28. if (err.code === "EADDRINUSE"){
  29. halt("port " + argv.port + " is already is use");
  30. } else {
  31. halt(err.message);
  32. }
  33. }
  34. /**
  35. Die here if invalid args received
  36. */
  37. if (!argv.valid) halt(argv.validationMessages);
  38. /**
  39. $ ws --help
  40. */
  41. if (argv.help){
  42. console.log(usage);
  43. } else {
  44. var total = {
  45. req: 0,
  46. bytes: 0,
  47. connections: 0
  48. };
  49. process.on("SIGINT", function(){
  50. console.show();
  51. console.log();
  52. process.exit(0);
  53. });
  54. /**
  55. customised connect.logger :date token, purely to satisfy Logstalgia.
  56. */
  57. connect.logger.token("date", function(){
  58. var a = new Date();
  59. return (a.getDate() + "/" + a.getUTCMonth() + "/" + a.getFullYear() + ":" + a.toTimeString())
  60. .replace("GMT", "").replace(" (BST)", "");
  61. });
  62. var app = connect();
  63. if(argv["log-format"]){
  64. app.use(connect.logger(argv["log-format"]));
  65. } else {
  66. app.use(function(req, res, next){
  67. console.column(1).write(++total.req);
  68. next();
  69. });
  70. }
  71. app.use(connect.static(argv.directory))
  72. .use(connect.directory(argv.directory, { icons: true }));
  73. var server = http.createServer(app)
  74. .on("error", handleServerError)
  75. .listen(argv.port);
  76. /*
  77. write status to stderr so stdout can be piped to disk ($ ws > log.txt)
  78. */
  79. if (argv.directory === process.cwd()){
  80. console.error("serving at %u{%s}", "http://localhost:" + argv.port);
  81. } else {
  82. console.error("serving %u{%s} at %u{%s}", argv.directory, "http://localhost:" + argv.port);
  83. }
  84. if (!argv["log-format"]){
  85. console.hide();
  86. console.log("%u{Requests} %u{Data} %u{Connections}");
  87. server.on("connection", function(socket){
  88. var oldWrite = socket.write;
  89. socket.write = function(data) {
  90. if (!Buffer.isBuffer(data)) {
  91. data = new Buffer(data);
  92. }
  93. oldWrite.call(this, data);
  94. total.bytes += data.length;
  95. console.column(12).write((w.bytesToSize(total.bytes, 2) + " ").slice(0,12));
  96. };
  97. console.column(24).write(++total.connections);
  98. socket.on("close", function(){
  99. console.column(24).write(--total.connections);
  100. });
  101. });
  102. }
  103. }