-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(libpostal): run libpostal in child process with IPC
- Loading branch information
1 parent
7a1de5f
commit 3eca6ea
Showing
20 changed files
with
135 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const util = require('util'); | ||
const child = require('child_process'); | ||
const child_script = require('path').join(__dirname, './libpostal_child.js'); | ||
const bus = require('ipc-messages-manager').parent; | ||
const stdio = ['inherit', 'inherit', 'inherit', 'ipc']; | ||
|
||
// spawn child process | ||
const proc = child.spawn('node', [child_script], { stdio }); | ||
|
||
// log subprocess errors | ||
proc.on('error', (e) => console.error(e)); | ||
|
||
// specify 'expand_address' IPC API | ||
function expand_address_ipc (address, options, cb) { | ||
bus.send(proc, 'expand.expand_address', { address, options }, (res) => cb(null, res)); | ||
} | ||
|
||
// specify 'expand_address' async API | ||
const expand_address_async = async function(address) { | ||
const promise = util.promisify(expand_address_ipc); | ||
return promise(address, {}); | ||
}; | ||
|
||
module.exports = { | ||
close: () => { | ||
proc.disconnect(); | ||
proc.kill(); | ||
}, | ||
expand: { | ||
expand_address: expand_address_async | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const bus = require('ipc-messages-manager').child; | ||
const isMock = process.env.hasOwnProperty('MOCK_LIBPOSTAL'); | ||
const moduleName = isMock ? '../test/lib/mock_libpostal' : 'node-postal'; | ||
let postal; | ||
|
||
function blockUntilModuleLoaded(){ | ||
if (!postal) { | ||
postal = require(moduleName); | ||
console.log('libpostal child process ready'); | ||
} | ||
} | ||
|
||
bus.actions.on('expand.expand_address', (args, cb) => { | ||
blockUntilModuleLoaded(); | ||
const expanded = postal.expand.expand_address(args.address, args.options); | ||
cb(expanded); | ||
}); | ||
|
||
console.log('libpostal child process connected'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.