diff --git a/lib/mbtiles.js b/lib/mbtiles.js index 59ad15c..f143167 100644 --- a/lib/mbtiles.js +++ b/lib/mbtiles.js @@ -55,9 +55,6 @@ function MBTiles(uri, callback) { this._batchSize = +uri.query.batch; Step(function() { mbtiles._db = new sqlite3.Database(mbtiles.filename, this); - }, function(err) { - if (err) throw err; - mbtiles._setup(this); }, function(err) { if (err) throw err; fs.stat(mbtiles.filename, this); @@ -174,7 +171,7 @@ MBTiles.prototype.getTile = function(z, x, y, callback) { } else { var options = { 'Content-Type': MBTiles.utils.getMimeType(row.tile_data), - 'Last-Modified': mbtiles._stat.mtime, + 'Last-Modified': new Date(mbtiles._stat.mtime).toUTCString(), 'ETag': mbtiles._stat.size + '-' + Number(mbtiles._stat.mtime) }; return callback(null, row.tile_data, options); @@ -216,21 +213,22 @@ MBTiles.prototype.getGrid = function(z, x, y, callback) { if (err) return callback(err); zlib.inflate(!Buffer.isBuffer(row.grid) - ? new Buffer(row.grid, 'binary') - : row.grid - , function(err,buffer) { - if (err) { - return callback(new Error('Grid is invalid:' + err.message)); - } - var data = rows.reduce(function(memo, r) { - memo[r.key_name] = JSON.parse(r.key_json); - return memo; - }, {}); - var result = _(JSON.parse(buffer.toString())).extend({ data: data }); - - callback(null, result); - }); - + ? new Buffer(row.grid, 'binary') + : row.grid, + function(err,buffer) { + if (err) return callback(new Error('Grid is invalid:' + err.message)); + var data = rows.reduce(function(memo, r) { + memo[r.key_name] = JSON.parse(r.key_json); + return memo; + }, {}); + var result = _(JSON.parse(buffer.toString())).extend({ data: data }); + var options = { + 'Content-Type': 'text/javascript', + 'Last-Modified': new Date(that._stat.mtime).toUTCString(), + 'ETag': that._stat.size + '-' + Number(that._stat.mtime) + }; + callback(null, result, options); + }); } ); }; @@ -385,18 +383,24 @@ MBTiles.prototype.startWriting = function(callback) { if (typeof callback !== 'function') throw new Error('Callback needed'); if (!this.open) return callback(new Error('MBTiles not yet loaded')); - // Sets the synchronous flag to OFF for (much) faster inserts. - // See http://www.sqlite3.org/pragma.html#pragma_synchronous - var mbtiles = this; - if (!this._isWritable) { - this._isWritable = 1; - this._clearCaches(); - this._db.run('PRAGMA synchronous=OFF', callback); - } else { - this._isWritable++; - return callback(null); - } + Step(function() { + mbtiles._setup(this); + }, function(err) { + if (err) throw err; + // Sets the synchronous flag to OFF for (much) faster inserts. + // See http://www.sqlite3.org/pragma.html#pragma_synchronous + if (!mbtiles._isWritable) { + mbtiles._isWritable = 1; + mbtiles._clearCaches(); + mbtiles._db.run('PRAGMA synchronous=OFF', this); + } else { + mbtiles._isWritable++; + this(); + } + }, function(err) { + return callback(err); + }); }; MBTiles.prototype._clearCaches = function() { diff --git a/package.json b/package.json index 723f10c..35b3534 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mbtiles", - "version": "0.1.17", + "version": "0.1.18", "description": "Utilities and tilelive integration for the MBTiles format.", "url": "http://github.com/mapbox/node-mbtiles", "author": { diff --git a/test/info.test.js b/test/info.test.js index 1dd19a3..c6467b2 100644 --- a/test/info.test.js +++ b/test/info.test.js @@ -60,7 +60,7 @@ exports['get/put metadata from empty file'] = function(beforeExit, assert) { assert.deepEqual({ basename: "empty.mbtiles", - filesize: 16384, + filesize: 0, id: "empty", scheme: "tms" }, data); @@ -88,7 +88,7 @@ exports['get/put metadata from empty file'] = function(beforeExit, assert) { assert.deepEqual({ basename: "empty.mbtiles", - filesize: 16384, + filesize: 0, id: "empty", scheme: "tms", version: "1.0.0" diff --git a/test/read.test.js b/test/read.test.js index 22aa685..b0f4ac5 100644 --- a/test/read.test.js +++ b/test/read.test.js @@ -38,9 +38,12 @@ exports['get tiles'] = function(beforeExit, assert) { if (coords) { // Flip Y coordinate because file names are TMS, but .getTile() expects XYZ. coords[2] = Math.pow(2, coords[3]) - 1 - coords[2]; - mbtiles.getTile(coords[3] | 0, coords[1] | 0, coords[2] | 0, function(err, tile) { + mbtiles.getTile(coords[3] | 0, coords[1] | 0, coords[2] | 0, function(err, tile, headers) { if (err) throw err; assert.deepEqual(tile, fs.readFileSync(__dirname + '/fixtures/images/' + file)); + assert.equal(headers['Content-Type'], 'image/png'); + assert.ok(!isNaN(Date.parse(headers['Last-Modified']))); + assert.ok(/\d+-\d+/.test(headers['ETag'])); status.success++; }); } @@ -75,9 +78,12 @@ exports['get grids'] = function(beforeExit, assert) { if (coords) { // Flip Y coordinate because file names are TMS, but .getTile() expects XYZ. coords[2] = Math.pow(2, coords[3]) - 1 - coords[2]; - mbtiles.getGrid(coords[3] | 0, coords[1] | 0, coords[2] | 0, function(err, grid) { + mbtiles.getGrid(coords[3] | 0, coords[1] | 0, coords[2] | 0, function(err, grid, headers) { if (err) throw err; assert.deepEqual(JSON.stringify(grid), fs.readFileSync(__dirname + '/fixtures/grids/' + file, 'utf8')); + assert.equal(headers['Content-Type'], 'text/javascript'); + assert.ok(!isNaN(Date.parse(headers['Last-Modified']))); + assert.ok(/\d+-\d+/.test(headers['ETag'])); status.success++; }); } diff --git a/test/write.test.js b/test/write.test.js index c3b0e3d..bfc2666 100644 --- a/test/write.test.js +++ b/test/write.test.js @@ -30,20 +30,17 @@ exports['test mbtiles file creation'] = function(beforeExit, assert) { // Flip Y coordinate because file names are TMS, but .putTile() expects XYZ. coords[2] = Math.pow(2, coords[3]) - 1 - coords[2]; - fs.readFile(__dirname + '/fixtures/images/' + file, function(err, tile) { + var tile = fs.readFileSync(__dirname + '/fixtures/images/' + file); + mbtiles.putTile(coords[3] | 0, coords[1] | 0, coords[2] | 0, tile, function(err) { if (err) throw err; - - mbtiles.putTile(coords[3] | 0, coords[1] | 0, coords[2] | 0, tile, function(err) { - if (err) throw err; - completed.written++; - if (completed.written === 285) { - mbtiles.stopWriting(function(err) { - completed.stopped = true; - if (err) throw err; - verifyWritten(); - }); - } - }); + completed.written++; + if (completed.written === 285) { + mbtiles.stopWriting(function(err) { + completed.stopped = true; + if (err) throw err; + verifyWritten(); + }); + } }); } diff --git a/test/write_grids.test.js b/test/write_grids.test.js index d22c7a3..a7a453e 100644 --- a/test/write_grids.test.js +++ b/test/write_grids.test.js @@ -29,20 +29,17 @@ exports['test mbtiles file creation'] = function(beforeExit, assert) { // Flip Y coordinate because file names are TMS, but .putGrid() expects XYZ. coords[2] = Math.pow(2, coords[3]) - 1 - coords[2]; - fs.readFile(__dirname + '/fixtures/grids/' + file, 'utf8', function(err, grid) { + var grid = fs.readFileSync(__dirname + '/fixtures/grids/' + file, 'utf8'); + mbtiles.putGrid(coords[3] | 0, coords[1] | 0, coords[2] | 0, JSON.parse(grid), function(err) { if (err) throw err; - - mbtiles.putGrid(coords[3] | 0, coords[1] | 0, coords[2] | 0, JSON.parse(grid), function(err) { - if (err) throw err; - completed.written++; - if (completed.written === 241) { - mbtiles.stopWriting(function(err) { - completed.stopped = true; - if (err) throw err; - verifyWritten(); - }); - } - }); + completed.written++; + if (completed.written === 241) { + mbtiles.stopWriting(function(err) { + completed.stopped = true; + if (err) throw err; + verifyWritten(); + }); + } }); }