@twin.org/core 0.0.1-next.31 → 0.0.1-next.33

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.
@@ -3412,6 +3412,32 @@ class RandomHelper {
3412
3412
  }
3413
3413
  }
3414
3414
 
3415
+ // Copyright 2024 IOTA Stiftung.
3416
+ // SPDX-License-Identifier: Apache-2.0.
3417
+ /**
3418
+ * Class to help with uint8 arrays.
3419
+ */
3420
+ class Uint8ArrayHelper {
3421
+ /**
3422
+ * Concatenate multiple arrays.
3423
+ * @param arrays The array to concatenate.
3424
+ * @returns The combined array.
3425
+ */
3426
+ static concat(arrays) {
3427
+ let totalLength = 0;
3428
+ for (const array of arrays) {
3429
+ totalLength += array.length;
3430
+ }
3431
+ const concatBytes = new Uint8Array(totalLength);
3432
+ let offset = 0;
3433
+ for (const array of arrays) {
3434
+ concatBytes.set(array, offset);
3435
+ offset += array.length;
3436
+ }
3437
+ return concatBytes;
3438
+ }
3439
+ }
3440
+
3415
3441
  // Copyright 2024 IOTA Stiftung.
3416
3442
  // SPDX-License-Identifier: Apache-2.0.
3417
3443
  /**
@@ -3961,19 +3987,20 @@ class Compression {
3961
3987
  static async compress(bytes, type) {
3962
3988
  Guards.uint8Array(Compression._CLASS_NAME, "bytes", bytes);
3963
3989
  Guards.arrayOneOf(Compression._CLASS_NAME, "type", type, Object.values(CompressionType));
3964
- const blob = new Blob([bytes]);
3965
- const ds = new CompressionStream(type);
3966
- const compressedStream = blob.stream().pipeThrough(ds);
3967
- const compressedBlob = await new Response(compressedStream).blob();
3968
- const ab = await compressedBlob.arrayBuffer();
3969
- const compressedBytes = new Uint8Array(ab);
3990
+ const cs = new CompressionStream(type);
3991
+ const writer = cs.writable.getWriter();
3992
+ await writer.write(bytes);
3993
+ await writer.close();
3994
+ const reader = cs.readable.getReader();
3995
+ const chunks = await Compression.streamToChunks(reader);
3996
+ const concatenated = Uint8ArrayHelper.concat(chunks);
3970
3997
  // GZIP header contains a byte which specifies the OS the
3971
3998
  // compression was performed on. We set this to 3 (Unix) to ensure
3972
3999
  // that we produce consistent results.
3973
- if (type === "gzip" && compressedBytes.length >= 10) {
3974
- compressedBytes[9] = 3;
4000
+ if (type === "gzip" && concatenated.length >= 10) {
4001
+ concatenated[9] = 3;
3975
4002
  }
3976
- return compressedBytes;
4003
+ return concatenated;
3977
4004
  }
3978
4005
  /**
3979
4006
  * Decompress a gzipped compressed byte array.
@@ -3984,12 +4011,31 @@ class Compression {
3984
4011
  static async decompress(compressedBytes, type) {
3985
4012
  Guards.uint8Array(Compression._CLASS_NAME, "compressedBytes", compressedBytes);
3986
4013
  Guards.arrayOneOf(Compression._CLASS_NAME, "type", type, Object.values(CompressionType));
3987
- const blob = new Blob([compressedBytes]);
3988
- const ds = new DecompressionStream(type);
3989
- const decompressedStream = blob.stream().pipeThrough(ds);
3990
- const decompressedBlob = await new Response(decompressedStream).blob();
3991
- const ab = await decompressedBlob.arrayBuffer();
3992
- return new Uint8Array(ab);
4014
+ const cs = new DecompressionStream(type);
4015
+ const writer = cs.writable.getWriter();
4016
+ await writer.write(compressedBytes);
4017
+ await writer.close();
4018
+ const reader = cs.readable.getReader();
4019
+ const chunks = await Compression.streamToChunks(reader);
4020
+ return Uint8ArrayHelper.concat(chunks);
4021
+ }
4022
+ /**
4023
+ * Read the stream and create a list of chunks.
4024
+ * @param reader The reader to read the chunks from.
4025
+ * @returns The chunks.
4026
+ * @internal
4027
+ */
4028
+ static async streamToChunks(reader) {
4029
+ const chunks = [];
4030
+ let done = false;
4031
+ do {
4032
+ const chunk = await reader.read();
4033
+ done = chunk.done;
4034
+ if (!done && Is.uint8Array(chunk.value)) {
4035
+ chunks.push(chunk.value);
4036
+ }
4037
+ } while (!done);
4038
+ return chunks;
3993
4039
  }
