From bb137febb5917bfc6f05454867f903ddb7c14657 Mon Sep 17 00:00:00 2001 From: Giacomo Citi Date: Thu, 26 Sep 2024 17:29:51 +0200 Subject: [PATCH] use async generator --- cli/lib/replace.ts | 26 +++++++++++++------------- cli/package.json | 2 +- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/cli/lib/replace.ts b/cli/lib/replace.ts index cc1686479..845754790 100644 --- a/cli/lib/replace.ts +++ b/cli/lib/replace.ts @@ -1,19 +1,19 @@ -import through2 from 'through2' import type { Quad } from '@rdfjs/types' -import $rdf from 'rdf-ext' +import { Context } from 'barnard59-core' -function replace(version: string) { +export default function replace(this: Context, version: string) { const prefix = 'https://cube.link/latest/' const replacement = `https://cube.link/${version}/` - return through2.obj(function (quad: Quad, _, done) { - if (quad.predicate.value === 'https://code.described.at/imports' && quad.object.value.startsWith(prefix)) { - const replaced = quad.object.value.replace(prefix, replacement) - const triple = $rdf.triple(quad.subject, quad.predicate, $rdf.namedNode(replaced)) - done(null, triple) - } else { - done(null, quad) + const { env } = this + + return async function * (stream: AsyncIterable) { + for await (const quad of stream) { + if (quad.predicate.equals(env.ns.code.imports) && quad.object.value.startsWith(prefix)) { + const replaced = quad.object.value.replace(prefix, replacement) + yield env.quad(quad.subject, quad.predicate, env.namedNode(replaced)) + } else { + yield quad + } } - }) + } } - -export default replace diff --git a/cli/package.json b/cli/package.json index 547f9e78d..0b6acfea7 100644 --- a/cli/package.json +++ b/cli/package.json @@ -43,6 +43,7 @@ "alcaeus": "^2", "aws-sdk": "^2.559.0", "barnard59": "^5.0.4", + "barnard59-core": "^6.0.0", "barnard59-base": "^2.4.2", "barnard59-cube": "^1.4.8", "barnard59-formats": "^4.0.0", @@ -112,7 +113,6 @@ "@types/sinon-chai": "^3.2.4", "@types/sparql-http-client": "^2", "@types/through2": "^2.0.36", - "barnard59-core": "^6.0.0", "chai": "^4.3.4", "dotenv": "^16.0.0", "dotenv-cli": "^5.0.0",