@alterior/common 3.0.1 → 3.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/dist/base64.d.ts CHANGED
@@ -1,18 +1,7 @@
1
- /**
2
- * Lightweight base64 encode / decode
3
- */
4
1
  export declare class Base64 {
5
- /**
6
- * Encode a string using base64.
7
- * @param input The string to encode
8
- */
9
- static encode(input: string): string;
10
- /**
11
- * Decode a base64 encoded string to raw format.
12
- * @param input The base64 encoded string
13
- */
14
- static decode(input: string): string;
15
- private static _utf8_encode;
16
- private static _utf8_decode;
2
+ static encode(str: string): string;
3
+ static encodeBytes(bytes: Uint8Array): string;
4
+ static decode(str: string): string;
5
+ static decodeBytes(str: string): Uint8Array;
17
6
  }
18
7
  //# sourceMappingURL=base64.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"base64.d.ts","sourceRoot":"","sources":["../src/base64.ts"],"names":[],"mappings":"AAMA;;GAEG;AACH,qBAAa,MAAM;IAClB;;;OAGG;WACW,MAAM,CAAC,KAAK,EAAG,MAAM,GAAG,MAAM;IAoC5C;;;OAGG;WACW,MAAM,CAAC,KAAK,EAAG,MAAM,GAAG,MAAM;IAqC5C,OAAO,CAAC,MAAM,CAAC,YAAY;IA2B3B,OAAO,CAAC,MAAM,CAAC,YAAY;CAgC3B"}
1
+ {"version":3,"file":"base64.d.ts","sourceRoot":"","sources":["../src/base64.ts"],"names":[],"mappings":"AAEA,qBAAa,MAAM;IACf,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM;IAIlC,MAAM,CAAC,WAAW,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM;IA4B7C,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM;IAIlC,MAAM,CAAC,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU;CAqB9C"}
package/dist/base64.js CHANGED
@@ -1,121 +1,51 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Base64 = void 0;
4
- /**
5
- * @hidden
6
- */
7
4
  const BASE64_KEY = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
8
- /**
9
- * Lightweight base64 encode / decode
10
- */
11
5
  class Base64 {
12
- /**
13
- * Encode a string using base64.
14
- * @param input The string to encode
15
- */
16
- static encode(input) {
17
- input = input.replace(/ /g, ' ');
18
- var output = "";
19
- var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
20
- var i = 0;
21
- input = Base64._utf8_encode(input);
22
- while (i < input.length) {
23
- chr1 = input.charCodeAt(i++);
24
- chr2 = input.charCodeAt(i++);
25
- chr3 = input.charCodeAt(i++);
26
- enc1 = chr1 >> 2;
27
- enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
28
- enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
29
- enc4 = chr3 & 63;
30
- if (isNaN(chr2)) {
31
- enc3 = enc4 = 64;
32
- }
33
- else if (isNaN(chr3)) {
34
- enc4 = 64;
35
- }
36
- output = output +
37
- BASE64_KEY.charAt(enc1) + BASE64_KEY.charAt(enc2) +
38
- BASE64_KEY.charAt(enc3) + BASE64_KEY.charAt(enc4);
39
- }
40
- return output;
6
+ static encode(str) {
7
+ return this.encodeBytes(new TextEncoder().encode(str));
41
8
  }
42
- /**
43
- * Decode a base64 encoded string to raw format.
44
- * @param input The base64 encoded string
45
- */
46
- static decode(input) {
47
- var output = "";
48
- var chr1, chr2, chr3;
49
- var enc1, enc2, enc3, enc4;
50
- var i = 0;
51
- input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
52
- while (i < input.length) {
53
- enc1 = BASE64_KEY.indexOf(input.charAt(i++));
54
- enc2 = BASE64_KEY.indexOf(input.charAt(i++));
55
- enc3 = BASE64_KEY.indexOf(input.charAt(i++));
56
- enc4 = BASE64_KEY.indexOf(input.charAt(i++));
57
- chr1 = (enc1 << 2) | (enc2 >> 4);
58
- chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
59
- chr3 = ((enc3 & 3) << 6) | enc4;
60
- output = output + String.fromCharCode(chr1);
61
- if (enc3 != 64) {
62
- output = output + String.fromCharCode(chr2);
63
- }
64
- if (enc4 != 64) {
65
- output = output + String.fromCharCode(chr3);
66
- }
9
+ static encodeBytes(bytes) {
10
+ let output = '';
11
+ let inputPad = 3 - bytes.length % 3;
12
+ let buffer = new Uint8Array(bytes.length + inputPad);
13
+ buffer.set(bytes, 0);
14
+ for (let i = 0, max = buffer.length; i < max; i += 3) {
15
+ let value = buffer[i + 0] << 16
16
+ | buffer[i + 1] << 8
17
+ | buffer[i + 2];
18
+ let char1 = (value >> 18) & 0x3f;
19
+ let char2 = (value >> 12) & 0x3f;
20
+ let char3 = (value >> 6) & 0x3f;
21
+ let char4 = (value >> 0) & 0x3f;
22
+ output += BASE64_KEY[char1];
23
+ output += BASE64_KEY[char2];
24
+ output += BASE64_KEY[char3];
25
+ output += BASE64_KEY[char4];
67
26
  }
68
- output = Base64._utf8_decode(output);
27
+ let outputPad = Math.floor(inputPad * 8 / 6);
28
+ output = output.slice(0, -outputPad) + Array(outputPad + 1).join('=');
69
29
  return output;
70
30
  }
71
- // private method for UTF-8 encoding
72
- static _utf8_encode(string) {
73
- string = string.replace(/\r\n/g, "\n");
74
- var utftext = "";
75
- for (var n = 0; n < string.length; n++) {
76
- var c = string.charCodeAt(n);
77
- if (c < 128) {
78
- utftext += String.fromCharCode(c);
79
- }
80
- else if ((c > 127) && (c < 2048)) {
81
- utftext += String.fromCharCode((c >> 6) | 192);
82
- utftext += String.fromCharCode((c & 63) | 128);
83
- }
84
- else {
85
- utftext += String.fromCharCode((c >> 12) | 224);
86
- utftext += String.fromCharCode(((c >> 6) & 63) | 128);
87
- utftext += String.fromCharCode((c & 63) | 128);
88
- }
89
- }
90
- return utftext;
31
+ static decode(str) {
32
+ return new TextDecoder().decode(this.decodeBytes(str));
91
33
  }
92
- // private method for UTF-8 decoding
93
- static _utf8_decode(utftext) {
94
- var string = "";
95
- var i = 0;
96
- var c = 0;
97
- var c1 = 0;
98
- var c2 = 0;
99
- var c3 = 0;
100
- while (i < utftext.length) {
101
- c = utftext.charCodeAt(i);
102
- if (c < 128) {
103
- string += String.fromCharCode(c);
104
- i++;
105
- }
106
- else if ((c > 191) && (c < 224)) {
107
- c2 = utftext.charCodeAt(i + 1);
108
- string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
109
- i += 2;
110
- }
111
- else {
112
- c2 = utftext.charCodeAt(i + 1);
113
- c3 = utftext.charCodeAt(i + 2);
114
- string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
115
- i += 3;
116
- }
34
+ static decodeBytes(str) {
35
+ str = str.replace(/=+$/g, '');
36
+ let paddingRequired = 4 - str.length % 4;
37
+ str += Array(paddingRequired + 1).join('=');
38
+ let decoded = new Uint8Array(str.length * 6 / 8);
39
+ for (let i = 0, max = str.length; i < max; i += 4) {
40
+ let value = BASE64_KEY.indexOf(str[i + 0]) << 18
41
+ | BASE64_KEY.indexOf(str[i + 1]) << 12
42
+ | BASE64_KEY.indexOf(str[i + 2]) << 6
43
+ | BASE64_KEY.indexOf(str[i + 3]) << 0;
44
+ decoded[i / 4 * 3 + 0] = value >> 16 & 0xFF;
45
+ decoded[i / 4 * 3 + 1] = value >> 8 & 0xFF;
46
+ decoded[i / 4 * 3 + 2] = value >> 0 & 0xFF;
117
47
  }
118
- return string;
48
+ return decoded.subarray(0, decoded.length - paddingRequired);
119
49
  }
120
50
  }
121
51
  exports.Base64 = Base64;
@@ -1 +1 @@
1
- {"version":3,"file":"base64.js","sourceRoot":"","sources":["../src/base64.ts"],"names":[],"mappings":";;;AACA;;GAEG;AACH,MAAM,UAAU,GAAG,mEAAmE,CAAC;AAEvF;;GAEG;AACH,MAAa,MAAM;IAClB;;;OAGG;IACI,MAAM,CAAC,MAAM,CAAC,KAAc;QAE5B,KAAK,GAAG,KAA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CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE;kBAClC,UAAU,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE;kBACpC,UAAU,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC;kBACnC,UAAU,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,CACpC;YAEL,OAAO,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,KAAK,IAAI,EAAE,GAAG,IAAI,CAAC;YAC5C,OAAO,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,KAAK,IAAI,CAAC,GAAG,IAAI,CAAC;YAC3C,OAAO,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,KAAK,IAAI,CAAC,GAAG,IAAI,CAAC;SAC9C;QAED,OAAO,OAAO,CAAC,QAAQ,CAAC,CAAC,EAAE,OAAO,CAAC,MAAM,GAAG,eAAe,CAAC,CAAC;IACjE,CAAC;CACJ;AA1DD,wBA0DC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=base64.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base64.test.d.ts","sourceRoot":"","sources":["../src/base64.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base64.test.js","sourceRoot":"","sources":["../src/base64.test.ts"],"names":[],"mappings":";;AAAA,qCAAkC;AAClC,+BAA8B;AAC9B,mCAAkC;AAElC,IAAA,iBAAQ,EAAC,QAAQ,EAAE,GAAG,EAAE;IACpB,IAAA,iBAAQ,EAAC,WAAW,EAAE,EAAE,CAAC,EAAE;QACvB,EAAE,CAAC,IAAI,CAAC,2BAA2B,EAAE,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AI,CAAC,2BAA2B,EAAE,GAAG,EAAE;YACtC,IAAA,aAAM,EAAC,eAAM,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;YACjD,IAAA,aAAM,EAAC,eAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QACnD,CAAC,CAAC,CAAA;IACN,CAAC,CAAC,CAAA;AACN,CAAC,CAAC,CAAA"}
@@ -1,18 +1,7 @@
1
- /**
2
- * Lightweight base64 encode / decode
3
- */
4
1
  export declare class Base64 {
5
- /**
6
- * Encode a string using base64.
7
- * @param input The string to encode
8
- */
9
- static encode(input: string): string;
10
- /**
11
- * Decode a base64 encoded string to raw format.
12
- * @param input The base64 encoded string
13
- */
14
- static decode(input: string): string;
15
- private static _utf8_encode;
16
- private static _utf8_decode;
2
+ static encode(str: string): string;
3
+ static encodeBytes(bytes: Uint8Array): string;
4
+ static decode(str: string): string;
5
+ static decodeBytes(str: string): Uint8Array;
17
6
  }
18
7
  //# sourceMappingURL=base64.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"base64.d.ts","sourceRoot":"","sources":["../src/base64.ts"],"names":[],"mappings":"AAMA;;GAEG;AACH,qBAAa,MAAM;IAClB;;;OAGG;WACW,MAAM,CAAC,KAAK,EAAG,MAAM,GAAG,MAAM;IAoC5C;;;OAGG;WACW,MAAM,CAAC,KAAK,EAAG,MAAM,GAAG,MAAM;IAqC5C,OAAO,CAAC,MAAM,CAAC,YAAY;IA2B3B,OAAO,CAAC,MAAM,CAAC,YAAY;CAgC3B"}
1
+ {"version":3,"file":"base64.d.ts","sourceRoot":"","sources":["../src/base64.ts"],"names":[],"mappings":"AAEA,qBAAa,MAAM;IACf,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM;IAIlC,MAAM,CAAC,WAAW,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM;IA4B7C,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM;IAIlC,MAAM,CAAC,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU;CAqB9C"}
@@ -1,118 +1,48 @@
1
- /**
2
- * @hidden
3
- */
4
1
  const BASE64_KEY = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
5
- /**
6
- * Lightweight base64 encode / decode
7
- */
8
2
  export class Base64 {
9
- /**
10
- * Encode a string using base64.
11
- * @param input The string to encode
12
- */
13
- static encode(input) {
14
- input = input.replace(/ /g, ' ');
15
- var output = "";
16
- var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
17
- var i = 0;
18
- input = Base64._utf8_encode(input);
19
- while (i < input.length) {
20
- chr1 = input.charCodeAt(i++);
21
- chr2 = input.charCodeAt(i++);
22
- chr3 = input.charCodeAt(i++);
23
- enc1 = chr1 >> 2;
24
- enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
25
- enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
26
- enc4 = chr3 & 63;
27
- if (isNaN(chr2)) {
28
- enc3 = enc4 = 64;
29
- }
30
- else if (isNaN(chr3)) {
31
- enc4 = 64;
32
- }
33
- output = output +
34
- BASE64_KEY.charAt(enc1) + BASE64_KEY.charAt(enc2) +
35
- BASE64_KEY.charAt(enc3) + BASE64_KEY.charAt(enc4);
36
- }
37
- return output;
3
+ static encode(str) {
4
+ return this.encodeBytes(new TextEncoder().encode(str));
38
5
  }
39
- /**
40
- * Decode a base64 encoded string to raw format.
41
- * @param input The base64 encoded string
42
- */
43
- static decode(input) {
44
- var output = "";
45
- var chr1, chr2, chr3;
46
- var enc1, enc2, enc3, enc4;
47
- var i = 0;
48
- input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
49
- while (i < input.length) {
50
- enc1 = BASE64_KEY.indexOf(input.charAt(i++));
51
- enc2 = BASE64_KEY.indexOf(input.charAt(i++));
52
- enc3 = BASE64_KEY.indexOf(input.charAt(i++));
53
- enc4 = BASE64_KEY.indexOf(input.charAt(i++));
54
- chr1 = (enc1 << 2) | (enc2 >> 4);
55
- chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
56
- chr3 = ((enc3 & 3) << 6) | enc4;
57
- output = output + String.fromCharCode(chr1);
58
- if (enc3 != 64) {
59
- output = output + String.fromCharCode(chr2);
60
- }
61
- if (enc4 != 64) {
62
- output = output + String.fromCharCode(chr3);
63
- }
6
+ static encodeBytes(bytes) {
7
+ let output = '';
8
+ let inputPad = 3 - bytes.length % 3;
9
+ let buffer = new Uint8Array(bytes.length + inputPad);
10
+ buffer.set(bytes, 0);
11
+ for (let i = 0, max = buffer.length; i < max; i += 3) {
12
+ let value = buffer[i + 0] << 16
13
+ | buffer[i + 1] << 8
14
+ | buffer[i + 2];
15
+ let char1 = (value >> 18) & 0x3f;
16
+ let char2 = (value >> 12) & 0x3f;
17
+ let char3 = (value >> 6) & 0x3f;
18
+ let char4 = (value >> 0) & 0x3f;
19
+ output += BASE64_KEY[char1];
20
+ output += BASE64_KEY[char2];
21
+ output += BASE64_KEY[char3];
22
+ output += BASE64_KEY[char4];
64
23
  }
65
- output = Base64._utf8_decode(output);
24
+ let outputPad = Math.floor(inputPad * 8 / 6);
25
+ output = output.slice(0, -outputPad) + Array(outputPad + 1).join('=');
66
26
  return output;
67
27
  }
68
- // private method for UTF-8 encoding
69
- static _utf8_encode(string) {
70
- string = string.replace(/\r\n/g, "\n");
71
- var utftext = "";
72
- for (var n = 0; n < string.length; n++) {
73
- var c = string.charCodeAt(n);
74
- if (c < 128) {
75
- utftext += String.fromCharCode(c);
76
- }
77
- else if ((c > 127) && (c < 2048)) {
78
- utftext += String.fromCharCode((c >> 6) | 192);
79
- utftext += String.fromCharCode((c & 63) | 128);
80
- }
81
- else {
82
- utftext += String.fromCharCode((c >> 12) | 224);
83
- utftext += String.fromCharCode(((c >> 6) & 63) | 128);
84
- utftext += String.fromCharCode((c & 63) | 128);
85
- }
86
- }
87
- return utftext;
28
+ static decode(str) {
29
+ return new TextDecoder().decode(this.decodeBytes(str));
88
30
  }
89
- // private method for UTF-8 decoding
90
- static _utf8_decode(utftext) {
91
- var string = "";
92
- var i = 0;
93
- var c = 0;
94
- var c1 = 0;
95
- var c2 = 0;
96
- var c3 = 0;
97
- while (i < utftext.length) {
98
- c = utftext.charCodeAt(i);
99
- if (c < 128) {
100
- string += String.fromCharCode(c);
101
- i++;
102
- }
103
- else if ((c > 191) && (c < 224)) {
104
- c2 = utftext.charCodeAt(i + 1);
105
- string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
106
- i += 2;
107
- }
108
- else {
109
- c2 = utftext.charCodeAt(i + 1);
110
- c3 = utftext.charCodeAt(i + 2);
111
- string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
112
- i += 3;
113
- }
31
+ static decodeBytes(str) {
32
+ str = str.replace(/=+$/g, '');
33
+ let paddingRequired = 4 - str.length % 4;
34
+ str += Array(paddingRequired + 1).join('=');
35
+ let decoded = new Uint8Array(str.length * 6 / 8);
36
+ for (let i = 0, max = str.length; i < max; i += 4) {
37
+ let value = BASE64_KEY.indexOf(str[i + 0]) << 18
38
+ | BASE64_KEY.indexOf(str[i + 1]) << 12
39
+ | BASE64_KEY.indexOf(str[i + 2]) << 6
40
+ | BASE64_KEY.indexOf(str[i + 3]) << 0;
41
+ decoded[i / 4 * 3 + 0] = value >> 16 & 0xFF;
42
+ decoded[i / 4 * 3 + 1] = value >> 8 & 0xFF;
43
+ decoded[i / 4 * 3 + 2] = value >> 0 & 0xFF;
114
44
  }
115
- return string;
45
+ return decoded.subarray(0, decoded.length - paddingRequired);
116
46
  }
117
47
  }
118
48
  //# sourceMappingURL=base64.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"base64.js","sourceRoot":"","sources":["../src/base64.ts"],"names":[],"mappings":"AACA;;GAEG;AACH,MAAM,UAAU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AAE,GAAG,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC,GAAG,GAAG,EAAE,CAAC,IAAI,CAAC,EAAE;YAC/C,IAAI,KAAK,GACL,UAAU,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE;kBAClC,UAAU,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE;kBACpC,UAAU,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC;kBACnC,UAAU,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,CACpC;YAEL,OAAO,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,KAAK,IAAI,EAAE,GAAG,IAAI,CAAC;YAC5C,OAAO,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,KAAK,IAAI,CAAC,GAAG,IAAI,CAAC;YAC3C,OAAO,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,GAAG,KAAK,IAAI,CAAC,GAAG,IAAI,CAAC;SAC9C;QAED,OAAO,OAAO,CAAC,QAAQ,CAAC,CAAC,EAAE,OAAO,CAAC,MAAM,GAAG,eAAe,CAAC,CAAC;IACjE,CAAC;CACJ"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=base64.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base64.test.d.ts","sourceRoot":"","sources":["../src/base64.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base64.test.js","sourceRoot":"","sources":["../src/base64.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAClC,OAAO,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC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AM,CAAC,MAAM,CAAC,4BAA4B,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,qBAAqB,CAAC,CAAC;QACxF,CAAC,CAAC,CAAC;QACH,EAAE,CAAC,IAAI,CAAC,2BAA2B,EAAE,GAAG,EAAE;YACtC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;YACjD,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QACnD,CAAC,CAAC,CAAA;IACN,CAAC,CAAC,CAAA;AACN,CAAC,CAAC,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alterior/common",
3
- "version": "3.0.1",
3
+ "version": "3.0.6",
4
4
  "description": "Useful utilities for Typescript apps",
5
5
  "author": "The Alterior Project (https://github.com/alterior-mvc)",
6
6
  "license": "MIT",
@@ -52,5 +52,5 @@
52
52
  "typescript": "^4.1.5",
53
53
  "zone.js": "^0.11.4"
54
54
  },
55
- "gitHead": "9a3c8a30275fb4757dc7e2711673b3bfa855cf09"
55
+ "gitHead": "32ef41f96bfc52dfcc8bc56f29bf3a3ce842a4eb"
56
56
  }
@@ -0,0 +1,28 @@
1
+ import { Base64 } from "./base64";
2
+ import { expect } from "chai";
3
+ import { describe } from "razmin";
4
+
5
+ describe('Base64', () => {
6
+ describe('.encode()', it => {
7
+ it.only('encodes ASCII as expected', () => {
8
+ expect(Base64.encode('A')).to.equal('QQ==');
9
+ expect(Base64.encode('hello world')).to.equal('aGVsbG8gd29ybGQ=');
10
+ expect(Base64.encode('hello 123 $ _ / @ -')).to.equal('aGVsbG8gMTIzICQgXyAvIEAgLQ==');
11
+ });
12
+ it.only('encodes UTF-8 as expected', () => {
13
+ expect(Base64.encode('🚀')).to.equal('8J+agA==');
14
+ })
15
+ })
16
+ describe('.decode()', it => {
17
+ it.only('decodes ASCII as expected', () => {
18
+ expect(Base64.decode('aGVsbG8gd29ybGQ=')).to.equal('hello world');
19
+ expect(Base64.decode('aGVsbG8gd29ybGQ')).to.equal('hello world');
20
+ expect(Base64.decode('aGVsbG8gMTIzICQgXyAvIEAgLQ==')).to.equal('hello 123 $ _ / @ -');
21
+ expect(Base64.decode('aGVsbG8gMTIzICQgXyAvIEAgLQ')).to.equal('hello 123 $ _ / @ -');
22
+ });
23
+ it.only('decodes UTF-8 as expected', () => {
24
+ expect(Base64.decode('8J+agA==')).to.equal('🚀');
25
+ expect(Base64.decode('8J+agA')).to.equal('🚀');
26
+ })
27
+ })
28
+ })
package/src/base64.ts CHANGED
@@ -1,151 +1,61 @@
1
-
2
- /**
3
- * @hidden
4
- */
5
1
  const BASE64_KEY = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
6
2
 
7
- /**
8
- * Lightweight base64 encode / decode
9
- */
10
3
  export class Base64 {
11
- /**
12
- * Encode a string using base64.
13
- * @param input The string to encode
14
- */
15
- public static encode(input : string): string {
4
+ static encode(str: string): string {
5
+ return this.encodeBytes(new TextEncoder().encode(str));
6
+ }
7
+
8
+ static encodeBytes(bytes: Uint8Array): string {
9
+ let output = '';
10
+ let inputPad = 3 - bytes.length % 3;
11
+ let buffer = new Uint8Array(bytes.length + inputPad);
12
+ buffer.set(bytes, 0);
13
+
14
+ for (let i = 0, max = buffer.length; i < max; i += 3) {
15
+ let value =
16
+ buffer[i + 0] << 16
17
+ | buffer[i + 1] << 8
18
+ | buffer[i + 2]
19
+ ;
20
+ let char1 = (value >> 18) & 0x3f;
21
+ let char2 = (value >> 12) & 0x3f;
22
+ let char3 = (value >> 6) & 0x3f;
23
+ let char4 = (value >> 0) & 0x3f;
24
+
25
+ output += BASE64_KEY[char1];
26
+ output += BASE64_KEY[char2];
27
+ output += BASE64_KEY[char3];
28
+ output += BASE64_KEY[char4];
29
+ }
30
+
31
+ let outputPad = Math.floor(inputPad * 8 / 6);
32
+ output = output.slice(0, -outputPad) + Array(outputPad + 1).join('=');
33
+ return output;
34
+ }
35
+
36
+ static decode(str: string): string {
37
+ return new TextDecoder().decode(this.decodeBytes(str));
38
+ }
39
+
40
+ static decodeBytes(str: string): Uint8Array {
41
+ str = str.replace(/=+$/g, '');
42
+ let paddingRequired = 4 - str.length % 4;
43
+ str += Array(paddingRequired + 1).join('=');
16
44
 
17
- input = input.replace(/ /g, ' ');
45
+ let decoded = new Uint8Array(str.length * 6 / 8);
46
+ for (let i = 0, max = str.length; i < max; i += 4) {
47
+ let value =
48
+ BASE64_KEY.indexOf(str[i + 0]) << 18
49
+ | BASE64_KEY.indexOf(str[i + 1]) << 12
50
+ | BASE64_KEY.indexOf(str[i + 2]) << 6
51
+ | BASE64_KEY.indexOf(str[i + 3]) << 0
52
+ ;
18
53
 
19
- var output = "";
20
- var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
21
- var i = 0;
22
-
23
- input = Base64._utf8_encode(input);
24
-
25
- while (i < input.length) {
26
-
27
- chr1 = input.charCodeAt(i++);
28
- chr2 = input.charCodeAt(i++);
29
- chr3 = input.charCodeAt(i++);
30
-
31
- enc1 = chr1 >> 2;
32
- enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
33
- enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
34
- enc4 = chr3 & 63;
35
-
36
- if (isNaN(chr2)) {
37
- enc3 = enc4 = 64;
38
- } else if (isNaN(chr3)) {
39
- enc4 = 64;
40
- }
41
-
42
- output = output +
43
- BASE64_KEY.charAt(enc1) + BASE64_KEY.charAt(enc2) +
44
- BASE64_KEY.charAt(enc3) + BASE64_KEY.charAt(enc4);
45
-
46
- }
47
-
48
- return output;
49
- }
50
-
51
- /**
52
- * Decode a base64 encoded string to raw format.
53
- * @param input The base64 encoded string
54
- */
55
- public static decode(input : string): string {
56
- var output = "";
57
- var chr1, chr2, chr3;
58
- var enc1, enc2, enc3, enc4;
59
- var i = 0;
60
-
61
- input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
62
-
63
- while (i < input.length) {
64
-
65
- enc1 = BASE64_KEY.indexOf(input.charAt(i++));
66
- enc2 = BASE64_KEY.indexOf(input.charAt(i++));
67
- enc3 = BASE64_KEY.indexOf(input.charAt(i++));
68
- enc4 = BASE64_KEY.indexOf(input.charAt(i++));
69
-
70
- chr1 = (enc1 << 2) | (enc2 >> 4);
71
- chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
72
- chr3 = ((enc3 & 3) << 6) | enc4;
73
-
74
- output = output + String.fromCharCode(chr1);
75
-
76
- if (enc3 != 64) {
77
- output = output + String.fromCharCode(chr2);
78
- }
79
- if (enc4 != 64) {
80
- output = output + String.fromCharCode(chr3);
81
- }
82
-
83
- }
84
-
85
- output = Base64._utf8_decode(output);
86
-
87
- return output;
88
-
89
- }
90
-
91
- // private method for UTF-8 encoding
92
- private static _utf8_encode(string) {
93
- string = string.replace(/\r\n/g,"\n");
94
- var utftext = "";
95
-
96
- for (var n = 0; n < string.length; n++) {
97
-
98
- var c = string.charCodeAt(n);
99
-
100
- if (c < 128) {
101
- utftext += String.fromCharCode(c);
102
- }
103
- else if((c > 127) && (c < 2048)) {
104
- utftext += String.fromCharCode((c >> 6) | 192);
105
- utftext += String.fromCharCode((c & 63) | 128);
106
- }
107
- else {
108
- utftext += String.fromCharCode((c >> 12) | 224);
109
- utftext += String.fromCharCode(((c >> 6) & 63) | 128);
110
- utftext += String.fromCharCode((c & 63) | 128);
111
- }
112
-
113
- }
114
-
115
- return utftext;
116
- }
117
-
118
- // private method for UTF-8 decoding
119
- private static _utf8_decode(utftext) {
120
- var string = "";
121
- var i = 0;
122
- var c = 0;
123
- var c1 = 0;
124
- var c2 = 0;
125
- var c3 = 0;
54
+ decoded[i / 4 * 3 + 0] = value >> 16 & 0xFF;
55
+ decoded[i / 4 * 3 + 1] = value >> 8 & 0xFF;
56
+ decoded[i / 4 * 3 + 2] = value >> 0 & 0xFF;
57
+ }
126
58
 
127
- while ( i < utftext.length ) {
128
-
129
- c = utftext.charCodeAt(i);
130
-
131
- if (c < 128) {
132
- string += String.fromCharCode(c);
133
- i++;
134
- }
135
- else if((c > 191) && (c < 224)) {
136
- c2 = utftext.charCodeAt(i+1);
137
- string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
138
- i += 2;
139
- }
140
- else {
141
- c2 = utftext.charCodeAt(i+1);
142
- c3 = utftext.charCodeAt(i+2);
143
- string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
144
- i += 3;
145
- }
146
-
147
- }
148
-
149
- return string;
150
- }
151
- }
59
+ return decoded.subarray(0, decoded.length - paddingRequired);
60
+ }
61
+ }