@next-core/easyops-runtime 0.12.37 → 0.12.38
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.
|
@@ -93,9 +93,6 @@ function _connect() {
|
|
|
93
93
|
}
|
|
94
94
|
// eslint-disable-next-line no-console
|
|
95
95
|
console.log("WebSocket close:", e);
|
|
96
|
-
for (const listener of (0, _classPrivateFieldGet3.default)(_closeListeners, this)) {
|
|
97
|
-
listener();
|
|
98
|
-
}
|
|
99
96
|
// Error code 1000 means that the connection was closed normally.
|
|
100
97
|
if (e.code !== 1000) {
|
|
101
98
|
(0, _assertClassBrand2.default)(_MessageService_brand, this, _reconnect).call(this);
|
|
@@ -120,6 +117,10 @@ function _reconnect() {
|
|
|
120
117
|
if ((0, _classPrivateFieldGet3.default)(_retryCount, this) >= RETRY_LIMIT) {
|
|
121
118
|
// eslint-disable-next-line no-console
|
|
122
119
|
console.error("WebSocket connect retry limit exceeded");
|
|
120
|
+
// Keep behavior as v2: emit close event only after retry limit exceeded
|
|
121
|
+
for (const listener of (0, _classPrivateFieldGet3.default)(_closeListeners, this)) {
|
|
122
|
+
listener();
|
|
123
|
+
}
|
|
123
124
|
return;
|
|
124
125
|
}
|
|
125
126
|
// Double the timeout for each retry
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MessageService.js","names":["RETRY_TIMEOUT_UNIT","RETRY_LIMIT","_url","WeakMap","_ws","_queuedMessages","_messageListeners","_closeListeners","_retryCount","_closed","_MessageService_brand","WeakSet","MessageService","constructor","url","_classPrivateMethodInitSpec2","default","_classPrivateFieldInitSpec2","Set","_classPrivateFieldSet2","send","data","_classPrivateFieldGet3","_assertClassBrand2","_connect","call","stringifiedData","JSON","stringify","readyState","WebSocket","OPEN","push","onMessage","listener","add","onClose","reset","length","clear","close","_classPrivateFieldGet2","exports","console","log","ws","resetRetryCountTimeout","addEventListener","e","setTimeout","undefined","msg","clearTimeout","code","_reconnect","error","response","parse","delay","Math","pow","_this$retryCount","_this$retryCount2"],"sources":["../../../src/websocket/MessageService.ts"],"sourcesContent":["export type MessageListener<T = unknown> = (response: T) => void;\nexport type CloseListener = () => void;\n\nconst RETRY_TIMEOUT_UNIT = 1000;\nconst RETRY_LIMIT = 5;\n\nexport class MessageService {\n #url: string;\n #ws: WebSocket | undefined;\n #queuedMessages: string[] = [];\n #messageListeners = new Set<MessageListener>();\n #closeListeners = new Set<CloseListener>();\n #retryCount = 0;\n #closed = false;\n\n constructor(url: string) {\n this.#url = url;\n }\n\n #connect(): WebSocket {\n // eslint-disable-next-line no-console\n console.log(\"WebSocket connecting ...\");\n const ws = new WebSocket(this.#url);\n let resetRetryCountTimeout: number | undefined;\n ws.addEventListener(\"open\", (e) => {\n // eslint-disable-next-line no-console\n console.log(\"WebSocket open:\", e);\n\n // If server closes the socket not more than 1 second after it's opened,\n // we should not reset the retry count.\n resetRetryCountTimeout = setTimeout(() => {\n resetRetryCountTimeout = undefined;\n this.#retryCount = 0;\n }, 1e3) as unknown as number;\n\n for (const msg of this.#queuedMessages) {\n ws.send(msg);\n }\n this.#queuedMessages.length = 0;\n });\n ws.addEventListener(\"close\", (e) => {\n if (this.#closed) {\n return;\n }\n if (resetRetryCountTimeout !== undefined) {\n clearTimeout(resetRetryCountTimeout);\n }\n // eslint-disable-next-line no-console\n console.log(\"WebSocket close:\", e);\n
|
|
1
|
+
{"version":3,"file":"MessageService.js","names":["RETRY_TIMEOUT_UNIT","RETRY_LIMIT","_url","WeakMap","_ws","_queuedMessages","_messageListeners","_closeListeners","_retryCount","_closed","_MessageService_brand","WeakSet","MessageService","constructor","url","_classPrivateMethodInitSpec2","default","_classPrivateFieldInitSpec2","Set","_classPrivateFieldSet2","send","data","_classPrivateFieldGet3","_assertClassBrand2","_connect","call","stringifiedData","JSON","stringify","readyState","WebSocket","OPEN","push","onMessage","listener","add","onClose","reset","length","clear","close","_classPrivateFieldGet2","exports","console","log","ws","resetRetryCountTimeout","addEventListener","e","setTimeout","undefined","msg","clearTimeout","code","_reconnect","error","response","parse","delay","Math","pow","_this$retryCount","_this$retryCount2"],"sources":["../../../src/websocket/MessageService.ts"],"sourcesContent":["export type MessageListener<T = unknown> = (response: T) => void;\nexport type CloseListener = () => void;\n\nconst RETRY_TIMEOUT_UNIT = 1000;\nconst RETRY_LIMIT = 5;\n\nexport class MessageService {\n #url: string;\n #ws: WebSocket | undefined;\n #queuedMessages: string[] = [];\n #messageListeners = new Set<MessageListener>();\n #closeListeners = new Set<CloseListener>();\n #retryCount = 0;\n #closed = false;\n\n constructor(url: string) {\n this.#url = url;\n }\n\n #connect(): WebSocket {\n // eslint-disable-next-line no-console\n console.log(\"WebSocket connecting ...\");\n const ws = new WebSocket(this.#url);\n let resetRetryCountTimeout: number | undefined;\n ws.addEventListener(\"open\", (e) => {\n // eslint-disable-next-line no-console\n console.log(\"WebSocket open:\", e);\n\n // If server closes the socket not more than 1 second after it's opened,\n // we should not reset the retry count.\n resetRetryCountTimeout = setTimeout(() => {\n resetRetryCountTimeout = undefined;\n this.#retryCount = 0;\n }, 1e3) as unknown as number;\n\n for (const msg of this.#queuedMessages) {\n ws.send(msg);\n }\n this.#queuedMessages.length = 0;\n });\n ws.addEventListener(\"close\", (e) => {\n if (this.#closed) {\n return;\n }\n if (resetRetryCountTimeout !== undefined) {\n clearTimeout(resetRetryCountTimeout);\n }\n // eslint-disable-next-line no-console\n console.log(\"WebSocket close:\", e);\n // Error code 1000 means that the connection was closed normally.\n if (e.code !== 1000) {\n this.#reconnect();\n }\n });\n ws.addEventListener(\"error\", (e) => {\n // No need to reconnect in error event listener,\n // there will always be a close event along with the error event.\n // eslint-disable-next-line no-console\n console.error(\"WebSocket error:\", e);\n });\n ws.addEventListener(\"message\", (e: MessageEvent<string>) => {\n const response = JSON.parse(e.data);\n for (const listener of this.#messageListeners) {\n listener(response);\n }\n });\n return ws;\n }\n\n #reconnect() {\n // istanbul ignore next: currently can't mock this\n if (this.#retryCount >= RETRY_LIMIT) {\n // eslint-disable-next-line no-console\n console.error(\"WebSocket connect retry limit exceeded\");\n // Keep behavior as v2: emit close event only after retry limit exceeded\n for (const listener of this.#closeListeners) {\n listener();\n }\n return;\n }\n // Double the timeout for each retry\n const delay = Math.pow(2, this.#retryCount) * RETRY_TIMEOUT_UNIT;\n // eslint-disable-next-line no-console\n console.log(\"WebSocket will reconnect after %d seconds\", delay / 1000);\n setTimeout(() => {\n this.#retryCount++;\n this.#ws = this.#connect();\n }, delay);\n }\n\n send(data: unknown): void {\n // Connect when data is sent for the first time\n if (!this.#ws) {\n this.#ws = this.#connect();\n }\n const stringifiedData = JSON.stringify(data);\n if (this.#ws.readyState === WebSocket.OPEN) {\n this.#ws.send(stringifiedData);\n } else {\n this.#queuedMessages.push(stringifiedData);\n }\n }\n\n onMessage<T = unknown>(listener: MessageListener<T>) {\n this.#messageListeners.add(listener as MessageListener<unknown>);\n }\n\n onClose(listener: CloseListener) {\n this.#closeListeners.add(listener);\n }\n\n reset() {\n this.#queuedMessages.length = 0;\n this.#messageListeners.clear();\n this.#closeListeners.clear();\n }\n\n close() {\n this.#closed = true;\n if (this.#ws?.readyState === WebSocket.OPEN) {\n this.#ws.close();\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;AAGA,MAAMA,kBAAkB,GAAG,IAAI;AAC/B,MAAMC,WAAW,GAAG,CAAC;AAAC,IAAAC,IAAA,oBAAAC,OAAA;AAAA,IAAAC,GAAA,oBAAAD,OAAA;AAAA,IAAAE,eAAA,oBAAAF,OAAA;AAAA,IAAAG,iBAAA,oBAAAH,OAAA;AAAA,IAAAI,eAAA,oBAAAJ,OAAA;AAAA,IAAAK,WAAA,oBAAAL,OAAA;AAAA,IAAAM,OAAA,oBAAAN,OAAA;AAAA,IAAAO,qBAAA,oBAAAC,OAAA;AAEf,MAAMC,cAAc,CAAC;EAS1BC,WAAWA,CAACC,GAAW,EAAE;IAAA,IAAAC,4BAAA,CAAAC,OAAA,QAAAN,qBAAA;IARzB,IAAAO,2BAAA,CA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AACc,QAAO,CAAC,CAAAC,IAAA,CAAb,IAAI,CAAR,CAAC;EACV,CAAC,EAAEiC,KAAK,CAAC;AACX","ignoreList":[]}
|
|
@@ -85,9 +85,6 @@ function _connect() {
|
|
|
85
85
|
}
|
|
86
86
|
// eslint-disable-next-line no-console
|
|
87
87
|
console.log("WebSocket close:", e);
|
|
88
|
-
for (const listener of _classPrivateFieldGet(_closeListeners, this)) {
|
|
89
|
-
listener();
|
|
90
|
-
}
|
|
91
88
|
// Error code 1000 means that the connection was closed normally.
|
|
92
89
|
if (e.code !== 1000) {
|
|
93
90
|
_assertClassBrand(_MessageService_brand, this, _reconnect).call(this);
|
|
@@ -112,6 +109,10 @@ function _reconnect() {
|
|
|
112
109
|
if (_classPrivateFieldGet(_retryCount, this) >= RETRY_LIMIT) {
|
|
113
110
|
// eslint-disable-next-line no-console
|
|
114
111
|
console.error("WebSocket connect retry limit exceeded");
|
|
112
|
+
// Keep behavior as v2: emit close event only after retry limit exceeded
|
|
113
|
+
for (const listener of _classPrivateFieldGet(_closeListeners, this)) {
|
|
114
|
+
listener();
|
|
115
|
+
}
|
|
115
116
|
return;
|
|
116
117
|
}
|
|
117
118
|
// Double the timeout for each retry
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MessageService.js","names":["RETRY_TIMEOUT_UNIT","RETRY_LIMIT","_url","WeakMap","_ws","_queuedMessages","_messageListeners","_closeListeners","_retryCount","_closed","_MessageService_brand","WeakSet","MessageService","constructor","url","_classPrivateMethodInitSpec","_classPrivateFieldInitSpec","Set","_classPrivateFieldSet","send","data","_classPrivateFieldGet","_assertClassBrand","_connect","call","stringifiedData","JSON","stringify","readyState","WebSocket","OPEN","push","onMessage","listener","add","onClose","reset","length","clear","close","_classPrivateFieldGet2","console","log","ws","resetRetryCountTimeout","addEventListener","e","setTimeout","undefined","msg","clearTimeout","code","_reconnect","error","response","parse","delay","Math","pow","_this$retryCount","_this$retryCount2"],"sources":["../../../src/websocket/MessageService.ts"],"sourcesContent":["export type MessageListener<T = unknown> = (response: T) => void;\nexport type CloseListener = () => void;\n\nconst RETRY_TIMEOUT_UNIT = 1000;\nconst RETRY_LIMIT = 5;\n\nexport class MessageService {\n #url: string;\n #ws: WebSocket | undefined;\n #queuedMessages: string[] = [];\n #messageListeners = new Set<MessageListener>();\n #closeListeners = new Set<CloseListener>();\n #retryCount = 0;\n #closed = false;\n\n constructor(url: string) {\n this.#url = url;\n }\n\n #connect(): WebSocket {\n // eslint-disable-next-line no-console\n console.log(\"WebSocket connecting ...\");\n const ws = new WebSocket(this.#url);\n let resetRetryCountTimeout: number | undefined;\n ws.addEventListener(\"open\", (e) => {\n // eslint-disable-next-line no-console\n console.log(\"WebSocket open:\", e);\n\n // If server closes the socket not more than 1 second after it's opened,\n // we should not reset the retry count.\n resetRetryCountTimeout = setTimeout(() => {\n resetRetryCountTimeout = undefined;\n this.#retryCount = 0;\n }, 1e3) as unknown as number;\n\n for (const msg of this.#queuedMessages) {\n ws.send(msg);\n }\n this.#queuedMessages.length = 0;\n });\n ws.addEventListener(\"close\", (e) => {\n if (this.#closed) {\n return;\n }\n if (resetRetryCountTimeout !== undefined) {\n clearTimeout(resetRetryCountTimeout);\n }\n // eslint-disable-next-line no-console\n console.log(\"WebSocket close:\", e);\n
|
|
1
|
+
{"version":3,"file":"MessageService.js","names":["RETRY_TIMEOUT_UNIT","RETRY_LIMIT","_url","WeakMap","_ws","_queuedMessages","_messageListeners","_closeListeners","_retryCount","_closed","_MessageService_brand","WeakSet","MessageService","constructor","url","_classPrivateMethodInitSpec","_classPrivateFieldInitSpec","Set","_classPrivateFieldSet","send","data","_classPrivateFieldGet","_assertClassBrand","_connect","call","stringifiedData","JSON","stringify","readyState","WebSocket","OPEN","push","onMessage","listener","add","onClose","reset","length","clear","close","_classPrivateFieldGet2","console","log","ws","resetRetryCountTimeout","addEventListener","e","setTimeout","undefined","msg","clearTimeout","code","_reconnect","error","response","parse","delay","Math","pow","_this$retryCount","_this$retryCount2"],"sources":["../../../src/websocket/MessageService.ts"],"sourcesContent":["export type MessageListener<T = unknown> = (response: T) => void;\nexport type CloseListener = () => void;\n\nconst RETRY_TIMEOUT_UNIT = 1000;\nconst RETRY_LIMIT = 5;\n\nexport class MessageService {\n #url: string;\n #ws: WebSocket | undefined;\n #queuedMessages: string[] = [];\n #messageListeners = new Set<MessageListener>();\n #closeListeners = new Set<CloseListener>();\n #retryCount = 0;\n #closed = false;\n\n constructor(url: string) {\n this.#url = url;\n }\n\n #connect(): WebSocket {\n // eslint-disable-next-line no-console\n console.log(\"WebSocket connecting ...\");\n const ws = new WebSocket(this.#url);\n let resetRetryCountTimeout: number | undefined;\n ws.addEventListener(\"open\", (e) => {\n // eslint-disable-next-line no-console\n console.log(\"WebSocket open:\", e);\n\n // If server closes the socket not more than 1 second after it's opened,\n // we should not reset the retry count.\n resetRetryCountTimeout = setTimeout(() => {\n resetRetryCountTimeout = undefined;\n this.#retryCount = 0;\n }, 1e3) as unknown as number;\n\n for (const msg of this.#queuedMessages) {\n ws.send(msg);\n }\n this.#queuedMessages.length = 0;\n });\n ws.addEventListener(\"close\", (e) => {\n if (this.#closed) {\n return;\n }\n if (resetRetryCountTimeout !== undefined) {\n clearTimeout(resetRetryCountTimeout);\n }\n // eslint-disable-next-line no-console\n console.log(\"WebSocket close:\", e);\n // Error code 1000 means that the connection was closed normally.\n if (e.code !== 1000) {\n this.#reconnect();\n }\n });\n ws.addEventListener(\"error\", (e) => {\n // No need to reconnect in error event listener,\n // there will always be a close event along with the error event.\n // eslint-disable-next-line no-console\n console.error(\"WebSocket error:\", e);\n });\n ws.addEventListener(\"message\", (e: MessageEvent<string>) => {\n const response = JSON.parse(e.data);\n for (const listener of this.#messageListeners) {\n listener(response);\n }\n });\n return ws;\n }\n\n #reconnect() {\n // istanbul ignore next: currently can't mock this\n if (this.#retryCount >= RETRY_LIMIT) {\n // eslint-disable-next-line no-console\n console.error(\"WebSocket connect retry limit exceeded\");\n // Keep behavior as v2: emit close event only after retry limit exceeded\n for (const listener of this.#closeListeners) {\n listener();\n }\n return;\n }\n // Double the timeout for each retry\n const delay = Math.pow(2, this.#retryCount) * RETRY_TIMEOUT_UNIT;\n // eslint-disable-next-line no-console\n console.log(\"WebSocket will reconnect after %d seconds\", delay / 1000);\n setTimeout(() => {\n this.#retryCount++;\n this.#ws = this.#connect();\n }, delay);\n }\n\n send(data: unknown): void {\n // Connect when data is sent for the first time\n if (!this.#ws) {\n this.#ws = this.#connect();\n }\n const stringifiedData = JSON.stringify(data);\n if (this.#ws.readyState === WebSocket.OPEN) {\n this.#ws.send(stringifiedData);\n } else {\n this.#queuedMessages.push(stringifiedData);\n }\n }\n\n onMessage<T = unknown>(listener: MessageListener<T>) {\n this.#messageListeners.add(listener as MessageListener<unknown>);\n }\n\n onClose(listener: CloseListener) {\n this.#closeListeners.add(listener);\n }\n\n reset() {\n this.#queuedMessages.length = 0;\n this.#messageListeners.clear();\n this.#closeListeners.clear();\n }\n\n close() {\n this.#closed = true;\n if (this.#ws?.readyState === WebSocket.OPEN) {\n this.#ws.close();\n }\n }\n}\n"],"mappings":";;;;;AAGA,MAAMA,kBAAkB,GAAG,IAAI;AAC/B,MAAMC,WAAW,GAAG,CAAC;AAAC,IAAAC,IAAA,oBAAAC,OAAA;AAAA,IAAAC,GAAA,oBAAAD,OAAA;AAAA,IAAAE,eAAA,oBAAAF,OAAA;AAAA,IAAAG,iBAAA,oBAAAH,OAAA;AAAA,IAAAI,eAAA,oBAAAJ,OAAA;AAAA,IAAAK,WAAA,oBAAAL,OAAA;AAAA,IAAAM,OAAA,oBAAAN,OAAA;AAAA,IAAAO,qBAAA,oBAAAC,OAAA;AAEtB,OAAO,MAAMC,cAAc,CAAC;EAS1BC,WAAWA,CAACC,GAAW,EAAE;IAAAC,2BAAA,OAAAL,qBAAA;IARzBM,0BAAA,OAAAd,IAAI;IACJc,0BAAA,OAAAZ,GAAG;IACHY,0BAAA,OAAAX,eAAe,EAAa,EAAE;IAC9BW,0BAAA,OA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,CAAC,CAAC,GAAGR,kBAAkB;EAChE;EACAyC,OAAO,CAACC,GAAG,CAAC,2CAA2C,EAAEc,KAAK,GAAG,IAAI,CAAC;EACtET,UAAU,CAAC,MAAM;IAAA,IAAAY,gBAAA,EAAAC,iBAAA;IACf1C,qBAAA,CAAKV,WAAW,EAAhB,IAAI,GAAAmD,gBAAA,GAAJtC,qBAAA,CAAKb,WAAW,EAAhB,IAAe,CAAC,EAAAoD,iBAAA,GAAAD,gBAAA,IAAAA,gBAAA,CAAD,CAAC,EAAAC,iBAAA;IAChB1C,qBAAA,CAAKd,GAAG,EAAR,IAAI,EAAOkB,iBAAA,CAAAZ,qBAAA,MAAI,EAACa,QAAO,CAAC,CAAAC,IAAA,CAAb,IAAI,CAAR,CAAC;EACV,CAAC,EAAEgC,KAAK,CAAC;AACX","ignoreList":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@next-core/easyops-runtime",
|
|
3
|
-
"version": "0.12.
|
|
3
|
+
"version": "0.12.38",
|
|
4
4
|
"homepage": "https://github.com/easyops-cn/next-core/tree/v3/packages/easyops-runtime",
|
|
5
5
|
"license": "GPL-3.0",
|
|
6
6
|
"repository": {
|
|
@@ -60,5 +60,5 @@
|
|
|
60
60
|
"jest-websocket-mock": "^2.5.0",
|
|
61
61
|
"whatwg-fetch": "^3.6.20"
|
|
62
62
|
},
|
|
63
|
-
"gitHead": "
|
|
63
|
+
"gitHead": "473c860b1e73598fb3cbebaa983a0ef9afbe9159"
|
|
64
64
|
}
|