PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/PhoneGap02/node_modules/socket.io-client/bin/builder.js

https://gitlab.com/hemantr/NetBeansProjects
JavaScript | 303 lines | 152 code | 49 blank | 102 comment | 21 complexity | b18efbca1681ebdaf8509748722406e8 MD5 | raw file
  1. /*!
  2. * socket.io-node
  3. * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
  4. * MIT Licensed
  5. */
  6. /**
  7. * Module dependencies.
  8. */
  9. var fs = require('fs')
  10. , socket = require('../lib/io')
  11. , uglify = require('uglify-js')
  12. , activeXObfuscator = require('active-x-obfuscator');
  13. /**
  14. * License headers.
  15. *
  16. * @api private
  17. */
  18. var template = '/*! Socket.IO.%ext% build:' + socket.version + ', %type%. Copyright(c) 2011 LearnBoost <dev@learnboost.com> MIT Licensed */\n'
  19. , development = template.replace('%type%', 'development').replace('%ext%', 'js')
  20. , production = template.replace('%type%', 'production').replace('%ext%', 'min.js');
  21. /**
  22. * If statements, these allows you to create serveride & client side compatible
  23. * code using specially designed `if` statements that remove serverside
  24. * designed code from the source files
  25. *
  26. * @api private
  27. */
  28. var starttagIF = '// if node'
  29. , endtagIF = '// end node';
  30. /**
  31. * The modules that are required to create a base build of Socket.IO.
  32. *
  33. * @const
  34. * @type {Array}
  35. * @api private
  36. */
  37. var base = [
  38. 'io.js'
  39. , 'util.js'
  40. , 'events.js'
  41. , 'json.js'
  42. , 'parser.js'
  43. , 'transport.js'
  44. , 'socket.js'
  45. , 'namespace.js'
  46. ];
  47. /**
  48. * The available transports for Socket.IO. These are mapped as:
  49. *
  50. * - `key` the name of the transport
  51. * - `value` the dependencies for the transport
  52. *
  53. * @const
  54. * @type {Object}
  55. * @api public
  56. */
  57. var baseTransports = {
  58. 'websocket': ['transports/websocket.js']
  59. , 'flashsocket': [
  60. 'transports/websocket.js'
  61. , 'transports/flashsocket.js'
  62. , 'vendor/web-socket-js/swfobject.js'
  63. , 'vendor/web-socket-js/web_socket.js'
  64. ]
  65. , 'htmlfile': ['transports/xhr.js', 'transports/htmlfile.js']
  66. /* FIXME: re-enable me once we have multi-part support
  67. , 'xhr-multipart': ['transports/xhr.js', 'transports/xhr-multipart.js'] */
  68. , 'xhr-polling': ['transports/xhr.js', 'transports/xhr-polling.js']
  69. , 'jsonp-polling': [
  70. 'transports/xhr.js'
  71. , 'transports/xhr-polling.js'
  72. , 'transports/jsonp-polling.js'
  73. ]
  74. };
  75. /**
  76. * Wrappers for client-side usage.
  77. * This enables usage in top-level browser window, client-side CommonJS systems and AMD loaders.
  78. * If doing a node build for server-side client, this wrapper is NOT included.
  79. * @api private
  80. */
  81. var wrapperPre = "\nvar io = ('undefined' === typeof module ? {} : module.exports);\n(function() {\n";
  82. var wrapperPost = "\nif (typeof define === \"function\" && define.amd) {" +
  83. "\n define([], function () { return io; });" +
  84. "\n}\n})();";
  85. /**
  86. * Builds a custom Socket.IO distribution based on the transports that you
  87. * need. You can configure the build to create development build or production
  88. * build (minified).
  89. *
  90. * @param {Array} transports The transports that needs to be bundled.
  91. * @param {Object} [options] Options to configure the building process.
  92. * @param {Function} callback Last argument should always be the callback
  93. * @callback {String|Boolean} err An optional argument, if it exists than an error
  94. * occurred during the build process.
  95. * @callback {String} result The result of the build process.
  96. * @api public
  97. */
  98. var builder = module.exports = function () {
  99. var transports, options, callback, error = null
  100. , args = Array.prototype.slice.call(arguments, 0)
  101. , settings = {
  102. minify: true
  103. , node: false
  104. , custom: []
  105. };
  106. // Fancy pancy argument support this makes any pattern possible mainly
  107. // because we require only one of each type
  108. args.forEach(function (arg) {
  109. var type = Object.prototype.toString.call(arg)
  110. .replace(/\[object\s(\w+)\]/gi , '$1' ).toLowerCase();
  111. switch (type) {
  112. case 'array':
  113. return transports = arg;
  114. case 'object':
  115. return options = arg;
  116. case 'function':
  117. return callback = arg;
  118. }
  119. });
  120. // Add defaults
  121. options = options || {};
  122. transports = transports || Object.keys(baseTransports);
  123. // Merge the data
  124. for(var option in options) {
  125. settings[option] = options[option];
  126. }
  127. // Start creating a dependencies chain with all the required files for the
  128. // custom Socket.IO bundle.
  129. var files = [];
  130. base.forEach(function (file) {
  131. files.push(__dirname + '/../lib/' + file);
  132. });
  133. transports.forEach(function (transport) {
  134. var dependencies = baseTransports[transport];
  135. if (!dependencies) {
  136. error = 'Unsupported transport `' + transport + '` supplied as argument.';
  137. return;
  138. }
  139. // Add the files to the files list, but only if they are not added before
  140. dependencies.forEach(function (file) {
  141. var path = __dirname + '/../lib/' + file;
  142. if (!~files.indexOf(path)) files.push(path);
  143. })
  144. });
  145. // check to see if the files tree compilation generated any errors.
  146. if (error) return callback(error);
  147. var results = {};
  148. files.forEach(function (file) {
  149. fs.readFile(file, function (err, content) {
  150. if (err) error = err;
  151. results[file] = content;
  152. // check if we are done yet, or not.. Just by checking the size of the result
  153. // object.
  154. if (Object.keys(results).length !== files.length) return;
  155. // we are done, did we error?
  156. if (error) return callback(error);
  157. // start with the license header
  158. var code = development
  159. , ignore = 0;
  160. // pre-wrapper for non-server-side builds
  161. if (!settings.node) code += wrapperPre;
  162. // concatenate the file contents in order
  163. files.forEach(function (file) {
  164. code += results[file];
  165. });
  166. // check if we need to add custom code
  167. if (settings.custom.length) {
  168. settings.custom.forEach(function (content) {
  169. code += content;
  170. });
  171. }
  172. // post-wrapper for non-server-side builds
  173. if (!settings.node) {
  174. code += wrapperPost;
  175. }
  176. code = activeXObfuscator(code);
  177. // Search for conditional code blocks that need to be removed as they
  178. // where designed for a server side env. but only if we don't want to
  179. // make this build node compatible.
  180. if (!settings.node) {
  181. code = code.split('\n').filter(function (line) {
  182. // check if there are tags in here
  183. var start = line.indexOf(starttagIF) >= 0
  184. , end = line.indexOf(endtagIF) >= 0
  185. , ret = ignore;
  186. // ignore the current line
  187. if (start) {
  188. ignore++;
  189. ret = ignore;
  190. }
  191. // stop ignoring the next line
  192. if (end) {
  193. ignore--;
  194. }
  195. return ret == 0;
  196. }).join('\n');
  197. }
  198. // check if we need to process it any further
  199. if (settings.minify) {
  200. var ast = uglify.parser.parse(code);
  201. ast = uglify.uglify.ast_mangle(ast);
  202. ast = uglify.uglify.ast_squeeze(ast);
  203. code = production + uglify.uglify.gen_code(ast, { ascii_only: true });
  204. }
  205. callback(error, code);
  206. })
  207. })
  208. };
  209. /**
  210. * Builder version is also the current client version
  211. * this way we don't have to do another include for the
  212. * clients version number and we can just include the builder.
  213. *
  214. * @type {String}
  215. * @api public
  216. */
  217. builder.version = socket.version;
  218. /**
  219. * A list of all build in transport types.
  220. *
  221. * @type {Object}
  222. * @api public
  223. */
  224. builder.transports = baseTransports;
  225. /**
  226. * Command line support, this allows us to generate builds without having
  227. * to load it as module.
  228. */
  229. if (!module.parent){
  230. // the first 2 are `node` and the path to this file, we don't need them
  231. var args = process.argv.slice(2);
  232. // build a development build
  233. builder(args.length ? args : false, { minify:false }, function (err, content) {
  234. if (err) return console.error(err);
  235. fs.write(
  236. fs.openSync(__dirname + '/../dist/socket.io.js', 'w')
  237. , content
  238. , 0
  239. , 'utf8'
  240. );
  241. console.log('Successfully generated the development build: socket.io.js');
  242. });
  243. // and build a production build
  244. builder(args.length ? args : false, function (err, content) {
  245. if (err) return console.error(err);
  246. fs.write(
  247. fs.openSync(__dirname + '/../dist/socket.io.min.js', 'w')
  248. , content
  249. , 0
  250. , 'utf8'
  251. );
  252. console.log('Successfully generated the production build: socket.io.min.js');
  253. });
  254. }