* This function will parse the chunk into metadata and data, * use the metadata to authenticate with vault and send the * data on to be stored if authentication passes * * @param {Buffer} chunk - chunk from request body * @param {string} encoding - Data encoding * @para
(chunk, encoding, callback)
| 187 | * @return {function} executes callback with err if applicable |
| 188 | */ |
| 189 | _transform(chunk, encoding, callback) { |
| 190 | // 'chunk' here is the node streaming chunk |
| 191 | // transfer-encoding chunks should be of the format: |
| 192 | // string(IntHexBase(chunk-size)) + ";chunk-signature=" + |
| 193 | // signature + \r\n + chunk-data + \r\n |
| 194 | // Last transfer-encoding chunk will have size 0 and no chunk-data. |
| 195 | |
| 196 | // if there was an error earlier, ignore the remaining data |
| 197 | if (this.clientError) { |
| 198 | return callback(); |
| 199 | } |
| 200 | if (this.lastPieceDone) { |
| 201 | const slice = chunk.slice(0, 10); |
| 202 | this.log.trace('received chunk after end.' + |
| 203 | 'See first 10 bytes of chunk', |
| 204 | { chunk: slice.toString() }); |
| 205 | return callback(); |
| 206 | } |
| 207 | let unparsedChunk = chunk; |
| 208 | let chunkLeftToEvaluate = true; |
| 209 | return async.whilst( |
| 210 | // test function |
| 211 | () => chunkLeftToEvaluate, |
| 212 | // async function |
| 213 | done => { |
| 214 | if (!this.haveMetadata) { |
| 215 | this.log.trace('do not have metadata so calling ' + |
| 216 | '_parseMetadata'); |
| 217 | // need to parse our metadata |
| 218 | const parsedMetadataResults = |
| 219 | this._parseMetadata(unparsedChunk); |
| 220 | if (parsedMetadataResults.err) { |
| 221 | return done(parsedMetadataResults.err); |
| 222 | } |
| 223 | // if do not have full metadata get next chunk |
| 224 | if (!parsedMetadataResults.completeMetadata) { |
| 225 | chunkLeftToEvaluate = false; |
| 226 | return done(); |
| 227 | } |
| 228 | // have metadata so reset unparsedChunk to remaining |
| 229 | // without metadata piece |
| 230 | unparsedChunk = parsedMetadataResults.unparsedChunk; |
| 231 | } |
| 232 | if (this.lastChunk) { |
| 233 | this.log.trace('authenticating final chunk with no data'); |
| 234 | return this._authenticate(null, err => { |
| 235 | if (err) { |
| 236 | return done(err); |
| 237 | } |
| 238 | chunkLeftToEvaluate = false; |
| 239 | this.lastPieceDone = true; |
| 240 | return done(); |
| 241 | }); |
| 242 | } |
| 243 | if (unparsedChunk.length < this.seekingDataSize) { |
| 244 | // add chunk to currentData and get next chunk |
| 245 | unparsedChunk.copy(this.currentData, this.dataCursor); |
| 246 | this.dataCursor += unparsedChunk.length; |
nothing calls this directly
no test coverage detected