@tutao/tutanota-utils 3.93.5 → 3.94.0

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/dist/Encoding.js CHANGED
@@ -1,11 +1,10 @@
1
1
  // TODO rename methods according to their JAVA counterparts (e.g. Uint8Array == bytes, Utf8Uint8Array == bytes...)
2
2
  export function uint8ArrayToArrayBuffer(uint8Array) {
3
- if (uint8Array.byteLength === uint8Array.buffer.byteLength) {
4
- return uint8Array.buffer;
5
- }
6
- else {
7
- return new Uint8Array(uint8Array).buffer; // create a new instance with the correct length, if uint8Array is only a DataView on a longer Array.buffer
8
- }
3
+ if (uint8Array.byteLength === uint8Array.buffer.byteLength) {
4
+ return uint8Array.buffer
5
+ } else {
6
+ return new Uint8Array(uint8Array).buffer // create a new instance with the correct length, if uint8Array is only a DataView on a longer Array.buffer
7
+ }
9
8
  }
10
9
  /**
11
10
  * Converts a hex coded string into a base64 coded string.
@@ -14,7 +13,7 @@ export function uint8ArrayToArrayBuffer(uint8Array) {
14
13
  * @return A base64 encoded string.
15
14
  */
16
15
  export function hexToBase64(hex) {
17
- return uint8ArrayToBase64(hexToUint8Array(hex));
16
+ return uint8ArrayToBase64(hexToUint8Array(hex))
18
17
  }
19
18
  /**
20
19
  * Converts a base64 coded string into a hex coded string.
@@ -23,7 +22,7 @@ export function hexToBase64(hex) {
23
22
  * @return A hex encoded string.
24
23
  */
25
24
  export function base64ToHex(base64) {
26
- return uint8ArrayToHex(base64ToUint8Array(base64));
25
+ return uint8ArrayToHex(base64ToUint8Array(base64))
27
26
  }
28
27
  /**
29
28
  * Converts a base64 string to a url-conform base64 string. This is used for
@@ -33,22 +32,22 @@ export function base64ToHex(base64) {
33
32
  * @return The base64url string.
34
33
  */
