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.

136 lines
3.7 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
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. .define({ name: "compress", alias: "c", type: "boolean" })
  24. .on("error", function(err){
  25. halt(err.message);
  26. })
  27. .set(process.argv);
  28. function handleServerError(err){
  29. if (err.code === "EADDRINUSE"){
  30. halt("port " + argv.port + " is already is use");
  31. } else {
  32. halt(err.message);
  33. }
  34. }
  35. /**
  36. Die here if invalid args received
  37. */
  38. if (!argv.valid) halt(argv.validationMessages);
  39. /**
  40. $ ws --help
  41. */
  42. if (argv.help){
  43. console.log(usage);
  44. } else {
  45. var total = {
  46. req: 0,
  47. bytes: 0,
  48. connections: 0
  49. };
  50. process.on("SIGINT", function(){
  51. console.show();
  52. console.log();
  53. process.exit(0);
  54. });
  55. /**
  56. customised connect.logger :date token, purely to satisfy Logstalgia.
  57. */
  58. connect.logger.token("date", function(){
  59. var a = new Date();
  60. return (a.getDate() + "/" + a.getUTCMonth() + "/" + a.getFullYear() + ":" + a.toTimeString())
  61. .replace("GMT", "").replace(" (BST)", "");
  62. });
  63. var app = connect();
  64. /*
  65. log using --log-format (if supplied), else output statics
  66. */
  67. if(argv["log-format"]){
  68. app.use(connect.logger(argv["log-format"]));
  69. } else {
  70. app.use(function(req, res, next){
  71. console.column(1).write(++total.req);
  72. next();
  73. });
  74. }
  75. /**
  76. --compress enables compression
  77. */
  78. if (argv.compress) app.use(connect.compress());
  79. /**
  80. static file server including directory browsing support
  81. */
  82. app.use(connect.static(argv.directory))
  83. .use(connect.directory(argv.directory, { icons: true }));
  84. /**
  85. launch server
  86. */
  87. var server = http.createServer(app)
  88. .on("error", handleServerError)
  89. .listen(argv.port);
  90. /*
  91. write status to stderr so stdout can be piped to disk ($ ws > log.txt)
  92. */
  93. if (argv.directory === process.cwd()){
  94. console.error("serving at %u{%s}", "http://localhost:" + argv.port);
  95. } else {
  96. console.error("serving %u{%s} at %u{%s}", argv.directory, "http://localhost:" + argv.port);
  97. }
  98. /**
  99. in stats mode, monitor connections and bytes transferred
  100. */
  101. if (!argv["log-format"]){
  102. console.hide();
  103. console.log("%u{Requests} %u{Data} %u{Connections}");
  104. server.on("connection", function(socket){
  105. var oldWrite = socket.write;
  106. socket.write = function(data) {
  107. if (!Buffer.isBuffer(data)) {
  108. data = new Buffer(data);
  109. }
  110. oldWrite.call(this, data);
  111. total.bytes += data.length;
  112. console.column(12).write((w.bytesToSize(total.bytes, 2) + " ").slice(0,12));
  113. };
  114. console.column(24).write(++total.connections);
  115. socket.on("close", function(){
  116. console.column(24).write(--total.connections);
  117. });
  118. });
  119. }
  120. }