Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
exports.SocketStream = function(socket) {
socket = util.makeEventTarget(socket);
function maybeCloseSocket() {
if (socket.readyState !== 'closing' && socket.readyState !== 'closed') {
socket.close();
}
}
var out;
this.readable = new streams.ReadableStream({
start: function(c) {
out = c;
socket.addEventListener('data', (evt) => {
c.enqueue(new Uint8Array(evt.data))
});
socket.addEventListener('close', () => {
try {
c.close();
} catch(e) {
// The stream has already been closed.
}
});
socket.addEventListener('error', (evt) => c.error(evt.data || evt));
},
cancel: function() {
maybeCloseSocket();
return function messageChunkedPartStream({
pimap, folderInfo, uid, parts, downloadChunkSize, saveChunkSize }) {
// Pull the parts off as we go.
let remainingPartsToFetch = parts.slice();
// A pull stream, where each pull() corresponds to fetching a single part and
// the generator will enqueue once for each blob and once to close out the
// part.
return new ReadableStream({
start() {
},
pull: co.wrap(function*(out) {
if (!remainingPartsToFetch.length) {
out.close();
return;
}
let blobIndex = 0;
let partInfo = remainingPartsToFetch.shift();
let mimeStream = chunkedDownloadMimeStream({
pimap,
folderInfo,
uid,
partInfo,
return function chunkedDownloadMimeStream(
{ ctx, pimap, folderInfo, uid, partInfo, downloadChunkSize, saveChunkSize }) {
let byteIndex = 0;
let byteStream = new ReadableStream({
start(out) {
out.enqueue(
mimefuncs.charset.encode(
'Content-Type: ' + partInfo.type + '\r\n' +
'Content-Transfer-Encoding: ' + partInfo.encoding + '\r\n' +
'\r\n'));
},
pull(out) {
return pimap.fetchBody(
ctx,
folderInfo,
{
uid,
part: partInfo.part,
byteRange: {
offset: byteIndex,
startPart(jsmimePartNum, jsmimeHeaders) {
var partNum = (jsmimePartNum === '' ? '1' : '1.' + jsmimePartNum);
var headers = MimeHeaderInfo.fromJSMime(jsmimeHeaders, {
parentContentType:
partNum.indexOf('.') !== -1
? partToContentTypeMap.get(
partNum.slice(0, partNum.lastIndexOf('.')))
: null
});
partToContentTypeMap.set(partNum, headers.contentType);
var bodyStream = null;
if (headers.mediatype !== 'multipart') {
bodyStream = new streams.ReadableStream({
start(controller) {
partToStreamControllerMap.set(partNum, controller);
}
}).pipeThrough(new exports.BlobTransformStream());
}
out.enqueue({
partNum: partNum,
headers: headers,
bodyStream: bodyStream
});
},
endPart(jsmimePartNum) {