Skip to content

Commit

Permalink
Merge pull request #2848 from OriginTrail/v6/fix/blazegraph-handling-…
Browse files Browse the repository at this point in the history
…utf-8

Handle Blazegraph corruption of special characters
  • Loading branch information
NZT48 authored Dec 25, 2023
2 parents 7ce252e + c83c41b commit 49d9a74
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import OtTripleStore from '../ot-triple-store.js';
class OtBlazegraph extends OtTripleStore {
async initialize(config, logger) {
await super.initialize(config, logger);
// this regex will match \Uxxxxxxxx but will exclude cases where there is a double slash before U (\\U)
this.unicodeRegex = /(?<!\\)\\U([a-fA-F0-9]{8})/g;

await Promise.all(
Object.keys(this.repositories).map(async (repository) => {
Expand Down Expand Up @@ -47,6 +49,40 @@ class OtBlazegraph extends OtTripleStore {
}
}

hasUnicodeCodePoints(input) {
return this.unicodeRegex.test(input);
}

decodeUnicodeCodePoints(input) {
const decodedString = input.replace(this.unicodeRegex, (match, hex) => {
const codePoint = parseInt(hex, 16);
return String.fromCodePoint(codePoint);
});

return decodedString;
}

async _executeQuery(repository, query, mediaType) {
const result = await this.queryEngine.query(
query,
this.repositories[repository].queryContext,
);
const { data } = await this.queryEngine.resultToString(result, mediaType);

let response = '';

for await (const chunk of data) {
response += chunk;
}

// Handle Blazegraph special characters corruption
if (this.hasUnicodeCodePoints(response)) {
response = this.decodeUnicodeCodePoints(response);
}

return response;
}

async healthCheck(repository) {
try {
const response = await axios.get(
Expand Down
13 changes: 10 additions & 3 deletions src/service/triple-store-service.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { formatAssertion } from 'assertion-tools';

import { SCHEMA_CONTEXT, TRIPLE_STORE_REPOSITORIES } from '../constants/constants.js';
import { SCHEMA_CONTEXT, TRIPLE_STORE_REPOSITORIES, MEDIA_TYPES } from '../constants/constants.js';

class TripleStoreService {
constructor(ctx) {
Expand Down Expand Up @@ -87,7 +87,14 @@ class TripleStoreService {
tokenId,
keyword,
) {
const assertion = await this.getAssertion(fromRepository, assertionId);
let assertion;
// Try-catch to prevent infinite processing loop when unexpected error is thrown while getting KA
try {
assertion = await this.getAssertion(fromRepository, assertionId);
} catch (e) {
this.logger.error(`Error while getting assertion for moving asset: ${e.message}`);
return;
}

// copy metadata and assertion
await this.localStoreAsset(
Expand Down Expand Up @@ -246,7 +253,7 @@ class TripleStoreService {
repository,
assertionId,
);
nquads = await this.dataService.toNQuads(nquads, 'application/n-quads');
nquads = await this.dataService.toNQuads(nquads, MEDIA_TYPES.N_QUADS);

this.logger.debug(
`Assertion: ${assertionId} ${
Expand Down

0 comments on commit 49d9a74

Please sign in to comment.