index.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { v4 as uuidv4 } from 'uuid';
  2. import sha256 from 'js-sha256';
  3. //////////////////////////
  4. // Helper functions
  5. //////////////////////////
  6. export const splitStream = (splitOn) => {
  7. let buffer = '';
  8. return new TransformStream({
  9. transform(chunk, controller) {
  10. buffer += chunk;
  11. const parts = buffer.split(splitOn);
  12. parts.slice(0, -1).forEach((part) => controller.enqueue(part));
  13. buffer = parts[parts.length - 1];
  14. },
  15. flush(controller) {
  16. if (buffer) controller.enqueue(buffer);
  17. }
  18. });
  19. };
  20. export const convertMessagesToHistory = (messages) => {
  21. let history = {
  22. messages: {},
  23. currentId: null
  24. };
  25. let parentMessageId = null;
  26. let messageId = null;
  27. for (const message of messages) {
  28. messageId = uuidv4();
  29. if (parentMessageId !== null) {
  30. history.messages[parentMessageId].childrenIds = [
  31. ...history.messages[parentMessageId].childrenIds,
  32. messageId
  33. ];
  34. }
  35. history.messages[messageId] = {
  36. ...message,
  37. id: messageId,
  38. parentId: parentMessageId,
  39. childrenIds: []
  40. };
  41. parentMessageId = messageId;
  42. }
  43. history.currentId = messageId;
  44. return history;
  45. };
  46. export const getGravatarURL = (email) => {
  47. // Trim leading and trailing whitespace from
  48. // an email address and force all characters
  49. // to lower case
  50. const address = String(email).trim().toLowerCase();
  51. // Create a SHA256 hash of the final string
  52. const hash = sha256(address);
  53. // Grab the actual image URL
  54. return `https://www.gravatar.com/avatar/${hash}`;
  55. };