topsyde-utils 1.0.150 → 1.0.152

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.
Files changed (78) hide show
  1. package/dist/application.d.ts +1 -0
  2. package/dist/application.js +12 -8
  3. package/dist/application.js.map +1 -1
  4. package/dist/client/rxjs/index.js.map +1 -1
  5. package/dist/client/rxjs/rxjs.js.map +1 -1
  6. package/dist/client/rxjs/useRxjs.js.map +1 -1
  7. package/dist/client/vite/plugins/index.js.map +1 -1
  8. package/dist/client/vite/plugins/topsydeUtilsVitePlugin.js.map +1 -1
  9. package/dist/consts.js.map +1 -1
  10. package/dist/enums.js.map +1 -1
  11. package/dist/errors.js.map +1 -1
  12. package/dist/index.d.ts +1 -1
  13. package/dist/index.js +1 -1
  14. package/dist/index.js.map +1 -1
  15. package/dist/initializable.js.map +1 -1
  16. package/dist/server/bun/index.js.map +1 -1
  17. package/dist/server/bun/router/controller-discovery.js.map +1 -1
  18. package/dist/server/bun/router/index.js.map +1 -1
  19. package/dist/server/bun/router/router.internal.js.map +1 -1
  20. package/dist/server/bun/router/router.js.map +1 -1
  21. package/dist/server/bun/router/routes.js.map +1 -1
  22. package/dist/server/bun/websocket/Channel.js.map +1 -1
  23. package/dist/server/bun/websocket/Client.js.map +1 -1
  24. package/dist/server/bun/websocket/Message.js.map +1 -1
  25. package/dist/server/bun/websocket/Websocket.js.map +1 -1
  26. package/dist/server/bun/websocket/index.js.map +1 -1
  27. package/dist/server/bun/websocket/websocket.enums.js.map +1 -1
  28. package/dist/server/bun/websocket/websocket.guards.js.map +1 -1
  29. package/dist/server/bun/websocket/websocket.types.js.map +1 -1
  30. package/dist/server/controller.js.map +1 -1
  31. package/dist/server/index.js.map +1 -1
  32. package/dist/server/service.js.map +1 -1
  33. package/dist/singleton.js.map +1 -1
  34. package/dist/throwable.js.map +1 -1
  35. package/dist/types.js.map +1 -1
  36. package/dist/utils/Console.js.map +1 -1
  37. package/dist/utils/Guards.js.map +1 -1
  38. package/dist/utils/Lib.js.map +1 -1
  39. package/dist/utils/index.js.map +1 -1
  40. package/package.json +5 -3
  41. package/src/__tests__/app.test.ts +205 -0
  42. package/src/__tests__/singleton.test.ts +402 -0
  43. package/src/__tests__/type-inference.test.ts +60 -0
  44. package/src/application.ts +48 -0
  45. package/src/client/rxjs/index.ts +5 -0
  46. package/src/client/rxjs/rxjs.ts +122 -0
  47. package/src/client/rxjs/useRxjs.ts +111 -0
  48. package/src/client/vite/plugins/index.ts +5 -0
  49. package/src/client/vite/plugins/topsydeUtilsVitePlugin.ts +80 -0
  50. package/src/consts.ts +48 -0
  51. package/src/enums.ts +14 -0
  52. package/src/errors.ts +56 -0
  53. package/src/index.ts +81 -0
  54. package/src/initializable.ts +375 -0
  55. package/src/server/bun/index.ts +6 -0
  56. package/src/server/bun/router/controller-discovery.ts +94 -0
  57. package/src/server/bun/router/index.ts +9 -0
  58. package/src/server/bun/router/router.internal.ts +64 -0
  59. package/src/server/bun/router/router.ts +51 -0
  60. package/src/server/bun/router/routes.ts +7 -0
  61. package/src/server/bun/websocket/Channel.ts +157 -0
  62. package/src/server/bun/websocket/Client.ts +129 -0
  63. package/src/server/bun/websocket/Message.ts +106 -0
  64. package/src/server/bun/websocket/Websocket.ts +221 -0
  65. package/src/server/bun/websocket/index.ts +14 -0
  66. package/src/server/bun/websocket/websocket.enums.ts +22 -0
  67. package/src/server/bun/websocket/websocket.guards.ts +6 -0
  68. package/src/server/bun/websocket/websocket.types.ts +186 -0
  69. package/src/server/controller.ts +121 -0
  70. package/src/server/index.ts +7 -0
  71. package/src/server/service.ts +36 -0
  72. package/src/singleton.ts +28 -0
  73. package/src/throwable.ts +87 -0
  74. package/src/types.ts +10 -0
  75. package/src/utils/Console.ts +85 -0
  76. package/src/utils/Guards.ts +61 -0
  77. package/src/utils/Lib.ts +506 -0
  78. package/src/utils/index.ts +9 -0
