Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Create store that converts incoming data when required
- Loading branch information
Showing
5 changed files
with
115 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import type { Representation } from '../../../src/ldp/representation/Representation'; | ||
import { RepresentationMetadata } from '../../../src/ldp/representation/RepresentationMetadata'; | ||
import type { RepresentationConverter } from '../../../src/storage/conversion/RepresentationConverter'; | ||
import { FixedConvertingStore } from '../../../src/storage/FixedConvertingStore'; | ||
import type { ResourceStore } from '../../../src/storage/ResourceStore'; | ||
import { StaticAsyncHandler } from '../../util/StaticAsyncHandler'; | ||
|
||
describe('A FixedConvertingStore', (): void => { | ||
let store: FixedConvertingStore; | ||
let source: ResourceStore; | ||
let converter: RepresentationConverter; | ||
const types = [ 'text/turtle' ]; | ||
let metadata: RepresentationMetadata; | ||
let representation: Representation; | ||
|
||
beforeEach(async(): Promise<void> => { | ||
source = { | ||
addResource: jest.fn(), | ||
setRepresentation: jest.fn(), | ||
} as any; | ||
|
||
converter = new StaticAsyncHandler(true, 'converted') as any; | ||
|
||
store = new FixedConvertingStore(source, converter, types); | ||
|
||
metadata = new RepresentationMetadata(); | ||
representation = { binary: true, data: 'data', metadata } as any; | ||
}); | ||
|
||
it('keeps the representation if the content-type is supported.', async(): Promise<void> => { | ||
metadata.contentType = types[0]; | ||
const id = { path: 'identifier' }; | ||
|
||
await expect(store.addResource(id, representation, 'conditions' as any)).resolves.toBeUndefined(); | ||
expect(source.addResource).toHaveBeenLastCalledWith(id, representation, 'conditions'); | ||
|
||
await expect(store.setRepresentation(id, representation, 'conditions' as any)).resolves.toBeUndefined(); | ||
expect(source.setRepresentation).toHaveBeenLastCalledWith(id, representation, 'conditions'); | ||
}); | ||
|
||
it('converts the data if the content-type is not supported.', async(): Promise<void> => { | ||
metadata.contentType = 'text/plain'; | ||
const id = { path: 'identifier' }; | ||
|
||
await expect(store.addResource(id, representation, 'conditions' as any)).resolves.toBeUndefined(); | ||
expect(source.addResource).toHaveBeenLastCalledWith(id, 'converted', 'conditions'); | ||
|
||
await expect(store.setRepresentation(id, representation, 'conditions' as any)).resolves.toBeUndefined(); | ||
expect(source.setRepresentation).toHaveBeenLastCalledWith(id, 'converted', 'conditions'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters