@frsource/base64 1.0.254 → 1.1.1

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/README.md CHANGED
@@ -38,7 +38,21 @@ const { encode, decode } = require('@frsource/base64');
38
38
  const encoded = encode('我喜欢解码lubię dekodować');
39
39
  console.log(encoded); // output: 5oiR5Zac5qyi6Kej56CBbHViacSZIGRla29kb3dhxIc=
40
40
  const decoded = decode(encoded);
41
- console.log(encoded); // output: 我喜欢解码lubię dekodować
41
+ console.log(decoded); // output: 我喜欢解码lubię dekodować
42
+ ```
43
+
44
+ ### URL safe variant (base64url)
45
+
46
+ `encodeUrl` produces the URL and filename safe alphabet (`-` and `_` instead of `+` and `/`) without `=` padding. `decode` accepts both alphabets, with or without padding:
47
+
48
+ ```js
49
+ import { encodeUrl, decode } from '@frsource/base64';
50
+
51
+ encodeUrl('???>>>'); // output: Pz8_Pj4- (encode gives: Pz8/Pj4+)
52
+ encodeUrl('a'); // output: YQ (encode gives: YQ==)
53
+
54
+ decode('Pz8_Pj4-'); // output: ???>>>
55
+ decode('YQ'); // output: a
42
56
  ```
43
57
 
44
58
  ## References
package/dist/index.d.mts CHANGED
@@ -4,11 +4,20 @@
4
4
  * @param {string} input - Text to encode
5
5
  */
6
6
  declare const encode: (input: string) => string;
7
+ /**
8
+ * Encodes given string using the URL and filename safe alphabet
9
+ * (base64url, RFC 4648 section 5), without padding.
10
+ *
11
+ * @param {string} input - Text to encode
12
+ */
13
+ declare const encodeUrl: (input: string) => string;
7
14
  /**
8
15
  * Decodes given string.
16
+ * Accepts both the standard and the URL safe (base64url) alphabet,
17
+ * with or without padding.
9
18
  *
10
19
  * @param {string} input - Text to decode
11
20
  */
12
21
  declare const decode: (input: string) => string;
13
22
 
14
- export { decode, encode };
23
+ export { decode, encode, encodeUrl };
package/dist/index.d.ts CHANGED
@@ -4,11 +4,20 @@
4
4
  * @param {string} input - Text to encode
5
5
  */
6
6
  declare const encode: (input: string) => string;
7
+ /**
8
+ * Encodes given string using the URL and filename safe alphabet
9
+ * (base64url, RFC 4648 section 5), without padding.
10
+ *
11
+ * @param {string} input - Text to encode
12
+ */
13
+ declare const encodeUrl: (input: string) => string;
7
14
  /**
8
15
  * Decodes given string.
16
+ * Accepts both the standard and the URL safe (base64url) alphabet,
17
+ * with or without padding.
9
18
  *
10
19
  * @param {string} input - Text to decode
11
20
  */
12
21
  declare const decode: (input: string) => string;
13
22
 
14
- export { decode, encode };
23
+ export { decode, encode, encodeUrl };
package/dist/index.js CHANGED
@@ -21,7 +21,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
23
  decode: () => decode,
24
- encode: () => encode
24
+ encode: () => encode,
25
+ encodeUrl: () => encodeUrl
25
26
  });
26
27
  module.exports = __toCommonJS(index_exports);
27
28
  var _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
@@ -46,10 +47,14 @@ var encode = (input) => {
46
47
  }
47
48
  return output;
48
49
  };
50
+ var encodeUrl = (input) => encode(input).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
49
51
  var decode = (input) => {
50
52
  let output = "";
51
53
  let i = 0;
52
- input = input.replace(/[^A-Za-z0-9+/=]/g, "");
54
+ input = input.replace(/-/g, "+").replace(/_/g, "/").replace(/[^A-Za-z0-9+/=]/g, "");
55
+ while (input.length % 4) {
56
+ input += "=";
57
+ }
53
58
  while (i < input.length) {
54
59
  const enc1 = _keyStr.indexOf(input.charAt(i++));
55
60
  const enc2 = _keyStr.indexOf(input.charAt(i++));
@@ -58,6 +63,9 @@ var decode = (input) => {
58
63
  const chr1 = enc1 << 2 | enc2 >> 4;
59
64
  const chr2 = (enc2 & 15) << 4 | enc3 >> 2;
60
65
  const chr3 = (enc3 & 3) << 6 | enc4;
66
+ if (enc2 === 64) {
67
+ continue;
68
+ }
61
69
  output += String.fromCharCode(chr1);
62
70
  if (enc3 !== 64) {
63
71
  output += String.fromCharCode(chr2);
@@ -69,11 +77,17 @@ var decode = (input) => {
69
77
  return _utf8_decode(output);
70
78
  };
71
79
  function _utf8_encode(str) {
72
- return encodeURIComponent(str).replace(
80
+ return encodeURIComponent(_to_well_formed(str)).replace(
73
81
  /%([0-9A-F]{2})/g,
74
82
  (_, p1) => String.fromCharCode(parseInt(p1, 16))
75
83
  );
76
84
  }
85
+ function _to_well_formed(str) {
86
+ return str.replace(
87
+ /[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDFFF]/g,
88
+ (match) => match.length === 2 ? match : "\uFFFD"
89
+ );
90
+ }
77
91
  function _utf8_decode(str) {
78
92
  return decodeURIComponent(
79
93
  str.split("").map((c) => "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2)).join("")
@@ -82,5 +96,6 @@ function _utf8_decode(str) {
82
96
  // Annotate the CommonJS export names for ESM import in node:
83
97
  0 && (module.exports = {
84
98
  decode,
85
- encode
99
+ encode,
100
+ encodeUrl
86
101
  });
package/dist/index.mjs CHANGED
@@ -21,10 +21,14 @@ var encode = (input) => {
21
21
  }
22
22
  return output;
23
23
  };
24
+ var encodeUrl = (input) => encode(input).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
24
25
  var decode = (input) => {
25
26
  let output = "";
26
27
  let i = 0;
27
- input = input.replace(/[^A-Za-z0-9+/=]/g, "");
28
+ input = input.replace(/-/g, "+").replace(/_/g, "/").replace(/[^A-Za-z0-9+/=]/g, "");
29
+ while (input.length % 4) {
30
+ input += "=";
31
+ }
28
32
  while (i < input.length) {
29
33
  const enc1 = _keyStr.indexOf(input.charAt(i++));
30
34
  const enc2 = _keyStr.indexOf(input.charAt(i++));
@@ -33,6 +37,9 @@ var decode = (input) => {
33
37
  const chr1 = enc1 << 2 | enc2 >> 4;
34
38
  const chr2 = (enc2 & 15) << 4 | enc3 >> 2;
35
39
  const chr3 = (enc3 & 3) << 6 | enc4;
40
+ if (enc2 === 64) {
41
+ continue;
42
+ }
36
43
  output += String.fromCharCode(chr1);
37
44
  if (enc3 !== 64) {
38
45
  output += String.fromCharCode(chr2);
@@ -44,11 +51,17 @@ var decode = (input) => {
44
51
  return _utf8_decode(output);
45
52
  };
46
53
  function _utf8_encode(str) {
47
- return encodeURIComponent(str).replace(
54
+ return encodeURIComponent(_to_well_formed(str)).replace(
48
55
  /%([0-9A-F]{2})/g,
49
56
  (_, p1) => String.fromCharCode(parseInt(p1, 16))
50
57
  );
51
58
  }
59
+ function _to_well_formed(str) {
60
+ return str.replace(
61
+ /[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDFFF]/g,
62
+ (match) => match.length === 2 ? match : "\uFFFD"
63
+ );
64
+ }
52
65
  function _utf8_decode(str) {
53
66
  return decodeURIComponent(
54
67
  str.split("").map((c) => "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2)).join("")
@@ -56,5 +69,6 @@ function _utf8_decode(str) {
56
69
  }
57
70
  export {
58
71
  decode,
59
- encode
72
+ encode,
73
+ encodeUrl
60
74
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@frsource/base64",
3
- "version": "1.0.254",
3
+ "version": "1.1.1",
4
4
  "description": "base64 encoder/decoder with UTF-8 support 📔",
5
5
  "repository": "https://github.com/FRSOURCE/base64.git",
6
6
  "homepage": "http://www.webtoolkit.info/javascript_base64.html",
@@ -37,7 +37,7 @@
37
37
  "license": "MIT",
38
38
  "packageManager": "pnpm@11.28.11",
39
39
  "devDependencies": {
40
- "@frsource/eslint-config": "1.86.5",
40
+ "@frsource/eslint-config": "1.86.6",
41
41
  "@frsource/prettier-config": "1.48.3",
42
42
  "@frsource/semantic-release-config": "1.117.9",
43
43
  "@vitest/coverage-v8": "^4.0.0",