Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

osm-p2p-server

Package Overview
Dependencies
Maintainers
2
Versions
64
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

osm-p2p-server - npm Package Compare versions

Comparing version 1.2.0 to 1.3.0

example/full.js

17

example/server.js

@@ -1,17 +0,4 @@

var level = require('level')
var hyperlog = require('hyperlog')
var fdstore = require('fd-chunk-store')
var db = {
log: level('/tmp/osm-p2p/log'),
index: level('/tmp/osm-p2p/index')
}
var osmdb = require('osm-p2p')
var osm = osmdb('/tmp/osm-p2p')
var osmdb = require('osm-p2p-db')
var osm = osmdb({
log: hyperlog(db.log, { valueEncoding: 'json' }),
db: db.index,
store: fdstore(4096, '/tmp/osm-p2p/tree'),
size: 4096
})
var osmrouter = require('../')

@@ -18,0 +5,0 @@ var router = osmrouter(osm)

@@ -1,25 +0,22 @@

var routes = require('routes')
var inherits = require('inherits')
var h = require('./lib/h.js')
var through = require('through2')
var body = require('body/any')
var randombytes = require('randombytes')
var toxml = require('osm-p2p-xml')
var Router = require('routes')
var routes = require('./lib/routes.js')
module.exports = Router
module.exports = Server
function Router (osmdb) {
if (!(this instanceof Router)) return new Router(osmdb)
this.osmdb = osmdb
this.routers = {
get: routes(),
post: routes()
function Server (osmdb) {
if (!(this instanceof Server)) return new Server(osmdb)
var self = this
self.osmdb = osmdb
self.routers = {
get: Router(),
post: Router(),
put: Router(),
delete: Router()
}
this.routers.get.addRoute('/api/capabilities', this._capRoute.bind(this))
this.routers.get.addRoute('/api/0.6/capabilities', this._capRoute.bind(this))
this.routers.get.addRoute('/api/0.6/map?*', this._mapRoute.bind(this))
this.routers.post.addRoute('/api/changeset', this._chRoute.bind(this))
routes.forEach(function (r) {
self.routers[r[0]].addRoute(r[1], r[2])
})
}
Router.prototype.match = function (method, url) {
Server.prototype.match = function (method, url) {
method = method.toLowerCase()

@@ -30,6 +27,8 @@ if (!this.routers.hasOwnProperty(method)) return null

Router.prototype.handle = function (req, res) {
var m = this.match(req.method, req.url)
Server.prototype.handle = function (req, res) {
var method = (req.headers.X_HTTP_METHOD_OVERRIDE || req.method)
.toLowerCase()
var m = this.match(method, req.url)
if (m) {
m.fn(m, req, res)
m.fn(req, res, this.osmdb, m)
return m

@@ -39,104 +38,1 @@ }

}
Router.prototype._capRoute = function (m, req, res) {
res.end(h('?xml', { version: '1.0', encoding: 'UTF-8' }, [
h('osm', { version: 0.6, generator: 'osm-p2p' }, [
h('api', [
h('version', { minimum: 0.6, maximum: 0.6 }),
h('area', { maximum: 0.25 }), // in square degrees
h('waynodes', { maximum: 2000 }),
h('tracepoints', { per_page: 5000 }),
h('timeout', { seconds: 300 }),
h('status', { database: 'online', api: 'online', gpx: 'online' }),
])
])
]))
}
Router.prototype._mapRoute = function (m, req, res) {
var bbox = req.url.replace(/[^?]*\?bbox=/,'').split(',').map(Number)
var q = [[bbox[1],bbox[3]],[bbox[0],bbox[2]]] // left,bottom,right,top
var r = this.osmdb.queryStream(q)
r.once('error', function (err) { res.end(err + '\n') })
res.setHeader('content-type', 'text/xml; charset=utf-8')
res.setHeader('content-disposition', 'attachment; filename="map.osm"')
res.setHeader('content-encoding', 'identity')
res.setHeader('cache-control', 'no-cache')
r.pipe(toxml(q)).pipe(res)
}
Router.prototype._chRoute = function (m, req, res) {
var self = this
var pending = 1, errors = [], keys = []
body(req, res, function (err, params) {
if (err) return error(400, res, err)
var docs = {}
params.changes.created.forEach(function (ch) {
docs[ch.id] = randombytes(8).toString('hex')
ch.id = docs[ch.id]
})
params.changes.created.forEach(function (ch) {
var id = fix(docs, ch)
pending++
self.osmdb.put(id, ch, function (err) {
if (err) errors.push(err)
else keys.push(id)
if (--pending === 0) done()
})
})
params.changes.modified.forEach(function (ch) {
var id = fix(docs, ch)
pending++
var opts = { links: ch.version ? [ch.version] : [] }
self.osmdb.put(id, ch, opts, function (err) {
if (err) errors.push(err)
else keys.push(id)
if (--pending === 0) done()
})
})
params.changes.deleted.forEach(function (ch) {
var id = ch.id.replace(/^[nw]/, '')
pending++
var opts = { links: ch.version ? [ch.version] : [] }
self.osmdb.del(id, opts, function (err) {
if (err) errors.push(err)
else keys.push(id)
if (--pending === 0) done()
})
})
if (--pending === 0) done()
})
function done () {
if (errors.length) {
return error(500, res, errors.map(String).join('\n'))
} else {
res.setHeader('content-type', 'application/json')
res.end(JSON.stringify(keys) + '\n')
}
}
}
function fix (docs, ch) {
if (ch.loc) {
ch.lat = ch.loc[1]
ch.lon = ch.loc[0]
ch.type = 'node'
delete ch.loc
} else if (ch.nodes) {
ch.type = 'way'
ch.refs = ch.nodes
.map(function (id) { return docs.hasOwnProperty(id) ? docs[id] : id })
.map(function (id) { return id.replace(/^[nw]/,'') })
delete ch.nodes
}
ch.timestamp = new Date().toISOString()
var id = ch.id
if (docs.hasOwnProperty(id)) id = docs[id]
delete ch.id
return id.replace(/^[nw]/, '')
}
function error (code, res, err) {
res.statusCode = code
res.end(err + '\n')
}
{
"name": "osm-p2p-server",
"version": "1.2.0",
"version": "1.3.0",
"description": "serve osm http endpoints over a p2p db",

@@ -14,16 +14,18 @@ "main": "index.js",

"license": "BSD",
"devDependencies": {
"fd-chunk-store": "^1.1.0",
"hyperlog": "^4.7.0",
"level": "^1.4.0",
"osm-p2p-db": "^2.1.0"
},
"dependencies": {
"body": "^5.1.0",
"concat-stream": "^1.5.1",
"has": "^1.0.1",
"inherits": "^2.0.1",
"osm-p2p-xml": "^1.0.0",
"randombytes": "^2.0.1",
"randombytes": "^2.0.2",
"routes": "^2.1.0",
"through2": "^2.0.0"
"through2": "^2.0.0",
"xml-parser": "^1.2.1"
},
"devDependencies": {
"hyperquest": "^1.2.0",
"osm-p2p": "^1.1.0",
"tape": "^4.4.0"
},
"directories": {

@@ -33,3 +35,3 @@ "example": "example"

"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "tape test/*.js"
},

@@ -36,0 +38,0 @@ "repository": {

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc