UNPKG

postal-mime

Version:

Email parser for Node.js and browser environments

49 lines (35 loc) 1.33 kB
import { decodeBase64, blobToArrayBuffer } from './decode-strings.js'; export default class Base64Decoder { constructor(opts) { opts = opts || {}; this.decoder = opts.decoder || new TextDecoder(); this.maxChunkSize = 100 * 1024; this.chunks = []; this.remainder = ''; } update(buffer) { let str = this.decoder.decode(buffer); str = str.replace(/[^a-zA-Z0-9+\/]+/g, ''); this.remainder += str; if (this.remainder.length >= this.maxChunkSize) { let allowedBytes = Math.floor(this.remainder.length / 4) * 4; let base64Str; if (allowedBytes === this.remainder.length) { base64Str = this.remainder; this.remainder = ''; } else { base64Str = this.remainder.substr(0, allowedBytes); this.remainder = this.remainder.substr(allowedBytes); } if (base64Str.length) { this.chunks.push(decodeBase64(base64Str)); } } } finalize() { if (this.remainder && !/^=+$/.test(this.remainder)) { this.chunks.push(decodeBase64(this.remainder)); } return blobToArrayBuffer(new Blob(this.chunks, { type: 'application/octet-stream' })); } }