Skip to content

Commit

Permalink
refactor(main): expose IO modules
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharmath committed Jun 27, 2016
1 parent 886a6d1 commit e034326
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 58 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"engines": {
"node": ">= 5.0.0"
},
"main": ".dist/index.js",
"main": ".dist/Main.js",
"dependencies": {
"graceful-fs": "^4.1.3",
"humanize-plus": "^1.8.1",
Expand Down
40 changes: 40 additions & 0 deletions src/IO.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict'

import {Observable as O} from 'rx'
import * as Rx from './RxFP'
import {demux} from 'muxer'
import R from 'ramda'
import {Request} from './Request'

export const fromCB = R.compose(R.apply, O.fromNodeCallback)
export const toOB = cb => R.compose(
Rx.shareReplay(1),
Rx.flatMap(fromCB(cb))
)
export const FILE = R.curry((fs) => {
return {
// New Methods
open: toOB(fs.open),
fstat: toOB(fs.fstat),
read: toOB(fs.read),
write: toOB(fs.write),
close: toOB(fs.close),
truncate: toOB(fs.truncate),
rename: toOB(fs.rename)
}
})

export const HTTP = R.curry((_request) => {
const request = Request(_request)
const requestHead = (params) => {
const [{response$}] = demux(request(params), 'response$')
return response$.first().tap(x => x.destroy()).share()
}

const select = R.curry((event, request$) => request$.filter(x => x.event === event).pluck('message'))
return {
requestHead,
select,
request
}
})
21 changes: 11 additions & 10 deletions src/index.js → src/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import fs from 'graceful-fs'
import {Observable as O} from 'rx'
import R from 'ramda'
import * as U from './Utils'
import * as T from './Transformers'
import * as T from './IO'
import {mux, demux} from 'muxer'

export const Utils = U
export const UTILS = U
export const createDownload = (_options) => {
const [HTTP] = T.HTTP(request)
const [FILE] = T.FILE(fs)

const HTTP = T.HTTP(request)
const FILE = T.FILE(fs)
const options = U.MergeDefaultOptions(_options)

/**
Expand All @@ -36,7 +37,11 @@ export const createDownload = (_options) => {
* Finalize Downloaded FILE
*/
const finalizeDownload$ = downloadFromMTDFile$.last()
.withLatestFrom(fdR$, meta$, (_, fd, meta) => ({FILE, fd$: O.just(fd), meta$: O.just(meta)}))
.withLatestFrom(fdR$, meta$, (_, fd, meta) => ({
FILE,
fd$: O.just(fd),
meta$: O.just(meta)
}))
.flatMap(U.FinalizeDownload)
.share()
.last()
Expand All @@ -48,9 +53,5 @@ export const createDownload = (_options) => {
.map(R.tail)
.flatMap(R.map(R.of))
const closed$ = FILE.close(fd$)

/**
* Create Sink
*/
return mux({response$, meta$, closed$})
return [mux({response$, meta$, closed$}), {FILE, HTTP, UTILS}]
}
40 changes: 0 additions & 40 deletions src/Transformers.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/bin/mtd.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import meow from 'meow'
import humanize from 'humanize-plus'
import newDownload from './NewDownload'
import resumeDownload from './ResumeDownload'
import {createDownload} from '../index'
import {createDownload} from '../Main'
import ProgressBar from 'progress'

const flags = meow(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import request from 'request'
import {server} from '../../perf/server'
import test from 'ava'
import {HTTP} from '../../src/Transformers'
import {HTTP} from '../../src/IO'
import {demux} from 'muxer'

const http = HTTP(request)[0]
const http = HTTP(request)
let closeHttp
/*eslint-disable */
test.before(async function () {
Expand Down
8 changes: 4 additions & 4 deletions test/integration/test.mtd.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import Path from 'path'
import test from 'ava'
import {removeFile, createFileDigest} from '../../perf/TestHelpers'
import {createDownload} from '../../src/index'
import {createDownload} from '../../src/Main'
import {server} from '../../perf/server'

const pathFactory = () => {
Expand Down Expand Up @@ -35,7 +35,7 @@ test.after(async function () {
})

test('http', async function (t) {
await createDownload({url: 'http://localhost:3200/files/pug.jpg', path: path1}).toPromise()
await createDownload({url: 'http://localhost:3200/files/pug.jpg', path: path1})[0].toPromise()
const digest = await createFileDigest(path1)
t.deepEqual(digest, '25FD4542D7FFFB3AEC9EF0D25A533DDE4803B9C1')
})
Expand All @@ -45,7 +45,7 @@ test('https', async function (t) {
url: 'https://localhost:3201/files/pug.jpg',
path: path2,
strictSSL: false
}).toPromise()
})[0].toPromise()
const digest = await createFileDigest(path2)
t.deepEqual(digest, '25FD4542D7FFFB3AEC9EF0D25A533DDE4803B9C1')
})
Expand All @@ -54,7 +54,7 @@ test('http(2)', async function (t) {
await createDownload({
url: 'http://localhost:3200/files/in.txt',
path: path3
}).toPromise()
})[0].toPromise()
const digest = await createFileDigest(path3)
t.deepEqual(digest, 'A9070D71168B5135910A04F0650A91541B72762E')
})

0 comments on commit e034326

Please sign in to comment.