<commit_before>/**
 * Created by tushar.mathur on 20/01/16.
 */

'use strict'
const _ = require('lodash')
const createStore = require('reactive-storage').create
const u = require('./utils')

exports.download = function (ob, options) {
  var writeAt = 0
  const path = options.mtdPath
  const fileDescriptor = ob.fsOpen(path, 'w+')
  const writtenAt = createStore(0)
  const requestStream = ob.requestBody(options)
  const bufferStream = requestStream.filter(x => x.event === 'data').pluck('message')
  const contentLength = requestStream
    .filter(x => x.event === 'response')
    .pluck('message', 'headers', 'content-length')
    .map(x => parseInt(x, 10))

  return fileDescriptor
    .combineLatest(bufferStream, u.selectAs('fd', 'buffer'))
    .map(buffer => _.assign({}, buffer, {offset: writeAt}))
    .tap(x => writeAt += x.buffer.length)
    .flatMap(ob.fsWriteBuffer).map(x => x[0])
    .tap(x => writtenAt.set(o => o + x))
    .tapOnCompleted(() => writtenAt.end())
    .combineLatest(writtenAt.getStream(), (a, b) => b)
    .distinctUntilChanged()
    .withLatestFrom(contentLength, u.selectAs('bytesSaved', 'totalBytes'))
    .map(x => _.assign({}, x, options))
    .map(x => u.toBuffer(x, options.maxBuffer))
    .withLatestFrom(fileDescriptor, contentLength, u.selectAs('buffer', 'fd', 'offset'))
    .flatMap(ob.fsWriteBuffer)
    .last().withLatestFrom(contentLength, (a, b) => b)
    .flatMap(len => ob.fsTruncate(path, len))
    .flatMap(() => ob.fsRename(path, options.path))
    .map(options)
}<commit_msg>refactor(download): remove optional buffer size in toBuffer() call<commit_after>/**
 * Created by tushar.mathur on 20/01/16.
 */

'use strict'
const _ = require('lodash')
const createStore = require('reactive-storage').create
const u = require('./utils')

exports.download = function (ob, options) {
  var writeAt = 0
  const path = options.mtdPath
  const fileDescriptor = ob.fsOpen(path, 'w+')
  const writtenAt = createStore(0)
  const requestStream = ob.requestBody(options)
  const bufferStream = requestStream.filter(x => x.event === 'data').pluck('message')
  const contentLength = requestStream
    .filter(x => x.event === 'response')
    .pluck('message', 'headers', 'content-length')
    .map(x => parseInt(x, 10))

  return fileDescriptor
    .combineLatest(bufferStream, u.selectAs('fd', 'buffer'))
    .map(buffer => _.assign({}, buffer, {offset: writeAt}))
    .tap(x => writeAt += x.buffer.length)
    .flatMap(ob.fsWriteBuffer).map(x => x[0])
    .tap(x => writtenAt.set(o => o + x))
    .tapOnCompleted(() => writtenAt.end())
    .combineLatest(writtenAt.getStream(), (a, b) => b)
    .distinctUntilChanged()
    .withLatestFrom(contentLength, u.selectAs('bytesSaved', 'totalBytes'))
    .map(x => _.assign({}, x, options))
    .map(u.toBuffer)
    .withLatestFrom(fileDescriptor, contentLength, u.selectAs('buffer', 'fd', 'offset'))
    .flatMap(ob.fsWriteBuffer)
    .last().withLatestFrom(contentLength, (a, b) => b)
    .flatMap(len => ob.fsTruncate(path, len))
    .flatMap(() => ob.fsRename(path, options.path))
    .map(options)
}
{'fraction_non_alphanumeric': 0.1819066147859922, 'fraction_numerical': 0.007133592736705577, 'mean_word_length': 4.1245847176079735, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1304047', 'n_tokens_mistral': 1065, 'n_tokens_neox': 1039, 'n_words': 217}