Comparing version 5.1.2 to 5.2.0
var concurrency = Math.ceil(require('os').cpus().length * 16); | ||
var util = require('util'); | ||
@@ -6,3 +7,4 @@ module.exports = {}; | ||
module.exports.Info = Info; | ||
module.exports.Stats = Stats;; | ||
module.exports.DeserializationError = DeserializationError; | ||
module.exports.Stats = Stats; | ||
module.exports.addChildren = addChildren; | ||
@@ -14,8 +16,17 @@ module.exports.sumChildren = sumChildren; | ||
module.exports.setConcurrency = setConcurrency; | ||
module.exports.serialize = serialize; | ||
module.exports.deserialize = deserialize; | ||
module.exports.serialHeader = 'JSONBREAKFASTTIME'; | ||
function DeserializationError(msg) { | ||
this.message = msg; | ||
this.name = 'DeserializationError'; | ||
} | ||
util.inherits(DeserializationError, Error); | ||
function Tile(z,x,y,buffer) { | ||
this.z = z; | ||
this.x = x; | ||
this.y = y; | ||
this.buffer = buffer; | ||
this.z = isNaN(z) ? undefined : Number(z); | ||
this.x = isNaN(x) ? undefined : Number(x); | ||
this.y = isNaN(y) ? undefined : Number(y); | ||
this.buffer = buffer instanceof Buffer ? buffer : undefined; | ||
return this; | ||
@@ -29,2 +40,58 @@ } | ||
function serialize(obj) { | ||
if (obj instanceof Tile) return serializeTile(obj); | ||
if (obj instanceof Info) return serializeInfo(obj); | ||
return ''; | ||
} | ||
function deserialize(data) { | ||
if (data.indexOf('{"z":') === 0) return deserializeTile(data); | ||
if (data.indexOf('{') === 0) return deserializeInfo(data); | ||
throw new DeserializationError('Invalid data'); | ||
} | ||
function serializeTile(tile) { | ||
var obj = { | ||
z: isNaN(tile.z) ? null : Number(tile.z), | ||
x: isNaN(tile.x) ? null : Number(tile.x), | ||
y: isNaN(tile.y) ? null : Number(tile.y), | ||
buffer: tile.buffer instanceof Buffer ? tile.buffer.toString('base64') : null | ||
}; | ||
return JSON.stringify(obj); | ||
} | ||
function serializeInfo(info) { | ||
return JSON.stringify(info); | ||
} | ||
function deserializeTile(data) { | ||
var obj, buf; | ||
try { obj = JSON.parse(data); } | ||
catch(err) { throw new DeserializationError('Could not parse data'); } | ||
if (!obj.hasOwnProperty('z') || | ||
!obj.hasOwnProperty('x') || | ||
!obj.hasOwnProperty('y') || | ||
!obj.hasOwnProperty('buffer')) { | ||
throw new DeserializationError('Invalid data'); | ||
} | ||
if (typeof obj.buffer === 'string' || typeof obj.buffer === 'number') { | ||
buf = new Buffer(obj.buffer, 'base64'); | ||
} else { | ||
throw new DeserializationError('Invalid buffer'); | ||
} | ||
return new Tile(obj.z, obj.x, obj.y, buf); | ||
} | ||
function deserializeInfo(data) { | ||
try { obj = JSON.parse(data); } | ||
catch(err) { throw new DeserializationError('Could not parse data'); } | ||
return new Info(obj); | ||
} | ||
function Stats() { | ||
@@ -138,2 +205,1 @@ this.ops = 0; | ||
} | ||
@@ -322,1 +322,6 @@ var tilelive = exports; | ||
tilelive.streamTypes.list = require('./stream-list'); | ||
var Serialize = require('./stream-serialize'); | ||
var Deserialize = require('./stream-deserialize'); | ||
tilelive.serialize = function() { return new Serialize(); }; | ||
tilelive.deserialize = function() { return new Deserialize(); }; |
{ | ||
"name": "tilelive", | ||
"version": "5.1.2", | ||
"version": "5.2.0", | ||
"main": "./lib/tilelive.js", | ||
@@ -5,0 +5,0 @@ "description": "API for various map tile backends", |
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
53234
16
913
1