@adaas/a-utils 0.1.10 → 0.1.11

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 (116) hide show
  1. package/dist/index.d.mts +914 -0
  2. package/dist/index.d.ts +914 -27
  3. package/dist/index.js +1760 -90
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.mjs +1738 -0
  6. package/dist/index.mjs.map +1 -0
  7. package/package.json +5 -3
  8. package/src/index.ts +70 -0
  9. package/src/lib/A-Logger/A-Logger.component.ts +10 -3
  10. package/src/lib/A-Polyfill/A-Polyfill.component.ts +153 -15
  11. package/src/lib/A-Polyfill/A-Polyfill.types.ts +54 -0
  12. package/src/lib/A-Polyfill/classes/A-Buffer-Polyfill.class.ts +85 -0
  13. package/src/lib/A-Polyfill/classes/A-Crypto-Polyfill.class.ts +87 -0
  14. package/src/lib/A-Polyfill/classes/A-FS-Polyfill.class.ts +61 -0
  15. package/src/lib/A-Polyfill/classes/A-Http-Polyfill.class.ts +100 -0
  16. package/src/lib/A-Polyfill/classes/A-Https-Polyfill.class.ts +100 -0
  17. package/src/lib/A-Polyfill/classes/A-Path-Polyfill.class.ts +97 -0
  18. package/src/lib/A-Polyfill/classes/A-Process-Polyfill.class.ts +73 -0
  19. package/src/lib/A-Polyfill/classes/A-Url-Polyfill.class.ts +88 -0
  20. package/tests/A-Config.test.ts +5 -5
  21. package/tests/A-Polyfill.test.ts +446 -29
  22. package/tsconfig.json +1 -1
  23. package/tsup.config.ts +15 -0
  24. package/dist/src/constants/errors.constants.d.ts +0 -0
  25. package/dist/src/constants/errors.constants.js +0 -2
  26. package/dist/src/constants/errors.constants.js.map +0 -1
  27. package/dist/src/lib/A-Channel/A-Channel.component.d.ts +0 -22
  28. package/dist/src/lib/A-Channel/A-Channel.component.js +0 -84
  29. package/dist/src/lib/A-Channel/A-Channel.component.js.map +0 -1
  30. package/dist/src/lib/A-Channel/A-Channel.error.d.ts +0 -4
  31. package/dist/src/lib/A-Channel/A-Channel.error.js +0 -9
  32. package/dist/src/lib/A-Channel/A-Channel.error.js.map +0 -1
  33. package/dist/src/lib/A-Channel/A-Channel.types.d.ts +0 -0
  34. package/dist/src/lib/A-Channel/A-Channel.types.js +0 -2
  35. package/dist/src/lib/A-Channel/A-Channel.types.js.map +0 -1
  36. package/dist/src/lib/A-Command/A-Command.constants.d.ts +0 -22
  37. package/dist/src/lib/A-Command/A-Command.constants.js +0 -28
  38. package/dist/src/lib/A-Command/A-Command.constants.js.map +0 -1
  39. package/dist/src/lib/A-Command/A-Command.entity.d.ts +0 -133
  40. package/dist/src/lib/A-Command/A-Command.entity.js +0 -273
  41. package/dist/src/lib/A-Command/A-Command.entity.js.map +0 -1
  42. package/dist/src/lib/A-Command/A-Command.error.d.ts +0 -3
  43. package/dist/src/lib/A-Command/A-Command.error.js +0 -8
  44. package/dist/src/lib/A-Command/A-Command.error.js.map +0 -1
  45. package/dist/src/lib/A-Command/A-Command.types.d.ts +0 -73
  46. package/dist/src/lib/A-Command/A-Command.types.js +0 -4
  47. package/dist/src/lib/A-Command/A-Command.types.js.map +0 -1
  48. package/dist/src/lib/A-Config/A-Config.constants.d.ts +0 -3
  49. package/dist/src/lib/A-Config/A-Config.constants.js +0 -6
  50. package/dist/src/lib/A-Config/A-Config.constants.js.map +0 -1
  51. package/dist/src/lib/A-Config/A-Config.container.d.ts +0 -6
  52. package/dist/src/lib/A-Config/A-Config.container.js +0 -77
  53. package/dist/src/lib/A-Config/A-Config.container.js.map +0 -1
  54. package/dist/src/lib/A-Config/A-Config.context.d.ts +0 -29
  55. package/dist/src/lib/A-Config/A-Config.context.js +0 -63
  56. package/dist/src/lib/A-Config/A-Config.context.js.map +0 -1
  57. package/dist/src/lib/A-Config/A-Config.error.d.ts +0 -4
  58. package/dist/src/lib/A-Config/A-Config.error.js +0 -9
  59. package/dist/src/lib/A-Config/A-Config.error.js.map +0 -1
  60. package/dist/src/lib/A-Config/A-Config.types.d.ts +0 -19
  61. package/dist/src/lib/A-Config/A-Config.types.js +0 -7
  62. package/dist/src/lib/A-Config/A-Config.types.js.map +0 -1
  63. package/dist/src/lib/A-Config/components/ConfigReader.component.d.ts +0 -30
  64. package/dist/src/lib/A-Config/components/ConfigReader.component.js +0 -103
  65. package/dist/src/lib/A-Config/components/ConfigReader.component.js.map +0 -1
  66. package/dist/src/lib/A-Config/components/ENVConfigReader.component.d.ts +0 -14
  67. package/dist/src/lib/A-Config/components/ENVConfigReader.component.js +0 -72
  68. package/dist/src/lib/A-Config/components/ENVConfigReader.component.js.map +0 -1
  69. package/dist/src/lib/A-Config/components/FileConfigReader.component.d.ts +0 -11
  70. package/dist/src/lib/A-Config/components/FileConfigReader.component.js +0 -47
  71. package/dist/src/lib/A-Config/components/FileConfigReader.component.js.map +0 -1
  72. package/dist/src/lib/A-Logger/A-Logger.component.d.ts +0 -29
  73. package/dist/src/lib/A-Logger/A-Logger.component.js +0 -152
  74. package/dist/src/lib/A-Logger/A-Logger.component.js.map +0 -1
  75. package/dist/src/lib/A-Logger/A-Logger.types.d.ts +0 -0
  76. package/dist/src/lib/A-Logger/A-Logger.types.js +0 -2
  77. package/dist/src/lib/A-Logger/A-Logger.types.js.map +0 -1
  78. package/dist/src/lib/A-Manifest/A-Manifest.context.d.ts +0 -52
  79. package/dist/src/lib/A-Manifest/A-Manifest.context.js +0 -154
  80. package/dist/src/lib/A-Manifest/A-Manifest.context.js.map +0 -1
  81. package/dist/src/lib/A-Manifest/A-Manifest.error.d.ts +0 -4
  82. package/dist/src/lib/A-Manifest/A-Manifest.error.js +0 -9
  83. package/dist/src/lib/A-Manifest/A-Manifest.error.js.map +0 -1
  84. package/dist/src/lib/A-Manifest/A-Manifest.types.d.ts +0 -43
  85. package/dist/src/lib/A-Manifest/A-Manifest.types.js +0 -3
  86. package/dist/src/lib/A-Manifest/A-Manifest.types.js.map +0 -1
  87. package/dist/src/lib/A-Manifest/classes/A-ManifestChecker.class.d.ts +0 -14
  88. package/dist/src/lib/A-Manifest/classes/A-ManifestChecker.class.js +0 -24
  89. package/dist/src/lib/A-Manifest/classes/A-ManifestChecker.class.js.map +0 -1
  90. package/dist/src/lib/A-Memory/A-Memory.context.d.ts +0 -64
  91. package/dist/src/lib/A-Memory/A-Memory.context.js +0 -105
  92. package/dist/src/lib/A-Memory/A-Memory.context.js.map +0 -1
  93. package/dist/src/lib/A-Polyfill/A-Polyfill.component.d.ts +0 -20
  94. package/dist/src/lib/A-Polyfill/A-Polyfill.component.js +0 -53
  95. package/dist/src/lib/A-Polyfill/A-Polyfill.component.js.map +0 -1
  96. package/dist/src/lib/A-Polyfill/A-Polyfill.types.d.ts +0 -9
  97. package/dist/src/lib/A-Polyfill/A-Polyfill.types.js +0 -3
  98. package/dist/src/lib/A-Polyfill/A-Polyfill.types.js.map +0 -1
  99. package/dist/src/lib/A-Polyfill/A-Polyfills.class.d.ts +0 -11
  100. package/dist/src/lib/A-Polyfill/A-Polyfills.class.js +0 -123
  101. package/dist/src/lib/A-Polyfill/A-Polyfills.class.js.map +0 -1
  102. package/dist/src/lib/A-Schedule/A-Deferred.class.d.ts +0 -12
  103. package/dist/src/lib/A-Schedule/A-Deferred.class.js +0 -23
  104. package/dist/src/lib/A-Schedule/A-Deferred.class.js.map +0 -1
  105. package/dist/src/lib/A-Schedule/A-Schedule.component.d.ts +0 -57
  106. package/dist/src/lib/A-Schedule/A-Schedule.component.js +0 -49
  107. package/dist/src/lib/A-Schedule/A-Schedule.component.js.map +0 -1
  108. package/dist/src/lib/A-Schedule/A-Schedule.types.d.ts +0 -10
  109. package/dist/src/lib/A-Schedule/A-Schedule.types.js +0 -3
  110. package/dist/src/lib/A-Schedule/A-Schedule.types.js.map +0 -1
  111. package/dist/src/lib/A-Schedule/A-ScheduleObject.class.d.ts +0 -29
  112. package/dist/src/lib/A-Schedule/A-ScheduleObject.class.js +0 -58
  113. package/dist/src/lib/A-Schedule/A-ScheduleObject.class.js.map +0 -1
  114. package/index.ts +0 -71
  115. package/src/lib/A-Polyfill/A-Polyfills.class.ts +0 -99
  116. package/tsconfig.build.json +0 -56
