diff --git a/lib/mbtiles.js b/lib/mbtiles.js index d07db48..1bfa462 100644 --- a/lib/mbtiles.js +++ b/lib/mbtiles.js @@ -2,7 +2,7 @@ var _ = require('underscore'), fs = require('fs'), Step = require('step'), crypto = require('crypto'), - _zlib = require('_zlib'), + zlib = require('zlib'), path = require('path'), url = require('url'), qs = require('querystring'), @@ -215,8 +215,9 @@ MBTiles.prototype.getGrid = function(z, x, y, callback) { } if (err) return callback(err); + /* try { - var grid = _zlib.inflate( + var grid = zlib.inflate( !Buffer.isBuffer(row.grid) ? new Buffer(row.grid, 'binary') : row.grid @@ -231,6 +232,24 @@ MBTiles.prototype.getGrid = function(z, x, y, callback) { } callback(null, result); + */ + + 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); + }); + } ); }; @@ -565,13 +584,23 @@ MBTiles.prototype.putGrid = function(z, x, y, data, callback) { var 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')); - // 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._gridCache[id] = zlib.deflate(new Buffer(json, 'utf8')); + */ + + Step(function() { + zlib.deflate(new Buffer(json, 'utf8'),this); + }, function(err,buffer) { + if (err) return callback(err); + this._gridCache[id] = buffer; + // 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 map table.