Skip to content

Commit

Permalink
Merge pull request #165 from zazuko/env
Browse files Browse the repository at this point in the history
build(deps): use @zazuko/env
  • Loading branch information
tpluscode authored Sep 13, 2023
2 parents 189c926 + 4270513 commit c015b3d
Show file tree
Hide file tree
Showing 14 changed files with 227 additions and 193 deletions.
5 changes: 5 additions & 0 deletions .changeset/clever-pianos-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@vocabulary/builder": patch
---

Updated example in docs
5 changes: 5 additions & 0 deletions .changeset/modern-melons-melt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@zazuko/vocabularies": patch
---

Replace `rdf-ext` with `@zazuko/env`
5 changes: 5 additions & 0 deletions .changeset/rare-cats-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@vocabulary/builder": patch
---

Replace `rdf-ext` with `@zazuko/env`
2 changes: 1 addition & 1 deletion packages/builder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ the vocabulary without the need for parsers.

```js
import foobarQuads from '@my-vocab/foobar'
import $rdf from 'rdf-ext'
import $rdf from '@zazuko/env'

const dataset = $rdf.dataset(foobarQuads({ factory: $rdf }))
```
25 changes: 13 additions & 12 deletions packages/builder/datasets.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/* eslint-disable no-console */
import fs from 'fs'
import { resolve } from 'path'
import rdf from 'rdf-ext'
import rdf from '@zazuko/env'
import formats from '@rdfjs/formats-common'
import rdfFetch, { FactoryInit, DatasetResponse } from '@rdfjs/fetch-lite'
import { RdfXmlParser } from 'rdfxml-streaming-parser'
import { DatasetCore, NamedNode } from '@rdfjs/types'
import { DatasetExt } from 'rdf-ext/lib/Dataset.js'
import { expand } from '@zazuko/prefixes'
import addAll from 'rdf-dataset-ext/addAll.js'
import toCanonical from 'rdf-dataset-ext/toCanonical.js'
import { Override } from './lib/overrides.js'
import rawFetch from './lib/fetch.js'

Expand All @@ -22,7 +23,7 @@ let lastW3FetchAt = 0

