| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3112,6 +3112,44 @@ While, the `Buffer` object is available as a global, there are additional | |||
| 3112 | 3112 | `Buffer`-related APIs that are available only via the `buffer` module | |
| 3113 | 3113 | accessed using `require('buffer')`. | |
| 3114 | 3114 | ||
| 3115 | + ### `buffer.atob(data)` | ||
| 3116 | + <!-- YAML | ||
| 3117 | + added: REPLACEME | ||
| 3118 | + --> | ||
| 3119 | + | ||
| 3120 | + * `data` {any} The Base64-encoded input string. | ||
| 3121 | + | ||
| 3122 | + Decodes a string of Base64-encoded data into bytes, and encodes those bytes | ||
| 3123 | + into a string using Latin-1 (ISO-8859-1). | ||
| 3124 | + | ||
| 3125 | + The `data` may be any JavaScript-value that can be coerced into a string. | ||
| 3126 | + | ||
| 3127 | + **This function is only provided for compatibility with legacy web platform APIs | ||
| 3128 | + and should never be used in new code, because they use strings to represent | ||
| 3129 | + binary data and predate the introduction of typed arrays in JavaScript. | ||
| 3130 | + For code running using Node.js APIs, converting between base64-encoded strings | ||
| 3131 | + and binary data should be performed using `Buffer.from(str, 'base64')` and | ||
| 3132 | + `buf.toString('base64')`.** | ||
| 3133 | + | ||
| 3134 | + ### `buffer.btoa(data)` | ||
| 3135 | + <!-- YAML | ||
| 3136 | + added: REPLACEME | ||
| 3137 | + --> | ||
| 3138 | + | ||
| 3139 | + * `data` {any} An ASCII (Latin1) string. | ||
| 3140 | + | ||
| 3141 | + Decodes a string into bytes using Latin-1 (ISO-8859), and encodes those bytes | ||
| 3142 | + into a string using Base64. | ||
| 3143 | + | ||
| 3144 | + The `data` may be any JavaScript-value that can be coerced into a string. | ||
| 3145 | + | ||
| 3146 | + **This function is only provided for compatibility with legacy web platform APIs | ||
| 3147 | + and should never be used in new code, because they use strings to represent | ||
| 3148 | + binary data and predate the introduction of typed arrays in JavaScript. | ||
| 3149 | + For code running using Node.js APIs, converting between base64-encoded strings | ||
| 3150 | + and binary data should be performed using `Buffer.from(str, 'base64')` and | ||
| 3151 | + `buf.toString('base64')`.** | ||
| 3152 | + | ||
| 3115 | 3153 | ### `buffer.INSPECT_MAX_BYTES` | |
| 3116 | 3154 | <!-- YAML | |
| 3117 | 3155 | added: v0.5.4 | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1202,13 +1202,49 @@ if (internalBinding('config').hasIntl) { | |||
| 1202 | 1202 | }; | |
| 1203 | 1203 | } | |
| 1204 | 1204 | ||
| 1205 | + let DOMException; | ||
| 1206 | + | ||
| 1207 | + const lazyInvalidCharError = hideStackFrames((message, name) => { | ||
| 1208 | + if (DOMException === undefined) | ||
| 1209 | + DOMException = internalBinding('messaging').DOMException; | ||
| 1210 | + throw new DOMException('Invalid character', 'InvalidCharacterError'); | ||
| 1211 | + }); | ||
| 1212 | + | ||
| 1213 | + function btoa(input) { | ||
| 1214 | + // TODO(@jasnell): The implementation here has not been performance | ||
| 1215 | + // optimized in any way. | ||
| 1216 | + input = `${input}`; | ||
| 1217 | + for (let n = 0; n < input.length; n++) { | ||
| 1218 | + if (input[n].charCodeAt(0) > 0xff) | ||
| 1219 | + lazyInvalidCharError(); | ||
| 1220 | + } | ||
| 1221 | + const buf = Buffer.from(input, 'latin1'); | ||
| 1222 | + return buf.toString('base64'); | ||
| 1223 | + } | ||
| 1224 | + | ||
| 1225 | + const kBase64Digits = | ||
| 1226 | + 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; | ||
| 1227 | + | ||
| 1228 | + function atob(input) { | ||
| 1229 | + // TODO(@jasnell): The implementation here has not been performance | ||
| 1230 | + // optimized in any way. | ||
| 1231 | + input = `${input}`; | ||
| 1232 | + for (let n = 0; n < input.length; n++) { | ||
| 1233 | + if (!kBase64Digits.includes(input[n])) | ||
| 1234 | + lazyInvalidCharError(); | ||
| 1235 | + } | ||
| 1236 | + return Buffer.from(input, 'base64').toString('latin1'); | ||
| 1237 | + } | ||
| 1238 | + | ||
| 1205 | 1239 | module.exports = { | |
| 1206 | 1240 | Buffer, | |
| 1207 | 1241 | SlowBuffer, | |
| 1208 | 1242 | transcode, | |
| 1209 | 1243 | // Legacy | |
| 1210 | 1244 | kMaxLength, | |
| 1211 | - kStringMaxLength | ||
| 1245 | + kStringMaxLength, | ||
| 1246 | + btoa, | ||
| 1247 | + atob, | ||
| 1212 | 1248 | }; | |
| 1213 | 1249 | ||
| 1214 | 1250 | ObjectDefineProperties(module.exports, { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments