eventsource-parser
Advanced tools
Comparing version 1.1.2 to 2.0.0
function createParser(onParse) { | ||
let isFirstChunk; | ||
let buffer; | ||
let startingPosition; | ||
let startingFieldLength; | ||
let eventId; | ||
let eventName; | ||
let data; | ||
reset(); | ||
return { | ||
feed, | ||
reset | ||
}; | ||
let isFirstChunk, buffer, startingPosition, startingFieldLength, eventId, eventName, data; | ||
return reset(), { feed, reset }; | ||
function reset() { | ||
isFirstChunk = true; | ||
buffer = ""; | ||
startingPosition = 0; | ||
startingFieldLength = -1; | ||
eventId = void 0; | ||
eventName = void 0; | ||
data = ""; | ||
isFirstChunk = !0, buffer = "", startingPosition = 0, startingFieldLength = -1, eventId = void 0, eventName = void 0, data = ""; | ||
} | ||
function feed(chunk) { | ||
buffer = buffer ? buffer + chunk : chunk; | ||
if (isFirstChunk && hasBom(buffer)) { | ||
buffer = buffer.slice(BOM.length); | ||
} | ||
isFirstChunk = false; | ||
buffer = buffer ? buffer + chunk : chunk, isFirstChunk && hasBom(buffer) && (buffer = buffer.slice(BOM.length)), isFirstChunk = !1; | ||
const length = buffer.length; | ||
let position = 0; | ||
let discardTrailingNewline = false; | ||
while (position < length) { | ||
if (discardTrailingNewline) { | ||
if (buffer[position] === "\n") { | ||
++position; | ||
} | ||
discardTrailingNewline = false; | ||
} | ||
let lineLength = -1; | ||
let fieldLength = startingFieldLength; | ||
let character; | ||
for (let index = startingPosition; lineLength < 0 && index < length; ++index) { | ||
character = buffer[index]; | ||
if (character === ":" && fieldLength < 0) { | ||
fieldLength = index - position; | ||
} else if (character === "\r") { | ||
discardTrailingNewline = true; | ||
lineLength = index - position; | ||
} else if (character === "\n") { | ||
lineLength = index - position; | ||
} | ||
} | ||
let position = 0, discardTrailingNewline = !1; | ||
for (; position < length; ) { | ||
discardTrailingNewline && (buffer[position] === ` | ||
` && ++position, discardTrailingNewline = !1); | ||
let lineLength = -1, fieldLength = startingFieldLength, character; | ||
for (let index = startingPosition; lineLength < 0 && index < length; ++index) | ||
character = buffer[index], character === ":" && fieldLength < 0 ? fieldLength = index - position : character === "\r" ? (discardTrailingNewline = !0, lineLength = index - position) : character === ` | ||
` && (lineLength = index - position); | ||
if (lineLength < 0) { | ||
startingPosition = length - position; | ||
startingFieldLength = fieldLength; | ||
startingPosition = length - position, startingFieldLength = fieldLength; | ||
break; | ||
} else { | ||
startingPosition = 0; | ||
startingFieldLength = -1; | ||
} | ||
parseEventStreamLine(buffer, position, fieldLength, lineLength); | ||
position += lineLength + 1; | ||
} else | ||
startingPosition = 0, startingFieldLength = -1; | ||
parseEventStreamLine(buffer, position, fieldLength, lineLength), position += lineLength + 1; | ||
} | ||
if (position === length) { | ||
buffer = ""; | ||
} else if (position > 0) { | ||
buffer = buffer.slice(position); | ||
} | ||
position === length ? buffer = "" : position > 0 && (buffer = buffer.slice(position)); | ||
} | ||
function parseEventStreamLine(lineBuffer, index, fieldLength, lineLength) { | ||
if (lineLength === 0) { | ||
if (data.length > 0) { | ||
onParse({ | ||
type: "event", | ||
id: eventId, | ||
event: eventName || void 0, | ||
data: data.slice(0, -1) | ||
// remove trailing newline | ||
}); | ||
data = ""; | ||
eventId = void 0; | ||
} | ||
eventName = void 0; | ||
data.length > 0 && (onParse({ | ||
type: "event", | ||
id: eventId, | ||
event: eventName || void 0, | ||
data: data.slice(0, -1) | ||
// remove trailing newline | ||
}), data = "", eventId = void 0), eventName = void 0; | ||
return; | ||
} | ||
const noValue = fieldLength < 0; | ||
const field = lineBuffer.slice(index, index + (noValue ? lineLength : fieldLength)); | ||
const noValue = fieldLength < 0, field = lineBuffer.slice(index, index + (noValue ? lineLength : fieldLength)); | ||
let step = 0; | ||
if (noValue) { | ||
step = lineLength; | ||
} else if (lineBuffer[index + fieldLength + 1] === " ") { | ||
step = fieldLength + 2; | ||
} else { | ||
step = fieldLength + 1; | ||
} | ||
const position = index + step; | ||
const valueLength = lineLength - step; | ||
const value = lineBuffer.slice(position, position + valueLength).toString(); | ||
if (field === "data") { | ||
data += value ? "".concat(value, "\n") : "\n"; | ||
} else if (field === "event") { | ||
noValue ? step = lineLength : lineBuffer[index + fieldLength + 1] === " " ? step = fieldLength + 2 : step = fieldLength + 1; | ||
const position = index + step, valueLength = lineLength - step, value = lineBuffer.slice(position, position + valueLength).toString(); | ||
if (field === "data") | ||
data += value ? `${value} | ||
` : ` | ||
`; | ||
else if (field === "event") | ||
eventName = value; | ||
} else if (field === "id" && !value.includes("\0")) { | ||
else if (field === "id" && !value.includes("\0")) | ||
eventId = value; | ||
} else if (field === "retry") { | ||
else if (field === "retry") { | ||
const retry = parseInt(value, 10); | ||
if (!Number.isNaN(retry)) { | ||
onParse({ | ||
type: "reconnect-interval", | ||
value: retry | ||
}); | ||
} | ||
Number.isNaN(retry) || onParse({ type: "reconnect-interval", value: retry }); | ||
} | ||
@@ -121,3 +60,5 @@ } | ||
} | ||
export { createParser }; | ||
export { | ||
createParser | ||
}; | ||
//# sourceMappingURL=index.js.map |
@@ -1,2 +0,2 @@ | ||
import { createParser } from './index.js'; | ||
import { createParser } from "./index.js"; | ||
class EventSourceParserStream extends TransformStream { | ||
@@ -7,6 +7,4 @@ constructor() { | ||
start(controller) { | ||
parser = createParser(event => { | ||
if (event.type === "event") { | ||
controller.enqueue(event); | ||
} | ||
parser = createParser((event) => { | ||
event.type === "event" && controller.enqueue(event); | ||
}); | ||
@@ -20,3 +18,5 @@ }, | ||
} | ||
export { EventSourceParserStream }; | ||
export { | ||
EventSourceParserStream | ||
}; | ||
//# sourceMappingURL=stream.js.map |
{ | ||
"name": "eventsource-parser", | ||
"version": "1.1.2", | ||
"version": "2.0.0", | ||
"description": "Streaming, source-agnostic EventSource/Server-Sent Events parser", | ||
"keywords": [ | ||
"sse", | ||
"eventsource", | ||
"server-sent-events" | ||
], | ||
"homepage": "https://github.com/rexxars/eventsource-parser#readme", | ||
"bugs": { | ||
"url": "https://github.com/rexxars/eventsource-parser/issues" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+ssh://git@github.com/rexxars/eventsource-parser.git" | ||
}, | ||
"license": "MIT", | ||
"author": "Espen Hovlandsdal <espen@hovlandsdal.com>", | ||
"sideEffects": false, | ||
"type": "module", | ||
"types": "./dist/index.d.ts", | ||
"module": "./dist/index.esm.js", | ||
"main": "./dist/index.cjs", | ||
"exports": { | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"source": "./src/index.ts", | ||
@@ -31,3 +18,2 @@ "import": "./dist/index.js", | ||
"./stream": { | ||
"types": "./dist/stream.d.ts", | ||
"source": "./src/stream.ts", | ||
@@ -40,10 +26,2 @@ "import": "./dist/stream.js", | ||
}, | ||
"main": "./dist/index.cjs", | ||
"module": "./dist/index.js", | ||
"source": "./src/index.ts", | ||
"types": "./dist/index.d.ts", | ||
"files": [ | ||
"dist", | ||
"src" | ||
], | ||
"typesVersions": { | ||
@@ -56,2 +34,16 @@ "*": { | ||
}, | ||
"engines": { | ||
"node": ">=18.0.0" | ||
}, | ||
"browserslist": [ | ||
"node >= 18", | ||
"chrome >= 71", | ||
"safari >= 14.1", | ||
"firefox >= 105", | ||
"edge >= 79" | ||
], | ||
"files": [ | ||
"dist", | ||
"src" | ||
], | ||
"scripts": { | ||
@@ -66,2 +58,33 @@ "prebuild": "npm run clean", | ||
}, | ||
"author": "Espen Hovlandsdal <espen@hovlandsdal.com>", | ||
"keywords": [ | ||
"sse", | ||
"eventsource", | ||
"server-sent-events" | ||
], | ||
"devDependencies": { | ||
"@sanity/pkg-utils": "^6.10.9", | ||
"@sanity/semantic-release-preset": "^5.0.0", | ||
"@types/node": "^18.0.0", | ||
"@typescript-eslint/eslint-plugin": "^7.0.0", | ||
"@typescript-eslint/parser": "^7.0.0", | ||
"eslint": "^8.51.0", | ||
"eslint-config-prettier": "^9.1.0", | ||
"eslint-config-sanity": "^7.1.2", | ||
"prettier": "^3.3.3", | ||
"rimraf": "^6.0.1", | ||
"rollup-plugin-visualizer": "^5.12.0", | ||
"semantic-release": "^24.0.0", | ||
"typescript": "^5.5.4", | ||
"vitest": "^2.0.5" | ||
}, | ||
"homepage": "https://github.com/rexxars/eventsource-parser#readme", | ||
"bugs": { | ||
"url": "https://github.com/rexxars/eventsource-parser/issues" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+ssh://git@github.com/rexxars/eventsource-parser.git" | ||
}, | ||
"license": "MIT", | ||
"prettier": { | ||
@@ -89,22 +112,3 @@ "bracketSpacing": false, | ||
] | ||
}, | ||
"devDependencies": { | ||
"@sanity/pkg-utils": "^2.4.9", | ||
"@sanity/semantic-release-preset": "^4.0.2", | ||
"@types/node": "^14.18.0", | ||
"@typescript-eslint/eslint-plugin": "^6.7.2", | ||
"@typescript-eslint/parser": "^6.7.2", | ||
"eslint": "^8.49.0", | ||
"eslint-config-prettier": "^9.0.0", | ||
"eslint-config-sanity": "^6.0.0", | ||
"prettier": "^3.0.3", | ||
"rimraf": "^5.0.1", | ||
"rollup-plugin-visualizer": "^5.9.2", | ||
"semantic-release": "^21.0.0", | ||
"typescript": "^5.2.2", | ||
"vitest": "^0.34.4" | ||
}, | ||
"engines": { | ||
"node": ">=14.18" | ||
} | ||
} |
@@ -0,8 +1,8 @@ | ||
export {createParser} from './parse.js' | ||
export type { | ||
EventSourceParseCallback, | ||
EventSourceParser, | ||
ParsedEvent, | ||
ParseEvent, | ||
ReconnectInterval, | ||
ParseEvent, | ||
EventSourceParseCallback, | ||
} from './types.js' | ||
export {createParser} from './parse.js' |
@@ -0,3 +1,3 @@ | ||
import {createParser} from './parse.js' | ||
import type {EventSourceParser, ParsedEvent} from './types.js' | ||
import {createParser} from './parse.js' | ||
@@ -4,0 +4,0 @@ /** |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
214011
24
655