Skip to content

Commit 6b9324a

Browse files
committed
Multiplexes 'target' param based on type.
1 parent 4bf004b commit 6b9324a

File tree

2 files changed

+14
-30
lines changed

2 files changed

+14
-30
lines changed

src/index.js

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,29 @@ exports = module.exports
1212
const CHUNK_SIZE = 262144
1313

1414
// Use a layout + chunkers to convert a directory (or file) to the layout format
15-
exports.import = (dagService, options, callback) => {
15+
exports.import = (target, dagService, options, callback) => {
1616
if (typeof options === 'function') { callback = options; options = {} }
17-
if (!dagService) { return callback(new Error('no dag service provided')) }
17+
18+
if (!target) { return callback(new Error('must specify target')) }
19+
if (!dagService) { return callback(new Error('must specify dag service')) }
1820

1921
// options.recursive : follow dirs
2022
// options.chunkers : obj with chunkers to each type of data, { default: dumb-chunker }
21-
// options.path : import a file hierarchy from a path
22-
// options.stream : import a stream
23-
// options.buffer : import a buffer
2423

25-
// TODO: make first param be 'target' and check type to decide how to import
26-
// path
27-
// stream
28-
// buffer
24+
options = options || {}
2925

30-
if (options.buffer) {
31-
if (!Buffer.isBuffer(options.buffer)) {
32-
return callback(new Error('buffer importer must take a buffer'))
33-
}
34-
bufferImporter(options.buffer, callback)
35-
} else if (options.stream) {
36-
if (!(typeof options.stream.on === 'function')) {
37-
return callback(new Error('stream importer must take a readable stream'))
38-
}
26+
if (Buffer.isBuffer(target)) {
27+
bufferImporter(target, callback)
28+
} else if (typeof target.on === 'function') {
3929
// TODO Create Stream Importer
4030
// streamImporter(options.stream, callback)
4131
return callback(new Error('stream importer has not been built yet'))
42-
} else if (options.path) {
43-
const stats = fs.statSync(options.path)
32+
} else if (typeof target === 'string') {
33+
const stats = fs.statSync(target)
4434
if (stats.isFile()) {
45-
fileImporter(options.path, callback)
35+
fileImporter(target, callback)
4636
} else if (stats.isDirectory() && options.recursive) {
47-
dirImporter(options.path, callback)
37+
dirImporter(target, callback)
4838
} else {
4939
return callback(new Error('recursive must be true to add a directory'))
5040
}

tests/test-import.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,7 @@ describe('layout: importer', function () {
191191
it('import a small buffer', (done) => {
192192
// this is just like "import a small file"
193193
var buf = fs.readFileSync(path.join(__dirname, '/test-data/200Bytes.txt'))
194-
importer.import({
195-
buffer: buf,
196-
dagService: ds
197-
}, function (err, stat) {
194+
importer.import(buf, ds, function (err, stat) {
198195
expect(err).to.not.exist
199196
ds.get(stat.Hash, (err, node) => {
200197
expect(err).to.not.exist
@@ -211,10 +208,7 @@ describe('layout: importer', function () {
211208
it('import a big buffer', (done) => {
212209
// this is just like "import a big file"
213210
var buf = fs.readFileSync(path.join(__dirname, '/test-data/1.2MiB.txt'))
214-
importer.import({
215-
buffer: buf,
216-
dagService: ds
217-
}, function (err, stat) {
211+
importer.import(buf, ds, function (err, stat) {
218212
expect(err).to.not.exist
219213
ds.get(stat.Hash, (err, node) => {
220214
expect(err).to.not.exist

0 commit comments

Comments
 (0)