PageRenderTime 38ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/nodemailer/node_modules/aws-sdk/lib/util.js

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