35
34
  export function base64ToBase64Url(base64) {
36
- let base64url = base64.replace(/\+/g, "-");
37
- base64url = base64url.replace(/\//g, "_");
38
- base64url = base64url.replace(/=/g, "");
39
- return base64url;
35
+ let base64url = base64.replace(/\+/g, "-")
36
+ base64url = base64url.replace(/\//g, "_")
37
+ base64url = base64url.replace(/=/g, "")
38
+ return base64url
40
39
  }
41
40
  function makeLookup(str) {
42
- const lookup = {};
43
- for (let i = 0; i < str.length; i++) {
44
- lookup[str.charAt(i)] = i;
45
- }
46
- return lookup;
41
+ const lookup = {}
42
+ for (let i = 0; i < str.length; i++) {
43
+ lookup[str.charAt(i)] = i
44
+ }
45
+ return lookup
47
46
  }
48
- const base64Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
49
- const base64Lookup = makeLookup(base64Alphabet);
50
- const base64extAlphabet = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz";
51
- const base64ExtLookup = makeLookup(base64extAlphabet);
47
+ const base64Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
48
+ const base64Lookup = makeLookup(base64Alphabet)
49
+ const base64extAlphabet = "-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
50
+ const base64ExtLookup = makeLookup(base64extAlphabet)
52
51
  /**
53
52
  * Converts a base64 string to a base64ext string. Base64ext uses another character set than base64 in order to make it sortable.
54
53
  *
@@ -57,13 +56,13 @@ const base64ExtLookup = makeLookup(base64extAlphabet);
57
56
  * @return The base64Ext string.
58
57
  */
59
58
  export function base64ToBase64Ext(base64) {
60
- base64 = base64.replace(/=/g, "");
61
- let base64ext = "";
62
- for (let i = 0; i < base64.length; i++) {
63
- let index = base64Lookup[base64.charAt(i)];
64
- base64ext += base64extAlphabet[index];
65
- }
66
- return base64ext;
59
+ base64 = base64.replace(/=/g, "")
60
+ let base64ext = ""
61
+ for (let i = 0; i < base64.length; i++) {
62
+ let index = base64Lookup[base64.charAt(i)]
63
+ base64ext += base64extAlphabet[index]
64
+ }
65
+ return base64ext
67
66
  }
68
67
  /**
69
68
  * Converts a Base64Ext string to a Base64 string and appends the padding if needed.
@@ -71,22 +70,20 @@ export function base64ToBase64Ext(base64) {
71
70
  * @returns The base64 string
72
71
  */
73
72
  export function base64ExtToBase64(base64ext) {
74
- let base64 = "";
75
- for (let i = 0; i < base64ext.length; i++) {
76
- const index = base64ExtLookup[base64ext.charAt(i)];
77
- base64 += base64Alphabet[index];
78
- }
79
- let padding;
80
- if (base64.length % 4 === 2) {
81
- padding = "==";
82
- }
83
- else if (base64.length % 4 === 3) {
84
- padding = "=";
85
- }
86
- else {
87
- padding = "";
88
- }
89
- return base64 + padding;
73
+ let base64 = ""
74
+ for (let i = 0; i < base64ext.length; i++) {
75
+ const index = base64ExtLookup[base64ext.charAt(i)]
76
+ base64 += base64Alphabet[index]
77
+ }
78
+ let padding
79
+ if (base64.length % 4 === 2) {
80
+ padding = "=="
81
+ } else if (base64.length % 4 === 3) {
82
+ padding = "="
83
+ } else {
84
+ padding = ""
85
+ }
86
+ return base64 + padding
90
87
  }
91
88
  /**
92
89
  * Converts a base64 url string to a "normal" base64 string. This is used for
@@ -96,82 +93,77 @@ export function base64ExtToBase64(base64ext) {
96
93
  * @return The base64 string.
97
94
  */
98
95
  export function base64UrlToBase64(base64url) {
99
- let base64 = base64url.replace(/-/g, "+");
100
- base64 = base64.replace(/_/g, "/");
101
- let nbrOfRemainingChars = base64.length % 4;
102
- if (nbrOfRemainingChars === 0) {
103
- return base64;
104
- }
105
- else if (nbrOfRemainingChars === 2) {
106
- return base64 + "==";
107
- }
108
- else if (nbrOfRemainingChars === 3) {
109
- return base64 + "=";
110
- }
111
- throw new Error("Illegal base64 string.");
96
+ let base64 = base64url.replace(/-/g, "+")
97
+ base64 = base64.replace(/_/g, "/")
98
+ let nbrOfRemainingChars = base64.length % 4
99
+ if (nbrOfRemainingChars === 0) {
100
+ return base64
101
+ } else if (nbrOfRemainingChars === 2) {
102
+ return base64 + "=="
103
+ } else if (nbrOfRemainingChars === 3) {
104
+ return base64 + "="
105
+ }
106
+ throw new Error("Illegal base64 string.")
112
107
  }
113
108
  // just for edge, as it does not support TextEncoder yet
114
109
  export function _stringToUtf8Uint8ArrayLegacy(string) {
115
- let fixedString;
116
- try {
117
- fixedString = encodeURIComponent(string);
118
- }
119
- catch (e) {
120
- fixedString = encodeURIComponent(_replaceLoneSurrogates(string)); // we filter lone surrogates as trigger URIErrors, otherwise (see https://github.com/tutao/tutanota/issues/618)
121
- }
122
- let utf8 = unescape(fixedString);
123
- let uint8Array = new Uint8Array(utf8.length);
124
- for (let i = 0; i < utf8.length; i++) {
125
- uint8Array[i] = utf8.charCodeAt(i);
126
- }
127
- return uint8Array;
110
+ let fixedString
111
+ try {
112
+ fixedString = encodeURIComponent(string)
113
+ } catch (e) {
114
+ fixedString = encodeURIComponent(_replaceLoneSurrogates(string)) // we filter lone surrogates as trigger URIErrors, otherwise (see https://github.com/tutao/tutanota/issues/618)
115
+ }
116
+ let utf8 = unescape(fixedString)
117
+ let uint8Array = new Uint8Array(utf8.length)
118
+ for (let i = 0; i < utf8.length; i++) {
119
+ uint8Array[i] = utf8.charCodeAt(i)
120
+ }
121
+ return uint8Array
128
122
  }
129
- const REPLACEMENT_CHAR = "\uFFFD";
123
+ const REPLACEMENT_CHAR = "\uFFFD"
130
124
  export function _replaceLoneSurrogates(s) {
131
- if (s == null) {
132
- return "";
133
- }
134
- let result = [];
135
- for (let i = 0; i < s.length; i++) {
136
- let code = s.charCodeAt(i);
137
- let char = s.charAt(i);
138
- if (0xd800 <= code && code <= 0xdbff) {
139
- if (s.length === i) {
140
- // replace high surrogate without following low surrogate
141
- result.push(REPLACEMENT_CHAR);
142
- }
143
- else {
144
- let next = s.charCodeAt(i + 1);
145
- if (0xdc00 <= next && next <= 0xdfff) {
146
- result.push(char);
147
- result.push(s.charAt(i + 1));
148
- i++; // valid high and low surrogate, skip next low surrogate check
149
- }
150
- else {
151
- result.push(REPLACEMENT_CHAR);
152
- }
153
- }
154
- }
155
- else if (0xdc00 <= code && code <= 0xdfff) {
156
- // replace low surrogate without preceding high surrogate
157
- result.push(REPLACEMENT_CHAR);
158
- }
159
- else {
160
- result.push(char);
161
- }
162
- }
163
- return result.join("");
125
+ if (s == null) {
126
+ return ""
127
+ }
128
+ let result = []
129
+ for (let i = 0; i < s.length; i++) {
130
+ let code = s.charCodeAt(i)
131
+ let char = s.charAt(i)
132
+ if (0xd800 <= code && code <= 0xdbff) {
133
+ if (s.length === i) {
134
+ // replace high surrogate without following low surrogate
135
+ result.push(REPLACEMENT_CHAR)
136
+ } else {
137
+ let next = s.charCodeAt(i + 1)
138
+ if (0xdc00 <= next && next <= 0xdfff) {
139
+ result.push(char)
140
+ result.push(s.charAt(i + 1))
141
+ i++ // valid high and low surrogate, skip next low surrogate check
142
+ } else {
143
+ result.push(REPLACEMENT_CHAR)
144
+ }
145
+ }
146
+ } else if (0xdc00 <= code && code <= 0xdfff) {
147
+ // replace low surrogate without preceding high surrogate
148
+ result.push(REPLACEMENT_CHAR)
149
+ } else {
150
+ result.push(char)
151
+ }
152
+ }
153
+ return result.join("")
164
154
  }
165
- const encoder = typeof TextEncoder == "function"
166
- ? new TextEncoder()
167
- : {
168
- encode: _stringToUtf8Uint8ArrayLegacy,
169
- };
170
- const decoder = typeof TextDecoder == "function"
171
- ? new TextDecoder()
172
- : {
173
- decode: _utf8Uint8ArrayToStringLegacy,
174
- };
155
+ const encoder =
156
+ typeof TextEncoder == "function"
157
+ ? new TextEncoder()
158
+ : {
159
+ encode: _stringToUtf8Uint8ArrayLegacy,
160
+ }
161
+ const decoder =
162
+ typeof TextDecoder == "function"
163
+ ? new TextDecoder()
164
+ : {
165
+ decode: _utf8Uint8ArrayToStringLegacy,
166
+ }
175
167
  /**
176
168
  * Converts a string to a Uint8Array containing a UTF-8 string data.
177
169
  *
@@ -179,16 +171,16 @@ const decoder = typeof TextDecoder == "function"
179
171
  * @return The array.
180
172
  */
181
173
  export function stringToUtf8Uint8Array(string) {
182
- return encoder.encode(string);
174
+ return encoder.encode(string)
183
175
  }
184
176
  // just for edge, as it does not support TextDecoder yet
185
177
  export function _utf8Uint8ArrayToStringLegacy(uint8Array) {
186
- let stringArray = [];
187
- stringArray.length = uint8Array.length;
188
- for (let i = 0; i < uint8Array.length; i++) {
189
- stringArray[i] = String.fromCharCode(uint8Array[i]);
190
- }
191
- return decodeURIComponent(escape(stringArray.join("")));
178
+ let stringArray = []
179
+ stringArray.length = uint8Array.length
180
+ for (let i = 0; i < uint8Array.length; i++) {
181
+ stringArray[i] = String.fromCharCode(uint8Array[i])
182
+ }
183
+ return decodeURIComponent(escape(stringArray.join("")))
192
184
  }
193
185
  /**
194
186
  * Converts an Uint8Array containing UTF-8 string data into a string.
@@ -197,23 +189,23 @@ export function _utf8Uint8ArrayToStringLegacy(uint8Array) {
197
189
  * @return The string.
198
190
  */
199
191
  export function utf8Uint8ArrayToString(uint8Array) {
200
- return decoder.decode(uint8Array);
192
+ return decoder.decode(uint8Array)
201
193
  }
202
194
  export function hexToUint8Array(hex) {
203
- let bufView = new Uint8Array(hex.length / 2);
204
- for (let i = 0; i < bufView.byteLength; i++) {
205
- bufView[i] = parseInt(hex.substring(i * 2, i * 2 + 2), 16);
206
- }
207
- return bufView;
195
+ let bufView = new Uint8Array(hex.length / 2)
196
+ for (let i = 0; i < bufView.byteLength; i++) {
197
+ bufView[i] = parseInt(hex.substring(i * 2, i * 2 + 2), 16)
198
+ }
199
+ return bufView
208
200
  }
209
- const hexDigits = "0123456789abcdef";
201
+ const hexDigits = "0123456789abcdef"
210
202
  export function uint8ArrayToHex(uint8Array) {
211
- let hex = "";
212
- for (let i = 0; i < uint8Array.byteLength; i++) {
213
- let value = uint8Array[i];
214
- hex += hexDigits[value >> 4] + hexDigits[value & 15];
215
- }
216
- return hex;
203
+ let hex = ""
204
+ for (let i = 0; i < uint8Array.byteLength; i++) {
205
+ let value = uint8Array[i]
206
+ hex += hexDigits[value >> 4] + hexDigits[value & 15]
207
+ }
208
+ return hex
217
209
  }
218
210
  /**
219
211
  * Converts an Uint8Array to a Base64 encoded string.
@@ -222,23 +214,23 @@ export function uint8ArrayToHex(uint8Array) {
222
214
  * @return The Base64 encoded string.
223
215
  */
224
216
  export function uint8ArrayToBase64(bytes) {
225
- if (bytes.length < 512) {
226
- // Apply fails on big arrays fairly often. We tried it with 60000 but if you're already
227
- // deep in the stack than we cannot allocate such a big argument array.
228
- return btoa(String.fromCharCode(...bytes));
229
- }
230
- let binary = "";
231
- const len = bytes.byteLength;
232
- for (let i = 0; i < len; i++) {
233
- binary += String.fromCharCode(bytes[i]);
234
- }
235
- return btoa(binary);
217
+ if (bytes.length < 512) {
218
+ // Apply fails on big arrays fairly often. We tried it with 60000 but if you're already
219
+ // deep in the stack than we cannot allocate such a big argument array.
220
+ return btoa(String.fromCharCode(...bytes))
221
+ }
222
+ let binary = ""
223
+ const len = bytes.byteLength
224
+ for (let i = 0; i < len; i++) {
225
+ binary += String.fromCharCode(bytes[i])
226
+ }
227
+ return btoa(binary)
236
228
  }
237
229
  export function int8ArrayToBase64(bytes) {
238
- // Values 0 to 127 are the same for signed and unsigned bytes
239
- // and -128 to -1 are mapped to the same chars as 128 to 255.
240
- let converted = new Uint8Array(bytes);
241
- return uint8ArrayToBase64(converted);
230
+ // Values 0 to 127 are the same for signed and unsigned bytes
231
+ // and -128 to -1 are mapped to the same chars as 128 to 255.
232
+ let converted = new Uint8Array(bytes)
233
+ return uint8ArrayToBase64(converted)
242
234
  }
243
235
  /**
244
236
  * Converts a base64 encoded string to a Uint8Array.
@@ -247,15 +239,15 @@ export function int8ArrayToBase64(bytes) {
247
239
  * @return The bytes.
248
240
  */
249
241
  export function base64ToUint8Array(base64) {
250
- if (base64.length % 4 !== 0) {
251
- throw new Error(`invalid base64 length: ${base64} (${base64.length})`);
252
- }
253
- const binaryString = atob(base64);
254
- const result = new Uint8Array(binaryString.length);
255
- for (let i = 0; i < binaryString.length; i++) {
256
- result[i] = binaryString.charCodeAt(i);
257
- }
258
- return result;
242
+ if (base64.length % 4 !== 0) {
243
+ throw new Error(`invalid base64 length: ${base64} (${base64.length})`)
244
+ }
245
+ const binaryString = atob(base64)
246
+ const result = new Uint8Array(binaryString.length)
247
+ for (let i = 0; i < binaryString.length; i++) {
248
+ result[i] = binaryString.charCodeAt(i)
249
+ }
250
+ return result
259
251
  }
260
252
  /**
261
253
  * Converts a Uint8Array containing string data into a string, given the charset the data is in.
@@ -265,8 +257,8 @@ export function base64ToUint8Array(base64) {
265
257
  * @return The string
266
258
  */
267
259
  export function uint8ArrayToString(charset, bytes) {
268
- const decoder = new TextDecoder(charset);
269
- return decoder.decode(bytes);
260
+ const decoder = new TextDecoder(charset)
261
+ return decoder.decode(bytes)
270
262
  }
271
263
  /**
272
264
  * Decodes a quoted-printable piece of text in a given charset.
@@ -278,29 +270,31 @@ export function uint8ArrayToString(charset, bytes) {
278
270
  * @returns The text as a JavaScript string
279
271
  */
280
272
  export function decodeQuotedPrintable(charset, input) {
281
- return (input // https://tools.ietf.org/html/rfc2045#section-6.7, rule 3:
282
- // “Therefore, when decoding a `Quoted-Printable` body, any trailing white
283
- // space on a line must be deleted, as it will necessarily have been added
284
- // by intermediate transport agents.”
285
- .replace(/[\t\x20]$/gm, "") // Remove hard line breaks preceded by `=`. Proper `Quoted-Printable`-
286
- // encoded data only contains CRLF line endings, but for compatibility
287
- // reasons we support separate CR and LF too.
288
- .replace(/=(?:\r\n?|\n|$)/g, "") // Decode escape sequences of the form `=XX` where `XX` is any
289
- // combination of two hexidecimal digits. For optimal compatibility,
290
- // lowercase hexadecimal digits are supported as well. See
291
- // https://tools.ietf.org/html/rfc2045#section-6.7, note 1.
292
- .replace(/(=([a-fA-F0-9]{2}))+/g, match => {
293
- const hexValues = match.split(/=/);
294
- // splitting on '=' is convenient, but adds an empty string at the start due to the first byte
295
- hexValues.shift();
296
- const intArray = hexValues.map(char => parseInt(char, 16));
297
- const bytes = Uint8Array.from(intArray);
298
- return uint8ArrayToString(charset, bytes);
299
- }));
273
+ return (
274
+ input // https://tools.ietf.org/html/rfc2045#section-6.7, rule 3:
275
+ // “Therefore, when decoding a `Quoted-Printable` body, any trailing white
276
+ // space on a line must be deleted, as it will necessarily have been added
277
+ // by intermediate transport agents.”
278
+ .replace(/[\t\x20]$/gm, "") // Remove hard line breaks preceded by `=`. Proper `Quoted-Printable`-
279
+ // encoded data only contains CRLF line endings, but for compatibility
280
+ // reasons we support separate CR and LF too.
281
+ .replace(/=(?:\r\n?|\n|$)/g, "") // Decode escape sequences of the form `=XX` where `XX` is any
282
+ // combination of two hexidecimal digits. For optimal compatibility,
283
+ // lowercase hexadecimal digits are supported as well. See
284
+ // https://tools.ietf.org/html/rfc2045#section-6.7, note 1.
285
+ .replace(/(=([a-fA-F0-9]{2}))+/g, match => {
286
+ const hexValues = match.split(/=/)
287
+ // splitting on '=' is convenient, but adds an empty string at the start due to the first byte
288
+ hexValues.shift()
289
+ const intArray = hexValues.map(char => parseInt(char, 16))
290
+ const bytes = Uint8Array.from(intArray)
291
+ return uint8ArrayToString(charset, bytes)
292
+ })
293
+ )
300
294
  }
301
295
  export function decodeBase64(charset, input) {
302
- return uint8ArrayToString(charset, base64ToUint8Array(input));
296
+ return uint8ArrayToString(charset, base64ToUint8Array(input))
303
297
  }
304
298
  export function stringToBase64(str) {
305
- return uint8ArrayToBase64(stringToUtf8Uint8Array(str));
299
+ return uint8ArrayToBase64(stringToUtf8Uint8Array(str))
306
300
  }
@@ -1,38 +1,38 @@
1
- import type { lazyAsync } from "./Utils.js";
1
+ import type { lazyAsync } from "./Utils.js"
2
2
  /**
3
3
  * A wrapper for an object that shall be lazy loaded asynchronously. If loading the object is triggered in parallel (getAsync()) the object is actually only loaded once but returned to all calls of getAsync().
4
4
  * If the object was loaded once it is not loaded again.
5
5
  */
6
6
  export declare class LazyLoaded<T> {
7
- _isLoaded: boolean;
8
- _loadingPromise: Promise<T> | null;
9
- _loadedObject: T | null;
10
- _loadFunction: lazyAsync<T>;
11
- /**
12
- * @param loadFunction The function that actually loads the object as soon as getAsync() is called the first time.
13
- * @param defaultValue The value that shall be returned by getSync() or getLoaded() as long as the object is not loaded yet.
14
- */
15
- constructor(loadFunction: lazyAsync<T>, defaultValue?: T);
16
- load(): this;
17
- isLoaded(): boolean;
18
- /**
19
- * Loads the object if it is not loaded yet. May be called in parallel and takes care that the load function is only called once.
20
- */
21
- getAsync(): Promise<T>;
22
- /**
23
- * Returns null if the object is not loaded yet.
24
- */
25
- getSync(): T | null;
26
- /**
27
- * Only call this function if you know that the object is already loaded.
28
- */
29
- getLoaded(): T;
30
- /**
31
- * Removes the currently loaded object, so it will be loaded again with the next getAsync() call. Does not set any default value.
32
- */
33
- reset(): void;
34
- /**
35
- * Loads the object again and replaces the current one
36
- */
37
- reload(): Promise<T>;
7
+ _isLoaded: boolean
8
+ _loadingPromise: Promise<T> | null
9
+ _loadedObject: T | null
10
+ _loadFunction: lazyAsync<T>
11
+ /**
12
+ * @param loadFunction The function that actually loads the object as soon as getAsync() is called the first time.
13
+ * @param defaultValue The value that shall be returned by getSync() or getLoaded() as long as the object is not loaded yet.
14
+ */
15
+ constructor(loadFunction: lazyAsync<T>, defaultValue?: T)
16
+ load(): this
17
+ isLoaded(): boolean
18
+ /**
19
+ * Loads the object if it is not loaded yet. May be called in parallel and takes care that the load function is only called once.
20
+ */
21
+ getAsync(): Promise<T>
22
+ /**
23
+ * Returns null if the object is not loaded yet.
24
+ */
25
+ getSync(): T | null
26
+ /**
27
+ * Only call this function if you know that the object is already loaded.
28
+ */
29
+ getLoaded(): T
30
+ /**
31
+ * Removes the currently loaded object, so it will be loaded again with the next getAsync() call. Does not set any default value.
32
+ */
33
+ reset(): void
34
+ /**
35
+ * Loads the object again and replaces the current one
36
+ */
37
+ reload(): Promise<T>
38
38
  }