@resolveio/server-lib 20.10.0 → 20.10.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.
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/managers/websocket.manager.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,iCAAqC;AACrC,qCAAgC;AAShC;IAWC;QATQ,oBAAe,GAAuC,IAAI,GAAG,EAAE,CAAC;QAChE,oBAAe,GAAgC,IAAI,GAAG,EAAE,CAAC;QAGzD,gBAAW,GAA2B,IAAI,GAAG,EAAE,CAAC;QAGvC,mBAAc,GAAG,WAAW,CAAC;IAE/B,CAAC;IAET,uBAAM,GAAb,UAAc,UAAU,EAAE,cAA8B,EAAE,YAAyB,EAAE,mBAAyC,EAAE,cAAmC;QAAzI,+BAAA,EAAA,sBAA8B;QAAE,6BAAA,EAAA,iBAAyB;QAAE,oCAAA,EAAA,sBAA8B,IAAI,GAAG,IAAI;QAAE,+BAAA,EAAA,iBAAyB,GAAG,GAAG,IAAI;QAClK,IAAM,gBAAgB,GAAG,IAAI,gBAAgB,EAAE,CAAC;QAChD,gBAAgB,CAAC,UAAU,CAAC,UAAU,EAAE,cAAc,EAAE,YAAY,EAAE,mBAAmB,EAAE,cAAc,CAAC,CAAC;QAC3G,OAAO,gBAAgB,CAAC;IACzB,CAAC;IAEM,qCAAU,GAAjB,UAAkB,UAAU,EAAE,cAA8B,EAAE,YAAyB,EAAE,mBAAyC,EAAE,cAAmC;QAAzI,+BAAA,EAAA,sBAA8B;QAAE,6BAAA,EAAA,iBAAyB;QAAE,oCAAA,EAAA,sBAA8B,IAAI,GAAG,IAAI;QAAE,+BAAA,EAAA,iBAAyB,GAAG,GAAG,IAAI;QACtK,IAAI,CAAC,WAAW,GAAG,UAAU,CAAC;QAC9B,IAAI,CAAC,eAAe,GAAG,cAAc,CAAC;QACtC,IAAI,CAAC,aAAa,GAAG,YAAY,CAAC;QAClC,IAAI,CAAC,oBAAoB,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,UAAC,KAAK;oBACzB,IAAI,KAAK,EAAE;wBACV,KAAI,CAAC,eAAe,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC;qBAChC;gBACF,CAAC,CAAC,CAAC;aACH;SACD;aACI;YACJ,IAAM,YAAY,GAAG,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;YAC3C,IAAM,QAAQ,GAAG,MAAM,CAAC,UAAU,CAAC,YAAY,EAAE,MAAM,CAAC,CAAC;YAEzD,IAAI,CAAC,eAAe,IAAI,QAAQ,GAAG,IAAI,CAAC,eAAe,EAAE;gBACxD,IAAI,CAAC,oBAAoB,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC;aACrC;iBACI;gBACJ,EAAE,CAAC,IAAI,CAAC,YAAY,EAAE,UAAC,KAAK;oBAC3B,IAAI,KAAK,EAAE;wBACV,KAAI,CAAC,eAAe,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC;qBAChC;gBACF,CAAC,CAAC,CAAC;aACH;SACD;IACF,CAAC;IAEO,0CAAe,GAAvB;QACC,OAAO,IAAA,oBAAW,EAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;IACvC,CAAC;IACF,uBAAC;AAAD,CAvUA,AAuUC,IAAA;AAvUY,4CAAgB","file":"websocket.manager.js","sourcesContent":["import { randomBytes } from 'crypto';\nimport { pack } from 'msgpackr';\nimport { WebSocket } from 'ws';\n\ninterface ServerResponseModel {\n\tmessageId: number;\n\thasError: boolean;\n\tdata: any;\n}\n\nexport class WebSocketManager {\n\tprivate _mainServer;\n\tprivate _messageBuffers: Map<string, ServerResponseModel[]> = new Map();\n\tprivate _batchingTimers: Map<string, NodeJS.Timeout> = new Map();\n\tprivate _maxMessageSize: number;\n\tprivate _messageDelay: number;\n\tprivate _webSockets: Map<string, WebSocket> = new Map();\n\tprivate _chunkThresholdBytes: number;\n\tprivate _chunkSizeBytes: number;\n\tprivate readonly _chunkMetaFlag = '__chunked';\n\n\tconstructor() {}\n\t\n\tstatic create(mainServer, maxMessageSize: number = 20480, messageDelay: number = 25, chunkThresholdBytes: number = 1024 * 1024, chunkSizeBytes: number = 512 * 1024) {\n\t\tconst websocketManager = new WebSocketManager();\n\t\twebsocketManager.initialize(mainServer, maxMessageSize, messageDelay, chunkThresholdBytes, chunkSizeBytes);\n\t\treturn websocketManager;\n\t}\n\n\tpublic initialize(mainServer, maxMessageSize: number = 20480, messageDelay: number = 25, chunkThresholdBytes: number = 1024 * 1024, chunkSizeBytes: number = 512 * 1024) {\n\t\tthis._mainServer = mainServer;\n\t\tthis._maxMessageSize = maxMessageSize;\n\t\tthis._messageDelay = messageDelay;\n\t\tthis._chunkThresholdBytes = chunkThresholdBytes;\n\t\tthis._chunkSizeBytes = chunkSizeBytes;\n\t}\n\n\tpublic addWebSocket(ws: WebSocket): void {\n\t\tconst wsId = ws['id_socket'];\n\t\tthis._webSockets.set(wsId, ws);\n\t}\n\n\tpublic removeWebSocket(ws: WebSocket): void {\n\t\tconst wsId = ws['id_socket'];\n\t\tthis._webSockets.delete(wsId);\n\t\tthis.closeConnection(ws);\n\t}\n\n\tpublic getWebSocket(id_socket: string): WebSocket | undefined {\n\t\treturn this._webSockets.get(id_socket);\n\t}\n\n\tpublic send(ws: WebSocket, data: ServerResponseModel | string): void {\n\t\tconst wsId = ws['id_socket'];\n\n\t\tif (!this._messageBuffers.has(wsId)) {\n\t\t\tthis._messageBuffers.set(wsId, []);\n\t\t}\n\n\t\tconst supportsBinary = ws['supportsBinary'] === true;\n\n\t\tif (typeof data === 'string') {\n\t\t\t// Handle string messages like 'ping' or 'pong'\n\t\t\tthis._messageBuffers.get(wsId).push({ messageId: null, hasError: false, data: data });\n\t\t}\n\t\telse {\n\t\t\tconst chunkContext = this.prepareChunkContext(data, supportsBinary);\n\t\t\tif (chunkContext) {\n\t\t\t\tthis.flushMessages(ws);\n\t\t\t\tthis.sendChunkSequence(ws, data, chunkContext, supportsBinary);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tthis._messageBuffers.get(wsId).push(data);\n\t\t}\n\n\t\tif (!this._batchingTimers.has(wsId)) {\n\t\t\t// Set a timer to send the batch after a short delay\n\t\t\tconst timer = setTimeout(() => {\n\t\t\t\tthis.flushMessages(ws);\n\t\t\t}, this._messageDelay);\n\t\t\tthis._batchingTimers.set(wsId, timer);\n\t\t}\n\t}\n\n\tprivate flushMessages(ws: WebSocket): void {\n\t\tconst wsId = ws['id_socket'];\n\t\tconst messages = this._messageBuffers.get(wsId);\n\t\tconst supportsBinary = ws['supportsBinary'] === true;\n\n\t\tif (messages && messages.length > 0) {\n\t\t\t// Combine messages into one payload\n\t\t\tthis.transmitBatch(ws, messages, supportsBinary);\n\n\t\t\tthis._messageBuffers.delete(wsId);\n\t\t\tclearTimeout(this._batchingTimers.get(wsId));\n\t\t\tthis._batchingTimers.delete(wsId);\n\t\t}\n\t}\n\n\tprivate sendMessagesInChunks(ws: WebSocket, messages: ServerResponseModel[]): void {\n\t\tlet chunk: ServerResponseModel[] = [];\n\t\tlet chunkSize = 0;\n\n\t\tfor (const message of messages) {\n\t\t\tconst messageData = JSON.stringify([message]); // Wrap message in array for consistency\n\t\t\tconst messageSize = Buffer.byteLength(messageData, 'utf8');\n\n\t\t\tif (chunkSize + messageSize > this._maxMessageSize) {\n\t\t\t\t// Send current chunk\n\t\t\t\tws.send(JSON.stringify(chunk), (error) => {\n\t\t\t\t\tif (error) {\n\t\t\t\t\t\tthis.handleSendError(ws, error);\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t\t// Start new chunk\n\t\t\t\tchunk = [message];\n\t\t\t\tchunkSize = messageSize;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tchunk.push(message);\n\t\t\t\tchunkSize += messageSize;\n\t\t\t}\n\t\t}\n\n\t\t// Send any remaining messages\n\t\tif (chunk.length > 0) {\n\t\t\tws.send(JSON.stringify(chunk), (error) => {\n\t\t\t\tif (error) {\n\t\t\t\t\tthis.handleSendError(ws, error);\n\t\t\t\t}\n\t\t\t});\n\t\t}\n\t}\n\n\tprivate sendMessagesInChunksBinary(ws: WebSocket, messages: ServerResponseModel[]): void {\n\t\tlet chunk: ServerResponseModel[] = [];\n\t\tlet chunkSize = 0;\n\n\t\tfor (const message of messages) {\n\t\t\tconst packedMessage = pack([message]);\n\t\t\tconst messageSize = packedMessage.byteLength;\n\n\t\t\tif (chunkSize + messageSize > this._maxMessageSize) {\n\t\t\t\tconst packedChunk = pack(chunk);\n\t\t\t\tws.send(packedChunk, (error) => {\n\t\t\t\t\tif (error) {\n\t\t\t\t\t\tthis.handleSendError(ws, error);\n\t\t\t\t\t}\n\t\t\t\t});\n\n\t\t\t\tchunk = [message];\n\t\t\t\tchunkSize = messageSize;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tchunk.push(message);\n\t\t\t\tchunkSize += messageSize;\n\t\t\t}\n\t\t}\n\n\t\tif (chunk.length > 0) {\n\t\t\tconst packedChunk = pack(chunk);\n\t\t\tws.send(packedChunk, (error) => {\n\t\t\t\tif (error) {\n\t\t\t\t\tthis.handleSendError(ws, error);\n\t\t\t\t}\n\t\t\t});\n\t\t}\n\t}\n\n\tprivate handleSendError(ws: WebSocket, error: Error): void {\n\t\tif (this._mainServer.getSubscriptionManager()) {\n\t\t\t// Handle send error, e.g., unsubscribe, close connection\n\t\t\tif (this._mainServer.getSubscriptionManager().getEnableDebug()) {\n\t\t\t\tconsole.log(new Date(), 'WebSocketManager', 'Unsub WS', ws['user'], ws['id_socket'], error);\n\t\t\t}\n\t\t\tthis._mainServer.getSubscriptionManager().unsubscribeAll(ws);\n\t\t\tthis.removeWebSocket(ws);\n\t\t}\n\t}\n\n\tpublic closeConnection(ws: WebSocket): void {\n\t\tws.close();\n\t\tconst wsId = ws['id_socket'];\n\t\t// Clean up buffers and timers\n\t\tthis._messageBuffers.delete(wsId);\n\t\tif (this._batchingTimers.has(wsId)) {\n\t\t\tclearTimeout(this._batchingTimers.get(wsId));\n\t\t\tthis._batchingTimers.delete(wsId);\n\t\t}\n \n process.nextTick(() => {\n if ([ws.OPEN, ws.CLOSING].includes(<any>ws.readyState)) {\n // Socket still hangs, hard close\n ws.terminate();\n }\n });\n\t}\n\n\tprivate prepareChunkContext(message: ServerResponseModel, supportsBinary: boolean): { buffer: Buffer, encoding: 'msgpack' | 'json' } | null {\n\t\tif (!message || message.hasError) {\n\t\t\treturn null;\n\t\t}\n\n\t\tconst data = message.data;\n\t\tif (data === undefined || data === null) {\n\t\t\treturn null;\n\t\t}\n\n\t\ttry {\n\t\t\tif (supportsBinary) {\n\t\t\t\tconst packed = Buffer.from(<any>pack(data));\n\t\t\t\tif (packed.byteLength > this._chunkThresholdBytes) {\n\t\t\t\t\treturn { buffer: packed, encoding: 'msgpack' };\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcatch (err) {\n\t\t\t// Fall back to JSON chunking if msgpack encoding fails\n\t\t\tif (this._mainServer.getSubscriptionManager()?.getEnableDebug()) {\n\t\t\t\tconsole.log(new Date(), 'WebSocketManager', 'Chunking pack failed, falling back to JSON', err?.message || err);\n\t\t\t}\n\t\t}\n\n\t\ttry {\n\t\t\tconst jsonString = JSON.stringify(data);\n\t\t\tconst jsonBytes = Buffer.byteLength(jsonString, 'utf8');\n\n\t\t\tif (jsonBytes > this._chunkThresholdBytes) {\n\t\t\t\treturn { buffer: Buffer.from(jsonString, 'utf8'), encoding: 'json' };\n\t\t\t}\n\t\t}\n\t\tcatch {\n\t\t\t// Ignore JSON stringify errors; fallback to normal send\n\t\t}\n\n\t\treturn null;\n\t}\n\n\tprivate sendChunkSequence(ws: WebSocket, original: ServerResponseModel, chunkContext: { buffer: Buffer, encoding: 'msgpack' | 'json' }, supportsBinary: boolean): void {\n\t\tconst chunkId = this.generateChunkId();\n\t\tconst totalChunks = Math.ceil(chunkContext.buffer.byteLength / this._chunkSizeBytes);\n\t\tconst useBinary = supportsBinary && chunkContext.encoding === 'msgpack';\n\n\t\tconst startMessage: ServerResponseModel = {\n\t\t\tmessageId: original.messageId,\n\t\t\thasError: original.hasError,\n\t\t\tdata: {\n\t\t\t\t[this._chunkMetaFlag]: true,\n\t\t\t\tchunkId: chunkId,\n\t\t\t\tchunkStatus: 'start',\n\t\t\t\ttotalChunks: totalChunks,\n\t\t\t\ttotalBytes: chunkContext.buffer.byteLength,\n\t\t\t\tchunkSize: this._chunkSizeBytes,\n\t\t\t\tencoding: chunkContext.encoding\n\t\t\t}\n\t\t};\n\n\t\tthis.transmitBatch(ws, [startMessage], useBinary);\n\n\t\tfor (let index = 0; index < totalChunks; index++) {\n\t\t\tconst start = index * this._chunkSizeBytes;\n\t\t\tconst end = Math.min(chunkContext.buffer.byteLength, start + this._chunkSizeBytes);\n\t\t\tconst slice = chunkContext.buffer.subarray(start, end);\n\t\t\tconst chunkPayload = chunkContext.encoding === 'msgpack' ? slice : slice.toString('base64');\n\n\t\t\tconst chunkMessage: ServerResponseModel = {\n\t\t\t\tmessageId: original.messageId,\n\t\t\t\thasError: original.hasError,\n\t\t\t\tdata: {\n\t\t\t\t\t[this._chunkMetaFlag]: true,\n\t\t\t\t\tchunkId: chunkId,\n\t\t\t\t\tchunkStatus: 'chunk',\n\t\t\t\t\tchunkIndex: index,\n\t\t\t\t\tpayload: chunkPayload,\n\t\t\t\t\tencoding: chunkContext.encoding\n\t\t\t\t}\n\t\t\t};\n\n\t\t\tthis.transmitBatch(ws, [chunkMessage], useBinary);\n\t\t}\n\n\t\tconst endMessage: ServerResponseModel = {\n\t\t\tmessageId: original.messageId,\n\t\t\thasError: original.hasError,\n\t\t\tdata: {\n\t\t\t\t[this._chunkMetaFlag]: true,\n\t\t\t\tchunkId: chunkId,\n\t\t\t\tchunkStatus: 'end'\n\t\t\t}\n\t\t};\n\n\t\tthis.transmitBatch(ws, [endMessage], useBinary);\n\t}\n\n\tprivate transmitBatch(ws: WebSocket, batch: ServerResponseModel[], useBinary: boolean): void {\n\t\tif (!batch || !batch.length) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst isChunkEnvelope = batch.length === 1 && batch[0]?.data && typeof batch[0].data === 'object' && batch[0].data[this._chunkMetaFlag];\n\n\t\tif (useBinary) {\n\t\t\tconst packedData = pack(batch);\n\t\t\tconst dataSize = packedData.byteLength;\n\n\t\t\tif (!isChunkEnvelope && dataSize > this._maxMessageSize) {\n\t\t\t\tthis.sendMessagesInChunksBinary(ws, batch);\n\t\t\t}\n\t\t\telse {\n\t\t\t\tws.send(packedData, (error) => {\n\t\t\t\t\tif (error) {\n\t\t\t\t\t\tthis.handleSendError(ws, error);\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\tconst combinedData = JSON.stringify(batch);\n\t\t\tconst dataSize = Buffer.byteLength(combinedData, 'utf8');\n\n\t\t\tif (!isChunkEnvelope && dataSize > this._maxMessageSize) {\n\t\t\t\tthis.sendMessagesInChunks(ws, batch);\n\t\t\t}\n\t\t\telse {\n\t\t\t\tws.send(combinedData, (error) => {\n\t\t\t\t\tif (error) {\n\t\t\t\t\t\tthis.handleSendError(ws, error);\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate generateChunkId(): string {\n\t\treturn randomBytes(8).toString('hex');\n\t}\n}\n"]}
1
+ {"version":3,"sources":["../../src/managers/websocket.manager.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,iCAAqC;AACrC,qCAAgC;AAShC;IAWC;QATQ,oBAAe,GAAuC,IAAI,GAAG,EAAE,CAAC;QAChE,oBAAe,GAAgC,IAAI,GAAG,EAAE,CAAC;QAGzD,gBAAW,GAA2B,IAAI,GAAG,EAAE,CAAC;QAGvC,mBAAc,GAAG,WAAW,CAAC;IAE/B,CAAC;IAET,uBAAM,GAAb,UAAc,UAAU,EAAE,cAA8B,EAAE,YAAyB,EAAE,mBAA6C,EAAE,cAAwC;QAAlJ,+BAAA,EAAA,sBAA8B;QAAE,6BAAA,EAAA,iBAAyB;QAAE,oCAAA,EAAA,sBAA8B,CAAC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Ae,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC;qBAChC;gBACF,CAAC,CAAC,CAAC;aACH;SACD;IACF,CAAC;IAEO,0CAAe,GAAvB;QACC,OAAO,IAAA,oBAAW,EAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;IACvC,CAAC;IACF,uBAAC;AAAD,CAvUA,AAuUC,IAAA;AAvUY,4CAAgB","file":"websocket.manager.js","sourcesContent":["import { randomBytes } from 'crypto';\nimport { pack } from 'msgpackr';\nimport { WebSocket } from 'ws';\n\ninterface ServerResponseModel {\n\tmessageId: number;\n\thasError: boolean;\n\tdata: any;\n}\n\nexport class WebSocketManager {\n\tprivate _mainServer;\n\tprivate _messageBuffers: Map<string, ServerResponseModel[]> = new Map();\n\tprivate _batchingTimers: Map<string, NodeJS.Timeout> = new Map();\n\tprivate _maxMessageSize: number;\n\tprivate _messageDelay: number;\n\tprivate _webSockets: Map<string, WebSocket> = new Map();\n\tprivate _chunkThresholdBytes: number;\n\tprivate _chunkSizeBytes: number;\n\tprivate readonly _chunkMetaFlag = '__chunked';\n\n\tconstructor() {}\n\t\n\tstatic create(mainServer, maxMessageSize: number = 20480, messageDelay: number = 25, chunkThresholdBytes: number = 3 * 1024 * 1024, chunkSizeBytes: number = 4 * 1024 * 1024) {\n\t\tconst websocketManager = new WebSocketManager();\n\t\twebsocketManager.initialize(mainServer, maxMessageSize, messageDelay, chunkThresholdBytes, chunkSizeBytes);\n\t\treturn websocketManager;\n\t}\n\n\tpublic initialize(mainServer, maxMessageSize: number = 20480, messageDelay: number = 25, chunkThresholdBytes: number = 3 * 1024 * 1024, chunkSizeBytes: number = 4 * 1024 * 1024) {\n\t\tthis._mainServer = mainServer;\n\t\tthis._maxMessageSize = maxMessageSize;\n\t\tthis._messageDelay = messageDelay;\n\t\tthis._chunkThresholdBytes = chunkThresholdBytes;\n\t\tthis._chunkSizeBytes = chunkSizeBytes;\n\t}\n\n\tpublic addWebSocket(ws: WebSocket): void {\n\t\tconst wsId = ws['id_socket'];\n\t\tthis._webSockets.set(wsId, ws);\n\t}\n\n\tpublic removeWebSocket(ws: WebSocket): void {\n\t\tconst wsId = ws['id_socket'];\n\t\tthis._webSockets.delete(wsId);\n\t\tthis.closeConnection(ws);\n\t}\n\n\tpublic getWebSocket(id_socket: string): WebSocket | undefined {\n\t\treturn this._webSockets.get(id_socket);\n\t}\n\n\tpublic send(ws: WebSocket, data: ServerResponseModel | string): void {\n\t\tconst wsId = ws['id_socket'];\n\n\t\tif (!this._messageBuffers.has(wsId)) {\n\t\t\tthis._messageBuffers.set(wsId, []);\n\t\t}\n\n\t\tconst supportsBinary = ws['supportsBinary'] === true;\n\n\t\tif (typeof data === 'string') {\n\t\t\t// Handle string messages like 'ping' or 'pong'\n\t\t\tthis._messageBuffers.get(wsId).push({ messageId: null, hasError: false, data: data });\n\t\t}\n\t\telse {\n\t\t\tconst chunkContext = this.prepareChunkContext(data, supportsBinary);\n\t\t\tif (chunkContext) {\n\t\t\t\tthis.flushMessages(ws);\n\t\t\t\tthis.sendChunkSequence(ws, data, chunkContext, supportsBinary);\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tthis._messageBuffers.get(wsId).push(data);\n\t\t}\n\n\t\tif (!this._batchingTimers.has(wsId)) {\n\t\t\t// Set a timer to send the batch after a short delay\n\t\t\tconst timer = setTimeout(() => {\n\t\t\t\tthis.flushMessages(ws);\n\t\t\t}, this._messageDelay);\n\t\t\tthis._batchingTimers.set(wsId, timer);\n\t\t}\n\t}\n\n\tprivate flushMessages(ws: WebSocket): void {\n\t\tconst wsId = ws['id_socket'];\n\t\tconst messages = this._messageBuffers.get(wsId);\n\t\tconst supportsBinary = ws['supportsBinary'] === true;\n\n\t\tif (messages && messages.length > 0) {\n\t\t\t// Combine messages into one payload\n\t\t\tthis.transmitBatch(ws, messages, supportsBinary);\n\n\t\t\tthis._messageBuffers.delete(wsId);\n\t\t\tclearTimeout(this._batchingTimers.get(wsId));\n\t\t\tthis._batchingTimers.delete(wsId);\n\t\t}\n\t}\n\n\tprivate sendMessagesInChunks(ws: WebSocket, messages: ServerResponseModel[]): void {\n\t\tlet chunk: ServerResponseModel[] = [];\n\t\tlet chunkSize = 0;\n\n\t\tfor (const message of messages) {\n\t\t\tconst messageData = JSON.stringify([message]); // Wrap message in array for consistency\n\t\t\tconst messageSize = Buffer.byteLength(messageData, 'utf8');\n\n\t\t\tif (chunkSize + messageSize > this._maxMessageSize) {\n\t\t\t\t// Send current chunk\n\t\t\t\tws.send(JSON.stringify(chunk), (error) => {\n\t\t\t\t\tif (error) {\n\t\t\t\t\t\tthis.handleSendError(ws, error);\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t\t// Start new chunk\n\t\t\t\tchunk = [message];\n\t\t\t\tchunkSize = messageSize;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tchunk.push(message);\n\t\t\t\tchunkSize += messageSize;\n\t\t\t}\n\t\t}\n\n\t\t// Send any remaining messages\n\t\tif (chunk.length > 0) {\n\t\t\tws.send(JSON.stringify(chunk), (error) => {\n\t\t\t\tif (error) {\n\t\t\t\t\tthis.handleSendError(ws, error);\n\t\t\t\t}\n\t\t\t});\n\t\t}\n\t}\n\n\tprivate sendMessagesInChunksBinary(ws: WebSocket, messages: ServerResponseModel[]): void {\n\t\tlet chunk: ServerResponseModel[] = [];\n\t\tlet chunkSize = 0;\n\n\t\tfor (const message of messages) {\n\t\t\tconst packedMessage = pack([message]);\n\t\t\tconst messageSize = packedMessage.byteLength;\n\n\t\t\tif (chunkSize + messageSize > this._maxMessageSize) {\n\t\t\t\tconst packedChunk = pack(chunk);\n\t\t\t\tws.send(packedChunk, (error) => {\n\t\t\t\t\tif (error) {\n\t\t\t\t\t\tthis.handleSendError(ws, error);\n\t\t\t\t\t}\n\t\t\t\t});\n\n\t\t\t\tchunk = [message];\n\t\t\t\tchunkSize = messageSize;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tchunk.push(message);\n\t\t\t\tchunkSize += messageSize;\n\t\t\t}\n\t\t}\n\n\t\tif (chunk.length > 0) {\n\t\t\tconst packedChunk = pack(chunk);\n\t\t\tws.send(packedChunk, (error) => {\n\t\t\t\tif (error) {\n\t\t\t\t\tthis.handleSendError(ws, error);\n\t\t\t\t}\n\t\t\t});\n\t\t}\n\t}\n\n\tprivate handleSendError(ws: WebSocket, error: Error): void {\n\t\tif (this._mainServer.getSubscriptionManager()) {\n\t\t\t// Handle send error, e.g., unsubscribe, close connection\n\t\t\tif (this._mainServer.getSubscriptionManager().getEnableDebug()) {\n\t\t\t\tconsole.log(new Date(), 'WebSocketManager', 'Unsub WS', ws['user'], ws['id_socket'], error);\n\t\t\t}\n\t\t\tthis._mainServer.getSubscriptionManager().unsubscribeAll(ws);\n\t\t\tthis.removeWebSocket(ws);\n\t\t}\n\t}\n\n\tpublic closeConnection(ws: WebSocket): void {\n\t\tws.close();\n\t\tconst wsId = ws['id_socket'];\n\t\t// Clean up buffers and timers\n\t\tthis._messageBuffers.delete(wsId);\n\t\tif (this._batchingTimers.has(wsId)) {\n\t\t\tclearTimeout(this._batchingTimers.get(wsId));\n\t\t\tthis._batchingTimers.delete(wsId);\n\t\t}\n \n process.nextTick(() => {\n if ([ws.OPEN, ws.CLOSING].includes(<any>ws.readyState)) {\n // Socket still hangs, hard close\n ws.terminate();\n }\n });\n\t}\n\n\tprivate prepareChunkContext(message: ServerResponseModel, supportsBinary: boolean): { buffer: Buffer, encoding: 'msgpack' | 'json' } | null {\n\t\tif (!message || message.hasError) {\n\t\t\treturn null;\n\t\t}\n\n\t\tconst data = message.data;\n\t\tif (data === undefined || data === null) {\n\t\t\treturn null;\n\t\t}\n\n\t\ttry {\n\t\t\tif (supportsBinary) {\n\t\t\t\tconst packed = Buffer.from(<any>pack(data));\n\t\t\t\tif (packed.byteLength > this._chunkThresholdBytes) {\n\t\t\t\t\treturn { buffer: packed, encoding: 'msgpack' };\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcatch (err) {\n\t\t\t// Fall back to JSON chunking if msgpack encoding fails\n\t\t\tif (this._mainServer.getSubscriptionManager()?.getEnableDebug()) {\n\t\t\t\tconsole.log(new Date(), 'WebSocketManager', 'Chunking pack failed, falling back to JSON', err?.message || err);\n\t\t\t}\n\t\t}\n\n\t\ttry {\n\t\t\tconst jsonString = JSON.stringify(data);\n\t\t\tconst jsonBytes = Buffer.byteLength(jsonString, 'utf8');\n\n\t\t\tif (jsonBytes > this._chunkThresholdBytes) {\n\t\t\t\treturn { buffer: Buffer.from(jsonString, 'utf8'), encoding: 'json' };\n\t\t\t}\n\t\t}\n\t\tcatch {\n\t\t\t// Ignore JSON stringify errors; fallback to normal send\n\t\t}\n\n\t\treturn null;\n\t}\n\n\tprivate sendChunkSequence(ws: WebSocket, original: ServerResponseModel, chunkContext: { buffer: Buffer, encoding: 'msgpack' | 'json' }, supportsBinary: boolean): void {\n\t\tconst chunkId = this.generateChunkId();\n\t\tconst totalChunks = Math.ceil(chunkContext.buffer.byteLength / this._chunkSizeBytes);\n\t\tconst useBinary = supportsBinary && chunkContext.encoding === 'msgpack';\n\n\t\tconst startMessage: ServerResponseModel = {\n\t\t\tmessageId: original.messageId,\n\t\t\thasError: original.hasError,\n\t\t\tdata: {\n\t\t\t\t[this._chunkMetaFlag]: true,\n\t\t\t\tchunkId: chunkId,\n\t\t\t\tchunkStatus: 'start',\n\t\t\t\ttotalChunks: totalChunks,\n\t\t\t\ttotalBytes: chunkContext.buffer.byteLength,\n\t\t\t\tchunkSize: this._chunkSizeBytes,\n\t\t\t\tencoding: chunkContext.encoding\n\t\t\t}\n\t\t};\n\n\t\tthis.transmitBatch(ws, [startMessage], useBinary);\n\n\t\tfor (let index = 0; index < totalChunks; index++) {\n\t\t\tconst start = index * this._chunkSizeBytes;\n\t\t\tconst end = Math.min(chunkContext.buffer.byteLength, start + this._chunkSizeBytes);\n\t\t\tconst slice = chunkContext.buffer.subarray(start, end);\n\t\t\tconst chunkPayload = chunkContext.encoding === 'msgpack' ? slice : slice.toString('base64');\n\n\t\t\tconst chunkMessage: ServerResponseModel = {\n\t\t\t\tmessageId: original.messageId,\n\t\t\t\thasError: original.hasError,\n\t\t\t\tdata: {\n\t\t\t\t\t[this._chunkMetaFlag]: true,\n\t\t\t\t\tchunkId: chunkId,\n\t\t\t\t\tchunkStatus: 'chunk',\n\t\t\t\t\tchunkIndex: index,\n\t\t\t\t\tpayload: chunkPayload,\n\t\t\t\t\tencoding: chunkContext.encoding\n\t\t\t\t}\n\t\t\t};\n\n\t\t\tthis.transmitBatch(ws, [chunkMessage], useBinary);\n\t\t}\n\n\t\tconst endMessage: ServerResponseModel = {\n\t\t\tmessageId: original.messageId,\n\t\t\thasError: original.hasError,\n\t\t\tdata: {\n\t\t\t\t[this._chunkMetaFlag]: true,\n\t\t\t\tchunkId: chunkId,\n\t\t\t\tchunkStatus: 'end'\n\t\t\t}\n\t\t};\n\n\t\tthis.transmitBatch(ws, [endMessage], useBinary);\n\t}\n\n\tprivate transmitBatch(ws: WebSocket, batch: ServerResponseModel[], useBinary: boolean): void {\n\t\tif (!batch || !batch.length) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst isChunkEnvelope = batch.length === 1 && batch[0]?.data && typeof batch[0].data === 'object' && batch[0].data[this._chunkMetaFlag];\n\n\t\tif (useBinary) {\n\t\t\tconst packedData = pack(batch);\n\t\t\tconst dataSize = packedData.byteLength;\n\n\t\t\tif (!isChunkEnvelope && dataSize > this._maxMessageSize) {\n\t\t\t\tthis.sendMessagesInChunksBinary(ws, batch);\n\t\t\t}\n\t\t\telse {\n\t\t\t\tws.send(packedData, (error) => {\n\t\t\t\t\tif (error) {\n\t\t\t\t\t\tthis.handleSendError(ws, error);\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t\telse {\n\t\t\tconst combinedData = JSON.stringify(batch);\n\t\t\tconst dataSize = Buffer.byteLength(combinedData, 'utf8');\n\n\t\t\tif (!isChunkEnvelope && dataSize > this._maxMessageSize) {\n\t\t\t\tthis.sendMessagesInChunks(ws, batch);\n\t\t\t}\n\t\t\telse {\n\t\t\t\tws.send(combinedData, (error) => {\n\t\t\t\t\tif (error) {\n\t\t\t\t\t\tthis.handleSendError(ws, error);\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate generateChunkId(): string {\n\t\treturn randomBytes(8).toString('hex');\n\t}\n}\n"]}
package/methods/logs.js CHANGED
@@ -42,14 +42,31 @@ var log_subscription_collection_1 = require("../collections/log-subscription.col
42
42
  var log_collection_1 = require("../collections/log.collection");
43
43
  var resolveio_server_app_1 = require("../resolveio-server-app");
44
44
  var common_1 = require("../util/common");
45
+ var error_tracking_1 = require("../util/error-tracking");
45
46
  function loadLogMethods(methodManager) {
46
47
  methodManager.methods({
47
48
  insertErrorLog: {
48
49
  function: function (message, data) {
49
50
  return __awaiter(this, void 0, void 0, function () {
51
+ var correlationId, payload;
50
52
  return __generator(this, function (_a) {
51
53
  switch (_a.label) {
52
- case 0: return [4 /*yield*/, this.sendEmail('dev@resolveio.com', 'Error Detected - ' + resolveio_server_app_1.ResolveIOServer.getClientName(), this.user + '\n' + message + '\n' + JSON.stringify(data, null, 2))];
54
+ case 0:
55
+ correlationId = (0, error_tracking_1.getCorrelationId)();
56
+ payload = {
57
+ user: this.user,
58
+ id_user: this.id_user,
59
+ message: message,
60
+ data: data,
61
+ correlationId: correlationId
62
+ };
63
+ return [4 /*yield*/, this.sendEmail('dev@resolveio.com', 'Error Detected - ' + resolveio_server_app_1.ResolveIOServer.getClientName(), JSON.stringify(payload, null, 2), null, null, null, null, false, false, {
64
+ correlationId: correlationId,
65
+ meta: {
66
+ context: 'insertErrorLog',
67
+ message: message
68
+ }
69
+ })];
53
70
  case 1:
54
71
  _a.sent();
55
72
  if (!(resolveio_server_app_1.ResolveIOServer.getServerConfig()['ROOT_URL'] !== 'https://resolveio.com'
@@ -62,12 +79,13 @@ function loadLogMethods(methodManager) {
62
79
  type: 'error',
63
80
  collection: '',
64
81
  id_document: '',
65
- payload: JSON.stringify([data], null, 2),
82
+ payload: JSON.stringify([payload], null, 2),
66
83
  method: '',
67
84
  id_user: this.id_user || '',
68
85
  user: this.user || '',
69
86
  messageId: 0,
70
87
  route: '',
88
+ correlationId: correlationId
71
89
  }
72
90
  });
73
91
  return [3 /*break*/, 4];
@@ -76,7 +94,7 @@ function loadLogMethods(methodManager) {
76
94
  type: 'error',
77
95
  collection: '',
78
96
  id_document: '',
79
- payload: JSON.stringify([data], null, 2),
97
+ payload: JSON.stringify([payload], null, 2),
80
98
  method: '',
81
99
  id_user: this.id_user || '',
82
100
  user: this.user || '',
@@ -84,7 +102,8 @@ function loadLogMethods(methodManager) {
84
102
  route: '',
85
103
  client: 'ResolveIO',
86
104
  instance: 'backend.resolveio.com',
87
- instance_index: process.env.NODE_APP_INSTANCE || ''
105
+ instance_index: process.env.NODE_APP_INSTANCE || '',
106
+ correlationId: correlationId
88
107
  })];
89
108
  case 3:
90
109
  _a.sent();
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/methods/logs.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,8FAAkF;AAClF,0FAA8E;AAC9E,gEAAqD;AAErD,gEAA0D;AAC1D,yCAAmD;AAEnD,SAAgB,cAAc,CAAC,aAA4B;IAC1D,aAAa,CAAC,OAAO,CAAC;QACrB,cAAc,EAAE;YACf,QAAQ,EAAE,UAAe,OAAO,EAAE,IAAI;;;;oCACrC,qBAAM,IAAI,CAAC,SAAS,CAAC,mBAAmB,EAAE,mBAAmB,GAAG,sCAAe,CAAC,aAAa,EAAE,EAAE,IAAI,CAAC,IAAI,GAAG,IAAI,GAAG,OAAO,GAAG,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,EAAA;;gCAAnK,SAAmK,CAAC;qCAGnK,CAAA,sCAAe,CAAC,eAAe,EAAE,CAAC,UAAU,CAA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ACN,IAAI,EAAE,CAAC;iDACP;6CACD;yCACD;wCACD;4CACC,KAAK,EAAE,SAAS;yCAChB;qCACD,EAAE,EAAC,YAAY,EAAE,IAAI,EAAE,cAAc,EAAE,IAAI,CAAC,YAAY,CAAC,UAAU,CAAC,KAAK,uBAAuB,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,SAAS,EAAC,EAAE,IAAI,CAAC,EAAA;;gCAxB/H,eAAe,GAAG,SAwB6G;gCAEhH,qBAAM,8CAAgB,CAAC,SAAS,CAAC;wCACnD;4CACC,MAAM,EAAE;gDACP,IAAI,EAAE;oDACL;wDACC,SAAS,EAAE,EAAC,IAAI,EAAE,UAAU,EAAC;qDAC7B;oDACD;wDACC,SAAS,EAAE,EAAC,IAAI,EAAE,QAAQ,EAAC;qDAC3B;iDACD;6CACD;yCACD;wCACD;4CACC,MAAM,EAAE;gDACP,GAAG,EAAE,OAAO;gDACZ,KAAK,EAAE;oDACN,IAAI,EAAE,CAAC;iDACP;6CACD;yCACD;wCACD;4CACC,KAAK,EAAE,SAAS;yCAChB;qCACD,EAAE,EAAC,YAAY,EAAE,IAAI,EAAE,cAAc,EAAE,IAAI,CAAC,YAAY,CAAC,UAAU,CAAC,KAAK,uBAAuB,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,SAAS,EAAC,EAAE,IAAI,CAAC,EAAA;;gCAxB/H,YAAY,GAAG,SAwBgH;gCAEnI,sBAAO;wCACN,OAAO,EAAE,SAAS;wCAClB,aAAa,EAAE,aAAa;wCAC5B,eAAe,EAAE,eAAe;wCAChC,YAAY,EAAE,YAAY;qCAC1B,EAAC;;;;aACF;YACD,cAAc,EAAE,IAAI;SACpB;KACD,CAAC,CAAC;AACJ,CAAC;AAnWD,wCAmWC","file":"logs.js","sourcesContent":["import { LogMethodLatencies } from '../collections/log-method-latency.collection';\nimport { LogSubscriptions } from '../collections/log-subscription.collection';\nimport { Logs } from '../collections/log.collection';\nimport { MethodManager } from '../managers/method.manager';\nimport { ResolveIOServer } from '../resolveio-server-app';\nimport { objectIdHexString } from '../util/common';\n\nexport function loadLogMethods(methodManager: MethodManager) {\n\tmethodManager.methods({\n\t\tinsertErrorLog: {\n\t\t\tfunction: async function(message, data) {\n\t\t\t\tawait this.sendEmail('dev@resolveio.com', 'Error Detected - ' + ResolveIOServer.getClientName(), this.user + '\\n' + message + '\\n' + JSON.stringify(data, null, 2));\n\n\t\t\t\tif (\n\t\t\t\t\tResolveIOServer.getServerConfig()['ROOT_URL'] !== 'https://resolveio.com'\n\t\t\t\t&& ResolveIOServer.getServerConfig()['ROOT_URL'] !== 'http://localhost:4200'\n\t\t\t\t) {\n\t\t\t\t\tResolveIOServer.getLocalLogManager().writeLog({\n\t\t\t\t\t\ttype: 'log',\n\t\t\t\t\t\tdata: {\n\t\t\t\t\t\t\t_id: objectIdHexString(),\n\t\t\t\t\t\t\tcreatedAt: new Date(),\n\t\t\t\t\t\t\ttype: 'error',\n\t\t\t\t\t\t\tcollection: '',\n\t\t\t\t\t\t\tid_document: '',\n\t\t\t\t\t\t\tpayload: JSON.stringify([data], null, 2),\n\t\t\t\t\t\t\tmethod: '',\n\t\t\t\t\t\t\tid_user: this.id_user || '',\n\t\t\t\t\t\t\tuser: this.user || '',\n\t\t\t\t\t\t\tmessageId: 0,\n\t\t\t\t\t\t\troute: '',\n\t\t\t\t\t\t}\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tawait Logs.insertOne({\n\t\t\t\t\t\t_id: objectIdHexString(),\n\t\t\t\t\t\ttype: 'error',\n\t\t\t\t\t\tcollection: '',\n\t\t\t\t\t\tid_document: '',\n\t\t\t\t\t\tpayload: JSON.stringify([data], null, 2),\n\t\t\t\t\t\tmethod: '',\n\t\t\t\t\t\tid_user: this.id_user || '',\n\t\t\t\t\t\tuser: this.user || '',\n\t\t\t\t\t\tmessageId: 0,\n\t\t\t\t\t\troute: '',\n\t\t\t\t\t\tclient: 'ResolveIO',\n\t\t\t\t\t\tinstance: 'backend.resolveio.com',\n\t\t\t\t\t\tinstance_index: process.env.NODE_APP_INSTANCE || ''\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\treturn true;\n\t\t\t},\n\t\t\tskipValidation: true\n\t\t},\n\t\tinsertSubscriptionLog: {\n\t\t\tfunction: async function(type, subscription, collection_name, subData) {\n\t\t\t\tif (\n\t\t\t\t\tResolveIOServer.getServerConfig()['ROOT_URL'] !== 'https://resolveio.com'\n\t\t\t\t&& ResolveIOServer.getServerConfig()['ROOT_URL'] !== 'http://localhost:4200'\n\t\t\t\t) {\n\t\t\t\t\tResolveIOServer.getLocalLogManager().writeLog({\n\t\t\t\t\t\ttype: 'log-subscription',\n\t\t\t\t\t\tdata: {\n\t\t\t\t\t\t\t_id: objectIdHexString(),\n\t\t\t\t\t\t\t__v: 0,\n\t\t\t\t\t\t\tdate: new Date(),\n\t\t\t\t\t\t\ttype: type,\n\t\t\t\t\t\t\tsubscription: subscription,\n\t\t\t\t\t\t\tcollection_name: collection_name,\n\t\t\t\t\t\t\tsubData: subData\n\t\t\t\t\t\t}\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tawait LogSubscriptions.create({\n\t\t\t\t\t\t_id: objectIdHexString(),\n\t\t\t\t\t\t__v: 0,\n\t\t\t\t\t\tdate: new Date(),\n\t\t\t\t\t\ttype: type,\n\t\t\t\t\t\tsubscription: subscription,\n\t\t\t\t\t\tcollection_name: collection_name,\n\t\t\t\t\t\tsubData: subData,\n\t\t\t\t\t\tclient: 'ResolveIO',\n\t\t\t\t\t\tinstance: 'backend.resolveio.com'\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\treturn true;\n\t\t\t},\n\t\t\tskipValidation: true\n\t\t},\n\t\tsuperadminAPM: {\n\t\t\tfunction: async function(date_start: Date, date_end: Date, graphInterval) {\n\t\t\t\tlet latencies = await LogMethodLatencies.aggregate([\n\t\t\t\t\t{\n\t\t\t\t\t\t$match: {\n\t\t\t\t\t\t\t$and: [\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tcreatedAt: {$gte: date_start}\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tcreatedAt: {$lte: date_end}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t]\n\t\t\t\t\t\t}\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\t$group: {\n\t\t\t\t\t\t\t_id: '$method',\n\t\t\t\t\t\t\tmethod: {$first: '$method'},\n\t\t\t\t\t\t\tcount: {$sum: 1},\n\t\t\t\t\t\t\tlatency_min: {$min: '$latency_ms'},\n\t\t\t\t\t\t\tlatency_avg: {$avg: '$latency_ms'},\n\t\t\t\t\t\t\tlatency_max: {$max: '$latency_ms'}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t], {allowDiskUse: true, readPreference: this.serverConfig['ROOT_URL'] !== 'http://localhost:4200' ? 'secondary' : 'primary'}, true);\n\n\t\t\t\tlet subscriptions = await LogSubscriptions.aggregate([\n\t\t\t\t\t{\n\t\t\t\t\t\t$match: {\n\t\t\t\t\t\t\t$and: [\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tcreatedAt: {$gte: date_start}\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tcreatedAt: {$lte: date_end}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t]\n\t\t\t\t\t\t}\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\t$group: {\n\t\t\t\t\t\t\t_id: '$subscription',\n\t\t\t\t\t\t\tsubscription: {$first: '$subscription'},\n\t\t\t\t\t\t\tcount: {$sum: 1}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t], {allowDiskUse: true, readPreference: this.serverConfig['ROOT_URL'] !== 'http://localhost:4200' ? 'secondary' : 'primary'}, true);\n\n\t\t\t\tlet groupId = {};\n\t\t\t\tlet groupSort = {};\n\n\t\t\t\tif (graphInterval === 'seconds') {\n\t\t\t\t\tgroupId = {\n\t\t\t\t\t\tsecond: {\n\t\t\t\t\t\t\t$second: {\n\t\t\t\t\t\t\t\tdate: '$createdAt',\n\t\t\t\t\t\t\t\ttimezone: process.env.TZ_CLIENT || 'America/Chicago'\n\t\t\t\t\t\t\t} \n\t\t\t\t\t\t},\n\t\t\t\t\t\tminute: {\n\t\t\t\t\t\t\t$minute: {\n\t\t\t\t\t\t\t\tdate: '$createdAt',\n\t\t\t\t\t\t\t\ttimezone: process.env.TZ_CLIENT || 'America/Chicago'\n\t\t\t\t\t\t\t} \n\t\t\t\t\t\t},\n\t\t\t\t\t\thour: {\n\t\t\t\t\t\t\t$hour: {\n\t\t\t\t\t\t\t\tdate: '$createdAt',\n\t\t\t\t\t\t\t\ttimezone: process.env.TZ_CLIENT || 'America/Chicago'\n\t\t\t\t\t\t\t} \n\t\t\t\t\t\t},\n\t\t\t\t\t\tday: {\n\t\t\t\t\t\t\t$dayOfMonth: {\n\t\t\t\t\t\t\t\tdate: '$createdAt',\n\t\t\t\t\t\t\t\ttimezone: process.env.TZ_CLIENT || 'America/Chicago'\n\t\t\t\t\t\t\t} \n\t\t\t\t\t\t},\n\t\t\t\t\t\tmonth: {\n\t\t\t\t\t\t\t$month: {\n\t\t\t\t\t\t\t\tdate: '$createdAt',\n\t\t\t\t\t\t\t\ttimezone: process.env.TZ_CLIENT || 'America/Chicago'\n\t\t\t\t\t\t\t} \n\t\t\t\t\t\t},\n\t\t\t\t\t\tyear: {\n\t\t\t\t\t\t\t$year: {\n\t\t\t\t\t\t\t\tdate: '$createdAt',\n\t\t\t\t\t\t\t\ttimezone: process.env.TZ_CLIENT || 'America/Chicago'\n\t\t\t\t\t\t\t} \n\t\t\t\t\t\t}\n\t\t\t\t\t};\n\n\t\t\t\t\tgroupSort = {\n\t\t\t\t\t\t'_id.year': 1,\n\t\t\t\t\t\t'_id.month': 1,\n\t\t\t\t\t\t'_id.day': 1,\n\t\t\t\t\t\t'_id.hour': 1,\n\t\t\t\t\t\t'_id.minute': 1,\n\t\t\t\t\t\t'_id.second': 1\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t\telse if (graphInterval === 'minutes') {\n\t\t\t\t\tgroupId = {\n\t\t\t\t\t\tminute: {\n\t\t\t\t\t\t\t$minute: {\n\t\t\t\t\t\t\t\tdate: '$createdAt',\n\t\t\t\t\t\t\t\ttimezone: process.env.TZ_CLIENT || 'America/Chicago'\n\t\t\t\t\t\t\t} \n\t\t\t\t\t\t},\n\t\t\t\t\t\thour: {\n\t\t\t\t\t\t\t$hour: {\n\t\t\t\t\t\t\t\tdate: '$createdAt',\n\t\t\t\t\t\t\t\ttimezone: process.env.TZ_CLIENT || 'America/Chicago'\n\t\t\t\t\t\t\t} \n\t\t\t\t\t\t},\n\t\t\t\t\t\tday: {\n\t\t\t\t\t\t\t$dayOfMonth: {\n\t\t\t\t\t\t\t\tdate: '$createdAt',\n\t\t\t\t\t\t\t\ttimezone: process.env.TZ_CLIENT || 'America/Chicago'\n\t\t\t\t\t\t\t} \n\t\t\t\t\t\t},\n\t\t\t\t\t\tmonth: {\n\t\t\t\t\t\t\t$month: {\n\t\t\t\t\t\t\t\tdate: '$createdAt',\n\t\t\t\t\t\t\t\ttimezone: process.env.TZ_CLIENT || 'America/Chicago'\n\t\t\t\t\t\t\t} \n\t\t\t\t\t\t},\n\t\t\t\t\t\tyear: {\n\t\t\t\t\t\t\t$year: {\n\t\t\t\t\t\t\t\tdate: '$createdAt',\n\t\t\t\t\t\t\t\ttimezone: process.env.TZ_CLIENT || 'America/Chicago'\n\t\t\t\t\t\t\t} \n\t\t\t\t\t\t}\n\t\t\t\t\t};\n\n\t\t\t\t\tgroupSort = {\n\t\t\t\t\t\t'_id.year': 1,\n\t\t\t\t\t\t'_id.month': 1,\n\t\t\t\t\t\t'_id.day': 1,\n\t\t\t\t\t\t'_id.hour': 1,\n\t\t\t\t\t\t'_id.minute': 1\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t\telse if (graphInterval === 'hours') {\n\t\t\t\t\tgroupId = {\n\t\t\t\t\t\thour: {\n\t\t\t\t\t\t\t$hour: {\n\t\t\t\t\t\t\t\tdate: '$createdAt',\n\t\t\t\t\t\t\t\ttimezone: process.env.TZ_CLIENT || 'America/Chicago'\n\t\t\t\t\t\t\t} \n\t\t\t\t\t\t},\n\t\t\t\t\t\tday: {\n\t\t\t\t\t\t\t$dayOfMonth: {\n\t\t\t\t\t\t\t\tdate: '$createdAt',\n\t\t\t\t\t\t\t\ttimezone: process.env.TZ_CLIENT || 'America/Chicago'\n\t\t\t\t\t\t\t} \n\t\t\t\t\t\t},\n\t\t\t\t\t\tmonth: {\n\t\t\t\t\t\t\t$month: {\n\t\t\t\t\t\t\t\tdate: '$createdAt',\n\t\t\t\t\t\t\t\ttimezone: process.env.TZ_CLIENT || 'America/Chicago'\n\t\t\t\t\t\t\t} \n\t\t\t\t\t\t},\n\t\t\t\t\t\tyear: {\n\t\t\t\t\t\t\t$year: {\n\t\t\t\t\t\t\t\tdate: '$createdAt',\n\t\t\t\t\t\t\t\ttimezone: process.env.TZ_CLIENT || 'America/Chicago'\n\t\t\t\t\t\t\t} \n\t\t\t\t\t\t}\n\t\t\t\t\t};\n\n\t\t\t\t\tgroupSort = {\n\t\t\t\t\t\t'_id.year': 1,\n\t\t\t\t\t\t'_id.month': 1,\n\t\t\t\t\t\t'_id.day': 1,\n\t\t\t\t\t\t'_id.hour': 1\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tgroupId = {\n\t\t\t\t\t\tday: {\n\t\t\t\t\t\t\t$dayOfMonth: {\n\t\t\t\t\t\t\t\tdate: '$createdAt',\n\t\t\t\t\t\t\t\ttimezone: process.env.TZ_CLIENT || 'America/Chicago'\n\t\t\t\t\t\t\t} \n\t\t\t\t\t\t},\n\t\t\t\t\t\tmonth: {\n\t\t\t\t\t\t\t$month: {\n\t\t\t\t\t\t\t\tdate: '$createdAt',\n\t\t\t\t\t\t\t\ttimezone: process.env.TZ_CLIENT || 'America/Chicago'\n\t\t\t\t\t\t\t} \n\t\t\t\t\t\t},\n\t\t\t\t\t\tyear: {\n\t\t\t\t\t\t\t$year: {\n\t\t\t\t\t\t\t\tdate: '$createdAt',\n\t\t\t\t\t\t\t\ttimezone: process.env.TZ_CLIENT || 'America/Chicago'\n\t\t\t\t\t\t\t} \n\t\t\t\t\t\t}\n\t\t\t\t\t};\n\n\t\t\t\t\tgroupSort = {\n\t\t\t\t\t\t'_id.year': 1,\n\t\t\t\t\t\t'_id.month': 1,\n\t\t\t\t\t\t'_id.day': 1\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\tlet methodGraphData = await LogMethodLatencies.aggregate([\n\t\t\t\t\t{\n\t\t\t\t\t\t$match: {\n\t\t\t\t\t\t\t$and: [\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tcreatedAt: {$gte: date_start}\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tcreatedAt: {$lte: date_end}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t]\n\t\t\t\t\t\t}\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\t$group: {\n\t\t\t\t\t\t\t_id: groupId,\n\t\t\t\t\t\t\tcount: {\n\t\t\t\t\t\t\t\t$sum: 1\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\t$sort: groupSort\n\t\t\t\t\t}\n\t\t\t\t], {allowDiskUse: true, readPreference: this.serverConfig['ROOT_URL'] !== 'http://localhost:4200' ? 'secondary' : 'primary'}, true);\n\n\t\t\t\tlet subGraphData = await LogSubscriptions.aggregate([\n\t\t\t\t\t{\n\t\t\t\t\t\t$match: {\n\t\t\t\t\t\t\t$and: [\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tcreatedAt: {$gte: date_start}\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tcreatedAt: {$lte: date_end}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t]\n\t\t\t\t\t\t}\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\t$group: {\n\t\t\t\t\t\t\t_id: groupId,\n\t\t\t\t\t\t\tcount: {\n\t\t\t\t\t\t\t\t$sum: 1\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\t$sort: groupSort\n\t\t\t\t\t}\n\t\t\t\t], {allowDiskUse: true, readPreference: this.serverConfig['ROOT_URL'] !== 'http://localhost:4200' ? 'secondary' : 'primary'}, true);\n\n\t\t\t\treturn {\n\t\t\t\t\tmethods: latencies,\n\t\t\t\t\tsubscriptions: subscriptions,\n\t\t\t\t\tmethodGraphData: methodGraphData,\n\t\t\t\t\tsubGraphData: subGraphData\n\t\t\t\t};\n\t\t\t},\n\t\t\tskipValidation: true\n\t\t}\n\t});\n}"]}
1
+ {"version":3,"sources":["../../src/methods/logs.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,8FAAkF;AAClF,0FAA8E;AAC9E,gEAAqD;AAErD,gEAA0D;AAC1D,yCAAmD;AACnD,yDAA0D;AAE1D,SAAgB,cAAc,CAAC,aAA4B;IAC1D,aAAa,CAAC,OAAO,CAAC;QACrB,cAAc,EAAE;YACf,QAAQ,EAAE,UAAe,OAAO,EAAE,IAAI;;;;;;gCAC/B,aAAa,GAAG,IAAA,iCAAgB,GAAE,CAAC;gCACnC,OAAO,GAAG;oCACf,IAAI,EAAE,IAAI,CAAC,IAAI;oCACf,OAAO,EAAE,IAAI,CAAC,OAAO;oCACrB,OAA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E,IAAI,CAAC,YAAY,CAAC,UAAU,CAAC,KAAK,uBAAuB,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,SAAS,EAAC,EAAE,IAAI,CAAC,EAAA;;gCAxB/H,YAAY,GAAG,SAwBgH;gCAEnI,sBAAO;wCACN,OAAO,EAAE,SAAS;wCAClB,aAAa,EAAE,aAAa;wCAC5B,eAAe,EAAE,eAAe;wCAChC,YAAY,EAAE,YAAY;qCAC1B,EAAC;;;;aACF;YACD,cAAc,EAAE,IAAI;SACpB;KACD,CAAC,CAAC;AACJ,CAAC;AA/XD,wCA+XC","file":"logs.js","sourcesContent":["import { LogMethodLatencies } from '../collections/log-method-latency.collection';\nimport { LogSubscriptions } from '../collections/log-subscription.collection';\nimport { Logs } from '../collections/log.collection';\nimport { MethodManager } from '../managers/method.manager';\nimport { ResolveIOServer } from '../resolveio-server-app';\nimport { objectIdHexString } from '../util/common';\nimport { getCorrelationId } from '../util/error-tracking';\n\nexport function loadLogMethods(methodManager: MethodManager) {\n\tmethodManager.methods({\n\t\tinsertErrorLog: {\n\t\t\tfunction: async function(message, data) {\n\t\t\t\tconst correlationId = getCorrelationId();\n\t\t\t\tconst payload = {\n\t\t\t\t\tuser: this.user,\n\t\t\t\t\tid_user: this.id_user,\n\t\t\t\t\tmessage,\n\t\t\t\t\tdata,\n\t\t\t\t\tcorrelationId\n\t\t\t\t};\n\n\t\t\t\tawait this.sendEmail(\n\t\t\t\t\t'dev@resolveio.com',\n\t\t\t\t\t'Error Detected - ' + ResolveIOServer.getClientName(),\n\t\t\t\t\tJSON.stringify(payload, null, 2),\n\t\t\t\t\tnull,\n\t\t\t\t\tnull,\n\t\t\t\t\tnull,\n\t\t\t\t\tnull,\n\t\t\t\t\tfalse,\n\t\t\t\t\tfalse,\n\t\t\t\t\t{\n\t\t\t\t\t\tcorrelationId,\n\t\t\t\t\t\tmeta: {\n\t\t\t\t\t\t\tcontext: 'insertErrorLog',\n\t\t\t\t\t\t\tmessage\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t);\n\n\t\t\t\tif (\n\t\t\t\t\tResolveIOServer.getServerConfig()['ROOT_URL'] !== 'https://resolveio.com'\n\t\t\t\t&& ResolveIOServer.getServerConfig()['ROOT_URL'] !== 'http://localhost:4200'\n\t\t\t\t) {\n\t\t\t\t\tResolveIOServer.getLocalLogManager().writeLog({\n\t\t\t\t\t\ttype: 'log',\n\t\t\t\t\t\tdata: {\n\t\t\t\t\t\t\t_id: objectIdHexString(),\n\t\t\t\t\t\t\tcreatedAt: new Date(),\n\t\t\t\t\t\t\ttype: 'error',\n\t\t\t\t\t\t\tcollection: '',\n\t\t\t\t\t\t\tid_document: '',\n\t\t\t\t\t\t\tpayload: JSON.stringify([payload], null, 2),\n\t\t\t\t\t\t\tmethod: '',\n\t\t\t\t\t\t\tid_user: this.id_user || '',\n\t\t\t\t\t\t\tuser: this.user || '',\n\t\t\t\t\t\t\tmessageId: 0,\n\t\t\t\t\t\t\troute: '',\n\t\t\t\t\t\t\tcorrelationId\n\t\t\t\t\t\t}\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tawait Logs.insertOne({\n\t\t\t\t\t\t_id: objectIdHexString(),\n\t\t\t\t\t\ttype: 'error',\n\t\t\t\t\t\tcollection: '',\n\t\t\t\t\t\tid_document: '',\n\t\t\t\t\t\tpayload: JSON.stringify([payload], null, 2),\n\t\t\t\t\t\tmethod: '',\n\t\t\t\t\t\tid_user: this.id_user || '',\n\t\t\t\t\t\tuser: this.user || '',\n\t\t\t\t\t\tmessageId: 0,\n\t\t\t\t\t\troute: '',\n\t\t\t\t\t\tclient: 'ResolveIO',\n\t\t\t\t\t\tinstance: 'backend.resolveio.com',\n\t\t\t\t\t\tinstance_index: process.env.NODE_APP_INSTANCE || '',\n\t\t\t\t\t\tcorrelationId\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\treturn true;\n\t\t\t},\n\t\t\tskipValidation: true\n\t\t},\n\t\tinsertSubscriptionLog: {\n\t\t\tfunction: async function(type, subscription, collection_name, subData) {\n\t\t\t\tif (\n\t\t\t\t\tResolveIOServer.getServerConfig()['ROOT_URL'] !== 'https://resolveio.com'\n\t\t\t\t&& ResolveIOServer.getServerConfig()['ROOT_URL'] !== 'http://localhost:4200'\n\t\t\t\t) {\n\t\t\t\t\tResolveIOServer.getLocalLogManager().writeLog({\n\t\t\t\t\t\ttype: 'log-subscription',\n\t\t\t\t\t\tdata: {\n\t\t\t\t\t\t\t_id: objectIdHexString(),\n\t\t\t\t\t\t\t__v: 0,\n\t\t\t\t\t\t\tdate: new Date(),\n\t\t\t\t\t\t\ttype: type,\n\t\t\t\t\t\t\tsubscription: subscription,\n\t\t\t\t\t\t\tcollection_name: collection_name,\n\t\t\t\t\t\t\tsubData: subData\n\t\t\t\t\t\t}\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tawait LogSubscriptions.create({\n\t\t\t\t\t\t_id: objectIdHexString(),\n\t\t\t\t\t\t__v: 0,\n\t\t\t\t\t\tdate: new Date(),\n\t\t\t\t\t\ttype: type,\n\t\t\t\t\t\tsubscription: subscription,\n\t\t\t\t\t\tcollection_name: collection_name,\n\t\t\t\t\t\tsubData: subData,\n\t\t\t\t\t\tclient: 'ResolveIO',\n\t\t\t\t\t\tinstance: 'backend.resolveio.com'\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\treturn true;\n\t\t\t},\n\t\t\tskipValidation: true\n\t\t},\n\t\tsuperadminAPM: {\n\t\t\tfunction: async function(date_start: Date, date_end: Date, graphInterval) {\n\t\t\t\tlet latencies = await LogMethodLatencies.aggregate([\n\t\t\t\t\t{\n\t\t\t\t\t\t$match: {\n\t\t\t\t\t\t\t$and: [\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tcreatedAt: {$gte: date_start}\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tcreatedAt: {$lte: date_end}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t]\n\t\t\t\t\t\t}\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\t$group: {\n\t\t\t\t\t\t\t_id: '$method',\n\t\t\t\t\t\t\tmethod: {$first: '$method'},\n\t\t\t\t\t\t\tcount: {$sum: 1},\n\t\t\t\t\t\t\tlatency_min: {$min: '$latency_ms'},\n\t\t\t\t\t\t\tlatency_avg: {$avg: '$latency_ms'},\n\t\t\t\t\t\t\tlatency_max: {$max: '$latency_ms'}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t], {allowDiskUse: true, readPreference: this.serverConfig['ROOT_URL'] !== 'http://localhost:4200' ? 'secondary' : 'primary'}, true);\n\n\t\t\t\tlet subscriptions = await LogSubscriptions.aggregate([\n\t\t\t\t\t{\n\t\t\t\t\t\t$match: {\n\t\t\t\t\t\t\t$and: [\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tcreatedAt: {$gte: date_start}\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tcreatedAt: {$lte: date_end}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t]\n\t\t\t\t\t\t}\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\t$group: {\n\t\t\t\t\t\t\t_id: '$subscription',\n\t\t\t\t\t\t\tsubscription: {$first: '$subscription'},\n\t\t\t\t\t\t\tcount: {$sum: 1}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t], {allowDiskUse: true, readPreference: this.serverConfig['ROOT_URL'] !== 'http://localhost:4200' ? 'secondary' : 'primary'}, true);\n\n\t\t\t\tlet groupId = {};\n\t\t\t\tlet groupSort = {};\n\n\t\t\t\tif (graphInterval === 'seconds') {\n\t\t\t\t\tgroupId = {\n\t\t\t\t\t\tsecond: {\n\t\t\t\t\t\t\t$second: {\n\t\t\t\t\t\t\t\tdate: '$createdAt',\n\t\t\t\t\t\t\t\ttimezone: process.env.TZ_CLIENT || 'America/Chicago'\n\t\t\t\t\t\t\t} \n\t\t\t\t\t\t},\n\t\t\t\t\t\tminute: {\n\t\t\t\t\t\t\t$minute: {\n\t\t\t\t\t\t\t\tdate: '$createdAt',\n\t\t\t\t\t\t\t\ttimezone: process.env.TZ_CLIENT || 'America/Chicago'\n\t\t\t\t\t\t\t} \n\t\t\t\t\t\t},\n\t\t\t\t\t\thour: {\n\t\t\t\t\t\t\t$hour: {\n\t\t\t\t\t\t\t\tdate: '$createdAt',\n\t\t\t\t\t\t\t\ttimezone: process.env.TZ_CLIENT || 'America/Chicago'\n\t\t\t\t\t\t\t} \n\t\t\t\t\t\t},\n\t\t\t\t\t\tday: {\n\t\t\t\t\t\t\t$dayOfMonth: {\n\t\t\t\t\t\t\t\tdate: '$createdAt',\n\t\t\t\t\t\t\t\ttimezone: process.env.TZ_CLIENT || 'America/Chicago'\n\t\t\t\t\t\t\t} \n\t\t\t\t\t\t},\n\t\t\t\t\t\tmonth: {\n\t\t\t\t\t\t\t$month: {\n\t\t\t\t\t\t\t\tdate: '$createdAt',\n\t\t\t\t\t\t\t\ttimezone: process.env.TZ_CLIENT || 'America/Chicago'\n\t\t\t\t\t\t\t} \n\t\t\t\t\t\t},\n\t\t\t\t\t\tyear: {\n\t\t\t\t\t\t\t$year: {\n\t\t\t\t\t\t\t\tdate: '$createdAt',\n\t\t\t\t\t\t\t\ttimezone: process.env.TZ_CLIENT || 'America/Chicago'\n\t\t\t\t\t\t\t} \n\t\t\t\t\t\t}\n\t\t\t\t\t};\n\n\t\t\t\t\tgroupSort = {\n\t\t\t\t\t\t'_id.year': 1,\n\t\t\t\t\t\t'_id.month': 1,\n\t\t\t\t\t\t'_id.day': 1,\n\t\t\t\t\t\t'_id.hour': 1,\n\t\t\t\t\t\t'_id.minute': 1,\n\t\t\t\t\t\t'_id.second': 1\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t\telse if (graphInterval === 'minutes') {\n\t\t\t\t\tgroupId = {\n\t\t\t\t\t\tminute: {\n\t\t\t\t\t\t\t$minute: {\n\t\t\t\t\t\t\t\tdate: '$createdAt',\n\t\t\t\t\t\t\t\ttimezone: process.env.TZ_CLIENT || 'America/Chicago'\n\t\t\t\t\t\t\t} \n\t\t\t\t\t\t},\n\t\t\t\t\t\thour: {\n\t\t\t\t\t\t\t$hour: {\n\t\t\t\t\t\t\t\tdate: '$createdAt',\n\t\t\t\t\t\t\t\ttimezone: process.env.TZ_CLIENT || 'America/Chicago'\n\t\t\t\t\t\t\t} \n\t\t\t\t\t\t},\n\t\t\t\t\t\tday: {\n\t\t\t\t\t\t\t$dayOfMonth: {\n\t\t\t\t\t\t\t\tdate: '$createdAt',\n\t\t\t\t\t\t\t\ttimezone: process.env.TZ_CLIENT || 'America/Chicago'\n\t\t\t\t\t\t\t} \n\t\t\t\t\t\t},\n\t\t\t\t\t\tmonth: {\n\t\t\t\t\t\t\t$month: {\n\t\t\t\t\t\t\t\tdate: '$createdAt',\n\t\t\t\t\t\t\t\ttimezone: process.env.TZ_CLIENT || 'America/Chicago'\n\t\t\t\t\t\t\t} \n\t\t\t\t\t\t},\n\t\t\t\t\t\tyear: {\n\t\t\t\t\t\t\t$year: {\n\t\t\t\t\t\t\t\tdate: '$createdAt',\n\t\t\t\t\t\t\t\ttimezone: process.env.TZ_CLIENT || 'America/Chicago'\n\t\t\t\t\t\t\t} \n\t\t\t\t\t\t}\n\t\t\t\t\t};\n\n\t\t\t\t\tgroupSort = {\n\t\t\t\t\t\t'_id.year': 1,\n\t\t\t\t\t\t'_id.month': 1,\n\t\t\t\t\t\t'_id.day': 1,\n\t\t\t\t\t\t'_id.hour': 1,\n\t\t\t\t\t\t'_id.minute': 1\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t\telse if (graphInterval === 'hours') {\n\t\t\t\t\tgroupId = {\n\t\t\t\t\t\thour: {\n\t\t\t\t\t\t\t$hour: {\n\t\t\t\t\t\t\t\tdate: '$createdAt',\n\t\t\t\t\t\t\t\ttimezone: process.env.TZ_CLIENT || 'America/Chicago'\n\t\t\t\t\t\t\t} \n\t\t\t\t\t\t},\n\t\t\t\t\t\tday: {\n\t\t\t\t\t\t\t$dayOfMonth: {\n\t\t\t\t\t\t\t\tdate: '$createdAt',\n\t\t\t\t\t\t\t\ttimezone: process.env.TZ_CLIENT || 'America/Chicago'\n\t\t\t\t\t\t\t} \n\t\t\t\t\t\t},\n\t\t\t\t\t\tmonth: {\n\t\t\t\t\t\t\t$month: {\n\t\t\t\t\t\t\t\tdate: '$createdAt',\n\t\t\t\t\t\t\t\ttimezone: process.env.TZ_CLIENT || 'America/Chicago'\n\t\t\t\t\t\t\t} \n\t\t\t\t\t\t},\n\t\t\t\t\t\tyear: {\n\t\t\t\t\t\t\t$year: {\n\t\t\t\t\t\t\t\tdate: '$createdAt',\n\t\t\t\t\t\t\t\ttimezone: process.env.TZ_CLIENT || 'America/Chicago'\n\t\t\t\t\t\t\t} \n\t\t\t\t\t\t}\n\t\t\t\t\t};\n\n\t\t\t\t\tgroupSort = {\n\t\t\t\t\t\t'_id.year': 1,\n\t\t\t\t\t\t'_id.month': 1,\n\t\t\t\t\t\t'_id.day': 1,\n\t\t\t\t\t\t'_id.hour': 1\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tgroupId = {\n\t\t\t\t\t\tday: {\n\t\t\t\t\t\t\t$dayOfMonth: {\n\t\t\t\t\t\t\t\tdate: '$createdAt',\n\t\t\t\t\t\t\t\ttimezone: process.env.TZ_CLIENT || 'America/Chicago'\n\t\t\t\t\t\t\t} \n\t\t\t\t\t\t},\n\t\t\t\t\t\tmonth: {\n\t\t\t\t\t\t\t$month: {\n\t\t\t\t\t\t\t\tdate: '$createdAt',\n\t\t\t\t\t\t\t\ttimezone: process.env.TZ_CLIENT || 'America/Chicago'\n\t\t\t\t\t\t\t} \n\t\t\t\t\t\t},\n\t\t\t\t\t\tyear: {\n\t\t\t\t\t\t\t$year: {\n\t\t\t\t\t\t\t\tdate: '$createdAt',\n\t\t\t\t\t\t\t\ttimezone: process.env.TZ_CLIENT || 'America/Chicago'\n\t\t\t\t\t\t\t} \n\t\t\t\t\t\t}\n\t\t\t\t\t};\n\n\t\t\t\t\tgroupSort = {\n\t\t\t\t\t\t'_id.year': 1,\n\t\t\t\t\t\t'_id.month': 1,\n\t\t\t\t\t\t'_id.day': 1\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\tlet methodGraphData = await LogMethodLatencies.aggregate([\n\t\t\t\t\t{\n\t\t\t\t\t\t$match: {\n\t\t\t\t\t\t\t$and: [\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tcreatedAt: {$gte: date_start}\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tcreatedAt: {$lte: date_end}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t]\n\t\t\t\t\t\t}\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\t$group: {\n\t\t\t\t\t\t\t_id: groupId,\n\t\t\t\t\t\t\tcount: {\n\t\t\t\t\t\t\t\t$sum: 1\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\t$sort: groupSort\n\t\t\t\t\t}\n\t\t\t\t], {allowDiskUse: true, readPreference: this.serverConfig['ROOT_URL'] !== 'http://localhost:4200' ? 'secondary' : 'primary'}, true);\n\n\t\t\t\tlet subGraphData = await LogSubscriptions.aggregate([\n\t\t\t\t\t{\n\t\t\t\t\t\t$match: {\n\t\t\t\t\t\t\t$and: [\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tcreatedAt: {$gte: date_start}\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\tcreatedAt: {$lte: date_end}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t]\n\t\t\t\t\t\t}\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\t$group: {\n\t\t\t\t\t\t\t_id: groupId,\n\t\t\t\t\t\t\tcount: {\n\t\t\t\t\t\t\t\t$sum: 1\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\t$sort: groupSort\n\t\t\t\t\t}\n\t\t\t\t], {allowDiskUse: true, readPreference: this.serverConfig['ROOT_URL'] !== 'http://localhost:4200' ? 'secondary' : 'primary'}, true);\n\n\t\t\t\treturn {\n\t\t\t\t\tmethods: latencies,\n\t\t\t\t\tsubscriptions: subscriptions,\n\t\t\t\t\tmethodGraphData: methodGraphData,\n\t\t\t\t\tsubGraphData: subGraphData\n\t\t\t\t};\n\t\t\t},\n\t\t\tskipValidation: true\n\t\t}\n\t});\n}\n"]}
@@ -1,4 +1,12 @@
1
1
  import { CollectionDocument } from './collection-document.model';
2
+ export interface EmailHistoryOccurrence {
3
+ _id: string;
4
+ createdAt: Date;
5
+ subject: string;
6
+ text?: string;
7
+ html?: string;
8
+ meta?: Record<string, any>;
9
+ }
2
10
  export interface EmailHistoryModel extends CollectionDocument {
3
11
  date: Date;
4
12
  id_user: string;
@@ -15,5 +23,8 @@ export interface EmailHistoryModel extends CollectionDocument {
15
23
  processingAt?: Date;
16
24
  completedAt?: Date;
17
25
  force_ses?: boolean;
26
+ correlationId?: string;
27
+ meta?: Record<string, any>;
28
+ occurrences?: EmailHistoryOccurrence[];
18
29
  }
19
30
  export type EmailHistoryStatusType = 'pending' | 'processing' | 'completed' | 'failed';
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/models/email-history.model.ts"],"names":[],"mappings":";;AAsBA,yDAAyD;AACzD,kGAAkG;AAClG,mEAAmE;AACnE,iHAAiH;AACjH,4DAA4D;AAC5D,0GAA0G;AAC1G,gHAAgH;AAChH,sGAAsG;AACtG,2EAA2E;AAC3E,4OAA4O;AAC5O,uFAAuF;AACvF,sKAAsK","file":"email-history.model.js","sourcesContent":["import { CollectionDocument } from './collection-document.model';\n\nexport interface EmailHistoryModel extends CollectionDocument {\n\tdate: Date;\n\tid_user: string;\n\tuser: string;\n\temail: string;\n\tsubject: string;\n\ttext: string;\n\thtml: string;\n\tattachments: any[];\n\tsend_from: string;\n\treply_to: string;\n\tstatus: EmailHistoryStatusType;\n\terror: string;\n\tprocessingAt?: Date;\n\tcompletedAt?: Date;\n\tforce_ses?: boolean;\n}\n\nexport type EmailHistoryStatusType = 'pending' | 'processing' | 'completed' | 'failed';\n\n// ------------------ Nodemailer Attachments ------------\n// filename - filename to be reported as the name of the attached file. Use of unicode is allowed.\n// content - String, Buffer or a Stream contents for the attachment\n// path - path to the file if you want to stream the file instead of including it (better for larger attachments)\n// href – an URL to the file (data uris are allowed as well)\n// httpHeaders - optional HTTP headers to pass on with the href request, eg. {authorization: \"bearer ...\"}\n// contentType - optional content type for the attachment, if not set will be derived from the filename property\n// contentDisposition - optional content disposition type for the attachment, defaults to ‘attachment’\n// cid - optional content id for using inline images in HTML message source\n// encoding - If set and content is string, then encodes the content to a Buffer using the specified encoding. Example values: ‘base64’, ‘hex’, ‘binary’ etc. Useful if you want to use binary attachments in a JSON formatted email object.\n// headers - custom headers for the attachment node. Same usage as with message headers\n// raw - is an optional special value that overrides entire contents of current mime node including mime headers. Useful if you want to prepare node contents yourself"]}
1
+ {"version":3,"sources":["../../src/models/email-history.model.ts"],"names":[],"mappings":";;AAkCA,yDAAyD;AACzD,kGAAkG;AAClG,mEAAmE;AACnE,iHAAiH;AACjH,4DAA4D;AAC5D,0GAA0G;AAC1G,gHAAgH;AAChH,sGAAsG;AACtG,2EAA2E;AAC3E,4OAA4O;AAC5O,uFAAuF;AACvF,sKAAsK","file":"email-history.model.js","sourcesContent":["import { CollectionDocument } from './collection-document.model';\n\nexport interface EmailHistoryOccurrence {\n\t_id: string;\n\tcreatedAt: Date;\n\tsubject: string;\n\ttext?: string;\n\thtml?: string;\n\tmeta?: Record<string, any>;\n}\n\nexport interface EmailHistoryModel extends CollectionDocument {\n\tdate: Date;\n\tid_user: string;\n\tuser: string;\n\temail: string;\n\tsubject: string;\n\ttext: string;\n\thtml: string;\n\tattachments: any[];\n\tsend_from: string;\n\treply_to: string;\n\tstatus: EmailHistoryStatusType;\n\terror: string;\n\tprocessingAt?: Date;\n\tcompletedAt?: Date;\n\tforce_ses?: boolean;\n\tcorrelationId?: string;\n\tmeta?: Record<string, any>;\n\toccurrences?: EmailHistoryOccurrence[];\n}\n\nexport type EmailHistoryStatusType = 'pending' | 'processing' | 'completed' | 'failed';\n\n// ------------------ Nodemailer Attachments ------------\n// filename - filename to be reported as the name of the attached file. Use of unicode is allowed.\n// content - String, Buffer or a Stream contents for the attachment\n// path - path to the file if you want to stream the file instead of including it (better for larger attachments)\n// href – an URL to the file (data uris are allowed as well)\n// httpHeaders - optional HTTP headers to pass on with the href request, eg. {authorization: \"bearer ...\"}\n// contentType - optional content type for the attachment, if not set will be derived from the filename property\n// contentDisposition - optional content disposition type for the attachment, defaults to ‘attachment’\n// cid - optional content id for using inline images in HTML message source\n// encoding - If set and content is string, then encodes the content to a Buffer using the specified encoding. Example values: ‘base64’, ‘hex’, ‘binary’ etc. Useful if you want to use binary attachments in a JSON formatted email object.\n// headers - custom headers for the attachment node. Same usage as with message headers\n// raw - is an optional special value that overrides entire contents of current mime node including mime headers. Useful if you want to prepare node contents yourself\n"]}
@@ -12,5 +12,6 @@ export interface LogModel extends CollectionDocument {
12
12
  client: string;
13
13
  instance: string;
14
14
  instance_index: string;
15
+ correlationId?: string;
15
16
  }
16
17
  export type LogTypeType = 'all' | 'error' | 'document' | 'query' | 'queryResponse' | 'client-request' | 'client-response' | 'callMethod';
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/models/log.model.ts"],"names":[],"mappings":"","file":"log.model.js","sourcesContent":["import { CollectionDocument } from './collection-document.model';\n\nexport interface LogModel extends CollectionDocument {\n\ttype: LogTypeType;\n\tcollection: string;\n\tid_document: string;\n\tpayload: string;\n\tmethod: string;\n\tid_user: string;\n\tuser: string;\n\tmessageId: number;\n\troute: string;\n\tclient: string;\n\tinstance: string;\n\tinstance_index: string;\n}\n\nexport type LogTypeType = 'all' | 'error' | 'document' | 'query' | 'queryResponse' | 'client-request' | 'client-response' | 'callMethod';"]}
1
+ {"version":3,"sources":["../../src/models/log.model.ts"],"names":[],"mappings":"","file":"log.model.js","sourcesContent":["import { CollectionDocument } from './collection-document.model';\n\nexport interface LogModel extends CollectionDocument {\n\ttype: LogTypeType;\n\tcollection: string;\n\tid_document: string;\n\tpayload: string;\n\tmethod: string;\n\tid_user: string;\n\tuser: string;\n\tmessageId: number;\n\troute: string;\n\tclient: string;\n\tinstance: string;\n\tinstance_index: string;\n\tcorrelationId?: string;\n}\n\nexport type LogTypeType = 'all' | 'error' | 'document' | 'query' | 'queryResponse' | 'client-request' | 'client-response' | 'callMethod';\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@resolveio/server-lib",
3
- "version": "20.10.0",
3
+ "version": "20.10.2",
4
4
  "description": "",
5
5
  "scripts": {
6
6
  "package": "./build_package.sh",
package/server-app.js CHANGED
@@ -89,6 +89,7 @@ var method_manager_1 = require("./managers/method.manager");
89
89
  var monitor_manager_1 = require("./managers/monitor.manager");
90
90
  var subscription_manager_1 = require("./managers/subscription.manager");
91
91
  var common_1 = require("./util/common");
92
+ var error_tracking_1 = require("./util/error-tracking");
92
93
  var mongodb_1 = require("mongodb");
93
94
  var auth_1 = require("./http/auth");
94
95
  var health_1 = require("./http/health");
@@ -162,103 +163,145 @@ var ResolveIOMainServer = /** @class */ (function () {
162
163
  }, 5000);
163
164
  process.removeAllListeners('unhandledRejection');
164
165
  process.on('unhandledRejection', function (error, rej) { return __awaiter(_this, void 0, void 0, function () {
165
- var diffTimeSec;
166
+ var _a, normalizedError, correlationId, errorDetails, diffTimeSec;
166
167
  var _this = this;
167
- return __generator(this, function (_a) {
168
- switch (_a.label) {
168
+ return __generator(this, function (_b) {
169
+ switch (_b.label) {
169
170
  case 0:
171
+ _a = (0, error_tracking_1.ensureErrorWithCorrelation)(error), normalizedError = _a.error, correlationId = _a.correlationId;
170
172
  if (this._methodManager.getEnableDebug()) {
171
- console.error(new Date(), 'ERROR DETECTED w/ Debug Flag Active: unhandledRejection', [error, rej]);
173
+ console.error(new Date(), 'ERROR DETECTED w/ Debug Flag Active: unhandledRejection', [normalizedError, rej, { correlationId: correlationId }]);
172
174
  }
173
- // Condition to filter out the MongoError with code 48 (NamespaceExists)
174
- if (error && error['name'] === 'MongoError' && (error['code'] === 48 || error['code'] === 26 || error['code'] === 11000 || error['code'] === 251)) {
175
+ // Condition to filter out the MongoError with specific codes
176
+ if (normalizedError && normalizedError['name'] === 'MongoError' && (normalizedError['code'] === 48 || normalizedError['code'] === 26 || normalizedError['code'] === 11000 || normalizedError['code'] === 251)) {
175
177
  return [2 /*return*/]; // Simply return without doing anything further
176
178
  }
177
- if (error && error['name'] === 'MongoServerError' && (!initServerFlag || error['code'] === 26 || error['code'] === 11000 || error['code'] === 86 || error['code'] === 251)) {
179
+ if (normalizedError && normalizedError['name'] === 'MongoServerError' && (!initServerFlag || normalizedError['code'] === 26 || normalizedError['code'] === 11000 || normalizedError['code'] === 86 || normalizedError['code'] === 251)) {
178
180
  return [2 /*return*/]; // Simply return without doing anything further
179
181
  }
180
- console.error(new Date(), 'Unhandled Rejection at Promise', [error, rej]);
182
+ errorDetails = {
183
+ id: correlationId,
184
+ name: normalizedError === null || normalizedError === void 0 ? void 0 : normalizedError.name,
185
+ message: normalizedError === null || normalizedError === void 0 ? void 0 : normalizedError.message,
186
+ stack: normalizedError === null || normalizedError === void 0 ? void 0 : normalizedError.stack,
187
+ code: normalizedError === null || normalizedError === void 0 ? void 0 : normalizedError.code,
188
+ codeName: normalizedError === null || normalizedError === void 0 ? void 0 : normalizedError.codeName
189
+ };
190
+ console.error(new Date(), 'Unhandled Rejection at Promise', [errorDetails]);
181
191
  diffTimeSec = moment().diff(this._serverStartTime, 'seconds');
182
- if (!(error && (error['name'] === 'MongoNetworkTimeoutError' || error instanceof mongodb_1.MongoNetworkTimeoutError))) return [3 /*break*/, 3];
192
+ if (!(normalizedError && (normalizedError['name'] === 'MongoNetworkTimeoutError' || normalizedError instanceof mongodb_1.MongoNetworkTimeoutError))) return [3 /*break*/, 3];
183
193
  if (!(diffTimeSec > 60 && !this._lastErrorMsg)) return [3 /*break*/, 2];
184
194
  this._lastErrorMsg = new Date();
185
195
  setTimeout(function () {
186
196
  _this._lastErrorMsg = null;
187
197
  }, 60000);
188
198
  // Sending email notification (using your existing method)
189
- return [4 /*yield*/, this._methodManager.sendEmail('dev@resolveio.com', 'SERVER - MongoNetworkTimeoutError - Quitting NodeJS - ' + resolveio_server_app_1.ResolveIOServer.getServerConfig()['CLIENT_NAME'], JSON.stringify({
190
- name: error['name'],
191
- message: error['message'],
192
- stack: error['stack']
193
- }, null, 2))];
199
+ return [4 /*yield*/, this._methodManager.sendEmail('dev@resolveio.com', 'SERVER - MongoNetworkTimeoutError - Quitting NodeJS - ' + resolveio_server_app_1.ResolveIOServer.getServerConfig()['CLIENT_NAME'], JSON.stringify(errorDetails, null, 2), null, null, null, null, false, false, {
200
+ correlationId: correlationId,
201
+ meta: {
202
+ context: 'unhandledRejection',
203
+ scenario: 'MongoNetworkTimeoutError'
204
+ }
205
+ })];
194
206
  case 1:
195
207
  // Sending email notification (using your existing method)
196
- _a.sent();
208
+ _b.sent();
197
209
  // Exiting the process
198
210
  process.exit(1);
199
- _a.label = 2;
211
+ _b.label = 2;
200
212
  case 2: return [3 /*break*/, 11];
201
213
  case 3:
202
- if (!(error && error['name'] === 'MongoError' && error['message'] === 'not master')) return [3 /*break*/, 6];
214
+ if (!(normalizedError && normalizedError['name'] === 'MongoError' && normalizedError['message'] === 'not master')) return [3 /*break*/, 6];
203
215
  if (!(diffTimeSec > 60 && !this._lastErrorMsg)) return [3 /*break*/, 5];
204
216
  this._lastErrorMsg = new Date();
205
217
  setTimeout(function () {
206
218
  _this._lastErrorMsg = null;
207
219
  }, 60000);
208
- return [4 /*yield*/, this._methodManager.sendEmail('dev@resolveio.com', 'SERVER - Unhandled Rejection - Quitting NodeJS - ' + resolveio_server_app_1.ResolveIOServer.getServerConfig()['CLIENT_NAME'], JSON.stringify([error['name'], error['message'], error['stack']], null, 2))];
220
+ return [4 /*yield*/, this._methodManager.sendEmail('dev@resolveio.com', 'SERVER - Unhandled Rejection - Quitting NodeJS - ' + resolveio_server_app_1.ResolveIOServer.getServerConfig()['CLIENT_NAME'], JSON.stringify(errorDetails, null, 2), null, null, null, null, false, false, {
221
+ correlationId: correlationId,
222
+ meta: {
223
+ context: 'unhandledRejection',
224
+ scenario: 'MongoNotMaster'
225
+ }
226
+ })];
209
227
  case 4:
210
- _a.sent();
211
- _a.label = 5;
228
+ _b.sent();
229
+ _b.label = 5;
212
230
  case 5:
213
231
  process.exit(1);
214
232
  return [3 /*break*/, 11];
215
233
  case 6:
216
- if (!(error && error['name'] === 'MongoError' && error['message'] === 'not master and slaveOk=false')) return [3 /*break*/, 9];
234
+ if (!(normalizedError && normalizedError['name'] === 'MongoError' && normalizedError['message'] === 'not master and slaveOk=false')) return [3 /*break*/, 9];
217
235
  if (!(diffTimeSec > 60 && !this._lastErrorMsg)) return [3 /*break*/, 8];
218
236
  this._lastErrorMsg = new Date();
219
237
  setTimeout(function () {
220
238
  _this._lastErrorMsg = null;
221
239
  }, 60000);
222
- return [4 /*yield*/, this._methodManager.sendEmail('dev@resolveio.com', 'SERVER - Unhandled Rejection - Quitting NodeJS - ' + resolveio_server_app_1.ResolveIOServer.getServerConfig()['CLIENT_NAME'], JSON.stringify([error['name'], error['message'], error['stack']], null, 2))];
240
+ return [4 /*yield*/, this._methodManager.sendEmail('dev@resolveio.com', 'SERVER - Unhandled Rejection - Quitting NodeJS - ' + resolveio_server_app_1.ResolveIOServer.getServerConfig()['CLIENT_NAME'], JSON.stringify(errorDetails, null, 2), null, null, null, null, false, false, {
241
+ correlationId: correlationId,
242
+ meta: {
243
+ context: 'unhandledRejection',
244
+ scenario: 'MongoNotMasterSlaveOk'
245
+ }
246
+ })];
223
247
  case 7:
224
- _a.sent();
225
- _a.label = 8;
248
+ _b.sent();
249
+ _b.label = 8;
226
250
  case 8:
227
251
  process.exit(1);
228
252
  return [3 /*break*/, 11];
229
253
  case 9:
230
- if (!error) return [3 /*break*/, 11];
231
- if (!(error['name'] !== 'StatusError' && error['message'] !== '')) return [3 /*break*/, 11];
254
+ if (!(normalizedError && normalizedError['name'] !== 'StatusError' && normalizedError['message'] !== '')) return [3 /*break*/, 11];
232
255
  if (!(diffTimeSec > 60 && !this._lastErrorMsg)) return [3 /*break*/, 11];
233
256
  this._lastErrorMsg = new Date();
234
257
  setTimeout(function () {
235
258
  _this._lastErrorMsg = null;
236
259
  }, 60000);
237
- return [4 /*yield*/, this._methodManager.sendEmail('dev@resolveio.com', 'SERVER - Unhandled Rejection - ' + resolveio_server_app_1.ResolveIOServer.getServerConfig()['CLIENT_NAME'], JSON.stringify([error['name'], error['message'], error['stack']], null, 2))];
260
+ return [4 /*yield*/, this._methodManager.sendEmail('dev@resolveio.com', 'SERVER - Unhandled Rejection - ' + resolveio_server_app_1.ResolveIOServer.getServerConfig()['CLIENT_NAME'], JSON.stringify(errorDetails, null, 2), null, null, null, null, false, false, {
261
+ correlationId: correlationId,
262
+ meta: {
263
+ context: 'unhandledRejection',
264
+ scenario: 'General'
265
+ }
266
+ })];
238
267
  case 10:
239
- _a.sent();
240
- _a.label = 11;
268
+ _b.sent();
269
+ _b.label = 11;
241
270
  case 11: return [2 /*return*/];
242
271
  }
243
272
  });
244
273
  }); });
245
274
  process.on('uncaughtException', function (error) { return __awaiter(_this, void 0, void 0, function () {
246
- var diffTimeSec;
275
+ var _a, normalizedError, correlationId, diffTimeSec, errorDetails;
247
276
  var _this = this;
248
- return __generator(this, function (_a) {
249
- switch (_a.label) {
277
+ return __generator(this, function (_b) {
278
+ switch (_b.label) {
250
279
  case 0:
251
- console.error(error, 'Uncaught Exception thrown');
280
+ _a = (0, error_tracking_1.ensureErrorWithCorrelation)(error), normalizedError = _a.error, correlationId = _a.correlationId;
281
+ console.error(normalizedError, 'Uncaught Exception thrown', { correlationId: correlationId });
252
282
  diffTimeSec = moment().diff(this._serverStartTime, 'seconds');
253
283
  if (!(diffTimeSec > 60 && !this._lastErrorMsg)) return [3 /*break*/, 2];
254
284
  this._lastErrorMsg = new Date();
255
285
  setTimeout(function () {
256
286
  _this._lastErrorMsg = null;
257
287
  }, 60000);
258
- return [4 /*yield*/, this._methodManager.sendEmail('dev@resolveio.com', 'SERVER - Unhandled Exception - ' + resolveio_server_app_1.ResolveIOServer.getServerConfig()['CLIENT_NAME'], JSON.stringify([error['name'], error['message'], error['stack']], null, 2))];
288
+ errorDetails = {
289
+ id: correlationId,
290
+ name: normalizedError === null || normalizedError === void 0 ? void 0 : normalizedError.name,
291
+ message: normalizedError === null || normalizedError === void 0 ? void 0 : normalizedError.message,
292
+ stack: normalizedError === null || normalizedError === void 0 ? void 0 : normalizedError.stack,
293
+ code: normalizedError === null || normalizedError === void 0 ? void 0 : normalizedError.code,
294
+ codeName: normalizedError === null || normalizedError === void 0 ? void 0 : normalizedError.codeName
295
+ };
296
+ return [4 /*yield*/, this._methodManager.sendEmail('dev@resolveio.com', 'SERVER - Unhandled Exception - ' + resolveio_server_app_1.ResolveIOServer.getServerConfig()['CLIENT_NAME'], JSON.stringify(errorDetails, null, 2), null, null, null, null, false, false, {
297
+ correlationId: correlationId,
298
+ meta: {
299
+ context: 'uncaughtException'
300
+ }
301
+ })];
259
302
  case 1:
260
- _a.sent();
261
- _a.label = 2;
303
+ _b.sent();
304
+ _b.label = 2;
262
305
  case 2: return [2 /*return*/];
263
306
  }
264
307
  });