@mtkruto/browser 0.0.6

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 (167) hide show
  1. package/LICENSE +165 -0
  2. package/README.md +1 -0
  3. package/esm/_dnt.shims.js +58 -0
  4. package/esm/client/client.js +169 -0
  5. package/esm/client/client_abstract.js +51 -0
  6. package/esm/client/client_plain.js +112 -0
  7. package/esm/connection/connection.js +2 -0
  8. package/esm/connection/connection_web_socket.js +89 -0
  9. package/esm/constants.js +60 -0
  10. package/esm/deps/deno.land/std@0.186.0/fmt/colors.js +474 -0
  11. package/esm/deps/deno.land/std@0.186.0/testing/_diff.js +311 -0
  12. package/esm/deps/deno.land/std@0.186.0/testing/_format.js +23 -0
  13. package/esm/deps/deno.land/std@0.186.0/testing/asserts.js +633 -0
  14. package/esm/deps/deno.land/x/crc32@v0.2.0/mod.js +105 -0
  15. package/esm/deps/deno.land/x/tgcrypto@0.1.1/mod.js +68 -0
  16. package/esm/deps/deno.land/x/tgcrypto@0.1.1/tgcrypto.js +1127 -0
  17. package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/gzip/gzip.js +238 -0
  18. package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/utils/uint8.js +10 -0
  19. package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/deflate.js +139 -0
  20. package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/inflate.js +176 -0
  21. package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/mod.js +2 -0
  22. package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/adler32.js +19 -0
  23. package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/crc32.js +25 -0
  24. package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/deflate.js +1941 -0
  25. package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/gzheader.js +85 -0
  26. package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/inffast.js +321 -0
  27. package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/inflate.js +1658 -0
  28. package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/inftrees.js +355 -0
  29. package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/messages.js +11 -0
  30. package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/status.js +42 -0
  31. package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/trees.js +1107 -0
  32. package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/zstream.js +86 -0
  33. package/esm/deps.js +4 -0
  34. package/esm/mod.js +4 -0
  35. package/esm/package.json +3 -0
  36. package/esm/tl/0_tl_raw_reader.js +62 -0
  37. package/esm/tl/0_tl_raw_writer.js +60 -0
  38. package/esm/tl/1_tl_object.js +149 -0
  39. package/esm/tl/2_types.js +48220 -0
  40. package/esm/tl/3_functions.js +19856 -0
  41. package/esm/tl/3_tl_object_deserializer.js +72 -0
  42. package/esm/tl/3_tl_reader.js +16 -0
  43. package/esm/tl/3_tl_writer.js +7 -0
  44. package/esm/tl/4_rpc_result.js +27 -0
  45. package/esm/tl/5_message.js +54 -0
  46. package/esm/tl/6_message_container.js +35 -0
  47. package/esm/transport/transport.js +32 -0
  48. package/esm/transport/transport_intermediate.js +56 -0
  49. package/esm/transport/transport_provider.js +23 -0
  50. package/esm/types.js +1 -0
  51. package/esm/utilities/0_bigint.js +36 -0
  52. package/esm/utilities/0_buffer.js +33 -0
  53. package/esm/utilities/0_crypto.js +35 -0
  54. package/esm/utilities/0_hash.js +6 -0
  55. package/esm/utilities/1_auth.js +28 -0
  56. package/esm/utilities/1_message.js +94 -0
  57. package/esm/utilities/1_obfuscation.js +32 -0
  58. package/package.json +29 -0
  59. package/script/_dnt.shims.js +61 -0
  60. package/script/client/client.js +173 -0
  61. package/script/client/client_abstract.js +55 -0
  62. package/script/client/client_plain.js +116 -0
  63. package/script/connection/connection.js +6 -0
  64. package/script/connection/connection_web_socket.js +93 -0
  65. package/script/constants.js +63 -0
  66. package/script/deps/deno.land/std@0.186.0/fmt/colors.js +548 -0
  67. package/script/deps/deno.land/std@0.186.0/testing/_diff.js +317 -0
  68. package/script/deps/deno.land/std@0.186.0/testing/_format.js +50 -0
  69. package/script/deps/deno.land/std@0.186.0/testing/asserts.js +659 -0
  70. package/script/deps/deno.land/x/crc32@v0.2.0/mod.js +113 -0
  71. package/script/deps/deno.land/x/tgcrypto@0.1.1/mod.js +82 -0
  72. package/script/deps/deno.land/x/tgcrypto@0.1.1/tgcrypto.js +1152 -0
  73. package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/gzip/gzip.js +247 -0
  74. package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/utils/uint8.js +14 -0
  75. package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/deflate.js +172 -0
  76. package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/inflate.js +208 -0
  77. package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/mod.js +18 -0
  78. package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/adler32.js +22 -0
  79. package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/crc32.js +30 -0
  80. package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/deflate.js +1976 -0
  81. package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/gzheader.js +88 -0
  82. package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/inffast.js +324 -0
  83. package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/inflate.js +1674 -0
  84. package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/inftrees.js +358 -0
  85. package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/messages.js +14 -0
  86. package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/status.js +44 -0
  87. package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/trees.js +1115 -0
  88. package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/zstream.js +89 -0
  89. package/script/deps.js +30 -0
  90. package/script/mod.js +32 -0
  91. package/script/package.json +3 -0
  92. package/script/tl/0_tl_raw_reader.js +66 -0
  93. package/script/tl/0_tl_raw_writer.js +64 -0
  94. package/script/tl/1_tl_object.js +156 -0
  95. package/script/tl/2_types.js +49824 -0
  96. package/script/tl/3_functions.js +20413 -0
  97. package/script/tl/3_tl_object_deserializer.js +76 -0
  98. package/script/tl/3_tl_reader.js +20 -0
  99. package/script/tl/3_tl_writer.js +11 -0
  100. package/script/tl/4_rpc_result.js +31 -0
  101. package/script/tl/5_message.js +58 -0
  102. package/script/tl/6_message_container.js +39 -0
  103. package/script/transport/transport.js +36 -0
  104. package/script/transport/transport_intermediate.js +60 -0
  105. package/script/transport/transport_provider.js +27 -0
  106. package/script/types.js +2 -0
  107. package/script/utilities/0_bigint.js +43 -0
  108. package/script/utilities/0_buffer.js +38 -0
  109. package/script/utilities/0_crypto.js +39 -0
  110. package/script/utilities/0_hash.js +11 -0
  111. package/script/utilities/1_auth.js +32 -0
  112. package/script/utilities/1_message.js +102 -0
  113. package/script/utilities/1_obfuscation.js +36 -0
  114. package/types/_dnt.shims.d.ts +1 -0
  115. package/types/client/client.d.ts +19 -0
  116. package/types/client/client_abstract.d.ts +20 -0
  117. package/types/client/client_plain.d.ts +10 -0
  118. package/types/connection/connection.d.ts +7 -0
  119. package/types/connection/connection_web_socket.d.ts +13 -0
  120. package/types/constants.d.ts +2 -0
  121. package/types/deps/deno.land/std@0.186.0/fmt/colors.d.ts +270 -0
  122. package/types/deps/deno.land/std@0.186.0/testing/_diff.d.ts +26 -0
  123. package/types/deps/deno.land/std@0.186.0/testing/_format.d.ts +1 -0
  124. package/types/deps/deno.land/std@0.186.0/testing/asserts.d.ts +284 -0
  125. package/types/deps/deno.land/x/crc32@v0.2.0/mod.d.ts +15 -0
  126. package/types/deps/deno.land/x/tgcrypto@0.1.1/mod.d.ts +47 -0
  127. package/types/deps/deno.land/x/tgcrypto@0.1.1/tgcrypto.d.ts +2 -0
  128. package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/gzip/gzip.d.ts +16 -0
  129. package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/utils/uint8.d.ts +1 -0
  130. package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/deflate.d.ts +29 -0
  131. package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/inflate.d.ts +23 -0
  132. package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/mod.d.ts +2 -0
  133. package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/adler32.d.ts +1 -0
  134. package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/crc32.d.ts +2 -0
  135. package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/deflate.d.ts +78 -0
  136. package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/gzheader.d.ts +12 -0
  137. package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/inffast.d.ts +1 -0
  138. package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/inflate.d.ts +47 -0
  139. package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/inftrees.d.ts +1 -0
  140. package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/messages.d.ts +12 -0
  141. package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/status.d.ts +30 -0
  142. package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/trees.d.ts +5 -0
  143. package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/zstream.d.ts +14 -0
  144. package/types/deps.d.ts +4 -0
  145. package/types/mod.d.ts +4 -0
  146. package/types/tl/0_tl_raw_reader.d.ts +13 -0
  147. package/types/tl/0_tl_raw_writer.d.ts +13 -0
  148. package/types/tl/1_tl_object.d.ts +36 -0
  149. package/types/tl/2_types.d.ts +14815 -0
  150. package/types/tl/3_functions.d.ts +6122 -0
  151. package/types/tl/3_tl_object_deserializer.d.ts +3 -0
  152. package/types/tl/3_tl_reader.d.ts +4 -0
  153. package/types/tl/3_tl_writer.d.ts +5 -0
  154. package/types/tl/4_rpc_result.d.ts +8 -0
  155. package/types/tl/5_message.d.ts +11 -0
  156. package/types/tl/6_message_container.d.ts +9 -0
  157. package/types/transport/transport.d.ts +15 -0
  158. package/types/transport/transport_intermediate.d.ts +11 -0
  159. package/types/transport/transport_provider.d.ts +12 -0
  160. package/types/types.d.ts +1 -0
  161. package/types/utilities/0_bigint.d.ts +4 -0
  162. package/types/utilities/0_buffer.d.ts +2 -0
  163. package/types/utilities/0_crypto.d.ts +8 -0
  164. package/types/utilities/0_hash.d.ts +2 -0
  165. package/types/utilities/1_auth.d.ts +1 -0
  166. package/types/utilities/1_message.d.ts +10 -0
  167. package/types/utilities/1_obfuscation.d.ts +6 -0
