Files
send-mail/node_modules/nodemailer/dist/esm/base64/index.js
T

113 lines
3.6 KiB
JavaScript
Raw Normal View History

2026-09-15 13:00:32 +02:00
import { Transform } from 'node:stream';
2020-02-29 23:01:03 +01:00
/**
* Encodes a Buffer into a base64 encoded string
*
2026-09-15 13:00:32 +02:00
* @param buffer Buffer to convert
* @returns base64 encoded string
2020-02-29 23:01:03 +01:00
*/
2026-09-15 13:00:32 +02:00
export function encode(buffer) {
2020-02-29 23:01:03 +01:00
if (typeof buffer === 'string') {
buffer = Buffer.from(buffer, 'utf-8');
}
return buffer.toString('base64');
}
/**
* Adds soft line breaks to a base64 string
*
2026-09-15 13:00:32 +02:00
* @param str base64 encoded string that might need line wrapping
* @param [lineLength=76] Maximum allowed length for a line
* @returns Soft-wrapped base64 encoded string
2020-02-29 23:01:03 +01:00
*/
2026-09-15 13:00:32 +02:00
export function wrap(str, lineLength) {
2020-02-29 23:01:03 +01:00
str = (str || '').toString();
lineLength = lineLength || 76;
if (str.length <= lineLength) {
return str;
}
2026-04-28 12:50:45 +02:00
const result = [];
2020-02-29 23:01:03 +01:00
let pos = 0;
2026-04-28 12:50:45 +02:00
const chunkLength = lineLength * 1024;
const wrapRegex = new RegExp('.{' + lineLength + '}', 'g');
2020-02-29 23:01:03 +01:00
while (pos < str.length) {
2026-04-28 12:50:45 +02:00
const wrappedLines = str.substr(pos, chunkLength).replace(wrapRegex, '$&\r\n').trim();
2020-02-29 23:01:03 +01:00
result.push(wrappedLines);
pos += chunkLength;
}
2026-04-28 12:50:45 +02:00
return result.join('\r\n').trim();
2020-02-29 23:01:03 +01:00
}
/**
* Creates a transform stream for encoding data to base64 encoding
*
* @constructor
2026-09-15 13:00:32 +02:00
* @param options Stream options
* @param [options.lineLength=76] Maximum length for lines, set to false to disable wrapping
2020-02-29 23:01:03 +01:00
*/
2026-09-15 13:00:32 +02:00
export class Encoder extends Transform {
2020-02-29 23:01:03 +01:00
constructor(options) {
super();
this.options = options || {};
if (this.options.lineLength !== false) {
this.options.lineLength = this.options.lineLength || 76;
}
this._curLine = '';
this._remainingBytes = false;
this.inputBytes = 0;
this.outputBytes = 0;
}
2026-09-15 13:00:32 +02:00
/** @internal */
2020-02-29 23:01:03 +01:00
_transform(chunk, encoding, done) {
2026-09-15 13:00:32 +02:00
let buf = encoding !== 'buffer' ? Buffer.from(chunk, encoding) : chunk;
if (!buf || !buf.length) {
setImmediate(done);
return;
2020-02-29 23:01:03 +01:00
}
2026-09-15 13:00:32 +02:00
this.inputBytes += buf.length;
2020-02-29 23:01:03 +01:00
if (this._remainingBytes && this._remainingBytes.length) {
2026-09-15 13:00:32 +02:00
buf = Buffer.concat([this._remainingBytes, buf], this._remainingBytes.length + buf.length);
2020-02-29 23:01:03 +01:00
this._remainingBytes = false;
}
2026-09-15 13:00:32 +02:00
if (buf.length % 3) {
this._remainingBytes = buf.slice(buf.length - (buf.length % 3));
buf = buf.slice(0, buf.length - (buf.length % 3));
}
else {
2020-02-29 23:01:03 +01:00
this._remainingBytes = false;
}
2026-09-15 13:00:32 +02:00
let b64 = this._curLine + encode(buf);
2020-02-29 23:01:03 +01:00
if (this.options.lineLength) {
b64 = wrap(b64, this.options.lineLength);
2026-04-28 12:50:45 +02:00
// remove last line as it is still most probably incomplete
const lastLF = b64.lastIndexOf('\n');
2020-02-29 23:01:03 +01:00
if (lastLF < 0) {
this._curLine = b64;
b64 = '';
2026-09-15 13:00:32 +02:00
}
else if (lastLF === b64.length - 1) {
2026-04-28 12:50:45 +02:00
this._curLine = '';
2026-09-15 13:00:32 +02:00
}
else {
2025-12-25 10:58:28 +01:00
this._curLine = b64.substring(lastLF + 1);
b64 = b64.substring(0, lastLF + 1);
2020-02-29 23:01:03 +01:00
}
}
if (b64) {
this.outputBytes += b64.length;
this.push(Buffer.from(b64, 'ascii'));
}
setImmediate(done);
}
2026-09-15 13:00:32 +02:00
/** @internal */
2020-02-29 23:01:03 +01:00
_flush(done) {
if (this._remainingBytes && this._remainingBytes.length) {
this._curLine += encode(this._remainingBytes);
}
if (this._curLine) {
2026-04-28 12:50:45 +02:00
this._curLine = wrap(this._curLine, this.options.lineLength);
2020-02-29 23:01:03 +01:00
this.outputBytes += this._curLine.length;
2025-12-25 10:58:28 +01:00
this.push(Buffer.from(this._curLine, 'ascii'));
2020-02-29 23:01:03 +01:00
this._curLine = '';
}
done();
}
}