@azure/web-pubsub-express 1.0.0-alpha.20211001.2 → 1.0.0-alpha.20211108.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +23 -1
- package/README.md +127 -31
- package/dist/index.js +52 -24
- package/dist/index.js.map +1 -1
- package/dist-esm/samples-dev/server.js +3 -3
- package/dist-esm/samples-dev/server.js.map +1 -1
- package/dist-esm/src/cloudEventsDispatcher.js +38 -18
- package/dist-esm/src/cloudEventsDispatcher.js.map +1 -1
- package/dist-esm/src/cloudEventsProtocols.js.map +1 -1
- package/dist-esm/src/logger.js +10 -0
- package/dist-esm/src/logger.js.map +1 -0
- package/dist-esm/src/webPubSubEventHandler.js +6 -6
- package/dist-esm/src/webPubSubEventHandler.js.map +1 -1
- package/dist-esm/test/connect.spec.js +25 -24
- package/dist-esm/test/connect.spec.js.map +1 -1
- package/dist-esm/test/connected.spec.js +11 -10
- package/dist-esm/test/connected.spec.js.map +1 -1
- package/dist-esm/test/ctor.spec.js +3 -3
- package/dist-esm/test/ctor.spec.js.map +1 -1
- package/dist-esm/test/disconnected.spec.js +11 -10
- package/dist-esm/test/disconnected.spec.js.map +1 -1
- package/dist-esm/test/user.spec.js +21 -20
- package/dist-esm/test/user.spec.js.map +1 -1
- package/dist-esm/test/validate.spec.js +26 -10
- package/dist-esm/test/validate.spec.js.map +1 -1
- package/package.json +2 -1
- package/types/web-pubsub-express.d.ts +11 -7
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
// Licensed under the MIT license.
|
|
3
3
|
import { HTTP } from "cloudevents";
|
|
4
4
|
import { URL } from "url";
|
|
5
|
+
import { logger } from "./logger";
|
|
5
6
|
import * as utils from "./utils";
|
|
6
7
|
var EventType;
|
|
7
8
|
(function (EventType) {
|
|
@@ -109,29 +110,51 @@ function tryGetWebPubSubEvent(req) {
|
|
|
109
110
|
return undefined;
|
|
110
111
|
}
|
|
111
112
|
}
|
|
113
|
+
function isWebPubSubRequest(req) {
|
|
114
|
+
return utils.getHttpHeader(req, "ce-awpsversion") !== undefined;
|
|
115
|
+
}
|
|
112
116
|
/**
|
|
113
117
|
* @internal
|
|
114
118
|
*/
|
|
115
119
|
export class CloudEventsDispatcher {
|
|
116
|
-
constructor(hub,
|
|
117
|
-
var _a;
|
|
120
|
+
constructor(hub, eventHandler) {
|
|
118
121
|
this.hub = hub;
|
|
119
122
|
this.eventHandler = eventHandler;
|
|
120
|
-
this.
|
|
121
|
-
this._allowedOrigins =
|
|
123
|
+
this._allowAll = true;
|
|
124
|
+
this._allowedOrigins = [];
|
|
125
|
+
if (Array.isArray(eventHandler)) {
|
|
126
|
+
throw new Error("Unexpected WebPubSubEventHandlerOptions");
|
|
127
|
+
}
|
|
128
|
+
if ((eventHandler === null || eventHandler === void 0 ? void 0 : eventHandler.allowedEndpoints) !== undefined) {
|
|
129
|
+
this._allowedOrigins = eventHandler.allowedEndpoints.map((endpoint) => new URL(endpoint).host.toLowerCase());
|
|
130
|
+
this._allowAll = false;
|
|
131
|
+
}
|
|
122
132
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
133
|
+
handlePreflight(req, res) {
|
|
134
|
+
var _a;
|
|
135
|
+
if (!isWebPubSubRequest(req)) {
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
const origin = (_a = utils.getHttpHeader(req, "webhook-request-origin")) === null || _a === void 0 ? void 0 : _a.toLowerCase();
|
|
139
|
+
if (origin === undefined) {
|
|
140
|
+
logger.warning("Expecting webhook-request-origin header.");
|
|
141
|
+
res.statusCode = 400;
|
|
142
|
+
}
|
|
143
|
+
else if (this._allowAll || this._allowedOrigins.indexOf(origin) > -1) {
|
|
144
|
+
res.setHeader("WebHook-Allowed-Origin", origin);
|
|
128
145
|
}
|
|
129
146
|
else {
|
|
130
|
-
|
|
147
|
+
logger.warning("Origin does not match the allowed origins: " + this._allowedOrigins);
|
|
148
|
+
res.statusCode = 400;
|
|
131
149
|
}
|
|
150
|
+
res.end();
|
|
151
|
+
return true;
|
|
132
152
|
}
|
|
133
|
-
async
|
|
153
|
+
async handleRequest(request, response) {
|
|
134
154
|
var _a, _b, _c, _d, _e;
|
|
155
|
+
if (!isWebPubSubRequest(request)) {
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
135
158
|
// check if it is a valid WebPubSub cloud events
|
|
136
159
|
const origin = utils.getHttpHeader(request, "webhook-request-origin");
|
|
137
160
|
if (origin === undefined) {
|
|
@@ -150,8 +173,7 @@ export class CloudEventsDispatcher {
|
|
|
150
173
|
switch (eventType) {
|
|
151
174
|
case EventType.Connect:
|
|
152
175
|
if (!((_a = this.eventHandler) === null || _a === void 0 ? void 0 : _a.handleConnect)) {
|
|
153
|
-
response.
|
|
154
|
-
response.end("Connect event handler is not configured.");
|
|
176
|
+
response.end();
|
|
155
177
|
return true;
|
|
156
178
|
}
|
|
157
179
|
break;
|
|
@@ -174,14 +196,12 @@ export class CloudEventsDispatcher {
|
|
|
174
196
|
}
|
|
175
197
|
break;
|
|
176
198
|
default:
|
|
177
|
-
|
|
199
|
+
logger.warning(`Unknown EventType ${eventType}`);
|
|
178
200
|
return false;
|
|
179
201
|
}
|
|
180
202
|
const eventRequest = await utils.convertHttpToEvent(request);
|
|
181
203
|
const receivedEvent = HTTP.toEvent(eventRequest);
|
|
182
|
-
|
|
183
|
-
console.log(receivedEvent);
|
|
184
|
-
}
|
|
204
|
+
logger.verbose(receivedEvent);
|
|
185
205
|
switch (eventType) {
|
|
186
206
|
case EventType.Connect: {
|
|
187
207
|
const connectRequest = receivedEvent.data;
|
|
@@ -230,7 +250,7 @@ export class CloudEventsDispatcher {
|
|
|
230
250
|
return true;
|
|
231
251
|
}
|
|
232
252
|
default:
|
|
233
|
-
|
|
253
|
+
logger.warning(`Unknown EventType ${eventType}`);
|
|
234
254
|
return false;
|
|
235
255
|
}
|
|
236
256
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|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Q,CAAC;wBACtD,QAAQ,EAAE,QAAQ;qBACnB,CAAC;iBACH;qBAAM,IAAI,aAAa,CAAC,IAAI,KAAK,SAAS,EAAE;oBAC3C,WAAW,GAAG;wBACZ,OAAO,EAAE,UAAU,CAAC,aAAa,EAAE,MAAM,CAAC;wBAC1C,IAAI,EAAE,aAAa,CAAC,IAAc;wBAClC,QAAQ,EAAE,CAAA,MAAA,aAAa,CAAC,eAAe,0CAAE,UAAU,CAAC,mBAAmB,CAAC;4BACtE,CAAC,CAAC,MAAM;4BACR,CAAC,CAAC,MAAM;qBACX,CAAC;iBACH;qBAAM;oBACL,MAAM,IAAI,KAAK,CAAC,kBAAkB,CAAC,CAAC;iBACrC;gBAED,IAAI,CAAC,YAAY,CAAC,eAAgB,CAChC,WAAW,EACX,2BAA2B,CAAC,WAAW,EAAE,QAAQ,CAAC,CACnD,CAAC;gBACF,OAAO,IAAI,CAAC;aACb;YACD;gBACE,OAAO,CAAC,IAAI,CAAC,qBAAqB,SAAS,EAAE,CAAC,CAAC;gBAC/C,OAAO,KAAK,CAAC;SAChB;IACH,CAAC;CACF","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\nimport { HTTP, CloudEvent } from \"cloudevents\";\nimport { IncomingMessage, ServerResponse } from \"http\";\nimport { URL } from \"url\";\n\nimport * as utils from \"./utils\";\n\nimport {\n ConnectRequest,\n ConnectResponse,\n UserEventRequest,\n DisconnectedRequest,\n ConnectedRequest,\n ConnectionContext,\n WebPubSubEventHandlerOptions,\n ConnectResponseHandler,\n UserEventResponseHandler\n} from \"./cloudEventsProtocols\";\n\nenum EventType {\n Connect,\n Connected,\n Disconnected,\n UserEvent\n}\n\nfunction getConnectResponseHandler(\n connectRequest: ConnectRequest,\n response: ServerResponse\n): ConnectResponseHandler {\n const states: Record<string, any> = connectRequest.context.states;\n let modified = false;\n const handler = {\n setState(name: string, value: unknown): void {\n states[name] = value;\n modified = true;\n },\n success(res?: ConnectResponse): void {\n response.statusCode = 200;\n if (modified) {\n response.setHeader(\"ce-connectionState\", utils.toBase64JsonString(states));\n }\n if (res === undefined) {\n response.end();\n } else {\n response.setHeader(\"Content-Type\", \"application/json; charset=utf-8\");\n response.end(JSON.stringify(res));\n }\n },\n fail(code: 400 | 401 | 500, detail?: string): void {\n response.statusCode = code;\n response.end(detail ?? \"\");\n }\n };\n\n return handler;\n}\n\nfunction getUserEventResponseHandler(\n userRequest: UserEventRequest,\n response: ServerResponse\n): UserEventResponseHandler {\n const states: Record<string, any> = userRequest.context.states;\n let modified = false;\n const handler = {\n setState(name: string, value: unknown): void {\n modified = true;\n states[name] = value;\n },\n success(data?: string | ArrayBuffer, dataType?: \"binary\" | \"text\" | \"json\"): void {\n response.statusCode = 200;\n if (modified) {\n response.setHeader(\"ce-connectionState\", utils.toBase64JsonString(states));\n }\n\n switch (dataType) {\n case \"json\":\n response.setHeader(\"Content-Type\", \"application/json; charset=utf-8\");\n break;\n case \"text\":\n response.setHeader(\"Content-Type\", \"text/plain; charset=utf-8\");\n break;\n default:\n response.setHeader(\"Content-Type\", \"application/octet-stream\");\n break;\n }\n response.end(data ?? \"\");\n },\n fail(code: 400 | 401 | 500, detail?: string): void {\n response.statusCode = code;\n response.end(detail ?? \"\");\n }\n };\n return handler;\n}\n\nfunction getContext(ce: CloudEvent, origin: string): ConnectionContext {\n const context = {\n signature: ce[\"signature\"] as string,\n userId: ce[\"userid\"] as string,\n hub: ce[\"hub\"] as string,\n connectionId: ce[\"connectionid\"] as string,\n eventName: ce[\"eventname\"] as string,\n origin: origin,\n states: utils.fromBase64JsonString(ce[\"connectionstate\"] as string)\n };\n\n // TODO: validation\n return context;\n}\n\nfunction tryGetWebPubSubEvent(req: IncomingMessage): EventType | undefined {\n // check ce-type to see if it is a valid WebPubSub CloudEvent request\n const prefix = \"azure.webpubsub.\";\n const connect = \"azure.webpubsub.sys.connect\";\n const connected = \"azure.webpubsub.sys.connected\";\n const disconnectd = \"azure.webpubsub.sys.disconnected\";\n const userPrefix = \"azure.webpubsub.user.\";\n const type = utils.getHttpHeader(req, \"ce-type\");\n if (!type?.startsWith(prefix)) {\n return undefined;\n }\n if (type.startsWith(userPrefix)) {\n return EventType.UserEvent;\n }\n switch (type) {\n case connect:\n return EventType.Connect;\n case connected:\n return EventType.Connected;\n case disconnectd:\n return EventType.Disconnected;\n default:\n return undefined;\n }\n}\n\n/**\n * @internal\n */\nexport class CloudEventsDispatcher {\n private readonly _dumpRequest: boolean;\n private readonly _allowedOrigins: string[];\n constructor(\n private hub: string,\n allowedEndpoints: string[],\n private eventHandler?: WebPubSubEventHandlerOptions\n ) {\n this._dumpRequest = eventHandler?.dumpRequest ?? false;\n this._allowedOrigins = allowedEndpoints.map((endpoint) =>\n endpoint === \"*\" ? \"*\" : new URL(endpoint).host\n );\n }\n\n public processValidateRequest(req: IncomingMessage, res: ServerResponse): boolean {\n if (req.headers[\"webhook-request-origin\"]) {\n res.setHeader(\"WebHook-Allowed-Origin\", this._allowedOrigins);\n res.end();\n return true;\n } else {\n return false;\n }\n }\n\n public async processRequest(\n request: IncomingMessage,\n response: ServerResponse\n ): Promise<boolean> {\n // check if it is a valid WebPubSub cloud events\n const origin = utils.getHttpHeader(request, \"webhook-request-origin\");\n if (origin === undefined) {\n return false;\n }\n\n const eventType = tryGetWebPubSubEvent(request);\n if (eventType === undefined) {\n return false;\n }\n\n // check if hub matches\n const hub = utils.getHttpHeader(request, \"ce-hub\");\n if (hub !== this.hub) {\n return false;\n }\n\n // No need to read body if handler is not specified\n switch (eventType) {\n case EventType.Connect:\n if (!this.eventHandler?.handleConnect) {\n response.statusCode = 401;\n response.end(\"Connect event handler is not configured.\");\n return true;\n }\n break;\n case EventType.Connected:\n if (!this.eventHandler?.onConnected) {\n response.end();\n return true;\n }\n break;\n case EventType.Disconnected:\n if (!this.eventHandler?.onDisconnected) {\n response.end();\n return true;\n }\n break;\n case EventType.UserEvent:\n if (!this.eventHandler?.handleUserEvent) {\n response.end();\n return true;\n }\n break;\n default:\n console.warn(`Unknown EventType ${eventType}`);\n return false;\n }\n\n const eventRequest = await utils.convertHttpToEvent(request);\n const receivedEvent = HTTP.toEvent(eventRequest);\n\n if (this._dumpRequest) {\n console.log(receivedEvent);\n }\n\n switch (eventType) {\n case EventType.Connect: {\n const connectRequest = receivedEvent.data as ConnectRequest;\n connectRequest.context = getContext(receivedEvent, origin);\n this.eventHandler.handleConnect!(\n connectRequest,\n getConnectResponseHandler(connectRequest, response)\n );\n return true;\n }\n case EventType.Connected: {\n // for unblocking events, we responds to the service as early as possible\n response.end();\n const connectedRequest = receivedEvent.data as ConnectedRequest;\n connectedRequest.context = getContext(receivedEvent, origin);\n this.eventHandler.onConnected!(connectedRequest);\n return true;\n }\n case EventType.Disconnected: {\n // for unblocking events, we responds to the service as early as possible\n response.end();\n const disconnectedRequest = receivedEvent.data as DisconnectedRequest;\n disconnectedRequest.context = getContext(receivedEvent, origin);\n this.eventHandler.onDisconnected!(disconnectedRequest);\n return true;\n }\n case EventType.UserEvent: {\n let userRequest: UserEventRequest;\n if (receivedEvent.data_base64 !== undefined) {\n userRequest = {\n context: getContext(receivedEvent, origin),\n data: Buffer.from(receivedEvent.data_base64, \"base64\"),\n dataType: \"binary\"\n };\n } else if (receivedEvent.data !== undefined) {\n userRequest = {\n context: getContext(receivedEvent, origin),\n data: receivedEvent.data as string,\n dataType: receivedEvent.datacontenttype?.startsWith(\"application/json;\")\n ? \"json\"\n : \"text\"\n };\n } else {\n throw new Error(\"Unexpected data.\");\n }\n\n this.eventHandler.handleUserEvent!(\n userRequest,\n getUserEventResponseHandler(userRequest, response)\n );\n return true;\n }\n default:\n console.warn(`Unknown EventType ${eventType}`);\n return false;\n }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"cloudEventsDispatcher.js","sourceRoot":"","sources":["../../src/cloudEventsDispatcher.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC,OAAO,EAAE,IAAI,EAAc,MAAM,aAAa,CAAC;AAE/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CAAC;gBACjD,OAAO,KAAK,CAAC;SAChB;IACH,CAAC;CACF","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\nimport { HTTP, CloudEvent } from \"cloudevents\";\nimport { IncomingMessage, ServerResponse } from \"http\";\nimport { URL } from \"url\";\nimport { logger } from \"./logger\";\nimport * as utils from \"./utils\";\n\nimport {\n ConnectRequest,\n ConnectResponse,\n UserEventRequest,\n DisconnectedRequest,\n ConnectedRequest,\n ConnectionContext,\n ConnectResponseHandler,\n UserEventResponseHandler,\n WebPubSubEventHandlerOptions\n} from \"./cloudEventsProtocols\";\n\nenum EventType {\n Connect,\n Connected,\n Disconnected,\n UserEvent\n}\n\nfunction getConnectResponseHandler(\n connectRequest: ConnectRequest,\n response: ServerResponse\n): ConnectResponseHandler {\n const states: Record<string, any> = connectRequest.context.states;\n let modified = false;\n const handler = {\n setState(name: string, value: unknown): void {\n states[name] = value;\n modified = true;\n },\n success(res?: ConnectResponse): void {\n response.statusCode = 200;\n if (modified) {\n response.setHeader(\"ce-connectionState\", utils.toBase64JsonString(states));\n }\n if (res === undefined) {\n response.end();\n } else {\n response.setHeader(\"Content-Type\", \"application/json; charset=utf-8\");\n response.end(JSON.stringify(res));\n }\n },\n fail(code: 400 | 401 | 500, detail?: string): void {\n response.statusCode = code;\n response.end(detail ?? \"\");\n }\n };\n\n return handler;\n}\n\nfunction getUserEventResponseHandler(\n userRequest: UserEventRequest,\n response: ServerResponse\n): UserEventResponseHandler {\n const states: Record<string, any> = userRequest.context.states;\n let modified = false;\n const handler = {\n setState(name: string, value: unknown): void {\n modified = true;\n states[name] = value;\n },\n success(data?: string | ArrayBuffer, dataType?: \"binary\" | \"text\" | \"json\"): void {\n response.statusCode = 200;\n if (modified) {\n response.setHeader(\"ce-connectionState\", utils.toBase64JsonString(states));\n }\n\n switch (dataType) {\n case \"json\":\n response.setHeader(\"Content-Type\", \"application/json; charset=utf-8\");\n break;\n case \"text\":\n response.setHeader(\"Content-Type\", \"text/plain; charset=utf-8\");\n break;\n default:\n response.setHeader(\"Content-Type\", \"application/octet-stream\");\n break;\n }\n response.end(data ?? \"\");\n },\n fail(code: 400 | 401 | 500, detail?: string): void {\n response.statusCode = code;\n response.end(detail ?? \"\");\n }\n };\n return handler;\n}\n\nfunction getContext(ce: CloudEvent, origin: string): ConnectionContext {\n const context = {\n signature: ce[\"signature\"] as string,\n userId: ce[\"userid\"] as string,\n hub: ce[\"hub\"] as string,\n connectionId: ce[\"connectionid\"] as string,\n eventName: ce[\"eventname\"] as string,\n origin: origin,\n states: utils.fromBase64JsonString(ce[\"connectionstate\"] as string)\n };\n\n // TODO: validation\n return context;\n}\n\nfunction tryGetWebPubSubEvent(req: IncomingMessage): EventType | undefined {\n // check ce-type to see if it is a valid WebPubSub CloudEvent request\n const prefix = \"azure.webpubsub.\";\n const connect = \"azure.webpubsub.sys.connect\";\n const connected = \"azure.webpubsub.sys.connected\";\n const disconnectd = \"azure.webpubsub.sys.disconnected\";\n const userPrefix = \"azure.webpubsub.user.\";\n const type = utils.getHttpHeader(req, \"ce-type\");\n if (!type?.startsWith(prefix)) {\n return undefined;\n }\n if (type.startsWith(userPrefix)) {\n return EventType.UserEvent;\n }\n switch (type) {\n case connect:\n return EventType.Connect;\n case connected:\n return EventType.Connected;\n case disconnectd:\n return EventType.Disconnected;\n default:\n return undefined;\n }\n}\n\nfunction isWebPubSubRequest(req: IncomingMessage): boolean {\n return utils.getHttpHeader(req, \"ce-awpsversion\") !== undefined;\n}\n\n/**\n * @internal\n */\nexport class CloudEventsDispatcher {\n private readonly _allowAll: boolean = true;\n private readonly _allowedOrigins: Array<string> = [];\n constructor(private hub: string, private eventHandler?: WebPubSubEventHandlerOptions) {\n if (Array.isArray(eventHandler)) {\n throw new Error(\"Unexpected WebPubSubEventHandlerOptions\");\n }\n if (eventHandler?.allowedEndpoints !== undefined) {\n this._allowedOrigins = eventHandler.allowedEndpoints.map((endpoint) =>\n new URL(endpoint).host.toLowerCase()\n );\n this._allowAll = false;\n }\n }\n\n public handlePreflight(req: IncomingMessage, res: ServerResponse): boolean {\n if (!isWebPubSubRequest(req)) {\n return false;\n }\n const origin = utils.getHttpHeader(req, \"webhook-request-origin\")?.toLowerCase();\n\n if (origin === undefined) {\n logger.warning(\"Expecting webhook-request-origin header.\");\n res.statusCode = 400;\n } else if (this._allowAll || this._allowedOrigins.indexOf(origin!) > -1) {\n res.setHeader(\"WebHook-Allowed-Origin\", origin!);\n } else {\n logger.warning(\"Origin does not match the allowed origins: \" + this._allowedOrigins);\n res.statusCode = 400;\n }\n\n res.end();\n return true;\n }\n\n public async handleRequest(request: IncomingMessage, response: ServerResponse): Promise<boolean> {\n if (!isWebPubSubRequest(request)) {\n return false;\n }\n\n // check if it is a valid WebPubSub cloud events\n const origin = utils.getHttpHeader(request, \"webhook-request-origin\");\n if (origin === undefined) {\n return false;\n }\n\n const eventType = tryGetWebPubSubEvent(request);\n if (eventType === undefined) {\n return false;\n }\n\n // check if hub matches\n const hub = utils.getHttpHeader(request, \"ce-hub\");\n if (hub !== this.hub) {\n return false;\n }\n\n // No need to read body if handler is not specified\n switch (eventType) {\n case EventType.Connect:\n if (!this.eventHandler?.handleConnect) {\n response.end();\n return true;\n }\n break;\n case EventType.Connected:\n if (!this.eventHandler?.onConnected) {\n response.end();\n return true;\n }\n break;\n case EventType.Disconnected:\n if (!this.eventHandler?.onDisconnected) {\n response.end();\n return true;\n }\n break;\n case EventType.UserEvent:\n if (!this.eventHandler?.handleUserEvent) {\n response.end();\n return true;\n }\n break;\n default:\n logger.warning(`Unknown EventType ${eventType}`);\n return false;\n }\n\n const eventRequest = await utils.convertHttpToEvent(request);\n const receivedEvent = HTTP.toEvent(eventRequest);\n\n logger.verbose(receivedEvent);\n\n switch (eventType) {\n case EventType.Connect: {\n const connectRequest = receivedEvent.data as ConnectRequest;\n connectRequest.context = getContext(receivedEvent, origin);\n this.eventHandler.handleConnect!(\n connectRequest,\n getConnectResponseHandler(connectRequest, response)\n );\n return true;\n }\n case EventType.Connected: {\n // for unblocking events, we responds to the service as early as possible\n response.end();\n const connectedRequest = receivedEvent.data as ConnectedRequest;\n connectedRequest.context = getContext(receivedEvent, origin);\n this.eventHandler.onConnected!(connectedRequest);\n return true;\n }\n case EventType.Disconnected: {\n // for unblocking events, we responds to the service as early as possible\n response.end();\n const disconnectedRequest = receivedEvent.data as DisconnectedRequest;\n disconnectedRequest.context = getContext(receivedEvent, origin);\n this.eventHandler.onDisconnected!(disconnectedRequest);\n return true;\n }\n case EventType.UserEvent: {\n let userRequest: UserEventRequest;\n if (receivedEvent.data_base64 !== undefined) {\n userRequest = {\n context: getContext(receivedEvent, origin),\n data: Buffer.from(receivedEvent.data_base64, \"base64\"),\n dataType: \"binary\"\n };\n } else if (receivedEvent.data !== undefined) {\n userRequest = {\n context: getContext(receivedEvent, origin),\n data: receivedEvent.data as string,\n dataType: receivedEvent.datacontenttype?.startsWith(\"application/json;\")\n ? \"json\"\n : \"text\"\n };\n } else {\n throw new Error(\"Unexpected data.\");\n }\n\n this.eventHandler.handleUserEvent!(\n userRequest,\n getUserEventResponseHandler(userRequest, response)\n );\n return true;\n }\n default:\n logger.warning(`Unknown EventType ${eventType}`);\n return false;\n }\n }\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cloudEventsProtocols.js","sourceRoot":"","sources":["../../src/cloudEventsProtocols.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\n/**\n * Response of the connect event.\n */\nexport interface ConnectResponse {\n /**\n * Set the groups the connection would like to join.\n */\n groups?: string[];\n /**\n * Set the roles the connection belongs to.\n */\n roles?: string[];\n /**\n * Set the userId for the connection.\n */\n userId?: string;\n /**\n * Set the subprotocol for the connection to complete WebSocket handshake.\n */\n subprotocol?: string;\n}\n\n/**\n * The connection context representing the client WebSocket connection.\n */\nexport interface ConnectionContext {\n /**\n * The hub the connection belongs to.\n */\n hub: string;\n /**\n * The Id of the connection.\n */\n connectionId: string;\n /**\n * The event name of this CloudEvents request.\n */\n eventName: string;\n /**\n * The origin this CloudEvents request comes from.\n */\n origin: string;\n /**\n * The user id of the connection.\n */\n userId?: string;\n /**\n * The subprotocol of this connection.\n */\n subprotocol?: string;\n /**\n * Get the additional states for the connection, such states are perserved throughout the lifetime of the connection.\n */\n states: Record<string, any>;\n}\n\n/**\n * Request for the connect event.\n */\nexport interface ConnectRequest {\n /**\n * The context of current CloudEvents request.\n */\n context: ConnectionContext;\n /**\n * The claims that the client WebSocket connection has when it connects.\n */\n claims?: Record<string, string[]>;\n /**\n * The queries that the client WebSocket connection has when it connects.\n */\n queries?: Record<string, string[]>;\n /**\n * The subprotocols that the client WebSocket connection uses to do handshake.\n */\n subprotocols?: string[];\n /**\n * The client certificate info that the client WebSocket connection uses to connect.\n */\n clientCertificates?: Certificate[];\n}\n\n/**\n * The client certificate.\n */\nexport interface Certificate {\n /**\n * The thumbprint of the certificate.\n */\n thumbprint: string;\n}\n\n/**\n * Request for the connected event.\n */\nexport interface ConnectedRequest {\n /**\n * The context of current CloudEvents request.\n */\n context: ConnectionContext;\n}\n\n/**\n * Request for the user event.\n */\nexport type UserEventRequest =\n | {\n /**\n * The context of current CloudEvents request.\n */\n context: ConnectionContext;\n\n /**\n * The content data.\n */\n data: string;\n /**\n * The type of the data.\n */\n dataType: \"text\" | \"json\";\n }\n | {\n /**\n * The context of current CloudEvents request.\n */\n context: ConnectionContext;\n\n /**\n * The content data.\n */\n data: ArrayBuffer;\n /**\n * The type of the data.\n */\n dataType: \"binary\";\n };\n\n/**\n * Request for the disconnected event.\n */\nexport interface DisconnectedRequest {\n /**\n * The context of current CloudEvents request.\n */\n context: ConnectionContext;\n /**\n * The reason that the connection disconnects.\n */\n reason?: string;\n}\n\n/**\n * The handler to set connect event response\n */\nexport interface ConnectResponseHandler {\n /**\n * Set the state of the connection\n * @param name - The name of the state\n * @param value - The value of the state\n */\n setState(name: string, value: unknown): void;\n /**\n * Return success response to the service.\n * @param response - The response for the connect event.\n */\n success(response?: ConnectResponse): void;\n /**\n * Return failed response and the service will reject the client WebSocket connection.\n * @param code - Code can be 400 user error, 401 unauthorized and 500 server error.\n * @param detail - The detail of the error.\n */\n fail(code: 400 | 401 | 500, detail?: string): void;\n}\n\n/**\n * The handler to set user event response\n */\nexport interface UserEventResponseHandler {\n /**\n * Set the state of the connection\n * @param name - The name of the state\n * @param value - The value of the state\n */\n setState(name: string, value: unknown): void;\n /**\n * Return success response with data to be delivered to the client WebSocket connection.\n * @param data - The payload data to be returned to the client.\n * @param dataType - The type of the payload data.\n */\n success(data?: string | ArrayBuffer, dataType?: \"binary\" | \"text\" | \"json\"): void;\n /**\n * Return failed response and the service will close the client WebSocket connection.\n * @param code - Code can be 400 user error, 401 unauthorized and 500 server error.\n * @param detail - The detail of the error.\n */\n fail(code: 400 | 401 | 500, detail?: string): void;\n}\n\n/**\n * The options for the CloudEvents handler.\n */\nexport interface WebPubSubEventHandlerOptions {\n /**\n * Custom serving path for the path of the CloudEvents handler.\n */\n path?: string;\n\n /**\n *
|
|
1
|
+
{"version":3,"file":"cloudEventsProtocols.js","sourceRoot":"","sources":["../../src/cloudEventsProtocols.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\n/**\n * Response of the connect event.\n */\nexport interface ConnectResponse {\n /**\n * Set the groups the connection would like to join.\n */\n groups?: string[];\n /**\n * Set the roles the connection belongs to.\n */\n roles?: string[];\n /**\n * Set the userId for the connection.\n */\n userId?: string;\n /**\n * Set the subprotocol for the connection to complete WebSocket handshake.\n */\n subprotocol?: string;\n}\n\n/**\n * The connection context representing the client WebSocket connection.\n */\nexport interface ConnectionContext {\n /**\n * The hub the connection belongs to.\n */\n hub: string;\n /**\n * The Id of the connection.\n */\n connectionId: string;\n /**\n * The event name of this CloudEvents request.\n */\n eventName: string;\n /**\n * The origin this CloudEvents request comes from.\n */\n origin: string;\n /**\n * The user id of the connection.\n */\n userId?: string;\n /**\n * The subprotocol of this connection.\n */\n subprotocol?: string;\n /**\n * Get the additional states for the connection, such states are perserved throughout the lifetime of the connection.\n */\n states: Record<string, any>;\n}\n\n/**\n * Request for the connect event.\n */\nexport interface ConnectRequest {\n /**\n * The context of current CloudEvents request.\n */\n context: ConnectionContext;\n /**\n * The claims that the client WebSocket connection has when it connects.\n */\n claims?: Record<string, string[]>;\n /**\n * The queries that the client WebSocket connection has when it connects.\n */\n queries?: Record<string, string[]>;\n /**\n * The headers that the client WebSocket connection has when it connects.\n */\n headers?: Record<string, string[]>;\n /**\n * The subprotocols that the client WebSocket connection uses to do handshake.\n */\n subprotocols?: string[];\n /**\n * The client certificate info that the client WebSocket connection uses to connect.\n */\n clientCertificates?: Certificate[];\n}\n\n/**\n * The client certificate.\n */\nexport interface Certificate {\n /**\n * The thumbprint of the certificate.\n */\n thumbprint: string;\n}\n\n/**\n * Request for the connected event.\n */\nexport interface ConnectedRequest {\n /**\n * The context of current CloudEvents request.\n */\n context: ConnectionContext;\n}\n\n/**\n * Request for the user event.\n */\nexport type UserEventRequest =\n | {\n /**\n * The context of current CloudEvents request.\n */\n context: ConnectionContext;\n\n /**\n * The content data.\n */\n data: string;\n /**\n * The type of the data.\n */\n dataType: \"text\" | \"json\";\n }\n | {\n /**\n * The context of current CloudEvents request.\n */\n context: ConnectionContext;\n\n /**\n * The content data.\n */\n data: ArrayBuffer;\n /**\n * The type of the data.\n */\n dataType: \"binary\";\n };\n\n/**\n * Request for the disconnected event.\n */\nexport interface DisconnectedRequest {\n /**\n * The context of current CloudEvents request.\n */\n context: ConnectionContext;\n /**\n * The reason that the connection disconnects.\n */\n reason?: string;\n}\n\n/**\n * The handler to set connect event response\n */\nexport interface ConnectResponseHandler {\n /**\n * Set the state of the connection\n * @param name - The name of the state\n * @param value - The value of the state\n */\n setState(name: string, value: unknown): void;\n /**\n * Return success response to the service.\n * @param response - The response for the connect event.\n */\n success(response?: ConnectResponse): void;\n /**\n * Return failed response and the service will reject the client WebSocket connection.\n * @param code - Code can be 400 user error, 401 unauthorized and 500 server error.\n * @param detail - The detail of the error.\n */\n fail(code: 400 | 401 | 500, detail?: string): void;\n}\n\n/**\n * The handler to set user event response\n */\nexport interface UserEventResponseHandler {\n /**\n * Set the state of the connection\n * @param name - The name of the state\n * @param value - The value of the state\n */\n setState(name: string, value: unknown): void;\n /**\n * Return success response with data to be delivered to the client WebSocket connection.\n * @param data - The payload data to be returned to the client.\n * @param dataType - The type of the payload data.\n */\n success(data?: string | ArrayBuffer, dataType?: \"binary\" | \"text\" | \"json\"): void;\n /**\n * Return failed response and the service will close the client WebSocket connection.\n * @param code - Code can be 400 user error, 401 unauthorized and 500 server error.\n * @param detail - The detail of the error.\n */\n fail(code: 400 | 401 | 500, detail?: string): void;\n}\n\n/**\n * The options for the CloudEvents handler.\n */\nexport interface WebPubSubEventHandlerOptions {\n /**\n * Custom serving path for the path of the CloudEvents handler.\n */\n path?: string;\n\n /**\n * Handle 'connect' event, the service waits for the response to proceed.\n */\n handleConnect?: (connectRequest: ConnectRequest, connectResponse: ConnectResponseHandler) => void;\n\n /**\n * Handle user events, the service waits for the response to proceed.\n */\n handleUserEvent?: (\n userEventRequest: UserEventRequest,\n userEventResponse: UserEventResponseHandler\n ) => void;\n\n /**\n * Event trigger for \"connected\" unblocking event. This is an unblocking event and the service does not wait for the response.\n */\n onConnected?: (connectedRequest: ConnectedRequest) => void;\n\n /**\n *\n * Event triggers for \"disconnected\" unblocking event. This is an unblocking event and the service does not wait for the response.\n */\n onDisconnected?: (disconnectedRequest: DisconnectedRequest) => void;\n\n /**\n * If not specified, by default allow all the endpoints, otherwise only allow specified endpoints\n */\n allowedEndpoints?: string[];\n}\n"]}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// Copyright (c) Microsoft Corporation.
|
|
2
|
+
// Licensed under the MIT license.
|
|
3
|
+
import { createClientLogger } from "@azure/logger";
|
|
4
|
+
/**
|
|
5
|
+
* The \@azure/logger configuration for this package.
|
|
6
|
+
*
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
9
|
+
export const logger = createClientLogger("web-pubsub-express");
|
|
10
|
+
//# sourceMappingURL=logger.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logger.js","sourceRoot":"","sources":["../../src/logger.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAEnD;;;;GAIG;AACH,MAAM,CAAC,MAAM,MAAM,GAAG,kBAAkB,CAAC,oBAAoB,CAAC,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\nimport { createClientLogger } from \"@azure/logger\";\n\n/**\n * The \\@azure/logger configuration for this package.\n *\n * @internal\n */\nexport const logger = createClientLogger(\"web-pubsub-express\");\n"]}
|
|
@@ -13,7 +13,7 @@ export class WebPubSubEventHandler {
|
|
|
13
13
|
* import express from "express";
|
|
14
14
|
* import { WebPubSubEventHandler } from "@azure/web-pubsub-express";
|
|
15
15
|
* const endpoint = "https://xxxx.webpubsubdev.azure.com"
|
|
16
|
-
* const handler = new WebPubSubEventHandler('chat',
|
|
16
|
+
* const handler = new WebPubSubEventHandler('chat', {
|
|
17
17
|
* handleConnect: (req, res) => {
|
|
18
18
|
* console.log(JSON.stringify(req));
|
|
19
19
|
* return {};
|
|
@@ -25,20 +25,20 @@ export class WebPubSubEventHandler {
|
|
|
25
25
|
* console.log(JSON.stringify(req));
|
|
26
26
|
* res.success("Hey " + req.data, req.dataType);
|
|
27
27
|
* };
|
|
28
|
+
* allowedEndpoints: [ endpoint ]
|
|
28
29
|
* },
|
|
29
30
|
* });
|
|
30
31
|
* ```
|
|
31
32
|
*
|
|
32
33
|
* @param hub - The name of the hub to listen to
|
|
33
|
-
* @param allowedEndpoints - The allowed endpoints for the incoming CloudEvents request
|
|
34
34
|
* @param options - Options to configure the event handler
|
|
35
35
|
*/
|
|
36
|
-
constructor(hub,
|
|
36
|
+
constructor(hub, options) {
|
|
37
37
|
var _a;
|
|
38
38
|
this.hub = hub;
|
|
39
39
|
const path = ((_a = options === null || options === void 0 ? void 0 : options.path) !== null && _a !== void 0 ? _a : `/api/webpubsub/hubs/${hub}/`).toLowerCase();
|
|
40
40
|
this.path = path.endsWith("/") ? path : path + "/";
|
|
41
|
-
this._cloudEventsHandler = new CloudEventsDispatcher(this.hub,
|
|
41
|
+
this._cloudEventsHandler = new CloudEventsDispatcher(this.hub, options);
|
|
42
42
|
}
|
|
43
43
|
/**
|
|
44
44
|
* Get the middleware to process the CloudEvents requests
|
|
@@ -51,13 +51,13 @@ export class WebPubSubEventHandler {
|
|
|
51
51
|
requestUrl = requestUrl.endsWith("/") ? requestUrl : requestUrl + "/";
|
|
52
52
|
if (requestUrl.startsWith(this.path)) {
|
|
53
53
|
if (req.method === "OPTIONS") {
|
|
54
|
-
if (this._cloudEventsHandler.
|
|
54
|
+
if (this._cloudEventsHandler.handlePreflight(req, res)) {
|
|
55
55
|
return;
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
58
|
else if (req.method === "POST") {
|
|
59
59
|
try {
|
|
60
|
-
if (await this._cloudEventsHandler.
|
|
60
|
+
if (await this._cloudEventsHandler.handleRequest(req, res)) {
|
|
61
61
|
return;
|
|
62
62
|
}
|
|
63
63
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"webPubSubEventHandler.js","sourceRoot":"","sources":["../../src/webPubSubEventHandler.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAIlC,OAAO,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAC;AAGhE;;GAEG;AACH,MAAM,OAAO,qBAAqB;IAQhC;;;;;;;;;;;;;;;;;;;;;;;;;;;OA2BG;IACH,
|
|
1
|
+
{"version":3,"file":"webPubSubEventHandler.js","sourceRoot":"","s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I,GAAG,CAAC,MAAM,KAAK,MAAM,EAAE;oBAChC,IAAI;wBACF,IAAI,MAAM,IAAI,CAAC,mBAAmB,CAAC,aAAa,CAAC,GAAG,EAAE,GAAG,CAAC,EAAE;4BAC1D,OAAO;yBACR;qBACF;oBAAC,OAAO,GAAG,EAAE;wBACZ,IAAI,CAAC,GAAG,CAAC,CAAC;wBACV,OAAO;qBACR;iBACF;aACF;YAED,IAAI,EAAE,CAAC;QACT,CAAC,CAAC;IACJ,CAAC;CACF","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\nimport express from \"express-serve-static-core\";\n\nimport { CloudEventsDispatcher } from \"./cloudEventsDispatcher\";\nimport { WebPubSubEventHandlerOptions } from \"./cloudEventsProtocols\";\n\n/**\n * The handler to handle incoming CloudEvents messages\n */\nexport class WebPubSubEventHandler {\n /**\n * The path this CloudEvents handler listens to\n */\n public readonly path: string;\n\n private _cloudEventsHandler: CloudEventsDispatcher;\n\n /**\n * Creates an instance of a WebPubSubEventHandler for handling incoming CloudEvents messages.\n *\n * Example usage:\n * ```ts\n * import express from \"express\";\n * import { WebPubSubEventHandler } from \"@azure/web-pubsub-express\";\n * const endpoint = \"https://xxxx.webpubsubdev.azure.com\"\n * const handler = new WebPubSubEventHandler('chat', {\n * handleConnect: (req, res) => {\n * console.log(JSON.stringify(req));\n * return {};\n * },\n * onConnected: req => {\n * console.log(JSON.stringify(req));\n * },\n * handleUserEvent: (req, res) => {\n * console.log(JSON.stringify(req));\n * res.success(\"Hey \" + req.data, req.dataType);\n * };\n * allowedEndpoints: [ endpoint ]\n * },\n * });\n * ```\n *\n * @param hub - The name of the hub to listen to\n * @param options - Options to configure the event handler\n */\n constructor(private hub: string, options?: WebPubSubEventHandlerOptions) {\n const path = (options?.path ?? `/api/webpubsub/hubs/${hub}/`).toLowerCase();\n this.path = path.endsWith(\"/\") ? path : path + \"/\";\n this._cloudEventsHandler = new CloudEventsDispatcher(this.hub, options);\n }\n\n /**\n * Get the middleware to process the CloudEvents requests\n */\n public getMiddleware(): express.RequestHandler {\n return async (\n req: express.Request,\n res: express.Response,\n next: express.NextFunction\n ): Promise<void> => {\n // Request originalUrl can contain query while baseUrl + path not\n let requestUrl = (req.baseUrl + req.path).toLowerCase();\n\n // normalize the Url\n requestUrl = requestUrl.endsWith(\"/\") ? requestUrl : requestUrl + \"/\";\n if (requestUrl.startsWith(this.path)) {\n if (req.method === \"OPTIONS\") {\n if (this._cloudEventsHandler.handlePreflight(req, res)) {\n return;\n }\n } else if (req.method === \"POST\") {\n try {\n if (await this._cloudEventsHandler.handleRequest(req, res)) {\n return;\n }\n } catch (err) {\n next(err);\n return;\n }\n }\n }\n\n next();\n };\n }\n}\n"]}
|
|
@@ -10,6 +10,7 @@ import { toBase64JsonString } from "../src/utils";
|
|
|
10
10
|
function buildRequest(req, hub, connectionId, userId, states) {
|
|
11
11
|
req.headers["webhook-request-origin"] = "xxx.webpubsub.azure.com";
|
|
12
12
|
req.headers["Content-Type"] = "application/json; charset=utf-8";
|
|
13
|
+
req.headers["ce-awpsversion"] = "1.0";
|
|
13
14
|
req.headers["ce-specversion"] = "1.0";
|
|
14
15
|
req.headers["ce-type"] = "azure.webpubsub.sys.connect";
|
|
15
16
|
req.headers["ce-source"] = `/hubs/${hub}/client/${connectionId}`;
|
|
@@ -36,46 +37,46 @@ describe("Can handle connect event", function () {
|
|
|
36
37
|
});
|
|
37
38
|
it("Should not handle the request if request is not cloud events", async function () {
|
|
38
39
|
const endSpy = sinon.spy(res.end);
|
|
39
|
-
const dispatcher = new CloudEventsDispatcher("hub1"
|
|
40
|
-
const result = await dispatcher.
|
|
40
|
+
const dispatcher = new CloudEventsDispatcher("hub1");
|
|
41
|
+
const result = await dispatcher.handleRequest(req, res);
|
|
41
42
|
assert.isFalse(result);
|
|
42
43
|
assert.isTrue(endSpy.notCalled);
|
|
43
44
|
});
|
|
44
45
|
it("Should not handle the request if hub does not match", async function () {
|
|
45
46
|
const endSpy = sinon.spy(res, "end");
|
|
46
47
|
buildRequest(req, "hub", "conn1");
|
|
47
|
-
const dispatcher = new CloudEventsDispatcher("hub1"
|
|
48
|
-
const result = await dispatcher.
|
|
48
|
+
const dispatcher = new CloudEventsDispatcher("hub1");
|
|
49
|
+
const result = await dispatcher.handleRequest(req, res);
|
|
49
50
|
assert.isFalse(result);
|
|
50
51
|
assert.isTrue(endSpy.notCalled);
|
|
51
52
|
});
|
|
52
|
-
it("Should response with
|
|
53
|
+
it("Should response with 200 when option is not specified", async function () {
|
|
53
54
|
const endSpy = sinon.spy(res, "end");
|
|
54
55
|
buildRequest(req, "hub", "conn1");
|
|
55
|
-
const dispatcher = new CloudEventsDispatcher("hub"
|
|
56
|
-
const result = await dispatcher.
|
|
56
|
+
const dispatcher = new CloudEventsDispatcher("hub");
|
|
57
|
+
const result = await dispatcher.handleRequest(req, res);
|
|
57
58
|
assert.isTrue(result, "should handle");
|
|
58
59
|
assert.isTrue(endSpy.calledOnce, "should call once");
|
|
59
|
-
assert.equal(
|
|
60
|
+
assert.equal(200, res.statusCode, "should be 200");
|
|
60
61
|
});
|
|
61
|
-
it("Should response with
|
|
62
|
+
it("Should response with 200 when handler is not specified", async function () {
|
|
62
63
|
const endSpy = sinon.spy(res, "end");
|
|
63
64
|
buildRequest(req, "hub", "conn1");
|
|
64
|
-
const dispatcher = new CloudEventsDispatcher("hub",
|
|
65
|
-
const result = await dispatcher.
|
|
65
|
+
const dispatcher = new CloudEventsDispatcher("hub", {});
|
|
66
|
+
const result = await dispatcher.handleRequest(req, res);
|
|
66
67
|
assert.isTrue(result, "should handle");
|
|
67
68
|
assert.isTrue(endSpy.calledOnce, "should call once");
|
|
68
|
-
assert.equal(
|
|
69
|
+
assert.equal(200, res.statusCode, "should be 200");
|
|
69
70
|
});
|
|
70
71
|
it("Should response with error when handler returns error", async function () {
|
|
71
72
|
const endSpy = sinon.spy(res, "end");
|
|
72
73
|
buildRequest(req, "hub", "conn1");
|
|
73
|
-
const dispatcher = new CloudEventsDispatcher("hub",
|
|
74
|
+
const dispatcher = new CloudEventsDispatcher("hub", {
|
|
74
75
|
handleConnect: async (_, response) => {
|
|
75
76
|
response.fail(400);
|
|
76
77
|
}
|
|
77
78
|
});
|
|
78
|
-
const process = dispatcher.
|
|
79
|
+
const process = dispatcher.handleRequest(req, res);
|
|
79
80
|
mockBody(req, JSON.stringify({}));
|
|
80
81
|
const result = await process;
|
|
81
82
|
assert.isTrue(result, "should handle");
|
|
@@ -85,12 +86,12 @@ describe("Can handle connect event", function () {
|
|
|
85
86
|
it("Should response with success when handler returns success", async function () {
|
|
86
87
|
const endSpy = sinon.spy(res, "end");
|
|
87
88
|
buildRequest(req, "hub", "conn1");
|
|
88
|
-
const dispatcher = new CloudEventsDispatcher("hub",
|
|
89
|
+
const dispatcher = new CloudEventsDispatcher("hub", {
|
|
89
90
|
handleConnect: async (_, response) => {
|
|
90
91
|
response.success();
|
|
91
92
|
}
|
|
92
93
|
});
|
|
93
|
-
const process = dispatcher.
|
|
94
|
+
const process = dispatcher.handleRequest(req, res);
|
|
94
95
|
mockBody(req, JSON.stringify({}));
|
|
95
96
|
const result = await process;
|
|
96
97
|
assert.isTrue(result, "should handle");
|
|
@@ -100,12 +101,12 @@ describe("Can handle connect event", function () {
|
|
|
100
101
|
it("Should response with success when handler returns success value", async function () {
|
|
101
102
|
const endSpy = sinon.spy(res, "end");
|
|
102
103
|
buildRequest(req, "hub", "conn1");
|
|
103
|
-
const dispatcher = new CloudEventsDispatcher("hub",
|
|
104
|
+
const dispatcher = new CloudEventsDispatcher("hub", {
|
|
104
105
|
handleConnect: async (_, response) => {
|
|
105
106
|
response.success({ userId: "vic" });
|
|
106
107
|
}
|
|
107
108
|
});
|
|
108
|
-
const process = dispatcher.
|
|
109
|
+
const process = dispatcher.handleRequest(req, res);
|
|
109
110
|
mockBody(req, JSON.stringify({}));
|
|
110
111
|
const result = await process;
|
|
111
112
|
assert.isTrue(result, "should handle");
|
|
@@ -115,7 +116,7 @@ describe("Can handle connect event", function () {
|
|
|
115
116
|
it("Should be able to set connection state", async function () {
|
|
116
117
|
const endSpy = sinon.spy(res, "end");
|
|
117
118
|
buildRequest(req, "hub", "conn1");
|
|
118
|
-
const dispatcher = new CloudEventsDispatcher("hub",
|
|
119
|
+
const dispatcher = new CloudEventsDispatcher("hub", {
|
|
119
120
|
handleConnect: async (_, response) => {
|
|
120
121
|
response.setState("key1", "val1");
|
|
121
122
|
response.setState("key2", "val2");
|
|
@@ -124,7 +125,7 @@ describe("Can handle connect event", function () {
|
|
|
124
125
|
response.success({ userId: "vic" });
|
|
125
126
|
}
|
|
126
127
|
});
|
|
127
|
-
const process = dispatcher.
|
|
128
|
+
const process = dispatcher.handleRequest(req, res);
|
|
128
129
|
mockBody(req, JSON.stringify({}));
|
|
129
130
|
const result = await process;
|
|
130
131
|
assert.isTrue(result, "should handle");
|
|
@@ -143,7 +144,7 @@ describe("Can handle connect event", function () {
|
|
|
143
144
|
key3: ""
|
|
144
145
|
});
|
|
145
146
|
buildRequest(req, "hub1", "conn1", undefined, states);
|
|
146
|
-
const dispatcher = new CloudEventsDispatcher("hub1",
|
|
147
|
+
const dispatcher = new CloudEventsDispatcher("hub1", {
|
|
147
148
|
handleConnect: (request, response) => {
|
|
148
149
|
assert.equal("val3", request.context.states["key1"][0]);
|
|
149
150
|
assert.equal("val2", request.context.states["key2"]);
|
|
@@ -151,7 +152,7 @@ describe("Can handle connect event", function () {
|
|
|
151
152
|
response.success();
|
|
152
153
|
}
|
|
153
154
|
});
|
|
154
|
-
const process = dispatcher.
|
|
155
|
+
const process = dispatcher.handleRequest(req, res);
|
|
155
156
|
mockBody(req, JSON.stringify({}));
|
|
156
157
|
const result = await process;
|
|
157
158
|
assert.isTrue(result, "should handle");
|
|
@@ -161,13 +162,13 @@ describe("Can handle connect event", function () {
|
|
|
161
162
|
it("Invalid state header gets ignored", async function () {
|
|
162
163
|
const endSpy = sinon.spy(res, "end");
|
|
163
164
|
buildRequest(req, "hub1", "conn1", undefined, "");
|
|
164
|
-
const dispatcher = new CloudEventsDispatcher("hub1",
|
|
165
|
+
const dispatcher = new CloudEventsDispatcher("hub1", {
|
|
165
166
|
handleConnect: (request, response) => {
|
|
166
167
|
assert.deepEqual({}, request.context.states);
|
|
167
168
|
response.success();
|
|
168
169
|
}
|
|
169
170
|
});
|
|
170
|
-
const process = dispatcher.
|
|
171
|
+
const process = dispatcher.handleRequest(req, res);
|
|
171
172
|
mockBody(req, JSON.stringify({}));
|
|
172
173
|
const result = await process;
|
|
173
174
|
assert.isTrue(result, "should handle");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"connect.spec.js","sourceRoot":"","sources":["../../test/connect.spec.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAClC,oCAAoC;AACpC,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AACrE,OAAO,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAC9B,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,MAAM,CAAC;AACvD,OAAO,EAAE,MAAM,EAAE,MAAM,KAAK,CAAC;AAC7B,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAC/B,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAElD,SAAS,YAAY,CACnB,GAAoB,EACpB,GAAW,EACX,YAAoB,EACpB,MAAe,EACf,MAAe;IAEf,GAAG,CAAC,OAAO,CAAC,wBAAwB,CAAC,GAAG,yBAAyB,CAAC;IAClE,GAAG,CAAC,OAAO,CAAC,cAAc,CAAC,GAAG,iCAAiC,CAAC;IAChE,GAAG,CAAC,OAAO,CAAC,gBAAgB,CAAC,GAAG,KAAK,CAAC;IACtC,GAAG,CAAC,OAAO,CAAC,SAAS,CAAC,GAAG,6BAA6B,CAAC;IACvD,GAAG,CAAC,OAAO,CAAC,WAAW,CAAC,GAAG,SAAS,GAAG,WAAW,YAAY,EAAE,CAAC;IACjE,GAAG,CAAC,OAAO,C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rB,CAAC;SACF,CAAC,CAAC;QACH,MAAM,OAAO,GAAG,UAAU,CAAC,cAAc,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;QACpD,QAAQ,CAAC,GAAG,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC,CAAC;QAClC,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC;QAC7B,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC;QACvC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,EAAE,kBAAkB,CAAC,CAAC;QACrD,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE,GAAG,CAAC,UAAU,EAAE,mBAAmB,CAAC,CAAC;IACzD,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n/* eslint-disable no-invalid-this */\nimport { CloudEventsDispatcher } from \"../src/cloudEventsDispatcher\";\nimport { assert } from \"chai\";\nimport { IncomingMessage, ServerResponse } from \"http\";\nimport { Socket } from \"net\";\nimport * as sinon from \"sinon\";\nimport { toBase64JsonString } from \"../src/utils\";\n\nfunction buildRequest(\n req: IncomingMessage,\n hub: string,\n connectionId: string,\n userId?: string,\n states?: string\n): void {\n req.headers[\"webhook-request-origin\"] = \"xxx.webpubsub.azure.com\";\n req.headers[\"Content-Type\"] = \"application/json; charset=utf-8\";\n req.headers[\"ce-specversion\"] = \"1.0\";\n req.headers[\"ce-type\"] = \"azure.webpubsub.sys.connect\";\n req.headers[\"ce-source\"] = `/hubs/${hub}/client/${connectionId}`;\n req.headers[\"ce-id\"] = \"1\";\n req.headers[\"ce-time\"] = new Date().toUTCString();\n if (userId !== undefined) {\n req.headers[\"ce-userId\"] = userId;\n }\n req.headers[\"ce-connectionId\"] = connectionId;\n req.headers[\"ce-hub\"] = hub;\n req.headers[\"ce-event\"] = \"connect\";\n req.headers[\"ce-connectionState\"] = states;\n}\n\nfunction mockBody(req: IncomingMessage, body: string): void {\n req.emit(\"data\", Buffer.from(body, \"utf-8\"));\n req.emit(\"end\");\n}\n\ndescribe(\"Can handle connect event\", function() {\n let req: IncomingMessage;\n let res: ServerResponse;\n\n beforeEach(function() {\n req = new IncomingMessage(new Socket());\n res = new ServerResponse(req);\n });\n\n it(\"Should not handle the request if request is not cloud events\", async function() {\n const endSpy = sinon.spy(res.end);\n\n const dispatcher = new CloudEventsDispatcher(\"hub1\", [\"*\"]);\n const result = await dispatcher.processRequest(req, res);\n assert.isFalse(result);\n assert.isTrue(endSpy.notCalled);\n });\n\n it(\"Should not handle the request if hub does not match\", async function() {\n const endSpy = sinon.spy(res, \"end\");\n buildRequest(req, \"hub\", \"conn1\");\n\n const dispatcher = new CloudEventsDispatcher(\"hub1\", [\"*\"]);\n const result = await dispatcher.processRequest(req, res);\n assert.isFalse(result);\n assert.isTrue(endSpy.notCalled);\n });\n\n it(\"Should response with 401 when option is not specified\", async function() {\n const endSpy = sinon.spy(res, \"end\");\n buildRequest(req, \"hub\", \"conn1\");\n\n const dispatcher = new CloudEventsDispatcher(\"hub\", [\"*\"]);\n const result = await dispatcher.processRequest(req, res);\n assert.isTrue(result, \"should handle\");\n assert.isTrue(endSpy.calledOnce, \"should call once\");\n assert.equal(401, res.statusCode, \"should be 401\");\n });\n\n it(\"Should response with 401 when handler is not specified\", async function() {\n const endSpy = sinon.spy(res, \"end\");\n buildRequest(req, \"hub\", \"conn1\");\n\n const dispatcher = new CloudEventsDispatcher(\"hub\", [\"*\"], {});\n const result = await dispatcher.processRequest(req, res);\n assert.isTrue(result, \"should handle\");\n assert.isTrue(endSpy.calledOnce, \"should call once\");\n assert.equal(401, res.statusCode, \"should be 401\");\n });\n\n it(\"Should response with error when handler returns error\", async function() {\n const endSpy = sinon.spy(res, \"end\");\n buildRequest(req, \"hub\", \"conn1\");\n\n const dispatcher = new CloudEventsDispatcher(\"hub\", [\"*\"], {\n handleConnect: async (_, response) => {\n response.fail(400);\n }\n });\n const process = dispatcher.processRequest(req, res);\n mockBody(req, JSON.stringify({}));\n const result = await process;\n assert.isTrue(result, \"should handle\");\n assert.isTrue(endSpy.calledOnce, \"should call once\");\n assert.equal(400, res.statusCode, \"should be error\");\n });\n\n it(\"Should response with success when handler returns success\", async function() {\n const endSpy = sinon.spy(res, \"end\");\n buildRequest(req, \"hub\", \"conn1\");\n\n const dispatcher = new CloudEventsDispatcher(\"hub\", [\"*\"], {\n handleConnect: async (_, response) => {\n response.success();\n }\n });\n const process = dispatcher.processRequest(req, res);\n mockBody(req, JSON.stringify({}));\n const result = await process;\n assert.isTrue(result, \"should handle\");\n assert.isTrue(endSpy.calledOnce, \"should call once\");\n assert.equal(200, res.statusCode, \"should be success\");\n });\n\n it(\"Should response with success when handler returns success value\", async function() {\n const endSpy = sinon.spy(res, \"end\");\n buildRequest(req, \"hub\", \"conn1\");\n\n const dispatcher = new CloudEventsDispatcher(\"hub\", [\"*\"], {\n handleConnect: async (_, response) => {\n response.success({ userId: \"vic\" });\n }\n });\n const process = dispatcher.processRequest(req, res);\n mockBody(req, JSON.stringify({}));\n const result = await process;\n assert.isTrue(result, \"should handle\");\n assert.isTrue(endSpy.calledOnce, \"should call once\");\n assert.equal(200, res.statusCode, \"should be success\");\n });\n\n it(\"Should be able to set connection state\", async function() {\n const endSpy = sinon.spy(res, \"end\");\n buildRequest(req, \"hub\", \"conn1\");\n\n const dispatcher = new CloudEventsDispatcher(\"hub\", [\"*\"], {\n handleConnect: async (_, response) => {\n response.setState(\"key1\", \"val1\");\n response.setState(\"key2\", \"val2\");\n response.setState(\"key1\", [\"val3\"]);\n response.setState(\"key3\", \"\");\n response.success({ userId: \"vic\" });\n }\n });\n const process = dispatcher.processRequest(req, res);\n mockBody(req, JSON.stringify({}));\n const result = await process;\n assert.isTrue(result, \"should handle\");\n assert.isTrue(endSpy.calledOnce, \"should call once\");\n assert.equal(\n toBase64JsonString({\n key1: [\"val3\"],\n key2: \"val2\",\n key3: \"\"\n }),\n res.getHeader(\"ce-connectionState\"),\n \"should contain multiple state headers\"\n );\n });\n\n it(\"Should be able to get the connection states if it exists in the header\", async function() {\n const endSpy = sinon.spy(res, \"end\");\n const states = toBase64JsonString({\n key1: [\"val3\"],\n key2: \"val2\",\n key3: \"\"\n });\n buildRequest(req, \"hub1\", \"conn1\", undefined, states);\n const dispatcher = new CloudEventsDispatcher(\"hub1\", [\"*\"], {\n handleConnect: (request, response) => {\n assert.equal(\"val3\", request.context.states[\"key1\"][0]);\n assert.equal(\"val2\", request.context.states[\"key2\"]);\n assert.equal(\"\", request.context.states[\"key3\"]);\n response.success();\n }\n });\n const process = dispatcher.processRequest(req, res);\n mockBody(req, JSON.stringify({}));\n const result = await process;\n assert.isTrue(result, \"should handle\");\n assert.isTrue(endSpy.calledOnce, \"should call once\");\n assert.equal(200, res.statusCode, \"should be success\");\n });\n\n it(\"Invalid state header gets ignored\", async function() {\n const endSpy = sinon.spy(res, \"end\");\n buildRequest(req, \"hub1\", \"conn1\", undefined, \"\");\n const dispatcher = new CloudEventsDispatcher(\"hub1\", [\"*\"], {\n handleConnect: (request, response) => {\n assert.deepEqual({}, request.context.states);\n response.success();\n }\n });\n const process = dispatcher.processRequest(req, res);\n mockBody(req, JSON.stringify({}));\n const result = await process;\n assert.isTrue(result, \"should handle\");\n assert.isTrue(endSpy.calledOnce, \"should call once\");\n assert.equal(200, res.statusCode, \"should be success\");\n });\n});\n"]}
|
|
1
|
+
{"version":3,"file":"connect.spec.js","sourceRoot":"","sources":["../../test/connect.spec.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAClC,oCAAoC;AACpC,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AACrE,OAAO,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAC9B,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,MAAM,CAAC;AACvD,OAAO,EAAE,MAAM,EAAE,MAAM,KAAK,CAAC;AAC7B,OAAO,KAAK,KAA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AC;QACrC,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,SAAS,EAAE,EAAE,CAAC,CAAC;QAClD,MAAM,UAAU,GAAG,IAAI,qBAAqB,CAAC,MAAM,EAAE;YACnD,aAAa,EAAE,CAAC,OAAO,EAAE,QAAQ,EAAE,EAAE;gBACnC,MAAM,CAAC,SAAS,CAAC,EAAE,EAAE,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;gBAC7C,QAAQ,CAAC,OAAO,EAAE,CAAC;YACrB,CAAC;SACF,CAAC,CAAC;QACH,MAAM,OAAO,GAAG,UAAU,CAAC,aAAa,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;QACnD,QAAQ,CAAC,GAAG,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC,CAAC;QAClC,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC;QAC7B,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC;QACvC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,EAAE,kBAAkB,CAAC,CAAC;QACrD,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE,GAAG,CAAC,UAAU,EAAE,mBAAmB,CAAC,CAAC;IACzD,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n/* eslint-disable no-invalid-this */\nimport { CloudEventsDispatcher } from \"../src/cloudEventsDispatcher\";\nimport { assert } from \"chai\";\nimport { IncomingMessage, ServerResponse } from \"http\";\nimport { Socket } from \"net\";\nimport * as sinon from \"sinon\";\nimport { toBase64JsonString } from \"../src/utils\";\n\nfunction buildRequest(\n req: IncomingMessage,\n hub: string,\n connectionId: string,\n userId?: string,\n states?: string\n): void {\n req.headers[\"webhook-request-origin\"] = \"xxx.webpubsub.azure.com\";\n req.headers[\"Content-Type\"] = \"application/json; charset=utf-8\";\n req.headers[\"ce-awpsversion\"] = \"1.0\";\n req.headers[\"ce-specversion\"] = \"1.0\";\n req.headers[\"ce-type\"] = \"azure.webpubsub.sys.connect\";\n req.headers[\"ce-source\"] = `/hubs/${hub}/client/${connectionId}`;\n req.headers[\"ce-id\"] = \"1\";\n req.headers[\"ce-time\"] = new Date().toUTCString();\n if (userId !== undefined) {\n req.headers[\"ce-userId\"] = userId;\n }\n req.headers[\"ce-connectionId\"] = connectionId;\n req.headers[\"ce-hub\"] = hub;\n req.headers[\"ce-event\"] = \"connect\";\n req.headers[\"ce-connectionState\"] = states;\n}\n\nfunction mockBody(req: IncomingMessage, body: string): void {\n req.emit(\"data\", Buffer.from(body, \"utf-8\"));\n req.emit(\"end\");\n}\n\ndescribe(\"Can handle connect event\", function() {\n let req: IncomingMessage;\n let res: ServerResponse;\n\n beforeEach(function() {\n req = new IncomingMessage(new Socket());\n res = new ServerResponse(req);\n });\n\n it(\"Should not handle the request if request is not cloud events\", async function() {\n const endSpy = sinon.spy(res.end);\n\n const dispatcher = new CloudEventsDispatcher(\"hub1\");\n const result = await dispatcher.handleRequest(req, res);\n assert.isFalse(result);\n assert.isTrue(endSpy.notCalled);\n });\n\n it(\"Should not handle the request if hub does not match\", async function() {\n const endSpy = sinon.spy(res, \"end\");\n buildRequest(req, \"hub\", \"conn1\");\n\n const dispatcher = new CloudEventsDispatcher(\"hub1\");\n const result = await dispatcher.handleRequest(req, res);\n assert.isFalse(result);\n assert.isTrue(endSpy.notCalled);\n });\n\n it(\"Should response with 200 when option is not specified\", async function() {\n const endSpy = sinon.spy(res, \"end\");\n buildRequest(req, \"hub\", \"conn1\");\n\n const dispatcher = new CloudEventsDispatcher(\"hub\");\n const result = await dispatcher.handleRequest(req, res);\n assert.isTrue(result, \"should handle\");\n assert.isTrue(endSpy.calledOnce, \"should call once\");\n assert.equal(200, res.statusCode, \"should be 200\");\n });\n\n it(\"Should response with 200 when handler is not specified\", async function() {\n const endSpy = sinon.spy(res, \"end\");\n buildRequest(req, \"hub\", \"conn1\");\n\n const dispatcher = new CloudEventsDispatcher(\"hub\", {});\n const result = await dispatcher.handleRequest(req, res);\n assert.isTrue(result, \"should handle\");\n assert.isTrue(endSpy.calledOnce, \"should call once\");\n assert.equal(200, res.statusCode, \"should be 200\");\n });\n\n it(\"Should response with error when handler returns error\", async function() {\n const endSpy = sinon.spy(res, \"end\");\n buildRequest(req, \"hub\", \"conn1\");\n\n const dispatcher = new CloudEventsDispatcher(\"hub\", {\n handleConnect: async (_, response) => {\n response.fail(400);\n }\n });\n const process = dispatcher.handleRequest(req, res);\n mockBody(req, JSON.stringify({}));\n const result = await process;\n assert.isTrue(result, \"should handle\");\n assert.isTrue(endSpy.calledOnce, \"should call once\");\n assert.equal(400, res.statusCode, \"should be error\");\n });\n\n it(\"Should response with success when handler returns success\", async function() {\n const endSpy = sinon.spy(res, \"end\");\n buildRequest(req, \"hub\", \"conn1\");\n\n const dispatcher = new CloudEventsDispatcher(\"hub\", {\n handleConnect: async (_, response) => {\n response.success();\n }\n });\n const process = dispatcher.handleRequest(req, res);\n mockBody(req, JSON.stringify({}));\n const result = await process;\n assert.isTrue(result, \"should handle\");\n assert.isTrue(endSpy.calledOnce, \"should call once\");\n assert.equal(200, res.statusCode, \"should be success\");\n });\n\n it(\"Should response with success when handler returns success value\", async function() {\n const endSpy = sinon.spy(res, \"end\");\n buildRequest(req, \"hub\", \"conn1\");\n\n const dispatcher = new CloudEventsDispatcher(\"hub\", {\n handleConnect: async (_, response) => {\n response.success({ userId: \"vic\" });\n }\n });\n const process = dispatcher.handleRequest(req, res);\n mockBody(req, JSON.stringify({}));\n const result = await process;\n assert.isTrue(result, \"should handle\");\n assert.isTrue(endSpy.calledOnce, \"should call once\");\n assert.equal(200, res.statusCode, \"should be success\");\n });\n\n it(\"Should be able to set connection state\", async function() {\n const endSpy = sinon.spy(res, \"end\");\n buildRequest(req, \"hub\", \"conn1\");\n\n const dispatcher = new CloudEventsDispatcher(\"hub\", {\n handleConnect: async (_, response) => {\n response.setState(\"key1\", \"val1\");\n response.setState(\"key2\", \"val2\");\n response.setState(\"key1\", [\"val3\"]);\n response.setState(\"key3\", \"\");\n response.success({ userId: \"vic\" });\n }\n });\n const process = dispatcher.handleRequest(req, res);\n mockBody(req, JSON.stringify({}));\n const result = await process;\n assert.isTrue(result, \"should handle\");\n assert.isTrue(endSpy.calledOnce, \"should call once\");\n assert.equal(\n toBase64JsonString({\n key1: [\"val3\"],\n key2: \"val2\",\n key3: \"\"\n }),\n res.getHeader(\"ce-connectionState\"),\n \"should contain multiple state headers\"\n );\n });\n\n it(\"Should be able to get the connection states if it exists in the header\", async function() {\n const endSpy = sinon.spy(res, \"end\");\n const states = toBase64JsonString({\n key1: [\"val3\"],\n key2: \"val2\",\n key3: \"\"\n });\n buildRequest(req, \"hub1\", \"conn1\", undefined, states);\n const dispatcher = new CloudEventsDispatcher(\"hub1\", {\n handleConnect: (request, response) => {\n assert.equal(\"val3\", request.context.states[\"key1\"][0]);\n assert.equal(\"val2\", request.context.states[\"key2\"]);\n assert.equal(\"\", request.context.states[\"key3\"]);\n response.success();\n }\n });\n const process = dispatcher.handleRequest(req, res);\n mockBody(req, JSON.stringify({}));\n const result = await process;\n assert.isTrue(result, \"should handle\");\n assert.isTrue(endSpy.calledOnce, \"should call once\");\n assert.equal(200, res.statusCode, \"should be success\");\n });\n\n it(\"Invalid state header gets ignored\", async function() {\n const endSpy = sinon.spy(res, \"end\");\n buildRequest(req, \"hub1\", \"conn1\", undefined, \"\");\n const dispatcher = new CloudEventsDispatcher(\"hub1\", {\n handleConnect: (request, response) => {\n assert.deepEqual({}, request.context.states);\n response.success();\n }\n });\n const process = dispatcher.handleRequest(req, res);\n mockBody(req, JSON.stringify({}));\n const result = await process;\n assert.isTrue(result, \"should handle\");\n assert.isTrue(endSpy.calledOnce, \"should call once\");\n assert.equal(200, res.statusCode, \"should be success\");\n });\n});\n"]}
|
|
@@ -9,6 +9,7 @@ import * as sinon from "sinon";
|
|
|
9
9
|
function buildRequest(req, hub, connectionId, userId) {
|
|
10
10
|
req.headers["webhook-request-origin"] = "xxx.webpubsub.azure.com";
|
|
11
11
|
req.headers["Content-Type"] = "application/json; charset=utf-8";
|
|
12
|
+
req.headers["ce-awpsversion"] = "1.0";
|
|
12
13
|
req.headers["ce-specversion"] = "1.0";
|
|
13
14
|
req.headers["ce-type"] = "azure.webpubsub.user.connected";
|
|
14
15
|
req.headers["ce-source"] = `/hubs/${hub}/client/${connectionId}`;
|
|
@@ -34,24 +35,24 @@ describe("Can handle connected event", function () {
|
|
|
34
35
|
});
|
|
35
36
|
it("Should not handle the request if request is not cloud events", async function () {
|
|
36
37
|
const endSpy = sinon.spy(res.end);
|
|
37
|
-
const dispatcher = new CloudEventsDispatcher("hub1"
|
|
38
|
-
const result = await dispatcher.
|
|
38
|
+
const dispatcher = new CloudEventsDispatcher("hub1");
|
|
39
|
+
const result = await dispatcher.handleRequest(req, res);
|
|
39
40
|
assert.isFalse(result);
|
|
40
41
|
assert.isTrue(endSpy.notCalled);
|
|
41
42
|
});
|
|
42
43
|
it("Should not handle the request if hub does not match", async function () {
|
|
43
44
|
const endSpy = sinon.spy(res.end);
|
|
44
45
|
buildRequest(req, "hub", "conn1");
|
|
45
|
-
const dispatcher = new CloudEventsDispatcher("hub1"
|
|
46
|
-
const result = await dispatcher.
|
|
46
|
+
const dispatcher = new CloudEventsDispatcher("hub1");
|
|
47
|
+
const result = await dispatcher.handleRequest(req, res);
|
|
47
48
|
assert.isFalse(result);
|
|
48
49
|
assert.isTrue(endSpy.notCalled);
|
|
49
50
|
});
|
|
50
51
|
it("Should response with 200 when option is not specified", async function () {
|
|
51
52
|
const endSpy = sinon.spy(res, "end");
|
|
52
53
|
buildRequest(req, "hub", "conn1");
|
|
53
|
-
const dispatcher = new CloudEventsDispatcher("hub"
|
|
54
|
-
const result = await dispatcher.
|
|
54
|
+
const dispatcher = new CloudEventsDispatcher("hub");
|
|
55
|
+
const result = await dispatcher.handleRequest(req, res);
|
|
55
56
|
assert.isTrue(result, "should handle");
|
|
56
57
|
assert.isTrue(endSpy.calledOnce, "should call once");
|
|
57
58
|
assert.equal(200, res.statusCode, "should be 200");
|
|
@@ -59,8 +60,8 @@ describe("Can handle connected event", function () {
|
|
|
59
60
|
it("Should response with 200 when handler is not specified", async function () {
|
|
60
61
|
const endSpy = sinon.spy(res, "end");
|
|
61
62
|
buildRequest(req, "hub", "conn1");
|
|
62
|
-
const dispatcher = new CloudEventsDispatcher("hub",
|
|
63
|
-
const result = await dispatcher.
|
|
63
|
+
const dispatcher = new CloudEventsDispatcher("hub", {});
|
|
64
|
+
const result = await dispatcher.handleRequest(req, res);
|
|
64
65
|
assert.isTrue(result, "should handle");
|
|
65
66
|
assert.isTrue(endSpy.calledOnce, "should call once");
|
|
66
67
|
assert.equal(200, res.statusCode, "should be 200");
|
|
@@ -68,12 +69,12 @@ describe("Can handle connected event", function () {
|
|
|
68
69
|
it("Should response 200 even the event handler throws", async function () {
|
|
69
70
|
const endSpy = sinon.spy(res, "end");
|
|
70
71
|
buildRequest(req, "hub", "conn1");
|
|
71
|
-
const dispatcher = new CloudEventsDispatcher("hub",
|
|
72
|
+
const dispatcher = new CloudEventsDispatcher("hub", {
|
|
72
73
|
onConnected: async (_) => {
|
|
73
74
|
throw new Error();
|
|
74
75
|
}
|
|
75
76
|
});
|
|
76
|
-
const process = dispatcher.
|
|
77
|
+
const process = dispatcher.handleRequest(req, res);
|
|
77
78
|
mockBody(req, JSON.stringify({}));
|
|
78
79
|
const result = await process;
|
|
79
80
|
assert.isTrue(result, "should handle");
|