Skip to content

Commit

Permalink
Remove mmmagic as dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
vanng822 committed May 13, 2016
1 parent 9d45f5b commit df160ee
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 57 deletions.
27 changes: 3 additions & 24 deletions lib/gpx-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ var xml2js = require('xml2js'), parser = new xml2js.Parser();
var fs = require('fs');
var http = require('http');
var https = require('https');
var mmmagic = require('mmmagic');

var ATTRIBUTE_NAME = '$';

Expand Down Expand Up @@ -54,21 +53,11 @@ var gpxParse = function(data, callback) {
});
};
var gpxParseFile = function(filename, callback) {
var magic = new mmmagic.Magic(mmmagic.MAGIC_MIME_TYPE);
magic.detectFile(filename, function(err, result) {
fs.readFile(filename, function(err, result) {
if(err) {
return callback(err, null);
}
if(result == 'application/xml' || result == 'text/xml') {
fs.readFile(filename, function(err, result) {
if(err) {
return callback(err, null);
}
return gpxParse(result, callback);
});
} else {
return callback(new Error('Got unexpected data type'), null);
}
return gpxParse(result, callback);
});
};
var gpxParseURL = function(url, callback, secure) {
Expand All @@ -79,21 +68,11 @@ var gpxParseURL = function(url, callback, secure) {
data += chunk;
});
res.on('end', function() {
var magic, buf;
if(res.statusCode == 200) {
magic = new mmmagic.Magic(mmmagic.MAGIC_MIME_TYPE);
buf = new Buffer(data);
magic.detect(buf, function(err, result) {
if(result == 'application/xml' || result == 'text/xml') {
return gpxParse(buf, callback);
} else {
return callback(new Error('Got unexpected data type'), null);
}
});
return gpxParse(new Buffer(data), callback);
} else {
return callback(new Error('Got unexpected response code'), null);
}

});
}).on('error', function(err) {
return callback(err, null);
Expand Down
31 changes: 4 additions & 27 deletions lib/tcx-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ var xml2js = require('xml2js'), parser = new xml2js.Parser();
var fs = require('fs');
var http = require('http');
var https = require('https');
var mmmagic = require('mmmagic');

var ATTRIBUTE_NAME = '$';

Expand Down Expand Up @@ -57,46 +56,24 @@ var tcxParse = function(data, callback) {
});
};
var tcxParseFile = function(filename, callback) {
var magic = new mmmagic.Magic(mmmagic.MAGIC_MIME_TYPE);
magic.detectFile(filename, function(err, result) {
fs.readFile(filename, function(err, result) {
if(err) {
return callback(err, null);
}
if(result == 'application/xml' || result == 'text/xml') {
fs.readFile(filename, function(err, result) {
if(err) {
return callback(err, null);
}
return tcxParse(result, callback);
});
} else {
return callback(new Error('Got unexpected data type'), null);
}
return tcxParse(result, callback);
});
};
var tcxParseURL = function(url, callback, secure) {
var h = secure? https : http;

h.get(url, function(res) {
var data = '';
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
var magic, buf;
if(res.statusCode == 200) {
magic = new mmmagic.Magic(mmmagic.MAGIC_MIME_TYPE);
buf = new Buffer(data);
magic.detect(buf, function(err, result) {
if(err) {
return callback(err, null);
}
if(result == 'application/xml' || result == 'text/xml') {
return tcxParse(buf, callback);
} else {
return callback(new Error('Got unexpected data type'), null);
}
});
return tcxParse(new Buffer(data), callback);
} else {
return callback(new Error('Got unexpected response code'), null);
}
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"dependencies": {
"xml2js": "0.4.0",
"data2xml": "0.9.0",
"mmmagic": "0.4.1",
"shelly": "0.0.3"
},
"devDependencies": {
Expand All @@ -31,5 +30,5 @@
"url": "https://github.com/vanng822/gps-util/blob/master/LICENSE"
}
],
"version": "0.1.0"
"version": "0.5.0"
}
4 changes: 2 additions & 2 deletions tests/test_parse_gpx.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ vows.describe('Test suite for parsing gpx').addBatch({
},
'Should return an error ': function(err, result) {
assert.equal(err != null, true);
assert.equal(err.message, 'Got unexpected data type');
assert.equal(/Non-whitespace before first tag/.test(err.message), true);
}
},
'Parse gpx URL' : {
Expand Down Expand Up @@ -168,7 +168,7 @@ vows.describe('Test suite for parsing gpx').addBatch({
},
'Should return an error' : function(err, result) {
assert.equal(err != null, true);
assert.equal(err.message, 'Got unexpected data type');
assert.equal(/Non-whitespace before first tag/.test(err.message), true);
}
}
}).export(module)
4 changes: 2 additions & 2 deletions tests/test_parse_tcx.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ vows.describe('Test suite for parsing tcx').addBatch({
},
'Should return an error ': function(err, result) {
assert.equal(err != null, true);
assert.equal(err.message, 'Got unexpected data type');
assert.equal(/Non-whitespace before first tag/.test(err.message), true);
}
},
'Parse tcx URL' : {
Expand Down Expand Up @@ -194,7 +194,7 @@ vows.describe('Test suite for parsing tcx').addBatch({
},
'Should return an error' : function(err, result) {
assert.equal(err != null, true);
assert.equal(err.message, 'Got unexpected data type');
assert.equal(/Non-whitespace before first tag/.test(err.message), true);
}
}
}).export(module)

0 comments on commit df160ee

Please sign in to comment.