@@ -0,0 +1,238 @@
1
+ import { crc32 } from "../../../../../deno.land/x/crc32@v0.2.0/mod.js";
2
+ /** very fast */
3
+ import { deflateRaw, inflateRaw } from "../zlib/mod.js";
4
+ /** slow */
5
+ // import { deflateRaw, inflateRaw } from "../deflate/mod.ts";
6
+ // magic numbers marking this file as GZIP
7
+ const ID1 = 0x1F;
8
+ const ID2 = 0x8B;
9
+ const compressionMethods = {
10
+ "deflate": 8,
11
+ };
12
+ const possibleFlags = {
13
+ "FTEXT": 0x01,
14
+ "FHCRC": 0x02,
15
+ "FEXTRA": 0x04,
16
+ "FNAME": 0x08,
17
+ "FCOMMENT": 0x10,
18
+ };
19
+ // const osMap = {
20
+ // "fat": 0, // FAT file system (DOS, OS/2, NT) + PKZIPW 2.50 VFAT, NTFS
21
+ // "amiga": 1, // Amiga
22
+ // "vmz": 2, // VMS (VAX or Alpha AXP)
23
+ // "unix": 3, // Unix
24
+ // "vm/cms": 4, // VM/CMS
25
+ // "atari": 5, // Atari
26
+ // "hpfs": 6, // HPFS file system (OS/2, NT 3.x)
27
+ // "macintosh": 7, // Macintosh
28
+ // "z-system": 8, // Z-System
29
+ // "cplm": 9, // CP/M
30
+ // "tops-20": 10, // TOPS-20
31
+ // "ntfs": 11, // NTFS file system (NT)
32
+ // "qdos": 12, // SMS/QDOS
33
+ // "acorn": 13, // Acorn RISC OS
34
+ // "vfat": 14, // VFAT file system (Win95, NT)
35
+ // "vms": 15, // MVS (code also taken for PRIMOS)
36
+ // "beos": 16, // BeOS (BeBox or PowerMac)
37
+ // "tandem": 17, // Tandem/NSK
38
+ // "theos": 18, // THEOS
39
+ // };
40
+ const os = {
41
+ "darwin": 3,
42
+ "linux": 3,
43
+ "windows": 0,
44
+ };
45
+ // @ts-ignore
46
+ const osCode = os[globalThis["Deno"]?.build?.os] ?? 255;
47
+ export const DEFAULT_LEVEL = 6;
48
+ function putByte(n, arr) {
49
+ arr.push(n & 0xFF);
50
+ }
51
+ // LSB first
52
+ function putShort(n, arr) {
53
+ arr.push(n & 0xFF);
54
+ arr.push(n >>> 8);
55
+ }
56
+ // LSB first
57
+ export function putLong(n, arr) {
58
+ putShort(n & 0xffff, arr);
59
+ putShort(n >>> 16, arr);
60
+ }
61
+ function putString(s, arr) {
62
+ for (let i = 0, len = s.length; i < len; i += 1) {
63
+ putByte(s.charCodeAt(i), arr);
64
+ }
65
+ }
66
+ function readByte(arr) {
67
+ return arr.shift();
68
+ }
69
+ function readShort(arr) {
70
+ return arr.shift() | (arr.shift() << 8);
71
+ }
72
+ function readLong(arr) {
73
+ const n1 = readShort(arr);
74
+ let n2 = readShort(arr);
75
+ // JavaScript can't handle bits in the position 32
76
+ // we'll emulate this by removing the left-most bit (if it exists)
77
+ // and add it back in via multiplication, which does work
78
+ if (n2 > 32768) {
79
+ n2 -= 32768;
80
+ return ((n2 << 16) | n1) + 32768 * Math.pow(2, 16);
81
+ }
82
+ return (n2 << 16) | n1;
83
+ }
84
+ function readString(arr) {
85
+ const charArr = [];
86
+ // turn all bytes into chars until the terminating null
87
+ while (arr[0] !== 0) {
88
+ charArr.push(String.fromCharCode(arr.shift()));
89
+ }
90
+ // throw away terminating null
91
+ arr.shift();
92
+ // join all characters into a cohesive string
93
+ return charArr.join("");
94
+ }
95
+ function readBytes(arr, n) {
96
+ const ret = [];
97
+ for (let i = 0; i < n; i += 1) {
98
+ ret.push(arr.shift());
99
+ }
100
+ return ret;
101
+ }
102
+ export function getHeader(options = {}) {
103
+ let flags = 0;
104
+ const level = options.level ?? DEFAULT_LEVEL;
105
+ const out = [];
106
+ putByte(ID1, out);
107
+ putByte(ID2, out);
108
+ putByte(compressionMethods["deflate"], out);
109
+ if (options.name) {
110
+ flags |= possibleFlags["FNAME"];
111
+ }
112
+ putByte(flags, out);
113
+ putLong(options.timestamp ?? Math.floor(Date.now() / 1000), out);
114
+ // put deflate args (extra flags)
115
+ if (level === 1) {
116
+ // fastest algorithm
117
+ putByte(4, out);
118
+ }
119
+ else if (level === 9) {
120
+ // maximum compression (fastest algorithm)
121
+ putByte(2, out);
122
+ }
123
+ else {
124
+ putByte(0, out);
125
+ }
126
+ // OS identifier
127
+ putByte(osCode, out);
128
+ if (options.name) {
129
+ // ignore the directory part
130
+ putString(options.name.substring(options.name.lastIndexOf("/") + 1), out);
131
+ // terminating null
132
+ putByte(0, out);
133
+ }
134
+ return new Uint8Array(out);
135
+ }
136
+ export function gzip(bytes, options = {}) {
137
+ let flags = 0;
138
+ const level = options.level ?? DEFAULT_LEVEL;
139
+ const out = [];
140
+ putByte(ID1, out);
141
+ putByte(ID2, out);
142
+ putByte(compressionMethods["deflate"], out);
143
+ if (options.name) {
144
+ flags |= possibleFlags["FNAME"];
145
+ }
146
+ putByte(flags, out);
147
+ putLong(options.timestamp ?? Math.floor(Date.now() / 1000), out);
148
+ // put deflate args (extra flags)
149
+ if (level === 1) {
150
+ // fastest algorithm
151
+ putByte(4, out);
152
+ }
153
+ else if (level === 9) {
154
+ // maximum compression (fastest algorithm)
155
+ putByte(2, out);
156
+ }
157
+ else {
158
+ putByte(0, out);
159
+ }
160
+ // OS identifier
161
+ putByte(osCode, out);
162
+ if (options.name) {
163
+ // ignore the directory part
164
+ putString(options.name.substring(options.name.lastIndexOf("/") + 1), out);
165
+ // terminating null
166
+ putByte(0, out);
167
+ }
168
+ deflateRaw(bytes).forEach(function (byte) {
169
+ putByte(byte, out);
170
+ });
171
+ // import { deflateRaw, inflateRaw } from "../deflate/mod.ts";
172
+ // deflateRaw(bytes, level).forEach(function (byte) {
173
+ // putByte(byte, out);
174
+ // });
175
+ putLong(parseInt(crc32(bytes), 16), out);
176
+ putLong(bytes.length, out);
177
+ return new Uint8Array(out);
178
+ }
179
+ export function gunzip(bytes) {
180
+ const arr = Array.from(bytes);
181
+ checkHeader(arr);
182
+ // give deflate everything but the last 8 bytes
183
+ // the last 8 bytes are for the CRC32 checksum and filesize
184
+ const res = inflateRaw(new Uint8Array(arr.splice(0, arr.length - 8)));
185
+ // if (flags & possibleFlags["FTEXT"]) {
186
+ // res = Array.prototype.map.call(res, function (byte) {
187
+ // return String.fromCharCode(byte);
188
+ // }).join("");
189
+ // }
190
+ const crc = readLong(arr) >>> 0;
191
+ if (crc !== parseInt(crc32(res), 16)) {
192
+ throw "Checksum does not match";
193
+ }
194
+ const size = readLong(arr);
195
+ if (size !== res.length) {
196
+ throw "Size of decompressed file not correct";
197
+ }
198
+ return res;
199
+ }
200
+ export function checkHeader(arr) {
201
+ // check the first two bytes for the magic numbers
202
+ if (readByte(arr) !== ID1 || readByte(arr) !== ID2) {
203
+ throw "Not a GZIP file";
204
+ }
205
+ if (readByte(arr) !== 8) {
206
+ throw "Unsupported compression method";
207
+ }
208
+ const flags = readByte(arr);
209
+ readLong(arr); // mtime
210
+ readByte(arr); // xFlags
211
+ readByte(arr); // os, throw away
212
+ // just throw away the bytes for now
213
+ if (flags & possibleFlags["FEXTRA"]) {
214
+ const t = readShort(arr);
215
+ readBytes(arr, t);
216
+ }
217
+ // just throw away for now
218
+ if (flags & possibleFlags["FNAME"]) {
219
+ readString(arr);
220
+ }
221
+ // just throw away for now
222
+ if (flags & possibleFlags["FCOMMENT"]) {
223
+ readString(arr);
224
+ }
225
+ // just throw away for now
226
+ if (flags & possibleFlags["FHCRC"]) {
227
+ readShort(arr);
228
+ }
229
+ }
230
+ export function checkTail(arr) {
231
+ const tail = arr.splice(arr.length - 8);
232
+ const crc32 = readLong(tail) >>> 0;
233
+ const size = readLong(tail);
234
+ return {
235
+ crc32,
236
+ size,
237
+ };
238
+ }
@@ -0,0 +1,10 @@
1
+ export function concatUint8Array(arr) {
2
+ const length = arr.reduce((pre, next) => pre + next.length, 0);
3
+ const result = new Uint8Array(length);
4
+ let offset = 0;
5
+ for (const v of arr) {
6
+ result.set(v, offset);
7
+ offset += v.length;
8
+ }
9
+ return result;
10
+ }
@@ -0,0 +1,139 @@
1
+ // from https://github.com/nodeca/pako
2
+ import * as zlibDeflate from "./zlib/deflate.js";
3
+ import { concatUint8Array } from "../utils/uint8.js";
4
+ import { message as msg } from "./zlib/messages.js";
5
+ import ZStream from "./zlib/zstream.js";
6
+ import STATUS from "./zlib/status.js";
7
+ export class Deflate {
8
+ constructor(options = {}) {
9
+ Object.defineProperty(this, "err", {
10
+ enumerable: true,
11
+ configurable: true,
12
+ writable: true,
13
+ value: 0
14
+ }); // error code, if happens (0 = Z_OK)
15
+ Object.defineProperty(this, "msg", {
16
+ enumerable: true,
17
+ configurable: true,
18
+ writable: true,
19
+ value: ""
20
+ }); // error message
21
+ Object.defineProperty(this, "ended", {
22
+ enumerable: true,
23
+ configurable: true,
24
+ writable: true,
25
+ value: false
26
+ }); // used to avoid multiple onEnd() calls
27
+ Object.defineProperty(this, "strm", {
28
+ enumerable: true,
29
+ configurable: true,
30
+ writable: true,
31
+ value: void 0
32
+ });
33
+ Object.defineProperty(this, "_dict_set", {
34
+ enumerable: true,
35
+ configurable: true,
36
+ writable: true,
37
+ value: false
38
+ });
39
+ Object.defineProperty(this, "options", {
40
+ enumerable: true,
41
+ configurable: true,
42
+ writable: true,
43
+ value: void 0
44
+ });
45
+ this.options = Object.assign({
46
+ level: STATUS.Z_DEFAULT_COMPRESSION,
47
+ method: STATUS.Z_DEFLATED,
48
+ chunkSize: 16384,
49
+ windowBits: 15,
50
+ memLevel: 8,
51
+ strategy: STATUS.Z_DEFAULT_STRATEGY,
52
+ to: "",
53
+ }, options);
54
+ const opt = this.options;
55
+ if (opt.raw && (opt.windowBits > 0)) {
56
+ opt.windowBits = -opt.windowBits;
57
+ }
58
+ else if (opt.gzip && (opt.windowBits > 0) && (opt.windowBits < 16)) {
59
+ opt.windowBits += 16;
60
+ }
61
+ this.strm = new ZStream();
62
+ this.strm.avail_out = 0;
63
+ let status = zlibDeflate.deflateInit2(this.strm, opt.level, opt.method, opt.windowBits, opt.memLevel, opt.strategy);
64
+ if (status !== STATUS.Z_OK) {
65
+ throw new Error(msg[status]);
66
+ }
67
+ if (opt.header) {
68
+ zlibDeflate.deflateSetHeader(this.strm, opt.header);
69
+ }
70
+ if (opt.dictionary) {
71
+ status = zlibDeflate.deflateSetDictionary(this.strm, opt.dictionary);
72
+ if (status !== STATUS.Z_OK) {
73
+ throw new Error(msg[status]);
74
+ }
75
+ this._dict_set = true;
76
+ }
77
+ }
78
+ push(data, mode) {
79
+ const strm = this.strm;
80
+ const chunkSize = this.options.chunkSize;
81
+ const chunks = [];
82
+ let status;
83
+ if (this.ended) {
84
+ throw new Error("can not call after ended");
85
+ }
86
+ const _mode = mode === ~~mode
87
+ ? mode
88
+ : (mode === true ? STATUS.Z_FINISH : STATUS.Z_NO_FLUSH);
89
+ strm.input = data;
90
+ strm.next_in = 0;
91
+ strm.avail_in = strm.input.length;
92
+ do {
93
+ if (strm.avail_out === 0) {
94
+ strm.output = new Uint8Array(chunkSize);
95
+ strm.next_out = 0;
96
+ strm.avail_out = chunkSize;
97
+ }
98
+ status = zlibDeflate.deflate(strm, _mode); /* no bad return value */
99
+ if (status !== STATUS.Z_STREAM_END && status !== STATUS.Z_OK) {
100
+ this.ended = true;
101
+ throw new Error(this.strm.msg);
102
+ }
103
+ if (strm.avail_out === 0 ||
104
+ (strm.avail_in === 0 &&
105
+ (_mode === STATUS.Z_FINISH || _mode === STATUS.Z_SYNC_FLUSH))) {
106
+ chunks.push(strm.output.subarray(0, strm.next_out));
107
+ }
108
+ } while ((strm.avail_in > 0 || strm.avail_out === 0) &&
109
+ status !== STATUS.Z_STREAM_END);
110
+ // Finalize on the last chunk.
111
+ if (_mode === STATUS.Z_FINISH) {
112
+ status = zlibDeflate.deflateEnd(this.strm);
113
+ this.ended = true;
114
+ if (status !== STATUS.Z_OK)
115
+ throw new Error(this.strm.msg);
116
+ }
117
+ // callback interim results if Z_SYNC_FLUSH.
118
+ if (_mode === STATUS.Z_SYNC_FLUSH) {
119
+ strm.avail_out = 0;
120
+ }
121
+ return concatUint8Array(chunks);
122
+ }
123
+ }
124
+ export function deflate(input, options = {}) {
125
+ const deflator = new Deflate(options);
126
+ const result = deflator.push(input, true);
127
+ // That will never happens, if you don't cheat with options :)
128
+ if (deflator.err)
129
+ throw deflator.msg || msg[deflator.err];
130
+ return result;
131
+ }
132
+ export function deflateRaw(input, options = {}) {
133
+ options.raw = true;
134
+ return deflate(input, options);
135
+ }
136
+ export function gzip(input, options = {}) {
137
+ options.gzip = true;
138
+ return deflate(input, options);
139
+ }
@@ -0,0 +1,176 @@
1
+ // from https://github.com/nodeca/pako
2
+ import { concatUint8Array } from "../utils/uint8.js";
3
+ import * as zlibInflate from "./zlib/inflate.js";
4
+ import STATUS from "./zlib/status.js";
5
+ import { message as msg } from "./zlib/messages.js";
6
+ import ZStream from "./zlib/zstream.js";
7
+ import GZheader from "./zlib/gzheader.js";
8
+ export class Inflate {
9
+ constructor(options) {
10
+ Object.defineProperty(this, "err", {
11
+ enumerable: true,
12
+ configurable: true,
13
+ writable: true,
14
+ value: 0
15
+ }); // error code, if happens (0 = Z_OK)
16
+ Object.defineProperty(this, "msg", {
17
+ enumerable: true,
18
+ configurable: true,
19
+ writable: true,
20
+ value: ""
21
+ }); // error message
22
+ Object.defineProperty(this, "ended", {
23
+ enumerable: true,
24
+ configurable: true,
25
+ writable: true,
26
+ value: false
27
+ }); // used to avoid multiple onEnd() calls
28
+ Object.defineProperty(this, "strm", {
29
+ enumerable: true,
30
+ configurable: true,
31
+ writable: true,
32
+ value: void 0
33
+ });
34
+ Object.defineProperty(this, "options", {
35
+ enumerable: true,
36
+ configurable: true,
37
+ writable: true,
38
+ value: void 0
39
+ });
40
+ Object.defineProperty(this, "header", {
41
+ enumerable: true,
42
+ configurable: true,
43
+ writable: true,
44
+ value: void 0
45
+ });
46
+ this.options = {
47
+ chunkSize: 16384,
48
+ windowBits: 0,
49
+ to: "",
50
+ ...options,
51
+ };
52
+ const opt = this.options;
53
+ // Force window size for `raw` data, if not set directly,
54
+ // because we have no header for autodetect.
55
+ if (opt.raw && (opt.windowBits >= 0) && (opt.windowBits < 16)) {
56
+ opt.windowBits = -opt.windowBits;
57
+ if (opt.windowBits === 0)
58
+ opt.windowBits = -15;
59
+ }
60
+ // If `windowBits` not defined (and mode not raw) - set autodetect flag for gzip/deflate
61
+ if ((opt.windowBits >= 0) && (opt.windowBits < 16) &&
62
+ !(options && options.windowBits)) {
63
+ opt.windowBits += 32;
64
+ }
65
+ // Gzip header has no info about windows size, we can do autodetect only
66
+ // for deflate. So, if window size not set, force it to max when gzip possible
67
+ if ((opt.windowBits > 15) && (opt.windowBits < 48)) {
68
+ // bit 3 (16) -> gzipped data
69
+ // bit 4 (32) -> autodetect gzip/deflate
70
+ if ((opt.windowBits & 15) === 0) {
71
+ opt.windowBits |= 15;
72
+ }
73
+ }
74
+ this.strm = new ZStream();
75
+ this.strm.avail_out = 0;
76
+ var status = zlibInflate.inflateInit2(this.strm, opt.windowBits);
77
+ if (status !== STATUS.Z_OK) {
78
+ throw new Error(msg[status]);
79
+ }
80
+ this.header = new GZheader();
81
+ zlibInflate.inflateGetHeader(this.strm, this.header);
82
+ // Setup dictionary
83
+ if (opt.dictionary) {
84
+ if (opt.raw) { //In raw mode we need to set the dictionary early
85
+ status = zlibInflate.inflateSetDictionary(this.strm, opt.dictionary);
86
+ if (status !== STATUS.Z_OK) {
87
+ throw new Error(msg[status]);
88
+ }
89
+ }
90
+ }
91
+ }
92
+ push(data, mode) {
93
+ const strm = this.strm;
94
+ const chunkSize = this.options.chunkSize;
95
+ const dictionary = this.options.dictionary;
96
+ const chunks = [];
97
+ let status;
98
+ // Flag to properly process Z_BUF_ERROR on testing inflate call
99
+ // when we check that all output data was flushed.
100
+ var allowBufError = false;
101
+ if (this.ended) {
102
+ throw new Error("can not call after ended");
103
+ }
104
+ let _mode = (mode === ~~mode)
105
+ ? mode
106
+ : ((mode === true) ? STATUS.Z_FINISH : STATUS.Z_NO_FLUSH);
107
+ strm.input = data;
108
+ strm.next_in = 0;
109
+ strm.avail_in = strm.input.length;
110
+ do {
111
+ if (strm.avail_out === 0) {
112
+ strm.output = new Uint8Array(chunkSize);
113
+ strm.next_out = 0;
114
+ strm.avail_out = chunkSize;
115
+ }
116
+ status = zlibInflate.inflate(strm, STATUS.Z_NO_FLUSH); /* no bad return value */
117
+ if (status === STATUS.Z_NEED_DICT && dictionary) {
118
+ status = zlibInflate.inflateSetDictionary(this.strm, dictionary);
119
+ }
120
+ if (status === STATUS.Z_BUF_ERROR && allowBufError === true) {
121
+ status = STATUS.Z_OK;
122
+ allowBufError = false;
123
+ }
124
+ if (status !== STATUS.Z_STREAM_END && status !== STATUS.Z_OK) {
125
+ this.ended = true;
126
+ throw new Error(this.strm.msg);
127
+ }
128
+ if (strm.next_out) {
129
+ if (strm.avail_out === 0 || status === STATUS.Z_STREAM_END ||
130
+ (strm.avail_in === 0 &&
131
+ (_mode === STATUS.Z_FINISH || _mode === STATUS.Z_SYNC_FLUSH))) {
132
+ chunks.push(strm.output.subarray(0, strm.next_out));
133
+ }
134
+ }
135
+ // When no more input data, we should check that internal inflate buffers
136
+ // are flushed. The only way to do it when avail_out = 0 - run one more
137
+ // inflate pass. But if output data not exists, inflate return Z_BUF_ERROR.
138
+ // Here we set flag to process this error properly.
139
+ //
140
+ // NOTE. Deflate does not return error in this case and does not needs such
141
+ // logic.
142
+ if (strm.avail_in === 0 && strm.avail_out === 0) {
143
+ allowBufError = true;
144
+ }
145
+ } while ((strm.avail_in > 0 || strm.avail_out === 0) &&
146
+ status !== STATUS.Z_STREAM_END);
147
+ if (status === STATUS.Z_STREAM_END) {
148
+ _mode = STATUS.Z_FINISH;
149
+ }
150
+ // Finalize on the last chunk.
151
+ if (_mode === STATUS.Z_FINISH) {
152
+ status = zlibInflate.inflateEnd(this.strm);
153
+ this.ended = true;
154
+ if (status !== STATUS.Z_OK)
155
+ throw new Error(this.strm.msg);
156
+ }
157
+ // callback interim results if Z_SYNC_FLUSH.
158
+ if (_mode === STATUS.Z_SYNC_FLUSH) {
159
+ strm.avail_out = 0;
160
+ }
161
+ return concatUint8Array(chunks);
162
+ }
163
+ }
164
+ export function inflate(input, options = {}) {
165
+ const inflator = new Inflate(options);
166
+ const result = inflator.push(input, true);
167
+ // That will never happens, if you don't cheat with options :)
168
+ if (inflator.err)
169
+ throw inflator.msg || msg[inflator.err];
170
+ return result;
171
+ }
172
+ export function inflateRaw(input, options = {}) {
173
+ options.raw = true;
174
+ return inflate(input, options);
175
+ }
176
+ export const gunzip = inflate;
@@ -0,0 +1,2 @@
1
+ export * from "./deflate.js";
2
+ export * from "./inflate.js";
@@ -0,0 +1,19 @@
1
+ export default function adler32(adler, buf, len, pos) {
2
+ let s1 = (adler & 0xffff) | 0;
3
+ let s2 = ((adler >>> 16) & 0xffff) | 0;
4
+ let n = 0;
5
+ while (len !== 0) {
6
+ // Set limit ~ twice less than 5552, to keep
7
+ // s2 in 31-bits, because we force signed ints.
8
+ // in other case %= will fail.
9
+ n = len > 2000 ? 2000 : len;
10
+ len -= n;
11
+ do {
12
+ s1 = (s1 + buf[pos++]) | 0;
13
+ s2 = (s2 + s1) | 0;
14
+ } while (--n);
15
+ s1 %= 65521;
16
+ s2 %= 65521;
17
+ }
18
+ return (s1 | (s2 << 16)) | 0;
19
+ }
@@ -0,0 +1,25 @@
1
+ export function makeTable() {
2
+ let c;
3
+ const table = [];
4
+ const m = 0xEDB88320;
5
+ for (let n = 0; n < 256; n++) {
6
+ c = n;
7
+ for (let k = 0; k < 8; k++) {
8
+ c = ((c & 1) ? (m ^ (c >>> 1)) : (c >>> 1));
9
+ }
10
+ table[n] = c;
11
+ }
12
+ return table;
13
+ }
14
+ // Create table on load. Just 255 signed longs. Not a problem.
15
+ const crcTable = makeTable();
16
+ export function crc32(crc, buf, len, pos) {
17
+ let t = crcTable;
18
+ let end = pos + len;
19
+ let f = 0xFF;
20
+ crc ^= -1;
21
+ for (let i = pos; i < end; i++) {
22
+ crc = (crc >>> 8) ^ t[(crc ^ buf[i]) & f];
23
+ }
24
+ return (crc ^ (-1)); // >>> 0;
25
+ }