chi-datapackage
Advanced tools
Comparing version 1.0.0 to 2.0.0
const Normalizer = require('./src/normalizer'); | ||
const DataPackageService = require('./src/service'); | ||
module.exports = { | ||
Normalizer | ||
}; | ||
const dataPackageService = new DataPackageService(); | ||
module.exports = dataPackageService; | ||
module.exports.Normalizer = Normalizer; |
{ | ||
"name": "chi-datapackage", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "Normalize datapackage and datapackage resources", | ||
"main": "index.js", | ||
"files": [ | ||
"src" | ||
"src", | ||
"dist" | ||
], | ||
"scripts": { | ||
"test": "xo && ava" | ||
"test": "xo && ava", | ||
"build": "babel src -d dist && cp ./src/lib/mime.json ./dist/lib/", | ||
"coverage": "nyc --reporter=lcov --reporter=text ava", | ||
"prepublish": "npm run build" | ||
}, | ||
@@ -16,2 +20,6 @@ "author": "J. Harshbarger", | ||
"ava": "^0.15.2", | ||
"babel-cli": "^6.11.4", | ||
"babel-preset-es2015": "^6.9.0", | ||
"eslint-plugin-node": "^2.0.0", | ||
"nyc": "^7.0.0", | ||
"xo": "^0.16.0" | ||
@@ -26,3 +34,12 @@ }, | ||
], | ||
"plugins": [ | ||
"node" | ||
], | ||
"rules": { | ||
"node/no-unsupported-features": [ | ||
2, | ||
{ | ||
"version": 4 | ||
} | ||
], | ||
"import/no-extraneous-dependencies": 0, | ||
@@ -45,6 +62,21 @@ "space-before-function-paren": [ | ||
] | ||
} | ||
}, | ||
"overrides": [ | ||
{ | ||
"files": "test/*.js", | ||
"rules": { | ||
"node/no-unsupported-features": 0 | ||
} | ||
} | ||
] | ||
}, | ||
"dependencies": { | ||
"babyparse": "^0.4.6", | ||
"crlf-helper": "^0.1.0", | ||
"datapackage-identifier": "^0.4.1", | ||
"debug": "^2.2.0", | ||
"deep-extend": "^0.4.1", | ||
"isomorphic-fetch": "^2.2.1", | ||
"js-yaml": "^3.6.1", | ||
"json5": "^0.5.0", | ||
"mime-lookup": "0.0.2", | ||
@@ -51,0 +83,0 @@ "urijs": "^1.18.1" |
@@ -14,17 +14,42 @@ # chi-datapackage | ||
## Usage | ||
## Basic usage | ||
```js | ||
import {readFileSync} from 'fs'; | ||
import {Normalizer} from 'chi-datapackage'; | ||
import {load} from 'chi-datapackage'; | ||
const normalize = new Normalizer(); | ||
load('//datapackage/path/or/url').then(datapackage => { | ||
/* so something */ | ||
}); | ||
``` | ||
const path = './some/file/path'; | ||
const content = readFileSync(filename, 'utf8'); | ||
## Advanced usage | ||
const datapackage = normalize.datapackage({ | ||
...JSON.parse(content), | ||
path | ||
}); | ||
```js | ||
const MimeLookup = require('mime-lookup'); | ||
const Normalizer = require('chi-datapackagesrc/normalizer'); | ||
const Processor = require('chi-datapackage/src/processor.js'); | ||
const SchemaProcessor = require('chi-datapackage/src/schema'); | ||
const Loader = require('chi-datapackage/src/loader'); | ||
const mimeDb = require('chi-datapackage/src/lib/mime.json'); // or your custom mimeDb | ||
const translators = require('chi-datapackage/src/lib/translators'); // or your custom translators | ||
const types = require('chi-datapackage/src/lib/types'); // or your custom types | ||
const fetch = require('chi-datapackage/src/lib/fetch'); // or your custom fetch promise | ||
const mimeLookup = new MimeLookup(mimeDb); | ||
const normalize = new Normalizer({mimeLookup}); | ||
const schemaProcessor = new SchemaProcessor({types}); | ||
const process = new Processor({translators, schemaProcessor}); | ||
const load = new Loader({fetch}); | ||
load | ||
.datapackage('//datapackage/path/or/url') | ||
.then(datapackage => normalize.datapackage(p)) | ||
.then(datapackage => normalize.resources(p)) | ||
.then(datapackage => load.resources(p)) | ||
.then(datapackage => process.datapackage(p)) | ||
.then(datapackage => { | ||
/* so something */ | ||
}); | ||
``` | ||
@@ -31,0 +56,0 @@ |
@@ -1,14 +0,85 @@ | ||
const readFile = require('fs').readFile; | ||
'use strict'; | ||
function readPackage (path, enc) { | ||
return new Promise((resolve, reject) => { | ||
readFile(path, enc, (err, res) => { | ||
if (err) { | ||
return reject(err); | ||
} | ||
resolve(Object.assign(JSON.parse(res), {path})); | ||
}); | ||
}); | ||
const debug = require('debug')('Loader'); | ||
const parse = require('json5').parse; | ||
const identifier = require('datapackage-identifier'); | ||
const absURLRegEx = /^([^\/]+:\/\/|\/)/; | ||
const resolvePath = (() => { | ||
if (typeof document !== 'undefined') { | ||
// in browser | ||
return function resolve (url) { | ||
const div = document.createElement('div'); | ||
div.innerHTML = '<a></a>'; | ||
div.firstChild.href = url; // Ensures that the href is properly escaped | ||
div.innerHTML = div.innerHTML; // Run the current innerHTML back through the parser | ||
return div.firstChild.href; | ||
}; | ||
} | ||
return require('path').resolve; | ||
})(); | ||
function normalizeDataPackageUrl (datapackage) { | ||
if (typeof datapackage === 'string') { | ||
datapackage = {url: datapackage}; | ||
} | ||
if (!datapackage.dataPackageJsonUrl) { | ||
let url = datapackage.path || datapackage.url; | ||
url = url.match(absURLRegEx) ? url : resolvePath(url); | ||
datapackage = Object.assign(datapackage, identifier.parse(url)); | ||
} | ||
return datapackage; | ||
} | ||
module.exports = readPackage; | ||
class Loader { | ||
constructor (opts) { | ||
this.fetch = opts.fetch; | ||
} | ||
datapackage (datapackage) { | ||
debug('Loading datapackage', datapackage); | ||
datapackage = normalizeDataPackageUrl(datapackage); | ||
const dataPackageJsonUrl = datapackage.dataPackageJsonUrl; | ||
return this | ||
.fetch(dataPackageJsonUrl) | ||
.catch(err => { | ||
if (err.code === 'ENOENT') { | ||
throw new Error(`No DataPackage at path '${dataPackageJsonUrl}'`); | ||
} | ||
/* istanbul ignore next */ | ||
throw err; | ||
}) | ||
.then(parse) | ||
.then(res => Object.assign(datapackage, res, datapackage)); | ||
} | ||
resources (datapackage) { | ||
return Promise | ||
.all(datapackage.resources.map(r => this.resource(r))) | ||
.then(() => datapackage); | ||
} | ||
resource (resource) { | ||
debug('Loading resource', resource); | ||
if (!resource.url) { | ||
return Promise.resolve(resource); | ||
} | ||
return this | ||
.fetch(resource.url) | ||
.catch(err => { | ||
if (err.code === 'ENOENT') { | ||
throw new Error(`No DataPackage resource at path '${resource.url}'`); | ||
} | ||
/* istanbul ignore next */ | ||
throw err; | ||
}) | ||
.then(res => { | ||
resource.content = res; | ||
return resource; | ||
}); | ||
} | ||
} | ||
module.exports = Loader; |
@@ -6,16 +6,13 @@ 'use strict'; | ||
const MimeLookup = require('mime-lookup'); | ||
const mimeTypes = require('./mime.json'); | ||
class Normalizer { | ||
constructor (opts) { | ||
opts = opts || {}; | ||
this.mime = new MimeLookup(opts.mimeDb || mimeTypes); | ||
this.mime = opts.mimeLookup; | ||
} | ||
datapackage (datapackage) { | ||
const path = datapackage.path; | ||
const path = datapackage.path || datapackage.url; | ||
const uri = urijs(path); | ||
const dir = uri.normalizePathname().directory(); | ||
const base = `${datapackage.base || dir}/`; | ||
const base = path; | ||
@@ -37,6 +34,2 @@ const normalized = deepExtend({ | ||
if (normalized.resources) { | ||
normalized.resources = normalized.resources.map(resource => this.resource(normalized, resource)); | ||
} | ||
if (normalized.schemas) { | ||
@@ -51,7 +44,13 @@ const schemas = normalized.schemas; | ||
this.index(normalized); | ||
return normalized; | ||
} | ||
resources (datapackage) { | ||
if (datapackage.resources) { | ||
datapackage.resources = datapackage.resources.map(resource => this.resource(datapackage, resource)); | ||
} | ||
Normalizer.index(datapackage); | ||
return datapackage; | ||
} | ||
resource (datapackage, resource) { | ||
@@ -68,5 +67,9 @@ if (typeof resource === 'string') { | ||
resource.url = resource.url || uri.absoluteTo(datapackage.base).href(); | ||
resource.url = resource.url || uri.absoluteTo(datapackage.url).href(); | ||
} | ||
if (!resource.format && !resource.content && resource.data) { | ||
resource.format = 'json'; | ||
} | ||
if (resource.format) { | ||
@@ -82,14 +85,14 @@ resource.mediatype = resource.mediatype || this.mime.lookup(resource.format); | ||
} | ||
index (datapackage) { | ||
datapackage.$resourcesByName = {}; | ||
datapackage.resources.forEach(r => { | ||
if (r.name) { | ||
datapackage.$resourcesByName[r.name] = r; | ||
} | ||
}); | ||
return datapackage; | ||
} | ||
} | ||
Normalizer.index = function index (datapackage) { | ||
datapackage.$resourcesByName = {}; | ||
datapackage.resources.forEach(r => { | ||
if (r.name) { | ||
datapackage.$resourcesByName[r.name] = r; | ||
} | ||
}); | ||
return datapackage; | ||
}; | ||
module.exports = Normalizer; |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
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
34936
25
898
71
10
6
2
5
+ Addedbabyparse@^0.4.6
+ Addedcrlf-helper@^0.1.0
+ Addeddebug@^2.2.0
+ Addedisomorphic-fetch@^2.2.1
+ Addedjs-yaml@^3.6.1
+ Addedjson5@^0.5.0
+ Addedargparse@1.0.10(transitive)
+ Addedbabyparse@0.4.6(transitive)
+ Addedcrlf-helper@0.1.0(transitive)
+ Addeddatapackage-identifier@0.4.2(transitive)
+ Addeddebug@2.6.9(transitive)
+ Addedencoding@0.1.13(transitive)
+ Addedesprima@4.0.1(transitive)
+ Addediconv-lite@0.6.3(transitive)
+ Addedis-stream@1.1.0(transitive)
+ Addedisomorphic-fetch@2.2.1(transitive)
+ Addedjs-yaml@3.14.1(transitive)
+ Addedjson5@0.5.1(transitive)
+ Addedms@2.0.0(transitive)
+ Addednode-fetch@1.7.3(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedsprintf-js@1.0.3(transitive)
+ Addedwhatwg-fetch@3.6.20(transitive)