Skip to content

Commit

Permalink
style(*): new renaming (on internal symbols only) to respect case con…
Browse files Browse the repository at this point in the history
…ventions
  • Loading branch information
toverux committed May 28, 2017
1 parent 2e7e812 commit 97ffe6d
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ export class Hub {
this.clients.delete(funcs);
}

public data(data: fmt.SSEValue, id?: string): void {
public data(data: fmt.SseValue, id?: string): void {
this.clients.forEach(client => client.data(data, id));
}

public event(event: string, data: fmt.SSEValue, id?: string): void {
public event(event: string, data: fmt.SseValue, id?: string): void {
this.clients.forEach(client => client.event(event, data, id));
}

Expand Down
30 changes: 15 additions & 15 deletions src/sse_formatter.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
export type SSEField = 'event'|'data'|'id'|'retry'; // keyof fieldBufs
export type SseField = 'event'|'data'|'id'|'retry'; // keyof fieldBufs

export type SSEValue = Buffer|any;
export type SseValue = Buffer|any;

export type SSESerializer = (value: any) => string|Buffer;
export type SseSerializer = (value: any) => string|Buffer;

export interface ISSEBlockConfiguration {
[field: string /* in fact SSEField */]: SSEValue;
export interface ISseBlockConfiguration {
[field: string /* in fact SseField */]: SseValue;
}

const fieldBuffers = {
Expand All @@ -18,8 +18,8 @@ const fieldBuffers = {

const eolBuf = Buffer.from('\n');

const stringSerialize: SSESerializer = String;
const jsonSerialize: SSESerializer = JSON.stringify;
const stringSerialize: SseSerializer = String;
const jsonSerialize: SseSerializer = JSON.stringify;

/**
* Creates a Buffer for a SSE "instruction" -- `event: myEvent\n`
Expand All @@ -28,7 +28,7 @@ const jsonSerialize: SSESerializer = JSON.stringify;
* @param value The instruction value
* @param serializer Value serializer for `data`
*/
export function instruction(field: SSEField, value: SSEValue, serializer?: SSESerializer): Buffer {
export function instruction(field: SseField, value: SseValue, serializer?: SseSerializer): Buffer {
return Buffer.concat([
fieldBuffers[field], toBuffer(value, serializer), eolBuf
]);
Expand All @@ -40,7 +40,7 @@ export function instruction(field: SSEField, value: SSEValue, serializer?: SSESe
* @param comment The comment message
*/
export function comment(comment: string): Buffer {
return instruction('__comment__' as SSEField, comment, stringSerialize);
return instruction('__comment__' as SseField, comment, stringSerialize);
}

/**
Expand All @@ -49,12 +49,12 @@ export function comment(comment: string): Buffer {
* @param instructions An object map of SSEFields to SSEValues
* @param serializer Value serializer for `data`
*/
export function block(instructions: ISSEBlockConfiguration, serializer?: SSESerializer): Buffer {
export function block(instructions: ISseBlockConfiguration, serializer?: SseSerializer): Buffer {
const lines = Object.keys(instructions).map((field) => {
const fieldSerializer = field === 'data' ? serializer : stringSerialize;

return instruction(
field as SSEField,
field as SseField,
toBuffer(instructions[field], fieldSerializer)
);
});
Expand All @@ -73,11 +73,11 @@ export function block(instructions: ISSEBlockConfiguration, serializer?: SSESeri
*/
export function message(
event: string|null,
data: SSEValue,
data: SseValue,
id?: string,
serializer?: SSESerializer
serializer?: SseSerializer
): Buffer {
const frame: ISSEBlockConfiguration = {};
const frame: ISseBlockConfiguration = {};

id != null && (frame.id = id);
event != null && (frame.event = event);
Expand All @@ -97,7 +97,7 @@ export function message(
* @param value The value to serialize
* @param serializer Value serializer
*/
function toBuffer(value: SSEValue, serializer: SSESerializer = jsonSerialize) {
function toBuffer(value: SseValue, serializer: SseSerializer = jsonSerialize) {
if (Buffer.isBuffer(value)) {
return value;
}
Expand Down
7 changes: 5 additions & 2 deletions src/sse_handler_middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ export interface ISseMiddlewareOptions {
/**
* Serializer function applied on all messages' data field (except when you direclty pass a Buffer).
* SSE comments are not serialized using this function.
* Defaults to JSON.stringify().
*
* @default JSON.stringify
*/
serializer: fmt.SSESerializer;
serializer: fmt.SseSerializer;

/**
* Determines the interval, in milliseconds, between keep-alive packets (neutral SSE comments).
*
* @default 5000
*/
keepAliveInterval: number;
}
Expand Down
4 changes: 4 additions & 0 deletions src/sse_hub_middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export interface ISseHubResponse extends Response {
}

export interface ISseHubMiddlewareOptions extends ISseMiddlewareOptions {
/**
* You can pass a Hub instance for controlling the stream outside of the middleware.
* Otherwise, a Hub is automatically created.
*/
hub: Hub;
}

Expand Down
8 changes: 4 additions & 4 deletions src/sse_middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface ISseFunctions {
* @param data The event data1
* @param id The event ID, useful for replay thanks to the Last-Event-ID header
*/
data(data: fmt.SSEValue, id?: string): void;
data(data: fmt.SseValue, id?: string): void;

/**
* Writes a standard SSE message (with named event) on the socket.
Expand All @@ -28,7 +28,7 @@ export interface ISseFunctions {
* @param data The event data (mandatory!)
* @param id The event ID, useful for replay thanks to the Last-Event-ID header
*/
event(event: string, data: fmt.SSEValue, id?: string): void;
event(event: string, data: fmt.SseValue, id?: string): void;

/**
* Writes a standard SSE comment on the socket.
Expand Down Expand Up @@ -59,10 +59,10 @@ export function sse(options: Partial<ISseMiddlewareOptions> = {}): Handler {
function middleware(req: Request, res: Response, next: NextFunction): void {
//=> Install the sse*() functions on Express' Response
(res as ISseResponse).sse = {
data(data: fmt.SSEValue, id?: string) {
data(data: fmt.SseValue, id?: string) {
res.write(fmt.message(null, data, id, serializer));
},
event(event: string, data: fmt.SSEValue, id?: string) {
event(event: string, data: fmt.SseValue, id?: string) {
res.write(fmt.message(event, data, id, serializer));
},
comment(comment: string) {
Expand Down
2 changes: 1 addition & 1 deletion test/sse_formatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('sse_formatter', () => {

describe('instruction()', () => {
it('formats a SSE instruction', () => {
availSseFields.forEach((field: sseFormatter.SSEField) => {
availSseFields.forEach((field: sseFormatter.SseField) => {
const line = sseFormatter.instruction(field, 'message', testSerializer).toString();

expect(line).to.equal(`${field}: ((message))\n`);
Expand Down

0 comments on commit 97ffe6d

Please sign in to comment.