Skip to content

Commit

Permalink
fix: loading vocabs from node ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
tpluscode committed Nov 20, 2022
1 parent 7e50d88 commit 6abf302
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/purple-seals-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hydrofoil/vocabularies": patch
---

`vocabularies` export would not work in node using ES modules
18 changes: 18 additions & 0 deletions loadDataset/index.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
import stringToStream from 'string-to-stream'

const AsyncFunction = (async function () {}).constructor;

export async function loadDatasetStream (prefix) {
if (is_node()) {
const path = new URL(`../ontologies/${prefix}.nq`, import.meta.url).toString()
const nodeLoad = new AsyncFunction('path', `
const { createReadStream } = await import('fs')
const url = await import('url')
return createReadStream(url.fileURLToPath(path))
`)

return nodeLoad(path)
}

return stringToStream((await import(`../ontologies/${prefix}.nq`)).default)
}

function is_node() {
return typeof global !== 'undefined' && global.global === global;
}

0 comments on commit 6abf302

Please sign in to comment.