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

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.
@@ -3987,20 +3987,18 @@ class Compression {
3987
3987
  static async compress(bytes, type) {
3988
3988
  Guards.uint8Array(Compression._CLASS_NAME, "bytes", bytes);
3989
3989
  Guards.arrayOneOf(Compression._CLASS_NAME, "type", type, Object.values(CompressionType));
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);
3990
+ const blob = new Blob([bytes]);
3991
+ const compressionStream = new CompressionStream(type);
3992
+ const compressionPipe = blob.stream().pipeThrough(compressionStream);
3993
+ const compressedBlob = await new Response(compressionPipe).blob();
3994
+ const compressedBytes = await compressedBlob.bytes();
3997
3995
  // GZIP header contains a byte which specifies the OS the
3998
3996
  // compression was performed on. We set this to 3 (Unix) to ensure
3999
3997
  // that we produce consistent results.
4000
- if (type === "gzip" && concatenated.length >= 10) {
4001
- concatenated[9] = 3;
3998
+ if (type === "gzip" && compressedBytes.length >= 10) {
3999
+ compressedBytes[9] = 3;
4002
4000
  }
4003
- return concatenated;
4001
+ return compressedBytes;
4004
4002
  }
4005
4003
  /**
4006
4004
  * Decompress a gzipped compressed byte array.
@@ -4011,31 +4009,11 @@ class Compression {
4011
4009
  static async decompress(compressedBytes, type) {
4012
4010
  Guards.uint8Array(Compression._CLASS_NAME, "compressedBytes", compressedBytes);
4013
4011
  Guards.arrayOneOf(Compression._CLASS_NAME, "type", type, Object.values(CompressionType));
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;
4012
+ const blob = new Blob([compressedBytes]);
4013
+ const decompressionStream = new DecompressionStream(type);
4014
+ const decompressionPipe = blob.stream().pipeThrough(decompressionStream);
4015
+ const decompressedBlob = await new Response(decompressionPipe).blob();
4016
+ return decompressedBlob.bytes();
4039
4017
  }
4040
4018
  }
4041
4019
 
@@ -3985,20 +3985,18 @@ class Compression {
3985
3985
  static async compress(bytes, type) {
3986
3986
  Guards.uint8Array(Compression._CLASS_NAME, "bytes", bytes);
3987
3987
  Guards.arrayOneOf(Compression._CLASS_NAME, "type", type, Object.values(CompressionType));
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);
3988
+ const blob = new Blob([bytes]);
3989
+ const compressionStream = new CompressionStream(type);
3990
+ const compressionPipe = blob.stream().pipeThrough(compressionStream);
3991
+ const compressedBlob = await new Response(compressionPipe).blob();
3992
+ const compressedBytes = await compressedBlob.bytes();
3995
3993
  // GZIP header contains a byte which specifies the OS the
3996
3994
  // compression was performed on. We set this to 3 (Unix) to ensure
3997
3995
  // that we produce consistent results.
3998
- if (type === "gzip" && concatenated.length >= 10) {
3999
- concatenated[9] = 3;
3996
+ if (type === "gzip" && compressedBytes.length >= 10) {
3997
+ compressedBytes[9] = 3;
4000
3998
  }
4001
- return concatenated;
3999
+ return compressedBytes;
4002
4000
  }
4003
4001
  /**
4004
4002
  * Decompress a gzipped compressed byte array.
@@ -4009,31 +4007,11 @@ class Compression {
4009
4007
  static async decompress(compressedBytes, type) {
4010
4008
  Guards.uint8Array(Compression._CLASS_NAME, "compressedBytes", compressedBytes);
4011
4009
  Guards.arrayOneOf(Compression._CLASS_NAME, "type", type, Object.values(CompressionType));
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;
4010
+ const blob = new Blob([compressedBytes]);
4011
+ const decompressionStream = new DecompressionStream(type);
4012
+ const decompressionPipe = blob.stream().pipeThrough(decompressionStream);
4013
+ const decompressedBlob = await new Response(decompressionPipe).blob();
4014
+ return decompressedBlob.bytes();
4037
4015
  }
4038
4016
  }
4039
4017
 
package/docs/changelog.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # @twin.org/core - Changelog
2
2
 
3
- ## 0.0.1-next.33
3
+ ## 0.0.1-next.34
4
4
 
5
5
  - Initial Release
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/core",
3
- "version": "0.0.1-next.33",
3
+ "version": "0.0.1-next.34",
4
4
  "description": "Helper methods/classes for data type checking/validation/guarding/error handling",
5
5
  "repository": {
6
6
  "type": "git",