PageRenderTime 28ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/aliyun-sdk/lib/util-browser.js

https://gitlab.com/imxieke/Ghost
JavaScript | 698 lines | 520 code | 91 blank | 87 comment | 103 complexity | f4d1fce187a7fdf87146a9fdd051b054 MD5 | raw file
  1. /*global escape:true */
  2. var ALY = require('./core');
  3. //var cryptoLib = require('crypto');
  4. var jsSHA = require('../bower_components/jsSHA/src/sha.js');
  5. var SparkMD5 = require('../bower_components/spark-md5/spark-md5.js');
  6. /* jshint -W079 */
  7. var Buffer = require('buffer').Buffer;
  8. /* jshint +W079 */
  9. /**
  10. * A set of utility methods for use with the ALY SDK.
  11. *
  12. * @!attribute abort
  13. * Return this value from an iterator function {each} or {arrayEach}
  14. * to break out of the iteration.
  15. * @example Breaking out of an iterator function
  16. * ALY.util.each({a: 1, b: 2, c: 3}, function(key, value) {
  17. * if (key == 'b') return ALY.util.abort;
  18. * });
  19. * @see each
  20. * @see arrayEach
  21. * @api private
  22. */
  23. ALY.util = {
  24. engine: function engine() {
  25. if (ALY.util.isBrowser() && typeof navigator !== 'undefined') {
  26. return navigator.userAgent;
  27. } else {
  28. return process.platform + '/' + process.version;
  29. }
  30. },
  31. userAgent: function userAgent() {
  32. // 在 浏览器中不能设置 ua
  33. return "";
  34. },
  35. isBrowser: function isBrowser() { return typeof window !== 'undefined'; },
  36. isNode: function isNode() { return !ALY.util.isBrowser(); },
  37. nodeRequire: function nodeRequire(module) {
  38. if (ALY.util.isNode()) return require(module);
  39. },
  40. // todo: atob, btoa
  41. hexToBase64: function (str) {
  42. return btoa(String.fromCharCode.apply(null,
  43. str.replace(/\r|\n/g, "").replace(/([\da-fA-F]{2}) ?/g, "0x$1 ").replace(/ +$/, "").split(" "))
  44. );
  45. },
  46. topEscape: function(clearString) {
  47. var output = '';
  48. var x = 0;
  49. clearString = clearString.toString();
  50. var regex = /(^[a-zA-Z0-9-_.~]*)/;
  51. while (x < clearString.length) {
  52. var match = regex.exec(clearString.substr(x));
  53. if (match != null && match.length > 1 && match[1] != '') {
  54. output += match[1];
  55. x += match[1].length;
  56. } else {
  57. if (clearString[x] == ' ')
  58. output += '%20';
  59. else {
  60. var charCode = clearString.charCodeAt(x);
  61. var hexVal = charCode.toString(16);
  62. output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
  63. }
  64. x++;
  65. }
  66. }
  67. return output;
  68. },
  69. opensearchEscape: function(clearString) {
  70. // http://v8.googlecode.com/svn/trunk/src/uri.js
  71. return encodeURIComponent(clearString)
  72. .replace(/\!/gi, '%21')
  73. .replace(/\'/gi, '%27')
  74. .replace(/\(/gi, '%28')
  75. .replace(/\)/gi, '%29')
  76. .replace(/\*/gi, '%2A')
  77. },
  78. uriEscape: function uriEscape(string) {
  79. /*jshint undef:false */
  80. var output = encodeURIComponent(string);
  81. output = output.replace(/[^A-Za-z0-9_.~\-%]+/g, escape);
  82. // percent-encodes some extra non-standard characters in a URI
  83. output = output.replace(/[*]/g, function(ch) {
  84. return '%' + ch.charCodeAt(0).toString(16).toUpperCase();
  85. });
  86. return output;
  87. },
  88. uriEscapePath: function uriEscapePath(string) {
  89. var parts = [];
  90. ALY.util.arrayEach(string.split('/'), function (part) {
  91. parts.push(ALY.util.uriEscape(part));
  92. });
  93. return parts.join('/');
  94. },
  95. urlParse: function urlParse(url) {
  96. return require('url').parse(url);
  97. },
  98. urlFormat: function urlFormat(url) {
  99. return require('url').format(url);
  100. },
  101. queryParamsToString: function queryParamsToString(params) {
  102. var items = [];
  103. var escape = ALY.util.uriEscape;
  104. var sortedKeys = Object.keys(params).sort();
  105. ALY.util.arrayEach(sortedKeys, function(name) {
  106. var value = params[name];
  107. var ename = escape(name);
  108. var result = ename;
  109. if (Array.isArray(value)) {
  110. var vals = [];
  111. ALY.util.arrayEach(value, function(item) { vals.push(escape(item)); });
  112. result = ename + '=' + vals.sort().join('&' + ename + '=');
  113. } else if (value !== undefined && value !== null) {
  114. result = ename + '=' + escape(value);
  115. }
  116. items.push(result);
  117. });
  118. return items.join('&');
  119. },
  120. readFileSync: function readFileSync(path) {
  121. if (typeof window !== 'undefined') return null;
  122. return require('fs').readFileSync(path, 'utf-8');
  123. },
  124. base64: {
  125. encode: function encode64(string) {
  126. return new Buffer(string).toString('base64');
  127. },
  128. decode: function decode64(string) {
  129. return new Buffer(string, 'base64').toString();
  130. }
  131. },
  132. Buffer: Buffer,
  133. buffer: {
  134. /**
  135. * Concatenates a list of Buffer objects.
  136. */
  137. concat: function(buffers) {
  138. var length = 0,
  139. offset = 0,
  140. buffer = null, i;
  141. for (i = 0; i < buffers.length; i++) {
  142. length += buffers[i].length;
  143. }
  144. buffer = new Buffer(length);
  145. for (i = 0; i < buffers.length; i++) {
  146. buffers[i].copy(buffer, offset);
  147. offset += buffers[i].length;
  148. }
  149. return buffer;
  150. }
  151. },
  152. string: {
  153. byteLength: function byteLength(string) {
  154. if (string === null || string === undefined) return 0;
  155. if (typeof string === 'string') string = new Buffer(string);
  156. if (typeof string.byteLength === 'number') {
  157. return string.byteLength;
  158. } else if (typeof string.length === 'number') {
  159. return string.length;
  160. } else if (typeof string.size === 'number') {
  161. return string.size;
  162. } else if (typeof string.path === 'string') {
  163. return require('fs').lstatSync(string.path).size;
  164. } else {
  165. throw ALY.util.error(new Error('Cannot determine length of ' + string),
  166. { object: string });
  167. }
  168. }
  169. },
  170. jamespath: {
  171. query: function query(expression, data) {
  172. if (!data) return [];
  173. var results = [];
  174. var expressions = expression.split(/\s+or\s+/);
  175. ALY.util.arrayEach.call(this, expressions, function (expr) {
  176. var objects = [data];
  177. var tokens = expr.split('.');
  178. ALY.util.arrayEach.call(this, tokens, function (token) {
  179. var match = token.match('^(.+?)(?:\\[(-?\\d+|\\*)\\])?$');
  180. var newObjects = [];
  181. ALY.util.arrayEach.call(this, objects, function (obj) {
  182. if (match[1] === '*') {
  183. ALY.util.arrayEach.call(this, obj, function (value) {
  184. newObjects.push(value);
  185. });
  186. } else if (obj.hasOwnProperty(match[1])) {
  187. newObjects.push(obj[match[1]]);
  188. }
  189. });
  190. objects = newObjects;
  191. // handle indexing (token[0], token[-1])
  192. if (match[2]) {
  193. newObjects = [];
  194. ALY.util.arrayEach.call(this, objects, function (obj) {
  195. if (Array.isArray(obj)) {
  196. if (match[2] === '*') {
  197. newObjects = newObjects.concat(obj);
  198. } else {
  199. var idx = parseInt(match[2], 10);
  200. if (idx < 0) idx = obj.length + idx; // negative indexing
  201. newObjects.push(obj[idx]);
  202. }
  203. }
  204. });
  205. objects = newObjects;
  206. }
  207. if (objects.length === 0) return ALY.util.abort;
  208. });
  209. if (objects.length > 0) {
  210. results = objects;
  211. return ALY.util.abort;
  212. }
  213. });
  214. return results;
  215. },
  216. find: function find(expression, data) {
  217. return ALY.util.jamespath.query(expression, data)[0];
  218. }
  219. },
  220. /**
  221. * Date and time utility functions.
  222. */
  223. date: {
  224. /**
  225. * @return [Date] the current JavaScript date object. Since all
  226. * ALY services rely on this date object, you can override
  227. * this function to provide a special time value to ALY service
  228. * requests.
  229. */
  230. getDate: function getDate() { return new Date(); },
  231. // for taobao open platform
  232. top: function top(date, fmt) {
  233. fmt = fmt || '%Y-%M-%dT%H:%m:%sZ';
  234. function pad(value) {
  235. return (value.toString().length < 2) ? '0' + value : value;
  236. };
  237. return fmt.replace(/%([a-zA-Z])/g, function (_, fmtCode) {
  238. switch (fmtCode) {
  239. case 'Y':
  240. return date.getUTCFullYear();
  241. case 'M':
  242. return pad(date.getUTCMonth() + 1);
  243. case 'd':
  244. return pad(date.getUTCDate());
  245. case 'H':
  246. return pad(date.getUTCHours());
  247. case 'm':
  248. return pad(date.getUTCMinutes());
  249. case 's':
  250. return pad(date.getUTCSeconds());
  251. default:
  252. throw new Error('Unsupported format code: ' + fmtCode);
  253. }
  254. });
  255. },
  256. /**
  257. * @return [String] the date in ISO-8601 format
  258. */
  259. iso8601: function iso8601(date) {
  260. if (date === undefined) { date = ALY.util.date.getDate(); }
  261. return date.toISOString();
  262. },
  263. /**
  264. * @return [String] the date in RFC 822 format
  265. */
  266. rfc822: function rfc822(date) {
  267. if (date === undefined) { date = ALY.util.date.getDate(); }
  268. return date.toUTCString();
  269. },
  270. unixSeconds: function unixSeconds(date) {
  271. if (date === undefined) { date = ALY.util.date.getDate(); }
  272. return Math.floor(date.getTime() / 1000);
  273. },
  274. unixMilliseconds: function unixMilliseconds(date) {
  275. if (date === undefined) { date = ALY.util.date.getDate(); }
  276. return date.getTime();
  277. },
  278. /**
  279. * @param [String,number,Date] date
  280. * @return [Date]
  281. */
  282. from: function format(date) {
  283. if (typeof date === 'number') {
  284. if(date.toString().length == 10) {
  285. return new Date(date * 1000); // unix timestamp in seconds
  286. }
  287. return new Date(date); // unix timestamp in mill seconds
  288. }
  289. else if(Object.prototype.toString.call(date) === '[object Date]') {
  290. return date;
  291. }
  292. else {
  293. return new Date(date);
  294. }
  295. },
  296. /**
  297. * Given a Date or date-like value, this function formats the
  298. * date into a string of the requested value.
  299. * @param [String,number,Date] date
  300. * @param [String] formatter Valid formats are:
  301. # * 'iso8601'
  302. # * 'rfc822'
  303. # * 'unixSeconds'
  304. # * 'unixMilliseconds'
  305. * @return [String]
  306. */
  307. format: function format(date, formatter) {
  308. if (!formatter) formatter = 'unixSeconds';
  309. return ALY.util.date[formatter](ALY.util.date.from(date));
  310. }
  311. },
  312. crypto: {
  313. crc32Table: [
  314. 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419,
  315. 0x706AF48F, 0xE963A535, 0x9E6495A3, 0x0EDB8832, 0x79DCB8A4,
  316. 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07,
  317. 0x90BF1D91, 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE,
  318. 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, 0x136C9856,
  319. 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9,
  320. 0xFA0F3D63, 0x8D080DF5, 0x3B6E20C8, 0x4C69105E, 0xD56041E4,
  321. 0xA2677172, 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B,
  322. 0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3,
  323. 0x45DF5C75, 0xDCD60DCF, 0xABD13D59, 0x26D930AC, 0x51DE003A,
  324. 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423, 0xCFBA9599,
  325. 0xB8BDA50F, 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924,
  326. 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, 0x76DC4190,
  327. 0x01DB7106, 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F,
  328. 0x9FBFE4A5, 0xE8B8D433, 0x7807C9A2, 0x0F00F934, 0x9609A88E,
  329. 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01,
  330. 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, 0x6C0695ED,
  331. 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 0x65B0D9C6, 0x12B7E950,
  332. 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3,
  333. 0xFBD44C65, 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2,
  334. 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB, 0x4369E96A,
  335. 0x346ED9FC, 0xAD678846, 0xDA60B8D0, 0x44042D73, 0x33031DE5,
  336. 0xAA0A4C5F, 0xDD0D7CC9, 0x5005713C, 0x270241AA, 0xBE0B1010,
  337. 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F,
  338. 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17,
  339. 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD, 0xEDB88320, 0x9ABFB3B6,
  340. 0x03B6E20C, 0x74B1D29A, 0xEAD54739, 0x9DD277AF, 0x04DB2615,
  341. 0x73DC1683, 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8,
  342. 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1, 0xF00F9344,
  343. 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB,
  344. 0x196C3671, 0x6E6B06E7, 0xFED41B76, 0x89D32BE0, 0x10DA7A5A,
  345. 0x67DD4ACC, 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5,
  346. 0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1,
  347. 0xA6BC5767, 0x3FB506DD, 0x48B2364B, 0xD80D2BDA, 0xAF0A1B4C,
  348. 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55, 0x316E8EEF,
  349. 0x4669BE79, 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236,
  350. 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, 0xC5BA3BBE,
  351. 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31,
  352. 0x2CD99E8B, 0x5BDEAE1D, 0x9B64C2B0, 0xEC63F226, 0x756AA39C,
  353. 0x026D930A, 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713,
  354. 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, 0x92D28E9B,
  355. 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, 0x86D3D2D4, 0xF1D4E242,
  356. 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1,
  357. 0x18B74777, 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C,
  358. 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45, 0xA00AE278,
  359. 0xD70DD2EE, 0x4E048354, 0x3903B3C2, 0xA7672661, 0xD06016F7,
  360. 0x4969474D, 0x3E6E77DB, 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66,
  361. 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9,
  362. 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605,
  363. 0xCDD70693, 0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8,
  364. 0x5D681B02, 0x2A6F2B94, 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B,
  365. 0x2D02EF8D],
  366. crc32: function crc32(data) {
  367. /*jshint bitwise:false*/
  368. var tbl = ALY.util.crypto.crc32Table;
  369. var crc = 0 ^ -1;
  370. if (typeof data === 'string') {
  371. data = new Buffer(data);
  372. }
  373. for (var i = 0; i < data.length; i++) {
  374. var code = data.readUInt8(i);
  375. crc = (crc>>>8) ^ tbl[(crc^code)&0xFF];
  376. }
  377. return (crc ^ -1) >>> 0;
  378. },
  379. hmac: function hmac(key, string, digest, fn) {
  380. if (!digest) digest = 'binary';
  381. if (digest === 'buffer') {
  382. digest = undefined;
  383. // todo: 不支持 buffer 类型的 hash
  384. return "";
  385. }
  386. if (!fn) fn = 'sha256';
  387. if (typeof string != 'string') {
  388. //string = new Buffer(string);
  389. // todo: 目前只支持 string
  390. return "";
  391. }
  392. var shaObj;
  393. switch (fn) {
  394. case "md5":
  395. // todo: 不支持 md5
  396. return "";
  397. case "sha1":
  398. shaObj = new jsSHA("SHA-1", "TEXT");
  399. break;
  400. case "sha256":
  401. shaObj = new jsSHA("SHA-256", "TEXT");
  402. break;
  403. case "sha512":
  404. shaObj = new jsSHA("SHA-512", "TEXT");
  405. break;
  406. default:
  407. return "";
  408. }
  409. shaObj.setHMACKey(key, "TEXT");
  410. shaObj.update(string);
  411. switch(digest) {
  412. case "binary":
  413. return shaObj.getHMAC("BYTES");
  414. case "hex":
  415. return shaObj.getHMAC("HEX");
  416. case "base64":
  417. return shaObj.getHMAC("B64");
  418. default:
  419. return "";
  420. }
  421. },
  422. // https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding
  423. md5: function md5(data, digest) {
  424. var hash = "";
  425. if (!digest) { digest = 'binary'; }
  426. if (digest === 'buffer') {
  427. digest = undefined;
  428. // todo: 不支持 buffer 类型的 hash
  429. return "";
  430. }
  431. if (typeof data === 'string') {
  432. switch(digest) {
  433. case "binary":
  434. return SparkMD5.hash(data, true);
  435. case "hex":
  436. return SparkMD5.hash(data);
  437. case "base64":
  438. return ALY.util.hexToBase64(SparkMD5.hash(data));
  439. default:
  440. return "";
  441. }
  442. }
  443. else { // array buffer
  444. switch(digest) {
  445. case "binary":
  446. return SparkMD5.ArrayBuffer.hash(data, true);
  447. case "hex":
  448. return SparkMD5.ArrayBuffer.hash(data);
  449. case "base64":
  450. return ALY.util.hexToBase64(SparkMD5.ArrayBuffer.hash(data));
  451. default:
  452. return "";
  453. }
  454. }
  455. },
  456. // 目前不支持 sha256 hash
  457. sha256: function sha256(string, digest) {
  458. //if (!digest) { digest = 'binary'; }
  459. //if (digest === 'buffer') { digest = undefined; }
  460. //if (typeof string === 'string') string = new Buffer(string);
  461. //return ALY.util.crypto.createHash('sha256').update(string).digest(digest);
  462. return "";
  463. },
  464. toHex: function toHex(data) {
  465. var out = [];
  466. for (var i = 0; i < data.length; i++) {
  467. out.push(('0' + data.charCodeAt(i).toString(16)).substr(-2, 2));
  468. }
  469. return out.join('');
  470. }
  471. },
  472. /** @!ignore */
  473. /* Abort constant */
  474. abort: {},
  475. each: function each(object, iterFunction) {
  476. for (var key in object) {
  477. if (object.hasOwnProperty(key)) {
  478. var ret = iterFunction.call(this, key, object[key]);
  479. if (ret === ALY.util.abort) break;
  480. }
  481. }
  482. },
  483. arrayEach: function arrayEach(array, iterFunction) {
  484. for (var idx in array) {
  485. if (array.hasOwnProperty(idx)) {
  486. var ret = iterFunction.call(this, array[idx], parseInt(idx, 10));
  487. if (ret === ALY.util.abort) break;
  488. }
  489. }
  490. },
  491. update: function update(obj1, obj2) {
  492. ALY.util.each(obj2, function iterator(key, item) {
  493. obj1[key] = item;
  494. });
  495. return obj1;
  496. },
  497. merge: function merge(obj1, obj2) {
  498. return ALY.util.update(ALY.util.copy(obj1), obj2);
  499. },
  500. copy: function copy(object) {
  501. if (object === null || object === undefined) return object;
  502. var dupe = {};
  503. /*jshint forin:false */
  504. for (var key in object) {
  505. dupe[key] = object[key];
  506. }
  507. return dupe;
  508. },
  509. isEmpty: function isEmpty(obj) {
  510. for (var prop in obj) {
  511. if (obj.hasOwnProperty(prop)) {
  512. return false;
  513. }
  514. }
  515. return true;
  516. },
  517. isType: function isType(obj, type) {
  518. // handle cross-"frame" objects
  519. if (typeof type === 'function') type = ALY.util.typeName(type);
  520. return Object.prototype.toString.call(obj) === '[object ' + type + ']';
  521. },
  522. typeName: function typeName(type) {
  523. if (type.hasOwnProperty('name')) return type.name;
  524. var str = type.toString();
  525. var match = str.match(/^\s*function (.+)\(/);
  526. return match ? match[1] : str;
  527. },
  528. error: function error(err, options) {
  529. var originalError = null;
  530. if (typeof err.message === 'string' && err.message !== '') {
  531. if (typeof options === 'string' || (options && options.message)) {
  532. originalError = ALY.util.copy(err);
  533. originalError.message = err.message;
  534. }
  535. }
  536. err.message = err.message || null;
  537. if (typeof options === 'string') {
  538. err.message = options;
  539. } else {
  540. ALY.util.update(err, options);
  541. }
  542. if (typeof Object.defineProperty === 'function') {
  543. Object.defineProperty(err, 'name', {writable: true, enumerable: false});
  544. Object.defineProperty(err, 'message', {enumerable: true});
  545. }
  546. err.name = err.name || err.code || 'Error';
  547. err.time = new Date();
  548. if (originalError) err.originalError = originalError;
  549. return err;
  550. },
  551. /**
  552. * @api private
  553. */
  554. inherit: function inherit(klass, features) {
  555. var newObject = null;
  556. if (features === undefined) {
  557. features = klass;
  558. klass = Object;
  559. newObject = {};
  560. } else {
  561. /*jshint newcap:false */
  562. /*jshint camelcase:false */
  563. var ctor = function __ctor_wrapper__() {};
  564. ctor.prototype = klass.prototype;
  565. newObject = new ctor();
  566. }
  567. // constructor not supplied, create pass-through ctor
  568. if (features.constructor === Object) {
  569. features.constructor = function() {
  570. if (klass !== Object) {
  571. return klass.apply(this, arguments);
  572. }
  573. };
  574. }
  575. features.constructor.prototype = newObject;
  576. ALY.util.update(features.constructor.prototype, features);
  577. features.constructor.__super__ = klass;
  578. return features.constructor;
  579. },
  580. /**
  581. * @api private
  582. */
  583. mixin: function mixin() {
  584. var klass = arguments[0];
  585. for (var i = 1; i < arguments.length; i++) {
  586. /*jshint forin:false*/
  587. for (var prop in arguments[i].prototype) {
  588. var fn = arguments[i].prototype[prop];
  589. if (prop != 'constructor') {
  590. klass.prototype[prop] = fn;
  591. }
  592. }
  593. }
  594. return klass;
  595. },
  596. /**
  597. * @api private
  598. */
  599. hideProperties: function hideProperties(obj, props) {
  600. if (typeof Object.defineProperty !== 'function') return;
  601. ALY.util.arrayEach(props, function (key) {
  602. Object.defineProperty(obj, key, {
  603. enumerable: false, writable: true, configurable: true });
  604. });
  605. }
  606. };
  607. module.exports = ALY.util;