-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #155 from zazuko/migrate-csvw
Migrate csvw
- Loading branch information
Showing
18 changed files
with
438 additions
and
11 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,35 @@ | ||
# bardnard59-csvw | ||
|
||
Simplifies handling CSVW mapping documents in barnard59 pipelines | ||
|
||
## Install | ||
|
||
``` | ||
npm install barnard59-csvw --save | ||
``` | ||
|
||
## Exported steps | ||
|
||
### `fetch` | ||
|
||
A step to automate loading CSVW mapping documents and the source CSV, by following the `csvw:url` property. The URL can be local filesystem path or remote URL. | ||
|
||
| Argument | Type | Description | | ||
| -- | -- | -- | | ||
| `csvw` | `string` | Local path or URL of the mapping to load | | ||
|
||
### Example: Loading CSVW from filesystem | ||
|
||
```turtle | ||
@prefix p: <https://pipeline.described.at/> . | ||
@prefix code: <https://code.described.at/> . | ||
<#CsvwStep> a p:Step; | ||
code:implementedBy [ a code:EcmaScriptModule; | ||
code:link <node:barnard59-csvw/fetch.js#default> | ||
]; | ||
code:arguments [ | ||
code:name "csvw"; | ||
code:value "file:/test/mappings/remote.csvw.json" | ||
]. | ||
``` |
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,25 @@ | ||
import rdf from '@zazuko/env' | ||
import toReadable from 'duplex-to/readable.js' | ||
import { PassThrough } from 'readable-stream' | ||
import fetchData from './lib/fetchData.js' | ||
import fetchMetadata from './lib/fetchMetadata.js' | ||
|
||
function fetch({ csvw }) { | ||
const output = new PassThrough() | ||
|
||
Promise.resolve().then(async () => { | ||
try { | ||
const metadata = await fetchMetadata(csvw) | ||
const url = metadata.any().has(rdf.ns.csvw.url).out(rdf.ns.csvw.url) | ||
const dataStream = await fetchData(url.value) | ||
|
||
dataStream.pipe(output) | ||
} catch (err) { | ||
output.destroy(err) | ||
} | ||
}) | ||
|
||
return toReadable(output) | ||
} | ||
|
||
export default fetch |
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,5 @@ | ||
import fetch from './fetch.js' | ||
|
||
export { | ||
fetch, | ||
} |
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,7 @@ | ||
async function checkResponse(res) { | ||
if (!res.ok) { | ||
throw new Error(`${res.statusText}(${res.status}): ${await res.text()}`) | ||
} | ||
} | ||
|
||
export default checkResponse |
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,11 @@ | ||
import fileFetch from 'file-fetch' | ||
import httpFetch from 'node-fetch' | ||
import protoFetch from 'proto-fetch' | ||
|
||
const commonFetch = protoFetch({ | ||
file: fileFetch, | ||
http: httpFetch, | ||
https: httpFetch, | ||
}) | ||
|
||
export default commonFetch |
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,12 @@ | ||
import checkResponse from './checkResponse.js' | ||
import commonFetch from './commonFetch.js' | ||
|
||
async function fetchData(url) { | ||
const res = await commonFetch(url) | ||
|
||
await checkResponse(res) | ||
|
||
return res.body | ||
} | ||
|
||
export default fetchData |
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,22 @@ | ||
import rdfFetch from '@rdfjs/fetch' | ||
import rdf from '@zazuko/env' | ||
import checkResponse from './checkResponse.js' | ||
import commonFetch from './commonFetch.js' | ||
|
||
async function fetchMetadata(url) { | ||
const res = await rdfFetch(url.toString(), { | ||
contentTypeLookup: extension => extension === '.json' ? 'application/ld+json' : undefined, | ||
factory: rdf, | ||
fetch: commonFetch, | ||
}) | ||
|
||
await checkResponse(res) | ||
|
||
if (!res.dataset) { | ||
throw new Error('response is empty') | ||
} | ||
|
||
return rdf.clownface({ dataset: await res.dataset() }) | ||
} | ||
|
||
export default fetchMetadata |
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,11 @@ | ||
@base <http://barnard59.zazuko.com/operations/csvw/> . | ||
@prefix code: <https://code.described.at/> . | ||
@prefix p: <https://pipeline.described.at/> . | ||
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . | ||
|
||
<fetch> a p:Operation, p:Readable; | ||
rdfs:label "Fetch CSVW"; | ||
rdfs:comment "Loads a CSVW file from the local filesystem or the web depending on the given argument"; | ||
code:implementedBy [ a code:EcmaScriptModule; | ||
code:link <node:barnard59-csvw/fetch.js#default> | ||
]. |
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,39 @@ | ||
{ | ||
"name": "barnard59-csvw", | ||
"version": "1.0.0", | ||
"description": "Simplifies handling CSVW mapping documents in barnard59 pipelines", | ||
"type": "module", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/zazuko/barnard59.git", | ||
"directory": "packages/csvw" | ||
}, | ||
"keywords": [], | ||
"author": "Zazuko GmbH", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/zazuko/barnard59/issues" | ||
}, | ||
"homepage": "https://github.com/zazuko/barnard59", | ||
"dependencies": { | ||
"@rdfjs/fetch": "^2.1.0", | ||
"@zazuko/env": "^1.0.1", | ||
"duplex-to": "^1.0.1", | ||
"file-fetch": "^1.7.0", | ||
"node-fetch": "^3.0.0", | ||
"proto-fetch": "^1.0.0", | ||
"readable-stream": "^3.6.0" | ||
}, | ||
"devDependencies": { | ||
"express-as-promise": "^1.2.0", | ||
"get-stream": "^7.0.1", | ||
"is-stream": "^3.0.0" | ||
}, | ||
"engines": { | ||
"node": ">= 14.0.0" | ||
} | ||
} |
Oops, something went wrong.