Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Browser compat #9

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 42 additions & 0 deletions lib/browser-buffer-ops.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const replacementCharacterBuffer = new Uint8Array([0xef, 0xbf, 0xbd]);

const base64js = require('base64-js');

module.exports = {
fromBase64: (b64str) => {
try {
return base64js.toByteArray(b64str);
} catch (e) {
return new Uint8Array();
}
},

toUtf8: TextEncoder.prototype.encode.bind(new TextEncoder()),
fromUtf8: TextDecoder.prototype.decode.bind(new TextDecoder()),
fromAscii: TextDecoder.prototype.decode.bind(new TextDecoder('ascii')),

allocByteBuffer: (length) => {
return new Uint8Array(length);
},

includesReplacementCharacter: (haystack) => {
const needle = replacementCharacterBuffer;
if (haystack.length < needle.length) {
return false;
}
let fromIndex = 0;
while (fromIndex !== haystack.length - 3) {
const foundFirst = haystack[fromIndex] === needle[0];
const foundSecond = haystack[fromIndex + 1] === needle[1];
const foundThird = haystack[fromIndex + 2] === needle[2];

if (foundFirst && foundSecond && foundThird) {
return true;
} else {
fromIndex += 1;
}
}

return false;
},
};
21 changes: 21 additions & 0 deletions lib/node-buffer-ops.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const replacementCharacterBuffer = new Uint8Array([0xef, 0xbf, 0xbd]);

module.exports = {
fromBase64: (b64str) => {
return Buffer.from(b64str, 'base64');
},

toUtf8: (str) => {
return Buffer.from(str, 'utf-8');
},
fromUtf8: String,
fromAscii: (buffer) => {
return buffer.toString('ascii');
},

allocByteBuffer: Buffer.alloc,

includesReplacementCharacter: (haystack) => {
return haystack.includes(replacementCharacterBuffer);
},
};
46 changes: 27 additions & 19 deletions lib/rfc2047.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
/* global unescape */

const isUtf8RegExp = /^utf-?8$/i;
const isLatin1RegExp = /^(?:iso-8859-1|latin1)$/i;
const iconvLite = require('iconv-lite');
const isLatin1RegExp = /^(?:iso-8859-1|latin1|us-ascii)$/i;
const bufferOps = require('./node-buffer-ops');
const rfc2047 = (module.exports = {});

let iconv;
let iconvLite;

try {
iconv = require('iconv');
} catch (e) {}

if (!iconv) {
try {
iconvLite = require('iconv-lite');
} catch (e) {}
}

function stringify(obj) {
if (typeof obj === 'string') {
return obj;
} else if (obj === null || typeof obj === 'undefined') {
return '';
} else if (obj instanceof Uint8Array) {
return bufferOps.fromUtf8(obj);
} else {
return String(obj);
}
}

let iconv;
try {
iconv = require('' + 'iconv'); // Prevent browserify from detecting iconv and failing
} catch (e) {}

const replacementCharacterBuffer = Buffer.from('�');

function decodeBuffer(encodedText, encoding) {
if (encoding === 'q') {
encodedText = encodedText.replace(/_/g, ' ');
Expand All @@ -35,7 +43,7 @@ function decodeBuffer(encodedText, encoding) {
numValidlyEncodedBytes += 1;
}
}
const buffer = Buffer.alloc(
const buffer = bufferOps.allocByteBuffer(
encodedText.length - numValidlyEncodedBytes * 2
);
let j = 0;
Expand All @@ -55,7 +63,7 @@ function decodeBuffer(encodedText, encoding) {
}
return buffer;
} else {
return Buffer.from(encodedText, 'base64');
return bufferOps.fromBase64(encodedText);
}
}

Expand Down Expand Up @@ -88,23 +96,23 @@ function decodeEncodedWord(encodedText, encoding, charset) {
converter = new iconv.Iconv('iso-8859-1', 'utf-8//TRANSLIT');
}
try {
return converter.convert(buffer).toString('utf-8');
return bufferOps.fromUtf8(converter.convert(buffer));
} catch (e2) {}
} else if (isUtf8RegExp.test(charset)) {
const decoded = buffer.toString('utf-8');
const decoded = bufferOps.fromUtf8(buffer);
if (
!/\ufffd/.test(decoded) ||
buffer.includes(replacementCharacterBuffer)
bufferOps.includesReplacementCharacter(buffer)
) {
return decoded;
}
} else if (/^(?:us-)?ascii$/i.test(charset)) {
return buffer.toString('ascii');
} else if (iconvLite.encodingExists(charset)) {
} else if (isLatin1RegExp.test(charset)) {
return bufferOps.fromAscii(buffer);
} else if (iconvLite && iconvLite.encodingExists(charset)) {
decoded = iconvLite.decode(buffer, charset);
if (
!/\ufffd/.test(decoded) ||
buffer.includes(replacementCharacterBuffer)
bufferOps.includesReplacementCharacter(buffer)
) {
return decoded;
}
Expand Down Expand Up @@ -257,7 +265,7 @@ rfc2047.encode = (text) => {
const charset = 'utf-8';
// Around 25% faster than encodeURIComponent(token.replace(/ /g, "_")).replace(/%/g, "="):
const encodedWordBody = bufferToQuotedPrintableString(
Buffer.from(token, 'utf-8')
bufferOps.toUtf8(token)
);
if (previousTokenWasEncodedWord) {
result += ' ';
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,20 @@
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1",
"iconv": "^3.0.0",
"mocha": "2.1.0",
"nyc": "^15.1.0",
"prettier": "~2.4.1",
"proxyquire": "^2.1.3",
"unexpected": "10.20.0"
},
"dependencies": {
"iconv": "^3.0.0",
"iconv-lite": "0.4.5"
},
"browser": {
"iconv": false,
"lib/node-buffer-ops.js": "lib/browser-buffer-ops.js"
},
"nyc": {
"include": [
"lib/**"
Expand Down