Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Windows support. Tests now pass on Windows. #33

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 43 additions & 19 deletions lib/mbtiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,56 @@ MBTiles.utils = require('./utils');
MBTiles.schema = fs.readFileSync(__dirname + '/schema.sql', 'utf8');

// Provides access to an mbtiles database file.
// - uri: A parsed URL hash, the only relevant part is `pathname`.
// - options:
// - A string of the file path.
// - Or a parsed URL hash, the relevant parts are `pathname` and
// an optional query string for the batch size.
// - Or an object:
// {path: '/path/to/file.mbtiles', batchSize: 100 (optional) }
//
// - callback: Will be called when the resources have been acquired
// or acquisition failed.
// or acquisition failed.
require('util').inherits(MBTiles, require('events').EventEmitter)
function MBTiles(uri, callback) {
if (typeof uri === 'string') uri = url.parse(uri, true);
else if (typeof uri.query === 'string') uri.query = qs.parse(uri.query);

if (!uri.pathname) {
callback(new Error('Invalid URI ' + url.format(uri)));
return;
function MBTiles(options, callback) {
this._batchSize = 100;
if (typeof options === 'string') {
var parsed = url.parse(options, true);
// only use the parsed url if it has a batch query string
if (parsed.query.batch) {
this.filename = parsed.pathname;
this._batchSize = +parsed.query.batch;
}
else {
// otherwise just use the url and let sqlite
// decide if it can handle it
this.filename = options;
}
}

if (uri.hostname === '.' || uri.hostname == '..') {
uri.pathname = uri.hostname + uri.pathname;
delete uri.hostname;
delete uri.host;
else if (typeof options === 'object') {
if (options.pathname) {
if (options.hostname === '.' || options.hostname == '..') {
this.filename = options.hostname + options.pathname;
}
else {
this.filename = options.pathname;
}
if (typeof options.query === 'string') {
options.query = qs.parse(options.query);
}
this._batchSize = +options.query.batch || this._batchSize;
}
else {
this.filename = options.path;
this._batchSize = +options.batchSize || this._batchSize;
}
}
if (!this.filename) {
callback(new Error('Invalid options. Could not determine file location.'));
}
uri.query = uri.query || {};
if (!uri.query.batch) uri.query.batch = 100;

var mbtiles = this;
this.setMaxListeners(0);
this.filename = uri.pathname;
this._batchSize = +uri.query.batch;
mbtiles._db = new sqlite3.Database(mbtiles.filename, function(err) {
mbtiles._db = new sqlite3.Database(this.filename, function(err) {
if (err) return callback(err);
fs.stat(mbtiles.filename, function(err, stat) {
if (err) return callback(err);
Expand Down