拓恒河湖长制全民护河平台WEB端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 5.3KB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. [![build status](https://app.travis-ci.com/dankogai/js-base64.svg)](https://app.travis-ci.com/github/dankogai/js-base64)
  2. # base64.js
  3. Yet another [Base64] transcoder.
  4. [Base64]: http://en.wikipedia.org/wiki/Base64
  5. ## Install
  6. ```shell
  7. $ npm install --save js-base64
  8. ```
  9. ## Usage
  10. ### In Browser
  11. Locally…
  12. ```html
  13. <script src="base64.js"></script>
  14. ```
  15. … or Directly from CDN. In which case you don't even need to install.
  16. ```html
  17. <script src="https://cdn.jsdelivr.net/npm/js-base64@3.7.2/base64.min.js"></script>
  18. ```
  19. This good old way loads `Base64` in the global context (`window`). Though `Base64.noConflict()` is made available, you should consider using ES6 Module to avoid tainting `window`.
  20. ### As an ES6 Module
  21. locally…
  22. ```javascript
  23. import { Base64 } from 'js-base64';
  24. ```
  25. ```javascript
  26. // or if you prefer no Base64 namespace
  27. import { encode, decode } from 'js-base64';
  28. ```
  29. or even remotely.
  30. ```html
  31. <script type="module">
  32. // note jsdelivr.net does not automatically minify .mjs
  33. import { Base64 } from 'https://cdn.jsdelivr.net/npm/js-base64@3.7.2/base64.mjs';
  34. </script>
  35. ```
  36. ```html
  37. <script type="module">
  38. // or if you prefer no Base64 namespace
  39. import { encode, decode } from 'https://cdn.jsdelivr.net/npm/js-base64@3.7.2/base64.mjs';
  40. </script>
  41. ```
  42. ### node.js (commonjs)
  43. ```javascript
  44. const {Base64} = require('js-base64');
  45. ```
  46. Unlike the case above, the global context is no longer modified.
  47. You can also use [esm] to `import` instead of `require`.
  48. [esm]: https://github.com/standard-things/esm
  49. ```javascript
  50. require=require('esm')(module);
  51. import {Base64} from 'js-base64';
  52. ```
  53. ## SYNOPSIS
  54. ```javascript
  55. let latin = 'dankogai';
  56. let utf8 = '小飼弾'
  57. let u8s = new Uint8Array([100,97,110,107,111,103,97,105]);
  58. Base64.encode(latin); // ZGFua29nYWk=
  59. Base64.encode(latin, true)); // ZGFua29nYWk skips padding
  60. Base64.encodeURI(latin)); // ZGFua29nYWk
  61. Base64.btoa(latin); // ZGFua29nYWk=
  62. Base64.btoa(utf8); // raises exception
  63. Base64.fromUint8Array(u8s); // ZGFua29nYWk=
  64. Base64.fromUint8Array(u8s, true); // ZGFua29nYW which is URI safe
  65. Base64.encode(utf8); // 5bCP6aO85by+
  66. Base64.encode(utf8, true) // 5bCP6aO85by-
  67. Base64.encodeURI(utf8); // 5bCP6aO85by-
  68. ```
  69. ```javascript
  70. Base64.decode( 'ZGFua29nYWk=');// dankogai
  71. Base64.decode( 'ZGFua29nYWk'); // dankogai
  72. Base64.atob( 'ZGFua29nYWk=');// dankogai
  73. Base64.atob( '5bCP6aO85by+');// '小飼弾' which is nonsense
  74. Base64.toUint8Array('ZGFua29nYWk=');// u8s above
  75. Base64.decode( '5bCP6aO85by+');// 小飼弾
  76. // note .decodeURI() is unnecessary since it accepts both flavors
  77. Base64.decode( '5bCP6aO85by-');// 小飼弾
  78. ```
  79. ```javascript
  80. Base64.isValid(0); // false: 0 is not string
  81. Base64.isValid(''); // true: a valid Base64-encoded empty byte
  82. Base64.isValid('ZA=='); // true: a valid Base64-encoded 'd'
  83. Base64.isValid('Z A='); // true: whitespaces are okay
  84. Base64.isValid('ZA'); // true: padding ='s can be omitted
  85. Base64.isValid('++'); // true: can be non URL-safe
  86. Base64.isValid('--'); // true: or URL-safe
  87. Base64.isValid('+-'); // false: can't mix both
  88. ```
  89. ### Built-in Extensions
  90. By default `Base64` leaves built-in prototypes untouched. But you can extend them as below.
  91. ```javascript
  92. // you have to explicitly extend String.prototype
  93. Base64.extendString();
  94. // once extended, you can do the following
  95. 'dankogai'.toBase64(); // ZGFua29nYWk=
  96. '小飼弾'.toBase64(); // 5bCP6aO85by+
  97. '小飼弾'.toBase64(true); // 5bCP6aO85by-
  98. '小飼弾'.toBase64URI(); // 5bCP6aO85by- ab alias of .toBase64(true)
  99. '小飼弾'.toBase64URL(); // 5bCP6aO85by- an alias of .toBase64URI()
  100. 'ZGFua29nYWk='.fromBase64(); // dankogai
  101. '5bCP6aO85by+'.fromBase64(); // 小飼弾
  102. '5bCP6aO85by-'.fromBase64(); // 小飼弾
  103. '5bCP6aO85by-'.toUint8Array();// u8s above
  104. ```
  105. ```javascript
  106. // you have to explicitly extend Uint8Array.prototype
  107. Base64.extendUint8Array();
  108. // once extended, you can do the following
  109. u8s.toBase64(); // 'ZGFua29nYWk='
  110. u8s.toBase64URI(); // 'ZGFua29nYWk'
  111. u8s.toBase64URL(); // 'ZGFua29nYWk' an alias of .toBase64URI()
  112. ```
  113. ```javascript
  114. // extend all at once
  115. Base64.extendBuiltins()
  116. ```
  117. ## `.decode()` vs `.atob` (and `.encode()` vs `btoa()`)
  118. Suppose you have:
  119. ```
  120. var pngBase64 =
  121. "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=";
  122. ```
  123. Which is a Base64-encoded 1x1 transparent PNG, **DO NOT USE** `Base64.decode(pngBase64)`.  Use `Base64.atob(pngBase64)` instead.  `Base64.decode()` decodes to UTF-8 string while `Base64.atob()` decodes to bytes, which is compatible to browser built-in `atob()` (Which is absent in node.js).  The same rule applies to the opposite direction.
  124. Or even better, `Base64.toUint8Array(pngBase64)`.
  125. ### If you really, really need an ES5 version
  126. You can transpiles to an ES5 that runs on IEs before 11. Do the following in your shell.
  127. ```shell
  128. $ make base64.es5.js
  129. ```
  130. ## Brief History
  131. * Since version 3.3 it is written in TypeScript. Now `base64.mjs` is compiled from `base64.ts` then `base64.js` is generated from `base64.mjs`.
  132. * Since version 3.7 `base64.js` is ES5-compatible again (hence IE11-compabile).
  133. * Since 3.0 `js-base64` switch to ES2015 module so it is no longer compatible with legacy browsers like IE (see above)