Ein Projekt das es ermöglicht Beerpong über das Internet von zwei unabhängigen positionen aus zu spielen. Entstehung im Rahmen einer Praktikumsaufgabe im Fach Interaktion.
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.

index.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*!
  2. * serve-favicon
  3. * Copyright(c) 2010 Sencha Inc.
  4. * Copyright(c) 2011 TJ Holowaychuk
  5. * Copyright(c) 2014 Douglas Christopher Wilson
  6. * MIT Licensed
  7. */
  8. /**
  9. * Module dependencies.
  10. * @private
  11. */
  12. var etag = require('etag');
  13. var fresh = require('fresh');
  14. var fs = require('fs');
  15. var ms = require('ms');
  16. var path = require('path');
  17. var resolve = path.resolve;
  18. /**
  19. * Module variables.
  20. * @private
  21. */
  22. var maxMaxAge = 60 * 60 * 24 * 365 * 1000; // 1 year
  23. /**
  24. * Serves the favicon located by the given `path`.
  25. *
  26. * @public
  27. * @param {String|Buffer} path
  28. * @param {Object} options
  29. * @return {Function} middleware
  30. */
  31. module.exports = function favicon(path, options){
  32. options = options || {};
  33. var buf;
  34. var icon; // favicon cache
  35. var maxAge = calcMaxAge(options.maxAge);
  36. var stat;
  37. if (!path) throw new TypeError('path to favicon.ico is required');
  38. if (Buffer.isBuffer(path)) {
  39. buf = new Buffer(path.length);
  40. path.copy(buf);
  41. icon = createIcon(buf, maxAge);
  42. } else if (typeof path === 'string') {
  43. path = resolve(path);
  44. stat = fs.statSync(path);
  45. if (stat.isDirectory()) throw createIsDirError(path);
  46. } else {
  47. throw new TypeError('path to favicon.ico must be string or buffer');
  48. }
  49. return function favicon(req, res, next){
  50. if ('/favicon.ico' !== req.url) return next();
  51. if ('GET' !== req.method && 'HEAD' !== req.method) {
  52. var status = 'OPTIONS' === req.method ? 200 : 405;
  53. res.writeHead(status, {'Allow': 'GET, HEAD, OPTIONS'});
  54. res.end();
  55. return;
  56. }
  57. if (icon) return send(req, res, icon);
  58. fs.readFile(path, function(err, buf){
  59. if (err) return next(err);
  60. icon = createIcon(buf, maxAge);
  61. send(req, res, icon);
  62. });
  63. };
  64. };
  65. /**
  66. * Calculate the max-age from a configured value.
  67. *
  68. * @private
  69. * @param {string|number} val
  70. * @return {number}
  71. */
  72. function calcMaxAge(val) {
  73. var num = typeof val === 'string'
  74. ? ms(val)
  75. : val;
  76. return num != null
  77. ? Math.min(Math.max(0, num), maxMaxAge)
  78. : maxMaxAge
  79. }
  80. /**
  81. * Create icon data from Buffer and max-age.
  82. *
  83. * @private
  84. * @param {Buffer} buf
  85. * @param {number} maxAge
  86. * @return {object}
  87. */
  88. function createIcon(buf, maxAge) {
  89. return {
  90. body: buf,
  91. headers: {
  92. 'Cache-Control': 'public, max-age=' + ~~(maxAge / 1000),
  93. 'ETag': etag(buf)
  94. }
  95. };
  96. }
  97. /**
  98. * Create EISDIR error.
  99. *
  100. * @private
  101. * @param {string} path
  102. * @return {Error}
  103. */
  104. function createIsDirError(path) {
  105. var error = new Error('EISDIR, illegal operation on directory \'' + path + '\'');
  106. error.code = 'EISDIR';
  107. error.errno = 28;
  108. error.path = path;
  109. error.syscall = 'open';
  110. return error;
  111. }
  112. /**
  113. * Send icon data in response to a request.
  114. *
  115. * @private
  116. * @param {IncomingMessage} req
  117. * @param {OutgoingMessage} res
  118. * @param {object} icon
  119. */
  120. function send(req, res, icon) {
  121. var headers = icon.headers;
  122. // Set headers
  123. var keys = Object.keys(headers);
  124. for (var i = 0; i < keys.length; i++) {
  125. var key = keys[i];
  126. res.setHeader(key, headers[key]);
  127. }
  128. if (fresh(req.headers, res._headers)) {
  129. res.statusCode = 304;
  130. res.end();
  131. return;
  132. }
  133. res.statusCode = 200;
  134. res.setHeader('Content-Length', icon.body.length);
  135. res.setHeader('Content-Type', 'image/x-icon');
  136. res.end(icon.body);
  137. }