@steelbreeze/broker
Advanced tools
Comparing version 1.0.0-beta.10 to 1.0.0-beta.11
@@ -9,4 +9,6 @@ "use strict"; | ||
// write a single message to a client | ||
function serverSentEvent(res, lastEventId, data) { | ||
res.write("id:" + lastEventId + "\ndata:" + data + "\n\n"); | ||
function sendEvent(res, topic) { | ||
if (topic.data) { | ||
res.write("id:" + topic.lastEventId + "\ndata:" + topic.data + "\n\n"); | ||
} | ||
} | ||
@@ -35,8 +37,6 @@ /** | ||
// update the client with the last message if available | ||
if (cacheLastMessage && topic.data) { | ||
serverSentEvent(res, topic.lastEventId, topic.data); | ||
if (cacheLastMessage) { | ||
sendEvent(res, topic); | ||
} | ||
}); | ||
// POST method publishes a message | ||
router.post('*', function (req, res) { | ||
}).post('*', function (req, res) { | ||
var topic = getTopic(topics, req.url); | ||
@@ -49,14 +49,10 @@ var body = []; | ||
topic.data = Buffer.concat(body).toString(); | ||
topic.lastEventId++; | ||
// recycle the message id when maxed out | ||
if (topic.lastEventId === Number.MAX_VALUE) { | ||
topic.lastEventId = 0; | ||
} | ||
topic.lastEventId = (topic.lastEventId === Number.MAX_VALUE ? -1 : topic.lastEventId) + 1; | ||
// queue dispatch of event to all current subscribers | ||
setImmediate(function (subscribers, lastEventId, data) { | ||
for (var _i = 0, subscribers_1 = subscribers; _i < subscribers_1.length; _i++) { | ||
var subscriber = subscribers_1[_i]; | ||
serverSentEvent(subscriber, lastEventId, data); | ||
setImmediate(function (topic) { | ||
for (var _i = 0, _a = topic.subscribers; _i < _a.length; _i++) { | ||
var subscriber = _a[_i]; | ||
sendEvent(subscriber, topic); | ||
} | ||
}, topic.subscribers, topic.lastEventId, topic.data); | ||
}, topic); | ||
}); | ||
@@ -63,0 +59,0 @@ // send response to publisher |
{ | ||
"name": "@steelbreeze/broker", | ||
"version": "1.0.0-beta.10", | ||
"version": "1.0.0-beta.11", | ||
"description": "Lightweight publish and subscribe using Server-Sent Events for node and express", | ||
@@ -30,9 +30,9 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"eventsource": "^1.0.5" | ||
"eventsource": "latest" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^10.5.1", | ||
"@types/express": "^4.16.0", | ||
"express": "^4.16.3" | ||
"@types/express": "latest", | ||
"@types/node": "latest", | ||
"express": "latest" | ||
} | ||
} |
@@ -21,4 +21,6 @@ import { Router, Request, Response } from 'express'; | ||
// write a single message to a client | ||
function serverSentEvent(res: Response, lastEventId: number, data: string): void { | ||
res.write(`id:${lastEventId}\ndata:${data}\n\n`); | ||
function sendEvent(res: Response, topic: Topic): void { | ||
if (topic.data) { | ||
res.write(`id:${topic.lastEventId}\ndata:${topic.data}\n\n`); | ||
} | ||
} | ||
@@ -52,9 +54,6 @@ | ||
// update the client with the last message if available | ||
if (cacheLastMessage && topic.data) { | ||
serverSentEvent(res, topic.lastEventId, topic.data); | ||
if (cacheLastMessage) { | ||
sendEvent(res, topic); | ||
} | ||
}); | ||
// POST method publishes a message | ||
router.post('*', (req: Request, res: Response) => { | ||
}).post('*', (req: Request, res: Response) => { | ||
var topic = getTopic(topics, req.url); | ||
@@ -68,15 +67,10 @@ var body: Array<Buffer> = []; | ||
topic.data = Buffer.concat(body).toString(); | ||
topic.lastEventId++; | ||
topic.lastEventId = (topic.lastEventId === Number.MAX_VALUE ? -1 : topic.lastEventId) + 1; | ||
// recycle the message id when maxed out | ||
if (topic.lastEventId === Number.MAX_VALUE) { | ||
topic.lastEventId = 0; | ||
} | ||
// queue dispatch of event to all current subscribers | ||
setImmediate((subscribers: Response[], lastEventId, data: string) => { | ||
for (var subscriber of subscribers) { | ||
serverSentEvent(subscriber, lastEventId, data); | ||
setImmediate((topic: Topic) => { | ||
for (var subscriber of topic.subscribers) { | ||
sendEvent(subscriber, topic); | ||
} | ||
}, topic.subscribers, topic.lastEventId, topic.data); | ||
}, topic); | ||
}); | ||
@@ -83,0 +77,0 @@ |
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
24039
380
+ Addedeventsource@2.0.2(transitive)
- Removedeventsource@1.1.2(transitive)
Updatedeventsource@latest