Skip to content

Commit

Permalink
WebSocket support (mockoon#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
isuru89 committed Feb 1, 2024
1 parent 29d3e50 commit b10d946
Show file tree
Hide file tree
Showing 95 changed files with 8,897 additions and 20,388 deletions.
6 changes: 4 additions & 2 deletions packages/cli/test/data/envs/file.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"uuid": "6cca9fb6-3585-4c0f-8cac-1150ce24452b",
"name": "Environment file",
"lastMigration": 32,
"lastMigration": 33,
"port": 3000,
"endpointPrefix": "",
"latency": 0,
Expand Down Expand Up @@ -34,7 +34,9 @@
}
],
"responseMode": null,
"type": "http"
"type": "http",
"streamingMode": null,
"streamingInterval": 0
}
],
"proxyMode": false,
Expand Down
6 changes: 4 additions & 2 deletions packages/cli/test/data/envs/mock1.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"uuid": "70d8639e-ef20-4d52-9749-c47d9ffc5c67",
"lastMigration": 32,
"lastMigration": 33,
"name": "mock1",
"endpointPrefix": "api",
"latency": 0,
Expand Down Expand Up @@ -33,7 +33,9 @@
}
],
"responseMode": null,
"type": "http"
"type": "http",
"streamingMode": null,
"streamingInterval": 0
}
],
"proxyMode": false,
Expand Down
6 changes: 4 additions & 2 deletions packages/cli/test/data/envs/mock1noext
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"uuid": "70d8639e-ef20-4d52-9749-c47d9ffc5c67",
"lastMigration": 32,
"lastMigration": 33,
"name": "mock1",
"endpointPrefix": "api",
"latency": 0,
Expand Down Expand Up @@ -33,7 +33,9 @@
}
],
"responseMode": null,
"type": "http"
"type": "http",
"streamingMode": null,
"streamingInterval": 0
}
],
"proxyMode": false,
Expand Down
6 changes: 4 additions & 2 deletions packages/cli/test/data/envs/mock2.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"uuid": "66f5a033-0d8b-4bc6-b5f9-a949d8fd0ec2",
"lastMigration": 32,
"lastMigration": 33,
"name": "mock2",
"endpointPrefix": "api",
"latency": 0,
Expand Down Expand Up @@ -33,7 +33,9 @@
}
],
"responseMode": null,
"type": "http"
"type": "http",
"streamingMode": null,
"streamingInterval": 0
}
],
"proxyMode": false,
Expand Down
6 changes: 4 additions & 2 deletions packages/cli/test/data/envs/mock3.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"uuid": "f4c8cd6e-dbf4-4865-9558-08195ef8b36b",
"lastMigration": 32,
"lastMigration": 33,
"name": "mock1",
"endpointPrefix": "api",
"latency": 0,
Expand Down Expand Up @@ -33,7 +33,9 @@
}
],
"responseMode": null,
"type": "http"
"type": "http",
"streamingMode": null,
"streamingInterval": 0
}
],
"proxyMode": false,
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/test/data/envs/mock4.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"uuid": "5d004e2c-6055-41bc-b0ac-4d0c064dddd1",
"lastMigration": 32,
"lastMigration": 33,
"name": "mockhttps",
"endpointPrefix": "api",
"latency": 0,
Expand Down
14 changes: 10 additions & 4 deletions packages/cli/test/data/envs/petstore-imported.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"uuid": "2721eb99-7031-40ae-8725-0c37edc3976a",
"lastMigration": 32,
"lastMigration": 33,
"name": "Swagger Petstore",
"endpointPrefix": "v1",
"latency": 0,
Expand Down Expand Up @@ -57,7 +57,9 @@
"callbacks": []
}
],
"responseMode": null
"responseMode": null,
"streamingMode": null,
"streamingInterval": 0
},
{
"uuid": "094031a7-93fc-4659-82b4-58c8c9de10b0",
Expand Down Expand Up @@ -105,7 +107,9 @@
"callbacks": []
}
],
"responseMode": null
"responseMode": null,
"streamingMode": null,
"streamingInterval": 0
},
{
"uuid": "4b9356a9-d656-4ea8-acf3-c254fb9d6616",
Expand Down Expand Up @@ -153,7 +157,9 @@
"callbacks": []
}
],
"responseMode": null
"responseMode": null,
"streamingMode": null,
"streamingInterval": 0
}
],
"rootChildren": [
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/test/data/envs/wrong-cert.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"uuid": "06d4e2f2-b7a1-4717-a61d-94666816c30f",
"lastMigration": 32,
"lastMigration": 33,
"name": "test",
"endpointPrefix": "",
"latency": 0,
Expand Down
46 changes: 46 additions & 0 deletions packages/commons-server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/commons-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"range-parser": "1.2.1",
"typed-emitter": "2.1.0",
"winston": "3.10.0",
"ws": "8.14.2",
"xml-js": "1.6.11"
},
"devDependencies": {
Expand All @@ -62,6 +63,7 @@
"@types/node": "20.7.1",
"@types/object-path": "0.11.2",
"@types/qs": "6.9.8",
"@types/ws": "8.5.10",
"@typescript-eslint/eslint-plugin": "6.7.3",
"@typescript-eslint/parser": "6.7.3",
"assert-request": "1.0.6",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,9 @@ export const ServerMessages = {
SERVER_STOPPED: 'Server stopped',
SERVER_CREATING_PROXY: 'Creating proxy to %s',
CALLBACK_FILE_ERROR: 'Error while attaching file to callback request: %s',
CALLBACK_ERROR: 'Error while executing callback: %s'
CALLBACK_ERROR: 'Error while executing callback: %s',

WS_UNKNOWN_ROUTE: 'No matching route found for websocket! %s',
WS_UNSUPPORTED_CONTENT: 'Unsupported content recieved from websocket! %s',
WS_SERVING_ERROR: 'Web socket serving error! %s'
};
75 changes: 75 additions & 0 deletions packages/commons-server/src/libs/server/events-listeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
CloneObject,
Environment,
Header,
InFlightRequest,
InvokedCallback,
Methods,
Transaction
Expand Down Expand Up @@ -74,6 +75,9 @@ export const listenServerEvents = function (
case 'HEADER_PARSING_ERROR':
case 'CALLBACK_ERROR':
case 'CALLBACK_FILE_ERROR':
case 'WS_SERVING_ERROR':
case 'WS_UNKNOWN_ROUTE':
case 'WS_UNSUPPORTED_CONTENT':
message = format(ServerMessages[errorCode], error?.message || '');
break;
case 'CERT_FILE_NOT_FOUND':
Expand Down Expand Up @@ -144,4 +148,75 @@ export const listenServerEvents = function (
}
logger.info('Callback invoked', logMeta);
});

