util.js 708 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. 'use strict';
  2. const crc = require('crc').crc32;
  3. module.exports = {
  4. /**
  5. * Decode the base64 cookie value to an object.
  6. *
  7. * @param {String} string
  8. * @return {Object}
  9. * @api private
  10. */
  11. decode(string) {
  12. const body = Buffer.from(string, 'base64').toString('utf8');
  13. const json = JSON.parse(body);
  14. return json;
  15. },
  16. /**
  17. * Encode an object into a base64-encoded JSON string.
  18. *
  19. * @param {Object} body
  20. * @return {String}
  21. * @api private
  22. */
  23. encode(body) {
  24. body = JSON.stringify(body);
  25. return Buffer.from(body).toString('base64');
  26. },
  27. hash(sess) {
  28. return crc(JSON.stringify(sess));
  29. },
  30. CookieDateEpoch: 'Thu, 01 Jan 1970 00:00:00 GMT',
  31. };