@adaas/a-utils 0.2.9 → 0.3.2

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.
@@ -1,679 +0,0 @@
1
- import { __decorateClass, __decorateParam } from './chunk-EQQGB2QZ.mjs';
2
- import { A_Concept, A_Inject, A_Component, A_Context } from '@adaas/a-concept';
3
- import { A_Frame } from '@adaas/a-frame';
4
-
5
- // src/lib/A-Polyfill/base/A-FS-Polyfill.base.ts
6
- var A_FSPolyfillBase = class {
7
- constructor(logger) {
8
- this.logger = logger;
9
- this._initialized = false;
10
- }
11
- get isInitialized() {
12
- return this._initialized;
13
- }
14
- async get() {
15
- if (!this._initialized) {
16
- await this.init();
17
- }
18
- return this._fs;
19
- }
20
- async init() {
21
- try {
22
- await this.initImplementation();
23
- this._initialized = true;
24
- } catch (error) {
25
- this.logger.error("Failed to initialize fs polyfill", error);
26
- throw error;
27
- }
28
- }
29
- };
30
-
31
- // src/lib/A-Polyfill/browser/A-FS-Polyfill.ts
32
- var A_FSPolyfill = class extends A_FSPolyfillBase {
33
- constructor(logger) {
34
- super(logger);
35
- }
36
- async initImplementation() {
37
- this._fs = {
38
- readFileSync: (path, encoding) => {
39
- this.logger.warning("fs.readFileSync not available in browser environment");
40
- return "";
41
- },
42
- existsSync: (path) => {
43
- this.logger.warning("fs.existsSync not available in browser environment");
44
- return false;
45
- },
46
- createReadStream: (path) => {
47
- this.logger.warning("fs.createReadStream not available in browser environment");
48
- return null;
49
- }
50
- };
51
- }
52
- };
53
-
54
- // src/lib/A-Polyfill/base/A-Crypto-Polyfill.base.ts
55
- var A_CryptoPolyfillBase = class {
56
- constructor(logger) {
57
- this.logger = logger;
58
- this._initialized = false;
59
- }
60
- get isInitialized() {
61
- return this._initialized;
62
- }
63
- async get(fsPolyfill) {
64
- if (!this._initialized) {
65
- this._fsPolyfill = fsPolyfill;
66
- await this.init();
67
- }
68
- return this._crypto;
69
- }
70
- async init() {
71
- try {
72
- await this.initImplementation();
73
- this._initialized = true;
74
- } catch (error) {
75
- this.logger.error("Failed to initialize crypto polyfill", error);
76
- throw error;
77
- }
78
- }
79
- };
80
-
81
- // src/lib/A-Polyfill/browser/A-Crypto-Polyfill.ts
82
- var A_CryptoPolyfill = class extends A_CryptoPolyfillBase {
83
- constructor(logger) {
84
- super(logger);
85
- }
86
- async initImplementation() {
87
- this._crypto = {
88
- createFileHash: () => {
89
- this.logger.warning("File hash not available in browser environment");
90
- return Promise.resolve("");
91
- },
92
- createTextHash: (text, algorithm = "SHA-384") => new Promise(async (resolve, reject) => {
93
- try {
94
- if (!crypto.subtle) {
95
- throw new Error("SubtleCrypto not available");
96
- }
97
- const encoder = new TextEncoder();
98
- const data = encoder.encode(text);
99
- const hashBuffer = await crypto.subtle.digest(algorithm, data);
100
- const hashArray = Array.from(new Uint8Array(hashBuffer));
101
- const hashBase64 = btoa(String.fromCharCode(...hashArray));
102
- resolve(`${algorithm}-${hashBase64}`);
103
- } catch (error) {
104
- reject(error);
105
- }
106
- })
107
- };
108
- }
109
- };
110
-
111
- // src/lib/A-Polyfill/base/A-Http-Polyfill.base.ts
112
- var A_HttpPolyfillBase = class {
113
- constructor(logger) {
114
- this.logger = logger;
115
- this._initialized = false;
116
- }
117
- get isInitialized() {
118
- return this._initialized;
119
- }
120
- async get() {
121
- if (!this._initialized) {
122
- await this.init();
123
- }
124
- return this._http;
125
- }
126
- async init() {
127
- try {
128
- await this.initImplementation();
129
- this._initialized = true;
130
- } catch (error) {
131
- this.logger.error("Failed to initialize http polyfill", error);
132
- throw error;
133
- }
134
- }
135
- };
136
-
137
- // src/lib/A-Polyfill/browser/A-Http-Polyfill.ts
138
- var A_HttpPolyfill = class extends A_HttpPolyfillBase {
139
- constructor(logger) {
140
- super(logger);
141
- }
142
- async initImplementation() {
143
- this._http = {
144
- request: (options, callback) => {
145
- this.logger.warning("http.request not available in browser/test environment, use fetch instead");
146
- return this.createMockRequest(options, callback, false);
147
- },
148
- get: (url, callback) => {
149
- this.logger.warning("http.get not available in browser/test environment, use fetch instead");
150
- return this.createMockRequest(typeof url === "string" ? { hostname: url } : url, callback, false);
151
- },
152
- createServer: () => {
153
- this.logger.error("http.createServer not available in browser/test environment");
154
- return null;
155
- }
156
- };
157
- }
158
- createMockRequest(options, callback, isHttps = false) {
159
- const request = {
160
- end: () => {
161
- if (callback) {
162
- const mockResponse = {
163
- statusCode: 200,
164
- headers: {},
165
- on: (event, handler) => {
166
- if (event === "data") {
167
- setTimeout(() => handler("mock data"), 0);
168
- } else if (event === "end") {
169
- setTimeout(() => handler(), 0);
170
- }
171
- },
172
- pipe: (dest) => {
173
- if (dest.write) dest.write("mock data");
174
- if (dest.end) dest.end();
175
- }
176
- };
177
- setTimeout(() => callback(mockResponse), 0);
178
- }
179
- },
180
- write: (data) => {
181
- },
182
- on: (event, handler) => {
183
- }
184
- };
185
- return request;
186
- }
187
- };
188
-
189
- // src/lib/A-Polyfill/base/A-Https-Polyfill.base.ts
190
- var A_HttpsPolyfillBase = class {
191
- constructor(logger) {
192
- this.logger = logger;
193
- this._initialized = false;
194
- }
195
- get isInitialized() {
196
- return this._initialized;
197
- }
198
- async get() {
199
- if (!this._initialized) {
200
- await this.init();
201
- }
202
- return this._https;
203
- }
204
- async init() {
205
- try {
206
- await this.initImplementation();
207
- this._initialized = true;
208
- } catch (error) {
209
- this.logger.error("Failed to initialize https polyfill", error);
210
- throw error;
211
- }
212
- }
213
- };
214
-
215
- // src/lib/A-Polyfill/browser/A-Https-Polyfill.ts
216
- var A_HttpsPolyfill = class extends A_HttpsPolyfillBase {
217
- constructor(logger) {
218
- super(logger);
219
- }
220
- async initImplementation() {
221
- this._https = {
222
- request: (options, callback) => {
223
- this.logger.warning("https.request not available in browser/test environment, use fetch instead");
224
- return this.createMockRequest(options, callback, true);
225
- },
226
- get: (url, callback) => {
227
- this.logger.warning("https.get not available in browser/test environment, use fetch instead");
228
- return this.createMockRequest(typeof url === "string" ? { hostname: url } : url, callback, true);
229
- },
230
- createServer: () => {
231
- this.logger.error("https.createServer not available in browser/test environment");
232
- return null;
233
- }
234
- };
235
- }
236
- createMockRequest(options, callback, isHttps = true) {
237
- const request = {
238
- end: () => {
239
- if (callback) {
240
- const mockResponse = {
241
- statusCode: 200,
242
- headers: {},
243
- on: (event, handler) => {
244
- if (event === "data") {
245
- setTimeout(() => handler("mock data"), 0);
246
- } else if (event === "end") {
247
- setTimeout(() => handler(), 0);
248
- }
249
- },
250
- pipe: (dest) => {
251
- if (dest.write) dest.write("mock data");
252
- if (dest.end) dest.end();
253
- }
254
- };
255
- setTimeout(() => callback(mockResponse), 0);
256
- }
257
- },
258
- write: (data) => {
259
- },
260
- on: (event, handler) => {
261
- }
262
- };
263
- return request;
264
- }
265
- };
266
-
267
- // src/lib/A-Polyfill/base/A-Path-Polyfill.base.ts
268
- var A_PathPolyfillBase = class {
269
- constructor(logger) {
270
- this.logger = logger;
271
- this._initialized = false;
272
- }
273
- get isInitialized() {
274
- return this._initialized;
275
- }
276
- async get() {
277
- if (!this._initialized) {
278
- await this.init();
279
- }
280
- return this._path;
281
- }
282
- async init() {
283
- try {
284
- await this.initImplementation();
285
- this._initialized = true;
286
- } catch (error) {
287
- this.logger.error("Failed to initialize path polyfill", error);
288
- throw error;
289
- }
290
- }
291
- };
292
-
293
- // src/lib/A-Polyfill/browser/A-Path-Polyfill.ts
294
- var A_PathPolyfill = class extends A_PathPolyfillBase {
295
- constructor(logger) {
296
- super(logger);
297
- }
298
- async initImplementation() {
299
- this._path = {
300
- join: (...paths) => {
301
- return paths.join("/").replace(/\/+/g, "/");
302
- },
303
- resolve: (...paths) => {
304
- let resolvedPath = "";
305
- for (const path of paths) {
306
- if (path.startsWith("/")) {
307
- resolvedPath = path;
308
- } else {
309
- resolvedPath = this._path.join(resolvedPath, path);
310
- }
311
- }
312
- return resolvedPath || "/";
313
- },
314
- dirname: (path) => {
315
- const parts = path.split("/");
316
- return parts.slice(0, -1).join("/") || "/";
317
- },
318
- basename: (path, ext) => {
319
- const base = path.split("/").pop() || "";
320
- return ext && base.endsWith(ext) ? base.slice(0, -ext.length) : base;
321
- },
322
- extname: (path) => {
323
- const parts = path.split(".");
324
- return parts.length > 1 ? "." + parts.pop() : "";
325
- },
326
- relative: (from, to) => {
327
- return to.replace(from, "").replace(/^\//, "");
328
- },
329
- normalize: (path) => {
330
- return path.replace(/\/+/g, "/").replace(/\/$/, "") || "/";
331
- },
332
- isAbsolute: (path) => {
333
- return path.startsWith("/") || /^[a-zA-Z]:/.test(path);
334
- },
335
- parse: (path) => {
336
- const ext = this._path.extname(path);
337
- const base = this._path.basename(path);
338
- const name = this._path.basename(path, ext);
339
- const dir = this._path.dirname(path);
340
- return { root: "/", dir, base, ext, name };
341
- },
342
- format: (pathObject) => {
343
- return this._path.join(pathObject.dir || "", pathObject.base || "");
344
- },
345
- sep: "/",
346
- delimiter: ":"
347
- };
348
- }
349
- };
350
-
351
- // src/lib/A-Polyfill/base/A-Url-Polyfill.base.ts
352
- var A_UrlPolyfillBase = class {
353
- constructor(logger) {
354
- this.logger = logger;
355
- this._initialized = false;
356
- }
357
- get isInitialized() {
358
- return this._initialized;
359
- }
360
- async get() {
361
- if (!this._initialized) {
362
- await this.init();
363
- }
364
- return this._url;
365
- }
366
- async init() {
367
- try {
368
- await this.initImplementation();
369
- this._initialized = true;
370
- } catch (error) {
371
- this.logger.error("Failed to initialize url polyfill", error);
372
- throw error;
373
- }
374
- }
375
- };
376
-
377
- // src/lib/A-Polyfill/browser/A-Url-Polyfill.ts
378
- var A_UrlPolyfill = class extends A_UrlPolyfillBase {
379
- constructor(logger) {
380
- super(logger);
381
- }
382
- async initImplementation() {
383
- this._url = {
384
- parse: (urlString) => {
385
- try {
386
- const url = new URL(urlString);
387
- return {
388
- protocol: url.protocol,
389
- hostname: url.hostname,
390
- port: url.port,
391
- pathname: url.pathname,
392
- search: url.search,
393
- hash: url.hash,
394
- host: url.host,
395
- href: url.href
396
- };
397
- } catch {
398
- return {};
399
- }
400
- },
401
- format: (urlObject) => {
402
- try {
403
- return new URL("", urlObject.href || `${urlObject.protocol}//${urlObject.host}${urlObject.pathname}${urlObject.search}${urlObject.hash}`).href;
404
- } catch {
405
- return "";
406
- }
407
- },
408
- resolve: (from, to) => {
409
- try {
410
- return new URL(to, from).href;
411
- } catch {
412
- return to;
413
- }
414
- },
415
- URL: globalThis.URL,
416
- URLSearchParams: globalThis.URLSearchParams
417
- };
418
- }
419
- };
420
-
421
- // src/lib/A-Polyfill/base/A-Buffer-Polyfill.base.ts
422
- var A_BufferPolyfillBase = class {
423
- constructor(logger) {
424
- this.logger = logger;
425
- this._initialized = false;
426
- }
427
- get isInitialized() {
428
- return this._initialized;
429
- }
430
- async get() {
431
- if (!this._initialized) {
432
- await this.init();
433
- }
434
- return this._buffer;
435
- }
436
- async init() {
437
- try {
438
- await this.initImplementation();
439
- this._initialized = true;
440
- } catch (error) {
441
- this.logger.error("Failed to initialize buffer polyfill", error);
442
- throw error;
443
- }
444
- }
445
- };
446
-
447
- // src/lib/A-Polyfill/browser/A-Buffer-Polyfill.ts
448
- var A_BufferPolyfill = class extends A_BufferPolyfillBase {
449
- constructor(logger) {
450
- super(logger);
451
- }
452
- async initImplementation() {
453
- this._buffer = {
454
- from: (data, encoding) => {
455
- if (typeof data === "string") {
456
- return new TextEncoder().encode(data);
457
- }
458
- return new Uint8Array(data);
459
- },
460
- alloc: (size, fill) => {
461
- const buffer = new Uint8Array(size);
462
- if (fill !== void 0) {
463
- buffer.fill(fill);
464
- }
465
- return buffer;
466
- },
467
- allocUnsafe: (size) => {
468
- return new Uint8Array(size);
469
- },
470
- isBuffer: (obj) => {
471
- return obj instanceof Uint8Array || obj instanceof ArrayBuffer;
472
- },
473
- concat: (list, totalLength) => {
474
- const length = totalLength || list.reduce((sum, buf) => sum + buf.length, 0);
475
- const result = new Uint8Array(length);
476
- let offset = 0;
477
- for (const buf of list) {
478
- result.set(buf, offset);
479
- offset += buf.length;
480
- }
481
- return result;
482
- }
483
- };
484
- }
485
- };
486
-
487
- // src/lib/A-Polyfill/base/A-Process-Polyfill.base.ts
488
- var A_ProcessPolyfillBase = class {
489
- constructor(logger) {
490
- this.logger = logger;
491
- this._initialized = false;
492
- }
493
- get isInitialized() {
494
- return this._initialized;
495
- }
496
- async get() {
497
- if (!this._initialized) {
498
- await this.init();
499
- }
500
- return this._process;
501
- }
502
- async init() {
503
- try {
504
- await this.initImplementation();
505
- this._initialized = true;
506
- } catch (error) {
507
- this.logger.error("Failed to initialize process polyfill", error);
508
- throw error;
509
- }
510
- }
511
- };
512
-
513
- // src/lib/A-Polyfill/browser/A-Process-Polyfill.ts
514
- var A_ProcessPolyfill = class extends A_ProcessPolyfillBase {
515
- constructor(logger) {
516
- super(logger);
517
- }
518
- async initImplementation() {
519
- this._process = {
520
- env: {
521
- NODE_ENV: "browser",
522
- ...globalThis.process?.env || {}
523
- },
524
- argv: ["browser"],
525
- platform: "browser",
526
- version: "browser",
527
- versions: { node: "browser" },
528
- cwd: () => "/",
529
- exit: (code) => {
530
- this.logger.warning("process.exit not available in browser");
531
- throw new Error(`Process exit with code ${code}`);
532
- },
533
- nextTick: (callback, ...args) => {
534
- setTimeout(() => callback(...args), 0);
535
- }
536
- };
537
- }
538
- };
539
- var A_Polyfill = class extends A_Component {
540
- constructor(logger) {
541
- super();
542
- this.logger = logger;
543
- this._initializing = null;
544
- }
545
- /**
546
- * Indicates whether the channel is connected
547
- */
548
- get ready() {
549
- if (!this._initialized) {
550
- this._initialized = this._loadInternal();
551
- }
552
- return this._initialized;
553
- }
554
- async load() {
555
- await this.ready;
556
- }
557
- async attachToWindow() {
558
- if (A_Context.environment !== "browser") return;
559
- globalThis.A_Polyfill = this;
560
- globalThis.process = { env: { NODE_ENV: "production" }, cwd: () => "/" };
561
- globalThis.__dirname = "/";
562
- }
563
- async _loadInternal() {
564
- this._fsPolyfill = new A_FSPolyfill(this.logger);
565
- this._cryptoPolyfill = new A_CryptoPolyfill(this.logger);
566
- this._httpPolyfill = new A_HttpPolyfill(this.logger);
567
- this._httpsPolyfill = new A_HttpsPolyfill(this.logger);
568
- this._pathPolyfill = new A_PathPolyfill(this.logger);
569
- this._urlPolyfill = new A_UrlPolyfill(this.logger);
570
- this._bufferPolyfill = new A_BufferPolyfill(this.logger);
571
- this._processPolyfill = new A_ProcessPolyfill(this.logger);
572
- await this._fsPolyfill.get();
573
- await this._cryptoPolyfill.get(await this._fsPolyfill.get());
574
- await this._httpPolyfill.get();
575
- await this._httpsPolyfill.get();
576
- await this._pathPolyfill.get();
577
- await this._urlPolyfill.get();
578
- await this._bufferPolyfill.get();
579
- await this._processPolyfill.get();
580
- }
581
- /**
582
- * Allows to use the 'fs' polyfill methods regardless of the environment
583
- * This method loads the 'fs' polyfill and returns its instance
584
- *
585
- * @returns
586
- */
587
- async fs() {
588
- await this.ready;
589
- return await this._fsPolyfill.get();
590
- }
591
- /**
592
- * Allows to use the 'crypto' polyfill methods regardless of the environment
593
- * This method loads the 'crypto' polyfill and returns its instance
594
- *
595
- * @returns
596
- */
597
- async crypto() {
598
- await this.ready;
599
- return await this._cryptoPolyfill.get();
600
- }
601
- /**
602
- * Allows to use the 'http' polyfill methods regardless of the environment
603
- * This method loads the 'http' polyfill and returns its instance
604
- *
605
- * @returns
606
- */
607
- async http() {
608
- await this.ready;
609
- return await this._httpPolyfill.get();
610
- }
611
- /**
612
- * Allows to use the 'https' polyfill methods regardless of the environment
613
- * This method loads the 'https' polyfill and returns its instance
614
- *
615
- * @returns
616
- */
617
- async https() {
618
- await this.ready;
619
- return await this._httpsPolyfill.get();
620
- }
621
- /**
622
- * Allows to use the 'path' polyfill methods regardless of the environment
623
- * This method loads the 'path' polyfill and returns its instance
624
- *
625
- * @returns
626
- */
627
- async path() {
628
- await this.ready;
629
- return await this._pathPolyfill.get();
630
- }
631
- /**
632
- * Allows to use the 'url' polyfill methods regardless of the environment
633
- * This method loads the 'url' polyfill and returns its instance
634
- *
635
- * @returns
636
- */
637
- async url() {
638
- await this.ready;
639
- return await this._urlPolyfill.get();
640
- }
641
- /**
642
- * Allows to use the 'buffer' polyfill methods regardless of the environment
643
- * This method loads the 'buffer' polyfill and returns its instance
644
- *
645
- * @returns
646
- */
647
- async buffer() {
648
- await this.ready;
649
- return await this._bufferPolyfill.get();
650
- }
651
- /**
652
- * Allows to use the 'process' polyfill methods regardless of the environment
653
- * This method loads the 'process' polyfill and returns its instance
654
- *
655
- * @returns
656
- */
657
- async process() {
658
- await this.ready;
659
- return await this._processPolyfill.get();
660
- }
661
- };
662
- __decorateClass([
663
- A_Concept.Load()
664
- ], A_Polyfill.prototype, "load", 1);
665
- __decorateClass([
666
- A_Concept.Load()
667
- ], A_Polyfill.prototype, "attachToWindow", 1);
668
- A_Polyfill = __decorateClass([
669
- A_Frame.Component({
670
- namespace: "A-Utils",
671
- name: "A-Polyfill",
672
- description: "Polyfill component that provides cross-environment compatibility for Node.js core modules such as fs, crypto, http, https, path, url, buffer, and process. It dynamically loads appropriate polyfills based on the execution environment (Node.js or browser), enabling seamless usage of these modules in different contexts."
673
- }),
674
- __decorateParam(0, A_Inject("A_Logger"))
675
- ], A_Polyfill);
676
-
677
- export { A_BufferPolyfill, A_BufferPolyfillBase, A_CryptoPolyfill, A_CryptoPolyfillBase, A_FSPolyfill, A_FSPolyfillBase, A_HttpPolyfillBase, A_HttpsPolyfill, A_HttpsPolyfillBase, A_PathPolyfill, A_PathPolyfillBase, A_Polyfill, A_ProcessPolyfill, A_ProcessPolyfillBase, A_UrlPolyfill, A_UrlPolyfillBase };
678
- //# sourceMappingURL=chunk-JTQYATZ5.mjs.map
679
- //# sourceMappingURL=chunk-JTQYATZ5.mjs.map