libmime 2.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.eslintrc.js CHANGED
@@ -45,7 +45,8 @@ module.exports = {
45
45
  'no-bitwise': 2,
46
46
  'no-lonely-if': 2,
47
47
  'no-mixed-spaces-and-tabs': 2,
48
- 'no-console': 0
48
+ 'no-console': 2,
49
+ 'no-control-regex': 0
49
50
  },
50
51
  env: {
51
52
  es6: false,
package/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # Changelog
2
2
 
3
+ ## v2.1.0 2016-07-24
4
+
5
+ * Changed handling of base64 encoded mime words where multiple words are joined together if possible. This fixes issues with multi byte characters getting split into different mime words (against the RFC but occurs)
6
+
7
+ ## v2.0.3 2016-02-29
8
+
9
+ * Fixed an issue with rfc2231 filenames
10
+
11
+ ## v2.0.2 2016-02-11
12
+
13
+ * Fixed an issue with base64 mime words encoding
14
+
15
+ ## v2.0.1 2016-02-11
16
+
17
+ * Fix base64 mime-word encoding. Final string length was calculated invalidly
18
+
3
19
  ## v2.0.0 2016-01-04
4
20
 
5
21
  * Replaced jshint with eslint
package/lib/charset.js CHANGED
@@ -72,7 +72,7 @@ var charset = module.exports = {
72
72
  return 'UTF-' + match[1];
73
73
  }
74
74
 
75
- if ((match = charset.match(/^win[\-_]?(\d+)$/i))) {
75
+ if ((match = charset.match(/^win(?:dows)?[\-_]?(\d+)$/i))) {
76
76
  return 'WINDOWS-' + match[1];
77
77
  }
78
78
 
package/lib/libmime.js CHANGED
@@ -105,7 +105,7 @@ var libmime = module.exports = {
105
105
 
106
106
  var encodedStr,
107
107
  toCharset = 'UTF-8',
108
- i, len, parts;
108
+ i, len, parts, lpart, chr;
109
109
 
110
110
  if (maxLength && maxLength > 7 + toCharset.length) {
111
111
  maxLength -= (7 + toCharset.length);
@@ -123,18 +123,30 @@ var libmime = module.exports = {
123
123
  });
124
124
  } else if (mimeWordEncoding === 'B') {
125
125
  encodedStr = typeof data === 'string' ? data : libbase64.encode(data);
126
- maxLength = Math.max(3, (maxLength - maxLength % 4) / 4 * 3);
126
+ maxLength = maxLength ? Math.max(3, (maxLength - maxLength % 4) / 4 * 3) : 0;
127
127
  }
128
128
 
129
- if (maxLength && encodedStr.length > maxLength) {
129
+ if (maxLength && (mimeWordEncoding !== 'B' ? encodedStr : libbase64.encode(data)).length > maxLength) {
130
130
  if (mimeWordEncoding === 'Q') {
131
131
  encodedStr = splitMimeEncodedString(encodedStr, maxLength).join('?= =?' + toCharset + '?' + mimeWordEncoding + '?');
132
132
  } else {
133
-
134
133
  // RFC2047 6.3 (2) states that encoded-word must include an integral number of characters, so no chopping unicode sequences
135
134
  parts = [];
136
- for (i = 0, len = encodedStr.length; i < len; i += maxLength) {
137
- parts.push(libbase64.encode(encodedStr.substr(i, maxLength)));
135
+ lpart = '';
136
+ for (i = 0, len = encodedStr.length; i < len; i++) {
137
+ chr = encodedStr.charAt(i);
138
+ // check if we can add this character to the existing string
139
+ // without breaking byte length limit
140
+ if (Buffer.byteLength(lpart + chr) <= maxLength || i === 0) {
141
+ lpart += chr;
142
+ } else {
143
+ // we hit the length limit, so push the existing string and start over
144
+ parts.push(libbase64.encode(lpart));
145
+ lpart = chr;
146
+ }
147
+ }
148
+ if (lpart) {
149
+ parts.push(libbase64.encode(lpart));
138
150
  }
139
151
 
140
152
  if (parts.length > 1) {
@@ -217,14 +229,29 @@ var libmime = module.exports = {
217
229
  * @return {String} Decoded unicode string
218
230
  */
219
231
  decodeWords: function (str) {
220
- str = (str || '').toString();
221
- str = str.
232
+ return (str || '').toString().
233
+
234
+ // find base64 words that can be joined
235
+ replace(/(=\?([^?]+)\?[Bb]\?[^?]+[^^=]\?=)\s*(?==\?([^?]+)\?[Bb]\?[^?]+\?=)/g,
236
+ function (match, left, chLeft, chRight) {
237
+ // only mark to b64 chunks to be joined if charsets match
238
+ if (libcharset.normalizeCharset(chLeft || '').toLowerCase().trim() === libcharset.normalizeCharset(chRight || '').toLowerCase().trim()) {
239
+ // set a joiner marker
240
+ return left + '__\x00JOIN\x00__';
241
+ }
242
+ return match;
243
+ }).
244
+
245
+ // join base64 encoded words
246
+ replace(/(\?=)?__\x00JOIN\x00__(=\?([^?]+)\?[Bb]\?)?/g, '').
247
+
248
+ // remove spaces between mime encoded words
222
249
  replace(/(=\?[^?]+\?[QqBb]\?[^?]+\?=)\s+(?==\?[^?]+\?[QqBb]\?[^?]+\?=)/g, '$1').
250
+
251
+ // decode words
223
252
  replace(/\=\?([\w_\-\*]+)\?([QqBb])\?[^\?]+\?\=/g, function (mimeWord) {
224
253
  return libmime.decodeWord(mimeWord);
225
254
  });
226
-
227
- return str;
228
255
  },
229
256
 
230
257
  /**
@@ -293,7 +320,7 @@ var libmime = module.exports = {
293
320
  var value = structured.params[param];
294
321
  if (!libmime.isPlainText(value) || value.length >= 75) {
295
322
  libmime.buildHeaderParam(param, value, 50).forEach(function (encodedParam) {
296
- if (!/[\s"\\;\/=]|^[\-']|'$/.test(encodedParam.value)) {
323
+ if (!/[\s"\\;\/=]|^[\-']|'$/.test(encodedParam.value) || encodedParam.key.substr(-1) === '*') {
297
324
  paramsArray.push(encodedParam.key + '=' + encodedParam.value);
298
325
  } else {
299
326
  paramsArray.push(encodedParam.key + '=' + JSON.stringify(encodedParam.value));
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "libmime",
3
3
  "description": "Encode and decode quoted printable and base64 strings",
4
- "version": "2.0.0",
4
+ "version": "2.1.0",
5
5
  "main": "lib/libmime",
6
6
  "homepage": "https://github.com/andris9/libmime",
7
7
  "repository": {
@@ -16,7 +16,7 @@
16
16
  ],
17
17
  "author": "Andris Reinman <andris@kreata.ee>",
18
18
  "scripts": {
19
- "test": "grunt"
19
+ "test": "grunt mochaTest"
20
20
  },
21
21
  "dependencies": {
22
22
  "iconv-lite": "0.4.13",
@@ -24,10 +24,11 @@
24
24
  "libqp": "1.1.0"
25
25
  },
26
26
  "devDependencies": {
27
- "chai": "^3.4.1",
28
- "grunt": "^0.4.5",
29
- "grunt-eslint": "^17.3.1",
27
+ "chai": "^3.5.0",
28
+ "grunt": "^1.0.1",
29
+ "grunt-cli": "^1.2.0",
30
+ "grunt-eslint": "^19.0.0",
30
31
  "grunt-mocha-test": "^0.12.7",
31
- "mocha": "^2.3.4"
32
+ "mocha": "^3.0.2"
32
33
  }
33
34
  }