diff --git a/lib/mbtiles.js b/lib/mbtiles.js index 2ace481..f7460b5 100644 --- a/lib/mbtiles.js +++ b/lib/mbtiles.js @@ -215,26 +215,25 @@ MBTiles.prototype.getGrid = function(z, x, y, callback) { } if (err) return callback(err); - try { - var grid = zlib.inflate( - !Buffer.isBuffer(row.grid) + zlib.inflate(!Buffer.isBuffer(row.grid) ? new Buffer(row.grid, 'binary') : row.grid - ).toString(); - var data = rows.reduce(function(memo, r) { - memo[r.key_name] = JSON.parse(r.key_json); - return memo; - }, {}); - var result = _(JSON.parse(grid)).extend({ data: data }); - } catch (err) { - return callback(new Error('Grid is invalid')); - } - 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); + , 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); + }); } ); }; @@ -582,13 +581,19 @@ MBTiles.prototype.putGrid = function(z, x, y, data, callback) { var id = data.id || crypto.createHash('md5').update(json).digest('hex'); if (!this._gridCache[id]) { // This corresponds to the grid_utfgrid table. - this._gridCache[id] = zlib.deflate(new Buffer(json, 'utf8')); + var that = this; + Step(function() { + zlib.deflate(new Buffer(json, 'utf8'),this); + }, function(err,buffer) { + if (err) return callback(err); + that._gridCache[id] = buffer; + // This corresponds to the grid_key table. + that._keyCache[id] = Object.keys(data.data || {}); - // This corresponds to the grid_key table. - this._keyCache[id] = Object.keys(data.data || {}); - - // This corresponds to the keymap table. - _(this._dataCache).extend(data.data || {}); + // This corresponds to the keymap table. + _(that._dataCache).extend(data.data || {}); + } + ); } // This corresponds to the map table. diff --git a/package.json b/package.json index 3cde336..ab77dae 100644 --- a/package.json +++ b/package.json @@ -30,14 +30,13 @@ ], "dependencies": { "optimist": "0.2.x", - "sqlite3": "~2.0.17", + "sqlite3": "~2.1.1", "step": "0.0.x", "sphericalmercator": "1.0.x", - "underscore": "1.1.x", - "zlib": "1.0.x" + "underscore": "1.1.x" }, "engines": { - "node": ">= 0.4.0" + "node": ">= 0.6.0" }, "scripts": { "test": "expresso"