3994
4040
  }
3995
4041
 
@@ -4799,6 +4845,7 @@ exports.NotSupportedError = NotSupportedError;
4799
4845
  exports.ObjectHelper = ObjectHelper;
4800
4846
  exports.RandomHelper = RandomHelper;
4801
4847
  exports.StringHelper = StringHelper;
4848
+ exports.Uint8ArrayHelper = Uint8ArrayHelper;
4802
4849
  exports.UnauthorizedError = UnauthorizedError;
4803
4850
  exports.UnprocessableError = UnprocessableError;
4804
4851
  exports.Url = Url;
@@ -3410,6 +3410,32 @@ class RandomHelper {
3410
3410
  }
3411
3411
  }
3412
3412
 
3413
+ // Copyright 2024 IOTA Stiftung.
3414
+ // SPDX-License-Identifier: Apache-2.0.
3415
+ /**
3416
+ * Class to help with uint8 arrays.
3417
+ */
3418
+ class Uint8ArrayHelper {
3419
+ /**
3420
+ * Concatenate multiple arrays.
3421
+ * @param arrays The array to concatenate.
3422
+ * @returns The combined array.
3423
+ */
3424
+ static concat(arrays) {
3425
+ let totalLength = 0;
3426
+ for (const array of arrays) {
3427
+ totalLength += array.length;
3428
+ }
3429
+ const concatBytes = new Uint8Array(totalLength);
3430
+ let offset = 0;
3431
+ for (const array of arrays) {
3432
+ concatBytes.set(array, offset);
3433
+ offset += array.length;
3434
+ }
3435
+ return concatBytes;
3436
+ }
3437
+ }
3438
+
3413
3439
  // Copyright 2024 IOTA Stiftung.
3414
3440
  // SPDX-License-Identifier: Apache-2.0.
3415
3441
  /**
@@ -3959,19 +3985,20 @@ class Compression {
3959
3985
  static async compress(bytes, type) {
3960
3986
  Guards.uint8Array(Compression._CLASS_NAME, "bytes", bytes);
3961
3987
  Guards.arrayOneOf(Compression._CLASS_NAME, "type", type, Object.values(CompressionType));
3962
- const blob = new Blob([bytes]);
3963
- const ds = new CompressionStream(type);
3964
- const compressedStream = blob.stream().pipeThrough(ds);
3965
- const compressedBlob = await new Response(compressedStream).blob();
3966
- const ab = await compressedBlob.arrayBuffer();
3967
- const compressedBytes = new Uint8Array(ab);
3988
+ const cs = new CompressionStream(type);
3989
+ const writer = cs.writable.getWriter();
3990
+ await writer.write(bytes);
3991
+ await writer.close();
3992
+ const reader = cs.readable.getReader();
3993
+ const chunks = await Compression.streamToChunks(reader);
3994
+ const concatenated = Uint8ArrayHelper.concat(chunks);
3968
3995
  // GZIP header contains a byte which specifies the OS the
3969
3996
  // compression was performed on. We set this to 3 (Unix) to ensure
3970
3997
  // that we produce consistent results.
3971
- if (type === "gzip" && compressedBytes.length >= 10) {
3972
- compressedBytes[9] = 3;
3998
+ if (type === "gzip" && concatenated.length >= 10) {
3999
+ concatenated[9] = 3;
3973
4000
  }
3974
- return compressedBytes;
4001
+ return concatenated;
3975
4002
  }
3976
4003
  /**
3977
4004
  * Decompress a gzipped compressed byte array.
@@ -3982,12 +4009,31 @@ class Compression {
3982
4009
  static async decompress(compressedBytes, type) {
3983
4010
  Guards.uint8Array(Compression._CLASS_NAME, "compressedBytes", compressedBytes);
3984
4011
  Guards.arrayOneOf(Compression._CLASS_NAME, "type", type, Object.values(CompressionType));
3985
- const blob = new Blob([compressedBytes]);
3986
- const ds = new DecompressionStream(type);
3987
- const decompressedStream = blob.stream().pipeThrough(ds);
3988
- const decompressedBlob = await new Response(decompressedStream).blob();
3989
- const ab = await decompressedBlob.arrayBuffer();
3990
- return new Uint8Array(ab);
4012
+ const cs = new DecompressionStream(type);
4013
+ const writer = cs.writable.getWriter();
4014
+ await writer.write(compressedBytes);
4015
+ await writer.close();
4016
+ const reader = cs.readable.getReader();
4017
+ const chunks = await Compression.streamToChunks(reader);
4018
+ return Uint8ArrayHelper.concat(chunks);
4019
+ }
4020
+ /**
4021
+ * Read the stream and create a list of chunks.
4022
+ * @param reader The reader to read the chunks from.
4023
+ * @returns The chunks.
4024
+ * @internal
4025
+ */
4026
+ static async streamToChunks(reader) {
4027
+ const chunks = [];
4028
+ let done = false;
4029
+ do {
4030
+ const chunk = await reader.read();
4031
+ done = chunk.done;
4032
+ if (!done && Is.uint8Array(chunk.value)) {
4033
+ chunks.push(chunk.value);
4034
+ }
4035
+ } while (!done);
4036
+ return chunks;
3991
4037
  }
