* This function will parse the metadata portion of the chunk * @param {Buffer} remainingChunk - chunk sent from _transform * @return {object} response - if error, will return 'err' key with * arsenal error value. * if incomplete metadata, will return 'completeMetadata' key with
(remainingChunk)
| 65 | * the parsed metadata piece |
| 66 | */ |
| 67 | _parseMetadata(remainingChunk) { |
| 68 | let remainingPlusStoredMetadata = remainingChunk; |
| 69 | // have metadata pieces so need to add to the front of |
| 70 | // remainingChunk |
| 71 | if (this.currentMetadata.length > 0) { |
| 72 | this.currentMetadata.push(remainingChunk); |
| 73 | remainingPlusStoredMetadata = Buffer.concat(this.currentMetadata); |
| 74 | // zero out stored metadata |
| 75 | this.currentMetadata.length = 0; |
| 76 | } |
| 77 | let lineBreakIndex = remainingPlusStoredMetadata.indexOf('\r\n'); |
| 78 | if (lineBreakIndex < 0) { |
| 79 | this.currentMetadata.push(remainingPlusStoredMetadata); |
| 80 | return { completeMetadata: false }; |
| 81 | } |
| 82 | let fullMetadata = remainingPlusStoredMetadata.slice(0, |
| 83 | lineBreakIndex); |
| 84 | |
| 85 | // handle extra line break on end of data chunk |
| 86 | if (fullMetadata.length === 0) { |
| 87 | const chunkWithoutLeadingLineBreak = remainingPlusStoredMetadata |
| 88 | .slice(2); |
| 89 | // find second line break |
| 90 | lineBreakIndex = chunkWithoutLeadingLineBreak.indexOf('\r\n'); |
| 91 | if (lineBreakIndex < 0) { |
| 92 | this.currentMetadata.push(chunkWithoutLeadingLineBreak); |
| 93 | return { completeMetadata: false }; |
| 94 | } |
| 95 | fullMetadata = chunkWithoutLeadingLineBreak.slice(0, |
| 96 | lineBreakIndex); |
| 97 | } |
| 98 | |
| 99 | const splitMeta = fullMetadata.toString().split(';'); |
| 100 | this.log.trace('parsed full metadata for chunk', { splitMeta }); |
| 101 | if (splitMeta.length !== 2) { |
| 102 | this.log.trace('chunk body did not contain correct ' + |
| 103 | 'metadata format'); |
| 104 | return { err: errors.InvalidArgument }; |
| 105 | } |
| 106 | let dataSize = splitMeta[0]; |
| 107 | // chunk-size is sent in hex |
| 108 | dataSize = Number.parseInt(dataSize, 16); |
| 109 | if (Number.isNaN(dataSize)) { |
| 110 | this.log.trace('chunk body did not contain valid size'); |
| 111 | return { err: errors.InvalidArgument }; |
| 112 | } |
| 113 | let chunkSig = splitMeta[1]; |
| 114 | if (!chunkSig || chunkSig.indexOf('chunk-signature=') < 0) { |
| 115 | this.log.trace('chunk body did not contain correct sig format'); |
| 116 | return { err: errors.InvalidArgument }; |
| 117 | } |
| 118 | chunkSig = chunkSig.replace('chunk-signature=', ''); |
| 119 | this.currentSignature = chunkSig; |
| 120 | this.haveMetadata = true; |
| 121 | if (dataSize === 0) { |
| 122 | this.lastChunk = true; |
| 123 | return { |
| 124 | completeMetadata: true, |