Code coverage report for lib/deps/multipart.js

Statements: 97.5% (39 / 40)      Branches: 75% (6 / 8)      Functions: 100% (5 / 5)      Lines: 97.5% (39 / 40)      Ignored: none     

All files » lib/deps/ » multipart.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79                1 1 1 1 1 1 1 1 1   1 67     67 747       1 68   68   68   68 280 280 6   274 273 273 273 273 273     67     67   67 273 273           273 273     67   67 67   67               1  
'use strict';
 
// Create a multipart/related stream out of a document,
// so we can upload documents in that format when
// attachments are large. This is shamefully stolen from
// https://github.com/sballesteros/couch-multipart-stream/
// and https://github.com/npm/npm-fullfat-registry
 
var base64 = require('./binary/base64');
var atob = base64.atob;
var uuid = require('./uuid');
var createBlob = require('./binary/blob');
var binaryStringToArrayBuffer = require('./binary/binaryStringToArrayBuffer');
var utils = require('../utils');
var clone = utils.clone;
var isBrowser = typeof process === 'undefined' || process.browser;
var buffer = require('./binary/buffer');
 
function createBlobOrBuffer(parts, type) {
  Iif (isBrowser) {
    return createBlob(parts, {type: type});
  }
  return buffer.concat(parts.map(function (part) {
    return new buffer(part, 'binary');
  }));
}
 
function createMultipart(doc) {
  doc = clone(doc);
 
  var boundary = uuid();
 
  var nonStubAttachments = {};
 
  Object.keys(doc._attachments).forEach(function (filename) {
    var att = doc._attachments[filename];
    if (att.stub) {
      return;
    }
    var binData = atob(att.data);
    nonStubAttachments[filename] = {type: att.content_type, data: binData};
    att.length = binData.length;
    att.follows = true;
    delete att.digest;
    delete att.data;
  });
 
  var preamble = '--' + boundary +
    '\r\nContent-Type: application/json\r\n\r\n';
 
  var parts = [preamble, JSON.stringify(doc)];
 
  Object.keys(nonStubAttachments).forEach(function (filename) {
    var att = nonStubAttachments[filename];
    var preamble = '\r\n--' + boundary +
      '\r\nContent-Disposition: attachment; filename=' +
        JSON.stringify(filename) +
      '\r\nContent-Type: ' + att.type +
      '\r\nContent-Length: ' + att.data.length +
      '\r\n\r\n';
    parts.push(preamble);
    parts.push(isBrowser ? binaryStringToArrayBuffer(att.data) : att.data);
  });
 
  parts.push('\r\n--' + boundary + '--');
 
  var type = 'multipart/related; boundary=' + boundary;
  var body = createBlobOrBuffer(parts, type);
 
  return {
    headers: {
      'Content-Type': type
    },
    body: body
  };
}
 
module.exports = createMultipart;