// other workarounds to avoid w3.org banning us for making 30 requests over 10 minutes every two months...
const randomInt = (a: number, b: number) => Math.floor(a + Math.random() * (b + 1 - a))
const fetchOptions = ({ headers, ...rest }: FactoryInit<DatasetExt>): FactoryInit<DatasetExt> => ({
const fetchOptions = ({ headers, ...rest }: FactoryInit<DatasetCore>): FactoryInit<DatasetCore> => ({
credentials: 'omit',
headers: {
'accept-language': 'en-US,en',
Expand Down Expand Up @@ -51,19 +52,19 @@ async function wait(milliseconds: number) {
return new Promise((resolve) => setTimeout(resolve, milliseconds))
}

function fetchWrapper(url: string, options: FactoryInit<DatasetExt>, timeout: number) {
function fetchWrapper(url: string, options: FactoryInit<DatasetCore>, timeout: number) {
// source: https://stackoverflow.com/a/46946588/4359369
return new Promise((resolve, reject) => {
rdfFetch <DatasetExt>(url, { ...fetchOptions(options), fetch: rawFetch }).then(resolve, reject)
rdfFetch <DatasetCore>(url, { ...fetchOptions(options), fetch: rawFetch }).then(resolve, reject)

if (timeout) {
const e = new Error('Connection timed out')
setTimeout(reject, timeout, e)
}
}) as Promise<DatasetResponse<DatasetExt>>
}) as Promise<DatasetResponse<DatasetCore>>
}

async function datasets(vocab: Vocab, indexOnly = false): Promise<DatasetExt> {
async function datasets(vocab: Vocab, indexOnly = false): Promise<DatasetCore> {
const prefix = vocab.prefix
const mappings = vocab.files || [vocab]

Expand Down Expand Up @@ -99,7 +100,7 @@ async function datasets(vocab: Vocab, indexOnly = false): Promise<DatasetExt> {
res.headers.set('content-type', mapping.mediaType)
}
const fetchedDataset = await res.dataset()
dataset = dataset.merge(fetchedDataset)
dataset = addAll(dataset, fetchedDataset)
} catch (err: any) {
console.warn(`${prefix}: failed fetching/processing: ${err.message}`)
}
Expand Down Expand Up @@ -151,12 +152,12 @@ function getDescription(dataset: DatasetCore): string {
return ''
}

function generateIndex(subject: NamedNode, packageName: string, mappings: Vocab, dataset: DatasetExt) {
function generateIndex(subject: NamedNode, packageName: string, mappings: Vocab, dataset: DatasetCore) {
const vocabUri = rdf.namedNode(mappings.namespace)
let filteredDataset = dataset.match(vocabUri)
if (vocabUri.value.endsWith('/') || vocabUri.value.endsWith('#')) {
const vocabUri2 = rdf.namedNode(vocabUri.value.substr(0, vocabUri.value.length - 1))
filteredDataset = filteredDataset.merge(dataset.match(vocabUri2))
filteredDataset = addAll(filteredDataset, dataset.match(vocabUri2))
}

const prefixDataset = rdf.dataset()
Expand Down Expand Up @@ -204,10 +205,10 @@ export async function buildDatasets(path: string, packageName: string, indexBase
let dataset = await datasets(vocab)
if (dataset && dataset.size) {
const graph = rdf.namedNode(vocab.namespace)
dataset = dataset.map(({ subject, predicate, object }) => rdf.quad(subject, predicate, object, graph))
dataset = rdf.dataset([...dataset].map(({ subject, predicate, object }) => rdf.quad(subject, predicate, object, graph)))

const file = resolve(path, `${vocab.prefix}.nq`)
fs.writeFileSync(file, dataset.toCanonical())
fs.writeFileSync(file, toCanonical(dataset))
console.log(`${vocab.prefix}: wrote ${dataset.size} quads to ${file}`)

const indexSubject = rdf.namedNode(`${indexBase}${vocab.prefix}:`)
Expand Down
3 changes: 2 additions & 1 deletion packages/builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
"@rdfjs/formats-common": "^3.1.0",
"@rdfjs/serializer-rdfjs": "^0.1.1",
"@rdfjs/types": "^1.1.0",
"@zazuko/env": "^1.3.0",
"@zazuko/prefixes": "^2.0.0",
"commander": "^10.0.0",
"get-stream": "^6.0.1",
"file-fetch": "^1.7.0",
"node-fetch": "^3.3.0",
"proto-fetch": "^1.0.0",
"rdfxml-streaming-parser": "^2.2.1",
"rdf-ext": "^2.2.0",
"rdf-dataset-ext": "^1.0.1",
"rdf-utils-fs": "^2.3.0",
"ts-node": "^10.9.1"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/vocabularies/expandWithCheck.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { NamedNode } from '@rdfjs/types'
import rdf from 'rdf-ext'
import rdf from '@zazuko/env'
import { expand, getParts } from './expand.js'
import { vocabularies, Datasets } from './vocabularies.js'

Expand Down
5 changes: 3 additions & 2 deletions packages/vocabularies/meta.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { createReadStream } from 'fs'
import module from 'module'
import Parser from '@rdfjs/parser-n3'
import $rdf from 'rdf-ext'
import rdf from '@zazuko/env'
import fromStream from 'rdf-dataset-ext/fromStream.js'

const { resolve } = module.createRequire(import.meta.url)
const metaQuads = resolve('./meta.nt')

export default function () {
const parser = new Parser()
return $rdf.dataset().import(parser.import(createReadStream(metaQuads)))
return fromStream(rdf.dataset(), parser.import(createReadStream(metaQuads)))
}
5 changes: 3 additions & 2 deletions packages/vocabularies/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
},
"dependencies": {
"@rdfjs/parser-n3": "^2.0.1",
"@types/rdf-ext": "^2.0.1",
"@types/rdfjs__environment": "^0.1",
"@vocabulary/acl": "^1.0.0",
"@vocabulary/as": "^1.0.0",
Expand Down Expand Up @@ -124,9 +123,10 @@
"@vocabulary/xhv": "^1.0.0",
"@vocabulary/xkos": "^1.0.0",
"@vocabulary/xsd": "^1.0.0",
"@zazuko/env": "^1.3.0",
"@zazuko/prefixes": "^2.0.0",
"commander": "^10.0.0",
"rdf-ext": "^2.2.0",
"rdf-dataset-ext": "^1",
"readable-stream": "^4.3.0"
},
"files": [
Expand All @@ -142,6 +142,7 @@
"devDependencies": {
"@types/chai": "^4.3.4",
"@types/mocha": "^10",
"@types/rdf-dataset-ext": "^1",
"@types/rdfjs__parser-n3": "^1.1.5",
"chai": "^4.3.7"
},
Expand Down
5 changes: 3 additions & 2 deletions packages/vocabularies/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import module from 'module'
import { resolve as resolvePath } from 'path'
import { expect } from 'chai'
import { describe, it, before, after } from 'mocha'
import rdf from 'rdf-ext'
import rdf from '@zazuko/env'
import prefixes, { shrink } from '@zazuko/prefixes'
import addAll from 'rdf-dataset-ext/addAll.js'
import { expand } from '../index.js'
import { vocabularies } from '../vocabularies.js'

Expand Down Expand Up @@ -41,7 +42,7 @@ describe('@zazuko/vocabularies', function () {
const result = await vocabularies({ only: ['skos', 'dcterms'] })
let mergedDataset = rdf.dataset()
Object.values(result).forEach((dataset) => {
mergedDataset = mergedDataset.merge(dataset)
mergedDataset = addAll(mergedDataset, dataset)
})
const stream = await vocabularies({ only: ['skos', 'dcterms'], stream: true })
let i = 0
Expand Down
23 changes: 12 additions & 11 deletions packages/vocabularies/vocabularies.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
/* eslint-disable no-console */
import { Stream, DataFactory } from 'rdf-js'
import { Stream, DataFactory, DatasetCore, DatasetCoreFactory } from 'rdf-js'
import { Readable } from 'stream'
import rdf from 'rdf-ext'
import rdf from '@zazuko/env'
import type { Environment } from '@rdfjs/environment/Environment.js'
import prefixes from '@zazuko/prefixes'
import type { DatasetExt } from 'rdf-ext/lib/Dataset.js'
import ParserN3 from '@rdfjs/parser-n3'
import { DatasetFactory } from 'rdf-ext/DatasetFactory.js'
import fromStream from 'rdf-dataset-ext/fromStream.js'
import addAll from 'rdf-dataset-ext/addAll.js'
import toStream from 'rdf-dataset-ext/toStream.js'
import { loadDatasetStream } from './lib/loadDataset.js'

export type Datasets = Record<keyof typeof prefixes, DatasetExt>
export type Datasets = Record<keyof typeof prefixes, DatasetCore>

interface VocabulariesOptions {
only?: (keyof typeof prefixes)[] | null
factory?: Environment<DatasetFactory | DataFactory>
factory?: Environment<DatasetCoreFactory | DataFactory>
}

interface VocabulariesDatasetOptions extends VocabulariesOptions {
Expand Down Expand Up @@ -46,13 +47,13 @@ export async function vocabularies({ only = null, factory = rdf, stream = false
const datasets = await Promise.all(promises)

if (stream !== false) {
let combinedDataset: any = factory.dataset()
let combinedDataset = factory.dataset()
datasets.forEach((dataset) => {
if (dataset && dataset.size) {
combinedDataset = combinedDataset.merge(dataset)
combinedDataset = addAll(combinedDataset, dataset)
}
})
return combinedDataset.toStream()
return toStream(combinedDataset) as any
}

const result: Datasets = {}
Expand All @@ -66,15 +67,15 @@ export async function vocabularies({ only = null, factory = rdf, stream = false

interface LoadFileOptions {
customSelection?: boolean
factory: Environment<DatasetFactory>
factory: Environment<DatasetCoreFactory>
}

export async function loadFile(prefix: keyof typeof prefixes, { customSelection, factory }: LoadFileOptions) {
try {
const parserN3 = new ParserN3()
const readStream = await loadDatasetStream(prefix)
const quadStream = parserN3.import(readStream)
return factory.dataset().import(quadStream)
return fromStream(factory.dataset(), quadStream)
} catch {
if (customSelection) {
console.warn(`unavailable prefix '${prefix}'`)
Expand Down
47 changes: 0 additions & 47 deletions patches/saxes+6.0.0.patch

This file was deleted.

2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"strict": true,
"strictNullChecks": true,
"lib": [
"es2017",
"ES2022",
"webworker"
],
"declaration": true,
Expand Down
Loading

0 comments on commit c015b3d

Please sign in to comment.