index.cjs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: !0 });
  3. class ParseError extends Error {
  4. constructor(message, options) {
  5. super(message), this.name = "ParseError", this.type = options.type, this.field = options.field, this.value = options.value, this.line = options.line;
  6. }
  7. }
  8. function noop(_arg) {
  9. }
  10. function createParser(callbacks) {
  11. if (typeof callbacks == "function")
  12. throw new TypeError(
  13. "`callbacks` must be an object, got a function instead. Did you mean `{onEvent: fn}`?"
  14. );
  15. const { onEvent = noop, onError = noop, onRetry = noop, onComment } = callbacks;
  16. let incompleteLine = "", isFirstChunk = !0, id, data = "", eventType = "";
  17. function feed(newChunk) {
  18. const chunk = isFirstChunk ? newChunk.replace(/^\xEF\xBB\xBF/, "") : newChunk, [complete, incomplete] = splitLines(`${incompleteLine}${chunk}`);
  19. for (const line of complete)
  20. parseLine(line);
  21. incompleteLine = incomplete, isFirstChunk = !1;
  22. }
  23. function parseLine(line) {
  24. if (line === "") {
  25. dispatchEvent();
  26. return;
  27. }
  28. if (line.startsWith(":")) {
  29. onComment && onComment(line.slice(line.startsWith(": ") ? 2 : 1));
  30. return;
  31. }
  32. const fieldSeparatorIndex = line.indexOf(":");
  33. if (fieldSeparatorIndex !== -1) {
  34. const field = line.slice(0, fieldSeparatorIndex), offset = line[fieldSeparatorIndex + 1] === " " ? 2 : 1, value = line.slice(fieldSeparatorIndex + offset);
  35. processField(field, value, line);
  36. return;
  37. }
  38. processField(line, "", line);
  39. }
  40. function processField(field, value, line) {
  41. switch (field) {
  42. case "event":
  43. eventType = value;
  44. break;
  45. case "data":
  46. data = `${data}${value}
  47. `;
  48. break;
  49. case "id":
  50. id = value.includes("\0") ? void 0 : value;
  51. break;
  52. case "retry":
  53. /^\d+$/.test(value) ? onRetry(parseInt(value, 10)) : onError(
  54. new ParseError(`Invalid \`retry\` value: "${value}"`, {
  55. type: "invalid-retry",
  56. value,
  57. line
  58. })
  59. );
  60. break;
  61. default:
  62. onError(
  63. new ParseError(
  64. `Unknown field "${field.length > 20 ? `${field.slice(0, 20)}\u2026` : field}"`,
  65. { type: "unknown-field", field, value, line }
  66. )
  67. );
  68. break;
  69. }
  70. }
  71. function dispatchEvent() {
  72. data.length > 0 && onEvent({
  73. id,
  74. event: eventType || void 0,
  75. // If the data buffer's last character is a U+000A LINE FEED (LF) character,
  76. // then remove the last character from the data buffer.
  77. data: data.endsWith(`
  78. `) ? data.slice(0, -1) : data
  79. }), id = void 0, data = "", eventType = "";
  80. }
  81. function reset(options = {}) {
  82. incompleteLine && options.consume && parseLine(incompleteLine), isFirstChunk = !0, id = void 0, data = "", eventType = "", incompleteLine = "";
  83. }
  84. return { feed, reset };
  85. }
  86. function splitLines(chunk) {
  87. const lines = [];
  88. let incompleteLine = "", searchIndex = 0;
  89. for (; searchIndex < chunk.length; ) {
  90. const crIndex = chunk.indexOf("\r", searchIndex), lfIndex = chunk.indexOf(`
  91. `, searchIndex);
  92. let lineEnd = -1;
  93. if (crIndex !== -1 && lfIndex !== -1 ? lineEnd = Math.min(crIndex, lfIndex) : crIndex !== -1 ? crIndex === chunk.length - 1 ? lineEnd = -1 : lineEnd = crIndex : lfIndex !== -1 && (lineEnd = lfIndex), lineEnd === -1) {
  94. incompleteLine = chunk.slice(searchIndex);
  95. break;
  96. } else {
  97. const line = chunk.slice(searchIndex, lineEnd);
  98. lines.push(line), searchIndex = lineEnd + 1, chunk[searchIndex - 1] === "\r" && chunk[searchIndex] === `
  99. ` && searchIndex++;
  100. }
  101. }
  102. return [lines, incompleteLine];
  103. }
  104. exports.ParseError = ParseError;
  105. exports.createParser = createParser;
  106. //# sourceMappingURL=index.cjs.map