From 58fa51dc1e923a8ea8fa71bbe9ac8673b68d0e3a Mon Sep 17 00:00:00 2001 From: Julian Simioni Date: Wed, 21 Aug 2019 07:58:07 -0400 Subject: [PATCH 1/4] Update .jshintrc It had fallen behind othe projects. Copied from pelias/api. --- .jshintrc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.jshintrc b/.jshintrc index f9df9b9..e5c5749 100644 --- a/.jshintrc +++ b/.jshintrc @@ -2,6 +2,7 @@ "node": true, "curly": true, "eqeqeq": true, + "esversion": 6, "freeze": true, "immed": true, "indent": 2, @@ -14,8 +15,8 @@ "plusplus": false, "quotmark": "single", "undef": true, - "unused": true, + "unused": false, "maxparams": 4, "maxdepth": 4, - "maxlen": 120 + "maxlen": 140 } From a7436a5d8929f714a4163aa65d588e9983b77976 Mon Sep 17 00:00:00 2001 From: Julian Simioni Date: Wed, 21 Aug 2019 07:58:47 -0400 Subject: [PATCH 2/4] chore(tests): Extract and test generateParams method This extracts the logic for converting the config object passed to the `pbf2json` NPM module to command line parameters used to call the `pbf2json` go executable. By extracting that logic its easier to test and change in the (very near) future. --- index.js | 12 ++++------- lib/generateParams.js | 15 +++++++++++++ test/lib/generateParams.js | 43 ++++++++++++++++++++++++++++++++++++++ test/run.js | 5 +++-- 4 files changed, 65 insertions(+), 10 deletions(-) create mode 100644 lib/generateParams.js create mode 100644 test/lib/generateParams.js diff --git a/index.js b/index.js index 3a98d74..f44b413 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,8 @@ var util = require('util'), split = require('split'), through = require('through2'), child = require('child_process'), - exec = path.join(__dirname, 'build', util.format( 'pbf2json.%s-%s', os.platform(), os.arch() ) ); + exec = path.join(__dirname, 'build', util.format( 'pbf2json.%s-%s', os.platform(), os.arch() ) ), + generateParams = require('./lib/generateParams'); // custom log levels can be detected for lines with the format: // [level] message @@ -29,14 +30,9 @@ function errorHandler( name, level ){ function createReadStream( config ){ - var flags = []; - flags.push( util.format( '-tags=%s', config.tags ) ); - if( config.hasOwnProperty( 'leveldb' ) ){ - flags.push( util.format( '-leveldb=%s', config.leveldb ) ); - } - flags.push( config.file ); + const params = generateParams(config); - var proc = child.spawn( exec, flags ); + var proc = child.spawn( exec, params ); // propagate signals from parent to child process.on('SIGINT', function(){ proc.kill(); }); diff --git a/lib/generateParams.js b/lib/generateParams.js new file mode 100644 index 0000000..d785684 --- /dev/null +++ b/lib/generateParams.js @@ -0,0 +1,15 @@ +const util = require('util'); + +function generateParams(config) { + const flags = []; + + flags.push( util.format( '-tags=%s', config.tags ) ); + if( config.hasOwnProperty( 'leveldb' ) ){ + flags.push( util.format( '-leveldb=%s', config.leveldb ) ); + } + flags.push( config.file ); + + return flags; +} + +module.exports = generateParams; diff --git a/test/lib/generateParams.js b/test/lib/generateParams.js new file mode 100644 index 0000000..9041bec --- /dev/null +++ b/test/lib/generateParams.js @@ -0,0 +1,43 @@ +const generateParams = require('../../lib/generateParams'); + +module.exports.tests = {}; + +module.exports.tests.params = function(test) { + test('PBF file', function(t) { + const config = { + file: '/some/path/to/osm.pbf' + }; + + const params = generateParams(config); + + t.equal(params[params.length - 1], '/some/path/to/osm.pbf', 'final parameter is path to PBF file'); + t.end(); + }); + test('PBF file', function(t) { + const config = { + tags: [ + 'tag:one', + 'tag:two', + 'combination~tags' + ] + }; + + const params = generateParams(config); + + const expected = '-tags=tag:one,tag:two,combination~tags'; + + t.equal(params[0], expected, 'tag array is serialized into parameter'); + t.end(); + }); +}; + +module.exports.all = function (tape, common) { + + function test(name, testFunction) { + return tape('generateParams: ' + name, testFunction); + } + + for( var testCase in module.exports.tests ){ + module.exports.tests[testCase](test, common); + } +}; diff --git a/test/run.js b/test/run.js index 6003a39..bbfc0e0 100644 --- a/test/run.js +++ b/test/run.js @@ -3,9 +3,10 @@ var tape = require('tape'); var common = {}; var tests = [ - require('./index.js') + require('./index.js'), + require('./lib/generateParams.js') ]; tests.map(function(t) { t.all(tape, common); -}); \ No newline at end of file +}); From 8948818f21bdebd569c166258b73a772f626c848 Mon Sep 17 00:00:00 2001 From: Julian Simioni Date: Wed, 21 Aug 2019 08:03:06 -0400 Subject: [PATCH 3/4] fix(flags): Explicitly convert array to parameter string The behavior of passing an arrayt to `util.format` has changed in Node.js 12. Arrays now include square brackets `[]` around them in the string representation, which was breaking the parameters passed to the `pbf2json` go executable. This change uses `Array.prototype.join` to ensure the array is converted to a string exactly how we want. Connects https://github.com/pelias/pelias/issues/800 --- lib/generateParams.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/generateParams.js b/lib/generateParams.js index d785684..611f2dd 100644 --- a/lib/generateParams.js +++ b/lib/generateParams.js @@ -3,7 +3,10 @@ const util = require('util'); function generateParams(config) { const flags = []; - flags.push( util.format( '-tags=%s', config.tags ) ); + if (config.tags) { + const tags = config.tags.join(','); + flags.push( util.format( '-tags=%s', tags ) ); + } if( config.hasOwnProperty( 'leveldb' ) ){ flags.push( util.format( '-leveldb=%s', config.leveldb ) ); } From 2a1be897e9a117ffd164f5d205cacf331b891a7f Mon Sep 17 00:00:00 2001 From: Julian Simioni Date: Wed, 21 Aug 2019 08:12:33 -0400 Subject: [PATCH 4/4] chore: Remove use of util.format --- lib/generateParams.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/generateParams.js b/lib/generateParams.js index 611f2dd..558bfc8 100644 --- a/lib/generateParams.js +++ b/lib/generateParams.js @@ -1,15 +1,14 @@ -const util = require('util'); - function generateParams(config) { const flags = []; if (config.tags) { const tags = config.tags.join(','); - flags.push( util.format( '-tags=%s', tags ) ); + flags.push( `-tags=${tags}` ); } if( config.hasOwnProperty( 'leveldb' ) ){ - flags.push( util.format( '-leveldb=%s', config.leveldb ) ); + flags.push( `-leveldb=${config.leveldb}` ); } + flags.push( config.file ); return flags;