|
| 1 | +// https://www.npmjs.com/package/eventsource-parser/v/1.1.1 |
| 2 | + |
| 3 | +function createParser(onParse) { |
| 4 | + let isFirstChunk |
| 5 | + let bytes |
| 6 | + let buffer |
| 7 | + let startingPosition |
| 8 | + let startingFieldLength |
| 9 | + let eventId |
| 10 | + let eventName |
| 11 | + let data |
| 12 | + reset() |
| 13 | + return { |
| 14 | + feed, |
| 15 | + reset, |
| 16 | + } |
| 17 | + function reset() { |
| 18 | + isFirstChunk = true |
| 19 | + bytes = [] |
| 20 | + buffer = '' |
| 21 | + startingPosition = 0 |
| 22 | + startingFieldLength = -1 |
| 23 | + eventId = void 0 |
| 24 | + eventName = void 0 |
| 25 | + data = '' |
| 26 | + } |
| 27 | + |
| 28 | + function feed(chunk) { |
| 29 | + bytes = bytes.concat(Array.from(chunk)) |
| 30 | + buffer = new TextDecoder().decode(new Uint8Array(bytes)) |
| 31 | + if (isFirstChunk && hasBom(buffer)) { |
| 32 | + buffer = buffer.slice(BOM.length) |
| 33 | + } |
| 34 | + isFirstChunk = false |
| 35 | + const length = buffer.length |
| 36 | + let position = 0 |
| 37 | + let discardTrailingNewline = false |
| 38 | + while (position < length) { |
| 39 | + if (discardTrailingNewline) { |
| 40 | + if (buffer[position] === '\n') { |
| 41 | + ++position |
| 42 | + } |
| 43 | + discardTrailingNewline = false |
| 44 | + } |
| 45 | + let lineLength = -1 |
| 46 | + let fieldLength = startingFieldLength |
| 47 | + let character |
| 48 | + for (let index = startingPosition; lineLength < 0 && index < length; ++index) { |
| 49 | + character = buffer[index] |
| 50 | + if (character === ':' && fieldLength < 0) { |
| 51 | + fieldLength = index - position |
| 52 | + } else if (character === '\r') { |
| 53 | + discardTrailingNewline = true |
| 54 | + lineLength = index - position |
| 55 | + } else if (character === '\n') { |
| 56 | + lineLength = index - position |
| 57 | + } |
| 58 | + } |
| 59 | + if (lineLength < 0) { |
| 60 | + startingPosition = length - position |
| 61 | + startingFieldLength = fieldLength |
| 62 | + break |
| 63 | + } else { |
| 64 | + startingPosition = 0 |
| 65 | + startingFieldLength = -1 |
| 66 | + } |
| 67 | + parseEventStreamLine(buffer, position, fieldLength, lineLength) |
| 68 | + position += lineLength + 1 |
| 69 | + } |
| 70 | + if (position === length) { |
| 71 | + bytes = [] |
| 72 | + buffer = '' |
| 73 | + } else if (position > 0) { |
| 74 | + bytes = bytes.slice(new TextEncoder().encode(buffer.slice(0, position)).length) |
| 75 | + buffer = buffer.slice(position) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + function parseEventStreamLine(lineBuffer, index, fieldLength, lineLength) { |
| 80 | + if (lineLength === 0) { |
| 81 | + if (data.length > 0) { |
| 82 | + onParse({ |
| 83 | + type: 'event', |
| 84 | + id: eventId, |
| 85 | + event: eventName || void 0, |
| 86 | + data: data.slice(0, -1), |
| 87 | + // remove trailing newline |
| 88 | + }) |
| 89 | + |
| 90 | + data = '' |
| 91 | + eventId = void 0 |
| 92 | + } |
| 93 | + eventName = void 0 |
| 94 | + return |
| 95 | + } |
| 96 | + const noValue = fieldLength < 0 |
| 97 | + const field = lineBuffer.slice(index, index + (noValue ? lineLength : fieldLength)) |
| 98 | + let step = 0 |
| 99 | + if (noValue) { |
| 100 | + step = lineLength |
| 101 | + } else if (lineBuffer[index + fieldLength + 1] === ' ') { |
| 102 | + step = fieldLength + 2 |
| 103 | + } else { |
| 104 | + step = fieldLength + 1 |
| 105 | + } |
| 106 | + const position = index + step |
| 107 | + const valueLength = lineLength - step |
| 108 | + const value = lineBuffer.slice(position, position + valueLength).toString() |
| 109 | + if (field === 'data') { |
| 110 | + data += value ? ''.concat(value, '\n') : '\n' |
| 111 | + } else if (field === 'event') { |
| 112 | + eventName = value |
| 113 | + } else if (field === 'id' && !value.includes('\0')) { |
| 114 | + eventId = value |
| 115 | + } else if (field === 'retry') { |
| 116 | + const retry = parseInt(value, 10) |
| 117 | + if (!Number.isNaN(retry)) { |
| 118 | + onParse({ |
| 119 | + type: 'reconnect-interval', |
| 120 | + value: retry, |
| 121 | + }) |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | +const BOM = [239, 187, 191] |
| 127 | +function hasBom(buffer) { |
| 128 | + return BOM.every((charCode, index) => buffer.charCodeAt(index) === charCode) |
| 129 | +} |
| 130 | +export { createParser } |
0 commit comments