Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jsonld.canonize workers implementation #3419

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions src/service/data-service.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import jsonld from 'jsonld';
import toNQuadsWorker from '../workers/data-service-toNQuads-worker.js';
import {
SCHEMA_CONTEXT,
MEDIA_TYPES,
Expand All @@ -19,14 +20,10 @@ class DataService {
algorithm: ALGORITHM,
format: MEDIA_TYPES.N_QUADS,
};

if (inputFormat) {
options.inputFormat = inputFormat;
}

const canonized = await jsonld.canonize(content, options);

return canonized.split('\n').filter((x) => x !== '');
return toNQuadsWorker(content, options);
}

async compact(content) {
Expand Down
24 changes: 24 additions & 0 deletions src/workers/data-service-toNQuads-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Worker, isMainThread, parentPort, workerData } from 'node:worker_threads';

import jsonld from 'jsonld';
import { fileURLToPath } from 'url';

if (!isMainThread) {
const { content, options } = workerData;
const canonized = await jsonld.canonize(content, options);
parentPort.postMessage(canonized.split('\n').filter((x) => x !== ''));
}

export default function toNQuadsWorker(content, options) {
return new Promise((resolve, reject) => {
const worker = new Worker(fileURLToPath(import.meta.url), {
workerData: { content, options },
});
worker.on('message', resolve);
worker.on('error', reject);
worker.on('exit', (code) => {
if (code !== 0)
reject(new Error(`Worker stopped with exit code ${code}`));
});
});
};
Loading