MCPcopy Index your code
hub / github.com/nodejs/node / forgivingBase64

Function forgivingBase64

lib/internal/data_url.js:265–300  ·  view source on GitHub ↗

* @param {string} data * @returns {object|'failure'}

(data)

Source from the content-addressed store, hash-verified

263 * @returns {object|'failure'}
264 */
265function forgivingBase64(data) {
266 // 1. Remove all ASCII whitespace from data.
267 data = RegExpPrototypeSymbolReplace(ASCII_WHITESPACE_REPLACE_REGEX, data, '');
268
269 let dataLength = data.length;
270 // 2. If data's code point length divides by 4 leaving
271 // no remainder, then:
272 if (dataLength % 4 === 0) {
273 // 1. If data ends with one or two U+003D (=) code points,
274 // then remove them from data.
275 if (data[dataLength - 1] === '=') {
276 --dataLength;
277 if (data[dataLength - 1] === '=') {
278 --dataLength;
279 }
280 }
281 }
282
283 // 3. If data's code point length divides by 4 leaving
284 // a remainder of 1, then return failure.
285 if (dataLength % 4 === 1) {
286 return 'failure';
287 }
288
289 // 4. If data contains a code point that is not one of
290 // U+002B (+)
291 // U+002F (/)
292 // ASCII alphanumeric
293 // then return failure.
294 if (RegExpPrototypeExec(/[^+/0-9A-Za-z]/, data.length === dataLength ? data : StringPrototypeSlice(data, 0, dataLength)) !== null) {
295 return 'failure';
296 }
297
298 const buffer = Buffer.from(data, 'base64');
299 return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
300}
301
302/**
303 * @see https://infra.spec.whatwg.org/#ascii-whitespace

Callers 1

dataURLProcessorFunction · 0.70

Calls 1

fromMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…