Skip to content

Commit

Permalink
jsonld.canonize workers implementation (#3419)
Browse files Browse the repository at this point in the history
* jsonld.canonize workers implementation

* refactor worker code structure
  • Loading branch information
aleksaelezovic authored Nov 20, 2024
1 parent cf0a035 commit f36a7a5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
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}`));
});
});
};

0 comments on commit f36a7a5

Please sign in to comment.