Compression.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import { gzip, ungzip } from "pako";
  2. /*
  3. MIT License
  4. Copyright (c) 2020 Egor Nepomnyaschih
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in all
  12. copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. SOFTWARE.
  20. */
  21. const base64abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split("");
  22. const l = 256;
  23. const base64codes = new Uint8Array(l);
  24. for (let i = 0; i < l; ++i) {
  25. base64codes[i] = 255; // invalid character
  26. }
  27. base64abc.forEach((char, index) => {
  28. base64codes[char.charCodeAt(0)] = index;
  29. });
  30. base64codes["=".charCodeAt(0)] = 0; // ignored anyway, so we just need to prevent an error
  31. function getBase64Code(charCode: number): number {
  32. if (charCode >= base64codes.length) {
  33. throw new Error("Unable to parse base64 string.");
  34. }
  35. const code = base64codes[charCode];
  36. if (code === 255) {
  37. throw new Error("Unable to parse base64 string.");
  38. }
  39. return code;
  40. }
  41. export function bytesToBase64(bytes: Uint8Array) {
  42. let result = "",
  43. i: number;
  44. const l = bytes.length;
  45. for (i = 2; i < l; i += 3) {
  46. result += base64abc[bytes[i - 2] >> 2];
  47. result += base64abc[((bytes[i - 2] & 0x03) << 4) | (bytes[i - 1] >> 4)];
  48. result += base64abc[((bytes[i - 1] & 0x0f) << 2) | (bytes[i] >> 6)];
  49. result += base64abc[bytes[i] & 0x3f];
  50. }
  51. if (i === l + 1) {
  52. // 1 octet yet to write
  53. result += base64abc[bytes[i - 2] >> 2];
  54. result += base64abc[(bytes[i - 2] & 0x03) << 4];
  55. result += "==";
  56. }
  57. if (i === l) {
  58. // 2 octets yet to write
  59. result += base64abc[bytes[i - 2] >> 2];
  60. result += base64abc[((bytes[i - 2] & 0x03) << 4) | (bytes[i - 1] >> 4)];
  61. result += base64abc[(bytes[i - 1] & 0x0f) << 2];
  62. result += "=";
  63. }
  64. return result;
  65. }
  66. export function base64ToBytes(str: string) {
  67. if (str.length % 4 !== 0) {
  68. throw new Error("Unable to parse base64 string.");
  69. }
  70. const index = str.indexOf("=");
  71. if (index !== -1 && index < str.length - 2) {
  72. throw new Error("Unable to parse base64 string.");
  73. }
  74. const missingOctets = str.endsWith("==") ? 2 : str.endsWith("=") ? 1 : 0;
  75. const n = str.length;
  76. const result = new Uint8Array(3 * (n / 4));
  77. let buffer: number;
  78. for (let i = 0, j = 0; i < n; i += 4, j += 3) {
  79. buffer =
  80. (getBase64Code(str.charCodeAt(i)) << 18) |
  81. (getBase64Code(str.charCodeAt(i + 1)) << 12) |
  82. (getBase64Code(str.charCodeAt(i + 2)) << 6) |
  83. getBase64Code(str.charCodeAt(i + 3));
  84. result[j] = buffer >> 16;
  85. result[j + 1] = (buffer >> 8) & 0xff;
  86. result[j + 2] = buffer & 0xff;
  87. }
  88. return result.subarray(0, result.length - missingOctets);
  89. }
  90. export function base64encode(str: string, encoder = new TextEncoder()): string {
  91. return bytesToBase64(encoder.encode(str));
  92. }
  93. export function base64decode(str: string, decoder = new TextDecoder()): string {
  94. return decoder.decode(base64ToBytes(str));
  95. }
  96. export function zipEncode(str: string): string {
  97. return bytesToBase64(gzip(str));
  98. }
  99. export function decodeUnzip(str: string): string {
  100. return ungzip(base64ToBytes(str), { to: "string" });
  101. }
  102. export type CompressedArray<T> = {
  103. k: Array<keyof T>;
  104. v: Array<Array<T[keyof T]>>;
  105. };
  106. export function compressArray<T>(arr: Array<T>): CompressedArray<T> {
  107. if (arr.length == 0) {
  108. return { k: [], v: [] };
  109. }
  110. const elt = arr[0];
  111. const keys = Object.keys(elt) as Array<keyof T>;
  112. return { k: keys, v: arr.map((o) => keys.map((k) => o[k])) };
  113. }
  114. export function restoreArray<T>(obj: CompressedArray<T>): Array<T> {
  115. return obj.v.map((a) => {
  116. const o: Partial<T> = {};
  117. obj.k.forEach((k, i) => (o[k] = a[i]));
  118. return o as T;
  119. });
  120. }
  121. export function compressArrayZipEncode<T>(obj: T): string {
  122. const temp: Partial<T> = {};
  123. Object.entries(obj).forEach((kv) => {
  124. const v = kv[1];
  125. temp[kv[0] as keyof T] = Array.isArray(v) ? compressArray(v) : v;
  126. });
  127. return zipEncode(JSON.stringify(temp));
  128. }
  129. export function decodeUnzipRestoreArray<T>(str: string): T {
  130. const obj = JSON.parse(decodeUnzip(str));
  131. Object.keys(obj).forEach((k) => {
  132. const v = obj[k];
  133. if (typeof v == "object" && v && "k" in v && "v" in v) {
  134. obj[k] = restoreArray(v);
  135. }
  136. });
  137. return obj as T;
  138. }