base-server.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * @method FS.Utility.binaryToBuffer
  3. * @public
  4. * @param {Uint8Array} data
  5. * @returns {Buffer}
  6. *
  7. * Converts a Uint8Array instance to a Node Buffer instance
  8. */
  9. FS.Utility.binaryToBuffer = function(data) {
  10. var len = data.length;
  11. var buffer = new Buffer(len);
  12. for (var i = 0; i < len; i++) {
  13. buffer[i] = data[i];
  14. }
  15. return buffer;
  16. };
  17. /**
  18. * @method FS.Utility.bufferToBinary
  19. * @public
  20. * @param {Buffer} data
  21. * @returns {Uint8Array}
  22. *
  23. * Converts a Node Buffer instance to a Uint8Array instance
  24. */
  25. FS.Utility.bufferToBinary = function(data) {
  26. var len = data.length;
  27. var binary = EJSON.newBinary(len);
  28. for (var i = 0; i < len; i++) {
  29. binary[i] = data[i];
  30. }
  31. return binary;
  32. };
  33. /**
  34. * @method FS.Utility.safeCallback
  35. * @public
  36. * @param {Function} callback
  37. * @returns {Function}
  38. *
  39. * Makes a callback safe for Meteor code
  40. */
  41. FS.Utility.safeCallback = function (callback) {
  42. return Meteor.bindEnvironment(callback, function(err) { throw err; });
  43. };
  44. /**
  45. * @method FS.Utility.safeStream
  46. * @public
  47. * @param {Stream} nodestream
  48. * @returns {Stream}
  49. *
  50. * Adds `safeOn` and `safeOnce` methods to a NodeJS Stream
  51. * object. These are the same as `on` and `once`, except
  52. * that the callback is wrapped for use in Meteor.
  53. */
  54. FS.Utility.safeStream = function(nodestream) {
  55. if (!nodestream || typeof nodestream.on !== 'function')
  56. throw new Error('FS.Utility.safeStream requires a NodeJS Stream');
  57. // Create Meteor safe events
  58. nodestream.safeOn = function(name, callback) {
  59. return nodestream.on(name, FS.Utility.safeCallback(callback));
  60. };
  61. // Create Meteor safe events
  62. nodestream.safeOnce = function(name, callback) {
  63. return nodestream.once(name, FS.Utility.safeCallback(callback));
  64. };
  65. // Return the modified stream - modified anyway
  66. return nodestream;
  67. };
  68. /**
  69. * @method FS.Utility.eachFileFromPath
  70. * @public
  71. * @param {String} p - Server path
  72. * @param {Function} f - Function to run for each file found in the path.
  73. * @returns {undefined}
  74. *
  75. * Utility for iteration over files from path on server
  76. */
  77. FS.Utility.eachFileFromPath = function(p, f) {
  78. var fs = Npm.require('fs');
  79. var path = Npm.require('path');
  80. var files = fs.readdirSync(p);
  81. files.map(function (file) {
  82. return path.join(p, file);
  83. }).filter(function (filePath) {
  84. return fs.statSync(filePath).isFile() && path.basename(filePath)[0] !== '.';
  85. }).forEach(function (filePath) {
  86. f(filePath);
  87. });
  88. };