@@ -1,20 +1,90 @@
1
- import { A_Component } from "@adaas/a-concept";
2
- import { A_PolyfillClass } from "./A-Polyfills.class";
1
+ import { A_Component, A_Concept, A_Context, A_Inject } from "@adaas/a-concept";
2
+ import { A_Logger } from "../A-Logger/A-Logger.component";
3
+ import { A_FSPolyfillClass } from "./classes/A-FS-Polyfill.class";
4
+ import { A_CryptoPolyfillClass } from "./classes/A-Crypto-Polyfill.class";
5
+ import { A_HttpPolyfillClass } from "./classes/A-Http-Polyfill.class";
6
+ import { A_HttpsPolyfillClass } from "./classes/A-Https-Polyfill.class";
7
+ import { A_PathPolyfillClass } from "./classes/A-Path-Polyfill.class";
8
+ import { A_UrlPolyfillClass } from "./classes/A-Url-Polyfill.class";
9
+ import { A_BufferPolyfillClass } from "./classes/A-Buffer-Polyfill.class";
10
+ import { A_ProcessPolyfillClass } from "./classes/A-Process-Polyfill.class";
3
11
 
12
+ export class A_Polyfill extends A_Component {
4
13
 
14
+ protected _fsPolyfill!: A_FSPolyfillClass;
15
+ protected _cryptoPolyfill!: A_CryptoPolyfillClass;
16
+ protected _httpPolyfill!: A_HttpPolyfillClass;
17
+ protected _httpsPolyfill!: A_HttpsPolyfillClass;
18
+ protected _pathPolyfill!: A_PathPolyfillClass;
19
+ protected _urlPolyfill!: A_UrlPolyfillClass;
20
+ protected _bufferPolyfill!: A_BufferPolyfillClass;
21
+ protected _processPolyfill!: A_ProcessPolyfillClass;
5
22
 
6
- export class A_Polyfill extends A_Component {
23
+ protected _initializing: Promise<void> | null = null;
24
+ /**
25
+ * Indicates whether the channel is connected
26
+ */
27
+ protected _initialized?: Promise<void>;
7
28
 
8
- protected _polyfill!: A_PolyfillClass;
9
29
 
30
+ constructor(
31
+ @A_Inject(A_Logger) protected logger: A_Logger
32
+ ) {
33
+ super();
34
+ }
35
+
36
+ /**
37
+ * Indicates whether the channel is connected
38
+ */
39
+ get ready(): Promise<void> {
40
+ if (!this._initialized) {
41
+ this._initialized = this._loadInternal();
42
+ }
43
+ return this._initialized;
44
+ }
45
+
46
+
47
+
48
+ @A_Concept.Load()
10
49
  async load() {
11
- this._polyfill = new A_PolyfillClass();
50
+ await this.ready;
51
+ }
52
+
53
+ @A_Concept.Load()
54
+ async attachToWindow() {
55
+ if (A_Context.environment !== 'browser') return;
56
+
57
+
58
+ (globalThis as any).A_Polyfill = this;
59
+ // attach env variables
60
+ (globalThis as any).process = { env: { NODE_ENV: 'production' }, cwd: () => "/" };
61
+ (globalThis as any).__dirname = "/";
12
62
 
13
- await this._polyfill.fs();
14
- await this._polyfill.crypto();
15
63
  }
16
64
 
17
65
 
66
+ protected async _loadInternal() {
67
+
68
+ this._fsPolyfill = new A_FSPolyfillClass(this.logger);
69
+ this._cryptoPolyfill = new A_CryptoPolyfillClass(this.logger);
70
+ this._httpPolyfill = new A_HttpPolyfillClass(this.logger);
71
+ this._httpsPolyfill = new A_HttpsPolyfillClass(this.logger);
72
+ this._pathPolyfill = new A_PathPolyfillClass(this.logger);
73
+ this._urlPolyfill = new A_UrlPolyfillClass(this.logger);
74
+ this._bufferPolyfill = new A_BufferPolyfillClass(this.logger);
75
+ this._processPolyfill = new A_ProcessPolyfillClass(this.logger);
76
+
77
+ // Initialize all polyfills to ensure they're ready
78
+ await this._fsPolyfill.get();
79
+ await this._cryptoPolyfill.get(await this._fsPolyfill.get());
80
+ await this._httpPolyfill.get();
81
+ await this._httpsPolyfill.get();
82
+ await this._pathPolyfill.get();
83
+ await this._urlPolyfill.get();
84
+ await this._bufferPolyfill.get();
85
+ await this._processPolyfill.get();
86
+ }
87
+
18
88
  /**
19
89
  * Allows to use the 'fs' polyfill methods regardless of the environment
20
90
  * This method loads the 'fs' polyfill and returns its instance
@@ -22,11 +92,9 @@ export class A_Polyfill extends A_Component {
22
92
  * @returns
23
93
  */
24
94
  async fs() {
25
- if (!this._polyfill) {
26
- await this.load();
27
- }
95
+ await this.ready;
28
96
 
29
- return await this._polyfill.fs();
97
+ return await this._fsPolyfill.get();
30
98
  }
31
99
 
32
100
  /**
@@ -36,10 +104,80 @@ export class A_Polyfill extends A_Component {
36
104
  * @returns
37
105
  */
38
106
  async crypto() {
39
- if (!this._polyfill) {
40
- await this.load();
41
- }
107
+ await this.ready;
108
+
109
+ return await this._cryptoPolyfill.get();
110
+ }
111
+
112
+ /**
113
+ * Allows to use the 'http' polyfill methods regardless of the environment
114
+ * This method loads the 'http' polyfill and returns its instance
115
+ *
116
+ * @returns
117
+ */
118
+ async http() {
119
+ await this.ready;
120
+
121
+ return await this._httpPolyfill.get();
122
+ }
123
+
124
+ /**
125
+ * Allows to use the 'https' polyfill methods regardless of the environment
126
+ * This method loads the 'https' polyfill and returns its instance
127
+ *
128
+ * @returns
129
+ */
130
+ async https() {
131
+ await this.ready;
132
+
133
+ return await this._httpsPolyfill.get();
134
+ }
135
+
136
+ /**
137
+ * Allows to use the 'path' polyfill methods regardless of the environment
138
+ * This method loads the 'path' polyfill and returns its instance
139
+ *
140
+ * @returns
141
+ */
142
+ async path() {
143
+ await this.ready;
144
+
145
+ return await this._pathPolyfill.get();
146
+ }
147
+
148
+ /**
149
+ * Allows to use the 'url' polyfill methods regardless of the environment
150
+ * This method loads the 'url' polyfill and returns its instance
151
+ *
152
+ * @returns
153
+ */
154
+ async url() {
155
+ await this.ready;
156
+
157
+ return await this._urlPolyfill.get();
158
+ }
159
+
160
+ /**
161
+ * Allows to use the 'buffer' polyfill methods regardless of the environment
162
+ * This method loads the 'buffer' polyfill and returns its instance
163
+ *
164
+ * @returns
165
+ */
166
+ async buffer() {
167
+ await this.ready;
168
+
169
+ return await this._bufferPolyfill.get();
170
+ }
171
+
172
+ /**
173
+ * Allows to use the 'process' polyfill methods regardless of the environment
174
+ * This method loads the 'process' polyfill and returns its instance
175
+ *
176
+ * @returns
177
+ */
178
+ async process() {
179
+ await this.ready;
42
180
 
43
- return await this._polyfill.crypto();
181
+ return await this._processPolyfill.get();
44
182
  }
45
183
  }
@@ -7,4 +7,58 @@ export interface Ifspolyfill {
7
7
  export interface IcryptoInterface {
8
8
  createTextHash(text: string, algorithm: string): Promise<string>
9
9
  createFileHash(filePath: string, algorithm: string): Promise<string>
10
+ }
11
+
12
+ export interface IhttpInterface {
13
+ request: (options: any, callback?: (res: any) => void) => any;
14
+ get: (url: string | any, callback?: (res: any) => void) => any;
15
+ createServer: (requestListener?: (req: any, res: any) => void) => any;
16
+ }
17
+
18
+ export interface IhttpsInterface {
19
+ request: (options: any, callback?: (res: any) => void) => any;
20
+ get: (url: string | any, callback?: (res: any) => void) => any;
21
+ createServer: (options: any, requestListener?: (req: any, res: any) => void) => any;
22
+ }
23
+
24
+ export interface IpathInterface {
25
+ join: (...paths: string[]) => string;
26
+ resolve: (...paths: string[]) => string;
27
+ dirname: (path: string) => string;
28
+ basename: (path: string, ext?: string) => string;
29
+ extname: (path: string) => string;
30
+ relative: (from: string, to: string) => string;
31
+ normalize: (path: string) => string;
32
+ isAbsolute: (path: string) => boolean;
33
+ parse: (path: string) => any;
34
+ format: (pathObject: any) => string;
35
+ sep: string;
36
+ delimiter: string;
37
+ }
38
+
39
+ export interface IurlInterface {
40
+ parse: (urlString: string) => any;
41
+ format: (urlObject: any) => string;
42
+ resolve: (from: string, to: string) => string;
43
+ URL: typeof URL;
44
+ URLSearchParams: typeof URLSearchParams;
45
+ }
46
+
47
+ export interface IbufferInterface {
48
+ from: (data: any, encoding?: string) => any;
49
+ alloc: (size: number, fill?: any) => any;
50
+ allocUnsafe: (size: number) => any;
51
+ isBuffer: (obj: any) => boolean;
52
+ concat: (list: any[], totalLength?: number) => any;
53
+ }
54
+
55
+ export interface IprocessInterface {
56
+ env: Record<string, string | undefined>;
57
+ argv: string[];
58
+ platform: string;
59
+ version: string;
60
+ versions: Record<string, string>;
61
+ cwd: () => string;
62
+ exit: (code?: number) => never;
63
+ nextTick: (callback: Function, ...args: any[]) => void;
10
64
  }
@@ -0,0 +1,85 @@
1
+ import { A_Context } from "@adaas/a-concept";
2
+ import { IbufferInterface } from "../A-Polyfill.types";
3
+ import { A_Logger } from "../../A-Logger/A-Logger.component";
4
+
5
+ export class A_BufferPolyfillClass {
6
+ private _buffer!: IbufferInterface;
7
+ private _initialized: boolean = false;
8
+
9
+ constructor(
10
+ protected logger: A_Logger
11
+ ) {
12
+
13
+ }
14
+
15
+ get isInitialized(): boolean {
16
+ return this._initialized;
17
+ }
18
+
19
+
20
+ async get(): Promise<IbufferInterface> {
21
+ if (!this._initialized) {
22
+ await this.init();
23
+ }
24
+ return this._buffer;
25
+ }
26
+
27
+ private async init(): Promise<void> {
28
+ try {
29
+ if (A_Context.environment === 'server') {
30
+ await this.initServer();
31
+ } else {
32
+ this.initBrowser();
33
+ }
34
+ this._initialized = true;
35
+ } catch (error) {
36
+ this.initBrowser();
37
+ this._initialized = true;
38
+ }
39
+ }
40
+
41
+ private async initServer(): Promise<void> {
42
+ const bufferModule = await import('buffer');
43
+ this._buffer = {
44
+ from: bufferModule.Buffer.from,
45
+ alloc: bufferModule.Buffer.alloc,
46
+ allocUnsafe: bufferModule.Buffer.allocUnsafe,
47
+ isBuffer: bufferModule.Buffer.isBuffer,
48
+ concat: bufferModule.Buffer.concat
49
+ };
50
+ }
51
+
52
+ private initBrowser(): void {
53
+ this._buffer = {
54
+ from: (data: any, encoding?: string) => {
55
+ if (typeof data === 'string') {
56
+ return new TextEncoder().encode(data);
57
+ }
58
+ return new Uint8Array(data);
59
+ },
60
+ alloc: (size: number, fill?: any) => {
61
+ const buffer = new Uint8Array(size);
62
+ if (fill !== undefined) {
63
+ buffer.fill(fill);
64
+ }
65
+ return buffer;
66
+ },
67
+ allocUnsafe: (size: number) => {
68
+ return new Uint8Array(size);
69
+ },
70
+ isBuffer: (obj: any) => {
71
+ return obj instanceof Uint8Array || obj instanceof ArrayBuffer;
72
+ },
73
+ concat: (list: any[], totalLength?: number) => {
74
+ const length = totalLength || list.reduce((sum, buf) => sum + buf.length, 0);
75
+ const result = new Uint8Array(length);
76
+ let offset = 0;
77
+ for (const buf of list) {
78
+ result.set(buf, offset);
79
+ offset += buf.length;
80
+ }
81
+ return result;
82
+ }
83
+ };
84
+ }
85
+ }
@@ -0,0 +1,87 @@
1
+ import { A_Context } from "@adaas/a-concept";
2
+ import { IcryptoInterface, Ifspolyfill } from "../A-Polyfill.types";
3
+ import { A_Logger } from "../../A-Logger/A-Logger.component";
4
+
5
+ export class A_CryptoPolyfillClass {
6
+ private _crypto!: IcryptoInterface;
7
+ private _initialized: boolean = false;
8
+ private _fsPolyfill?: Ifspolyfill;
9
+
10
+ constructor(
11
+ protected logger: A_Logger
12
+ ) {
13
+ }
14
+
15
+ get isInitialized(): boolean {
16
+ return this._initialized;
17
+ }
18
+
19
+ async get(fsPolyfill?: Ifspolyfill): Promise<IcryptoInterface> {
20
+ if (!this._initialized) {
21
+ this._fsPolyfill = fsPolyfill;
22
+ await this.init();
23
+ }
24
+ return this._crypto;
25
+ }
26
+
27
+ private async init(): Promise<void> {
28
+ try {
29
+ if (A_Context.environment === 'server') {
30
+ await this.initServer();
31
+ } else {
32
+ this.initBrowser();
33
+ }
34
+ this._initialized = true;
35
+ } catch (error) {
36
+ this.initBrowser();
37
+ this._initialized = true;
38
+ }
39
+ }
40
+
41
+ private async initServer(): Promise<void> {
42
+ const crypto = await import('crypto');
43
+ this._crypto = {
44
+ createTextHash: (text: string, algorithm: string = 'sha384') => Promise.resolve(
45
+ `${algorithm}-${crypto.createHash(algorithm).update(text).digest('base64')}`
46
+ ),
47
+ createFileHash: (filePath: string, algorithm: string = 'sha384') => new Promise(async (resolve, reject) => {
48
+ try {
49
+ if (!this._fsPolyfill) {
50
+ throw new Error('FS polyfill is required for file hashing');
51
+ }
52
+ const hash = crypto.createHash(algorithm);
53
+ const fileStream = this._fsPolyfill.createReadStream(filePath);
54
+ fileStream.on('data', (data: any) => hash.update(data));
55
+ fileStream.on('end', () => resolve(`${algorithm}-${hash.digest('base64')}`));
56
+ fileStream.on('error', (err: any) => reject(err));
57
+ } catch (error) {
58
+ reject(error);
59
+ }
60
+ })
61
+ };
62
+ }
63
+
64
+ private initBrowser(): void {
65
+ this._crypto = {
66
+ createFileHash: () => {
67
+ this.logger.warning('File hash not available in browser environment');
68
+ return Promise.resolve('');
69
+ },
70
+ createTextHash: (text: string, algorithm: string = 'SHA-384') => new Promise<string>(async (resolve, reject) => {
71
+ try {
72
+ if (!crypto.subtle) {
73
+ throw new Error('SubtleCrypto not available');
74
+ }
75
+ const encoder = new TextEncoder();
76
+ const data = encoder.encode(text);
77
+ const hashBuffer = await crypto.subtle.digest(algorithm, data);
78
+ const hashArray = Array.from(new Uint8Array(hashBuffer));
79
+ const hashBase64 = btoa(String.fromCharCode(...hashArray));
80
+ resolve(`${algorithm}-${hashBase64}`);
81
+ } catch (error) {
82
+ reject(error);
83
+ }
84
+ })
85
+ };
86
+ }
87
+ }
@@ -0,0 +1,61 @@
1
+ import { A_Context } from "@adaas/a-concept";
2
+ import { Ifspolyfill } from "../A-Polyfill.types";
3
+ import { A_Logger } from "../../A-Logger/A-Logger.component";
4
+
5
+ export class A_FSPolyfillClass {
6
+ private _fs!: Ifspolyfill;
7
+ private _initialized: boolean = false;
8
+
9
+ constructor(
10
+ protected logger: A_Logger
11
+ ) {
12
+
13
+ }
14
+
15
+ get isInitialized(): boolean {
16
+ return this._initialized;
17
+ }
18
+
19
+
20
+ async get(): Promise<Ifspolyfill> {
21
+ if (!this._initialized) {
22
+ await this.init();
23
+ }
24
+ return this._fs;
25
+ }
26
+
27
+ private async init(): Promise<void> {
28
+ try {
29
+ if (A_Context.environment=== 'server') {
30
+ await this.initServer();
31
+ } else {
32
+ this.initBrowser();
33
+ }
34
+ this._initialized = true;
35
+ } catch (error) {
36
+ this.initBrowser();
37
+ this._initialized = true;
38
+ }
39
+ }
40
+
41
+ private async initServer(): Promise<void> {
42
+ this._fs = await import('fs') as Ifspolyfill;
43
+ }
44
+
45
+ private initBrowser(): void {
46
+ this._fs = {
47
+ readFileSync: (path: string, encoding: string) => {
48
+ this.logger.warning('fs.readFileSync not available in browser environment');
49
+ return '';
50
+ },
51
+ existsSync: (path: string) => {
52
+ this.logger.warning('fs.existsSync not available in browser environment');
53
+ return false;
54
+ },
55
+ createReadStream: (path: string) => {
56
+ this.logger.warning('fs.createReadStream not available in browser environment');
57
+ return null;
58
+ }
59
+ };
60
+ }
61
+ }
@@ -0,0 +1,100 @@
1
+ import { A_Context } from "@adaas/a-concept";
2
+ import { IhttpInterface } from "../A-Polyfill.types";
3
+ import { A_Logger } from "../../A-Logger/A-Logger.component";
4
+
5
+ export class A_HttpPolyfillClass {
6
+ private _http!: IhttpInterface;
7
+ private _initialized: boolean = false;
8
+
9
+ constructor(
10
+ protected logger: A_Logger
11
+ ) {
12
+
13
+ }
14
+
15
+ get isInitialized(): boolean {
16
+ return this._initialized;
17
+ }
18
+
19
+ async get(): Promise<IhttpInterface> {
20
+ if (!this._initialized) {
21
+ await this.init();
22
+ }
23
+ return this._http;
24
+ }
25
+
26
+ private async init(): Promise<void> {
27
+ try {
28
+ if (A_Context.environment === 'server') {
29
+ await this.initServer();
30
+ } else {
31
+ this.initBrowser();
32
+ }
33
+ this._initialized = true;
34
+ } catch (error) {
35
+ this.initBrowser();
36
+ this._initialized = true;
37
+ }
38
+ }
39
+
40
+ private async initServer(): Promise<void> {
41
+ const httpModule = await import('http');
42
+ this._http = {
43
+ request: httpModule.request,
44
+ get: httpModule.get,
45
+ createServer: httpModule.createServer
46
+ };
47
+ }
48
+
49
+ private initBrowser(): void {
50
+ this._http = {
51
+ request: (options: any, callback?: (res: any) => void) => {
52
+ this.logger.warning('http.request not available in browser/test environment, use fetch instead');
53
+ return this.createMockRequest(options, callback, false);
54
+ },
55
+ get: (url: string | any, callback?: (res: any) => void) => {
56
+ this.logger.warning('http.get not available in browser/test environment, use fetch instead');
57
+ return this.createMockRequest(typeof url === 'string' ? { hostname: url } : url, callback, false);
58
+ },
59
+ createServer: () => {
60
+ this.logger.error('http.createServer not available in browser/test environment');
61
+ return null;
62
+ }
63
+ };
64
+ }
65
+
66
+ private createMockRequest(options: any, callback?: (res: any) => void, isHttps: boolean = false) {
67
+ // Return a mock request that doesn't make real network calls
68
+ const request = {
69
+ end: () => {
70
+ // Mock response for all environments
71
+ if (callback) {
72
+ const mockResponse = {
73
+ statusCode: 200,
74
+ headers: {},
75
+ on: (event: string, handler: Function) => {
76
+ if (event === 'data') {
77
+ setTimeout(() => handler('mock data'), 0);
78
+ } else if (event === 'end') {
79
+ setTimeout(() => handler(), 0);
80
+ }
81
+ },
82
+ pipe: (dest: any) => {
83
+ if (dest.write) dest.write('mock data');
84
+ if (dest.end) dest.end();
85
+ }
86
+ };
87
+ setTimeout(() => callback(mockResponse), 0);
88
+ }
89
+ },
90
+ write: (data: any) => {
91
+ // Mock write
92
+ },
93
+ on: (event: string, handler: Function) => {
94
+ // Mock event handling
95
+ }
96
+ };
97
+
98
+ return request;
99
+ }
100
+ }
@@ -0,0 +1,100 @@
1
+ import { A_Context } from "@adaas/a-concept";
2
+ import { IhttpsInterface } from "../A-Polyfill.types";
3
+ import { A_Logger } from "../../A-Logger/A-Logger.component";
4
+
5
+ export class A_HttpsPolyfillClass {
6
+ private _https!: IhttpsInterface;
7
+ private _initialized: boolean = false;
8
+
9
+ constructor(
10
+ protected logger: A_Logger
11
+ ) {
12
+
13
+ }
14
+
15
+ get isInitialized(): boolean {
16
+ return this._initialized;
17
+ }
18
+
19
+ async get(): Promise<IhttpsInterface> {
20
+ if (!this._initialized) {
21
+ await this.init();
22
+ }
23
+ return this._https;
24
+ }
25
+
26
+ private async init(): Promise<void> {
27
+ try {
28
+ if (A_Context.environment === 'server') {
29
+ await this.initServer();
30
+ } else {
31
+ this.initBrowser();
32
+ }
33
+ this._initialized = true;
34
+ } catch (error) {
35
+ this.initBrowser();
36
+ this._initialized = true;
37
+ }
38
+ }
39
+
40
+ private async initServer(): Promise<void> {
41
+ const httpsModule = await import('https');
42
+ this._https = {
43
+ request: httpsModule.request,
44
+ get: httpsModule.get,
45
+ createServer: httpsModule.createServer
46
+ };
47
+ }
48
+
49
+ private initBrowser(): void {
50
+ this._https = {
51
+ request: (options: any, callback?: (res: any) => void) => {
52
+ this.logger.warning('https.request not available in browser/test environment, use fetch instead');
53
+ return this.createMockRequest(options, callback, true);
54
+ },
55
+ get: (url: string | any, callback?: (res: any) => void) => {
56
+ this.logger.warning('https.get not available in browser/test environment, use fetch instead');
57
+ return this.createMockRequest(typeof url === 'string' ? { hostname: url } : url, callback, true);
58
+ },
59
+ createServer: () => {
60
+ this.logger.error('https.createServer not available in browser/test environment');
61
+ return null;
62
+ }
63
+ };
64
+ }
65
+
66
+ private createMockRequest(options: any, callback?: (res: any) => void, isHttps: boolean = true) {
67
+ // Return a mock request that doesn't make real network calls
68
+ const request = {
69
+ end: () => {
70
+ // Mock response for all environments
71
+ if (callback) {
72
+ const mockResponse = {
73
+ statusCode: 200,
74
+ headers: {},
75
+ on: (event: string, handler: Function) => {
76
+ if (event === 'data') {
77
+ setTimeout(() => handler('mock data'), 0);
78
+ } else if (event === 'end') {
79
+ setTimeout(() => handler(), 0);
80
+ }
81
+ },
82
+ pipe: (dest: any) => {
83
+ if (dest.write) dest.write('mock data');
84
+ if (dest.end) dest.end();
85
+ }
86
+ };
87
+ setTimeout(() => callback(mockResponse), 0);
88
+ }
89
+ },
90
+ write: (data: any) => {
91
+ // Mock write
92
+ },
93
+ on: (event: string, handler: Function) => {
94
+ // Mock event handling
95
+ }
96
+ };
97
+
98
+ return request;
99
+ }
100
+ }