3992
4038
  }
3993
4039
 
@@ -4764,4 +4810,4 @@ class Validation {
4764
4810
  }
4765
4811
  }
4766
4812
 
4767
- export { AlreadyExistsError, ArrayHelper, AsyncCache, Base32, Base58, Base64, Base64Url, BaseError, BitString, Coerce, CoerceType, ComponentFactory, Compression, CompressionType, ConflictError, Converter, EnvHelper, ErrorHelper, Factory, FilenameHelper, GeneralError, GuardError, Guards, HexHelper, I18n, Is, JsonHelper, NotFoundError, NotImplementedError, NotSupportedError, ObjectHelper, RandomHelper, StringHelper, UnauthorizedError, UnprocessableError, Url, Urn, Validation, ValidationError };
4813
+ export { AlreadyExistsError, ArrayHelper, AsyncCache, Base32, Base58, Base64, Base64Url, BaseError, BitString, Coerce, CoerceType, ComponentFactory, Compression, CompressionType, ConflictError, Converter, EnvHelper, ErrorHelper, Factory, FilenameHelper, GeneralError, GuardError, Guards, HexHelper, I18n, Is, JsonHelper, NotFoundError, NotImplementedError, NotSupportedError, ObjectHelper, RandomHelper, StringHelper, Uint8ArrayHelper, UnauthorizedError, UnprocessableError, Url, Urn, Validation, ValidationError };
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Class to help with uint8 arrays.
3
+ */
4
+ export declare class Uint8ArrayHelper {
5
+ /**
6
+ * Concatenate multiple arrays.
7
+ * @param arrays The array to concatenate.
8
+ * @returns The combined array.
9
+ */
10
+ static concat(arrays: Uint8Array[]): Uint8Array;
11
+ }
@@ -24,6 +24,7 @@ export * from "./helpers/jsonHelper";
24
24
  export * from "./helpers/objectHelper";
25
25
  export * from "./helpers/randomHelper";
26
26
  export * from "./helpers/stringHelper";
27
+ export * from "./helpers/uint8ArrayHelper";
27
28
  export * from "./models/coerceType";
28
29
  export * from "./models/compressionType";
29
30
  export * from "./models/IComponent";
package/docs/changelog.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # @twin.org/core - Changelog
2
2
 
3
- ## 0.0.1-next.31
3
+ ## 0.0.1-next.33
4
4
 
5
5
  - Initial Release
@@ -0,0 +1,35 @@
1
+ # Class: Uint8ArrayHelper
2
+
3
+ Class to help with uint8 arrays.
4
+
5
+ ## Constructors
6
+
7
+ ### new Uint8ArrayHelper()
8
+
9
+ > **new Uint8ArrayHelper**(): [`Uint8ArrayHelper`](Uint8ArrayHelper.md)
10
+
11
+ #### Returns
12
+
13
+ [`Uint8ArrayHelper`](Uint8ArrayHelper.md)
14
+
15
+ ## Methods
16
+
17
+ ### concat()
18
+
19
+ > `static` **concat**(`arrays`): `Uint8Array`
20
+
21
+ Concatenate multiple arrays.
22
+
23
+ #### Parameters
24
+
25
+ ##### arrays
26
+
27
+ `Uint8Array`[]
28
+
29
+ The array to concatenate.
30
+
31
+ #### Returns
32
+
33
+ `Uint8Array`
34
+
35
+ The combined array.
@@ -27,6 +27,7 @@
27
27
  - [ObjectHelper](classes/ObjectHelper.md)
28
28
  - [RandomHelper](classes/RandomHelper.md)
29
29
  - [StringHelper](classes/StringHelper.md)
30
+ - [Uint8ArrayHelper](classes/Uint8ArrayHelper.md)
30
31
  - [BitString](classes/BitString.md)
31
32
  - [Url](classes/Url.md)
32
33
  - [Urn](classes/Urn.md)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/core",
3
- "version": "0.0.1-next.31",
3
+ "version": "0.0.1-next.33",
4
4
  "description": "Helper methods/classes for data type checking/validation/guarding/error handling",
5
5
  "repository": {
6
6
  "type": "git",