osm-p2p-server
Advanced tools
Comparing version 2.0.0-beta2 to 2.0.0-beta3
var contentType = require('content-type') | ||
var randombytes = require('randombytes') | ||
var convert = require('base-convertor') | ||
var JSONStream = require('JSONStream') | ||
var version = require('../package.json').version | ||
/** | ||
@@ -41,2 +44,18 @@ * Converts objects from osm-p2p to objects compatible with the OSM JSON format | ||
var TEMPLATE = { | ||
version: 0.6, | ||
generator: 'osm-p2p v' + version, | ||
elements: [] | ||
} | ||
var SEP = ',\n ' | ||
function OsmJSONStream (opts) { | ||
var template = Object.assign({}, TEMPLATE, opts) | ||
var openClose = JSON.stringify(template, null, 4).split('"elements": [') | ||
var open = openClose[0] + '"elements": [\n ' | ||
var close = '\n ' + openClose[1] | ||
return JSONStream.stringify(open, SEP, close) | ||
} | ||
/** | ||
@@ -78,3 +97,4 @@ * Sort function to sort forks by most recent first, or by version id | ||
isValidContentType: isValidContentType, | ||
cmpFork: cmpFork | ||
cmpFork: cmpFork, | ||
OsmJSONStream: OsmJSONStream | ||
} |
{ | ||
"name": "osm-p2p-server", | ||
"version": "2.0.0-beta2", | ||
"version": "2.0.0-beta3", | ||
"description": "Peer-to-peer OpenStreetMap API v0.6 Server", | ||
@@ -21,8 +21,12 @@ "main": "index.js", | ||
"author": "substack", | ||
"contributors": [{ | ||
"name": "Gregor MacLennan", | ||
"email": "gmaclennan@digital-democracy.org" | ||
}], | ||
"contributors": [ | ||
{ | ||
"name": "Gregor MacLennan", | ||
"email": "gmaclennan@digital-democracy.org" | ||
} | ||
], | ||
"license": "BSD-2-Clause", | ||
"dependencies": { | ||
"JSONStream": "^1.1.4", | ||
"accepts": "^1.3.3", | ||
"base-convertor": "^1.1.0", | ||
@@ -29,0 +33,0 @@ "collect-stream": "^1.1.1", |
@@ -32,5 +32,5 @@ /** | ||
res.setHeader('content-type', 'text/plain') | ||
res.end(id + '\n') | ||
res.end(id) | ||
}) | ||
}) | ||
} |
var qs = require('query-string') | ||
var toOsm = require('obj2osm') | ||
var fromArray = require('from2-array') | ||
var accepts = require('accepts') | ||
var cmpFork = require('../lib/util').cmpFork | ||
var OsmJSONStream = require('../lib/util').OsmJSONStream | ||
module.exports = function (req, res, api, params, next) { | ||
// TODO: Validate bbox | ||
var accept = accepts(req) | ||
var query = qs.parse(qs.extract(req.url)) | ||
@@ -32,4 +35,15 @@ var bbox = query.bbox.split(',').map(Number) | ||
var r = fromArray.obj(byType).on('error', next) | ||
var t = toOsm(toOsmOptions).on('error', next) | ||
res.setHeader('content-type', 'text/xml; charset=utf-8') | ||
var t | ||
switch (accept.types(['xml', 'json'])) { | ||
case 'json': | ||
t = OsmJSONStream(toOsmOptions) | ||
res.setHeader('content-type', 'application/json; charset=utf-8') | ||
break | ||
case 'xml': | ||
default: | ||
t = toOsm(toOsmOptions) | ||
res.setHeader('content-type', 'text/xml; charset=utf-8') | ||
break | ||
} | ||
t.on('error', next) | ||
r.pipe(t).pipe(res) | ||
@@ -36,0 +50,0 @@ }) |
@@ -5,3 +5,3 @@ var test = require('tape') | ||
var concat = require('concat-stream') | ||
var fs = require('fs') | ||
var createServer = require('./test_server.js') | ||
@@ -100,3 +100,3 @@ | ||
test('bbox', function (t) { | ||
t.plan(7 + SIZE * 3) | ||
t.plan(8 + SIZE * 3) | ||
var href = base + 'map?bbox=-123,63,-120,66' | ||
@@ -112,2 +112,3 @@ var hq = hyperquest(href) | ||
t.equal(xml.root.children[0].name, 'bounds') | ||
t.deepEqual(xml.root.children[0].attributes, { maxlat: '66', maxlon: '-120', minlat: '63', minlon: '-123' }, 'bounds matches request') | ||
t.ok(orderedTypes(xml.root.children.map(function (c) { | ||
@@ -134,2 +135,33 @@ return c.name | ||
test('bbox json', function (t) { | ||
t.plan(7 + SIZE * 3) | ||
var href = base + 'map?bbox=-123,63,-120,66' | ||
var hq = hyperquest(href, {headers: { 'Accept': 'application/json' }}) | ||
hq.once('response', function (res) { | ||
t.equal(res.statusCode, 200) | ||
t.equal(res.headers['content-type'].split(/\s*;\s*/)[0], 'application/json') | ||
}) | ||
hq.pipe(concat({ encoding: 'string' }, function (body) { | ||
var json = JSON.parse(body) | ||
t.equal(json.version, 0.6) | ||
t.deepEqual(json.bounds, { maxlat: 66, maxlon: -120, minlat: 63, minlon: -123 }, 'bounds matches request') | ||
t.ok(orderedTypes(json.elements.map(function (c) { | ||
return c.type | ||
})), 'ordered types') | ||
for (var i = 0; i < json.elements.length; i++) { | ||
var c = json.elements[i] | ||
var node = uploaded[c.id] | ||
if (c.type === 'node') { | ||
t.equal(c.changeset, node.changeset) | ||
t.equal(c.lat, node.lat) | ||
t.equal(c.lon, node.lon) | ||
} else if (c.type === 'way') { | ||
t.equal(c.nodes.length, SIZE, 'way') | ||
t.deepEqual(c.nodes, node.refs, 'way refs') | ||
} | ||
} | ||
})) | ||
}) | ||
test('bbox.js: teardown server', function (t) { | ||
@@ -136,0 +168,0 @@ server.cleanup() |
167807
4768
22
3
+ AddedJSONStream@^1.1.4
+ Addedaccepts@^1.3.3
+ AddedJSONStream@1.3.5(transitive)
+ Addedaccepts@1.3.8(transitive)
+ Addedjsonparse@1.3.1(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addednegotiator@0.6.3(transitive)
+ Addedthrough@2.3.8(transitive)