IosParser.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _dayjs = _interopRequireDefault(require("dayjs"));
  7. var _constants = require("./constants");
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  10. class IosParser {
  11. splitMessages(raw) {
  12. const messages = [];
  13. let data = raw.toString();
  14. let match = data.match(IosParser.timeRegex);
  15. while (match) {
  16. const timeMatch = match[0];
  17. data = data.slice((match.index || 0) + timeMatch.length);
  18. const nextMatch = data.match(IosParser.timeRegex);
  19. const body = nextMatch ? data.slice(0, nextMatch.index) : data;
  20. messages.push(`${timeMatch} ${body}`);
  21. match = nextMatch;
  22. }
  23. return messages;
  24. }
  25. parseMessages(messages) {
  26. return messages.map(rawMessage => {
  27. const timeMatch = rawMessage.match(IosParser.timeRegex);
  28. if (!timeMatch) {
  29. throw new Error(`Time regex was not matched in message: ${rawMessage}`);
  30. }
  31. const headerMatch = rawMessage.slice(timeMatch[0].length).match(IosParser.headerRegex) || ['', 'Default', '-1', 'unknown'];
  32. const [, priority, pid, tag] = headerMatch;
  33. return {
  34. platform: 'ios',
  35. date: (0, _dayjs.default)(timeMatch[0]).set('millisecond', 0),
  36. pid: parseInt(pid.trim(), 10) || 0,
  37. priority: _constants.Priority.fromName(priority),
  38. tag,
  39. messages: [rawMessage.slice(timeMatch[0].length + headerMatch[0].length).trim()]
  40. };
  41. }).reduce((acc, entry) => {
  42. if (acc.length > 0 && acc[acc.length - 1].date.isSame(entry.date) && acc[acc.length - 1].appId === entry.appId && acc[acc.length - 1].pid === entry.pid && acc[acc.length - 1].priority === entry.priority) {
  43. acc[acc.length - 1].messages.push(...entry.messages);
  44. return acc;
  45. }
  46. return [...acc, entry];
  47. }, []);
  48. }
  49. }
  50. exports.default = IosParser;
  51. _defineProperty(IosParser, "timeRegex", /\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.[\d+]+/m);
  52. _defineProperty(IosParser, "headerRegex", /^\s+[a-z0-9]+\s+(\w+)\s+[a-z0-9]+\s+(\d+)\s+\d+\s+([^:]+):/);
  53. //# sourceMappingURL=IosParser.js.map