Skip to content

Commit

Permalink
Merge c86eae9 into 5fa0686
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimvh committed Oct 21, 2020
2 parents 5fa0686 + c86eae9 commit 4be48ed
Show file tree
Hide file tree
Showing 9 changed files with 739 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.js
Expand Up @@ -5,6 +5,9 @@ module.exports = {
tsconfigRootDir: __dirname, // this is the reason this is a .js file
project: ['./tsconfig.json'],
},
globals: {
NodeJS: 'readonly'
},
plugins: [
'eslint-plugin-tsdoc',
'eslint-plugin-import',
Expand Down
2 changes: 2 additions & 0 deletions index.ts
Expand Up @@ -84,6 +84,7 @@ export * from './src/server/HttpResponse';
export * from './src/storage/accessors/DataAccessor';
export * from './src/storage/accessors/FileDataAccessor';
export * from './src/storage/accessors/InMemoryDataAccessor';
export * from './src/storage/accessors/SparqlDataAccessor';

// Storage/Conversion
export * from './src/storage/conversion/ChainedConverter';
Expand All @@ -102,6 +103,7 @@ export * from './src/storage/Conditions';
export * from './src/storage/ContainerManager';
export * from './src/storage/DataAccessorBasedStore';
export * from './src/storage/ExtensionBasedMapper';
export * from './src/storage/FixedConvertingStore';
export * from './src/storage/Lock';
export * from './src/storage/LockingResourceStore';
export * from './src/storage/PassthroughStore';
Expand Down
125 changes: 113 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Expand Up @@ -71,6 +71,7 @@
"@types/n3": "^1.4.4",
"@types/node": "^14.10.2",
"@types/rdf-js": "^4.0.0",
"@types/sparqljs": "^3.1.0",
"@types/streamify-array": "^1.0.0",
"@types/uuid": "^8.3.0",
"@types/yargs": "^15.0.5",
Expand All @@ -79,12 +80,14 @@
"componentsjs": "^3.6.0",
"cors": "^2.8.5",
"express": "^4.17.1",
"fetch-sparql-endpoint": "^1.8.0",
"mime-types": "^2.1.27",
"n3": "^1.6.3",
"rdf-parse": "^1.5.0",
"rdf-serialize": "^1.0.0",
"rdf-terms": "^1.5.1",
"sparqlalgebrajs": "^2.3.1",
"sparqljs": "^3.1.2",
"streamify-array": "^1.0.1",
"uuid": "^8.3.0",
"winston": "^3.3.3",
Expand Down
45 changes: 45 additions & 0 deletions src/storage/FixedConvertingStore.ts
@@ -0,0 +1,45 @@
import type { Representation } from '../ldp/representation/Representation';
import type { RepresentationPreference } from '../ldp/representation/RepresentationPreference';
import type { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier';
import type { Conditions } from './Conditions';
import type { RepresentationConverter } from './conversion/RepresentationConverter';
import { PassthroughStore } from './PassthroughStore';
import type { ResourceStore } from './ResourceStore';

/**
* Store that converts incoming data when required.
* If the content-type of an incoming representation does not match one of the stored types it will be converted.
*/
export class FixedConvertingStore extends PassthroughStore {
private readonly types: string[];
private readonly converter: RepresentationConverter;

public constructor(source: ResourceStore, converter: RepresentationConverter, types: string[]) {
super(source);
this.converter = converter;
this.types = types;
}

public async addResource(container: ResourceIdentifier, representation: Representation,
conditions?: Conditions): Promise<ResourceIdentifier> {
// We can potentially run into problems here if we convert a turtle document where the base IRI is required,
// since we don't know the resource IRI yet at this point.
representation = await this.convertRepresentation(container, representation);
return this.source.addResource(container, representation, conditions);
}

public async setRepresentation(identifier: ResourceIdentifier, representation: Representation,
conditions?: Conditions): Promise<void> {
representation = await this.convertRepresentation(identifier, representation);
return this.source.setRepresentation(identifier, representation, conditions);
}

private async convertRepresentation(identifier: ResourceIdentifier, representation: Representation):
Promise<Representation> {
if (this.types.includes(representation.metadata.contentType!)) {
return representation;
}
const preferences = this.types.map((type): RepresentationPreference => ({ value: type, weight: 1 }));
return this.converter.handleSafe({ identifier, representation, preferences: { type: preferences }});
}
}

0 comments on commit 4be48ed

Please sign in to comment.