utils.js 998 B

12345678910111213141516171819202122232425262728293031323334
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const crypto = require("crypto");
  4. function formateDate(timestamp) {
  5. return new Date(timestamp * 1000).toISOString().split('T')[0];
  6. }
  7. exports.formateDate = formateDate;
  8. function second() {
  9. // istanbul ignore next
  10. return Math.floor(new Date().getTime() / 1000);
  11. }
  12. exports.second = second;
  13. function stringify(v) {
  14. return typeof v !== 'string' ? JSON.stringify(v) : v;
  15. }
  16. exports.stringify = stringify;
  17. function sha256hash(string, encoding = 'hex') {
  18. return crypto
  19. .createHash('sha256')
  20. .update(string)
  21. .digest(encoding);
  22. }
  23. exports.sha256hash = sha256hash;
  24. function sha256hmac(string, secret = '', encoding) {
  25. return crypto
  26. .createHmac('sha256', secret)
  27. .update(string)
  28. .digest(encoding);
  29. }
  30. exports.sha256hmac = sha256hmac;
  31. function isNodeEnv() {
  32. return process && process.release && process.release.name === 'node';
  33. }
  34. exports.isNodeEnv = isNodeEnv;