Comparing version 1.0.2 to 1.0.3
37
index.js
const fs = require('fs'); | ||
const readline = require('readline'); | ||
const fetch = require('node-fetch'); | ||
const zip = require('node-stream-zip'); | ||
const Zip = require('node-stream-zip'); | ||
const xml2js = require('xml2js'); | ||
function urlfn(url) { | ||
return './' + url.replace(/.*\//, ''); | ||
} | ||
var self = module.exports = { | ||
FORCE: true, | ||
url: 'https://www.treasury.gov/ofac/downloads/sdn_xml.zip', | ||
init: async (force = false) => { | ||
opts: { | ||
force: false, | ||
path: '/tmp', | ||
xml: 'sdn.xml' | ||
}, | ||
init: async (opts = self.opts) => { | ||
self.opts = Object.assign(self.opts, opts); | ||
var {force} = self.opts; | ||
// if database already fetched, don't fetch again | ||
var fn = urlfn(self.url); | ||
var fn = self.fn(self.url); | ||
if (!fs.existsSync(fn) || force) | ||
await self.fetch(); | ||
if (!fs.existsSync(self.db) || force) | ||
self.db = await self.zipExtract(fn); | ||
if (!fs.existsSync(self.fn(self.opts.xml)) || force) | ||
await self.zipExtract(fn, self.opts.xml, self.opts.path); | ||
return self; | ||
}, | ||
fn: (url) => { | ||
return self.opts.path + '/' + url.replace(/.*\//, ''); | ||
}, | ||
fetch: (url = self.url) => { | ||
return fetch(url).then(res => { | ||
var fn = urlfn(url); | ||
var fn = self.fn(url); | ||
const dest = fs.createWriteStream(fn); | ||
@@ -34,11 +40,10 @@ res.body.pipe(dest); | ||
}, | ||
zipExtract: (fn, path = './') => { | ||
var z = new zip({file: fn}); | ||
zipExtract: (zip, fn, dest = './') => { | ||
var z = new Zip({file: zip}); | ||
return new Promise((resolve, reject) => { | ||
var xfn = 'sdn.xml'; | ||
z.on('error', reject); | ||
z.on('ready', () => { | ||
z.extract(xfn, path, err => { | ||
z.extract(fn, dest, err => { | ||
if (err) reject(err); | ||
resolve(xfn); | ||
resolve(dest + fn); | ||
z.close(); | ||
@@ -45,0 +50,0 @@ }) |
{ | ||
"name": "ofac", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "A module to facilitate OFAC searches", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -80,12 +80,5 @@ # OFAC | ||
The method returns the file name used, wrapped in a promise | ||
### zipInfo(filename, [property]) | ||
* filename - specifies the name of the file to read | ||
* property - optionally passed, retrieves only a property of the meta-data | ||
This method returns meta-data on a Zip archive as produced by the `unzip` module. When | ||
the second parameter is specified, it names the property of the object to return instead | ||
The method returns a promise | ||
### unzip(filename, [path]) | ||
* filename - the name of the file to unpack | ||
### zipExtract(archive, filename, [path]) | ||
* archive - the name of the archive from which to extract | ||
* filename - the name of the file to extract | ||
* path - the location to which the archive should be unpacked | ||
@@ -92,0 +85,0 @@ |
@@ -41,5 +41,5 @@ var fs = require('fs'); | ||
var actual = await ofac.zipExtract(zip); | ||
assert.equal(actual, fn, 'Extracted a different file'); | ||
assert.ok(fs.existsSync(fn), 'Extract file does not exist'); | ||
var actual = await ofac.zipExtract(zip, fn, '/tmp/'); | ||
assert.equal(actual, '/tmp/' + fn, 'Extracted a different file'); | ||
assert.ok(fs.existsSync('/tmp/' + fn), 'Extract file does not exist'); | ||
}); | ||
@@ -46,0 +46,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
198
14206
145