Comparing version 2.2.2 to 2.2.3
@@ -8,3 +8,3 @@ /** | ||
var Transform = require('readable-stream').Transform | ||
var sax = require('sax') | ||
var Parser = require('htmlparser2').Parser | ||
var util = require('util') | ||
@@ -21,3 +21,3 @@ | ||
var VALID_ACTIONS = ['create', 'modify', 'delete'] | ||
var VALID_NODES = ['node', 'way', 'relation', 'changeset', 'bounds'] | ||
var VALID_NODES = ['node', 'way', 'relation', 'changeset', 'bounds', 'osmChange'] | ||
@@ -90,10 +90,16 @@ // Attributes that are not in these whitelists are ignored. | ||
this.opts = Object.assign({}, DEFAULTS, opts) | ||
this.parser = sax.parser(this.opts.strict, { | ||
lowercase: !this.opts.strict | ||
var parserHandlers = { | ||
onerror: this.onError.bind(this), | ||
onopentag: this.onOpenTag.bind(this), | ||
onclosetag: this.onCloseTag.bind(this) | ||
} | ||
var parserOpts = { | ||
xmlMode: true | ||
} | ||
this.parser = new Parser(parserHandlers, parserOpts) | ||
this.on('_resetparser', function () { | ||
this.parser = new Parser(parserHandlers, parserOpts) | ||
}) | ||
if (this.opts.bounds) this.opts.types.push('bounds') | ||
this.nodes = [] | ||
this.parser.onerror = this.onError.bind(this) | ||
this.parser.onopentag = this.onOpenTag.bind(this) | ||
this.parser.onclosetag = this.onCloseTag.bind(this) | ||
Transform.call(this, { readableObjectMode: true }) | ||
@@ -115,4 +121,6 @@ | ||
Osm2Obj.prototype.parse = function (str) { | ||
if (!this.parser) this.emit('_resetparser') | ||
this.parser.write(str) | ||
this.parser.end() | ||
this.parser = null | ||
if (this.error) { | ||
@@ -135,16 +143,17 @@ var err = this.error | ||
Osm2Obj.prototype.onOpenTag = function (node) { | ||
Osm2Obj.prototype.onOpenTag = function (name, attributes) { | ||
if (this.error) return | ||
if (!this.root && is(VALID_ROOTS, node.name)) { | ||
this.root = node.name | ||
if (name === 'root') return | ||
if (!this.root && is(VALID_ROOTS, name)) { | ||
this.root = name | ||
} else if ((!this.opts.strict || this.root === 'osmChange') && | ||
!this.currentAction && is(VALID_ACTIONS, node.name)) { | ||
this.currentAction = node.name | ||
this.ifUnused = !!node.attributes['if-unused'] | ||
} else if (!this.currentNode && is(this.opts.types, node.name)) { | ||
this.processNode(node) | ||
} else if (this.currentNode && isValidChild(this.currentNode.type, node.name)) { | ||
this.processChild(node) | ||
} else if (this.opts.strict && !is(VALID_NODES, node.name)) { | ||
this.onError(new Error('invalid tag <' + node.name + '>')) | ||
!this.currentAction && is(VALID_ACTIONS, name)) { | ||
this.currentAction = name | ||
this.ifUnused = !!attributes['if-unused'] | ||
} else if (!this.currentNode && is(this.opts.types, name)) { | ||
this.processNode(name, attributes) | ||
} else if (this.currentNode && isValidChild(this.currentNode.type, name)) { | ||
this.processChild(name, attributes) | ||
} else if (this.opts.strict && !is(VALID_NODES, name)) { | ||
this.onError(new Error('invalid tag <' + name + '>')) | ||
} | ||
@@ -163,9 +172,9 @@ } | ||
Osm2Obj.prototype.processNode = function (node) { | ||
Osm2Obj.prototype.processNode = function (name, attributes) { | ||
this.currentNode = {} | ||
this.currentNode.type = node.name | ||
var attr = node.attributes | ||
this.currentNode.type = name | ||
var attr = attributes | ||
for (var attrName in attr) { | ||
if (!attr.hasOwnProperty(attrName)) continue | ||
if (!isValidAttribute(node.name, attrName)) continue | ||
if (!isValidAttribute(name, attrName)) continue | ||
this.currentNode[attrName] = this.coerce(attrName, attr[attrName]) | ||
@@ -177,12 +186,15 @@ } | ||
Osm2Obj.prototype.processChild = function (node) { | ||
Osm2Obj.prototype.processChild = function (name, attributes) { | ||
var currentNode = this.currentNode | ||
var attr = node.attributes | ||
switch (node.name) { | ||
var attr = attributes | ||
switch (name) { | ||
case 'tag': | ||
if (!attr.k || attr.v == null) { | ||
return this.onError(new Error('<tag> missing k or v attribute')) | ||
if (this.opts.strict) { | ||
return this.onError(new Error('<tag> missing k or v attribute')) | ||
} | ||
} else { | ||
currentNode.tags = currentNode.tags || {} | ||
currentNode.tags[attr.k] = attr.v | ||
} | ||
currentNode.tags = currentNode.tags || {} | ||
currentNode.tags[attr.k] = attr.v | ||
break | ||
@@ -189,0 +201,0 @@ case 'nd': |
{ | ||
"name": "osm2obj", | ||
"version": "2.2.2", | ||
"version": "2.2.3", | ||
"description": "Converts an OSM XML file to OSM objects as a transform stream", | ||
@@ -14,2 +14,3 @@ "main": "lib/osm2obj.js", | ||
"dependencies": { | ||
"htmlparser2": "^3.9.2", | ||
"readable-stream": "^2.1.5", | ||
@@ -16,0 +17,0 @@ "sax": "^1.2.1" |
@@ -9,3 +9,3 @@ # osm2obj | ||
Implements a [Node Transport Stream](http://nodejs.org/api/stream.html#stream_class_stream_transform). Takes a readable stream of [OSM XML](http://wiki.openstreetmap.org/wiki/OSM_XML) and outputs a stream of objects compatible with Overpass [OSM JSON](http://overpass-api.de/output_formats.html#json). Also reads [OsmChange](http://wiki.openstreetmap.org/wiki/OsmChange) XML and outputs the same format but with an additional property `action` which is one of `create`, `modify`, `delete`. Uses [sax-js](https://github.com/isaacs/sax-js) to work in both node and the browser. | ||
Implements a [Node Transform Stream](http://nodejs.org/api/stream.html#stream_class_stream_transform). Takes a readable stream of [OSM XML](http://wiki.openstreetmap.org/wiki/OSM_XML) and outputs a stream of objects compatible with Overpass [OSM JSON](http://overpass-api.de/output_formats.html#json). Also reads [OsmChange](http://wiki.openstreetmap.org/wiki/OsmChange) XML and outputs the same format but with an additional property `action` which is one of `create`, `modify`, `delete`. Uses [sax-js](https://github.com/isaacs/sax-js) to work in both node and the browser. | ||
@@ -12,0 +12,0 @@ ## Table of Contents |
@@ -125,3 +125,3 @@ var test = require('tape') | ||
]) | ||
var parser = new Osm2Obj() | ||
var parser = new Osm2Obj({strict: true}) | ||
var rs1 = fs.readFileSync(path.join(__dirname, 'osmChange.xml')) | ||
@@ -139,1 +139,24 @@ var rs2 = fs.readFileSync(path.join(__dirname, 'osmChange_ifunused.xml')) | ||
}) | ||
test('empty key', function (t) { | ||
var expected = [ | ||
{ | ||
type: 'node', | ||
id: 5104989386, | ||
version: 1, | ||
timestamp: '2017-09-14T10:25:02Z', | ||
uid: 499500, | ||
user: 'hanchao', | ||
changeset: 52032689, | ||
lat: 38.9875205, | ||
lon: 116.4903698, | ||
tags: { name: '文安鲁能华美达广场酒店' } | ||
} | ||
] | ||
var rs = fs.createReadStream(path.join(__dirname, 'empty_key.xml')) | ||
rs.pipe(new Osm2Obj()).pipe(concat(function (data) { | ||
t.deepEqual(data, expected) | ||
t.end() | ||
})) | ||
}) | ||
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
17564256
20
846
3
4
1
+ Addedhtmlparser2@^3.9.2
+ Addeddom-serializer@0.2.2(transitive)
+ Addeddomelementtype@1.3.12.3.0(transitive)
+ Addeddomhandler@2.4.2(transitive)
+ Addeddomutils@1.7.0(transitive)
+ Addedentities@1.1.22.2.0(transitive)
+ Addedhtmlparser2@3.10.1(transitive)
+ Addedreadable-stream@3.6.2(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedstring_decoder@1.3.0(transitive)