Skip to content

Commit

Permalink
feat: add optional parameter to skip schema validate (#38)
Browse files Browse the repository at this point in the history
* add optional parameter to skip schema validate

* add optional parameter to skip schema validate

* add optional parameter to skip schema validate

* add optional parameter to skip schema validate

* Update lib/read.js

Co-Authored-By: fent <fentbox@gmail.com>
  • Loading branch information
purebaba and fent committed Jul 21, 2019
1 parent ef87b19 commit 134d1e4
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions lib/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const fs = require('fs');
const BStream = require('bncode').Stream;

const schema = require('./schema');
const Torrent = require('./torrent');


/**
Expand All @@ -10,12 +11,18 @@ const schema = require('./schema');
* @param {String|ReadableStream} file File where the torrent
* resides, can be local file, remote, or a readable stream.
* @param {Function(!Error, Torrent)} callback
* @param {Boolean} validate validate or not schema
* @param {ReadableStream}
*/
module.exports = (file, callback) => {
module.exports = (file, validate = true, callback) => {
if (typeof validate === 'function') {
callback = validate;
validate = true
}
if (!callback) {
callback = () => {};
}

var rs = typeof file === 'string' ? fs.createReadStream(file) : file;
var bstream = new BStream();
rs.pipe(bstream);
Expand All @@ -28,7 +35,11 @@ module.exports = (file, callback) => {
});

bstream.on('data', (result) => {
schema.validate(result, callback);
if (validate) {
schema.validate(result, callback);
return;
}
callback(null, new Torrent(result));
});

return rs;
Expand Down

0 comments on commit 134d1e4

Please sign in to comment.