@mtkruto/node 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.
- package/LICENSE +165 -0
- package/README.md +1 -0
- package/esm/_dnt.shims.js +68 -0
- package/esm/client/client.js +169 -0
- package/esm/client/client_abstract.js +51 -0
- package/esm/client/client_plain.js +112 -0
- package/esm/connection/connection.js +2 -0
- package/esm/connection/connection_web_socket.js +90 -0
- package/esm/constants.js +60 -0
- package/esm/deps/deno.land/std@0.186.0/fmt/colors.js +474 -0
- package/esm/deps/deno.land/std@0.186.0/testing/_diff.js +311 -0
- package/esm/deps/deno.land/std@0.186.0/testing/_format.js +23 -0
- package/esm/deps/deno.land/std@0.186.0/testing/asserts.js +633 -0
- package/esm/deps/deno.land/x/crc32@v0.2.0/mod.js +105 -0
- package/esm/deps/deno.land/x/tgcrypto@0.1.1/mod.js +68 -0
- package/esm/deps/deno.land/x/tgcrypto@0.1.1/tgcrypto.js +1127 -0
- package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/gzip/gzip.js +239 -0
- package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/utils/uint8.js +10 -0
- package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/deflate.js +139 -0
- package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/inflate.js +176 -0
- package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/mod.js +2 -0
- package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/adler32.js +19 -0
- package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/crc32.js +25 -0
- package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/deflate.js +1941 -0
- package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/gzheader.js +85 -0
- package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/inffast.js +321 -0
- package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/inflate.js +1658 -0
- package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/inftrees.js +355 -0
- package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/messages.js +11 -0
- package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/status.js +42 -0
- package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/trees.js +1107 -0
- package/esm/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/zstream.js +86 -0
- package/esm/deps.js +4 -0
- package/esm/mod.js +4 -0
- package/esm/package.json +3 -0
- package/esm/tl/0_tl_raw_reader.js +62 -0
- package/esm/tl/0_tl_raw_writer.js +60 -0
- package/esm/tl/1_tl_object.js +149 -0
- package/esm/tl/2_types.js +48220 -0
- package/esm/tl/3_functions.js +19856 -0
- package/esm/tl/3_tl_object_deserializer.js +72 -0
- package/esm/tl/3_tl_reader.js +16 -0
- package/esm/tl/3_tl_writer.js +7 -0
- package/esm/tl/4_rpc_result.js +27 -0
- package/esm/tl/5_message.js +54 -0
- package/esm/tl/6_message_container.js +35 -0
- package/esm/transport/transport.js +32 -0
- package/esm/transport/transport_intermediate.js +56 -0
- package/esm/transport/transport_provider.js +23 -0
- package/esm/types.js +1 -0
- package/esm/utilities/0_bigint.js +37 -0
- package/esm/utilities/0_buffer.js +33 -0
- package/esm/utilities/0_crypto.js +35 -0
- package/esm/utilities/0_hash.js +7 -0
- package/esm/utilities/1_auth.js +29 -0
- package/esm/utilities/1_message.js +94 -0
- package/esm/utilities/1_obfuscation.js +33 -0
- package/package.json +40 -0
- package/script/_dnt.shims.js +77 -0
- package/script/client/client.js +173 -0
- package/script/client/client_abstract.js +55 -0
- package/script/client/client_plain.js +116 -0
- package/script/connection/connection.js +6 -0
- package/script/connection/connection_web_socket.js +117 -0
- package/script/constants.js +63 -0
- package/script/deps/deno.land/std@0.186.0/fmt/colors.js +548 -0
- package/script/deps/deno.land/std@0.186.0/testing/_diff.js +317 -0
- package/script/deps/deno.land/std@0.186.0/testing/_format.js +50 -0
- package/script/deps/deno.land/std@0.186.0/testing/asserts.js +659 -0
- package/script/deps/deno.land/x/crc32@v0.2.0/mod.js +113 -0
- package/script/deps/deno.land/x/tgcrypto@0.1.1/mod.js +82 -0
- package/script/deps/deno.land/x/tgcrypto@0.1.1/tgcrypto.js +1152 -0
- package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/gzip/gzip.js +271 -0
- package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/utils/uint8.js +14 -0
- package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/deflate.js +172 -0
- package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/inflate.js +208 -0
- package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/mod.js +18 -0
- package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/adler32.js +22 -0
- package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/crc32.js +30 -0
- package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/deflate.js +1976 -0
- package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/gzheader.js +88 -0
- package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/inffast.js +324 -0
- package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/inflate.js +1674 -0
- package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/inftrees.js +358 -0
- package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/messages.js +14 -0
- package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/status.js +44 -0
- package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/trees.js +1115 -0
- package/script/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/zstream.js +89 -0
- package/script/deps.js +30 -0
- package/script/mod.js +32 -0
- package/script/package.json +3 -0
- package/script/tl/0_tl_raw_reader.js +66 -0
- package/script/tl/0_tl_raw_writer.js +64 -0
- package/script/tl/1_tl_object.js +156 -0
- package/script/tl/2_types.js +49824 -0
- package/script/tl/3_functions.js +20413 -0
- package/script/tl/3_tl_object_deserializer.js +76 -0
- package/script/tl/3_tl_reader.js +20 -0
- package/script/tl/3_tl_writer.js +11 -0
- package/script/tl/4_rpc_result.js +31 -0
- package/script/tl/5_message.js +58 -0
- package/script/tl/6_message_container.js +39 -0
- package/script/transport/transport.js +36 -0
- package/script/transport/transport_intermediate.js +60 -0
- package/script/transport/transport_provider.js +27 -0
- package/script/types.js +2 -0
- package/script/utilities/0_bigint.js +67 -0
- package/script/utilities/0_buffer.js +38 -0
- package/script/utilities/0_crypto.js +39 -0
- package/script/utilities/0_hash.js +35 -0
- package/script/utilities/1_auth.js +56 -0
- package/script/utilities/1_message.js +102 -0
- package/script/utilities/1_obfuscation.js +60 -0
- package/types/_dnt.shims.d.ts +10 -0
- package/types/client/client.d.ts +19 -0
- package/types/client/client_abstract.d.ts +20 -0
- package/types/client/client_plain.d.ts +10 -0
- package/types/connection/connection.d.ts +7 -0
- package/types/connection/connection_web_socket.d.ts +13 -0
- package/types/constants.d.ts +2 -0
- package/types/deps/deno.land/std@0.186.0/fmt/colors.d.ts +270 -0
- package/types/deps/deno.land/std@0.186.0/testing/_diff.d.ts +26 -0
- package/types/deps/deno.land/std@0.186.0/testing/_format.d.ts +1 -0
- package/types/deps/deno.land/std@0.186.0/testing/asserts.d.ts +284 -0
- package/types/deps/deno.land/x/crc32@v0.2.0/mod.d.ts +15 -0
- package/types/deps/deno.land/x/tgcrypto@0.1.1/mod.d.ts +47 -0
- package/types/deps/deno.land/x/tgcrypto@0.1.1/tgcrypto.d.ts +2 -0
- package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/gzip/gzip.d.ts +16 -0
- package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/utils/uint8.d.ts +1 -0
- package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/deflate.d.ts +29 -0
- package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/inflate.d.ts +23 -0
- package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/mod.d.ts +2 -0
- package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/adler32.d.ts +1 -0
- package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/crc32.d.ts +2 -0
- package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/deflate.d.ts +78 -0
- package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/gzheader.d.ts +12 -0
- package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/inffast.d.ts +1 -0
- package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/inflate.d.ts +47 -0
- package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/inftrees.d.ts +1 -0
- package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/messages.d.ts +12 -0
- package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/status.d.ts +30 -0
- package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/trees.d.ts +5 -0
- package/types/deps/raw.githubusercontent.com/MTKruto/compress/master/zlib/zlib/zstream.d.ts +14 -0
- package/types/deps.d.ts +4 -0
- package/types/mod.d.ts +4 -0
- package/types/tl/0_tl_raw_reader.d.ts +13 -0
- package/types/tl/0_tl_raw_writer.d.ts +13 -0
- package/types/tl/1_tl_object.d.ts +36 -0
- package/types/tl/2_types.d.ts +14815 -0
- package/types/tl/3_functions.d.ts +6122 -0
- package/types/tl/3_tl_object_deserializer.d.ts +3 -0
- package/types/tl/3_tl_reader.d.ts +4 -0
- package/types/tl/3_tl_writer.d.ts +5 -0
- package/types/tl/4_rpc_result.d.ts +8 -0
- package/types/tl/5_message.d.ts +11 -0
- package/types/tl/6_message_container.d.ts +9 -0
- package/types/transport/transport.d.ts +15 -0
- package/types/transport/transport_intermediate.d.ts +11 -0
- package/types/transport/transport_provider.d.ts +12 -0
- package/types/types.d.ts +1 -0
- package/types/utilities/0_bigint.d.ts +4 -0
- package/types/utilities/0_buffer.d.ts +2 -0
- package/types/utilities/0_crypto.d.ts +8 -0
- package/types/utilities/0_hash.d.ts +2 -0
- package/types/utilities/1_auth.d.ts +1 -0
- package/types/utilities/1_message.d.ts +10 -0
- package/types/utilities/1_obfuscation.d.ts +6 -0
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import * as dntShim from "../../../../../../_dnt.shims.js";
|
|
2
|
+
import { crc32 } from "../../../../../deno.land/x/crc32@v0.2.0/mod.js";
|
|
3
|
+
/** very fast */
|
|
4
|
+
import { deflateRaw, inflateRaw } from "../zlib/mod.js";
|
|
5
|
+
/** slow */
|
|
6
|
+
// import { deflateRaw, inflateRaw } from "../deflate/mod.ts";
|
|
7
|
+
// magic numbers marking this file as GZIP
|
|
8
|
+
const ID1 = 0x1F;
|
|
9
|
+
const ID2 = 0x8B;
|
|
10
|
+
const compressionMethods = {
|
|
11
|
+
"deflate": 8,
|
|
12
|
+
};
|
|
13
|
+
const possibleFlags = {
|
|
14
|
+
"FTEXT": 0x01,
|
|
15
|
+
"FHCRC": 0x02,
|
|
16
|
+
"FEXTRA": 0x04,
|
|
17
|
+
"FNAME": 0x08,
|
|
18
|
+
"FCOMMENT": 0x10,
|
|
19
|
+
};
|
|
20
|
+
// const osMap = {
|
|
21
|
+
// "fat": 0, // FAT file system (DOS, OS/2, NT) + PKZIPW 2.50 VFAT, NTFS
|
|
22
|
+
// "amiga": 1, // Amiga
|
|
23
|
+
// "vmz": 2, // VMS (VAX or Alpha AXP)
|
|
24
|
+
// "unix": 3, // Unix
|
|
25
|
+
// "vm/cms": 4, // VM/CMS
|
|
26
|
+
// "atari": 5, // Atari
|
|
27
|
+
// "hpfs": 6, // HPFS file system (OS/2, NT 3.x)
|
|
28
|
+
// "macintosh": 7, // Macintosh
|
|
29
|
+
// "z-system": 8, // Z-System
|
|
30
|
+
// "cplm": 9, // CP/M
|
|
31
|
+
// "tops-20": 10, // TOPS-20
|
|
32
|
+
// "ntfs": 11, // NTFS file system (NT)
|
|
33
|
+
// "qdos": 12, // SMS/QDOS
|
|
34
|
+
// "acorn": 13, // Acorn RISC OS
|
|
35
|
+
// "vfat": 14, // VFAT file system (Win95, NT)
|
|
36
|
+
// "vms": 15, // MVS (code also taken for PRIMOS)
|
|
37
|
+
// "beos": 16, // BeOS (BeBox or PowerMac)
|
|
38
|
+
// "tandem": 17, // Tandem/NSK
|
|
39
|
+
// "theos": 18, // THEOS
|
|
40
|
+
// };
|
|
41
|
+
const os = {
|
|
42
|
+
"darwin": 3,
|
|
43
|
+
"linux": 3,
|
|
44
|
+
"windows": 0,
|
|
45
|
+
};
|
|
46
|
+
// @ts-ignore
|
|
47
|
+
const osCode = os[dntShim.dntGlobalThis["Deno"]?.build?.os] ?? 255;
|
|
48
|
+
export const DEFAULT_LEVEL = 6;
|
|
49
|
+
function putByte(n, arr) {
|
|
50
|
+
arr.push(n & 0xFF);
|
|
51
|
+
}
|
|
52
|
+
// LSB first
|
|
53
|
+
function putShort(n, arr) {
|
|
54
|
+
arr.push(n & 0xFF);
|
|
55
|
+
arr.push(n >>> 8);
|
|
56
|
+
}
|
|
57
|
+
// LSB first
|
|
58
|
+
export function putLong(n, arr) {
|
|
59
|
+
putShort(n & 0xffff, arr);
|
|
60
|
+
putShort(n >>> 16, arr);
|
|
61
|
+
}
|
|
62
|
+
function putString(s, arr) {
|
|
63
|
+
for (let i = 0, len = s.length; i < len; i += 1) {
|
|
64
|
+
putByte(s.charCodeAt(i), arr);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
function readByte(arr) {
|
|
68
|
+
return arr.shift();
|
|
69
|
+
}
|
|
70
|
+
function readShort(arr) {
|
|
71
|
+
return arr.shift() | (arr.shift() << 8);
|
|
72
|
+
}
|
|
73
|
+
function readLong(arr) {
|
|
74
|
+
const n1 = readShort(arr);
|
|
75
|
+
let n2 = readShort(arr);
|
|
76
|
+
// JavaScript can't handle bits in the position 32
|
|
77
|
+
// we'll emulate this by removing the left-most bit (if it exists)
|
|
78
|
+
// and add it back in via multiplication, which does work
|
|
79
|
+
if (n2 > 32768) {
|
|
80
|
+
n2 -= 32768;
|
|
81
|
+
return ((n2 << 16) | n1) + 32768 * Math.pow(2, 16);
|
|
82
|
+
}
|
|
83
|
+
return (n2 << 16) | n1;
|
|
84
|
+
}
|
|
85
|
+
function readString(arr) {
|
|
86
|
+
const charArr = [];
|
|
87
|
+
// turn all bytes into chars until the terminating null
|
|
88
|
+
while (arr[0] !== 0) {
|
|
89
|
+
charArr.push(String.fromCharCode(arr.shift()));
|
|
90
|
+
}
|
|
91
|
+
// throw away terminating null
|
|
92
|
+
arr.shift();
|
|
93
|
+
// join all characters into a cohesive string
|
|
94
|
+
return charArr.join("");
|
|
95
|
+
}
|
|
96
|
+
function readBytes(arr, n) {
|
|
97
|
+
const ret = [];
|
|
98
|
+
for (let i = 0; i < n; i += 1) {
|
|
99
|
+
ret.push(arr.shift());
|
|
100
|
+
}
|
|
101
|
+
return ret;
|
|
102
|
+
}
|
|
103
|
+
export function getHeader(options = {}) {
|
|
104
|
+
let flags = 0;
|
|
105
|
+
const level = options.level ?? DEFAULT_LEVEL;
|
|
106
|
+
const out = [];
|
|
107
|
+
putByte(ID1, out);
|
|
108
|
+
putByte(ID2, out);
|
|
109
|
+
putByte(compressionMethods["deflate"], out);
|
|
110
|
+
if (options.name) {
|
|
111
|
+
flags |= possibleFlags["FNAME"];
|
|
112
|
+
}
|
|
113
|
+
putByte(flags, out);
|
|
114
|
+
putLong(options.timestamp ?? Math.floor(Date.now() / 1000), out);
|
|
115
|
+
// put deflate args (extra flags)
|
|
116
|
+
if (level === 1) {
|
|
117
|
+
// fastest algorithm
|
|
118
|
+
putByte(4, out);
|
|
119
|
+
}
|
|
120
|
+
else if (level === 9) {
|
|
121
|
+
// maximum compression (fastest algorithm)
|
|
122
|
+
putByte(2, out);
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
putByte(0, out);
|
|
126
|
+
}
|
|
127
|
+
// OS identifier
|
|
128
|
+
putByte(osCode, out);
|
|
129
|
+
if (options.name) {
|
|
130
|
+
// ignore the directory part
|
|
131
|
+
putString(options.name.substring(options.name.lastIndexOf("/") + 1), out);
|
|
132
|
+
// terminating null
|
|
133
|
+
putByte(0, out);
|
|
134
|
+
}
|
|
135
|
+
return new Uint8Array(out);
|
|
136
|
+
}
|
|
137
|
+
export function gzip(bytes, options = {}) {
|
|
138
|
+
let flags = 0;
|
|
139
|
+
const level = options.level ?? DEFAULT_LEVEL;
|
|
140
|
+
const out = [];
|
|
141
|
+
putByte(ID1, out);
|
|
142
|
+
putByte(ID2, out);
|
|
143
|
+
putByte(compressionMethods["deflate"], out);
|
|
144
|
+
if (options.name) {
|
|
145
|
+
flags |= possibleFlags["FNAME"];
|
|
146
|
+
}
|
|
147
|
+
putByte(flags, out);
|
|
148
|
+
putLong(options.timestamp ?? Math.floor(Date.now() / 1000), out);
|
|
149
|
+
// put deflate args (extra flags)
|
|
150
|
+
if (level === 1) {
|
|
151
|
+
// fastest algorithm
|
|
152
|
+
putByte(4, out);
|
|
153
|
+
}
|
|
154
|
+
else if (level === 9) {
|
|
155
|
+
// maximum compression (fastest algorithm)
|
|
156
|
+
putByte(2, out);
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
putByte(0, out);
|
|
160
|
+
}
|
|
161
|
+
// OS identifier
|
|
162
|
+
putByte(osCode, out);
|
|
163
|
+
if (options.name) {
|
|
164
|
+
// ignore the directory part
|
|
165
|
+
putString(options.name.substring(options.name.lastIndexOf("/") + 1), out);
|
|
166
|
+
// terminating null
|
|
167
|
+
putByte(0, out);
|
|
168
|
+
}
|
|
169
|
+
deflateRaw(bytes).forEach(function (byte) {
|
|
170
|
+
putByte(byte, out);
|
|
171
|
+
});
|
|
172
|
+
// import { deflateRaw, inflateRaw } from "../deflate/mod.ts";
|
|
173
|
+
// deflateRaw(bytes, level).forEach(function (byte) {
|
|
174
|
+
// putByte(byte, out);
|
|
175
|
+
// });
|
|
176
|
+
putLong(parseInt(crc32(bytes), 16), out);
|
|
177
|
+
putLong(bytes.length, out);
|
|
178
|
+
return new Uint8Array(out);
|
|
179
|
+
}
|
|
180
|
+
export function gunzip(bytes) {
|
|
181
|
+
const arr = Array.from(bytes);
|
|
182
|
+
checkHeader(arr);
|
|
183
|
+
// give deflate everything but the last 8 bytes
|
|
184
|
+
// the last 8 bytes are for the CRC32 checksum and filesize
|
|
185
|
+
const res = inflateRaw(new Uint8Array(arr.splice(0, arr.length - 8)));
|
|
186
|
+
// if (flags & possibleFlags["FTEXT"]) {
|
|
187
|
+
// res = Array.prototype.map.call(res, function (byte) {
|
|
188
|
+
// return String.fromCharCode(byte);
|
|
189
|
+
// }).join("");
|
|
190
|
+
// }
|
|
191
|
+
const crc = readLong(arr) >>> 0;
|
|
192
|
+
if (crc !== parseInt(crc32(res), 16)) {
|
|
193
|
+
throw "Checksum does not match";
|
|
194
|
+
}
|
|
195
|
+
const size = readLong(arr);
|
|
196
|
+
if (size !== res.length) {
|
|
197
|
+
throw "Size of decompressed file not correct";
|
|
198
|
+
}
|
|
199
|
+
return res;
|
|
200
|
+
}
|
|
201
|
+
export function checkHeader(arr) {
|
|
202
|
+
// check the first two bytes for the magic numbers
|
|
203
|
+
if (readByte(arr) !== ID1 || readByte(arr) !== ID2) {
|
|
204
|
+
throw "Not a GZIP file";
|
|
205
|
+
}
|
|
206
|
+
if (readByte(arr) !== 8) {
|
|
207
|
+
throw "Unsupported compression method";
|
|
208
|
+
}
|
|
209
|
+
const flags = readByte(arr);
|
|
210
|
+
readLong(arr); // mtime
|
|
211
|
+
readByte(arr); // xFlags
|
|
212
|
+
readByte(arr); // os, throw away
|
|
213
|
+
// just throw away the bytes for now
|
|
214
|
+
if (flags & possibleFlags["FEXTRA"]) {
|
|
215
|
+
const t = readShort(arr);
|
|
216
|
+
readBytes(arr, t);
|
|
217
|
+
}
|
|
218
|
+
// just throw away for now
|
|
219
|
+
if (flags & possibleFlags["FNAME"]) {
|
|
220
|
+
readString(arr);
|
|
221
|
+
}
|
|
222
|
+
// just throw away for now
|
|
223
|
+
if (flags & possibleFlags["FCOMMENT"]) {
|
|
224
|
+
readString(arr);
|
|
225
|
+
}
|
|
226
|
+
// just throw away for now
|
|
227
|
+
if (flags & possibleFlags["FHCRC"]) {
|
|
228
|
+
readShort(arr);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
export function checkTail(arr) {
|
|
232
|
+
const tail = arr.splice(arr.length - 8);
|
|
233
|
+
const crc32 = readLong(tail) >>> 0;
|
|
234
|
+
const size = readLong(tail);
|
|
235
|
+
return {
|
|
236
|
+
crc32,
|
|
237
|
+
size,
|
|
238
|
+
};
|
|
239
|
+
}
|
|
@@ -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,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
|
+
}
|