@steelbreeze/broker
Advanced tools
Comparing version 1.0.0-beta.9 to 1.0.0-beta.10
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var express_1 = require("express"); | ||
// find or create a topic given its name | ||
function getTopic(topics, topic) { | ||
return topics[topic] || (topics[topic] = { lastEventId: -1, data: undefined, subscribers: [] }); | ||
} | ||
// write a single message to a client | ||
function serverSentEvent(res, lastEventId, data) { | ||
res.write("id:" + lastEventId + "\ndata:" + data + "\n\n"); | ||
} | ||
/** | ||
@@ -14,13 +22,5 @@ * Creates an instance of a message broker server. | ||
var topics = {}; | ||
// find or create a topic given its name | ||
function getTopic(topic) { | ||
return topics[topic] || (topics[topic] = { lastEventId: -1, data: undefined, subscribers: [] }); | ||
} | ||
// write a single message to a client | ||
function serverSentEvent(res, lastEventId, data) { | ||
res.write("id:" + lastEventId + "\ndata:" + data + "\n\n"); | ||
} | ||
// GET method is used to subscribe by EventSource clients | ||
router.get('*', function (req, res) { | ||
var topic = getTopic(req.url); | ||
var topic = getTopic(topics, req.url); | ||
// remove the subscription when the connection closes | ||
@@ -33,5 +33,3 @@ req.on('close', function () { | ||
// set the response headers to specify this is an event stream | ||
res.setHeader('Content-Type', 'text/event-stream'); | ||
res.setHeader('Cache-Control', 'no-cache'); | ||
res.setHeader('Connection', 'keep-alive'); | ||
res.set({ 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache', 'Connection': 'keep-alive' }); | ||
// update the client with the last message if available | ||
@@ -44,8 +42,7 @@ if (cacheLastMessage && topic.data) { | ||
router.post('*', function (req, res) { | ||
var topic = getTopic(topics, req.url); | ||
var body = []; | ||
req.on('data', function (chunk) { | ||
body.push(chunk); | ||
}); | ||
req.on('end', function () { | ||
var topic = getTopic(req.url); | ||
}).on('end', function () { | ||
// update the topic with the new event details | ||
@@ -52,0 +49,0 @@ topic.data = Buffer.concat(body).toString(); |
{ | ||
"name": "@steelbreeze/broker", | ||
"version": "1.0.0-beta.9", | ||
"version": "1.0.0-beta.10", | ||
"description": "Lightweight publish and subscribe using Server-Sent Events for node and express", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
import { Router, Request, Response } from 'express'; | ||
// internal interface used to manage the content associated with a topic | ||
interface ITopic { | ||
interface Topic { | ||
lastEventId: number; | ||
@@ -10,2 +10,17 @@ data: string | undefined; | ||
// internatl interface used to manage a dictionary of topics keyed on topic name | ||
interface Topics { | ||
[id: string]: Topic; | ||
} | ||
// find or create a topic given its name | ||
function getTopic(topics: Topics, topic: string): Topic { | ||
return topics[topic] || (topics[topic] = { lastEventId: -1, data: undefined, subscribers: [] }); | ||
} | ||
// write a single message to a client | ||
function serverSentEvent(res: Response, lastEventId: number, data: string): void { | ||
res.write(`id:${lastEventId}\ndata:${data}\n\n`); | ||
} | ||
/** | ||
@@ -19,17 +34,7 @@ * Creates an instance of a message broker server. | ||
const router = Router(); | ||
const topics: { [id: string]: ITopic } = {}; | ||
const topics: Topics = {}; | ||
// find or create a topic given its name | ||
function getTopic(topic: string): ITopic { | ||
return topics[topic] || (topics[topic] = { lastEventId: -1, data: undefined, subscribers: [] }); | ||
} | ||
// write a single message to a client | ||
function serverSentEvent(res: Response, lastEventId: number, data: string): void { | ||
res.write(`id:${lastEventId}\ndata:${data}\n\n`); | ||
} | ||
// GET method is used to subscribe by EventSource clients | ||
router.get('*', (req: Request, res: Response) => { | ||
var topic = getTopic(req.url); | ||
var topic = getTopic(topics, req.url); | ||
@@ -45,5 +50,3 @@ // remove the subscription when the connection closes | ||
// set the response headers to specify this is an event stream | ||
res.setHeader('Content-Type', 'text/event-stream'); | ||
res.setHeader('Cache-Control', 'no-cache'); | ||
res.setHeader('Connection', 'keep-alive'); | ||
res.set({ 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache', 'Connection': 'keep-alive' }); | ||
@@ -58,2 +61,3 @@ // update the client with the last message if available | ||
router.post('*', (req: Request, res: Response) => { | ||
var topic = getTopic(topics, req.url); | ||
var body: Array<Buffer> = []; | ||
@@ -63,7 +67,3 @@ | ||
body.push(chunk); | ||
}); | ||
req.on('end', (): void => { | ||
var topic = getTopic(req.url); | ||
}).on('end', (): void => { | ||
// update the topic with the new event details | ||
@@ -70,0 +70,0 @@ topic.data = Buffer.concat(body).toString(); |
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
24549
17
388