Skip to content

Commit

Permalink
lib, test: update Buffer API usage
Browse files Browse the repository at this point in the history
  • Loading branch information
mscdex committed Feb 17, 2022
1 parent 867aa88 commit 7dbc664
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 21 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ An upgrade guide from node-imap v0.7.x to v0.8.x can be found [here](https://git
Requirements
============

* [node.js](http://nodejs.org/) -- v0.8.0 or newer

* NOTE: node v0.8.x users are supported via the readable-stream module which
may not be up-to-date (compared to node v0.10 streams2 implementation)
* [node.js](http://nodejs.org/) -- v10.0.0 or newer

* An IMAP server to connect to -- tested with gmail

Expand Down
9 changes: 5 additions & 4 deletions lib/Connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,15 +240,16 @@ Connection.prototype.connect = function() {
// now idling
self._idle.started = Date.now();
} else if (/^AUTHENTICATE XOAUTH/.test(self._curReq.fullcmd)) {
self._curReq.oauthError = new Buffer(info.text, 'base64').toString('utf8');
self._curReq.oauthError =
Buffer.from(info.text, 'base64').toString('utf8');
self.debug && self.debug('=> ' + inspect(CRLF));
self._sock.write(CRLF);
} else if (type === 'APPEND') {
self._sockWriteAppendData(self._curReq.appendData);
} else if (self._curReq.lines && self._curReq.lines.length) {
var line = self._curReq.lines.shift() + '\r\n';
self.debug && self.debug('=> ' + inspect(line));
self._sock.write(line, 'binary');
self._sock.write(line, 'latin1');
}
});
parser.on('other', function(line) {
Expand Down Expand Up @@ -1847,8 +1848,8 @@ function buildString(str) {
str = ''+str;

if (hasNonASCII(str)) {
var buf = new Buffer(str, 'utf8');
return '{' + buf.length + '}\r\n' + buf.toString('binary');
var buf = Buffer.from(str, 'utf8');
return '{' + buf.length + '}\r\n' + buf.toString('latin1');
} else
return '"' + escape(str) + '"';
}
Expand Down
15 changes: 8 additions & 7 deletions lib/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ function parseFetch(text, literals, seqno) {
val = parseBodyStructure(val);
else if (m = RE_BODYINLINEKEY.exec(list[i])) {
// a body was sent as a non-literal
val = new Buffer(''+val);
val = Buffer.from(''+val);
body = new ReadableStream();
body._readableState.sync = false;
body._read = EMPTY_READCB;
Expand Down Expand Up @@ -764,7 +764,7 @@ function decodeBytes(buf, encoding, offset, mlen, pendoffset, state, nextBuf) {
if (state.encoding === encoding && state.consecutive) {
// concatenate buffer + current bytes in hopes of finally having
// something that's decodable
var newbuf = new Buffer(state.buffer.length + buf.length);
var newbuf = Buffer.allocUnsafe(state.buffer.length + buf.length);
state.buffer.copy(newbuf, 0);
buf.copy(newbuf, state.buffer.length);
buf = newbuf;
Expand Down Expand Up @@ -795,7 +795,8 @@ function decodeBytes(buf, encoding, offset, mlen, pendoffset, state, nextBuf) {
// try to decode a lookahead buffer (current buffer + next buffer)
// and see if it starts with the decoded value of the current buffer.
// if not, the current buffer is partial
var lookahead, lookaheadBuf = new Buffer(buf.length + nextBuf.length);
var lookahead, lookaheadBuf =
Buffer.allocUnsafe(buf.length + nextBuf.length);
buf.copy(lookaheadBuf);
nextBuf.copy(lookaheadBuf, buf.length);
try {
Expand Down Expand Up @@ -862,7 +863,7 @@ function decodeBytes(buf, encoding, offset, mlen, pendoffset, state, nextBuf) {
state.replaces.push({
fromOffset: offset,
toOffset: offset + mlen,
val: buf.toString('binary')
val: buf.toString('latin1')
});
}

Expand Down Expand Up @@ -924,17 +925,17 @@ function decodeWords(str, state) {
state.consecutive = m.consecutive;
if (m.encoding === 'q') {
// q-encoding, similar to quoted-printable
bytes = new Buffer(m.chunk.replace(RE_QENC, qEncReplacer), 'binary');
bytes = Buffer.from(m.chunk.replace(RE_QENC, qEncReplacer), 'latin1');
next = undefined;
} else {
// base64
bytes = m.buf || new Buffer(m.chunk, 'base64');
bytes = m.buf || Buffer.from(m.chunk, 'base64');
next = replaces[i + 1];
if (next && next.consecutive && next.encoding === m.encoding
&& next.charset === m.charset) {
// we use the next base64 chunk, if any, to determine the integrity
// of the current chunk
next.buf = new Buffer(next.chunk, 'base64');
next.buf = Buffer.from(next.chunk, 'base64');
}
}
decodeBytes(bytes, m.charset, m.index, m.length, m.pendoffset, state,
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
"description": "An IMAP module for node.js that makes communicating with IMAP servers easy",
"main": "./lib/Connection",
"dependencies": {
"utf7": ">=1.0.2",
"readable-stream": "1.1.x"
"utf7": ">=1.0.2"
},
"scripts": {
"test": "node test/test.js"
},
"engines": { "node": ">=0.8.0" },
"engines": { "node": ">=10.0.0" },
"keywords": [ "imap", "mail", "email", "reader", "client" ],
"licenses": [ { "type": "MIT", "url": "http://github.com/mscdex/node-imap/raw/master/LICENSE" } ],
"repository": { "type": "git", "url": "http://github.com/mscdex/node-imap.git" }
Expand Down
6 changes: 3 additions & 3 deletions test/test-connection-fetch-spillover.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ srv.listen(0, '127.0.0.1', function() {
// first allow body.push() to return false in parser.js
setTimeout(function() {
stream.on('data', function(chunk) {
body[nbody] += chunk.toString('binary');
body[nbody] += chunk.toString('latin1');
});
setTimeout(function() {
var oldRead = stream._read,
Expand Down Expand Up @@ -137,7 +137,7 @@ srv.listen(0, '127.0.0.1', function() {
}, 100);
} else {
stream.on('data', function(chunk) {
body[nbody] += chunk.toString('binary');
body[nbody] += chunk.toString('latin1');
});
}
});
Expand Down Expand Up @@ -173,4 +173,4 @@ process.once('exit', function() {
which: 'TEXT',
size: 200
}]);
});
});

0 comments on commit 7dbc664

Please sign in to comment.