@@ -0,0 +1,111 @@
1
+ import { Subscription } from "rxjs";
2
+ import { computed, onBeforeUnmount, onMounted, ref } from "vue";
3
+ import { Rxjs, RxjsNamespaces, I_RxjsPayload } from "./rxjs";
4
+ import { Guards, Lib } from "../../utils";
5
+
6
+ export type RxjsDataType = string | Record<string, any>;
7
+ type NamespaceActions = Record<string, Function>;
8
+ type MultiNamespaceActions<T extends string> = Partial<Record<RxjsNamespaces<T>, NamespaceActions>>;
9
+
10
+ export const useRxjs = <T extends string>(
11
+ _namespace: RxjsNamespaces<T> | RxjsNamespaces<T>[],
12
+ actions?: NamespaceActions | MultiNamespaceActions<T>,
13
+ options?: { static_instance: boolean },
14
+ ) => {
15
+ const subs = ref<Map<RxjsNamespaces<T>, Subscription>>(new Map());
16
+ const _actions = ref(actions);
17
+ const instance = Rxjs.GetInstance<Rxjs<T>>();
18
+ const namespaces = ref<RxjsNamespaces<T>[]>(Guards.IsArray(_namespace) ? _namespace : [_namespace]);
19
+
20
+ namespaces.value.forEach((ns) => {
21
+ if (instance.has(ns)) return;
22
+ console.log("Creating namespace", ns);
23
+ instance.create(ns);
24
+ });
25
+
26
+ function _getAction(cta: string, ns: RxjsNamespaces<T>) {
27
+ if (Guards.IsArray(_namespace)) return _actions.value?.[ns]?.[cta];
28
+ return _actions.value?.[cta];
29
+ }
30
+
31
+ function $next(namespace: RxjsNamespaces<T>, payload: I_RxjsPayload<any>): void;
32
+ function $next(cta: string, data: RxjsDataType): void;
33
+ function $next(firstParam: RxjsNamespaces<T> | string, secondParam: I_RxjsPayload<RxjsDataType> | any): void {
34
+ if (typeof firstParam !== "string" && secondParam && typeof secondParam === "object" && "cta" in secondParam && "data" in secondParam) {
35
+ const ns = firstParam;
36
+ const payload: I_RxjsPayload<any> = secondParam;
37
+ instance.next(ns, payload);
38
+ return;
39
+ }
40
+
41
+ const cta = firstParam as string;
42
+ const data = secondParam as RxjsDataType;
43
+ const namespaces = Guards.IsArray(_namespace) ? _namespace : [_namespace];
44
+ namespaces.forEach((ns) => {
45
+ instance.next(ns, { cta, data });
46
+ });
47
+ }
48
+
49
+ function $clear(namespace: RxjsNamespaces<T>) {
50
+ instance.clear(namespace);
51
+ }
52
+
53
+ function $subscribe(actions: NamespaceActions | MultiNamespaceActions<T>) {
54
+ _actions.value = actions;
55
+ $unsubscribe(); // Clear existing subscriptions
56
+
57
+ const namespaces = Guards.IsArray(_namespace) ? _namespace : [_namespace];
58
+
59
+ namespaces.forEach((ns) => {
60
+ if (!instance.namespaces.has(ns)) {
61
+ Lib.Warn(`Rxjs namespace ${ns} does not exist`);
62
+ return;
63
+ }
64
+
65
+ if (subs.value.has(ns)) return;
66
+
67
+ subs.value.set(
68
+ ns,
69
+ instance.subscribe(ns, ({ cta, data }) => {
70
+ const action = _getAction(cta, ns) || (() => {});
71
+ action(data);
72
+ }),
73
+ );
74
+ });
75
+ }
76
+
77
+ function $unsubscribe() {
78
+ subs.value.forEach((sub) => sub.unsubscribe());
79
+ subs.value.clear();
80
+ }
81
+
82
+ function $mount() {
83
+ if (subs.value.size > 0 || !actions) return;
84
+ $subscribe(actions);
85
+ }
86
+
87
+ function $unmount() {
88
+ $unsubscribe();
89
+ }
90
+
91
+ if (!options?.static_instance) {
92
+ onMounted(() => {
93
+ $mount();
94
+ });
95
+ }
96
+
97
+ if (!options?.static_instance) {
98
+ onBeforeUnmount(() => {
99
+ $unmount();
100
+ });
101
+ }
102
+
103
+ return {
104
+ $next,
105
+ $clear,
106
+ $subscribe,
107
+ $unsubscribe,
108
+ $mount,
109
+ $unmount,
110
+ };
111
+ };
@@ -0,0 +1,5 @@
1
+ // This file is auto-generated by scripts/generate-indexes.ts
2
+ // Do not edit this file directly
3
+
4
+ export * from './topsydeUtilsVitePlugin';
5
+ export { default as TopsydeUtilsVitePlugin } from './topsydeUtilsVitePlugin';
@@ -0,0 +1,80 @@
1
+ import type { Plugin } from "vite";
2
+
3
+ /**
4
+ * Creates a Vite plugin that provides compatibility for topsyde-utils in browser environments
5
+ * by disabling sourcemaps and providing mock implementations of Node.js modules.
6
+ *
7
+ * @returns A single Vite plugin with all necessary functionality
8
+ */
9
+ export function TopsydeUtilsVitePlugin(): Plugin {
10
+ return {
11
+ name: "topsyde-utils-compatibility",
12
+
13
+ // Disable sourcemaps for topsyde-utils
14
+ transform(code, id) {
15
+ if (id.includes("node_modules/topsyde-utils")) {
16
+ return {
17
+ code,
18
+ map: { mappings: "" }, // Return empty sourcemap
19
+ };
20
+ }
21
+ },
22
+
23
+ // Handle virtual modules for Node.js built-ins
24
+ resolveId(id) {
25
+ if (id === "virtual:path" || id === "virtual:fs") {
26
+ return id;
27
+ }
28
+ return null;
29
+ },
30
+
31
+ // Provide virtual module implementations
32
+ load(id) {
33
+ if (id === "virtual:path") {
34
+ return `
35
+ export function join() { return ''; }
36
+ export function resolve() { return ''; }
37
+ export function dirname() { return ''; }
38
+ export function basename() { return ''; }
39
+ export function extname() { return ''; }
40
+ export default { join, resolve, dirname, basename, extname };
41
+ `;
42
+ }
43
+ if (id === "virtual:fs") {
44
+ return `
45
+ export function readFileSync() { return ''; }
46
+ export function existsSync() { return false; }
47
+ export function writeFileSync() { return null; }
48
+ export function readdirSync() { return []; }
49
+ export function statSync() {
50
+ return {
51
+ isDirectory: () => false,
52
+ isFile: () => true
53
+ };
54
+ }
55
+ export default { readFileSync, existsSync, writeFileSync, readdirSync, statSync };
56
+ `;
57
+ }
58
+ return null;
59
+ },
60
+
61
+ // Configure aliases and optimization
62
+ config() {
63
+ return {
64
+ resolve: {
65
+ alias: {
66
+ // Alias Node.js built-ins to virtual modules
67
+ path: "virtual:path",
68
+ fs: "virtual:fs",
69
+ },
70
+ },
71
+ optimizeDeps: {
72
+ exclude: ["topsyde-utils"],
73
+ },
74
+ };
75
+ },
76
+ };
77
+ }
78
+
79
+ // Export as default for compatibility with different import styles
80
+ export default TopsydeUtilsVitePlugin;
package/src/consts.ts ADDED
@@ -0,0 +1,48 @@
1
+ export const DEFAULT_FALSE_RESPONSE = "Something went wrong. Please try again later.";
2
+
3
+ export const LOG_COLORS = {
4
+ reset: "\x1b[0m",
5
+ bright: "\x1b[1m",
6
+ dim: "\x1b[2m",
7
+ underscore: "\x1b[4m",
8
+ blink: "\x1b[5m",
9
+ reverse: "\x1b[7m",
10
+ hidden: "\x1b[8m",
11
+
12
+ text: {
13
+ black: "\x1b[30m",
14
+ red: "\x1b[31m",
15
+ green: "\x1b[32m",
16
+ yellow: "\x1b[33m",
17
+ blue: "\x1b[34m",
18
+ magenta: "\x1b[35m",
19
+ cyan: "\x1b[36m",
20
+ white: "\x1b[37m",
21
+ },
22
+ bg: {
23
+ black: "\x1b[40m",
24
+ red: "\x1b[41m",
25
+ green: "\x1b[42m",
26
+ yellow: "\x1b[43m",
27
+ blue: "\x1b[44m",
28
+ magenta: "\x1b[45m",
29
+ cyan: "\x1b[46m",
30
+ white: "\x1b[47m",
31
+ },
32
+ };
33
+
34
+ export const LOG_ICONS = {
35
+ info: 'ℹ️',
36
+ success: '✅',
37
+ warning: '⚠️',
38
+ error: '❌',
39
+ debug: '🔍',
40
+ arrow: '➜',
41
+ bullet: '•',
42
+ star: '★',
43
+ heart: '❤️',
44
+ rocket: '🚀',
45
+ fire: '🔥',
46
+ dice: '🎲',
47
+ bunny: '🐰',
48
+ };
package/src/enums.ts ADDED
@@ -0,0 +1,14 @@
1
+ export enum E_IS {
2
+ ARRAY = "array",
3
+ OBJECT = "object",
4
+ FUNCTION = "function",
5
+ STRING = "string",
6
+ NUMBER = "number",
7
+ BOOLEAN = "boolean",
8
+ REGEX = "regex",
9
+ }
10
+
11
+ export enum E_ENVIRONMENTS {
12
+ DEV = "development",
13
+ PROD = "production",
14
+ }
package/src/errors.ts ADDED
@@ -0,0 +1,56 @@
1
+ // Error code enums
2
+ export enum ERROR_CODE {
3
+ NO_REQUEST = -1,
4
+ UNKNOWN_ERROR = 0,
5
+ INVALID_METHOD = 1,
6
+ INVALID_REQUEST = 2,
7
+ INVALID_CONTROLLER = 3,
8
+ INVALID_ACTION = 4,
9
+ NO_ACTION_IN_MAP = 5,
10
+ NO_METHOD_HANDLER = 6,
11
+ INVALID_METHOD_INPUT = 7,
12
+ REQ_BODY_EMPTY = 8,
13
+ }
14
+
15
+ export enum HTTP_ERROR_CODE {
16
+ OK = 200,
17
+ BAD_REQUEST = 400,
18
+ UNAUTHORIZED = 401,
19
+ FORBIDDEN = 403,
20
+ NOT_FOUND = 404,
21
+ INTERNAL_SERVER_ERROR = 500,
22
+ }
23
+
24
+ /**
25
+ * WebSocket close event codes
26
+ *
27
+ * Standard codes (1000-1999):
28
+ * - 1000-1015: Reserved for the WebSocket protocol
29
+ *
30
+ * Application-specific codes:
31
+ * - 4000-4999: Reserved for application use
32
+ */
33
+ export enum WS_ERROR_CODE {
34
+ // Standard WebSocket close codes (1000-1015)
35
+ NORMAL_CLOSURE = 1000, // Normal closure; the connection successfully completed whatever purpose for which it was created
36
+ GOING_AWAY = 1001, // The endpoint is going away (server shutdown or browser navigating away)
37
+ PROTOCOL_ERROR = 1002, // Protocol error
38
+ UNSUPPORTED_DATA = 1003, // Received data of a type it cannot accept (e.g., text-only endpoint received binary data)
39
+ NO_STATUS_RECEIVED = 1005, // No status code was provided even though one was expected
40
+ ABNORMAL_CLOSURE = 1006, // Connection was closed abnormally (e.g., without sending or receiving a Close control frame)
41
+ INVALID_FRAME_PAYLOAD_DATA = 1007, // Received data that was not consistent with the type of the message
42
+ POLICY_VIOLATION = 1008, // Policy violation
43
+ MESSAGE_TOO_BIG = 1009, // Message was too big and was rejected
44
+ MISSING_EXTENSION = 1010, // Client expected the server to negotiate one or more extension, but server didn't
45
+ INTERNAL_ERROR = 1011, // Server encountered an unexpected condition that prevented it from fulfilling the request
46
+ SERVICE_RESTART = 1012, // Server is restarting
47
+ TRY_AGAIN_LATER = 1013, // Server is too busy or the client is rate-limited
48
+ BAD_GATEWAY = 1014, // Gateway or proxy received an invalid response from the upstream server
49
+ TLS_HANDSHAKE = 1015, // TLS handshake failure
50
+
51
+ // Application-specific codes (4000-4999)
52
+ // Add your application-specific codes here as needed
53
+ // Example:
54
+ APP_AUTHENTICATION_FAILED = 4000,
55
+ APP_INVALID_MESSAGE_FORMAT = 4001,
56
+ }
package/src/index.ts ADDED
@@ -0,0 +1,81 @@
1
+ // This file is auto-generated by scripts/generate-indexes.ts
2
+ // Do not edit this file directly
3
+
4
+ // Export all modules
5
+ export * from "./errors";
6
+ export * from "./singleton";
7
+ export * from "./initializable";
8
+ export * from "./application";
9
+ export * from "./consts";
10
+ export * from "./types";
11
+ export * from "./throwable";
12
+ export * from "./enums";
13
+ export * from "./client/rxjs/rxjs";
14
+ export * from "./client/rxjs/useRxjs";
15
+ export * from "./client/vite/plugins/topsydeUtilsVitePlugin";
16
+ export * from "./server/controller";
17
+ export * from "./server/service";
18
+ export * from "./server/bun/router/controller-discovery";
19
+ export * from "./server/bun/router/routes";
20
+ export * from "./server/bun/router/router";
21
+ export * from "./server/bun/router/router.internal";
22
+ export * from "./server/bun/websocket/websocket.guards";
23
+ export * from "./server/bun/websocket/Client";
24
+ export * from "./server/bun/websocket/Websocket";
25
+ export * from "./server/bun/websocket/websocket.enums";
26
+ export * from "./server/bun/websocket/websocket.types";
27
+ export * from "./server/bun/websocket/Message";
28
+ export * from "./server/bun/websocket/Channel";
29
+ export * from "./utils/Guards";
30
+ export * from "./utils/Lib";
31
+ export * from "./utils/Console";
32
+
33
+ // Export default classes
34
+ export { default as Singleton } from "./singleton";
35
+ export { default as Initializable } from "./initializable";
36
+ export { default as Application } from "./application";
37
+ export { default as Throwable } from "./throwable";
38
+ export { default as TopsydeUtilsVitePlugin } from "./client/vite/plugins/topsydeUtilsVitePlugin";
39
+ export { default as Controller } from "./server/controller";
40
+ export { default as Service } from "./server/service";
41
+ export { default as Router } from "./server/bun/router/router";
42
+ export { default as Router_Internal } from "./server/bun/router/router.internal";
43
+ export { default as Client } from "./server/bun/websocket/Client";
44
+ export { default as Websocket } from "./server/bun/websocket/Websocket";
45
+ export { default as Message } from "./server/bun/websocket/Message";
46
+ export { default as Channel } from "./server/bun/websocket/Channel";
47
+ export { default as Guards } from "./utils/Guards";
48
+ export { default as Lib } from "./utils/Lib";
49
+ export { default as Console } from "./utils/Console";
50
+
51
+ // Re-export specific items for backward compatibility
52
+ export { ERROR_CODE, HTTP_ERROR_CODE, WS_ERROR_CODE } from "./errors";
53
+ export { InitializableOptions, InitializableEvent } from "./initializable";
54
+ export { RESPONSE_INIT, HEADERS_INIT, RESPONSE_METHOD_OPTIONS } from "./application";
55
+ export { DEFAULT_FALSE_RESPONSE, LOG_COLORS, LOG_ICONS } from "./consts";
56
+ export { ClassConstructor, NonNullableType, ObjectKeys, KVObj, I_ApplicationResponse } from "./types";
57
+ export { E_IS, E_ENVIRONMENTS } from "./enums";
58
+ export { E_SUBJET_TYPE, I_RxjsPayload, RxjsNamespaces } from "./client/rxjs/rxjs";
59
+ export { RxjsDataType } from "./client/rxjs/useRxjs";
60
+ export { ControllerResponse, ControllerAction, ControllerMap, ControllerOptions } from "./server/controller";
61
+ export { Routes } from "./server/bun/router/routes";
62
+ export { WebsocketConstructorOptions, I_WebsocketConstructor } from "./server/bun/websocket/Websocket";
63
+ export { E_WebsocketMessageType, E_WebsocketMessagePriority } from "./server/bun/websocket/websocket.enums";
64
+ export {
65
+ BunWebsocketMessage,
66
+ WebsocketChannel,
67
+ WebsocketClients,
68
+ WebsocketMessageOptions,
69
+ WebsocketMessage,
70
+ WebsocketStructuredMessage,
71
+ WebsocketEntityId,
72
+ WebsocketEntityName,
73
+ WebsocketEntityData,
74
+ I_WebsocketEntity,
75
+ I_WebsocketClient,
76
+ I_WebsocketChannelEntity,
77
+ BroadcastOptions,
78
+ I_WebsocketChannel,
79
+ WebsocketInterfaceHandlers,
80
+ I_WebsocketInterface,
81
+ } from "./server/bun/websocket/websocket.types";