util.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. var util = {
  2. debug: false,
  3. inherits: function(ctor, superCtor) {
  4. ctor.super_ = superCtor;
  5. ctor.prototype = Object.create(superCtor.prototype, {
  6. constructor: {
  7. value: ctor,
  8. enumerable: false,
  9. writable: true,
  10. configurable: true
  11. }
  12. });
  13. },
  14. extend: function(dest, source) {
  15. source = source || {};
  16. for(var key in source) {
  17. if(source.hasOwnProperty(key)) {
  18. dest[key] = source[key];
  19. }
  20. }
  21. return dest;
  22. },
  23. randomId: function () {
  24. return Math.random().toString(36).substr(2);
  25. },
  26. prettyError: function (msg) {
  27. if (util.debug) {
  28. console.log('ERROR PeerServer: ', msg);
  29. }
  30. },
  31. log: function() {
  32. if (util.debug) {
  33. var copy = [];
  34. for (var i = 0; i < arguments.length; i += 1) {
  35. copy[i] = arguments[i];
  36. }
  37. console.log.apply(console, copy);
  38. }
  39. },
  40. allowCrossDomain: function(req, res, next) {
  41. res.setHeader('Access-Control-Allow-Origin', '*');
  42. res.setHeader('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
  43. res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
  44. next();
  45. }
  46. };
  47. // if node
  48. module.exports = util;
  49. // end node