server.on('ws-new-connection', (inflightRequest: InFlightRequest) => {
const logMeta: { inflightRequest: InFlightRequest } = {
...defaultLogMeta,
websocketId: inflightRequest.requestId,
url: inflightRequest.request.urlPath,
routeUUID: inflightRequest.routeUUID
};

if (logTransaction) {
logMeta.inflightRequest = CloneObject(inflightRequest) as InFlightRequest;
logMeta.inflightRequest.request.headers = logMeta.inflightRequest.request
?.headers
? logMeta.inflightRequest.request?.headers.map(
filterAuthorizationHeaders
)
: [];
}
logger.info('WebSocket New Connection', logMeta);
});

server.on(
'ws-message-received',
(inflightRequest: InFlightRequest, message: string) => {
const logMeta: { inflightRequest: InFlightRequest; messageData: string } =
{
...defaultLogMeta,
websocketId: inflightRequest.requestId,
url: inflightRequest.request.urlPath,
routeUUID: inflightRequest.routeUUID
};

if (logTransaction) {
logMeta.inflightRequest = CloneObject(
inflightRequest
) as InFlightRequest;
logMeta.messageData = message;
logMeta.inflightRequest.request.headers = logMeta.inflightRequest
.request?.headers
? logMeta.inflightRequest.request?.headers.map(
filterAuthorizationHeaders
)
: [];
}
logger.info('WebSocket Message Recieved', logMeta);
}
);

server.on(
'ws-closed',
(request: InFlightRequest, code: number, reason?: string | null) => {
const logMeta: { inflightRequest: InFlightRequest } = {
...defaultLogMeta,
websocketId: request.requestId,
code,
reason
};

if (logTransaction) {
logMeta.inflightRequest = CloneObject(request) as InFlightRequest;
logMeta.inflightRequest.request.headers = logMeta.inflightRequest
.request?.headers
? logMeta.inflightRequest.request?.headers.map(
filterAuthorizationHeaders
)
: [];
}

logger.info('WebSocket Closed', logMeta);
}
);
};

0 comments on commit b10d946

Please sign in to comment.