src/message.js
function multipartDecode(response) {
    const message = new Uint8Array(response);

    /* Set a maximum length to search for the header boundaries, otherwise
       findToken can run for a long time
    */
    const maxSearchLength = 1000;


    // First look for the multipart mime header
    let separator = stringToUint8Array('\r\n\r\n');
    let headerIndex = findToken(message, separator, 0, maxSearchLength);
    if (headerIndex === -1) {
      throw new Error('Response message has no multipart mime header');
    }

    const header = uint8ArrayToString(message, 0, headerIndex);
    const boundaryString = identifyBoundary(header);
    if (!boundaryString) {
      throw new Error('Header of response message does not specify boundary');
    }

    const boundary = stringToUint8Array(boundaryString);
    const boundaryLength = boundary.length;
    const components = [];

    let offset = headerIndex + separator.length;

    // Loop until we cannot find any more boundaries
    let boundaryIndex;

    while (boundaryIndex !== -1) {
      // Search for the next boundary in the message, starting
      // from the current offset position
      boundaryIndex = findToken(message, boundary, offset);

      // If no further boundaries are found, stop here.
      if (boundaryIndex === -1) {
        break;
      }

      // Extract data from response message, excluding "\r\n"
      const spacingLength = 2;
      const length = boundaryIndex - offset - spacingLength;
      const data = response.slice(offset, offset + length);

      // Add the data to the array of results
      components.push(data);

      // Move the offset to the end of the current section,
      // plus the identified boundary
      offset += length + spacingLength + boundaryLength;
    }

    return components;
}
function multipartDecode(response) {
    const message = new Uint8Array(response);

    /* Set a maximum length to search for the header boundaries, otherwise
       findToken can run for a long time
    */
    const maxSearchLength = 1000;


    // First look for the multipart mime header
    let separator = stringToUint8Array('\r\n\r\n');
    let headerIndex = findToken(message, separator, 0, maxSearchLength);
    if (headerIndex === -1) {
      throw new Error('Response message has no multipart mime header');
    }

    const header = uint8ArrayToString(message, 0, headerIndex);
    const boundaryString = identifyBoundary(header);
    if (!boundaryString) {
      throw new Error('Header of response message does not specify boundary');
    }

    const boundary = stringToUint8Array(boundaryString);
    const boundaryLength = boundary.length;
    const components = [];

    let offset = headerIndex + separator.length;

    // Loop until we cannot find any more boundaries
    let boundaryIndex;

    while (boundaryIndex !== -1) {
      // Search for the next boundary in the message, starting
      // from the current offset position
      boundaryIndex = findToken(message, boundary, offset);

      // If no further boundaries are found, stop here.
      if (boundaryIndex === -1) {
        break;
      }

      // Extract data from response message, excluding "\r\n"
      const spacingLength = 2;
      const length = boundaryIndex - offset - spacingLength;
      const data = response.slice(offset, offset + length);

      // Add the data to the array of results
      components.push(data);

      // Move the offset to the end of the current section,
      // plus the identified boundary
      offset += length + spacingLength + boundaryLength;
    }

    return components;
}
function multipartDecode ( response ) { const message = new Uint8Array ( response ) ; /* Set a maximum length to search for the header boundaries, otherwise
       findToken can run for a long time
    */ const maxSearchLength = 1000 ; // First look for the multipart mime header let separator = stringToUint8Array ( '\r\n\r\n' ) ; let headerIndex = findToken ( message , separator , 0 , maxSearchLength ) ; if ( headerIndex === - 1 ) { throw new Error ( 'Response message has no multipart mime header' ) ; } const header = uint8ArrayToString ( message , 0 , headerIndex ) ; const boundaryString = identifyBoundary ( header ) ; if ( ! boundaryString ) { throw new Error ( 'Header of response message does not specify boundary' ) ; } const boundary = stringToUint8Array ( boundaryString ) ; const boundaryLength = boundary . length ; const components = [ ] ; let offset = headerIndex + separator . length ; // Loop until we cannot find any more boundaries let boundaryIndex ; while ( boundaryIndex !== - 1 ) { // Search for the next boundary in the message, starting // from the current offset position boundaryIndex = findToken ( message , boundary , offset ) ; // If no further boundaries are found, stop here. if ( boundaryIndex === - 1 ) { break ; } // Extract data from response message, excluding "\r\n" const spacingLength = 2 ; const length = boundaryIndex - offset - spacingLength ; const data = response . slice ( offset , offset + length ) ; // Add the data to the array of results components . push ( data ) ; // Move the offset to the end of the current section, // plus the identified boundary offset += length + spacingLength + boundaryLength ; } return components ; }
Decode a Multipart encoded ArrayBuffer and return the components as an Array.

@param {ArrayBuffer} response Data encoded as a 'multipart/related' message
@returns {Array} The content
Decode a Multipart encoded ArrayBuffer and return the components as an Array .