@ooneex/socket 0.12.0 → 0.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client/index.js +2 -100
- package/dist/client/index.js.map +2 -2
- package/package.json +6 -5
package/dist/client/index.js
CHANGED
|
@@ -1,102 +1,4 @@
|
|
|
1
1
|
// @bun
|
|
2
|
-
|
|
3
|
-
class Socket {
|
|
4
|
-
url;
|
|
5
|
-
ws;
|
|
6
|
-
messageHandler;
|
|
7
|
-
openHandler;
|
|
8
|
-
errorHandler;
|
|
9
|
-
closeHandler;
|
|
10
|
-
queuedMessages = [];
|
|
11
|
-
constructor(url) {
|
|
12
|
-
this.url = url;
|
|
13
|
-
const fullURL = this.buildURL(this.url);
|
|
14
|
-
this.ws = new WebSocket(fullURL);
|
|
15
|
-
this.setupEventHandlers();
|
|
16
|
-
}
|
|
17
|
-
close(code, reason) {
|
|
18
|
-
this.ws.close(code, reason);
|
|
19
|
-
}
|
|
20
|
-
send(data) {
|
|
21
|
-
const text = JSON.stringify(data);
|
|
22
|
-
this.sendRaw(text);
|
|
23
|
-
}
|
|
24
|
-
sendRaw(payload) {
|
|
25
|
-
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
|
|
26
|
-
this.ws.send(payload);
|
|
27
|
-
} else {
|
|
28
|
-
this.queuedMessages.push(payload);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
onMessage(handler) {
|
|
32
|
-
this.messageHandler = handler;
|
|
33
|
-
}
|
|
34
|
-
onOpen(handler) {
|
|
35
|
-
this.openHandler = handler;
|
|
36
|
-
}
|
|
37
|
-
onClose(handler) {
|
|
38
|
-
this.closeHandler = handler;
|
|
39
|
-
}
|
|
40
|
-
onError(handler) {
|
|
41
|
-
this.errorHandler = handler;
|
|
42
|
-
}
|
|
43
|
-
buildURL(url) {
|
|
44
|
-
if (url.startsWith("ws://") || url.startsWith("wss://")) {
|
|
45
|
-
return url;
|
|
46
|
-
}
|
|
47
|
-
if (url.startsWith("http://")) {
|
|
48
|
-
url = url.replace("http://", "ws://");
|
|
49
|
-
} else if (url.startsWith("https://")) {
|
|
50
|
-
url = url.replace("https://", "wss://");
|
|
51
|
-
} else if (!url.startsWith("ws://") && !url.startsWith("wss://")) {
|
|
52
|
-
url = `wss://${url}`;
|
|
53
|
-
}
|
|
54
|
-
return url;
|
|
55
|
-
}
|
|
56
|
-
setupEventHandlers() {
|
|
57
|
-
this.ws.onmessage = (event) => {
|
|
58
|
-
if (this.messageHandler) {
|
|
59
|
-
const data = JSON.parse(event.data);
|
|
60
|
-
if (data.done) {
|
|
61
|
-
this.ws.close();
|
|
62
|
-
}
|
|
63
|
-
if (data.success) {
|
|
64
|
-
this.messageHandler(data);
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
if (this.errorHandler) {
|
|
68
|
-
this.errorHandler(event, data);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
this.ws.onopen = (event) => {
|
|
73
|
-
this.flushQueuedMessages();
|
|
74
|
-
if (this.openHandler) {
|
|
75
|
-
this.openHandler(event);
|
|
76
|
-
}
|
|
77
|
-
};
|
|
78
|
-
this.ws.onerror = (event) => {
|
|
79
|
-
if (this.errorHandler) {
|
|
80
|
-
this.errorHandler(event);
|
|
81
|
-
}
|
|
82
|
-
};
|
|
83
|
-
this.ws.onclose = (event) => {
|
|
84
|
-
if (this.closeHandler) {
|
|
85
|
-
this.closeHandler(event);
|
|
86
|
-
}
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
flushQueuedMessages() {
|
|
90
|
-
while (this.queuedMessages.length > 0) {
|
|
91
|
-
const message = this.queuedMessages.shift();
|
|
92
|
-
if (message !== undefined && this.ws.readyState === WebSocket.OPEN) {
|
|
93
|
-
this.ws.send(message);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
export {
|
|
99
|
-
Socket
|
|
100
|
-
};
|
|
2
|
+
class n{url;ws;messageHandler;openHandler;errorHandler;closeHandler;queuedMessages=[];constructor(e){this.url=e;let o=this.buildURL(this.url);this.ws=new WebSocket(o),this.setupEventHandlers()}close(e,o){this.ws.close(e,o)}send(e){let o=JSON.stringify(e);this.sendRaw(o)}sendRaw(e){if(this.ws&&this.ws.readyState===WebSocket.OPEN)this.ws.send(e);else this.queuedMessages.push(e)}onMessage(e){this.messageHandler=e}onOpen(e){this.openHandler=e}onClose(e){this.closeHandler=e}onError(e){this.errorHandler=e}buildURL(e){if(e.startsWith("ws://")||e.startsWith("wss://"))return e;if(e.startsWith("http://"))e=e.replace("http://","ws://");else if(e.startsWith("https://"))e=e.replace("https://","wss://");else if(!e.startsWith("ws://")&&!e.startsWith("wss://"))e=`wss://${e}`;return e}setupEventHandlers(){this.ws.onmessage=(e)=>{if(this.messageHandler){let o=JSON.parse(e.data);if(o.done)this.ws.close();if(o.success){this.messageHandler(o);return}if(this.errorHandler)this.errorHandler(e,o)}},this.ws.onopen=(e)=>{if(this.flushQueuedMessages(),this.openHandler)this.openHandler(e)},this.ws.onerror=(e)=>{if(this.errorHandler)this.errorHandler(e)},this.ws.onclose=(e)=>{if(this.closeHandler)this.closeHandler(e)}}flushQueuedMessages(){while(this.queuedMessages.length>0){let e=this.queuedMessages.shift();if(e!==void 0&&this.ws.readyState===WebSocket.OPEN)this.ws.send(e)}}}export{n as Socket};
|
|
101
3
|
|
|
102
|
-
//# debugId=
|
|
4
|
+
//# debugId=DDC4BBAA6B121D9B64756E2164756E21
|
package/dist/client/index.js.map
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"sourcesContent": [
|
|
5
5
|
"import type { ResponseDataType } from \"@ooneex/http-response\";\nimport type { ISocket, RequestDataType } from \"./types\";\n\nexport class Socket<\n SendData extends RequestDataType = RequestDataType,\n Response extends Record<string, unknown> = Record<string, unknown>,\n> implements ISocket<SendData, Response>\n{\n private ws: WebSocket;\n private messageHandler?: (response: ResponseDataType<Response>) => void;\n private openHandler?: (event: Event) => void;\n private errorHandler?: (event: Event, response?: ResponseDataType<Response>) => void;\n private closeHandler?: (event: CloseEvent) => void;\n private queuedMessages: (string | ArrayBufferLike | Blob | ArrayBufferView)[] = [];\n\n constructor(private readonly url: string) {\n const fullURL = this.buildURL(this.url);\n this.ws = new WebSocket(fullURL);\n this.setupEventHandlers();\n }\n\n public close(code?: number, reason?: string): void {\n this.ws.close(code, reason);\n }\n\n public send(data: SendData): void {\n const text = JSON.stringify(data);\n this.sendRaw(text);\n }\n\n private sendRaw(payload: string | ArrayBufferLike | Blob | ArrayBufferView): void {\n if (this.ws && this.ws.readyState === WebSocket.OPEN) {\n this.ws.send(payload);\n } else {\n this.queuedMessages.push(payload);\n }\n }\n\n public onMessage(handler: (response: ResponseDataType<Response>) => void): void {\n this.messageHandler = handler;\n }\n\n public onOpen(handler: (event: Event) => void): void {\n this.openHandler = handler;\n }\n\n public onClose(handler: (event: CloseEvent) => void): void {\n this.closeHandler = handler;\n }\n\n public onError(handler: (event: Event, response?: ResponseDataType<Response>) => void): void {\n this.errorHandler = handler;\n }\n\n private buildURL(url: string): string {\n if (url.startsWith(\"ws://\") || url.startsWith(\"wss://\")) {\n return url;\n }\n\n // Convert HTTP(S) to WebSocket protocol\n if (url.startsWith(\"http://\")) {\n url = url.replace(\"http://\", \"ws://\");\n } else if (url.startsWith(\"https://\")) {\n url = url.replace(\"https://\", \"wss://\");\n } else if (!url.startsWith(\"ws://\") && !url.startsWith(\"wss://\")) {\n url = `wss://${url}`;\n }\n\n return url;\n }\n\n private setupEventHandlers(): void {\n this.ws.onmessage = (event: MessageEvent) => {\n if (this.messageHandler) {\n const data = JSON.parse(event.data) as ResponseDataType<Response>;\n\n if (data.done) {\n this.ws.close();\n }\n\n if (data.success) {\n this.messageHandler(data);\n\n return;\n }\n\n if (this.errorHandler) {\n this.errorHandler(event, data);\n }\n }\n };\n\n this.ws.onopen = (event: Event) => {\n this.flushQueuedMessages();\n if (this.openHandler) {\n this.openHandler(event);\n }\n };\n\n this.ws.onerror = (event: Event) => {\n if (this.errorHandler) {\n this.errorHandler(event);\n }\n };\n\n this.ws.onclose = (event: CloseEvent) => {\n if (this.closeHandler) {\n this.closeHandler(event);\n }\n };\n }\n\n private flushQueuedMessages(): void {\n while (this.queuedMessages.length > 0) {\n const message = this.queuedMessages.shift();\n if (message !== undefined && this.ws.readyState === WebSocket.OPEN) {\n this.ws.send(message);\n }\n }\n }\n}\n"
|
|
6
6
|
],
|
|
7
|
-
"mappings": "
|
|
8
|
-
"debugId": "
|
|
7
|
+
"mappings": ";AAGO,MAAM,CAIb,CAQ+B,IAPrB,GACA,eACA,YACA,aACA,aACA,eAAwE,CAAC,EAEjF,WAAW,CAAkB,EAAa,CAAb,WAC3B,IAAM,EAAU,KAAK,SAAS,KAAK,GAAG,EACtC,KAAK,GAAK,IAAI,UAAU,CAAO,EAC/B,KAAK,mBAAmB,EAGnB,KAAK,CAAC,EAAe,EAAuB,CACjD,KAAK,GAAG,MAAM,EAAM,CAAM,EAGrB,IAAI,CAAC,EAAsB,CAChC,IAAM,EAAO,KAAK,UAAU,CAAI,EAChC,KAAK,QAAQ,CAAI,EAGX,OAAO,CAAC,EAAkE,CAChF,GAAI,KAAK,IAAM,KAAK,GAAG,aAAe,UAAU,KAC9C,KAAK,GAAG,KAAK,CAAO,EAEpB,UAAK,eAAe,KAAK,CAAO,EAI7B,SAAS,CAAC,EAA+D,CAC9E,KAAK,eAAiB,EAGjB,MAAM,CAAC,EAAuC,CACnD,KAAK,YAAc,EAGd,OAAO,CAAC,EAA4C,CACzD,KAAK,aAAe,EAGf,OAAO,CAAC,EAA8E,CAC3F,KAAK,aAAe,EAGd,QAAQ,CAAC,EAAqB,CACpC,GAAI,EAAI,WAAW,OAAO,GAAK,EAAI,WAAW,QAAQ,EACpD,OAAO,EAIT,GAAI,EAAI,WAAW,SAAS,EAC1B,EAAM,EAAI,QAAQ,UAAW,OAAO,EAC/B,QAAI,EAAI,WAAW,UAAU,EAClC,EAAM,EAAI,QAAQ,WAAY,QAAQ,EACjC,QAAI,CAAC,EAAI,WAAW,OAAO,GAAK,CAAC,EAAI,WAAW,QAAQ,EAC7D,EAAM,SAAS,IAGjB,OAAO,EAGD,kBAAkB,EAAS,CACjC,KAAK,GAAG,UAAY,CAAC,IAAwB,CAC3C,GAAI,KAAK,eAAgB,CACvB,IAAM,EAAO,KAAK,MAAM,EAAM,IAAI,EAElC,GAAI,EAAK,KACP,KAAK,GAAG,MAAM,EAGhB,GAAI,EAAK,QAAS,CAChB,KAAK,eAAe,CAAI,EAExB,OAGF,GAAI,KAAK,aACP,KAAK,aAAa,EAAO,CAAI,IAKnC,KAAK,GAAG,OAAS,CAAC,IAAiB,CAEjC,GADA,KAAK,oBAAoB,EACrB,KAAK,YACP,KAAK,YAAY,CAAK,GAI1B,KAAK,GAAG,QAAU,CAAC,IAAiB,CAClC,GAAI,KAAK,aACP,KAAK,aAAa,CAAK,GAI3B,KAAK,GAAG,QAAU,CAAC,IAAsB,CACvC,GAAI,KAAK,aACP,KAAK,aAAa,CAAK,GAKrB,mBAAmB,EAAS,CAClC,MAAO,KAAK,eAAe,OAAS,EAAG,CACrC,IAAM,EAAU,KAAK,eAAe,MAAM,EAC1C,GAAI,IAAY,QAAa,KAAK,GAAG,aAAe,UAAU,KAC5D,KAAK,GAAG,KAAK,CAAO,GAI5B",
|
|
8
|
+
"debugId": "DDC4BBAA6B121D9B64756E2164756E21",
|
|
9
9
|
"names": []
|
|
10
10
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ooneex/socket",
|
|
3
3
|
"description": "WebSocket server and client implementation for real-time bidirectional communication",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.14.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
7
7
|
"dist",
|
|
@@ -34,10 +34,11 @@
|
|
|
34
34
|
"npm:publish": "bun publish --tolerate-republish --access public"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
|
-
"@ooneex/
|
|
38
|
-
"@ooneex/
|
|
39
|
-
"@ooneex/http-
|
|
40
|
-
"@ooneex/
|
|
37
|
+
"@ooneex/app-env": "0.0.13",
|
|
38
|
+
"@ooneex/controller": "0.12.0",
|
|
39
|
+
"@ooneex/http-request": "0.12.0",
|
|
40
|
+
"@ooneex/http-response": "0.12.0",
|
|
41
|
+
"@ooneex/translation": "0.0.13"
|
|
41
42
|
},
|
|
42
43
|
"keywords": [
|
|
43
44
|
"bun",
|