@ooneex/socket 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Ooneex
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # @ooneex/router
@@ -0,0 +1,43 @@
1
+ import { ResponseDataType as ResponseDataType2 } from "@ooneex/http-response";
2
+ import { ResponseDataType } from "@ooneex/http-response";
3
+ import { LocaleInfoType } from "@ooneex/translation";
4
+ type RequestDataType = {
5
+ payload?: Record<string, unknown>;
6
+ queries?: Record<string, string | number>;
7
+ language?: LocaleInfoType;
8
+ };
9
+ interface ISocket<
10
+ SendData extends RequestDataType = RequestDataType,
11
+ Response extends Record<string, unknown> = Record<string, unknown>
12
+ > {
13
+ close: (code?: number, reason?: string) => void;
14
+ send: (data: SendData) => void;
15
+ onMessage: (handler: (response: ResponseDataType<Response>) => void) => void;
16
+ onOpen: (handler: (event: Event) => void) => void;
17
+ onClose: (handler: (event: CloseEvent) => void) => void;
18
+ onError: (handler: (event: Event) => void) => void;
19
+ }
20
+ declare class Socket<
21
+ SendData extends RequestDataType = RequestDataType,
22
+ Response extends Record<string, unknown> = Record<string, unknown>
23
+ > implements ISocket<SendData, Response> {
24
+ private readonly url;
25
+ private ws;
26
+ private messageHandler?;
27
+ private openHandler?;
28
+ private errorHandler?;
29
+ private closeHandler?;
30
+ private queuedMessages;
31
+ constructor(url: string);
32
+ close(code?: number, reason?: string): void;
33
+ send(data: SendData): void;
34
+ private sendRaw;
35
+ onMessage(handler: (response: ResponseDataType2<Response>) => void): void;
36
+ onOpen(handler: (event: Event) => void): void;
37
+ onClose(handler: (event: CloseEvent) => void): void;
38
+ onError(handler: (event: Event) => void): void;
39
+ private buildURL;
40
+ private setupEventHandlers;
41
+ private flushQueuedMessages;
42
+ }
43
+ export { Socket, RequestDataType, ISocket };
@@ -0,0 +1,4 @@
1
+ // @bun
2
+ class n{url;ws;messageHandler;openHandler;errorHandler;closeHandler;queuedMessages=[];constructor(o){this.url=o;let e=this.buildURL(this.url);this.ws=new WebSocket(e),this.setupEventHandlers()}close(o,e){this.ws.close(o,e)}send(o){let e=JSON.stringify(o);this.sendRaw(e)}sendRaw(o){if(this.ws&&this.ws.readyState===WebSocket.OPEN)this.ws.send(o);else this.queuedMessages.push(o)}onMessage(o){this.messageHandler=o}onOpen(o){this.openHandler=o}onClose(o){this.closeHandler=o}onError(o){this.errorHandler=o}buildURL(o){if(o.startsWith("ws://")||o.startsWith("wss://"))return o;if(o.startsWith("http://"))o=o.replace("http://","ws://");else if(o.startsWith("https://"))o=o.replace("https://","wss://");else if(!o.startsWith("ws://")&&!o.startsWith("wss://"))o=`wss://${o}`;return o}setupEventHandlers(){this.ws.onmessage=(o)=>{if(this.messageHandler){let e=JSON.parse(o.data);if(e.done)this.ws.close();if(e.success){this.messageHandler(e);return}if(this.errorHandler)this.errorHandler(o,e)}},this.ws.onopen=(o)=>{if(this.flushQueuedMessages(),this.openHandler)this.openHandler(o)},this.ws.onerror=(o)=>{if(this.errorHandler)this.errorHandler(o)},this.ws.onclose=(o)=>{if(this.closeHandler)this.closeHandler(o)}}flushQueuedMessages(){while(this.queuedMessages.length>0){let o=this.queuedMessages.shift();if(o!==void 0&&this.ws.readyState===WebSocket.OPEN)this.ws.send(o)}}}export{n as Socket};
3
+
4
+ //# debugId=B1BC1332EB24344564756E2164756E21
@@ -0,0 +1,10 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["src/client/Socket.ts"],
4
+ "sourcesContent": [
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) => 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
+ ],
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,IAA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,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": "B1BC1332EB24344564756E2164756E21",
9
+ "names": []
10
+ }
@@ -0,0 +1,58 @@
1
+ import { IAnalytics } from "@ooneex/analytics";
2
+ import { IAppEnv } from "@ooneex/app-env";
3
+ import { ICache } from "@ooneex/cache";
4
+ import { IDatabase, IRedisDatabaseAdapter } from "@ooneex/database";
5
+ import { Header } from "@ooneex/http-header";
6
+ import { RequestConfigType } from "@ooneex/http-request";
7
+ import { IRequestFile } from "@ooneex/http-request-file";
8
+ import { IResponse } from "@ooneex/http-response";
9
+ import { ILogger, LogsEntity } from "@ooneex/logger";
10
+ import { IMailer } from "@ooneex/mailer";
11
+ import { IPermission } from "@ooneex/permission";
12
+ import { IStorage } from "@ooneex/storage";
13
+ import { LocaleInfoType } from "@ooneex/translation";
14
+ import { ScalarType } from "@ooneex/types";
15
+ import { IUrl } from "@ooneex/url";
16
+ import { IUser } from "@ooneex/user";
17
+ type ControllerClassType = new (...args: any[]) => IController;
18
+ interface IController<T extends ContextConfigType = ContextConfigType> {
19
+ index: (context: ContextType<T>) => Promise<IResponse<T["response"]>> | IResponse<T["response"]>;
20
+ }
21
+ type ContextConfigType = {
22
+ response: Record<string, unknown>;
23
+ } & RequestConfigType;
24
+ type ContextType<T extends ContextConfigType = ContextConfigType> = {
25
+ logger: ILogger<Record<string, ScalarType>> | ILogger<LogsEntity>;
26
+ analytics?: IAnalytics;
27
+ cache?: ICache;
28
+ permission?: IPermission;
29
+ storage?: IStorage;
30
+ database?: IDatabase;
31
+ redis?: IRedisDatabaseAdapter;
32
+ mailer?: IMailer;
33
+ app: {
34
+ url: IAppEnv;
35
+ env: IAppEnv;
36
+ };
37
+ channel: {
38
+ send: (response: IResponse<T["response"]>) => Promise<void>;
39
+ close(code?: number, reason?: string): void;
40
+ subscribe: () => Promise<void>;
41
+ isSubscribed(): boolean;
42
+ unsubscribe: () => Promise<void>;
43
+ publish: (response: IResponse<T["response"]>) => Promise<void>;
44
+ };
45
+ response: IResponse<T["response"]>;
46
+ path: string;
47
+ url: IUrl;
48
+ header: Header;
49
+ params: T["params"];
50
+ payload: T["payload"];
51
+ queries: T["queries"];
52
+ files: Record<string, IRequestFile>;
53
+ ip: string | null;
54
+ host: string;
55
+ language: LocaleInfoType;
56
+ user: IUser | null;
57
+ };
58
+ export { IController, ControllerClassType, ContextType, ContextConfigType };
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ // @bun
2
+
3
+ //# debugId=A14A88B5DA50735F64756E2164756E21
@@ -0,0 +1,9 @@
1
+ {
2
+ "version": 3,
3
+ "sources": [],
4
+ "sourcesContent": [
5
+ ],
6
+ "mappings": "",
7
+ "debugId": "A14A88B5DA50735F64756E2164756E21",
8
+ "names": []
9
+ }
Binary file
package/package.json ADDED
@@ -0,0 +1,59 @@
1
+ {
2
+ "name": "@ooneex/socket",
3
+ "description": "",
4
+ "version": "0.0.1",
5
+ "type": "module",
6
+ "files": [
7
+ "dist",
8
+ "LICENSE",
9
+ "README.md",
10
+ "package.json"
11
+ ],
12
+ "module": "./dist/index.js",
13
+ "types": "./dist/index.d.ts",
14
+ "exports": {
15
+ ".": {
16
+ "import": {
17
+ "types": "./dist/index.d.ts",
18
+ "default": "./dist/index.js"
19
+ }
20
+ },
21
+ "./client": {
22
+ "import": {
23
+ "types": "./dist/client/index.d.ts",
24
+ "default": "./dist/client/index.js"
25
+ }
26
+ },
27
+ "./package.json": "./package.json"
28
+ },
29
+ "license": "MIT",
30
+ "scripts": {
31
+ "test": "bun test tests",
32
+ "build": "bunup",
33
+ "lint": "tsgo --noEmit && bunx biome lint",
34
+ "publish:prod": "bun publish --tolerate-republish --access public",
35
+ "publish:pack": "bun pm pack --destination ./dist",
36
+ "publish:dry": "bun publish --dry-run"
37
+ },
38
+ "devDependencies": {
39
+ "@ooneex/analytics": "0.0.1",
40
+ "@ooneex/cache": "0.0.1",
41
+ "@ooneex/database": "0.0.1",
42
+ "@ooneex/http-header": "0.0.1",
43
+ "@ooneex/http-request": "0.0.1",
44
+ "@ooneex/http-request-file": "0.0.1",
45
+ "@ooneex/http-response": "0.0.1",
46
+ "@ooneex/logger": "0.0.1",
47
+ "@ooneex/mailer": "0.0.1",
48
+ "@ooneex/permission": "0.0.1",
49
+ "@ooneex/storage": "0.0.1",
50
+ "@ooneex/translation": "0.0.1",
51
+ "@ooneex/types": "0.0.1",
52
+ "@ooneex/url": "0.0.1",
53
+ "@ooneex/user": "0.0.1"
54
+ },
55
+ "peerDependencies": {},
56
+ "dependencies": {
57
+ "@ooneex/app-env": "0.0.1"
58
+ }
59
+ }